Restore directory.lua - this fixes load balancing as taught in the FusionPBX Advanced Class.
This commit is contained in:
parent
d3a1e48c18
commit
c2b0645c50
|
|
@ -13,7 +13,7 @@
|
||||||
-- notice, this list of conditions and the following disclaimer in the
|
-- notice, this list of conditions and the following disclaimer in the
|
||||||
-- documentation and/or other materials provided with the distribution.
|
-- documentation and/or other materials provided with the distribution.
|
||||||
--
|
--
|
||||||
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
-- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||||
|
|
@ -26,7 +26,7 @@
|
||||||
--
|
--
|
||||||
-- Contributor(s):
|
-- Contributor(s):
|
||||||
-- Mark J Crane <markjcrane@fusionpbx.com>
|
-- Mark J Crane <markjcrane@fusionpbx.com>
|
||||||
-- Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
|
-- Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
|
||||||
|
|
||||||
--set the default
|
--set the default
|
||||||
continue = true;
|
continue = true;
|
||||||
|
|
@ -62,32 +62,26 @@
|
||||||
--all other directory actions: sip_auth, user_call
|
--all other directory actions: sip_auth, user_call
|
||||||
--except for the action: group_call
|
--except for the action: group_call
|
||||||
|
|
||||||
-- Make sance only for extensions with number_alias
|
|
||||||
-- true - you should register with AuthID=Extension and UserID=Number Alias
|
|
||||||
-- false - you should register with AuthID=UserID=Extension
|
|
||||||
-- also in this case you need 2 records in memcache for one extension
|
|
||||||
local DIAL_STRING_BASED_ON_USERID = false
|
|
||||||
|
|
||||||
local sip_auth_method = params:getHeader("sip_auth_method")
|
|
||||||
if sip_auth_method then
|
|
||||||
sip_auth_method = sip_auth_method:upper();
|
|
||||||
end
|
|
||||||
|
|
||||||
local from_user = params:getHeader("sip_from_user")
|
|
||||||
|
|
||||||
-- verify from_user and number alias for this methods
|
|
||||||
local METHODS = {
|
|
||||||
-- _ANY_ = true,
|
|
||||||
REGISTER = true,
|
|
||||||
-- INVITE = true,
|
|
||||||
}
|
|
||||||
|
|
||||||
if (user == nil) then
|
if (user == nil) then
|
||||||
user = "";
|
user = "";
|
||||||
end
|
end
|
||||||
|
|
||||||
if (from_user == "") or (from_user == nil) then
|
--get the cache
|
||||||
from_user = user
|
if (trim(api:execute("module_exists", "mod_memcache")) == "true") then
|
||||||
|
if (domain_name) then
|
||||||
|
XML_STRING = trim(api:execute("memcache", "get directory:" .. user .. "@" .. domain_name));
|
||||||
|
end
|
||||||
|
if (XML_STRING == "-ERR NOT FOUND") or (XML_STRING == "-ERR CONNECTION FAILURE") then
|
||||||
|
source = "database";
|
||||||
|
continue = true;
|
||||||
|
else
|
||||||
|
source = "cache";
|
||||||
|
continue = true;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
XML_STRING = "";
|
||||||
|
source = "database";
|
||||||
|
continue = true;
|
||||||
end
|
end
|
||||||
|
|
||||||
--prevent processing for invalid user
|
--prevent processing for invalid user
|
||||||
|
|
@ -96,40 +90,6 @@
|
||||||
continue = false;
|
continue = false;
|
||||||
end
|
end
|
||||||
|
|
||||||
-- cleanup
|
|
||||||
XML_STRING = nil;
|
|
||||||
|
|
||||||
--get the cache
|
|
||||||
if (continue) then
|
|
||||||
if (trim(api:execute("module_exists", "mod_memcache")) == "true") then
|
|
||||||
if (domain_name) then
|
|
||||||
local key = "directory:" .. (from_user or user) .. "@" .. domain_name
|
|
||||||
XML_STRING = trim(api:execute("memcache", "get " .. key));
|
|
||||||
|
|
||||||
if debug['cache'] then
|
|
||||||
if XML_STRING:sub(1, 4) == '-ERR' then
|
|
||||||
freeswitch.consoleLog("notice", "[xml_handler-directory][memcache] get key: " .. key .. " fail: " .. XML_STRING .. "\n")
|
|
||||||
else
|
|
||||||
freeswitch.consoleLog("notice", "[xml_handler-directory][memcache] get key: " .. key .. " pass!" .. "\n")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
XML_STRING = "-ERR NOT FOUND"
|
|
||||||
end
|
|
||||||
if (XML_STRING == "-ERR NOT FOUND") or (XML_STRING == "-ERR CONNECTION FAILURE") then
|
|
||||||
source = "database";
|
|
||||||
continue = true;
|
|
||||||
else
|
|
||||||
source = "cache";
|
|
||||||
continue = true;
|
|
||||||
end
|
|
||||||
else
|
|
||||||
XML_STRING = "";
|
|
||||||
source = "database";
|
|
||||||
continue = true;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--show the params in the console
|
--show the params in the console
|
||||||
--if (params:serialize() ~= nil) then
|
--if (params:serialize() ~= nil) then
|
||||||
-- freeswitch.consoleLog("notice", "[xml_handler-directory.lua] Params:\n" .. params:serialize() .. "\n");
|
-- freeswitch.consoleLog("notice", "[xml_handler-directory.lua] Params:\n" .. params:serialize() .. "\n");
|
||||||
|
|
@ -144,10 +104,8 @@
|
||||||
--freeswitch.consoleLog("notice", "[xml_handler-directory.lua] dialed_extension is " .. dialed_extension .. "\n");
|
--freeswitch.consoleLog("notice", "[xml_handler-directory.lua] dialed_extension is " .. dialed_extension .. "\n");
|
||||||
end
|
end
|
||||||
|
|
||||||
local loaded_from_db = false
|
|
||||||
--build the XML string from the database
|
--build the XML string from the database
|
||||||
if (source == "database") or (load_balancing) then
|
if (source == "database") or (load_balancing) then
|
||||||
loaded_from_db = true
|
|
||||||
--database connection
|
--database connection
|
||||||
if (continue) then
|
if (continue) then
|
||||||
--connect to the database
|
--connect to the database
|
||||||
|
|
@ -239,10 +197,8 @@
|
||||||
if (debug["sql"]) then
|
if (debug["sql"]) then
|
||||||
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n");
|
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n");
|
||||||
end
|
end
|
||||||
continue = false
|
|
||||||
dbh:query(sql, function(row)
|
dbh:query(sql, function(row)
|
||||||
--general
|
--general
|
||||||
continue = true
|
|
||||||
domain_uuid = row.domain_uuid;
|
domain_uuid = row.domain_uuid;
|
||||||
extension_uuid = row.extension_uuid;
|
extension_uuid = row.extension_uuid;
|
||||||
extension = row.extension;
|
extension = row.extension;
|
||||||
|
|
@ -260,7 +216,6 @@
|
||||||
auth_acl = row.auth_acl;
|
auth_acl = row.auth_acl;
|
||||||
--variables
|
--variables
|
||||||
sip_from_user = row.extension;
|
sip_from_user = row.extension;
|
||||||
sip_from_number = (#number_alias > 0) and number_alias or row.extension;
|
|
||||||
call_group = row.call_group;
|
call_group = row.call_group;
|
||||||
call_screen_enabled = row.call_screen_enabled;
|
call_screen_enabled = row.call_screen_enabled;
|
||||||
user_record = row.user_record;
|
user_record = row.user_record;
|
||||||
|
|
@ -294,22 +249,13 @@
|
||||||
forward_no_answer_destination = row.forward_no_answer_destination;
|
forward_no_answer_destination = row.forward_no_answer_destination;
|
||||||
do_not_disturb = row.do_not_disturb;
|
do_not_disturb = row.do_not_disturb;
|
||||||
|
|
||||||
-- check matching UserID and AuthName
|
|
||||||
if sip_auth_method and (METHODS[sip_auth_method] or METHODS._ANY_) then
|
|
||||||
continue = (sip_from_user == user) and ((sip_from_number == from_user) or (from_user == user))
|
|
||||||
if not continue then
|
|
||||||
XML_STRING = nil;
|
|
||||||
return 1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--set the dial_string
|
--set the dial_string
|
||||||
if (string.len(row.dial_string) > 0) then
|
if (string.len(row.dial_string) > 0) then
|
||||||
dial_string = row.dial_string;
|
dial_string = row.dial_string;
|
||||||
else
|
else
|
||||||
--set a default dial string
|
--set a default dial string
|
||||||
if (dial_string == null) then
|
if (dial_string == null) then
|
||||||
dial_string = "{sip_invite_domain=" .. domain_name .. ",presence_id=" .. user .. "@" .. domain_name .. "}${sofia_contact(" .. (DIAL_STRING_BASED_ON_USERID and sip_from_number or sip_from_user) .. "@" .. domain_name .. ")}";
|
dial_string = "{sip_invite_domain=" .. domain_name .. ",presence_id=" .. user .. "@" .. domain_name .. "}${sofia_contact(" .. extension .. "@" .. domain_name .. ")}";
|
||||||
end
|
end
|
||||||
--set the an alternative dial string if the hostnames don't match
|
--set the an alternative dial string if the hostnames don't match
|
||||||
if (load_balancing) then
|
if (load_balancing) then
|
||||||
|
|
@ -336,8 +282,8 @@
|
||||||
--get the voicemail from the database
|
--get the voicemail from the database
|
||||||
if (continue) then
|
if (continue) then
|
||||||
vm_enabled = "true";
|
vm_enabled = "true";
|
||||||
if number_alias and #number_alias > 0 then
|
if tonumber(user) == nil then
|
||||||
sql = "SELECT * FROM v_voicemails WHERE domain_uuid = '" .. domain_uuid .. "' and voicemail_id = '" .. number_alias .. "' ";
|
sql = "SELECT * FROM v_voicemails WHERE domain_uuid = '" .. domain_uuid .. "' and voicemail_id = '" .. number_alias .. "' ";
|
||||||
else
|
else
|
||||||
sql = "SELECT * FROM v_voicemails WHERE domain_uuid = '" .. domain_uuid .. "' and voicemail_id = '" .. user .. "' ";
|
sql = "SELECT * FROM v_voicemails WHERE domain_uuid = '" .. domain_uuid .. "' and voicemail_id = '" .. user .. "' ";
|
||||||
end
|
end
|
||||||
|
|
@ -426,7 +372,7 @@
|
||||||
table.insert(xml, [[ <variable name="extension_uuid" value="]] .. extension_uuid .. [["/>]]);
|
table.insert(xml, [[ <variable name="extension_uuid" value="]] .. extension_uuid .. [["/>]]);
|
||||||
table.insert(xml, [[ <variable name="call_timeout" value="]] .. call_timeout .. [["/>]]);
|
table.insert(xml, [[ <variable name="call_timeout" value="]] .. call_timeout .. [["/>]]);
|
||||||
table.insert(xml, [[ <variable name="caller_id_name" value="]] .. sip_from_user .. [["/>]]);
|
table.insert(xml, [[ <variable name="caller_id_name" value="]] .. sip_from_user .. [["/>]]);
|
||||||
table.insert(xml, [[ <variable name="caller_id_number" value="]] .. sip_from_number .. [["/>]]);
|
table.insert(xml, [[ <variable name="caller_id_number" value="]] .. sip_from_user .. [["/>]]);
|
||||||
if (string.len(call_group) > 0) then
|
if (string.len(call_group) > 0) then
|
||||||
table.insert(xml, [[ <variable name="call_group" value="]] .. call_group .. [["/>]]);
|
table.insert(xml, [[ <variable name="call_group" value="]] .. call_group .. [["/>]]);
|
||||||
end
|
end
|
||||||
|
|
@ -543,11 +489,9 @@
|
||||||
dbh:release();
|
dbh:release();
|
||||||
|
|
||||||
--set the cache
|
--set the cache
|
||||||
local key = "directory:" .. (DIAL_STRING_BASED_ON_USERID and sip_from_number or sip_from_user) .. "@" .. domain_name
|
if (user and domain_name) then
|
||||||
if debug['cache'] then
|
result = trim(api:execute("memcache", "set directory:" .. user .. "@" .. domain_name .. " '"..XML_STRING:gsub("'", "'").."' "..expire["directory"]));
|
||||||
freeswitch.consoleLog("notice", "[xml_handler-directory][memcache] set key: " .. key .. "\n")
|
|
||||||
end
|
end
|
||||||
result = trim(api:execute("memcache", "set " .. key .. " '"..XML_STRING:gsub("'", "'").."' "..expire["directory"]));
|
|
||||||
|
|
||||||
--send the xml to the console
|
--send the xml to the console
|
||||||
if (debug["xml_string"]) then
|
if (debug["xml_string"]) then
|
||||||
|
|
@ -563,14 +507,11 @@
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if XML_STRING and (not loaded_from_db)
|
--disable registration for number-alias
|
||||||
and sip_auth_method and (METHODS[sip_auth_method] or METHODS._ANY_)
|
if (params:getHeader("sip_auth_method") == "REGISTER") then
|
||||||
then
|
if (api:execute("user_data", user .. "@" .. domain_name .." attr id") ~= user) then
|
||||||
local user_id = api:execute("user_data", from_user .. "@" .. domain_name .." attr id")
|
XML_STRING = nil;
|
||||||
--disable registration for number-alias and foreign number_alias
|
end
|
||||||
if user_id ~= user then
|
|
||||||
XML_STRING = nil;
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--get the XML string from the cache
|
--get the XML string from the cache
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue