diff --git a/app/dialplan/resources/switch/conf/dialplan/500_call-forward.xml b/app/dialplan/resources/switch/conf/dialplan/500_call-forward.xml index 7fc7cb2a73..7a8ab2f04d 100644 --- a/app/dialplan/resources/switch/conf/dialplan/500_call-forward.xml +++ b/app/dialplan/resources/switch/conf/dialplan/500_call-forward.xml @@ -1,12 +1,10 @@ - - diff --git a/resources/install/scripts/call_forward.lua b/resources/install/scripts/call_forward.lua index b39fc28306..620ee046d7 100644 --- a/resources/install/scripts/call_forward.lua +++ b/resources/install/scripts/call_forward.lua @@ -16,112 +16,172 @@ -- -- The Initial Developer of the Original Code is -- Mark J Crane --- Copyright (C) 2010 +-- Copyright (C) 2010-2014 -- the Initial Developer. All Rights Reserved. -- -- Contributor(s): -- Mark J Crane -pin_number = ""; -max_tries = "3"; -digit_timeout = "3000"; +--set default variables + min_digits = "1"; + max_digits = "11"; + max_tries = "3"; + digit_timeout = "3000"; -function file_exists(fname) - local f = io.open(fname, "r") - if (f and f:read()) then return true end -end +--debug + debug["sql"] = false; -if ( session:ready() ) then - session:answer(); - --session:execute("info", ""); - extension = session:getVariable("user_name"); - pin_number = session:getVariable("pin_number"); - sounds_dir = session:getVariable("sounds_dir"); - dialplan_default_dir = session:getVariable("dialplan_default_dir"); - call_forward_number = session:getVariable("call_forward_number"); - extension_required = session:getVariable("extension_required"); - 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 - - --if the pin number is provided then require it - if (pin_number) then - min_digits = string.len(pin_number); - max_digits = string.len(pin_number)+1; - digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/custom/please_enter_the_pin_number.wav", "", "\\d+"); - if (digits == pin_number) then - --pin is correct - else - session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/custom/your_pin_number_is_incorect_goodbye.wav"); - session:hangup("NORMAL_CLEARING"); - return; - end +--define the trim function + function trim (s) + return (string.gsub(s, "^%s*(.-)%s*$", "%1")) end - --if extension_requires is true then get the extension number - if (extension_required) then - if (extension_required == "true") then - extension = session:playAndGetDigits(3, 6, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/custom/please_enter_the_extension_number.wav", "", "\\d+"); +--define the explode function + function explode ( seperator, str ) + local pos, arr = 0, {} + for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found + table.insert( arr, string.sub( str, pos, st-1 ) ) -- attach chars left of current divider + pos = sp + 1 -- jump past current divider end + table.insert( arr, string.sub( str, pos ) ) -- attach chars right of last divider + return arr end +--create the api object + api = freeswitch.API(); - if (file_exists(dialplan_default_dir.."/000_call_forward_"..extension..".xml")) then - --file exists - - --remove the call forward dialplan entry - os.remove (dialplan_default_dir.."/000_call_forward_"..extension..".xml"); - - --stream file - session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/custom/call_forward_has_been_deleted.wav"); +--include config.lua + scripts_dir = string.sub(debug.getinfo(1).source,2,string.len(debug.getinfo(1).source)-(string.len(argv[0])+1)); + dofile(scripts_dir.."/resources/functions/config.lua"); + dofile(config()); +--check if the session is ready + if ( session:ready() ) then + --answer the call + session:answer(); + + --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 + + --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 + dofile(scripts_dir.."/resources/functions/database_handle.lua"); + 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", "[call_forward] "..sql.."\n"); + end + status = dbh:query(sql, function(row) + extension = row.extension; + number_alias = row.number_alias; + 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); + + --set the variables + if (enabled == "true") 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 + + --set the dial string + if (enabled == "true") then + 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 .. "}"; + + cmd = "user_exists id ".. forward_all_destination .." "..domain_name; + user_exists = trim(api:executeString(cmd)); + if (user_exists) then + dial_string = dial_string .. "user/"..forward_all_destination.."@"..domain_name; + else + dial_string = dial_string .. "loopback/"..forward_all_destination; + end + end + + --set call forward + if (enabled == "true") then + --set forward_all_enabled + forward_all_enabled = "true"; + --notify the caller + session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-call_forwarding_has_been_set.wav"); + end + + --unset call forward + if (enabled == "false") then + --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 + + --disable the follow me + if (enabled == "true" and follow_me_uuid ~= nil) 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); + end + + --update the extension + 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 + 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); + + --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(1000); - - else - --file does not exist - - dtmf = ""; --clear dtmf digits to prepare for next dtmf request - if (call_forward_number) then - -- do nothing - else - -- get the call forward number - call_forward_number = session:playAndGetDigits(3, 15, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/custom/please_enter_the_phone_number.wav", "", "\\d+"); - end - if (string.len(call_forward_number) > 0) then - --write the xml file - xml = "\n"; - xml = xml .. " \n"; - xml = xml .. " \n"; - xml = xml .. " \n"; - xml = xml .. "\n"; - session:execute("log", xml); - local file = assert(io.open(dialplan_default_dir.."/000_call_forward_"..extension..".xml", "w")); - file:write(xml); - file:close(); - - --wait for the file to be written before proceeding - --session:sleep(20000); - - --stream file - session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/custom/call_forward_has_been_set.wav"); - end - end - - --reloadxml - api = freeswitch.API(); - reply = api:executeString("reloadxml"); - - --wait for the file to be written before proceeding - session:sleep(1000); - - session:hangup(); - -end \ No newline at end of file + + --end the call + session:hangup(); + + end \ No newline at end of file