From e60476751757e209e1de5f4f2059986c3be745c7 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 15 Mar 2015 13:52:30 -0700 Subject: [PATCH] Add support for shitlisting hostmasks --- PBot/AntiFlood.pm | 35 ++++++-- PBot/PBot.pm | 2 + PBot/ShitList.pm | 204 ++++++++++++++++++++++++++++++++++++++++++++++ config/shitlist | 0 pbot.pl | 3 + 5 files changed, 235 insertions(+), 9 deletions(-) create mode 100644 PBot/ShitList.pm create mode 100644 config/shitlist diff --git a/PBot/AntiFlood.pm b/PBot/AntiFlood.pm index c60d5e8f..4e9f4c10 100644 --- a/PBot/AntiFlood.pm +++ b/PBot/AntiFlood.pm @@ -202,6 +202,14 @@ sub check_flood { $self->{pbot}->{logger}->log(sprintf("%-18s | %-65s | %s\n", lc $channel eq lc $mask ? "QUIT" : $channel, $mask, $text)); } + # do not do flood processing for bot messages + if($nick eq $self->{pbot}->{registry}->get_value('irc', 'botnick')) { + foreach my $channel (keys %{ $self->{pbot}->{channels}->{channels}->hash }) { + $self->{channels}->{$channel}->{last_spoken_nick} = $nick; + } + return; + } + # 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) @@ -225,14 +233,6 @@ sub check_flood { $self->check_join_watch($account, $channel, $text, $mode); push @$channels, $channel; } - - # do not do flood processing for bot messages - if($nick eq $self->{pbot}->{registry}->get_value('irc', 'botnick')) { - foreach my $channel (@$channels) { - $self->{channels}->{$channel}->{last_spoken_nick} = $nick; - } - return; - } foreach my $channel (@$channels) { # do not do flood processing if channel is not in bot's channel list or bot is not set as chanop for the channel @@ -408,6 +408,7 @@ sub check_flood { $self->{pbot}->{conn}->privmsg($nick, "You have used too many commands in too short a time period, you have been ignored for $length."); } } elsif($mode == $self->{pbot}->{messagehistory}->{MSG_NICKCHANGE} and $self->{nickflood}->{$account}->{changes} >= $max_messages) { + next if $channel !~ /^#/; ($nick) = $text =~ m/NICKCHANGE (.*)/; $self->{nickflood}->{$account}->{offenses}++; @@ -582,6 +583,18 @@ sub check_bans { CHECKBAN: if($check_ban) { + $self->{pbot}->{logger}->log("anti-flood: [check-bans] checking shitlist for $hostmask->{hostmask} in channel $channel\n") if $debug_checkban >= 4; + if ($self->{pbot}->{shitlist}->check_shitlist($hostmask->{hostmask}, $channel)) { + my $baninfo = {}; + $baninfo->{banmask} = $hostmask->{hostmask}; + $baninfo->{channel} = $channel; + $baninfo->{owner} = 'shitlist'; + $baninfo->{when} = 0; + $baninfo->{type} = 'shitlist'; + push @$bans, $baninfo; + next; + } + $self->{pbot}->{logger}->log("anti-flood: [check-bans] checking for bans in $channel on $hostmask->{hostmask} using $hostmask->{nickserv}\n") if $debug_checkban >= 4; my $baninfos = $self->{pbot}->{bantracker}->get_baninfo($hostmask->{hostmask}, $channel, $hostmask->{nickserv}); @@ -649,7 +662,11 @@ sub check_bans { $self->{pbot}->{logger}->log("anti-flood: [check-bans] $mask evaded $baninfo->{banmask} banned in $baninfo->{channel} by $baninfo->{owner}, banning $banmask\n"); my ($bannick) = $mask =~ m/^([^!]+)/; if($self->{pbot}->{registry}->get_value('antiflood', 'enforce')) { - $self->{pbot}->{chanops}->add_op_command($baninfo->{channel}, "kick $baninfo->{channel} $bannick Ban evasion"); + if ($baninfo->{type} eq 'shitlist') { + $self->{pbot}->{chanops}->add_op_command($baninfo->{channel}, "kick $baninfo->{channel} $bannick I don't think so"); + } else { + $self->{pbot}->{chanops}->add_op_command($baninfo->{channel}, "kick $baninfo->{channel} $bannick Ban evasion"); + } $self->{pbot}->{chanops}->ban_user_timed($banmask, $baninfo->{channel}, 60 * 60 * 12); } my $channel_data = $self->{pbot}->{messagehistory}->{database}->get_channel_data($message_account, $channel, 'validated'); diff --git a/PBot/PBot.pm b/PBot/PBot.pm index 7882f73d..6ced930f 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -44,6 +44,7 @@ use PBot::ChanOps; use PBot::Factoids; use PBot::BotAdmins; use PBot::IgnoreList; +use PBot::ShitList; use PBot::Quotegrabs; use PBot::Timer; use PBot::AntiAway; @@ -112,6 +113,7 @@ sub initialize { $self->{messagehistory} = PBot::MessageHistory->new(pbot => $self, filename => delete $conf{messagehistory_file}, %conf); $self->{antiflood} = PBot::AntiFlood->new(pbot => $self, %conf); $self->{ignorelist} = PBot::IgnoreList->new(pbot => $self, filename => delete $conf{ignorelist_file}, %conf); + $self->{shitlist} = PBot::ShitList->new(pbot => $self, filename => delete $conf{shitlist_file}, %conf); $self->{irc} = PBot::IRC->new(); $self->{irchandlers} = PBot::IRCHandlers->new(pbot => $self, %conf); $self->{channels} = PBot::Channels->new(pbot => $self, filename => delete $conf{channels_file}, %conf); diff --git a/PBot/ShitList.pm b/PBot/ShitList.pm new file mode 100644 index 00000000..6155aca5 --- /dev/null +++ b/PBot/ShitList.pm @@ -0,0 +1,204 @@ +# File: ShitList.pm +# Author: pragma_ +# +# Purpose: Manages list of hostmasks that are not allowed to join a channel. + +package PBot::ShitList; + +use warnings; +use strict; + +use Carp (); +use Time::HiRes qw(gettimeofday); + +sub new { + if(ref($_[1]) eq 'HASH') { + Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference"); + } + + 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->{filename} = delete $conf{filename}; + + $self->{shitlist} = {}; + + $self->{pbot}->{commands}->register(sub { return $self->shitlist_user(@_) }, "shitlist", 10); + $self->{pbot}->{commands}->register(sub { return $self->unshitlist_user(@_) }, "unshitlist", 10); + + $self->load_shitlist; +} + +sub add { + my ($self, $channel, $hostmask) = @_; + + $self->{shitlist}->{lc $channel}->{lc $hostmask} = 1; + $self->save_shitlist(); +} + +sub remove { + my $self = shift; + my ($channel, $hostmask) = @_; + + $channel = lc $channel; + $hostmask = lc $hostmask; + + if (exists $self->{shitlist}->{$channel}) { + delete $$self->{shitlist}->{$channel}->{$hostmask}; + } + + if (keys $self->{shitlist}->{$channel} == 0) { + delete $self->{shitlist}->{$channel}; + } + + $self->save_shitlist(); +} + +sub load_shitlist { + my $self = shift; + my $filename; + + if(@_) { $filename = shift; } else { $filename = $self->{filename}; } + + if(not defined $filename) { + Carp::carp "No shitlist path specified -- skipping loading of shitlist"; + return; + } + + $self->{pbot}->{logger}->log("Loading shitlist from $filename ...\n"); + + open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n"; + my @contents = ; + close(FILE); + + my $i = 0; + + foreach my $line (@contents) { + chomp $line; + $i++; + + my ($channel, $hostmask) = split(/\s+/, $line); + + if(not defined $hostmask || not defined $channel) { + Carp::croak "Syntax error around line $i of $filename\n"; + } + + if(exists $self->{shitlist}->{$channel}->{$hostmask}) { + Carp::croak "Duplicate shitlist entry [$hostmask][$channel] found in $filename around line $i\n"; + } + + $self->{shitlist}->{$channel}->{$hostmask} = 1; + } + + $self->{pbot}->{logger}->log(" $i entries in shitlist\n"); + $self->{pbot}->{logger}->log("Done.\n"); +} + +sub save_shitlist { + my $self = shift; + my $filename; + + if(@_) { $filename = shift; } else { $filename = $self->{filename}; } + + if(not defined $filename) { + Carp::carp "No shitlist path specified -- skipping saving of shitlist\n"; + return; + } + + open(FILE, "> $filename") or die "Couldn't open $filename: $!\n"; + + foreach my $channel (keys %{ $self->{shitlist} }) { + foreach my $hostmask (keys %{ $self->{shitlist}->{$channel} }) { + print FILE "$channel $hostmask\n"; + } + } + + close(FILE); +} + +sub check_shitlist { + my $self = shift; + my ($hostmask, $channel) = @_; + + return 0 if not defined $channel; + + foreach my $shit_channel (keys %{ $self->{shitlist} }) { + foreach my $shit_hostmask (keys %{ $self->{shitlist}->{$shit_channel} }) { + my $shit_channel_escaped = quotemeta $shit_channel; + my $shit_hostmask_escaped = quotemeta $shit_hostmask; + + $shit_channel_escaped =~ s/\\(\.|\*)/$1/g; + $shit_hostmask_escaped =~ s/\\(\.|\*)/$1/g; + + if(($channel =~ /$shit_channel_escaped/i) && ($hostmask =~ /$shit_hostmask_escaped/i)) { + $self->{pbot}->{logger}->log("$hostmask shitlisted in channel $channel (matches [$shit_hostmask] host and [$shit_channel] channel)\n"); + return 1; + } + } + } + return 0; +} + +sub shitlist_user { + my $self = shift; + my ($from, $nick, $user, $host, $arguments) = @_; + + return "Usage: shitlist [channel]" if not $arguments; + + my ($target, $channel) = split /\s+/, $arguments; + + if($target =~ /^list$/i) { + my $text = "Shitlisted: "; + + my $sep = ""; + foreach my $channel (keys %{ $self->{shitlist} }) { + $text .= "$channel: "; + foreach my $hostmask (keys %{ $self->{shitlist}->{$channel} }) { + $text .= $sep . $hostmask; + $sep = ";\n"; + } + } + return $text; + } + + if(not defined $channel) { + $channel = ".*"; # all channels + } + + $self->{pbot}->{logger}->log("$nick!$user\@$host added [$target] to shitlist for channel [$channel]\n"); + $self->add($channel, $target); + return "$target added to shitlist for channel $channel"; +} + +sub unshitlist_user { + my $self = shift; + my ($from, $nick, $user, $host, $arguments) = @_; + my ($channel, $target) = split /\s+/, $arguments if $arguments; + + if(not defined $target) { + return "Usage: unshitlist [channel]"; + } + + if(not defined $channel) { + $channel = ".*"; + } + + if(not exists $self->{shitlist}->{$channel} and not exists $self->{shitlist}->{$channel}->{$target}) { + $self->{pbot}->{logger}->log("$nick attempt to remove nonexistent [$target][$channel] from shitlist\n"); + return "$target not found in shitlist for channel $channel (use `shitlist list` to display shitlist)"; + } + + $self->remove($channel, $target); + $self->{pbot}->{logger}->log("$nick!$user\@$host removed [$target] from shitlist for channel [$channel]\n"); + return "$target removed from shitlist for channel $channel"; +} + +1; diff --git a/config/shitlist b/config/shitlist new file mode 100644 index 00000000..e69de29b diff --git a/pbot.pl b/pbot.pl index abab2789..e09d0ace 100755 --- a/pbot.pl +++ b/pbot.pl @@ -103,6 +103,9 @@ $config{channels_file} = "$config{config_dir}/channels"; # Location of file containing ignorelist entries $config{ignorelist_file} = "$config{config_dir}/ignorelist"; +# Location of file containing shitlist entries +$config{shitlist_file} = "$config{config_dir}/shitlist"; + # Location of file containing factoids and modules $config{factoids_file} = "$config{data_dir}/factoids";