Irssi Notifications Using Ruby and Jabber

I have long wished I could use a text-based IRC client like BitchX or irssi with the same effectiveness as I have with X-Chat or Colloquy. What do I mean by effectiveness? Well, notifications of course. If someone says something to me in IRC, I need to be notified somehow, right away, of the pending message. Since I like to run my apps in screen, sometimes on a remote server, getting local notifications of these events has been beyond me.

Until now…

Special thanks to Daniel Hengeveld of neoglam for giving me the general idea of how this might work.

There are basically two parts to setting up notifications in irssi and Jabber:

  • Setting up a small Ruby script that can send Jabber IM’s to your Jabber client.
  • Getting irssi to log channel highlights and private messages to a special file.

Let’s start with the Ruby script. You’ll need the xmpp4r-simple gem. Install it like so:

1
gem install xmpp4r-simple -y
Next, put this script in your home directory (say ~/jabber.rb):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env ruby

require 'rubygems'
gem 'xmpp4r-simple'

require 'xmpp4r-simple'

# Change @jabber and @notify as appropriate
@jabber = { :username => 'my_jabber_handle@jabber.org',
            :password => 'my_pass' }

@notify = "jabber_handle_to_notify@jabber.org"

im = Jabber::Simple.new(@jabber[:username], @jabber[:password])

Signal.trap(:TERM) { exit; }

while true
  if s = $stdin.gets
    im.deliver(@notify, s)
  end

  sleep 0.2 # Save some resources
end

Now, I have two Jabber accounts, one to act as the “script” and one to listen to on my Adium. Be sure to change @jabber and @notify as appropriate. I’m not sure if a Jabber account can send an IM to itself (didn’t work for me), so if you can get it to work, you can make the script a bit more succinct.

Next, let’s set up irssi to log channel highlights (people talking to you) and private messages. Download this Perl script fnotify.pl and put it in ~/.irssi/scripts, then do:

1
2
cd ~/.irssi/scripts/autorun
ln -s ../fnotify.pl .

Start your irssi. If irssi is already running, type:

1
/run fnotify.pl

The fnotify.pl will run every time you start irssi, so you don’t need to worry about it again.

Now you will get a file called ~/.irssi/fnotify that is a running log of channel highlights and private messages. You can just “tail -f ~/.irssi/fnotify” and do what you wish with it. To hook it up with Jabber, just do (remember you need the ~/jabber.rb from above):

1
tail -f .irssi/fnotify | ruby jabber.rb &

This will run continuously, while consuming very little resources, and will dutifully send you a Jabber IM every time someone directs a chat at you or PM’s you. You’ll receive the channel name, sender nick, and message all in one swoop! Here’s an example of what my Adium showed me:

1
[3:19] <garrydolley2@jabber.org> #rubyonrails < garry> up_the_irons: how's it goin?!

Pretty neat!!

Exercise for the reader: set up an autorun irssi script to automatically launch the tail / ruby pipe above. :)

Things I still need to figure out: If I’m focused on my irssi window (in screen or otherwise), I don’t really need to get the Jabber IM. If I put my screen window to focus on IRC, perhaps there is some “eval” hook I can run to temporarily turn off the IM’s. If anyone figures it out before I do, please send me an email: gdolley [at-] ucla.edu.

Enjoy!

Comments