MUC: Don't force-save rooms where not needed
[prosody.git] / plugins / muc / muc.lib.lua
index b797619952c10a8359b1e06c19e1ed993869d071..14aad42c329d7cca1db896c92924f26d85af56f9 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
@@ -191,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";};
@@ -310,16 +315,17 @@ function room_mt:get_disco_info(stanza)
        local form = dataform.new {
                { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" };
        };
-       module:fire_event("muc-disco#info", {room = self; reply = reply; form = form;});
-       reply:add_child(form:form(nil, "result"));
+       local formdata = {};
+       module:fire_event("muc-disco#info", {room = self; reply = reply; form = form, formdata = formdata ;});
+       reply:add_child(form:form(formdata, "result"));
        return reply;
 end
 module:hook("muc-disco#info", function(event)
        event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up();
 end);
 module:hook("muc-disco#info", function(event)
-       local count = iterators.count(event.room:each_occupant());
-       table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) });
+       table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants" });
+       event.formdata["muc#roominfo_occupants"] = tostring(iterators.count(event.room:each_occupant()));
 end);
 
 function room_mt:get_disco_items(stanza)
@@ -351,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");
@@ -489,7 +505,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
 
@@ -527,9 +549,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
@@ -674,7 +709,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();
                origin.send(st.reply(stanza));
 
                if next(event.status_codes) then
@@ -757,6 +792,7 @@ function room_mt:handle_admin_query_set_command(origin, stanza)
        else
                success, errtype, err = nil, "cancel", "bad-request";
        end
+       self:save();
        if not success then
                origin.send(st.error_reply(stanza, errtype, err));
        else
@@ -1123,7 +1159,7 @@ function room_mt:set_affiliation(actor, jid, affiliation, reason)
                end
        end
 
-       if self.save then self:save(); end
+       self:save();
 
        module:fire_event("muc-set-affiliation", {
                room = self;
@@ -1147,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";
@@ -1197,18 +1233,99 @@ 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(live)
+       local frozen = {
+               _jid = self.jid;
+               _data = self._data;
+       };
+       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;
 
 return _M;