# You should have received a copy of the GNU General Public License
# along with IPyPBX. If not, see <http://www.gnu.org/licenses/>.
+import xml.etree.ElementTree as etree
from PyQt4 import QtCore, QtNetwork
"""
TCP server that receives config requests from freeswitch.
"""
- def __init__(self, parent=None):
- super(FreeswitchConfigServer, self).__init__(parent)
+ configNotFound = '''
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<document type="freeswitch/xml">
+ <section name="result">
+ <result status="not found" />
+ </section>
+</document>
+ '''
+ responseTemplate = '''HTTP/1.1 200 OK
+Content-Type: text/xml; charset=utf-8
+Content-Length: %i
+
+%s'''
+
+ def __init__(self, window):
+ super(FreeswitchConfigServer, self).__init__(window)
self.host = None
self.port = None
+ self.connection_id = None
self.is_running = False
+ self.generators = [
+ GenClass(window, self) for GenClass in (
+ SofiaConfGenerator,)]
- self.httpRequestParser = HttpRequestParser()
+ self.httpRequestParser = HttpRequestParser(self)
- def setSocket(self, host, port):
+ def setSocketData(self, host, port, connection_id):
"""
Set host and port for socket to listen on.
+
+ If the settings differ from previous values, server gets restarted.
"""
+ # Check if restart is needed before new settings are applied.
+ needs_restart = (
+ (host, port) != (self.host, self.port)) and connection_id
+
+ # Save new settings.
self.host = host
self.port = port
+ if connection_id:
+ self.connection_id = connection_id
+
+ # Restart server if necessary.
+ if needs_restart:
+ self.restartServer()
def startServer(self):
"""
self.stopServer()
self.startServer()
- def clientConnecting(self, socket):
+ def clientConnecting(self):
"""
Handle client connection.
"""
if self.hasPendingConnections():
- connectingClient = self.server.nextPendingConnection()
- connectingClient.readyRead.connect(self.receiveData)
+ self.socket = self.nextPendingConnection()
+ self.socket.readyRead.connect(self.receiveData)
- def receiveData(self, socket):
- while socket.canReadLine():
- line = socket.readLine().strip()
-
+ def receiveData(self):
+ # TODO: read in chunks.
+ for line in str(self.socket.readAll()).split('\r\n'):
+ print line
+ self.httpRequestParser.handle(line)
+
+ response = self.httpRequestParser.result or self.configNotFound
+ http_response = self.responseTemplate % (len(response), response)
+ self.socket.write(http_response)
+ self.httpRequestParser.reset()
+ self.socket.close()
class HttpParseError(Exception):
"""
"""
A simple state machine for parsing HTTP requests.
"""
- HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE = range(5)
- HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE']
+ HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE, \
+ HTTP_DONE = range(6)
+ HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE', 'DONE']
- def __init__(self):
- super(HttpRequestParser, self).__init__()
+ def __init__(self, parent):
+ self.parent = parent
+ self.reset()
def reset(self):
"""
self.method = None
self.request_path = None
self.http_version = None
- self.message = ''
+ self.headers = {}
+ self.data = {}
+ self.result = None
# Set initial state.
- self.state = HTTP_NONE
+ self.state = self.HTTP_NONE
def handle(self, line):
"""
Dispatch line to current state handler.
"""
- for state in HTTP_STATES:
+ for state in self.HTTP_STATES:
if getattr(self, 'HTTP_%s' % state) == self.state:
getattr(self, 'handle%s' % state.title())(line)
break
self.headers[key] = value
else:
self.state += 1
+ self.handle(line)
def handleEmpty(self, line):
"""
"""
Append to message body.
"""
- self.message += line
+ self.data = dict(pair.split('=', 2) for pair in line.split('&'))
+
+ #for k, v in self.data.items():
+ # print k, '=>', v
+ #print
+
+ for generator in self.parent.generators:
+ if generator.canHandle(self.data):
+ self.state += 1
+ self.result = etree.tostring(generator.generateConfig(
+ self.headers))
+ break
+
+
+class FreeswitchConfigGenerator(object):
+ """
+ Base class for generating XML configs.
+ """
+
+ param_match = {}
+ section_name = None
+
+ def __init__(self, model, parent):
+ self.model = model
+ self.parent = parent
+
+ def canHandle(self, params):
+ for key, value in self.param_match.iteritems():
+ if params.get(key, None) != value:
+ return False
+ else:
+ return True
+
+ def baseElements(self):
+ root_elt = etree.Element('document')
+ section_elt = etree.SubElement(
+ root_elt, 'section', name=self.section_name)
+ return root_elt, section_elt
+ baseElements = property(baseElements)
+
+ def generateConfig(self, params):
+ return NotImplemented
+
+ @staticmethod
+ def addParams(parent_elt, params):
+ for name, value in params:
+ etree.SubElement(
+ parent_elt, 'param', name=name, value=str(value))
+
+
+class SofiaConfGenerator(FreeswitchConfigGenerator):
+ """
+ Generates sofia.conf.xml config file.
+ """
+ param_match = {'section': 'configuration', 'key_value': 'sofia.conf'}
+ section_name = 'configuration'
+ config_name = 'sofia.conf'
+
+ def generateConfig(self, params):
+ # Get base elements.
+ root_elt, section_elt = self.baseElements
+
+ # Create configuration, settings and profiles elements.
+ configuration_elt = etree.SubElement(
+ section_elt, 'configuration', name=self.config_name,
+ description='%s config' % self.config_name)
+ settings_elt = etree.SubElement(configuration_elt, 'settings')
+ profiles_elt = etree.SubElement(settings_elt, 'profiles')
+
+ database = self.model.controllers['connection'].model.database()
+
+ # Create all profiles for current host.
+ profiles_query = database.exec_(
+ '''
+ select id, name, external_sip_ip, external_rtp_ip, sip_ip, rtp_ip,
+ sip_port, accept_blind_registration, authenticate_calls
+ from ipypbxweb_sipprofile where connection_id = %i
+ ''' % self.parent.connection_id)
+ while profiles_query.next():
+ profile_id, _ok = profiles_query.value(0).toInt()
+ profile_elt = etree.SubElement(
+ profiles_elt, 'profile',
+ name=profiles_query.value(1).toString())
+
+ # Create domains for current profile.
+ domains_elt = etree.SubElement(profile_elt, 'domains')
+ domains_query = database.exec_(
+ 'select host_name from ipypbxweb_domain where profile_id = '
+ '%i' % profile_id)
+ while domains_query.next():
+ domain_elt = etree.SubElement(
+ domains_elt, 'domain', name=domains_quey.value(0),
+ alias='true', parse='true')
+
+ profile_sip_port, _ok = profiles_query.value(6).toInt()
+
+ # Create settings for current profile.
+ settings_elt = etree.SubElement(profile_elt, 'settings')
+ params = (
+ ('dialplan', 'XML,enum'),
+ ('ext-sip-ip', profiles_query.value(2).toString()),
+ ('ext-rtp-ip', profiles_query.value(3).toString()),
+ ('sip-ip', profiles_query.value(4).toString()),
+ ('rtp-ip', profiles_query.value(5).toString()),
+ ('sip-port', profile_sip_port),
+ ('nonce-ttl', '60'),
+ ('rtp-timer-name', 'soft'),
+ ('codec-prefs', 'PCMU@20i'),
+ ('debug', '1'),
+ ('rfc2833-pt', '1'),
+ ('dtmf-duration', '100'),
+ ('codec-ms', '20'),
+ ('accept-blind-reg', profiles_query.value(7).toBool()),
+ ('auth-calls', profiles_query.value(8).toBool()))
+ self.addParams(settings_elt, params)
+
+ # Create gateways for current profile.
+ gateways_elt = etree.SubElement(profile_elt, 'gateways')
+ gateways_query = database.exec_(
+ '''
+ select name, username, realm, from_domain, password,
+ retry_seconds, expire_seconds, caller_id_in_from, extension
+ from ipypbxweb_gateway where sipprofile_id = %i
+ ''' % profile_id)
+ while gateways_query.next():
+ gateway_elt = etree.SubElement(
+ gateways_elt, 'gateway', name=gateways_query.value(0).toString())
+ retry_seconds, _ok = gateways_query.value(5).toInt()
+ expire_seconds, _ok = gateways_query.value(6).toInt()
+ params = (
+ ('username', gateways_query.value(1).toString()),
+ ('realm', gateways_query.value(2).toString()),
+ ('from-domain', gateways_query.value(3).toString()),
+ ('password', gateways_query.value(4).toString()),
+ ('retry-seconds', retry_seconds),
+ ('expire-seconds', expire_seconds),
+ ('caller-id-in-from', gateways_query.value(7).toBool()),
+ ('extension', gateways_query.value(8).toString()),
+ # TODO: proxy, register
+ )
+ self.addParams(gateway_elt, params)
+
+ return root_elt