3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-26 04:32:35 +01:00

NickList: improve output of nicklist command

This commit is contained in:
Pragmatic Software 2020-05-16 23:13:13 -07:00
parent 72eb7f9e24
commit 9898634b3a

View File

@ -21,6 +21,7 @@ use Data::Dumper;
$Data::Dumper::Sortkeys = 1; $Data::Dumper::Sortkeys = 1;
use Time::HiRes qw/gettimeofday/; use Time::HiRes qw/gettimeofday/;
use Time::Duration qw/concise ago/;
sub initialize { sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
@ -45,21 +46,61 @@ sub initialize {
sub cmd_nicklist { sub cmd_nicklist {
my ($self, $context) = @_; my ($self, $context) = @_;
my $nicklist;
return "Usage: nicklist <channel> [nick]" if not length $context->{arguments}; return "Usage: nicklist <channel> [nick]" if not length $context->{arguments};
my $nicklist;
my @args = split / /, $context->{arguments}; my @args = split / /, $context->{arguments};
if (@args == 1) { if (@args == 1) {
if (not exists $self->{nicklist}->{lc $context->{arguments}}) { if (not exists $self->{nicklist}->{lc $context->{arguments}}) {
return "No nicklist for $context->{arguments}."; return "No nicklist for $context->{arguments}.";
} }
$nicklist = Dumper($self->{nicklist}->{lc $context->{arguments}});
} else { my $count = keys %{$self->{nicklist}->{lc $context->{arguments}}};
if (not exists $self->{nicklist}->{lc $args[0]}) { return "No nicklist for $args[0]."; } $nicklist = "$count nick" . ($count == 1 ? '' : 's') . " in $context->{arguments}:\n";
elsif (not exists $self->{nicklist}->{lc $args[0]}->{lc $args[1]}) { return "No such nick $args[1] in channel $args[0]."; }
$nicklist = Dumper($self->{nicklist}->{lc $args[0]}->{lc $args[1]}); foreach my $entry (sort keys %{$self->{nicklist}->{lc $context->{arguments}}}) {
$nicklist .= " $self->{nicklist}->{lc $context->{arguments}}->{$entry}->{hostmask}";
my $sep = ': ';
if ($self->{nicklist}->{lc $context->{arguments}}->{$entry}->{timestamp} > 0) {
my $duration = concise ago (gettimeofday - $self->{nicklist}->{lc $context->{arguments}}->{$entry}->{timestamp});
$nicklist .= "${sep}last spoken $duration";
$sep = ', ';
} }
foreach my $key (sort keys %{$self->{nicklist}->{lc $context->{arguments}}->{$entry}}) {
next if grep { $key eq $_ } qw/nick user host timestamp hostmask/;
$nicklist .= "$sep$key => $self->{nicklist}->{lc $context->{arguments}}->{$entry}->{$key}";
$sep = ', ';
}
$nicklist .= "\n";
}
} else {
if (not exists $self->{nicklist}->{lc $args[0]}) {
return "No nicklist for $args[0].";
} elsif (not exists $self->{nicklist}->{lc $args[0]}->{lc $args[1]}) {
return "No such nick $args[1] in channel $args[0].";
}
$nicklist = "Nicklist information for $self->{nicklist}->{lc $args[0]}->{lc $args[1]}->{hostmask} in $args[0]: ";
my $sep = '';
if ($self->{nicklist}->{lc $args[0]}->{lc $args[1]}->{timestamp} > 0) {
my $duration = concise ago (gettimeofday - $self->{nicklist}->{lc $args[0]}->{lc $args[1]}->{timestamp});
$nicklist .= "last spoken $duration";
$sep = ', ';
}
foreach my $key (sort keys %{$self->{nicklist}->{lc $args[0]}->{lc $args[1]}}) {
next if grep { $key eq $_ } qw/nick user host timestamp hostmask/;
$nicklist .= "$sep$key => $self->{nicklist}->{lc $args[0]}->{lc $args[1]}->{$key}";
$sep = ', ';
}
$nicklist .= 'no details' if $sep eq '';
}
return $nicklist; return $nicklist;
} }