Diff

plugins/muc/muc.lib.lua @ 6278:fcc3ef191293

plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
author daurnimator <quae@daurnimator.com>
date Tue, 17 Jun 2014 15:27:00 -0400
parent 6277:f2c9c36979b3
child 6317:ec57067c1e0d
line wrap: on
line diff
--- a/plugins/muc/muc.lib.lua	Tue Jun 17 15:18:43 2014 -0400
+++ b/plugins/muc/muc.lib.lua	Tue Jun 17 15:27:00 2014 -0400
@@ -186,30 +186,32 @@
 	end
 end
 
-local function get_base_presence(occupant)
+-- Broadcasts an occupant's presence to the whole room
+-- Takes the x element that goes into the stanzas
+function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason)
+	-- Build real jid and (optionally) occupant jid template presences
+	local base_presence;
 	if occupant.role ~= nil then
 		-- Try to use main jid's presence
 		local pr = occupant:get_presence();
 		if pr ~= nil then
-			return st.clone(pr);
+			base_presence = st.clone(pr);
 		end
 	end
-	return st.presence {from = occupant.nick; type = "unavailable";};
-end
+	base_presence = base_presence or st.presence {from = occupant.nick; type = "unavailable";};
 
--- Broadcasts an occupant's presence to the whole room
--- Takes the x element that goes into the stanzas
-function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason)
-	-- Build real jid and (optionally) occupant jid template presences
+	-- Fire event (before full_p and anon_p are created)
+	module:fire_event("muc-broadcast-presence", {room = self; stanza = base_presence; x = base_x;});
+
 	local function get_presence(is_anonymous)
 		local x = st.clone(base_x);
 		self:build_item_list(occupant, x, is_anonymous, nick, actor, reason);
-		return get_base_presence(occupant):add_child(x), x;
+		return st.clone(base_presence):add_child(x), x;
 	end
+
 	local full_p, full_x = get_presence(false);
 
-	module:fire_event("muc-broadcast-presence", {room = self; stanza = full_p; x = full_x;});
-
+	-- Create anon_p lazily
 	local anon_p, anon_x;
 	local function get_anon_p()
 		if anon_p == nil then