- if self:get_members_only() and not self:get_affiliation(_invitee) then
- log("debug", "%s invited %s into members only room %s, granting membership", _from, _invitee, _to);
- self:set_affiliation(_from, _invitee, "member", nil, "Invited by " .. self._jid_nick[_from])
- end
- self:_route_stanza(invite);
+ module:fire_event("muc-invite", { room = self, stanza = invite, origin = origin, incoming = stanza });
+ return true;
+ else
+ origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
+ return true;
+ end
+end
+
+module:hook("muc-invite", function(event)
+ event.room:_route_stanza(event.stanza);
+ return true;
+end, -1)
+
+-- When an invite is sent; add an affiliation for the invitee
+module:hook("muc-invite", function(event)
+ local room, stanza = event.room, event.stanza
+ local invitee = stanza.attr.to
+ if room:get_members_only() and not room:get_affiliation(invitee) then
+ local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from
+ local current_nick = room:get_occupant_jid(from)
+ log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid);
+ room:set_affiliation(from, invitee, "member", nil, "Invited by " .. current_nick)
+ end
+end)
+
+function room_mt:handle_mediated_decline(origin, stanza)
+ local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline")
+ local declinee = jid_prep(payload.attr.to);
+ if declinee then
+ local from, to = stanza.attr.from, stanza.attr.to;
+ -- TODO: Validate declinee
+ local reason = payload:get_child_text("reason")
+ local decline = st.message({from = to, to = declinee, id = stanza.attr.id})
+ :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
+ :tag('decline', {from=from})
+ :tag('reason'):text(reason or ""):up()
+ :up()
+ :up()
+ :tag('body') -- Add a plain message for clients which don't support declines
+ :text(from..' declined your invite to the room '..to..(reason and (' ('..reason..')') or ""))
+ :up();
+ module:fire_event("muc-decline", { room = self, stanza = decline, origin = origin, incoming = stanza });