diff --git a/PBot/AntiFlood.pm b/PBot/AntiFlood.pm index f7e02b1b..e7aa4d3d 100644 --- a/PBot/AntiFlood.pm +++ b/PBot/AntiFlood.pm @@ -15,7 +15,7 @@ use feature 'switch'; use vars qw($VERSION); $VERSION = $PBot::PBot::VERSION; -use Time::HiRes qw(gettimeofday); +use Time::HiRes qw(gettimeofday tv_interval); use Time::Duration; use Carp (); @@ -48,9 +48,15 @@ sub initialize { $self->{last_timestamp} = gettimeofday; $self->{message_history} = {}; + $self->{lag_history} = []; + $self->{LAG_HISTORY_MAX} = 3; + $self->{LAG_HISTORY_INTERVAL} = 10; + $pbot->timer->register(sub { $self->prune_message_history }, 60 * 60 * 1); + $pbot->timer->register(sub { $self->send_ping }, $self->{LAG_HISTORY_INTERVAL}); $pbot->commands->register(sub { return $self->unbanme(@_) }, "unbanme", 0); + $pbot->commands->register(sub { return $self->lagcheck(@_) }, "lagcheck", 0); } sub get_flood_account { @@ -286,6 +292,47 @@ sub prune_message_history { } } +sub send_ping { + my $self = shift; + + return unless defined $self->{pbot}->conn; + + $self->{ping_send_time} = [gettimeofday]; + $self->{pbot}->conn->sl("PING :lagcheck"); + # $self->{pbot}->logger->log("sent lagcheck PING\n"); +} + +sub on_pong { + my $self = shift; + + my $elapsed = tv_interval($self->{ping_send_time}); + push @{ $self->{lag_history} }, $elapsed; + + # $self->{pbot}->logger->log("got lagcheck PONG\n"); + # $self->{pbot}->logger->log("Lag: $elapsed\n"); + + my $len = @{ $self->{lag_history} }; + + if($len > $self->{LAG_HISTORY_MAX}) { + shift @{ $self->{lag_history} }; + $len--; + } + + my $lag = 0; + foreach my $l (@{ $self->{lag_history} }) { + $lag += $l; + } + + $self->{lag} = $lag / $len; +} + +sub lagcheck { + my ($self, $from, $nick, $user, $host, $arguments) = @_; + + my $lag = $self->{lag} || "initializing"; + return "Lag: $lag"; +} + sub unbanme { my ($self, $from, $nick, $user, $host, $arguments) = @_; my $channel = lc $arguments; diff --git a/PBot/PBot.pm b/PBot/PBot.pm index 06ce4320..36940bf5 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -214,6 +214,7 @@ sub connect { $self->conn->add_handler('part' , sub { $self->irchandlers->on_departure(@_) }); $self->conn->add_handler('join' , sub { $self->irchandlers->on_join(@_) }); $self->conn->add_handler('quit' , sub { $self->irchandlers->on_departure(@_) }); + $self->conn->add_handler('pong' , sub { $self->antiflood->on_pong(@_) }); } #main loop diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 495b88c4..d7ab0056 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 245, + BUILD_REVISION => 246, BUILD_DATE => "2011-01-22", };