From d2e53cd125a2c5003653cd9a571baee97faabd2e Mon Sep 17 00:00:00 2001 From: Mark Crane Date: Thu, 29 Nov 2012 03:43:58 +0000 Subject: [PATCH] Add gateways and sip profiles to the xml handler. --- includes/install/scripts/xml_handler.lua | 1115 +++++++++++++++------- 1 file changed, 763 insertions(+), 352 deletions(-) diff --git a/includes/install/scripts/xml_handler.lua b/includes/install/scripts/xml_handler.lua index 7f8fb1aa34..a16e3e9ea9 100644 --- a/includes/install/scripts/xml_handler.lua +++ b/includes/install/scripts/xml_handler.lua @@ -24,11 +24,56 @@ -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -- POSSIBILITY OF SUCH DAMAGE. +--to do list + --adding gateways + --to an array + --then add to the sip profiles + --system variables + --replace ${var} and $${var}. + +--set defaults + expire = {} + expire["directory"] = "90"; + expire["dialplan"] = "300"; + expire["sofia.conf"] = "3600"; + --set the debug level debug["params"] = false; debug["sql"] = false; debug["xml_request"] = false; debug["xml_string"] = false; + debug["cache"] = false; + +--include the lua script + scripts_dir = string.sub(debug.getinfo(1).source,2,string.len(debug.getinfo(1).source)-(string.len(argv[0])+1)); + include = assert(loadfile(scripts_dir .. "/resources/config.lua")); + include(); + +--connect to the database + --ODBC - data source name + if (dsn_name) then + dbh = freeswitch.Dbh(dsn_name,dsn_username,dsn_password); + end + --FreeSWITCH core db handler + if (db_type == "sqlite") then + dbh = freeswitch.Dbh("core:"..db_path.."/"..db_name); + end + +--add the trim function + function trim(s) + return s:gsub("^%s+", ""):gsub("%s+$", "") + end + +--if the params class and methods do not exist then add them to prevent errors + if (not params) then + params = {} + function params:getHeader(name) + self.name = name + end + function params:serialize(name) + self.name = name + end + end --show param debug info if (debug["params"]) then @@ -46,20 +91,8 @@ local destination_number = params:getHeader("Caller-Destination-Number"); local caller_id_number = params:getHeader("Caller-Caller-ID-Number"); ---include the lua script - scripts_dir = string.sub(debug.getinfo(1).source,2,string.len(debug.getinfo(1).source)-(string.len(argv[0])+1)); - include = assert(loadfile(scripts_dir .. "/resources/config.lua")); - include(); - ---connect to the database - --ODBC - data source name - if (dsn_name) then - dbh = freeswitch.Dbh(dsn_name,dsn_username,dsn_password); - end - --FreeSWITCH core db handler - if (db_type == "sqlite") then - dbh = freeswitch.Dbh("core:"..db_path.."/"..db_name); - end +--prepare the api object + api = freeswitch.API(); --get the domain_uuid if (domain_name ~= nil) then @@ -72,17 +105,355 @@ domain_uuid = rows["domain_uuid"]; end); end - ---handle gateways - if (purpose == "gateways" and profile) then - --freeswitch.consoleLog("notice", "[xml_handler] Gateways: " .. profile .." profile\n"); --purpose + if (domain_uuid == nil) then + domain_uuid = "00000000-0000-0000-0000-000000000000"; end +--handle the configuration + if (XML_REQUEST["section"] == "configuration") then + --sofia.conf - profiles and gateways + if (XML_REQUEST["key_value"] == "sofia.conf") then + + --get the cache + if (trim(api:execute("module_exists", "mod_memcache")) == "true") then + XML_STRING = trim(api:execute("memcache", "get sofia.conf")); + else + XML_STRING = "-ERR NOT FOUND"; + end + + if (XML_STRING == "-ERR NOT FOUND") then + + --get the variables + vars = trim(api:execute("global_getvar", "")); + + --start the xml array + local xml = {} + table.insert(xml, [[]]); + table.insert(xml, [[]]); + table.insert(xml, [[
]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + --table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + --table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + + --set defaults + previous_sip_profile_name = ""; + profile_tag_status = "closed"; + + --run the query + sql = "select p.sip_profile_name, p.sip_profile_description, s.sip_profile_setting_name, s.sip_profile_setting_value "; + sql = sql .. "from v_sip_profiles as p, v_sip_profile_settings as s "; + sql = sql .. "where p.sip_profile_uuid = s.sip_profile_uuid "; + sql = sql .. "and s.sip_profile_setting_enabled = 'true' "; + sql = sql .. "order by p.sip_profile_name asc "; + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); + end + x = 0; + dbh:query(sql, function(row) + --set as variables + sip_profile_name = row.sip_profile_name; + --sip_profile_description = row.sip_profile_description; + sip_profile_setting_name = row.sip_profile_setting_name; + sip_profile_setting_value = row.sip_profile_setting_value; + + --open xml tag + if (sip_profile_name ~= previous_sip_profile_name) then + if (x > 1) then + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + end + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + --table.insert(xml, [[ ]]); + + --get the gateways + if (domain_count > 1) then + sql = "select * from v_gateways as g, v_domains as d "; + sql = sql .. "where g.profile = '"..sip_profile_name.."' "; + sql = sql .. "and g.enabled = 'true' "; + sql = sql .. "and g.domain_uuid = d.domain_uuid "; + else + sql = "select * from v_gateways "; + sql = sql .. "where profile = '"..sip_profile_name.."' and enabled = 'true' "; + end + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); + end + x = 0; + dbh:query(sql, function(field) + --set as variables + gateway = field.gateway; + gateway = gateway:gsub(" ", "_"); + + if (domain_count > 1) then + table.insert(xml, [[ ]]); + else + table.insert(xml, [[ ]]); + end + + if (string.len(field.username) > 0) then + table.insert(xml, [[ ]]); + else + table.insert(xml, [[ ]]); + end + if (string.len(field.distinct_to) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.auth_username) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.password) > 0) then + table.insert(xml, [[ ]]); + else + table.insert(xml, [[ ]]); + end + if (string.len(field.realm) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.from_user) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.from_domain) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.proxy) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.register_proxy) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.outbound_proxy) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.expire_seconds) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.register) > 0) then + table.insert(xml, [[ ]]); + end + + if (field.register_transport) then + if (field.register_transport == "udp") then + table.insert(xml, [[ ]]); + elseif (field.register_transport == "tcp") then + table.insert(xml, [[ ]]); + elseif (field.register_transport == "tls") then + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + else + table.insert(xml, [[ ]]); + end + end + + if (string.len(field.retry_seconds) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.extension) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.ping) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.context) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.caller_id_in_from) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.supress_cng) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.sip_cid_type) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(field.extension_in_contact) > 0) then + table.insert(xml, [[ ]]); + end + table.insert(xml, [[ ]]); + end) + + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + profile_tag_status = "open"; + end + + --loop through the var array + for line in (vars.."\n"):gmatch"(.-)\n" do + if (line) then + --name = string.sub( line, 0, pos-1); + --value = string.sub( line, pos+1); + pos = string.find(line, "=", 0, true); + sip_profile_setting_value = sip_profile_setting_value:gsub("%$%${"..string.sub( line, 0, pos-1).."}", string.sub( line, pos+1)); + end + end + + --remove $ and replace with "" + --if (sip_profile_setting_value) then + -- sip_profile_setting_value = sip_profile_setting_value:gsub("%$", ""); + --end + + --set the parameters + if (sip_profile_setting_name) then + table.insert(xml, [[ ]]); + end + + --set the previous value + previous_sip_profile_name = sip_profile_name; + + --increment the value of x + x = x + 1; + end) + + --close the extension tag if it was left open + if (profile_tag_status == "open") then + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + profile_tag_status = "close"; + end + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[
]]); + table.insert(xml, [[
]]); + XML_STRING = table.concat(xml, "\n"); + if (debug["xml_string"]) then + freeswitch.consoleLog("notice", "[xml_handler] XML_STRING: " .. XML_STRING .. "\n"); + end + + --set the cache + result = trim(api:execute("memcache", "set sofia.conf '"..XML_STRING:gsub("'", "'").."' "..expire["sofia.conf"])); + + --send the xml to the console + if (debug["xml_string"]) then + local file = assert(io.open("/tmp/sofia.conf.xml", "w")); + file:write(XML_STRING); + file:close(); + end + + --send to the console + if (debug["cache"]) then + freeswitch.consoleLog("notice", "[xml_handler] sofia.conf source: database\n"); + end + else + --send to the console + if (debug["cache"]) then + freeswitch.consoleLog("notice", "[xml_handler] sofia.conf source: memcache\n"); + end + end --if XML_STRING + end --sofia.conf + + --conference.conf - conference controls, and conference profiles + if (XML_REQUEST["key_value"] == "conference.conf") then + + --start the xml array + local xml = {} + table.insert(xml, [[]]); + table.insert(xml, [[]]); + table.insert(xml, [[
]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, ""); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + + --set the xml array and then concatenate the array to a string + table.insert(xml, [[ ]]); + table.insert(xml, [[
]]); + table.insert(xml, [[
]]); + XML_STRING = table.concat(xml, "\n"); + + --send the xml to the console + if (debug["xml_string"]) then + local file = assert(io.open("/tmp/conference.conf.xml", "w")); + file:write(XML_STRING); + file:close(); + end + end --conference.conf + end --section configuration + --handle the directory if (XML_REQUEST["section"] == "directory" and key and user and domain_name) then - --prevent processing for invalid user + --set the default continue = true; + + --get the cache + if (trim(api:execute("module_exists", "mod_memcache")) == "true") then + XML_STRING = trim(api:execute("memcache", "get " .. user .. "@" .. domain_name)); + if (XML_STRING == "-ERR NOT FOUND") then + continue = true; + else + continue = false; + end + else + XML_STRING = ""; + continue = true; + end + + --prevent processing for invalid user if (user == "*97") then continue = false; end @@ -159,162 +530,179 @@ end --outbound hot desking - get the extension variables - sql = "SELECT * FROM v_extensions WHERE dial_domain = '" .. domain_name .. "' and dial_user = '" .. user .. "' and enabled = 'true' "; - if (debug["sql"]) then - freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); + if (continue) then + sql = "SELECT * FROM v_extensions WHERE dial_domain = '" .. domain_name .. "' and dial_user = '" .. user .. "' and enabled = 'true' "; + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); + end + dbh:query(sql, function(row) + --variables + extension_uuid = row.extension_uuid; + domain_uuid = row.domain_uuid; + extension = row.extension; + sip_from_user = row.extension; + call_group = row.call_group; + hold_music = row.hold_music; + toll_allow = row.toll_allow; + accountcode = row.accountcode; + user_context = row.user_context; + effective_caller_id_name = row.effective_caller_id_name; + effective_caller_id_number = row.effective_caller_id_number; + outbound_caller_id_name = row.outbound_caller_id_name; + outbound_caller_id_number = row.outbound_caller_id_number; + emergency_caller_id_number = row.emergency_caller_id_number; + directory_full_name = row.directory_full_name; + directory_visible = row.directory_visible; + directory_exten_visible = row.directory_exten_visible; + limit_max = row.limit_max; + limit_destination = row.limit_destination; + sip_force_contact = row.sip_force_contact; + sip_force_expires = row.sip_force_expires; + nibble_account = row.nibble_account; + sip_bypass_media = row.sip_bypass_media; + end); end - dbh:query(sql, function(row) - --variables - extension_uuid = row.extension_uuid; - domain_uuid = row.domain_uuid; - sip_from_user = row.extension; - call_group = row.call_group; - hold_music = row.hold_music; - toll_allow = row.toll_allow; - accountcode = row.accountcode; - user_context = row.user_context; - effective_caller_id_name = row.effective_caller_id_name; - effective_caller_id_number = row.effective_caller_id_number; - outbound_caller_id_name = row.outbound_caller_id_name; - outbound_caller_id_number = row.outbound_caller_id_number; - emergency_caller_id_number = row.emergency_caller_id_number; - directory_full_name = row.directory_full_name; - directory_visible = row.directory_visible; - directory_exten_visible = row.directory_exten_visible; - limit_max = row.limit_max; - limit_destination = row.limit_destination; - sip_force_contact = row.sip_force_contact; - sip_force_expires = row.sip_force_expires; - nibble_account = row.nibble_account; - sip_bypass_media = row.sip_bypass_media; - end); --set the xml array and then concatenate the array to a string - if (password) then - local xml = {} - table.insert(xml, [[]]); - table.insert(xml, [[]]); - table.insert(xml, [[
]]); - table.insert(xml, [[ ]]); - if (number_alias) then - if (cidr) then - table.insert(xml, [[ ]]); + if (continue and password) then + --build the xml + local xml = {} + table.insert(xml, [[]]); + table.insert(xml, [[]]); + table.insert(xml, [[
]]); + table.insert(xml, [[ ]]); + if (number_alias) then + if (cidr) then + table.insert(xml, [[ ]]); + else + table.insert(xml, [[ ]]); + end else - table.insert(xml, [[ ]]); + if (cidr) then + table.insert(xml, [[ ]]); + else + table.insert(xml, [[ ]]); + end end - else - if (cidr) then - table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + if (string.len(vm_mailto) > 0) then + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + end + if (string.len(mwi_account) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(auth_acl) > 0) then + table.insert(xml, [[ ]]); + end + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + if (user_context ~= "default" and user_context ~= "public" and user_context ~= "features") then + table.insert(xml, [[ ]]); + end + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + if (string.len(call_group) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(hold_music) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(toll_allow) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(accountcode) > 0) then + table.insert(xml, [[ ]]); + end + table.insert(xml, [[ ]]); + if (string.len(effective_caller_id_name) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(effective_caller_id_number) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(outbound_caller_id_name) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(outbound_caller_id_number) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(emergency_caller_id_number) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(directory_full_name) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(directory_visible) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(directory_exten_visible) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(limit_max) > 0) then + table.insert(xml, [[ ]]); else - table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); end - end - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - if (string.len(vm_mailto) > 0) then - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - end - if (string.len(mwi_account) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(auth_acl) > 0) then - table.insert(xml, [[ ]]); - end - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - if (user_context ~= "default" and user_context ~= "public" and user_context ~= "features") then - table.insert(xml, [[ ]]); - end - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - if (string.len(call_group) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(hold_music) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(toll_allow) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(accountcode) > 0) then - table.insert(xml, [[ ]]); - end - table.insert(xml, [[ ]]); - if (string.len(effective_caller_id_name) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(effective_caller_id_number) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(outbound_caller_id_name) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(outbound_caller_id_number) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(emergency_caller_id_number) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(directory_full_name) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(directory_visible) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(directory_exten_visible) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(limit_max) > 0) then - table.insert(xml, [[ ]]); - else - table.insert(xml, [[ ]]); - end - if (string.len(limit_destination) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(sip_force_contact) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(sip_force_expires) > 0) then - table.insert(xml, [[ ]]); - end - if (string.len(nibble_account) > 0) then - table.insert(xml, [[ ]]); - end - if (sip_bypass_media == "bypass-media") then - table.insert(xml, [[ ]]); - end - if (sip_bypass_media == "bypass-media-after-bridge") then - table.insert(xml, [[ ]]); - end - if (sip_bypass_media == "proxy-media") then - table.insert(xml, [[ ]]); - end - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - table.insert(xml, [[
]]); - table.insert(xml, [[
]]); - XML_STRING = table.concat(xml, "\n"); - else - XML_STRING = ""; - end + if (string.len(limit_destination) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(sip_force_contact) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(sip_force_expires) > 0) then + table.insert(xml, [[ ]]); + end + if (string.len(nibble_account) > 0) then + table.insert(xml, [[ ]]); + end + if (sip_bypass_media == "bypass-media") then + table.insert(xml, [[ ]]); + end + if (sip_bypass_media == "bypass-media-after-bridge") then + table.insert(xml, [[ ]]); + end + if (sip_bypass_media == "proxy-media") then + table.insert(xml, [[ ]]); + end + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[
]]); + table.insert(xml, [[
]]); + table.insert(xml, [[
]]); + table.insert(xml, [[
]]); + XML_STRING = table.concat(xml, "\n"); - --send the xml to the console - if (debug["xml_string"]) then - local file = assert(io.open("/tmp/directory.xml", "w")); - file:write(XML_STRING); - file:close(); + --set the cache + result = trim(api:execute("memcache", "set " .. user .. "@" .. domain_name .. " '"..XML_STRING:gsub("'", "'").."' "..expire["directory"])); + + --send the xml to the console + if (debug["xml_string"]) then + local file = assert(io.open("/tmp/directory.xml", "w")); + file:write(XML_STRING); + file:close(); + end + + --send to the console + if (debug["cache"]) then + freeswitch.consoleLog("notice", "[xml_handler] directory: " .. user .. "@" .. domain_name .. " source: database\n"); + end + else + --send to the console + if (debug["cache"]) then + if (XML_STRING) then + freeswitch.consoleLog("notice", "[xml_handler] directory: " .. user .. "@" .. domain_name .. " source: memcache \n"); + end + end end --send the xml to the console @@ -328,213 +716,236 @@ if (debug["params"]) then freeswitch.consoleLog("notice", "[xml_handler] Params:\n" .. params:serialize() .. "\n"); end - --set the xml array and then concatenate the array to a string - local xml = {} - table.insert(xml, [[]]); - table.insert(xml, [[]]); - table.insert(xml, [[
]]); - table.insert(xml, [[ ]]); - --set defaults - previous_dialplan_uuid = ""; - previous_dialplan_detail_group = ""; - previous_dialplan_detail_tag = ""; - previous_dialplan_detail_type = ""; - previous_dialplan_detail_data = ""; - dialplan_tag_status = "closed"; - condition_tag_status = "closed"; + --get the cache + if (trim(api:execute("module_exists", "mod_memcache")) == "true") then + XML_STRING = trim(api:execute("memcache", "get " .. call_context)); + else + XML_STRING = "-ERR NOT FOUND"; + end - --get the dialplan and related details - sql = "select * from v_dialplans as d, v_dialplan_details as s "; - sql = sql .. "where d.dialplan_context = '" .. call_context .. "' "; - sql = sql .. "and d.dialplan_enabled = 'true' "; - sql = sql .. "and d.dialplan_uuid = s.dialplan_uuid "; - sql = sql .. "order by "; - sql = sql .. "d.dialplan_order asc, "; - sql = sql .. "d.dialplan_name asc, "; - sql = sql .. "s.dialplan_detail_group asc, "; - sql = sql .. "CASE s.dialplan_detail_tag "; - sql = sql .. "WHEN 'condition' THEN 1 "; - sql = sql .. "WHEN 'action' THEN 2 "; - sql = sql .. "WHEN 'anti-action' THEN 3 "; - sql = sql .. "ELSE 100 END, "; - sql = sql .. "s.dialplan_detail_order asc "; - if (debug["sql"]) then - freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); - end - x = 0; - dbh:query(sql, function(row) - --get the dialplan - --domain_uuid = row.domain_uuid; - dialplan_uuid = row.dialplan_uuid; - --app_uuid = row.app_uuid; - --dialplan_context = row.dialplan_context; - dialplan_name = row.dialplan_name; - --dialplan_number = row.dialplan_number; - dialplan_continue = row.dialplan_continue; - --dialplan_order = row.dialplan_order; - --dialplan_enabled = row.dialplan_enabled; - --dialplan_description = row.dialplan_description; - --get the dialplan details - --dialplan_detail_uuid = row.dialplan_detail_uuid; - dialplan_detail_tag = row.dialplan_detail_tag; - dialplan_detail_type = row.dialplan_detail_type; - dialplan_detail_data = row.dialplan_detail_data; - dialplan_detail_break = row.dialplan_detail_break; - dialplan_detail_inline = row.dialplan_detail_inline; - dialplan_detail_group = row.dialplan_detail_group; - --dialplan_detail_order = row.dialplan_detail_order; + if (XML_STRING == "-ERR NOT FOUND") then + --set the xml array and then concatenate the array to a string + local xml = {} + table.insert(xml, [[]]); + table.insert(xml, [[]]); + table.insert(xml, [[
]]); + table.insert(xml, [[ ]]); - --remove $$ and replace with $ - dialplan_detail_data = dialplan_detail_data:gsub("%$%$", "$"); + --set defaults + previous_dialplan_uuid = ""; + previous_dialplan_detail_group = ""; + previous_dialplan_detail_tag = ""; + previous_dialplan_detail_type = ""; + previous_dialplan_detail_data = ""; + dialplan_tag_status = "closed"; + condition_tag_status = "closed"; - --get the dialplan detail inline - detail_inline = ""; - if (dialplan_detail_inline) then - if (string.len(dialplan_detail_inline) > 0) then - detail_inline = [[ inline="]] .. dialplan_detail_inline .. [["]]; + --get the dialplan and related details + sql = "select * from v_dialplans as d, v_dialplan_details as s "; + sql = sql .. "where d.dialplan_context = '" .. call_context .. "' "; + sql = sql .. "and d.dialplan_enabled = 'true' "; + sql = sql .. "and d.dialplan_uuid = s.dialplan_uuid "; + sql = sql .. "order by "; + sql = sql .. "d.dialplan_order asc, "; + sql = sql .. "d.dialplan_name asc, "; + sql = sql .. "s.dialplan_detail_group asc, "; + sql = sql .. "CASE s.dialplan_detail_tag "; + sql = sql .. "WHEN 'condition' THEN 1 "; + sql = sql .. "WHEN 'action' THEN 2 "; + sql = sql .. "WHEN 'anti-action' THEN 3 "; + sql = sql .. "ELSE 100 END, "; + sql = sql .. "s.dialplan_detail_order asc "; + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); + end + x = 0; + dbh:query(sql, function(row) + --get the dialplan + --domain_uuid = row.domain_uuid; + dialplan_uuid = row.dialplan_uuid; + --app_uuid = row.app_uuid; + --dialplan_context = row.dialplan_context; + dialplan_name = row.dialplan_name; + --dialplan_number = row.dialplan_number; + dialplan_continue = row.dialplan_continue; + --dialplan_order = row.dialplan_order; + --dialplan_enabled = row.dialplan_enabled; + --dialplan_description = row.dialplan_description; + --get the dialplan details + --dialplan_detail_uuid = row.dialplan_detail_uuid; + dialplan_detail_tag = row.dialplan_detail_tag; + dialplan_detail_type = row.dialplan_detail_type; + dialplan_detail_data = row.dialplan_detail_data; + dialplan_detail_break = row.dialplan_detail_break; + dialplan_detail_inline = row.dialplan_detail_inline; + dialplan_detail_group = row.dialplan_detail_group; + --dialplan_detail_order = row.dialplan_detail_order; + + --remove $$ and replace with $ + dialplan_detail_data = dialplan_detail_data:gsub("%$%$", "$"); + + --get the dialplan detail inline + detail_inline = ""; + if (dialplan_detail_inline) then + if (string.len(dialplan_detail_inline) > 0) then + detail_inline = [[ inline="]] .. dialplan_detail_inline .. [["]]; + end end - end - --close the tags - if (condition_tag_status ~= "closed") then - if (previous_dialplan_uuid ~= dialplan_uuid) then - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - dialplan_tag_status = "closed"; - condition_tag_status = "closed"; - else - if (previous_dialplan_detail_group ~= dialplan_detail_group and previous_dialplan_detail_tag == "condition") then - table.insert(xml, [[ ]]); + --close the tags + if (condition_tag_status ~= "closed") then + if (previous_dialplan_uuid ~= dialplan_uuid) then + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + dialplan_tag_status = "closed"; condition_tag_status = "closed"; - end - end - end - - --open the tags - if (dialplan_tag_status == "closed") then - table.insert(xml, [[ ]]); - dialplan_tag_status = "open"; - end - if (dialplan_detail_tag == "condition") then - --determine the type of condition - if (dialplan_detail_type == "hour") then - condition_type = 'time'; - elseif (dialplan_detail_type == "minute") then - condition_type = 'time'; - elseif (dialplan_detail_type == "minute-of-day") then - condition_type = 'time'; - elseif (dialplan_detail_type == "mday") then - condition_type = 'time'; - elseif (dialplan_detail_type == "mweek") then - condition_type = 'time'; - elseif (dialplan_detail_type == "mon") then - condition_type = 'time'; - elseif (dialplan_detail_type == "yday") then - condition_type = 'time'; - elseif (dialplan_detail_type == "year") then - condition_type = 'time'; - elseif (dialplan_detail_type == "wday") then - condition_type = 'time'; - elseif (dialplan_detail_type == "week") then - condition_type = 'time'; else - condition_type = 'default'; - end - - --get the condition break attribute - condition_break = ""; - if (dialplan_detail_break) then - if (string.len(dialplan_detail_break) > 0) then - condition_break = [[ break="]] .. dialplan_detail_break .. [["]]; + if (previous_dialplan_detail_group ~= dialplan_detail_group and previous_dialplan_detail_tag == "condition") then + table.insert(xml, [[ ]]); + condition_tag_status = "closed"; end end + end - if (condition_tag_status == "open") then - if (previous_dialplan_detail_tag == "condition") then - --add the condition self closing tag - if (condition) then - if (string.len(condition) > 0) then - table.insert(xml, condition .. [[/>]]); + --open the tags + if (dialplan_tag_status == "closed") then + table.insert(xml, [[ ]]); + dialplan_tag_status = "open"; + end + if (dialplan_detail_tag == "condition") then + --determine the type of condition + if (dialplan_detail_type == "hour") then + condition_type = 'time'; + elseif (dialplan_detail_type == "minute") then + condition_type = 'time'; + elseif (dialplan_detail_type == "minute-of-day") then + condition_type = 'time'; + elseif (dialplan_detail_type == "mday") then + condition_type = 'time'; + elseif (dialplan_detail_type == "mweek") then + condition_type = 'time'; + elseif (dialplan_detail_type == "mon") then + condition_type = 'time'; + elseif (dialplan_detail_type == "yday") then + condition_type = 'time'; + elseif (dialplan_detail_type == "year") then + condition_type = 'time'; + elseif (dialplan_detail_type == "wday") then + condition_type = 'time'; + elseif (dialplan_detail_type == "week") then + condition_type = 'time'; + else + condition_type = 'default'; + end + + --get the condition break attribute + condition_break = ""; + if (dialplan_detail_break) then + if (string.len(dialplan_detail_break) > 0) then + condition_break = [[ break="]] .. dialplan_detail_break .. [["]]; end end - end - if (previous_dialplan_detail_tag == "action" or previous_dialplan_detail_tag == "anti-action") then - table.insert(xml, [[ ]]); - condition_tag_status = "closed"; - condition_type = ""; - condition_attribute = ""; - condition_expression = ""; - end - end - --condition tag but leave off the ending - if (condition_type == "default") then - condition = [[ 0) then + table.insert(xml, condition .. [[/>]]); + end + end + end + if (previous_dialplan_detail_tag == "action" or previous_dialplan_detail_tag == "anti-action") then + table.insert(xml, [[ ]]); + condition_tag_status = "closed"; + condition_type = ""; + condition_attribute = ""; + condition_expression = ""; end end - table.insert(xml, condition .. [[>]]); - condition = ""; --prevents duplicate time conditions + + --condition tag but leave off the ending + if (condition_type == "default") then + condition = [[ ]]); + condition = ""; --prevents duplicate time conditions + end + end + if (dialplan_detail_tag == "action") then + table.insert(xml, [[ ]]); + end + if (dialplan_detail_tag == "anti-action") then + table.insert(xml, [[ ]]); end - end - if (dialplan_detail_tag == "action") then - table.insert(xml, [[ ]]); - end - if (dialplan_detail_tag == "anti-action") then - table.insert(xml, [[ ]]); - end - --save the previous values - previous_dialplan_uuid = dialplan_uuid; - previous_dialplan_detail_group = dialplan_detail_group; - previous_dialplan_detail_tag = dialplan_detail_tag; - previous_dialplan_detail_type = dialplan_detail_type; - previous_dialplan_detail_data = dialplan_detail_data; + --save the previous values + previous_dialplan_uuid = dialplan_uuid; + previous_dialplan_detail_group = dialplan_detail_group; + previous_dialplan_detail_tag = dialplan_detail_tag; + previous_dialplan_detail_type = dialplan_detail_type; + previous_dialplan_detail_data = dialplan_detail_data; - --increment the x - x = x + 1; - end); + --increment the x + x = x + 1; + end); - --close the extension tag if it was left open - if (dialplan_tag_status == "open") then - table.insert(xml, [[ ]]); - table.insert(xml, [[ ]]); - end + --close the extension tag if it was left open + if (dialplan_tag_status == "open") then + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + end - --set the xml array and then concatenate the array to a string - table.insert(xml, [[ ]]); - table.insert(xml, [[
]]); - table.insert(xml, [[
]]); - XML_STRING = table.concat(xml, "\n"); + --set the xml array and then concatenate the array to a string + table.insert(xml, [[
]]); + table.insert(xml, [[
]]); + table.insert(xml, [[
]]); + XML_STRING = table.concat(xml, "\n"); - --send the xml to the console - if (debug["xml_string"]) then - local file = assert(io.open("/tmp/dialplan-" .. call_context .. ".xml", "w")); - file:write(XML_STRING); - file:close(); - end + --set the cache + result = trim(api:execute("memcache", "set " .. call_context .. " '"..XML_STRING:gsub("'", "'").."' "..expire["dialplan"])); + + --send the xml to the console + if (debug["xml_string"]) then + local file = assert(io.open("/tmp/dialplan-" .. call_context .. ".xml", "w")); + file:write(XML_STRING); + file:close(); + end + + --send to the console + if (debug["cache"]) then + freeswitch.consoleLog("notice", "[xml_handler] dialplan: "..call_context.." source: database\n"); + end + else + --send to the console + if (debug["cache"]) then + freeswitch.consoleLog("notice", "[xml_handler] dialplan: "..call_context.." source: memcache\n"); + end + end end --send debug info to the console