2019-09-16 08:06:54 +02:00
|
|
|
-- see: mail.md
|
|
|
|
|
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
|
|
|
|
|
|
|
|
mail.receive_mail_message = "You have a new message from %s! Subject: %s\nTo view it, type /mail"
|
|
|
|
mail.read_later_message = "You can read your messages later by using the /mail command"
|
|
|
|
|
|
|
|
--[[
|
|
|
|
mail sending function, can be invoked with one object argument (new api) or
|
|
|
|
all 4 parameters (old compat version)
|
|
|
|
see: "Mail format" api.md
|
2020-09-20 20:18:34 +02:00
|
|
|
|
|
|
|
TODO: refactor this garbage code!
|
2019-09-16 08:06:54 +02:00
|
|
|
--]]
|
2023-02-23 13:41:41 -08:00
|
|
|
function mail.send(...)
|
2020-07-31 12:36:28 +02:00
|
|
|
-- figure out format
|
2019-09-16 08:06:54 +02:00
|
|
|
local m
|
2023-02-23 13:41:41 -08:00
|
|
|
if #{...} == 1 then
|
|
|
|
-- new format (one table param)
|
|
|
|
m = ...
|
|
|
|
-- populate "to" field
|
|
|
|
m.to = m.to or m.dst
|
|
|
|
-- populate "from" field
|
|
|
|
m.from = m.from or m.src
|
2019-09-16 08:06:54 +02:00
|
|
|
else
|
|
|
|
-- old format
|
|
|
|
m = {}
|
2023-02-23 13:41:41 -08:00
|
|
|
m.from, m.to, m.subject, m.body = ...
|
2020-09-20 20:18:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- sane default values
|
|
|
|
m.subject = m.subject or ""
|
|
|
|
m.body = m.body or ""
|
|
|
|
|
2023-02-23 13:41:41 -08:00
|
|
|
if m.subject == "" then
|
|
|
|
m.subject = "(No subject)"
|
|
|
|
end
|
|
|
|
if string.len(m.subject) > 30 then
|
|
|
|
m.subject = string.sub(m.subject,1,27) .. "..."
|
2020-07-31 12:36:28 +02:00
|
|
|
end
|
2019-09-16 08:06:54 +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 = {}
|
|
|
|
m.to = mail.normalize_players_and_add_recipients(m.to, recipients, undeliverable)
|
2020-07-31 12:36:28 +02:00
|
|
|
if m.cc then
|
2023-02-23 13:41:41 -08:00
|
|
|
m.cc = mail.normalize_players_and_add_recipients(m.cc, recipients, undeliverable)
|
2020-07-31 12:36:28 +02:00
|
|
|
end
|
|
|
|
if m.bcc then
|
2023-02-23 13:41:41 -08:00
|
|
|
m.bcc = mail.normalize_players_and_add_recipients(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
|
|
|
|
local undeliverable_names = {}
|
|
|
|
for name in pairs(undeliverable) do
|
|
|
|
undeliverable_names[#undeliverable_names + 1] = '"' .. name .. '"'
|
|
|
|
end
|
|
|
|
return f("recipients %s don't exist; cannot send mail.",
|
|
|
|
table.concat(undeliverable_names, ", ")
|
|
|
|
)
|
|
|
|
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
|
|
|
|
))
|
|
|
|
|
2020-07-31 12:36:28 +02:00
|
|
|
-- form the actual mail
|
2020-07-31 19:08:31 +02:00
|
|
|
local msg = {
|
2019-09-16 08:15:43 +02:00
|
|
|
unread = true,
|
2020-08-12 11:51:01 +02:00
|
|
|
sender = m.from,
|
2020-07-31 12:36:28 +02:00
|
|
|
to = m.to,
|
2023-02-23 13:41:41 -08:00
|
|
|
cc = m.cc,
|
2019-09-16 08:15:43 +02:00
|
|
|
subject = m.subject,
|
|
|
|
body = m.body,
|
|
|
|
time = os.time(),
|
2020-07-31 12:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
-- send the mail to all recipients
|
2023-02-23 13:41:41 -08:00
|
|
|
for recipient in pairs(recipients) do
|
2020-07-31 12:36:28 +02:00
|
|
|
local messages = mail.getMessages(recipient)
|
|
|
|
table.insert(messages, 1, msg)
|
|
|
|
mail.setMessages(recipient, messages)
|
|
|
|
end
|
2019-09-16 08:06:54 +02:00
|
|
|
|
2020-07-31 12:36:28 +02:00
|
|
|
-- notify recipients that happen to be online
|
2023-02-23 13:41:41 -08:00
|
|
|
local mail_alert = f(mail.receive_mail_message, m.from, m.subject)
|
2019-09-16 08:06:54 +02:00
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
|
|
|
local name = player:get_player_name()
|
2023-02-23 13:41:41 -08:00
|
|
|
if recipients[name] then
|
|
|
|
minetest.chat_send_player(name, mail_alert)
|
2019-09-16 08:06:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1, #mail.registered_on_receives do
|
|
|
|
if mail.registered_on_receives[i](m) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|