plugins/muc/mod_muc: Remove attempt_room_creation and create_room function. Instead have a 'track_room' function called from the end of the pre-create hook, and just create an un-tracked room object when we get a presence
authordaurnimator <quae@daurnimator.com>
Tue, 29 Apr 2014 19:35:25 -0400
changeset 6244 dfaacf042cfe
parent 6243 b7c95e9c13de
child 6245 8ec4ff630eb4
plugins/muc/mod_muc: Remove attempt_room_creation and create_room function. Instead have a 'track_room' function called from the end of the pre-create hook, and just create an un-tracked room object when we get a presence
plugins/muc/mod_muc.lua
--- a/plugins/muc/mod_muc.lua	Tue Apr 29 19:00:45 2014 -0400
+++ b/plugins/muc/mod_muc.lua	Tue Apr 29 19:35:25 2014 -0400
@@ -13,7 +13,6 @@
 end
 
 local muclib = module:require "muc";
-local muc_new_room = muclib.new_room;
 room_mt = muclib.room_mt; -- Yes, global.
 local jid_split = require "util.jid".split;
 local jid_bare = require "util.jid".bare;
@@ -48,11 +47,8 @@
 	end
 end
 
-function create_room(jid)
-	local room = muc_new_room(jid);
-	rooms[jid] = room;
-	module:fire_event("muc-room-created", { room = room });
-	return room;
+function track_room(room)
+	rooms[room.jid] = room;
 end
 
 function forget_room(jid)
@@ -93,9 +89,9 @@
 	end
 
 	-- When room is created, over-ride 'save' method
-	module:hook("muc-room-created", function(event)
+	module:hook("muc-occupant-pre-create", function(event)
 		event.room.save = room_save;
-	end);
+	end, 1000);
 
 	-- Automatically destroy empty non-persistent rooms
 	module:hook("muc-occupant-left",function(event)
@@ -110,9 +106,10 @@
 		local node = jid_split(jid);
 		local data = room_configs:get(node);
 		if data then
-			local room = create_room(jid);
+			local room = muclib.new_room(jid);
 			room._data = data._data;
 			room._affiliations = data._affiliations;
+			track_room(room);
 		else -- missing room data
 			persistent_rooms[jid] = nil;
 			module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
@@ -132,6 +129,10 @@
 	end
 end);
 
+module:hook("muc-room-pre-create", function(event)
+	track_room(event.room);
+end, -1000);
+
 module:hook("muc-room-destroyed",function(event)
 	local room = event.room
 	forget_room(room.jid)
@@ -157,18 +158,6 @@
 	end
 end
 
--- Watch presence to create rooms
-local function attempt_room_creation(event)
-	local origin, stanza = event.origin, event.stanza;
-	local room_jid = jid_bare(stanza.attr.to);
-	if stanza.attr.type == nil and get_room_from_jid(room_jid) == nil then
-		create_room(room_jid);
-	end
-end
-module:hook("presence/full", attempt_room_creation, -1)
-module:hook("presence/bare", attempt_room_creation, -1)
-module:hook("presence/host", attempt_room_creation, -1)
-
 for event_name, method in pairs {
 	-- Normal room interactions
 	["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
@@ -195,10 +184,16 @@
 } do
 	module:hook(event_name, function (event)
 		local origin, stanza = event.origin, event.stanza;
-		local room = get_room_from_jid(jid_bare(stanza.attr.to))
+		local room_jid = jid_bare(stanza.attr.to);
+		local room = get_room_from_jid(room_jid);
 		if room == nil then
-			origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
-			return true;
+			-- Watch presence to create rooms
+			if stanza.attr.type == nil and stanza.name == "presence" then
+				room = muclib.new_room(room_jid);
+			else
+				origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
+				return true;
+			end
 		end
 		return room[method](room, origin, stanza);
 	end, -2)