Added tests for ipypbx.http.FreeswitchConfigGenerator
[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     configNotFound = '''
27 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
28 <document type="freeswitch/xml">
29   <section name="result">
30     <result status="not found" />
31   </section>
32 </document>
33     '''
34     responseTemplate = '''HTTP/1.1 200 OK
35 Content-Type: text/xml; charset=utf-8
36 Content-Length: %i
37
38 %s'''
39   
40     def __init__(self, database, parent=None):
41         super(FreeswitchConfigServer, self).__init__(parent)
42
43         self.host = None
44         self.port = None
45         self.connection_id = None
46         self.is_running = False
47         self.generators = [
48             GenClass(database) for GenClass in (
49                 SofiaConfGenerator,)]
50         
51         self.httpRequestParser = HttpRequestParser()
52         
53     def setSocketData(self, host, port, connection_id):
54         """
55         Set host and port for socket to listen on.
56
57         If the settings differ from previous values, server gets restarted.
58         """
59         # Check if restart is needed before new settings are applied.
60         needs_restart = (
61             (host, port) != (self.host, self.port)) and connection_id
62
63         # Save new settings.
64         self.host = host
65         self.port = port
66         if connection_id:
67             self.connection_id = connection_id
68             for generator in self.generators:
69                 generator.connection_id = connection_id
70
71         # Restart server if necessary.
72         if needs_restart:
73             self.restartServer()
74
75     def startServer(self):
76         """
77         Start listening on our socket.
78         """
79         if not self.is_running:
80             if self.host and self.port:
81                 self.newConnection.connect(self.clientConnecting)
82                 self.listen(QtNetwork.QHostAddress(self.host), self.port)
83                 self.is_running = True
84
85     def stopServer(self):
86         """
87         Stop listening on our socket.
88         """
89         if self.is_running:
90             self.close()
91             self.is_running = False
92
93     def restartServer(self):
94         """
95         Restart server.
96         """
97         self.stopServer()
98         self.startServer()
99
100     def clientConnecting(self):
101         """
102         Handle client connection.
103         """
104         if self.hasPendingConnections():
105             self.socket = self.nextPendingConnection()
106             self.socket.readyRead.connect(self.receiveData)
107
108     def receiveData(self):
109         # TODO: read in chunks.
110         for line in str(self.socket.readAll()).split('\r\n'):
111             self.httpRequestParser.handle(line)
112
113         for generator in self.generators:
114             if generator.canHandle(self.httpRequestParser.data):
115                 self.state += 1
116                 self.result = etree.tostring(generator.generateConfig(
117                     self.httpRequestParser.headers))
118                 break
119
120         response = self.httpRequestParser.result or self.configNotFound
121         http_response = self.responseTemplate % (len(response), response)
122         self.socket.write(http_response)
123         self.httpRequestParser.reset()        
124         self.socket.close()
125
126
127 class HttpParseError(Exception):
128     """
129     Error parsing HTTP request.
130     """
131
132
133 class HttpRequestParser(object):
134     """
135     A simple state machine for parsing HTTP requests.
136     """
137     HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_BODY, HTTP_DONE = \
138         range(6)
139     HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'BODY', 'DONE']
140     
141     def __init__(self):
142         self.reset()
143
144     def reset(self):
145         """
146         Reset parser to initial state.
147         """
148         # Initial values for request data.
149         self.method = None
150         self.request_path = None
151         self.http_version = None
152         self.headers = {}
153         self.data = {}
154         self.result = None
155         
156         # Set initial state.
157         self.state = self.HTTP_NONE        
158
159     def handle(self, line):
160         """
161         Dispatch line to current state handler.
162         """
163         for state in self.HTTP_STATES:
164             if getattr(self, 'HTTP_%s' % state) == self.state:
165                 getattr(self, 'handle%s' % state.title())(line)
166                 break
167         else:
168             raise HttpParseError('Unknown HTTP state')
169                 
170     def handleNone(self, line):
171         """
172         Pass line to next state.
173         """
174         self.state += 1
175         self.handle(line)
176
177     def handleRequest(self, line):
178         """
179         Retrieve HTTP method, request path and HTTP version from request.
180         """
181         try:
182             self.method, self.request_path, self.http_version = line.split(' ')
183             self.state += 1
184         except ValueError:
185             pass
186
187     def handleHeaders(self, line):
188         """
189         Parse headers while not found an empty line.
190         """
191         if line:
192             key, value = line.split(': ')
193             self.headers[key] = value
194         else:
195             self.state += 1
196             self.handle(line)
197
198     def handleEmpty(self, line):
199         """
200         Empty line separator is found - proceed to next state.
201         """
202         self.state += 1
203
204     def handleBody(self, line):
205         """
206         Append to message body.
207         """
208         if self.method != 'POST':
209             raise HttpParseError('Only POST request are supported')
210             
211         self.data = dict(pair.split('=', 2) for pair in line.split('&'))
212
213     def handleDone(self, line):
214         raise HttpParseError("Can't read past request end")
215     
216
217 class FreeswitchConfigGenerator(object):
218     """
219     Base class for generating XML configs.
220     """
221     
222     param_match = {}
223
224     def __init__(self, database=None, connection_id=None):
225         self.database = database
226         self.connection_id = connection_id
227
228     def canHandle(self, params):
229         """
230         Check if this generator can handle a request from freeswitch.
231         """
232         for key, value in self.param_match.iteritems():
233             if params.get(key, None) != value:
234                 return False
235         else:
236             return True
237
238     def baseElements(self):
239         root_elt = etree.Element('document', type='freeswitch/xml')
240         section_elt = etree.SubElement(
241             root_elt, 'section', name=self.param_match['section'])
242         return root_elt, section_elt
243     baseElements = property(baseElements)
244
245     def generateConfig(self, params):
246         return NotImplemented
247
248     @staticmethod
249     def addParams(parent_elt, params):
250         """
251         Create params element based on data passed in a list.
252         """
253         # Create params element.
254         params_elt = etree.SubElement(parent_elt, 'params')
255
256         # Add param elements to params element.
257         for name, value in params:
258             etree.SubElement(
259                 params_elt, 'param', name=name, value=str(value))
260             
261         return parent_elt
262             
263         
264 class SofiaConfGenerator(FreeswitchConfigGenerator):
265     """
266     Generates sofia.conf.xml config file.
267     """
268     param_match = {'section': 'configuration', 'key_value': 'sofia.conf'}
269     config_name = 'sofia.conf'
270
271     def generateConfig(self, params):
272         # Get base elements.
273         root_elt, section_elt = self.baseElements
274
275         # Create configuration, settings and profiles elements.
276         configuration_elt = etree.SubElement(
277             section_elt, 'configuration', name=self.config_name,
278             description='%s config' % self.config_name)
279         profiles_elt = etree.SubElement(configuration_elt, 'profiles')
280
281         database = self.database
282         
283         # Create all profiles for current host.
284         profiles_query = database.exec_(
285             '''
286             select id, name, external_sip_ip, external_rtp_ip, sip_ip, rtp_ip,
287             sip_port, accept_blind_registration, authenticate_calls
288             from ipypbxweb_sipprofile where connection_id = %i
289             ''' % self.parent.connection_id)
290         while profiles_query.next():
291             # Create profile element.
292             profile_id, _ok = profiles_query.value(0).toInt()
293             profile_elt = etree.SubElement(
294                 profiles_elt, 'profile',
295                 name=profiles_query.value(1).toString())
296
297             # Create domains for current profile.
298             domains_elt = etree.SubElement(profile_elt, 'domains')
299             domains_query = database.exec_(
300                 'select host_name from ipypbxweb_domain where sip_profile_id = '
301                 '%i' % profile_id)
302             while domains_query.next():
303                 domain_elt = etree.SubElement(
304                     domains_elt, 'domain',
305                     name=domains_query.value(0).toString(), alias='true',
306                     parse='true')
307
308
309             profile_sip_port, _ok = profiles_query.value(6).toInt()
310
311             # Create settings for current profile.
312             settings_elt = etree.SubElement(profile_elt, 'settings')
313             params = (
314                 ('dialplan', 'XML,enum'),
315                 ('ext-sip-ip', profiles_query.value(2).toString()),
316                 ('ext-rtp-ip', profiles_query.value(3).toString()),
317                 ('sip-ip', profiles_query.value(4).toString()),
318                 ('rtp-ip', profiles_query.value(5).toString()),
319                 ('sip-port', profile_sip_port),
320                 ('nonce-ttl', '60'),
321                 ('rtp-timer-name', 'soft'),
322                 ('codec-prefs', 'PCMU@20i'),
323                 ('debug', '1'),
324                 ('rfc2833-pt', '1'),
325                 ('dtmf-duration', '100'),
326                 ('codec-ms', '20'),
327                 ('accept-blind-reg', profiles_query.value(7).toBool()),
328                 ('auth-calls', profiles_query.value(8).toBool()))
329             self.addParams(settings_elt, params)
330
331             # Create gateways for current profile.
332             gateways_elt = etree.SubElement(profile_elt, 'gateways')
333             gateways_query = database.exec_(
334                 '''
335                 select name, username, realm, from_domain, password,
336                 retry_in_seconds, expire_in_seconds, caller_id_in_from_field,
337                 extension
338                 from ipypbxweb_gateway where sip_profile_id = %i
339                 '''  % profile_id)
340             while gateways_query.next():
341                 # Create gateway element.
342                 gateway_elt = etree.SubElement(
343                     gateways_elt, 'gateway', name=gateways_query.value(0).toString())
344                 retry_seconds, _ok = gateways_query.value(5).toInt()
345                 expire_seconds, _ok = gateways_query.value(6).toInt()
346                 params = (
347                     ('username', gateways_query.value(1).toString()),
348                     ('realm', gateways_query.value(2).toString()),
349                     ('from-domain', gateways_query.value(3).toString()),
350                     ('password', gateways_query.value(4).toString()),
351                     ('retry-seconds', retry_seconds),
352                     ('expire-seconds', expire_seconds),
353                     ('caller-id-in-from', gateways_query.value(7).toBool()),
354                     ('extension', gateways_query.value(8).toString()),
355                     # TODO: proxy, register
356                     )
357                 self.addParams(gateway_elt, params)
358
359         return root_elt    
360
361 class DirectoryGenerator(FreeswitchConfigGenerator):
362     """
363     Generates user directory.
364     """
365     param_match = {'section': 'directory'}
366
367     def generateConfig(self, params):
368         #Get base elemenets.
369         root_elt, section_elt = self.baseELements
370
371         database = self.database
372
373         # Find profile id from params.
374         profile_query = database.exec_(
375             '''
376             select id from ipypbxweb_sipprofile
377             where name= '%s' and connection_id = %i limit 1
378             ''' % (params['profile'], self.parent.connection_id))
379
380         _ok = False
381         if profile_query.next():
382             profile_id, _ok = profile_query.value(0).toInt()
383
384         if not _ok:
385             # Matching SIP profile not found.
386             return
387         
388         # List all domains for this profile.        
389         domains_query = database.exec_(
390             '''
391             select id, host_name from ipypbxweb_domain
392             where sip_profile_id = %i
393             ''' % profile_id)
394
395         while domains_query.next():
396             domain_id, _ok = domains_query.value(0).toInt()
397
398             # Create domaim element.
399             domain_elt = etree.SubElement(
400                 section_elt, 'domain', name=domains_query.value(1).toString())
401             
402             # TODO: add domain params section if we need it, i.e.:
403             #<params>
404             #     <param name="dial-string"
405             #            value="{presence_id=${dialed_user}@${dialed_domain}}$\
406             #                   {sofia_contact(${dialed_user}@${dialed_domain})}"/>
407             #</params>            
408
409             # For new we put all users into one group called default.
410             groups_elt = etree.SubElement(domain_elt, 'groups')
411             group_elt = etree.SubElement(groups_elt, 'group', name='default')
412
413             users_elt = etree.SubElement(group_elt, 'users')
414
415             users_query = database.exec_(
416                 '''
417                 select user_id, password from ipypbxweb_endpoint
418                 where domain_id = %i
419                 ''' % domain_id)
420
421             # Create user entries for all endpoints for this domain.
422             while users_query.next():
423                 user_elt = etree.SubElement(
424                     users_elt, 'user', id=users_query.value(0).toString())
425
426                 # Specify endpoint password.
427                 params = (
428                     ('password', users_query.value(1).toString()),
429                     )
430                 self.addParams(user_elt, params)
431
432         return root_elt
433
434
435 class DialplanGenerator(FreeswitchConfigGenerator):
436     """
437     Generates XML dialplans.
438     """
439
440     param_match = {'section': 'dialplan'}
441