3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-22 11:59:43 +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 Carp ();
use Time::HiRes qw/gettimeofday/;
use Time::Duration;
use Time::ParseDate;
sub new {
if(ref($_[1]) eq 'HASH') {
@ -40,30 +43,46 @@ sub initialize {
sub ban_user {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my ($target, $length) = split(/\s+/, $arguments);
my ($target, $channel, $length) = split(/\s+/, $arguments, 3);
if(not defined $from) {
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
return "";
}
if(not $from =~ /^#/) { #not a channel
return "/msg $nick This command must be used in the channel.";
}
$channel = $from if not defined $channel;
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) {
$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');
return "" if $target =~ /\Q$botnick\E/i;
$self->{pbot}->{chanops}->ban_user_timed($target, $from, $length);
return "/msg $nick $target banned in $from for $length seconds";
$self->{pbot}->{chanops}->ban_user_timed($target, $channel, $length);
$length = duration($length);
return "/msg $nick $target banned in $channel for $length";
}
sub unban_user {
@ -83,7 +102,7 @@ sub unban_user {
$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);
return "/msg $nick $target has been unbanned from $channel.";

View File

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