2014-09-04 09:12:47 +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>
2015-07-31 03:32:18 +02:00
-- Copyright (C) 2010 - 2015
2014-09-04 09:12:47 +02:00
-- the Initial Developer. All Rights Reserved.
--
-- Contributor(s):
-- Mark J. Crane
-- James O. Rose
2015-01-26 20:54:52 +01:00
-- Luis Daniel Lucio Quiroz
2012-11-23 19:28:14 +01:00
2012-10-10 04:18:40 +02:00
--set default variables
2012-11-29 00:03:05 +01:00
fax_retry_sleep = 30 ;
2012-11-23 19:28:14 +01:00
fax_retry_limit = 4 ;
2014-01-21 22:55:38 +01:00
fax_busy_limit = 5 ;
2012-11-23 19:28:14 +01:00
api = freeswitch.API ( ) ;
2012-10-08 20:31:06 +02:00
2014-09-04 09:12:47 +02:00
--include config.lua
2015-08-10 19:50:29 +02:00
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 ( ) ) ;
2014-09-04 09:12:47 +02:00
--connect to the database
2015-08-10 19:50:29 +02:00
dofile ( scripts_dir .. " /resources/functions/database_handle.lua " ) ;
2014-09-04 09:12:47 +02:00
dbh = database_handle ( ' system ' ) ;
2014-09-07 07:18:35 +02:00
--define the explode function
2015-08-10 19:50:29 +02:00
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
2014-09-07 07:18:35 +02:00
--array count
2015-08-10 19:50:29 +02:00
function count ( t )
c = 0 ;
for k , v in pairs ( t ) do
c = c + 1 ;
end
return c ;
end
2014-09-07 07:18:35 +02:00
2012-06-04 16:58:40 +02:00
-- show all channel variables
2014-02-19 07:15:53 +01:00
--dat = env:serialize()
2015-07-31 03:32:18 +02:00
--freeswitch.consoleLog("INFO","[FAX] info:\n" .. dat .. "\n")
2012-06-04 16:58:40 +02:00
-- example channel variables relating to fax
--variable_fax_success: 0
--variable_fax_result_code: 49
--variable_fax_result_text: The%20call%20dropped%20prematurely
--variable_fax_ecm_used: off
--variable_fax_local_station_id: SpanDSP%20Fax%20Ident
--variable_fax_document_transferred_pages: 0
--variable_fax_document_total_pages: 0
--variable_fax_image_resolution: 0x0
--variable_fax_image_size: 0
--variable_fax_bad_rows: 0
--variable_fax_transfer_rate: 14400
-- set channel variables to lua variables
uuid = env : getHeader ( " uuid " ) ;
2015-07-31 03:32:18 +02:00
domain_uuid = env : getHeader ( " domain_uuid " ) ;
domain_name = env : getHeader ( " domain_name " ) ;
2012-06-04 16:58:40 +02:00
fax_success = env : getHeader ( " fax_success " ) ;
fax_result_text = env : getHeader ( " fax_result_text " ) ;
2014-09-04 09:12:47 +02:00
fax_local_station_id = env : getHeader ( " fax_local_station_id " ) ;
fax_ecm_used = env : getHeader ( " fax_ecm_used " ) ;
2012-06-04 16:58:40 +02:00
fax_retry_attempts = tonumber ( env : getHeader ( " fax_retry_attempts " ) ) ;
fax_retry_limit = tonumber ( env : getHeader ( " fax_retry_limit " ) ) ;
2012-11-23 19:28:14 +01:00
--fax_retry_sleep = tonumber(env:getHeader("fax_retry_sleep"));
2012-06-04 16:58:40 +02:00
fax_uri = env : getHeader ( " fax_uri " ) ;
fax_file = env : getHeader ( " fax_file " ) ;
fax_extension_number = env : getHeader ( " fax_extension_number " ) ;
origination_caller_id_name = env : getHeader ( " origination_caller_id_name " ) ;
origination_caller_id_number = env : getHeader ( " origination_caller_id_number " ) ;
2014-09-04 09:12:47 +02:00
fax_bad_rows = env : getHeader ( " fax_bad_rows " ) ;
fax_transfer_rate = env : getHeader ( " fax_transfer_rate " ) ;
2015-01-26 20:54:52 +01:00
accountcode = env : getHeader ( " accountcode " ) ;
2015-04-27 16:32:29 +02:00
sip_to_user = env : getHeader ( " sip_to_user " ) ;
2012-11-23 19:28:14 +01:00
bridge_hangup_cause = env : getHeader ( " bridge_hangup_cause " ) ;
fax_result_code = env : getHeader ( " fax_result_code " ) ;
fax_busy_attempts = tonumber ( env : getHeader ( " fax_busy_attempts " ) ) ;
2013-07-01 22:08:08 +02:00
hangup_cause_q850 = tonumber ( env : getHeader ( " hangup_cause_q850 " ) ) ;
2014-01-21 22:55:38 +01:00
2012-06-04 16:58:40 +02:00
--set default values
if ( not origination_caller_id_name ) then
origination_caller_id_name = ' 000000000000000 ' ;
end
if ( not origination_caller_id_number ) then
origination_caller_id_number = ' 000000000000000 ' ;
end
2012-11-23 19:28:14 +01:00
if ( not fax_busy_attempts ) then
fax_busy_attempts = 0 ;
end
2015-04-27 16:32:29 +02:00
--we got a busy signal.... may want to check the sip_term_cause
2013-07-01 22:08:08 +02:00
if ( not fax_success ) then
2014-01-21 22:55:38 +01:00
fax_success = " 0 " ;
2013-07-01 22:08:08 +02:00
fax_result_code = 2 ;
end
if ( hangup_cause_q850 == " 17 " ) then
2014-01-21 22:55:38 +01:00
fax_success = " 0 " ;
fax_result_code = 2 ;
2013-07-01 22:08:08 +02:00
end
if ( not fax_result_text ) then
fax_result_text = " FS_NOT_SET " ;
end
2014-09-07 07:18:35 +02:00
--get the values from the fax file
array = explode ( " / " , fax_file ) ;
domain_name = array [ count ( array ) - 3 ] ;
2014-09-09 13:03:00 +02:00
fax_extension = array [ count ( array ) - 2 ] ;
2014-09-07 07:18:35 +02:00
file_name = array [ count ( array ) ] ;
--get the domain_uuid using the domain name required for multi-tenant
2015-07-31 03:32:18 +02:00
if ( domain_uuid == nil and domain_name ~= nil ) then
2014-09-07 07:18:35 +02:00
sql = " SELECT domain_uuid FROM v_domains " ;
sql = sql .. " WHERE domain_name = ' " .. domain_name .. " ' " ;
status = dbh : query ( sql , function ( rows )
domain_uuid = rows [ " domain_uuid " ] ;
end ) ;
end
2015-06-30 20:01:30 +02:00
--settings
2015-08-10 19:50:29 +02:00
dofile ( scripts_dir .. " /resources/functions/settings.lua " ) ;
2015-06-30 20:01:30 +02:00
settings = settings ( domain_uuid ) ;
storage_type = " " ;
storage_path = " " ;
if ( settings [ ' fax ' ] ~= nil ) then
if ( settings [ ' fax ' ] [ ' storage_type ' ] ~= nil ) then
if ( settings [ ' fax ' ] [ ' storage_type ' ] [ ' text ' ] ~= nil ) then
storage_type = settings [ ' fax ' ] [ ' storage_type ' ] [ ' text ' ] ;
end
end
if ( settings [ ' fax ' ] [ ' storage_path ' ] ~= nil ) then
if ( settings [ ' fax ' ] [ ' storage_path ' ] [ ' text ' ] ~= nil ) then
storage_path = settings [ ' fax ' ] [ ' storage_path ' ] [ ' text ' ] ;
storage_path = storage_path : gsub ( " ${domain_name} " , domain_name ) ;
storage_path = storage_path : gsub ( " ${voicemail_id} " , voicemail_id ) ;
storage_path = storage_path : gsub ( " ${voicemail_dir} " , voicemail_dir ) ;
end
end
end
2015-01-26 20:54:52 +01:00
--be sure accountcode is not empty
if ( accountcode == nil ) then
accountcode = domain_name ;
end
2014-09-09 13:03:00 +02:00
--get the domain_uuid using the domain name required for multi-tenant
if ( domain_uuid ~= nil and fax_extension ~= nil ) then
sql = " SELECT fax_uuid FROM v_fax " ;
sql = sql .. " WHERE domain_uuid = ' " .. domain_uuid .. " ' " ;
sql = sql .. " AND fax_extension = ' " .. fax_extension .. " ' " ;
status = dbh : query ( sql , function ( rows )
fax_uuid = rows [ " fax_uuid " ] ;
end ) ;
end
2014-09-04 09:12:47 +02:00
--add to fax logs
sql = " insert into v_fax_logs " ;
sql = sql .. " ( " ;
2014-09-07 07:18:35 +02:00
sql = sql .. " fax_log_uuid, " ;
sql = sql .. " domain_uuid, " ;
2014-09-09 13:03:00 +02:00
if ( fax_uuid ~= nil ) then
sql = sql .. " fax_uuid, " ;
end
2014-09-04 09:12:47 +02:00
sql = sql .. " fax_success, " ;
sql = sql .. " fax_result_code, " ;
sql = sql .. " fax_result_text, " ;
sql = sql .. " fax_file, " ;
if ( fax_ecm_used ~= nil ) then
sql = sql .. " fax_ecm_used, " ;
end
if ( fax_local_station_id ~= nil ) then
sql = sql .. " fax_local_station_id, " ;
end
sql = sql .. " fax_document_transferred_pages, " ;
sql = sql .. " fax_document_total_pages, " ;
if ( fax_image_resolution ~= nil ) then
sql = sql .. " fax_image_resolution, " ;
end
if ( fax_image_size ~= nil ) then
sql = sql .. " fax_image_size, " ;
end
if ( fax_bad_rows ~= nil ) then
sql = sql .. " fax_bad_rows, " ;
end
if ( fax_transfer_rate ~= nil ) then
sql = sql .. " fax_transfer_rate, " ;
end
if ( fax_retry_attempts ~= nil ) then
sql = sql .. " fax_retry_attempts, " ;
end
if ( fax_retry_limit ~= nil ) then
sql = sql .. " fax_retry_limit, " ;
end
if ( fax_retry_sleep ~= nil ) then
sql = sql .. " fax_retry_sleep, " ;
end
2014-09-07 07:18:35 +02:00
sql = sql .. " fax_uri, " ;
sql = sql .. " fax_date, " ;
sql = sql .. " fax_epoch " ;
2014-09-04 09:12:47 +02:00
sql = sql .. " ) " ;
sql = sql .. " values " ;
sql = sql .. " ( " ;
2014-09-07 07:18:35 +02:00
sql = sql .. " ' " .. uuid .. " ', " ;
sql = sql .. " ' " .. domain_uuid .. " ', " ;
2014-09-09 13:03:00 +02:00
if ( fax_uuid ~= nil ) then
sql = sql .. " ' " .. fax_uuid .. " ', " ;
end
2014-09-04 09:12:47 +02:00
sql = sql .. " ' " .. fax_success .. " ', " ;
sql = sql .. " ' " .. fax_result_code .. " ', " ;
sql = sql .. " ' " .. fax_result_text .. " ', " ;
sql = sql .. " ' " .. fax_file .. " ', " ;
if ( fax_ecm_used ~= nil ) then
sql = sql .. " ' " .. fax_ecm_used .. " ', " ;
end
if ( fax_local_station_id ~= nil ) then
sql = sql .. " ' " .. fax_local_station_id .. " ', " ;
end
if ( fax_document_transferred_pages == nil ) then
sql = sql .. " '0', " ;
else
sql = sql .. " ' " .. fax_document_transferred_pages .. " ', " ;
end
if ( fax_document_total_pages == nil ) then
sql = sql .. " '0', " ;
else
sql = sql .. " ' " .. fax_document_total_pages .. " ', " ;
end
if ( fax_image_resolution ~= nil ) then
sql = sql .. " ' " .. fax_image_resolution .. " ', " ;
end
if ( fax_image_size ~= nil ) then
sql = sql .. " ' " .. fax_image_size .. " ', " ;
end
if ( fax_bad_rows ~= nil ) then
sql = sql .. " ' " .. fax_bad_rows .. " ', " ;
end
if ( fax_transfer_rate ~= nil ) then
sql = sql .. " ' " .. fax_transfer_rate .. " ', " ;
end
if ( fax_retry_attempts ~= nil ) then
sql = sql .. " ' " .. fax_retry_attempts .. " ', " ;
end
if ( fax_retry_limit ~= nil ) then
sql = sql .. " ' " .. fax_retry_limit .. " ', " ;
end
if ( fax_retry_sleep ~= nil ) then
sql = sql .. " ' " .. fax_retry_sleep .. " ', " ;
end
2014-09-07 07:18:35 +02:00
sql = sql .. " ' " .. fax_uri .. " ', " ;
if ( database [ " type " ] == " sqlite " ) then
sql = sql .. " ' " .. os.date ( " %Y-%m-%d %X " ) .. " ', " ;
else
sql = sql .. " now(), " ;
end
sql = sql .. " ' " .. os.time ( ) .. " ' " ;
2014-09-04 09:12:47 +02:00
sql = sql .. " ) " ;
--if (debug["sql"]) then
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " notice " , " [FAX] retry: " .. sql .. " \n " ) ;
2014-09-04 09:12:47 +02:00
--end
dbh : query ( sql ) ;
2013-07-01 22:08:08 +02:00
2012-11-23 19:28:14 +01:00
--for email
email_address = env : getHeader ( " mailto_address " ) ;
--email_address = api:execute("system", "/bin/echo -n "..email_address.." | /bin/sed -e s/\,/\\\\,/g");
2014-01-21 22:55:38 +01:00
--if (not email_address) then
-- email_address = '';
--end
2012-11-23 19:28:14 +01:00
email_address = email_address : gsub ( " , " , " \\ , " ) ;
from_address = env : getHeader ( " mailfrom_address " ) ;
2014-11-29 16:24:02 +01:00
if ( from_address == nil ) then
2013-08-15 01:07:35 +02:00
from_address = email_address ;
end
2014-07-23 23:02:28 +02:00
--needs to be fixed on operating systems that do not have sed or echo utilities.
2012-11-23 19:28:14 +01:00
number_dialed = api : execute ( " system " , " /bin/echo -n " .. fax_uri .. " | sed -e s,.*/,,g " ) ;
--do not use apostrophies in message, they are not excaped and the mail will fail.
email_message_fail = " We are sorry the fax failed to go through. It has been attached. Please check the number " .. number_dialed .. " , and if it was correct you might consider emailing it instead. "
email_message_success = " We are happy to report the fax was sent successfully. It has been attached for your records. "
2012-06-04 16:58:40 +02:00
2015-04-27 16:32:29 +02:00
--add the fax files
if ( fax_success ~= nil ) then
if ( fax_success == " 1 " ) then
if ( storage_type == " base64 " ) then
--include the base64 function
2015-08-10 19:50:29 +02:00
dofile ( scripts_dir .. " /resources/functions/base64.lua " ) ;
2015-04-27 18:45:23 +02:00
2015-04-27 16:32:29 +02:00
--base64 encode the file
local f = io.open ( fax_file , " rb " ) ;
local file_content = f : read ( " *all " ) ;
f : close ( ) ;
fax_base64 = base64.encode ( file_content ) ;
end
local sql = { }
table.insert ( sql , " insert into v_fax_files " ) ;
table.insert ( sql , " ( " ) ;
table.insert ( sql , " fax_file_uuid, " ) ;
table.insert ( sql , " fax_uuid, " ) ;
table.insert ( sql , " fax_mode, " ) ;
if ( sip_to_user ~= nil ) then
table.insert ( sql , " fax_destination, " ) ;
end
table.insert ( sql , " fax_file_type, " ) ;
table.insert ( sql , " fax_file_path, " ) ;
table.insert ( sql , " fax_caller_id_name, " ) ;
table.insert ( sql , " fax_caller_id_number, " ) ;
table.insert ( sql , " fax_date, " ) ;
table.insert ( sql , " fax_epoch, " ) ;
if ( storage_type == " base64 " ) then
table.insert ( sql , " fax_base64, " ) ;
end
table.insert ( sql , " domain_uuid " ) ;
table.insert ( sql , " ) " ) ;
table.insert ( sql , " values " ) ;
table.insert ( sql , " ( " ) ;
table.insert ( sql , " ' " .. uuid .. " ', " ) ;
table.insert ( sql , " ' " .. fax_uuid .. " ', " ) ;
table.insert ( sql , " 'tx', " ) ;
if ( sip_to_user ~= nil ) then
table.insert ( sql , " ' " .. sip_to_user .. " ', " ) ;
end
table.insert ( sql , " 'tif', " ) ;
2015-05-09 07:22:29 +02:00
fax_file = string.gsub ( fax_file , ' /temp/ ' , ' /sent/ ' ) ;
2015-04-27 16:32:29 +02:00
table.insert ( sql , " ' " .. fax_file .. " ', " ) ;
table.insert ( sql , " ' " .. origination_caller_id_name .. " ', " ) ;
table.insert ( sql , " ' " .. origination_caller_id_number .. " ', " ) ;
if ( database [ " type " ] == " sqlite " ) then
table.insert ( sql , " ' " .. os.date ( " %Y-%m-%d %X " ) .. " ', " ) ;
else
table.insert ( sql , " now(), " ) ;
end
table.insert ( sql , " ' " .. os.time ( ) .. " ', " ) ;
if ( storage_type == " base64 " ) then
table.insert ( sql , " ' " .. fax_base64 .. " ', " ) ;
end
table.insert ( sql , " ' " .. domain_uuid .. " ' " ) ;
table.insert ( sql , " ) " ) ;
sql = table.concat ( sql , " \n " ) ;
--if (debug["sql"]) then
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " notice " , " [FAX] SQL: " .. sql .. " \n " ) ;
2015-04-27 16:32:29 +02:00
--end
if ( storage_type == " base64 " ) then
array = explode ( " :// " , database [ " system " ] ) ;
local luasql = require " luasql.postgres " ;
local env = assert ( luasql.postgres ( ) ) ;
local dbh = env : connect ( array [ 2 ] ) ;
res , serr = dbh : execute ( sql ) ;
dbh : close ( ) ;
env : close ( ) ;
else
result = dbh : query ( sql ) ;
end
end
end
2012-06-04 16:58:40 +02:00
-- send the selected variables to the console
2014-11-29 16:24:02 +01:00
if ( fax_success ~= nil ) then
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] Success: ' " .. fax_success .. " ' \n " ) ;
2014-04-30 07:04:54 +02:00
end
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] fax_result_text: ' " .. fax_result_text .. " ' \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] fax_file: ' " .. fax_file .. " ' \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] uuid: ' " .. uuid .. " ' \n " ) ;
2013-07-01 22:08:08 +02:00
--freeswitch.consoleLog("INFO","fax_ecm_used: '" .. fax_ecm_used .. "'\n");
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] fax_retry_attempts: " .. fax_retry_attempts .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] fax_retry_limit: " .. fax_retry_limit .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] fax_retry_sleep: " .. fax_retry_sleep .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] fax_uri: ' " .. fax_uri .. " ' \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] accountcode: ' " .. accountcode .. " ' \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] origination_caller_id_name: " .. origination_caller_id_name .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] origination_caller_id_number: " .. origination_caller_id_number .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] fax_result_code: " .. fax_result_code .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] mailfrom_address: " .. from_address .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] mailto_address: " .. email_address .. " \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] hangup_cause_q850: ' " .. hangup_cause_q850 .. " ' \n " ) ;
2012-06-04 16:58:40 +02:00
-- if the fax failed then try again
if ( fax_success == " 0 " ) then
2015-07-31 03:32:18 +02:00
--DEBUG
--email_cmd = "/bin/echo '"..email_message_fail.."' | /usr/bin/mail -s 'Fax to: "..number_dialed.." FAILED' -r "..from_address.." -a '"..fax_file.."' "..email_address;
2012-11-23 19:28:14 +01:00
2015-07-31 03:32:18 +02:00
--to keep the originate command shorter these are things we always send. One place to adjust for all.
originate_same = " for_fax=1,absolute_codec_string='PCMU,PCMA',accountcode=' " .. accountcode .. " ',domain_uuid= " .. domain_uuid .. " ,domain_name= " .. domain_name .. " ,mailto_address=' " .. email_address .. " ',mailfrom_address=' " .. from_address .. " ',origination_caller_id_name=' " .. origination_caller_id_name .. " ',origination_caller_id_number= " .. origination_caller_id_number .. " ,fax_uri= " .. fax_uri .. " ,fax_retry_limit= " .. fax_retry_limit .. " ,fax_retry_sleep= " .. fax_retry_sleep .. " ,fax_verbose=true,fax_file=' " .. fax_file .. " ' " ;
2012-11-23 19:28:14 +01:00
2012-06-04 16:58:40 +02:00
if ( fax_retry_attempts < fax_retry_limit ) then
2014-02-19 07:15:53 +01:00
2013-07-01 22:08:08 +02:00
--timed out waitng for comm or on first message, or busy code
if ( fax_result_code == " 2 " or fax_result_code == " 3 " or hangup_cause_q850 == 17 ) then
2012-11-23 19:28:14 +01:00
--do nothing. don't want to increment
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] Last Fax was probably Busy, don't increment retry_attempts. \n " ) ;
2012-11-23 19:28:14 +01:00
fax_busy_attempts = fax_busy_attempts + 1 ;
2013-07-01 22:08:08 +02:00
if ( fax_busy_attempts > fax_busy_limit ) then
fax_retry_attempts = 17 ;
else
freeswitch.msleep ( fax_retry_sleep * 1000 ) ;
end
--unallocated number
elseif ( hangup_cause_q850 == 1 ) then
fax_retry_attempts = 10 ;
2015-07-31 03:32:18 +02:00
email_message_fail = " We are sorry the fax failed to go through. The number you specified is not a working number. The fax has been attached. Please check the number " .. number_dialed .. " , and if it was correct you might consider emailing it instead. "
2012-11-23 19:28:14 +01:00
elseif ( fax_retry_attempts < 5 ) then
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] Last Fax Failed, try a different way. Wait first. \n " ) ;
2012-11-23 19:28:14 +01:00
freeswitch.msleep ( fax_retry_sleep * 500 ) ;
else
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] All attempts to send fax to " .. number_dialed .. " FAILED \n " ) ;
2012-11-23 19:28:14 +01:00
end
if ( fax_retry_attempts == 1 ) then
2015-07-31 03:32:18 +02:00
--send t38 on ECM on
freeswitch.consoleLog ( " INFO " , " [FAX] TRYING [ " .. fax_retry_attempts .. " ] of [4] to: " .. number_dialed .. " with: t38 ON ECM ON, Fast \n " ) ;
2013-07-01 22:08:08 +02:00
if ( hangup_cause_q850 ~= 17 ) then
fax_retry_attempts = fax_retry_attempts + 1 ;
end
2012-11-23 19:28:14 +01:00
cmd = " originate {fax_retry_attempts= " .. fax_retry_attempts .. " , " .. originate_same .. " ,fax_use_ecm=true,fax_enable_t38=true,fax_enable_t38_request=true,fax_disable_v17=false,fax_busy_attempts=' " .. fax_busy_attempts .. " ',api_hangup_hook='lua fax_retry.lua'} " .. fax_uri .. " &txfax(' " .. fax_file .. " ') " ;
elseif ( fax_retry_attempts == 2 ) then
2015-07-31 03:32:18 +02:00
--send t38 off, ECM on
freeswitch.consoleLog ( " INFO " , " [FAX] TRYING [ " .. fax_retry_attempts .. " ] of [4] to: " .. number_dialed .. " with: t38 OFF ECM ON, Fast \n " ) ;
2013-07-01 22:08:08 +02:00
if ( hangup_cause_q850 ~= 17 ) then
fax_retry_attempts = fax_retry_attempts + 1 ;
end
2012-11-23 19:28:14 +01:00
cmd = " originate {fax_retry_attempts= " .. fax_retry_attempts .. " , " .. originate_same .. " ,fax_use_ecm=true,fax_enable_t38=false,fax_enable_t38_request=false,fax_disable_v17=false,fax_busy_attempts=' " .. fax_busy_attempts .. " ',api_hangup_hook='lua fax_retry.lua'} " .. fax_uri .. " &txfax(' " .. fax_file .. " ') " ;
elseif ( fax_retry_attempts == 3 ) then
2015-07-31 03:32:18 +02:00
--send t38 on v17 [slow] on ECM off
freeswitch.consoleLog ( " INFO " , " [FAX] TRYING [ " .. fax_retry_attempts .. " ] of [4] to: " .. number_dialed .. " with: t38 ON ECM OFF, SLOW \n " ) ;
2013-07-01 22:08:08 +02:00
if ( hangup_cause_q850 ~= 17 ) then
fax_retry_attempts = fax_retry_attempts + 1 ;
end
2012-11-23 19:28:14 +01:00
cmd = " originate {fax_retry_attempts= " .. fax_retry_attempts .. " , " .. originate_same .. " ,fax_use_ecm=false,fax_enable_t38=true,fax_enable_t38_request=true,fax_disable_v17=true,fax_busy_attempts=' " .. fax_busy_attempts .. " ',api_hangup_hook='lua fax_retry.lua'} " .. fax_uri .. " &txfax(' " .. fax_file .. " ') " ;
elseif ( fax_retry_attempts == 4 ) then
2015-07-31 03:32:18 +02:00
--send t38 off v17 [slow] on ECM off
freeswitch.consoleLog ( " INFO " , " [FAX] TRYING [ " .. fax_retry_attempts .. " ] of [4] to: " .. number_dialed .. " with: t38 OFF ECM OFF, SLOW \n " ) ;
2013-07-01 22:08:08 +02:00
if ( hangup_cause_q850 ~= 17 ) then
fax_retry_attempts = fax_retry_attempts + 1 ;
end
2012-11-23 19:28:14 +01:00
cmd = " originate {fax_retry_attempts= " .. fax_retry_attempts .. " , " .. originate_same .. " ,fax_use_ecm=false,fax_enable_t38=false,fax_enable_t38_request=false,fax_disable_v17=true,fax_busy_attempts=' " .. fax_busy_attempts .. " ',api_hangup_hook='lua fax_retry.lua'} " .. fax_uri .. " &txfax(' " .. fax_file .. " ') " ;
2013-07-01 22:08:08 +02:00
--bad number
elseif ( fax_retry_attempts == 10 ) then
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] RETRY FAILED: BAD NUMBER \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] RETRY_STATS FAILURE BAD NUMBER: GATEWAY[ " .. fax_uri .. " ] " ) ;
2014-02-19 07:15:53 +01:00
email_message_fail = email_message_fail .. " We tried sending, but the number entered was not a working phone number "
email_address = email_address : gsub ( " \\ , " , " , " ) ;
freeswitch.email ( " " ,
" " ,
" To: " .. email_address .. " \n From: " .. from_address .. " \n Subject: Fax to: " .. number_dialed .. " was INVALID " ,
email_message_fail ,
fax_file
) ;
2013-07-01 22:08:08 +02:00
--busy number
elseif ( fax_retry_attempts == 17 ) then
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] RETRY FAILED: TRIED [ " .. fax_busy_attempts .. " ] of [4]: BUSY NUMBER \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] RETRY STATS FAILURE BUSY: GATEWAY[ " .. fax_uri .. " ], BUSY NUMBER " ) ;
2013-07-01 22:08:08 +02:00
email_message_fail = email_message_fail .. " We tried sending, but the call was busy " .. fax_busy_attempts .. " of those times. "
email_address = email_address : gsub ( " \\ , " , " , " ) ;
freeswitch.email ( " " ,
2014-02-19 07:15:53 +01:00
" " ,
" To: " .. email_address .. " \n From: " .. from_address .. " \n Subject: Fax to: " .. number_dialed .. " was BUSY " ,
email_message_fail ,
fax_file
) ;
2013-07-01 22:08:08 +02:00
2012-11-23 19:28:14 +01:00
else
--the fax failed completely. send a message
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] RETRY FAILED: tried [ " .. fax_retry_attempts .. " ] of [4]: GIVING UP \n " ) ;
freeswitch.consoleLog ( " INFO " , " [FAX] RETRY STATS FAILURE: GATEWAY[ " .. fax_uri .. " ], tried 5 combinations without success " ) ;
2013-07-01 22:08:08 +02:00
email_message_fail = email_message_fail .. " We tried sending 5 times ways. You may also want to know that the call was busy " .. fax_busy_attempts .. " of those times. "
2012-11-23 19:28:14 +01:00
email_address = email_address : gsub ( " \\ , " , " , " ) ;
2014-02-19 07:15:53 +01:00
2012-11-23 19:28:14 +01:00
freeswitch.email ( " " ,
2014-02-19 07:15:53 +01:00
" " ,
" To: " .. email_address .. " \n From: " .. from_address .. " \n Subject: Fax to: " .. number_dialed .. " FAILED " ,
email_message_fail ,
fax_file
) ;
2012-11-23 19:28:14 +01:00
fax_retry_attempts = fax_retry_attempts + 1 ;
end
2012-06-04 16:58:40 +02:00
api = freeswitch.API ( ) ;
2012-11-29 00:03:05 +01:00
if ( not cmd ) then
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] Last Attempt (5th) of fax_retry.lua: \n " ) ;
2012-11-29 00:03:05 +01:00
else
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] Retry Command: " .. cmd .. " \n " ) ;
2012-11-29 00:03:05 +01:00
reply = api : executeString ( cmd ) ;
end
2012-06-04 16:58:40 +02:00
end
2012-11-21 21:54:15 +01:00
else
2014-09-04 09:12:47 +02:00
--Success
2012-11-29 00:03:05 +01:00
if ( fax_retry_attempts == 0 ) then
fax_trial = " fax_use_ecm=false,fax_enable_t38=true,fax_enable_t38_request=true,fax_disable_v17=default " ;
elseif ( fax_retry_attempts == 1 ) then
fax_trial = " fax_use_ecm=true,fax_enable_t38=true,fax_enable_t38_request=true,fax_disable_v17=false " ;
elseif ( fax_retry_attempts == 2 ) then
fax_trial = " fax_use_ecm=true,fax_enable_t38=false,fax_enable_t38_request=false,fax_disable_v17=false " ;
elseif ( fax_retry_attempts == 3 ) then
fax_trial = " fax_use_ecm=true,fax_enable_t38=true,fax_enable_t38_request=true,fax_disable_v17=true " ;
elseif ( fax_retry_attempts == 4 ) then
fax_trial = " fax_use_ecm=false,fax_enable_t38=false,fax_enable_t38_request=false,fax_disable_v17=false " ;
2014-02-19 07:15:53 +01:00
else
2012-11-29 00:03:05 +01:00
fax_trial = " fax_retry had an issue and tried more than 5 times "
end
2015-07-31 03:32:18 +02:00
freeswitch.consoleLog ( " INFO " , " [FAX] RETRY STATS SUCCESS: GATEWAY[ " .. fax_uri .. " ] VARS[ " .. fax_trial .. " ] " ) ;
2012-11-23 19:28:14 +01:00
email_address = email_address : gsub ( " \\ , " , " , " ) ;
freeswitch.email ( " " ,
2015-04-27 16:32:29 +02:00
" " ,
" To: " .. email_address .. " \n From: " .. from_address .. " \n Subject: Fax to: " .. number_dialed .. " SENT " ,
email_message_success ,
fax_file
) ;
end