Software / code / prosody-modules
Comparison
mod_muc_ban_ip/mod_muc_ban_ip.lua @ 1005:591590de34ef
mod_muc_ban_ip: When a user is banned from a MUC, ban their IP from the MUC also (works for remote rooms too)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 09 May 2013 10:15:47 +0100 |
| child | 1647:8860405e2af6 |
comparison
equal
deleted
inserted
replaced
| 1004:290c21a5e0ee | 1005:591590de34ef |
|---|---|
| 1 module:set_global(); | |
| 2 | |
| 3 local jid_bare = require "util.jid".bare; | |
| 4 local st = require "util.stanza"; | |
| 5 local xmlns_muc_user = "http://jabber.org/protocol/muc#user"; | |
| 6 | |
| 7 local ip_bans = module:shared("bans"); | |
| 8 local full_sessions = prosody.full_sessions; | |
| 9 | |
| 10 local function ban_ip(session, from) | |
| 11 local ip = session.ip; | |
| 12 if not ip then | |
| 13 module:log("warn", "Failed to ban IP (IP unknown) for %s", session.full_jid); | |
| 14 return; | |
| 15 end | |
| 16 local banned_from = ip_bans[ip]; | |
| 17 if not banned_from then | |
| 18 banned_from = {}; | |
| 19 ip_bans[ip] = banned_from; | |
| 20 end | |
| 21 banned_from[from] = true; | |
| 22 module:log("debug", "Banned IP address %s from %s", ip, from); | |
| 23 end | |
| 24 | |
| 25 function check_for_incoming_ban(event) | |
| 26 local stanza = event.stanza; | |
| 27 local to_session = full_sessions[stanza.attr.to]; | |
| 28 if to_session then | |
| 29 local directed = to_session.directed; | |
| 30 local from = stanza.attr.from; | |
| 31 if directed and directed[from] and stanza.attr.type == "unavailable" then | |
| 32 -- This is a stanza from somewhere we sent directed presence to (may be a MUC) | |
| 33 local x = stanza:get_child("x", xmlns_muc_user); | |
| 34 if x then | |
| 35 for status in x:childtags("status") do | |
| 36 if status.attr.code == '301' then | |
| 37 ban_ip(to_session, jid_bare(from)); | |
| 38 end | |
| 39 end | |
| 40 end | |
| 41 end | |
| 42 end | |
| 43 end | |
| 44 | |
| 45 function check_for_ban(event) | |
| 46 local ip = event.origin.ip; | |
| 47 local to = jid_bare(event.stanza.attr.to); | |
| 48 if ip_bans[ip] and ip_bans[ip][to] then | |
| 49 event.origin.send(st.error_reply(event.stanza, "auth", "forbidden") | |
| 50 :tag("x", { xmlns = xmlns_muc_user }) | |
| 51 :tag("status", { code = '301' })); | |
| 52 return true; | |
| 53 end | |
| 54 module:log("debug", "Not banned: %s from %s", ip, to) | |
| 55 end | |
| 56 | |
| 57 function module.add_host(module) | |
| 58 module:hook("presence/full", check_for_incoming_ban); | |
| 59 module:hook("pre-presence/full", check_for_ban); | |
| 60 end |