MUC: Expose create_room(jid).
[prosody.git] / plugins / muc / muc.lib.lua
index 0203df2621f49a6d3e8eaa70cce1f21c9f1e6b47..c5be3a91c10f1a1c44c34a8119cad203507950f1 100644 (file)
@@ -24,7 +24,7 @@ local base64 = require "util.encodings".base64;
 local md5 = require "util.hashes".md5;
 
 local muc_domain = nil; --module:get_host();
-local default_history_length = 20;
+local default_history_length, max_history_length = 20, math.huge;
 
 ------------
 local function filter_xmlns_from_array(array, filters)
@@ -135,7 +135,7 @@ function room_mt:broadcast_message(stanza, historic)
                stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
                local entry = { stanza = stanza, stamp = stamp };
                t_insert(history, entry);
-               while #history > self._data.history_length do t_remove(history, 1) end
+               while #history > (self._data.history_length or default_history_length) do t_remove(history, 1) end
        end
 end
 function room_mt:broadcast_except_nick(stanza, nick)
@@ -339,18 +339,39 @@ function room_mt:get_historylength()
        return self._data.history_length or default_history_length;
 end
 function room_mt:set_historylength(length)
-       if tonumber(length) == nil then
-               return
-       end
-       length = tonumber(length);
-       log("debug", "max_history_length %s", self._data.max_history_length or "nil");
-       if self._data.max_history_length and length > self._data.max_history_length then
-               length = self._data.max_history_length
+       length = math.min(tonumber(length) or default_history_length, max_history_length or math.huge);
+       if length == default_history_length then
+               length = nil;
        end
        self._data.history_length = length;
 end
 
 
+local function construct_stanza_id(room, stanza)
+       local from_jid, to_nick = stanza.attr.from, stanza.attr.to;
+       local from_nick = room._jid_nick[from_jid];
+       local occupant = room._occupants[to_nick];
+       local to_jid = occupant.jid;
+       
+       return from_nick, to_jid, base64.encode(to_jid.."\0"..stanza.attr.id.."\0"..md5(from_jid));
+end
+local function deconstruct_stanza_id(room, stanza)
+       local from_jid_possiblybare, to_nick = stanza.attr.from, stanza.attr.to;
+       local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$");
+       local from_nick = room._jid_nick[from_jid];
+
+       if not(from_nick) then return; end
+       if not(from_jid_possiblybare == from_jid or from_jid_possiblybare == jid_bare(from_jid)) then return; end
+
+       local occupant = room._occupants[to_nick];
+       for to_jid in pairs(occupant and occupant.sessions or {}) do
+               if md5(to_jid) == to_jid_hash then
+                       return from_nick, to_jid, id;
+               end
+       end
+end
+
+
 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
        local from, to = stanza.attr.from, stanza.attr.to;
        local room = jid_bare(to);
@@ -501,24 +522,13 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                        end
                end
        elseif not current_nick then -- not in room
-               if type == "error" or type == "result" then
-                       local id = stanza.name == "iq" and stanza.attr.id and base64.decode(stanza.attr.id);
-                       local _nick, _id, _hash = (id or ""):match("^(.+)%z(.*)%z(.+)$");
-                       local occupant = self._occupants[stanza.attr.to];
-                       if occupant and _nick and self._jid_nick[_nick] and _id and _hash then
-                               local id, _to = stanza.attr.id;
-                               for jid in pairs(occupant.sessions) do
-                                       if md5(jid) == _hash then
-                                               _to = jid;
-                                               break;
-                                       end
-                               end
-                               if _to then
-                                       stanza.attr.to, stanza.attr.from, stanza.attr.id = _to, self._jid_nick[_nick], _id;
-                                       self:_route_stanza(stanza);
-                                       stanza.attr.to, stanza.attr.from, stanza.attr.id = to, from, id;
-                               end
+               if (type == "error" or type == "result") and stanza.name == "iq" then
+                       local id = stanza.attr.id;
+                       stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza);
+                       if stanza.attr.id then
+                               self:_route_stanza(stanza);
                        end
+                       stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
                else
                        origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
                end
@@ -531,16 +541,28 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                local o_data = self._occupants[to];
                if o_data then
                        log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
-                       local jid = o_data.jid;
-                       local bare = jid_bare(jid);
-                       stanza.attr.to, stanza.attr.from = jid, current_nick;
-                       local id = stanza.attr.id;
-                       if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' and bare ~= jid then
-                               stanza.attr.to = bare;
-                               stanza.attr.id = base64.encode(jid.."\0"..id.."\0"..md5(from));
+                       if stanza.name == "iq" then
+                               local id = stanza.attr.id;
+                               if stanza.attr.type == "get" or stanza.attr.type == "set" then
+                                       stanza.attr.from, stanza.attr.to, stanza.attr.id = construct_stanza_id(self, stanza);
+                               else
+                                       stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza);
+                               end
+                               if type == 'get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then
+                                       stanza.attr.to = jid_bare(stanza.attr.to);
+                               end
+                               if stanza.attr.id then
+                                       self:_route_stanza(stanza);
+                               end
+                               stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
+                       else -- message
+                               stanza.attr.from = current_nick;
+                               for jid in pairs(o_data.sessions) do
+                                       stanza.attr.to = jid;
+                                       self:_route_stanza(stanza);
+                               end
+                               stanza.attr.from, stanza.attr.to = from, to;
                        end
-                       self:_route_stanza(stanza);
-                       stanza.attr.to, stanza.attr.from, stanza.attr.id = to, from, id;
                elseif type ~= "error" and type ~= "result" then -- recipient not in room
                        origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
                end
@@ -676,8 +698,8 @@ function room_mt:process_form(origin, stanza)
        dirty = dirty or (self:get_changesubject() ~= (not changesubject and true or nil))
        module:log('debug', 'changesubject=%s', changesubject and "true" or "false")
 
-       local historylength = fields['muc#roomconfig_historylength'];
-       dirty = dirty or (self:get_historylength() ~= (historylength and true or nil))
+       local historylength = tonumber(fields['muc#roomconfig_historylength']);
+       dirty = dirty or (historylength and (self:get_historylength() ~= historylength));
        module:log('debug', 'historylength=%s', historylength)
 
 
@@ -860,7 +882,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                if not occupant then -- not in room
                        origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
                elseif occupant.role == "visitor" then
-                       origin.send(st.error_reply(stanza, "cancel", "forbidden"));
+                       origin.send(st.error_reply(stanza, "auth", "forbidden"));
                else
                        local from = stanza.attr.from;
                        stanza.attr.from = current_nick;
@@ -871,7 +893,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                        self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
                                else
                                        stanza.attr.from = from;
-                                       origin.send(st.error_reply(stanza, "cancel", "forbidden"));
+                                       origin.send(st.error_reply(stanza, "auth", "forbidden"));
                                end
                        else
                                self:broadcast_message(stanza, self:get_historylength() > 0);
@@ -1128,11 +1150,17 @@ function _M.new_room(jid, config)
                _occupants = {};
                _data = {
                    whois = 'moderators';
-                   history_length = (config and config.max_history_length) or default_history_length;
-                   max_history_length = (config and config.max_history_length) or default_history_length;
+                   history_length = math.min((config and config.history_length)
+                       or default_history_length, max_history_length);
                };
                _affiliations = {};
        }, room_mt);
 end
 
+function _M.set_max_history_length(_max_history_length)
+       max_history_length = _max_history_length or math.huge;
+end
+
+_M.room_mt = room_mt;
+
 return _M;