diff --git a/PBot/ChanOpCommands.pm b/PBot/ChanOpCommands.pm index becf40e3..f95271ba 100644 --- a/PBot/ChanOpCommands.pm +++ b/PBot/ChanOpCommands.pm @@ -9,9 +9,9 @@ use warnings; use strict; use Carp (); -use Time::HiRes qw/gettimeofday/; use Time::Duration; -use Time::ParseDate; + +use PBot::Utils::ParseDate; sub new { if(ref($_[1]) eq 'HASH') { @@ -59,22 +59,9 @@ sub ban_user { if(not defined $length) { $length = 60 * 60 * 24; # 24 hours } else { - my $now = gettimeofday; - my @inputs = split /(?:,?\s+and\s+|\s*,\s*)/, $length; - - my $seconds = 0; - foreach my $input (@inputs) { - $input .= ' seconds' if $input =~ m/^\d+$/; - my $parse = parsedate($input, NOW => $now); - - if (not defined $parse) { - return "I don't know what '$input' means.\n"; - } else { - $seconds += $parse - $now; - } - } - - $length = $seconds; + my $error; + ($length, $error) = parsedate($length); + return $error if defined $error; } my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick'); diff --git a/PBot/IgnoreListCommands.pm b/PBot/IgnoreListCommands.pm index deffecfe..5e3d9d56 100644 --- a/PBot/IgnoreListCommands.pm +++ b/PBot/IgnoreListCommands.pm @@ -12,6 +12,8 @@ use Time::HiRes qw(gettimeofday); use Time::Duration; use Carp (); +use PBot::Utils::ParseDate; + sub new { if(ref($_[1]) eq 'HASH') { Carp::croak("Options to IgnoreListCommands should be key/value pairs, not hash reference"); @@ -44,7 +46,7 @@ sub ignore_user { return "/msg $nick Usage: ignore nick!user\@host [channel] [timeout]" if not defined $arguments; - my ($target, $channel, $length) = split /\s+/, $arguments; + my ($target, $channel, $length) = split /\s+/, $arguments, 3; if(not defined $target) { return "/msg $nick Usage: ignore host [channel] [timeout]"; @@ -69,6 +71,10 @@ sub ignore_user { if(not defined $length) { $length = -1; # permanently + } else { + my $error; + ($length, $error) = parsedate($length); + return $error if defined $error; } $self->{pbot}->{ignorelist}->add($target, $channel, $length); diff --git a/PBot/Utils/ParseDate.pm b/PBot/Utils/ParseDate.pm new file mode 100644 index 00000000..2a85736b --- /dev/null +++ b/PBot/Utils/ParseDate.pm @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +package PBot::Utils::ParseDate; + +require Exporter; +our @ISA = qw/Exporter/; +our @EXPORT = qw/parsedate/; + +use Time::HiRes qw/gettimeofday/; + +require Time::ParseDate; + +sub parsedate { + my $input = shift @_; + my $now = gettimeofday; + my @inputs = split /(?:,?\s+and\s+|\s*,\s*)/, $input; + + my $seconds = 0; + foreach my $input (@inputs) { + $input .= ' seconds' if $input =~ m/^\d+$/; + my $parse = Time::ParseDate::parsedate($input, NOW => $now); + + if (not defined $parse) { + return (0, "I don't know what '$input' means.\n"); + } else { + $seconds += $parse - $now; + } + } + + return ($seconds, undef); +} + +1;