Posting to IRC Using ActiveMQ

March 06, 2009

Previously I wrote about querying your app using IRC and IRCCat. But that's only half the story. IRCCat can also let your applications talk to you. A source code commit, a user logging in, a server going down — these are all things worth knowing about, and they're surprisingly easy to pipe into IRC.

The IRCCat examples typically use netcat to send data over the network to the IRCCat process. I prefer a small Ruby script backed by a message bus. Since I already have ActiveMQ running, there's very little extra overhead:

#! /usr/bin/env ruby

STDOUT.sync = true

require 'rubygems'
require 'smqueue'
require 'yaml'
require 'socket'

puts "Starting..."

messages = SMQueue(:name => "/queue/irc.outgoing", :host => "mq.domain.com", :reliable => true, :adapter => "StompAdapter")

messages.get do |job|
  message = YAML.parse(job.body).transform
  puts "Posting #{message['text']} in #{message.headers['message-id']}."
  irc = TCPSocket.open('localhost', '12345')
  irc.send("#{message['text']}\r\n", 0)
  irc.close
  puts "Posted #{message.headers['message-id']}."
end

With this running on the same box as IRCCat, any other process can drop a message onto the /queue/irc.outgoing queue and it will appear in IRC. If IRCCat happens to be down, the messages sit safely in the queue until it comes back up.

I like this approach because the various processes that generate notifications don't need to know anything about where IRCCat is running. They just talk to the message queue, which SMQueue makes painless.

Questions or thoughts? Get in touch.