Merge s2s branch
[prosody.git] / plugins / muc / muc.lib.lua
index a7603535910007f9b6c0a31ad18488796115bdb3..a5aba3c8d31986854e946282b5d940c3fe0292ac 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)
@@ -88,6 +88,10 @@ local function getText(stanza, path) return getUsingPath(stanza, path, true); en
 local room_mt = {};
 room_mt.__index = room_mt;
 
+function room_mt:__tostring()
+       return "MUC room ("..self.jid..")";
+end
+
 function room_mt:get_default_role(affiliation)
        if affiliation == "owner" or affiliation == "admin" then
                return "moderator";
@@ -339,7 +343,7 @@ function room_mt:get_historylength()
        return self._data.history_length or default_history_length;
 end
 function room_mt:set_historylength(length)
-       length = math.min(tonumber(length) or default_history_length, self._data_max_history_length or math.huge);
+       length = math.min(tonumber(length) or default_history_length, max_history_length or math.huge);
        if length == default_history_length then
                length = nil;
        end
@@ -522,14 +526,14 @@ 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" and stanza.name == "iq" then
+               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
+               elseif type ~= "error" then
                        origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
                end
        elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
@@ -576,10 +580,9 @@ function room_mt:send_form(origin, stanza)
 end
 
 function room_mt:get_form_layout()
-       local title = "Configuration for "..self.jid;
-       return dataform.new({
-               title = title,
-               instructions = title,
+       local form = dataform.new({
+               title = "Configuration for "..self.jid,
+               instructions = "Complete and submit this form to configure the room.",
                {
                        name = 'FORM_TYPE',
                        type = 'hidden',
@@ -649,6 +652,7 @@ function room_mt:get_form_layout()
                        value = tostring(self:get_historylength())
                }
        });
+       return module:fire_event("muc-config-form", { room = self, form = form }) or form;
 end
 
 local valid_whois = {
@@ -669,6 +673,10 @@ function room_mt:process_form(origin, stanza)
 
        local dirty = false
 
+       local event = { room = self, fields = fields, changed = dirty };
+       module:fire_event("muc-config-submitted", event);
+       dirty = event.changed or dirty;
+
        local name = fields['muc#roomconfig_roomname'];
        if name ~= self:get_name() then
                self:set_name(name);
@@ -765,13 +773,9 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
        local type = stanza.attr.type;
        local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
        if stanza.name == "iq" then
-               if xmlns == "http://jabber.org/protocol/disco#info" and type == "get" then
-                       if stanza.tags[1].attr.node then
-                               origin.send(st.error_reply(stanza, "cancel", "feature-not-implemented"));
-                       else
-                               origin.send(self:get_disco_info(stanza));
-                       end
-               elseif xmlns == "http://jabber.org/protocol/disco#items" and type == "get" then
+               if xmlns == "http://jabber.org/protocol/disco#info" and type == "get" and not stanza.tags[1].attr.node then
+                       origin.send(self:get_disco_info(stanza));
+               elseif xmlns == "http://jabber.org/protocol/disco#items" and type == "get" and not stanza.tags[1].attr.node then
                        origin.send(self:get_disco_items(stanza));
                elseif xmlns == "http://jabber.org/protocol/muc#admin" then
                        local actor = stanza.attr.from;
@@ -896,7 +900,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                        origin.send(st.error_reply(stanza, "auth", "forbidden"));
                                end
                        else
-                               self:broadcast_message(stanza, self:get_historylength() > 0);
+                               self:broadcast_message(stanza, self:get_historylength() > 0 and stanza:get_child("body"));
                        end
                        stanza.attr.from = from;
                end
@@ -914,8 +918,8 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                elseif type ~= "error" and type ~= "result" then
                        origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
                end
-       elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from]
-               and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" then
+       elseif stanza.name == "message" and not(type == "chat" or type == "error" or type == "groupchat" or type == "headline") and #stanza.tags == 1
+               and self._jid_nick[stanza.attr.from] and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" then
                local x = stanza.tags[1];
                local payload = (#x.tags == 1 and x.tags[1]);
                if payload and payload.name == "invite" and payload.attr.to then
@@ -987,7 +991,7 @@ function room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
                        return true;
                end
                if actor_affiliation ~= "owner" then
-                       if actor_affiliation ~= "admin" or target_affiliation == "owner" or target_affiliation == "admin" then
+                       if affiliation == "owner" or affiliation == "admin" or actor_affiliation ~= "admin" or target_affiliation == "owner" or target_affiliation == "admin" then
                                return nil, "cancel", "not-allowed";
                        end
                elseif target_affiliation == "owner" and jid_bare(actor) == jid then -- self change
@@ -1049,11 +1053,12 @@ function room_mt:get_role(nick)
        return session and session.role or nil;
 end
 function room_mt:can_set_role(actor_jid, occupant_jid, role)
-       local actor = self._occupants[self._jid_nick[actor_jid]];
        local occupant = self._occupants[occupant_jid];
-       
        if not occupant or not actor then return nil, "modify", "not-acceptable"; end
 
+       if actor_jid == true then return true; end
+
+       local actor = self._occupants[self._jid_nick[actor_jid]];
        if actor.role == "moderator" then
                if occupant.affiliation ~= "owner" and occupant.affiliation ~= "admin" then
                        if actor.affiliation == "owner" or actor.affiliation == "admin" then
@@ -1150,13 +1155,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;