Begin implementing sofia.conf generation
[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     def __init__(self, parent=None):
27         super(FreeswitchConfigServer, self).__init__(parent)
28
29         self.host = None
30         self.port = None
31         self.is_running = False
32         
33         self.httpRequestParser = HttpRequestParser()
34         
35     def setSocketData(self, host, port):
36         """
37         Set host and port for socket to listen on.
38         """
39         self.host = host
40         self.port = port
41
42     def startServer(self):
43         """
44         Start listening on our socket.
45         """
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
51
52     def stopServer(self):
53         """
54         Stop listening on our socket.
55         """
56         if self.is_running:
57             self.close()
58             self.is_running = False
59
60     def restartServer(self):
61         """
62         Restart server.
63         """
64         self.stopServer()
65         self.startServer()
66
67     def clientConnecting(self):
68         """
69         Handle client connection.
70         """
71         if self.hasPendingConnections():
72             self.socket = self.nextPendingConnection()
73             self.socket.readyRead.connect(self.receiveData)
74
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)
79
80
81 class HttpParseError(Exception):
82     """
83     Error parsing HTTP request.
84     """
85
86
87 class HttpRequestParser(object):
88     """
89     A simple state machine for parsing HTTP requests.
90     """
91     HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE = range(5)
92     HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE']
93     
94     def __init__(self):
95         super(HttpRequestParser, self).__init__()
96         self.reset()
97
98     def reset(self):
99         """
100         Reset parser to initial state.
101         """
102         # Initial values for request data.
103         self.method = None
104         self.request_path = None
105         self.http_version = None
106         self.headers = {}
107         self.data = {}
108         
109         # Set initial state.
110         self.state = self.HTTP_NONE        
111
112     def handle(self, line):
113         """
114         Dispatch line to current state handler.
115         """
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)
120                 break
121         else:
122             raise HttpParseError('Unknown HTTP state')
123                 
124     def handleNone(self, line):
125         """
126         Pass line to next state.
127         """
128         self.state += 1
129         self.handle(line)
130
131     def handleRequest(self, line):
132         """
133         Retrieve HTTP method, request path and HTTP version from request.
134         """
135         self.method, self.request_path, self.http_version = line.split(' ')
136         self.state += 1
137
138     def handleHeaders(self, line):
139         """
140         Parse headers while not found an empty line.
141         """
142         if line:
143             key, value = line.split(': ')
144             self.headers[key] = value
145         else:
146             self.state += 1
147             self.handle(line)
148
149     def handleEmpty(self, line):
150         """
151         Empty line separator is found - proceed to next state.
152         """
153         self.state += 1
154
155     def handleMessage(self, line):
156         """
157         Append to message body.
158         """
159         self.data = dict(pair.split('=', 2) for pair in line.split('&'))
160
161         for k, v in self.data.items():
162             print k, '=>', v
163         print
164
165
166
167 class FreeswitchConfigGenerator(object):
168     """
169     Base class for generating XML configs.
170     """
171     
172     param_match = {}
173     section_name = None
174
175     def __init__(self, model):
176         self.model = model
177
178     def check_params(self, params):
179         for key, value in self.param_match.iteritems():
180             if params.get(key, None) != value:
181                 return False
182         else:
183             return True
184
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)
191
192     def generate_config(self, params):
193         return NotImplemented
194
195     def add_params(parent_elt, params):
196         for name, value in params:
197             etree.SubElement(parent_elt, 'param', name=name, value=value)
198             
199         
200 class SofiaConfGenerator(FreeswitchConfigGenerator):
201     """
202     Generates sofia.conf.xml config file.
203     """
204     param_match = {'section': 'configuration', 'key_value': 'sofia.conf'}
205     section_name = 'configuration'
206     config_name = 'sofia.conf'
207
208     def generate_config(self, params):
209         # Get base elements.
210         root_elt, section_elt = self.base_elements
211
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')
218
219         # Create all profiles for current host.
220         for profile in self.parent.get_profiles():
221             profile_elt = etree.SubElement(profiles_elt, 'profile')
222
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')
229
230             # Create settings for current profile.
231             settings_elt = etree.SubElement(profile_elt, 'settings')
232             params = (
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),
239                 ('nonce-ttl'. '60'),
240                 ('rtp-timer-name', 'soft'),
241                 ('codec-prefs', 'PCMU@20i'),
242                 ('debug', '1'),
243                 ('rfc2833-pt', '1'),
244                 ('dtmf-duration', '100'),
245                 ('codec-ms', '20'),
246                 ('accept-blind-reg', profile.accept_blind_registration),
247                 ('auth-calls', profile.authenticate_calls))
248             self.add_params(settings_elt, params)
249
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)
254                 params = (
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)
267
268         return root_elt