fusionpbx/resources/install/scripts/app/speed_dial/index.lua

97 lines
2.9 KiB
Lua
Raw Normal View History

2016-12-08 11:03:58 +01:00
-- FusionPBX
-- Version: MPL 1.1
-- The contents of this file are subject to the Mozilla Public License Version
-- 1.1 (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
-- http://www.mozilla.org/MPL/
-- Software distributed under the License is distributed on an "AS IS" basis,
-- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-- for the specific language governing rights and limitations under the
-- License.
-- The Original Code is FusionPBX
-- The Initial Developer of the Original Code is
-- Mark J Crane <markjcrane@fusionpbx.com>
-- Portions created by the Initial Developer are Copyright (C) 2016
-- the Initial Developer. All Rights Reserved.
2016-12-08 18:23:38 +01:00
-- load config
require "resources.functions.config";
2016-12-08 11:03:58 +01:00
--set debug
--debug["sql"] = false;
--get the variables
domain_name = session:getVariable("domain_name");
domain_uuid = session:getVariable("domain_uuid");
destination_number = session:getVariable("destination_number");
context = session:getVariable("context");
2016-12-08 18:23:38 +01:00
--load libraries
local log = require "resources.functions.log"["app:dialplan:outbound:speed_dial"]
2016-12-08 11:03:58 +01:00
local Database = require "resources.functions.database";
2016-12-08 18:23:38 +01:00
local cache = require "resources.functions.cache";
local json = require "resources.functions.lunajson";
-- search in memcache first
local key = "app:dialplan:outbound:speed_dial:" .. destination_number .. "@" .. context
local source = "memcache"
local value = cache.get(key)
-- decode value from memcache
if value then
local t = json.decode(value)
if not (t and t.phone_number and t.context) then
log.warning("can not decode value from memcache: %s", value)
value = nil
else
value = t
end
2016-12-08 11:03:58 +01:00
end
2016-12-08 18:23:38 +01:00
-- search in database
if not value then
-- set source flag
source = "database"
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
-- connect to database
local dbh = Database.new('system');
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
-- search real phone number in database
local sql = "SELECT phone_number "
sql = sql .. "FROM v_contact_phones "
sql = sql .. "WHERE phone_speed_dial = :phone_speed_dial "
sql = sql .. "AND domain_uuid = :domain_uuid "
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
local params = {phone_speed_dial = destination_number, domain_uuid = domain_uuid};
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
if (debug["sql"]) then
log.noticef("SQL: %s; params: %s", sql, json.encode(params));
end
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
local phone_number = dbh:first_value(sql, params)
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
-- release database connection
dbh:release()
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
-- set the cache
if phone_number then
value = {context = context, phone_number = phone_number}
cache.set(key, json.encode(value), expire["speed_dial"])
2016-12-08 11:03:58 +01:00
end
2016-12-08 18:23:38 +01:00
end
2016-12-08 11:03:58 +01:00
2016-12-08 18:23:38 +01:00
-- transfer
if value then
--log the result
log.noticef("%s XML %s source: %s", destination_number, value.context, source)
2016-12-08 11:03:58 +01:00
--transfer the call
2016-12-08 18:23:38 +01:00
session:transfer(value.phone_number, "XML", value.context);
else
log.warningf('can not find number: %s in domain: %s', destination_number, domain_name)
2016-12-08 11:03:58 +01:00
end