From 0442cd19bc9383669b506185356227361a16e442 Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Mon, 25 Feb 2013 09:29:40 -0500 Subject: call_forwards - polymorphism added --- misc/freeswitch/scripts/common/call_forwarding.lua | 55 ++++++++++++++++++++++ misc/freeswitch/scripts/common/phone_number.lua | 55 ++-------------------- misc/freeswitch/scripts/dialplan/dialplan.lua | 9 +++- 3 files changed, 67 insertions(+), 52 deletions(-) (limited to 'misc') diff --git a/misc/freeswitch/scripts/common/call_forwarding.lua b/misc/freeswitch/scripts/common/call_forwarding.lua index 400fcde..192c694 100644 --- a/misc/freeswitch/scripts/common/call_forwarding.lua +++ b/misc/freeswitch/scripts/common/call_forwarding.lua @@ -37,6 +37,61 @@ function CallForwarding.find_by_id(self, id) return nil end + +function CallForwarding.list_by_owner(self, call_forwardable_id, call_forwardable_type, caller_ids) + require 'common.str'; + + if not tonumber(call_forwardable_id) or common.str.blank(call_forwardable_type) then + return {}; + end + + local sql_query = 'SELECT \ + `a`.`destination` AS `number`, \ + `a`.`destinationable_id` AS `id`, \ + `a`.`destinationable_type` AS `type`, \ + `a`.`call_forwardable_id`, \ + `a`.`call_forwardable_type`, \ + `a`.`timeout`, `a`.`depth`, \ + `a`.`source`, \ + `b`.`value` AS `service` \ + FROM `call_forwards` `a` JOIN `call_forward_cases` `b` ON `a`.`call_forward_case_id` = `b`.`id` \ + WHERE `a`.`call_forwardable_id`= ' .. tonumber(call_forwardable_id) .. ' \ + AND `a`.`call_forwardable_type`= ' .. self.database:escape(call_forwardable_type, '"') .. ' \ + AND `a`.`active` IS TRUE'; + + local call_forwarding_entries = {}; + + self.database:query(sql_query, function(forwarding_entry) + local entry_match = false; + + if common.str.blank(forwarding_entry.source) then + entry_match = true; + else + local sources = common.str.strip_to_a(forwarding_entry.source, ',') + for source_index=1, #sources do + for caller_id_index=1, #caller_ids do + if caller_ids[caller_id_index]:match(sources[source_index]) then + entry_match = true; + self.log:debug('CALL_FORWARDING - source match: ', sources[source_index], ' ~ ', caller_ids[caller_id_index] ); + break; + end + end + end + end + + if entry_match then + call_forwarding_entries[forwarding_entry.service] = forwarding_entry; + self.log:debug('CALL_FORWARDING - ', call_forwardable_type, '=', call_forwardable_id, + ', service: ', forwarding_entry.service, + ', destination: ',forwarding_entry.type, '=', forwarding_entry.id, + ', number: ', forwarding_entry.number); + end + end) + + return call_forwarding_entries; +end + + function CallForwarding.presence_set(self, presence_state) require 'dialplan.presence' local presence = dialplan.presence.Presence:new(); diff --git a/misc/freeswitch/scripts/common/phone_number.lua b/misc/freeswitch/scripts/common/phone_number.lua index 6635296..d6ee42a 100644 --- a/misc/freeswitch/scripts/common/phone_number.lua +++ b/misc/freeswitch/scripts/common/phone_number.lua @@ -55,6 +55,8 @@ function PhoneNumber.find_by_number(self, number, phone_numberable_types) self.database:query(sql_query, function(number_entry) phone_number = PhoneNumber:new(self); phone_number.record = number_entry; + phone_number.id = tonumber(number_entry.id); + phone_number.uuid = number_entry.uuid; end) return phone_number; @@ -68,6 +70,8 @@ function PhoneNumber.find_all_by_owner(self, owner_id, owner_type) self.database:query(sql_query, function(number_entry) phone_numbers[tonumber(number_entry.id)] = PhoneNumber:new(self); phone_numbers[tonumber(number_entry.id)].record = number_entry; + phone_numbers[tonumber(number_entry.id)].id = tonumber(number_entry.id); + phone_numbers[tonumber(number_entry.id)].uuid = number_entry.uuid; end) return phone_numbers; @@ -94,57 +98,6 @@ function PhoneNumber.list_by_same_owner(self, number, owner_types) end end --- Retrieve call forwarding -function PhoneNumber.call_forwarding(self, caller_ids) - require 'common.str' - - sources = sources or {}; - table.insert(sources, ''); - - local sql_query = 'SELECT \ - `a`.`destination` AS `number`, \ - `a`.`call_forwardable_id` AS `id`, \ - `a`.`call_forwardable_type` AS `type`, \ - `a`.`timeout`, `a`.`depth`, \ - `a`.`source`, \ - `b`.`value` AS `service` \ - FROM `call_forwards` `a` JOIN `call_forward_cases` `b` ON `a`.`call_forward_case_id` = `b`.`id` \ - WHERE `a`.`phone_number_id`= ' .. tonumber(self.record.id) .. ' \ - AND `a`.`active` IS TRUE'; - - local call_forwarding = {} - - self.database:query(sql_query, function(forwarding_entry) - local entry_match = false; - - if common.str.blank(forwarding_entry.source) then - entry_match = true; - else - local sources = common.str.strip_to_a(forwarding_entry.source, ',') - for index, source in ipairs(sources) do - for index, caller_id in ipairs(caller_ids) do - if caller_id:match(source) then - entry_match = true; - self.log:debug('CALL_FORWARDING_GET - source match: ', source, ' ~ ', caller_id ); - break; - end - end - end - end - - if entry_match then - call_forwarding[forwarding_entry.service] = forwarding_entry; - self.log:debug('CALL_FORWARDING_GET - PhoneNumber=', self.record.id, '/', self.record.uuid, '@', self.record.gs_node_id, - ', number: ', self.record.number, - ', service: ', forwarding_entry.service, - ', destination: ',forwarding_entry.type, '=', forwarding_entry.id, - ', number: ', forwarding_entry.number); - end - end) - - return call_forwarding; -end - function PhoneNumber.call_forwarding_effective(self, service, source) local conditions = {} diff --git a/misc/freeswitch/scripts/dialplan/dialplan.lua b/misc/freeswitch/scripts/dialplan/dialplan.lua index c182fb1..7d9ac58 100644 --- a/misc/freeswitch/scripts/dialplan/dialplan.lua +++ b/misc/freeswitch/scripts/dialplan/dialplan.lua @@ -330,7 +330,13 @@ function Dialplan.destination_new(self, arg) destination.uuid = common.str.to_s(destination.phone_number.record.phone_numberable_uuid); destination.node_id = common.str.to_i(destination.phone_number.record.gs_node_id); if self.caller then - destination.call_forwarding = destination.phone_number:call_forwarding(self.caller.caller_phone_numbers); + require 'common.call_forwarding'; + local call_forwarding_class = common.call_forwarding.CallForwarding:new{ log = self.log, database = self.database } + destination.call_forwarding = call_forwarding_class:list_by_owner(destination.id, destination.type, self.caller.caller_phone_numbers); + for service, call_forwarding_entry in pairs(call_forwarding_class:list_by_owner(destination.phone_number.id, destination.phone_number.class, self.caller.caller_phone_numbers)) do + destination.call_forwarding[service] = call_forwarding_entry; + end + -- destination.call_forwarding = destination.phone_number:call_forwarding(self.caller.caller_phone_numbers); end elseif destination.type == 'unknown' then require 'common.sip_account' @@ -1018,6 +1024,7 @@ function Dialplan.run(self, destination) destination = self:destination_new(result.call_forwarding); self.caller.destination = destination; + self.caller.destination_number = destination.number; if not result.no_cdr and auth_account then require 'common.call_history' -- cgit v1.2.3