1 # Copyright (c) Stas Shtin, 2010
3 # This file is part of IPyPBX.
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.
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.
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/>.
18 import xml.etree.ElementTree as etree
19 from PyQt4 import QtCore, QtNetwork
22 class FreeswitchConfigServer(QtNetwork.QTcpServer):
24 TCP server that receives config requests from freeswitch.
26 def __init__(self, window):
27 super(FreeswitchConfigServer, self).__init__(window)
31 self.connection_id = None
32 self.is_running = False
34 GenClass(window, self) for GenClass in (
37 self.httpRequestParser = HttpRequestParser(self)
39 def setSocketData(self, host, port, connection_id):
41 Set host and port for socket to listen on.
43 If the settings differ from previous values, server gets restarted.
45 # Check if restart is needed before new settings are applied.
47 (host, port) != (self.host, self.port)) and connection_id
53 self.connection_id = connection_id
55 # Restart server if necessary.
59 def startServer(self):
61 Start listening on our socket.
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
71 Stop listening on our socket.
75 self.is_running = False
77 def restartServer(self):
84 def clientConnecting(self):
86 Handle client connection.
88 if self.hasPendingConnections():
89 self.socket = self.nextPendingConnection()
90 self.socket.readyRead.connect(self.receiveData)
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)
98 class HttpParseError(Exception):
100 Error parsing HTTP request.
104 class HttpRequestParser(object):
106 A simple state machine for parsing HTTP requests.
108 HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE, \
110 HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE', 'DONE']
112 def __init__(self, parent):
118 Reset parser to initial state.
120 # Initial values for request data.
122 self.request_path = None
123 self.http_version = None
128 self.state = self.HTTP_NONE
130 def handle(self, line):
132 Dispatch line to current state handler.
134 for state in self.HTTP_STATES:
135 if getattr(self, 'HTTP_%s' % state) == self.state:
136 getattr(self, 'handle%s' % state.title())(line)
139 raise HttpParseError('Unknown HTTP state')
141 def handleNone(self, line):
143 Pass line to next state.
148 def handleRequest(self, line):
150 Retrieve HTTP method, request path and HTTP version from request.
152 self.method, self.request_path, self.http_version = line.split(' ')
155 def handleHeaders(self, line):
157 Parse headers while not found an empty line.
160 key, value = line.split(': ')
161 self.headers[key] = value
166 def handleEmpty(self, line):
168 Empty line separator is found - proceed to next state.
172 def handleMessage(self, line):
174 Append to message body.
176 self.data = dict(pair.split('=', 2) for pair in line.split('&'))
178 for k, v in self.data.items():
182 for generator in self.parent.generators:
183 if generator.canHandle(self.data):
185 print generator.generateConfig(self.headers)
187 print 'No generator found'
191 class FreeswitchConfigGenerator(object):
193 Base class for generating XML configs.
199 def __init__(self, model, parent):
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:
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)
218 def generateConfig(self, params):
219 return NotImplemented
222 def addParams(parent_elt, params):
223 for name, value in params:
224 etree.SubElement(parent_elt, 'param', name=name, value=value)
227 class SofiaConfGenerator(FreeswitchConfigGenerator):
229 Generates sofia.conf.xml config file.
231 param_match = {'section': 'configuration', 'key_value': 'sofia.conf'}
232 section_name = 'configuration'
233 config_name = 'sofia.conf'
235 def generateConfig(self, params):
237 root_elt, section_elt = self.baseElements
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')
246 database = self.model.controllers['connection'].model.database()
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')
256 # Create domains for current profile.
257 domains_elt = etree.SubElement(profile_elt, 'domains')
259 domains_query = database.exec_(
260 'select host_name from ipypbxweb_domain where 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')
267 # Create settings for current profile.
268 settings_elt = etree.SubElement(profile_elt, 'settings')
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),
277 ('rtp-timer-name', 'soft'),
278 ('codec-prefs', 'PCMU@20i'),
281 ('dtmf-duration', '100'),
283 ('accept-blind-reg', profile.accept_blind_registration),
284 ('auth-calls', profile.authenticate_calls))
285 self.add_params(settings_elt, params)
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)
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)