3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-14 17:59:25 +02:00
pbot/PBot/ChanOpCommands.pm
Pragmatic Software 111717c798 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.
2015-04-11 16:00:20 -07:00

143 lines
3.8 KiB
Perl

# File: ChanOpCommands.pm
# Author: pragma_
#
# Purpose: Channel operator command subroutines.
package PBot::ChanOpCommands;
use warnings;
use strict;
use Carp ();
use Time::HiRes qw/gettimeofday/;
use Time::Duration;
use Time::ParseDate;
sub new {
if(ref($_[1]) eq 'HASH') {
Carp::croak("Options to ChanOpCommands should be key/value pairs, not hash reference");
}
my ($class, %conf) = @_;
my $self = bless {}, $class;
$self->initialize(%conf);
return $self;
}
sub initialize {
my ($self, %conf) = @_;
my $pbot = delete $conf{pbot};
if(not defined $pbot) {
Carp::croak("Missing pbot reference to ChanOpCommands");
}
$self->{pbot} = $pbot;
$pbot->{commands}->register(sub { return $self->ban_user(@_) }, "ban", 10);
$pbot->{commands}->register(sub { return $self->unban_user(@_) }, "unban", 10);
$pbot->{commands}->register(sub { return $self->kick_user(@_) }, "kick", 10);
}
sub ban_user {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my ($target, $channel, $length) = split(/\s+/, $arguments, 3);
if(not defined $from) {
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
return "";
}
$channel = $from if not defined $channel;
if(not defined $target) {
return "/msg $nick Usage: ban <mask> [channel [timeout (default: 24 hours)]]";
}
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 $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
return "" if $target =~ /\Q$botnick\E/i;
$self->{pbot}->{chanops}->ban_user_timed($target, $channel, $length);
$length = duration($length);
return "/msg $nick $target banned in $channel for $length";
}
sub unban_user {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
if(not defined $from) {
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
return "";
}
my ($target, $channel) = split / /, $arguments;
if(not defined $target) {
return "/msg $nick Usage: unban <mask> [channel]";
}
$channel = $from if not defined $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.";
}
sub kick_user {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
if(not defined $from) {
$self->{pbot}->{logger}->log("Command missing ~from parameter!\n");
return "";
}
# used in private message
if(not $from =~ /^#/) {
if(not $arguments =~ /(^#\S+) (\S+) (.*)/) {
$self->{pbot}->{logger}->log("$nick!$user\@$host: invalid arguments to kick\n");
return "/msg $nick Usage from private message: kick <channel> <nick> <reason>";
}
$self->{pbot}->{chanops}->add_op_command($1, "kick $1 $2 $3");
$self->{pbot}->{chanops}->gain_ops($1);
return "/msg $nick Kicking $2 from $1 with reason '$3'";
}
# used in channel
if(not $arguments =~ /(.*?) (.*)/) {
$self->{pbot}->{logger}->log("$nick!$user\@$host: invalid arguments to kick\n");
return "/msg $nick Usage: kick <nick> <reason>";
}
$self->{pbot}->{chanops}->add_op_command($from, "kick $from $1 $2");
$self->{pbot}->{chanops}->gain_ops($from);
return "/msg $nick Kicking $1 from $from with reason '$2'";
}
1;