migrate.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. -- migrate from mail.db to player-file-based mailbox
  2. mail.migrate = function()
  3. -- create directory, just in case
  4. minetest.mkdir(mail.maildir)
  5. minetest.mkdir(mail.contactsdir)
  6. local file = io.open(minetest.get_worldpath().."/mail.db", "r")
  7. if file then
  8. print("[mail] migrating to new per-player storage")
  9. local data = file:read("*a")
  10. local oldmails = minetest.deserialize(data)
  11. file:close()
  12. for name, oldmessages in pairs(oldmails) do
  13. mail.setMessages(name, oldmessages)
  14. end
  15. -- rename file
  16. print("[mail] migration done, renaming old mail.db")
  17. os.rename(minetest.get_worldpath().."/mail.db", minetest.get_worldpath().."/mail.db.old")
  18. end
  19. end
  20. mail.migrate_contacts = function(playername)
  21. local file = io.open(mail.getContactsFile(playername), 'r')
  22. if not file then
  23. -- file doesn't exist! This is a case for Migrate Man!
  24. local messages = mail.getMessages(playername)
  25. local contacts = {}
  26. if messages and not contacts then
  27. for _, message in pairs(messages) do
  28. mail.ensure_new_format(message)
  29. if contacts[string.lower(message.from)] == nil then
  30. contacts[string.lower(message.from)] = {
  31. name = message.from,
  32. note = "",
  33. }
  34. end
  35. end
  36. end
  37. else
  38. file:close() -- uh, um, nope, let's leave those alone, shall we?
  39. end
  40. end