2019-09-16 08:06:54 +02:00
|
|
|
-- 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
|
2020-09-20 20:18:34 +02:00
|
|
|
|
|
|
|
TODO: refactor this garbage code!
|
2019-09-16 08:06:54 +02:00
|
|
|
--]]
|
2019-09-16 08:15:43 +02:00
|
|
|
function mail.send(src, dst, subject, body)
|
2020-07-31 12:36:28 +02:00
|
|
|
-- figure out format
|
2019-09-16 08:06:54 +02:00
|
|
|
local m
|
2019-09-16 08:15:43 +02:00
|
|
|
if dst == nil and subject == nil and body == nil then
|
2019-09-16 08:06:54 +02:00
|
|
|
-- new format (one object param)
|
2019-09-16 08:15:43 +02:00
|
|
|
m = src
|
2019-09-16 08:06:54 +02:00
|
|
|
else
|
|
|
|
-- old format
|
|
|
|
m = {}
|
2020-07-31 12:36:28 +02:00
|
|
|
m.from = src
|
|
|
|
m.to = dst
|
2019-09-16 08:06:54 +02:00
|
|
|
m.subject = subject
|
|
|
|
m.body = body
|
|
|
|
end
|
|
|
|
|
2020-09-19 13:22:33 +02:00
|
|
|
if m.dst and not m.to then
|
|
|
|
-- populate "to" field
|
|
|
|
m.to = m.dst
|
|
|
|
end
|
|
|
|
|
2020-09-20 20:18:34 +02:00
|
|
|
if m.src and not m.from then
|
|
|
|
-- populate "from" field
|
|
|
|
m.from = m.src
|
|
|
|
end
|
|
|
|
|
|
|
|
-- sane default values
|
|
|
|
m.subject = m.subject or ""
|
|
|
|
m.body = m.body or ""
|
|
|
|
|
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 11:43:10 +02:00
|
|
|
local cc
|
|
|
|
local bcc
|
|
|
|
local extra
|
2020-07-31 12:36:28 +02:00
|
|
|
-- 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
|
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 11:43:10 +02:00
|
|
|
extra = " (" .. cc .. bcc .. ")"
|
2020-07-31 12:36:28 +02:00
|
|
|
else
|
|
|
|
extra = ""
|
|
|
|
end
|
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 11:43:10 +02:00
|
|
|
minetest.log("action", "[mail] '" .. m.from .. "' sends mail to '" .. m.to .. "'" ..
|
2020-07-31 12:36:28 +02:00
|
|
|
extra .. "' with subject '" .. m.subject .. "' and body: '" .. m.body .. "'")
|
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 = {}
|
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 11:43:10 +02:00
|
|
|
m.to = mail.normalize_players_and_add_recipients(m.to, recipients)
|
2020-07-31 12:36:28 +02:00
|
|
|
if m.cc then
|
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 11:43:10 +02:00
|
|
|
m.cc = mail.normalize_players_and_add_recipients(m.cc, recipients)
|
2020-07-31 12:36:28 +02:00
|
|
|
end
|
|
|
|
if m.bcc then
|
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 11:43:10 +02:00
|
|
|
m.bcc = mail.normalize_players_and_add_recipients(m.bcc, recipients)
|
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
|
|
|
-- 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,
|
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
|
|
|
}
|
|
|
|
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
|
2019-09-16 08:06:54 +02:00
|
|
|
|
2020-07-31 12:36:28 +02:00
|
|
|
-- notify recipients that happen to be online
|
2019-09-16 08:06:54 +02:00
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
|
|
|
local name = player:get_player_name()
|
2020-07-31 12:36:28 +02:00
|
|
|
if recipients[string.lower(name)] ~= nil then
|
2019-09-16 08:06:54 +02: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) .. "..."
|
|
|
|
end
|
2020-07-31 12:36:28 +02:00
|
|
|
minetest.chat_send_player(name,
|
|
|
|
string.format(mail.receive_mail_message, m.from, m.subject))
|
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
|