irssiNMA.pl 4.59 KB
use strict;
use warnings;

#####################################################################
# This script sends notifications to your
# iPhone using the Notify My Android API when you are away.
#
#
# Commands:
# /set nma_api_key API_KEY
# /set nma_general_hilight on/off
# /set nma_priority_channel -2 up to 2
# /set nma_priority_pm -2 up to 2
# /set nma_priority_general -2 up to 2
# /set nma_ignore_nettag NETWORKS # Networks in this case is a
#                                     comma separated list of
#                                     network tags that should
#                                     be ignored.
#
# "General hilight" basically referrs to ALL the hilights you have
# added manually in irssi, if many, it can get really bloated if
# turned on. Default is Off.
#
# You will need the following packages:
# LWP::UserAgent (You can install this using cpan -i LWP::UserAgent)
# Crypt::SSLeay  (You can install this using cpan -i Crypt::SSLeay)
# 
# Or if you're using Debian GNU/Linux:
# apt-get update;apt-get install libwww-perl libcrypt-ssleay-perl
#
# Creds to Kayjay for beta testing, this wouldn't have been
# possible otherwise since I don't own an Android device.
#
# eth0 will prevail. || irc.eth0.info
#
#####################################################################


use Irssi;
use Irssi::Irc;
use vars qw($VERSION %IRSSI);

use LWP::UserAgent;
use HTTP::Request::Common;

$VERSION = "0.2";

%IRSSI = (
    authors     => "Caesar 'sniker' Ahlenhed",
    contact     => "sniker\@codebase.nu",
    name        => "irssiNMA",
    description => "Sends notifonotifications when away",
    license     => "GPLv2",
    url         => "http://sniker.codebase.nu",
    changed     => "Mon Jun 23 18:13:25 CEST 2014",
);

# Configuration settings and default values.
Irssi::settings_add_str("irssiNMA", "nma_api_key", "");
Irssi::settings_add_bool("irssiNMA", "nma_general_hilight", 0);
Irssi::settings_add_str("irssiNMA", "nma_priority_channel", 0);
Irssi::settings_add_str("irssiNMA", "nma_priority_pm", 0);
Irssi::settings_add_str("irssiNMA", "nma_priority_general", 0);
Irssi::settings_add_str("irssiNMA", "nma_ignore_nettag", "");

sub send_noti {
    my ($title, $text, $priority) = @_;

    my %options = ();

    $options{'application'} ||= "Irssi";
    $options{'event'} = $title;
    $options{'msg'} = $text;
    $options{'priority'} = $priority;

    if(!exists($options{'apikey'})){
        $options{'apikey'} = Irssi::settings_get_str("nma_api_key");
        chomp $options{'apikey'};
    }else{
        Irssi::print("There is some kind of problem with the API key");
    }
    
    my ($userAgent, $req, $response);
    $userAgent = LWP::UserAgent->new;
    $userAgent->agent("irssiNMA/0.2");
    
    $req = POST("https://www.notifymyandroid.com/publicapi/notify",
                [
                  apikey => $options{'apikey'}, 
                  application => $options{'application'},
                  event => $options{'event'},
                  description => $options{'msg'},
                  priority => $options{'priority'},
                ]
    );
    
    $response = $userAgent->request($req);

    if ($response->is_success) {
        # I guess it worked, eh?
    } else {
        Irssi::print("Notification not posted: " . $response->content);
    }
}

sub pubmsg {
    my ($server, $data, $nick, $nick_addr, $target) = @_;

    if(!Irssi::settings_get_bool("nma_general_hilight")){
        if($server->{usermode_away} == 1 && $data =~ /$server->{nick}/i && index(Irssi::settings_get_str("nma_ignore_nettag"), $server->{tag}) == -1){
            send_noti("Hilighted" . $server->{tag} . "/" . $target, $nick . ': ' . $data, Irssi::settings_get_str("nma_priority_channel"));
        }
    }
}

sub privmsg {
    my ($server, $data, $nick) = @_;
    if($server->{usermode_away} == 1 && index(Irssi::settings_get_str("nma_ignore_nettag"), $server->{tag}) == -1){
        send_noti("PM @ " . $server->{tag}, $nick . ': ' . $data, Irssi::settings_get_str("nma_priority_pm"));
    }
}

sub genhilight {
    my($dest, $text, $stripped) = @_;
    my $server = $dest->{server};

    if($dest->{level} & MSGLEVEL_HILIGHT) {
        if($server->{usermode_away} == 1 && index(Irssi::settings_get_str("nma_ignore_nettag"), $server->{tag}) == -1){
            if(Irssi::settings_get_bool("nma_general_hilight")){
                send_noti("General Hilight" . $server->{tag} . "/" . $dest->{target}, $stripped, Irssi::settings_get_str("nma_priority_general"));
            }
        }
    }
}

Irssi::signal_add_last('message public', 'pubmsg');
Irssi::signal_add_last('message private', 'privmsg');
Irssi::signal_add_last('print text', 'genhilight');