From dd4be2184c8f94d20989a523869342080486abc3 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 7 Aug 2024 14:48:05 -0700 Subject: [PATCH] Add Plugins/AntiHello to warn about stand-alone greetings in large channels --- data/plugin_autoload | 1 + lib/PBot/Plugin/AntiAway.pm | 4 +- lib/PBot/Plugin/AntiHello.pm | 108 +++++++++++++++++++++++++++++++++++ lib/PBot/VERSION.pm | 4 +- 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 lib/PBot/Plugin/AntiHello.pm diff --git a/data/plugin_autoload b/data/plugin_autoload index 979d065b..0eb0e254 100644 --- a/data/plugin_autoload +++ b/data/plugin_autoload @@ -1,5 +1,6 @@ ActionTrigger AntiAway +AntiHello AntiKickAutoRejoin AntiNickSpam AntiRepeat diff --git a/lib/PBot/Plugin/AntiAway.pm b/lib/PBot/Plugin/AntiAway.pm index ddbacd58..58e5e499 100644 --- a/lib/PBot/Plugin/AntiAway.pm +++ b/lib/PBot/Plugin/AntiAway.pm @@ -34,8 +34,10 @@ sub unload($self) { sub punish($self, $msg, $channel, $nick, $user, $host) { $self->{kick_counter}->{$channel}->{$nick}++; + $self->{pbot}->{logger}->log("[anti-away] $nick!$user\@$host offense $self->{kick_counter}->{$channel}->{$nick}\n"); + if ($self->{kick_counter}->{$channel}->{$nick} == 2) { - $msg .= ' (WARNING: next offense will result in a temp ban)'; + $msg .= ' (WARNING: next offense will result in a temp-ban)'; } elsif ($self->{kick_counter}->{$channel}->{$nick} > 2) { $msg .= ' (temp ban for repeated offenses)'; } diff --git a/lib/PBot/Plugin/AntiHello.pm b/lib/PBot/Plugin/AntiHello.pm new file mode 100644 index 00000000..97ed56a7 --- /dev/null +++ b/lib/PBot/Plugin/AntiHello.pm @@ -0,0 +1,108 @@ +# File: AntiHello.pm +# +# Purpose: Handles people that do stand-alone channel greetings without any +# other meaningful content. +# This plugin is opt-in only. Set #channel.nohello to 1 to enable. + +# SPDX-FileCopyrightText: 2024 Pragmatic Software +# SPDX-License-Identifier: MIT + +package PBot::Plugin::AntiHello; +use parent 'PBot::Plugin::Base'; + +use PBot::Imports; + +sub initialize($self, %conf) { + $self->{pbot}->{registry}->add_default('text', 'antihello', 'bad_greetings', + $conf{bad_greetings} // '^\s*(?:[[:punct:]]|\p{Emoticons})*\s*(?:h*e+l+l+o+|h*e+n+l+o+|l+o+|hi+|g+r+e+t+s*z*|g+r+e+t+i+n+g+s*|h*o+l+a+|o+i+|h*e+y+|h*a+y+)\s*(?:everyone|guys|peeps?z?|ppl|people|\s+.{1,20})*\s*(?:[[:punct:]]|\p{Emoticons})*\s*$' + ); + + $self->{pbot}->{registry}->add_default('text', 'antihello', 'kick_msg', 'https://nohello.net/'); + + $self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->on_action(@_) }); + $self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_public(@_) }); + + $self->{offense_counter} = {}; + $self->{last_warning} = 0; +} + +sub unload($self) { + $self->{pbot}->{event_dispatcher}->remove_handler('irc.caction'); + $self->{pbot}->{event_dispatcher}->remove_handler('irc.public'); +} + +sub punish($self, $msg, $channel, $nick, $user, $host) { + $self->{offense_counter}->{$channel}->{$nick}++; + + $self->{pbot}->{logger}->log("[anti-hello] $nick!$user\@$host offense $self->{offense_counter}->{$channel}->{$nick}\n"); + + if ($self->{offense_counter}->{$channel}->{$nick} == 1) { + # just do a private warning message for the first offense + my $now = time; + + if ($now - $self->{last_warning} >= 60 * 15) { + $self->{last_warning} = $now; + $self->{pbot}->{conn}->privmsg($channel, "Please do not send stand-alone channel greeting messages; include your question/statement along with the greeting. For more info, see https://nohello.net/ (repeated offenses will result in an automatic ban)"); + } + return 0; + } elsif ($self->{offense_counter}->{$channel}->{$nick} == 2) { + $msg .= ' (WARNING: next offense will result in a temp-ban)'; + } elsif ($self->{offense_counter}->{$channel}->{$nick} > 2) { + $msg .= ' (temp ban for repeated offenses)'; + } + + $self->{pbot}->{chanops}->add_op_command($channel, "kick $channel $nick $msg"); + $self->{pbot}->{chanops}->gain_ops($channel); + + if ($self->{offense_counter}->{$channel}->{$nick} > 2) { + my $botnick = $self->{pbot}->{conn}->nick; + $self->{pbot}->{banlist}->ban_user_timed($channel, 'b', "*!*\@$host", 60 * 60 * 2, $botnick, 'anti-hello'); + } +} + +sub on_public($self, $event_type, $event) { + my ($nick, $user, $host, $msg) = ($event->nick, $event->user, $event->host, $event->args); + my $channel = $event->{to}[0]; + + return 0 if $channel !~ /^#/; + return 0 if not $self->{pbot}->{registry}->get_value($channel, 'nohello'); + return 0 if not $self->{pbot}->{chanops}->can_gain_ops($channel); + + my $u = $self->{pbot}->{users}->loggedin($channel, "$nick!$user\@$host"); + return 0 if $self->{pbot}->{capabilities}->userhas($u, 'is-whitelisted'); + + my $bad_greetings = $self->{pbot}->{registry}->get_value('antihello', 'bad_greetings'); + + if ($msg =~ m/$bad_greetings/i) { + my $kick_msg = $self->{pbot}->{registry}->get_value('antihello', 'kick_msg'); + $self->punish($kick_msg, $channel, $nick, $user, $host); + } + return 0; +} + +sub on_action($self, $event_type, $event) { + my ($nick, $user, $host, $msg, $channel) = ( + $event->nick, + $event->user, + $event->host, + $event->{args}[0], + $event->{to}[0], + ); + + return 0 if $channel !~ /^#/; + return 0 if not $self->{pbot}->{registry}->get_value($channel, 'nohello'); + return 0 if not $self->{pbot}->{chanops}->can_gain_ops($channel); + + my $u = $self->{pbot}->{users}->loggedin($channel, "$nick!$user\@$host"); + return 0 if $self->{pbot}->{capabilities}->userhas($u, 'is-whitelisted'); + + my $bad_actions = $self->{pbot}->{registry}->get_value('antihello', 'bad_greetings'); + + if ($msg =~ m/$bad_actions/i) { + my $kick_msg = $self->{pbot}->{registry}->get_value('antihello', 'kick_msg'); + $self->punish($kick_msg, $channel, $nick, $user, $host); + } + return 0; +} + +1; diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 2dad4ef9..9a927e23 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4774, - BUILD_DATE => "2024-08-02", + BUILD_REVISION => 4775, + BUILD_DATE => "2024-08-07", }; sub initialize {}