a049e426d1a036f37201746bdbd21de3b2e3058d
[ipypbx] / src / ipypbx / http.py
1 # Copyright (c) Stas Shtin, 2010
2
3 # This file is part of IPyPBX.
4
5 # IPyPBX is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # IPyPBX is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with IPyPBX.  If not, see <http://www.gnu.org/licenses/>.
17
18 import xml.etree.ElementTree as etree
19 from PyQt4 import QtCore, QtNetwork
20
21
22 class FreeswitchConfigServer(QtNetwork.QTcpServer):
23     """
24     TCP server that receives config requests from freeswitch.
25     """
26     def __init__(self, window):
27         super(FreeswitchConfigServer, self).__init__(window)
28
29         self.host = None
30         self.port = None
31         self.connection_id = None
32         self.is_running = False
33         self.generators = [
34             GenClass(window, self) for GenClass in (
35                 SofiaConfGenerator,)]
36         
37         self.httpRequestParser = HttpRequestParser(self)
38         
39     def setSocketData(self, host, port, connection_id):
40         """
41         Set host and port for socket to listen on.
42
43         If the settings differ from previous values, server gets restarted.
44         """
45         # Check if restart is needed before new settings are applied.
46         needs_restart = (
47             (host, port) != (self.host, self.port)) and connection_id
48
49         # Save new settings.
50         self.host = host
51         self.port = port
52         if connection_id:
53             self.connection_id = connection_id
54
55         # Restart server if necessary.
56         if needs_restart:
57             self.restartServer()
58
59     def startServer(self):
60         """
61         Start listening on our socket.
62         """
63         if not self.is_running:
64             if self.host and self.port:
65                 self.newConnection.connect(self.clientConnecting)
66                 self.listen(QtNetwork.QHostAddress(self.host), self.port)
67                 self.is_running = True
68
69     def stopServer(self):
70         """
71         Stop listening on our socket.
72         """
73         if self.is_running:
74             self.close()
75             self.is_running = False
76
77     def restartServer(self):
78         """
79         Restart server.
80         """
81         self.stopServer()
82         self.startServer()
83
84     def clientConnecting(self):
85         """
86         Handle client connection.
87         """
88         if self.hasPendingConnections():
89             self.socket = self.nextPendingConnection()
90             self.socket.readyRead.connect(self.receiveData)
91
92     def receiveData(self):
93         # TODO: read in chunks.
94         for line in str(self.socket.readAll()).split('\r\n'):
95             self.httpRequestParser.handle(line)
96
97
98 class HttpParseError(Exception):
99     """
100     Error parsing HTTP request.
101     """
102
103
104 class HttpRequestParser(object):
105     """
106     A simple state machine for parsing HTTP requests.
107     """
108     HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE, \
109         HTTP_DONE = range(6)
110     HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE', 'DONE']
111     
112     def __init__(self, parent):
113         self.parent = parent
114         self.reset()
115
116     def reset(self):
117         """
118         Reset parser to initial state.
119         """
120         # Initial values for request data.
121         self.method = None
122         self.request_path = None
123         self.http_version = None
124         self.headers = {}
125         self.data = {}
126         
127         # Set initial state.
128         self.state = self.HTTP_NONE        
129
130     def handle(self, line):
131         """
132         Dispatch line to current state handler.
133         """
134         for state in self.HTTP_STATES:
135             if getattr(self, 'HTTP_%s' % state) == self.state:
136                 getattr(self, 'handle%s' % state.title())(line)
137                 break
138         else:
139             raise HttpParseError('Unknown HTTP state')
140                 
141     def handleNone(self, line):
142         """
143         Pass line to next state.
144         """
145         self.state += 1
146         self.handle(line)
147
148     def handleRequest(self, line):
149         """
150         Retrieve HTTP method, request path and HTTP version from request.
151         """
152         self.method, self.request_path, self.http_version = line.split(' ')
153         self.state += 1
154
155     def handleHeaders(self, line):
156         """
157         Parse headers while not found an empty line.
158         """
159         if line:
160             key, value = line.split(': ')
161             self.headers[key] = value
162         else:
163             self.state += 1
164             self.handle(line)
165
166     def handleEmpty(self, line):
167         """
168         Empty line separator is found - proceed to next state.
169         """
170         self.state += 1
171
172     def handleMessage(self, line):
173         """
174         Append to message body.
175         """
176         self.data = dict(pair.split('=', 2) for pair in line.split('&'))
177
178         for k, v in self.data.items():
179             print k, '=>', v
180         print
181
182         for generator in self.parent.generators:
183             if generator.canHandle(self.data):
184                 self.state += 1
185                 print generator.generateConfig(self.headers)
186         else:
187             print 'No generator found'
188             
189
190
191 class FreeswitchConfigGenerator(object):
192     """
193     Base class for generating XML configs.
194     """
195     
196     param_match = {}
197     section_name = None
198
199     def __init__(self, model, parent):
200         self.model = model
201         self.parent = parent
202
203     def canHandle(self, params):
204         for key, value in self.param_match.iteritems():
205             print key, value, params.get(key, None)
206             if params.get(key, None) != value:
207                 return False
208         else:
209             return True
210
211     def baseElements(self):
212         root_elt = etree.Element('document')
213         section_elt = etree.SubElement(
214             root_elt, 'section', name=self.section_name)
215         return root_elt, section_elt
216     baseElements = property(baseElements)
217
218     def generateConfig(self, params):
219         return NotImplemented
220
221     @staticmethod
222     def addParams(parent_elt, params):
223         for name, value in params:
224             etree.SubElement(parent_elt, 'param', name=name, value=value)
225             
226         
227 class SofiaConfGenerator(FreeswitchConfigGenerator):
228     """
229     Generates sofia.conf.xml config file.
230     """
231     param_match = {'section': 'configuration', 'key_value': 'sofia.conf'}
232     section_name = 'configuration'
233     config_name = 'sofia.conf'
234
235     def generateConfig(self, params):
236         # Get base elements.
237         root_elt, section_elt = self.baseElements
238
239         # Create configuration, settings and profiles elements.
240         configuration_elt = etree.SubElement(
241             section_elt, 'configuration', name=self.config_name,
242             description='%s config' % self.config_name)
243         settings_elt = etree.SubElement(configuration_elt, 'settings')
244         profiles_elt = etree.SubElement(settings_elt, 'profiles')
245
246         database = self.model.controllers['connection'].model.database()
247         
248         # Create all profiles for current host.
249         profiles_query = database.exec_(
250             'select id from ipypbxweb_sipprofile where connection_id = %i' %
251             self.parent.connection_id)
252         while profiles_query.next():
253             profile_id, _ok = profiles_query.value(0).toInt()
254             profile_elt = etree.SubElement(profiles_elt, 'profile')
255
256             # Create domains for current profile.
257             domains_elt = etree.SubElement(profile_elt, 'domains')
258
259             domains_query = database.exec_(
260                 'select host_name from ipypbxweb_domain where profile_id = '
261                 '%i' % profile_id)
262             while domains_query.next():
263                 domain_elt = etree.SubElement(
264                     domains_elt, 'domain', name=domains_quey.value(0),
265                     alias='true', parse='true')
266
267             # Create settings for current profile.
268             settings_elt = etree.SubElement(profile_elt, 'settings')
269             params = (
270                 ('dialplan', 'XML,enum'),
271                 ('ext-sip-ip', profile.ext_sip_ip),
272                 ('ext-rtp-ip', profile.ext_rtp_ip),
273                 ('sip-ip', profile.sip_ip),
274                 ('rtp-ip', profile.rtp_ip),
275                 ('sip-port', profile.sip_port),
276                 ('nonce-ttl', '60'),
277                 ('rtp-timer-name', 'soft'),
278                 ('codec-prefs', 'PCMU@20i'),
279                 ('debug', '1'),
280                 ('rfc2833-pt', '1'),
281                 ('dtmf-duration', '100'),
282                 ('codec-ms', '20'),
283                 ('accept-blind-reg', profile.accept_blind_registration),
284                 ('auth-calls', profile.authenticate_calls))
285             self.add_params(settings_elt, params)
286
287             # Create gateways for current profile.
288             gateways_elt = etree.SubElement(profile, 'gateways')
289             for gateway in self.parent.get_gateways_for_profile(profile):
290                 gateway_elt = etree.SubElement(gateways_elt, 'gateway', name=gateway.name)
291                 params = (
292                     ('username', gateway.username),
293                     ('realm', gateway.realm),
294                     ('from-domain', gateway.from_domain),
295                     ('password', gateway.password),
296                     ('retry-seconds', gateway.retry_seconds),
297                     ('expire-seconds', gateway.expire_seconds),
298                     ('caller-id-in-from', gateway.caller_id_in_from),
299                     ('extension', gateway.extension),
300                     # TODO: proxy, register
301                     ('expire-seconds', gateway.expire_seconds),
302                     ('retry-seconds', gateway.retry_seconds))
303                 self.add_params(gateway_elt, params)
304
305         return root_elt