Use local variables.

This commit is contained in:
Alexey Melnichuk
2015-10-25 09:57:20 +03:00
parent c55de4769c
commit 8bbf8d6777
2 changed files with 27 additions and 30 deletions
+25 -28
View File
@@ -24,9 +24,7 @@
-- Errol W Samuels <ewsamuels@gmail.com> -- Errol W Samuels <ewsamuels@gmail.com>
--user defined variables --user defined variables
max_tries = "3"; local extension = argv[1];
digit_timeout = "5000";
extension = argv[1];
-- we can use any number because other box should check sip_h_X_*** headers first -- we can use any number because other box should check sip_h_X_*** headers first
local pickup_number = '*8' -- extension and '**' or '*8' local pickup_number = '*8' -- extension and '**' or '*8'
@@ -58,15 +56,6 @@
local function make_proxy_call(destination, call_hostname) local function make_proxy_call(destination, call_hostname)
destination = destination .. "@" .. domain_name destination = destination .. "@" .. domain_name
local profile, proxy = "internal", call_hostname; local profile, proxy = "internal", call_hostname;
local peer = CLASTER_PEERS and CLASTER_PEERS[call_hostname];
if peer then
if type(peer) == "string" then
proxy = peer;
else
profile = peer[1] or profile;
proxy = peer[2] or proxy;
end
end
local sip_auth_username = session:getVariable("sip_auth_username"); local sip_auth_username = session:getVariable("sip_auth_username");
local sip_auth_password = api:execute("user_data", sip_auth_username .. "@" .. domain_name .." param password"); local sip_auth_password = api:execute("user_data", sip_auth_username .. "@" .. domain_name .." param password");
@@ -85,11 +74,14 @@
--sleep --sleep
session:sleep(500); session:sleep(500);
--get the user pin number --get the user pin number
min_digits = 2; local min_digits = 2;
max_digits = 20; local max_digits = 20;
digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+"); local max_tries = "3";
local digit_timeout = "5000";
local digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
--validate the user pin number --validate the user pin number
pin_number_table = explode(",",pin_number); local pin_number_table = explode(",",pin_number);
for index,pin_number in pairs(pin_number_table) do for index,pin_number in pairs(pin_number_table) do
if (digits == pin_number) then if (digits == pin_number) then
--set the authorized pin number that was used --set the authorized pin number that was used
@@ -98,6 +90,7 @@
return true; return true;
end end
end end
--if not authorized play a message and then hangup --if not authorized play a message and then hangup
session:streamFile("phrase:voicemail_fail_auth:#"); session:streamFile("phrase:voicemail_fail_auth:#");
session:hangup("NORMAL_CLEARING"); session:hangup("NORMAL_CLEARING");
@@ -138,7 +131,7 @@
end end
-- search parent hostname -- search parent hostname
call_hostname = hostname call_hostname = hostname
--[[ parent and child have to be on same box so we do not search it --[[ parent and child have to be on same box so we do not search it
log.notice("Found parent channel try detect parent hostname") log.notice("Found parent channel try detect parent hostname")
local dbh = Database.new('switch') local dbh = Database.new('switch')
@@ -167,15 +160,15 @@
-- return array of extensions for group -- return array of extensions for group
local function select_group_extensions() local function select_group_extensions()
-- connect to Fusion database -- connect to Fusion database
dbh = Database.new('system'); local dbh = Database.new('system');
--get the call groups the extension is a member of --get the call groups the extension is a member of
sql = "SELECT call_group FROM v_extensions "; local sql = "SELECT call_group FROM v_extensions ";
sql = sql .. "WHERE domain_uuid = '"..domain_uuid.."' "; sql = sql .. "WHERE domain_uuid = '"..domain_uuid.."' ";
sql = sql .. "AND (extension = '"..caller_id_number.."'"; sql = sql .. "AND (extension = '"..caller_id_number.."'";
sql = sql .. "OR number_alias = '"..caller_id_number.."')"; sql = sql .. "OR number_alias = '"..caller_id_number.."')";
sql = sql .. "limit 1"; sql = sql .. "limit 1";
call_group = dbh:first_value(sql) or '' local call_group = dbh:first_value(sql) or ''
log.noticef("call_group: `%s`", call_group); log.noticef("call_group: `%s`", call_group);
call_groups = explode(",", call_group); call_groups = explode(",", call_group);
@@ -198,7 +191,7 @@
log.notice("sql "..sql); log.notice("sql "..sql);
end end
local extensions = {} local extensions = {}
status = dbh:query(sql, function(row) dbh:query(sql, function(row)
local member = row.extension local member = row.extension
if row.number_alias and #row.number_alias > 0 then if row.number_alias and #row.number_alias > 0 then
member = row.number_alias member = row.number_alias
@@ -239,7 +232,9 @@
if not pin(pin_number) then if not pin(pin_number) then
return return
end end
end
if ( session:ready() ) then
-- select intercept mode -- select intercept mode
if not extension then if not extension then
log.notice("GROUP INTERCEPT") log.notice("GROUP INTERCEPT")
@@ -250,15 +245,18 @@
end end
--connect to FS database --connect to FS database
dbh = Database.new('switch') local dbh = Database.new('switch')
--check the database to get the uuid of a ringing call --check the database to get the uuid of a ringing call
call_hostname = ""; call_hostname = "";
sql = "SELECT uuid, call_uuid, hostname, ip_addr FROM channels "; sql = "SELECT uuid, call_uuid, hostname FROM channels ";
sql = sql .. "WHERE callstate in ('RINGING', 'EARLY') "; sql = sql .. "WHERE callstate in ('RINGING', 'EARLY') ";
if not extension then -- next check should prevent pickup call from extension
sql = sql .. "AND direction = 'outbound' "; -- e.g. if extension 100 dial some cell phone and some one else dial *8
end -- he can pickup this call.
-- if not extension then
-- sql = sql .. "AND direction = 'outbound' ";
-- end
sql = sql .. "AND (1=1 "; sql = sql .. "AND (1=1 ";
for key,extension in pairs(extensions) do for key,extension in pairs(extensions) do
sql = sql .. "OR presence_id = '"..extension.."@"..domain_name.."' "; sql = sql .. "OR presence_id = '"..extension.."@"..domain_name.."' ";
@@ -274,11 +272,10 @@
-- for key, val in pairs(row) do -- for key, val in pairs(row) do
-- log.notice("row "..key.." "..val); -- log.notice("row "..key.." "..val);
-- end -- end
log.notice("-----------------------"); -- log.notice("-----------------------");
is_child = (row.uuid == row.call_uuid) is_child = (row.uuid == row.call_uuid)
uuid = row.call_uuid; uuid = row.call_uuid;
call_hostname = row.hostname; call_hostname = row.hostname;
ip_addr = row.ip_addr;
end); end);
if is_child then if is_child then
@@ -77,8 +77,8 @@ function Database:connected(sql)
return self._dbh and self._dbh:connected() return self._dbh and self._dbh:connected()
end end
function Database.__self_test__() function Database.__self_test__(name)
local db = Database.new('system') local db = Database.new(name or 'system')
assert(db:connected()) assert(db:connected())
assert("1" == db:first_value("select 1 as v union all select 2 as v")) assert("1" == db:first_value("select 1 as v union all select 2 as v"))