MUC: Assign occupants unavailable presence on room destruction, fixes destruction...
[prosody.git] / plugins / muc / muc.lib.lua
index 795f8ccb2049d25f2c44ffd7753ca3056842c8f7..434127b4f2fd4e87ba2996284b9abfaf3fc0074a 100644 (file)
@@ -19,10 +19,11 @@ local jid_bare = require "util.jid".bare;
 local jid_prep = require "util.jid".prep;
 local jid_join = require "util.jid".join;
 local st = require "util.stanza";
-local log = require "util.logger".init("mod_muc");
 local base64 = require "util.encodings".base64;
 local md5 = require "util.hashes".md5;
 
+local log = module._log;
+
 local occupant_lib = module:require "muc/occupant"
 local muc_util = module:require "muc/util";
 local is_kickable_error = muc_util.is_kickable_error;
@@ -35,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
@@ -490,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
 
@@ -528,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
@@ -675,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
@@ -703,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
@@ -758,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
@@ -1124,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;
@@ -1198,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;