Advertisement
Guest User

New prowlnotify.pl

a guest
Jun 27th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.48 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3.  
  4. #####
  5. #
  6. # Version history
  7. #
  8. # 0.4
  9. #   Push hilights
  10. # 0.3
  11. #   Add ability to toggle prowl mode (on/off/auto)
  12. # 0.2
  13. #   Modify to use new API keys, much better!
  14. # 0.1
  15. #   Initial working version
  16. #
  17.  
  18. # irssi imports
  19. use Irssi;
  20. use Irssi::Irc;
  21. use vars qw($VERSION %IRSSI %config);
  22.  
  23. use LWP::UserAgent;
  24.  
  25. $VERSION = "0.2";
  26.  
  27. %IRSSI = (
  28.     authors => "Denis Lemire",
  29.     contact => "denis\@lemire.name",
  30.     name => "prowl",
  31.     description => "Sets nick away when client discconects from the "
  32.         . "irssi-proxy sends messages to an iPhone via prowl.",
  33.     license => "GPLv2",
  34.     url => "http://www.denis.lemire.name",
  35. );
  36.  
  37. $config{away_level} = 0;
  38. $config{awayreason} = 'Auto-away because client has disconnected from proxy.';
  39. $config{mode} = 'auto';
  40. $config{debug} = 0;
  41. $config{clientcount} = 0;
  42.  
  43. sub debug
  44. {
  45.     if ($config{debug}) {
  46.         my $text = shift;
  47.         my $caller = caller;
  48.         Irssi::print('From ' . $caller . ":\n" . $text);
  49.     }
  50. }
  51.  
  52. sub send_prowl
  53. {
  54.     my ($event, $text) = @_;
  55.  
  56.     if ($config{mode} eq 'off') {
  57.         debug("Not sending notification, prowl is off.");
  58.         return;
  59.     }
  60.  
  61.     debug("Sending prowl");
  62.  
  63.     my %options = ();
  64.  
  65.     $options{'application'} ||= "Irssi";
  66.     $options{'event'} = $event;
  67.     $options{'notification'} = $text;
  68.     $options{'priority'} ||= 0;
  69.  
  70.     # Get the API key from STDIN if one isn't provided via a file or from the command line.
  71.  
  72.     if (open(APIKEYFILE, $ENV{HOME} . "/.prowlkey")) {
  73.         $options{apikey} = <APIKEYFILE>;
  74.  
  75.         chomp $options{apikey};
  76.  
  77.         close(APIKEYFILE);
  78.     } else {
  79.         debug ("Unable to open prowl key file\n");
  80.     }
  81.  
  82.     # URL encode our arguments
  83.     $options{'application'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  84.     $options{'event'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  85.     $options{'notification'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
  86.  
  87.     # Generate our HTTP request.
  88.     my ($userAgent, $request, $response, $requestURL);
  89.     $userAgent = LWP::UserAgent->new;
  90.     $userAgent->agent("ProwlScript/1.0");
  91.  
  92.     $requestURL = sprintf("https://prowlapp.com/publicapi/add?apikey=%s&application=%s&event=%s&description=%s&priority=%d",
  93.                     $options{'apikey'},
  94.                     $options{'application'},
  95.                     $options{'event'},
  96.                     $options{'notification'},
  97.                     $options{'priority'});
  98.  
  99.     $request = HTTP::Request->new(GET => $requestURL);
  100.  
  101.     $response = $userAgent->request($request);
  102.  
  103.     if ($response->is_success) {
  104.         debug ("Notification successfully posted.\n");
  105.     } elsif ($response->code == 401) {
  106.         debug ("Notification not posted: incorrect API key.\n");
  107.     } else {
  108.         debug ("Notification not posted: " . $response->content . "\n");
  109.     }
  110. }
  111.  
  112. sub client_connect
  113. {
  114.     # Originally, this subroutine and the one below set you away or
  115.     # back on _all_ servers, dependent on whether one or more clients
  116.     # were connected.
  117.  
  118.     # I changed them so that each client connection only controls your
  119.     # away state on the server associated with the proxy port it
  120.     # connects to.
  121.    
  122.     # No longer need full list of servers, just the one the client is
  123.     # using
  124.     my $client = shift;
  125.     my $server = $client->{server};
  126.  
  127.     $config{clientcount}++;
  128.     debug("Client connected.");
  129.  
  130.     # setback
  131.     # if you're away on that server send yourself back
  132.     if ($server->{usermode_away}) {
  133.     $server->send_raw('AWAY :');
  134.     }
  135. }
  136.  
  137. sub client_disconnect
  138. {
  139.     my $client = shift;
  140.     my $server = $client->{server};
  141.     debug('Client Disconnectted');
  142.  
  143.     $config{clientcount}-- unless $config{clientcount} == 0;
  144.  
  145.     # setaway
  146.     unless ($server->{usermode_away}) {
  147.     # we are not away on this server already.. set the autoaway
  148.     # reason
  149.     $server->send_raw(
  150.         'AWAY :' . $config{awayreason}
  151.         );
  152.     }
  153. }
  154.  
  155. sub msg_pub
  156. {
  157.     my ($server, $data, $nick, $mask, $target) = @_;
  158.      
  159.     if (($server->{usermode_away} == "1" || $config{mode} eq 'on')  && ($data =~ /$server->{nick}/i)) {
  160.         debug("Got pub msg with my name");
  161.         send_prowl ("Mention", $nick . ': ' . $data);
  162.     }
  163. }
  164.  
  165. sub msg_pri
  166. {
  167.     my ($server, $data, $nick, $address) = @_;
  168.     if ($server->{usermode_away} == "1" || $config{mode} eq 'on') {
  169.         send_prowl ("Private msg", $nick . ': ' . $data);
  170.     }
  171. }
  172.  
  173. sub msg_hilight
  174. {
  175.     my ($dest, $text, $stripped) = @_;
  176.  
  177.     my $server = $dest->{server};
  178.  
  179.     if ($dest->{level} & MSGLEVEL_HILIGHT) {
  180.         if ($server->{usermode_away} == "1" || $config{mode} eq 'on') {
  181.             send_prowl ("Highlight", $stripped);
  182.         }
  183.     }
  184. }
  185.  
  186. sub cmd_prowl
  187. {
  188.     my ($args, $server, $winit) = @_;
  189.  
  190.     $args = lc($args);
  191.  
  192.     if (
  193.         $args =~ /^auto$/ ||
  194.         $args =~ /^on$/ ||
  195.         $args =~ /^off$/
  196.     ) {
  197.         if ($args eq $config{mode}) {
  198.             Irssi::print("Prowl mode already $args");
  199.         } else {
  200.             Irssi::print("Prowl mode: $args (was " . $config{mode} . ')' );
  201.             $config{mode} = $args;
  202.         }
  203.     } elsif ($args =~/^test$/) {
  204.         Irssi::print("Sending test prowl notification");
  205.         send_prowl ("Test", "If you can read this, it worked.");
  206.     } elsif ($args =~/^debug$/) {
  207.         if ($config{debug}) {
  208.             $config{debug} = 0;
  209.             Irssi::print("Prowl debug disabled");
  210.         } else {
  211.             $config{debug} = 1;
  212.             Irssi::print("Prowl debug enabled");
  213.         }
  214.     } else {
  215.         Irssi::print('Prowl: Say what?!');
  216.     }
  217. }
  218.  
  219. Irssi::signal_add_last('proxy client connected', 'client_connect');
  220. Irssi::signal_add_last('proxy client disconnected', 'client_disconnect');
  221. Irssi::signal_add_last('message public', 'msg_pub');
  222. Irssi::signal_add_last('message private', 'msg_pri');
  223. Irssi::command_bind 'prowl' => \&cmd_prowl;
  224. Irssi::signal_add_last("print text", "msg_hilight");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement