X-Git-Url: http://git.maemo.org/git/?p=ipypbx;a=blobdiff_plain;f=src%2Fipypbx%2Fhttp.py;h=b1256f380728f48de9190ee722db309d9b08f439;hp=86699b9804a104e96ab0d1c4b33d9a83b8208fe1;hb=892f5925c16c3b3c6af84cb7fdd47574408f1a5b;hpb=9769ab5dc895879c6090e134722a7796e6654247 diff --git a/src/ipypbx/http.py b/src/ipypbx/http.py index 86699b9..b1256f3 100644 --- a/src/ipypbx/http.py +++ b/src/ipypbx/http.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with IPyPBX. If not, see . +import xml.etree.ElementTree as etree from PyQt4 import QtCore, QtNetwork @@ -75,7 +76,7 @@ class FreeswitchConfigServer(QtNetwork.QTcpServer): # TODO: read in chunks. for line in str(self.socket.readAll()).split('\r\n'): self.httpRequestParser.handle(line) - + class HttpParseError(Exception): """ @@ -156,4 +157,112 @@ class HttpRequestParser(object): Append to message body. """ self.data = dict(pair.split('=', 2) for pair in line.split('&')) - print self.data + + for k, v in self.data.items(): + print k, '=>', v + print + + + +class FreeswitchConfigGenerator(object): + """ + Base class for generating XML configs. + """ + + param_match = {} + section_name = None + + def __init__(self, model): + self.model = model + + def check_params(self, params): + for key, value in self.param_match.iteritems(): + if params.get(key, None) != value: + return False + else: + return True + + def base_elements(self): + root_elt = etree.Element('document') + section_elt = etree.SubElement( + root_elt, 'section', name=self.section_name) + return root_elt, section_elt + base_elements = property(base_elements) + + def generate_config(self, params): + return NotImplemented + + def add_params(parent_elt, params): + for name, value in params: + etree.SubElement(parent_elt, 'param', name=name, value=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 generate_config(self, params): + # Get base elements. + root_elt, section_elt = self.base_elements + + # 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(self.settings_elt, 'profiles') + + # Create all profiles for current host. + for profile in self.parent.get_profiles(): + profile_elt = etree.SubElement(profiles_elt, 'profile') + + # Create domains for current profile. + domains_elt = etree.SubElement(profile_elt, 'domains') + for domain in self.parent.get_domains_for_profile(profile): + domain_elt = etree.SubElement( + domains_elt, 'domain', name=domain.host_name, + alias='true', parse='true') + + # Create settings for current profile. + settings_elt = etree.SubElement(profile_elt, 'settings') + params = ( + ('dialplan', 'XML,enum'), + ('ext-sip-ip', profile.ext_sip_ip), + ('ext-rtp-ip', profile.ext_rtp_ip), + ('sip-ip', profile.sip_ip), + ('rtp-ip', profile.rtp_ip), + ('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', profile.accept_blind_registration), + ('auth-calls', profile.authenticate_calls)) + self.add_params(settings_elt, params) + + # Create gateways for current profile. + gateways_elt = etree.SubElement(profile, 'gateways') + for gateway in self.parent.get_gateways_for_profile(profile): + gateway_elt = etree.SubElement(gateways_elt, 'gateway', name=gateway.name) + params = ( + ('username', gateway.username), + ('realm', gateway.realm), + ('from-domain', gateway.from_domain), + ('password', gateway.password), + ('retry-seconds', gateway.retry_seconds), + ('expire-seconds', gateway.expire_seconds), + ('caller-id-in-from', gateway.caller_id_in_from), + ('extension', gateway.extension), + # TODO: proxy, register + ('expire-seconds', gateway.expire_seconds), + ('retry-seconds', gateway.retry_seconds)) + self.add_params(gateway_elt, params) + + return root_elt