forked from xintron/irssi-pushover
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpushover.pl
359 lines (302 loc) · 10.6 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Push hilights and private while away
# Author: Marcus Carlsson <[email protected]>
use strict;
use warnings;
use Irssi;
use vars qw($VERSION %IRSSI %config);
use LWP::UserAgent;
use Scalar::Util qw(looks_like_number);
$VERSION = '0.3';
%IRSSI = (
authors => 'Marcus Carlsson',
contact => '[email protected]',
name => 'pushover',
description => 'Push hilights and private messages when away',
license => 'BSD',
url => 'https://github.com/xintron',
);
my $pushover_ignorefile;
sub cmd_help {
my $out = <<'HELP_EOF';
PUSHIGNORE LIST
PUSHIGNORE ADD <hostmask>
PUSHIGNORE REMOVE <number>
The mask matches in the format ident@host. Notice that no-ident responses puts
a tilde in front of the ident.
Examples:
Will match [email protected] but *not* [email protected].
/PUSHIGNORE ADD foo@*.bar.se
Use the list-command to show a list of ignores and the number in front
combined with remove to delete that mask.
/PUSHIGNORE REMOVE 2
For a list of available settings, run:
/set pushover
HELP_EOF
chomp $out;
Irssi::print($out, MSGLEVEL_CLIENTCRAP);
}
sub read_settings {
$pushover_ignorefile = Irssi::settings_get_str('pushover_ignorefile');
}
sub debug {
return unless Irssi::settings_get_bool('pushover_debug');
my $text = shift;
my @caller = caller(1);
Irssi::print('From '.$caller[3].': '.$text);
}
sub send_push {
my $user_key = Irssi::settings_get_str('pushover_user_key');
my $api_token = Irssi::settings_get_str('pushover_api_token');
if (!$user_key) {
debug('Missing Pushover user key.');
return;
}
debug('Sending notification.');
my ($channel, $text) = @_;
my $resp = LWP::UserAgent->new()->post(
'https://api.pushover.net/1/messages.json', [
token => $api_token,
user => $user_key,
message => $text,
sound => Irssi::settings_get_str('pushover_sound'),
title => $channel,
url => Irssi::settings_get_str('pushover_url'),
url_title => Irssi::settings_get_str('pushover_url_title'),
]
);
if ($resp->is_success) {
debug('Notification successfully sent.');
}
else {
debug('Notification not sent: '.$resp->decoded_content);
}
}
sub msg_pub {
my ($server, $data, $nick, $address, $target) = @_;
my $safeNick = quotemeta($server->{nick});
if(check_ignore_channels($target)) {
return;
}
if(check_ignore($address) || check_away($server) || check_tmux_attached()) {
return;
}
if ($data =~ /$safeNick/i) {
debug('Got pub msg.');
send_push($target, $nick.': '.strip_formating($data));
}
}
sub msg_print_text {
my ($dest, $text, $stripped) = @_;
my $server = $dest->{server};
my $target = $dest->{target};
return if (!$server || !($dest->{level} & MSGLEVEL_HILIGHT));
if(check_ignore_channels($target)) {
return;
}
if(check_away($server) || check_tmux_attached()) {
return;
}
debug('Got nick highlight');
$stripped =~ s/^\s+|\s+$//g;
send_push($target, $stripped);
}
sub msg_pri {
my ($server, $data, $nick, $address) = @_;
if(check_ignore($address) || check_away($server) || check_tmux_attached()) {
return;
}
debug('Got priv msg.');
send_push('Priv, '.$nick, strip_formating($data));
}
sub msg_kick {
my ($server, $channel, $nick, $kicker, $address, $reason) = @_;
if(check_ignore($address) || check_away($server)) {
return;
}
if ($nick eq $server->{nick}) {
debug('Was kicked.');
send_push('Kicked: '.$channel, 'Was kicked by: '.$kicker.'. Reason: '.strip_formating($reason));
}
}
sub msg_test {
my ($data, $server, $item) = @_;
$data =~ s/^([\s]+).*$/$1/;
my $orig_debug = Irssi::settings_get_bool('pushover_debug');
Irssi::settings_set_bool('pushover_debug', 1);
debug("Sending test message :" . $data);
send_push("Test Message", strip_formating($data));
Irssi::settings_set_bool('pushover_debug', $orig_debug);
}
sub strip_formating {
my ($msg) = @_;
$msg =~ s/\x03[0-9]{0,2}(,[0-9]{1,2})?//g;
$msg =~ s/[^\x20-\xFF]//g;
$msg =~ s/\xa0/ /g;
return $msg;
}
# check our away status & pushover_only_if_away. returns 0 if it's ok to send a message.
sub check_away {
my ($server) = @_;
my $msg_only_if_away = Irssi::settings_get_bool('pushover_only_if_away');
if ($msg_only_if_away && $server->{usermode_away} != '1') {
debug("Only sending messages if we're marked as away, and we're not");
return 1;
}
return 0;
}
# check our tmux status to see if we're attached. returns 0 if it's ok to send a message.
sub check_tmux_attached {
my $msg_only_if_detached = Irssi::settings_get_bool('pushover_tmux_only_if_unattached');
my $tmux_session_name = Irssi::settings_get_str('pushover_tmux_session_name');
if ($msg_only_if_detached && $tmux_session_name eq '') {
debug('tmux_session_name is blank, exiting');
return 1;
}
my $tmux_attached_command = "tmux list-sessions | grep \"^${tmux_session_name}.*(attached)\$\"";
my $tmux_attached_status = `$tmux_attached_command`;
if ($msg_only_if_detached && ($tmux_attached_status ne '')) {
debug("Only sending messages if we're detached as away, and we're attached");
return 1;
}
return 0;
}
sub check_ignore {
return 0 unless(Irssi::settings_get_bool('pushover_ignore'));
my @ignores = read_file();
return 0 unless(@ignores);
my ($mask) = @_;
foreach (@ignores) {
$_ =~ s/\./\\./g;
$_ =~ s/\*/.*?/g;
if ($mask =~ m/^$_$/i) {
debug('Ignore matches, not pushing.');
return 1;
}
}
return 0;
}
sub check_ignore_channels {
my ($target) = @_;
my @ignore_channels = split(' ', Irssi::settings_get_str('pushover_ignorechannels'));
return 0 unless @ignore_channels;
if (grep {lc($_) eq lc($target)} @ignore_channels) {
debug("$target set as ignored channel.");
return 1;
}
return 0;
}
sub ignore_handler {
my ($data, $server, $item) = @_;
$data =~ s/\s+$//g;
Irssi::command_runsub('pushignore', $data, $server, $item);
}
sub ignore_unknown {
cmd_help();
Irssi::signal_stop(); # Don't print 'no such command' error.
}
sub ignore_list {
my @data = read_file();
if (@data) {
my $i = 1;
my $out;
foreach(@data) {
$out .= $i++.". $_\n";
}
chomp $out;
Irssi::print($out, MSGLEVEL_CLIENTCRAP);
}
}
sub ignore_add {
my ($data, $server, $item) = @_;
$data =~ s/^([\s]+).*$/$1/;
return Irssi::print("No hostmask given.", MSGLEVEL_CLIENTCRAP) unless($data ne "");
my @ignores = read_file();
push(@ignores, $data);
write_file(@ignores);
Irssi::print("Successfully added '$data'.", MSGLEVEL_CLIENTCRAP);
}
sub ignore_remove {
my($num, $server, $item) = @_;
$num =~ s/^(\d+).*$/$1/;
return Irssi::print("List-number is needed when removing", MSGLEVEL_CLIENTCRAP) unless(looks_like_number($num));
my @ignores = read_file();
# Index out of range
return Irssi::print("Number was out of range.", MSGLEVEL_CLIENTCRAP) unless(scalar(@ignores) >= $num);
delete $ignores[$num-1];
write_file(@ignores);
}
sub write_file {
read_settings();
my $fp;
if (!open($fp, ">", $pushover_ignorefile)) {
Irssi::print("Error opening ignore file", MSGLEVEL_CLIENTCRAP);
return;
}
print $fp join("\n", @_);
close $fp;
}
sub read_file {
read_settings();
return if !(-f $pushover_ignorefile);
my $fp;
if (!open($fp, "<", $pushover_ignorefile)) {
Irssi::print("Error opening ignore file", MSGLEVEL_CLIENTCRAP);
return;
}
my @out;
while (<$fp>) {
chomp;
next if $_ eq '';
push(@out, $_);
}
close $fp;
return @out;
}
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_user_key', '');
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_api_token', '');
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_url', '');
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_url_title', '');
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_debug', 0);
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_ignore', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_only_if_away', 0);
Irssi::settings_add_bool($IRSSI{'name'}, 'pushover_tmux_only_if_unattached', 0);
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_tmux_session_name', '');
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_ignorefile', Irssi::get_irssi_dir().'/pushover_ignores');
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_ignorechannels', '');
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_sound', '');
Irssi::command_bind('help pushignore', \&cmd_help);
Irssi::command_bind('pushignore help', \&cmd_help);
Irssi::command_bind('pushignore add', \&ignore_add);
Irssi::command_bind('pushignore remove', \&ignore_remove);
Irssi::command_bind('pushignore list', \&ignore_list);
Irssi::command_bind('pushignore', \&ignore_handler);
Irssi::command_bind('pushtest', \&msg_test);
Irssi::signal_add_first("default command pushignore", \&ignore_unknown);
Irssi::signal_add_last('print text', 'msg_print_text');
Irssi::signal_add_last('message private', 'msg_pri');
Irssi::signal_add_last('message kick', 'msg_kick');
Irssi::print('%Y>>%n '.$IRSSI{name}.' '.$VERSION.' loaded.');
if (!Irssi::settings_get_str('pushover_user_key')) {
# try migrating from old setting
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_token', '');
if (Irssi::settings_get_str('pushover_token')) {
Irssi::settings_set_str('pushover_user_key', Irssi::settings_get_str('pushover_token'));
Irssi::print('%Y>>%n '.$IRSSI{name}.' Pushover User Key migrated from old setting, you should probably /save');
Irssi::signal_emit('setup changed');
}
}
if (!Irssi::settings_get_str('pushover_user_key')) {
Irssi::print('%Y>>%n '.$IRSSI{name}.' Pushover User Key is not set, set it with /set pushover_user_key key');
}
if (!Irssi::settings_get_str('pushover_api_token')) {
# try migrating from old setting
Irssi::settings_add_str($IRSSI{'name'}, 'pushover_apptoken', '');
if (Irssi::settings_get_str('pushover_apptoken')) {
Irssi::settings_set_str('pushover_api_token', Irssi::settings_get_str('pushover_apptoken'));
Irssi::print('%Y>>%n '.$IRSSI{name}.' Pushover API Token migrated from old setting, you should probably /save');
Irssi::signal_emit('setup changed');
}
}
if (!Irssi::settings_get_str('pushover_api_token')) {
Irssi::print('%Y>>%n '.$IRSSI{name}.' Pushover API token is not set, set it with /set pushover_api_token token');
}