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, parent=None):
27 super(FreeswitchConfigServer, self).__init__(parent)
31 self.is_running = False
33 self.httpRequestParser = HttpRequestParser()
35 def setSocketData(self, host, port):
37 Set host and port for socket to listen on.
42 def startServer(self):
44 Start listening on our socket.
46 if not self.is_running:
47 if self.host and self.port:
48 self.newConnection.connect(self.clientConnecting)
49 self.listen(QtNetwork.QHostAddress(self.host), self.port)
50 self.is_running = True
54 Stop listening on our socket.
58 self.is_running = False
60 def restartServer(self):
67 def clientConnecting(self):
69 Handle client connection.
71 if self.hasPendingConnections():
72 self.socket = self.nextPendingConnection()
73 self.socket.readyRead.connect(self.receiveData)
75 def receiveData(self):
76 # TODO: read in chunks.
77 for line in str(self.socket.readAll()).split('\r\n'):
78 self.httpRequestParser.handle(line)
81 class HttpParseError(Exception):
83 Error parsing HTTP request.
87 class HttpRequestParser(object):
89 A simple state machine for parsing HTTP requests.
91 HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE = range(5)
92 HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE']
95 super(HttpRequestParser, self).__init__()
100 Reset parser to initial state.
102 # Initial values for request data.
104 self.request_path = None
105 self.http_version = None
110 self.state = self.HTTP_NONE
112 def handle(self, line):
114 Dispatch line to current state handler.
116 for state in self.HTTP_STATES:
117 if getattr(self, 'HTTP_%s' % state) == self.state:
118 print self.state, line
119 getattr(self, 'handle%s' % state.title())(line)
122 raise HttpParseError('Unknown HTTP state')
124 def handleNone(self, line):
126 Pass line to next state.
131 def handleRequest(self, line):
133 Retrieve HTTP method, request path and HTTP version from request.
135 self.method, self.request_path, self.http_version = line.split(' ')
138 def handleHeaders(self, line):
140 Parse headers while not found an empty line.
143 key, value = line.split(': ')
144 self.headers[key] = value
149 def handleEmpty(self, line):
151 Empty line separator is found - proceed to next state.
155 def handleMessage(self, line):
157 Append to message body.
159 self.data = dict(pair.split('=', 2) for pair in line.split('&'))
161 for k, v in self.data.items():
167 class FreeswitchConfigGenerator(object):
169 Base class for generating XML configs.
175 def __init__(self, model):
178 def check_params(self, params):
179 for key, value in self.param_match.iteritems():
180 if params.get(key, None) != value:
185 def base_elements(self):
186 root_elt = etree.Element('document')
187 section_elt = etree.SubElement(
188 root_elt, 'section', name=self.section_name)
189 return root_elt, section_elt
190 base_elements = property(base_elements)
192 def generate_config(self, params):
193 return NotImplemented
195 def add_params(parent_elt, params):
196 for name, value in params:
197 etree.SubElement(parent_elt, 'param', name=name, value=value)
200 class SofiaConfGenerator(FreeswitchConfigGenerator):
202 Generates sofia.conf.xml config file.
204 param_match = {'section': 'configuration', 'key_value': 'sofia.conf'}
205 section_name = 'configuration'
206 config_name = 'sofia.conf'
208 def generate_config(self, params):
210 root_elt, section_elt = self.base_elements
212 # Create configuration, settings and profiles elements.
213 configuration_elt = etree.SubElement(
214 section_elt, 'configuration', name=self.config_name,
215 description='%s config' % self.config_name)
216 settings_elt = etree.SubElement(configuration_elt, 'settings')
217 profiles_elt = etree.SubElement(self.settings_elt, 'profiles')
219 # Create all profiles for current host.
220 for profile in self.parent.get_profiles():
221 profile_elt = etree.SubElement(profiles_elt, 'profile')
223 # Create domains for current profile.
224 domains_elt = etree.SubElement(profile_elt, 'domains')
225 for domain in self.parent.get_domains_for_profile(profile):
226 domain_elt = etree.SubElement(
227 domains_elt, 'domain', name=domain.host_name,
228 alias='true', parse='true')
230 # Create settings for current profile.
231 settings_elt = etree.SubElement(profile_elt, 'settings')
233 ('dialplan', 'XML,enum'),
234 ('ext-sip-ip', profile.ext_sip_ip),
235 ('ext-rtp-ip', profile.ext_rtp_ip),
236 ('sip-ip', profile.sip_ip),
237 ('rtp-ip', profile.rtp_ip),
238 ('sip-port', profile.sip_port),
240 ('rtp-timer-name', 'soft'),
241 ('codec-prefs', 'PCMU@20i'),
244 ('dtmf-duration', '100'),
246 ('accept-blind-reg', profile.accept_blind_registration),
247 ('auth-calls', profile.authenticate_calls))
248 self.add_params(settings_elt, params)
250 # Create gateways for current profile.
251 gateways_elt = etree.SubElement(profile, 'gateways')
252 for gateway in self.parent.get_gateways_for_profile(profile):
253 gateway_elt = etree.SubElement(gateways_elt, 'gateway', name=gateway.name)
255 ('username', gateway.username),
256 ('realm', gateway.realm),
257 ('from-domain', gateway.from_domain),
258 ('password', gateway.password),
259 ('retry-seconds', gateway.retry_seconds),
260 ('expire-seconds', gateway.expire_seconds),
261 ('caller-id-in-from', gateway.caller_id_in_from),
262 ('extension', gateway.extension),
263 # TODO: proxy, register
264 ('expire-seconds', gateway.expire_seconds),
265 ('retry-seconds', gateway.retry_seconds))
266 self.add_params(gateway_elt, params)