2010-03-22 08:33:44 +01:00
|
|
|
# File: BotAdmins.pm
|
2010-03-24 07:47:40 +01:00
|
|
|
# Author: pragma_
|
2010-03-22 08:33:44 +01:00
|
|
|
#
|
|
|
|
# Purpose: Manages list of bot admins and whether they are logged in.
|
|
|
|
|
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::BotAdmins;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2010-08-15 10:25:35 +02:00
|
|
|
use PBot::DualIndexHashObject;
|
2014-05-18 02:27:57 +02:00
|
|
|
use PBot::BotAdminCommands;
|
2010-08-15 10:25:35 +02:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
sub new {
|
2019-05-28 18:19:42 +02:00
|
|
|
if (ref($_[1]) eq 'HASH') {
|
2014-05-18 02:27:57 +02:00
|
|
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
my $filename = delete $conf{filename};
|
|
|
|
my $export_path = delete $conf{export_path};
|
|
|
|
my $export_site = delete $conf{export_site};
|
2010-03-22 08:33:44 +01:00
|
|
|
my $export_timeout = delete $conf{export_timeout};
|
2014-05-18 02:27:57 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $export_timeout) {
|
|
|
|
if (defined $export_path) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$export_timeout = 300; # every 5 minutes
|
|
|
|
} else {
|
|
|
|
$export_timeout = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{admins} = PBot::DualIndexHashObject->new(name => 'Admins', filename => $filename, pbot => $self->{pbot});
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{commands} = PBot::BotAdminCommands->new(pbot => $self->{pbot});
|
|
|
|
$self->{export_path} = $export_path;
|
|
|
|
$self->{export_site} = $export_site;
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{export_timeout} = $export_timeout;
|
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->load_admins;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub add_admin {
|
|
|
|
my $self = shift;
|
2014-05-17 22:08:19 +02:00
|
|
|
my ($name, $channel, $hostmask, $level, $password, $dont_save) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$channel = lc $channel;
|
|
|
|
$hostmask = lc $hostmask;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{admins}->hash->{$channel}->{$hostmask}->{name} = $name;
|
|
|
|
$self->{admins}->hash->{$channel}->{$hostmask}->{level} = $level;
|
|
|
|
$self->{admins}->hash->{$channel}->{$hostmask}->{password} = $password;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Adding new level $level admin: [$name] [$hostmask] for channel [$channel]\n");
|
2010-08-15 10:25:35 +02:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->save_admins unless $dont_save;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub remove_admin {
|
|
|
|
my $self = shift;
|
|
|
|
my ($channel, $hostmask) = @_;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
my $admin = delete $self->{admins}->hash->{$channel}->{$hostmask};
|
2018-01-30 05:44:02 +01:00
|
|
|
|
|
|
|
if (not keys %{$self->{admins}->hash->{$channel}}) {
|
|
|
|
delete $self->{admins}->hash->{$channel};
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $admin) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Removed level $admin->{level} admin [$admin->{name}] [$hostmask] from channel [$channel]\n");
|
2010-03-29 14:30:35 +02:00
|
|
|
$self->save_admins;
|
|
|
|
return 1;
|
|
|
|
} else {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Attempt to remove non-existent admin [$hostmask] from channel [$channel]\n");
|
2010-03-29 14:30:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub load_admins {
|
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $filename = shift; } else { $filename = $self->{admins}->filename; }
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $filename) {
|
2010-03-22 08:33:44 +01:00
|
|
|
Carp::carp "No admins path specified -- skipping loading of admins";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{admins}->load;
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
my $i = 0;
|
2014-05-18 22:09:05 +02:00
|
|
|
foreach my $channel (keys %{ $self->{admins}->hash } ) {
|
|
|
|
foreach my $hostmask (keys %{ $self->{admins}->hash->{$channel} }) {
|
2010-08-15 10:25:35 +02:00
|
|
|
$i++;
|
2014-05-18 22:09:05 +02:00
|
|
|
my $name = $self->{admins}->hash->{$channel}->{$hostmask}->{name};
|
|
|
|
my $level = $self->{admins}->hash->{$channel}->{$hostmask}->{level};
|
|
|
|
my $password = $self->{admins}->hash->{$channel}->{$hostmask}->{password};
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $name or not defined $level or not defined $password) {
|
2019-06-28 09:56:18 +02:00
|
|
|
Carp::croak "Admin #$i of $filename is missing critical data\n";
|
2010-08-15 10:25:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Adding new level $level admin: [$name] [$hostmask] for channel [$channel]\n");
|
2010-08-15 10:25:35 +02:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log(" $i admins loaded.\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub save_admins {
|
|
|
|
my $self = shift;
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{admins}->save;
|
2010-08-15 10:25:35 +02:00
|
|
|
$self->export_admins;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub export_admins {
|
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $filename = shift; } else { $filename = $self->export_path; }
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
return if not defined $filename;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-24 07:47:40 +01:00
|
|
|
sub find_admin {
|
2010-03-29 14:30:35 +02:00
|
|
|
my ($self, $from, $hostmask) = @_;
|
2010-03-24 07:47:40 +01:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$from = $self->{pbot}->{registry}->get_value('irc', 'botnick') if not defined $from;
|
2010-03-29 14:30:35 +02:00
|
|
|
$hostmask = '.*' if not defined $hostmask;
|
2017-11-23 00:25:14 +01:00
|
|
|
$hostmask = lc $hostmask;
|
2010-03-24 07:47:40 +01:00
|
|
|
|
|
|
|
my $result = eval {
|
2014-05-18 22:09:05 +02:00
|
|
|
foreach my $channel_regex (keys %{ $self->{admins}->hash }) {
|
2018-02-22 02:21:38 +01:00
|
|
|
if ($from =~ m/^$channel_regex$/i) {
|
2014-05-18 22:09:05 +02:00
|
|
|
foreach my $hostmask_regex (keys %{ $self->{admins}->hash->{$channel_regex} }) {
|
2017-11-23 00:25:14 +01:00
|
|
|
return $self->{admins}->hash->{$channel_regex}->{$hostmask_regex} if $hostmask =~ m/$hostmask_regex/i or $hostmask eq lc $hostmask_regex;
|
2010-03-24 07:47:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
};
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($@) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Error in find_admin parameters: $@\n");
|
2010-03-24 07:47:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub loggedin {
|
|
|
|
my ($self, $channel, $hostmask) = @_;
|
|
|
|
|
2010-03-24 07:47:40 +01:00
|
|
|
my $admin = $self->find_admin($channel, $hostmask);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $admin && $admin->{loggedin}) {
|
2010-03-24 07:47:40 +01:00
|
|
|
return $admin;
|
2010-03-22 08:33:44 +01:00
|
|
|
} else {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub login {
|
|
|
|
my ($self, $channel, $hostmask, $password) = @_;
|
|
|
|
|
2010-03-24 07:47:40 +01:00
|
|
|
my $admin = $self->find_admin($channel, $hostmask);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $admin) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Attempt to login non-existent [$channel][$hostmask] failed\n");
|
2010-03-29 14:30:35 +02:00
|
|
|
return "You do not have an account in $channel.";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($admin->{password} ne $password) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Bad login password for [$channel][$hostmask]\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
return "I don't think so.";
|
|
|
|
}
|
|
|
|
|
2010-03-24 07:47:40 +01:00
|
|
|
$admin->{loggedin} = 1;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("$hostmask logged into $channel\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2010-03-29 14:30:35 +02:00
|
|
|
return "Logged into $channel.";
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub logout {
|
|
|
|
my ($self, $channel, $hostmask) = @_;
|
|
|
|
|
2010-03-24 07:47:40 +01:00
|
|
|
my $admin = $self->find_admin($channel, $hostmask);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2010-03-24 07:47:40 +01:00
|
|
|
delete $admin->{loggedin} if defined $admin;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2010-03-29 14:30:35 +02:00
|
|
|
sub export_path {
|
|
|
|
my $self = shift;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $self->{export_path} = shift; }
|
2010-03-29 14:30:35 +02:00
|
|
|
return $self->{export_path};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub export_timeout {
|
|
|
|
my $self = shift;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $self->{export_timeout} = shift; }
|
2010-03-29 14:30:35 +02:00
|
|
|
return $self->{export_timeout};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub export_site {
|
|
|
|
my $self = shift;
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $self->{export_site} = shift; }
|
2010-03-29 14:30:35 +02:00
|
|
|
return $self->{export_site};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub admins {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{admins};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub filename {
|
|
|
|
my $self = shift;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $self->{filename} = shift; }
|
2010-03-29 14:30:35 +02:00
|
|
|
return $self->{filename};
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
1;
|