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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import getpass
import json
import os
import sys
import cherrypy
__author__ = 'Simeon Simeonov'
__version__ = '1.0-beta'
__license__ = "GPL3"
BEINC_OSD_TYPE_NONE = 0
BEINC_OSD_TYPE_PYNOTIFY = 1
class BEINCInstance(object):
"""
Represents a single server-instance
"""
def __init__(self, instance_dict):
"""
instance_dict: the config-dictionary node that represents this instance
"""
self.__message_queue = list()
self.__osd_type = BEINC_OSD_TYPE_NONE
self.__osd_notification = None
try:
self.__name = instance_dict['name']
self.__password = instance_dict['password']
self.__queue_size = int(instance_dict['queue_size'])
except Exception as e:
sys.stderr.write(
'Instance processing error {0}:\n{1}\n'.format(self.__name,
e))
sys.exit(1)
if instance_dict['osd_system'].lower() == 'pynotify':
if not pynotify:
sys.stderr.write(
'This server does not possess pynotify capability\n')
sys.stderr.write(
'Remove the instance {0}'.format(self.__name))
sys.stderr.write("or define it with 'osd_system': 'none'\n")
sys.exit(1)
try:
self.__osd_notification = pynotify.Notification(' ')
self.__osd_notification.set_timeout(
instance_dict['osd_timeout'])
self.__osd_notification.set_property(
'app_name',
'{0} {1}'.format(sys.argv[0], __version__))
except Exception as e:
sys.stderr.write(
'Unable to set up a notification object for {0} ({1})\n')
sys.exit(1)
self.__osd_type = BEINC_OSD_TYPE_PYNOTIFY
@property
def name(self):
"""
name-property for the server instance
"""
return self.__name
@property
def queueable(self):
"""
True if this instance has a queueing capability
"""
return bool(self.__queue_size)
def password_match(self, password):
"""
Returns True if 'passowrd' matches the instance-password,
otherwise - False
"""
return True if self.__password == password else False
def send_message(self, title, message):
"""
Displays or enqueues the message,
depending on the instance's type in regard to the osd_system
"""
if self.__osd_type == BEINC_OSD_TYPE_PYNOTIFY:
self.__send_pynotify_messaage(title, message)
else:
self.__send_message_to_queue(title, message)
def get_queue(self):
"""
Reruens a json representation of the message queue
"""
jstr = json.dumps(self.__message_queue)
self.__message_queue = list()
return jstr
def __send_pynotify_messaage(self, title, message):
"""
Displays pynotify message
"""
self.__osd_notification.set_properties(summary=title, body=message)
self.__osd_notification.show()
def __send_message_to_queue(self, title, message):
"""
Enqueues the message
"""
if len(self.__message_queue) >= self.__queue_size:
self.__message_queue.pop(0)
self.__message_queue.append({'title': title, 'message': message})
def beinc_instance_login(method):
"""
decorator for checking login credentials
"""
from functools import wraps
@wraps(method)
def tmp_func(self, *args, **kwargs):
if not args:
raise cherrypy.HTTPError(400)
print('args: {0}'.format(args))
print('kwargs: {0}'.format(kwargs))
print(cherrypy.request.config)
print('args[0]: {0} ({1})'.format(args[0], str(type(args[0]))))
print('instances: {0}'.format(str(self.__instances)))
try:
instance = self.__instances[args[0]]
except Exception as e:
sys.stderr.write('Wrong instance or password: {0}\n'.format(e))
raise cherrypy.HTTPError('403 Forbidden',
'Wrong instance or password')
if not instance.password_match(kwargs.get('password')):
raise cherrypy.HTTPError('403 Forbidden',
'Wrong instance or password')
return method(self, *args, **kwargs)
return tmp_func
class WebNotifyServer(object):
def __init__(self, config):
"""
"""
self.__config = config
self.__instances = dict()
try:
for instance in self.__config['server']['instances']:
self.__instances[instance['name']] = BEINCInstance(instance)
print('Instance "{0}" added'.format(instance['name']))
except Exception as e:
sys.stderr.write('Unable to initialize queues: {0}\n'.format(e))
sys.exit(1)
@cherrypy.expose
def index(self):
"""
default dispatcher
"""
return 'index'
@cherrypy.expose
def default(self, *args):
"""
default dispatcher
"""
return 'default'
@cherrypy.expose
def push(self, *args, **kwargs):
print('push called')
if not args:
raise cherrypy.HTTPError(400)
# print('args: {0}'.format(args))
# print('kwargs: {0}'.format(unicode(kwargs)))
# print(cherrypy.request.config)
# print('args[0]: {0} ({1})'.format(args[0], str(type(args[0]))))
# print('instances: {0}'.format(str(self.__instances)))
try:
instance = self.__instances[args[0]]
except Exception as e:
sys.stderr.write('Wrong instance or password\n')
#print('DEBUG: {0}'.format(e))
raise cherrypy.HTTPError('403 Forbidden',
'Wrong instance or password')
if not instance.password_match(kwargs.get('password')):
sys.stderr.write('Wrong instance or password\n')
raise cherrypy.HTTPError('403 Forbidden',
'Wrong instance or password')
# instance = self.__instances[args[0]]
title = kwargs.get('title', '')
message = kwargs.get('message', '')
try:
instance.send_message(title, message)
return 'OK'
except Exception as e:
sys.stderr.write(
'Unable to handle message in {0}: ({1})\n'.format(
instance.name,
e))
raise cherrypy.HTTPError(500, 'Unable to send message')
@cherrypy.expose
@beinc_instance_login
def pull(self, *args, **kwargs):
instance = self.__instances(args[0])
return 'OK'
def main():
parser = argparse.ArgumentParser(
description='The following options are available')
parser.add_argument('-d',
action='store_true',
dest='daemonize',
default=False,
help='daemonize the server process')
parser.add_argument('-H', '--hostname',
metavar='HOSTNAME',
type=str,
dest='hostname',
default='127.0.0.1',
help="Server IP / hostname")
parser.add_argument('-p', '--port',
metavar='PORT',
type=int,
dest='port',
default=9998,
help="Server port")
parser.add_argument('-c', '--config-file',
metavar='FILE',
type=str,
default=os.path.expanduser('~/.beinc_server.json'),
dest='config_file',
help="config file")
parser.add_argument('-v', '--version',
action='version',
version='%(prog)s {0}'.format(__version__),
help='display program-version and exit')
args = parser.parse_args()
try:
with open(args.config_file, 'r') as fp:
config_dict = json.load(fp)
except Exception as e:
sys.stderr.write('Unable to parse {0}: {1}'.format(args.config_file,
e))
sys.exit(1)
cherrypy.config.update({
'server.socket_host': args.hostname,
'server.socket_port': args.port,
'server.ssl_module': config_dict['server']['general']['ssl_module'].encode('utf-8'),
'server.ssl_certificate': config_dict['server']['general']['ssl_certificate'],
'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'],
'tools.encode.on': True,
'tools.encode.encoding': 'utf-8'
})
global pynotify
try:
import pynotify
if not pynotify.init('BEINC Notify'):
sys.stderr.write('pynotify.init failed! Exiting...\n')
sys.exit(1)
except Exception as e:
sys.stderr.write(
'Notice: pynotify support unavailable ({0})\n'.format(e))
pynotify = False
try:
cherrypy.quickstart(WebNotifyServer(config_dict))
except Exception as e:
sys.stderr.write("WebServer error: {0}".format(e))
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()
|