2010-03-22 08:33:44 +01:00
|
|
|
# File: BotAdminCommands.pm
|
2010-03-24 07:47:40 +01:00
|
|
|
# Author: pragma_
|
2010-03-22 08:33:44 +01:00
|
|
|
#
|
|
|
|
# Purpose: Administrative command subroutines.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
package PBot::BotAdminCommands;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2017-08-06 06:38:46 +02:00
|
|
|
use feature 'switch';
|
|
|
|
no if $] >= 5.018, warnings => "experimental::smartmatch";
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
|
|
|
Carp::croak("Options to BotAdminCommands 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 BotAdminCommands");
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{pbot} = $pbot;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$pbot->{commands}->register(sub { return $self->login(@_) }, "login", 0);
|
|
|
|
$pbot->{commands}->register(sub { return $self->logout(@_) }, "logout", 0);
|
2015-07-13 11:47:30 +02:00
|
|
|
$pbot->{commands}->register(sub { return $self->join_channel(@_) }, "join", 40);
|
|
|
|
$pbot->{commands}->register(sub { return $self->part_channel(@_) }, "part", 40);
|
|
|
|
$pbot->{commands}->register(sub { return $self->ack_die(@_) }, "die", 90);
|
2014-05-18 22:09:05 +02:00
|
|
|
$pbot->{commands}->register(sub { return $self->adminadd(@_) }, "adminadd", 60);
|
|
|
|
$pbot->{commands}->register(sub { return $self->adminrem(@_) }, "adminrem", 60);
|
|
|
|
$pbot->{commands}->register(sub { return $self->adminset(@_) }, "adminset", 60);
|
|
|
|
$pbot->{commands}->register(sub { return $self->adminunset(@_) }, "adminunset", 60);
|
2015-07-13 11:47:30 +02:00
|
|
|
$pbot->{commands}->register(sub { return $self->sl(@_) }, "sl", 90);
|
|
|
|
$pbot->{commands}->register(sub { return $self->export(@_) }, "export", 90);
|
2017-08-06 06:38:46 +02:00
|
|
|
$pbot->{commands}->register(sub { return $self->reload(@_) }, "reload", 90);
|
2017-08-18 09:53:12 +02:00
|
|
|
$pbot->{commands}->register(sub { return $self->evalcmd(@_) }, "eval", 99);
|
2011-02-10 09:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub sl {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{conn}->sl($arguments);
|
2017-08-03 23:13:29 +02:00
|
|
|
return "";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub login {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
if($self->{pbot}->{admins}->loggedin($from, "$nick!$user\@$host")) {
|
2010-03-29 14:30:35 +02:00
|
|
|
return "/msg $nick You are already logged into channel $from.";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
my $result = $self->{pbot}->{admins}->login($from, "$nick!$user\@$host", $arguments);
|
2010-03-22 08:33:44 +01:00
|
|
|
return "/msg $nick $result";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub logout {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
2014-05-18 22:09:05 +02:00
|
|
|
return "/msg $nick Uh, you aren't logged into channel $from." if(not $self->{pbot}->{admins}->loggedin($from, "$nick!$user\@$host"));
|
|
|
|
$self->{pbot}->{admins}->logout($from, "$nick!$user\@$host");
|
2010-03-22 08:33:44 +01:00
|
|
|
return "/msg $nick Good-bye, $nick.";
|
|
|
|
}
|
|
|
|
|
2010-08-15 10:25:35 +02:00
|
|
|
sub adminadd {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
2010-03-29 14:30:35 +02:00
|
|
|
|
|
|
|
my ($name, $channel, $hostmask, $level, $password) = split / /, $arguments, 5;
|
|
|
|
|
|
|
|
if(not defined $name or not defined $channel or not defined $hostmask or not defined $level
|
|
|
|
or not defined $password) {
|
2015-07-10 08:25:31 +02:00
|
|
|
return "/msg $nick Usage: adminadd <name> <channel> <hostmask> <level> <password>";
|
2010-03-29 14:30:35 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 08:25:31 +02:00
|
|
|
$channel = '.*' if lc $channel eq 'global';
|
|
|
|
|
2010-03-29 14:30:35 +02:00
|
|
|
$self->{pbot}->{admins}->add_admin($name, $channel, $hostmask, $level, $password);
|
|
|
|
return "Admin added.";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2010-08-15 10:25:35 +02:00
|
|
|
sub adminrem {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
2010-03-29 14:30:35 +02:00
|
|
|
|
|
|
|
my ($channel, $hostmask) = split / /, $arguments, 2;
|
|
|
|
|
|
|
|
if(not defined $channel or not defined $hostmask) {
|
2015-07-10 08:25:31 +02:00
|
|
|
return "/msg $nick Usage: adminrem <channel> <hostmask/name>";
|
|
|
|
}
|
|
|
|
|
|
|
|
$channel = lc $channel;
|
|
|
|
$hostmask = lc $hostmask;
|
|
|
|
|
|
|
|
$channel = '.*' if $channel eq 'global';
|
|
|
|
|
|
|
|
if (exists $self->{pbot}->{admins}->{admins}->hash->{$channel}) {
|
|
|
|
if (not exists $self->{pbot}->{admins}->{admins}->hash->{$channel}->{$hostmask}) {
|
2017-04-11 04:55:52 +02:00
|
|
|
foreach my $mask (keys %{ $self->{pbot}->{admins}->{admins}->hash->{$channel} }) {
|
2015-07-10 08:25:31 +02:00
|
|
|
if ($self->{pbot}->{admins}->{admins}->hash->{$channel}->{$mask}->{name} eq $hostmask) {
|
|
|
|
$hostmask = $mask;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-29 14:30:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($self->{pbot}->{admins}->remove_admin($channel, $hostmask)) {
|
|
|
|
return "Admin removed.";
|
|
|
|
} else {
|
|
|
|
return "No such admin found.";
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2010-08-15 10:25:35 +02:00
|
|
|
sub adminset {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
my ($channel, $hostmask, $key, $value) = split / /, $arguments, 4 if defined $arguments;
|
|
|
|
|
|
|
|
if(not defined $channel or not defined $hostmask) {
|
2015-07-13 11:47:30 +02:00
|
|
|
return "Usage: adminset <channel> <hostmask/name> [key] [value]";
|
2015-07-10 08:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$channel = lc $channel;
|
|
|
|
$hostmask = lc $hostmask;
|
|
|
|
|
|
|
|
$channel = '.*' if $channel eq 'global';
|
|
|
|
|
|
|
|
if (exists $self->{pbot}->{admins}->{admins}->hash->{$channel}) {
|
|
|
|
if (not exists $self->{pbot}->{admins}->{admins}->hash->{$channel}->{$hostmask}) {
|
2017-04-11 04:55:52 +02:00
|
|
|
foreach my $mask (keys %{ $self->{pbot}->{admins}->{admins}->hash->{$channel} }) {
|
2015-07-10 08:25:31 +02:00
|
|
|
if ($self->{pbot}->{admins}->{admins}->hash->{$channel}->{$mask}->{name} eq $hostmask) {
|
|
|
|
$hostmask = $mask;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-15 10:25:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
return $self->{pbot}->{admins}->{admins}->set($channel, $hostmask, $key, $value);
|
2010-08-15 10:25:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub adminunset {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
my ($channel, $hostmask, $key) = split / /, $arguments, 3 if defined $arguments;
|
|
|
|
|
|
|
|
if(not defined $channel or not defined $hostmask) {
|
2015-07-10 08:25:31 +02:00
|
|
|
return "Usage: adminunset <channel> <hostmask/name> <key>";
|
|
|
|
}
|
|
|
|
|
|
|
|
$channel = lc $channel;
|
|
|
|
$hostmask = lc $hostmask;
|
|
|
|
|
|
|
|
$channel = '.*' if $channel eq 'global';
|
|
|
|
|
|
|
|
if (exists $self->{pbot}->{admins}->{admins}->hash->{$channel}) {
|
|
|
|
if (not exists $self->{pbot}->{admins}->{admins}->hash->{$channel}->{$hostmask}) {
|
2017-04-11 04:55:52 +02:00
|
|
|
foreach my $mask (keys %{ $self->{pbot}->{admins}->{admins}->hash->{$channel} }) {
|
2015-07-10 08:25:31 +02:00
|
|
|
if ($self->{pbot}->{admins}->{admins}->hash->{$channel}->{$mask}->{name} eq $hostmask) {
|
|
|
|
$hostmask = $mask;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-15 10:25:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
return $self->{pbot}->{admins}->{admins}->unset($channel, $hostmask, $key);
|
2010-08-15 10:25:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub join_channel {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
2014-05-13 12:15:52 +02:00
|
|
|
foreach my $channel (split /\s+/, $arguments) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("$nick!$user\@$host made me join $channel\n");
|
2014-12-18 09:46:13 +01:00
|
|
|
$self->{pbot}->{chanops}->join_channel($channel);
|
2014-05-13 12:15:52 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 19:33:18 +02:00
|
|
|
return "/msg $nick Joining $arguments";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub part_channel {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
2014-05-13 12:15:52 +02:00
|
|
|
$arguments = $from if not $arguments;
|
|
|
|
|
|
|
|
foreach my $channel (split /\s+/, $arguments) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("$nick!$user\@$host made me part $channel\n");
|
2014-12-18 09:46:13 +01:00
|
|
|
$self->{pbot}->{chanops}->part_channel($channel);
|
2014-05-13 12:15:52 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 19:33:18 +02:00
|
|
|
return "/msg $nick Parting $arguments";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ack_die {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("$nick!$user\@$host made me exit.\n");
|
2014-05-17 00:11:31 +02:00
|
|
|
$self->{pbot}->atexit();
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{conn}->privmsg($from, "Good-bye.") if defined $from;
|
|
|
|
$self->{pbot}->{conn}->quit("Departure requested.");
|
2010-03-22 08:33:44 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub export {
|
2012-08-24 00:50:07 +02:00
|
|
|
my $self = shift;
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
|
|
|
if(not defined $arguments) {
|
2015-09-08 20:42:43 +02:00
|
|
|
return "/msg $nick Usage: export <modules|factoids|admins>";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($arguments =~ /^modules$/i) {
|
|
|
|
return "/msg $nick Coming soon.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($arguments =~ /^factoids$/i) {
|
2014-05-18 22:09:05 +02:00
|
|
|
return $self->{pbot}->{factoids}->export_factoids;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($arguments =~ /^admins$/i) {
|
|
|
|
return "/msg $nick Coming soon.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:53:12 +02:00
|
|
|
sub evalcmd {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("[$from] $nick!$user\@$host Evaluating [$arguments]\n");
|
|
|
|
|
|
|
|
my $ret;
|
|
|
|
my $result = eval $arguments;
|
|
|
|
if ($@) {
|
|
|
|
if (length $result) {
|
|
|
|
$ret .= "[Error: $@] ";
|
|
|
|
} else {
|
|
|
|
$ret .= "Error: $@";
|
|
|
|
}
|
|
|
|
$ret =~ s/ at \(eval \d+\) line 1.//;
|
|
|
|
}
|
|
|
|
return "$ret $result";
|
|
|
|
}
|
|
|
|
|
2017-08-06 06:38:46 +02:00
|
|
|
sub reload {
|
|
|
|
my $self = shift;
|
|
|
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
|
|
|
given ($arguments) {
|
|
|
|
when ("blacklist") {
|
|
|
|
$self->{pbot}->{blacklist}->clear_blacklist;
|
|
|
|
$self->{pbot}->{blacklist}->load_blacklist;
|
|
|
|
return "Blacklist reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("whitelist") {
|
|
|
|
$self->{pbot}->{antiflood}->{whitelist}->clear;
|
|
|
|
$self->{pbot}->{antiflood}->{whitelist}->load;
|
|
|
|
return "Whitelist reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("ignores") {
|
|
|
|
$self->{pbot}->{ignorelist}->clear_ignores;
|
|
|
|
$self->{pbot}->{ignorelist}->load_ignores;
|
|
|
|
return "Ignore list reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("admins") {
|
|
|
|
$self->{pbot}->{admins}->{admins}->clear;
|
|
|
|
$self->{pbot}->{admins}->load_admins;
|
|
|
|
return "Admins reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("channels") {
|
|
|
|
$self->{pbot}->{channels}->{channels}->clear;
|
|
|
|
$self->{pbot}->{channels}->load_channels;
|
|
|
|
return "Channels reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("bantimeouts") {
|
|
|
|
$self->{pbot}->{chanops}->{unban_timeout}->clear;
|
|
|
|
$self->{pbot}->{chanops}->{unban_timeout}->load;
|
|
|
|
return "Ban timeouts reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("mutetimeouts") {
|
|
|
|
$self->{pbot}->{chanops}->{unmute_timeout}->clear;
|
|
|
|
$self->{pbot}->{chanops}->{unmute_timeout}->load;
|
|
|
|
return "Mute timeouts reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("registry") {
|
|
|
|
$self->{pbot}->{registry}->{registry}->clear;
|
|
|
|
$self->{pbot}->{registry}->load;
|
|
|
|
return "Registry reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ("factoids") {
|
|
|
|
$self->{pbot}->{factoids}->{factoids}->clear;
|
|
|
|
$self->{pbot}->{factoids}->load_factoids;
|
|
|
|
return "Factoids reloaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
default {
|
|
|
|
return "Usage: reload <blacklist|whitelist|ignores|admins|channels|bantimeouts|mutetimeouts|registry|factoids>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
1;
|