MUC: Fix logic for when to broadcast unavailable presence (actual fix for 14170d161b39)
[prosody.git] / plugins / muc / muc.lib.lua
index 14cbd63c63e84f593f27fcac8b15c7923f7186c1..e98e99b8a50c8e1d1989e8bf798d5d731e4d7422 100644 (file)
@@ -196,7 +196,7 @@ function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason
        local base_presence do
                -- Try to use main jid's presence
                local pr = occupant:get_presence();
-               if pr and (pr.attr.type ~= "unavailable" or occupant.role == nil) then
+               if pr and (pr.attr.type ~= "unavailable" and occupant.role ~= nil) then
                        base_presence = st.clone(pr);
                else -- user is leaving but didn't send a leave presence. make one for them
                        base_presence = st.presence {from = occupant.nick; type = "unavailable";};
@@ -357,6 +357,16 @@ function room_mt:handle_kickable(origin, stanza) -- luacheck: ignore 212
        return true;
 end
 
+if not module:get_option_boolean("muc_compat_create", true) then
+       module:hook("muc-room-pre-create", function(event)
+               local origin, stanza = event.origin, event.stanza;
+               if not stanza:get_child("x", "http://jabber.org/protocol/muc") then
+                       origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
+                       return true;
+               end
+       end, -1);
+end
+
 -- Give the room creator owner affiliation
 module:hook("muc-room-pre-create", function(event)
        event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
@@ -782,7 +792,7 @@ function room_mt:handle_admin_query_set_command(origin, stanza)
        else
                success, errtype, err = nil, "cancel", "bad-request";
        end
-       room:save();
+       self:save();
        if not success then
                origin.send(st.error_reply(stanza, errtype, err));
        else
@@ -1173,7 +1183,7 @@ function room_mt:set_role(actor, occupant_jid, role, reason)
        if not actor then return nil, "modify", "not-acceptable"; end
 
        local occupant = self:get_occupant_by_nick(occupant_jid);
-       if not occupant then return nil, "modify", "not-acceptable"; end
+       if not occupant then return nil, "modify", "item-not-found"; end
 
        if valid_roles[role or "none"] == nil then
                return nil, "modify", "not-acceptable";
@@ -1233,12 +1243,87 @@ function _M.new_room(jid, config)
        }, room_mt);
 end
 
-function room_mt:freeze()
-       return {
-               jid = self.jid;
+function room_mt:freeze(live)
+       local frozen = {
+               _jid = self.jid;
                _data = self._data;
-               _affiliations = self._affiliations;
-       }
+       };
+       for user, affiliation in pairs(self._affiliations) do
+               frozen[user] = affiliation;
+       end
+       if live then
+               for nick, occupant in self:each_occupant() do
+                       frozen[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);
+                       end
+               end
+       end
+       return frozen;
+end
+
+function _M.restore_room(frozen)
+       -- COMPAT
+       if frozen.jid and frozen._affiliations then
+               local room = _M.new_room(frozen.jid, frozen._data);
+               room._affiliations = frozen._affiliations;
+               return room;
+       end
+
+       local room_jid = frozen._jid;
+       local room = _M.new_room(room_jid, frozen._data);
+
+       local occupants = {};
+       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 node or host:sub(1,1) ~= "_" then
+                       if not resource then
+                               -- bare jid: affiliation
+                               room._affiliations[jid] = data;
+                       elseif host == room_host and node == room_name then
+                               -- full room jid: bare real jid and role
+                               local bare_jid = data.bare_jid;
+                               local   occupant = occupant_lib.new(bare_jid, jid);
+                               occupant.jid = data.jid;
+                               occupant.role = data.role;
+                               occupants[bare_jid] = occupant;
+                               local sessions = occupant_sessions[bare_jid];
+                               if sessions then
+                                       for full_jid, presence in pairs(sessions) do
+                                               occupant:set_session(full_jid, presence);
+                                       end
+                               end
+                               occupant_sessions[bare_jid] = nil;
+                       else
+                               -- full user jid: presence
+                               local presence = st.deserialize(data);
+                               local bare_jid = jid_bare(jid);
+                               local occupant = occupants[bare_jid];
+                               local sessions = occupant_sessions[bare_jid];
+                               if occupant then
+                                       occupant:set_session(jid, presence);
+                               elseif sessions then
+                                       sessions[jid] = presence;
+                               else
+                                       occupant_sessions[bare_jid] = {
+                                               [jid] = presence;
+                                       };
+                               end
+                       end
+               end
+       end
+
+       for _, occupant in pairs(occupants) do
+               room:save_occupant(occupant);
+       end
+
+       return room;
 end
 
 _M.room_mt = room_mt;