-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathweebullet.py
396 lines (309 loc) · 11.2 KB
/
weebullet.py
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
#!/usr/bin/env python3
import json
import re
import urllib.request, urllib.parse, urllib.error
import time
import weechat as w
description = "weebullet pushes notifications from weechat to pushbullet"
help_text = """
commands:
/weebullet ignore adds to the list of ignored channels
/weebullet unignore removes from the list of ignored channels
/weebullet subscribe adds to the list of subscribed channels
/weebullet unsubscribe removes from the list of subscribed channels
/weebullet listignored list ignored channels
/weebullet listsubscribed list subscribed channels
/weebullet listdevices list devices associated with your pushbullet API key
/weebullet test send a test notification to the devices in notify_devices
/weebullet help prints this help message
examples:
/weebullet ignore #foo,#bar,#baz
/weebullet subscribe #foo,#bar
/weebullet listignored
/weebullet listdevices
settings: (prefix with plugins.var.python.weebullet.<setting>)
api_key your pushbullet API key (required)
away_only send only when marked as away
value: [0|1], default: 1
inactive_only send only when the message is in an inactive buffer
value: [0|1], default: 0
ignore_on_relay ignore notifications when a relay is connected
value: [0|1], default: 0
notify_devices list of device identifiers to notify
value: comma separated device identifiers, default: all
min_notify_interval minimum number of seconds to wait before another notification
value: integer number of seconds, default: 60
api_timeout number of seconds to api request timeout
value: integer number of seconds, default: 20
"""
configs = { # some sane defaults
'api_key': '_required',
'ignored_channels': '', # no ignored channels
'subscribed_channels': '', # no subscribed channels
'away_only': '1', # send only when away
'inactive_only': '0', # send even if buffer is active
'ignore_on_relay': '0', # send even if relay is connected
'notify_devices': 'all', # send to all devices
'min_notify_interval': '60', # send notifications at least a minute apart
'api_timeout': '20', # 20 seconds ought to be enough
'debug': '0', # enable debugging
}
last_notification = 0
def debug(msg):
if str(w.config_get_plugin('debug')) != '0':
w.prnt('', '[weebullet] debug: {}'.format(str(msg)))
def register():
w.register('weebullet', 'Lefty', '0.6', 'BSD', description, '', '')
def load_settings():
for (option, default_value) in list(configs.items()):
if w.config_get_plugin(option) == '':
if configs[option] == '_required':
w.prnt('', 'missing plugins.var.python.weebullet.{}'.format(option))
else:
w.config_set_plugin(option, configs[option])
def setup_hooks():
global description
global help_text
w.hook_print('', '', '', 1, 'message_hook', '')
w.hook_command('weebullet', description, '[command]', help_text, '', 'process_command', '')
def process_devicelist_cb(
data,
url,
status,
response,
err,
):
try:
devices = json.loads(response)['devices']
w.prnt('', 'devices:')
for device in devices:
if device['pushable']:
if 'nickname' in device:
device_name = device['nickname']
else:
device_name = '<unnamed>'
w.prnt('', ' {}: {}'.format(device_name, device['iden']))
except KeyError:
w.prnt('', '[weebullet] error accessing devices: {}'.format(response))
return w.WEECHAT_RC_ERROR
return w.WEECHAT_RC_OK
def get_api_key():
return w.string_eval_expression(w.config_get_plugin('api_key'), {}, {}, {})
def get_devices():
api_key = get_api_key()
apiurl = 'https://{}@api.pushbullet.com/v2/devices'.format(api_key)
w.hook_process(
'url:' + apiurl,
int(w.config_get_plugin('api_timeout'))*1000,
'process_devicelist_cb',
'',
)
return ""
def get_channels(kind):
channels = w.config_get_plugin('{}_channels'.format(kind))
if channels == '':
return set([])
else:
return set([channel.strip() for channel in channels.split(' ')])
def process_command(data, buffer, args):
# process the following commands:
# /weebullet ignore CHANNELS
# /weebullet subscribe CHANNELS
for kind in ['ignore', 'subscribe']:
channels = get_channels(kind + "d")
action_commands = {
kind: lambda channel: channels.add(channel),
'un{}'.format(kind): lambda channel: channels.remove(channel),
}
for command in action_commands:
command_regex = re.match('^' + command + '\s+(.+)', args)
if command_regex is not None:
new_channels = command_regex.group(1).split(' ')
for new_channel in new_channels:
try:
action_commands[command](new_channel)
except Exception as e:
pass
w.config_set_plugin('{}_channels'.format(kind + "d"), ' '.join(channels))
return w.WEECHAT_RC_OK
# process the following commands:
# /weebullet listignored
# /weebullet listsubscribed
# /weebullet listdevices
list_commands = {
'listignored': lambda: 'Ignored: {}'.format(w.config_get_plugin('ignored_channels')),
'listsubscribed': lambda: 'Subscribed: {}'.format(w.config_get_plugin('subscribed_channels')),
'listdevices': lambda: get_devices(),
}
for command in list_commands:
if args == command:
result = list_commands[command]()
if result:
w.prnt( '', result)
return w.WEECHAT_RC_OK
# process the following commands:
# /weebullet test
if args == 'test':
send_push(
title='Test push notification',
body='Test push notification from weebullet',
)
return w.WEECHAT_RC_OK
# process the following:
# /weebullet help
# /weebullet <any uncaught command>
w.prnt('', help_text)
return w.WEECHAT_RC_OK
def process_pushbullet_cb(
data,
url,
status,
response,
err,
):
body = None
headers = {}
lines = response.rstrip().splitlines()
status_code = int(lines.pop(0).split()[1])
for line in lines:
if body == '':
body += line
continue
header_line = line.split(':', 2)
if len(header_line) != 2:
body = ''
continue
headers[header_line[0].strip()] = header_line[1].strip()
# response is the string of http body
if status == w.WEECHAT_HOOK_PROCESS_ERROR:
w.prnt(
'',
'[weebullet] error sending to pushbullet: {} - {}'.format(
status,
url,
),
)
return w.WEECHAT_RC_ERROR
if status_code == 401 or status_code == 403:
w.prnt(
'',
'[weebullet] invalid API key: {}'.format(get_api_key()),
)
return w.WEECHAT_RC_ERROR
if status_code != 200:
w.prnt(
'',
'[weebullet] Error sending to pushbullet: {} - {} - {}'.format(
url,
status_code,
body,
),
)
return w.WEECHAT_RC_ERROR
return w.WEECHAT_RC_OK
def away_only_check(bufferp):
if w.config_get_plugin('away_only') != '1':
return False
return not w.buffer_get_string(bufferp, 'localvar_away')
def inactive_only_check(bufferp):
if w.config_get_plugin('inactive_only') != '1':
return False
return w.current_buffer() == bufferp
def interval_limit_check():
interval = w.config_get_plugin('min_notify_interval')
if interval is None or interval == '':
return False
try:
interval = int(interval)
except ValueError:
w.prnt('', '[weebullet] min_notify_interval not an integer')
return False
global last_notification
earliest_allowed = last_notification + interval
return time.time() < earliest_allowed
def relay_check():
check_relays = w.config_string_to_boolean(w.config_get_plugin('ignore_on_relay'))
if not check_relays:
return False
infolist = w.infolist_get('relay', '', '')
if infolist:
while w.infolist_next(infolist):
status = w.infolist_string(infolist, 'status_string')
if status == 'connected':
return True
w.infolist_free(infolist)
return False
def get_buf_name(bufferp):
short_name = w.buffer_get_string(bufferp, 'short_name')
name = w.buffer_get_string(bufferp, 'name')
return (short_name or name)
def is_ignored(bufferp):
buf_name = get_buf_name(bufferp)
return buf_name in get_channels('ignored')
def is_subscribed(bufferp):
buf_name = get_buf_name(bufferp)
return buf_name in get_channels('subscribed')
def message_hook(
data,
bufferp,
uber_empty,
tagsn,
is_displayed,
is_highlighted,
prefix,
message,
):
is_pm = w.buffer_get_string(bufferp, 'localvar_type') == 'private'
regular_channel = not is_subscribed(bufferp) and not is_pm
skip = False
skip = skip or away_only_check(bufferp)
skip = skip or inactive_only_check(bufferp)
skip = skip or interval_limit_check()
skip = skip or relay_check()
skip = skip or (is_ignored(bufferp) and regular_channel)
skip = skip or (not is_displayed)
skip = skip or (not is_highlighted and regular_channel)
if skip:
return w.WEECHAT_RC_OK
if is_pm:
title = 'PM from {}'.format(prefix)
else:
title = 'Message on {} from {}'.format(get_buf_name(bufferp), prefix)
send_push(title=title, body=message)
return w.WEECHAT_RC_OK
def send_push(title, body):
global last_notification
api_key = get_api_key()
apiurl = 'https://{}@api.pushbullet.com/v2/pushes'.format(api_key)
timeout = int(w.config_get_plugin('api_timeout')) * 1000
if len(title) != 0 or len(body) != 0:
deviceiden = w.config_get_plugin('devices')
last_notification = time.time()
if deviceiden == 'all':
payload = urllib.parse.urlencode({
'type': 'note',
'title': title,
'body': body.encode('utf-8')
})
else:
payload = urllib.parse.urlencode({
'type': 'note',
'title': title,
'body': body.encode('utf-8'),
'devices': deviceiden,
})
w.hook_process_hashtable(
'url:' + apiurl,
{
'postfields': payload,
'header': '1',
},
timeout,
'process_pushbullet_cb',
'',
)
def main():
register()
load_settings()
setup_hooks()
main()