-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirssi2pushover.pl
130 lines (116 loc) · 4.66 KB
/
irssi2pushover.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = "0.1";
%IRSSI = (
authors => "dap",
contact => "@dapithor",
name => "pushover",
description => "Sends PUSH notifcations to Pushover",
license => "",
url => "https://github.com/dapithor/irssi2pushover",
changed => "Tue Jan 07 11:11:11 EDT 2014",
);
sub debug {
return unless Irssi::settings_get_bool('pushover_debug');
my $text = shift;
my @caller = caller(1);
Irssi::print('From '.$caller[3].': '.$text);
}
sub pushmsgs {
my ($type, $message, $target) = @_;
# drop message if empty line
#return if ($message =~ m/^\s*$/);
# Pushover API URL
my $pushover_uri = 'https://api.pushover.net/1/messages.json';
# Setup constructor to connect
#my $pushover_agent = LWP::UserAgent->new;
my $pushover = LWP::UserAgent->new;
$pushover->agent("irssi-pushover/$VERSION");
my %msg_opts = (
'token' => Irssi::settings_get_str('pushover_apptoken'),
'user' => Irssi::settings_get_str('pushover_userkey'),
'title' => &create_title($type, $target),
'message' => $message,
);
if (!$msg_opts{'token'}) { debug('Missing pushover app token.'); return; }
debug('pushmsgs: Sending notification.');
my $pushmsg = $pushover->post( $pushover_uri, 'Content-Type' => 'application/x-www-form-urlencoded', Content => \%msg_opts);
if ($pushmsg->is_success) {
Irssi::print("Pushed: " . $message);
debug('Notification sent: ' . $pushmsg->decoded_content);
} else {
Irssi::print("Push Error received from Pushover.");
debug('Push ERROR: ' . $pushmsg->status_line);
}
}
# create a title for the pushover message
sub create_title {
my ($type, $target) = @_;
if ($type eq 'private') {
return "irssi: priv msg from $target";
} elsif ($type eq 'public') {
return "irssi: mentioned in $target";
} elsif ($type eq 'hilightcatcher') {
return "irssi: hilight $target";
}
else {
die "Received unknown message type \"$type\" in create_title\n";
}
}
# handles the processing of messages
sub event_handler {
my ($server, $msg, $nick, $address, $target) = @_;
#foreach(@_) { Irssi::print("Debug event_handler arg: ". $_); }
return unless (Irssi::settings_get_bool('pushover_enable'));
if ($target) {
if (!Irssi::settings_get_bool('pushover_hilight_catcher_enable')) {
# Send public message since $target is a channel AND hilight_catcher is disabled
pushmsgs('public', "<$nick> $msg", $target) if ($msg =~ m/$server->{'nick'}/);
}
} else {
# private message (this is NOT the correct way to do this. Might catch other events)
pushmsgs('private', $msg, $nick) if (Irssi::settings_get_bool('pushover_notify_on_privmsg'));
#pushmsgs('private', $nick, $msg);
}
}
sub hilight_catcher {
my($dest, $text, $stripped) = @_;
my $server = $dest->{server};
if($dest->{level} & MSGLEVEL_HILIGHT) {
pushmsgs('hilightcatcher', $stripped, 'catcher') if (Irssi::settings_get_bool('pushover_hilight_catcher_enable'));
}
}
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_apptoken', '');
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_userkey', '');
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_debug', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_enable', 1);
#extras:
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_notify_on_privmsg', 1);
# signal handlers
Irssi::signal_add_last("message public", "event_handler");
Irssi::signal_add_last("message private", "event_handler");
Irssi::signal_add_last('print text', 'hilight_catcher');
# hilight catcher extras:
# enable notifications on irssi specified /hilight(s)
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_hilight_catcher_enable', 0);
# onload print version and tell user to set options:
Irssi::print('%Y>>%n '.$IRSSI{name}.' '.$VERSION.' loaded.');
if (!Irssi::settings_get_str('pushover_userkey')) {
Irssi::print('%Y>>%n '.$IRSSI{name}.' Pushover User Key is not set, set it with /set pushover_userkey KEY');
}
if (!Irssi::settings_get_str('pushover_apptoken')) {
Irssi::print('%Y>>%n '.$IRSSI{name}.' Pushover application token is not set, set it with /set pushover_apptoken TOKEN');
}
if (Irssi::settings_get_bool('pushover_notify_on_privmsg')) {
Irssi::print('%Y>>%n '.$IRSSI{name}.' Notify on PrivMsg is on, disable with /set pushover_notify_on_privmsg OFF');
}
if (Irssi::settings_get_bool('pushover_hilight_catcher_enable')) {
Irssi::print('%Y>>%n '.$IRSSI{name}.' Notify on hilights is off, enable with /set pushover_hilight_catcher_enable OFF');
}
if (!Irssi::settings_get_bool('pushover_enable')) {
Irssi::print('%Y>>%n '.$IRSSI{name}.' Pushover disabled, enable with /set pushover_enable ON');
}