changeset 3829:f20a5d28910f

mod_muc_occupant_id: Ensure occupants have a generated id
author Maxime “pep” Buquet <pep@bouah.net>
date Thu, 02 Jan 2020 18:32:02 +0100
parents a8aa11cc351d
children 63d8072dc864
files mod_muc_occupant_id/mod_muc_occupant_id.lua
diffstat 1 files changed, 19 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_occupant_id/mod_muc_occupant_id.lua	Thu Jan 02 10:45:19 2020 +0100
+++ b/mod_muc_occupant_id/mod_muc_occupant_id.lua	Thu Jan 02 18:32:02 2020 +0100
@@ -10,8 +10,7 @@
 
 local xmlns_occupant_id = "urn:xmpp:occupant-id:0";
 
-local function edit_occupant(event)
-	local occupant, room = event.occupant, event.room;
+local function generate_id(occupant, room)
 	local bare = occupant.bare_jid;
 
 	-- TODO: Move the salt on the MUC component. Setting the salt on the room
@@ -23,7 +22,11 @@
 		room._data.occupant_id_salt = salt;
 	end
 
-	local unique_id = b64encode(hmac_sha256(bare, room._data.occupant_id_salt));
+	return b64encode(hmac_sha256(bare, room._data.occupant_id_salt));
+end
+
+local function edit_occupant(event)
+	local unique_id = generate_id(event.occupant, event.room);
 
 	-- TODO: Store this only once per bare jid and not once per occupant?
 	local stanza = event.stanza;
@@ -33,16 +36,23 @@
 end
 
 local function handle_stanza(event)
-	local stanza, occupant = event.stanza, event.occupant;
-
-	-- TODO: Handle MAM.
+	local stanza, occupant, room = event.stanza, event.occupant, event.room;
 
 	-- strip any existing <occupant-id/> tags to avoid forgery
 	stanza:remove_children("occupant-id", xmlns_occupant_id);
 
-	local unique_id = occupant.sessions[stanza.attr.from]
-		:get_child("occupant-id", xmlns_occupant_id)
-		:get_text();
+	local occupant_tag = occupant.sessions[stanza.attr.from]
+		:get_child("occupant-id", xmlns_occupant_id);
+
+	local unique_id = nil;
+	if occupant_tag == nil then
+		unique_id = generate_id(occupant, room);
+	else
+		unique_id = occupant.sessions[stanza.attr.from]
+			:get_child("occupant-id", xmlns_occupant_id)
+			:get_text();
+	end
+
 	stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
 		:text(unique_id)
 		:up();