Make the two inbound lua scripts more robust when the domain is not found.
This commit is contained in:
parent
a9bbdd1f28
commit
11ee2be8f1
|
|
@ -86,10 +86,11 @@
|
||||||
source = "memcache";
|
source = "memcache";
|
||||||
end
|
end
|
||||||
|
|
||||||
--set the call direction as a session variable
|
if (domain_name ~= nil) then
|
||||||
session:setVariable("domain_name", domain_name);
|
--set the call direction as a session variable
|
||||||
session:setVariable("domain", domain_name);
|
session:setVariable("domain_name", domain_name);
|
||||||
session:setVariable("domain_uuid", domain_uuid);
|
session:setVariable("domain", domain_name);
|
||||||
|
session:setVariable("domain_uuid", domain_uuid);
|
||||||
--send information to the console
|
--send information to the console
|
||||||
freeswitch.consoleLog("notice", "[app:dialplan:inbound:get_domain] " .. cache .. " source: ".. source .."\n");
|
freeswitch.consoleLog("notice", "[app:dialplan:inbound:get_domain] " .. cache .. " source: ".. source .."\n");
|
||||||
|
end
|
||||||
|
|
|
||||||
|
|
@ -40,84 +40,86 @@ This method causes the script to get its manadatory arguments directly from the
|
||||||
21 Feb, 2014: move to app/diaplan/resources/call_block
|
21 Feb, 2014: move to app/diaplan/resources/call_block
|
||||||
]]
|
]]
|
||||||
|
|
||||||
-- Command line parameters
|
if (session:getVariable("domain") ~= nil) then
|
||||||
local params = { cmd = "",
|
-- Command line parameters
|
||||||
cid_num = string.gsub(tostring(session:getVariable("caller_id_number")), "+", ""),
|
local params = { cmd = "",
|
||||||
cid_name = session:getVariable("caller_id_name"),
|
cid_num = string.gsub(tostring(session:getVariable("caller_id_number")), "+", ""),
|
||||||
domain = session:getVariable("domain"),
|
cid_name = session:getVariable("caller_id_name"),
|
||||||
userid = "", -- session:getVariable("id")
|
domain = session:getVariable("domain"),
|
||||||
loglevel = "W" -- Warning, Debug, Info
|
userid = "", -- session:getVariable("id")
|
||||||
}
|
loglevel = "W" -- Warning, Debug, Info
|
||||||
|
}
|
||||||
|
|
||||||
-- local storage
|
-- local storage
|
||||||
local sql = nil
|
local sql = nil
|
||||||
|
|
||||||
--define the logger function
|
--define the logger function
|
||||||
local function logger(level, log, data)
|
local function logger(level, log, data)
|
||||||
-- output data to console 'log' if debug level is on
|
-- output data to console 'log' if debug level is on
|
||||||
if string.find(params["loglevel"], level) then
|
if string.find(params["loglevel"], level) then
|
||||||
freeswitch.consoleLog(log, "[Call Block]: " .. data .. "\n");
|
freeswitch.consoleLog(log, "[Call Block]: " .. data .. "\n");
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--connect to the database
|
|
||||||
dofile(scripts_dir.."/resources/functions/database_handle.lua");
|
|
||||||
dbh = database_handle('system');
|
|
||||||
|
|
||||||
--log if not connect
|
|
||||||
if dbh:connected() == false then
|
|
||||||
logger("W", "NOTICE", "db was not connected");
|
|
||||||
end
|
|
||||||
|
|
||||||
-- We have a single command letter
|
|
||||||
-- Use session variables
|
|
||||||
logger("D", "NOTICE", "params default from session, count " .. string.format("%d", #argv[1]) .. " \n");
|
|
||||||
params["cmd"] = argv[1];
|
|
||||||
|
|
||||||
-- ensure that we have a fresh status on exit
|
|
||||||
session:setVariable("call_block", "");
|
|
||||||
|
|
||||||
--send to the log
|
|
||||||
logger("D", "NOTICE", "params are: " .. string.format("'%s', '%s', '%s', '%s'", params["cid_num"],
|
|
||||||
params["cid_name"], params["userid"], params["domain"]));
|
|
||||||
|
|
||||||
--Check if number is in call_block list
|
|
||||||
-- If it is, then increment the counter and block the call
|
|
||||||
--if (params["cmd"] == "C") then
|
|
||||||
sql = "SELECT * FROM v_call_block as c "
|
|
||||||
sql = sql .. "JOIN v_domains as d ON c.domain_uuid=d.domain_uuid "
|
|
||||||
sql = sql .. "WHERE c.call_block_number = '" .. params["cid_num"] .. "' AND d.domain_name = '" .. params["domain"] .."'"
|
|
||||||
--logger("W", "INFO", "sql: " .. sql);
|
|
||||||
status = dbh:query(sql, function(rows)
|
|
||||||
found_cid_num = rows["call_block_number"]
|
|
||||||
found_uuid = rows["call_block_uuid"]
|
|
||||||
found_enabled = rows["call_block_enabled"]
|
|
||||||
found_action = rows["call_block_action"]
|
|
||||||
found_count = rows["call_block_count"]
|
|
||||||
end)
|
|
||||||
-- dbh:affected_rows() doesn't do anything if using core:db so this is the workaround:
|
|
||||||
if found_cid_num then -- caller id exists
|
|
||||||
if (found_enabled == "true") then
|
|
||||||
details = {}
|
|
||||||
k = 0
|
|
||||||
for v in string.gmatch(found_action, "[%w%.]+") do
|
|
||||||
details[k] = v
|
|
||||||
logger("W", "INFO", "Details: " .. details[k]);
|
|
||||||
k = k + 1
|
|
||||||
end
|
|
||||||
dbh:query("UPDATE v_call_block SET call_block_count = " .. found_count + 1 .. " WHERE call_block_uuid = '" .. found_uuid .. "'")
|
|
||||||
session:setVariable("call_block", "block")
|
|
||||||
logger("W", "NOTICE", "number " .. params["cid_num"] .. " blocked with " .. found_count .. " previous hits, domain: " .. params["domain"])
|
|
||||||
if (found_action == "Reject") then
|
|
||||||
session:hangup("CALL_REJECTED")
|
|
||||||
end
|
|
||||||
if (found_action == "Busy") then
|
|
||||||
session:hangup("USER_BUSY")
|
|
||||||
end
|
|
||||||
if (details[0] =="Voicemail") then
|
|
||||||
session:setAutoHangup(false)
|
|
||||||
session:execute("transfer", "*99" .. details[2] .. " XML " .. details[1])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--end
|
|
||||||
|
--connect to the database
|
||||||
|
dofile(scripts_dir.."/resources/functions/database_handle.lua");
|
||||||
|
dbh = database_handle('system');
|
||||||
|
|
||||||
|
--log if not connect
|
||||||
|
if dbh:connected() == false then
|
||||||
|
logger("W", "NOTICE", "db was not connected");
|
||||||
|
end
|
||||||
|
|
||||||
|
-- We have a single command letter
|
||||||
|
-- Use session variables
|
||||||
|
logger("D", "NOTICE", "params default from session, count " .. string.format("%d", #argv[1]) .. " \n");
|
||||||
|
params["cmd"] = argv[1];
|
||||||
|
|
||||||
|
-- ensure that we have a fresh status on exit
|
||||||
|
session:setVariable("call_block", "");
|
||||||
|
|
||||||
|
--send to the log
|
||||||
|
logger("D", "NOTICE", "params are: " .. string.format("'%s', '%s', '%s', '%s'", params["cid_num"],
|
||||||
|
params["cid_name"], params["userid"], params["domain"]));
|
||||||
|
|
||||||
|
--Check if number is in call_block list
|
||||||
|
-- If it is, then increment the counter and block the call
|
||||||
|
--if (params["cmd"] == "C") then
|
||||||
|
sql = "SELECT * FROM v_call_block as c "
|
||||||
|
sql = sql .. "JOIN v_domains as d ON c.domain_uuid=d.domain_uuid "
|
||||||
|
sql = sql .. "WHERE c.call_block_number = '" .. params["cid_num"] .. "' AND d.domain_name = '" .. params["domain"] .."'"
|
||||||
|
--logger("W", "INFO", "sql: " .. sql);
|
||||||
|
status = dbh:query(sql, function(rows)
|
||||||
|
found_cid_num = rows["call_block_number"]
|
||||||
|
found_uuid = rows["call_block_uuid"]
|
||||||
|
found_enabled = rows["call_block_enabled"]
|
||||||
|
found_action = rows["call_block_action"]
|
||||||
|
found_count = rows["call_block_count"]
|
||||||
|
end)
|
||||||
|
-- dbh:affected_rows() doesn't do anything if using core:db so this is the workaround:
|
||||||
|
if found_cid_num then -- caller id exists
|
||||||
|
if (found_enabled == "true") then
|
||||||
|
details = {}
|
||||||
|
k = 0
|
||||||
|
for v in string.gmatch(found_action, "[%w%.]+") do
|
||||||
|
details[k] = v
|
||||||
|
logger("W", "INFO", "Details: " .. details[k]);
|
||||||
|
k = k + 1
|
||||||
|
end
|
||||||
|
dbh:query("UPDATE v_call_block SET call_block_count = " .. found_count + 1 .. " WHERE call_block_uuid = '" .. found_uuid .. "'")
|
||||||
|
session:setVariable("call_block", "block")
|
||||||
|
logger("W", "NOTICE", "number " .. params["cid_num"] .. " blocked with " .. found_count .. " previous hits, domain: " .. params["domain"])
|
||||||
|
if (found_action == "Reject") then
|
||||||
|
session:hangup("CALL_REJECTED")
|
||||||
|
end
|
||||||
|
if (found_action == "Busy") then
|
||||||
|
session:hangup("USER_BUSY")
|
||||||
|
end
|
||||||
|
if (details[0] =="Voicemail") then
|
||||||
|
session:setAutoHangup(false)
|
||||||
|
session:execute("transfer", "*99" .. details[2] .. " XML " .. details[1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue