MUC: Separate force-save parameter from save-entire-state flag
[prosody.git] / plugins / muc / mod_muc.lua
index 5ac230adad796244d180442fb081afe98b1d6649..5b342c02c2c21ef5083306f51c604edb75cff8e3 100644 (file)
@@ -95,14 +95,17 @@ local persistent_rooms_storage = module:open_store("persistent");
 local persistent_rooms = module:open_store("persistent", "map");
 local room_configs = module:open_store("config");
 
-local function room_save(room, forced)
+local room_items_cache = {};
+
+local function room_save(room, forced, savestate)
        local node = jid_split(room.jid);
        local is_persistent = persistent.get(room);
-       if is_persistent or forced then
+       room_items_cache[room.jid] = room:get_public() and room:get_name() or nil;
+       if is_persistent or savestate then
                persistent_rooms:set(nil, room.jid, true);
-               local data = room:freeze(forced);
+               local data = room:freeze(savestate);
                return room_configs:set(node, data);
-       else
+       elseif forced then
                persistent_rooms:set(nil, room.jid, nil);
                return room_configs:set(node, nil);
        end
@@ -110,7 +113,7 @@ end
 
 local rooms = cache.new(module:get_option_number("muc_room_cache_size", 100), function (_, room)
        module:log("debug", "%s evicted", room);
-       room_save(room, true); -- Force to disk
+       room_save(room, nil, true); -- Force to disk
 end);
 
 -- Automatically destroy empty non-persistent rooms
@@ -147,11 +150,12 @@ 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()
        for room in rooms:values() do
-               room:save(true);
+               room:save(nil, true);
                forget_room(room);
        end
 end
@@ -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,14 +195,22 @@ 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);
 
-module:hook("muc-room-pre-create", function(event)
+module:hook("muc-room-created", function(event)
        track_room(event.room);
 end, -1000);
 
@@ -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(nil, true);
        end
 end
 module:hook_global("server-stopping", shutdown_component);