2012-06-04 16:58:40 +02:00
|
|
|
--
|
|
|
|
|
-- FusionPBX
|
|
|
|
|
-- Version: MPL 1.1
|
|
|
|
|
--
|
|
|
|
|
-- The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
-- 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
-- the License. You may obtain a copy of the License at
|
|
|
|
|
-- http://www.mozilla.org/MPL/
|
|
|
|
|
--
|
|
|
|
|
-- Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
-- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
-- for the specific language governing rights and limitations under the
|
|
|
|
|
-- License.
|
|
|
|
|
--
|
|
|
|
|
-- The Original Code is FusionPBX
|
|
|
|
|
--
|
|
|
|
|
-- The Initial Developer of the Original Code is
|
|
|
|
|
-- Mark J Crane <markjcrane@fusionpbx.com>
|
2016-05-13 04:43:57 +02:00
|
|
|
-- Copyright (C) 2010 - 2016
|
2012-06-04 16:58:40 +02:00
|
|
|
-- the Initial Developer. All Rights Reserved.
|
|
|
|
|
--
|
|
|
|
|
-- Contributor(s):
|
|
|
|
|
-- Mark J Crane <markjcrane@fusionpbx.com>
|
2014-08-23 16:03:23 +02:00
|
|
|
-- Errol W Samuels <ewsamuels@gmail.com>
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2012-12-02 09:38:24 +01:00
|
|
|
--user defined variables
|
2015-10-25 07:57:20 +01:00
|
|
|
local extension = argv[1];
|
2016-04-18 15:06:27 +02:00
|
|
|
local direction = argv[2] or extension and 'inbound' or 'all';
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
-- we can use any number because other box should check sip_h_X_*** headers first
|
|
|
|
|
local pickup_number = '*8' -- extension and '**' or '*8'
|
|
|
|
|
|
2012-12-02 09:38:24 +01:00
|
|
|
--set the debug options
|
|
|
|
|
debug["sql"] = false;
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2013-03-31 10:18:41 +02:00
|
|
|
--include config.lua
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.config";
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--add the function
|
|
|
|
|
require "resources.functions.explode";
|
|
|
|
|
require "resources.functions.trim";
|
|
|
|
|
require "resources.functions.channel_utils";
|
2013-03-31 10:18:41 +02:00
|
|
|
|
2015-01-30 02:32:38 +01:00
|
|
|
--prepare the api object
|
|
|
|
|
api = freeswitch.API();
|
|
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--Get intercept logger
|
|
|
|
|
local log = require "resources.functions.log".intercept
|
|
|
|
|
|
|
|
|
|
--include database class
|
|
|
|
|
local Database = require "resources.functions.database"
|
2015-01-30 02:32:38 +01:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--get the hostname
|
|
|
|
|
local hostname = trim(api:execute("switchname", ""));
|
|
|
|
|
|
|
|
|
|
-- redirect call to another box
|
|
|
|
|
local function make_proxy_call(destination, call_hostname)
|
|
|
|
|
destination = destination .. "@" .. domain_name
|
|
|
|
|
local profile, proxy = "internal", call_hostname;
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
local sip_auth_username = session:getVariable("sip_auth_username");
|
|
|
|
|
local sip_auth_password = api:execute("user_data", sip_auth_username .. "@" .. domain_name .." param password");
|
|
|
|
|
local auth = "sip_auth_username="..sip_auth_username..",sip_auth_password='"..sip_auth_password.."'"
|
|
|
|
|
dial_string = "{sip_invite_domain=" .. domain_name .. "," .. auth .. "}sofia/" .. profile .. "/" .. destination .. ";fs_path=sip:" .. proxy;
|
|
|
|
|
log.notice("Send call to other host....");
|
|
|
|
|
session:execute("bridge", dial_string);
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- check pin number if defined
|
|
|
|
|
local function pin(pin_number)
|
|
|
|
|
if not pin_number then
|
|
|
|
|
return true
|
2012-06-04 16:58:40 +02:00
|
|
|
end
|
|
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--sleep
|
|
|
|
|
session:sleep(500);
|
|
|
|
|
--get the user pin number
|
2015-10-25 07:57:20 +01:00
|
|
|
local min_digits = 2;
|
|
|
|
|
local max_digits = 20;
|
|
|
|
|
local max_tries = "3";
|
|
|
|
|
local digit_timeout = "5000";
|
|
|
|
|
local digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
|
|
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--validate the user pin number
|
2015-10-25 07:57:20 +01:00
|
|
|
local pin_number_table = explode(",",pin_number);
|
2015-09-25 13:30:57 +02:00
|
|
|
for index,pin_number in pairs(pin_number_table) do
|
|
|
|
|
if (digits == pin_number) then
|
|
|
|
|
--set the authorized pin number that was used
|
|
|
|
|
session:setVariable("pin_number", pin_number);
|
|
|
|
|
--done
|
|
|
|
|
return true;
|
2014-07-31 10:40:15 +02:00
|
|
|
end
|
2015-09-25 13:30:57 +02:00
|
|
|
end
|
2015-10-25 07:57:20 +01:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--if not authorized play a message and then hangup
|
|
|
|
|
session:streamFile("phrase:voicemail_fail_auth:#");
|
|
|
|
|
session:hangup("NORMAL_CLEARING");
|
|
|
|
|
return;
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- do intercept if we get redirected request from another box
|
|
|
|
|
local function proxy_intercept()
|
|
|
|
|
-- Proceed calls from other boxes
|
|
|
|
|
|
|
|
|
|
-- Check if this call from other box with setted intercept_uuid
|
|
|
|
|
local intercept_uuid = session:getVariable("sip_h_X-intercept_uuid")
|
|
|
|
|
|
|
|
|
|
if intercept_uuid and #intercept_uuid > 0 then
|
|
|
|
|
log.notice("Get intercept_uuid from sip header. Do intercept....")
|
|
|
|
|
session:execute("intercept", intercept_uuid)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Check if this call from other box and we need parent uuid for channel
|
|
|
|
|
local child_intercept_uuid = session:getVariable("sip_h_X-child_intercept_uuid")
|
|
|
|
|
if (not child_intercept_uuid) or (#child_intercept_uuid == 0) then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- search parent uuid
|
|
|
|
|
log.notice("Get child_intercept_uuid from sip header.")
|
|
|
|
|
local parent_uuid =
|
|
|
|
|
channel_variable(child_intercept_uuid, 'ent_originate_aleg_uuid') or
|
|
|
|
|
channel_variable(child_intercept_uuid, 'cc_member_session_uuid') or
|
|
|
|
|
channel_variable(child_intercept_uuid, 'fifo_bridge_uuid') or
|
|
|
|
|
child_intercept_uuid
|
|
|
|
|
|
|
|
|
|
if parent_uuid == child_intercept_uuid then
|
|
|
|
|
log.notice("Can not found parent call. Try intercept child.")
|
|
|
|
|
session:execute("intercept", child_intercept_uuid)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- search parent hostname
|
2015-10-25 07:57:20 +01:00
|
|
|
call_hostname = hostname
|
2015-09-28 09:35:12 +02:00
|
|
|
--[[ parent and child have to be on same box so we do not search it
|
2015-09-25 13:30:57 +02:00
|
|
|
log.notice("Found parent channel try detect parent hostname")
|
|
|
|
|
local dbh = Database.new('switch')
|
|
|
|
|
local sql = "SELECT hostname FROM channels WHERE uuid='" .. parent_uuid .. "'"
|
|
|
|
|
local call_hostname = dbh:first_value(sql)
|
|
|
|
|
dbh:release()
|
|
|
|
|
|
|
|
|
|
if not call_hostname then
|
|
|
|
|
log.notice("Can not find host name. Channels is dead?")
|
|
|
|
|
return true
|
|
|
|
|
end
|
2015-09-28 09:35:12 +02:00
|
|
|
--]]
|
2015-09-25 13:30:57 +02:00
|
|
|
|
|
|
|
|
if hostname == call_hostname then
|
|
|
|
|
log.notice("Found parent call on local machine. Do intercept....")
|
|
|
|
|
session:execute("intercept", parent_uuid);
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
log.noticef("Found parent call on remote machine `%s`.", call_hostname)
|
|
|
|
|
session:execute("export", "sip_h_X-intercept_uuid="..parent_uuid);
|
|
|
|
|
make_proxy_call(pickup_number, call_hostname)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- return array of extensions for group
|
|
|
|
|
local function select_group_extensions()
|
|
|
|
|
-- connect to Fusion database
|
2015-10-25 07:57:20 +01:00
|
|
|
local dbh = Database.new('system');
|
2015-09-25 13:30:57 +02:00
|
|
|
|
|
|
|
|
--get the call groups the extension is a member of
|
2015-10-25 07:57:20 +01:00
|
|
|
local sql = "SELECT call_group FROM v_extensions ";
|
2015-09-25 13:30:57 +02:00
|
|
|
sql = sql .. "WHERE domain_uuid = '"..domain_uuid.."' ";
|
|
|
|
|
sql = sql .. "AND (extension = '"..caller_id_number.."'";
|
|
|
|
|
sql = sql .. "OR number_alias = '"..caller_id_number.."')";
|
|
|
|
|
sql = sql .. "limit 1";
|
2015-10-25 07:57:20 +01:00
|
|
|
local call_group = dbh:first_value(sql) or ''
|
2015-09-25 13:30:57 +02:00
|
|
|
log.noticef("call_group: `%s`", call_group);
|
|
|
|
|
call_groups = explode(",", call_group);
|
|
|
|
|
|
|
|
|
|
--get the extensions in the call groups
|
|
|
|
|
sql = "SELECT extension, number_alias FROM v_extensions ";
|
|
|
|
|
sql = sql .. "WHERE domain_uuid = '"..domain_uuid.."' ";
|
|
|
|
|
sql = sql .. "AND (";
|
|
|
|
|
for key,call_group in ipairs(call_groups) do
|
|
|
|
|
if (key > 1) then
|
|
|
|
|
sql = sql .. "OR ";
|
|
|
|
|
end
|
|
|
|
|
if (#call_group > 0) then
|
|
|
|
|
sql = sql .. "call_group like '%"..call_group.."%' ";
|
|
|
|
|
else
|
|
|
|
|
sql = sql .. "call_group = '' ";
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
sql = sql .. ") ";
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
log.notice("sql "..sql);
|
|
|
|
|
end
|
|
|
|
|
local extensions = {}
|
2015-10-25 07:57:20 +01:00
|
|
|
dbh:query(sql, function(row)
|
2015-09-25 13:30:57 +02:00
|
|
|
local member = row.extension
|
|
|
|
|
if row.number_alias and #row.number_alias > 0 then
|
|
|
|
|
member = row.number_alias
|
2014-07-31 10:40:15 +02:00
|
|
|
end
|
2015-09-25 13:30:57 +02:00
|
|
|
extensions[#extensions+1] = member
|
|
|
|
|
log.noticef("member `%s`", member)
|
|
|
|
|
end);
|
|
|
|
|
|
|
|
|
|
-- release Fusion database
|
|
|
|
|
dbh:release()
|
|
|
|
|
|
|
|
|
|
-- return result
|
|
|
|
|
return extensions
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--check if the session is ready
|
|
|
|
|
if ( session:ready() ) then
|
|
|
|
|
--answer the session
|
|
|
|
|
session:answer();
|
|
|
|
|
--get session variables
|
|
|
|
|
domain_uuid = session:getVariable("domain_uuid");
|
|
|
|
|
domain_name = session:getVariable("domain_name");
|
|
|
|
|
pin_number = session:getVariable("pin_number");
|
|
|
|
|
context = session:getVariable("context");
|
|
|
|
|
caller_id_number = session:getVariable("caller_id_number");
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--check if the session is ready
|
|
|
|
|
if ( session:ready() ) then
|
|
|
|
|
if proxy_intercept() then
|
|
|
|
|
return
|
2012-06-04 16:58:40 +02:00
|
|
|
end
|
2015-09-25 13:30:57 +02:00
|
|
|
end
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--check if the session is ready
|
|
|
|
|
if ( session:ready() ) then
|
|
|
|
|
--if the pin number is provided then require it
|
|
|
|
|
if not pin(pin_number) then
|
|
|
|
|
return
|
2012-12-05 10:08:36 +01:00
|
|
|
end
|
2015-10-25 07:57:20 +01:00
|
|
|
end
|
2015-09-25 13:30:57 +02:00
|
|
|
|
2015-10-25 07:57:20 +01:00
|
|
|
if ( session:ready() ) then
|
2015-09-25 13:30:57 +02:00
|
|
|
-- select intercept mode
|
|
|
|
|
if not extension then
|
|
|
|
|
log.notice("GROUP INTERCEPT")
|
|
|
|
|
extensions = select_group_extensions()
|
2015-09-22 17:33:41 +02:00
|
|
|
else
|
2015-09-25 13:30:57 +02:00
|
|
|
log.noticef("INTERCEPT %s", extension)
|
|
|
|
|
extensions = {extension}
|
2015-09-22 17:33:41 +02:00
|
|
|
end
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
--connect to FS database
|
2016-05-13 04:43:57 +02:00
|
|
|
--local dbh = Database.new('switch')
|
2016-05-13 07:06:45 +02:00
|
|
|
if (file_exists(database_dir.."/core.db")) then
|
|
|
|
|
--dbh = freeswitch.Dbh("core:core"); -- when using sqlite
|
|
|
|
|
dbh = freeswitch.Dbh("sqlite://"..database_dir.."/core.db");
|
|
|
|
|
else
|
|
|
|
|
dofile(scripts_dir.."/resources/functions/database_handle.lua");
|
|
|
|
|
dbh = database_handle('switch');
|
|
|
|
|
end
|
2015-09-25 13:30:57 +02:00
|
|
|
|
|
|
|
|
--check the database to get the uuid of a ringing call
|
|
|
|
|
call_hostname = "";
|
2015-10-25 07:57:20 +01:00
|
|
|
sql = "SELECT uuid, call_uuid, hostname FROM channels ";
|
2015-09-25 13:30:57 +02:00
|
|
|
sql = sql .. "WHERE callstate in ('RINGING', 'EARLY') ";
|
2015-10-25 07:57:20 +01:00
|
|
|
-- next check should prevent pickup call from extension
|
|
|
|
|
-- e.g. if extension 100 dial some cell phone and some one else dial *8
|
|
|
|
|
-- he can pickup this call.
|
2016-04-18 15:06:27 +02:00
|
|
|
if not direction:find('all') then
|
|
|
|
|
sql = sql .. "AND (1 <> 1 "
|
|
|
|
|
-- calls from freeswitch to user
|
|
|
|
|
if direction:find('inbound') then
|
|
|
|
|
sql = sql .. "OR direction = 'outbound' ";
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- calls from user to freeswitch
|
|
|
|
|
if direction:find('outbound') then
|
|
|
|
|
sql = sql .. "OR direction = 'inbound' ";
|
|
|
|
|
end
|
|
|
|
|
sql = sql .. ")"
|
2015-11-12 12:11:05 +01:00
|
|
|
end
|
2016-04-18 15:06:27 +02:00
|
|
|
|
2015-11-10 20:41:34 +01:00
|
|
|
sql = sql .. "AND (1<>1 ";
|
2015-09-25 13:30:57 +02:00
|
|
|
for key,extension in pairs(extensions) do
|
|
|
|
|
sql = sql .. "OR presence_id = '"..extension.."@"..domain_name.."' ";
|
|
|
|
|
end
|
|
|
|
|
sql = sql .. ") ";
|
|
|
|
|
sql = sql .. "and call_uuid is not null ";
|
|
|
|
|
sql = sql .. "limit 1 ";
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
log.notice("sql "..sql);
|
|
|
|
|
end
|
|
|
|
|
local is_child
|
|
|
|
|
dbh:query(sql, function(row)
|
|
|
|
|
-- for key, val in pairs(row) do
|
|
|
|
|
-- log.notice("row "..key.." "..val);
|
|
|
|
|
-- end
|
2015-10-25 07:57:20 +01:00
|
|
|
-- log.notice("-----------------------");
|
2015-09-25 13:30:57 +02:00
|
|
|
is_child = (row.uuid == row.call_uuid)
|
|
|
|
|
uuid = row.call_uuid;
|
|
|
|
|
call_hostname = row.hostname;
|
|
|
|
|
end);
|
|
|
|
|
|
|
|
|
|
if is_child then
|
|
|
|
|
-- we need intercept `parent` call e.g. call in FIFO/CallCenter Queue
|
|
|
|
|
if (call_hostname == hostname) then
|
|
|
|
|
log.notice("Found child call on local machine. Try find parent channel.")
|
|
|
|
|
local parent_uuid =
|
|
|
|
|
channel_variable(uuid, 'ent_originate_aleg_uuid') or
|
|
|
|
|
channel_variable(uuid, 'cc_member_session_uuid') or
|
|
|
|
|
channel_variable(uuid, 'fifo_bridge_uuid') or
|
|
|
|
|
uuid
|
|
|
|
|
|
2015-09-28 09:35:12 +02:00
|
|
|
--[[ parent and child have to be on same box so we do not search it
|
2015-09-25 13:30:57 +02:00
|
|
|
if parent_uuid ~= uuid then
|
|
|
|
|
local sql = "SELECT hostname FROM channels WHERE uuid='" .. uuid .. "'"
|
|
|
|
|
call_hostname = dbh:first_value(sql)
|
|
|
|
|
end
|
2015-09-28 09:35:12 +02:00
|
|
|
--]]
|
|
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
if call_hostname then
|
2015-09-28 09:35:12 +02:00
|
|
|
uuid = parent_uuid
|
2015-09-25 13:30:57 +02:00
|
|
|
if call_hostname ~= hostname then
|
|
|
|
|
log.noticef("Found parent call on remote machine `%s`.", call_hostname)
|
|
|
|
|
else
|
|
|
|
|
log.notice("Found parent call on local machine.")
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-09-28 09:35:12 +02:00
|
|
|
|
2015-09-25 13:30:57 +02:00
|
|
|
else
|
|
|
|
|
log.noticef("Found child call on remote machine `%s`.", call_hostname)
|
2016-03-11 14:21:52 +01:00
|
|
|
-- we can not find parent on this box because channel on other box so we have to
|
2015-09-25 13:30:57 +02:00
|
|
|
-- forward call to this box
|
|
|
|
|
session:execute("export", "sip_h_X-child_intercept_uuid="..uuid);
|
|
|
|
|
return make_proxy_call(pickup_number, call_hostname)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--release FS database
|
|
|
|
|
dbh:release()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
log.noticef( "Hostname: %s Call Hostname: %s", hostname, call_hostname);
|
2014-08-22 09:57:47 +02:00
|
|
|
|
2012-06-04 16:58:40 +02:00
|
|
|
--intercept a call that is ringing
|
2015-09-25 13:30:57 +02:00
|
|
|
if (uuid ~= nil) then
|
2015-02-21 21:33:23 +01:00
|
|
|
if (session:getVariable("billmsec") == nil) then
|
|
|
|
|
if (hostname == call_hostname) then
|
|
|
|
|
session:execute("intercept", uuid);
|
|
|
|
|
else
|
|
|
|
|
session:execute("export", "sip_h_X-intercept_uuid="..uuid);
|
2015-09-25 13:30:57 +02:00
|
|
|
make_proxy_call(pickup_number, call_hostname)
|
2015-02-21 21:33:23 +01:00
|
|
|
end
|
2014-08-22 09:57:47 +02:00
|
|
|
end
|
2012-06-04 16:58:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--notes
|
|
|
|
|
--originate a call
|
|
|
|
|
--cmd = "originate user/1007@voip.example.com &intercept("..uuid..")";
|
|
|
|
|
--api = freeswitch.API();
|
|
|
|
|
--result = api:executeString(cmd);
|