2014-07-27 13:52:38 +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>
|
2019-09-11 21:26:37 +02:00
|
|
|
-- Copyright (C) 2010-2019
|
2014-07-27 13:52:38 +02:00
|
|
|
-- the Initial Developer. All Rights Reserved.
|
|
|
|
|
--
|
|
|
|
|
-- Contributor(s):
|
|
|
|
|
-- Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
|
|
|
|
|
--set default variables
|
|
|
|
|
min_digits = "1";
|
|
|
|
|
max_digits = "11";
|
|
|
|
|
max_tries = "3";
|
|
|
|
|
digit_timeout = "3000";
|
|
|
|
|
|
|
|
|
|
--define the trim function
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.trim";
|
2014-07-27 13:52:38 +02:00
|
|
|
|
|
|
|
|
--define the explode function
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.explode";
|
2014-07-27 13:52:38 +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-27 13:52:38 +02:00
|
|
|
|
2017-06-20 17:23:23 +02:00
|
|
|
local blf = require "resources.functions.blf"
|
2017-08-23 00:16:35 +02:00
|
|
|
local cache = require "resources.functions.cache"
|
2019-09-05 19:01:59 +02:00
|
|
|
local Settings = require "resources.functions.lazy_settings"
|
|
|
|
|
local notify = require "app.feature_event.resources.functions.feature_event_notify"
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--answer the call
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
session:answer();
|
|
|
|
|
end
|
2016-10-07 22:54:02 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--get the variables
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
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");
|
|
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--set the sounds path for the language, dialect and voice
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
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
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--wait a moment to sleep
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
session:sleep(1000);
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--connect to the database
|
|
|
|
|
local Database = require "resources.functions.database";
|
|
|
|
|
dbh = Database.new('system');
|
|
|
|
|
|
|
|
|
|
local settings = Settings.new(dbh, domain_name, domain_uuid);
|
|
|
|
|
|
|
|
|
|
--include json library
|
2019-09-05 21:43:49 +02:00
|
|
|
--debug["sql"] = true;
|
2019-09-05 19:01:59 +02:00
|
|
|
local json
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
json = require "resources.functions.lunajson"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--determine whether to update the dial string
|
|
|
|
|
local sql = "select * from v_extensions ";
|
|
|
|
|
sql = sql .. "where domain_uuid = :domain_uuid ";
|
|
|
|
|
sql = sql .. "and extension_uuid = :extension_uuid ";
|
|
|
|
|
local params = {domain_uuid = domain_uuid, extension_uuid = extension_uuid};
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("notice", "[do_not_disturb] " .. sql .. "; params:" .. json.encode(params) .. "\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql, params, function(row)
|
|
|
|
|
extension = row.extension;
|
|
|
|
|
number_alias = row.number_alias or '';
|
|
|
|
|
accountcode = row.accountcode;
|
|
|
|
|
do_not_disturb = row.do_not_disturb;
|
|
|
|
|
forward_all_destination = row.forward_all_destination;
|
|
|
|
|
forward_all_enabled = row.forward_all_enabled;
|
|
|
|
|
context = row.user_context;
|
|
|
|
|
end);
|
|
|
|
|
|
|
|
|
|
--send information to the console
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[do_not_disturb] do_not_disturb "..do_not_disturb.."\n");
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[do_not_disturb] extension "..extension.."\n");
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[do_not_disturb] accountcode "..accountcode.."\n");
|
|
|
|
|
--freeswitch.consoleLog("NOTICE", "[do_not_disturb] enabled before "..enabled.."\n");
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--toggle do not disturb
|
|
|
|
|
if (enabled == "toggle") then
|
|
|
|
|
if (do_not_disturb == "true") then
|
|
|
|
|
enabled = "false";
|
|
|
|
|
else
|
|
|
|
|
enabled = "true";
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--send information to the console
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[do_not_disturb] enabled "..enabled.."\n");
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--set the dial string
|
|
|
|
|
if (enabled == "true") then
|
|
|
|
|
local user = (number_alias and #number_alias > 0) and number_alias or extension;
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--set do not disturb
|
|
|
|
|
if (enabled == "true") then
|
|
|
|
|
--set do_not_disturb_enabled
|
|
|
|
|
do_not_disturb_enabled = "true";
|
|
|
|
|
--notify the caller
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-dnd_activated.wav");
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
2019-09-05 19:01:59 +02:00
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--unset do not disturb
|
|
|
|
|
if (enabled == "false") then
|
|
|
|
|
--set fdo_not_disturb_enabled
|
|
|
|
|
do_not_disturb_enabled = "false";
|
|
|
|
|
--notify the caller
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-dnd_cancelled.wav");
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
2019-09-05 19:01:59 +02:00
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--update the extension
|
|
|
|
|
sql = "update v_extensions set ";
|
|
|
|
|
if (enabled == "true") then
|
|
|
|
|
sql = sql .. "do_not_disturb = 'true', ";
|
|
|
|
|
sql = sql .. "forward_all_enabled = 'false' ";
|
|
|
|
|
else
|
|
|
|
|
sql = sql .. "do_not_disturb = 'false' ";
|
|
|
|
|
end
|
|
|
|
|
sql = sql .. "where domain_uuid = :domain_uuid ";
|
|
|
|
|
sql = sql .. "and extension_uuid = :extension_uuid ";
|
2019-09-11 21:26:37 +02:00
|
|
|
local params = {domain_uuid = domain_uuid, extension_uuid = extension_uuid};
|
2019-09-05 19:01:59 +02:00
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("notice", "[do_not_disturb] "..sql.."; params:" .. json.encode(params) .. "\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql, params);
|
|
|
|
|
|
2021-10-28 17:44:27 +02:00
|
|
|
--update the user and agent status
|
2019-09-05 19:01:59 +02:00
|
|
|
sql = "select * from v_extension_users as e, v_users as u ";
|
|
|
|
|
sql = sql .. "where e.extension_uuid = :extension_uuid ";
|
|
|
|
|
sql = sql .. "and e.user_uuid = u.user_uuid ";
|
|
|
|
|
sql = sql .. "and e.domain_uuid = :domain_uuid ";
|
|
|
|
|
local params = {domain_uuid = domain_uuid, extension_uuid = extension_uuid};
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("notice", "[do_not_disturb] "..sql.."; params:" .. json.encode(params) .. "\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql, params, function(row)
|
|
|
|
|
--update the call center status
|
|
|
|
|
if (enabled == "true") then
|
|
|
|
|
user_status = "Logged Out";
|
|
|
|
|
api:execute("callcenter_config", "agent set status "..row.username.."@"..domain_name.." '"..user_status.."'");
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if (enabled == "true") then
|
2019-09-05 19:01:59 +02:00
|
|
|
user_status = "Do Not Disturb";
|
2014-07-27 13:52:38 +02:00
|
|
|
else
|
2019-09-05 19:01:59 +02:00
|
|
|
user_status = "Available";
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
2019-09-05 19:01:59 +02:00
|
|
|
local sql = "update v_users set ";
|
|
|
|
|
sql = sql .. "user_status = :user_status ";
|
2016-11-22 06:05:58 +01:00
|
|
|
sql = sql .. "where domain_uuid = :domain_uuid ";
|
2019-09-05 19:01:59 +02:00
|
|
|
sql = sql .. "and user_uuid = :user_uuid ";
|
|
|
|
|
local params = {user_status = user_status, domain_uuid = domain_uuid, user_uuid = row.user_uuid};
|
2014-07-27 13:52:38 +02:00
|
|
|
if (debug["sql"]) then
|
2016-11-22 06:05:58 +01:00
|
|
|
freeswitch.consoleLog("notice", "[do_not_disturb] "..sql.."; params:" .. json.encode(params) .. "\n");
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
2016-11-22 06:05:58 +01:00
|
|
|
dbh:query(sql, params);
|
2019-09-05 19:01:59 +02:00
|
|
|
end);
|
2015-09-05 18:05:43 +02:00
|
|
|
|
2017-11-09 20:09:56 +01:00
|
|
|
--send notify to phone if feature sync is enabled
|
|
|
|
|
if settings:get('device', 'feature_sync', 'boolean') == 'true' then
|
|
|
|
|
-- Get values from the database
|
2019-09-05 19:01:59 +02:00
|
|
|
do_not_disturb, forward_all_enabled, forward_all_destination, forward_busy_enabled, forward_busy_destination, forward_no_answer_enabled, forward_no_answer_destination, call_timeout = notify.get_db_values(extension, domain_name)
|
|
|
|
|
|
2017-11-09 20:09:56 +01:00
|
|
|
-- Get the sip_profile
|
2019-09-05 19:01:59 +02:00
|
|
|
if (extension ~= nil and domain_name ~= nil) then
|
2020-11-19 18:30:14 +01:00
|
|
|
sip_profiles = notify.get_profiles(extension, domain_name);
|
2019-09-05 19:01:59 +02:00
|
|
|
end
|
2017-11-09 20:09:56 +01:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
-- check if not nil
|
2020-11-19 18:30:14 +01:00
|
|
|
if (sip_profiles ~= nil) then
|
2017-11-09 20:09:56 +01:00
|
|
|
freeswitch.consoleLog("NOTICE", "[feature_event] SIP NOTIFY: CFWD set to "..forward_all_enabled.."\n");
|
2019-09-05 19:01:59 +02:00
|
|
|
|
2017-11-09 20:09:56 +01:00
|
|
|
--Do Not Disturb
|
2020-11-19 18:30:14 +01:00
|
|
|
notify.dnd(extension, domain_name, sip_profiles, do_not_disturb);
|
2017-11-09 20:09:56 +01:00
|
|
|
|
|
|
|
|
--Forward all
|
|
|
|
|
forward_immediate_enabled = forward_all_enabled;
|
|
|
|
|
forward_immediate_destination = forward_all_destination;
|
2019-09-05 19:01:59 +02:00
|
|
|
|
2017-11-09 20:09:56 +01:00
|
|
|
--workaround for freeswitch not sending NOTIFY when destination values are nil. Send 0.
|
|
|
|
|
if (string.len(forward_immediate_destination) < 1) then
|
|
|
|
|
forward_immediate_destination = '0';
|
|
|
|
|
end
|
2019-09-05 19:01:59 +02:00
|
|
|
|
2020-11-19 18:30:14 +01:00
|
|
|
notify.forward_immediate(extension, domain_name, sip_profiles, forward_immediate_enabled, forward_immediate_destination);
|
2019-09-05 19:01:59 +02:00
|
|
|
|
2017-11-09 20:09:56 +01:00
|
|
|
--Forward busy
|
|
|
|
|
--workaround for freeswitch not sending NOTIFY when destination values are nil. Send 0.
|
|
|
|
|
if (string.len(forward_busy_destination) < 1) then
|
|
|
|
|
forward_busy_destination = '0';
|
|
|
|
|
end
|
2019-09-05 19:01:59 +02:00
|
|
|
|
2020-11-19 18:30:14 +01:00
|
|
|
notify.forward_busy(extension, domain_name, sip_profiles, forward_busy_enabled, forward_busy_destination);
|
2017-11-09 20:09:56 +01:00
|
|
|
|
|
|
|
|
--Forward No Answer
|
|
|
|
|
ring_count = math.ceil (call_timeout / 6);
|
|
|
|
|
--workaround for freeswitch not sending NOTIFY when destination values are nil. Send 0.
|
|
|
|
|
if (string.len(forward_no_answer_destination) < 1) then
|
|
|
|
|
forward_no_answer_destination = '0';
|
|
|
|
|
end
|
2019-09-05 19:01:59 +02:00
|
|
|
|
2020-11-19 18:30:14 +01:00
|
|
|
notify.forward_no_answer(extension, domain_name, sip_profiles, forward_no_answer_enabled, forward_no_answer_destination, ring_count);
|
2017-11-09 20:09:56 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--clear the cache
|
|
|
|
|
if extension and #extension > 0 and cache.support() then
|
|
|
|
|
cache.del("directory:"..extension.."@"..context);
|
|
|
|
|
if #number_alias > 0 then
|
|
|
|
|
cache.del("directory:"..number_alias.."@"..context);
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--wait for the file to be written before proceeding
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
session:sleep(1000);
|
|
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
--end the call
|
|
|
|
|
if (session:ready()) then
|
|
|
|
|
session:hangup();
|
|
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
-- BLF for display DND status
|
|
|
|
|
blf.dnd(enabled == "true", extension, number_alias, domain_name)
|
2017-05-29 17:50:20 +02:00
|
|
|
|
2019-09-05 19:01:59 +02:00
|
|
|
-- Turn off BLF for call forward
|
|
|
|
|
if forward_all_enabled == 'true' and enabled == 'true' then
|
|
|
|
|
blf.forward(false, extension, number_alias,
|
|
|
|
|
forward_all_destination, nil, domain_name
|
|
|
|
|
)
|
2016-10-07 22:54:02 +02:00
|
|
|
end
|
2019-09-05 19:01:59 +02:00
|
|
|
|
|
|
|
|
--disconnect from database
|
|
|
|
|
dbh:release()
|