Page 1 of 1

irissi Prowl Script

Posted: Wed Jul 08, 2009 10:47 pm
by ixtli
The following script will notify you when someone uses a hilight word or msg's you in irssi:

Code: Select all

# todo: grab topic changes

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

#prowl
#use lib '/System/Library/Perl/Extras/5.8.8';
use LWP::UserAgent;

use Irssi;
$VERSION = '0.0.3';
%IRSSI = (
   authors     => 'Thorsten Leemhuis',
   contact     => 'fedora@leemhuis.info',
   name        => 'fnotify',
   description => 'Write a notification to a file that shows who is talking to you in which channel.',
   url         => 'http://www.leemhuis.info/files/fnotify/',
   license     => 'GNU General Public License',
   changed     => '$Date: 2007-01-13 12:00:00 +0100 (Sat, 13 Jan 2007) $'
);

#--------------------------------------------------------------------
# In parts based on knotify.pl 0.1.1 by Hugo Haas
# http://larve.net/people/hugo/2005/01/knotify.pl
# which is based on osd.pl 0.3.3 by Jeroen Coekaerts, Koenraad Heijlen
# http://www.irssi.org/scripts/scripts/osd.pl
#
# Other parts based on notify.pl from Luke Macken
# http://fedora.feedjack.org/user/918/
#
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# Private message parsing
#--------------------------------------------------------------------

sub priv_msg {
   my ($server,$msg,$nick,$address,$target) = @_;
   prowlNotification($server->{chatnet}, $nick, $msg);
}

#--------------------------------------------------------------------
# Printing hilight's
#--------------------------------------------------------------------

sub hilight {
   my ($dest, $text, $stripped) = @_;
   return if (!$dest->{server} || !($dest->{level} & MSGLEVEL_HILIGHT) );
   #change the following two lines to put the user's nick in $x[0]
   my @x = split(/>/,$stripped);
   my @y = split(/\|\s/, $x[0]);
   $x[0] = $y[1];
   prowlNotification($dest->{target},$x[0], $x[1]);

}

#--------------------------------------------------------------------
# The actual printing
#--------------------------------------------------------------------

sub filewrite {
   my ($text) = @_;
   # FIXME: there is probably a better way to get the irssi-dir...
        open(FILE,">>$ENV{HOME}/.irssi/fnotify");
   print FILE $text . "\n";
        close (FILE);
}

# post a prowl notification
sub prowlNotification {

   my ($app, $event, $not) = @_;
   my (%options) = ();
   $options{'application'} = $app;
   $options{'event'} = $event;
   $options{'notification'} = $not;
   $options{'priority'} = 0;
   #changeme
   $options{'apikey'} = "KEY";

   # URL encode our arguments
   $options{'application'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
   $options{'event'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
   $options{'notification'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;

   # Generate our HTTP request.
   my ($userAgent, $request, $response, $requestURL);
   $userAgent = LWP::UserAgent->new;
   $userAgent->agent("ProwlScript/1.0");

   $requestURL = sprintf("https://prowl.weks.net/publicapi/add?apikey=%s&application=%s&event=%s&description=%s&priority=%d",
               $options{'apikey'},
               $options{'application'},
               $options{'event'},
               $options{'notification'},
               $options{'priority'});
   
   $request = HTTP::Request->new(GET => $requestURL);

   $response = $userAgent->request($request);
   if( !$response->is_success) {
      print "ERROR: Notification not posted: ".$response->content;
   }
}

#--------------------------------------------------------------------
# Irssi::signal_add_last / Irssi::command_bind
#--------------------------------------------------------------------

Irssi::signal_add_last("message private", "priv_msg");
Irssi::signal_add_last("print text", "hilight");

#- end

Save this script to ~/.irssi/scripts/fnotify.pl
Installation:
You must make sure that you have irssi built with perl. Try doing /load perl to make sure. If it doesnt work then do:

Code: Select all

port install irssi +perl.

Then, if you installed irssi using macports, do the following:

Code: Select all

sudo /opt/local/bin/perl -MCPAN -e 'install HTML::Parser'
sudo /opt/local/bin/perl -MCPAN -e 'install Crypt::SSLeay'
(Note that you have to have these two packages no matter how you installed perl/irssi, it's just that macports presents some interesting configuration difficulties when utilities overlap.)

Following that you must edit all of the lines marked with #changeme. It's not hard: the first one is a regexp that selects the user's name from the message that contained the hilighted word. I don't know how to make this automatic, since the handler is passed the full text of the line, and the formatting is different based on your theme. For reference, my lines look like this:

Code: Select all

19:21 | ag > I just use the default.
and the two "split" commands isolate "ag" and put it into $x[0]. The second required change is just to substitute your API key for "KEY" in the line:

Code: Select all

$options{'apikey'} = "KEY";


Then, in irssi do

Code: Select all

/script load fnotify