mail/util/uuid.lua
2024-04-01 13:23:53 +02:00

39 lines
1.1 KiB
Lua

-- source: https://gist.github.com/jrus/3197011
local random = math.random
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
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 is_uuid_existing(candidate_uuid)
return candidate_uuid
end