-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkanouchd.py
executable file
·86 lines (65 loc) · 2.34 KB
/
kanouchd.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
#!/usr/bin/env python
# encoding=utf-8
# maintainer: rgaudin
''' webserver expecting identity and text to store incoming sms in CouchDB '''
import sys
from datetime import datetime
import logging as log
import cherrypy
import couchdb
from config import *
log.basicConfig(level=log.INFO, filename=KANOUCHD_LOG_FILE)
def document_from_sms(identity, text, date=datetime.now()):
''' returns dictionary matching CouchDB and RSMS backend format '''
document = {'identity': identity,
'datetime': date.isoformat(),
'text': text,
'direction': 'incoming',
'status': 'created'}
return document
def save_document_to_couch(document):
''' saves an arbitrary document to CouchDB '''
# connect DB upon message reception.
# this is not very good for performance
# but it allows CouchDB restarts without effects.
couch = couchdb.Server(COUCH_SERVER)
database = couch[COUCH_DB]
# create a Couch document and save it to DB.
doc_id, doc_rev = database.save(document)
doc = database[doc_id]
return doc_id
class SMSReceiver:
@cherrypy.expose
def index(self):
return "SMS receiver working."
@cherrypy.expose
def new(self, identity=None, text=None):
# we don't give a shit about empty or anonymous message
if not identity or not text:
return
document = document_from_sms(identity, text, date=datetime.now())
try:
doc = save_document_to_couch(document)
except:
# I believe this should prevent kannel from discarding the
# incoming sms and add it to retry spool.
log.critical("can't connect or create document on CouchDB.")
raise cherrypy.HTTPError(500, u"Server could not create message" \
" into CouchDB.")
return doc
def main():
cherrypy.quickstart(SMSReceiver(), \
'/', \
{'global': {'server.socket_port': KANOUCHD_PORT}})
if __name__ == '__main__':
try:
log.info("started.")
main()
log.info("soft shutdown.")
shutdown()
except KeyboardInterrupt:
log.warning("shutdown with ^C.")
shutdown()
except Exception, e:
log.error("shutdown by exception: %r" % e)
shutdown()