-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
87 lines (71 loc) · 2.53 KB
/
server.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
#!/usr/bin/env python2.7
import logging
import xmpp
import sys
import traceback
from urlparse import parse_qsl
logger = logging.getLogger('synology-xmpp')
try:
import config
except ImportError:
raise RuntimeError("no config")
class BadRequest(Exception):
pass
def send_xmpp_message(params, environ):
try:
user, dom = params['user'].strip().split('@')
password = params['password'].strip()
message = params['text'].strip()
except (ValueError, KeyError) as e:
raise BadRequest("Missing/invalid parameter: %s" % e)
dests = config.DESTINATIONS
if "to" in params and "@" in params.get('to', ''):
dests += (params['to'].strip(), )
for dst in dests:
logger.info('Request: user=%s, to=%s: %s'.format(user, dst, message))
try:
client = xmpp.Client(dom)
client.connect(server=config.XMPP_SERVER)
client.auth(user=user, password=password, sasl=0)
client.send(xmpp.Message(dst, message))
except Exception as e:
logger.warn(traceback.format_exc())
return 'ok'
def app(environ, start_response):
if environ.get('PATH_INFO', '').lstrip('/').rstrip('/') != '':
start_response('200 OK', [('Content-type', 'text/plain')])
return ['error 404']
try:
args = dict(parse_qsl(environ['QUERY_STRING']))
response = send_xmpp_message(args, environ)
except BadRequest, e:
logger.error('Error: %s' % e)
start_response('400 Bad Request', [('Content-type', 'text/plain')])
return ['%s' % e]
except Exception, e:
logger.error('Error: %s' % e)
raise
else:
logger.info('Success')
start_response('200 Ok', [('Content-type', 'text/plain')])
return [response]
def serve():
from wsgiref.simple_server import make_server
httpd = make_server('localhost', config.PORT, app)
httpd.serve_forever()
if __name__ == '__main__':
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
if len(sys.argv) == 2 and sys.argv[1] == '-d':
handler = logging.FileHandler(config.LOGFILE)
handler.setFormatter(formatter)
logger.addHandler(handler)
import daemon # doesn't work on Windows, btw.
with daemon.DaemonContext(files_preserve=[handler.stream]):
serve()
else:
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger.addHandler(handler)
serve()