2017-02-21 14:20:17 +01:00
# File: AntiTwitter.pm
#
2021-06-19 06:23:34 +02:00
# Purpose: Warns people off from using @nick style addressing. Temp-bans
# if they persist.
2017-02-21 14:20:17 +01:00
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-03-05 22:33:31 +01:00
2021-07-14 04:45:56 +02:00
package PBot::Plugin::AntiTwitter ;
use parent 'PBot::Plugin::Base' ;
2017-02-21 14:20:17 +01:00
2021-06-19 06:23:34 +02:00
use PBot::Imports ;
2019-07-11 03:40:53 +02:00
2017-02-21 14:20:17 +01:00
use Time::HiRes qw/gettimeofday/ ;
use Time::Duration qw/duration/ ;
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 - > { offenses } = { } ;
2017-02-21 14:20:17 +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' ) ;
2021-06-22 02:26:24 +02:00
$ self - > { pbot } - > { event_queue } - > dequeue_event ( 'antitwitter .*' ) ;
2020-02-07 21:24:21 +01:00
}
2017-02-21 14:20:17 +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 , $ channel , $ msg ) = ( $ event - > nick , $ event - > user , $ event - > host , $ event - > { to } [ 0 ] , $ event - > args ) ;
2017-02-21 14:20:17 +01:00
2020-02-15 23:38:32 +01:00
return 0 if $ event - > { interpreted } ;
2023-01-31 14:44:34 +01:00
2020-02-15 23:38:32 +01:00
$ channel = lc $ channel ;
return 0 if not $ self - > { pbot } - > { chanops } - > can_gain_ops ( $ channel ) ;
2017-02-21 14:20:17 +01:00
2020-05-22 04:25:31 +02:00
my $ u = $ self - > { pbot } - > { users } - > loggedin ( $ channel , "$nick!$user\@$host" ) ;
return 0 if $ self - > { pbot } - > { capabilities } - > userhas ( $ u , 'is-whitelisted' ) ;
2020-02-15 23:38:32 +01:00
while ( $ msg =~ m/\B[@ @]([a-z0-9_^{}\-\\\[\]\|]+)/ig ) {
my $ n = $ 1 ;
if ( $ self - > { pbot } - > { nicklist } - > is_present_similar ( $ channel , $ n , 0.05 ) ) {
$ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } + + ;
$ self - > { offenses } - > { $ channel } - > { $ nick } - > { time } = gettimeofday ;
2019-06-26 18:34:19 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "$nick!$user\@$host is a twit. ($self->{offenses}->{$channel}->{$nick}->{offenses} offenses) $channel: $msg\n" ) ;
2017-02-21 14:20:17 +01:00
2020-02-15 23:38:32 +01:00
given ( $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } ) {
2020-04-29 06:33:49 +02:00
when ( 1 ) {
$ event - > { conn } - > privmsg ( $ nick , "Please do not use \@nick to address people. Drop the @ symbol; it's not necessary and it's ugly." ) ;
}
2020-02-15 23:38:32 +01:00
when ( 2 ) {
2020-04-29 06:33:49 +02:00
$ event - > { conn } - > privmsg ( $ nick , "Please do not use \@nick to address people. Drop the @ symbol; it's not necessary and it's ugly. Doing this again will result in a temporary ban." ) ;
2020-02-15 23:38:32 +01:00
}
2020-04-29 06:33:49 +02:00
2020-02-15 23:38:32 +01:00
default {
my $ offenses = $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } - 2 ;
my $ length = 60 * ( $ offenses * $ offenses + 1 ) ;
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { banlist } - > ban_user_timed (
$ channel ,
'b' ,
"*!*\@$host" ,
$ length ,
$ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ,
'using @nick too much' ,
) ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { chanops } - > gain_ops ( $ channel ) ;
2020-03-08 03:45:34 +01:00
2021-06-22 02:26:24 +02:00
$ self - > { pbot } - > { event_queue } - > enqueue_event ( sub {
2020-03-08 03:45:34 +01:00
my ( $ event ) = @ _ ;
if ( - - $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } <= 0 ) {
delete $ self - > { offenses } - > { $ channel } - > { $ nick } ;
delete $ self - > { offenses } - > { $ channel } if not keys % { $ self - > { offenses } - > { $ channel } } ;
$ event - > { repeating } = 0 ;
}
2020-04-29 06:33:49 +02:00
} , 60 * 60 * 24 * 2 , "antitwitter $channel $nick" , 1
2020-03-08 03:45:34 +01:00
) ;
2020-02-15 23:38:32 +01:00
$ length = duration $ length ;
$ event - > { conn } - > privmsg (
$ nick ,
"Please do not use \@nick to address people. Drop the @ symbol; it's not necessary and it's ugly. You were warned. You will be allowed to speak again in $length."
) ;
}
}
last ;
2017-02-21 14:20:17 +01:00
}
}
2020-02-15 23:38:32 +01:00
return 0 ;
2017-02-21 14:20:17 +01:00
}
1 ;