diff --git a/migrate.lua b/migrate.lua index 0cf3953..70e39eb 100644 --- a/migrate.lua +++ b/migrate.lua @@ -80,34 +80,70 @@ local function migrate_v2_to_v3() end) end + + +local function search_box(playername, box, uuid) + local e = mail.get_storage_entry(playername) + for _, m in ipairs(e[box]) do + if m.id == uuid then + return { time = m.time, from = m.from, to = m.to, cc = m.cc, bcc = m.bcc, subject = m.subject, body = m.body } end + end + return false +end + +local function is_uuid_existing(uuid) + for _, k in ipairs(mail.storage:get_keys()) do + if string.sub(k,1,5) == "mail/" then + local p = string.sub(k, 6) + local result + local boxes = {"inbox", "outbox", "drafts", "trash"} + for _, b in ipairs(boxes) do + result = search_box(p, b, uuid) + if result then return result end + end + end + end + return false +end + +local function are_message_sames(a, b) + return a.time == b.time + and a.from == b.from + and a.to == b.to + and a.cc == b.cc + and a.bcc == b.bcc + and a.subject == b.subject + and a.body == b.body +end + local function repair_box(playername, box) local e = mail.get_storage_entry(playername) for _, m in ipairs(e[box]) do local uuid = m.id - local exists = mail.is_uuid_existing(uuid) - if exists and not mail.are_message_sames(exists, m) then + local exists = is_uuid_existing(uuid) + if exists and not are_message_sames(exists, m) then local new_uuid = mail.new_uuid() -- generates a new uuid to replace doublons for _, k in ipairs(mail.storage:get_keys()) do if string.sub(k,1,5) == "mail/" then local p = string.sub(k, 6) local er = mail.get_storage_entry(p) for _, r in ipairs(er.inbox) do - if r.id == uuid and not mail.are_message_sames(m, r) then + if r.id == uuid and not are_message_sames(m, r) then r.id = new_uuid end end for _, r in ipairs(er.outbox) do - if r.id == uuid and not mail.are_message_sames(m, r) then + if r.id == uuid and not are_message_sames(m, r) then r.id = new_uuid end end for _, r in ipairs(er.drafts) do - if r.id == uuid and not mail.are_message_sames(m, r) then + if r.id == uuid and not are_message_sames(m, r) then r.id = new_uuid end end for _, r in ipairs(er.trash) do - if r.id == uuid and not mail.are_message_sames(m, r) then + if r.id == uuid and not are_message_sames(m, r) then r.id = new_uuid end end diff --git a/util/uuid.lua b/util/uuid.lua index 17127bd..4e2740a 100644 --- a/util/uuid.lua +++ b/util/uuid.lua @@ -1,49 +1,11 @@ -- source: https://gist.github.com/jrus/3197011 local random = math.random -function mail.search_box(playername, box, uuid) - local e = mail.get_storage_entry(playername) - for _, m in ipairs(e[box]) do - if m.id == uuid then - return { time = m.time, from = m.from, to = m.to, cc = m.cc, bcc = m.bcc, subject = m.subject, body = m.body } end - end - return false -end - -function mail.is_uuid_existing(uuid) - for _, k in ipairs(mail.storage:get_keys()) do - if string.sub(k,1,5) == "mail/" then - local p = string.sub(k, 6) - local result - local boxes = {"inbox", "outbox", "drafts", "trash"} - for _, b in ipairs(boxes) do - result = mail.search_box(p, b, uuid) - if result then return result end - end - end - end - return false -end - -function mail.are_message_sames(a, b) - return a.time == b.time - and a.from == b.from - and a.to == b.to - and a.cc == b.cc - and a.bcc == b.bcc - and a.subject == b.subject - and a.body == b.body -end - function mail.new_uuid() local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' - local candidate_uuid - repeat - candidate_uuid = string.gsub(template, '[xy]', - function (c) - local v = (c == 'x') and random(0, 0xf) or random(8, 0xb) - return string.format('%x', v) - end) - until not mail.is_uuid_existing(candidate_uuid) - return candidate_uuid + return string.gsub(template, '[xy]', + function (c) + local v = (c == 'x') and random(0, 0xf) or random(8, 0xb) + return string.format('%x', v) + end) end