2010-11-09

Irssi notifications (and more)



This explains how to get desktop notifications and sound from irssi when you receive a private message, a DCC request, og someone says your nick in a channel.
Most of it was taken from:
http://jaredquinn.info/2007/09/using-irssi-with-libnotify-over-secure-shell/
I made some small modifications to get sound as well, and the settings for Xterm didn't work, but as I use urxvt anyways, I made it work for that instead.


Two scripts need to be added to your system, and you need to add a line to ~/.Xdefaults :

### Script ~/.irssi/scripts/libnotify.pl ###

## Put me in ~/.irssi/scripts, and then execute the following in irssi:
##
##       /load perl
##       /script load libnotify
##

# Obtained from: http://jaredquinn.info/wp-content/uploads/2009/11/libnotify.pl_.txt
# Needs: http://jaredquinn.info/wp-content/uploads/2009/11/notifier.sh.txt
# See: http://jaredquinn.info/2007/09/using-irssi-with-libnotify-over-secure-shell/
# Odd Eivind Ebbesen @ 2010-11-09 12:55:51

use strict;
use Irssi;
use vars qw($VERSION %IRSSI);

$VERSION = "0.01";
%IRSSI = (
    authors     => 'Jared Quinn',
    origauthors => 'Luke Macken, Paul W. Frields, ',
    contact     => 'jared@jaredquinn.info',
    name        => 'notify.pl',
    description => 'Use libnotify to alert user to hilighted messages',
    license     => 'GNU General Public License',
    url         => 'http://jaredquinn.info/irssi',
);

Irssi::settings_add_str('notify', 'notify_icon', 'gtk-dialog-info');
Irssi::settings_add_str('notify', 'notify_time', '5000');
Irssi::settings_add_str('notify', 'notify_command', 'remote-notify-send');

sub notify {
    my ($server, $summary, $message) = @_;
    # Make the message entity-safe
    $message =~ s/&/&/g; # That could have been done better.
    $message =~ s/    $message =~ s/>/>/g;
    $message =~ s/'/'/g;

    print STDERR "\033[5i";
    print STDERR "TYPE IRC\n";
    print STDERR "ICON " . Irssi::settings_get_str('notify_icon') . "\n";
    print STDERR "SHOWFOR " . Irssi::settings_get_str('notify_time') . "\n";
    print STDERR "SUBJECT " . $summary . "\n";
    print STDERR "CONTENT " . $message . "\n";
    print STDERR "\033[4i";
}

sub print_text_notify {
    my ($dest, $text, $stripped) = @_;
    my $server = $dest->{server};
    return if (!$server || !($dest->{level} & MSGLEVEL_HILIGHT));
    my $sender = $stripped;
    $sender =~ s/^\<.([^\>]+)\>.+/\1/ ;
    $stripped =~ s/^\<.[^\>]+\>.// ;
    my $summary = "Hilight in " . $dest->{target};
    notify($server, $summary, $stripped);
}

sub message_private_notify {
    my ($server, $msg, $nick, $address) = @_;
    return if (!$server);
    notify($server, "Private message from ".$nick, $msg);
}

sub dcc_request_notify {
    my ($dcc, $sendaddr) = @_;
    my $server = $dcc->{server};

    return if (!$dcc);
    notify($server, "DCC ".$dcc->{type}." request", $dcc->{nick});
}

Irssi::signal_add('print text', 'print_text_notify');
Irssi::signal_add('message private', 'message_private_notify');
Irssi::signal_add('dcc request', 'dcc_request_notify');

### END Script ~/.irssi/scripts/libnotify.pl ###

### Script ~/.irssi/scripts/notifier.sh ###

#!/bin/bash
# Obtained from: http://jaredquinn.info/wp-content/uploads/2009/11/notifier.sh.txt
# Used with: http://jaredquinn.info/wp-content/uploads/2009/11/libnotify.pl_.txt
# See: http://jaredquinn.info/2007/09/using-irssi-with-libnotify-over-secure-shell/

# Added the line with canberra-gtk-play for sound.
# Odd Eivind Ebbesen @ 2010-11-09 12:58:09

cat - | {
    nt_icon="gtk-dialog-info"
    nt_time=5000
    nt_head="Notify"
    nt_text="Error Occured"
    nt_type="Message"
    while read k v; do
        case $k in
            TYPE) nt_type=$v;;
            ICON) nt_icon=$v;;
            CONTENT) nt_text=$v;;
            TIMEOUT) nt_time=$v;;
            SUBJECT) nt_head=$v;;
        esac
    done
    notify-send -i "$nt_icon" -c "$nt_type" -t $nt_time -- "$nt_head" "$nt_text"
}
/usr/bin/canberra-gtk-play --id="message" >/dev/null 2>&1

### END Script ~/.irssi/scripts/notifier.sh ###

### Modification ~/.Xdefaults ###

urxvt*print-pipe: ~/.irssi/scripts/notifier.sh

### END Modification ~/.Xdefaults ###



The above is all that is needed to make this work on a single machine. As I use several machines all the time, I've made my setup partially mirrored over machines using Dropbox and symlinks, so I have most settings for apps (like irssi), and utility scripts in the same location on all machines, and they're synced at all times.

The files/dirs involved for this setup to be perfect for me, are:
~/Dropbox/bin/oscr.sh
~/.irssi -> Dropbox/dotfiles/.irssi.odd-t500
~/.ssh/config -> ../Dropbox/dotfiles/.ssh_config.odd-t500
~/.Xdefaults -> Dropbox/dotfiles/.Xdefaults.odd-t500
~/.zsh_aliases -> Dropbox/dotfiles/.zsh_aliases.odd-eee
oscr.sh is a small shell script that simplifies creation/reattaching of
screens:
### Script oscr.sh ###

#!/bin/bash
# oscr.sh ("Odds screen")
# Some wrappers for running screen quicker.
#
# Odd Eivind Ebbesen, 2010-11-08 20:16:43

sessions=$(screen -ls | tail -n +2 | head -n -2 | awk '{print $1}')
hostname=$(hostname)
cmd=
session_name=
session_default=
p_rs="-dRAa"
p_cr="-aAUOS"

for s in $sessions; do
    if [[ $s =~ $hostname ]]; then
        session_default="$s"
    fi
    if [[ -n $1 && $s =~ "$1" ]]; then
        session_name="$s"
    fi
done

if [[ -n $session_name ]]; then
    cmd="screen $p_rs $session_name"
elif [[ -z $session_name && -n $1 ]]; then
    cmd="screen $p_cr $1"
elif [[ -z $1 && -n $session_default ]]; then
    cmd="screen $p_rs $session_default"
elif [[ -z $1 && -z $session_default ]]; then
    cmd="screen $p_cr $hostname"
fi

$cmd

### END Script oscr.sh ###

I have setup host aliases and options in ~/.ssh/config, and in ~/.zsh_aliases I have aliased oscr="~/Dropbox/bin/oscr.sh ". This means I can now on my main machine start a screen dedicated to irssi with $oscr irssi and then $irssi inside screen (I know I could have automated even more). I then use irc, MSN & Jabber (via Minbif), and Twitter (via Twirssi) all in irssi, with popups and sound. If I detach screen, I'm automatically set as away (screen_away.pl), and I don't get any notifications (which is reversed back if I reattach). If I
then move away (to the couch or the other side of the world, for that matter..), I just type in from my netbook: $ssh odd-t500 and $oscr ir (oscr matches "ir" with the screen session name "irssi" set earlier), and I'm back to where I was, getting notifications and sound on my netbook instead.

Happy chatting!

No comments:

Post a Comment