2015-04-30 07:42:30 +02:00
|
|
|
-- Part of FusionPBX
|
|
|
|
|
-- Copyright (C) 2015 Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
-- All rights reserved.
|
|
|
|
|
--
|
|
|
|
|
-- Redistribution and use in source and binary forms, with or without
|
|
|
|
|
-- modification, are permitted provided that the following conditions are met:
|
|
|
|
|
--
|
|
|
|
|
-- 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
-- this list of conditions and the following disclaimer.
|
|
|
|
|
--
|
|
|
|
|
-- 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
-- notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
-- documentation and/or other materials provided with the distribution.
|
|
|
|
|
--
|
|
|
|
|
-- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
|
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
|
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
|
-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
2015-05-01 15:37:04 +02:00
|
|
|
--set the debug options
|
|
|
|
|
debug["sql"] = false;
|
|
|
|
|
|
2015-08-11 04:06:33 +02:00
|
|
|
--define the explode function
|
|
|
|
|
require "resources.functions.explode";
|
2015-04-30 07:42:30 +02:00
|
|
|
|
|
|
|
|
--set the defaults
|
|
|
|
|
max_tries = 3;
|
|
|
|
|
digit_timeout = 5000;
|
|
|
|
|
max_retries = 3;
|
|
|
|
|
tries = 0;
|
|
|
|
|
profile = "internal";
|
|
|
|
|
|
|
|
|
|
--connect to the database
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.database_handle";
|
2015-04-30 07:42:30 +02:00
|
|
|
dbh = database_handle('system');
|
|
|
|
|
|
|
|
|
|
--answer
|
2015-05-30 06:50:19 +02:00
|
|
|
session:answer();
|
2015-04-30 07:42:30 +02:00
|
|
|
|
|
|
|
|
--sleep
|
2015-05-30 06:50:19 +02:00
|
|
|
session:sleep(500);
|
2015-04-30 07:42:30 +02:00
|
|
|
|
|
|
|
|
--get the domain_uuid
|
|
|
|
|
domain_uuid = session:getVariable("domain_uuid");
|
|
|
|
|
|
|
|
|
|
--get the action
|
|
|
|
|
action = session:getVariable("action");
|
|
|
|
|
|
|
|
|
|
--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
|
|
|
|
|
|
|
|
|
|
--get the user id
|
|
|
|
|
min_digits = 2;
|
|
|
|
|
max_digits = 20;
|
2015-04-30 11:33:19 +02:00
|
|
|
user_id = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_id:#", "", "\\d+");
|
|
|
|
|
--user_id = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-please_enter_extension_followed_by_pound.wav", "", "\\d+");
|
2015-04-30 07:42:30 +02:00
|
|
|
|
|
|
|
|
--get the user password
|
|
|
|
|
min_digits = 2;
|
|
|
|
|
max_digits = 20;
|
2015-04-30 11:33:19 +02:00
|
|
|
password = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
|
|
|
|
|
--password = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-please_enter_pin_followed_by_pound.wav", "", "\\d+");
|
|
|
|
|
|
2015-04-30 07:42:30 +02:00
|
|
|
--get the user and domain name from the user argv user@domain
|
|
|
|
|
sip_from_uri = session:getVariable("sip_from_uri");
|
|
|
|
|
user_table = explode("@",sip_from_uri);
|
|
|
|
|
user = user_table[1];
|
|
|
|
|
domain = user_table[2];
|
|
|
|
|
|
2015-05-01 14:57:06 +02:00
|
|
|
--show the phone that will be overridden
|
2015-05-01 15:37:04 +02:00
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] sip_from_uri: ".. sip_from_uri .. "\n");
|
2015-04-30 07:42:30 +02:00
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] user: ".. user .. "\n");
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] domain: ".. domain .. "\n");
|
|
|
|
|
|
2015-05-01 14:57:06 +02:00
|
|
|
--get the device uuid for the phone that will have its configuration overridden
|
2015-04-30 07:42:30 +02:00
|
|
|
sql = [[SELECT * FROM v_device_lines ]];
|
|
|
|
|
sql = sql .. [[WHERE user_id = ']] .. user .. [[' ]];
|
|
|
|
|
sql = sql .. [[AND server_address = ']]..domain..[[' ]];
|
2015-05-01 15:37:04 +02:00
|
|
|
sql = sql .. [[AND domain_uuid = ']]..domain_uuid..[[' ]];
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] sql: ".. sql .. "\n");
|
|
|
|
|
end
|
2015-04-30 07:42:30 +02:00
|
|
|
dbh:query(sql, function(row)
|
2015-04-30 12:21:23 +02:00
|
|
|
--get device uuid
|
|
|
|
|
device_uuid = row.device_uuid;
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] device_uuid: ".. device_uuid .. "\n");
|
|
|
|
|
end);
|
2015-04-30 11:33:19 +02:00
|
|
|
|
2015-05-06 06:39:26 +02:00
|
|
|
--get the alternate device uuid using the device username and password
|
2015-05-01 15:37:04 +02:00
|
|
|
authorized = 'false';
|
|
|
|
|
if (user_id ~= '' and password ~= '') then
|
2015-04-30 11:33:19 +02:00
|
|
|
sql = [[SELECT * FROM v_devices ]];
|
|
|
|
|
sql = sql .. [[WHERE device_username = ']]..user_id..[[' ]];
|
|
|
|
|
sql = sql .. [[AND device_password = ']]..password..[[' ]]
|
2015-05-01 15:37:04 +02:00
|
|
|
sql = sql .. [[AND domain_uuid = ']]..domain_uuid..[[' ]];
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] sql: ".. sql .. "\n");
|
|
|
|
|
end
|
2015-04-30 07:42:30 +02:00
|
|
|
dbh:query(sql, function(row)
|
2015-05-06 06:39:26 +02:00
|
|
|
--get the alternate device_uuid
|
2015-04-30 12:21:23 +02:00
|
|
|
device_uuid_alternate = row.device_uuid;
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] alternate device_uuid: ".. device_uuid_alternate .. "\n");
|
|
|
|
|
--authorize the user
|
2015-05-01 14:57:06 +02:00
|
|
|
authorized = 'true';
|
2015-04-30 07:42:30 +02:00
|
|
|
end);
|
2015-05-01 14:57:06 +02:00
|
|
|
end
|
2015-04-30 07:42:30 +02:00
|
|
|
|
|
|
|
|
--authentication failed
|
2015-04-30 12:21:23 +02:00
|
|
|
if (authorized == 'false') then
|
2015-04-30 07:42:30 +02:00
|
|
|
result = session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/voicemail/vm-fail_auth.wav");
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-25 18:31:08 +02:00
|
|
|
--remove the previous device and send a sync command to it
|
|
|
|
|
if (authorized == 'true' and action == "login") then
|
|
|
|
|
sql = [[SELECT * FROM v_device_lines ]];
|
|
|
|
|
sql = sql .. [[WHERE device_uuid = ']]..device_uuid_alternate..[[' ]];
|
|
|
|
|
sql = sql .. [[AND domain_uuid = ']]..domain_uuid..[[' ]];
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] sql: ".. sql .. "\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql, function(row)
|
|
|
|
|
--remove the previous alternate device uuid
|
|
|
|
|
sql = [[UPDATE v_devices SET device_uuid_alternate = null ]];
|
|
|
|
|
sql = sql .. [[WHERE device_uuid_alternate = ']]..device_uuid_alternate..[[' ]];
|
|
|
|
|
sql = sql .. [[AND domain_uuid = ']]..domain_uuid..[[' ]];
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
--freeswitch.consoleLog("NOTICE", "[provision] sql: ".. sql .. "\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql);
|
|
|
|
|
--send a sync command to the previous device
|
|
|
|
|
--create the event notify object
|
|
|
|
|
local event = freeswitch.Event('NOTIFY');
|
|
|
|
|
--add the headers
|
|
|
|
|
event:addHeader('profile', profile);
|
|
|
|
|
event:addHeader('user', row.user_id);
|
|
|
|
|
event:addHeader('host', row.server_address);
|
|
|
|
|
event:addHeader('content-type', 'application/simple-message-summary');
|
|
|
|
|
--check sync
|
2015-05-30 06:50:19 +02:00
|
|
|
event:addHeader('event-string', 'check-sync;reboot=false');
|
2015-05-25 18:31:08 +02:00
|
|
|
--send the event
|
|
|
|
|
event:fire();
|
|
|
|
|
end);
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-01 14:57:06 +02:00
|
|
|
--add the override to the device uuid (login)
|
|
|
|
|
if (authorized == 'true' and action == "login") then
|
2015-04-30 12:21:23 +02:00
|
|
|
if (device_uuid_alternate ~= nil) then
|
2015-05-30 06:50:19 +02:00
|
|
|
--send a hangup
|
|
|
|
|
session:hangup();
|
2015-05-25 18:31:08 +02:00
|
|
|
--add the new alternate
|
2015-05-30 06:50:19 +02:00
|
|
|
sql = [[UPDATE v_devices SET device_uuid_alternate = ']]..device_uuid_alternate..[[']];
|
|
|
|
|
sql = sql .. [[WHERE device_uuid = ']]..device_uuid..[[' ]];
|
|
|
|
|
sql = sql .. [[AND domain_uuid = ']]..domain_uuid..[[' ]];
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] sql: ".. sql .. "\n");
|
|
|
|
|
end
|
|
|
|
|
dbh:query(sql);
|
2015-04-30 07:42:30 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-01 14:57:06 +02:00
|
|
|
--remove the override to the device uuid (logout)
|
|
|
|
|
if (authorized == 'true' and action == "logout") then
|
2015-04-30 12:21:23 +02:00
|
|
|
if (device_uuid_alternate ~= nil) then
|
|
|
|
|
sql = [[UPDATE v_devices SET device_uuid_alternate = null ]];
|
|
|
|
|
sql = sql .. [[WHERE device_uuid_alternate = ']]..device_uuid..[[' ]];
|
2015-05-01 15:37:04 +02:00
|
|
|
sql = sql .. [[AND domain_uuid = ']]..domain_uuid..[[' ]];
|
|
|
|
|
if (debug["sql"]) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[provision] sql: ".. sql .. "\n");
|
|
|
|
|
end
|
2015-04-30 07:42:30 +02:00
|
|
|
dbh:query(sql);
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--found the device send a sync command
|
2015-04-30 12:21:23 +02:00
|
|
|
if (authorized == 'true') then
|
2015-05-30 06:50:19 +02:00
|
|
|
--send a hangup
|
|
|
|
|
session:hangup();
|
2015-04-30 07:42:30 +02:00
|
|
|
--create the event notify object
|
|
|
|
|
local event = freeswitch.Event('NOTIFY');
|
|
|
|
|
--add the headers
|
|
|
|
|
event:addHeader('profile', profile);
|
|
|
|
|
event:addHeader('user', user);
|
|
|
|
|
event:addHeader('host', domain);
|
|
|
|
|
event:addHeader('content-type', 'application/simple-message-summary');
|
|
|
|
|
--check sync
|
2015-05-30 06:50:19 +02:00
|
|
|
event:addHeader('event-string', 'check-sync;reboot=false');
|
2015-04-30 07:42:30 +02:00
|
|
|
--send the event
|
|
|
|
|
event:fire();
|
|
|
|
|
end
|