Take white_list as command line arg.
[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
8
9 def uriCheck(elem, uri):
10     """
11     This is a hack for older versions of twisted words, we need to get rid of it.
12     """
13     if str(elem.toXml()).find('xmlns') == -1:
14         elem['xmlns'] = uri
15
16 class Service(service.Service):
17     """
18     Punjab generice service
19     """
20     def error(self, failure, body = None):
21         """
22         A Punjab error has occurred
23         """
24         # need a better way to trap this
25         if failure.getErrorMessage() != 'remote-stream-error':
26             log.msg('Punjab Error: ')
27             log.msg(failure.printBriefTraceback())
28             log.msg(body)
29         failure.raiseException()                
30         
31             
32     def success(self, result, body = None):
33         """
34         If success we log it and return result
35         """
36         log.msg(body)
37         return result
38
39
40
41 def makeService(config):
42     """
43     Create a punjab service to run
44     """
45     from twisted.web import  server, resource, static
46     from twisted.application import service, internet
47
48     import httpb
49
50
51     serviceCollection = service.MultiService()
52
53     if config['html_dir']:
54         r = static.File(config['html_dir'])
55     else:
56         print "The html directory is needed."
57         return
58
59     if config['white_list']:
60         httpb.HttpbService.white_list = config['white_list'].split(',')
61
62     if config['httpb']:
63         b = httpb.HttpbService(config['verbose'], config['polling'])
64         if config['httpb'] == '':
65             r.putChild('http-bind', resource.IResource(b))
66         else:
67             r.putChild(config['httpb'], resource.IResource(b))
68
69     site  = server.Site(r)
70
71
72     if config['ssl']:
73         from twisted.internet import ssl
74         from OpenSSL import SSL
75         ssl_context = ssl.DefaultOpenSSLContextFactory(config['ssl_privkey'],
76                                                        config['ssl_cert'],
77                                                        SSL.SSLv23_METHOD,)
78         sm = internet.SSLServer(int(config['port']),
79                                 site,
80                                 ssl_context,
81                                 backlog = int(config['verbose']))
82         sm.setServiceParent(serviceCollection)
83     else:
84         sm = internet.TCPServer(int(config['port']), site)
85         
86         sm.setServiceParent(serviceCollection)
87
88     return sm
89