Commit 088b0e35c53a20442bc5fbe69138f423c124cba3

Authored by Caesar Ahlenhed
0 parents

First commit

Showing 1 changed file with 142 additions and 0 deletions   Show diff stats
prowl.pl 0 → 100755
  1 +++ a/prowl.pl
... ... @@ -0,0 +1,142 @@
  1 +use strict;
  2 +use warnings;
  3 +
  4 +#####################################################################
  5 +# This script sends notifications to your
  6 +# iPhone using the prowl API when you are away.
  7 +#
  8 +# This script is loosely based on Denis Lemire's
  9 +# script, you can find his page on: http://www.denis.lemire.name/
  10 +#
  11 +# This script have some major differences in both code and usage
  12 +# from his script. The configuration he uses *won't* work with
  13 +# this script.
  14 +#
  15 +# Commands:
  16 +# /set prowl_api_key API_KEY
  17 +# /set prowl_general_hilight on/off
  18 +# /set prowl_priority_channel -2 up to 2
  19 +# /set prowl_priority_pm -2 up to 2
  20 +# /set prowl_priority_general -2 up to 2
  21 +#
  22 +# "General hilight" basically referrs to ALL the hilights you have
  23 +# added manually in irssi, if many, it can get really bloated if
  24 +# turned on. Default is Off.
  25 +#
  26 +# You will need the following packages:
  27 +# LWP::UserAgent (You can install this using cpan -i LWP::UserAgent)
  28 +# Crypt::SSLeay (You can install this using cpan -i Crypt::SSLeay)
  29 +#
  30 +# Or if you're using Debian GNU/Linux:
  31 +# apt-get update;apt-get install libwww-perl libcrypt-ssleay-perl
  32 +#
  33 +#
  34 +# eth0 will prevail. || irc.eth0.info
  35 +#
  36 +#####################################################################
  37 +
  38 +
  39 +use Irssi;
  40 +use Irssi::Irc;
  41 +use vars qw($VERSION %IRSSI);
  42 +
  43 +use LWP::UserAgent;
  44 +
  45 +$VERSION = "0.4";
  46 +
  47 +%IRSSI = (
  48 + authors => "Caesar 'sniker' Ahlenhed",
  49 + contact => "sniker\@se.linux.org",
  50 + name => "prowl",
  51 + description => "Sends prowlnotifcations when away",
  52 + license => "GPLv2",
  53 + url => "http://sniker.codebase.nu",
  54 + changed => "Mon Mar 15 18:53:32 CET 2010",
  55 +);
  56 +
  57 +# Configuration settings and default values.
  58 +Irssi::settings_add_str("prowl", "prowl_api_key", "");
  59 +Irssi::settings_add_bool("prowl", "prowl_general_hilight", 0);
  60 +Irssi::settings_add_str("prowl", "prowl_priority_channel", 0);
  61 +Irssi::settings_add_str("prowl", "prowl_priority_pm", 0);
  62 +Irssi::settings_add_str("prowl", "prowl_priority_general", 0);
  63 +
  64 +# The whole "send_noti" function is pretty much taken from the prowl example script.
  65 +sub send_noti {
  66 + my ($event, $text, $priority) = @_;
  67 +
  68 + my %options = ();
  69 +
  70 + $options{'application'} ||= "Irssi";
  71 + $options{'event'} = $event;
  72 + $options{'notification'} = $text;
  73 + $options{'priority'} = $priority;
  74 +
  75 +
  76 + # This check isn't really needed, just added if someone would
  77 + # would define $options{'apikey'} manually in the script.
  78 + if(!exists($options{'apikey'})){
  79 + $options{'apikey'} = Irssi::settings_get_str("prowl_api_key");
  80 + chomp $options{'apikey'};
  81 + }else{
  82 + Irssi::print("There is some kind of trouble with the API key.");
  83 + }
  84 +
  85 + $options{'application'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  86 + $options{'event'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  87 + $options{'notification'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  88 +
  89 + my ($userAgent, $request, $response, $requestURL);
  90 + $userAgent = LWP::UserAgent->new;
  91 + $userAgent->agent("ProwlScript/1.0");
  92 +
  93 + $requestURL = sprintf("https://prowl.weks.net/publicapi/add?apikey=%s&application=%s&event=%s&description=%s&priority=%d",
  94 + $options{'apikey'},
  95 + $options{'application'},
  96 + $options{'event'},
  97 + $options{'notification'},
  98 + $options{'priority'});
  99 +
  100 + $request = HTTP::Request->new(GET => $requestURL);
  101 + $response = $userAgent->request($request);
  102 +
  103 + if ($response->is_success) {
  104 + # I guess it worked, eh?
  105 + } elsif ($response->code == 401) {
  106 + Irssi::print( "Notification not posted: incorrect API key.");
  107 + } else {
  108 + Irssi::print("Notification not posted: " . $response->content);
  109 + }
  110 +}
  111 +
  112 +sub pubmsg {
  113 + my ($server, $data, $nick) = @_;
  114 +
  115 + if($server->{usermode_away} == 1 && $data =~ /$server->{nick}/i){
  116 + send_noti("Hilighted", $nick . ': ' . $data, Irssi::settings_get_str("prowl_priority_channel"));
  117 + }
  118 +}
  119 +
  120 +sub privmsg {
  121 + my ($server, $data, $nick) = @_;
  122 + if($server->{usermode_away} == 1){
  123 + send_noti("PM", $nick . ': ' . $data, Irssi::settings_get_str("prowl_priority_pm"));
  124 + }
  125 +}
  126 +
  127 +sub genhilight {
  128 + my($dest, $text, $stripped) = @_;
  129 + my $server = $dest->{server};
  130 +
  131 + if($dest->{level} & MSGLEVEL_HILIGHT) {
  132 + if($server->{usermode_away} == 1){
  133 + if(Irssi::settings_get_bool("prowl_general_hilight")){
  134 + send_noti("General Hilight", $stripped, Irssi::settings_get_str("prowl_priority_general"));
  135 + }
  136 + }
  137 + }
  138 +}
  139 +
  140 +Irssi::signal_add_last('message public', 'pubmsg');
  141 +Irssi::signal_add_last('message private', 'privmsg');
  142 +Irssi::signal_add_last('print text', 'genhilight');
... ...