2010-03-22 08:33:44 +01:00
|
|
|
# File: IgnoreList.pm
|
2010-03-24 07:47:40 +01:00
|
|
|
# Author: pragma_
|
2010-03-17 07:36:54 +01:00
|
|
|
#
|
2010-03-22 08:33:44 +01:00
|
|
|
# Purpose: Manages ignore list.
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
package PBot::IgnoreList;
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::Class';
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
use PBot::IgnoreListCommands;
|
2010-03-17 07:36:54 +01:00
|
|
|
use Time::HiRes qw(gettimeofday);
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
2020-01-25 21:28:05 +01:00
|
|
|
$self->{filename} = $conf{filename};
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{ignore_list} = {};
|
2010-04-06 21:15:42 +02:00
|
|
|
$self->{ignore_flood_counter} = {};
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{last_timestamp} = {};
|
|
|
|
|
|
|
|
$self->{commands} = PBot::IgnoreListCommands->new(pbot => $self->{pbot});
|
2020-01-25 21:28:05 +01:00
|
|
|
$self->load_ignores();
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{timer}->register(sub { $self->check_ignore_timeouts }, 10);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub add {
|
|
|
|
my $self = shift;
|
|
|
|
my ($hostmask, $channel, $length) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($length < 0) {
|
2015-03-17 05:08:25 +01:00
|
|
|
$self->{ignore_list}->{$hostmask}->{$channel} = -1;
|
2010-04-06 20:38:27 +02:00
|
|
|
} else {
|
2015-03-17 05:08:25 +01:00
|
|
|
$self->{ignore_list}->{$hostmask}->{$channel} = gettimeofday + $length;
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$self->save_ignores();
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub remove {
|
|
|
|
my $self = shift;
|
|
|
|
my ($hostmask, $channel) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-03-17 05:08:25 +01:00
|
|
|
delete $self->{ignore_list}->{$hostmask}->{$channel};
|
|
|
|
|
|
|
|
if (not keys %{ $self->{ignore_list}->{$hostmask} }) {
|
|
|
|
delete $self->{ignore_list}->{$hostmask};
|
|
|
|
}
|
|
|
|
|
2010-04-06 20:38:27 +02:00
|
|
|
$self->save_ignores();
|
|
|
|
}
|
|
|
|
|
2017-08-06 06:38:46 +02:00
|
|
|
sub clear_ignores {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{ignore_list} = {};
|
|
|
|
}
|
|
|
|
|
2010-04-06 20:38:27 +02:00
|
|
|
sub load_ignores {
|
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $filename = shift; } else { $filename = $self->{filename}; }
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $filename) {
|
2010-04-06 20:38:27 +02:00
|
|
|
Carp::carp "No ignorelist path specified -- skipping loading of ignorelist";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Loading ignorelist from $filename ...\n");
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2010-04-06 20:38:27 +02:00
|
|
|
open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n";
|
|
|
|
my @contents = <FILE>;
|
|
|
|
close(FILE);
|
|
|
|
|
|
|
|
my $i = 0;
|
|
|
|
|
|
|
|
foreach my $line (@contents) {
|
|
|
|
chomp $line;
|
|
|
|
$i++;
|
|
|
|
|
|
|
|
my ($hostmask, $channel, $length) = split(/\s+/, $line);
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $hostmask || not defined $channel || not defined $length) {
|
2010-04-06 20:38:27 +02:00
|
|
|
Carp::croak "Syntax error around line $i of $filename\n";
|
|
|
|
}
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (exists ${ $self->{ignore_list} }{$hostmask}{$channel}) {
|
2010-04-06 20:38:27 +02:00
|
|
|
Carp::croak "Duplicate ignore [$hostmask][$channel] found in $filename around line $i\n";
|
|
|
|
}
|
|
|
|
|
2015-03-17 05:08:25 +01:00
|
|
|
$self->{ignore_list}->{$hostmask}->{$channel} = $length;
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log(" $i entries in ignorelist\n");
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub save_ignores {
|
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $filename = shift; } else { $filename = $self->{filename}; }
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $filename) {
|
2010-04-06 20:38:27 +02:00
|
|
|
Carp::carp "No ignorelist path specified -- skipping saving of ignorelist\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
|
|
|
|
|
2015-03-17 05:08:25 +01:00
|
|
|
foreach my $hostmask (keys %{ $self->{ignore_list} }) {
|
|
|
|
foreach my $channel (keys %{ $self->{ignore_list}->{$hostmask} }) {
|
|
|
|
my $length = $self->{ignore_list}->{$hostmask}->{$channel};
|
|
|
|
print FILE "$hostmask $channel $length\n";
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(FILE);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub check_ignore {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2016-02-25 09:45:03 +01:00
|
|
|
my ($nick, $user, $host, $channel, $silent) = @_;
|
2010-03-23 04:09:03 +01:00
|
|
|
my $pbot = $self->{pbot};
|
2010-03-17 07:36:54 +01:00
|
|
|
$channel = lc $channel;
|
|
|
|
|
2019-06-26 18:34:19 +02:00
|
|
|
my $hostmask = "$nick!$user\@$host";
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-23 04:09:03 +01:00
|
|
|
my $now = gettimeofday;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $channel) { # do not execute following if text is coming from STDIN ($channel undef)
|
|
|
|
if ($channel =~ /^#/) {
|
2014-04-19 12:34:21 +02:00
|
|
|
$self->{ignore_flood_counter}->{$channel}++;
|
2010-03-23 04:09:03 +01:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not exists $self->{last_timestamp}->{$channel}) {
|
2010-04-06 21:15:42 +02:00
|
|
|
$self->{last_timestamp}->{$channel} = $now;
|
2019-05-28 18:19:42 +02:00
|
|
|
} elsif ($now - $self->{last_timestamp}->{$channel} >= 30) {
|
2010-04-06 21:15:42 +02:00
|
|
|
$self->{last_timestamp}->{$channel} = $now;
|
2019-05-28 18:19:42 +02:00
|
|
|
if (exists $self->{ignore_flood_counter}->{$channel} and $self->{ignore_flood_counter}->{$channel} > 0) {
|
2010-04-14 02:01:42 +02:00
|
|
|
$self->{ignore_flood_counter}->{$channel} = 0;
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-01 01:15:21 +01:00
|
|
|
=cut
|
2019-05-28 18:19:42 +02:00
|
|
|
if (exists $self->{ignore_flood_counter}->{$channel} and $self->{ignore_flood_counter}->{$channel} > 5) {
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{commands}->ignore_user("", "floodcontrol", "", "", ".* $channel 300");
|
2010-04-06 21:15:42 +02:00
|
|
|
$self->{ignore_flood_counter}->{$channel} = 0;
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($channel =~ /^#/) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$pbot->{conn}->me($channel, "has been overwhelmed.");
|
2019-06-26 18:34:19 +02:00
|
|
|
$pbot->{conn}->me($channel, "lies down and falls asleep.");
|
2010-04-06 21:15:42 +02:00
|
|
|
return 1;
|
2019-06-26 18:34:19 +02:00
|
|
|
}
|
2010-03-23 04:09:03 +01:00
|
|
|
}
|
2014-11-01 01:15:21 +01:00
|
|
|
=cut
|
2010-03-23 04:09:03 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
foreach my $ignored (keys %{ $self->{ignore_list} }) {
|
2015-03-17 05:08:25 +01:00
|
|
|
foreach my $ignored_channel (keys %{ $self->{ignore_list}->{$ignored} }) {
|
2012-09-06 12:09:44 +02:00
|
|
|
my $ignored_channel_escaped = quotemeta $ignored_channel;
|
|
|
|
my $ignored_escaped = quotemeta $ignored;
|
|
|
|
|
|
|
|
$ignored_channel_escaped =~ s/\\(\.|\*)/$1/g;
|
|
|
|
$ignored_escaped =~ s/\\(\.|\*)/$1/g;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (($channel =~ /$ignored_channel_escaped/i) && ($hostmask =~ /$ignored_escaped/i)) {
|
2016-02-25 09:45:03 +01:00
|
|
|
$self->{pbot}->{logger}->log("$nick!$user\@$host message ignored in channel $channel (matches [$ignored] host and [$ignored_channel] channel)\n") unless $silent;
|
2010-03-17 07:36:54 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
return 0;
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
sub check_ignore_timeouts {
|
|
|
|
my $self = shift;
|
|
|
|
my $now = gettimeofday();
|
|
|
|
|
|
|
|
foreach my $hostmask (keys %{ $self->{ignore_list} }) {
|
|
|
|
foreach my $channel (keys %{ $self->{ignore_list}->{$hostmask} }) {
|
2019-05-28 18:19:42 +02:00
|
|
|
next if ($self->{ignore_list}->{$hostmask}->{$channel} == -1); #permanent ignore
|
2010-03-23 19:24:02 +01:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($self->{ignore_list}->{$hostmask}->{$channel} < $now) {
|
2015-03-17 05:08:25 +01:00
|
|
|
$self->{pbot}->{logger}->log("Unignoring $hostmask in channel $channel.\n");
|
|
|
|
$self->remove($hostmask, $channel);
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($hostmask eq ".*") {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{conn}->me($channel, "awakens.");
|
2010-03-23 19:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
1;
|