-
Notifications
You must be signed in to change notification settings - Fork 5
/
growl.pl
398 lines (351 loc) · 13.4 KB
/
growl.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env perl -w
# growl.pl
# Copyright (c) 2011 Sorin Ionescu <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
use strict;
use vars qw($VERSION %IRSSI);
use Irssi;
use Growl::GNTP;
use IO::Socket::PortState qw(check_ports);
$VERSION = '1.0.7';
%IRSSI = (
authors => 'Sorin Ionescu',
contact => '[email protected]',
name => 'Growl',
description => 'Sends Growl notifications from Irssi',
license => 'GPLv3',
url => 'http://github.com/sorin-ionescu/irssi-growl',
);
# Notification Settings
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_message_public', 0);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_message_private', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_message_action', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_message_notice', 0);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_message_invite', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_highlight', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_notifylist', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_server', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_channel_topic', 1);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_show_dcc', 1);
Irssi::settings_add_str($IRSSI{'name'}, 'growl_channel_filter', '.*');
# Network Settings
Irssi::settings_add_str($IRSSI{'name'}, 'growl_net_host', 'localhost');
Irssi::settings_add_str($IRSSI{'name'}, 'growl_net_port', '23053');
Irssi::settings_add_str($IRSSI{'name'}, 'growl_net_pass', '');
# Icon Settings
Irssi::settings_add_str($IRSSI{'name'}, 'growl_net_icon', 'icon.png');
# Sticky Settings
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_net_sticky', 0);
Irssi::settings_add_bool($IRSSI{'name'}, 'growl_net_sticky_away', 1);
# Growl Initialization
my $growl = Growl::GNTP->new(
AppName => "Irssi",
PeerHost => Irssi::settings_get_str('growl_net_host'),
PeerPort => Irssi::settings_get_str('growl_net_port'),
AppIcon => Irssi::settings_get_str('growl_net_icon'),
Password => Irssi::settings_get_str('growl_net_pass'),
);
# Growl Registration
eval {
$growl->register([
{ Name => "Public", },
{ Name => "Private", },
{ Name => "Action", },
{ Name => "Notice", },
{ Name => "Invite", },
{ Name => "Highlight", },
{ Name => "Notify List", },
{ Name => "Server", },
{ Name => "Channel", },
{ Name => "DCC", },
]);
} or do {
if ($@) {
Irssi::print("growl: Could not register, connection refused.");
}
};
sub cmd_help {
Irssi::print('Growl can be configured with these settings:');
Irssi::print('%WNotification Settings%n');
Irssi::print(' %ygrowl_show_message_public%n : Notify on public message. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_message_private%n : Notify on private message. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_message_action%n : Notify on action message. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_message_notice%n : Notify on notice message. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_message_invite%n : Notify on channel invitation message. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_highlight%n : Notify on nick highlight. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_notifylist%n : Notify on notification list connect and disconnect. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_server%n : Notify on server connect and disconnect. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_channel_topic%n : Notify on channel topic change. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_show_dcc_request%n : Notify on DCC chat/file transfer messeges. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_channel_filter%n : Restrict public/action messages to certain channels');
Irssi::print('%WNetwork Settings%n');
Irssi::print(' %ygrowl_net_host%n : Set the Growl server host.');
Irssi::print(' %ygrowl_net_port%n : Set the Growl server port.');
Irssi::print(' %ygrowl_net_pass%n : Set the Growl server password.');
Irssi::print('%WIcon Settings%n');
Irssi::print(' %ygrowl_net_icon%n : Set the Growl notification icon path.');
Irssi::print('%WSticky Settings%n');
Irssi::print(' %ygrowl_net_sticky%n : Set sticky notifications. (ON/OFF/TOGGLE)');
Irssi::print(' %ygrowl_net_sticky_away%n : Set sticky notifications only when away. (ON/OFF/TOGGLE)');
}
sub is_growl_connectable {
my $host = Irssi::settings_get_str('growl_net_host');
my $port = Irssi::settings_get_str('growl_net_port');
my $timeout = 5;
my %port_hash = (
tcp => {
$port => {
name => 'Growl',
},
},
);
check_ports($host, $timeout, \%port_hash);
return $port_hash{tcp}{$port}{open};
}
sub is_notification_sticky {
my ($server);
$server = Irssi::active_server();
if (Irssi::settings_get_bool('growl_net_sticky_away')) {
if (!$server->{usermode_away}) {
return 0;
} else {
return 1;
}
} else {
return Irssi::settings_get_bool('growl_net_sticky');
}
}
sub do_notify {
my $icon = "file://$ENV{'HOME'}/.irssi/" . Irssi::settings_get_str('growl_net_icon');
my $sticky = is_notification_sticky();
my ($event, $title, $message, $priority) = @_;
if (!is_growl_connectable()) {
return 'growl: Could not notify, connection refused.';
}
eval {
$growl->notify(
Event => $event,
Title => $title,
Message => $message,
Icon => $icon,
Priotity => $priority,
Sticky => $sticky
);
} or do {
if ($@) {
return 'growl: Could not notify, connection refused.';
}
};
}
sub read_from_notifier_child {
my $target = shift;
my $read_handler = $target->{fh};
my $result = <$read_handler>;
Irssi::print($result) if $result;
close($target->{fh});
Irssi::input_remove($target->{tag});
}
sub growl_notify {
pipe READ, WRITE;
my $pid = fork();
unless (defined($pid)) {
Irssi::print("growl: Can't fork - aborting");
return;
}
if ($pid > 0) {
# the original process, just add a listener for pipe
close (WRITE);
Irssi::pidwait_add($pid);
my $target = {fh => \*READ, tag => undef};
$target->{tag} = Irssi::input_add(fileno(READ), INPUT_READ, \&read_from_notifier_child, $target);
} else {
# the new process, fetch addresses and write to the pipe
print WRITE do_notify(@_);
close (READ);
close (WRITE);
POSIX::_exit(1);
}
}
sub sig_message_public {
return unless Irssi::settings_get_bool('growl_show_message_public');
my ($server, $msg, $nick, $address, $target) = @_;
my $channel_filter = Irssi::settings_get_str('growl_channel_filter');
return if $target !~ /$channel_filter/;
growl_notify("Public", "Public Message: $target", "$nick: $msg", 0);
}
sub sig_message_private {
return unless Irssi::settings_get_bool('growl_show_message_private');
my ($server, $msg, $nick, $address) = @_;
growl_notify("Private", "Private Message", "$nick: $msg", 1);
}
sub sig_message_dcc {
return unless Irssi::settings_get_bool('growl_show_message_private');
my ($dcc, $msg) = @_;
growl_notify("Private", "Private Message", "$dcc->{nick}: $msg", 1);
}
sub sig_ctcp_action {
return unless Irssi::settings_get_bool('growl_show_message_action');
my ($server, $args, $nick, $address, $target) = @_;
my $channel_filter = Irssi::settings_get_str('growl_channel_filter');
return if $target !~ /$channel_filter/;
growl_notify("Action", "Action Message: $target", "$nick: $args", 1);
}
sub sig_message_dcc_action {
return unless Irssi::settings_get_bool('growl_show_message_action');
my ($dcc, $msg) = @_;
growl_notify("Action", "Action Message", "$dcc->{nick}: $msg", 1);
}
sub sig_event_notice {
return unless Irssi::settings_get_bool('growl_show_message_notice');
my ($server, $data, $source) = @_;
$data =~ s/^[^:]*://;
growl_notify("Notice", "Notice Message", "$source: $data", 1);
}
sub sig_message_invite {
return unless Irssi::settings_get_bool('growl_show_message_invite');
my ($server, $channel, $nick, $address) = @_;
growl_notify(
"Invite",
"Channel Invitation",
"$nick has invited you to join $channel.",
1
);
}
sub sig_print_text {
return unless Irssi::settings_get_bool('growl_show_highlight');
my ($dest, $text, $stripped) = @_;
my $nick;
my $msg;
if ($dest->{level} & MSGLEVEL_HILIGHT) {
$stripped =~ s/^\s+|\s+$//g;
growl_notify("Highlight", "Highlighted Message", $stripped, 2);
}
}
sub sig_notifylist_joined {
return unless Irssi::settings_get_bool('growl_show_notifylist');
my ($server, $nick, $user, $host, $realname, $awaymsg) = @_;
growl_notify(
"Notify List",
"Friend Connected",
("$realname" || "$nick") . " has connected to $server->{chatnet}.",
0
);
}
sub sig_notifylist_left {
return unless Irssi::settings_get_bool('growl_show_notifylist');
my ($server, $nick, $user, $host, $realname, $awaymsg) = @_;
growl_notify(
"Notify List",
"Friend Disconnected",
("$realname" || "$nick") . " has disconnected from $server->{chatnet}.",
0
);
}
sub sig_server_connected {
return unless Irssi::settings_get_bool('growl_show_server');
my($server) = @_;
growl_notify(
"Server",
"Server Connected",
"Connected to network $server->{chatnet}.",
0
);
}
sub sig_server_disconnected {
return unless Irssi::settings_get_bool('growl_show_server');
my($server) = @_;
growl_notify(
"Server",
"Server Disconnected",
"Disconnected from network $server->{chatnet}.",
0
);
}
sub sig_channel_topic_changed {
return unless Irssi::settings_get_bool('growl_show_channel_topic');
my ($channel) = @_;
growl_notify(
"Channel",
"Channel Topic",
"$channel->{name}: $channel->{topic}",
0
);
}
sub sig_dcc_request {
return unless Irssi::settings_get_bool('growl_show_dcc');
my ($dcc, $sendaddr) = @_;
my $title;
my $message;
if ($dcc->{type} =~ /CHAT/) {
$title = "Direct Chat Request";
$message = "$dcc->{nick} wants to chat directly.";
}
if ($dcc->{type} =~ /GET/) {
$title = "File Transfer Request";
$message = "$dcc->{nick} wants to send you $dcc->{arg}.";
}
growl_notify("DCC", $title, $message, 0);
}
sub sig_dcc_closed {
return unless Irssi::settings_get_bool('growl_show_dcc');
my ($dcc) = @_;
my $title;
my $message;
if ($dcc->{type} =~ /GET|SEND/) {
if ($dcc->{size} == $dcc->{transfd}) {
if ($dcc->{type} =~ /GET/) {
$title = "Download Complete";
}
if ($dcc->{type} =~ /SEND/) {
$title = "Upload Complete";
}
}
else {
if ($dcc->{type} =~ /GET/) {
$title = "Download Failed";
}
if ($dcc->{type} =~ /SEND/) {
$title = "Upload Failed";
}
}
$message = $dcc->{arg};
}
if ($dcc->{type} =~ /CHAT/) {
$title = "Direct Chat Ended";
$message = "Direct chat with $dcc->{nick} has ended.";
}
growl_notify("DCC", $title, $message, 0);
}
Irssi::command_bind('growl', 'cmd_help');
Irssi::signal_add_last('message public', \&sig_message_public);
Irssi::signal_add_last('message private', \&sig_message_private);
Irssi::signal_add_last('message dcc', \&sig_message_dcc);
Irssi::signal_add_last('ctcp action', \&sig_ctcp_action);
Irssi::signal_add_last('message dcc action', \&sig_message_dcc_action);
Irssi::signal_add_last('event notice', \&sig_event_notice);
Irssi::signal_add_last('message invite', \&sig_message_invite);
Irssi::signal_add_last('print text', \&sig_print_text);
Irssi::signal_add_last('notifylist joined', \&sig_notifylist_joined);
Irssi::signal_add_last('notifylist left', \&sig_notifylist_left);
Irssi::signal_add_last('server connected', \&sig_server_connected);
Irssi::signal_add_last('server disconnected', \&sig_server_disconnected);
Irssi::signal_add_last('channel topic changed', \&sig_channel_topic_changed);
Irssi::signal_add_last('dcc request', \&sig_dcc_request);
Irssi::signal_add_last('dcc closed', \&sig_dcc_closed);