MUC: Separate config from live state
[prosody.git] / plugins / muc / mod_muc.lua
index c50567e911f0a7f57313805363815f38762440d8..c1ecb4f15c86cbd5c9aa1ec9f6bedfe924180260 100644 (file)
@@ -94,26 +94,29 @@ end
 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_state = module:open_store("state");
 
 local room_items_cache = {};
 
-local function room_save(room, forced)
+local function room_save(room, forced, savestate)
        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
+       if is_persistent or savestate then
                persistent_rooms:set(nil, room.jid, true);
-               local data = room:freeze(forced);
+               local data, state = room:freeze(savestate);
+               room_state:set(node, state);
                return room_configs:set(node, data);
-       else
+       elseif forced then
                persistent_rooms:set(nil, room.jid, nil);
+               room_state:set(node, nil);
                return room_configs:set(node, nil);
        end
 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
@@ -133,8 +136,9 @@ end
 local function restore_room(jid)
        local node = jid_split(jid);
        local data = room_configs:get(node);
+       local state = room_state:get(node);
        if data then
-               local room = muclib.restore_room(data);
+               local room = muclib.restore_room(data, state);
                track_room(room);
                return room;
        end
@@ -155,7 +159,7 @@ end
 
 function module.unload()
        for room in rooms:values() do
-               room:save(true);
+               room:save(nil, true);
                forget_room(room);
        end
 end
@@ -287,7 +291,7 @@ end
 
 function shutdown_component()
        for room in each_room(true) do
-               room:save(true);
+               room:save(nil, true);
        end
 end
 module:hook_global("server-stopping", shutdown_component);