2023-03-29 18:25:01 +03:00
|
|
|
-- storage getter/setter
|
|
|
|
local STORAGE_PREFIX = "mail/"
|
2019-09-16 09:06:54 +03:00
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- create or populate empty fields on an entry
|
|
|
|
local function populate_entry(e)
|
|
|
|
e = e or {}
|
|
|
|
e.contacts = e.contacts or {}
|
|
|
|
e.inbox = e.inbox or {}
|
|
|
|
e.outbox = e.outbox or {}
|
2023-03-31 18:14:52 +03:00
|
|
|
e.drafts = e.drafts or {}
|
2023-03-29 18:25:01 +03:00
|
|
|
e.lists = e.lists or {}
|
|
|
|
return e
|
2019-09-16 09:06:54 +03:00
|
|
|
end
|
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
function mail.get_storage_entry(playername)
|
|
|
|
local str = mail.storage:get_string(STORAGE_PREFIX .. playername)
|
|
|
|
if str == "" then
|
|
|
|
-- new entry
|
|
|
|
return populate_entry()
|
|
|
|
else
|
|
|
|
-- deserialize existing entry
|
|
|
|
local e = minetest.parse_json(str)
|
|
|
|
return populate_entry(e)
|
|
|
|
end
|
2020-08-10 19:29:11 +03:00
|
|
|
end
|
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
function mail.set_storage_entry(playername, entry)
|
|
|
|
mail.storage:set_string(STORAGE_PREFIX .. playername, minetest.write_json(entry))
|
|
|
|
end
|
2020-08-10 19:29:11 +03:00
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- get a mail by id from the players in- or outbox
|
|
|
|
function mail.get_message(playername, msg_id)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for _, msg in ipairs(entry.inbox) do
|
|
|
|
if msg.id == msg_id then
|
|
|
|
return msg
|
2022-08-06 21:23:35 +03:00
|
|
|
end
|
2023-03-29 18:25:01 +03:00
|
|
|
end
|
|
|
|
for _, msg in ipairs(entry.outbox) do
|
|
|
|
if msg.id == msg_id then
|
|
|
|
return msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-06 21:23:35 +03:00
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- marks a mail read by its id
|
|
|
|
function mail.mark_read(playername, msg_id)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for _, msg in ipairs(entry.inbox) do
|
|
|
|
if msg.id == msg_id then
|
|
|
|
msg.read = true
|
|
|
|
mail.set_storage_entry(playername, entry)
|
2023-04-03 17:47:52 +03:00
|
|
|
mail.hud_update(playername, entry.inbox)
|
2023-03-29 18:25:01 +03:00
|
|
|
return
|
|
|
|
end
|
2019-09-16 09:06:54 +03:00
|
|
|
end
|
2023-03-29 18:25:01 +03:00
|
|
|
end
|
2019-09-16 09:06:54 +03:00
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- marks a mail unread by its id
|
|
|
|
function mail.mark_unread(playername, msg_id)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for _, msg in ipairs(entry.inbox) do
|
|
|
|
if msg.id == msg_id then
|
|
|
|
msg.read = false
|
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2019-09-16 09:06:54 +03:00
|
|
|
end
|
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- deletes a mail by its id
|
|
|
|
function mail.delete_mail(playername, msg_id)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for i, msg in ipairs(entry.inbox) do
|
|
|
|
if msg.id == msg_id then
|
2023-04-01 11:38:00 +03:00
|
|
|
table.remove(entry.inbox, i)
|
2023-03-29 18:25:01 +03:00
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for i, msg in ipairs(entry.outbox) do
|
|
|
|
if msg.id == msg_id then
|
|
|
|
table.remove(entry.outbox, i)
|
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2023-03-31 18:14:52 +03:00
|
|
|
for i, msg in ipairs(entry.drafts) do
|
|
|
|
if msg.id == msg_id then
|
|
|
|
table.remove(entry.drafts, i)
|
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2023-03-29 18:25:01 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
-- add or update a contact
|
|
|
|
function mail.update_contact(playername, contact)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
local existing_updated = false
|
|
|
|
for i, existing_contact in ipairs(entry.contacts) do
|
|
|
|
if existing_contact.name == contact.name then
|
|
|
|
-- update
|
|
|
|
entry.contacts[i] = contact
|
|
|
|
existing_updated = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not existing_updated then
|
|
|
|
-- insert
|
|
|
|
table.insert(entry.contacts, contact)
|
|
|
|
end
|
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- deletes a contact
|
|
|
|
function mail.delete_contact(playername, contactname)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for i, existing_contact in ipairs(entry.contacts) do
|
|
|
|
if existing_contact.name == contactname then
|
|
|
|
-- delete
|
|
|
|
table.remove(entry.contacts, i)
|
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
return
|
|
|
|
end
|
2020-08-10 19:29:11 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- get all contacts
|
|
|
|
function mail.get_contacts(playername)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
return entry.contacts
|
|
|
|
end
|
2020-08-10 19:29:11 +03:00
|
|
|
|
2023-03-29 18:25:01 +03:00
|
|
|
-- returns the maillists of a player
|
|
|
|
function mail.get_maillists(playername)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
return entry.lists
|
|
|
|
end
|
|
|
|
|
|
|
|
-- returns the maillists of a player
|
|
|
|
function mail.get_maillist_by_name(playername, listname)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for _, list in ipairs(entry.lists) do
|
|
|
|
if list.name == listname then
|
|
|
|
return list
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- updates or creates a maillist
|
2023-04-02 11:37:58 +03:00
|
|
|
function mail.update_maillist(playername, list, old_list_name)
|
2023-03-29 18:25:01 +03:00
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for i, existing_list in ipairs(entry.lists) do
|
2023-04-02 11:37:58 +03:00
|
|
|
if existing_list.name == old_list_name then
|
|
|
|
-- delete
|
|
|
|
table.remove(entry.lists, i)
|
2023-03-29 18:25:01 +03:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-04-02 11:37:58 +03:00
|
|
|
-- insert
|
|
|
|
table.insert(entry.lists, list)
|
2023-03-29 18:25:01 +03:00
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
end
|
|
|
|
|
|
|
|
function mail.delete_maillist(playername, listname)
|
|
|
|
local entry = mail.get_storage_entry(playername)
|
|
|
|
for i, list in ipairs(entry.lists) do
|
|
|
|
if list.name == listname then
|
|
|
|
-- delete
|
|
|
|
table.remove(entry.lists, i)
|
|
|
|
mail.set_storage_entry(playername, entry)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function mail.extractMaillists(receivers_string, maillists_owner)
|
2023-04-02 11:37:58 +03:00
|
|
|
local receivers = mail.parse_player_list(receivers_string) -- extracted receivers
|
2023-03-29 18:25:01 +03:00
|
|
|
|
|
|
|
-- extract players from mailing lists
|
2023-04-02 11:37:58 +03:00
|
|
|
while string.find(receivers_string, "@") do
|
|
|
|
local globalReceivers = mail.parse_player_list(receivers_string) -- receivers including maillists
|
|
|
|
receivers = {}
|
|
|
|
for _, receiver in ipairs(globalReceivers) do
|
|
|
|
local receiverInfo = receiver:split("@") -- @maillist
|
|
|
|
if receiverInfo[1] and receiver == "@" .. receiverInfo[1] then
|
|
|
|
local maillist = mail.get_maillist_by_name(maillists_owner, receiverInfo[1])
|
|
|
|
if maillist then
|
|
|
|
for _, playername in ipairs(maillist.players) do
|
|
|
|
table.insert(receivers, playername)
|
|
|
|
end
|
2023-03-29 18:25:01 +03:00
|
|
|
end
|
2023-04-02 11:37:58 +03:00
|
|
|
else -- in case of player
|
|
|
|
table.insert(receivers, receiver)
|
2023-03-29 18:25:01 +03:00
|
|
|
end
|
|
|
|
end
|
2023-04-02 11:37:58 +03:00
|
|
|
receivers_string = mail.concat_player_list(receivers)
|
2023-03-29 18:25:01 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
return receivers
|
2020-08-10 19:29:11 +03:00
|
|
|
end
|
|
|
|
|
2020-10-13 20:20:18 +03:00
|
|
|
function mail.pairsByKeys(t, f)
|
2020-08-15 14:52:26 +03:00
|
|
|
-- http://www.lua.org/pil/19.3.html
|
|
|
|
local a = {}
|
|
|
|
for n in pairs(t) do table.insert(a, n) end
|
|
|
|
table.sort(a, f)
|
|
|
|
local i = 0 -- iterator variable
|
|
|
|
local iter = function() -- iterator function
|
|
|
|
i = i + 1
|
|
|
|
if a[i] == nil then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
--return a[i], t[a[i]]
|
|
|
|
-- add the current position and the length for convenience
|
|
|
|
return a[i], t[a[i]], i, #a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return iter
|
|
|
|
end
|
|
|
|
|