Annotate

mod_throttle_unsolicited/mod_throttle_unsolicited.lua @ 2142:d6fbb57a216c

mod_throttle_unsolicited: Skip checking messages to self
author Kim Alvefur <zash@zash.se>
date Wed, 30 Mar 2016 10:54:47 +0200
parent 2120:f6dcfe263b85
child 2143:7cab309a26b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require"util.stanza";
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local jid_split = require "util.jid".split;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local jid_bare = require "util.jid".bare;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local throttle = require "util.throttle";
2120
f6dcfe263b85 mod_throttle_unsolicited: Mark sessions so they can be matched with 'ORIGIN_MARKED: throttle_unsolicited' by mod_firewall
Kim Alvefur <zash@zash.se>
parents: 2082
diff changeset
6 local gettime = require "socket".gettime;
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local sessions = prosody.full_sessions;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local max = module:get_option_number("unsolicited_messages_per_minute", 10);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local multiplier = module:get_option_number("throttle_unsolicited_burst", 1);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 function check_subscribed(event)
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local stanza, origin = event.stanza, event.origin;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local log = origin.log or module._log;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 log("debug", "check_subscribed(%s)", stanza:top_tag());
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 if stanza.attr.type == "error" then return end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
2142
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
19 local to_orig = stanza.attr.to;
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
20 if to_orig == nil or to_orig == origin.full_jid then return end -- to self
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
21
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
22 local to_bare = jid_bare(to_orig);
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
23 local from_jid = jid_bare(stanza.attr.from);
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
24 if to_bare == from_jid then return end -- to own resource
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
25
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 -- Check if it's a message to a joined room
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local rooms = origin.rooms_joined;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 if rooms and rooms[to_bare] then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 log("debug", "Message to joined room, no limit");
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 return
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 -- Retrieve or create throttle object
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local lim = origin.throttle_unsolicited;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if not lim then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 log("debug", "New throttle");
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 lim = throttle.create(max * multiplier, 60 * multiplier);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 origin.throttle_unsolicited = lim;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
2142
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
41 local to_user, to_host = jid_split(to_orig);
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if to_user and not is_contact_subscribed(to_user, to_host, from_jid) then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 log("debug", "%s is not subscribed to %s@%s", from_jid, to_user, to_host);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 if not lim:poll(1) then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 log("warn", "Sent too many messages to non-contacts, bouncing message");
2120
f6dcfe263b85 mod_throttle_unsolicited: Mark sessions so they can be matched with 'ORIGIN_MARKED: throttle_unsolicited' by mod_firewall
Kim Alvefur <zash@zash.se>
parents: 2082
diff changeset
46 event.origin.firewall_mark_throttle_unsolicited = gettime();
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 event.origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return true;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 module:hook("pre-message/bare", check_subscribed, 200);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 module:hook("pre-message/full", check_subscribed, 200);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 module:depends("track_muc_joins");