From c743a068755cba57341c8d6fbff02f8a30e0c548 Mon Sep 17 00:00:00 2001 From: James Rose Date: Fri, 23 Nov 2012 18:28:14 +0000 Subject: [PATCH] fax retry will now send using different combinations of t38 (on/off) ecm (on/off) fast/slow. also sends an email based on success or failure. --- app/fax/fax_view.php | 34 +++- includes/install/scripts/fax_retry.lua | 207 ++++++++++++++----------- 2 files changed, 149 insertions(+), 92 deletions(-) diff --git a/app/fax/fax_view.php b/app/fax/fax_view.php index a9a37b4767..54ff646149 100644 --- a/app/fax/fax_view.php +++ b/app/fax/fax_view.php @@ -256,11 +256,41 @@ else { exec("gs -q -sDEVICE=tiffg3 -r204x196 -g1728x2156 -dNOPAUSE -sOutputFile=".$fax_name.".tif -- ".$fax_name.".pdf -c quit"); //exec("rm ".$dir_fax_temp.'/'.$fax_name.".pdf"); } + //get some more info to send the fax + $mailfrom_address = $_SESSION['email']['smtp_from']['var']; + + $sql = "select fax_email from v_fax where fax_uuid = '".$fax_uuid."'; "; + $prep_statement = $db->prepare(check_sql($sql)); + $prep_statement->execute(); + $result = $prep_statement->fetch(PDO::FETCH_NAMED); + $mailto_address_fax = $result["fax_email"]; + echo $mailto_address_fax; + + $sql = "select contact_uuid from v_users where user_uuid = '".$_SESSION['user_uuid']."'; "; + $prep_statement = $db->prepare(check_sql($sql)); + $prep_statement->execute(); + $result = $prep_statement->fetch(PDO::FETCH_NAMED); + //print_r($result); + + $sql = "select contact_email from v_contacts where contact_uuid = '".$result["contact_uuid"]."'; "; + $prep_statement = $db->prepare(check_sql($sql)); + $prep_statement->execute(); + $result = $prep_statement->fetch(PDO::FETCH_NAMED); + //print_r($result); + $mailto_address_user = $result["contact_email"]; + echo $mailto_address_user; + + if ($mailto_address_user != $mailto_address_fax) { + $mailto_address = "'".$mailto_address_fax."\,".$mailto_address_user."'"; + } + else { + $mailto_address = $mailto_address_user; + } //send the fax $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); if ($fp) { - //prepare the fax originate command + //prepare the fax command $route_array = outbound_route_to_bridge($_SESSION['domain_uuid'], $fax_number); $fax_file = $dir_fax_temp."/".$fax_name.".tif"; if (count($route_array) == 0) { @@ -273,7 +303,7 @@ else { $fax_uri = $route_array[0]; $t38 = "fax_enable_t38=true,fax_enable_t38_request=true,"; } - $cmd = "api originate {origination_caller_id_name='".$fax_caller_id_name."',origination_caller_id_number='".$fax_caller_id_number."',fax_ident='".$fax_caller_id_number."',fax_header='".$fax_caller_id_name."',fax_uri=".$fax_uri.",fax_file='".$fax_file."',fax_retry_attempts=1,fax_retry_limit=20,fax_retry_sleep=180,fax_verbose=true,fax_use_ecm=off,".$t38."api_hangup_hook='lua fax_retry.lua'}".$fax_uri." &txfax('".$fax_file."')"; + $cmd = "api originate {mailto_address='".$mailto_address."',mailfrom_address='".$mailfrom_address."',origination_caller_id_name='".$fax_caller_id_name."',origination_caller_id_number='".$fax_caller_id_number."',fax_ident='".$fax_caller_id_number."',fax_header='".$fax_caller_id_name."',fax_uri=".$fax_uri.",fax_file='".$fax_file."',fax_retry_attempts=1,fax_retry_limit=20,fax_retry_sleep=180,fax_verbose=true,fax_use_ecm=off,".$t38."api_hangup_hook='lua fax_retry.lua'}".$fax_uri." &txfax('".$fax_file."')"; //send the command to event socket $response = event_socket_request($fp, $cmd); $response = str_replace("\n", "", $response); diff --git a/includes/install/scripts/fax_retry.lua b/includes/install/scripts/fax_retry.lua index 217564be44..18b5ebf314 100644 --- a/includes/install/scripts/fax_retry.lua +++ b/includes/install/scripts/fax_retry.lua @@ -1,21 +1,10 @@ +--contribtors: Mark J. Crane, James O. Rose + --set default variables - fax_retry_sleep = 300; - fax_retry_limit = 5; - ---include the lua script - scripts_dir = string.sub(debug.getinfo(1).source,2,string.len(debug.getinfo(1).source)-(string.len(argv[0])+1)); - include = assert(loadfile(scripts_dir .. "/resources/config.lua")); - include(); - ---connect to the database - --ODBC - data source name - if (dsn_name) then - dbh = freeswitch.Dbh(dsn_name,dsn_username,dsn_password); - end - --FreeSWITCH core db handler - if (db_type == "sqlite") then - dbh = freeswitch.Dbh("core:"..db_path.."/"..db_name); - end + fax_retry_sleep = 10; + fax_retry_limit = 4; + fax_busy_limit = 3; + api = freeswitch.API(); -- show all channel variables --dat = env:serialize() @@ -35,28 +24,23 @@ --variable_fax_transfer_rate: 14400 -- set channel variables to lua variables - domain_uuid = env:getHeader("domain_uuid"); uuid = env:getHeader("uuid"); fax_success = env:getHeader("fax_success"); - fax_result_code = env:getHeader("fax_result_code"); fax_result_text = env:getHeader("fax_result_text"); fax_ecm_used = env:getHeader("fax_ecm_used"); - fax_local_station_id = env:getHeader("fax_local_station_id"); - fax_document_transferred_pages = env:getHeader("fax_document_transferred_pages"); - fax_document_total_pages = env:getHeader("fax_document_total_pages"); - fax_image_resolution = env:getHeader("fax_image_resolution"); - fax_image_size = env:getHeader("fax_image_size"); - fax_bad_rows = env:getHeader("fax_bad_rows"); - fax_transfer_rate = env:getHeader("fax_transfer_rate"); fax_retry_attempts = tonumber(env:getHeader("fax_retry_attempts")); fax_retry_limit = tonumber(env:getHeader("fax_retry_limit")); - fax_retry_sleep = tonumber(env:getHeader("fax_retry_sleep")); + --fax_retry_sleep = tonumber(env:getHeader("fax_retry_sleep")); 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"); + 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")); + --set default values if (not origination_caller_id_name) then origination_caller_id_name = '000000000000000'; @@ -64,78 +48,121 @@ if (not origination_caller_id_number) then origination_caller_id_number = '000000000000000'; end + if (not fax_busy_attempts) then + fax_busy_attempts = 0; + end + +--for email + email_address = env:getHeader("mailto_address"); + --email_address = api:execute("system", "/bin/echo -n "..email_address.." | /bin/sed -e s/\,/\\\\,/g"); + email_address = email_address:gsub(",", "\\,"); + from_address = env:getHeader("mailfrom_address"); + --needs to be fixed on lesser operating systems that do not have GNU utils. + 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." -- send the selected variables to the console - if (fax_success) then - freeswitch.consoleLog("INFO","fax_success: '" .. fax_success .. "'\n"); - end - if (fax_result_text) then - freeswitch.consoleLog("INFO","fax_result_text: '" .. fax_result_text .. "'\n"); - end - if (fax_file) then - freeswitch.consoleLog("INFO","fax_file: '" .. fax_file .. "'\n"); - end - if (uuid) then - freeswitch.consoleLog("INFO","uuid: '" .. uuid .. "'\n"); - end - if (fax_ecm_used) then - freeswitch.consoleLog("INFO","fax_ecm_used: '" .. fax_ecm_used .. "'\n"); - end - if (fax_retry_attempts) then - freeswitch.consoleLog("INFO","fax_retry_attempts: " .. fax_retry_attempts.. "\n"); - end - if (fax_retry_limit) then - freeswitch.consoleLog("INFO","fax_retry_limit: " .. fax_retry_limit.. "\n"); - end - if (fax_retry_sleep) then - freeswitch.consoleLog("INFO","fax_retry_sleep: " .. fax_retry_sleep.. "\n"); - end - if (fax_uri) then - freeswitch.consoleLog("INFO","fax_uri: '" .. fax_uri.. "'\n"); - end - if (origination_caller_id_name) then - freeswitch.consoleLog("INFO","origination_caller_id_name: " .. origination_caller_id_name .. "\n"); - end - if (origination_caller_id_number) then - freeswitch.consoleLog("INFO","origination_caller_id_number: " .. origination_caller_id_number .. "\n"); - end + freeswitch.consoleLog("INFO","fax_success: '" .. fax_success .. "'\n"); + freeswitch.consoleLog("INFO","fax_result_text: '" .. fax_result_text .. "'\n"); + freeswitch.consoleLog("INFO","fax_file: '" .. fax_file .. "'\n"); + freeswitch.consoleLog("INFO","uuid: '" .. uuid .. "'\n"); + freeswitch.consoleLog("INFO","fax_ecm_used: '" .. fax_ecm_used .. "'\n"); + freeswitch.consoleLog("INFO","fax_retry_attempts: " .. fax_retry_attempts.. "\n"); + freeswitch.consoleLog("INFO","fax_retry_limit: " .. fax_retry_limit.. "\n"); + freeswitch.consoleLog("INFO","fax_retry_sleep: " .. fax_retry_sleep.. "\n"); + freeswitch.consoleLog("INFO","fax_uri: '" .. fax_uri.. "'\n"); + freeswitch.consoleLog("INFO","origination_caller_id_name: " .. origination_caller_id_name .. "\n"); + freeswitch.consoleLog("INFO","origination_caller_id_number: " .. origination_caller_id_number .. "\n"); + freeswitch.consoleLog("INFO","fax_result_code: ".. fax_result_code .."\n"); + freeswitch.consoleLog("INFO","mailfrom_address: ".. from_address .."\n"); + freeswitch.consoleLog("INFO","mailto_address: ".. email_address .."\n"); -- if the fax failed then try again if (fax_success == "0") then + --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; + +--to keep the originate command shorter these are things we always send. One place to adjust for all. + originate_same = "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.."'"; + + if (fax_retry_attempts < fax_retry_limit) then - -- sleep - freeswitch.msleep(fax_retry_sleep * 1000); - --increment the retry attempts - fax_retry_attempts = fax_retry_attempts + 1; - fax_params = ",fax_use_ecm=off"; - --fax_enable_t38=false - --fax_enable_t38_request=false - --fax_disable_v17 - cmd = "originate {origination_caller_id_name='"..origination_caller_id_name.. "',origination_caller_id_number="..origination_caller_id_number..",fax_uri="..fax_uri..",fax_retry_attempts="..fax_retry_attempts..",fax_retry_limit="..fax_retry_limit..",fax_retry_sleep="..fax_retry_sleep..",fax_verbose=true,fax_file='"..fax_file.."'"..fax_params..",api_hangup_hook='lua fax_retry.lua'}"..fax_uri.." &txfax('"..fax_file.."')"; - --cmd = "sofia/internal/"..fax_number.."@"..domain_name.." &txfax('"..fax_file.."') XML default "; + + --timed out waitng for comm or on first message (busy code?) + if (fax_result_code == "2" or fax_result_code == "3" ) then + --do nothing. don't want to increment + freeswitch.consoleLog("INFO","Last Fax was probably Busy, don't increment retry_attempts. \n"); + fax_busy_attempts = fax_busy_attempts + 1; + freeswitch.msleep(fax_retry_sleep * 1000); + + elseif (fax_retry_attempts < 5 ) then + freeswitch.consoleLog("INFO","Last Fax Failed, try a different way. Wait first.\n"); + freeswitch.msleep(fax_retry_sleep * 500); + else + freeswitch.consoleLog("INFO","All attempts to send fax to "..number_dialed.."FAILED\n"); + end + + if (fax_retry_attempts == 1) then + --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"); + fax_retry_attempts = fax_retry_attempts + 1; + 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 + --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"); + fax_retry_attempts = fax_retry_attempts + 1; + 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 + --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"); + fax_retry_attempts = fax_retry_attempts + 1; + + 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 + --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"); + fax_retry_attempts = fax_retry_attempts + 1; + + 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.."')"; + + else + --the fax failed completely. send a message + freeswitch.consoleLog("INFO","FAX TRIED ["..fax_retry_attempts.."] of [4]: GIVING UP\n"); + + email_address = email_address:gsub("\\,", ","); + + freeswitch.email("", + "", + "To: "..email_address.."\nFrom: "..from_address.."\nSubject: Fax to: "..number_dialed.." FAILED", + email_message_fail , + fax_file + ); + + fax_retry_attempts = fax_retry_attempts + 1; + + + + + end freeswitch.consoleLog("INFO","retry cmd: " .. cmd .. "\n"); api = freeswitch.API(); reply = api:executeString(cmd); - else - -- from = ""; - -- to = ""; - -- subject = "FAX Status: failed"; - -- body = [[file: ]] .. fax_file"; - -- freeswitch.email("", - -- "", - -- "To: "..to.."\nFrom: "..from.."\nSubject:" .. subject, - -- body - -- ); end + else - --send email notice when the fax was sent succesffully - --from = ""; - --to = ""; - --subject = "FAX Status: succeeded"; - --body = [[file: ]] .. fax_file"; - --freeswitch.email("", - -- "", - -- "To: "..to.."\nFrom: "..from.."\nSubject:" .. subject, - -- body - -- ); + --Huzah! Success! + + email_address = email_address:gsub("\\,", ","); + + freeswitch.email("", + "", + "To: "..email_address.."\nFrom: "..from_address.."\nSubject: Fax to: "..number_dialed.." SENT", + email_message_success , + fax_file + ); end