MUC: Assign occupants unavailable presence on room destruction, fixes destruction...
[prosody.git] / plugins / muc / muc.lib.lua
index 8a3a3ebd74dd9b7ac12f1688f445d2624a0990d3..434127b4f2fd4e87ba2996284b9abfaf3fc0074a 100644 (file)
@@ -36,6 +36,10 @@ function room_mt:__tostring()
        return "MUC room ("..self.jid..")";
 end
 
+function room_mt.save()
+       -- overriden by mod_muc.lua
+end
+
 function room_mt:get_occupant_jid(real_jid)
        return self._jid_nick[real_jid]
 end
@@ -495,6 +499,8 @@ function room_mt:handle_presence_to_occupant(origin, stanza)
                                        room = self;
                                        nick = orig_occupant.nick;
                                        occupant = orig_occupant;
+                                       origin = origin;
+                                       stanza = stanza;
                                });
                        end
                end
@@ -537,6 +543,8 @@ function room_mt:handle_presence_to_occupant(origin, stanza)
                                                room = self;
                                                nick = dest_occupant.nick;
                                                occupant = dest_occupant;
+                                               stanza = stanza;
+                                               origin = origin;
                                        });
                                end
                                module:fire_event("muc-occupant-session-new", {
@@ -691,7 +699,7 @@ function room_mt:process_form(origin, stanza)
                end
                event.field, event.value = nil, nil;
 
-               if self.save then self:save(true); end
+               self:save(true);
                origin.send(st.reply(stanza));
 
                if next(event.status_codes) then
@@ -719,6 +727,7 @@ function room_mt:clear(x)
                occupants_updated[occupant] = true;
        end
        for occupant in pairs(occupants_updated) do
+               occupant:set_session(occupant.jid, st.presence({type="unavailable"}), true);
                self:publicise_occupant_status(occupant, x);
                module:fire_event("muc-occupant-left", { room = self; nick = occupant.nick; occupant = occupant;});
        end
@@ -774,6 +783,7 @@ function room_mt:handle_admin_query_set_command(origin, stanza)
        else
                success, errtype, err = nil, "cancel", "bad-request";
        end
+       room:save();
        if not success then
                origin.send(st.error_reply(stanza, errtype, err));
        else
@@ -1140,7 +1150,7 @@ function room_mt:set_affiliation(actor, jid, affiliation, reason)
                end
        end
 
-       if self.save then self:save(); end
+       self:save(true);
 
        module:fire_event("muc-set-affiliation", {
                room = self;
@@ -1214,18 +1224,24 @@ room_mt.set_whois = whois.set;
 
 local _M = {}; -- module "muc"
 
-function _M.new_room(jid, config) -- luacheck: ignore 212
-       -- TODO use config?
+function _M.new_room(jid, config)
        return setmetatable({
                jid = jid;
                _jid_nick = {};
                _occupants = {};
-               _data = {
-               };
+               _data = config or {};
                _affiliations = {};
        }, room_mt);
 end
 
+function room_mt:freeze()
+       return {
+               jid = self.jid;
+               _data = self._data;
+               _affiliations = self._affiliations;
+       }
+end
+
 _M.room_mt = room_mt;
 
 return _M;