-
Notifications
You must be signed in to change notification settings - Fork 6
/
prowlnotify.pl
211 lines (173 loc) · 4.75 KB
/
prowlnotify.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
use strict;
use warnings;
#####
#
# Version history
#
# 0.4
# Push hilights
# 0.3
# Add ability to toggle prowl mode (on/off/auto)
# 0.2
# Modify to use new API keys, much better!
# 0.1
# Initial working version
#
# irssi imports
use Irssi;
use Irssi::Irc;
use vars qw($VERSION %IRSSI %config);
use LWP::UserAgent;
$VERSION = "0.2";
%IRSSI = (
authors => "Denis Lemire",
contact => "denis\@lemire.name",
name => "prowl",
description => "Sets nick away when client discconects from the "
. "irssi-proxy sends messages to an iPhone via prowl.",
license => "GPLv2",
url => "http://www.denis.lemire.name",
);
$config{away_level} = 0;
$config{awayreason} = 'Auto-away because client has disconnected from proxy.';
$config{mode} = 'auto';
$config{debug} = 0;
$config{clientcount} = 0;
sub debug
{
if ($config{debug}) {
my $text = shift;
my $caller = caller;
Irssi::print('From ' . $caller . ":\n" . $text);
}
}
sub send_prowl
{
my ($event, $text) = @_;
if ($config{mode} eq 'off') {
debug("Not sending notification, prowl is off.");
return;
}
debug("Sending prowl");
my %options = ();
$options{'application'} ||= "Irssi";
$options{'event'} = $event;
$options{'notification'} = $text;
$options{'priority'} ||= 0;
# Get the API key from STDIN if one isn't provided via a file or from the command line.
if (open(APIKEYFILE, $ENV{HOME} . "/.prowlkey")) {
$options{apikey} = <APIKEYFILE>;
chomp $options{apikey};
close(APIKEYFILE);
} else {
debug ("Unable to open prowl key file\n");
}
# URL encode our arguments
$options{'application'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
$options{'event'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
$options{'notification'} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
# Generate our HTTP request.
my ($userAgent, $request, $response, $requestURL);
$userAgent = LWP::UserAgent->new;
$userAgent->agent("ProwlScript/1.0");
$requestURL = sprintf("https://prowlapp.com/publicapi/add?apikey=%s&application=%s&event=%s&description=%s&priority=%d",
$options{'apikey'},
$options{'application'},
$options{'event'},
$options{'notification'},
$options{'priority'});
$request = HTTP::Request->new(GET => $requestURL);
$response = $userAgent->request($request);
if ($response->is_success) {
debug ("Notification successfully posted.\n");
} elsif ($response->code == 401) {
debug ("Notification not posted: incorrect API key.\n");
} else {
debug ("Notification not posted: " . $response->content . "\n");
}
}
sub client_connect
{
my $client = shift;
my $server = $client->{server};
$config{clientcount}++;
debug("Client connected.");
if ($server->{usermode_away}) {
$server->send_raw('AWAY :');
}
}
sub client_disconnect
{
my $client = shift;
my $server = $client->{server};
debug('Client Disconnectted');
$config{clientcount}-- unless $config{clientcount} == 0;
unless ($server->{usermode_away}) {
# we are not away on this server already.. set the autoaway
# reason
$server->send_raw(
'AWAY :' . $config{awayreason}
);
}
}
sub msg_pub
{
my ($server, $data, $nick, $mask, $target) = @_;
if (($server->{usermode_away} == "1" || $config{mode} eq 'on') && ($data =~ /$server->{nick}/i)) {
debug("Got pub msg with my name");
send_prowl ("Mention", $nick . ': ' . $data);
}
}
sub msg_pri
{
my ($server, $data, $nick, $address) = @_;
if ($server->{usermode_away} == "1" || $config{mode} eq 'on') {
send_prowl ("Private msg", $nick . ': ' . $data);
}
}
sub msg_hilight
{
my ($dest, $text, $stripped) = @_;
my $server = $dest->{server};
if ($dest->{level} & MSGLEVEL_HILIGHT) {
if ($server->{usermode_away} == "1" || $config{mode} eq 'on') {
send_prowl ("Highlight", $stripped);
}
}
}
sub cmd_prowl
{
my ($args, $server, $winit) = @_;
$args = lc($args);
if (
$args =~ /^auto$/ ||
$args =~ /^on$/ ||
$args =~ /^off$/
) {
if ($args eq $config{mode}) {
Irssi::print("Prowl mode already $args");
} else {
Irssi::print("Prowl mode: $args (was " . $config{mode} . ')' );
$config{mode} = $args;
}
} elsif ($args =~/^test$/) {
Irssi::print("Sending test prowl notification");
send_prowl ("Test", "If you can read this, it worked.");
} elsif ($args =~/^debug$/) {
if ($config{debug}) {
$config{debug} = 0;
Irssi::print("Prowl debug disabled");
} else {
$config{debug} = 1;
Irssi::print("Prowl debug enabled");
}
} else {
Irssi::print('Prowl: Say what?!');
}
}
Irssi::signal_add_last('proxy client connected', 'client_connect');
Irssi::signal_add_last('proxy client disconnected', 'client_disconnect');
Irssi::signal_add_last('message public', 'msg_pub');
Irssi::signal_add_last('message private', 'msg_pri');
Irssi::command_bind 'prowl' => \&cmd_prowl;
Irssi::signal_add_last("print text", "msg_hilight");