diff --git a/resources/install/scripts/app/fax/resources/scripts/queue/next.lua b/resources/install/scripts/app/fax/resources/scripts/queue/next.lua index fb0df5c1cd..4780867a1d 100644 --- a/resources/install/scripts/app/fax/resources/scripts/queue/next.lua +++ b/resources/install/scripts/app/fax/resources/scripts/queue/next.lua @@ -4,7 +4,6 @@ require "resources.functions.sleep" local log = require "resources.functions.log".next_fax_task local Tasks = require "app.fax.resources.scripts.queue.tasks" local Esl = require "resources.functions.esl" -local send_mail = require "resources.functions.send_mail" local FAX_OPTIONS = { "fax_use_ecm=false,fax_enable_t38=true,fax_enable_t38_request=true,fax_disable_v17=default"; @@ -15,22 +14,16 @@ local FAX_OPTIONS = { } local function task_send_mail(task) - local mail_x_headers = { - ["X-FusionPBX-Domain-UUID"] = task.domain_uuid; - ["X-FusionPBX-Domain-Name"] = task.domain_name; - ["X-FusionPBX-Email-Type"] = 'email2fax'; - } local number_dialed = task.uri:match("/([^/]-)%s*$") - if task.reply_address and #task.reply_address > 0 then - send_mail(mail_x_headers, task.reply_address, { - "Fax to: " .. number_dialed .. " FAILED", - table.concat{ - "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.", - } - }) - end + + Tasks.send_mail_task(task, { + "Fax to: " .. number_dialed .. " FAILED", + table.concat{ + "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.", + }} + ) end local function next_task() diff --git a/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua b/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua index 32ef3fac07..1d634ea725 100644 --- a/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua +++ b/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua @@ -11,6 +11,10 @@ local send_mail = require "resources.functions.send_mail" local fax_task_uuid = env:getHeader('fax_task_uuid') + if not fax_task_uuid then + log.warning("No [fax_task_uuid] channel variable") + return + end local task = Tasks.select_task(fax_task_uuid) if not task then log.warningf("Can not find fax task: %q", tostring(fax_task_uuid)) @@ -31,7 +35,7 @@ local domain_name = env:getHeader("domain_name") or task.domain_name local origination_caller_id_name = env:getHeader("origination_caller_id_name") or '000000000000000' local origination_caller_id_number = env:getHeader("origination_caller_id_number") or '000000000000000' - local accountcode = env:getHeader("accountcode") + local accountcode = env:getHeader("accountcode") or domain_name local duration = tonumber(env:getHeader("billmsec")) or 0 local sip_to_user = env:getHeader("sip_to_user") local bridge_hangup_cause = env:getHeader("bridge_hangup_cause") @@ -142,39 +146,29 @@ ) end ---get the values from the fax file - if not (fax_uuid and domain_name) then - local array = split(fax_file, "[\\/]+") - domain_name = domain_name or array[#array - 3] - local fax_extension = fax_extension or array[#array - 2] + log.debug([[<<< DEBUG >>> + domain_name = '%s' + domain_uuid = '%s' + task.domain_name = '%s' + task.domain_uuid = '%s' +]], + tostring(domain_name ), + tostring(domain_uuid ), + tostring(task.domain_name ), + tostring(task.domain_uuid ) +) - if not fax_uuid then - local sql = "SELECT fax_uuid FROM v_fax " - sql = sql .. "WHERE domain_uuid = '" .. domain_uuid .. "' " - sql = sql .. "AND fax_extension = '" .. fax_extension .. "' " - fax_uuid = dbh:first_value(sql); - end - end - ---get the domain_uuid using the domain name required for multi-tenant - if domain_name and not domain_uuid then - local sql = "SELECT domain_uuid FROM v_domains "; - sql = sql .. "WHERE domain_name = '" .. domain_name .. "' " - domain_uuid = dbh:first_value(sql) - end - - assert(domain_name and domain_uuid) + assert(fax_uuid, 'no fax server uuid') + assert(domain_name, 'no domain name') + assert(domain_uuid, 'no domain uuid') + assert(domain_uuid:lower() == task.domain_uuid:lower(), 'invalid domain uuid') + assert(domain_name:lower() == task.domain_name:lower(), 'invalid domain name') --settings local settings = Settings.new(dbh, domain_name, domain_uuid) local keep_local = settings:get('fax', 'keep_local','boolean') local storage_type = (keep_local == "false") and "" or settings:get('fax', 'storage_type', 'text') ---be sure accountcode is not empty - if (accountcode == nil) then - accountcode = domain_name - end - local function opt(v, default) if v then return "'" .. v .. "'" end return default or 'NULL' @@ -244,14 +238,6 @@ dbh:query(sql); end ---prepare the headers - local mail_x_headers = { - ["X-FusionPBX-Domain-UUID"] = domain_uuid; - ["X-FusionPBX-Domain-Name"] = domain_name; - ["X-FusionPBX-Call-UUID"] = uuid; - ["X-FusionPBX-Email-Type"] = 'email2fax'; - } - -- add the fax files if fax_success == "1" then @@ -331,16 +317,13 @@ end Tasks.remove_task(task) - - if task.reply_address and #task.reply_address > 0 then - send_mail(mail_x_headers, task.reply_address, { - "Fax to: " .. number_dialed .. " SENT", - table.concat{ - "We are happy to report the fax was sent successfully.", - "It has been attached for your records.", - } - }) - end + Tasks.send_mail_task(task, { + "Fax to: " .. number_dialed .. " SENT", + table.concat{ + "We are happy to report the fax was sent successfully.", + "It has been attached for your records.", + }}, uuid + ) end if fax_success ~= "1" then @@ -362,16 +345,14 @@ Tasks.wait_task(task, answered, hangup_cause_q850) if task.status ~= 0 then Tasks.remove_task(task) - if task.reply_address and #task.reply_address > 0 then - send_mail(mail_x_headers, task.reply_address, { - "Fax to: " .. number_dialed .. " FAILED", - table.concat{ - "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.", - } - }) - end + Tasks.send_mail_task(task, { + "Fax to: " .. number_dialed .. " FAILED", + table.concat{ + "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.", + }}, uuid + ) end end end diff --git a/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua b/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua index dd049fd8ef..e6ba83835f 100644 --- a/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua +++ b/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua @@ -1,5 +1,6 @@ -local Database = require "resources.functions.database" -local Settings = require "resources.functions.lazy_settings" +local Database = require "resources.functions.database" +local Settings = require "resources.functions.lazy_settings" +local send_mail = require "resources.functions.send_mail" local db @@ -244,6 +245,21 @@ local function cleanup_tasks() db:query(remove_finished_tasks_sql) end +local function send_mail_task(task, message, call_uuid) + if not task.reply_address or #task.reply_address == 0 then + return + end + + local mail_x_headers = { + ["X-FusionPBX-Domain-UUID"] = task.domain_uuid; + ["X-FusionPBX-Domain-Name"] = task.domain_name; + ["X-FusionPBX-Call-UUID"] = call_uuid; + ["X-FusionPBX-Email-Type"] = 'email2fax'; + } + + return send_mail(mail_x_headers, task.reply_address, message) +end + return { release_db = function() if db then @@ -251,10 +267,11 @@ return { db = nil end end; - next_task = next_task; - wait_task = wait_task; - select_task = select_task; - remove_task = remove_task; - release_task = release_task; - cleanup_tasks = cleanup_tasks; + next_task = next_task; + wait_task = wait_task; + select_task = select_task; + remove_task = remove_task; + release_task = release_task; + cleanup_tasks = cleanup_tasks; + send_mail_task = send_mail_task; } diff --git a/resources/install/scripts/resources/functions/lazy_settings.lua b/resources/install/scripts/resources/functions/lazy_settings.lua index 3d3b72b342..c6950b4d4e 100644 --- a/resources/install/scripts/resources/functions/lazy_settings.lua +++ b/resources/install/scripts/resources/functions/lazy_settings.lua @@ -99,7 +99,7 @@ function Settings:_load(category, subcategory, name) local found = false --get the domain settings if domain_uuid then - sql = "SELECT domain_setting_uuid,domain_setting_category,domain_setting_subcategory,domain_setting_name,domain_setting_value " + local sql = "SELECT domain_setting_uuid,domain_setting_category,domain_setting_subcategory,domain_setting_name,domain_setting_value " sql = sql .. "FROM v_domain_settings "; sql = sql .. "WHERE domain_uuid = '" .. domain_uuid .. "'"; sql = sql .. "AND domain_setting_enabled = 'true' "; diff --git a/resources/install/scripts/resources/functions/log.lua b/resources/install/scripts/resources/functions/log.lua index b88818447b..eb73e5c13c 100644 --- a/resources/install/scripts/resources/functions/log.lua +++ b/resources/install/scripts/resources/functions/log.lua @@ -46,6 +46,7 @@ local LEVELS = { 'warning', 'notice', 'info', + 'debug', } local TRACE_LEVEL = 'notice'