From 8b66bd82ec89592082720dbd57d70d6f1eeb1885 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 14 Dec 2015 16:13:43 -0800 Subject: [PATCH] Improve factlog command Now, by default, uses concise timestamp relative durations; e.g. "2d5h ago" instead of "2 days and 5 hours ago". Now, by default, shows only nick instead of full hostmask for each entry. Now accepts optional arguments -t and -h to control the above behavior. If -t is specified, then it shows a full timedate instead of a relative duration; e.g., "Sun Dec 13 14:26:56 PST 2015" instead of "2d5h ago". If -h is specified, then it shows the full hostmask for each entry instead of just the nick. --- PBot/FactoidCommands.pm | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/PBot/FactoidCommands.pm b/PBot/FactoidCommands.pm index 86393fe4..ac715867 100644 --- a/PBot/FactoidCommands.pm +++ b/PBot/FactoidCommands.pm @@ -11,6 +11,8 @@ use strict; use Carp (); use Time::Duration; use Time::HiRes qw(gettimeofday); +use Getopt::Long qw(GetOptionsFromString); +use POSIX qw(strftime); sub new { if(ref($_[1]) eq 'HASH') { @@ -702,10 +704,29 @@ sub factlog { my $self = shift; my ($from, $nick, $user, $host, $arguments) = @_; - my ($chan, $trig) = split / /, $arguments; + my $usage = "Usage: factlog [-h] [-t] [channel] ; -h show full hostmask; -t show actual timestamp instead of relative"; + + return $usage if not $arguments; + + my $getopt_error; + local $SIG{__WARN__} = sub { + $getopt_error = shift; + chomp $getopt_error; + }; + + my ($show_hostmask, $actual_timestamp); + my ($ret, $args) = GetOptionsFromString($arguments, + 'h' => \$show_hostmask, + 't' => \$actual_timestamp); + + return "$getopt_error -- $usage" if defined $getopt_error; + return "Too many arguments -- $usage" if @$args > 2; + return "Missing argument -- $usage" if not @$args; + + my ($chan, $trig) = (@$args[0], @$args[1]); if(not defined $chan and not defined $trig) { - return "Usage: factlog [channel] "; + return $usage; } my $needs_disambig; @@ -753,7 +774,18 @@ sub factlog { while (my $line = <$fh>) { my ($timestamp, $hostmask, $msg) = split / /, $line, 3; - $result .= "[" . ago(gettimeofday - $timestamp) . "] $hostmask $msg\n"; + + if (not $show_hostmask) { + $hostmask =~ s/!.*$//; + } + + if ($actual_timestamp) { + $timestamp = strftime "%a %b %e %H:%M:%S %Z %Y", localtime $timestamp; + } else { + $timestamp = concise ago gettimeofday - $timestamp; + } + + $result .= "[$timestamp] $hostmask $msg\n"; } close $fh;