mod_muc_limits/mod_muc_limits.lua
changeset 1038 edb06824a5a4
parent 1036 a44e755f7579
child 1040 6574303a8169
equal deleted inserted replaced
1037:892272432703 1038:edb06824a5a4
     1 
     1 
     2 local jid = require "util.jid";
     2 local jid = require "util.jid";
     3 local st = require "util.stanza";
     3 local st = require "util.stanza";
     4 local new_throttle = require "util.throttle".create;
     4 local new_throttle = require "util.throttle".create;
       
     5 local t_insert, t_concat = table.insert, table.concat;
     5 
     6 
     6 local xmlns_muc = "http://jabber.org/protocol/muc";
     7 local xmlns_muc = "http://jabber.org/protocol/muc";
     7 
     8 
     8 local period = math.max(module:get_option_number("muc_event_rate", 0.5), 0);
     9 local period = math.max(module:get_option_number("muc_event_rate", 0.5), 0);
     9 local burst = math.max(module:get_option_number("muc_burst_factor", 6), 1);
    10 local burst = math.max(module:get_option_number("muc_burst_factor", 6), 1);
    10 
    11 
    11 local max_nick_length = module:get_option_number("muc_max_nick_length", 23); -- Default chosen through scientific methods
    12 local max_nick_length = module:get_option_number("muc_max_nick_length", 23); -- Default chosen through scientific methods
       
    13 local dropped_count = 0;
       
    14 local dropped_jids;
       
    15 
       
    16 local function log_dropped()
       
    17 	module:log("warn", "Dropped %d stanzas from %d JIDs: %s", dropped_count, #dropped_jids, t_concat(dropped_jids, ", "));
       
    18 	dropped_count = 0;
       
    19 	dropped_jids = nil;
       
    20 end
    12 
    21 
    13 local function handle_stanza(event)
    22 local function handle_stanza(event)
    14 	local origin, stanza = event.origin, event.stanza;
    23 	local origin, stanza = event.origin, event.stanza;
    15 	if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Don't limit room leaving
    24 	if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Don't limit room leaving
    16 		return;
    25 		return;
    33 	if not room.throttle then
    42 	if not room.throttle then
    34 		throttle = new_throttle(period*burst, burst);
    43 		throttle = new_throttle(period*burst, burst);
    35 		room.throttle = throttle;
    44 		room.throttle = throttle;
    36 	end
    45 	end
    37 	if not throttle:poll(1) then
    46 	if not throttle:poll(1) then
    38 		module:log("warn", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid);
    47 		module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid);
       
    48 		if not dropped_jids then
       
    49 			dropped_jids = { [from_jid] = true, from_jid };
       
    50 			module:add_timer(5, log_dropped);
       
    51 		elseif not dropped_jids[from_jid] then
       
    52 			dropped_jids[from_jid] = true;
       
    53 			t_insert(dropped_jids, from_jid);
       
    54 		end
       
    55 		dropped_count = dropped_count + 1;
    39 		local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later");
    56 		local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later");
    40 		local body = stanza:get_child_text("body");
    57 		local body = stanza:get_child_text("body");
    41 		if body then
    58 		if body then
    42 			reply:up():tag("body"):text(body):up();
    59 			reply:up():tag("body"):text(body):up();
    43 		end
    60 		end