-
Notifications
You must be signed in to change notification settings - Fork 0
/
weerestnotify.py
170 lines (130 loc) · 4.68 KB
/
weerestnotify.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
import json
import re
import time
import weechat as w
import hmac
import hashlib
import base64
from urllib.parse import urlencode
from cryptography.fernet import Fernet
import base64
SCRIPT_NAME = 'weerestnotify'
SCRIPT_AUTHOR = 'patali'
SCRIPT_VERSION = '0.1'
SCRIPT_LICENSE = 'MIT'
SCRIPT_DESC = 'Push notifications from WeeChat to a rest api endpoint'
help_text = '''
[commands]
show_config Shows your current configuration details
test Sends "Hello world" message to the end point
[settings]: (prefix with plugins.var.python.weerestnotify.<setting>)
api_key API Key used for signature verification (required)
encryption_key Shared key used for encryption of the notification messages (required)
end_point_url Endpoint where the message has to be sent to (required)
'''
configs = {
'api_key': '_required',
'encryption_key': '_required',
'end_point_url': '_required',
}
def register():
w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', '')
def log(msg):
w.prnt('', '[weerestnotify] debug: {}'.format(str(msg)))
def load_settings():
for (option, default_value) in configs.items():
if w.config_get_plugin(option) == '':
if configs[option] == '_required':
log('missing plugins.var.python.weerestnotify.{}'.format(option))
else:
w.config_set_plugin(option, configs[option])
def setup_hooks():
global SCRIPT_NAME
global SCRIPT_DESC
global help_text
w.hook_print('', '', '', 1, 'message_hook', '')
w.hook_command(SCRIPT_NAME, SCRIPT_DESC, '[command] show_config/test', help_text, '', 'handle_argument', '')
def get_channels(kind):
channels = w.config_get_plugin('{}_channels'.format(kind)).strip()
if channels == '':
return set([])
else:
return set([channel.strip() for channel in channels.split(' ') if channel])
def handle_argument(data, buffer, args):
if(args == 'show_config'):
w.prnt('', 'Show config')
elif(args == 'test'):
send_push('weerestnotify', 'This is a test message from weerestnotify notification plugin')
else:
w.prnt('', help_text)
return w.WEECHAT_RC_OK
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
if is_ignored(bufferp) and regular_channel:
log('ignored regular channel skipping')
return w.WEECHAT_RC_OK
if not is_displayed:
log('not a displayed message skipping')
return w.WEECHAT_RC_OK
if not is_highlighted and regular_channel:
return w.WEECHAT_RC_OK
log('passed all checks')
if is_pm:
title = 'Private message from {}'.format(prefix)
else:
title = 'Message on {} from {}'.format(get_buf_name(bufferp), prefix)
send_push(title=title, message=message)
return w.WEECHAT_RC_OK
def http_request_callback(data, url, status, response, err):
j = json.loads(response)
if j['ok'] != True:
w.prnt('', '[weerestnotify] error: {}'.format(response))
return w.WEECHAT_RC_ERROR
return w.WEECHAT_RC_OK
def encrypt(data):
key = w.config_get_plugin('encryption_key')
fernet = Fernet(key)
clean_data = data
if type(clean_data) != 'str':
clean_data = str(data)
encrypted_data = fernet.encrypt(bytes(clean_data, 'utf-8'))
return base64.b64encode(encrypted_data)
def send_push(title, message):
# encrypt the messages
title_enc = encrypt(title)
text_enc = encrypt(message)
# Send the message to endpoint
postfields = {
'signature': encrypt(w.config_get_plugin('api_key')),
'title': title_enc,
'text': text_enc
}
w.hook_process_hashtable(
'url: %s' % w.config_get_plugin('end_point_url'),
{
"httpheader" : "\n".join([
"Content-Type: application/x-www-form-urlencoded"
]),
"postFields": urlencode(postfields)
},
20*1000,
'http_request_callback',
''
)
w.prnt('', '[weerestnotify] notification sent')
def main():
register()
load_settings()
setup_hooks()
main()