mod_muc_limits/mod_muc_limits.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 15 Jan 2012 01:36:11 +0000
changeset 555 2356ad05fdb6
parent 554 a2b0174b5c48
child 556 e50bdbaa7802
permissions -rw-r--r--
mod_muc_limits: Don't limit room leaving
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
     2
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
     3
local new_throttle = require "util.throttle".create;
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
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
     5
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
     6
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
     7
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
     8
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
     9
	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
    10
	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
    11
		return;
2356ad05fdb6 mod_muc_limits: Don't limit room leaving
Matthew Wild <mwild1@gmail.com>
parents: 554
diff changeset
    12
	end
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
    13
	local dest_room, dest_host, dest_nick = jid.split(stanza.attr.to);
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
    14
	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
    15
	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
    16
	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
    17
	local occupant = room._occupants[room._jid_nick[from_jid]];
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
    18
	if occupant and occupant.affiliation 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
    19
		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
    20
		return;
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
    21
	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
    22
	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
    23
	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
    24
		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
    25
		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
    26
	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
    27
	if not throttle:poll(1) 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
    28
		module:log("warn", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid);
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
		origin.send(st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later"));
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
		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
    31
	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
    32
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
    33
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:hook("message/bare", handle_stanza, 10);
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
module:hook("message/full", handle_stanza, 10);
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
    36
module:hook("presence/bare", handle_stanza, 10);
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
    37
module:hook("presence/full", handle_stanza, 10);