3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-29 23:39:24 +01:00

anti-flood: added last offense timestamp; decrease offense counter once every 24 hours without offense

This commit is contained in:
Pragmatic Software 2011-01-27 06:17:39 +00:00
parent 31ae068202
commit 8d524089db
3 changed files with 23 additions and 8 deletions

View File

@ -46,8 +46,6 @@ sub initialize {
$self->{FLOOD_CHAT} = 0; $self->{FLOOD_CHAT} = 0;
$self->{FLOOD_JOIN} = 1; $self->{FLOOD_JOIN} = 1;
$self->{flood_msg_count} = 0;
$self->{last_timestamp} = gettimeofday;
$self->{message_history} = {}; $self->{message_history} = {};
$pbot->timer->register(sub { $self->prune_message_history }, 60 * 60 * 1); $pbot->timer->register(sub { $self->prune_message_history }, 60 * 60 * 1);
@ -154,6 +152,7 @@ sub check_flood {
if(not exists ${ $self->message_history }{$account}{$chan}) { if(not exists ${ $self->message_history }{$account}{$chan}) {
#$self->{pbot}->logger->log("adding new channel for existing nick\n"); #$self->{pbot}->logger->log("adding new channel for existing nick\n");
${ $self->message_history }{$account}{$chan}{offenses} = 0; ${ $self->message_history }{$account}{$chan}{offenses} = 0;
${ $self->message_history }{$account}{$chan}{last_offense_timestamp} = 0;
${ $self->message_history }{$account}{$chan}{join_watch} = 0; ${ $self->message_history }{$account}{$chan}{join_watch} = 0;
${ $self->message_history }{$account}{$chan}{messages} = []; ${ $self->message_history }{$account}{$chan}{messages} = [];
} }
@ -168,6 +167,7 @@ sub check_flood {
if(not exists ${ $self->message_history }{$account}{$channel}) { if(not exists ${ $self->message_history }{$account}{$channel}) {
#$self->{pbot}->logger->log("adding new channel for existing nick\n"); #$self->{pbot}->logger->log("adding new channel for existing nick\n");
${ $self->message_history }{$account}{$channel}{offenses} = 0; ${ $self->message_history }{$account}{$channel}{offenses} = 0;
${ $self->message_history }{$account}{$channel}{last_offense_timestamp} = 0;
${ $self->message_history }{$account}{$channel}{join_watch} = 0; ${ $self->message_history }{$account}{$channel}{join_watch} = 0;
${ $self->message_history }{$account}{$channel}{messages} = []; ${ $self->message_history }{$account}{$channel}{messages} = [];
} }
@ -216,6 +216,7 @@ sub check_flood {
if($mode == $self->{FLOOD_JOIN}) { if($mode == $self->{FLOOD_JOIN}) {
if(${ $self->message_history }{$account}{$channel}{join_watch} >= $max_messages) { if(${ $self->message_history }{$account}{$channel}{join_watch} >= $max_messages) {
${ $self->message_history }{$account}{$channel}{offenses}++; ${ $self->message_history }{$account}{$channel}{offenses}++;
${ $self->message_history }{$account}{$channel}{last_offense_timestamp} = gettimeofday;
my $timeout = (2 ** (($self->message_history->{$account}{$channel}{offenses} + 2) < 10 ? ${ $self->message_history }{$account}{$channel}{offenses} + 2 : 10)); my $timeout = (2 ** (($self->message_history->{$account}{$channel}{offenses} + 2) < 10 ? ${ $self->message_history }{$account}{$channel}{offenses} + 2 : 10));
@ -233,6 +234,7 @@ sub check_flood {
} }
} elsif($mode == $self->{FLOOD_CHAT}) { } elsif($mode == $self->{FLOOD_CHAT}) {
${ $self->message_history }{$account}{$channel}{offenses}++; ${ $self->message_history }{$account}{$channel}{offenses}++;
${ $self->message_history }{$account}{$channel}{last_offense_timestamp} = gettimeofday;
my $length = ${ $self->message_history }{$account}{$channel}{offenses} ** ${ $self->message_history }{$account}{$channel}{offenses} * ${ $self->message_history }{$account}{$channel}{offenses} * 30; my $length = ${ $self->message_history }{$account}{$channel}{offenses} ** ${ $self->message_history }{$account}{$channel}{offenses} * ${ $self->message_history }{$account}{$channel}{offenses} * 30;
if($channel =~ /^#/) { #channel flood (opposed to private message or otherwise) if($channel =~ /^#/) { #channel flood (opposed to private message or otherwise)
# don't ban again if already banned # don't ban again if already banned
@ -289,6 +291,13 @@ sub prune_message_history {
if(gettimeofday - $last{timestamp} >= 60 * 60 * 24 * 3) { if(gettimeofday - $last{timestamp} >= 60 * 60 * 24 * 3) {
$self->{pbot}->logger->log("$nick in $channel hasn't spoken in three days, removing message history.\n"); $self->{pbot}->logger->log("$nick in $channel hasn't spoken in three days, removing message history.\n");
delete $self->{message_history}->{$nick}{$channel}; delete $self->{message_history}->{$nick}{$channel};
} else {
# decrease offenses counter if 24 hours of elapsed without any new offense
if ($self->{message_history}->{$nick}{$channel}{offenses} > 0 and $self->{message_history}->{$nick}{$channel}{last_offense_timestamp} > 0 and (gettimeofday - $self->{message_history}->{$nick}{$channel}{last_offense_timestamp} >= 60 * 60 * 24)) {
$self->{message_history}->{$nick}{$channel}{offenses}--;
$self->{message_history}->{$nick}{$channel}{last_offense_timestamp} = gettimeofday;
$self->{pbot}->logger->log("anti-flood: [$channel][$nick] 24 hours since last offense/decrease -- decreasing offenses to $self->{message_history}->{$nick}{$channel}{offenses}\n");
}
} }
} }
} }

View File

@ -211,7 +211,7 @@ sub list {
for(my $i = 0; $i <= $#messages; $i++) { for(my $i = 0; $i <= $#messages; $i++) {
next if $messages[$i]->{msg} =~ /^!login/; next if $messages[$i]->{msg} =~ /^!login/;
push @ret, { offenses => ${ $self->{pbot}->antiflood->message_history }{$history_nick}{$history_channel}{offenses}, join_watch => ${ $self->{pbot}->antiflood->message_history }{$history_nick}{$history_channel}{join_watch}, text => $messages[$i]->{msg}, timestamp => $messages[$i]->{timestamp}, nick => $history_nick, channel => $history_channel } if $messages[$i]->{msg} =~ m/$text_search/i; push @ret, { offenses => ${ $self->{pbot}->antiflood->message_history }{$history_nick}{$history_channel}{offenses}, last_offense_timestamp => $self->{pbot}->antiflood->message_history->{$history_nick}{$history_channel}{last_offense_timestamp}, join_watch => ${ $self->{pbot}->antiflood->message_history }{$history_nick}{$history_channel}{join_watch}, text => $messages[$i]->{msg}, timestamp => $messages[$i]->{timestamp}, nick => $history_nick, channel => $history_channel } if $messages[$i]->{msg} =~ m/$text_search/i;
} }
} }
} }
@ -226,13 +226,19 @@ sub list {
} }
my $text = ""; my $text = "";
my %seen_nicks = ();
my @sorted = sort { $a->{timestamp} <=> $b->{timestamp} } @results; my @sorted = sort { $a->{timestamp} <=> $b->{timestamp} } @results;
foreach my $msg (@sorted) { foreach my $msg (@sorted) {
$self->{pbot}->logger->log("[$msg->{channel}] " . localtime($msg->{timestamp}) . " [o: $msg->{offenses}, j: $msg->{join_watch}] <$msg->{nick}> " . $msg->{text} . "\n"); if(not exists $seen_nicks{$msg->{nick}}) {
$text .= "[$msg->{channel}] " . localtime($msg->{timestamp}) . " <$msg->{nick}> " . $msg->{text} . "\n"; $seen_nicks{$msg->{nick}} = 1;
$text .= "--- [$msg->{nick}: join counter: $msg->{join_watch}; offenses: $msg->{offenses}; last offense/decrease: " . ($msg->{last_offense_timestamp} > 0 ? ago(gettimeofday - $msg->{last_offense_timestamp}) : "unknown") . "]\n";
}
$text .= "[$msg->{channel}] " . localtime($msg->{timestamp}) . " <$msg->{nick}> " . $msg->{text} . "\n";
} }
return "Messages: $text"; $self->{pbot}->logger->log($text);
return "Messages:\n\n$text";
} }
if($arguments =~ /^modules$/i) { if($arguments =~ /^modules$/i) {

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 273, BUILD_REVISION => 274,
BUILD_DATE => "2011-01-26", BUILD_DATE => "2011-01-26",
}; };