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>
|
2014-07-25 13:04:16 +02:00
|
|
|
-- Copyright (C) 2010-2014
|
2012-06-04 16:58:40 +02:00
|
|
|
-- the Initial Developer. All Rights Reserved.
|
|
|
|
|
--
|
|
|
|
|
-- Contributor(s):
|
|
|
|
|
-- Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--set default variables
|
|
|
|
|
min_digits = "1";
|
|
|
|
|
max_digits = "11";
|
|
|
|
|
max_tries = "3";
|
|
|
|
|
digit_timeout = "3000";
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--debug
|
|
|
|
|
debug["sql"] = false;
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--define the trim function
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.trim"
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--define the explode function
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.explode"
|
2012-06-04 16:58:40 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--create the api object
|
|
|
|
|
api = freeswitch.API();
|
|
|
|
|
|
|
|
|
|
--include config.lua
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.config";
|
2014-07-25 13:04:16 +02:00
|
|
|
|
|
|
|
|
--check if the session is ready
|
2014-08-22 09:36:07 +02:00
|
|
|
if (session:ready()) then
|
2014-07-25 13:04:16 +02:00
|
|
|
--answer the call
|
|
|
|
|
session:answer();
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--get the variables
|
|
|
|
|
enabled = session:getVariable("enabled");
|
|
|
|
|
pin_number = session:getVariable("pin_number");
|
|
|
|
|
sounds_dir = session:getVariable("sounds_dir");
|
|
|
|
|
domain_uuid = session:getVariable("domain_uuid");
|
|
|
|
|
domain_name = session:getVariable("domain_name");
|
|
|
|
|
extension_uuid = session:getVariable("extension_uuid");
|
|
|
|
|
context = session:getVariable("context");
|
|
|
|
|
if (not context ) then context = 'default'; end
|
2014-08-22 09:36:07 +02:00
|
|
|
request_id = session:getVariable("request_id");
|
|
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--set the sounds path for the language, dialect and voice
|
|
|
|
|
default_language = session:getVariable("default_language");
|
|
|
|
|
default_dialect = session:getVariable("default_dialect");
|
|
|
|
|
default_voice = session:getVariable("default_voice");
|
|
|
|
|
if (not default_language) then default_language = 'en'; end
|
|
|
|
|
if (not default_dialect) then default_dialect = 'us'; end
|
|
|
|
|
if (not default_voice) then default_voice = 'callie'; end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--a moment to sleep
|
2012-06-04 16:58:40 +02:00
|
|
|
session:sleep(1000);
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--connect to the database
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.database_handle";
|
2014-07-25 13:04:16 +02:00
|
|
|
dbh = database_handle('system');
|
2014-08-22 09:36:07 +02:00
|
|
|
|
|
|
|
|
--request id is true
|
|
|
|
|
if (request_id == "true") then
|
|
|
|
|
--unset extension uuid
|
|
|
|
|
extension_uuid = nil;
|
|
|
|
|
|
|
|
|
|
--get the id
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
min_digits = 2;
|
|
|
|
|
max_digits = 20;
|
|
|
|
|
id = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_id:#", "", "\\d+");
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--get the pin number
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
min_digits = 3;
|
|
|
|
|
max_digits = 20;
|
|
|
|
|
caller_pin_number = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--check to see if the pin number is correct
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
sql = "SELECT * FROM v_voicemails ";
|
|
|
|
|
sql = sql .. "WHERE domain_uuid = '" .. domain_uuid .."' ";
|
|
|
|
|
sql = sql .. "AND voicemail_id = '" .. id .."' ";
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("notice", "[call_forward] "..sql .."\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql, function(row)
|
|
|
|
|
voicemail_password = row.voicemail_password;
|
|
|
|
|
--freeswitch.consoleLog("notice", "[call_forward] "..voicemail_password .."\n");
|
|
|
|
|
end);
|
|
|
|
|
if (voicemail_password ~= caller_pin_number) then
|
|
|
|
|
--access denied
|
|
|
|
|
session:streamFile("phrase:voicemail_fail_auth:#");
|
|
|
|
|
session:hangup("NORMAL_CLEARING");
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--determine whether to update the dial string
|
2014-08-22 09:36:07 +02:00
|
|
|
if (session:ready()) then
|
|
|
|
|
sql = "select * from v_extensions ";
|
|
|
|
|
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
|
|
|
|
if (extension_uuid ~= nil) then
|
|
|
|
|
sql = sql .. "and extension_uuid = '"..extension_uuid.."' ";
|
|
|
|
|
else
|
|
|
|
|
sql = sql .. "and (extension = '"..id.."' or number_alias = '"..id.."') ";
|
|
|
|
|
end
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("notice", "[call_forward] "..sql.."\n");
|
|
|
|
|
end
|
|
|
|
|
status = dbh:query(sql, function(row)
|
|
|
|
|
extension_uuid = row.extension_uuid;
|
|
|
|
|
extension = row.extension;
|
|
|
|
|
number_alias = row.number_alias;
|
|
|
|
|
accountcode = row.accountcode;
|
|
|
|
|
forward_all_enabled = row.forward_all_enabled;
|
2014-11-18 12:16:59 +01:00
|
|
|
forward_all_destination = row.forward_all_destination;
|
2014-08-22 09:36:07 +02:00
|
|
|
follow_me_uuid = row.follow_me_uuid;
|
|
|
|
|
--freeswitch.consoleLog("NOTICE", "[call forward] extension "..row.extension.."\n");
|
|
|
|
|
--freeswitch.consoleLog("NOTICE", "[call forward] accountcode "..row.accountcode.."\n");
|
|
|
|
|
end);
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--toggle enabled
|
|
|
|
|
if (session:ready() and enabled == "toggle") then
|
|
|
|
|
if (forward_all_enabled == "true") then
|
|
|
|
|
enabled = "false";
|
|
|
|
|
else
|
|
|
|
|
enabled = "true";
|
|
|
|
|
end
|
2014-07-25 13:04:16 +02:00
|
|
|
end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
|
|
|
|
--get the forward destination
|
|
|
|
|
if (session:ready() and enabled == "true" or enabled == "toggle") then
|
2014-11-18 12:16:59 +01:00
|
|
|
if (string.len(forward_all_destination) == 0) then
|
|
|
|
|
forward_all_destination = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-enter_destination_telephone_number.wav", "", "\\d+");
|
|
|
|
|
end
|
2014-07-25 13:04:16 +02:00
|
|
|
end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--set the dial string
|
2014-08-22 09:36:07 +02:00
|
|
|
if (session:ready() and enabled == "true") then
|
2014-09-23 09:45:40 +02:00
|
|
|
--used for number_alias to get the correct user
|
|
|
|
|
sql = "select * from v_extensions ";
|
|
|
|
|
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
|
|
|
|
sql = sql .. "and number_alias = '"..forward_all_destination.."' ";
|
|
|
|
|
status = dbh:query(sql, function(row)
|
|
|
|
|
destination_user = row.extension;
|
|
|
|
|
end);
|
|
|
|
|
|
|
|
|
|
--set the dial_string
|
2014-07-25 13:04:16 +02:00
|
|
|
dial_string = "{presence_id="..forward_all_destination.."@"..domain_name;
|
|
|
|
|
dial_string = dial_string .. ",instant_ringback=true";
|
|
|
|
|
dial_string = dial_string .. ",domain_uuid="..domain_uuid;
|
|
|
|
|
dial_string = dial_string .. ",sip_invite_domain="..domain_name;
|
|
|
|
|
dial_string = dial_string .. ",domain_name="..domain_name;
|
|
|
|
|
dial_string = dial_string .. ",domain="..domain_name;
|
|
|
|
|
if (accountcode ~= nil) then
|
|
|
|
|
dial_string = dial_string .. ",accountcode="..accountcode;
|
|
|
|
|
end
|
|
|
|
|
dial_string = dial_string .. "}";
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-09-23 09:45:40 +02:00
|
|
|
if (destination_user ~= nil) then
|
|
|
|
|
cmd = "user_exists id ".. destination_user .." "..domain_name;
|
|
|
|
|
else
|
|
|
|
|
cmd = "user_exists id ".. forward_all_destination .." "..domain_name;
|
|
|
|
|
end
|
2014-07-25 13:04:16 +02:00
|
|
|
user_exists = trim(api:executeString(cmd));
|
2014-09-23 10:01:56 +02:00
|
|
|
if (user_exists == "true") then
|
2014-09-23 09:45:40 +02:00
|
|
|
if (destination_user ~= nil) then
|
|
|
|
|
dial_string = dial_string .. "user/"..destination_user.."@"..domain_name;
|
|
|
|
|
else
|
|
|
|
|
dial_string = dial_string .. "user/"..forward_all_destination.."@"..domain_name;
|
|
|
|
|
end
|
2014-07-25 13:04:16 +02:00
|
|
|
else
|
|
|
|
|
dial_string = dial_string .. "loopback/"..forward_all_destination;
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--set call forward
|
2014-08-22 09:36:07 +02:00
|
|
|
if (session:ready() and enabled == "true") then
|
2014-07-25 13:04:16 +02:00
|
|
|
--set forward_all_enabled
|
|
|
|
|
forward_all_enabled = "true";
|
2015-07-20 20:59:45 +02:00
|
|
|
--say the destination number
|
|
|
|
|
session:say(forward_all_destination, default_language, "number", "iterated");
|
2014-07-25 13:04:16 +02:00
|
|
|
--notify the caller
|
|
|
|
|
session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-call_forwarding_has_been_set.wav");
|
|
|
|
|
end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--unset call forward
|
2014-08-22 09:36:07 +02:00
|
|
|
if (session:ready() and enabled == "false") then
|
2014-07-25 13:04:16 +02:00
|
|
|
--set forward_all_enabled
|
|
|
|
|
forward_all_enabled = "false";
|
|
|
|
|
--notify the caller
|
|
|
|
|
session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-call_forwarding_has_been_cancelled.wav");
|
|
|
|
|
end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--disable the follow me
|
2014-08-22 09:36:07 +02:00
|
|
|
if (session:ready() and enabled == "true" and follow_me_uuid ~= nil) then
|
2014-11-28 21:40:59 +01:00
|
|
|
if (string.len(follow_me_uuid) > 0) then
|
|
|
|
|
sql = "update v_follow_me set ";
|
|
|
|
|
sql = sql .. "follow_me_enabled = 'false' ";
|
|
|
|
|
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
|
|
|
|
sql = sql .. "and follow_me_uuid = '"..follow_me_uuid.."' ";
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("notice", "[call_forward] "..sql.."\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql);
|
2014-07-25 13:04:16 +02:00
|
|
|
end
|
|
|
|
|
end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
2014-09-13 11:30:25 +02:00
|
|
|
--check the destination
|
2014-09-23 09:45:40 +02:00
|
|
|
if (forward_all_destination == nil) then
|
2014-09-13 11:30:25 +02:00
|
|
|
enabled = false;
|
|
|
|
|
forward_all_enabled = "false";
|
|
|
|
|
else
|
|
|
|
|
if (string.len(forward_all_destination) == 0) then
|
|
|
|
|
enabled = false;
|
2014-11-18 12:16:59 +01:00
|
|
|
forward_all_enabled = "false";
|
2014-09-13 11:30:25 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-25 13:04:16 +02:00
|
|
|
--update the extension
|
2014-08-22 09:36:07 +02:00
|
|
|
if (session:ready()) then
|
|
|
|
|
sql = "update v_extensions set ";
|
|
|
|
|
if (enabled == "true") then
|
|
|
|
|
sql = sql .. "forward_all_destination = '"..forward_all_destination.."', ";
|
|
|
|
|
sql = sql .. "dial_string = '"..dial_string.."', ";
|
|
|
|
|
sql = sql .. "do_not_disturb = 'false', ";
|
|
|
|
|
else
|
2015-06-18 08:55:46 +02:00
|
|
|
sql = sql .. "forward_all_destination = null, ";
|
2014-08-22 09:36:07 +02:00
|
|
|
sql = sql .. "dial_string = null, ";
|
|
|
|
|
end
|
|
|
|
|
sql = sql .. "forward_all_enabled = '"..forward_all_enabled.."' ";
|
|
|
|
|
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
|
|
|
|
sql = sql .. "and extension_uuid = '"..extension_uuid.."' ";
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("notice", "[call_forward] "..sql.."\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql);
|
2014-07-25 13:04:16 +02:00
|
|
|
end
|
2014-08-22 09:36:07 +02:00
|
|
|
|
|
|
|
|
--clear the cache and hangup
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
--clear the cache
|
|
|
|
|
if (extension ~= nil) then
|
|
|
|
|
api:execute("memcache", "delete directory:"..extension.."@"..domain_name);
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--wait for the file to be written before proceeding
|
|
|
|
|
session:sleep(100);
|
|
|
|
|
|
|
|
|
|
--end the call
|
|
|
|
|
session:hangup();
|
2014-07-25 13:04:16 +02:00
|
|
|
end
|
|
|
|
|
end
|