3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 05:49:27 +01:00

BanList: misc refactors to improve readability

This commit is contained in:
Pragmatic Software 2021-09-12 22:24:29 -07:00
parent 153d771960
commit 5cf9ac1b9f
4 changed files with 48 additions and 44 deletions

View File

@ -97,9 +97,7 @@ sub checkban {
sub is_ban_exempted {
my ($self, $channel, $hostmask) = @_;
$channel = lc $channel;
$hostmask = lc $hostmask;
return 1 if $self->{'ban-exemptions'}->exists($channel, $hostmask);
return 1 if $self->{'ban-exemptions'}->exists(lc $channel, lc $hostmask);
return 0;
}
@ -286,8 +284,11 @@ sub get_baninfo {
foreach my $entry (@lists) {
my ($mode, $list) = @$entry;
foreach my $banmask ($list->get_keys($channel)) {
if ($banmask =~ m/^\$a:(.*)/) { $ban_nickserv = lc $1; }
else { $ban_nickserv = ""; }
if ($banmask =~ m/^\$a:(.*)/) {
$ban_nickserv = lc $1;
} else {
$ban_nickserv = '';
}
my $banmask_regex = quotemeta $banmask;
$banmask_regex =~ s/\\\*/.*?/g;
@ -338,17 +339,20 @@ sub nick_to_banmask {
}
}
# make sure $mask always has full wildcards
# there's probably a better way to do this...
if ($mask !~ /^\$/) {
# $a:account, etc, don't need wildcards appended
if ($mask =~ /^\$/) {
return $mask;
}
# ensure $mask is a complete hostmask by appending missing bits with wildcards
if ($mask !~ /!/) {
$mask .= '!*@*';
} elsif ($mask !~ /@/) {
$mask =~ s/\*?$/*@*/;
} else {
# TODO find out if/where this weird case happens and why...
$mask =~ s/\@$/@*/;
}
}
return $mask;
}

View File

@ -98,7 +98,7 @@ sub cmd_checkban {
return "Usage: checkban <mask> [channel]" if not defined $target;
$channel = $context->{from} if not defined $channel;
return "Please specify a channel." if $channel !~ /^#/;
return "Channel must be specified in /msg; usage: checkban <mask> <channel>" if $channel !~ /^#/;
return $self->{pbot}->{banlist}->checkban($channel, 'b', $target);
}
@ -109,7 +109,7 @@ sub cmd_checkmute {
return "Usage: checkmute <mask> [channel]" if not defined $target;
$channel = $context->{from} if not defined $channel;
return "Please specify a channel." if $channel !~ /^#/;
return "Channel must be specified in /msg; usage: checkmute <mask> <channel>" if $channel !~ /^#/;
return $self->{pbot}->{banlist}->checkban($channel, $self->{pbot}->{registry}->get_value('banlist', 'mute_mode_char'), $target);
}

View File

@ -16,25 +16,27 @@ use Time::Duration;
sub initialize {
my ($self, %conf) = @_;
$self->{pbot}->{event_dispatcher}->register_handler('irc.endofnames', sub { $self->get_banlist(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.endofnames', sub { $self->on_endofnames(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.banlist', sub { $self->on_banlist_entry(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.quietlist', sub { $self->on_quietlist_entry(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.endofbanlist', sub { $self->compare_banlist(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.endofquietlist', sub { $self->compare_quietlist(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.endofbanlist', sub { $self->on_endofbanlist(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.endofquietlist', sub { $self->on_endofquietlist(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.modeflag', sub { $self->on_modeflag(@_) });
$self->{mute_char} = $self->{pbot}->{registry}->get_value('banlist', 'mute_mode_char');
}
# irc.endofnames
sub get_banlist {
# typically, immediately after joining a channel...
sub on_endofnames {
my ($self, $event_type, $event) = @_;
my $channel = lc $event->{event}->{args}[1];
$self->{pbot}->{logger}->log("Retrieving banlist for $channel.\n");
delete $self->{temp_banlist};
# banlist cache temporarily holds the banlist/quietlist entries until
# irc.endofbanlist and irc.endofquietlist are received.
delete $self->{banlist_cache};
my $mute_char = $self->{mute_char};
@ -57,7 +59,7 @@ sub on_banlist_entry {
my $ago = concise ago(gettimeofday - $timestamp);
$self->{pbot}->{logger}->log("Ban List: [banlist entry] $channel: $target banned by $source $ago.\n");
$self->{temp_banlist}->{$channel}->{'+b'}->{$target} = [$source, $timestamp];
$self->{banlist_cache}->{$channel}->{'+b'}->{$target} = [$source, $timestamp];
return 1;
}
@ -72,18 +74,17 @@ sub on_quietlist_entry {
my $ago = concise ago(gettimeofday - $timestamp);
$self->{pbot}->{logger}->log("Ban List: [quietlist entry] $channel: $target quieted by $source $ago.\n");
my $mute_char = $self->{mute_char};
$self->{temp_banlist}->{$channel}->{"+$mute_char"}->{$target} = [$source, $timestamp];
$self->{banlist_cache}->{$channel}->{"+$mute_char"}->{$target} = [$source, $timestamp];
return 1;
}
# irc.endofbanlist
sub compare_banlist {
sub on_endofbanlist {
my ($self, $event_type, $event) = @_;
my $channel = lc $event->{event}->{args}[1];
# first check for saved bans no longer in channel
foreach my $mask ($self->{pbot}->{banlist}->{banlist}->get_keys($channel)) {
if (not exists $self->{temp_banlist}->{$channel}->{'+b'}->{$mask}) {
if (not exists $self->{banlist_cache}->{$channel}->{'+b'}->{$mask}) {
$self->{pbot}->{logger}->log("BanList: Saved ban +b $mask no longer exists in $channel.\n");
# TODO option to restore ban
$self->{pbot}->{banlist}->{banlist}->remove($channel, $mask, undef, 1);
@ -94,9 +95,9 @@ sub compare_banlist {
my $save = 0;
# add channel bans to saved bans
foreach my $mask (keys %{$self->{temp_banlist}->{$channel}->{'+b'}}) {
my $owner = $self->{temp_banlist}->{$channel}->{'+b'}->{$mask}->[0];
my $timestamp = $self->{temp_banlist}->{$channel}->{'+b'}->{$mask}->[1];
foreach my $mask (keys %{$self->{banlist_cache}->{$channel}->{'+b'}}) {
my $owner = $self->{banlist_cache}->{$channel}->{'+b'}->{$mask}->[0];
my $timestamp = $self->{banlist_cache}->{$channel}->{'+b'}->{$mask}->[1];
my $add = 0;
@ -105,7 +106,7 @@ sub compare_banlist {
# only update owner/timestamp if there's no existing entry or if the owner is a full hostmask.
# this prevents updating the owner/timestamp to a server value, losing information about who
# originally set the ban
if (!defined $data || $owner =~ /@/) {
if (!defined $data || $owner =~ /^.+!.+@.+$/) {
$data->{owner} = $owner;
$data->{timestamp} = $timestamp;
$add = 1;
@ -135,12 +136,11 @@ sub compare_banlist {
}
$self->{pbot}->{banlist}->{banlist}->save if $save;
delete $self->{temp_banlist}->{$channel}->{'+b'};
delete $self->{banlist_cache}->{$channel}->{'+b'};
return 1;
}
# irc.endofquietlist
sub compare_quietlist {
sub on_endofquietlist {
my ($self, $event_type, $event) = @_;
my $channel = lc $event->{event}->{args}[1];
@ -148,7 +148,7 @@ sub compare_quietlist {
# first check for saved quiets no longer in channel
foreach my $mask ($self->{pbot}->{banlist}->{quietlist}->get_keys($channel)) {
if (not exists $self->{temp_banlist}->{$channel}->{"+$mute_char"}->{$mask}) {
if (not exists $self->{banlist_cache}->{$channel}->{"+$mute_char"}->{$mask}) {
$self->{pbot}->{logger}->log("BanList: Saved quiet +q $mask no longer exists in $channel.\n");
# TODO option to restore quiet
$self->{pbot}->{banlist}->{quietlist}->remove($channel, $mask, undef, 1);
@ -159,16 +159,16 @@ sub compare_quietlist {
my $save = 0;
# add channel bans to saved bans
foreach my $mask (keys %{$self->{temp_banlist}->{$channel}->{"+$mute_char"}}) {
my $owner = $self->{temp_banlist}->{$channel}->{"+$mute_char"}->{$mask}->[0];
my $timestamp = $self->{temp_banlist}->{$channel}->{"+$mute_char"}->{$mask}->[1];
foreach my $mask (keys %{$self->{banlist_cache}->{$channel}->{"+$mute_char"}}) {
my $owner = $self->{banlist_cache}->{$channel}->{"+$mute_char"}->{$mask}->[0];
my $timestamp = $self->{banlist_cache}->{$channel}->{"+$mute_char"}->{$mask}->[1];
my $data = $self->{pbot}->{banlist}->{quietlist}->get_data($channel, $mask);
# only update owner/timestamp if there's no existing entry or if the owner is a full hostmask.
# this prevents updating the owner/timestamp to a server value, losing information about who
# originally set the quiet
if (!defined $data || $owner =~ /@/) {
if (!defined $data || $owner =~ /^.+!.+@.+$/) {
$data->{owner} = $owner;
$data->{timestamp} = $timestamp;
$self->{pbot}->{banlist}->{quietlist}->add($channel, $mask, $data, 1);
@ -177,7 +177,7 @@ sub compare_quietlist {
}
$self->{pbot}->{banlist}->{quietlist}->save if $save;
delete $self->{temp_banlist}->{$channel}->{"+$mute_char"};
delete $self->{banlist_cache}->{$channel}->{"+$mute_char"};
return 1;
}
@ -206,7 +206,7 @@ sub on_modeflag {
# only update owner/timestamp if there's no existing entry or if the source is a full hostmask.
# this prevents updating the owner/timestamp to a server value, losing information about who
# originally set it
if (!defined $data || $source =~ /@/) {
if (!defined $data || $source =~ /^.+!.+@.+$/) {
$data->{owner} = $source;
$data->{timestamp} = scalar gettimeofday;
$self->{pbot}->{banlist}->{banlist}->add($channel, $mask, $data);
@ -217,7 +217,7 @@ sub on_modeflag {
# only update owner/timestamp if there's no existing entry or if the source is a full hostmask.
# this prevents updating the owner/timestamp to a server value, losing information about who
# originally set it
if (!defined $data || $source =~ /@/) {
if (!defined $data || $source =~ /^.+!.+@.+$/) {
$data->{owner} = $source;
$data->{timestamp} = scalar gettimeofday;
$self->{pbot}->{banlist}->{quietlist}->add($channel, $mask, $data);

View File

@ -25,7 +25,7 @@ use PBot::Imports;
# These are set by the /misc/update_version script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 4387,
BUILD_REVISION => 4388,
BUILD_DATE => "2021-09-12",
};