Mercurial > prosody-modules
comparison mod_compat_muc_admin/mod_compat_muc_admin.lua @ 627:a8ff69c9b498
mod_compat_muc_admin: first commit.
| author | Marco Cirillo <maranda@lightwitch.org> |
|---|---|
| date | Tue, 27 Mar 2012 15:49:31 +0000 |
| parents | |
| children | c72be31941fa |
comparison
equal
deleted
inserted
replaced
| 626:f19f723571d9 | 627:a8ff69c9b498 |
|---|---|
| 1 local st = require "util.stanza"; | |
| 2 local jid_split = require "util.jid".split; | |
| 3 local jid_bare = require "util.jid".bare; | |
| 4 local jid_prep = require "util.jid".prep; | |
| 5 local log = require "util.logger".init("mod_muc"); | |
| 6 local muc_host = module:get_host(); | |
| 7 | |
| 8 if not hosts[muc_host].modules.muc then -- Not a MUC host | |
| 9 module:log("error", "this module can only be used on muc hosts."); return false; | |
| 10 end | |
| 11 | |
| 12 local xmlns_ma, xmlns_mo = "http://jabber.org/protocol/muc#admin", "http://jabber.org/protocol/muc#owner"; | |
| 13 | |
| 14 -- COMPAT: iq condensed function | |
| 15 hosts[muc_host].modules.muc.stanza_handler.muc_new_room.room_mt["compat_iq"] = function (self, origin, stanza, xmlns) | |
| 16 local actor = stanza.attr.from; | |
| 17 local affiliation = self:get_affiliation(actor); | |
| 18 local current_nick = self._jid_nick[actor]; | |
| 19 local role = current_nick and self._occupants[current_nick].role or self:get_default_role(affiliation); | |
| 20 local item = stanza.tags[1].tags[1]; | |
| 21 if item and item.name == "item" then | |
| 22 if stanza.attr.type == "set" then | |
| 23 local callback = function() origin.send(st.reply(stanza)); end | |
| 24 if item.attr.jid then -- Validate provided JID | |
| 25 item.attr.jid = jid_prep(item.attr.jid); | |
| 26 if not item.attr.jid then | |
| 27 origin.send(st.error_reply(stanza, "modify", "jid-malformed")); | |
| 28 return; | |
| 29 end | |
| 30 end | |
| 31 if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation | |
| 32 local occupant = self._occupants[self.jid.."/"..item.attr.nick]; | |
| 33 if occupant then item.attr.jid = occupant.jid; end | |
| 34 elseif not item.attr.nick and item.attr.jid then | |
| 35 local nick = self._jid_nick[item.attr.jid]; | |
| 36 if nick then item.attr.nick = select(3, jid_split(nick)); end | |
| 37 end | |
| 38 local reason = item.tags[1] and item.tags[1].name == "reason" and #item.tags[1] == 1 and item.tags[1][1]; | |
| 39 if item.attr.affiliation and item.attr.jid and not item.attr.role then | |
| 40 local success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, callback, reason); | |
| 41 if not success then origin.send(st.error_reply(stanza, errtype, err)); end | |
| 42 elseif item.attr.role and item.attr.nick and not item.attr.affiliation then | |
| 43 local success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, callback, reason); | |
| 44 if not success then origin.send(st.error_reply(stanza, errtype, err)); end | |
| 45 else | |
| 46 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | |
| 47 end | |
| 48 elseif stanza.attr.type == "get" then | |
| 49 local _aff = item.attr.affiliation; | |
| 50 local _rol = item.attr.role; | |
| 51 if _aff and not _rol then | |
| 52 if affiliation == "owner" or (affiliation == "admin" and _aff ~= "owner" and _aff ~= "admin") then | |
| 53 local reply = st.reply(stanza):query(xmlns); | |
| 54 for jid, affiliation in pairs(self._affiliations) do | |
| 55 if affiliation == _aff then | |
| 56 reply:tag("item", {affiliation = _aff, jid = jid}):up(); | |
| 57 end | |
| 58 end | |
| 59 origin.send(reply); | |
| 60 else | |
| 61 origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
| 62 end | |
| 63 elseif _rol and not _aff then | |
| 64 if role == "moderator" then | |
| 65 -- TODO allow admins and owners not in room? Provide read-only access to everyone who can see the participants anyway? | |
| 66 if _rol == "none" then _rol = nil; end | |
| 67 local reply = st.reply(stanza):query(xmlns); | |
| 68 for occupant_jid, occupant in pairs(self._occupants) do | |
| 69 if occupant.role == _rol then | |
| 70 reply:tag("item", { | |
| 71 nick = select(3, jid_split(occupant_jid)), | |
| 72 role = _rol or "none", | |
| 73 affiliation = occupant.affiliation or "none", | |
| 74 jid = occupant.jid | |
| 75 }):up(); | |
| 76 end | |
| 77 end | |
| 78 origin.send(reply); | |
| 79 else | |
| 80 origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
| 81 end | |
| 82 else | |
| 83 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | |
| 84 end | |
| 85 end | |
| 86 elseif stanza.attr.type == "set" or stanza.attr.type == "get" then | |
| 87 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | |
| 88 end | |
| 89 end | |
| 90 | |
| 91 -- COMPAT: reworked handle_to_room function | |
| 92 hosts[muc_host].modules.muc.stanza_handler.muc_new_room.room_mt["handle_to_room"] = function (self, origin, stanza) | |
| 93 local type = stanza.attr.type; | |
| 94 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns; | |
| 95 if stanza.name == "iq" then | |
| 96 if xmlns == "http://jabber.org/protocol/disco#info" and type == "get" then | |
| 97 origin.send(self:get_disco_info(stanza)); | |
| 98 elseif xmlns == "http://jabber.org/protocol/disco#items" and type == "get" then | |
| 99 origin.send(self:get_disco_items(stanza)); | |
| 100 elseif (xmlns == xmlns_ma or xmlns == xmlns_mo) then | |
| 101 if xmlns == xmlns_ma then | |
| 102 self:compat_iq(origin, stanza, xmlns); | |
| 103 elseif xmlns == xmlns_mo and stanza.tags[1].name == "query" and #stanza.tags[1].tags == 0 and | |
| 104 stanza.attr.type == "get" then -- form request | |
| 105 if self:get_affiliation(stanza.attr.from) ~= "owner" then | |
| 106 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms")); | |
| 107 else | |
| 108 self:send_form(origin, stanza); | |
| 109 end | |
| 110 elseif xmlns == xmlns_mo and stanza.tags[1].name == "query" and stanza.tags[1]:get_child("x", "jabber:x:data") and | |
| 111 stanza.attr.type == "set" then | |
| 112 if self:get_affiliation(stanza.attr.from) ~= "owner" then | |
| 113 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms")); | |
| 114 else | |
| 115 self:process_form(origin, stanza); | |
| 116 end | |
| 117 elseif xmlns == xmlns_mo and stanza.tags[1].tags[1] then | |
| 118 local child = stanza.tags[1].tags[1]; | |
| 119 if child.name == "destroy" then | |
| 120 local newjid = child.attr.jid; | |
| 121 local reason, password; | |
| 122 for _,tag in ipairs(child.tags) do | |
| 123 if tag.name == "reason" then | |
| 124 reason = #tag.tags == 0 and tag[1]; | |
| 125 elseif tag.name == "password" then | |
| 126 password = #tag.tags == 0 and tag[1]; | |
| 127 end | |
| 128 end | |
| 129 self:destroy(newjid, reason, password); | |
| 130 origin.send(st.reply(stanza)); | |
| 131 else | |
| 132 self:compat_iq(origin, stanza, xmlns); | |
| 133 end | |
| 134 else | |
| 135 origin.send(st.error_reply(stanza, "modify", "bad-request")); | |
| 136 end | |
| 137 elseif type == "set" or type == "get" then | |
| 138 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 139 end | |
| 140 elseif stanza.name == "message" and type == "groupchat" then | |
| 141 local from, to = stanza.attr.from, stanza.attr.to; | |
| 142 local room = jid_bare(to); | |
| 143 local current_nick = self._jid_nick[from]; | |
| 144 local occupant = self._occupants[current_nick]; | |
| 145 if not occupant then -- not in room | |
| 146 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
| 147 elseif occupant.role == "visitor" then | |
| 148 origin.send(st.error_reply(stanza, "cancel", "forbidden")); | |
| 149 else | |
| 150 local from = stanza.attr.from; | |
| 151 stanza.attr.from = current_nick; | |
| 152 local subject = getText(stanza, {"subject"}); | |
| 153 if subject then | |
| 154 if occupant.role == "moderator" or | |
| 155 ( self._data.changesubject and occupant.role == "participant" ) then -- and participant | |
| 156 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza | |
| 157 else | |
| 158 stanza.attr.from = from; | |
| 159 origin.send(st.error_reply(stanza, "cancel", "forbidden")); | |
| 160 end | |
| 161 else | |
| 162 self:broadcast_message(stanza, true); | |
| 163 end | |
| 164 stanza.attr.from = from; | |
| 165 end | |
| 166 elseif stanza.name == "message" and type == "error" and is_kickable_error(stanza) then | |
| 167 local current_nick = self._jid_nick[stanza.attr.from]; | |
| 168 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); | |
| 169 self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable | |
| 170 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick | |
| 171 local to = stanza.attr.to; | |
| 172 local current_nick = self._jid_nick[stanza.attr.from]; | |
| 173 if current_nick then | |
| 174 stanza.attr.to = current_nick; | |
| 175 self:handle_to_occupant(origin, stanza); | |
| 176 stanza.attr.to = to; | |
| 177 elseif type ~= "error" and type ~= "result" then | |
| 178 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 179 end | |
| 180 elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from] | |
| 181 and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" then | |
| 182 local x = stanza.tags[1]; | |
| 183 local payload = (#x.tags == 1 and x.tags[1]); | |
| 184 if payload and payload.name == "invite" and payload.attr.to then | |
| 185 local _from, _to = stanza.attr.from, stanza.attr.to; | |
| 186 local _invitee = jid_prep(payload.attr.to); | |
| 187 if _invitee then | |
| 188 local _reason = payload.tags[1] and payload.tags[1].name == 'reason' and #payload.tags[1].tags == 0 and payload.tags[1][1]; | |
| 189 local invite = st.message({from = _to, to = _invitee, id = stanza.attr.id}) | |
| 190 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) | |
| 191 :tag('invite', {from=_from}) | |
| 192 :tag('reason'):text(_reason or ""):up() | |
| 193 :up(); | |
| 194 if self:get_password() then | |
| 195 invite:tag("password"):text(self:get_password()):up(); | |
| 196 end | |
| 197 invite:up() | |
| 198 :tag('x', {xmlns="jabber:x:conference", jid=_to}) -- COMPAT: Some older clients expect this | |
| 199 :text(_reason or "") | |
| 200 :up() | |
| 201 :tag('body') -- Add a plain message for clients which don't support invites | |
| 202 :text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or "")) | |
| 203 :up(); | |
| 204 if self:is_members_only() and not self:get_affiliation(_invitee) then | |
| 205 log("debug", "%s invited %s into members only room %s, granting membership", _from, _invitee, _to); | |
| 206 self:set_affiliation(_from, _invitee, "member", nil, "Invited by " .. self._jid_nick[_from]) | |
| 207 end | |
| 208 self:_route_stanza(invite); | |
| 209 else | |
| 210 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); | |
| 211 end | |
| 212 else | |
| 213 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | |
| 214 end | |
| 215 else | |
| 216 if type == "error" or type == "result" then return; end | |
| 217 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 218 end | |
| 219 end | |
| 220 | |
| 221 |
