plugins/muc: Have utility methods for locking the room
authordaurnimator <quae@daurnimator.com>
Thu, 20 Mar 2014 11:06:10 -0400
changeset 6129 6c66571ab0f9
parent 6128 8a71a1c34202
child 6130 c95d9132592a
plugins/muc: Have utility methods for locking the room
plugins/muc/mod_muc.lua
plugins/muc/muc.lib.lua
--- a/plugins/muc/mod_muc.lua	Wed Mar 19 17:50:49 2014 -0400
+++ b/plugins/muc/mod_muc.lua	Thu Mar 20 11:06:10 2014 -0400
@@ -91,10 +91,10 @@
 	room.save = room_save;
 	rooms[jid] = room;
 	if lock_rooms then
-		room.locked = true;
+		room:lock();
 		if lock_room_timeout and lock_room_timeout > 0 then
 			module:add_timer(lock_room_timeout, function ()
-				if room.locked then
+				if room:is_locked() then
 					room:destroy(); -- Not unlocked in time
 				end
 			end);
--- a/plugins/muc/muc.lib.lua	Wed Mar 19 17:50:49 2014 -0400
+++ b/plugins/muc/muc.lib.lua	Thu Mar 20 11:06:10 2014 -0400
@@ -85,6 +85,17 @@
 	end
 end
 
+function room_mt:lock()
+	self.locked = true
+end
+function room_mt:unlock()
+	module:fire_event("muc-room-unlocked", { room = self });
+	self.locked = nil
+end
+function room_mt:is_locked()
+	return not not self.locked
+end
+
 function room_mt:broadcast_presence(stanza, sid, code, nick)
 	stanza = get_filtered_presence(stanza);
 	local occupant = self._occupants[stanza.attr.from];
@@ -465,10 +476,10 @@
 	log("debug", "%s joining as %s", from, to);
 	if not next(self._affiliations) then -- new room, no owners
 		self._affiliations[jid_bare(from)] = "owner";
-		if self.locked and not stanza:get_child("x", "http://jabber.org/protocol/muc") then
-			self.locked = nil; -- Older groupchat protocol doesn't lock
+		if self:is_locked() and not stanza:get_child("x", "http://jabber.org/protocol/muc") then
+			self:unlock(); -- Older groupchat protocol doesn't lock
 		end
-	elseif self.locked then -- Deny entry
+	elseif self:is_locked() then -- Deny entry
 		origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
 		return true;
 	end
@@ -494,7 +505,7 @@
 		if self:get_whois() == 'anyone' then
 			pr:tag("status", {code='100'}):up();
 		end
-		if self.locked then
+		if self:is_locked() then
 			pr:tag("status", {code='201'}):up();
 		end
 		pr.attr.to = from;
@@ -777,9 +788,8 @@
 	handle_option("password", "muc#roomconfig_roomsecret");
 
 	if self.save then self:save(true); end
-	if self.locked then
-		module:fire_event("muc-room-unlocked", { room = self });
-		self.locked = nil;
+	if self:is_locked() then
+		self:unlock();
 	end
 	origin.send(st.reply(stanza));
 
@@ -1267,6 +1277,7 @@
 function _M.new_room(jid, config)
 	return setmetatable({
 		jid = jid;
+		locked = nil;
 		_jid_nick = {};
 		_occupants = {};
 		_data = {