Take Two: Fix bug: An email is not sent if the Mailbox has “Local After Email” = FALSE

This commit is contained in:
Mark Crane 2013-01-12 02:12:34 +00:00
parent deb2a2afa5
commit 115e8cd835
1 changed files with 665 additions and 647 deletions

View File

@ -780,85 +780,6 @@
end
end
--define a function to send email
function send_email(id, uuid)
--get voicemail message details
sql = [[SELECT * FROM v_voicemails
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND voicemail_id = ']] .. id ..[[']]
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
end
status = dbh:query(sql, function(row)
--voicemail_uuid = string.lower(row["voicemail_uuid"]);
--voicemail_password = row["voicemail_password"];
--greeting_id = row["greeting_id"];
voicemail_mail_to = row["voicemail_mail_to"];
voicemail_attach_file = row["voicemail_attach_file"];
voicemail_local_after_email = row["voicemail_local_after_email"];
end);
--get voicemail message details
sql = [[SELECT * FROM v_voicemail_messages
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND voicemail_message_uuid = ']] .. uuid ..[[']]
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
end
status = dbh:query(sql, function(row)
--get the values from the database
--uuid = row["voicemail_message_uuid"];
created_epoch = row["created_epoch"];
caller_id_name = row["caller_id_name"];
caller_id_number = row["caller_id_number"];
message_length = row["message_length"];
--message_status = row["message_status"];
--message_priority = row["message_priority"];
end);
--calculate the message length
message_length_formatted = format_seconds(message_length);
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] message length: " .. message_length .. "\n");
end
--send the email
subject = [[Voicemail from ]]..caller_id_name..[[ <]]..caller_id_number..[[> ]]..message_length_formatted;
local message = {}
table.insert(message, [[<font face="arial">]]);
table.insert(message, [[<b>Message From "]]..caller_id_name..[[" <A HREF="tel:]]..caller_id_number..[[">]]..caller_id_number..[[</A></b><br>]]);
table.insert(message, [[<hr noshade="noshade" size="1">]]);
table.insert(message, [[Created: ]]..os.date("%A, %d %b %Y %I:%M %p", created_epoch)..[[<br>]]);
table.insert(message, [[Duration: ]]..message_length_formatted..[[<br>]]);
table.insert(message, [[Account: ]]..id..[[@]]..domain_name..[[<br>]]);
table.insert(message, [[</font>]]);
body = table.concat(message, "");
body = body:gsub("'", "&#39;");
body = body:gsub([["]], "&#34;");
if (voicemail_attach_file == "true") then
file = voicemail_dir.."/"..id.."/msg_"..uuid.."."..vm_message_ext;
cmd = "luarun email.lua "..voicemail_mail_to.." "..voicemail_mail_to.." '"..subject.."' '"..body.."' '"..file.."'";
else
cmd = "luarun email.lua "..voicemail_mail_to.." "..voicemail_mail_to.." '"..subject.."' '"..body.."'";
end
api = freeswitch.API();
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] cmd: " .. cmd .. "\n");
end
result = api:executeString(cmd);
--local after email is false so delete the recording file
if (voicemail_local_after_email == "false") then
os.remove(voicemail_dir.."/"..id.."/msg_"..uuid.."."..vm_message_ext);
end
--emailed
if (session:ready()) then
dtmf_digits = '';
macro(session, "emailed", 1, 1000, '');
end
end
--define a function to forward a message to an extension
function forward_to_extension(uuid)
@ -975,82 +896,7 @@
end
end
--leave a voicemail
if (voicemail_action == "save") then
--voicemail prompt
if (skip_greeting == "true") then
--skip the greeting
else
if (session:ready()) then
if (string.len(greeting_id) > 0) then
--play the greeting
session:streamFile(voicemail_dir.."/"..voicemail_id.."/greeting_"..greeting_id..".wav");
else
--if there is no greeting then play digits of the voicemail_id
dtmf_digits = '';
result = macro(session, "person_not_available_record_message", 1, 100);
end
end
end
--save the recording
timeouts = 0;
record_message();
--save the message to the voicemail messages
if (message_length > 2) then
local sql = {}
table.insert(sql, "INSERT INTO v_voicemail_messages ");
table.insert(sql, "(");
table.insert(sql, "voicemail_message_uuid, ");
table.insert(sql, "domain_uuid, ");
table.insert(sql, "voicemail_uuid, ");
table.insert(sql, "created_epoch, ");
table.insert(sql, "caller_id_name, ");
table.insert(sql, "caller_id_number, ");
table.insert(sql, "message_length ");
--table.insert(sql, "message_status, ");
--table.insert(sql, "message_priority, ");
table.insert(sql, ") ");
table.insert(sql, "VALUES ");
table.insert(sql, "( ");
table.insert(sql, "'".. uuid .."', ");
table.insert(sql, "'".. domain_uuid .."', ");
table.insert(sql, "'".. voicemail_uuid .."', ");
table.insert(sql, "'".. start_epoch .."', ");
table.insert(sql, "'".. caller_id_name .."', ");
table.insert(sql, "'".. caller_id_number .."', ");
table.insert(sql, "'".. message_length .."' ");
--table.insert(sql, "'".. message_status .."', ");
--table.insert(sql, "'".. message_priority .."' ");
table.insert(sql, ") ");
if (voicemail_local_after_email == "true") then
sql = table.concat(sql, "\n");
end
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
end
dbh:query(sql);
end
--set the message waiting event
if (message_length > 2) then
local event = freeswitch.Event("message_waiting");
event:addHeader("MWI-Messages-Waiting", "yes");
event:addHeader("MWI-Message-Account", "sip:"..voicemail_id.."@"..domain_name);
event:fire();
end
--send the email with the voicemail recording attached
if (message_length > 2) then
if (string.len(voicemail_mail_to) > 3) then
send_email(voicemail_id, uuid);
end
end
end
--define function main menu
function main_menu ()
if (voicemail_uuid) then
--clear the value
@ -1141,6 +987,7 @@ function main_menu ()
end
end
--define function to listen to the recording
function listen_to_recording (message_number, uuid, created_epoch, caller_id_name, caller_id_number)
--set default values
@ -1291,6 +1138,99 @@ end
end
end
--define a function to send email
function send_email(id, uuid)
--get voicemail message details
sql = [[SELECT * FROM v_voicemails
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND voicemail_id = ']] .. id ..[[']]
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
end
status = dbh:query(sql, function(row)
--voicemail_uuid = string.lower(row["voicemail_uuid"]);
--voicemail_password = row["voicemail_password"];
--greeting_id = row["greeting_id"];
voicemail_mail_to = row["voicemail_mail_to"];
voicemail_attach_file = row["voicemail_attach_file"];
voicemail_local_after_email = row["voicemail_local_after_email"];
end);
--get voicemail message details
sql = [[SELECT * FROM v_voicemail_messages
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND voicemail_message_uuid = ']] .. uuid ..[[']]
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
end
status = dbh:query(sql, function(row)
--get the values from the database
--uuid = row["voicemail_message_uuid"];
created_epoch = row["created_epoch"];
caller_id_name = row["caller_id_name"];
caller_id_number = row["caller_id_number"];
message_length = row["message_length"];
--message_status = row["message_status"];
--message_priority = row["message_priority"];
end);
--format the message length
message_length_formatted = format_seconds(message_length);
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] message length: " .. message_length .. "\n");
end
--send the email
subject = [[Voicemail from ]]..caller_id_name..[[ <]]..caller_id_number..[[> ]]..message_length_formatted;
local message = {}
table.insert(message, [[<font face="arial">]]);
table.insert(message, [[<b>Message From "]]..caller_id_name..[[" <A HREF="tel:]]..caller_id_number..[[">]]..caller_id_number..[[</A></b><br>]]);
table.insert(message, [[<hr noshade="noshade" size="1">]]);
table.insert(message, [[Created: ]]..os.date("%A, %d %b %Y %I:%M %p", created_epoch)..[[<br>]]);
table.insert(message, [[Duration: ]]..message_length_formatted..[[<br>]]);
table.insert(message, [[Account: ]]..id..[[@]]..domain_name..[[<br>]]);
table.insert(message, [[</font>]]);
body = table.concat(message, "");
body = body:gsub("'", "&#39;");
body = body:gsub([["]], "&#34;");
if (voicemail_attach_file == "true") then
if (voicemail_local_after_email == "false") then
delete = "true";
else
delete = "false";
end
file = voicemail_dir.."/"..id.."/msg_"..uuid.."."..vm_message_ext;
cmd = "luarun email.lua "..voicemail_mail_to.." "..voicemail_mail_to.." '"..subject.."' '"..body.."' '"..file.."' "..delete;
else
cmd = "luarun email.lua "..voicemail_mail_to.." "..voicemail_mail_to.." '"..subject.."' '"..body.."'";
end
api = freeswitch.API();
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] cmd: " .. cmd .. "\n");
end
result = api:executeString(cmd);
--whether to keep the voicemail message and details local after email
if (voicemail_local_after_email == "false") then
--delete the voicemail message details
sql = [[DELETE FROM v_voicemail_messages
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND voicemail_message_uuid = ']] .. uuid ..[[']]
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
end
status = dbh:query(sql);
--set message waiting indicator
message_waiting(id);
end
--emailed
if (session:ready()) then
dtmf_digits = '';
macro(session, "emailed", 1, 1000, '');
end
end
--delete the recording
function delete_recording(uuid)
if (session:ready()) then
@ -1352,6 +1292,7 @@ end
end
end
--define function for messages menu
function menu_messages (message_status)
--set default values
@ -1437,6 +1378,7 @@ function menu_messages (message_status)
end
end
--define a function for the advanced menu
function advanced ()
--clear the dtmf
dtmf_digits = '';
@ -1502,6 +1444,7 @@ function advanced ()
end
end
--define a function to record the greeting
function record_greeting()
--flush dtmf digits from the input buffer
@ -1573,6 +1516,7 @@ function record_greeting()
end
end
--define a function to choose the greeting
function choose_greeting()
--flush dtmf digits from the input buffer
@ -1658,6 +1602,7 @@ function choose_greeting()
end
--define a function to record the name
function record_name()
if (session:ready()) then
@ -1700,6 +1645,79 @@ end
end
end
--leave a voicemail
if (voicemail_action == "save") then
--voicemail prompt
if (skip_greeting == "true") then
--skip the greeting
else
if (session:ready()) then
if (string.len(greeting_id) > 0) then
--play the greeting
session:streamFile(voicemail_dir.."/"..voicemail_id.."/greeting_"..greeting_id..".wav");
else
--if there is no greeting then play digits of the voicemail_id
dtmf_digits = '';
result = macro(session, "person_not_available_record_message", 1, 100);
end
end
end
--save the recording
timeouts = 0;
record_message();
--save the message to the voicemail messages
if (message_length > 2) then
local sql = {}
table.insert(sql, "INSERT INTO v_voicemail_messages ");
table.insert(sql, "(");
table.insert(sql, "voicemail_message_uuid, ");
table.insert(sql, "domain_uuid, ");
table.insert(sql, "voicemail_uuid, ");
table.insert(sql, "created_epoch, ");
table.insert(sql, "caller_id_name, ");
table.insert(sql, "caller_id_number, ");
table.insert(sql, "message_length ");
--table.insert(sql, "message_status, ");
--table.insert(sql, "message_priority, ");
table.insert(sql, ") ");
table.insert(sql, "VALUES ");
table.insert(sql, "( ");
table.insert(sql, "'".. uuid .."', ");
table.insert(sql, "'".. domain_uuid .."', ");
table.insert(sql, "'".. voicemail_uuid .."', ");
table.insert(sql, "'".. start_epoch .."', ");
table.insert(sql, "'".. caller_id_name .."', ");
table.insert(sql, "'".. caller_id_number .."', ");
table.insert(sql, "'".. message_length .."' ");
--table.insert(sql, "'".. message_status .."', ");
--table.insert(sql, "'".. message_priority .."' ");
table.insert(sql, ") ");
sql = table.concat(sql, "\n");
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
end
dbh:query(sql);
end
--set the message waiting event
if (message_length > 2) then
local event = freeswitch.Event("message_waiting");
event:addHeader("MWI-Messages-Waiting", "yes");
event:addHeader("MWI-Message-Account", "sip:"..voicemail_id.."@"..domain_name);
event:fire();
end
--send the email with the voicemail recording attached
if (message_length > 2) then
if (string.len(voicemail_mail_to) > 3) then
send_email(voicemail_id, uuid);
end
end
end
--notes
--record the video
--records audio only