Saturday, 18 September 2010

Growl notifications using irssi and screen


My IRC client of choice is irssi, always running in a screen session on a Linux server. Keeping the client running even when I am logged off is really useful if you want to know happens when you are away. The disadvantage is that there is no obvious Growl integration like in Linkinus and Colloquy. I want those notifications when someone mentions my nick to get my attention.


After considering using remote notifications and an SSH tunnel, I ended up with a solution simply tailing irssi's log files and using growlnotify, Growl's command-line tool.


If you are new to irssi and screen, you probably want to read the startup HOWTO and A Guide to Efficiently Using Irssi and Screen before reading on. I also assume you already know how to use the command line and some very basic unix knowledge.


1) Turn on channel logging in irssi. Type the following commands:


/set autolog on
/save


The first tells irssi to log everything that is said and done in channel and query windows. By default these log files are put in the directory $HOME/irclogs. The second command saves your settings to disk in case you have to restart irssi at some point.


2) Install growlnotify. Fetch the image from http://growl.info/, open it in Finder, open the Extras folder, open the growlnotify folder and run the growlnotify installer. Just follow the usual steps in the installer and you and up having growlnotify installed in /usr/local/bin. If you want to verify that it is installed correctly, type the following in a terminal window:


growlnotify -m "test message"


3) Put this script in $HOME/bin/ircnotify (create the bin directory first or put it somewhere else):


#!/bin/bash


regex_file_changed='^==> .*/(.*).log <==$'
regex_empty='^$'
regex_ignore='^--- '
regex_lowpri='^[0-9]{2}:[0-9]{2} -!-'
regex_highpri='mynick'
regex_own='< *mynick *>|\* mynick'
regex_channel_query='^[a-zA-Z]+$'


ssh myusername@login.example.com "tail -n0 -f irclogs/*/*.log" | while read LINE ; do 
    SKIP=false
    if [[ "$LINE" =~ $regex_file_changed ]] ; then
        # file changed
        SOURCE=${BASH_REMATCH[1]}
        #echo "new source: $SOURCE"
    elif [[ "$LINE" =~ $regex_empty || "$LINE" =~ $regex_ignore ]] ; then
        # empty line - skipping
        SKIP=true
    elif [[ "$LINE" =~ $regex_own ]] ; then
        # my own nick
        growlnotify -n irssi -m "$SOURCE: $LINE"
        echo "$SOURCE: $LINE"
    elif [[ "$LINE" =~ $regex_lowpri ]] ; then
        growlnotify -n irssi -p -1 -m "$SOURCE: $LINE" 
        #echo "$SOURCE: $LINE"
    elif [[ "$LINE" =~ $regex_highpri || "$SOURCE" =~ $regex_channel_query ]] ; then
        growlnotify -n irssi -s -d "$SOURCE" -p 1 -m "$SOURCE: $LINE"
        printf "\033[1m$SOURCE: $LINE\033[0m\n"
    else 
        growlnotify -n irssi -m "$SOURCE: $LINE" 
        echo "$SOURCE: $LINE"
    fi
done

You have to change "mynick" to your own nick, "myusername" to your user name on the server where irssi is running, and "login.example.com" to the server. If you want less or more notifications, or different priority levels, just edit the script. It's pretty straight forward.

Then make the script executable:

chmod a+x $HOME/bin/ircnotify

4) Run the script. Either just run it from a terminal window:

$HOME/bin/ircnotify

or do as I do: run it in a loop to make it automatically restart when the computer has been asleep:

while true ; do $HOME/bin/ircnotify ; sleep 10 ; done

Finally, if you are a Linux user and want to get similar notifications: you should be able to use other notification tools instead of growl.