From 81e646de2740d4a378623d2351aefaabe43b4801 Mon Sep 17 00:00:00 2001 From: spag Date: Wed, 23 Jan 2013 11:34:42 +0100 Subject: prefer route variables in variables expansion --- misc/freeswitch/scripts/dialplan/router.lua | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'misc/freeswitch/scripts/dialplan') diff --git a/misc/freeswitch/scripts/dialplan/router.lua b/misc/freeswitch/scripts/dialplan/router.lua index 7e64d7b..cdcb58b 100644 --- a/misc/freeswitch/scripts/dialplan/router.lua +++ b/misc/freeswitch/scripts/dialplan/router.lua @@ -52,27 +52,27 @@ function Router.read_table(self, table_name) end -function Router.expand_variables(self, line, variables) +function Router.expand_variables(self, line, variables, variables2) return (line:gsub('{([%a%d%._]+)}', function(captured) - return common.str.try(variables, captured) or ''; + return common.str.try(variables, captured) or common.str.try(variables2, captured) or ''; end)) end -function Router.element_match(self, pattern, search_string, replacement) +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, self.variables)); + return true, search_string:gsub(pattern, self:expand_variables(replacement, route_variables, self.variables)); end return false; end -function Router.element_match_group(self, pattern, groups, replacement, use_key) +function Router.element_match_group(self, pattern, groups, replacement, use_key, route_variables) if type(groups) ~= 'table' then return false; end @@ -81,7 +81,7 @@ function Router.element_match_group(self, pattern, groups, replacement, use_key) if use_key then value = key; end - result, replaced_value = self:element_match(pattern, tostring(value), replacement); + result, replaced_value = self:element_match(pattern, tostring(value), replacement, route_variables); if result then return true, replaced_value; end @@ -94,7 +94,8 @@ function Router.route_match(self, route) gateway = 'gateway' .. route.endpoint_id, ['type'] = route.endpoint_type, id = route.endpoint_id, - channel_variables = {} + destination_number = common.str.try(self, 'caller.destination_number'), + channel_variables = {}, }; local route_matches = false; @@ -108,13 +109,16 @@ 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, self.variables); + replacement = self: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 or command == 'var' then - local search_string = tostring(common.str.try(self.caller, element.var_in)) - result, replacement = self:element_match(tostring(element.pattern), tostring(search_string), tostring(element.replacement)); + 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)); + 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)); + 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); result, replacement = self:element_match_group(tostring(element.pattern), groups, tostring(element.replacement), command == 'key'); -- cgit v1.2.3 From 0f26298b241fc92f05972a9e47a0cec01245dd10 Mon Sep 17 00:00:00 2001 From: spag Date: Thu, 24 Jan 2013 16:00:25 +0100 Subject: trigger voicemail notification --- misc/freeswitch/scripts/dialplan/voicemail.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'misc/freeswitch/scripts/dialplan') diff --git a/misc/freeswitch/scripts/dialplan/voicemail.lua b/misc/freeswitch/scripts/dialplan/voicemail.lua index 5d79ba3..4c96fbe 100644 --- a/misc/freeswitch/scripts/dialplan/voicemail.lua +++ b/misc/freeswitch/scripts/dialplan/voicemail.lua @@ -117,6 +117,7 @@ function Voicemail.leave(self, caller, phone_number) caller.uuid .. "'" ); caller:set_variable('voicemail_message_len', duration); + self:trigger_notification(caller); else caller:set_variable('voicemail_message_len'); end @@ -125,14 +126,11 @@ function Voicemail.leave(self, caller, phone_number) end -function Voicemail.send_notify(self, caller) - self.log:debug('VOICEMAIL_NOTIFY - account: ' .. self.record.auth_name .. ", id: " .. tostring(caller.uuid)); +function Voicemail.trigger_notification(self, caller) + local command = 'http_request.lua ' .. caller.uuid .. ' http://127.0.0.1/trigger/voicemail?sip_account_id=' .. tostring(self.id); - local file = io.popen("/opt/GS5/script/voicemail_new '" .. tostring(self.record.auth_name) .. "' '" .. tostring(caller.uuid) .. "' 2>&1"); - self.log:debug('VOICEMAIL_NOTIFY - result: ' .. tostring(file:read("*a"))); - file:close(); - - return true; + require 'common.fapi' + return common.fapi.FApi:new():execute('luarun', command); end -- cgit v1.2.3 From 1e0d9c3344b1726b2b95e67b59a7cde820374617 Mon Sep 17 00:00:00 2001 From: spag Date: Thu, 24 Jan 2013 16:07:51 +0100 Subject: trigger_notification method added --- misc/freeswitch/scripts/dialplan/fax.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'misc/freeswitch/scripts/dialplan') diff --git a/misc/freeswitch/scripts/dialplan/fax.lua b/misc/freeswitch/scripts/dialplan/fax.lua index aa29ff6..49a45d9 100644 --- a/misc/freeswitch/scripts/dialplan/fax.lua +++ b/misc/freeswitch/scripts/dialplan/fax.lua @@ -230,3 +230,10 @@ function Fax.insert_document(self, record) return self.database:query(sql_query); end + +function Fax.trigger_notification(self, fax_document_id, uuid) + local command = 'http_request.lua ' .. uuid .. ' http://127.0.0.1/trigger/fax?fax_account_id=' .. tostring(fax_document_id); + + require 'common.fapi' + return common.fapi.FApi:new():execute('luarun', command); +end -- cgit v1.2.3 From 108bab40025a41a03f42bdb6db12ca843a329d4a Mon Sep 17 00:00:00 2001 From: spag Date: Thu, 24 Jan 2013 16:08:14 +0100 Subject: trigger_notification on fax receive --- misc/freeswitch/scripts/dialplan/dialplan.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'misc/freeswitch/scripts/dialplan') diff --git a/misc/freeswitch/scripts/dialplan/dialplan.lua b/misc/freeswitch/scripts/dialplan/dialplan.lua index b92dc70..49d698b 100644 --- a/misc/freeswitch/scripts/dialplan/dialplan.lua +++ b/misc/freeswitch/scripts/dialplan/dialplan.lua @@ -565,6 +565,7 @@ function Dialplan.faxaccount(self, destination) if not fax_account:insert_document(fax_document) then self.log:error('FAX_RECEIVE - error inserting fax document to database - fax_account=', fax_account.id, '/', fax_account.uuid, ', file: ', fax_document.filename); end + fax_account:trigger_notification(fax_account.id, self.caller.uuid); end return { continue = false, code = 200, phrase = 'OK' } -- cgit v1.2.3 From a83fbe961572c3f2f0358e6a28d540c5fc3b4d89 Mon Sep 17 00:00:00 2001 From: spag Date: Fri, 25 Jan 2013 17:24:03 +0100 Subject: gateway technology independent --- misc/freeswitch/scripts/dialplan/sip_call.lua | 47 +++++++++++++++++---------- 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'misc/freeswitch/scripts/dialplan') diff --git a/misc/freeswitch/scripts/dialplan/sip_call.lua b/misc/freeswitch/scripts/dialplan/sip_call.lua index 3f56753..d1557e9 100644 --- a/misc/freeswitch/scripts/dialplan/sip_call.lua +++ b/misc/freeswitch/scripts/dialplan/sip_call.lua @@ -89,20 +89,21 @@ function SipCall.fork(self, destinations, arg ) for index, destination in ipairs(destinations) do local origination_variables = { 'gs_fork_index=' .. index } - self.log:info('FORK ', index, '/', #destinations, ' - ', destination.type, '=', destination.id, '/', destination.gateway or destination.uuid, '@', destination.node_id, ', number: ', destination.number, ', caller_id: "', destination.caller_id_name, '" <', destination.caller_id_number, '>'); + self.log:info('FORK ', index, '/', #destinations, ' - ', destination.type, '=', destination.id, '/', destination.uuid, '@', destination.node_id, ', number: ', destination.number, ', caller_id: "', destination.caller_id_name, '" <', destination.caller_id_number, '>'); if not common.str.to_b(arg.update_callee_display) then table.insert(origination_variables, 'ignore_display_updates=true'); end - if not destination.node_local or destination.type == 'node' then + if not destination.node_local then require 'common.node' - local node = nil; - if tonumber(destination.gateway) then - node = common.node.Node:new{ log = self.log, database = self.database }:find_by_id(tonumber(destination.gateway)); - else - node = common.node.Node:new{ log = self.log, database = self.database }:find_by_id(destination.node_id); + local node = common.node.Node:new{ log = self.log, database = self.database }:find_by_id(destination.node_id); + if node then + table.insert(origination_variables, 'sip_h_X-GS_node_id=' .. self.caller.local_node_id); + table.insert(dial_strings, '[' .. table.concat(origination_variables , ',') .. ']sofia/gateway/' .. node.record.name .. '/' .. destination.number); end + elseif destination.type == 'node' then + local node = common.node.Node:new{ log = self.log, database = self.database }:find_by_id(destination.id); if node then table.insert(origination_variables, 'sip_h_X-GS_node_id=' .. self.caller.local_node_id); table.insert(dial_strings, '[' .. table.concat(origination_variables , ',') .. ']sofia/gateway/' .. node.record.name .. '/' .. destination.number); @@ -129,18 +130,26 @@ function SipCall.fork(self, destinations, arg ) call_result = { code = 486, phrase = 'User busy', disposition = 'USER_BUSY' }; end elseif destination.type == 'gateway' then - if destination.caller_id_number then - table.insert(origination_variables, "origination_caller_id_number='" .. destination.caller_id_number .. "'"); - end - if destination.caller_id_name then - table.insert(origination_variables, "origination_caller_id_name='" .. destination.caller_id_name .. "'"); - end - if destination.channel_variables then - for key, value in pairs(destination.channel_variables) do - table.insert(origination_variables, tostring(key) .. "='" .. tostring(value) .. "'"); + require 'common.gateway' + local gateway = common.gateway.Gateway:new{ log = self.log, database = self.database}:find_by_id(destination.id); + + if gateway and gateway.outbound then + if destination.caller_id_number then + table.insert(origination_variables, "origination_caller_id_number='" .. destination.caller_id_number .. "'"); + end + if destination.caller_id_name then + table.insert(origination_variables, "origination_caller_id_name='" .. destination.caller_id_name .. "'"); end + if destination.channel_variables then + for key, value in pairs(destination.channel_variables) do + table.insert(origination_variables, tostring(key) .. "='" .. tostring(value) .. "'"); + end + end + + table.insert(dial_strings, '[' .. table.concat(origination_variables , ',') .. ']' .. gateway:call_url(destination.number)); + else + self.log:notice('FORK - gateway not found - gateway=', destination.id); end - table.insert(dial_strings, '[' .. table.concat(origination_variables , ',') .. ']sofia/gateway/' .. tostring(destination.gateway) .. '/' .. tostring(destination.number)); elseif destination.type == 'dial' then if destination.caller_id_number then table.insert(origination_variables, "origination_caller_id_number='" .. destination.caller_id_number .. "'"); @@ -173,8 +182,10 @@ function SipCall.fork(self, destinations, arg ) self.caller:execute('ring_ready'); end + local session_dialstring = '{local_var_clobber=true}' .. table.concat(dial_strings, ','); + self.log:debug('FORK SESSION_START - call_url: ', session_dialstring); local start_time = os.time(); - local session_callee = freeswitch.Session('{local_var_clobber=true}' .. table.concat(dial_strings, ','), self.caller.session); + local session_callee = freeswitch.Session(session_dialstring, self.caller.session); self.log:debug('FORK SESSION_INIT - dial_time: ', os.time() - start_time); local answer_result = self:wait_answer(self.caller.session, session_callee, arg.timeout, start_time); local fork_index = nil; -- cgit v1.2.3 From 45af07cb093eb0202967cf0d0fc058ca58f0b782 Mon Sep 17 00:00:00 2001 From: spag Date: Sun, 27 Jan 2013 10:17:21 +0100 Subject: added gateway technology --- misc/freeswitch/scripts/dialplan/dialplan.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'misc/freeswitch/scripts/dialplan') diff --git a/misc/freeswitch/scripts/dialplan/dialplan.lua b/misc/freeswitch/scripts/dialplan/dialplan.lua index 49d698b..ff4adc6 100644 --- a/misc/freeswitch/scripts/dialplan/dialplan.lua +++ b/misc/freeswitch/scripts/dialplan/dialplan.lua @@ -135,7 +135,7 @@ end function Dialplan.auth_gateway(self) require 'common.gateway' local gateway_class = common.gateway.Gateway:new{ log = self.log, database = self.database}; - local gateway = gateway_class:authenticate('sip', self.caller); + local gateway = gateway_class:authenticate(self.caller); if gateway then log:info('AUTH_GATEWAY - ', gateway.auth_source, ' ~ ', gateway.auth_pattern, ', gateway=', gateway.id, ', name: ', gateway.name, ', ip: ', self.caller.sip_contact_host); -- cgit v1.2.3