Software /
code /
prosody-modules
Annotate
mod_muc_limits/mod_muc_limits.lua @ 1047:38781835c911
mod_lastlog: Keep track of last logout time
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 03 Jun 2013 19:03:42 +0200 |
parent | 1042:5fd0860c86cd |
child | 1057:0b41122b19f9 |
rev | line source |
---|---|
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
1042
5fd0860c86cd
mod_muc_limits: Allow stanzas from affiliated users even if they are not in the room
Matthew Wild <mwild1@gmail.com>
parents:
1040
diff
changeset
|
2 local jid_split, jid_bare = require "util.jid".split, require "util.jid".bare; |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local st = require "util.stanza"; |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local new_throttle = require "util.throttle".create; |
1038
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
5 local t_insert, t_concat = table.insert, table.concat; |
1042
5fd0860c86cd
mod_muc_limits: Allow stanzas from affiliated users even if they are not in the room
Matthew Wild <mwild1@gmail.com>
parents:
1040
diff
changeset
|
6 local hosts = prosody.hosts; |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
557
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
8 local xmlns_muc = "http://jabber.org/protocol/muc"; |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
9 |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local period = math.max(module:get_option_number("muc_event_rate", 0.5), 0); |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local burst = math.max(module:get_option_number("muc_burst_factor", 6), 1); |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
1036
a44e755f7579
mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents:
1035
diff
changeset
|
13 local max_nick_length = module:get_option_number("muc_max_nick_length", 23); -- Default chosen through scientific methods |
1038
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
14 local dropped_count = 0; |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
15 local dropped_jids; |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
16 |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
17 local function log_dropped() |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
18 module:log("warn", "Dropped %d stanzas from %d JIDs: %s", dropped_count, #dropped_jids, t_concat(dropped_jids, ", ")); |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
19 dropped_count = 0; |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
20 dropped_jids = nil; |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
21 end |
1036
a44e755f7579
mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents:
1035
diff
changeset
|
22 |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local function handle_stanza(event) |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local origin, stanza = event.origin, event.stanza; |
555
2356ad05fdb6
mod_muc_limits: Don't limit room leaving
Matthew Wild <mwild1@gmail.com>
parents:
554
diff
changeset
|
25 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Don't limit room leaving |
2356ad05fdb6
mod_muc_limits: Don't limit room leaving
Matthew Wild <mwild1@gmail.com>
parents:
554
diff
changeset
|
26 return; |
2356ad05fdb6
mod_muc_limits: Don't limit room leaving
Matthew Wild <mwild1@gmail.com>
parents:
554
diff
changeset
|
27 end |
1042
5fd0860c86cd
mod_muc_limits: Allow stanzas from affiliated users even if they are not in the room
Matthew Wild <mwild1@gmail.com>
parents:
1040
diff
changeset
|
28 local dest_room, dest_host, dest_nick = jid_split(stanza.attr.to); |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local room = hosts[module.host].modules.muc.rooms[dest_room.."@"..dest_host]; |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 if not room then return; end |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local from_jid = stanza.attr.from; |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 local occupant = room._occupants[room._jid_nick[from_jid]]; |
1042
5fd0860c86cd
mod_muc_limits: Allow stanzas from affiliated users even if they are not in the room
Matthew Wild <mwild1@gmail.com>
parents:
1040
diff
changeset
|
33 if (occupant and occupant.affiliation) or (not(occupant) and room._affiliations[jid_bare(from_jid)]) then |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 module:log("debug", "Skipping stanza from affiliated user..."); |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 return; |
1036
a44e755f7579
mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents:
1035
diff
changeset
|
36 elseif max_nick_length and stanza.name == "presence" and not room._occupants[stanza.attr.to] and #dest_nick > max_nick_length then |
a44e755f7579
mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents:
1035
diff
changeset
|
37 module:log("debug", "Forbidding long (%d bytes) nick in %s", #dest_nick, dest_room) |
a44e755f7579
mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents:
1035
diff
changeset
|
38 origin.send(st.error_reply(stanza, "modify", "policy-violation", "Your nick name is too long, please use a shorter one") |
a44e755f7579
mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents:
1035
diff
changeset
|
39 :up():tag("x", { xmlns = xmlns_muc })); |
a44e755f7579
mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents:
1035
diff
changeset
|
40 return true; |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 local throttle = room.throttle; |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if not room.throttle then |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 throttle = new_throttle(period*burst, burst); |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 room.throttle = throttle; |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 if not throttle:poll(1) then |
1038
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
48 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid); |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
49 if not dropped_jids then |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
50 dropped_jids = { [from_jid] = true, from_jid }; |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
51 module:add_timer(5, log_dropped); |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
52 elseif not dropped_jids[from_jid] then |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
53 dropped_jids[from_jid] = true; |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
54 t_insert(dropped_jids, from_jid); |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
55 end |
edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
56 dropped_count = dropped_count + 1; |
557
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
57 local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later"); |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
58 local body = stanza:get_child_text("body"); |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
59 if body then |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
60 reply:up():tag("body"):text(body):up(); |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
61 end |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
62 local x = stanza:get_child("x", xmlns_muc); |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
63 if x then |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
64 reply:add_child(st.clone(x)); |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
65 end |
14f39769c9e0
mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents:
556
diff
changeset
|
66 origin.send(reply); |
554
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 return true; |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
a2b0174b5c48
mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
556
e50bdbaa7802
mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents:
555
diff
changeset
|
71 function module.unload() |
e50bdbaa7802
mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents:
555
diff
changeset
|
72 for room_jid, room in pairs(hosts[module.host].modules.muc.rooms) do |
e50bdbaa7802
mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents:
555
diff
changeset
|
73 room.throttle = nil; |
e50bdbaa7802
mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents:
555
diff
changeset
|
74 end |
e50bdbaa7802
mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents:
555
diff
changeset
|
75 end |
e50bdbaa7802
mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents:
555
diff
changeset
|
76 |
1040
6574303a8169
mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents:
1038
diff
changeset
|
77 module:hook("message/bare", handle_stanza, 501); |
6574303a8169
mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents:
1038
diff
changeset
|
78 module:hook("message/full", handle_stanza, 501); |
6574303a8169
mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents:
1038
diff
changeset
|
79 module:hook("presence/bare", handle_stanza, 501); |
6574303a8169
mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents:
1038
diff
changeset
|
80 module:hook("presence/full", handle_stanza, 501); |