mod_c2s, mod_s2s: Set session.encrypted as session.secure does not allways mean encry...
[prosody.git] / plugins / muc / mod_muc.lua
index 981c0bcd3d3f106c31e67136b03be740dcde912a..cb967c908a0f7b369ceee91183d642c498cbaf4a 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;
@@ -87,6 +91,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
@@ -224,3 +238,39 @@ function shutdown_component()
 end
 module.unload = shutdown_component;
 module:hook_global("server-stopping", shutdown_component);
+
+-- Ad-hoc commands
+module:depends("adhoc")
+local t_concat = table.concat;
+local keys = require "util.iterators".keys;
+local adhoc_new = module:require "adhoc".new;
+local adhoc_initial = require "util.adhoc".new_initial_data_form;
+local dataforms_new = require "util.dataforms".new;
+
+local destroy_rooms_layout = dataforms_new {
+       title = "Destroy rooms";
+       instructions = "Select the rooms to destroy";
+
+       { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
+       { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
+};
+
+local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
+       return { rooms = array.collect(keys(rooms)):sort() };
+end, function(fields, errors)
+       if errors then
+               local errmsg = {};
+               for name, err in pairs(errors) do
+                       errmsg[#errmsg + 1] = name .. ": " .. err;
+               end
+               return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
+       end
+       for _, room in ipairs(fields.rooms) do
+               rooms[room]:destroy();
+               rooms[room] = nil;
+       end
+       return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
+end);
+local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
+
+module:provides("adhoc", destroy_rooms_desc);