Report the original stream:error stanza to clients.
[punjab-krb5-preauth] / punjab / __init__.py
1 """
2 Punjab - multiple http interfaces to jabber.
3
4 """
5 from twisted.python import log
6 from twisted.application import service
7 import twisted_patches
8
9
10 def uriCheck(elem, uri):
11     """
12     This is a hack for older versions of twisted words, we need to get rid of it.
13     """
14     if str(elem.toXml()).find('xmlns') == -1:
15         elem['xmlns'] = uri
16
17
18 class PunjabService(service.MultiService):
19     """Punjab parent service"""
20
21     httpb = None
22
23     def startService(self):
24         return service.MultiService.startService(self)
25
26     def stopService(self):
27         def cb(result):
28             return service.MultiService.stopService(self)
29
30         d = self.httpb.stopService()
31         d.addCallback(cb).addErrback(log.err)
32         return d
33
34 class Service(service.Service):
35     """
36     Punjab generice service
37     """
38     def error(self, failure, body = None):
39         """
40         A Punjab error has occurred
41         """
42         # need a better way to trap this
43         if failure.getErrorMessage() != 'remote-stream-error':
44             log.msg('Punjab Error: ')
45             log.msg(failure.printBriefTraceback())
46             log.msg(body)
47         failure.raiseException()                
48         
49             
50     def success(self, result, body = None):
51         """
52         If success we log it and return result
53         """
54         log.msg(body)
55         return result
56
57
58
59 def makeService(config):
60     """
61     Create a punjab service to run
62     """
63     from twisted.web import  server, resource, static
64     from twisted.application import service, internet
65
66     import httpb
67
68
69     serviceCollection = PunjabService()
70
71     if config['html_dir']:
72         r = static.File(config['html_dir'])
73     else:
74         print "The html directory is needed."
75         return
76
77     if config['white_list']:
78         httpb.HttpbService.white_list = config['white_list'].split(',')
79
80     if config['black_list']:
81         httpb.HttpbService.black_list = config['black_list'].split(',')
82
83     if config['httpb']:
84         b = httpb.HttpbService(config['verbose'], config['polling'])
85         if config['httpb'] == '':
86             r.putChild('http-bind', resource.IResource(b))
87         else:
88             r.putChild(config['httpb'], resource.IResource(b))
89
90     site  = server.Site(r)
91
92
93     if config['ssl']:
94         from twisted.internet import ssl
95         from OpenSSL import SSL
96         ssl_context = ssl.DefaultOpenSSLContextFactory(config['ssl_privkey'],
97                                                        config['ssl_cert'],
98                                                        SSL.SSLv23_METHOD,)
99         sm = internet.SSLServer(int(config['port']),
100                                 site,
101                                 ssl_context,
102                                 backlog = int(config['verbose']))
103         sm.setServiceParent(serviceCollection)
104     else:
105         sm = internet.TCPServer(int(config['port']), site)
106         
107         sm.setServiceParent(serviceCollection)
108
109     serviceCollection.httpb = b
110
111     return serviceCollection
112