diff options
-rw-r--r-- | misc/freeswitch/scripts/common/gateway.lua | 122 | ||||
-rw-r--r-- | misc/freeswitch/scripts/configuration.lua | 31 |
2 files changed, 140 insertions, 13 deletions
diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua new file mode 100644 index 0000000..2c50c0f --- /dev/null +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -0,0 +1,122 @@ +-- Gemeinschaft 5 module: gateway class +-- (c) AMOOMA GmbH 2013 +-- + +module(...,package.seeall) + +Gateway = {} + +-- Create Gateway object +function Gateway.new(self, arg) + arg = arg or {} + object = arg.object or {} + setmetatable(object, self); + self.__index = self; + self.class = 'gateway'; + self.log = arg.log; + self.database = arg.database; + self.record = arg.record; + return object; +end + + +function Gateway.list(self, technology) + technology = technology or 'sip'; + local sql_query = 'SELECT * FROM `gateways` WHERE (`outbound` IS TRUE OR `inbound` IS TRUE) AND `technology` = "' .. technology .. '"'; + local gateways = {}; + self.database:query(sql_query, function(entry) + table.insert(gateways, entry); + end) + + return gateways; +end + + +function Gateway.find_by_sql(self, where) + local sql_query = 'SELECT * FROM `gateways` WHERE ' .. where .. ' LIMIT 1'; + + local gateway = nil; + self.database:query(sql_query, function(entry) + gateway = Gateway:new(self); + gateway.record = entry; + gateway.id = tonumber(entry.id); + gateway.uuid = entry.uuid; + end) + + return gateway; +end + + +-- find gateway by id +function Gateway.find_by_id(self, id) + local sql_query = '`id`= ' .. tonumber(id); + return self:find_by_sql(sql_query); +end + +-- find gateway name +function Gateway.find_by_name(self, name) + local sql_query = '`name`= "' .. name .. '"'; + + return self:find_by_sql(sql_query); +end + + +function Gateway.profile_get(self, gateway_id) + local sql_query = 'SELECT `value` FROM `gateway_settings` WHERE `gateway_id` = ' .. tonumber(gateway_id) .. ' AND `name` = "profile" LIMIT 1'; + + return self.database:query_return_value(sql_query); +end + + +function Gateway.config_table_get(self, config_table, gateway_id) + require 'common.str' + + local sql_query = 'SELECT * FROM `'.. config_table ..'` WHERE `gateway_id` = ' .. tonumber(gateway_id); + + local settings = {}; + self.database:query(sql_query, function(entry) + local p_class_type = common.str.strip(entry.class_type):lower(); + local p_name = common.str.strip(entry.name):lower(); + + if p_class_type == 'boolean' then + settings[p_name] = common.str.to_b(entry.value); + elseif p_class_type == 'integer' then + settings[p_name] = common.str.to_i(entry.value); + else + settings[p_name] = tostring(entry.value); + end + end) + + return settings +end + + +function Gateway.parameters_build(self, gateway_id) + local settings = self:config_table_get('gateway_settings', gateway_id); + local parameters = { + realm = settings.domain, + extension = 'auto_to_user', + }; + + require 'common.str' + + if common.str.blank(settings.username) then + parameters.username = 'gateway' .. gateway_id; + parameters.register = false; + else + parameters.username = settings.username; + parameters.register = true; + end + + if common.str.blank(settings.password) then + parameters.password = 'gateway' .. gateway_id; + else + parameters.password = settings.password; + end + + for key, value in pairs(self:config_table_get('gateway_parameters', gateway_id)) do + parameters[key] = value; + end + + return parameters; +end diff --git a/misc/freeswitch/scripts/configuration.lua b/misc/freeswitch/scripts/configuration.lua index 3ef1a9c..b4dc0f6 100644 --- a/misc/freeswitch/scripts/configuration.lua +++ b/misc/freeswitch/scripts/configuration.lua @@ -28,25 +28,29 @@ function nodes(database, local_node_id) return gateways_xml; end -function gateways(profile_name) + +function gateways(database, profile_name) require 'configuration.simple_xml' local xml = configuration.simple_xml.SimpleXml:new(); - require 'common.configuration_file' - local gateways_xml = ''; - local gateways = common.configuration_file.get('/opt/freeswitch/scripts/ini/gateways.ini', false); + require 'common.str' - if not gateways then - return ''; - end + require 'common.gateway' + local gateway_class = common.gateway.Gateway:new{ log = log, database = database}; + local gateways = gateway_class:list('sip'); + + local gateways_xml = ''; + for index=1, #gateways do + local gateway = gateways[index]; + local gateway_profile = gateway_class:profile_get(gateway.id); + if tostring(gateway_profile) == profile_name or (profile_name == 'gemeinschaft' and common.str.blank(gateway_profile)) then + log:debug('GATEWAY - name: ', gateway.name); + local parameters = gateway_class:parameters_build(gateway.id); - for gateway_name, gateway_parameters in pairs(gateways) do - if tostring(gateway_parameters.profile) == profile_name then - log:debug('GATEWAY - name: ', gateway_name, ', address: ', gateway_parameters.proxy); gateways_xml = gateways_xml .. xml:element{ 'gateway', - name = gateway_name, - xml:from_hash('param', gateway_parameters, 'name', 'value'), + name = gateway.name, + xml:from_hash('param', parameters, 'name', 'value'), }; end end @@ -54,6 +58,7 @@ function gateways(profile_name) return gateways_xml; end + function profile(database, sofia_ini, profile_name, index, domains, node_id) require 'configuration.simple_xml' local xml = configuration.simple_xml.SimpleXml:new(); @@ -81,7 +86,7 @@ function profile(database, sofia_ini, profile_name, index, domains, node_id) log:debug('SOFIA_PROFILE ', index,' - name: ', profile_name, ' - no domains'); end - local gateways_xml = gateways(profile_name); + local gateways_xml = gateways(database, profile_name); if index == 1 then gateways_xml = gateways_xml .. nodes(database, node_id); |