From a0a61ff0d572545adcb8667bbab9f4fafe0b9bb7 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 22 Jul 2024 13:47:30 -0700 Subject: [PATCH] Plugin/AntiAway: monitor PRIVMSG for away nicks --- lib/PBot/Plugin/AntiAway.pm | 46 ++++++++++++++++++++++++++++++++----- lib/PBot/VERSION.pm | 4 ++-- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/PBot/Plugin/AntiAway.pm b/lib/PBot/Plugin/AntiAway.pm index 8571ff54..5067f724 100644 --- a/lib/PBot/Plugin/AntiAway.pm +++ b/lib/PBot/Plugin/AntiAway.pm @@ -12,7 +12,7 @@ use PBot::Imports; sub initialize($self, %conf) { $self->{pbot}->{registry}->add_default('text', 'antiaway', 'bad_nicks', - $conf{bad_nicks} // '([[:punct:]](afk|brb|bbl|away|sleep|z+|work|gone|study|out|home|busy|off)[[:punct:]]*$|.+\[.*\]$)' + $conf{bad_nicks} // '(^zz+[[:punct:]]|[[:punct:]](afk|brb|bbl|away|a?sleep|nap|zz+|work|gone|study|out|home|busy|off)[[:punct:]]*$|afk$)' ); $self->{pbot}->{registry}->add_default('text', 'antiaway', 'bad_actions', $conf{bad_actions} // '^/me (is (away|gone)|.*auto.?away)'); @@ -20,6 +20,9 @@ sub initialize($self, %conf) { $self->{pbot}->{event_dispatcher}->register_handler('irc.nick', sub { $self->on_nickchange(@_) }); $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->{kick_counter} = {}; } sub unload($self) { @@ -27,6 +30,40 @@ sub unload($self) { $self->{pbot}->{event_dispatcher}->remove_handler('irc.caction'); } +sub punish($self, $msg, $channel, $nick, $user, $host) { + $self->{kick_counter}->{$channel}->{$nick}++; + + if ($self->{kick_counter}->{$channel}->{$nick} >= 2) { + $msg .= ' (WARNING: next offense will result in a temp ban)'; + } + + $self->{pbot}->{chanops}->add_op_command($channel, "kick $channel $nick $msg"); + $self->{pbot}->{chanops}->gain_ops($channel); + + if ($self->{kick_counter}->{$channel}->{$nick} >= 3) { + my $botnick = $self->{pbot}->{conn}->nick; + $self->{pbot}->{banlist}->ban_user_timed($channel, 'b', "*!*\@$host", 60 * 60 * 2, $botnick, 'anti-away'); + } +} + +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 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_nicks = $self->{pbot}->{registry}->get_value('antiaway', 'bad_nicks'); + + if ($nick =~ m/$bad_nicks/i) { + my $kick_msg = $self->{pbot}->{registry}->get_value('antiaway', 'kick_msg'); + $self->punish($kick_msg, $channel, $nick, $user, $host); + } + return 0; +} + sub on_nickchange($self, $event_type, $event) { my ($nick, $user, $host, $newnick) = ( $event->nick, @@ -47,9 +84,7 @@ sub on_nickchange($self, $event_type, $event) { my $u = $self->{pbot}->{users}->loggedin($chan, "$nick!$user\@$host"); next if $self->{pbot}->{capabilities}->userhas($u, 'is-whitelisted'); - $self->{pbot}->{logger}->log("$newnick matches bad away nick regex, kicking from $chan\n"); - $self->{pbot}->{chanops}->add_op_command($chan, "kick $chan $newnick $kick_msg"); - $self->{pbot}->{chanops}->gain_ops($chan); + $self->punish($kick_msg, $chan, $newnick, $user, $host); } } return 0; @@ -75,8 +110,7 @@ sub on_action($self, $event_type, $event) { if ($msg =~ m/$bad_actions/i) { $self->{pbot}->{logger}->log("$nick $msg matches bad away actions regex, kicking...\n"); my $kick_msg = $self->{pbot}->{registry}->get_value('antiaway', 'kick_msg'); - $self->{pbot}->{chanops}->add_op_command($channel, "kick $channel $nick $kick_msg"); - $self->{pbot}->{chanops}->gain_ops($channel); + $self->punish($kick_msg, $channel, $nick, $user, $host); } return 0; } diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 78119b29..1e3f2c5a 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 => 4768, - BUILD_DATE => "2024-07-11", + BUILD_REVISION => 4771, + BUILD_DATE => "2024-07-22", }; sub initialize {}