3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-01 17:16:39 +02:00

MessageHistory: aka can now sort output; added option -l to show last seen time

This commit is contained in:
Pragmatic Software 2020-05-18 00:05:15 -07:00
parent 565f4b080b
commit da9abb4fbc
2 changed files with 94 additions and 7 deletions

View File

@ -55,7 +55,7 @@ sub initialize {
sub cmd_list_also_known_as {
my ($self, $context) = @_;
my $usage = "Usage: aka [-hingr] <nick | hostmask>; -h show hostmasks; -i show ids; -n show nickserv accounts; -g show gecos, -r show relationships";
my $usage = "Usage: aka [-hilngr] <nick> [-sort <by>]; -h show hostmasks; -i show ids; -l show last seen, -n show nickserv accounts; -g show gecos, -r show relationships";
if (not length $context->{arguments}) { return $usage; }
@ -65,25 +65,95 @@ sub cmd_list_also_known_as {
chomp $getopt_error;
};
Getopt::Long::Configure("bundling");
Getopt::Long::Configure("bundling_override");
my ($show_hostmasks, $show_gecos, $show_nickserv, $show_id, $show_relationship, $show_weak, $dont_use_aliases_table);
my $sort_method = 'nick';
my ($show_hostmasks, $show_gecos, $show_nickserv, $show_id, $show_relationship, $show_weak, $show_last_seen, $dont_use_aliases_table);
my @opt_args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1);
GetOptionsFromArray(
\@opt_args,
'h' => \$show_hostmasks,
'l' => \$show_last_seen,
'n' => \$show_nickserv,
'r' => \$show_relationship,
'g' => \$show_gecos,
'w' => \$show_weak,
'z' => \$dont_use_aliases_table,
'i' => \$show_id
'i' => \$show_id,
'sort|s=s' => \$sort_method,
);
return "/say $getopt_error -- $usage" if defined $getopt_error;
return "Too many arguments -- $usage" if @opt_args > 1;
return "Missing argument -- $usage" if @opt_args != 1;
my %sort = (
'id' => sub {
if ($_[1] eq '+') {
return $_[0]->{$a}->{id} <=> $_[0]->{$b}->{id};
} else {
return $_[0]->{$b}->{id} <=> $_[0]->{$a}->{id};
}
},
'seen' => sub {
if ($_[1] eq '+') {
return $_[0]->{$b}->{last_seen} <=> $_[0]->{$a}->{last_seen};
} else {
return $_[0]->{$a}->{last_seen} <=> $_[0]->{$b}->{last_seen};
}
},
'nickserv' => sub {
if ($_[1] eq '+') {
return lc $_[0]->{$a}->{nickserv} cmp lc $_[0]->{$b}->{nickserv};
} else {
return lc $_[0]->{$b}->{nickserv} cmp lc $_[0]->{$a}->{nickserv};
}
},
'nick' => sub {
if ($_[1] eq '+') {
return lc $_[0]->{$a}->{nick} cmp lc $_[0]->{$b}->{nick};
} else {
return lc $_[0]->{$b}->{nick} cmp lc $_[0]->{$a}->{nick};
}
},
'user' => sub {
if ($_[1] eq '+') {
return lc $_[0]->{$a}->{user} cmp lc $_[0]->{$b}->{user};
} else {
return lc $_[0]->{$b}->{user} cmp lc $_[0]->{$a}->{user};
}
},
'host' => sub {
if ($_[1] eq '+') {
return lc $_[0]->{$a}->{host} cmp lc $_[0]->{$b}->{host};
} else {
return lc $_[0]->{$b}->{host} cmp lc $_[0]->{$a}->{host};
}
},
'hostmask' => sub {
if ($_[1] eq '+') {
return lc $_[0]->{$a}->{hostmask} cmp lc $_[0]->{$b}->{hostmask};
} else {
return lc $_[0]->{$b}->{hostmask} cmp lc $_[0]->{$a}->{hostmask};
}
},
);
my $sort_direction = '+';
if ($sort_method =~ s/^(\+|\-)//) {
$sort_direction = $1;
}
if (not exists $sort{$sort_method}) {
return "Invalid sort method '$sort_method'; valid methods are: " . join(', ', sort keys %sort) . "; prefix with - to invert sort direction.";
}
my %akas = $self->{database}->get_also_known_as($opt_args[0], $dont_use_aliases_table);
if (%akas) {
@ -91,7 +161,7 @@ sub cmd_list_also_known_as {
my %nicks;
my $sep = "";
foreach my $aka (sort keys %akas) {
foreach my $aka (sort { $sort{$sort_method}->(\%akas, $sort_direction) } keys %akas) {
next if $aka =~ /^Guest\d+(?:!.*)?$/;
next if exists $akas{$aka}->{type} and $akas{$aka}->{type} == $self->{database}->{alias_type}->{WEAK} && not $show_weak;
@ -117,6 +187,11 @@ sub cmd_list_also_known_as {
$result .= " [WEAK]" if exists $akas{$aka}->{type} and $akas{$aka}->{type} == $self->{database}->{alias_type}->{WEAK};
if ($show_last_seen) {
my $seen = concise ago (gettimeofday - $akas{$aka}->{last_seen});
$result .= " last seen: $seen";
}
if ($show_hostmasks or $show_nickserv or $show_gecos or $show_id or $show_relationship) { $sep = ",\n"; }
else { $sep = ", "; }
}

View File

@ -1608,7 +1608,7 @@ sub get_also_known_as {
last if not $new_aliases;
}
my $hostmask_sth = $self->{dbh}->prepare('SELECT hostmask, nickchange FROM Hostmasks WHERE id = ?');
my $hostmask_sth = $self->{dbh}->prepare('SELECT hostmask, nickchange, last_seen FROM Hostmasks WHERE id = ?');
my $nickserv_sth = $self->{dbh}->prepare('SELECT nickserv FROM Nickserv WHERE id = ?');
my $gecos_sth = $self->{dbh}->prepare('SELECT gecos FROM Gecos WHERE id = ?');
@ -1619,7 +1619,19 @@ sub get_also_known_as {
$rows = $hostmask_sth->fetchall_arrayref({});
foreach my $row (@$rows) {
$akas{$row->{hostmask}} = {hostmask => $row->{hostmask}, id => $id, alias => $ids{$id}->{id}, type => $ids{$id}->{type}, nickchange => $row->{nickchange}};
my ($nick, $user, $host) = $row->{hostmask} =~ m/^([^!]+)!([^@]+)@(.*)/;
$akas{$row->{hostmask}} = {
id => $id,
alias => $ids{$id}->{id},
nick => $nick,
user => $user,
host => $host,
hostmask => $row->{hostmask},
type => $ids{$id}->{type},
nickchange => $row->{nickchange},
last_seen => $row->{last_seen},
};
$self->{pbot}->{logger}->log("[$id] Adding hostmask $row->{hostmask} -> $ids{$id}->{id} [type $ids{$id}->{type}]\n") if $debug;
}