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';
|
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
use Time::Duration qw/concise duration/;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2020-03-04 22:24:40 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{filename} = $conf{filename};
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
$self->{ignorelist} = PBot::DualIndexHashObject->new(pbot => $self->{pbot}, name => 'IgnoreList', filename => $self->{filename});
|
|
|
|
$self->{ignorelist}->load;
|
|
|
|
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->ignore_cmd(@_) }, "ignore", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->unignore_cmd(@_) }, "unignore", 1);
|
2014-05-18 02:27:57 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
$self->{pbot}->{capabilities}->add('admin', 'can-ignore', 1);
|
|
|
|
$self->{pbot}->{capabilities}->add('admin', 'can-unignore', 1);
|
|
|
|
|
|
|
|
$self->{pbot}->{capabilities}->add('chanop', 'can-ignore', 1);
|
|
|
|
$self->{pbot}->{capabilities}->add('chanop', 'can-unignore', 1);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01: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 {
|
2020-03-04 22:24:40 +01:00
|
|
|
my ($self, $channel, $hostmask, $length, $owner) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
if ($hostmask !~ /!/) {
|
|
|
|
$hostmask .= '!*@*';
|
|
|
|
} elsif ($hostmask !~ /@/) {
|
|
|
|
$hostmask .= '@*';
|
|
|
|
}
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
my $regex = quotemeta $hostmask;
|
|
|
|
$regex =~ s/\\\*/.*/g;
|
|
|
|
$regex =~ s/\\\?/./g;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
my $data = {
|
|
|
|
owner => $owner,
|
|
|
|
created_on => time,
|
|
|
|
regex => $regex,
|
|
|
|
};
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
if ($length < 0) {
|
|
|
|
$data->{timeout} = -1;
|
|
|
|
} else {
|
|
|
|
$data->{timeout} = time + $length;
|
|
|
|
}
|
2015-03-17 05:08:25 +01:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
$self->{ignorelist}->add($channel, $hostmask, $data);
|
2015-03-17 05:08:25 +01:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
my $duration = $data->{timeout} == -1 ? 'all eternity' : duration $length;
|
|
|
|
return "$hostmask ignored for $duration";
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
sub remove {
|
|
|
|
my ($self, $channel, $hostmask) = @_;
|
|
|
|
return $self->{ignorelist}->remove($channel, $hostmask);
|
2017-08-06 06:38:46 +02:00
|
|
|
}
|
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
sub is_ignored {
|
|
|
|
my ($self, $channel, $hostmask) = @_;
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
return 0 if $self->{pbot}->{users}->loggedin_admin($channel, $hostmask);
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
foreach my $chan ('.*', $channel) {
|
|
|
|
foreach my $ignored ($self->{ignorelist}->get_keys($chan)) {
|
|
|
|
my $regex = $self->{ignorelist}->get_data($chan, $ignored, 'regex');
|
|
|
|
return 1 if $hostmask =~ /^$regex$/i;
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
return 0;
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
sub check_ignore_timeouts {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $now = time;
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
foreach my $channel ($self->{ignorelist}->get_keys) {
|
|
|
|
foreach my $hostmask ($self->{ignorelist}->get_keys($channel)) {
|
|
|
|
my $timeout = $self->{ignorelist}->get_data($channel, $hostmask, 'timeout');
|
|
|
|
next if $timeout == -1; # permanent ignore
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
if ($now >= $timeout) {
|
|
|
|
$self->{pbot}->{logger}->log("Unignoring $hostmask in channel $channel.\n");
|
|
|
|
$self->remove($channel, $hostmask);
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2010-04-06 20:38:27 +02:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
sub ignore_cmd {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
|
|
|
|
|
|
|
my ($target, $channel, $length) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 3);
|
|
|
|
|
|
|
|
return "Usage: ignore <hostmask> [channel [timeout]] | ignore list" if not defined $target;
|
|
|
|
|
|
|
|
if ($target =~ /^list$/i) {
|
|
|
|
my $text = "Ignored: ";
|
|
|
|
my $now = time;
|
|
|
|
my $ignored = 0;
|
|
|
|
|
|
|
|
foreach my $channel (sort $self->{ignorelist}->get_keys) {
|
|
|
|
$text .= $channel eq '.*' ? "global:\n" : "$channel:\n";
|
|
|
|
my @list = ();
|
|
|
|
foreach my $hostmask (sort $self->{ignorelist}->get_keys($channel)) {
|
|
|
|
my $timeout = $self->{ignorelist}->get_data($channel, $hostmask, 'timeout');
|
|
|
|
if ($timeout == -1) {
|
|
|
|
push @list, " $hostmask";
|
|
|
|
} else {
|
|
|
|
push @list, " $hostmask (" . (concise duration $timeout - $now) . ')';
|
|
|
|
}
|
|
|
|
$ignored++;
|
|
|
|
}
|
|
|
|
$text .= join ";\n", @list;
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2020-03-04 22:24:40 +01:00
|
|
|
return "Ignore list is empty." if not $ignored;
|
|
|
|
return "/msg $nick $text";
|
2010-03-23 04:09:03 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
if (not defined $channel) {
|
|
|
|
$channel = ".*"; # all channels
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2012-09-06 12:09:44 +02:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
if (not defined $length) {
|
|
|
|
$length = -1; # permanently
|
|
|
|
} else {
|
|
|
|
my $error;
|
|
|
|
($length, $error) = $self->{pbot}->{parsedate}->parsedate($length);
|
|
|
|
return $error if defined $error;
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2020-03-04 22:24:40 +01:00
|
|
|
|
|
|
|
return $self->add($channel, $target, $length, "$nick!$user\@$host");
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
sub unignore_cmd {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
|
|
|
my ($target, $channel) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2);
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
if (not defined $target) { return "Usage: unignore <hostmask> [channel]"; }
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-03-04 22:24:40 +01:00
|
|
|
if (not defined $channel) { $channel = '.*'; }
|
|
|
|
|
|
|
|
return $self->remove($channel, $target);
|
2010-03-23 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
1;
|