Change. toll_allow use ordered template-list.
Add. Russian toll_allow templates. This needs because we need 89%d is mobile except 8940%d which one is international.
This commit is contained in:
parent
d5592672f0
commit
0b11ab57de
|
|
@ -22,159 +22,184 @@
|
||||||
-- Contributor(s):
|
-- Contributor(s):
|
||||||
-- Riccardo Granchi <riccardo.granchi@nems.it>
|
-- Riccardo Granchi <riccardo.granchi@nems.it>
|
||||||
-- Philippe Rioual <bhouba@gmail.com>
|
-- Philippe Rioual <bhouba@gmail.com>
|
||||||
|
-- Alexey Melnichuck <alexeymelnichuck@gmail.com>
|
||||||
|
|
||||||
--debug
|
--Configuration
|
||||||
debug["toll_type"] = false
|
-- Define known template names
|
||||||
|
local known_templates = {
|
||||||
|
["mobile" ] = true,
|
||||||
|
["landline" ] = true,
|
||||||
|
["international"] = true,
|
||||||
|
["tollfree" ] = true,
|
||||||
|
["sharedcharge" ] = true,
|
||||||
|
["premium" ] = true,
|
||||||
|
["unknown" ] = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
--Define templates for every toll type for your country
|
||||||
|
local templates = {
|
||||||
|
IT = {
|
||||||
|
{"mobile", "[35]%d%d%d%d%d%d+" },
|
||||||
|
{"landline", "0[123456789]%d+" },
|
||||||
|
{"international", "00%d+" },
|
||||||
|
{"tollfree", "119|1[3456789]%d|19[24]%d|192[01]%d%d|800%d%d%d%d%d+|803%d%d%d+|456%d%d%d%d%d%d+|11[2345678]|15%d%d|116%d%d%d|196%d%d" },
|
||||||
|
{"sharedcharge", "84[0178]%d%d%d%d+|199%d%d%d%d%d+|178%d%d%d%d%d+|12%d%d|10%d%d%d+|1482|149%d+|4[012]%d+|70%d%d%d%d%d+" },
|
||||||
|
{"premium", "89[2459]%d%d%d+|16[456]%d%d%d+|144%d%d%d+|4[346789]%d%d+" },
|
||||||
|
{"unknown", "%d%d+" },
|
||||||
|
};
|
||||||
|
FR = {
|
||||||
|
{"mobile", "0[67]%d%d%d%d%d%d%d%d" },
|
||||||
|
{"landline", "0[1234589]%d%d%d%d%d%d%d%d" },
|
||||||
|
{"international", "00%d+" },
|
||||||
|
{"tollfree", "15|17|18|112|114|115|116%d%d%d|118%d%d%d|119|19[16]|1[06]%d%d|080%d+" },
|
||||||
|
{"sharedcharge", "081%d+|082[0156]%d+|0884%d+|089[0123789]%d+" },
|
||||||
|
{"premium", "%d%d+" },
|
||||||
|
{"unknown", "%d%d+" },
|
||||||
|
};
|
||||||
|
US = {
|
||||||
|
{"unknown", "%d+"},
|
||||||
|
};
|
||||||
|
RU = {
|
||||||
|
{"international", "810%d+|8[89]40%d+|87%d%d%d%d%d%d%d%d%d"};
|
||||||
|
{"mobile", "89%d%d%d%d%d%d%d%d%d" };
|
||||||
|
{"tollfree", "8800%d%d%d%d%d%d%d|10[1-9]" };
|
||||||
|
{"landline", "8[3-68]%d%d%d%d%d%d%d%d%d%d" };
|
||||||
|
{"unknown", "" };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
--Set to true to allow all calls for extensions without toll_allow
|
||||||
|
local ACCEPT_EMPTY_TOLL_ALLOW = false
|
||||||
|
|
||||||
|
--debug
|
||||||
|
debug["toll_type"] = true
|
||||||
|
|
||||||
require "resources.functions.explode";
|
require "resources.functions.explode";
|
||||||
|
|
||||||
--create the api object and get variables
|
--create the api object and get variables
|
||||||
api = freeswitch.API()
|
local api = freeswitch.API()
|
||||||
uuid = argv[2]
|
local uuid = argv[2]
|
||||||
|
|
||||||
if not uuid or uuid == "" then
|
if not uuid or uuid == "" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
template_indexes = { "mobile", "landline", "international", "tollfree", "sharedcharge", "premium", "unknown"}
|
local function hungup()
|
||||||
|
session:hangup("OUTGOING_CALL_BARRED")
|
||||||
--Define templates for every toll type for your country
|
|
||||||
function get_toll_types_it()
|
|
||||||
if (debug["toll_type"]) then
|
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] using IT toll types\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
templates["mobile"] = "[35]%d%d%d%d%d%d+"
|
|
||||||
templates["landline"] = "0[123456789]%d+"
|
|
||||||
templates["international"] = "00%d+"
|
|
||||||
templates["tollfree"] = "119|1[3456789]%d|19[24]%d|192[01]%d%d|800%d%d%d%d%d+|803%d%d%d+|456%d%d%d%d%d%d+|11[2345678]|15%d%d|116%d%d%d|196%d%d"
|
|
||||||
templates["sharedcharge"] = "84[0178]%d%d%d%d+|199%d%d%d%d%d+|178%d%d%d%d%d+|12%d%d|10%d%d%d+|1482|149%d+|4[012]%d+|70%d%d%d%d%d+"
|
|
||||||
templates["premium"] = "89[2459]%d%d%d+|16[456]%d%d%d+|144%d%d%d+|4[346789]%d%d+"
|
|
||||||
templates["unknown"] = "%d%d+"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_toll_types_us()
|
local function log(level, msg)
|
||||||
if (debug["toll_type"]) then
|
freeswitch.consoleLog(level, "[toll_allow] " .. msg .. "\n")
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] using US toll types\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
templates["unknown"] = "%d+"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_toll_types_fr()
|
local function logf(level, ...)
|
||||||
if (debug["toll_type"]) then
|
return log(level, string.format(...))
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] using FR toll types\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
templates["mobile"] = "0[67]%d%d%d%d%d%d%d%d"
|
|
||||||
templates["landline"] = "0[1234589]%d%d%d%d%d%d%d%d"
|
|
||||||
templates["international"] = "00%d+"
|
|
||||||
templates["tollfree"] = "15|17|18|112|114|115|116%d%d%d|118%d%d%d|119|19[16]|1[06]%d%d|080%d+"
|
|
||||||
templates["sharedcharge"] = "081%d+|082[0156]%d+|0884%d+|089[0123789]%d+"
|
|
||||||
templates["premium"] = "%d%d+"
|
|
||||||
templates["unknown"] = "%d%d+"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
called = api:executeString("uuid_getvar " .. uuid .. " destination_number")
|
local function trace(type, ...)
|
||||||
prefix = api:executeString("uuid_getvar " .. uuid .. " outbound_prefix")
|
if debug[type] then log(...) end
|
||||||
country = api:executeString("uuid_getvar " .. uuid .. " default_country")
|
end
|
||||||
toll_allow = api:executeString("uuid_getvar " .. uuid .. " toll_allow")
|
|
||||||
domain_name = api:executeString("uuid_getvar " .. uuid .. " domain_name")
|
local function tracef(type, ...)
|
||||||
caller = api:executeString("uuid_getvar " .. uuid .. " caller_id_number")
|
if debug[type] then logf(...) end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function channel_variable(uuid, name)
|
||||||
|
local result = api:executeString("uuid_getvar " .. uuid .. " " .. name)
|
||||||
|
|
||||||
|
tracef("toll_type", "NOTICE", "channel_variable %s - %s", name, result)
|
||||||
|
|
||||||
|
if result:sub(1, 4) == '-ERR' then return nil end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
local function template_match(prefix, template, called)
|
||||||
|
local parts = explode("|", template)
|
||||||
|
|
||||||
|
for index,part in ipairs(parts) do
|
||||||
|
local pattern = "^" .. prefix .. part .. "$"
|
||||||
|
if ( string.match(called, pattern) ~= nil ) then
|
||||||
|
return pattern
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_toll_type(prefix, templates, called)
|
||||||
|
for _,params in ipairs(templates) do
|
||||||
|
local label, template = params[1], params[2]
|
||||||
|
if not known_templates[label] then
|
||||||
|
logf("WARNING", "unknown template name: %s in country template array", label)
|
||||||
|
end
|
||||||
|
|
||||||
|
trace("toll_type", "NOTICE", "checking toll type " .. label .. " template: " .. template)
|
||||||
|
|
||||||
|
local pattern = template_match(prefix, template, called)
|
||||||
|
if pattern then
|
||||||
|
trace("toll_type", "NOTICE", "destination number " .. called .. " matches " .. label .. " pattern: " .. pattern)
|
||||||
|
return label
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function is_undef(str)
|
||||||
|
return (not str) or (#str == 0) or (str == "_undef_")
|
||||||
|
end
|
||||||
|
|
||||||
|
local called = channel_variable(uuid, "destination_number") or ""
|
||||||
|
local caller = channel_variable(uuid, "caller_id_number") or ""
|
||||||
|
local prefix = channel_variable(uuid, "outbound_prefix") or ""
|
||||||
|
local country = channel_variable(uuid, "default_country") or ""
|
||||||
|
local toll_allow = channel_variable(uuid, "toll_allow") or ""
|
||||||
|
|
||||||
if (debug["toll_type"]) then
|
if (debug["toll_type"]) then
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] called: " .. called .. "\n")
|
logf("NOTICE", "called: %s", called)
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] prefix: " .. prefix .. "\n")
|
logf("NOTICE", "prefix: %s", prefix)
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] country: " .. country .. "\n")
|
logf("NOTICE", "country: %s", country)
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] tollAllow: " .. toll_allow .. "\n")
|
logf("NOTICE", "tollAllow: %s", toll_allow)
|
||||||
end
|
end
|
||||||
|
|
||||||
templates = {}
|
if is_undef(toll_allow) then
|
||||||
local toll_type = "unknown"
|
if ACCEPT_EMPTY_TOLL_ALLOW then
|
||||||
|
logf("NOTICE", "unknown call authorized from %s to %s", caller, called)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
logf("WARNING", "unknown call not authorized from %s to %s : OUTGOING_CALL_BARRED", caller, called)
|
||||||
|
return hungup()
|
||||||
|
end
|
||||||
|
|
||||||
if ((prefix == nil) or (string.len(prefix) == 0) or (prefix == "_undef_") ) then
|
if is_undef(prefix) then
|
||||||
prefix = ""
|
prefix = ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if is_undef(country) then
|
||||||
|
log("WARNING", "undefined country")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
if ((country ~= nil) and (string.len(country) > 0)) then
|
local templates = templates[country]
|
||||||
--set templates for default country
|
if not templates then
|
||||||
if country == "IT" then get_toll_types_it()
|
log("WARNING", "undefined templates")
|
||||||
elseif country == "US" then get_toll_types_us()
|
return
|
||||||
elseif country == "FR" then get_toll_types_fr()
|
end
|
||||||
else
|
|
||||||
if (debug["toll_type"]) then
|
--set toll_type
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] toll type: " .. toll_type .. "\n")
|
local toll_type = get_toll_type(prefix, templates, called) or "unknown"
|
||||||
end
|
|
||||||
return toll_type
|
log("NOTICE", "toll type: " .. toll_type)
|
||||||
|
|
||||||
|
local parts = explode(",", toll_allow)
|
||||||
|
|
||||||
|
for i,part in ipairs(parts) do
|
||||||
|
if not known_templates[part] then
|
||||||
|
logf("WARNING", "unknown toll_allow name: %s in extension", part)
|
||||||
end
|
end
|
||||||
|
|
||||||
--set toll_type
|
tracef("toll_type", "NOTICE", "checking toll allow part " .. part)
|
||||||
local found = false
|
if ( part == toll_type ) then
|
||||||
for i,label in pairs(template_indexes) do
|
logf("NOTICE", "%s call authorized from %s to %s", toll_type, caller, called)
|
||||||
template = templates[label]
|
return
|
||||||
if (debug["toll_type"]) then
|
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] checking toll type " .. label .. " template: " .. template .. "\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
--Doing split on | character
|
|
||||||
parts = explode("|", template)
|
|
||||||
|
|
||||||
for index,part in pairs(parts) do
|
|
||||||
pattern = "^" .. prefix .. part .. "$"
|
|
||||||
if (debug["toll_type"]) then
|
|
||||||
--freeswitch.consoleLog("NOTICE", "[toll_allow] checking toll type " .. label .. " pattern: " .. pattern .. "\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
if ( string.match(called, pattern) ~= nil ) then
|
|
||||||
if (debug["toll_type"]) then
|
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] destination number " .. called .. " matches " .. label .. " pattern: " .. pattern .. "\n")
|
|
||||||
end
|
|
||||||
toll_type = label
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if (found) then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] toll type: " .. toll_type .. "\n")
|
|
||||||
-- api:executeString("uuid_setvar " .. uuid .. " toll_type " .. toll_type);
|
|
||||||
-- session:setVariable('toll_type', toll_type);
|
|
||||||
|
|
||||||
|
|
||||||
--check toll allow
|
|
||||||
allow = false
|
|
||||||
|
|
||||||
if ((toll_allow ~= nil) and (string.len(toll_allow) > 0) and (toll_allow ~= "_undef_")) then
|
|
||||||
parts = explode(",", toll_allow)
|
|
||||||
|
|
||||||
for i,part in pairs(parts) do
|
|
||||||
if (debug["toll_type"]) then
|
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] checking toll allow part " .. part .. "\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
if ( part == toll_type ) then
|
|
||||||
allow = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
freeswitch.consoleLog("WARNING", "[toll_allow] toll_allow not defined for extension " .. caller .. "\n")
|
|
||||||
|
|
||||||
-- Uncomment following line to allow all calls for extensions without toll_allow
|
|
||||||
-- allow = true
|
|
||||||
end
|
|
||||||
|
|
||||||
--hangup not allowed calls
|
|
||||||
if ( not allow ) then
|
|
||||||
freeswitch.consoleLog("WARNING", "[toll_allow] " .. toll_type .. " call not authorized from " .. caller .. " to " .. called .. " : OUTGOING_CALL_BARRED\n")
|
|
||||||
session:hangup("OUTGOING_CALL_BARRED")
|
|
||||||
else
|
|
||||||
freeswitch.consoleLog("NOTICE", "[toll_allow] " .. toll_type .. " call authorized from " .. caller .. " to " .. called .. "\n")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
logf("WARNING", "%s call not authorized from %s to %s : OUTGOING_CALL_BARRED", toll_type, caller, called)
|
||||||
|
return hungup()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue