From ccb2ccc255218ec44a1694431cfbccd6308be6d5 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Wed, 17 Apr 2013 16:51:30 +0200 Subject: router function speeddial added --- misc/freeswitch/scripts/dialplan/router.lua | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (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 322c748..d6d00de 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -149,6 +149,14 @@ function Router.route_match(self, route) elseif command == 'hdr' then local search_string = self.caller:to_s('sip_h_' .. variable_name); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement)); + elseif command == 'fun' then + if self['fun_' .. variable_name] then + local arguments = {}; + for index, argument in ipairs(common.str.to_a(element.replacement, ',')) do + table.insert(arguments, common.array.expand_variables(argument, destination, self.variables)); + end + result, replacement = self['fun_' .. variable_name](self, unpack(arguments)) + end end end @@ -214,3 +222,32 @@ function Router.route_run(self, table_name, find_first) return routes; end end + + +function Router.fun_speeddial(self, number, name) + local owner_class = common.array.try(self, 'caller.auth_account.owner.class'); + local owner_id = common.array.try(self, 'caller.auth_account.owner.id') + + local user_id = nil; + local tenant_id = nil; + + if tostring(owner_class) == 'user' then + user_id = owner_id; + tenant_id = common.array.try(self, 'caller.auth_account.owner.record.current_nenant_id'); + elseif + tostring(owner_class) == 'tenant' then + tenant_id = owner_id; + end + + require 'dialplan.phone_book' + local phone_book_class = dialplan.phone_book.PhoneBook:new{ log = self.log, database = self.database } + local phone_book_entry = phone_book_class:find_entry_by_number_user_tenant({number}, user_id, tenant_id, 'speeddial'); + + if phone_book_entry then + local phone_numbers = phone_book_class:numbers(phone_book_entry.id, name, 'speeddial'); + for index, phone_number in ipairs(phone_numbers) do + self.log:info('SPEEDDIAL: ', number, ' => ', phone_number.number) + return true, phone_number.number; + end + end +end -- cgit v1.2.3 From 75047661f380c62dae7bab4942c9e8a8cf39501a Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Thu, 18 Apr 2013 08:30:06 +0200 Subject: logging --- misc/freeswitch/scripts/dialplan/router.lua | 13 +++++++++++-- 1 file changed, 11 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 d6d00de..dde527c 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -156,6 +156,13 @@ function Router.route_match(self, route) table.insert(arguments, common.array.expand_variables(argument, destination, self.variables)); end result, replacement = self['fun_' .. variable_name](self, unpack(arguments)) + if self.log_details or true then + if result then + self.log:debug('ELEMENT_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', replacement); + else + self.log:debug('ELEMENT_NO_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', tostring(replacement)); + end + end end end end @@ -233,7 +240,7 @@ function Router.fun_speeddial(self, number, name) if tostring(owner_class) == 'user' then user_id = owner_id; - tenant_id = common.array.try(self, 'caller.auth_account.owner.record.current_nenant_id'); + tenant_id = common.array.try(self, 'caller.auth_account.owner.record.current_tenant_id'); elseif tostring(owner_class) == 'tenant' then tenant_id = owner_id; @@ -243,10 +250,12 @@ function Router.fun_speeddial(self, number, name) local phone_book_class = dialplan.phone_book.PhoneBook:new{ log = self.log, database = self.database } local phone_book_entry = phone_book_class:find_entry_by_number_user_tenant({number}, user_id, tenant_id, 'speeddial'); + self.log:debug('SPEEDDIAL - user=', user_id, ', tenant=', tenant_id, ', entry: "', common.array.try(phone_book_entry, 'phone_book_name'), '" => "', common.array.try(phone_book_entry, 'caller_id_name'), '"'); + if phone_book_entry then local phone_numbers = phone_book_class:numbers(phone_book_entry.id, name, 'speeddial'); for index, phone_number in ipairs(phone_numbers) do - self.log:info('SPEEDDIAL: ', number, ' => ', phone_number.number) + self.log:info('SPEEDDIAL - ', number, ' => ', phone_number.number) return true, phone_number.number; end end -- cgit v1.2.3 From 7804dbb985f25c8bafbcfad1f9345d14a9a6adc3 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Tue, 23 Apr 2013 22:04:07 +0200 Subject: elements loading order fixed --- misc/freeswitch/scripts/dialplan/router.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 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 dde527c..10551f1 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -38,15 +38,19 @@ function Router.read_table(self, table_name, force_reload) JOIN `route_elements` `b` ON `a`.`id` = `b`.`call_route_id`\ WHERE `a`.`routing_table` = "' .. table_name .. '" \ ORDER BY `a`.`position`, `b`.`position`'; - - local last_id = 0; + + local call_routes = {}; + self.database:query(sql_query, function(route) - if last_id ~= tonumber(route.call_route_id) then - last_id = tonumber(route.call_route_id); - table.insert(routing_table, {id = route.call_route_id, name = route.name, endpoint_type = route.endpoint_type , endpoint_id = route.endpoint_id, elements = {} }); + if call_routes[route.call_route_id] then + call_route = call_routes[route.call_route_id]; + else + call_route = {id = route.call_route_id, name = route.name, endpoint_type = route.endpoint_type , endpoint_id = route.endpoint_id, elements = {} }; + call_routes[route.call_route_id] = call_route; + table.insert(routing_table, call_route); end - table.insert(routing_table[#routing_table].elements, { + table.insert(call_route.elements, { var_in = route.var_in, var_out = route.var_out, pattern = route.pattern, -- cgit v1.2.3 From bb4ba3747de3f06970dddf4171305d473c11aa7f Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Wed, 12 Jun 2013 12:21:33 +0200 Subject: replacement on no_match fixed --- misc/freeswitch/scripts/dialplan/router.lua | 3 +++ 1 file changed, 3 insertions(+) (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 10551f1..50653d8 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -173,6 +173,9 @@ function Router.route_match(self, route) if element.action == 'not_match' then result = not result; + if result then + replacement = tostring(element.replacement); + end end if not result then -- cgit v1.2.3 From 66ef95959eabf843832eeae4c844cbd05eabd5e3 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Fri, 14 Jun 2013 10:55:28 +0200 Subject: expression function added --- misc/freeswitch/scripts/dialplan/router.lua | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (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 50653d8..c2b229d 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -160,13 +160,21 @@ function Router.route_match(self, route) table.insert(arguments, common.array.expand_variables(argument, destination, self.variables)); end result, replacement = self['fun_' .. variable_name](self, unpack(arguments)) - if self.log_details or true then + if not common.str.blank(element.pattern) then + if self.log_details then + self.log:debug('ELEMENT_FUNCTION - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', replacement); + end + result, replacement = self:element_match(tostring(element.pattern), tostring(replacement), tostring(replacement)); + end + if self.log_details then if result then self.log:debug('ELEMENT_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', replacement); else self.log:debug('ELEMENT_NO_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', tostring(replacement)); end end + else + self.log:error('ELEMENT_FUNCTION - function not found: ', 'fun_' .. variable_name); end end end @@ -267,3 +275,14 @@ function Router.fun_speeddial(self, number, name) end end end + + +function Router.fun_expression(self, expression_str) + expression_str = expression_str:gsub('[^%d%.%+%(%)%^%%%*%/-<>]', ''); + result = loadstring("return (" .. expression_str .. ")")(); + if result then + return true, result; + else + return false, result; + end +end -- cgit v1.2.3 From e48434bf0e1bac7687d08734578c01367b880562 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Fri, 14 Jun 2013 11:14:04 +0200 Subject: expression can be nil, empty or invalid --- misc/freeswitch/scripts/dialplan/router.lua | 20 ++++++++++++++++++-- 1 file changed, 18 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 c2b229d..b762010 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -278,8 +278,24 @@ end function Router.fun_expression(self, expression_str) - expression_str = expression_str:gsub('[^%d%.%+%(%)%^%%%*%/-<>]', ''); - result = loadstring("return (" .. expression_str .. ")")(); + if common.str.blank(expression_str) then + self.log:error('EXPRESSION - no expression specified'); + return false; + end + + expression_str = expression_str:gsub('[^%d%.%+%(%)%^%%%*%/-<>=!|&]', ''); + expression_str = expression_str:gsub('&&', ' and '); + expression_str = expression_str:gsub('||', ' or '); + expression_str = expression_str:gsub('!=', '~='); + + local expression = loadstring("return (" .. expression_str .. ")") + + if not expression then + self.log:error('EXPRESSION - invalid expression: ', expression_str); + return false; + end + + result = expression(); if result then return true, result; else -- cgit v1.2.3 From 4f5c5e93fe01dec4eea933ab7cb805d95b0bd647 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Fri, 14 Jun 2013 11:59:15 +0200 Subject: action:set added --- misc/freeswitch/scripts/dialplan/router.lua | 72 ++++++++++++++++++----------- 1 file changed, 46 insertions(+), 26 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 b762010..4e6398e 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -109,6 +109,37 @@ function Router.element_match_group(self, pattern, groups, replacement, use_key, end +function Router.element_run_function(self, variable_name, element, destination) + local result = nil; + local replacement = nil; + + if self['fun_' .. variable_name] then + local arguments = {}; + for index, argument in ipairs(common.str.to_a(element.replacement, ',')) do + table.insert(arguments, common.array.expand_variables(argument, destination, self.variables)); + end + result, replacement = self['fun_' .. variable_name](self, unpack(arguments)) + if not common.str.blank(element.pattern) then + if self.log_details then + self.log:debug('ELEMENT_FUNCTION - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', replacement); + end + result, replacement = self:element_match(tostring(element.pattern), tostring(replacement), tostring(replacement)); + end + if self.log_details then + if result then + self.log:debug('ELEMENT_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', replacement); + else + self.log:debug('ELEMENT_NO_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', tostring(replacement)); + end + end + else + self.log:error('ELEMENT_FUNCTION - function not found: ', 'fun_' .. variable_name); + end + + return result, replacement; +end + + function Router.route_match(self, route) local destination = { gateway = 'gateway' .. route.endpoint_id, @@ -132,9 +163,19 @@ function Router.route_match(self, route) 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; - replacement = common.array.expand_variables(element.replacement, destination, self.variables); + if element.action == 'set' then + if common.str.blank(element.var_in) then + result = true; + replacement = common.array.expand_variables(element.replacement, destination, self.variables); + else + local command, variable_name = common.str.partition(element.var_in, ':'); + if command == 'fun' then + result, replacement = self:element_run_function(variable_name, element, destination); + else + result = true; + replacement = common.array.expand_variables(element.replacement, destination, self.variables); + end + end else local command, variable_name = common.str.partition(element.var_in, ':'); @@ -154,28 +195,7 @@ function Router.route_match(self, route) local search_string = self.caller:to_s('sip_h_' .. variable_name); result, replacement = self:element_match(tostring(element.pattern), search_string, tostring(element.replacement)); elseif command == 'fun' then - if self['fun_' .. variable_name] then - local arguments = {}; - for index, argument in ipairs(common.str.to_a(element.replacement, ',')) do - table.insert(arguments, common.array.expand_variables(argument, destination, self.variables)); - end - result, replacement = self['fun_' .. variable_name](self, unpack(arguments)) - if not common.str.blank(element.pattern) then - if self.log_details then - self.log:debug('ELEMENT_FUNCTION - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', replacement); - end - result, replacement = self:element_match(tostring(element.pattern), tostring(replacement), tostring(replacement)); - end - if self.log_details then - if result then - self.log:debug('ELEMENT_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', replacement); - else - self.log:debug('ELEMENT_NO_MATCH - function: ', variable_name, '(', table.concat(arguments, ', '), ') => ', tostring(replacement)); - end - end - else - self.log:error('ELEMENT_FUNCTION - function not found: ', 'fun_' .. variable_name); - end + result, replacement = self:element_run_function(variable_name, element, destination); end end @@ -282,7 +302,7 @@ function Router.fun_expression(self, expression_str) self.log:error('EXPRESSION - no expression specified'); return false; end - + expression_str = expression_str:gsub('[^%d%.%+%(%)%^%%%*%/-<>=!|&]', ''); expression_str = expression_str:gsub('&&', ' and '); expression_str = expression_str:gsub('||', ' or '); -- cgit v1.2.3 From b3a96040433edc2ee2a1d85cf989b6d6adb60ee2 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Thu, 20 Jun 2013 11:24:29 +0200 Subject: previous behavior of set action restored --- misc/freeswitch/scripts/dialplan/router.lua | 20 ++++++-------------- 1 file changed, 6 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 4e6398e..24d2462 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -22,6 +22,7 @@ function Router.new(self, arg) self.variables = arg.variables or {}; self.log_details = arg.log_details; self.routing_tables = {}; + return object; end @@ -75,8 +76,9 @@ function Router.element_match(self, pattern, search_string, replacement, route_v 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); + self.log:debug('ELEMENT_MATCH - ', search_string, ' ~= ', pattern, ' => ', replacement, ' => ', result); end + return true, result; end @@ -163,19 +165,9 @@ function Router.route_match(self, route) end if element.action ~= 'none' then - if element.action == 'set' then - if common.str.blank(element.var_in) then - result = true; - replacement = common.array.expand_variables(element.replacement, destination, self.variables); - else - local command, variable_name = common.str.partition(element.var_in, ':'); - if command == 'fun' then - result, replacement = self:element_run_function(variable_name, element, destination); - else - result = true; - replacement = common.array.expand_variables(element.replacement, destination, self.variables); - end - end + if common.str.blank(element.var_in) and common.str.blank(element.pattern) and element.action == 'set' then + result = true; + replacement = common.array.expand_variables(element.replacement, destination, self.variables); else local command, variable_name = common.str.partition(element.var_in, ':'); -- cgit v1.2.3 From 22c5ce2ab518a548428320ae38818fe1f16ce567 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Thu, 20 Jun 2013 11:36:37 +0200 Subject: pattern can be ignored if action=set and var_in is blank --- misc/freeswitch/scripts/dialplan/router.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (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 24d2462..20b833b 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -165,7 +165,7 @@ function Router.route_match(self, route) end if element.action ~= 'none' then - if common.str.blank(element.var_in) and common.str.blank(element.pattern) and element.action == 'set' then + if common.str.blank(element.var_in) and element.action == 'set' then result = true; replacement = common.array.expand_variables(element.replacement, destination, self.variables); else -- cgit v1.2.3