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>
|
2016-10-07 22:54:02 +02:00
|
|
|
-- Copyright (C) 2010-2016
|
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";
|
|
|
|
|
|
|
|
|
|
--debug
|
|
|
|
|
debug["sql"] = false;
|
|
|
|
|
|
|
|
|
|
--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-05-29 17:50:20 +02:00
|
|
|
local presence_in = require "resources.functions.presence_in"
|
|
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--check if the session is ready
|
|
|
|
|
if ( session:ready() ) then
|
|
|
|
|
--answer the call
|
|
|
|
|
session:answer();
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +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
|
2017-05-29 17:50:20 +02:00
|
|
|
toggle = (enabled == "toggle")
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +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-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--a moment to sleep
|
|
|
|
|
session:sleep(1000);
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--connect to the database
|
2016-11-22 06:05:58 +01:00
|
|
|
local Database = require "resources.functions.database";
|
|
|
|
|
dbh = Database.new('system');
|
|
|
|
|
|
|
|
|
|
--include json library
|
|
|
|
|
local json
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
json = require "resources.functions.lunajson"
|
|
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--determine whether to update the dial string
|
2016-11-22 06:05:58 +01:00
|
|
|
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};
|
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, function(row)
|
2014-07-27 13:52:38 +02:00
|
|
|
extension = row.extension;
|
2015-08-26 11:44:34 +02:00
|
|
|
number_alias = row.number_alias or '';
|
2014-07-27 13:52:38 +02:00
|
|
|
accountcode = row.accountcode;
|
|
|
|
|
follow_me_uuid = row.follow_me_uuid;
|
2016-10-07 22:54:02 +02:00
|
|
|
do_not_disturb = row.do_not_disturb;
|
2017-05-29 17:50:20 +02:00
|
|
|
if toggle then
|
|
|
|
|
enabled = (do_not_disturb == 'true') and 'false' or 'true'
|
|
|
|
|
end
|
2015-08-13 11:14:46 +02:00
|
|
|
--freeswitch.consoleLog("NOTICE", "[do_not_disturb] extension "..row.extension.."\n");
|
|
|
|
|
--freeswitch.consoleLog("NOTICE", "[do_not_disturb] accountcode "..row.accountcode.."\n");
|
2014-07-27 13:52:38 +02:00
|
|
|
end);
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2016-10-07 22:54:02 +02:00
|
|
|
--toggle do not disturb
|
|
|
|
|
if (enabled == "toggle") then
|
|
|
|
|
if (do_not_disturb == "true") then
|
|
|
|
|
enabled = "false";
|
|
|
|
|
else
|
|
|
|
|
enabled = "true";
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--set the dial string
|
|
|
|
|
if (enabled == "true") then
|
2015-08-13 19:34:48 +02:00
|
|
|
local user = (number_alias and #number_alias > 0) and number_alias or extension;
|
2015-09-08 22:38:46 +02:00
|
|
|
dial_string = "error/user_busy";
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--set do not disturb
|
|
|
|
|
if (enabled == "true") then
|
|
|
|
|
--set do_not_disturb_enabled
|
|
|
|
|
do_not_disturb_enabled = "true";
|
|
|
|
|
--notify the caller
|
|
|
|
|
session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-dnd_activated.wav");
|
|
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--unset do not disturb
|
|
|
|
|
if (enabled == "false") then
|
|
|
|
|
--set fdo_not_disturb_enabled
|
|
|
|
|
do_not_disturb_enabled = "false";
|
|
|
|
|
--notify the caller
|
|
|
|
|
session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-dnd_cancelled.wav");
|
|
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--disable follow me
|
2014-08-29 10:33:15 +02:00
|
|
|
if (follow_me_uuid ~= nil) then
|
2014-10-20 11:03:23 +02:00
|
|
|
if (string.len(follow_me_uuid) > 0 and enabled == "true") then
|
2016-11-22 06:05:58 +01:00
|
|
|
local sql = "update v_follow_me set ";
|
2014-08-29 10:33:15 +02:00
|
|
|
sql = sql .. "follow_me_enabled = 'false' ";
|
2016-11-22 06:05:58 +01:00
|
|
|
sql = sql .. "where domain_uuid = :domain_uuid ";
|
|
|
|
|
sql = sql .. "and follow_me_uuid = :follow_me_uuid ";
|
|
|
|
|
local params = {domain_uuid = domain_uuid, follow_me_uuid = follow_me_uuid};
|
2014-08-29 10:33:15 +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-08-29 10:33:15 +02:00
|
|
|
end
|
2016-11-22 06:05:58 +01:00
|
|
|
dbh:query(sql, params);
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--update the extension
|
|
|
|
|
sql = "update v_extensions set ";
|
|
|
|
|
if (enabled == "true") then
|
2016-11-22 06:05:58 +01:00
|
|
|
sql = sql .. "dial_string = :dial_string, ";
|
2014-07-27 13:52:38 +02:00
|
|
|
sql = sql .. "do_not_disturb = 'true', ";
|
|
|
|
|
else
|
|
|
|
|
sql = sql .. "dial_string = null, ";
|
|
|
|
|
sql = sql .. "do_not_disturb = 'false', ";
|
|
|
|
|
end
|
|
|
|
|
sql = sql .. "forward_all_enabled = 'false' ";
|
2016-11-22 06:05:58 +01:00
|
|
|
sql = sql .. "where domain_uuid = :domain_uuid ";
|
|
|
|
|
sql = sql .. "and extension_uuid = :extension_uuid ";
|
|
|
|
|
local params = {dial_string = dial_string, domain_uuid = domain_uuid, extension_uuid = extension_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);
|
2014-07-27 13:52:38 +02:00
|
|
|
|
2015-09-05 18:05:43 +02:00
|
|
|
--determine whether to update the dial string
|
|
|
|
|
sql = "select * from v_extension_users as e, v_users as u ";
|
2016-11-22 06:05:58 +01:00
|
|
|
sql = sql .. "where e.extension_uuid = :extension_uuid ";
|
2015-09-05 18:05:43 +02:00
|
|
|
sql = sql .. "and e.user_uuid = u.user_uuid ";
|
2016-11-22 06:05:58 +01:00
|
|
|
sql = sql .. "and e.domain_uuid = :domain_uuid ";
|
|
|
|
|
local params = {domain_uuid = domain_uuid, extension_uuid = extension_uuid};
|
2015-09-05 18:05:43 +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");
|
2015-09-05 18:05:43 +02:00
|
|
|
end
|
2016-11-22 06:05:58 +01:00
|
|
|
dbh:query(sql, params, function(row)
|
2015-09-05 18:05:43 +02:00
|
|
|
--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.."'");
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--update the database user_status
|
|
|
|
|
if (enabled == "true") then
|
|
|
|
|
user_status = "Do Not Disturb";
|
|
|
|
|
else
|
|
|
|
|
user_status = "Available";
|
|
|
|
|
end
|
2016-11-22 06:05:58 +01:00
|
|
|
local sql = "update v_users set ";
|
|
|
|
|
sql = sql .. "user_status = :user_status ";
|
|
|
|
|
sql = sql .. "where domain_uuid = :domain_uuid ";
|
|
|
|
|
sql = sql .. "and user_uuid = :user_uuid ";
|
|
|
|
|
local params = {user_status = user_status, domain_uuid = domain_uuid, user_uuid = row.user_uuid};
|
2015-09-05 18:05:43 +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");
|
2015-09-05 18:05:43 +02:00
|
|
|
end
|
2016-11-23 09:59:58 +01:00
|
|
|
dbh:query(sql, params);
|
2015-09-05 18:05:43 +02:00
|
|
|
end);
|
|
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--clear the cache
|
|
|
|
|
if (extension ~= nil) then
|
2016-01-13 03:34:57 +01:00
|
|
|
freeswitch.consoleLog("notice", "[do_not_disturb] memcache delete directory:"..extension.."@"..domain_name);
|
2014-07-27 13:52:38 +02:00
|
|
|
api:execute("memcache", "delete directory:"..extension.."@"..domain_name);
|
2015-08-26 11:44:34 +02:00
|
|
|
if #number_alias > 0 then
|
2016-01-13 03:34:57 +01:00
|
|
|
freeswitch.consoleLog("notice", "[do_not_disturb] memcache delete directory:"..number_alias.."@"..domain_name);
|
2015-08-26 11:44:34 +02:00
|
|
|
api:execute("memcache", "delete directory:"..number_alias.."@"..domain_name);
|
|
|
|
|
end
|
2014-07-27 13:52:38 +02:00
|
|
|
end
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--wait for the file to be written before proceeding
|
|
|
|
|
session:sleep(1000);
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2014-07-27 13:52:38 +02:00
|
|
|
--end the call
|
|
|
|
|
session:hangup();
|
2014-08-29 10:33:15 +02:00
|
|
|
|
2017-05-29 17:50:20 +02:00
|
|
|
-- BLF for display DND status
|
|
|
|
|
local function dnd_blf(enabled, id, domain)
|
|
|
|
|
local user = string.format('dnd+%s@%s', id, domain)
|
|
|
|
|
presence_in.turn_lamp(enabled, user)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
dnd_blf(enabled == "true", extension, domain_name)
|
|
|
|
|
if number_alias and #number_alias > 0 then
|
|
|
|
|
dnd_blf(enabled == "true", number_alias, domain_name)
|
|
|
|
|
end
|
2016-10-07 22:54:02 +02:00
|
|
|
end
|