forked from juergh/hipchat-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hipchat-notifications.pl
269 lines (211 loc) · 7.69 KB
/
hipchat-notifications.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
#!/usr/bin/perl
#
# Copyright (C) 2013 Juerg Haefliger <juergh@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# 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 Pidgin;
use Purple;
%PLUGIN_INFO = (
perl_api_version => 2,
name => "HipChat Notifications",
version => "0.2.0",
summary => "Customized notifications for HipChat rooms",
description => "",
author => "Juerg Haefliger <juergh\@gmail.com",
url => "https://github.com/juergh/hipchat-notifications",
load => "plugin_load",
unload => "plugin_unload",
prefs_info => "prefs_info_cb"
);
$PLUGIN_NAME = "hipchat-notifications";
$PLUGIN_PREFS = "/plugins/core/" . $PLUGIN_NAME;
%CONV_TITLE = ();
%CONV_COUNT = ();
%CONV_NOTIFY = ();
sub plugin_init {
return %PLUGIN_INFO;
}
sub prefs_info_cb {
my ($frame, $ppref);
Purple::Debug::info($PLUGIN_NAME, "prefs_info_cb()\n");
# Initialize the frame
$frame = Purple::PluginPref::Frame->new();
# Visual notifications
$ppref = Purple::PluginPref->new_with_label("Visual Notifications");
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/visual-alias", "Color \@<alias> messages");
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/visual-alias-color", "Color");
$ppref->set_type(2);
$ppref->set_max_length(7);
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/visual-all", "Color \@all messages");
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/visual-all-color", "Color");
$ppref->set_type(2);
$ppref->set_max_length(7);
$frame->add($ppref);
# Audible notifications
$ppref = Purple::PluginPref->new_with_label("Audible Notifications");
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/audible-alias", "Play sound for \@<alias> messages");
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/audible-alias-sound", "Sound file");
$ppref->set_type(2);
$ppref->set_max_length(64);
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/audible-all", "Play sound for \@all messages");
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label($PLUGIN_PREFS .
"/audible-all-sound", "Sound file");
$ppref->set_type(2);
$ppref->set_max_length(64);
$frame->add($ppref);
return $frame;
}
sub enable_notifications_cb {
my ($conv) = @_;
my $name = $conv->get_name();
Purple::Debug::info($PLUGIN_NAME, "enable notifications for " . $name .
"\n");
$CONV_NOTIFY{$name} = 1;
return false;
}
sub conversation_switched_cb {
my ($conv) = @_;
my $name = $conv->get_name();
Purple::Debug::info($PLUGIN_NAME, "conversation switched to " . $name .
"\n");
# Reset the conversation title and count
$conv->set_title($CONV_TITLE{$name});
$CONV_COUNT{$name} = 0;
}
sub conversation_created_cb {
my ($conv, $plugin) = @_;
my $name = $conv->get_name();
my $title = $conv->get_title();
Purple::Debug::info($PLUGIN_NAME, "conversation " . $name . " (" . $title .
") created\n");
# Save the original conversation title and reset the count
$CONV_TITLE{$name} = $title;
$CONV_COUNT{$name} = 0;
$CONV_NOTIFY{$name} = 0;
# Enable notifications in 5 seconds
Purple::timeout_add($plugin, 5, \&enable_notifications_cb, $conv);
}
sub receiving_chat_msg_cb {
my ($account, $sender, $message, $conv, $flags, $plugin) = @_;
my ($all, $nick, $quoted);
my $name = $conv->get_name();
my $color = 0;
my $sound = 0;
my $count = 0;
# Check if this is a HipChat account
if ($account->get_username() =~ m|chat.hipchat.com/xmpp$|) {
# Check if the message was sent to all
$all = "\@all";
$quoted = quotemeta($all);
if ($message =~ /(^|\s)$quoted($|\s)/) {
Purple::Debug::info($PLUGIN_NAME, "message for " . $quoted .
" received\n");
if (Purple::Prefs::get_bool($PLUGIN_PREFS . "/visual-all")) {
$color = Purple::Prefs::get_string($PLUGIN_PREFS .
"/visual-all-color");
}
if (Purple::Prefs::get_bool($PLUGIN_PREFS . "/audible-all")) {
$sound = Purple::Prefs::get_string($PLUGIN_PREFS .
"/audible-all-sound");
}
$count = 1;
goto DONE;
}
# Check if the message was sent to me
$alias = "\@" . $account->get_alias();
$alias =~ s/\s//g;
$quoted = quotemeta($alias);
if ($message =~ /(^|\s)$quoted($|\s)/) {
Purple::Debug::info($PLUGIN_NAME, "message for " . $quoted .
" received\n");
if (Purple::Prefs::get_bool($PLUGIN_PREFS . "/visual-alias")) {
$color = Purple::Prefs::get_string($PLUGIN_PREFS .
"/visual-alias-color");
}
if (Purple::Prefs::get_bool($PLUGIN_PREFS . "/audible-alias")) {
$sound = Purple::Prefs::get_string($PLUGIN_PREFS .
"/audible-alias-sound");
}
$count = 1;
goto DONE;
}
}
DONE:
# Color the message
if ($color) {
$message = "<font color=" . $color . ">" . $message . "</font>";
}
# Update the conversation title and play a sound if notifications are
# enabled and the conversation doesn't have the focus
if ($CONV_NOTIFY{$name} && !$conv->has_focus()) {
# Update the conversation title and count
if ($count) {
$CONV_COUNT{$name}++;
$conv->set_title("(" . $CONV_COUNT{$name}.") " .
$CONV_TITLE{$name});
}
# Play a sound
if ($sound) {
system("/usr/bin/canberra-gtk-play -l 1 -V 10.0 -f " . $sound .
" &");
}
}
@_[2] = $message;
return false;
}
sub plugin_load {
my ($plugin) = @_;
my ($conv_handle);
Purple::Debug::info($PLUGIN_NAME, "plugin_load()\n");
# Initialize the preferences
Purple::Prefs::add_none($PLUGIN_PREFS);
Purple::Prefs::add_bool($PLUGIN_PREFS . "/visual-alias", 1);
Purple::Prefs::add_string($PLUGIN_PREFS . "/visual-alias-color", "#c00000");
Purple::Prefs::add_bool($PLUGIN_PREFS . "/visual-all", 1);
Purple::Prefs::add_string($PLUGIN_PREFS . "/visual-all-color", "#008000");
Purple::Prefs::add_bool($PLUGIN_PREFS . "/audible-alias", 1);
Purple::Prefs::add_string($PLUGIN_PREFS . "/audible-alias-sound",
"/usr/share/sounds/gnome/default/alerts/bark.ogg");
Purple::Prefs::add_bool($PLUGIN_PREFS . "/audible-all", 1);
Purple::Prefs::add_string($PLUGIN_PREFS . "/audible-all-sound",
"/usr/share/sounds/gnome/default/alerts/bark.ogg");
# Register the Purple callbacks
$conv_handle = Purple::Conversations::get_handle();
Purple::Signal::connect($conv_handle, "receiving-chat-msg", $plugin,
\&receiving_chat_msg_cb, 0);
Purple::Signal::connect($conv_handle, "conversation-created", $plugin,
\&conversation_created_cb, $plugin);
# Register the Pidgin callbacks
$conv_handle = Pidgin::Conversations::get_handle();
Purple::Signal::connect($conv_handle, "conversation-switched", $plugin,
\&conversation_switched_cb, 0);
}
sub plugin_unload {
my ($plugin) = @_;
Purple::Debug::info($PLUGIN_NAME, "plugin_unload()\n");
}