storage.lua 830 B

123456789101112131415161718192021222324252627282930313233
  1. -- TODO: maybe local cache?
  2. function getMailFile(playername)
  3. local saneplayername = string.gsub(playername, "[.|/]", "")
  4. return mail.maildir .. "/" .. saneplayername .. ".json"
  5. end
  6. mail.getMessages = function(playername)
  7. local file = io.open(getMailFile(playername), "r")
  8. local messages = {}
  9. if file then
  10. local json = file:read("*a")
  11. messages = minetest.parse_json(json or "[]") or {}
  12. mail.hud_update(playername, messages)
  13. file:close()
  14. end
  15. return messages
  16. end
  17. mail.setMessages = function(playername, messages)
  18. local file = io.open(getMailFile(playername),"w")
  19. local json = minetest.write_json(messages)
  20. if file and file:write(json) and file:close() then
  21. mail.hud_update(playername, messages)
  22. return true
  23. else
  24. minetest.log("error","[mail] Save failed - messages may be lost!")
  25. return false
  26. end
  27. end