3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 13:59:47 +01:00

Users: improve find_user_account

This commit is contained in:
Pragmatic Software 2020-02-09 20:38:44 -08:00
parent c9fed700ef
commit 9afb8c506d

View File

@ -138,21 +138,24 @@ sub save {
sub find_user_account { sub find_user_account {
my ($self, $channel, $hostmask) = @_; my ($self, $channel, $hostmask) = @_;
$channel = lc $channel; $channel = lc $channel;
$hostmask = lc $hostmask; $hostmask = lc $hostmask;
my ($found_channel, $found_hostmask) = (undef, $hostmask);
foreach my $chan (keys %{ $self->{users}->{hash} }) { my $sort;
if ($channel =~ m/^#/) {
$sort = sub { $a cmp $b };
} else {
$sort = sub { $b cmp $a };
}
foreach my $chan (sort $sort keys %{ $self->{users}->{hash} }) {
if ($channel !~ m/^#/ or $channel =~ m/^$chan$/i) { if ($channel !~ m/^#/ or $channel =~ m/^$chan$/i) {
if (not exists $self->{users}->{hash}->{$chan}->{$hostmask}) { if (not exists $self->{users}->{hash}->{$chan}->{$hostmask}) {
# find hostmask by account name or wildcard # find hostmask by account name or wildcard
foreach my $mask (keys %{ $self->{users}->{hash}->{$chan} }) { foreach my $mask (keys %{ $self->{users}->{hash}->{$chan} }) {
next if $mask eq '_name'; next if $mask eq '_name';
if (lc $self->{users}->{hash}->{$chan}->{$mask}->{name} eq $hostmask) { if (lc $self->{users}->{hash}->{$chan}->{$mask}->{name} eq $hostmask) {
$found_hostmask = $mask; return ($chan, $mask);
$found_channel = $chan;
last;
} }
if ($mask =~ /[*?]/) { if ($mask =~ /[*?]/) {
@ -161,19 +164,16 @@ sub find_user_account {
$mask_quoted =~ s/\\\*/.*?/g; $mask_quoted =~ s/\\\*/.*?/g;
$mask_quoted =~ s/\\\?/./g; $mask_quoted =~ s/\\\?/./g;
if ($hostmask =~ m/^$mask_quoted$/i) { if ($hostmask =~ m/^$mask_quoted$/i) {
$found_hostmask = $mask; return ($chan, $mask);
$found_channel = $chan;
last;
} }
} }
} }
} else { } else {
$found_channel = $chan; return ($chan, $hostmask);
last;
} }
} }
} }
return ($found_channel, $found_hostmask); return (undef, $hostmask);
} }
sub find_user { sub find_user {
@ -184,8 +184,7 @@ sub find_user {
$hostmask = '.*' if not defined $hostmask; $hostmask = '.*' if not defined $hostmask;
$hostmask = lc $hostmask; $hostmask = lc $hostmask;
my $result = eval { my $user = eval {
my $user;
foreach my $channel_regex (keys %{ $self->{users}->{hash} }) { foreach my $channel_regex (keys %{ $self->{users}->{hash} }) {
if ($channel !~ m/^#/ or $channel =~ m/^$channel_regex$/i) { if ($channel !~ m/^#/ or $channel =~ m/^$channel_regex$/i) {
foreach my $hostmask_regex (keys %{ $self->{users}->{hash}->{$channel_regex} }) { foreach my $hostmask_regex (keys %{ $self->{users}->{hash}->{$channel_regex} }) {
@ -213,7 +212,7 @@ sub find_user {
if ($@) { if ($@) {
$self->{pbot}->{logger}->log("Error in find_user parameters: $@\n"); $self->{pbot}->{logger}->log("Error in find_user parameters: $@\n");
} }
return $result; return $user;
} }
sub find_admin { sub find_admin {