mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2025-12-30 00:53:50 +00:00
Update follow_me.lua
1. Use `cache` class so now it generates memcache events. 2. Do not use nested if (simplify code) 3. Use local variables. 4. Remove redundant checks.
This commit is contained in:
@@ -22,158 +22,140 @@
|
||||
-- Contributor(s):
|
||||
-- Mark J Crane <markjcrane@fusionpbx.com>
|
||||
|
||||
--set default variables
|
||||
min_digits = "1";
|
||||
max_digits = "11";
|
||||
max_tries = "3";
|
||||
digit_timeout = "3000";
|
||||
|
||||
--debug
|
||||
debug["sql"] = true;
|
||||
|
||||
--define the trim function
|
||||
require "resources.functions.trim";
|
||||
|
||||
--define the explode function
|
||||
require "resources.functions.explode";
|
||||
--include config.lua
|
||||
require "resources.functions.config";
|
||||
|
||||
--create the api object
|
||||
api = freeswitch.API();
|
||||
|
||||
--include config.lua
|
||||
require "resources.functions.config";
|
||||
require "resources.functions.channel_utils";
|
||||
local log = require "resources.functions.log".follow_me
|
||||
local cache = require "resources.functions.cache"
|
||||
local Database = require "resources.functions.database"
|
||||
|
||||
--check if the session is ready
|
||||
if ( session:ready() ) then
|
||||
--answer the call
|
||||
session:answer();
|
||||
|
||||
--get the variables
|
||||
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
|
||||
|
||||
--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
|
||||
|
||||
--a moment to sleep
|
||||
session:sleep(1000);
|
||||
|
||||
--connect to the database
|
||||
require "resources.functions.database_handle";
|
||||
dbh = database_handle('system');
|
||||
|
||||
--determine whether to update the dial string
|
||||
sql = "select * from v_extensions ";
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and extension_uuid = '"..extension_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
freeswitch.consoleLog("notice", "[follow_me] "..sql.."\n");
|
||||
end
|
||||
status = dbh:query(sql, function(row)
|
||||
extension = row.extension;
|
||||
number_alias = row.number_alias or '';
|
||||
accountcode = row.accountcode;
|
||||
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);
|
||||
if not session:ready() then return end
|
||||
|
||||
--determine whether to update the dial string
|
||||
enabled = "false";
|
||||
sql = "select * from v_follow_me ";
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and follow_me_uuid = '"..follow_me_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
freeswitch.consoleLog("notice", "[follow_me] "..sql.."\n");
|
||||
end
|
||||
status = dbh:query(sql, function(row)
|
||||
enabled = row.follow_me_enabled;
|
||||
call_prompt = row.call_prompt;
|
||||
cid_name_prefix = row.cid_name_prefix;
|
||||
cid_number_prefix = row.cid_number_prefix;
|
||||
dial_string = row.dial_string;
|
||||
end);
|
||||
|
||||
--set follow me
|
||||
if (enabled == "false") then
|
||||
--answer and play a tone
|
||||
session:answer();
|
||||
api = freeswitch.API();
|
||||
reply = api:executeString("uuid_display "..session:get_uuid().." Activated ");
|
||||
--answer the call
|
||||
session:answer();
|
||||
|
||||
session:execute("sleep", "2000");
|
||||
session:execute("playback", "tone_stream://%(200,0,500,600,700)");
|
||||
--notify the caller
|
||||
--session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-call_forwarding_has_been_set.wav");
|
||||
end
|
||||
|
||||
--unset follow me
|
||||
if (enabled == "true") then
|
||||
--answer and play a tone
|
||||
session:answer();
|
||||
api = freeswitch.API();
|
||||
reply = api:executeString("uuid_display "..session:get_uuid().." Cancelled ");
|
||||
--get the variables
|
||||
local domain_uuid = session:getVariable("domain_uuid");
|
||||
local domain_name = session:getVariable("domain_name");
|
||||
local extension_uuid = session:getVariable("extension_uuid");
|
||||
|
||||
session:execute("sleep", "2000");
|
||||
session:execute("playback", "tone_stream://%(500,0,300,200,100,50,25)");
|
||||
--notify the caller
|
||||
--session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-call_forwarding_has_been_cancelled.wav");
|
||||
end
|
||||
--set the sounds path for the language, dialect and voice
|
||||
local sounds_dir = session:getVariable("sounds_dir");
|
||||
local default_language = session:getVariable("default_language") or 'en';
|
||||
local default_dialect = session:getVariable("default_dialect") or 'us';
|
||||
local default_voice = session:getVariable("default_voice") or 'callie';
|
||||
|
||||
--enable or disable follow me
|
||||
if (follow_me_uuid ~= nil) then
|
||||
sql = "update v_follow_me set ";
|
||||
if (enabled == "true") then
|
||||
sql = sql .. "follow_me_enabled = 'false' ";
|
||||
else
|
||||
sql = sql .. "follow_me_enabled = 'true' ";
|
||||
end
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and follow_me_uuid = '"..follow_me_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
freeswitch.consoleLog("notice", "[follow_me] "..sql.."\n");
|
||||
end
|
||||
dbh:query(sql);
|
||||
end
|
||||
|
||||
--update the extension
|
||||
sql = "update v_extensions set ";
|
||||
if (enabled == "true") then
|
||||
sql = sql .. "dial_string = null, ";
|
||||
else
|
||||
sql = sql .. "dial_string = '"..dial_string.."', ";
|
||||
end
|
||||
sql = sql .. "do_not_disturb = 'false', ";
|
||||
sql = sql .. "forward_all_enabled= 'false' ";
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and extension_uuid = '"..extension_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
freeswitch.consoleLog("notice", "[follow_me] "..sql.."\n");
|
||||
end
|
||||
dbh:query(sql);
|
||||
|
||||
--clear the cache
|
||||
if (extension ~= nil) then
|
||||
api:execute("memcache", "delete directory:"..extension.."@"..domain_name);
|
||||
if #number_alias > 0 then
|
||||
api:execute("memcache", "delete directory:"..number_alias.."@"..domain_name);
|
||||
end
|
||||
end
|
||||
|
||||
--wait for the file to be written before proceeding
|
||||
session:sleep(1000);
|
||||
|
||||
--end the call
|
||||
session:hangup();
|
||||
|
||||
--a moment to sleep
|
||||
session:sleep(1000);
|
||||
|
||||
--check if the session is ready
|
||||
if not session:ready() then return end
|
||||
|
||||
--connect to the database
|
||||
local dbh = Database.new('system');
|
||||
|
||||
--determine whether to update the dial string
|
||||
local sql = "select extension, number_alias, accountcode, follow_me_uuid ";
|
||||
sql = sql .. "from v_extensions ";
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and extension_uuid = '"..extension_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
log.notice(sql);
|
||||
end
|
||||
|
||||
local row = dbh:first_row(sql)
|
||||
if not row then return end
|
||||
|
||||
local extension = row.extension;
|
||||
local number_alias = row.number_alias or '';
|
||||
local accountcode = row.accountcode;
|
||||
local follow_me_uuid = row.follow_me_uuid;
|
||||
|
||||
--determine whether to update the dial string
|
||||
sql = "select follow_me_enabled, call_prompt, cid_name_prefix, cid_number_prefix, dial_string "
|
||||
sql = sql .. "from v_follow_me ";
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and follow_me_uuid = '"..follow_me_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
log.notice(sql);
|
||||
end
|
||||
|
||||
row = dbh:first_row(sql)
|
||||
if not row then return end
|
||||
|
||||
local enabled = row.follow_me_enabled;
|
||||
local call_prompt = row.call_prompt;
|
||||
local cid_name_prefix = row.cid_name_prefix;
|
||||
local cid_number_prefix = row.cid_number_prefix;
|
||||
local dial_string = row.dial_string;
|
||||
|
||||
--set follow me
|
||||
if (enabled == "false") then
|
||||
--play a tone
|
||||
channel_display(session:get_uuid(), "Activated")
|
||||
|
||||
session:execute("sleep", "2000");
|
||||
session:execute("playback", "tone_stream://%(200,0,500,600,700)");
|
||||
--notify the caller
|
||||
--session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-call_forwarding_has_been_set.wav");
|
||||
end
|
||||
|
||||
--unset follow me
|
||||
if (enabled == "true") then
|
||||
--play a tone
|
||||
channel_display(session:get_uuid(), "Cancelled")
|
||||
|
||||
session:execute("sleep", "2000");
|
||||
session:execute("playback", "tone_stream://%(500,0,300,200,100,50,25)");
|
||||
--notify the caller
|
||||
--session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-call_forwarding_has_been_cancelled.wav");
|
||||
end
|
||||
|
||||
--enable or disable follow me
|
||||
sql = "update v_follow_me set ";
|
||||
if (enabled == "true") then
|
||||
sql = sql .. "follow_me_enabled = 'false' ";
|
||||
else
|
||||
sql = sql .. "follow_me_enabled = 'true' ";
|
||||
end
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and follow_me_uuid = '"..follow_me_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
log.notice(sql);
|
||||
end
|
||||
dbh:query(sql);
|
||||
|
||||
--update the extension
|
||||
sql = "update v_extensions set ";
|
||||
if (enabled == "true") then
|
||||
sql = sql .. "dial_string = null, ";
|
||||
else
|
||||
sql = sql .. "dial_string = '"..dial_string:gsub("'", "''").."', ";
|
||||
end
|
||||
sql = sql .. "do_not_disturb = 'false', ";
|
||||
sql = sql .. "forward_all_enabled= 'false' ";
|
||||
sql = sql .. "where domain_uuid = '"..domain_uuid.."' ";
|
||||
sql = sql .. "and extension_uuid = '"..extension_uuid.."' ";
|
||||
if (debug["sql"]) then
|
||||
log.notice(sql);
|
||||
end
|
||||
dbh:query(sql);
|
||||
|
||||
--clear the cache
|
||||
if (extension ~= nil) and cache.support() then
|
||||
cache.del("directory:"..extension.."@"..domain_name);
|
||||
if #number_alias > 0 then
|
||||
cache.del("directory:"..number_alias.."@"..domain_name);
|
||||
end
|
||||
end
|
||||
|
||||
--wait for the file to be written before proceeding
|
||||
session:sleep(1000);
|
||||
|
||||
--end the call
|
||||
session:hangup();
|
||||
|
||||
@@ -23,6 +23,14 @@ function channel_evalute(uuid, cmd)
|
||||
return result
|
||||
end
|
||||
|
||||
function channel_display(uuid, text)
|
||||
local cmd = ("uuid_display %s '%s'"):format(uuid, text)
|
||||
local result = trim(api:executeString(cmd))
|
||||
if result:sub(1, 4) == '-ERR' then return nil, result end
|
||||
if result == '_undef_' then return false end
|
||||
return result
|
||||
end
|
||||
|
||||
local _switchname
|
||||
local function switchname()
|
||||
if _switchname then return _switchname end
|
||||
|
||||
Reference in New Issue
Block a user