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/.
2017-02-21 14:20:17 +01:00
package PBot::Plugins::AntiTwitter ;
use warnings ;
use strict ;
use Carp ( ) ;
use Time::HiRes qw/gettimeofday/ ;
use Time::Duration qw/duration/ ;
use feature 'switch' ;
no if $] >= 5.018 , warnings = > "experimental::smartmatch" ;
sub new {
Carp:: croak ( "Options to " . __FILE__ . " should be key/value pairs, not hash reference" ) if ref $ _ [ 1 ] eq 'HASH' ;
my ( $ class , % conf ) = @ _ ;
my $ self = bless { } , $ class ;
$ self - > initialize ( % conf ) ;
return $ self ;
}
sub initialize {
my ( $ self , % conf ) = @ _ ;
$ self - > { pbot } = delete $ conf { pbot } // Carp:: croak ( "Missing pbot reference to " . __FILE__ ) ;
$ 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 } = { } ;
}
sub on_public {
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:37:43 +01:00
return 0 if $ event - > { interpreted } ;
2017-02-21 14:20:17 +01:00
$ channel = lc $ channel ;
return 0 if not $ self - > { pbot } - > { chanops } - > can_gain_ops ( $ channel ) ;
2017-10-18 07:05:00 +02:00
while ( $ msg =~ m/\B[@ @]\s*([a-z0-9_^{}\-\\\[\]\|]+)/ig ) {
2017-02-21 14:20:17 +01:00
my $ n = $ 1 ;
2017-05-21 08:45:30 +02:00
if ( $ self - > { pbot } - > { nicklist } - > is_present_similar ( $ channel , $ n , 0.05 ) ) {
2017-02-21 14:20:17 +01:00
$ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } + + ;
$ self - > { offenses } - > { $ channel } - > { $ nick } - > { time } = gettimeofday ;
2017-02-22 13:18:26 +01:00
2017-04-11 04:16:14 +02: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
given ( $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } ) {
when ( 1 ) {
2017-02-22 13:18:26 +01:00
$ event - > { conn } - > privmsg ( $ nick , "$nick: Please do not use \@nick to address people. Drop the @ symbol; it's not necessary and it's ugly." ) ;
}
when ( 2 ) {
2017-02-21 14:20:17 +01:00
$ event - > { conn } - > privmsg ( $ nick , "$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 {
2017-02-22 13:18:26 +01:00
my $ offenses = $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } - 2 ;
2017-02-21 14:20:17 +01:00
my $ length = 60 * ( $ offenses * $ offenses + 1 ) ;
$ self - > { pbot } - > { chanops } - > ban_user_timed ( "*!*\@$host" , $ channel , $ length ) ;
$ self - > { pbot } - > { chanops } - > gain_ops ( $ channel ) ;
$ length = duration $ length ;
$ event - > { conn } - > privmsg ( $ nick , "$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." ) ;
}
}
2017-02-21 14:26:26 +01:00
last ;
2017-02-21 14:20:17 +01:00
}
}
return 0 ;
}
sub adjust_offenses {
my $ self = shift ;
my $ now = gettimeofday ;
2017-10-10 04:40:32 +02:00
foreach my $ channel ( keys % { $ self - > { offenses } } ) {
foreach my $ nick ( keys % { $ self - > { offenses } - > { $ channel } } ) {
2017-04-11 04:16:14 +02:00
if ( $ now - $ self - > { offenses } - > { $ channel } - > { $ nick } - > { time } >= 60 * 60 * 24 * 7 ) {
2017-02-21 14:20:17 +01:00
if ( - - $ self - > { offenses } - > { $ channel } - > { $ nick } - > { offenses } <= 0 ) {
delete $ self - > { offenses } - > { $ channel } - > { $ nick } ;
2017-10-10 04:40:32 +02:00
delete $ self - > { offenses } - > { $ channel } if not keys % { $ self - > { offenses } - > { $ channel } } ;
2017-02-21 14:20:17 +01:00
}
}
}
}
}
1 ;