1-- 2-- FusionPBX 3-- Version: MPL 1.1 4-- 5-- The contents of this file are subject to the Mozilla Public License Version 6-- 1.1 (the "License"); you may not use this file except in compliance with 7-- the License. You may obtain a copy of the License at 8-- http://www.mozilla.org/MPL/ 9-- 10-- Software distributed under the License is distributed on an "AS IS" basis, 11-- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12-- for the specific language governing rights and limitations under the 13-- License. 14-- 15-- The Original Code is FusionPBX 16-- 17-- The Initial Developer of the Original Code is 18-- Mark J Crane <markjcrane@fusionpbx.com> 19-- Copyright (C) 2010-2016 20-- the Initial Developer. All Rights Reserved. 21-- 22-- Contributor(s): 23-- Luis Daniel Lucio Quiroz <dlucio@okay.com.mx> 24-- Riccardo Granchi <riccardo.granchi@nems.it> 25-- 26-- add this in Inbound Routes before transfer to use it: 27-- action set caller_id_name=${luarun cidlookup.lua ${uuid}} 28 29--define the trim function 30 require "resources.functions.trim" 31 32--define the explode function 33 require "resources.functions.explode" 34 35--create the api object 36 api = freeswitch.API(); 37 uuid = argv[1]; 38 if not uuid or uuid == "" then return end; 39 caller = api:executeString("uuid_getvar " .. uuid .. " caller_id_number"); 40 callee = api:executeString("uuid_getvar " .. uuid .. " destination_number"); 41 42--clean local country prefix from caller (ex: +39 or 0039 in Italy) 43 exitCode = api:executeString("uuid_getvar " .. uuid .. " default_exitcode"); 44 countryCode = api:executeString("uuid_getvar " .. uuid .. " default_countrycode"); 45 46 if ((countryCode ~= nil) and (string.len(countryCode) > 0)) then 47 48 countryPrefix = "+" .. countryCode; 49 50 if (string.sub(caller, 1, string.len(countryPrefix)) == countryPrefix) then 51 cleanCaller = string.sub(caller, string.len(countryPrefix)+1); 52 freeswitch.consoleLog("NOTICE", "[cidlookup] ignoring local international prefix " .. countryPrefix .. ": " .. caller .. " ==> " .. cleanCaller .. "\n"); 53 caller = cleanCaller; 54 else 55 if ((exitCode ~= nil) and (string.len(exitCode) > 0)) then 56 57 countryPrefix = exitCode .. countryCode; 58 59 if (string.sub(caller, 1, string.len(countryPrefix)) == countryPrefix) then 60 cleanCaller = string.sub(caller, string.len(countryPrefix)+1); 61 freeswitch.consoleLog("NOTICE", "[cidlookup] ignoring local international prefix " .. countryPrefix .. ": " .. caller .. " ==> " .. cleanCaller .. "\n"); 62 caller = cleanCaller; 63 end; 64 end; 65 end; 66 end; 67 68--include config.lua 69 require "resources.functions.config"; 70 71--include json library 72 local json 73 if (debug["sql"]) then 74 json = require "resources.functions.lunajson" 75 end 76 77--connect to the database 78 local Database = require "resources.functions.database"; 79 dbh = Database.new('system'); 80 if (database["type"] == "mysql") then 81 sql = "SELECT CONCAT(v_contacts.contact_name_given, ' ', v_contacts.contact_name_family,' (',v_contact_phones.phone_type,')') AS name FROM v_contacts "; 82 elseif (database["type"] == "pgsql") then 83 sql = "SELECT CASE WHEN contact_name_given = '' THEN v_contacts.contact_organization ELSE v_contacts.contact_name_given || ' ' || v_contacts.contact_name_family || ' (' || v_contact_phones.phone_label || ')' END AS name FROM v_contacts "; 84 else 85 sql = "SELECT v_contacts.contact_name_given || ' ' || v_contacts.contact_name_family || ' (' || v_contact_phones.phone_type || ')' AS name FROM v_contacts "; 86 end 87 sql = sql .. "INNER JOIN v_contact_phones ON v_contact_phones.contact_uuid = v_contacts.contact_uuid "; 88 sql = sql .. "INNER JOIN v_destinations ON v_destinations.domain_uuid = v_contacts.domain_uuid "; 89 sql = sql .. "WHERE v_contact_phones.phone_number = :caller " 90 local params = {caller = caller} 91 92 if (debug["sql"]) then 93 freeswitch.consoleLog("notice", "[cidlookup] SQL: "..sql.."; params:" .. json.encode(params) .. "\n"); 94 end 95 96 dbh:query(sql, params, function(row) 97 name = row.name; 98 end); 99 100 if (name == nil) then 101 freeswitch.consoleLog("NOTICE", "[cidlookup] caller name from contacts db is nil\n"); 102 else 103 freeswitch.consoleLog("NOTICE", "[cidlookup] caller name from contacts db: "..name.."\n"); 104 end 105 106--check if there is a record, if it not then use cidlookup 107 if ((name == nil) or (string.len(name) == 0)) then 108 cidlookup_exists = api:executeString("module_exists cidlookup"); 109 if (cidlookup_exists == "true") then 110 name = api:executeString("cidlookup " .. caller); 111 end 112 end 113 114--set the caller id name 115 if ((name ~= nil) and (string.len(name) > 0)) then 116 freeswitch.consoleLog("NOTICE", "[cidlookup] uuid_setvar " .. uuid .. " caller_id_name " .. name); 117 api:executeString("uuid_setvar " .. uuid .. " caller_id_name " .. name); 118 119 freeswitch.consoleLog("NOTICE", "[cidlookup] uuid_setvar " .. uuid .. " effective_caller_id_name " .. name); 120 api:executeString("uuid_setvar " .. uuid .. " effective_caller_id_name " .. name); 121 end 122