MUC: Assign occupants unavailable presence on room destruction, fixes destruction...
[prosody.git] / plugins / muc / muc.lib.lua
index 69d34e75415e22cb964ec4450ffca10eec5c53ac..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
@@ -491,7 +495,13 @@ function room_mt:handle_presence_to_occupant(origin, stanza)
                        self:publicise_occupant_status(orig_occupant, orig_x, dest_nick);
 
                        if is_last_orig_session then
-                               module:fire_event("muc-occupant-left", {room = self; nick = orig_occupant.nick; occupant = orig_occupant;});
+                               module:fire_event("muc-occupant-left", {
+                                       room = self;
+                                       nick = orig_occupant.nick;
+                                       occupant = orig_occupant;
+                                       origin = origin;
+                                       stanza = stanza;
+                               });
                        end
                end
 
@@ -529,9 +539,22 @@ function room_mt:handle_presence_to_occupant(origin, stanza)
 
                        if orig_occupant == nil then
                                if is_first_dest_session then
-                                       module:fire_event("muc-occupant-joined", {room = self; nick = dest_occupant.nick; occupant = dest_occupant;});
+                                       module:fire_event("muc-occupant-joined", {
+                                               room = self;
+                                               nick = dest_occupant.nick;
+                                               occupant = dest_occupant;
+                                               stanza = stanza;
+                                               origin = origin;
+                                       });
                                end
-                               module:fire_event("muc-occupant-session-new", {room = self; nick = dest_occupant.nick; occupant = dest_occupant; stanza = stanza; jid = real_jid;});
+                               module:fire_event("muc-occupant-session-new", {
+                                       room = self;
+                                       nick = dest_occupant.nick;
+                                       occupant = dest_occupant;
+                                       stanza = stanza;
+                                       origin = origin;
+                                       jid = real_jid;
+                               });
                        end
                end
        elseif type ~= 'result' then -- bad type
@@ -676,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
@@ -704,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
@@ -759,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
@@ -1125,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;
@@ -1199,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;