From 178f475e643ff1a7fa6f55fa51a9175d9bf9f994 Mon Sep 17 00:00:00 2001 From: Mark Crane Date: Sun, 1 Dec 2013 03:46:04 +0000 Subject: [PATCH] Add scripts/app/dialplan accessible with action: lua data: app.lua dialplan --- .../install/scripts/app/dialplan/index.lua | 51 +++++++++ .../resources/inbound/000_get_domain.lua | 102 ++++++++++++++++++ .../resources/outbound/100_is_local.lua | 91 ++++++++++++++++ 3 files changed, 244 insertions(+) create mode 100644 resources/install/scripts/app/dialplan/index.lua create mode 100644 resources/install/scripts/app/dialplan/resources/inbound/000_get_domain.lua create mode 100644 resources/install/scripts/app/dialplan/resources/outbound/100_is_local.lua diff --git a/resources/install/scripts/app/dialplan/index.lua b/resources/install/scripts/app/dialplan/index.lua new file mode 100644 index 0000000000..899b786892 --- /dev/null +++ b/resources/install/scripts/app/dialplan/index.lua @@ -0,0 +1,51 @@ + + +--include config.lua + --scripts_dir = string.sub(debug.getinfo(1).source,2,string.len(debug.getinfo(1).source)-(string.len(argv[0])+1)); + --dofile(scripts_dir.."/resources/functions/config.lua"); + --dofile(config()); + +--add functions + dofile(scripts_dir.."/resources/functions/file_exists.lua"); + dofile(scripts_dir.."/resources/functions/trim.lua"); + +--set the api object + api = freeswitch.API(); + +--windows (/ad show only directories) + --dir "C:\program files\fusionpbx" /b +--unix + -- dir /usr/local/freeswitch/scripts -1 + + +--set local variables + local context = session:getVariable("context"); + local destination_number = session:getVariable("destination_number"); + +--determine the call direction + if (context == "public") then + call_direction = "inbound"; + else + if (string.len(destination_number) > 6) then + call_direction = "outbound"; + else + call_direction = "local"; + end + end + +--set the call direction as a session variable + session:setVariable("call_direction", call_direction); + --freeswitch.consoleLog("notice", "[app:dialplan] set call_direction " .. call_direction .. "\n"); + +--include the dialplans + result = assert (io.popen ("dir " ..scripts_dir.."/app/dialplan/resources/"..call_direction.." /b -1")); + for file in result:lines() do + if (string.sub(file, -4) == ".lua") then + --order = string.match(file, "%d+"); + --if (order == nil) then order = file; end + if file_exists(scripts_dir.."/app/dialplan/resources/"..call_direction.."/"..file) then + dofile(scripts_dir.."/app/dialplan/resources/"..call_direction.."/"..file); + end + freeswitch.consoleLog("notice", "[app:dialplan] lua: " .. file .. "\n"); + end + end diff --git a/resources/install/scripts/app/dialplan/resources/inbound/000_get_domain.lua b/resources/install/scripts/app/dialplan/resources/inbound/000_get_domain.lua new file mode 100644 index 0000000000..67fab7a5f4 --- /dev/null +++ b/resources/install/scripts/app/dialplan/resources/inbound/000_get_domain.lua @@ -0,0 +1,102 @@ +-- ring_groups.lua +-- Part of FusionPBX +-- Copyright (C) 2010-2013 Mark J Crane +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions are met: +-- +-- 1. Redistributions of source code must retain the above copyright notice, +-- this list of conditions and the following disclaimer. +-- +-- 2. Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +-- POSSIBILITY OF SUCH DAMAGE. + +--set defaults + expire = {} + expire["get_domain"] = "3600"; + source = ""; + +--get the variables + local destination_number = session:getVariable("destination_number"); + +--connect to the database + dofile(scripts_dir.."/resources/functions/database_handle.lua"); + dbh = database_handle('system'); + +--get the cache + freeswitch.consoleLog("notice", "[app:dialplan:inbound:get_domain] memcache get app:dialplan:inbound:get_domain:" .. destination_number .. "\n"); + cache = trim(api:execute("memcache", "get app:dialplan:inbound:get_domain:" .. destination_number)); + +--get the ring group destinations + if (cache == "-ERR NOT FOUND") then + sql = "SELECT d.domain_uuid, d.domain_name, n.destination_number, n.destination_context " + sql = sql .. "FROM v_destinations as n, v_domains as d " + sql = sql .. "WHERE n.destination_number like '%"..destination_number.."' " + sql = sql .. "AND n.destination_type = 'inbound' " + sql = sql .. "AND n.destination_enabled = 'true' " + sql = sql .. "AND n.domain_uuid = d.domain_uuid " + --freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n"); + assert(dbh:query(sql, function(row) + --set the local variables + domain_uuid = row.domain_uuid; + domain_name = row.domain_name; + --local destination_number = row.destination_number; + --local destination_context = row.destination_context; + + --set the cache + cache = "domain_uuid=" .. domain_uuid .. "&domain_name=" .. domain_name; + result = trim(api:execute("memcache", "set app:dialplan:inbound:get_domain:" .. destination_number .. " '"..cache.."' "..expire["get_domain"])); + + --set the source + source = "database"; + end)); + + else + --add the function + dofile(scripts_dir.."/resources/functions/explode.lua"); + + --parse the cache + array = explode("&", cache); + + --define the array/table and variables + local var = {} + local key = ""; + local value = ""; + + --parse the cache + key_pairs = explode("&", cache); + for k,v in pairs(key_pairs) do + f = explode("=", v); + key = f[1]; + value = f[2]; + var[key] = value; + end + + --set the variables + domain_uuid = var["domain_uuid"]; + domain_name = var["domain_name"]; + + --set the source + source = "memcache"; + end + +--set the call direction as a session variable + session:setVariable("domain_name", domain_name); + session:setVariable("domain", domain_name); + session:setVariable("domain_uuid", domain_uuid); + +--send information to the console + freeswitch.consoleLog("notice", "[app:dialplan:inbound:get_domain] " .. cache .. " source: ".. source .."\n"); diff --git a/resources/install/scripts/app/dialplan/resources/outbound/100_is_local.lua b/resources/install/scripts/app/dialplan/resources/outbound/100_is_local.lua new file mode 100644 index 0000000000..37b2d97332 --- /dev/null +++ b/resources/install/scripts/app/dialplan/resources/outbound/100_is_local.lua @@ -0,0 +1,91 @@ +-- ring_groups.lua +-- Part of FusionPBX +-- Copyright (C) 2010-2013 Mark J Crane +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions are met: +-- +-- 1. Redistributions of source code must retain the above copyright notice, +-- this list of conditions and the following disclaimer. +-- +-- 2. Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +-- POSSIBILITY OF SUCH DAMAGE. + +--set defaults + expire = {} + expire["is_local"] = "3600"; + +--get the variables + domain_name = session:getVariable("domain_name"); + destination_number = session:getVariable("destination_number"); + +--connect to the database + dofile(scripts_dir.."/resources/functions/database_handle.lua"); + dbh = database_handle('system'); + +--get the cache + cache = trim(api:execute("memcache", "get app:dialplan:outbound:is_local:" .. destination_number .. "@" .. domain_name)); + +--get the ring group destinations + if (cache == "-ERR NOT FOUND") then + sql = "SELECT destination_number, destination_context " + sql = sql .. "FROM v_destinations " + sql = sql .. "WHERE destination_number like '%"..destination_number.."' " + sql = sql .. "AND destination_type = 'inbound' " + sql = sql .. "AND destination_enabled = 'true' " + --freeswitch.consoleLog("notice", "SQL:" .. sql .. "\n"); + assert(dbh:query(sql, function(row) + --set the local variables + destination_context = row.destination_context; + + --set the cache + if (destination_number == row.destination_number) then + result = trim(api:execute("memcache", "set app:dialplan:outbound:is_local:" .. destination_number .. "@" .. domain_name .. " 'destination_number=" .. row.destination_number .. "&destination_context=" .. destination_context .. "' "..expire["is_local"])); + else + result = trim(api:execute("memcache", "set app:dialplan:outbound:is_local:" .. destination_number .. "@" .. domain_name .. " 'destination_number=" .. row.destination_number .. "&destination_context=" .. destination_context .. "' "..expire["is_local"])); + result = trim(api:execute("memcache", "set app:dialplan:outbound:is_local:" .. row.destination_number .. "@" .. domain_name .. " 'destination_number=" .. row.destination_number .. "&destination_context=" .. destination_context .. "' "..expire["is_local"])); + end + + --log the result + freeswitch.consoleLog("notice", "[app:dialplan:outbound:is_local] " .. destination_number .. " XML " .. destination_context .. " source: database\n"); + + --transfer the call + session:transfer(row.destination_number, "XML", row.destination_context); + end)); + else + --add the function + dofile(scripts_dir.."/resources/functions/explode.lua"); + + --define the array/table and variables + local var = {} + local key = ""; + local value = ""; + + --parse the cache + key_pairs = explode("&", cache); + for k,v in pairs(key_pairs) do + f = explode("=", v); + key = f[1]; + value = f[2]; + var[key] = value; + end + + --send to the console + freeswitch.consoleLog("notice", "[app:dialplan:outbound:is_local] " .. cache .. " source: memcache\n"); + + --transfer the call + session:transfer(var["destination_number"], "XML", var["destination_context"]); + end \ No newline at end of file