2014-12-28 01:44:15 +01:00
# File: AntiKickAutoRejoin.pm
#
# Purpose: Temporarily bans people who immediately auto-rejoin after a kick.
2023-02-21 06:31:52 +01:00
# SPDX-FileCopyrightText: 2014-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::AntiKickAutoRejoin ;
use parent 'PBot::Plugin::Base' ;
2014-12-28 01:44:15 +01:00
2021-06-19 06:23:34 +02:00
use PBot::Imports ;
2019-07-11 03:40:53 +02:00
2014-12-28 01:44:15 +01:00
use Time::HiRes qw/gettimeofday/ ;
use Time::Duration ;
2023-04-14 02:01:23 +02:00
sub initialize ($self, %conf) {
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { registry } - > add_default ( 'array' , 'antikickautorejoin' , 'punishment' , '30,90,180,300,28800' ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antikickautorejoin' , 'threshold' , '2' ) ;
2014-12-28 01:44:15 +01:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { event_dispatcher } - > register_handler ( 'irc.kick' , sub { $ self - > on_kick ( @ _ ) } ) ;
$ self - > { pbot } - > { event_dispatcher } - > register_handler ( 'irc.join' , sub { $ self - > on_join ( @ _ ) } ) ;
$ self - > { kicks } = { } ;
2014-12-28 01:44:15 +01:00
}
2023-04-14 02:01:23 +02:00
sub unload ($self) {
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { event_dispatcher } - > remove_handler ( 'irc.kick' ) ;
$ self - > { pbot } - > { event_dispatcher } - > remove_handler ( 'irc.join' ) ;
2020-02-07 21:24:21 +01:00
}
2023-04-14 02:01:23 +02:00
sub on_kick ($self, $event_type, $event) {
2023-01-31 14:44:34 +01:00
my ( $ nick , $ user , $ host ) = ( $ event - > nick , $ event - > user , $ event - > host ) ;
my ( $ target , $ channel , $ reason ) = ( $ event - > to , $ event - > { args } [ 0 ] , $ event - > { args } [ 1 ] ) ;
2014-12-28 01:44:15 +01:00
2020-02-15 23:38:32 +01:00
$ channel = lc $ channel ;
return 0 if not $ self - > { pbot } - > { chanops } - > can_gain_ops ( $ channel ) ;
2023-01-31 14:44:34 +01:00
return 0 if $ reason eq '*BANG!*' ; # roulette
2015-06-20 16:24:13 +02:00
2020-04-29 06:33:49 +02:00
if ( not exists $ self - > { kicks } - > { $ channel } or not exists $ self - > { kicks } - > { $ channel } - > { $ target } ) {
$ self - > { kicks } - > { $ channel } - > { $ target } - > { rejoins } = 0 ;
}
2014-12-28 01:44:15 +01:00
2020-02-15 23:38:32 +01:00
$ self - > { kicks } - > { $ channel } - > { $ target } - > { last_kick } = gettimeofday ;
return 0 ;
2014-12-28 01:44:15 +01:00
}
2023-04-14 02:01:23 +02:00
sub on_join ($self, $event_type, $event) {
2023-01-31 14:44:34 +01:00
my ( $ nick , $ user , $ host , $ channel ) = ( $ event - > nick , $ event - > user , $ event - > host , $ event - > to ) ;
2020-02-15 23:38:32 +01:00
$ channel = lc $ channel ;
return 0 if not $ self - > { pbot } - > { chanops } - > can_gain_ops ( $ channel ) ;
2023-01-31 14:44:34 +01:00
2020-02-15 23:38:32 +01:00
my $ u = $ self - > { pbot } - > { users } - > loggedin ( $ channel , "$nick!$user\@$host" ) ;
return 0 if $ self - > { pbot } - > { capabilities } - > userhas ( $ u , 'is-whitelisted' ) ;
if ( exists $ self - > { kicks } - > { $ channel } and exists $ self - > { kicks } - > { $ channel } - > { $ nick } ) {
my $ now = gettimeofday ;
if ( $ now - $ self - > { kicks } - > { $ channel } - > { $ nick } - > { last_kick } <= $ self - > { pbot } - > { registry } - > get_value ( 'antikickautorejoin' , 'threshold' ) ) {
my $ timeout = $ self - > { pbot } - > { registry } - > get_array_value ( 'antikickautorejoin' , 'punishment' , $ self - > { kicks } - > { $ channel } - > { $ nick } - > { rejoins } ) ;
my $ duration = duration ( $ timeout ) ;
$ duration =~ s/s$// ; # hours -> hour, minutes -> minute
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { banlist } - > ban_user_timed (
$ channel ,
'b' ,
"*!$user\@$host" ,
$ timeout ,
$ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ,
'autorejoining after kick' ,
) ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { chanops } - > add_op_command ( $ channel , "kick $channel $nick $duration ban for auto-rejoining after kick; use this time to think about why you were kicked" ) ;
$ self - > { pbot } - > { chanops } - > gain_ops ( $ channel ) ;
$ self - > { kicks } - > { $ channel } - > { $ nick } - > { rejoins } + + ;
}
2014-12-28 01:44:15 +01:00
}
2020-04-29 06:33:49 +02:00
2020-02-15 23:38:32 +01:00
return 0 ;
2014-12-28 01:44:15 +01:00
}
1 ;