2017-12-31 02:27:48 +01:00
|
|
|
# File: AntiNickSpam.pm
|
|
|
|
#
|
|
|
|
# Purpose: Temporarily mutes $~a in channel if too many nicks were
|
|
|
|
# mentioned within a time period; used to combat botnet spam
|
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2017-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2017-12-31 02:27:48 +01:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
package PBot::Plugin::AntiNickSpam;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
2017-12-31 02:27:48 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2017-12-31 02:27:48 +01:00
|
|
|
use Time::Duration qw/duration/;
|
|
|
|
use Time::HiRes qw/gettimeofday/;
|
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_public(@_) });
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->on_action(@_) });
|
|
|
|
$self->{nicks} = {};
|
2017-12-31 02:27:48 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:24:21 +01:00
|
|
|
sub unload {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
$self->{pbot}->{event_dispatcher}->remove_handler('irc.public');
|
|
|
|
$self->{pbot}->{event_dispatcher}->remove_handler('irc.caction');
|
2020-02-07 21:24:21 +01:00
|
|
|
}
|
2017-12-31 02:27:48 +01:00
|
|
|
|
|
|
|
sub on_action {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $event_type, $event) = @_;
|
2023-01-31 14:44:34 +01:00
|
|
|
my ($nick, $user, $host, $msg) = ($event->nick, $event->user, $event->host, $event->args);
|
|
|
|
my $channel = $event->{to}[0];
|
2020-02-15 23:38:32 +01:00
|
|
|
return 0 if $event->{interpreted};
|
|
|
|
$self->check_flood($nick, $user, $host, $channel, $msg);
|
|
|
|
return 0;
|
2017-12-31 02:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_public {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $event_type, $event) = @_;
|
2023-01-31 14:44:34 +01:00
|
|
|
my ($nick, $user, $host, $msg) = ($event->nick, $event->user, $event->host, $event->args);
|
|
|
|
my $channel = $event->{to}[0];
|
2020-02-15 23:38:32 +01:00
|
|
|
return 0 if $event->{interpreted};
|
|
|
|
$self->check_flood($nick, $user, $host, $channel, $msg);
|
|
|
|
return 0;
|
2017-12-31 02:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub check_flood {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $nick, $user, $host, $channel, $msg) = @_;
|
|
|
|
return 0 if not $self->{pbot}->{chanops}->can_gain_ops($channel);
|
|
|
|
|
|
|
|
$channel = lc $channel;
|
|
|
|
my @words = split /\s+/, $msg;
|
|
|
|
my @nicks;
|
|
|
|
|
|
|
|
foreach my $word (@words) {
|
|
|
|
$word =~ s/[:;\+,\.!?\@\%\$]+$//g;
|
|
|
|
if ($self->{pbot}->{nicklist}->is_present($channel, $word) and not grep { $_ eq $word } @nicks) {
|
|
|
|
push @{$self->{nicks}->{$channel}}, [scalar gettimeofday, $word];
|
|
|
|
push @nicks, $word;
|
|
|
|
}
|
2017-12-31 02:27:48 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->clear_old_nicks($channel);
|
2017-12-31 02:27:48 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (exists $self->{nicks}->{$channel} and @{$self->{nicks}->{$channel}} >= 10) {
|
|
|
|
$self->{pbot}->{logger}->log("Nick spam flood detected in $channel\n");
|
2020-04-29 06:33:49 +02:00
|
|
|
$self->{pbot}->{banlist}->ban_user_timed(
|
|
|
|
$channel,
|
|
|
|
'q',
|
|
|
|
'$~a',
|
|
|
|
60 * 15,
|
|
|
|
$self->{pbot}->{registry}->get_value('irc', 'botnick'),
|
|
|
|
'nick spam flooding',
|
|
|
|
);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2017-12-31 02:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub clear_old_nicks {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
|
|
|
my $now = gettimeofday;
|
|
|
|
return if not exists $self->{nicks}->{$channel};
|
|
|
|
|
|
|
|
while (1) {
|
2020-04-29 06:33:49 +02:00
|
|
|
if (@{$self->{nicks}->{$channel}} and $self->{nicks}->{$channel}->[0]->[0] <= $now - 15) {
|
|
|
|
shift @{$self->{nicks}->{$channel}};
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
2017-12-31 02:27:48 +01:00
|
|
|
}
|
2020-04-29 06:33:49 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
delete $self->{nicks}->{$channel} if not @{$self->{nicks}->{$channel}};
|
2017-12-31 02:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|