MUC: Prevent visitors from broadcasting messages.
[prosody.git] / plugins / muc / muc.lib.lua
index 312dcc37ccdb5539daf748a47c888b9b3661b4e6..7907c0c3ea54674b6e70c28fffdff72e6eff34ac 100644 (file)
@@ -124,23 +124,25 @@ function room_mt:broadcast_presence(stanza, sid, code, nick)
        if me then
                stanza:tag("status", {code='110'});
                stanza.attr.to = sid;
-               self:route_stanza(stanza);
+               self:_route_stanza(stanza);
        end
 end
 function room_mt:broadcast_message(stanza, historic)
+       local to = stanza.attr.to;
        for occupant, o_data in pairs(self._occupants) do
                for jid in pairs(o_data.sessions) do
                        stanza.attr.to = jid;
-                       self:route_stanza(stanza);
+                       self:_route_stanza(stanza);
                end
        end
+       stanza.attr.to = to;
        if historic then -- add to history
                local history = self._data['history'];
                if not history then history = {}; self._data['history'] = history; end
-               -- stanza = st.clone(stanza);
+               stanza = st.clone(stanza);
                stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
                stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
-               t_insert(history, st.clone(st.preserialize(stanza)));
+               t_insert(history, st.preserialize(stanza));
                while #history > history_length do t_remove(history, 1) end
        end
 end
@@ -149,7 +151,7 @@ function room_mt:broadcast_except_nick(stanza, nick)
                if rnick ~= nick then
                        for jid in pairs(occupant.sessions) do
                                stanza.attr.to = jid;
-                               self:route_stanza(stanza);
+                               self:_route_stanza(stanza);
                        end
                end
        end
@@ -163,7 +165,7 @@ function room_mt:send_occupant_list(to)
                        pres.attr.to, pres.attr.from = to, occupant;
                        pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
                                :tag("item", {affiliation=o_data.affiliation or "none", role=o_data.role or "none"}):up();
-                       self:route_stanza(pres);
+                       self:_route_stanza(pres);
                end
        end
 end
@@ -173,11 +175,11 @@ function room_mt:send_history(to)
                for _, msg in ipairs(history) do
                        msg = st.deserialize(msg);
                        msg.attr.to=to;
-                       self:route_stanza(msg);
+                       self:_route_stanza(msg);
                end
        end
        if self._data['subject'] then
-               self:route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject']));
+               self:_route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject']));
        end
 end
 
@@ -234,7 +236,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                        pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
                                                :tag("item", {affiliation=occupant.affiliation or "none", role='none'}):up()
                                                :tag("status", {code='110'});
-                                       self:route_stanza(pr);
+                                       self:_route_stanza(pr);
                                        if jid ~= new_jid then
                                                pr = st.clone(occupant.sessions[new_jid])
                                                        :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
@@ -322,7 +324,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                                        self:broadcast_presence(pr, from);
                                                else
                                                        pr.attr.to = from;
-                                                       self:route_stanza(pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
+                                                       self:_route_stanza(pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
                                                                :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up()
                                                                :tag("status", {code='110'}));
                                                end
@@ -354,7 +356,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                end
                                if _to then
                                        stanza.attr.to, stanza.attr.from, stanza.attr.id = _to, self._jid_nick[_nick], _id;
-                                       self:route_stanza(stanza);
+                                       self:_route_stanza(stanza);
                                        stanza.attr.to, stanza.attr.from, stanza.attr.id = to, from, id;
                                end
                        end
@@ -379,7 +381,7 @@ function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
                                stanza.attr.to = bare;
                                stanza.attr.id = base64.encode(jid.."\0"..id.."\0"..md5(from));
                        end
-                       self:route_stanza(stanza);
+                       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"));
@@ -517,8 +519,11 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                local from, to = stanza.attr.from, stanza.attr.to;
                local room = jid_bare(to);
                local current_nick = self._jid_nick[from];
-               if not current_nick then -- not in room
+               local occupant = self._occupants[current_nick];
+               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"));
                else
                        local from = stanza.attr.from;
                        stanza.attr.from = current_nick;
@@ -528,6 +533,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                        else
                                self:broadcast_message(stanza, true);
                        end
+                       stanza.attr.from = from;
                end
        elseif stanza.name == "message" and type == "error" and is_kickable_error(stanza) then
                local current_nick = self._jid_nick[stanza.attr.from];
@@ -565,7 +571,7 @@ function room_mt:handle_to_room(origin, stanza) -- presence changes and groupcha
                                        :tag('body') -- Add a plain message for clients which don't support invites
                                                :text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or ""))
                                        :up();
-                               self:route_stanza(invite);
+                               self:_route_stanza(invite);
                        else
                                origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
                        end
@@ -634,7 +640,7 @@ function room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
                        for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
                                if not role then self._jid_nick[jid] = nil; end
                                p.attr.to = jid;
-                               self:route_stanza(p);
+                               self:_route_stanza(p);
                        end
                end
        end
@@ -675,13 +681,51 @@ function room_mt:set_role(actor, nick, role, callback, reason)
        end
        for jid in pairs(occupant.sessions) do -- send to all sessions of the nick
                p.attr.to = jid;
-               self:route_stanza(p);
+               self:_route_stanza(p);
        end
        if callback then callback(); end
        self:broadcast_except_nick(p, nick);
        return true;
 end
 
+function room_mt:_route_stanza(stanza)
+       local muc_child;
+       local to_occupant = self._occupants[self._jid_nick[stanza.attr.to]];
+       local from_occupant = self._occupants[stanza.attr.from];
+       if stanza.name == "presence" then
+               if to_occupant and from_occupant then
+                       if to_occupant.role == "moderator" or jid_bare(to_occupant.jid) == jid_bare(from_occupant.jid) then
+                               for i=#stanza.tags,1,-1 do
+                                       local tag = stanza.tags[i];
+                                       if tag.name == "x" and tag.attr.xmlns == "http://jabber.org/protocol/muc#user" then
+                                               muc_child = tag;
+                                               break;
+                                       end
+                               end
+                       end
+               end
+       end
+       if muc_child then
+               for _, item in pairs(muc_child.tags) do
+                       if item.name == "item" then
+                               if from_occupant == to_occupant then
+                                       item.attr.jid = stanza.attr.to;
+                               else
+                                       item.attr.jid = from_occupant.jid;
+                               end
+                       end
+               end
+       end
+       self:route_stanza(stanza);
+       if muc_child then
+               for _, item in pairs(muc_child.tags) do
+                       if item.name == "item" then
+                               item.attr.jid = nil;
+                       end
+               end
+       end
+end
+
 local _M = {}; -- module "muc"
 
 function _M.new_room(jid)