Changeset

3834:2f189f022b84

mod_muc_occupant_id: Simplify handling of occupants (all into one function)
author Maxime “pep” Buquet <pep@bouah.net>
date Sat, 04 Jan 2020 19:47:55 +0100
parents 3833:580862decd77
children 3835:5258f0afa8b4
files mod_muc_occupant_id/mod_muc_occupant_id.lua
diffstat 1 files changed, 15 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_occupant_id/mod_muc_occupant_id.lua	Sat Jan 04 11:09:28 2020 +0100
+++ b/mod_muc_occupant_id/mod_muc_occupant_id.lua	Sat Jan 04 19:47:55 2020 +0100
@@ -13,46 +13,29 @@
 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
-	-- can be problematic when the room is destroyed. Next time it's recreated
-	-- the salt will be different and so will be the unique_id. Or maybe we want
-	-- this anyway?
 	if room._data.occupant_id_salt == nil then
-		local salt = uuid.generate();
-		room._data.occupant_id_salt = salt;
+		room._data.occupant_id_salt = uuid.generate();
 	end
 
-	return b64encode(hmac_sha256(bare, room._data.occupant_id_salt));
+	if room._data.occupant_ids == nil then
+		room._data.occupant_ids = {};
+	end
+
+	if room._data.occupant_ids[bare] == nil then
+		local unique_id = b64encode(hmac_sha256(bare, room._data.occupant_id_salt));
+		room._data.occupant_ids[bare] = unique_id;
+	end
+
+	return room._data.occupant_ids[bare];
 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;
-	stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
-		:text(unique_id)
-		:up();
-end
-
-local function handle_stanza(event)
+local function update_occupant(event)
 	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 occupant_tag = occupant.sessions[stanza.attr.from]
-		:get_child("occupant-id", xmlns_occupant_id);
-
-	local unique_id;
-	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
-
+	local unique_id = generate_id(occupant, room);
 	stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
 		:text(unique_id)
 		:up();
@@ -63,5 +46,5 @@
 	event.reply:tag("feature", { var = xmlns_occupant_id }):up();
 end);
 
-module:hook("muc-occupant-pre-join", edit_occupant);
-module:hook("muc-occupant-groupchat", handle_stanza);
+module:hook("muc-occupant-pre-join", update_occupant);
+module:hook("muc-occupant-groupchat", update_occupant);