Software / code / prosody
Comparison
plugins/muc/muc.lib.lua @ 6180:35388114439f
plugins/muc/muc.lib: non-function changes (reordering, semicolons and comments)
| author | daurnimator <quae@daurnimator.com> |
|---|---|
| date | Thu, 27 Mar 2014 19:18:57 -0400 |
| parent | 6179:e488a90195bc |
| child | 6181:6baa9a59aa03 |
comparison
equal
deleted
inserted
replaced
| 6179:e488a90195bc | 6180:35388114439f |
|---|---|
| 7 -- COPYING file in the source package for more information. | 7 -- COPYING file in the source package for more information. |
| 8 -- | 8 -- |
| 9 | 9 |
| 10 local select = select; | 10 local select = select; |
| 11 local pairs, ipairs = pairs, ipairs; | 11 local pairs, ipairs = pairs, ipairs; |
| 12 local next = next; | |
| 13 local setmetatable = setmetatable; | |
| 14 local t_insert, t_remove = table.insert, table.remove; | |
| 12 | 15 |
| 13 local gettime = os.time; | 16 local gettime = os.time; |
| 14 local datetime = require "util.datetime"; | 17 local datetime = require "util.datetime"; |
| 15 | 18 |
| 16 local dataform = require "util.dataforms"; | 19 local dataform = require "util.dataforms"; |
| 18 local jid_split = require "util.jid".split; | 21 local jid_split = require "util.jid".split; |
| 19 local jid_bare = require "util.jid".bare; | 22 local jid_bare = require "util.jid".bare; |
| 20 local jid_prep = require "util.jid".prep; | 23 local jid_prep = require "util.jid".prep; |
| 21 local st = require "util.stanza"; | 24 local st = require "util.stanza"; |
| 22 local log = require "util.logger".init("mod_muc"); | 25 local log = require "util.logger".init("mod_muc"); |
| 23 local t_insert, t_remove = table.insert, table.remove; | |
| 24 local setmetatable = setmetatable; | |
| 25 local base64 = require "util.encodings".base64; | 26 local base64 = require "util.encodings".base64; |
| 26 local md5 = require "util.hashes".md5; | 27 local md5 = require "util.hashes".md5; |
| 27 | 28 |
| 28 local occupant_lib = module:require "muc/occupant" | 29 local occupant_lib = module:require "muc/occupant" |
| 29 | 30 |
| 353 msg.attr.to = to; | 354 msg.attr.to = to; |
| 354 i = i + 1 | 355 i = i + 1 |
| 355 return msg | 356 return msg |
| 356 end | 357 end |
| 357 return true; | 358 return true; |
| 358 end) | 359 end); |
| 359 | 360 |
| 360 function room_mt:send_history(stanza) | 361 function room_mt:send_history(stanza) |
| 361 local maxchars, maxstanzas, since = parse_history(stanza) | 362 local maxchars, maxstanzas, since = parse_history(stanza) |
| 362 local event = { | 363 local event = { |
| 363 room = self; | 364 room = self; |
| 582 local reply = st.error_reply(stanza, "auth", "not-authorized"):up(); | 583 local reply = st.error_reply(stanza, "auth", "not-authorized"):up(); |
| 583 reply.tags[1].attr.code = "401"; | 584 reply.tags[1].attr.code = "401"; |
| 584 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | 585 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
| 585 return true; | 586 return true; |
| 586 end | 587 end |
| 587 end, -1) | 588 end, -1); |
| 588 | 589 |
| 589 module:hook("muc-occupant-pre-join/locked", function(event) | 590 module:hook("muc-occupant-pre-join/locked", function(event) |
| 590 if event.room:is_locked() then -- Deny entry | 591 if event.room:is_locked() then -- Deny entry |
| 591 event.origin.send(st.error_reply(event.stanza, "cancel", "item-not-found")); | 592 event.origin.send(st.error_reply(event.stanza, "cancel", "item-not-found")); |
| 592 return true; | 593 return true; |
| 601 local reply = st.error_reply(stanza, "auth", "registration-required"):up(); | 602 local reply = st.error_reply(stanza, "auth", "registration-required"):up(); |
| 602 reply.tags[1].attr.code = "407"; | 603 reply.tags[1].attr.code = "407"; |
| 603 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | 604 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
| 604 return true; | 605 return true; |
| 605 end | 606 end |
| 606 end, -1) | 607 end, -1); |
| 607 | 608 |
| 608 -- banned | 609 -- check if user is banned |
| 609 module:hook("muc-occupant-pre-join/affiliation", function(event) | 610 module:hook("muc-occupant-pre-join/affiliation", function(event) |
| 610 local room, stanza = event.room, event.stanza; | 611 local room, stanza = event.room, event.stanza; |
| 611 local affiliation = room:get_affiliation(stanza.attr.from); | 612 local affiliation = room:get_affiliation(stanza.attr.from); |
| 612 if affiliation == "outcast" then | 613 if affiliation == "outcast" then |
| 613 local reply = st.error_reply(stanza, "auth", "forbidden"):up(); | 614 local reply = st.error_reply(stanza, "auth", "forbidden"):up(); |
| 614 reply.tags[1].attr.code = "403"; | 615 reply.tags[1].attr.code = "403"; |
| 615 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | 616 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
| 616 return true; | 617 return true; |
| 617 end | 618 end |
| 618 end, -1) | 619 end, -1); |
| 619 | 620 |
| 620 module:hook("muc-occupant-joined", function(event) | 621 module:hook("muc-occupant-joined", function(event) |
| 621 local room, stanza = event.room, event.stanza; | 622 local room, stanza = event.room, event.stanza; |
| 622 local real_jid = stanza.attr.from; | 623 local real_jid = stanza.attr.from; |
| 623 room:send_occupant_list(real_jid, function(nick, occupant) | 624 room:send_occupant_list(real_jid, function(nick, occupant) |
| 855 end | 856 end |
| 856 log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid); | 857 log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid); |
| 857 stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up(); | 858 stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up(); |
| 858 stanza.attr.from = current_nick; | 859 stanza.attr.from = current_nick; |
| 859 self:route_to_occupant(o_data, stanza) | 860 self:route_to_occupant(o_data, stanza) |
| 861 -- TODO: Remove x tag? | |
| 860 stanza.attr.from = from; | 862 stanza.attr.from = from; |
| 861 return true; | 863 return true; |
| 862 end | 864 end |
| 863 | 865 |
| 864 function room_mt:send_form(origin, stanza) | 866 function room_mt:send_form(origin, stanza) |
| 1187 origin.send(st.error_reply(stanza, "auth", "forbidden")); | 1189 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
| 1188 return true; | 1190 return true; |
| 1189 end | 1191 end |
| 1190 local _invitee = jid_prep(payload.attr.to); | 1192 local _invitee = jid_prep(payload.attr.to); |
| 1191 if _invitee then | 1193 if _invitee then |
| 1194 local _reason = payload:get_child_text("reason"); | |
| 1192 if self:get_whois() == "moderators" then | 1195 if self:get_whois() == "moderators" then |
| 1193 _from = current_nick; | 1196 _from = current_nick; |
| 1194 end | 1197 end |
| 1195 local _reason = payload:get_child_text("reason") | |
| 1196 local invite = st.message({from = _to, to = _invitee, id = stanza.attr.id}) | 1198 local invite = st.message({from = _to, to = _invitee, id = stanza.attr.id}) |
| 1197 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) | 1199 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) |
| 1198 :tag('invite', {from=_from}) | 1200 :tag('invite', {from=_from}) |
| 1199 :tag('reason'):text(_reason or ""):up() | 1201 :tag('reason'):text(_reason or ""):up() |
| 1200 :up(); | 1202 :up(); |
| 1201 local password = self:get_password() | 1203 local password = self:get_password(); |
| 1202 if password then | 1204 if password then |
| 1203 invite:tag("password"):text(password):up(); | 1205 invite:tag("password"):text(password):up(); |
| 1204 end | 1206 end |
| 1205 invite:up() | 1207 invite:up() |
| 1206 :tag('x', {xmlns="jabber:x:conference", jid=_to}) -- COMPAT: Some older clients expect this | 1208 :tag('x', {xmlns="jabber:x:conference", jid=_to}) -- COMPAT: Some older clients expect this |
| 1207 :text(_reason or "") | 1209 :text(_reason or "") |
| 1208 :up() | 1210 :up() |
| 1209 :tag('body') -- Add a plain message for clients which don't support invites | 1211 :tag('body') -- Add a plain message for clients which don't support invites |
| 1210 :text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or "")) | 1212 :text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or "")) |
| 1211 :up(); | 1213 :up(); |
| 1212 module:fire_event("muc-invite", { room = self, stanza = invite, origin = origin, incoming = stanza }); | 1214 module:fire_event("muc-invite", {room = self, stanza = invite, origin = origin, incoming = stanza}); |
| 1213 return true; | 1215 return true; |
| 1214 else | 1216 else |
| 1215 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); | 1217 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); |
| 1216 return true; | 1218 return true; |
| 1217 end | 1219 end |
| 1230 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from | 1232 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from |
| 1231 local current_nick = room:get_occupant_jid(from) | 1233 local current_nick = room:get_occupant_jid(from) |
| 1232 log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid); | 1234 log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid); |
| 1233 room:set_affiliation(from, invitee, "member", nil, "Invited by " .. current_nick) | 1235 room:set_affiliation(from, invitee, "member", nil, "Invited by " .. current_nick) |
| 1234 end | 1236 end |
| 1235 end) | 1237 end); |
| 1236 | 1238 |
| 1237 function room_mt:handle_mediated_decline(origin, stanza) | 1239 function room_mt:handle_mediated_decline(origin, stanza) |
| 1238 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline") | 1240 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline") |
| 1239 local declinee = jid_prep(payload.attr.to); | 1241 local declinee = jid_prep(payload.attr.to); |
| 1240 if declinee then | 1242 if declinee then |
| 1287 return self:handle_mediated_decline(origin, stanza) | 1289 return self:handle_mediated_decline(origin, stanza) |
| 1288 end | 1290 end |
| 1289 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | 1291 origin.send(st.error_reply(stanza, "cancel", "bad-request")); |
| 1290 return true; | 1292 return true; |
| 1291 end | 1293 end |
| 1292 else | |
| 1293 return nil; | |
| 1294 end | 1294 end |
| 1295 end | 1295 end |
| 1296 | 1296 |
| 1297 function room_mt:route_stanza(stanza) | 1297 function room_mt:route_stanza(stanza) |
| 1298 module:send(stanza) | 1298 module:send(stanza); |
| 1299 end | 1299 end |
| 1300 | 1300 |
| 1301 function room_mt:get_affiliation(jid) | 1301 function room_mt:get_affiliation(jid) |
| 1302 local node, host, resource = jid_split(jid); | 1302 local node, host, resource = jid_split(jid); |
| 1303 local bare = node and node.."@"..host or host; | 1303 local bare = node and node.."@"..host or host; |