fusionpbx/resources/install/scripts/app/call_block/index.lua

206 lines
7.2 KiB
Lua
Raw Normal View History

2019-10-28 16:19:52 +01: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>
-- Copyright (C) 2019
-- the Initial Developer. All Rights Reserved.
--
-- Contributor(s):
-- Mark J Crane <markjcrane@fusionpbx.com>
--set the debug level
debug["sql"] = false;
--includes
local cache = require"resources.functions.cache";
local log = require"resources.functions.log"["call_block"];
--include json library
local json
if (debug["sql"]) then
json = require "resources.functions.lunajson";
end
--include functions
require "resources.functions.trim";
require "resources.functions.explode";
require "resources.functions.file_exists";
--get the variables
if (session:ready()) then
--session:setAutoHangup(false);
domain_uuid = session:getVariable("domain_uuid");
2019-10-31 21:30:56 +01:00
caller_id_name = session:getVariable("caller_id_name");
2019-10-28 16:19:52 +01:00
caller_id_number = session:getVariable("caller_id_number");
context = session:getVariable("context");
call_block = session:getVariable("call_block");
user_exists = session:getVariable("user_exists");
if (user_exists == 'true') then
extension_uuid = session:getVariable("extension_uuid");
end
end
--set default variables
api = freeswitch.API();
--set the dialplan cache key
local call_block_cache_key = "call_block:" .. caller_id_number;
--get the cache
cached_value, err = cache.get(call_block_cache_key);
if (debug['cache']) then
if cached_value then
log.notice(call_block_cache_key.." source: cache");
elseif err ~= 'NOT FOUND' then
log.notice("error cache: " .. err);
end
end
--disable the cache
cached_value = nil;
--run call block one time
if (call_block == nil and call_block ~= 'true') then
--set the cache
if (not cached_value) then
--connect to the database
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
--exits the script if we didn't connect properly
assert(dbh:connected());
2019-10-31 20:48:55 +01:00
--check to see if the call should be blocked
2019-10-28 16:19:52 +01:00
sql = "select * from v_call_block ";
2019-10-31 20:48:55 +01:00
sql = sql .. "where (call_block_name = :call_block_name and call_block_number = :call_block_number) ";
sql = sql .. "or (call_block_name is null and call_block_number = :call_block_number) ";
sql = sql .. "or (call_block_name = :call_block_name and call_block_number is null) ";
2019-10-29 00:20:26 +01:00
if (user_exists == 'true' and extension_uuid ~= nil) then
sql = sql .. "and (extension_uuid is null or extension_uuid = :extension_uuid) ";
end
2019-10-28 16:19:52 +01:00
sql = sql .. "and domain_uuid = :domain_uuid ";
2019-10-29 00:20:26 +01:00
if (user_exists == 'true' and extension_uuid ~= nil) then
2019-10-31 21:30:56 +01:00
params = {domain_uuid = domain_uuid, call_block_name = caller_id_name, call_block_number = caller_id_number, extension_uuid = extension_uuid};
2019-10-29 00:20:26 +01:00
else
2019-10-31 21:30:56 +01:00
params = {domain_uuid = domain_uuid, call_block_name = caller_id_name, call_block_number = caller_id_number};
2019-10-29 00:20:26 +01:00
end
2019-10-28 16:19:52 +01:00
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
end
local found = false;
dbh:query(sql, params, function(row)
call_block_uuid = row.call_block_uuid;
2019-10-31 20:48:55 +01:00
call_block_app = row.call_block_app;
call_block_data = row.call_block_data;
2019-10-28 16:19:52 +01:00
call_block_count = row.call_block_count;
extension_uuid = row.extension_uuid;
cached_value = domain_uuid..','..caller_id_number;
found = true;
end);
--set call block default to false
call_block = false;
if (call_block_action ~= nil) then
call_block = true;
if (session:ready()) then
session:execute('set', 'call_block=true');
end
end
--call block action
2019-10-31 20:48:55 +01:00
if (call_block_app ~= nil and call_block_app == 'busy') then
2019-10-28 16:19:52 +01:00
if (session:ready()) then
session:execute("respond", '486');
session:execute('set', 'call_block_uuid='..call_block_uuid);
2019-10-31 20:48:55 +01:00
session:execute('set', 'call_block_app=busy');
2019-10-28 16:19:52 +01:00
freeswitch.consoleLog("notice", "[call_block] caller id number " .. caller_id_number .. " action: Busy\n");
end
end
2019-10-31 20:48:55 +01:00
if (call_block_app ~= nil and call_block_app == 'hold') then
2019-10-28 16:19:52 +01:00
if (session:ready()) then
2019-10-31 20:48:55 +01:00
session:execute("respond", '607');
2019-10-28 16:19:52 +01:00
session:execute('set', 'call_block_uuid='..call_block_uuid);
2019-10-31 20:48:55 +01:00
session:execute('set', 'call_block_app=hold');
2019-10-28 16:19:52 +01:00
freeswitch.consoleLog("notice", "[call_block] caller id number " .. caller_id_number .. " action: Hold\n");
end
end
2019-10-31 20:48:55 +01:00
if (call_block_app ~= nil and call_block_app == 'reject') then
2019-10-28 16:19:52 +01:00
if (session:ready()) then
2019-10-31 20:48:55 +01:00
session:execute("respond", '607');
2019-10-28 16:19:52 +01:00
session:execute('set', 'call_block_uuid='..call_block_uuid);
2019-10-31 20:48:55 +01:00
session:execute('set', 'call_block_app=reject');
2019-10-28 16:19:52 +01:00
freeswitch.consoleLog("notice", "[call_block] caller id number " .. caller_id_number .. " action: Reject\n");
end
end
2019-10-31 20:48:55 +01:00
if (call_block_app ~= nil and call_block_data ~= nil) then
if (call_block_app == 'voicemail') then
2019-10-28 16:19:52 +01:00
if (session:ready()) then
session:execute('set', 'call_block_uuid='..call_block_uuid);
2019-10-31 20:48:55 +01:00
session:execute('set', 'call_block_app='..call_block_app);
session:execute('set', 'call_block_data='..call_block_data);
session:execute("transfer", '*99'..call_block_data..' XML '.. context);
freeswitch.consoleLog("notice", "[call_block] caller id number " .. caller_id_number .. " action: *99".. call_block_data.."\n");
2019-10-28 16:19:52 +01:00
end
end
end
--update the call block count
if (call_block) then
sql = "update v_call_block ";
sql = sql .. "set call_block_count = :call_block_count ";
sql = sql .. "where call_block_uuid = :call_block_uuid ";
local params = {call_block_uuid = call_block_uuid, call_block_count = call_block_count + 1};
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
end
dbh:query(sql, params);
end
--close the database connection
dbh:release();
--set the cache
local ok, err = cache.set(call_block_cache_key, cached_value, '3600');
if debug["cache"] then
if ok then
freeswitch.consoleLog("notice", "[call_block] " .. call_block_cache_key .. " stored in the cache\n");
else
freeswitch.consoleLog("warning", "[call_block] " .. call_block_cache_key .. " can not be stored in the cache: " .. tostring(err) .. "\n");
end
end
--send to the console
if (debug["cache"]) then
freeswitch.consoleLog("notice", "[call_block] " .. call_block_cache_key .. " source: database\n");
end
else
--send to the console
if (debug["cache"]) then
freeswitch.consoleLog("notice", "[call_block] " .. call_block_cache_key .. " source: cache\n");
end
end
end