Jabber messages in your dwm status bar

One of the features of dwm is that whatever you pipe into stdin ends up in the status bar at the top of the screen. I thought this would be a great way of getting the last message from a jabber chat onto the screen.

There aren’t really a lot of examples of how this works. I think there is one in the dwm README file that shows how to get a clock into the status bar. You put this into your ~/.xinitrc:

while :
   sleep 1
done | /usr/local/bin/dwm

I thought I’d just redirect stdin for dwm from a fifo file and then I can pump data into the fifo whenever I like. ie. Put this in your .xinitrc

[ -f /tmp/myfifo ] && rm -f /tmp/myfifo
mkfifo /tmp/myfifo
/usr/local/bin/dwm </tmp/myfifo

I found that didn’t work as I expected and dwm tended to hang on startup (maybe it was waiting for something in the fifo??

I use mcabber as a jabber chat client, so here is what I did to get the status bar to update with the last chat message

1. In .xinitrc I have:

touch ~/.mcabber/log
tail -f ~/.mcabber/log | while read x
   cat "$x"
   # remove the message file since nothing else will
   rm -f "$x"
done | /usr/local/bin/dwm

2. In ~/.mcabberrc I turn on event logging and have an event script:

set events_command = ~/mcabber-eventcmd.sh
set event_log_files = 1
set event_log_dir = ~/.mcabber/event_files

3. In my ~/mcabber-eventcmd.sh script I have:

if [ "$1" = MSG ];then
   aplay ~/chat2.wav
if [ "$1" = MSG ];then
   echo "$4" >>~/.mcabber/log

The aplay command is purely to play a sound when I get a new message, but the 2nd if statement appends the event file for the message to a log file (this is the log file referenced in the .xinitrc).

So what happens is that you get a message in or out in mcabber, it runs the mcabber-eventcmd.sh script. That script appends details of a message file to a log file. Meanwhile the while loop in the .xinitrc is watching that log file. Everytime there’s a new line in the log file, the content of the file is piped into the stdin of dwm (via the ‘cat’) and the message appears on screen.

The scripts above are simplistic .. and don’t handle very long messages or multiline messages very well. And you’ll notice that the status bar takes about 1 second to change (this is probably to do with the tail -f)