-
Notifications
You must be signed in to change notification settings - Fork 2
/
pushover.pl
132 lines (116 loc) · 4.23 KB
/
pushover.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
131
132
#!/usr/bin/perl
# irssi-pushover by Tobias 'towo' Wolter <irssi@towo.eu>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use Irssi;
use LWP::UserAgent;
use strict;
use warnings;
use vars qw($VERSION %IRSSI);
$VERSION = "20121003-1";
%IRSSI = (
authors => 'Tobias \'towo\' Wolter',
contact => 'irssi@towo.eu',
name => 'irssi-pushover',
description => 'Sends irssi mentions and messages via Pushover',
license => 'GPLv3',
url => 'http://github.com/towo/irssi-pushover',
);
my $api = new LWP::UserAgent;
my $apiurl = 'https://api.pushover.net/1/messages.json';
my $response;
# some niceties
$api->agent("irssi-pushover/$VERSION");
$api->env_proxy;
$api->timeout(5); # too long timeouts would foul up IRC experience
# create a title for the pushover message
sub create_title {
my ($type, $target) = @_;
if ($type eq 'private') {
return "irssi: private message from $target";
} elsif ($type eq 'public') {
return "irssi: public hilight in $target";
} else {
die "Received unknown message type \"$type\" in create_title\n";
}
}
# check if we're actually allowed to send a notification
sub allowed_to_send {
my $server = shift;
# filter to check for away status
if (Irssi::settings_get_bool('pushover_away_only')) {
return 0 unless ($server->{usermode_away});
}
return 1;
}
# check if api token and target user are set to something
sub sanity_check {
if (Irssi::settings_get_str('pushover_api_token') =~ m/^\s*$/) {
Irssi::print("pushover_api_token has not been set, disabling pushover.");
Irssi::settings_set_bool('pushover_api_token', 0);
return 0;
} elsif (Irssi::settings_get_str('pushover_target_user') =~ m/^\s*$/) {
Irssi::print("pushover_target_user has not been set, disabling pushover.");
Irssi::settings_set_bool('pushover_target_user', 0);
return 0;
} else {
return 1;
}
}
# send the request to pushover
sub send_to_pushover {
my ($type, $target, $message) = @_;
# drop message if empty line
return if ($message =~ m/^\s*$/);
my %request = (
'token' => Irssi::settings_get_str('pushover_api_token'),
'user' => Irssi::settings_get_str('pushover_target_user'),
'title' => &create_title($type, $target),
'message' => $message,
);
if (Irssi::settings_get_str('pushover_target_device')) {
$request{device} = Irssi::settings_get_str('pushover_target_device');
}
$response = $api->post( $apiurl, \%request );
if ($response->is_error()) {
# FIXME: this is lazy
Irssi::print("Pushover returned an error while sending, please correct"
. " the mistake by analyzing this statement:\n"
. $response->decoded_content
. "\nDisabling pushover notifications.");
Irssi::settings_set_bool('pushover_enable', 0);
}
}
# handles the processing of messages
sub event_handler {
my ($server, $msg, $nick, $address, $target) = @_;
return unless (Irssi::settings_get_bool('pushover_enable'));
return unless (&allowed_to_send($server));
return unless (&sanity_check);
if ($target) {
# public message
send_to_pushover('public', $target, "<$nick> $msg") if ($msg =~ m/$server->{'nick'}/);
} else {
# private message
send_to_pushover('private', $nick, $msg);
}
}
# signal handlers
Irssi::signal_add("message public", "event_handler");
Irssi::signal_add("message private", "event_handler");
# relevant settings; self-explanatory
Irssi::settings_add_bool('pushover', 'pushover_enable', 0);
Irssi::settings_add_str('pushover', 'pushover_api_token', 'iV2kqvQaDnUEOK4UIIT9HtXnL0RtGL');
Irssi::settings_add_str('pushover', 'pushover_target_user', '');
Irssi::settings_add_str('pushover', 'pushover_target_device', '');
Irssi::settings_add_bool('pushover', 'pushover_away_only', '1');