MUC: Split out handling of the room-creating presence into its own method
[prosody.git] / plugins / muc / mod_muc.lua
index 5ac230adad796244d180442fb081afe98b1d6649..657823383481f95abe63c31c45a746d331970ab2 100644 (file)
@@ -95,9 +95,12 @@ local persistent_rooms_storage = module:open_store("persistent");
 local persistent_rooms = module:open_store("persistent", "map");
 local room_configs = module:open_store("config");
 
+local room_items_cache = {};
+
 local function room_save(room, forced)
        local node = jid_split(room.jid);
        local is_persistent = persistent.get(room);
+       room_items_cache[room.jid] = room:get_public() and room:get_name() or nil;
        if is_persistent or forced then
                persistent_rooms:set(nil, room.jid, true);
                local data = room:freeze(forced);
@@ -147,6 +150,7 @@ function delete_room(room)
        module:log("debug", "Deleting %s", room);
        room_configs:set(jid_split(room.jid), nil);
        persistent_rooms:set(nil, room.jid, nil);
+       room_items_cache[room.jid] = nil;
 end
 
 function module.unload()
@@ -176,7 +180,7 @@ function each_room(local_only)
                        seen[room.jid] = true;
                end
                for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
-                       if seen[room_jid] then
+                       if not seen[room_jid] then
                                local room = restore_room(room_jid);
                                if room == nil then
                                        module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
@@ -191,9 +195,17 @@ end
 module:hook("host-disco-items", function(event)
        local reply = event.reply;
        module:log("debug", "host-disco-items called");
-       for room in each_room() do
-               if not room:get_hidden() then
-                       reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
+       if next(room_items_cache) ~= nil then
+               for jid, room_name in pairs(room_items_cache) do
+                       reply:tag("item", { jid = jid, name = room_name }):up();
+               end
+       else
+               for room in each_room() do
+                       if not room:get_hidden() then
+                               local jid, room_name = room.jid, room:get_name();
+                               room_items_cache[jid] = room_name;
+                               reply:tag("item", { jid = jid, name = room_name }):up();
+                       end
                end
        end
 end);
@@ -261,6 +273,7 @@ for event_name, method in pairs {
                        -- Watch presence to create rooms
                        if stanza.attr.type == nil and stanza.name == "presence" then
                                room = muclib.new_room(room_jid);
+                               return room:handle_first_presence(origin, stanza);
                        elseif stanza.attr.type ~= "error" then
                                origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
                                return true;
@@ -273,10 +286,8 @@ for event_name, method in pairs {
 end
 
 function shutdown_component()
-       local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
-               :tag("status", { code = "332"}):up();
        for room in each_room(true) do
-               room:clear(x);
+               room:save(true);
        end
 end
 module:hook_global("server-stopping", shutdown_component);