Commit 6d12f6ae8490f5ddb33344528fa467f44c4d7159

Authored by Caesar Ahlenhed
0 parents

First commit

Showing 1 changed file with 131 additions and 0 deletions   Show diff stats
irssiNMA.pl 0 → 100644
  1 +++ a/irssiNMA.pl
... ... @@ -0,0 +1,131 @@
  1 +use strict;
  2 +use warnings;
  3 +
  4 +#####################################################################
  5 +# This script sends notifications to your
  6 +# iPhone using the Notify My Android API when you are away.
  7 +#
  8 +#
  9 +# Commands:
  10 +# /set nma_api_key API_KEY
  11 +# /set nma_general_hilight on/off
  12 +# /set nma_priority_channel -2 up to 2
  13 +# /set nma_priority_pm -2 up to 2
  14 +# /set nma_priority_general -2 up to 2
  15 +#
  16 +# "General hilight" basically referrs to ALL the hilights you have
  17 +# added manually in irssi, if many, it can get really bloated if
  18 +# turned on. Default is Off.
  19 +#
  20 +# You will need the following packages:
  21 +# LWP::UserAgent (You can install this using cpan -i LWP::UserAgent)
  22 +# Crypt::SSLeay (You can install this using cpan -i Crypt::SSLeay)
  23 +#
  24 +# Or if you're using Debian GNU/Linux:
  25 +# apt-get update;apt-get install libwww-perl libcrypt-ssleay-perl
  26 +#
  27 +# Creds to Kayjay for beta testing, this wouldn't have been
  28 +# possible otherwise since I don't own an Android device.
  29 +#
  30 +# eth0 will prevail. || irc.eth0.info
  31 +#
  32 +#####################################################################
  33 +
  34 +
  35 +use Irssi;
  36 +use Irssi::Irc;
  37 +use vars qw($VERSION %IRSSI);
  38 +
  39 +use LWP::UserAgent;
  40 +use HTTP::Request::Common;
  41 +
  42 +$VERSION = "0.1";
  43 +
  44 +%IRSSI = (
  45 + authors => "Caesar 'sniker' Ahlenhed",
  46 + contact => "sniker\@se.linux.org",
  47 + name => "irssiNMA",
  48 + description => "Sends notifonotifications when away",
  49 + license => "GPLv2",
  50 + url => "http://sniker.codebase.nu",
  51 + changed => "Mon Mar 12 12:33:32 CET 2012",
  52 +);
  53 +
  54 +# Configuration settings and default values.
  55 +Irssi::settings_add_str("irssiNMA", "nma_api_key", "");
  56 +Irssi::settings_add_bool("irssiNMA", "nma_general_hilight", 0);
  57 +Irssi::settings_add_str("irssiNMA", "nma_priority_channel", 0);
  58 +Irssi::settings_add_str("irssiNMA", "nma_priority_pm", 0);
  59 +Irssi::settings_add_str("irssiNMA", "nma_priority_general", 0);
  60 +
  61 +sub send_noti {
  62 + my ($title, $text, $priority) = @_;
  63 +
  64 + my %options = ();
  65 +
  66 + $options{'application'} ||= "Irssi";
  67 + $options{'event'} = $title;
  68 + $options{'msg'} = $text;
  69 + $options{'priority'} = $priority;
  70 +
  71 + if(!exists($options{'apikey'})){
  72 + $options{'apikey'} = Irssi::settings_get_str("nma_api_key");
  73 + chomp $options{'apikey'};
  74 + }else{
  75 + Irssi::print("There is some kind of problem with the API key");
  76 + }
  77 +
  78 + my ($userAgent, $req, $response);
  79 + $userAgent = LWP::UserAgent->new;
  80 + $userAgent->agent("irssiNMA/0.1");
  81 +
  82 + $req = POST("https://www.notifymyandroid.com/publicapi/notify",
  83 + [
  84 + apikey => $options{'apikey'},
  85 + application => $options{'application'},
  86 + event => $options{'event'},
  87 + description => $options{'msg'},
  88 + priority => $options{'priority'},
  89 + ]
  90 + );
  91 +
  92 + $response = $userAgent->request($req);
  93 +
  94 + if ($response->is_success) {
  95 + # I guess it worked, eh?
  96 + } else {
  97 + Irssi::print("Notification not posted: " . $response->content);
  98 + }
  99 +}
  100 +
  101 +sub pubmsg {
  102 + my ($server, $data, $nick) = @_;
  103 +
  104 + if($server->{usermode_away} == 1 && $data =~ /$server->{nick}/i){
  105 + send_noti("Hilighted", $nick . ': ' . $data, Irssi::settings_get_str("nma_priority_channel"));
  106 + }
  107 +}
  108 +
  109 +sub privmsg {
  110 + my ($server, $data, $nick) = @_;
  111 + if($server->{usermode_away} == 1){
  112 + send_noti("PM", $nick . ': ' . $data, Irssi::settings_get_str("nma_priority_pm"));
  113 + }
  114 +}
  115 +
  116 +sub genhilight {
  117 + my($dest, $text, $stripped) = @_;
  118 + my $server = $dest->{server};
  119 +
  120 + if($dest->{level} & MSGLEVEL_HILIGHT) {
  121 + if($server->{usermode_away} == 1){
  122 + if(Irssi::settings_get_bool("nma_general_hilight")){
  123 + send_noti("General Hilight", $stripped, Irssi::settings_get_str("nma_priority_general"));
  124 + }
  125 + }
  126 + }
  127 +}
  128 +
  129 +Irssi::signal_add_last('message public', 'pubmsg');
  130 +Irssi::signal_add_last('message private', 'privmsg');
  131 +Irssi::signal_add_last('print text', 'genhilight');
... ...