2014-08-09 05:25:42 +02: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>
|
|
|
|
|
-- Copyright (C) 2010-2014
|
|
|
|
|
-- the Initial Developer. All Rights Reserved.
|
|
|
|
|
--
|
|
|
|
|
-- Contributor(s):
|
|
|
|
|
-- Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
|
2014-11-04 16:29:05 +01:00
|
|
|
-- Riccardo Granchi <riccardo.granchi@nems.it>
|
|
|
|
|
--
|
|
|
|
|
-- add this in Inbound Routes before transfer to use it:
|
|
|
|
|
-- action set caller_id_name=${luarun cidlookup.lua ${uuid}}
|
|
|
|
|
|
2014-08-09 05:25:42 +02:00
|
|
|
--debug
|
|
|
|
|
debug["sql"] = true;
|
|
|
|
|
|
|
|
|
|
--define the trim function
|
|
|
|
|
function trim (s)
|
|
|
|
|
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--define the explode function
|
|
|
|
|
function explode ( seperator, str )
|
|
|
|
|
local pos, arr = 0, {}
|
|
|
|
|
for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found
|
|
|
|
|
table.insert( arr, string.sub( str, pos, st-1 ) ) -- attach chars left of current divider
|
|
|
|
|
pos = sp + 1 -- jump past current divider
|
|
|
|
|
end
|
|
|
|
|
table.insert( arr, string.sub( str, pos ) ) -- attach chars right of last divider
|
|
|
|
|
return arr
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--create the api object
|
|
|
|
|
api = freeswitch.API();
|
|
|
|
|
uuid = argv[1];
|
|
|
|
|
if not uuid or uuid == "" then return end;
|
|
|
|
|
caller = api:executeString("uuid_getvar " .. uuid .. " caller_id_number");
|
|
|
|
|
callee = api:executeString("uuid_getvar " .. uuid .. " destination_number");
|
|
|
|
|
|
2014-11-04 16:29:05 +01:00
|
|
|
--clean local country prefix from caller (ex: +39 or 0039 in Italy)
|
|
|
|
|
exitCode = api:executeString("uuid_getvar " .. uuid .. " default_exitcode");
|
|
|
|
|
countryCode = api:executeString("uuid_getvar " .. uuid .. " default_countrycode");
|
|
|
|
|
|
|
|
|
|
if ((countryCode ~= nil) and (string.len(countryCode) > 0)) then
|
|
|
|
|
|
|
|
|
|
countryPrefix = "+" .. countryCode;
|
|
|
|
|
|
|
|
|
|
if (string.sub(caller, 1, string.len(countryPrefix)) == countryPrefix) then
|
|
|
|
|
cleanCaller = string.sub(caller, string.len(countryPrefix)+1);
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[cidlookup] ignoring local international prefix " .. countryPrefix .. ": " .. caller .. " ==> " .. cleanCaller .. "\n");
|
|
|
|
|
caller = cleanCaller;
|
|
|
|
|
else
|
|
|
|
|
if ((exitCode ~= nil) and (string.len(exitCode) > 0)) then
|
|
|
|
|
|
|
|
|
|
countryPrefix = exitCode .. countryCode;
|
|
|
|
|
|
|
|
|
|
if (string.sub(caller, 1, string.len(countryPrefix)) == countryPrefix) then
|
|
|
|
|
cleanCaller = string.sub(caller, string.len(countryPrefix)+1);
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[cidlookup] ignoring local international prefix " .. countryPrefix .. ": " .. caller .. " ==> " .. cleanCaller .. "\n");
|
|
|
|
|
caller = cleanCaller;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2014-08-09 05:25:42 +02:00
|
|
|
--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());
|
|
|
|
|
|
|
|
|
|
--check if the session is ready
|
|
|
|
|
|
|
|
|
|
--connect to the database
|
|
|
|
|
dofile(scripts_dir.."/resources/functions/database_handle.lua");
|
|
|
|
|
dbh = database_handle('system');
|
|
|
|
|
|
2014-11-04 16:29:05 +01:00
|
|
|
if (database["type"] == "mysql") then
|
|
|
|
|
sql = "SELECT CONCAT(v_contacts.contact_name_given, ' ', v_contacts.contact_name_family,' (',v_contact_phones.phone_type,')') AS name FROM v_contacts ";
|
|
|
|
|
else
|
|
|
|
|
sql = "SELECT v_contacts.contact_name_given || ' ' || v_contacts.contact_name_family || ' (' || v_contact_phones.phone_type || ')' AS name FROM v_contacts ";
|
|
|
|
|
end
|
|
|
|
|
|
2014-08-09 05:25:42 +02:00
|
|
|
sql = sql .. "INNER JOIN v_contact_phones ON v_contact_phones.contact_uuid = v_contacts.contact_uuid ";
|
|
|
|
|
sql = sql .. "INNER JOIN v_destinations ON v_destinations.domain_uuid = v_contacts.domain_uuid ";
|
|
|
|
|
sql = sql .. "WHERE v_contact_phones.phone_number = '"..caller.."' AND v_destinations.destination_number='"..callee.."'";
|
|
|
|
|
|
|
|
|
|
if (debug["sql"]) then
|
2014-08-11 19:48:46 +02:00
|
|
|
freeswitch.consoleLog("notice", "[cidlookup] "..sql.."\n");
|
2014-08-09 05:25:42 +02:00
|
|
|
end
|
|
|
|
|
status = dbh:query(sql, function(row)
|
|
|
|
|
name = row.name;
|
|
|
|
|
end);
|
|
|
|
|
|
2014-08-11 19:48:46 +02:00
|
|
|
if (name == nil) then
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[cidlookup] caller name from contacts db is nil\n");
|
|
|
|
|
else
|
2014-11-04 16:29:05 +01:00
|
|
|
freeswitch.consoleLog("NOTICE", "[cidlookup] caller name from contacts db: "..name.."\n");
|
2014-08-11 19:48:46 +02:00
|
|
|
end
|
|
|
|
|
|
2014-08-09 05:25:42 +02:00
|
|
|
--check if there is a record, if it doesnt, then use common cidlookup
|
2014-08-11 19:48:46 +02:00
|
|
|
if ((name == nil) or (string.len(name) == 0)) then
|
2014-11-04 16:29:05 +01:00
|
|
|
name = api:executeString("cidlookup " .. caller);
|
2014-08-11 19:48:46 +02:00
|
|
|
end
|
2014-08-09 05:25:42 +02:00
|
|
|
|
2014-11-04 16:29:05 +01:00
|
|
|
freeswitch.consoleLog("NOTICE", "[cidlookup] uuid_setvar " .. uuid .. " caller_id_name " .. name);
|
2014-08-11 19:48:46 +02:00
|
|
|
api:executeString("uuid_setvar " .. uuid .. " caller_id_name " .. name);
|
2014-11-04 16:29:05 +01:00
|
|
|
|
|
|
|
|
freeswitch.consoleLog("NOTICE", "[cidlookup] uuid_setvar " .. uuid .. " effective_caller_id_name " .. name);
|
|
|
|
|
api:executeString("uuid_setvar " .. uuid .. " effective_caller_id_name " .. name);
|