3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-18 18:09:49 +01:00
pbot/PBot/ChanOpCommands.pm

116 lines
2.9 KiB
Perl
Raw Normal View History

# File: ChanOpCommands.pm
# Author: pragma_
#
# Purpose: Channel operator command subroutines.
package PBot::ChanOpCommands;
use warnings;
use strict;
use vars qw($VERSION);
$VERSION = $PBot::PBot::VERSION;
use Carp ();
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, $length) = split(/\s+/, $arguments);
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.";
}
if(not defined $target) {
return "/msg $nick Usage: ban <mask> [timeout seconds (default: 3600 or 1 hour)]";
}
if(not defined $length) {
$length = 60 * 60; # one hour
}
return "" if $target =~ /\Q$self->{pbot}->botnick\E/i;
$self->{pbot}->chanops->ban_user_timed($target, $from, $length);
2013-07-30 15:12:21 +02:00
return "/msg $nick $target banned in $from for $length seconds";
}
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 "";
}
2010-04-03 21:49:41 +02:00
my ($target, $channel) = split / /, $arguments;
2010-04-03 21:49:41 +02:00
if(not defined $target) {
return "/msg $nick Usage: unban <mask> [channel]";
}
2010-04-03 21:49:41 +02:00
$channel = $from if not defined $channel;
return "/msg $nick Usage for /msg: !unban $target <channel>" if $channel !~ /^#/;
2011-10-31 20:56:08 +01:00
$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 "";
}
if(not $from =~ /^#/) {
$self->{pbot}->logger->log("$nick!$user\@$host attempted to /msg kick\n");
return "/msg $nick Kick must be used in the channel.";
}
if(not $arguments =~ /(.*?) (.*)/) {
$self->{pbot}->logger->log("$nick!$user\@$host: invalid arguments to kick\n");
return "/msg $nick Usage: !kick <nick> <reason>";
}
unshift @{ $self->{pbot}->chanops->{op_commands}->{$from} }, "kick $from $1 $2";
$self->{pbot}->chanops->gain_ops($from);
}
1;