Mercurial > prosody-modules
comparison mod_muc_limits/mod_muc_limits.lua @ 554:a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 15 Jan 2012 01:08:15 +0000 |
parents | |
children | 2356ad05fdb6 |
comparison
equal
deleted
inserted
replaced
553:7310ceb7564f | 554:a2b0174b5c48 |
---|---|
1 | |
2 local st = require "util.stanza"; | |
3 local new_throttle = require "util.throttle".create; | |
4 | |
5 local period = math.max(module:get_option_number("muc_event_rate", 0.5), 0); | |
6 local burst = math.max(module:get_option_number("muc_burst_factor", 6), 1); | |
7 | |
8 local function handle_stanza(event) | |
9 local origin, stanza = event.origin, event.stanza; | |
10 local dest_room, dest_host, dest_nick = jid.split(stanza.attr.to); | |
11 local room = hosts[module.host].modules.muc.rooms[dest_room.."@"..dest_host]; | |
12 if not room then return; end | |
13 local from_jid = stanza.attr.from; | |
14 local occupant = room._occupants[room._jid_nick[from_jid]]; | |
15 if occupant and occupant.affiliation then | |
16 module:log("debug", "Skipping stanza from affiliated user..."); | |
17 return; | |
18 end | |
19 local throttle = room.throttle; | |
20 if not room.throttle then | |
21 throttle = new_throttle(period*burst, burst); | |
22 room.throttle = throttle; | |
23 end | |
24 if not throttle:poll(1) then | |
25 module:log("warn", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid); | |
26 origin.send(st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later")); | |
27 return true; | |
28 end | |
29 end | |
30 | |
31 module:hook("message/bare", handle_stanza, 10); | |
32 module:hook("message/full", handle_stanza, 10); | |
33 module:hook("presence/bare", handle_stanza, 10); | |
34 module:hook("presence/full", handle_stanza, 10); |