From 45b3d318e2d495fe1558f504dbe8c6bdaab41b98 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Mon, 11 Mar 2013 07:09:40 -0400 Subject: use array module --- misc/freeswitch/scripts/dialplan/router.lua | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'misc/freeswitch/scripts/dialplan/router.lua') diff --git a/misc/freeswitch/scripts/dialplan/router.lua b/misc/freeswitch/scripts/dialplan/router.lua index 8473c2b..277958a 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -9,6 +9,7 @@ Router = {} -- create route object function Router.new(self, arg) require 'common.str'; + require 'common.array'; arg = arg or {} object = arg.object or {} setmetatable(object, self); @@ -60,20 +61,13 @@ function Router.read_table(self, table_name, force_reload) end -function Router.expand_variables(self, line, variables, variables2) - return (line:gsub('{([%a%d%._]+)}', function(captured) - return common.str.try(variables, captured) or common.str.try(variables2, captured) or ''; - end)) -end - - function Router.element_match(self, pattern, search_string, replacement, route_variables) local success, result = pcall(string.find, search_string, pattern); if not success then self.log:error('ELEMENT_MATCH - table error - pattern: ', pattern, ', search_string: ', search_string); elseif result then - return true, search_string:gsub(pattern, self:expand_variables(replacement, route_variables, self.variables)); + return true, search_string:gsub(pattern, common.array.expand_variables(replacement, route_variables, self.variables)); end return false; @@ -102,7 +96,7 @@ function Router.route_match(self, route) gateway = 'gateway' .. route.endpoint_id, ['type'] = route.endpoint_type, id = route.endpoint_id, - destination_number = common.str.try(self, 'caller.destination_number'), + destination_number = common.array.try(self, 'caller.destination_number'), channel_variables = {}, }; @@ -117,18 +111,18 @@ function Router.route_match(self, route) if element.action ~= 'none' then if common.str.blank(element.var_in) or common.str.blank(element.pattern) and element.action == 'set' then result = true; - replacement = self:expand_variables(element.replacement, destination, self.variables); + replacement = common.array.expand_variables(element.replacement, destination, self.variables); else local command, variable_name = common.str.partition(element.var_in, ':'); if not command or not variable_name then - local search_string = tostring(common.str.try(destination, element.var_in) or common.str.try(self.caller, element.var_in)); + local search_string = tostring(common.array.try(destination, element.var_in) or common.array.try(self.caller, element.var_in)); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement), destination); elseif command == 'var' then - local search_string = tostring(common.str.try(self.caller, element.var_in)); + local search_string = tostring(common.array.try(self.caller, element.var_in)); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement)); elseif command == 'key' or command == 'val' then - local groups = common.str.try(self.caller, variable_name); + local groups = common.array.try(self.caller, variable_name); result, replacement = self:element_match_group(tostring(element.pattern), groups, tostring(element.replacement), command == 'key'); elseif command == 'chv' then local search_string = self.caller:to_s(variable_name); @@ -151,7 +145,7 @@ function Router.route_match(self, route) if not common.str.blank(element.var_out) then local command, variable_name = common.str.partition(element.var_out, ':'); if not command or not variable_name or command == 'var' then - common.str.set(destination, element.var_out, replacement); + common.array.set(destination, element.var_out, replacement); elseif command == 'chv' then destination.channel_variables[variable_name] = replacement; elseif command == 'hdr' then -- cgit v1.2.3 From f3842463e70d5d5bb557f887c62c68509a523825 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Wed, 13 Mar 2013 11:54:16 -0400 Subject: return route_id --- misc/freeswitch/scripts/dialplan/router.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'misc/freeswitch/scripts/dialplan/router.lua') diff --git a/misc/freeswitch/scripts/dialplan/router.lua b/misc/freeswitch/scripts/dialplan/router.lua index 277958a..2d82f91 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -98,6 +98,7 @@ function Router.route_match(self, route) id = route.endpoint_id, destination_number = common.array.try(self, 'caller.destination_number'), channel_variables = {}, + route_id = route.id, }; local route_matches = false; -- cgit v1.2.3 From dc212986e401335fbd27216e029889f0cd941340 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Wed, 13 Mar 2013 13:42:09 -0400 Subject: log_details flag added --- misc/freeswitch/scripts/dialplan/router.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'misc/freeswitch/scripts/dialplan/router.lua') diff --git a/misc/freeswitch/scripts/dialplan/router.lua b/misc/freeswitch/scripts/dialplan/router.lua index 2d82f91..751d24f 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -20,6 +20,7 @@ function Router.new(self, arg) self.routes = arg.routes or {}; self.caller = arg.caller; self.variables = arg.variables or {}; + self.log_details = arg.log_details; self.routing_tables = {}; return object; end @@ -65,11 +66,19 @@ function Router.element_match(self, pattern, search_string, replacement, route_v local success, result = pcall(string.find, search_string, pattern); if not success then - self.log:error('ELEMENT_MATCH - table error - pattern: ', pattern, ', search_string: ', search_string); + self.log:error('ELEMENT_ERROR - table error - pattern: ', pattern, ', search_string: ', search_string); elseif result then - return true, search_string:gsub(pattern, common.array.expand_variables(replacement, route_variables, self.variables)); + local replace_by = common.array.expand_variables(replacement, route_variables, self.variables) + result = search_string:gsub(pattern, replace_by); + if self.log_details then + self.log:debug('ELEMENT_MATCH - ', search_string, ' ~= ', pattern, ' => ', replacement, ' => ', replace_by); + end + return true, result; end + if self.log_details then + self.log:debug('ELEMENT_NO_MATCH - ', search_string, ' != ', pattern); + end return false; end @@ -109,6 +118,10 @@ function Router.route_match(self, route) local element = route.elements[index]; + if self.log_details then + self.log:debug('ROUTE_ELEMENT ', element.id, ' - var_in: ', element.var_in, ', var_out: ', element.var_out, ', action: ', element.action, ', mandatory: ', element.mandatory); + end + if element.action ~= 'none' then if common.str.blank(element.var_in) or common.str.blank(element.pattern) and element.action == 'set' then result = true; -- cgit v1.2.3 From a6b3a069c2723e321ebfb79a06026bfe97cec9e6 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Fri, 15 Mar 2013 08:40:36 -0400 Subject: prefer destination variables --- misc/freeswitch/scripts/dialplan/router.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'misc/freeswitch/scripts/dialplan/router.lua') diff --git a/misc/freeswitch/scripts/dialplan/router.lua b/misc/freeswitch/scripts/dialplan/router.lua index 751d24f..c335186 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -83,11 +83,16 @@ function Router.element_match(self, pattern, search_string, replacement, route_v end -function Router.element_match_group(self, pattern, groups, replacement, use_key, route_variables) +function Router.element_match_group(self, pattern, groups, replacement, use_key, route_variables, variable_name) if type(groups) ~= 'table' then + self.log:debug('ELEMENT_FIND_IN_ARRAY - no such array: ', variable_name, ', use_keys: ', tostring(use_key)); return false; end + if self.log_details then + self.log:debug('ELEMENT_FIND_IN_ARRAY - array: ', variable_name, ', use_keys: ', tostring(use_key)); + end + for key, value in pairs(groups) do if use_key then value = key; @@ -136,8 +141,8 @@ function Router.route_match(self, route) local search_string = tostring(common.array.try(self.caller, element.var_in)); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement)); elseif command == 'key' or command == 'val' then - local groups = common.array.try(self.caller, variable_name); - result, replacement = self:element_match_group(tostring(element.pattern), groups, tostring(element.replacement), command == 'key'); + local groups = common.array.try(destination, variable_name) or common.array.try(self.caller, variable_name); + result, replacement = self:element_match_group(tostring(element.pattern), groups, tostring(element.replacement), command == 'key', destination, variable_name); elseif command == 'chv' then local search_string = self.caller:to_s(variable_name); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement)); -- cgit v1.2.3 From 98971a87f60055e3047d9d09144a071557404f84 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Thu, 21 Mar 2013 08:47:51 +0100 Subject: wrong source variable fixed --- misc/freeswitch/scripts/dialplan/router.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'misc/freeswitch/scripts/dialplan/router.lua') diff --git a/misc/freeswitch/scripts/dialplan/router.lua b/misc/freeswitch/scripts/dialplan/router.lua index c335186..322c748 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -138,7 +138,7 @@ function Router.route_match(self, route) local search_string = tostring(common.array.try(destination, element.var_in) or common.array.try(self.caller, element.var_in)); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement), destination); elseif command == 'var' then - local search_string = tostring(common.array.try(self.caller, element.var_in)); + local search_string = tostring(common.array.try(self.caller, variable_name)); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement)); elseif command == 'key' or command == 'val' then local groups = common.array.try(destination, variable_name) or common.array.try(self.caller, variable_name); @@ -193,7 +193,10 @@ function Router.route_run(self, table_name, find_first) local routes = {}; if type(routing_table) == 'table' then - for index=1, #routing_table do + for index=1, #routing_table do + if self.log_details then + self.log:info('ROUTE_',table_name:upper(),' ', index,' - ', table_name,'=', routing_table[index].id, '/', routing_table[index].name); + end local route = self:route_match(routing_table[index]); if route then table.insert(routes, route); -- cgit v1.2.3