2018-07-31 22:29:52 +02:00
package PBot::Plugins::RegisterNickserv ;
use warnings ;
use strict ;
use Carp ( ) ;
2018-08-01 01:20:49 +02:00
use Time::HiRes qw/gettimeofday/ ;
2018-07-31 22:29:52 +02:00
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 ( @ _ ) } ) ;
2018-08-01 01:20:49 +02:00
$ self - > { queue } = [] ;
$ self - > { pbot } - > { timer } - > register ( sub { $ self - > check_queue } , 1 , 'RegisterNickserv' ) ;
2018-07-31 22:29:52 +02:00
}
sub unload {
my $ self = shift ;
2018-08-01 01:20:49 +02:00
$ self - > { pbot } - > { timer } - > unregister ( 'RegisterNickserv' ) ;
2018-07-31 22:29:52 +02:00
}
sub on_public {
my ( $ self , $ event_type , $ event ) = @ _ ;
my ( $ nick , $ user , $ host , $ msg ) = ( $ event - > { event } - > nick , $ event - > { event } - > user , $ event - > { event } - > host , $ event - > { event } - > args ) ;
my $ channel = lc $ event - > { event } - > { to } [ 0 ] ;
# exit if channel hasn't muted $~a
return 0 if not exists $ self - > { pbot } - > { bantracker } - > { banlist } - > { $ channel } - > { '+q' } - > { '$~a' } ;
my $ account = $ self - > { pbot } - > { messagehistory } - > { database } - > get_message_account ( $ nick , $ user , $ host ) ;
my $ nickserv = $ self - > { pbot } - > { messagehistory } - > { database } - > get_current_nickserv_account ( $ account ) ;
2018-08-01 01:20:49 +02:00
# debug
# my $nickserv_text = $nickserv ? "is logged into $nickserv" : "is not logged in";
# $self->{pbot}->{logger}->log("RegisterNickserv: $nick!$user\@$host ($account) $nickserv_text.\n");
2018-07-31 22:29:52 +02:00
2018-08-01 01:20:49 +02:00
# exit if user is identified
2018-07-31 22:29:52 +02:00
return 0 if defined $ nickserv && length $ nickserv ;
my @ filters = (
qr{ https://bryanostergaard.com/ } ,
qr{ https://encyclopediadramatica.rs/Freenodegate } ,
qr{ https://MattSTrout.com/ } ,
qr{ Contact me on twitter } ,
qr{ At the beginning there was only Chaos } ,
qr{ https://williampitcock.com/ } ,
2018-08-03 16:30:07 +02:00
qr{ Achievement Method } ,
qr{ perceived death signal } ,
2018-07-31 22:29:52 +02:00
qr{ efnet } ,
) ;
foreach my $ filter ( @ filters ) {
if ( $ msg =~ m/$filter/i ) {
$ self - > { pbot } - > { logger } - > log ( "RegisterNickserv: Ignoring filtered message.\n" ) ;
return 0 ;
}
}
2018-08-03 21:32:16 +02:00
return 0 if $ self - > { pbot } - > { nicklist } - > get_meta ( $ channel , $ nick , '+v' ) ;
2018-07-31 22:29:52 +02:00
$ self - > { pbot } - > { logger } - > log ( "RegisterNickserv: Notifying $nick to register with NickServ in $channel.\n" ) ;
2018-08-01 01:35:56 +02:00
$ event - > { conn } - > privmsg ( $ nick , "Please register your nick to speak in $channel. See https://freenode.net/kb/answer/registration and https://freenode.net/kb/answer/sasl" ) ;
2018-07-31 22:29:52 +02:00
# don't relay unregistered chat unless enabled
return 0 if not $ self - > { pbot } - > { registry } - > get_value ( $ channel , 'relay_unregistered_chat' ) ;
2018-08-01 01:20:49 +02:00
# add message to delay send queue to see if Sigyn kills them first (or if they leave)
$ self - > { pbot } - > { logger } - > log ( "RegisterNickserv: Queuing unregistered message for $channel: <$nick> $msg\n" ) ;
2018-08-03 16:30:07 +02:00
push @ { $ self - > { queue } } , [ gettimeofday + 10 , $ channel , $ nick , $ user , $ host , $ msg ] ;
2018-07-31 22:29:52 +02:00
return 0 ;
}
2018-08-01 01:20:49 +02:00
sub check_queue {
my $ self = shift ;
my $ now = gettimeofday ;
return if not @ { $ self - > { queue } } ;
2018-08-03 16:30:07 +02:00
my ( $ time , $ channel , $ nick , $ user , $ host , $ msg ) = @ { $ self - > { queue } - > [ 0 ] } ;
2018-08-01 01:20:49 +02:00
if ( $ now >= $ time ) {
# if nick is still present in channel, send the message
if ( $ self - > { pbot } - > { nicklist } - > is_present ( $ channel , $ nick ) ) {
2018-08-03 16:30:07 +02:00
# ensure they're not banned (+z allows us to see +q/+b messages as normal ones)
my $ message_account = $ self - > { pbot } - > { messagehistory } - > { database } - > get_message_account ( $ nick , $ user , $ host ) ;
my @ nickserv_accounts = $ self - > { pbot } - > { messagehistory } - > { database } - > get_nickserv_accounts ( $ message_account ) ;
push @ nickserv_accounts , undef ;
my $ no_relay = 0 ;
foreach my $ nickserv_account ( @ nickserv_accounts ) {
my $ baninfos = $ self - > { pbot } - > { bantracker } - > get_baninfo ( "$nick!$user\@$host" , $ channel , $ nickserv_account ) ;
if ( defined $ baninfos ) {
foreach my $ baninfo ( @$ baninfos ) {
if ( $ self - > { pbot } - > { antiflood } - > whitelisted ( $ baninfo - > { channel } , $ baninfo - > { banmask } , 'ban' ) || $ self - > { pbot } - > { antiflood } - > whitelisted ( $ baninfo - > { channel } , "$nick!$user\@$host" , 'user' ) ) {
$ self - > { pbot } - > { logger } - > log ( "[RegisterNickserv] $nick!$user\@$host banned as $baninfo->{banmask} in $baninfo->{channel}, but allowed through whitelist\n" ) ;
} else {
if ( $ channel eq lc $ baninfo - > { channel } ) {
my $ mode = $ baninfo - > { type } eq "+b" ? "banned" : "quieted" ;
$ self - > { pbot } - > { logger } - > log ( "[RegisterNickserv] $nick!$user\@$host $mode as $baninfo->{banmask} in $baninfo->{channel} by $baninfo->{owner}, not relaying unregistered message\n" ) ;
$ no_relay = 1 ;
last ;
}
}
}
}
}
$ self - > { pbot } - > { conn } - > privmsg ( $ channel , "(unreg) <$nick> $msg" ) unless $ no_relay ;
2018-08-01 01:20:49 +02:00
}
shift @ { $ self - > { queue } } ;
}
}
2018-07-31 22:29:52 +02:00
1 ;