From 1c57939c79f4164bf3561d2c787be5a08d553314 Mon Sep 17 00:00:00 2001 From: Athozus Date: Sat, 30 Mar 2024 21:13:45 +0100 Subject: [PATCH] Check if candidate uuid does not already exist before returning it --- util/uuid.lua | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/util/uuid.lua b/util/uuid.lua index 571aa4a..089b546 100644 --- a/util/uuid.lua +++ b/util/uuid.lua @@ -1,9 +1,34 @@ -- source: https://gist.github.com/jrus/3197011 local random = math.random + +local function is_uuid_unexisting(uuid) + for p, _ in minetest.get_auth_handler().iterate() do + local e = mail.get_storage_entry(p) + for _, m in ipairs(e.inbox) do + if m.id == uuid then return false end + end + for _, m in ipairs(e.outbox) do + if m.id == uuid then return false end + end + for _, m in ipairs(e.drafts) do + if m.id == uuid then return false end + end + for _, m in ipairs(e.trash) do + if m.id == uuid then return false end + end + end + return true +end + function mail.new_uuid() local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' - 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 \ No newline at end of file + 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 is_uuid_unexisting(candidate_uuid) + return candidate_uuid +end