2010-03-22 08:33:44 +01:00
# File: AntiFlood.pm
2010-03-17 07:36:54 +01:00
#
2014-05-13 12:15:52 +02:00
# Purpose: Tracks message and nickserv statistics to enforce anti-flooding and
# ban-evasion detection.
2013-10-26 04:39:54 +02:00
#
2014-05-13 12:15:52 +02:00
# The nickserv/ban-evasion stuff probably ought to be in BanTracker or some
# such suitable class.
2010-03-17 07:36:54 +01:00
2023-01-31 14:44:34 +01:00
# SPDX-FileCopyrightText: 2021-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-21 07:44:51 +02:00
package PBot::Core::AntiFlood ;
use parent 'PBot::Core::Class' ;
2010-03-17 07:36:54 +01:00
2021-06-19 06:23:34 +02:00
use PBot::Imports ;
2010-06-18 06:17:30 +02:00
2021-07-21 07:44:51 +02:00
use PBot::Core::MessageHistory::Constants ':all' ;
2021-07-21 06:38:07 +02:00
2011-01-22 10:26:04 +01:00
use Time::HiRes qw( gettimeofday tv_interval ) ;
2010-06-27 07:36:58 +02:00
use Time::Duration ;
2016-01-26 18:04:03 +01:00
use POSIX qw/strftime/ ;
2016-02-10 16:10:37 +01:00
use Text::CSV ;
2010-03-17 07:36:54 +01:00
2010-03-22 08:33:44 +01:00
sub initialize {
2020-02-15 23:38:32 +01:00
my ( $ self , % conf ) = @ _ ;
2010-03-22 08:33:44 +01:00
2020-02-15 23:38:32 +01:00
# flags for 'validated' field
2023-01-27 20:48:01 +01:00
use constant {
NICKSERV_VALIDATED = > ( 1 << 0 ) ,
NEEDS_CHECKBAN = > ( 1 << 1 ) ,
} ;
2010-03-22 08:33:44 +01:00
2020-02-15 23:38:32 +01:00
$ self - > { channels } = { } ; # per-channel statistics, e.g. for optimized tracking of last spoken nick for enter-abuse detection, etc
$ self - > { nickflood } = { } ; # statistics to track nickchange flooding
$ self - > { whois_pending } = { } ; # prevents multiple whois for nick joining multiple channels at once
$ self - > { changinghost } = { } ; # tracks nicks changing hosts/identifying to strongly link them
2010-03-23 19:24:02 +01:00
2021-06-22 02:26:24 +02:00
$ self - > { pbot } - > { event_queue } - > enqueue ( sub { $ self - > adjust_offenses } , 60 * 60 * 1 , 'Adjust anti-flood offenses' ) ;
2014-05-18 22:09:05 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'enforce' , $ conf { enforce_antiflood } // 1 ) ;
2014-05-20 12:14:09 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'join_flood_threshold' , $ conf { join_flood_threshold } // 4 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'join_flood_time_threshold' , $ conf { join_flood_time_threshold } // 60 * 30 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'array' , 'antiflood' , 'join_flood_punishment' , $ conf { join_flood_punishment } // '28800,3600,86400,604800,2419200,14515200' ) ;
2014-05-19 04:42:18 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'chat_flood_threshold' , $ conf { chat_flood_threshold } // 4 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'chat_flood_time_threshold' , $ conf { chat_flood_time_threshold } // 10 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'array' , 'antiflood' , 'chat_flood_punishment' , $ conf { chat_flood_punishment } // '60,300,3600,86400,604800,2419200' ) ;
2014-05-19 04:42:18 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'nick_flood_threshold' , $ conf { nick_flood_threshold } // 3 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'nick_flood_time_threshold' , $ conf { nick_flood_time_threshold } // 60 * 30 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'array' , 'antiflood' , 'nick_flood_punishment' , $ conf { nick_flood_punishment } // '60,300,3600,86400,604800,2419200' ) ;
2014-05-19 04:42:18 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'enter_abuse_threshold' , $ conf { enter_abuse_threshold } // 4 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'enter_abuse_time_threshold' , $ conf { enter_abuse_time_threshold } // 20 ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'array' , 'antiflood' , 'enter_abuse_punishment' , $ conf { enter_abuse_punishment } // '60,300,3600,86400,604800,2419200' ) ;
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'enter_abuse_max_offenses' , $ conf { enter_abuse_max_offenses } // 3 ) ;
2014-05-18 22:09:05 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { registry } - > add_default ( 'text' , 'antiflood' , 'debug_checkban' , $ conf { debug_checkban } // 0 ) ;
2014-05-21 06:52:17 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { event_dispatcher } - > register_handler ( 'irc.whoisaccount' , sub { $ self - > on_whoisaccount ( @ _ ) } ) ;
$ self - > { pbot } - > { event_dispatcher } - > register_handler ( 'irc.whoisuser' , sub { $ self - > on_whoisuser ( @ _ ) } ) ;
$ self - > { pbot } - > { event_dispatcher } - > register_handler ( 'irc.endofwhois' , sub { $ self - > on_endofwhois ( @ _ ) } ) ;
$ self - > { pbot } - > { event_dispatcher } - > register_handler ( 'irc.account' , sub { $ self - > on_accountnotify ( @ _ ) } ) ;
2011-12-11 23:56:57 +01:00
}
2015-06-06 07:26:55 +02:00
sub update_join_watch {
2020-02-15 23:38:32 +01:00
my ( $ self , $ account , $ channel , $ text , $ mode ) = @ _ ;
2010-06-21 05:18:39 +02:00
2020-02-15 23:38:32 +01:00
return if $ channel =~ /[@!]/ ; # ignore QUIT messages from nick!user@host channels
2010-06-19 21:19:56 +02:00
2020-02-15 23:38:32 +01:00
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ channel , 'join_watch' ) ;
2010-06-21 05:18:39 +02:00
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_JOIN ) {
2020-02-15 23:38:32 +01:00
$ channel_data - > { join_watch } + + ;
2014-05-15 03:39:33 +02:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ channel , $ channel_data ) ;
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_DEPARTURE ) {
2020-02-15 23:38:32 +01:00
# PART or QUIT
# check QUIT message for netsplits, and decrement joinwatch to allow a free rejoin
if ( $ text =~ /^QUIT .*\.net .*\.split/ ) {
if ( $ channel_data - > { join_watch } > 0 ) {
$ channel_data - > { join_watch } - - ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ channel , $ channel_data ) ;
}
}
# check QUIT message for Ping timeout or Excess Flood
elsif ( $ text =~ /^QUIT Excess Flood/ or $ text =~ /^QUIT Max SendQ exceeded/ or $ text =~ /^QUIT Ping timeout/ ) {
# treat these as an extra join so they're snagged more quickly since these usually will keep flooding
$ channel_data - > { join_watch } + + ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ channel , $ channel_data ) ;
} else {
# some other type of QUIT or PART
}
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_CHAT ) {
2020-02-15 23:38:32 +01:00
# reset joinwatch if they send a message
if ( $ channel_data - > { join_watch } > 0 ) {
$ channel_data - > { join_watch } = 0 ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ channel , $ channel_data ) ;
}
2014-05-13 12:15:52 +02:00
}
2010-06-19 21:19:56 +02:00
}
2021-07-21 06:38:07 +02:00
# TODO: break this gigantic function up into simple plugins
# e.g. PBot::Plugin::AntiAbuse::ChatFlood, ::JoinFlood, ::EnterAbuse, etc.
2010-03-17 07:36:54 +01:00
sub check_flood {
2020-05-02 05:59:51 +02:00
my ( $ self , $ channel , $ nick , $ user , $ host , $ text , $ max_messages , $ max_time , $ mode , $ context ) = @ _ ;
2023-01-22 16:36:11 +01:00
2020-02-15 23:38:32 +01:00
$ channel = lc $ channel ;
my $ mask = "$nick!$user\@$host" ;
my $ oldnick = $ nick ;
my $ account ;
2023-02-02 02:47:25 +01:00
# handle old-style pseudo-QUIT for changing-host if CHGHOST is not available
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_JOIN and exists $ self - > { changinghost } - > { $ nick } ) {
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { logger } - > log ( "Finalizing host change for $nick.\n" ) ;
2023-01-22 16:36:11 +01:00
2020-02-15 23:38:32 +01:00
$ account = delete $ self - > { changinghost } - > { $ nick } ;
my $ id = $ self - > { pbot } - > { messagehistory } - > { database } - > get_message_account_id ( $ mask ) ;
2021-06-11 23:58:16 +02:00
2020-02-15 23:38:32 +01:00
if ( defined $ id ) {
if ( $ id != $ account ) {
$ self - > { pbot } - > { logger } - > log ( "Linking $mask [$id] to account $account\n" ) ;
2023-02-02 02:27:06 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > link_alias ( $ account , $ id , LINK_STRONG , 1 ) ;
2020-02-15 23:38:32 +01:00
} else {
$ self - > { pbot } - > { logger } - > log ( "New hostmask already belongs to original account.\n" ) ;
}
$ account = $ id ;
} else {
$ self - > { pbot } - > { logger } - > log ( "Adding $mask to account $account\n" ) ;
2023-02-02 02:27:06 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > add_message_account ( $ mask , $ account , LINK_STRONG ) ;
2020-02-15 23:38:32 +01:00
}
$ self - > { pbot } - > { messagehistory } - > { database } - > devalidate_all_channels ( $ account ) ;
2016-08-18 05:34:45 +02:00
} else {
2020-02-15 23:38:32 +01:00
$ account = $ self - > { pbot } - > { messagehistory } - > get_message_account ( $ nick , $ user , $ host ) ;
2016-08-18 05:34:45 +02:00
}
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_hostmask_data ( $ mask , { last_seen = > scalar gettimeofday } ) ;
2010-06-21 05:18:39 +02:00
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_NICKCHANGE ) {
2020-02-15 23:38:32 +01:00
my ( $ newnick ) = $ text =~ m/NICKCHANGE (.*)/ ;
2023-01-31 00:50:26 +01:00
$ self - > { pbot } - > { logger } - > log ( "[NICKCHANGE] ($account) $mask changed nick to $newnick\n" ) ;
2020-02-15 23:38:32 +01:00
$ mask = "$newnick!$user\@$host" ;
$ account = $ self - > { pbot } - > { messagehistory } - > get_message_account ( $ newnick , $ user , $ host ) ;
$ nick = $ newnick ;
2023-01-22 16:36:11 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_hostmask_data ( $ mask , { last_seen = > scalar gettimeofday } ) ;
2020-02-15 23:38:32 +01:00
} else {
2023-01-31 00:50:26 +01:00
if ( $ mode == MSG_CHAT ) {
$ self - > { pbot } - > { logger } - > log ( "[MSG] $channel ($account) $mask => $text\n" ) ;
} else {
2023-02-02 02:47:25 +01:00
my $ from = $ channel eq lc $ mask ? undef : $ channel ;
2023-02-02 18:22:51 +01:00
$ text =~ s/^(\w+) // ;
my $ type = $ 1 ;
2023-02-02 02:47:25 +01:00
$ self - > { pbot } - > { logger } - > log ( "[$type] " . ( defined $ from ? "$from " : '' ) . "($account) $mask => $text\n" ) ;
2023-01-31 00:50:26 +01:00
}
2016-08-18 05:34:45 +02:00
}
2020-02-15 23:38:32 +01:00
# do not do flood processing for bot messages
if ( $ nick eq $ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ) {
$ self - > { channels } - > { $ channel } - > { last_spoken_nick } = $ nick ;
return ;
2014-05-13 12:15:52 +02:00
}
2013-09-14 03:56:00 +02:00
2020-02-15 23:38:32 +01:00
# don't do flood processing for unidentified or banned users in +z channels
2023-01-22 16:36:11 +01:00
return if defined $ context and $ context - > { 'chan-z' } and ( $ context - > { 'unidentified' } or $ context - > { 'banned' } ) ;
2020-02-15 23:38:32 +01:00
my $ ancestor = $ self - > { pbot } - > { messagehistory } - > { database } - > get_ancestor_id ( $ account ) ;
$ self - > { pbot } - > { logger } - > log ( "Processing anti-flood account $account " . ( $ ancestor != $ account ? "[ancestor $ancestor] " : '' ) . "for mask $mask\n" )
if $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'debug_account' ) ;
2016-01-26 18:04:03 +01:00
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_NICKCHANGE ) {
2020-02-15 23:38:32 +01:00
$ self - > { nickflood } - > { $ ancestor } - > { changes } + + ;
$ self - > { pbot } - > { logger } - > log ( "account $ancestor has $self->{nickflood}->{$ancestor}->{changes} nickchanges\n" ) ;
2013-07-28 16:12:57 +02:00
}
2013-07-28 12:31:12 +02:00
2020-02-15 23:38:32 +01:00
# handle QUIT events
# (these events come from $channel nick!user@host, not a specific channel or nick,
# so they need to be dispatched to all channels the nick has been seen on)
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_DEPARTURE and $ text =~ /^QUIT/ ) {
2020-02-15 23:38:32 +01:00
my $ channels = $ self - > { pbot } - > { nicklist } - > get_channels ( $ nick ) ;
2021-07-21 06:38:07 +02:00
2020-02-15 23:38:32 +01:00
foreach my $ chan ( @$ channels ) {
next if $ chan !~ m/^#/ ;
$ self - > update_join_watch ( $ account , $ chan , $ text , $ mode ) ;
2015-06-06 07:26:55 +02:00
}
2014-05-13 12:15:52 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > devalidate_all_channels ( $ account ) ;
2014-05-17 22:08:19 +02:00
2020-02-15 23:38:32 +01:00
if ( $ text eq 'QUIT Changing host' ) {
$ self - > { changinghost } - > { $ nick } = $ account ;
}
2015-06-14 01:08:06 +02:00
2020-02-15 23:38:32 +01:00
# don't do flood processing for QUIT events
return ;
2015-05-07 06:10:55 +02:00
}
2015-03-11 10:00:10 +01:00
2023-01-27 20:48:01 +01:00
my $ needs_checkban = 0 ;
if ( defined $ context && defined $ context - > { tags } ) {
my $ tags = $ self - > { pbot } - > { irc } - > get_tags ( $ context - > { tags } ) ;
if ( defined $ tags - > { account } ) {
my $ nickserv_account = $ tags - > { account } ;
my $ current_nickserv_account = $ self - > { pbot } - > { messagehistory } - > { database } - > get_current_nickserv_account ( $ account ) ;
if ( $ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'debug_tags' ) ) {
$ self - > { pbot } - > { logger } - > log ( "($account) $mask got account-tag $nickserv_account\n" ) ;
}
if ( $ current_nickserv_account ne $ nickserv_account ) {
$ self - > { pbot } - > { logger } - > log ( "[MH] ($account) $mask updating NickServ to $nickserv_account\n" ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > set_current_nickserv_account ( $ account , $ nickserv_account ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_nickserv_account ( $ account , $ nickserv_account , scalar gettimeofday ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > link_aliases ( $ account , $ mask , $ nickserv_account ) ;
$ needs_checkban = 1 ;
}
}
}
2020-02-15 23:38:32 +01:00
my $ channels ;
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_NICKCHANGE ) {
2020-03-22 21:28:04 +01:00
$ channels = $ self - > { pbot } - > { nicklist } - > get_channels ( $ oldnick ) ;
} else {
2020-02-15 23:38:32 +01:00
$ self - > update_join_watch ( $ account , $ channel , $ text , $ mode ) ;
push @$ channels , $ channel ;
}
2010-06-18 06:15:28 +02:00
2020-02-15 23:38:32 +01:00
foreach my $ chan ( @$ channels ) {
$ chan = lc $ chan ;
# do not do flood processing if channel is not in bot's channel list or bot is not set as chanop for the channel
next if $ chan =~ /^#/ and not $ self - > { pbot } - > { chanops } - > can_gain_ops ( $ chan ) ;
my $ u = $ self - > { pbot } - > { users } - > loggedin ( $ chan , "$nick!$user\@$host" ) ;
2011-02-11 03:46:35 +01:00
2021-07-21 06:38:07 +02:00
if ( $ chan =~ /^#/ and $ mode == MSG_DEPARTURE ) {
2020-02-15 23:38:32 +01:00
# remove validation on PART or KICK so we check for ban-evasion when user returns at a later time
my $ chan_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ chan , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( $ chan_data - > { validated } & NICKSERV_VALIDATED ) {
$ chan_data - > { validated } & = ~ NICKSERV_VALIDATED ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
2015-03-11 10:00:10 +01:00
}
2020-02-15 23:38:32 +01:00
next ;
2014-05-20 12:14:09 +02:00
}
2015-09-14 18:06:43 +02:00
2023-01-27 20:48:01 +01:00
next if $ self - > { pbot } - > { capabilities } - > userhas ( $ u , 'is-whitelisted' ) ;
$ self - > check_bans ( $ account , $ mask , $ chan ) if $ needs_checkban ;
2015-09-14 18:06:43 +02:00
2020-02-15 23:38:32 +01:00
if ( $ max_messages > $ self - > { pbot } - > { registry } - > get_value ( 'messagehistory' , 'max_messages' ) ) {
$ self - > { pbot } - > { logger } - > log ( "Warning: max_messages greater than max_messages limit; truncating.\n" ) ;
$ max_messages = $ self - > { pbot } - > { registry } - > get_value ( 'messagehistory' , 'max_messages' ) ;
}
# check for ban evasion if channel begins with # (not private message) and hasn't yet been validated against ban evasion
if ( $ chan =~ m/^#/ ) {
my $ validated = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ chan , 'validated' ) - > { 'validated' } ;
2023-01-27 20:48:01 +01:00
if ( $ validated & NEEDS_CHECKBAN or not $ validated & NICKSERV_VALIDATED ) {
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_DEPARTURE ) {
2020-02-15 23:38:32 +01:00
# don't check for evasion on PART/KICK
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_NICKCHANGE ) {
2020-02-15 23:38:32 +01:00
if ( not exists $ self - > { whois_pending } - > { $ nick } ) {
$ self - > { pbot } - > { messagehistory } - > { database } - > set_current_nickserv_account ( $ account , '' ) ;
$ self - > { pbot } - > { conn } - > whois ( $ nick ) ;
$ self - > { whois_pending } - > { $ nick } = gettimeofday ;
}
} else {
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_JOIN && exists $ self - > { pbot } - > { irc_capabilities } - > { 'extended-join' } ) {
2020-02-15 23:38:32 +01:00
# don't WHOIS joins if extended-join capability is active
} elsif ( not exists $ self - > { pbot } - > { irc_capabilities } - > { 'account-notify' } ) {
if ( not exists $ self - > { whois_pending } - > { $ nick } ) {
$ self - > { pbot } - > { messagehistory } - > { database } - > set_current_nickserv_account ( $ account , '' ) ;
$ self - > { pbot } - > { conn } - > whois ( $ nick ) ;
$ self - > { whois_pending } - > { $ nick } = gettimeofday ;
}
} else {
$ self - > check_bans ( $ account , "$nick!$user\@$host" , $ chan ) ;
}
2015-09-14 18:06:43 +02:00
}
}
}
2010-03-17 07:36:54 +01:00
2020-02-15 23:38:32 +01:00
# do not do flood enforcement for this event if bot is lagging
if ( $ self - > { pbot } - > { lagchecker } - > lagging ) {
$ self - > { pbot } - > { logger } - > log ( "Disregarding enforcement of anti-flood due to lag: " . $ self - > { pbot } - > { lagchecker } - > lagstring . "\n" ) ;
$ self - > { channels } - > { $ chan } - > { last_spoken_nick } = $ nick ;
return ;
}
2015-03-27 12:08:47 +01:00
2020-02-15 23:38:32 +01:00
# do not do flood enforcement for whitelisted users
if ( $ self - > { pbot } - > { capabilities } - > userhas ( $ u , 'is-whitelisted' ) ) {
$ self - > { channels } - > { $ chan } - > { last_spoken_nick } = $ nick ;
next ;
}
2014-03-10 04:50:02 +01:00
2020-02-15 23:38:32 +01:00
# do not do flood enforcement for channels that do not want it
if ( $ self - > { pbot } - > { registry } - > get_value ( $ chan , 'dont_enforce_antiflood' ) ) {
$ self - > { channels } - > { $ chan } - > { last_spoken_nick } = $ nick ;
next ;
}
2015-11-21 06:08:30 +01:00
2020-02-15 23:38:32 +01:00
# check for chat/join/private message flooding
if ( $ max_messages > 0
2021-07-21 06:38:07 +02:00
and $ self - > { pbot } - > { messagehistory } - > { database } - > get_max_messages ( $ account , $ chan , $ mode == MSG_NICKCHANGE ? $ nick : undef ) >=
2020-02-15 23:38:32 +01:00
$ max_messages )
{
my $ msg ;
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_CHAT ) {
2020-02-15 23:38:32 +01:00
$ msg = $ self - > { pbot } - > { messagehistory } - > { database } - > recall_message_by_count ( $ account , $ chan , $ max_messages - 1 ) ;
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_JOIN ) {
my $ joins = $ self - > { pbot } - > { messagehistory } - > { database } - > get_recent_messages ( $ account , $ chan , $ max_messages , MSG_JOIN ) ;
2020-02-15 23:38:32 +01:00
$ msg = $ joins - > [ 0 ] ;
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_NICKCHANGE ) {
2020-02-15 23:38:32 +01:00
my $ nickchanges =
2021-07-21 06:38:07 +02:00
$ self - > { pbot } - > { messagehistory } - > { database } - > get_recent_messages ( $ ancestor , $ chan , $ max_messages , MSG_NICKCHANGE , $ nick ) ;
2020-02-15 23:38:32 +01:00
$ msg = $ nickchanges - > [ 0 ] ;
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_DEPARTURE ) {
2020-02-15 23:38:32 +01:00
# no flood checks to be done for departure events
next ;
} else {
$ self - > { pbot } - > { logger } - > log ( "Unknown flood mode [$mode] ... aborting flood enforcement.\n" ) ;
return ;
}
2015-11-21 06:08:30 +01:00
2020-02-15 23:38:32 +01:00
my $ last ;
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_NICKCHANGE ) {
2020-02-15 23:38:32 +01:00
$ last = $ self - > { pbot } - > { messagehistory } - > { database } - > recall_message_by_count ( $ ancestor , $ chan , 0 , undef , $ nick ) ;
} else {
$ last = $ self - > { pbot } - > { messagehistory } - > { database } - > recall_message_by_count ( $ account , $ chan , 0 ) ;
}
2017-08-05 14:31:46 +02:00
2020-02-15 23:38:32 +01:00
if ( $ last - > { timestamp } - $ msg - > { timestamp } <= $ max_time ) {
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_JOIN ) {
2020-02-15 23:38:32 +01:00
my $ chan_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ chan , 'offenses' , 'last_offense' , 'join_watch' ) ;
2021-07-21 06:38:07 +02:00
$ self - > { pbot } - > { logger } - > log ( "$account offenses $chan_data->{offenses}, join watch $chan_data->{join_watch}, max messages $max_messages\n" ) ;
2020-02-15 23:38:32 +01:00
if ( $ chan_data - > { join_watch } >= $ max_messages ) {
$ chan_data - > { offenses } + + ;
$ chan_data - > { last_offense } = gettimeofday ;
if ( $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enforce' ) ) {
my $ timeout = $ self - > { pbot } - > { registry } - > get_array_value ( 'antiflood' , 'join_flood_punishment' , $ chan_data - > { offenses } - 1 ) ;
my $ duration = duration ( $ timeout ) ;
my $ banmask = $ self - > address_to_mask ( $ host ) ;
2021-06-17 18:58:42 +02:00
my $ join_flood_channel = $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'join_flood_channel' ) // '#stop-join-flood' ;
2020-02-15 23:38:32 +01:00
if ( $ self - > { pbot } - > { channels } - > is_active_op ( "${channel}-floodbans" ) ) {
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { banlist } - > ban_user_timed (
$ chan . '-floodbans' ,
'b' ,
2021-06-17 18:58:42 +02:00
"*!$user\@$banmask\$$join_flood_channel" ,
2020-04-29 06:33:49 +02:00
$ timeout ,
$ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ,
'join flooding' ,
) ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "$nick!$user\@$banmask banned for $duration due to join flooding (offense #" . $ chan_data - > { offenses } . ").\n" ) ;
$ self - > { pbot } - > { conn } - > privmsg (
$ nick ,
"You have been banned from $chan due to join flooding. If your connection issues have been fixed, or this was an accident, you may request an unban at any time by responding to this message with `unbanme`, otherwise you will be automatically unbanned in $duration."
) ;
} else {
$ self - > { pbot } - > { logger } - > log ( "[anti-flood] I am not an op for ${channel}-floodbans, disregarding join-flood.\n" ) ;
}
}
$ chan_data - > { join_watch } = $ max_messages - 2 ; # give them a chance to rejoin
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
}
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_CHAT ) {
2020-02-16 00:16:38 +01:00
if ( $ chan =~ /^#/ ) { #channel flood (opposed to private message or otherwise)
# don't increment offenses again if already banned
2020-04-29 07:36:13 +02:00
if ( $ self - > { pbot } - > { banlist } - > has_ban_timeout ( $ chan , "*!$user\@" . $ self - > address_to_mask ( $ host ) ) ) {
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "$nick $chan flood offense disregarded due to existing ban\n" ) ;
next ;
}
my $ chan_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ chan , 'offenses' , 'last_offense' ) ;
$ chan_data - > { offenses } + + ;
$ chan_data - > { last_offense } = gettimeofday ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
if ( $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enforce' ) ) {
my $ length = $ self - > { pbot } - > { registry } - > get_array_value ( 'antiflood' , 'chat_flood_punishment' , $ chan_data - > { offenses } - 1 ) ;
2020-05-30 03:10:49 +02:00
if ( $ self - > { pbot } - > { nicklist } - > get_meta ( $ chan , $ nick , '+o' ) ) {
$ self - > { pbot } - > { logger } - > log ( "Disregarding flood enforcement for opped user $nick in $chan.\n" ) ;
next ;
}
if ( $ self - > { pbot } - > { nicklist } - > get_meta ( $ chan , $ nick , '+v' ) ) {
2020-05-30 03:19:42 +02:00
$ self - > { pbot } - > { chanops } - > add_op_command ( $ chan , "mode $chan -v $nick" ) ;
$ self - > { pbot } - > { chanops } - > gain_ops ( $ chan ) ;
2020-05-30 03:10:49 +02:00
}
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { banlist } - > ban_user_timed (
$ chan ,
'b' ,
"*!$user\@" . $ self - > address_to_mask ( $ host ) ,
$ length ,
$ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ,
'chat flooding' ,
) ;
2020-02-15 23:38:32 +01:00
$ length = duration ( $ length ) ;
$ self - > { pbot } - > { logger } - > log ( "$nick $chan flood offense " . $ chan_data - > { offenses } . " earned $length ban\n" ) ;
$ self - > { pbot } - > { conn } - > privmsg (
$ nick ,
"You have been muted due to flooding. Please use a web paste service such as http://codepad.org for lengthy pastes. You will be allowed to speak again in approximately $length."
) ;
}
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
} else { # private message flood
my $ hostmask = $ self - > address_to_mask ( $ host ) ;
2021-07-09 23:39:35 +02:00
next if $ self - > { pbot } - > { ignorelist } - > { storage } - > exists ( $ chan , "*!$user\@$hostmask" ) ;
2020-02-15 23:38:32 +01:00
my $ chan_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ chan , 'offenses' , 'last_offense' ) ;
$ chan_data - > { offenses } + + ;
$ chan_data - > { last_offense } = gettimeofday ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
my $ length = $ self - > { pbot } - > { registry } - > get_array_value ( 'antiflood' , 'chat_flood_punishment' , $ chan_data - > { offenses } - 1 ) ;
2020-03-05 03:26:39 +01:00
my $ owner = $ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ;
$ self - > { pbot } - > { ignorelist } - > add ( $ chan , "*!$user\@$hostmask" , $ length , $ owner ) ;
2020-02-15 23:38:32 +01:00
$ length = duration ( $ length ) ;
$ self - > { pbot } - > { logger } - > log ( "$nick msg flood offense " . $ chan_data - > { offenses } . " earned $length ignore\n" ) ;
$ self - > { pbot } - > { conn } - > privmsg ( $ nick , "You have used too many commands in too short a time period, you have been ignored for $length." ) ;
}
next ;
2021-07-21 06:38:07 +02:00
} elsif ( $ mode == MSG_NICKCHANGE and $ self - > { nickflood } - > { $ ancestor } - > { changes } >= $ max_messages ) {
2020-02-15 23:38:32 +01:00
next if $ chan !~ /^#/ ;
( $ nick ) = $ text =~ m/NICKCHANGE (.*)/ ;
$ self - > { nickflood } - > { $ ancestor } - > { offenses } + + ;
$ self - > { nickflood } - > { $ ancestor } - > { changes } = $ max_messages - 2 ; # allow 1 more change (to go back to original nick)
$ self - > { nickflood } - > { $ ancestor } - > { timestamp } = gettimeofday ;
if ( $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enforce' ) ) {
my $ length = $ self - > { pbot } - > { registry } - > get_array_value ( 'antiflood' , 'nick_flood_punishment' , $ self - > { nickflood } - > { $ ancestor } - > { offenses } - 1 ) ;
2020-04-29 06:33:49 +02:00
2020-05-30 03:10:49 +02:00
if ( $ self - > { pbot } - > { nicklist } - > get_meta ( $ chan , $ nick , '+o' ) ) {
$ self - > { pbot } - > { logger } - > log ( "Disregarding flood enforcement for opped user $nick in $chan.\n" ) ;
next ;
}
if ( $ self - > { pbot } - > { nicklist } - > get_meta ( $ chan , $ nick , '+v' ) ) {
2020-05-30 03:19:42 +02:00
$ self - > { pbot } - > { chanops } - > add_op_command ( $ chan , "mode $chan -v $nick" ) ;
$ self - > { pbot } - > { chanops } - > gain_ops ( $ chan ) ;
2020-05-30 03:10:49 +02:00
}
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { banlist } - > ban_user_timed (
$ chan ,
'b' ,
"*!$user\@" . $ self - > address_to_mask ( $ host ) ,
$ length ,
$ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ,
'nick flooding' ,
) ;
2020-02-15 23:38:32 +01:00
$ length = duration ( $ length ) ;
$ self - > { pbot } - > { logger } - > log ( "$nick nickchange flood offense " . $ self - > { nickflood } - > { $ ancestor } - > { offenses } . " earned $length ban\n" ) ;
$ self - > { pbot } - > { conn } - > privmsg ( $ nick , "You have been temporarily banned due to nick-change flooding. You will be unbanned in $length." ) ;
}
}
}
}
2017-08-05 14:31:46 +02:00
2020-02-15 23:38:32 +01:00
# check for enter abuse
2021-07-21 06:38:07 +02:00
if ( $ mode == MSG_CHAT and $ chan =~ m/^#/ ) {
2020-02-15 23:38:32 +01:00
my $ chan_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ chan , 'enter_abuse' , 'enter_abuses' , 'offenses' ) ;
my $ other_offenses = delete $ chan_data - > { offenses } ;
my $ debug_enter_abuse = $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'debug_enter_abuse' ) ;
if ( defined $ self - > { channels } - > { $ chan } - > { last_spoken_nick } and $ nick eq $ self - > { channels } - > { $ chan } - > { last_spoken_nick } ) {
2021-07-21 06:38:07 +02:00
my $ messages = $ self - > { pbot } - > { messagehistory } - > { database } - > get_recent_messages ( $ account , $ chan , 2 , MSG_CHAT ) ;
2020-02-15 23:38:32 +01:00
my $ enter_abuse_threshold = $ self - > { pbot } - > { registry } - > get_value ( $ chan , 'enter_abuse_threshold' ) ;
my $ enter_abuse_time_threshold = $ self - > { pbot } - > { registry } - > get_value ( $ chan , 'enter_abuse_time_threshold' ) ;
my $ enter_abuse_max_offenses = $ self - > { pbot } - > { registry } - > get_value ( $ chan , 'enter_abuse_max_offenses' ) ;
$ enter_abuse_threshold = $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enter_abuse_threshold' ) if not defined $ enter_abuse_threshold ;
$ enter_abuse_time_threshold = $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enter_abuse_time_threshold' ) if not defined $ enter_abuse_time_threshold ;
$ enter_abuse_max_offenses = $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enter_abuse_max_offenses' ) if not defined $ enter_abuse_max_offenses ;
if ( $ messages - > [ 1 ] - > { timestamp } - $ messages - > [ 0 ] - > { timestamp } <= $ enter_abuse_time_threshold ) {
if ( + + $ chan_data - > { enter_abuse } >= $ enter_abuse_threshold - 1 ) {
$ chan_data - > { enter_abuse } = $ enter_abuse_threshold / 2 - 1 ;
$ chan_data - > { enter_abuses } + + ;
if ( $ chan_data - > { enter_abuses } >= $ enter_abuse_max_offenses ) {
if ( $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enforce' ) ) {
2020-05-30 03:10:49 +02:00
if ( $ self - > { pbot } - > { nicklist } - > get_meta ( $ chan , $ nick , '+o' ) ) {
$ self - > { pbot } - > { logger } - > log ( "Disregarding flood enforcement for opped user $nick in $chan.\n" ) ;
next ;
}
if ( $ self - > { pbot } - > { nicklist } - > get_meta ( $ chan , $ nick , '+v' ) ) {
2020-05-30 03:19:42 +02:00
$ self - > { pbot } - > { chanops } - > add_op_command ( $ chan , "mode $chan -v $nick" ) ;
$ self - > { pbot } - > { chanops } - > gain_ops ( $ chan ) ;
2020-05-30 03:10:49 +02:00
}
2020-04-29 07:36:13 +02:00
if ( $ self - > { pbot } - > { banlist } - > has_ban_timeout ( $ chan , "*!$user\@" . $ self - > address_to_mask ( $ host ) ) ) {
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "$nick $chan enter abuse offense disregarded due to existing ban\n" ) ;
next ;
}
my $ offenses = $ chan_data - > { enter_abuses } - $ enter_abuse_max_offenses + 1 + $ other_offenses ;
my $ ban_length = $ self - > { pbot } - > { registry } - > get_array_value ( 'antiflood' , 'enter_abuse_punishment' , $ offenses - 1 ) ;
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { banlist } - > ban_user_timed (
$ chan ,
'b' ,
"*!$user\@" . $ self - > address_to_mask ( $ host ) ,
$ ban_length ,
$ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ,
'enter abuse' ,
) ;
2020-02-15 23:38:32 +01:00
$ ban_length = duration ( $ ban_length ) ;
$ self - > { pbot } - > { logger } - > log ( "$nick $chan enter abuse offense " . $ chan_data - > { enter_abuses } . " earned $ban_length ban\n" ) ;
2020-04-29 06:33:49 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { conn } - > privmsg (
$ nick ,
"You have been muted due to abusing the enter key. Please do not split your sentences over multiple messages. You will be allowed to speak again in approximately $ban_length."
) ;
2020-04-29 06:33:49 +02:00
2020-02-15 23:38:32 +01:00
$ chan_data - > { last_offense } = gettimeofday ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
next ;
}
} else {
$ self - > { pbot } - > { logger } - > log ( "$nick $chan enter abuses counter incremented to " . $ chan_data - > { enter_abuses } . "\n" ) if $ debug_enter_abuse ;
if ( $ chan_data - > { enter_abuses } == $ enter_abuse_max_offenses - 1 && $ chan_data - > { enter_abuse } == $ enter_abuse_threshold / 2 - 1 ) {
if ( $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enforce' ) ) {
$ self - > { pbot } - > { conn } - > privmsg (
$ chan ,
"$nick: Please stop abusing the enter key. Feel free to type longer messages and to take a moment to think of anything else to say before you hit that enter key."
) ;
}
}
}
} else {
$ self - > { pbot } - > { logger } - > log ( "$nick $chan enter abuse counter incremented to " . $ chan_data - > { enter_abuse } . "\n" ) if $ debug_enter_abuse ;
}
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
} else {
if ( $ chan_data - > { enter_abuse } > 0 ) {
$ self - > { pbot } - > { logger } - > log ( "$nick $chan more than $enter_abuse_time_threshold seconds since last message, enter abuse counter reset\n" ) if $ debug_enter_abuse ;
$ chan_data - > { enter_abuse } = 0 ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
}
}
2017-08-05 14:31:46 +02:00
} else {
2020-02-15 23:38:32 +01:00
$ self - > { channels } - > { $ chan } - > { last_spoken_nick } = $ nick ;
$ self - > { pbot } - > { logger } - > log ( "last spoken nick set to $nick\n" ) if $ debug_enter_abuse ;
if ( $ chan_data - > { enter_abuse } > 0 ) {
$ self - > { pbot } - > { logger } - > log ( "$nick $chan enter abuse counter reset\n" ) if $ debug_enter_abuse ;
$ chan_data - > { enter_abuse } = 0 ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ chan , $ chan_data ) ;
}
2015-11-21 06:08:30 +01:00
}
2013-07-28 12:31:12 +02:00
}
2020-02-15 23:38:32 +01:00
}
2020-03-22 21:28:04 +01:00
2021-07-21 06:38:07 +02:00
$ self - > { channels } - > { $ channel } - > { last_spoken_nick } = $ nick if $ mode == MSG_CHAT ;
2020-02-15 23:38:32 +01:00
}
2017-08-05 14:31:46 +02:00
2010-06-18 06:15:28 +02:00
sub address_to_mask {
2020-02-15 23:38:32 +01:00
my ( $ self , $ address ) = @ _ ;
my $ banmask ;
if ( $ address =~ m/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) {
my ( $ a , $ b , $ c , $ d ) = ( $ 1 , $ 2 , $ 3 , $ 4 ) ;
given ( $ a ) {
when ( $ _ <= 127 ) { $ banmask = "$a.*" ; }
when ( $ _ <= 191 ) { $ banmask = "$a.$b.*" ; }
default { $ banmask = "$a.$b.$c.*" ; }
}
} elsif ( $ address =~ m {^gateway/([^/]+)/([^/]+)/} ) {
$ banmask = "gateway/$1/$2/*" ;
} elsif ( $ address =~ m {^nat/([^/]+)/} ) {
$ banmask = "nat/$1/*" ;
} elsif ( $ address =~ m/^([^:]+):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)$/ ) {
$ banmask = "$1:$2:*" ;
} elsif ( $ address =~ m/[^.]+\.([^.]+\.[a-zA-Z]+)$/ ) {
$ banmask = "*.$1" ;
} else {
$ banmask = $ address ;
2010-06-18 06:15:28 +02:00
}
2020-02-15 23:38:32 +01:00
return $ banmask ;
2013-09-13 23:48:19 +02:00
}
2020-02-15 23:38:32 +01:00
sub devalidate_accounts {
# remove validation on accounts in $channel that match a ban/quiet $mask
my ( $ self , $ mask , $ channel ) = @ _ ;
my @ message_accounts ;
2016-08-30 05:47:46 +02:00
2020-02-15 23:38:32 +01:00
#$self->{pbot}->{logger}->log("Devalidating accounts for $mask in $channel\n");
2016-11-29 10:50:49 +01:00
2020-02-15 23:38:32 +01:00
if ( $ mask =~ m/^\$a:(.*)/ ) {
my $ ban_account = lc $ 1 ;
@ message_accounts = $ self - > { pbot } - > { messagehistory } - > { database } - > find_message_accounts_by_nickserv ( $ ban_account ) ;
2016-11-29 10:50:49 +01:00
} else {
2020-02-15 23:38:32 +01:00
@ message_accounts = $ self - > { pbot } - > { messagehistory } - > { database } - > find_message_accounts_by_mask ( $ mask ) ;
2015-06-20 09:20:23 +02:00
}
2013-07-24 14:35:40 +02:00
2020-02-15 23:38:32 +01:00
foreach my $ account ( @ message_accounts ) {
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ account , $ channel , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( defined $ channel_data and $ channel_data - > { validated } & NICKSERV_VALIDATED ) {
$ channel_data - > { validated } & = ~ NICKSERV_VALIDATED ;
2015-06-20 09:20:23 +02:00
2020-02-15 23:38:32 +01:00
#$self->{pbot}->{logger}->log("Devalidating account $account\n");
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ account , $ channel , $ channel_data ) ;
}
}
}
2016-02-10 16:10:37 +01:00
2020-02-15 23:38:32 +01:00
sub check_bans {
my ( $ self , $ message_account , $ mask , $ channel , $ dry_run ) = @ _ ;
$ channel = lc $ channel ;
2015-06-20 09:20:23 +02:00
2020-02-15 23:38:32 +01:00
return if not $ self - > { pbot } - > { chanops } - > can_gain_ops ( $ channel ) ;
my $ user = $ self - > { pbot } - > { users } - > loggedin ( $ channel , $ mask ) ;
return if $ self - > { pbot } - > { capabilities } - > userhas ( $ user , 'botowner' ) ;
2017-08-05 14:31:46 +02:00
2020-02-15 23:38:32 +01:00
my $ debug_checkban = $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'debug_checkban' ) ;
2015-07-15 09:18:57 +02:00
2020-02-15 23:38:32 +01:00
my $ current_nickserv_account = $ self - > { pbot } - > { messagehistory } - > { database } - > get_current_nickserv_account ( $ message_account ) ;
2013-07-28 12:31:12 +02:00
2023-01-22 16:36:11 +01:00
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] checking for bans on ($message_account) $mask "
2020-02-15 23:38:32 +01:00
. ( defined $ current_nickserv_account and length $ current_nickserv_account ? "[$current_nickserv_account] " : "" )
. "in $channel\n" ) ;
2014-05-13 12:15:52 +02:00
2020-02-15 23:38:32 +01:00
my ( $ do_not_validate , $ bans ) ;
2016-02-10 12:42:42 +01:00
2020-02-15 23:38:32 +01:00
if ( defined $ current_nickserv_account and length $ current_nickserv_account ) {
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] current nickserv [$current_nickserv_account] found for $mask\n" ) if $ debug_checkban >= 2 ;
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ message_account , $ channel , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( $ channel_data - > { validated } & NEEDS_CHECKBAN ) {
$ channel_data - > { validated } & = ~ NEEDS_CHECKBAN ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ message_account , $ channel , $ channel_data ) ;
}
} else {
if ( not exists $ self - > { pbot } - > { irc_capabilities } - > { 'account-notify' } ) {
# mark this account as needing check-bans when nickserv account is identified
2014-05-21 06:52:17 +02:00
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ message_account , $ channel , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( not $ channel_data - > { validated } & NEEDS_CHECKBAN ) {
$ channel_data - > { validated } |= NEEDS_CHECKBAN ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ message_account , $ channel , $ channel_data ) ;
2014-05-13 12:15:52 +02:00
}
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] no account for $mask; marking for later validation\n" ) if $ debug_checkban >= 1 ;
} else {
2014-05-21 06:52:17 +02:00
$ do_not_validate = 1 ;
2020-02-15 23:38:32 +01:00
}
}
2014-03-10 04:50:02 +01:00
2020-02-15 23:38:32 +01:00
my ( $ nick ) = $ mask =~ m/^([^!]+)/ ;
my % aliases = $ self - > { pbot } - > { messagehistory } - > { database } - > get_also_known_as ( $ nick ) ;
2014-03-10 04:50:02 +01:00
2020-02-15 23:38:32 +01:00
my $ csv = Text::CSV - > new ( { binary = > 1 } ) ;
2016-07-01 21:55:52 +02:00
2020-02-15 23:38:32 +01:00
foreach my $ alias ( keys % aliases ) {
next if $ alias =~ /^Guest\d+(?:!.*)?$/ ;
2013-07-28 12:31:12 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "[after aka] processing $alias\n" ) if $ debug_checkban >= 1 ;
2013-07-28 12:31:12 +02:00
2023-02-02 02:27:06 +01:00
if ( $ aliases { $ alias } - > { type } == LINK_WEAK ) {
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] skipping WEAK alias $alias in channel $channel\n" ) if $ debug_checkban >= 2 ;
2015-05-12 06:27:22 +02:00
next ;
2020-02-15 23:38:32 +01:00
}
my @ nickservs ;
2014-05-13 12:15:52 +02:00
2020-02-15 23:38:32 +01:00
if ( exists $ aliases { $ alias } - > { nickserv } ) { @ nickservs = split /,/ , $ aliases { $ alias } - > { nickserv } ; }
else { @ nickservs = ( undef ) ; }
foreach my $ nickserv ( @ nickservs ) {
my @ gecoses ;
if ( exists $ aliases { $ alias } - > { gecos } ) {
$ csv - > parse ( $ aliases { $ alias } - > { gecos } ) ;
@ gecoses = $ csv - > fields ;
} else {
@ gecoses = ( undef ) ;
}
2014-05-21 06:52:17 +02:00
2020-02-15 23:38:32 +01:00
foreach my $ gecos ( @ gecoses ) {
my $ tgecos = defined $ gecos ? $ gecos : "[undefined]" ;
my $ tnickserv = defined $ nickserv ? $ nickserv : "[undefined]" ;
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] checking blacklist for $alias in channel $channel using gecos '$tgecos' and nickserv '$tnickserv'\n" )
if $ debug_checkban >= 5 ;
2021-07-24 01:41:32 +02:00
if ( $ self - > { pbot } - > { blacklist } - > is_blacklisted ( $ alias , $ channel , $ nickserv , $ gecos ) ) {
2020-02-15 23:38:32 +01:00
my $ u = $ self - > { pbot } - > { users } - > loggedin ( $ channel , $ mask ) ;
if ( $ self - > { pbot } - > { capabilities } - > userhas ( $ u , 'is-whitelisted' ) ) {
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] $mask [$alias] blacklisted in $channel, but allowed through whitelist\n" ) ;
next ;
}
my $ baninfo = { } ;
2020-04-29 07:36:13 +02:00
$ baninfo - > { mask } = $ alias ;
2020-02-15 23:38:32 +01:00
$ baninfo - > { channel } = $ channel ;
$ baninfo - > { owner } = 'blacklist' ;
$ baninfo - > { when } = 0 ;
$ baninfo - > { type } = 'blacklist' ;
push @$ bans , $ baninfo ;
next ;
}
}
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] checking for bans in $channel on $alias using nickserv " . ( defined $ nickserv ? $ nickserv : "[undefined]" ) . "\n" )
if $ debug_checkban >= 2 ;
2020-04-29 06:33:49 +02:00
my $ baninfos = $ self - > { pbot } - > { banlist } - > get_baninfo ( $ channel , $ alias , $ nickserv ) ;
2020-02-15 23:38:32 +01:00
if ( defined $ baninfos ) {
foreach my $ baninfo ( @$ baninfos ) {
if ( time - $ baninfo - > { when } < 5 ) {
$ self - > { pbot } - > { logger }
2020-04-29 07:36:13 +02:00
- > log ( "anti-flood: [check-bans] $mask [$alias] evaded $baninfo->{mask} in $baninfo->{channel}, but within 5 seconds of establishing ban; giving another chance\n" ) ;
2020-02-15 23:38:32 +01:00
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ message_account , $ channel , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( $ channel_data - > { validated } & NICKSERV_VALIDATED ) {
$ channel_data - > { validated } & = ~ NICKSERV_VALIDATED ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ message_account , $ channel , $ channel_data ) ;
}
$ do_not_validate = 1 ;
next ;
}
my $ u = $ self - > { pbot } - > { users } - > loggedin ( $ baninfo - > { channel } , $ mask ) ;
my $ whitelisted = $ self - > { pbot } - > { capabilities } - > userhas ( $ u , 'is-whitelisted' ) ;
2021-08-02 00:53:58 +02:00
if ( $ self - > { pbot } - > { banlist } - > is_ban_exempted ( $ baninfo - > { channel } , $ baninfo - > { mask } ) || $ whitelisted ) {
2020-04-29 07:36:13 +02:00
#$self->{pbot}->{logger}->log("anti-flood: [check-bans] $mask [$alias] evaded $baninfo->{mask} in $baninfo->{channel}, but allowed through whitelist\n");
2020-02-15 23:38:32 +01:00
next ;
}
# special case for twkm clone bans
2020-04-29 07:36:13 +02:00
if ( $ baninfo - > { mask } =~ m/\?\*!\*@\*$/ ) {
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] $mask [$alias] evaded $baninfo->{mask} in $baninfo->{channel}, but disregarded due to clone ban\n" ) ;
2020-02-15 23:38:32 +01:00
next ;
}
2020-04-29 07:36:13 +02:00
my $ banmask_regex = quotemeta $ baninfo - > { mask } ;
2020-02-15 23:38:32 +01:00
$ banmask_regex =~ s/\\\*/.*/g ;
$ banmask_regex =~ s/\\\?/./g ;
if ( $ mask =~ /^$banmask_regex$/i ) {
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] Hostmask ($mask) matches $baninfo->{type} banmask ($banmask_regex), disregarding\n" ) ;
next ;
}
2020-04-30 23:27:10 +02:00
if ( defined $ nickserv and $ baninfo - > { type } eq 'q' and $ baninfo - > { mask } =~ /^\$a:(.*)/ and lc $ 1 eq $ nickserv and $ nickserv eq $ current_nickserv_account ) {
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] Hostmask ($mask) matches quiet on account ($nickserv), disregarding\n" ) ;
next ;
}
if ( not defined $ bans ) { $ bans = [] ; }
$ self - > { pbot } - > { logger }
2020-04-29 07:36:13 +02:00
- > log ( "anti-flood: [check-bans] Hostmask ($mask [$alias" . ( defined $ nickserv ? "/$nickserv" : "" ) . "]) matches $baninfo->{type} $baninfo->{mask}, adding ban\n" ) ;
2020-02-15 23:38:32 +01:00
push @$ bans , $ baninfo ;
goto GOT_BAN ;
}
}
2013-07-28 12:31:12 +02:00
}
2011-12-17 12:43:21 +01:00
}
2015-06-21 02:48:42 +02:00
GOT_BAN:
2020-02-15 23:38:32 +01:00
if ( defined $ bans ) {
foreach my $ baninfo ( @$ bans ) {
my $ banmask ;
my ( $ user , $ host ) = $ mask =~ m/[^!]+!([^@]+)@(.*)/ ;
if ( $ host =~ m {^([^/]+)/.+} and $ 1 ne 'gateway' and $ 1 ne 'nat' ) { $ banmask = "*!*\@$host" ; }
elsif ( $ current_nickserv_account
2020-04-29 07:36:13 +02:00
and $ baninfo - > { mask } !~ m/^\$a:/i
2020-04-29 06:33:49 +02:00
and not $ self - > { pbot } - > { banlist } - > { banlist } - > exists ( $ baninfo - > { channel } , "\$a:$current_nickserv_account" ) )
2020-02-15 23:38:32 +01:00
{
$ banmask = "\$a:$current_nickserv_account" ;
} else {
2023-01-21 23:47:11 +01:00
if ( $ host =~ m {\.irccloud.com$} ) { $ banmask = "*!$user\@*.irccloud.com" ; }
2020-02-15 23:38:32 +01:00
elsif ( $ host =~ m {^nat/([^/]+)/} ) { $ banmask = "*!$user\@nat/$1/*" ; }
else {
$ banmask = "*!*\@$host" ;
#$banmask = "*!$user@" . $self->address_to_mask($host);
}
}
2016-01-23 15:03:47 +01:00
2020-04-29 07:36:13 +02:00
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] $mask evaded $baninfo->{mask} banned in $baninfo->{channel} by $baninfo->{owner}, banning $banmask\n" ) ;
2020-02-15 23:38:32 +01:00
my ( $ bannick ) = $ mask =~ m/^([^!]+)/ ;
if ( $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'enforce' ) ) {
2020-04-29 07:36:13 +02:00
if ( $ self - > { pbot } - > { banlist } - > has_ban_timeout ( $ baninfo - > { channel } , $ banmask ) ) {
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] $banmask already banned in $channel, disregarding\n" ) ;
return ;
}
2016-09-02 11:12:20 +02:00
2020-02-15 23:38:32 +01:00
my $ ancestor = $ self - > { pbot } - > { messagehistory } - > { database } - > get_ancestor_id ( $ message_account ) ;
if ( exists $ self - > { nickflood } - > { $ ancestor } and $ self - > { nickflood } - > { $ ancestor } - > { offenses } > 0 and $ baninfo - > { type } ne 'blacklist' ) {
if ( gettimeofday - $ self - > { nickflood } - > { $ ancestor } - > { timestamp } < 60 * 15 ) {
$ self - > { pbot } - > { logger } - > log ( "anti-flood: [check-bans] $mask evading nick-flood ban, disregarding\n" ) ;
return ;
}
}
if ( defined $ dry_run && $ dry_run != 0 ) {
$ self - > { pbot } - > { logger } - > log ( "Skipping ban due to dry-run.\n" ) ;
return ;
}
2020-05-30 03:10:49 +02:00
if ( $ baninfo - > { type } eq 'blacklist' ) {
$ self - > { pbot } - > { chanops } - > add_op_command ( $ baninfo - > { channel } , "kick $baninfo->{channel} $bannick I don't think so" ) ;
} else {
2020-02-15 23:38:32 +01:00
my $ owner = $ baninfo - > { owner } ;
$ owner =~ s/!.*$// ;
2020-04-29 07:36:13 +02:00
$ self - > { pbot } - > { chanops } - > add_op_command ( $ baninfo - > { channel } , "kick $baninfo->{channel} $bannick Evaded $baninfo->{mask} set by $owner" ) ;
2020-02-15 23:38:32 +01:00
}
2020-05-30 03:10:49 +02:00
2020-04-29 06:33:49 +02:00
$ self - > { pbot } - > { banlist } - > ban_user_timed (
2020-05-09 12:15:12 +02:00
$ baninfo - > { channel } ,
2020-04-29 06:33:49 +02:00
'b' ,
2020-05-09 12:15:12 +02:00
$ banmask ,
2020-04-29 06:33:49 +02:00
60 * 60 * 24 * 14 ,
$ self - > { pbot } - > { registry } - > get_value ( 'irc' , 'botnick' ) ,
2020-05-09 12:15:12 +02:00
$ baninfo - > { type } eq 'blacklist' ? 'blacklisted' : 'ban evasion' ,
2020-04-29 06:33:49 +02:00
) ;
2020-02-15 23:38:32 +01:00
}
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ message_account , $ channel , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( $ channel_data - > { validated } & NICKSERV_VALIDATED ) {
$ channel_data - > { validated } & = ~ NICKSERV_VALIDATED ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ message_account , $ channel , $ channel_data ) ;
}
return ;
2015-03-15 21:52:30 +01:00
}
2013-07-28 12:31:12 +02:00
}
2020-02-15 23:38:32 +01:00
unless ( $ do_not_validate ) {
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ message_account , $ channel , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( not $ channel_data - > { validated } & NICKSERV_VALIDATED ) {
$ channel_data - > { validated } |= NICKSERV_VALIDATED ;
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ message_account , $ channel , $ channel_data ) ;
}
2014-05-13 12:15:52 +02:00
}
2011-12-17 12:43:21 +01:00
}
2015-05-10 21:36:47 +02:00
sub on_endofwhois {
2020-02-15 23:38:32 +01:00
my ( $ self , $ event_type , $ event ) = @ _ ;
2023-01-31 14:44:34 +01:00
my $ nick = $ event - > { args } [ 1 ] ;
2020-02-15 23:38:32 +01:00
delete $ self - > { whois_pending } - > { $ nick } ;
my ( $ id , $ hostmask ) = $ self - > { pbot } - > { messagehistory } - > { database } - > find_message_account_by_nick ( $ nick ) ;
# $self->{pbot}->{logger}->log("endofwhois: Found [$id][$hostmask] for [$nick]\n");
$ self - > { pbot } - > { messagehistory } - > { database } - > link_aliases ( $ id , $ hostmask ) if $ id ;
# check to see if any channels need check-ban validation
my $ channels = $ self - > { pbot } - > { nicklist } - > get_channels ( $ nick ) ;
foreach my $ channel ( @$ channels ) {
next unless $ channel =~ /^#/ ;
my $ channel_data = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_data ( $ id , $ channel , 'validated' ) ;
2023-01-27 20:48:01 +01:00
if ( $ channel_data - > { validated } & NEEDS_CHECKBAN or not $ channel_data - > { validated } & NICKSERV_VALIDATED ) {
2023-01-22 16:36:11 +01:00
$ self - > check_bans ( $ id , $ hostmask , $ channel ) ;
}
2015-06-20 09:20:23 +02:00
}
2020-02-15 23:38:32 +01:00
return 0 ;
2015-05-10 21:36:47 +02:00
}
2016-02-10 12:42:42 +01:00
sub on_whoisuser {
2020-02-15 23:38:32 +01:00
my ( $ self , $ event_type , $ event ) = @ _ ;
2023-01-31 14:44:34 +01:00
my $ nick = $ event - > { args } [ 1 ] ;
my $ gecos = lc $ event - > { args } [ 5 ] ;
2016-02-10 12:42:42 +01:00
2020-02-15 23:38:32 +01:00
my ( $ id ) = $ self - > { pbot } - > { messagehistory } - > { database } - > find_message_account_by_nick ( $ nick ) ;
2016-02-10 12:42:42 +01:00
2020-02-15 23:38:32 +01:00
if ( $ self - > { pbot } - > { registry } - > get_value ( 'antiflood' , 'debug_checkban' ) >= 2 ) { $ self - > { pbot } - > { logger } - > log ( "Got gecos for $nick ($id): '$gecos'\n" ) ; }
2016-02-10 12:42:42 +01:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > update_gecos ( $ id , $ gecos , scalar gettimeofday ) ;
2016-02-10 12:42:42 +01:00
}
2011-02-11 03:46:35 +01:00
sub on_whoisaccount {
2020-02-15 23:38:32 +01:00
my ( $ self , $ event_type , $ event ) = @ _ ;
2023-01-31 14:44:34 +01:00
my $ nick = $ event - > { args } [ 1 ] ;
my $ account = lc $ event - > { args } [ 2 ] ;
2020-02-15 23:38:32 +01:00
2023-01-22 16:36:11 +01:00
$ self - > { pbot } - > { logger } - > log ( "[MH] $nick is using NickServ account [$account]\n" ) ;
2011-02-11 03:46:35 +01:00
2020-02-15 23:38:32 +01:00
my ( $ id , $ hostmask ) = $ self - > { pbot } - > { messagehistory } - > { database } - > find_message_account_by_nick ( $ nick ) ;
2015-05-12 06:27:22 +02:00
2023-01-22 16:36:11 +01:00
if ( $ id ) {
$ self - > { pbot } - > { messagehistory } - > { database } - > link_aliases ( $ id , undef , $ account ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_nickserv_account ( $ id , $ account , scalar gettimeofday ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > set_current_nickserv_account ( $ id , $ account ) ;
} else {
$ self - > { pbot } - > { logger } - > log ( "[MH] No message account found for $nick [$account]; cannot update database.\n" ) ;
}
2015-06-20 09:20:23 +02:00
2020-02-15 23:38:32 +01:00
return 0 ;
2011-02-11 03:46:35 +01:00
}
2016-08-28 12:49:27 +02:00
sub on_accountnotify {
2020-02-15 23:38:32 +01:00
my ( $ self , $ event_type , $ event ) = @ _ ;
2016-08-28 12:49:27 +02:00
2023-01-31 14:44:34 +01:00
my $ mask = $ event - > { from } ;
2023-01-22 16:36:11 +01:00
my ( $ nick , $ user , $ host ) = $ mask =~ m/^([^!]+)!([^@]+)@(.*)/ ;
2023-01-31 14:44:34 +01:00
my $ account = $ event - > { args } [ 0 ] ;
2023-01-22 16:36:11 +01:00
my $ id = $ self - > { pbot } - > { messagehistory } - > { database } - > get_message_account ( $ nick , $ user , $ host ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_hostmask_data ( $ mask , { last_seen = > scalar gettimeofday } ) ;
2016-11-29 10:50:49 +01:00
2023-01-22 16:36:11 +01:00
if ( $ account eq '*' ) {
$ self - > { pbot } - > { logger } - > log ( "[MH] ($id) $mask logged out of NickServ\n" ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > set_current_nickserv_account ( $ id , '' ) ;
2020-02-15 23:38:32 +01:00
} else {
2023-01-22 16:36:11 +01:00
$ self - > { pbot } - > { logger } - > log ( "[MH] ($id) $mask logged into NickServ account $account\n" ) ;
2016-08-28 12:49:27 +02:00
2023-01-22 16:36:11 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > link_aliases ( $ id , undef , $ account ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_nickserv_account ( $ id , $ account , scalar gettimeofday ) ;
$ self - > { pbot } - > { messagehistory } - > { database } - > set_current_nickserv_account ( $ id , $ account ) ;
2016-08-28 12:49:27 +02:00
2020-02-15 23:38:32 +01:00
$ self - > { pbot } - > { messagehistory } - > { database } - > devalidate_all_channels ( $ id ) ;
2016-08-28 12:49:27 +02:00
2020-02-15 23:38:32 +01:00
my $ channels = $ self - > { pbot } - > { nicklist } - > get_channels ( $ nick ) ;
foreach my $ channel ( @$ channels ) {
next unless $ channel =~ /^#/ ;
2023-01-22 16:36:11 +01:00
$ self - > check_bans ( $ id , $ mask , $ channel ) ;
2020-02-15 23:38:32 +01:00
}
2016-08-28 12:49:27 +02:00
}
2023-01-22 16:36:11 +01:00
2020-02-15 23:38:32 +01:00
return 0 ;
2016-08-28 12:49:27 +02:00
}
2014-05-13 12:15:52 +02:00
sub adjust_offenses {
2020-02-15 23:38:32 +01:00
my $ self = shift ;
2014-02-05 02:10:56 +01:00
2020-02-15 23:38:32 +01:00
#$self->{pbot}->{logger}->log("Adjusting offenses . . .\n");
2014-05-13 12:15:52 +02:00
2020-02-15 23:38:32 +01:00
# decrease offenses counter if 24 hours have elapsed since latest offense
my $ channel_datas = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_datas_where_last_offense_older_than ( gettimeofday - 60 * 60 * 24 ) ;
foreach my $ channel_data ( @$ channel_datas ) {
my $ id = delete $ channel_data - > { id } ;
my $ channel = delete $ channel_data - > { channel } ;
my $ update = 0 ;
2016-01-17 01:55:48 +01:00
2020-02-15 23:38:32 +01:00
if ( $ channel_data - > { offenses } > 0 ) {
$ channel_data - > { offenses } - - ;
$ update = 1 ;
}
2016-01-17 01:55:48 +01:00
2020-02-15 23:38:32 +01:00
if ( defined $ channel_data - > { unbanmes } and $ channel_data - > { unbanmes } > 0 ) {
$ channel_data - > { unbanmes } - - ;
$ update = 1 ;
}
2016-01-17 01:55:48 +01:00
2020-02-15 23:38:32 +01:00
if ( $ update ) {
$ channel_data - > { last_offense } = gettimeofday ;
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ id , $ channel , $ channel_data ) ;
}
2014-05-13 12:15:52 +02:00
}
2020-02-15 23:38:32 +01:00
$ channel_datas = $ self - > { pbot } - > { messagehistory } - > { database } - > get_channel_datas_with_enter_abuses ( ) ;
foreach my $ channel_data ( @$ channel_datas ) {
my $ id = delete $ channel_data - > { id } ;
my $ channel = delete $ channel_data - > { channel } ;
my $ last_offense = delete $ channel_data - > { last_offense } ;
if ( gettimeofday - $ last_offense >= 60 * 60 * 3 ) {
$ channel_data - > { enter_abuses } - - ;
#$self->{pbot}->{logger}->log("[adjust-offenses] [$id][$channel] decreasing enter abuse offenses to $channel_data->{enter_abuses}\n");
$ self - > { pbot } - > { messagehistory } - > { database } - > update_channel_data ( $ id , $ channel , $ channel_data ) ;
}
2014-07-11 14:54:56 +02:00
}
2014-05-16 02:48:46 +02:00
2020-02-15 23:38:32 +01:00
foreach my $ account ( keys % { $ self - > { nickflood } } ) {
if ( $ self - > { nickflood } - > { $ account } - > { offenses } and gettimeofday - $ self - > { nickflood } - > { $ account } - > { timestamp } >= 60 * 60 ) {
$ self - > { nickflood } - > { $ account } - > { offenses } - - ;
2014-05-16 02:48:46 +02:00
2023-01-22 16:36:11 +01:00
if ( $ self - > { nickflood } - > { $ account } - > { offenses } <= 0 ) {
delete $ self - > { nickflood } - > { $ account } ;
} else {
$ self - > { nickflood } - > { $ account } - > { timestamp } = gettimeofday ;
}
2020-02-15 23:38:32 +01:00
}
2014-05-16 02:48:46 +02:00
}
2014-02-05 02:10:56 +01:00
}
2010-03-17 07:36:54 +01:00
1 ;