2019-09-16 09:06:54 +03:00
|
|
|
-- see: mail.md
|
|
|
|
|
2023-05-05 17:05:07 +03:00
|
|
|
-- translation
|
|
|
|
local S = minetest.get_translator("mail")
|
|
|
|
|
2023-02-24 00:41:41 +03:00
|
|
|
local f = string.format
|
|
|
|
|
2019-09-16 09:06:54 +03:00
|
|
|
mail.registered_on_receives = {}
|
|
|
|
function mail.register_on_receive(func)
|
|
|
|
mail.registered_on_receives[#mail.registered_on_receives + 1] = func
|
|
|
|
end
|
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
function mail.send(m)
|
|
|
|
if type(m.from) ~= "string" then return false, "'from' is not a string" end
|
|
|
|
if type(m.to) ~= "string" then return false, "'to' is not a string" end
|
|
|
|
if type(m.body) ~= "string" then return false, "'body' is not a string" end
|
2020-09-20 21:18:34 +03:00
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- defaults
|
|
|
|
m.subject = m.subject or "(No subject)"
|
2020-09-20 21:18:34 +03:00
|
|
|
|
2020-07-31 13:36:28 +03:00
|
|
|
-- normalize to, cc and bcc while compiling a list of all recipients
|
|
|
|
local recipients = {}
|
2023-02-24 00:41:41 +03:00
|
|
|
local undeliverable = {}
|
2023-03-29 18:25:01 +03:00
|
|
|
m.to = mail.concat_player_list(mail.extractMaillists(m.to, m.from))
|
2023-02-24 00:41:41 +03:00
|
|
|
m.to = mail.normalize_players_and_add_recipients(m.to, recipients, undeliverable)
|
2020-07-31 13:36:28 +03:00
|
|
|
if m.cc then
|
2023-03-29 18:25:01 +03:00
|
|
|
m.cc = mail.concat_player_list(mail.extractMaillists(m.cc, m.from))
|
2023-02-24 00:41:41 +03:00
|
|
|
m.cc = mail.normalize_players_and_add_recipients(m.cc, recipients, undeliverable)
|
2020-07-31 13:36:28 +03:00
|
|
|
end
|
|
|
|
if m.bcc then
|
2023-03-29 18:25:01 +03:00
|
|
|
m.bcc = mail.concat_player_list(mail.extractMaillists(m.bcc, m.from))
|
2023-02-24 00:41:41 +03:00
|
|
|
m.bcc = mail.normalize_players_and_add_recipients(m.bcc, recipients, undeliverable)
|
2020-07-31 13:36:28 +03:00
|
|
|
end
|
2019-09-16 09:06:54 +03:00
|
|
|
|
2023-02-24 00:41:41 +03: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
|
2023-03-29 18:25:01 +03:00
|
|
|
return false, f("recipients %s don't exist; cannot send mail.", table.concat(undeliverable_names, ", "))
|
2023-02-24 00:41:41 +03: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 19:08:51 +03:00
|
|
|
local id
|
2023-03-31 18:14:52 +03:00
|
|
|
if m.id then
|
|
|
|
mail.delete_mail(m.from, m.id)
|
|
|
|
id = m.id
|
|
|
|
end
|
|
|
|
|
2020-07-31 13:36:28 +03:00
|
|
|
-- form the actual mail
|
2020-07-31 20:08:31 +03:00
|
|
|
local msg = {
|
2023-03-31 19:08:51 +03:00
|
|
|
id = id or mail.new_uuid(),
|
2023-03-29 18:25:01 +03:00
|
|
|
from = m.from,
|
|
|
|
to = m.to,
|
|
|
|
cc = m.cc,
|
|
|
|
bcc = m.bcc,
|
2019-09-16 09:15:43 +03:00
|
|
|
subject = m.subject,
|
2023-03-29 18:25:01 +03:00
|
|
|
body = m.body,
|
|
|
|
time = os.time(),
|
2020-07-31 13:36:28 +03:00
|
|
|
}
|
|
|
|
|
2023-03-29 18:25:01 +03: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)
|
|
|
|
|
|
|
|
-- add in every receivers inbox
|
2023-02-24 00:41:41 +03:00
|
|
|
for recipient in pairs(recipients) do
|
2023-03-29 18:25:01 +03:00
|
|
|
entry = mail.get_storage_entry(recipient)
|
|
|
|
table.insert(entry.inbox, msg)
|
|
|
|
mail.set_storage_entry(recipient, entry)
|
2020-07-31 13:36:28 +03:00
|
|
|
end
|
2019-09-16 09:06:54 +03:00
|
|
|
|
2020-07-31 13:36:28 +03:00
|
|
|
-- notify recipients that happen to be online
|
2023-05-05 17:05:07 +03:00
|
|
|
local mail_alert = S("You have a new message from @1! Subject: @2", m.from, m.subject) ..
|
|
|
|
"\n" .. S("To view it, type /mail")
|
|
|
|
local unified_inventory_alert = S("You could also use the button in your inventory.")
|
2019-09-16 09:06:54 +03:00
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
|
|
|
local name = player:get_player_name()
|
2023-02-24 00:41:41 +03:00
|
|
|
if recipients[name] then
|
2023-05-05 12:38:19 +03:00
|
|
|
if mail.get_setting(name, "chat_notifications") == true then
|
|
|
|
minetest.chat_send_player(name, mail_alert)
|
2023-05-05 17:05:07 +03:00
|
|
|
if minetest.get_modpath("unified_inventory") then
|
|
|
|
minetest.chat_send_player(name, unified_inventory_alert)
|
|
|
|
end
|
2023-05-05 12:38:19 +03:00
|
|
|
end
|
2023-05-06 22:21:20 +03:00
|
|
|
if mail.get_setting(name, "sound_notifications") == true then
|
|
|
|
minetest.sound_play("mail_notif", {to_player=name})
|
|
|
|
end
|
2023-04-01 20:14:53 +03:00
|
|
|
local receiver_entry = mail.get_storage_entry(name)
|
|
|
|
local receiver_messages = receiver_entry.inbox
|
|
|
|
mail.hud_update(name, receiver_messages)
|
2019-09-16 09:06:54 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1, #mail.registered_on_receives do
|
|
|
|
if mail.registered_on_receives[i](m) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-03-29 18:25:01 +03:00
|
|
|
|
|
|
|
return true
|
2019-09-16 09:06:54 +03:00
|
|
|
end
|
2023-03-31 18:14:52 +03:00
|
|
|
|
|
|
|
function mail.save_draft(m)
|
|
|
|
if type(m.from) ~= "string" then return false, "'from' is not a string" end
|
|
|
|
if type(m.to) ~= "string" then return false, "'to' is not a string" end
|
|
|
|
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 19:08:51 +03:00
|
|
|
minetest.log("verbose", f("[mail] %q saves draft with subject %q and body %q",
|
2023-03-31 18:14:52 +03:00
|
|
|
m.from, m.subject, m.body
|
|
|
|
))
|
|
|
|
|
|
|
|
-- remove it is an update
|
2023-03-31 19:08:51 +03:00
|
|
|
local id
|
2023-03-31 18:14:52 +03: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 19:08:51 +03:00
|
|
|
id = id or mail.new_uuid(),
|
2023-03-31 18:14:52 +03: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
|