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
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
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
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);
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);
-- 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;
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);