Merge 0.9->0.10
[prosody.git] / plugins / muc / mod_muc.lua
index a9480465c0417adf5c3f043b03ee2c9c6936393c..c514bafd375b36e810ba3d9887e460f19984abee 100644 (file)
@@ -1,11 +1,12 @@
 -- Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
 
+local array = require "util.array";
 
 if module:get_host_type() ~= "component" then
        error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
@@ -16,12 +17,15 @@ local muc_name = module:get_option("name");
 if type(muc_name) ~= "string" then muc_name = "Prosody Chatrooms"; end
 local restrict_room_creation = module:get_option("restrict_room_creation");
 if restrict_room_creation then
-       if restrict_room_creation == true then 
+       if restrict_room_creation == true then
                restrict_room_creation = "admin";
        elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
                restrict_room_creation = nil;
        end
 end
+local lock_rooms = module:get_option_boolean("muc_room_locking", false);
+local lock_room_timeout = module:get_option_number("muc_room_lock_timeout", 300);
+
 local muclib = module:require "muc";
 local muc_new_room = muclib.new_room;
 local jid_split = require "util.jid".split;
@@ -48,8 +52,9 @@ local function is_admin(jid)
        return um_is_admin(jid, module.host);
 end
 
-local _set_affiliation = muc_new_room.room_mt.set_affiliation;
-local _get_affiliation = muc_new_room.room_mt.get_affiliation;
+room_mt = muclib.room_mt; -- Yes, global.
+local _set_affiliation = room_mt.set_affiliation;
+local _get_affiliation = room_mt.get_affiliation;
 function muclib.room_mt:get_affiliation(jid)
        if is_admin(jid) then return "owner"; end
        return _get_affiliation(self, jid);
@@ -87,6 +92,16 @@ function create_room(jid)
        room.route_stanza = room_route_stanza;
        room.save = room_save;
        rooms[jid] = room;
+       if lock_rooms then
+               room.locked = true;
+               if lock_room_timeout and lock_room_timeout > 0 then
+                       module:add_timer(lock_room_timeout, function ()
+                               if room.locked then
+                                       room:destroy(); -- Not unlocked in time
+                               end
+                       end);
+               end
+       end
        module:fire_event("muc-room-created", { room = room });
        return room;
 end
@@ -150,7 +165,7 @@ function stanza_handler(event)
                        return true;
                end
                if not(restrict_room_creation) or
-                 (restrict_room_creation == "admin" and is_admin(stanza.attr.from)) or
+                 is_admin(stanza.attr.from) or
                  (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
                        room = create_room(bare);
                end
@@ -158,6 +173,7 @@ function stanza_handler(event)
        if room then
                room:handle_stanza(origin, stanza);
                if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
+                       module:fire_event("muc-room-destroyed", { room = room });
                        rooms[bare] = nil; -- discard room
                end
        else