3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-25 21:39:27 +01:00

Improve ban command to understand English time durations

You can now use sentences like "1 hour and 30 minutes" to ban for 5400 seconds.
Also, can now ban in channels from a /msg.
This commit is contained in:
Pragmatic Software 2015-04-11 16:00:20 -07:00
parent a6bafb12a4
commit 111717c798
2 changed files with 30 additions and 10 deletions

View File

@ -9,6 +9,9 @@ use warnings;
use strict; use strict;
use Carp (); use Carp ();
use Time::HiRes qw/gettimeofday/;
use Time::Duration;
use Time::ParseDate;
sub new { sub new {
if(ref($_[1]) eq 'HASH') { if(ref($_[1]) eq 'HASH') {
@ -40,30 +43,46 @@ sub initialize {
sub ban_user { sub ban_user {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my ($target, $length) = split(/\s+/, $arguments); my ($target, $channel, $length) = split(/\s+/, $arguments, 3);
if(not defined $from) { if(not defined $from) {
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n"); $self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
return ""; return "";
} }
if(not $from =~ /^#/) { #not a channel $channel = $from if not defined $channel;
return "/msg $nick This command must be used in the channel.";
}
if(not defined $target) { if(not defined $target) {
return "/msg $nick Usage: ban <mask> [timeout seconds (default: 3600 or 1 hour)]"; return "/msg $nick Usage: ban <mask> [channel [timeout (default: 24 hours)]]";
} }
if(not defined $length) { if(not defined $length) {
$length = 60 * 60; # one hour $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 $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick'); my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
return "" if $target =~ /\Q$botnick\E/i; return "" if $target =~ /\Q$botnick\E/i;
$self->{pbot}->{chanops}->ban_user_timed($target, $from, $length); $self->{pbot}->{chanops}->ban_user_timed($target, $channel, $length);
return "/msg $nick $target banned in $from for $length seconds"; $length = duration($length);
return "/msg $nick $target banned in $channel for $length";
} }
sub unban_user { sub unban_user {
@ -83,7 +102,7 @@ sub unban_user {
$channel = $from if not defined $channel; $channel = $from if not defined $channel;
return "/msg $nick Usage for /msg: !unban $target <channel>" if $channel !~ /^#/; return "/msg $nick Usage for /msg: unban $target <channel>" if $channel !~ /^#/;
$self->{pbot}->{chanops}->unban_user($target, $channel); $self->{pbot}->{chanops}->unban_user($target, $channel);
return "/msg $nick $target has been unbanned from $channel."; return "/msg $nick $target has been unbanned from $channel.";

View File

@ -20,6 +20,7 @@ Text::Balanced
Text::Levenshtein Text::Levenshtein
Text::WordDiff Text::WordDiff
Time::Duration Time::Duration
Time::ParseDate
re::engine::RE2 re::engine::RE2
URI::Escape URI::Escape
WWW::Wikipedia WWW::Wikipedia