MUC: Hide new MUC room storage format behind an off-by-default option
[prosody.git] / plugins / muc / muc.lib.lua
index 071f1f8b01f05eaf1a90e86233adf20c7052fcd5..ac8ebaddfb746525a9cb3747511e62bec844e8f8 100644 (file)
@@ -531,10 +531,10 @@ function room_mt:handle_normal_presence(origin, stanza)
                        if not is_first_dest_session then -- User is swapping into another pre-existing session
                                log("debug", "session %s is swapping into multisession %s, showing it leave.", real_jid, dest_occupant.nick);
                                -- Show the other session leaving
-                               local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";})
-                                       :tag("status"):text("you are joining pre-existing session " .. dest_nick):up();
+                               local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
                                add_item(x, self:get_affiliation(bare_jid), "none");
                                local pr = st.presence{from = dest_occupant.nick, to = real_jid, type = "unavailable"}
+                                       :tag("status"):text("you are joining pre-existing session " .. dest_nick):up()
                                        :add_child(x);
                                self:route_stanza(pr);
                        end
@@ -577,7 +577,7 @@ function room_mt:handle_normal_presence(origin, stanza)
                end
                self:save_occupant(dest_occupant);
 
-               if orig_occupant == nil then
+               if orig_occupant == nil or muc_x then
                        -- Send occupant list to newly joined user
                        self:send_occupant_list(real_jid, function(nick, occupant) -- luacheck: ignore 212
                                -- Don't include self
@@ -598,7 +598,7 @@ function room_mt:handle_normal_presence(origin, stanza)
                        self:route_stanza(pr);
                end
 
-               if orig_occupant == nil then
+               if orig_occupant == nil or muc_x then
                        if is_first_dest_session then
                                module:fire_event("muc-occupant-joined", {
                                        room = self;
@@ -769,7 +769,7 @@ function room_mt:process_form(origin, stanza)
                end
                event.field, event.value = nil, nil;
 
-               self:save();
+               self:save(true);
                origin.send(st.reply(stanza));
 
                if next(event.status_codes) then
@@ -852,7 +852,7 @@ function room_mt:handle_admin_query_set_command(origin, stanza)
        else
                success, errtype, err = nil, "cancel", "bad-request";
        end
-       self:save();
+       self:save(true);
        if not success then
                origin.send(st.error_reply(stanza, errtype, err));
        else
@@ -872,7 +872,8 @@ function room_mt:handle_admin_query_get_command(origin, stanza)
                -- You need to be at least an admin, and be requesting info about your affifiliation or lower
                -- e.g. an admin can't ask for a list of owners
                local affiliation_rank = valid_affiliations[affiliation or "none"];
-               if affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank then
+               if affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank
+               or self:get_members_only() and self:get_whois() == "anyone" and affiliation_rank >= valid_affiliations.member then
                        local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
                        for jid in self:each_affiliation(_aff or "none") do
                                reply:tag("item", {affiliation = _aff, jid = jid}):up();
@@ -1219,7 +1220,7 @@ function room_mt:set_affiliation(actor, jid, affiliation, reason)
                end
        end
 
-       self:save();
+       self:save(true);
 
        module:fire_event("muc-set-affiliation", {
                room = self;
@@ -1303,35 +1304,47 @@ function _M.new_room(jid, config)
        }, room_mt);
 end
 
+local new_format = module:get_option_boolean("new_muc_storage_format", false);
+
 function room_mt:freeze(live)
-       local frozen = {
-               _jid = self.jid;
-               _data = self._data;
-       };
-       for user, affiliation in pairs(self._affiliations) do
-               frozen[user] = affiliation;
+       local frozen, state;
+       if new_format then
+               frozen = {
+                       _jid = self.jid;
+                       _data = self._data;
+               };
+               for user, affiliation in pairs(self._affiliations) do
+                       frozen[user] = affiliation;
+               end
+       else
+               frozen = {
+                       jid = self.jid;
+                       _data = self._data;
+                       _affiliations = self._affiliations;
+               };
        end
        if live then
+               state = {};
                for nick, occupant in self:each_occupant() do
-                       frozen[nick] = {
+                       state[nick] = {
                                bare_jid = occupant.bare_jid;
                                role = occupant.role;
                                jid = occupant.jid;
                        }
                        for jid, presence in occupant:each_session() do
-                               frozen[jid] = st.preserialize(presence);
+                               state[jid] = st.preserialize(presence);
                        end
                end
                local history = self._history;
                if history then
-                       frozen._last_message = st.preserialize(history[#history].stanza);
-                       frozen._last_message_at = history[#history].timestamp;
+                       state._last_message = st.preserialize(history[#history].stanza);
+                       state._last_message_at = history[#history].timestamp;
                end
        end
-       return frozen;
+       return frozen, state;
 end
 
-function _M.restore_room(frozen)
+function _M.restore_room(frozen, state)
        -- COMPAT
        if frozen.jid and frozen._affiliations then
                local room = _M.new_room(frozen.jid, frozen._data);
@@ -1342,10 +1355,10 @@ function _M.restore_room(frozen)
        local room_jid = frozen._jid;
        local room = _M.new_room(room_jid, frozen._data);
 
-       if frozen._last_message and frozen._last_message_at then
+       if state and state._last_message and state._last_message_at then
                room._history = {
-                       { stanza = st.deserialize(frozen._last_message),
-                         timestamp = frozen._last_message_at, },
+                       { stanza = st.deserialize(state._last_message),
+                         timestamp = state._last_message_at, },
                };
        end
 
@@ -1353,12 +1366,16 @@ function _M.restore_room(frozen)
        local occupant_sessions = {};
        local room_name, room_host = jid_split(room_jid);
        for jid, data in pairs(frozen) do
+               local node, host, resource = jid_split(jid);
+               if host:sub(1,1) ~= "_" and not resource and type(data) == "string" then
+                       -- bare jid: affiliation
+                       room._affiliations[jid] = data;
+               end
+       end
+       for jid, data in pairs(state or frozen) do
                local node, host, resource = jid_split(jid);
                if node or host:sub(1,1) ~= "_" then
-                       if not resource and type(data) == "string" then
-                               -- bare jid: affiliation
-                               room._affiliations[jid] = data;
-                       elseif host == room_host and node == room_name and resource then
+                       if host == room_host and node == room_name and resource and type(data) == "table" then
                                -- full room jid: bare real jid and role
                                local bare_jid = data.bare_jid;
                                local   occupant = occupant_lib.new(bare_jid, jid);
@@ -1372,7 +1389,7 @@ function _M.restore_room(frozen)
                                        end
                                end
                                occupant_sessions[bare_jid] = nil;
-                       else
+                       elseif type(data) == "table" and data.name then
                                -- full user jid: presence
                                local presence = st.deserialize(data);
                                local bare_jid = jid_bare(jid);