2017-02-21 14:20:17 +01:00
# File: AntiTwitter.pm
# Author: pragma_
#
# Purpose: Warns people off from using @nick style addressing. Temp-bans if they
# persist.
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/.
2019-09-01 20:01:18 +02:00
package Plugins::AntiTwitter ;
2020-02-15 23:38:32 +01:00
2020-02-09 04:48:05 +01:00
use parent 'Plugins::Plugin' ;
2017-02-21 14:20:17 +01:00
2020-02-09 04:48:05 +01:00
use warnings ; use strict ;
2019-07-11 03:40:53 +02:00
use feature 'unicode_strings' ;
2017-02-21 14:20:17 +01:00
use Time::HiRes qw/gettimeofday/ ;
use Time::Duration qw/duration/ ;
use feature 'switch' ;
2020-02-15 23:38:32 +01:00
2017-02-21 14:20:17 +01:00
no if $] >= 5.018 , warnings = > "experimental::smartmatch" ;
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 } - > { timer } - > register ( sub { $ self - > adjust_offenses } , 60 * 60 * 1 , 'antitwitter' ) ;
$ 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' ) ;
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 ) = @ _ ;
my ( $ nick , $ user , $ host , $ channel , $ msg ) = ( $ event - > { event } - > nick , $ event - > { event } - > user , $ event - > { event } - > host , $ event - > { event } - > { to } [ 0 ] , $ event - > { event } - > args ) ;
2017-02-21 14:20:17 +01:00
2020-02-15 23:38:32 +01:00
return 0 if $ event - > { interpreted } ;
$ channel = lc $ channel ;
return 0 if not $ self - > { pbot } - > { chanops } - > can_gain_ops ( $ channel ) ;
2017-02-21 14:20:17 +01:00
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 } ) {
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." ) ; }
when ( 2 ) {
$ 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." ) ;
}
default {
my $ offenses = $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } - 2 ;
my $ length = 60 * ( $ offenses * $ offenses + 1 ) ;
$ self - > { pbot } - > { chanops } - > ban_user_timed ( $ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) , 'using @nick too much' , "*!*\@$host" , $ channel , $ length ) ;
$ self - > { pbot } - > { chanops } - > gain_ops ( $ channel ) ;
$ 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
}
sub adjust_offenses {
2020-02-15 23:38:32 +01:00
my $ self = shift ;
my $ now = gettimeofday ;
foreach my $ channel ( keys % { $ self - > { offenses } } ) {
foreach my $ nick ( keys % { $ self - > { offenses } - > { $ channel } } ) {
if ( $ now - $ self - > { offenses } - > { $ channel } - > { $ nick } - > { time } >= 60 * 60 * 24 * 7 ) {
if ( - - $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } <= 0 ) {
delete $ self - > { offenses } - > { $ channel } - > { $ nick } ;
delete $ self - > { offenses } - > { $ channel } if not keys % { $ self - > { offenses } - > { $ channel } } ;
}
}
2017-02-21 14:20:17 +01:00
}
}
}
1 ;