3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-24 11:42:35 +01:00

Move English-to-seconds time duration parser to Utils::ParseDate

Update ban_user_timed() and ignore_user() to use Utils::ParseDate
This commit is contained in:
Pragmatic Software 2015-04-13 15:43:19 -07:00
parent 9ff76ad500
commit a9df8351d3
3 changed files with 48 additions and 19 deletions

View File

@ -9,9 +9,9 @@ use warnings;
use strict; use strict;
use Carp (); use Carp ();
use Time::HiRes qw/gettimeofday/;
use Time::Duration; use Time::Duration;
use Time::ParseDate;
use PBot::Utils::ParseDate;
sub new { sub new {
if(ref($_[1]) eq 'HASH') { if(ref($_[1]) eq 'HASH') {
@ -59,22 +59,9 @@ sub ban_user {
if(not defined $length) { if(not defined $length) {
$length = 60 * 60 * 24; # 24 hours $length = 60 * 60 * 24; # 24 hours
} else { } else {
my $now = gettimeofday; my $error;
my @inputs = split /(?:,?\s+and\s+|\s*,\s*)/, $length; ($length, $error) = parsedate($length);
return $error if defined $error;
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 $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick'); my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');

View File

@ -12,6 +12,8 @@ use Time::HiRes qw(gettimeofday);
use Time::Duration; use Time::Duration;
use Carp (); use Carp ();
use PBot::Utils::ParseDate;
sub new { sub new {
if(ref($_[1]) eq 'HASH') { if(ref($_[1]) eq 'HASH') {
Carp::croak("Options to IgnoreListCommands should be key/value pairs, not hash reference"); 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; 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) { if(not defined $target) {
return "/msg $nick Usage: ignore host [channel] [timeout]"; return "/msg $nick Usage: ignore host [channel] [timeout]";
@ -69,6 +71,10 @@ sub ignore_user {
if(not defined $length) { if(not defined $length) {
$length = -1; # permanently $length = -1; # permanently
} else {
my $error;
($length, $error) = parsedate($length);
return $error if defined $error;
} }
$self->{pbot}->{ignorelist}->add($target, $channel, $length); $self->{pbot}->{ignorelist}->add($target, $channel, $length);

36
PBot/Utils/ParseDate.pm Normal file
View File

@ -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;