Added user directory config generation. Not tested yet.
authorStas Shtin <antisvin@gmail.com>
Sun, 18 Apr 2010 20:37:00 +0000 (00:37 +0400)
committerStas Shtin <antisvin@gmail.com>
Sun, 18 Apr 2010 20:37:00 +0000 (00:37 +0400)
src/ipypbx/http.py

index 85b7062..b470c4b 100644 (file)
@@ -222,12 +222,18 @@ class FreeswitchConfigGenerator(object):
     """
     
     param_match = {}
-    section_name = None
 
     def __init__(self, model, parent):
         self.model = model
         self.parent = parent
 
+    def database(self):
+        """
+        Return database instance.
+        """
+        return self.model.controllers['connection'].model.database()
+    database = property(database)
+
     def canHandle(self, params):
         for key, value in self.param_match.iteritems():
             if params.get(key, None) != value:
@@ -238,7 +244,7 @@ class FreeswitchConfigGenerator(object):
     def baseElements(self):
         root_elt = etree.Element('document', type='freeswitch/xml')
         section_elt = etree.SubElement(
-            root_elt, 'section', name=self.section_name)
+            root_elt, 'section', name=self.param_match['section'])
         return root_elt, section_elt
     baseElements = property(baseElements)
 
@@ -257,7 +263,6 @@ 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):
@@ -270,7 +275,7 @@ class SofiaConfGenerator(FreeswitchConfigGenerator):
             description='%s config' % self.config_name)
         profiles_elt = etree.SubElement(configuration_elt, 'profiles')
 
-        database = self.model.controllers['connection'].model.database()
+        database = self.database
         
         # Create all profiles for current host.
         profiles_query = database.exec_(
@@ -347,3 +352,76 @@ class SofiaConfGenerator(FreeswitchConfigGenerator):
                 self.addParams(gateway_elt, params)
 
         return root_elt    
+
+class DirectoryGenerator(FreeswitchConfigGenerator):
+    """
+    Generates user directory.
+    """
+    param_match = {'section': 'directory'}
+
+    def generateConfig(self, params):
+        #Get base elemenets.
+        root_elt, section_elt = self.baseELements
+
+        database = self.database
+
+        # Find profile id from params.
+        profile_query = database.exec_(
+            '''
+            select id from ipypbxweb_sipprofile
+            where name= '%s' and connection_id = %i limit 1
+            ''' % (params['profile'], self.parent.connection_id))
+
+        _ok = False
+        if profile_query.next():
+            profile_id, _ok = profile_query.value(0).toInt()
+
+        if not _ok:
+            # Matching SIP profile not found.
+            return
+        
+        # List all domains for this profile.        
+        domains_query = database.exec_(
+            '''
+            select id, host_name from ipypbxweb_domain
+            where sip_profile_id = %i
+            ''' % profile_id)
+
+        while domains_query.next():
+            domain_id, _ok = domains_query.value(0).toInt()
+
+            # Create domaim element.
+            domain_elt = etree.SubElement(
+                section_elt, 'domain', name=domains_query.value(1).toString())
+            
+            # TODO: add domain params section if we need it, i.e.:
+            #<params>
+            #     <param name="dial-string"
+            #            value="{presence_id=${dialed_user}@${dialed_domain}}$\
+            #                   {sofia_contact(${dialed_user}@${dialed_domain})}"/>
+            #</params>            
+
+            # For new we put all users into one group called default.
+            groups_elt = etree.SubElement(domain_elt, 'groups')
+            group_elt = etree.SubElement(groups_elt, 'group', name='default')
+
+            users_elt = etree.SubElement(group_elt, 'users')
+
+            users_query = database.exec_(
+                '''
+                select user_id, password from ipypbxweb_endpoint
+                where domain_id = %i
+                ''' % domain_id)
+
+            # Create user entries for all endpoints for this domain.
+            while users_query.next():
+                user_elt = etree.SubElement(
+                    users_elt, 'user', id=users_query.value(0).toString())
+
+                # Specify endpoint password.
+                params = (
+                    ('password', users_query.value(1).toString()),
+                    )
+                self.addParams(user_elt, params)
+
+        return root_elt