2019-09-16 09:06:54 +03:00
|
|
|
|
|
|
|
-- TODO: maybe local cache?
|
|
|
|
|
2020-08-10 19:29:11 +03:00
|
|
|
function mail.getMailFile(playername)
|
2019-09-16 09:06:54 +03:00
|
|
|
local saneplayername = string.gsub(playername, "[.|/]", "")
|
|
|
|
return mail.maildir .. "/" .. saneplayername .. ".json"
|
|
|
|
end
|
|
|
|
|
2020-08-10 19:29:11 +03:00
|
|
|
function mail.getContactsFile(playername)
|
|
|
|
local saneplayername = string.gsub(playername, "[.|/]", "")
|
|
|
|
return mail.maildir .. "/contacts/" .. saneplayername .. ".json"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-09-16 09:06:54 +03:00
|
|
|
mail.getMessages = function(playername)
|
2020-08-10 19:29:11 +03:00
|
|
|
local messages = mail.read_json_file(mail.getMailFile(playername))
|
|
|
|
if messages then
|
2019-09-16 09:06:54 +03:00
|
|
|
mail.hud_update(playername, messages)
|
|
|
|
end
|
|
|
|
|
|
|
|
return messages
|
|
|
|
end
|
|
|
|
|
|
|
|
mail.setMessages = function(playername, messages)
|
2020-08-10 19:29:11 +03:00
|
|
|
if mail.write_json_file(mail.getMailFile(playername), messages) then
|
2019-09-16 09:06:54 +03:00
|
|
|
mail.hud_update(playername, messages)
|
|
|
|
return true
|
|
|
|
else
|
2020-08-10 19:29:11 +03:00
|
|
|
minetest.log("error","[mail] Save failed - messages may be lost! ("..playername..")")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
mail.getContacts = function(playername)
|
|
|
|
return mail.read_json_file(mail.getContactsFile(playername))
|
|
|
|
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
|
|
|
|
|
2020-08-10 19:29:11 +03:00
|
|
|
mail.setContacts = function(playername, contacts)
|
|
|
|
if mail.write_json_file(mail.getContactsFile(playername), contacts) then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
minetest.log("error","[mail] Save failed - contacts may be lost! ("..playername..")")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function mail.read_json_file(path)
|
|
|
|
local file = io.open(path, "r")
|
|
|
|
local content = {}
|
|
|
|
if file then
|
|
|
|
local json = file:read("*a")
|
|
|
|
content = minetest.parse_json(json or "[]") or {}
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
return content
|
|
|
|
end
|
|
|
|
|
|
|
|
function mail.write_json_file(path, content)
|
|
|
|
local file = io.open(path,"w")
|
|
|
|
local json = minetest.write_json(content)
|
|
|
|
if file and file:write(json) and file:close() then
|
|
|
|
return true
|
|
|
|
else
|
2019-09-16 09:06:54 +03:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|