mail/api.lua

119 lines
2.9 KiB
Lua
Raw Normal View History

2019-09-16 08:06:54 +02:00
-- see: mail.md
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
--]]
function mail.send(...)
-- figure out format
2019-09-16 08:06:54 +02:00
local m
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 = {}
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 ""
if m.subject == "" then
m.subject = "(No subject)"
end
if string.len(m.subject) > 30 then
m.subject = string.sub(m.subject,1,27) .. "..."
end
2019-09-16 08:06:54 +02:00
-- normalize to, cc and bcc while compiling a list of all recipients
local recipients = {}
local undeliverable = {}
m.to = mail.normalize_players_and_add_recipients(m.to, recipients, undeliverable)
if m.cc then
m.cc = mail.normalize_players_and_add_recipients(m.cc, recipients, undeliverable)
end
if m.bcc then
m.bcc = mail.normalize_players_and_add_recipients(m.bcc, recipients, undeliverable)
end
2019-09-16 08:06:54 +02: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
))
-- form the actual mail
2020-07-31 19:08:31 +02:00
local msg = {
2019-09-16 08:15:43 +02:00
unread = true,
sender = m.from,
to = m.to,
cc = m.cc,
2019-09-16 08:15:43 +02:00
subject = m.subject,
body = m.body,
time = os.time(),
}
-- send the mail to all recipients
for recipient in pairs(recipients) do
local messages = mail.getMessages(recipient)
table.insert(messages, 1, msg)
mail.setMessages(recipient, messages)
end
2019-09-16 08:06:54 +02:00
-- notify recipients that happen to be online
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()
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