2019-09-16 08:06:54 +02:00
|
|
|
-- see: mail.md
|
|
|
|
|
2023-05-05 16:05:07 +02:00
|
|
|
-- translation
|
|
|
|
local S = minetest.get_translator("mail")
|
|
|
|
|
2023-02-23 13:41:41 -08:00
|
|
|
local f = string.format
|
|
|
|
|
2019-09-16 08:06:54 +02:00
|
|
|
mail.registered_on_receives = {}
|
|
|
|
function mail.register_on_receive(func)
|
|
|
|
mail.registered_on_receives[#mail.registered_on_receives + 1] = func
|
|
|
|
end
|
|
|
|
|
2024-02-01 20:46:26 +01:00
|
|
|
mail.registered_on_player_receives = {}
|
|
|
|
function mail.register_on_player_receive(func)
|
|
|
|
table.insert(mail.registered_on_player_receives, func)
|
|
|
|
end
|
|
|
|
|
|
|
|
mail.registered_recipient_handlers = {}
|
|
|
|
function mail.register_recipient_handler(func)
|
|
|
|
table.insert(mail.registered_recipient_handlers, func)
|
|
|
|
end
|
|
|
|
|
2023-03-29 17:25:01 +02:00
|
|
|
function mail.send(m)
|
|
|
|
if type(m.from) ~= "string" then return false, "'from' is not a string" end
|
2023-07-19 22:16:27 +02:00
|
|
|
if type(m.to or "") ~= "string" then return false, "'to' is not a string" end
|
|
|
|
if type(m.cc or "") ~= "string" then return false, "'cc' is not a string" end
|
|
|
|
if type(m.bcc or "") ~= "string" then return false, "'bcc' is not a string" end
|
|
|
|
if type(m.subject or "") ~= "string" then return false, "'subject' is not a string" end
|
2023-03-29 17:25:01 +02:00
|
|
|
if type(m.body) ~= "string" then return false, "'body' is not a string" end
|
2020-09-20 20:18:34 +02:00
|
|
|
|
2023-03-29 17:25:01 +02:00
|
|
|
-- defaults
|
|
|
|
m.subject = m.subject or "(No subject)"
|
2020-09-20 20:18:34 +02:00
|
|
|
|
2020-07-31 12:36:28 +02:00
|
|
|
-- normalize to, cc and bcc while compiling a list of all recipients
|
|
|
|
local recipients = {}
|
2023-02-23 13:41:41 -08:00
|
|
|
local undeliverable = {}
|
2024-03-22 22:43:26 +01:00
|
|
|
m.to = mail.concat_player_list(mail.extract_maillists(m.to, m.from))
|
2024-02-01 20:46:26 +01:00
|
|
|
m.to = mail.normalize_players_and_add_recipients(m.from, m.to, recipients, undeliverable)
|
2020-07-31 12:36:28 +02:00
|
|
|
if m.cc then
|
2024-03-22 22:43:26 +01:00
|
|
|
m.cc = mail.concat_player_list(mail.extract_maillists(m.cc, m.from))
|
2024-02-01 20:46:26 +01:00
|
|
|
m.cc = mail.normalize_players_and_add_recipients(mail.from, m.cc, recipients, undeliverable)
|
2020-07-31 12:36:28 +02:00
|
|
|
end
|
|
|
|
if m.bcc then
|
2024-03-22 22:43:26 +01:00
|
|
|
m.bcc = mail.concat_player_list(mail.extract_maillists(m.bcc, m.from))
|
2024-02-01 20:46:26 +01:00
|
|
|
m.bcc = mail.normalize_players_and_add_recipients(m.from, m.bcc, recipients, undeliverable)
|
2020-07-31 12:36:28 +02:00
|
|
|
end
|
2019-09-16 08:06:54 +02:00
|
|
|
|
2023-02-23 13:41:41 -08:00
|
|
|
if next(undeliverable) then -- table is not empty
|
2024-02-01 20:46:26 +01:00
|
|
|
local undeliverable_reason = {S("The mail could not be sent:")}
|
|
|
|
for _, reason in pairs(undeliverable) do
|
|
|
|
table.insert(undeliverable_reason, reason)
|
2023-02-23 13:41:41 -08:00
|
|
|
end
|
2024-02-01 20:46:26 +01:00
|
|
|
return false, table.concat(undeliverable_reason, "\n")
|
2024-03-22 21:59:04 +01:00
|
|
|
elseif not next(recipients) then
|
|
|
|
return false, S("You did not specify any valid recipient.")
|
2023-02-23 13:41:41 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
local extra = {}
|
|
|
|
local extra_log
|
|
|
|
if m.cc then
|
|
|
|
table.insert(extra, "CC: " .. m.cc)
|
|
|
|
end
|
|
|
|
if m.bcc then
|
|
|
|
table.insert(extra, "BCC: " .. m.bcc)
|
|
|
|
end
|
|
|
|
if #extra > 0 then
|
|
|
|
extra_log = f(" (%s)", table.concat(extra, " - "))
|
|
|
|
else
|
|
|
|
extra_log = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.log("action", f("[mail] %q send mail to %q%s with subject %q and body %q",
|
|
|
|
m.from, m.to, extra_log, m.subject, m.body
|
|
|
|
))
|
|
|
|
|
2023-03-31 18:08:51 +02:00
|
|
|
local id
|
2023-03-31 17:14:52 +02:00
|
|
|
if m.id then
|
|
|
|
mail.delete_mail(m.from, m.id)
|
|
|
|
id = m.id
|
|
|
|
end
|
|
|
|
|
2020-07-31 12:36:28 +02:00
|
|
|
-- form the actual mail
|
2020-07-31 19:08:31 +02:00
|
|
|
local msg = {
|
2023-03-31 18:08:51 +02:00
|
|
|
id = id or mail.new_uuid(),
|
2023-03-29 17:25:01 +02:00
|
|
|
from = m.from,
|
|
|
|
to = m.to,
|
|
|
|
cc = m.cc,
|
|
|
|
bcc = m.bcc,
|
2019-09-16 08:15:43 +02:00
|
|
|
subject = m.subject,
|
2023-03-29 17:25:01 +02:00
|
|
|
body = m.body,
|
|
|
|
time = os.time(),
|
2020-07-31 12:36:28 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 17:25:01 +02:00
|
|
|
-- add in senders outbox
|
|
|
|
local entry = mail.get_storage_entry(m.from)
|
|
|
|
table.insert(entry.outbox, 1, msg)
|
|
|
|
mail.set_storage_entry(m.from, entry)
|
2023-12-24 15:44:29 +01:00
|
|
|
msg.spam = #mail.check_spam(msg) >= 1
|
2023-03-29 17:25:01 +02:00
|
|
|
|
|
|
|
-- add in every receivers inbox
|
2024-02-01 20:46:26 +01:00
|
|
|
for _, deliver in pairs(recipients) do
|
|
|
|
deliver(msg)
|
2019-09-16 08:06:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
for i=1, #mail.registered_on_receives do
|
|
|
|
if mail.registered_on_receives[i](m) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-03-29 17:25:01 +02:00
|
|
|
|
|
|
|
return true
|
2019-09-16 08:06:54 +02:00
|
|
|
end
|
2023-03-31 17:14:52 +02:00
|
|
|
|
|
|
|
function mail.save_draft(m)
|
|
|
|
if type(m.from) ~= "string" then return false, "'from' is not a string" end
|
2023-07-19 22:16:27 +02:00
|
|
|
if type(m.to or "") ~= "string" then return false, "'to' is not a string" end
|
|
|
|
if type(m.cc or "") ~= "string" then return false, "'cc' is not a string" end
|
|
|
|
if type(m.bcc or "") ~= "string" then return false, "'bcc' is not a string" end
|
|
|
|
if type(m.subject or "") ~= "string" then return false, "'subject' is not a string" end
|
2023-03-31 17:14:52 +02:00
|
|
|
if type(m.body) ~= "string" then return false, "'body' is not a string" end
|
|
|
|
|
|
|
|
-- defaults
|
|
|
|
m.subject = m.subject or "(No subject)"
|
|
|
|
|
2023-03-31 18:08:51 +02:00
|
|
|
minetest.log("verbose", f("[mail] %q saves draft with subject %q and body %q",
|
2023-03-31 17:14:52 +02:00
|
|
|
m.from, m.subject, m.body
|
|
|
|
))
|
|
|
|
|
|
|
|
-- remove it is an update
|
2023-03-31 18:08:51 +02:00
|
|
|
local id
|
2023-03-31 17:14:52 +02:00
|
|
|
if m.id then
|
|
|
|
mail.delete_mail(m.from, m.id)
|
|
|
|
id = m.id
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add (again ie. update) in sender drafts
|
|
|
|
local entry = mail.get_storage_entry(m.from)
|
|
|
|
table.insert(entry.drafts, 1, {
|
2023-03-31 18:08:51 +02:00
|
|
|
id = id or mail.new_uuid(),
|
2023-03-31 17:14:52 +02:00
|
|
|
from = m.from,
|
|
|
|
to = m.to,
|
|
|
|
cc = m.cc,
|
|
|
|
bcc = m.bcc,
|
|
|
|
subject = m.subject,
|
|
|
|
body = m.body,
|
|
|
|
time = os.time(),
|
|
|
|
})
|
|
|
|
mail.set_storage_entry(m.from, entry)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
end
|