mail/api.lua
Peter Nerlich ea0de708da fix some things, adjust GUI elements
- messages are now actually sent (bug in parse player list)
- no more crashes on sending mail (forgot to make variables local)
- actually handle CC and BCC fields instead of leaving them empty, duh
- make new functions be under the mail namespace
- add util functions to ensure the new format, parse and player list as well as checking whether a player is in that list or not
- rearrange some GUI elements (tighter spacing, grouping, increase window height to be consistent)
- convert mails to new format only as needed (old mails stay intact in case someone reverts to old version)
- mails are shaded differently in inbox, depending on whether the player is in the TO field
- FROM, TO and CC fields are all displayed when reading a mail
- add "Reply All" button (TO includes all original recipients plus the sender, but excluding the player himself, while adopting the CC field. To contrast: "Reply" just sets the original sender as TO and leaves the rest empty)
- move reply, replyall and forward to their own functions in GUI (was duplicated for inbox and show mail)
- don't needlessly set messages table when we do nothing but go back
2020-08-10 12:15:34 +02:00

106 lines
2.5 KiB
Lua

-- see: mail.md
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
--]]
function mail.send(src, dst, subject, body)
-- figure out format
local m
if dst == nil and subject == nil and body == nil then
-- new format (one object param)
m = src
else
-- old format
m = {}
m.from = src
m.to = dst
m.subject = subject
m.body = body
end
local cc
local bcc
local extra
-- log mail send action
if m.cc or m.bcc then
if m.cc then
cc = "CC: " .. m.cc
if m.bcc then
cc = cc .. " - "
end
else
cc = ""
end
if m.bcc then
bcc = "BCC: " .. m.bcc
else
bcc = ""
end
extra = " (" .. cc .. bcc .. ")"
else
extra = ""
end
minetest.log("action", "[mail] '" .. m.from .. "' sends mail to '" .. m.to .. "'" ..
extra .. "' with subject '" .. m.subject .. "' and body: '" .. m.body .. "'")
-- normalize to, cc and bcc while compiling a list of all recipients
local recipients = {}
m.to = mail.normalize_players_and_add_recipients(m.to, recipients)
if m.cc then
m.cc = mail.normalize_players_and_add_recipients(m.cc, recipients)
end
if m.bcc then
m.bcc = mail.normalize_players_and_add_recipients(m.bcc, recipients)
end
-- form the actual mail
local msg = {
unread = true,
from = m.from,
to = m.to,
subject = m.subject,
body = m.body,
time = os.time(),
}
if m.cc then
msg.cc = m.cc
end
-- 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
-- notify recipients that happen to be online
for _, player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if recipients[string.lower(name)] ~= nil then
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
minetest.chat_send_player(name,
string.format(mail.receive_mail_message, m.from, m.subject))
end
end
for i=1, #mail.registered_on_receives do
if mail.registered_on_receives[i](m) then
break
end
end
end