Update code to iterate over numbers. (#1727)
* Update code to iterate over numbers. This code fixes some problems * range like `009-010` current code produce numbers `009` and `0010` * range like `200-100` now raise error so it will be easy to debug * range like `010-20` now generate error. * `destination` is string so condition `destination == tonumber(sip_from_user)` is always false so change it `destination == sip_from_user` * Fix. handle ranges like `100-100`
This commit is contained in:
committed by
FusionPBX
parent
73af3e3dbd
commit
e4a0127906
@@ -35,17 +35,33 @@
|
|||||||
--define the explode function
|
--define the explode function
|
||||||
require "resources.functions.explode";
|
require "resources.functions.explode";
|
||||||
|
|
||||||
--define the leading_zeros function
|
--define the split function
|
||||||
function leading_zeros(str)
|
require "resources.functions.split";
|
||||||
zeros = '';
|
|
||||||
for i = 1, string.len(str) do
|
--iterator over numbers.
|
||||||
c = string.sub(str, i, i);
|
local function each_number(value)
|
||||||
if (c == '0') then
|
local begin_value, end_value = split_first(value, "-", true)
|
||||||
zeros = zeros .. '0';
|
if (not end_value) or (begin_value == end_value) then
|
||||||
else
|
return function()
|
||||||
return zeros;
|
local result = begin_value
|
||||||
|
begin_value = nil
|
||||||
|
return result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if string.find(begin_value, "^0") then
|
||||||
|
assert(#begin_value == #end_value, "number in range with leading `0` should have same length")
|
||||||
|
end
|
||||||
|
|
||||||
|
local number_length = ("." .. tostring(#begin_value))
|
||||||
|
begin_value, end_value = tonumber(begin_value), tonumber(end_value)
|
||||||
|
assert(begin_value and end_value and (begin_value <= end_value), "Invalid range: " .. value)
|
||||||
|
|
||||||
|
return function()
|
||||||
|
value, begin_value = begin_value, begin_value + 1
|
||||||
|
if value > end_value then return end
|
||||||
|
return string.format("%" .. number_length .. "d", value)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--make sure the session is ready
|
--make sure the session is ready
|
||||||
@@ -138,15 +154,7 @@
|
|||||||
destination_count = 0;
|
destination_count = 0;
|
||||||
api = freeswitch.API();
|
api = freeswitch.API();
|
||||||
for index,value in pairs(destination_table) do
|
for index,value in pairs(destination_table) do
|
||||||
if (string.find(value, "-") == nil) then
|
for destination in each_number(value) do
|
||||||
value = value..'-'..value;
|
|
||||||
end
|
|
||||||
sub_table = explode("-",value);
|
|
||||||
zeros = leading_zeros(sub_table[1]);
|
|
||||||
for destination=sub_table[1],sub_table[2] do
|
|
||||||
|
|
||||||
--add the leading zeros back again
|
|
||||||
destination = zeros .. destination;
|
|
||||||
|
|
||||||
--get the destination required for number-alias
|
--get the destination required for number-alias
|
||||||
destination = api:execute("user_data", destination .. "@" .. domain_name .. " attr id");
|
destination = api:execute("user_data", destination .. "@" .. domain_name .. " attr id");
|
||||||
@@ -160,21 +168,21 @@
|
|||||||
reply = trim(api:executeString(destination_status));
|
reply = trim(api:executeString(destination_status));
|
||||||
if (reply == "0 total.") then
|
if (reply == "0 total.") then
|
||||||
freeswitch.consoleLog("NOTICE", "[page] destination "..destination.." available\n");
|
freeswitch.consoleLog("NOTICE", "[page] destination "..destination.." available\n");
|
||||||
if (destination == tonumber(sip_from_user)) then
|
if destination == sip_from_user then
|
||||||
--this destination is the caller that initated the page
|
--this destination is the caller that initated the page
|
||||||
else
|
else
|
||||||
--originate the call
|
--originate the call
|
||||||
cmd_string = "bgapi originate {sip_auto_answer=true,sip_h_Alert-Info='Ring Answer',hangup_after_bridge=false,rtp_secure_media="..rtp_secure_media..",origination_caller_id_name='"..caller_id_name.."',origination_caller_id_number="..caller_id_number.."}user/"..destination.."@"..domain_name.." conference:"..conference_name.."+"..flags.." inline";
|
cmd_string = "bgapi originate {sip_auto_answer=true,sip_h_Alert-Info='Ring Answer',hangup_after_bridge=false,rtp_secure_media="..rtp_secure_media..",origination_caller_id_name='"..caller_id_name.."',origination_caller_id_number="..caller_id_number.."}user/"..destination.."@"..domain_name.." conference:"..conference_name.."+"..flags.." inline";
|
||||||
api:executeString(cmd_string);
|
api:executeString(cmd_string);
|
||||||
destination_count = destination_count + 1;
|
destination_count = destination_count + 1;
|
||||||
end
|
end
|
||||||
--freeswitch.consoleLog("NOTICE", "cmd_string "..cmd_string.."\n");
|
--freeswitch.consoleLog("NOTICE", "cmd_string "..cmd_string.."\n");
|
||||||
else
|
else
|
||||||
--look inside the reply to check for the correct domain_name
|
--look inside the reply to check for the correct domain_name
|
||||||
if string.find(reply, domain_name) then
|
if string.find(reply, domain_name) then
|
||||||
--found: user is busy
|
--found: user is busy
|
||||||
else
|
else
|
||||||
--not found
|
--not found
|
||||||
if (destination == tonumber(sip_from_user)) then
|
if (destination == tonumber(sip_from_user)) then
|
||||||
--this destination is the caller that initated the page
|
--this destination is the caller that initated the page
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user