2014-08-09 03:25:42 +00: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>
2016-11-06 00:15:59 -06:00
-- Copyright (C) 2010-2016
2014-08-09 03:25:42 +00:00
-- the Initial Developer. All Rights Reserved.
--
-- Contributor(s):
-- Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
2014-11-04 15:29:05 +00:00
-- Riccardo Granchi <riccardo.granchi@nems.it>
2019-05-18 12:21:33 +01:00
-- Adrian Fretwell <adrian.fretwell@topgreen.co.uk>
2014-11-04 15:29:05 +00:00
--
2016-03-11 13:21:52 +00:00
-- add this in Inbound Routes before transfer to use it:
2019-05-18 12:21:33 +01:00
-- action set caller_id_name=${luarun cidlookup.lua ${uuid} ${domain_uuid}}
2014-11-04 15:29:05 +00:00
2014-08-09 03:25:42 +00:00
--define the trim function
2015-08-11 05:06:33 +03:00
require "resources.functions.trim"
2014-08-09 03:25:42 +00:00
--define the explode function
2015-08-11 05:06:33 +03:00
require "resources.functions.explode"
2014-08-09 03:25:42 +00:00
--create the api object
api = freeswitch.API ();
uuid = argv [ 1 ];
2019-05-18 12:21:33 +01:00
domain_uuid = argv [ 2 ];
2014-08-09 03:25:42 +00:00
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 15:29:05 +00: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
2016-03-11 13:21:52 +00:00
2014-11-04 15:29:05 +00:00
countryPrefix = "+" .. countryCode ;
2016-03-11 13:21:52 +00:00
2014-11-04 15:29:05 +00:00
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
2016-03-11 13:21:52 +00:00
2014-11-04 15:29:05 +00:00
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 ;
2016-03-11 13:21:52 +00:00
end ;
2014-11-04 15:29:05 +00:00
end ;
2014-08-09 03:25:42 +00:00
--include config.lua
2015-08-11 05:06:33 +03:00
require "resources.functions.config" ;
2014-08-09 03:25:42 +00:00
2016-11-18 20:16:22 +03:00
--include json library
local json
if ( debug [ "sql" ]) then
json = require "resources.functions.lunajson"
end
2016-11-06 00:15:59 -06:00
--connect to the database
2016-11-18 20:16:22 +03:00
local Database = require "resources.functions.database" ;
dbh = Database.new ( 'system' );
2016-11-06 00:15:59 -06:00
if ( database [ "type" ] == "mysql" ) then
2019-09-20 12:38:15 -04:00
sql = "SELECT CONCAT(v_contacts.contact_name_given, ' ', v_contacts.contact_name_family) AS name FROM v_contacts " ;
2016-11-06 00:15:59 -06:00
elseif ( database [ "type" ] == "pgsql" ) then
2019-09-20 12:38:15 -04:00
sql = "SELECT CASE WHEN contact_name_given = '' THEN v_contacts.contact_organization ELSE v_contacts.contact_name_given || ' ' || v_contacts.contact_name_family END AS name FROM v_contacts " ;
2016-11-06 00:15:59 -06:00
else
2019-09-20 12:38:15 -04:00
sql = "SELECT v_contacts.contact_name_given || ' ' || v_contacts.contact_name_family AS name FROM v_contacts " ;
2016-11-06 00:15:59 -06:00
end
sql = sql .. "INNER JOIN v_contact_phones ON v_contact_phones.contact_uuid = v_contacts.contact_uuid " ;
2020-01-29 04:33:10 +00:00
sql = sql .. "INNER JOIN v_destinations ON v_destinations.domain_uuid = v_contacts.domain_uuid AND v_destinations.destination_number = :callee " ;
2019-05-18 12:21:33 +01:00
local params ;
if (( not domain_uuid ) or ( domain_uuid == "" )) then
sql = sql .. "WHERE v_contact_phones.phone_number = :caller " ;
2020-01-29 04:33:10 +00:00
params = { caller = caller , callee = callee };
2019-05-18 12:21:33 +01:00
else
sql = sql .. "WHERE v_contacts.domain_uuid = :domain_uuid and v_contact_phones.phone_number = :caller " ;
2020-01-29 04:33:10 +00:00
params = { caller = caller , domain_uuid = domain_uuid , callee = callee };
2019-05-18 12:21:33 +01:00
end
2016-11-06 00:15:59 -06:00
if ( debug [ "sql" ]) then
2016-11-18 20:16:22 +03:00
freeswitch.consoleLog ( "notice" , "[cidlookup] SQL: " .. sql .. "; params:" .. json.encode ( params ) .. " \n " );
2016-11-06 00:15:59 -06:00
end
2016-11-18 20:16:22 +03:00
dbh : query ( sql , params , function ( row )
2016-11-06 00:15:59 -06:00
name = row.name ;
end );
if ( name == nil ) then
freeswitch.consoleLog ( "NOTICE" , "[cidlookup] caller name from contacts db is nil \n " );
else
freeswitch.consoleLog ( "NOTICE" , "[cidlookup] caller name from contacts db: " .. name .. " \n " );
end
--check if there is a record, if it not then use cidlookup
if (( name == nil ) or ( string.len ( name ) == 0 )) then
2019-09-20 12:38:15 -04:00
cidlookup_exists = api : executeString ( "module_exists mod_cidlookup" );
2016-11-06 00:15:59 -06:00
if ( cidlookup_exists == "true" ) then
name = api : executeString ( "cidlookup " .. caller );
end
end
--set the caller id name
if (( name ~= nil ) and ( string.len ( name ) > 0 )) then
2019-09-20 12:38:15 -04:00
api : executeString ( "uuid_setvar " .. uuid .. " ignore_display_updates false" );
2021-11-22 11:08:03 -05:00
freeswitch.consoleLog ( "NOTICE" , "[cidlookup] uuid_setvar " .. uuid .. " caller_id_name " .. name .. " \n " );
2016-11-06 00:15:59 -06:00
api : executeString ( "uuid_setvar " .. uuid .. " caller_id_name " .. name );
2021-11-22 11:08:03 -05:00
freeswitch.consoleLog ( "NOTICE" , "[cidlookup] uuid_setvar " .. uuid .. " effective_caller_id_name " .. name .. " \n " );
2016-11-06 00:15:59 -06:00
api : executeString ( "uuid_setvar " .. uuid .. " effective_caller_id_name " .. name );
end