add contacts dir, leave mail files as is
- restructured storage.lua so reading/writing json is not duplicated - when a player joins and has no contacts file yet, automatically add all players he wrote to
This commit is contained in:
parent
498ef84026
commit
f82c3d2b82
1
init.lua
1
init.lua
@ -8,6 +8,7 @@ mail = {
|
|||||||
|
|
||||||
-- mail directory
|
-- mail directory
|
||||||
maildir = minetest.get_worldpath().."/mails",
|
maildir = minetest.get_worldpath().."/mails",
|
||||||
|
contactsdir = minetest.get_worldpath().."/mails/contacts",
|
||||||
|
|
||||||
-- allow item/node attachments
|
-- allow item/node attachments
|
||||||
allow_attachments = minetest.settings:get("mail.allow_attachments") == "true",
|
allow_attachments = minetest.settings:get("mail.allow_attachments") == "true",
|
||||||
|
26
migrate.lua
26
migrate.lua
@ -4,6 +4,7 @@
|
|||||||
mail.migrate = function()
|
mail.migrate = function()
|
||||||
-- create directory, just in case
|
-- create directory, just in case
|
||||||
minetest.mkdir(mail.maildir)
|
minetest.mkdir(mail.maildir)
|
||||||
|
minetest.mkdir(mail.contactsdir)
|
||||||
|
|
||||||
local file = io.open(minetest.get_worldpath().."/mail.db", "r")
|
local file = io.open(minetest.get_worldpath().."/mail.db", "r")
|
||||||
if file then
|
if file then
|
||||||
@ -23,3 +24,28 @@ mail.migrate = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
mail.migrate_contacts = function(playername)
|
||||||
|
local file = io.open(mail.getContactsFile(playername), 'r')
|
||||||
|
if not file then
|
||||||
|
file:close() -- file doesn't exist! This is a case for Migrate Man!
|
||||||
|
|
||||||
|
local messages = mail.getMessages(playername)
|
||||||
|
local contacts = {}
|
||||||
|
|
||||||
|
if messages and not contacts then
|
||||||
|
for k,message in pairs(messages) do
|
||||||
|
mail.ensure_new_format(message)
|
||||||
|
if contacts[string.lower(message.from)] == nil then
|
||||||
|
contacts[string.lower(message.from)] = {
|
||||||
|
name = message.from,
|
||||||
|
note = "",
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
file:close() -- uh, um, nope, let's leave those alone, shall we?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@ -16,4 +16,6 @@ minetest.register_on_joinplayer(function(player)
|
|||||||
|
|
||||||
end
|
end
|
||||||
end, player:get_player_name())
|
end, player:get_player_name())
|
||||||
|
|
||||||
|
mail.migrate_contacts(player:get_player_name())
|
||||||
end)
|
end)
|
||||||
|
60
storage.lua
60
storage.lua
@ -1,32 +1,70 @@
|
|||||||
|
|
||||||
-- TODO: maybe local cache?
|
-- TODO: maybe local cache?
|
||||||
|
|
||||||
function getMailFile(playername)
|
function mail.getMailFile(playername)
|
||||||
local saneplayername = string.gsub(playername, "[.|/]", "")
|
local saneplayername = string.gsub(playername, "[.|/]", "")
|
||||||
return mail.maildir .. "/" .. saneplayername .. ".json"
|
return mail.maildir .. "/" .. saneplayername .. ".json"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mail.getContactsFile(playername)
|
||||||
|
local saneplayername = string.gsub(playername, "[.|/]", "")
|
||||||
|
return mail.maildir .. "/contacts/" .. saneplayername .. ".json"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
mail.getMessages = function(playername)
|
mail.getMessages = function(playername)
|
||||||
local file = io.open(getMailFile(playername), "r")
|
local messages = mail.read_json_file(mail.getMailFile(playername))
|
||||||
local messages = {}
|
if messages then
|
||||||
if file then
|
|
||||||
local json = file:read("*a")
|
|
||||||
messages = minetest.parse_json(json or "[]") or {}
|
|
||||||
mail.hud_update(playername, messages)
|
mail.hud_update(playername, messages)
|
||||||
file:close()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
end
|
end
|
||||||
|
|
||||||
mail.setMessages = function(playername, messages)
|
mail.setMessages = function(playername, messages)
|
||||||
local file = io.open(getMailFile(playername),"w")
|
if mail.write_json_file(mail.getMailFile(playername), messages) then
|
||||||
local json = minetest.write_json(messages)
|
|
||||||
if file and file:write(json) and file:close() then
|
|
||||||
mail.hud_update(playername, messages)
|
mail.hud_update(playername, messages)
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
minetest.log("error","[mail] Save failed - messages may be lost!")
|
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
|
||||||
|
|
||||||
|
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")
|
||||||
|
print(string.format('read from %s: %s', path, json))
|
||||||
|
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)
|
||||||
|
print(string.format('writing to %s: %s', path, json))
|
||||||
|
if file and file:write(json) and file:close() then
|
||||||
|
return true
|
||||||
|
else
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user