mirror of
https://github.com/pragma-/pbot.git
synced 2024-12-23 11:12:42 +01:00
Updated BotAdmins to use DualIndexHashObject
This commit is contained in:
parent
0f47100dc9
commit
7eea052b36
@ -40,8 +40,10 @@ sub initialize {
|
|||||||
$pbot->commands->register(sub { return $self->join_channel(@_) }, "join", 45);
|
$pbot->commands->register(sub { return $self->join_channel(@_) }, "join", 45);
|
||||||
$pbot->commands->register(sub { return $self->part_channel(@_) }, "part", 45);
|
$pbot->commands->register(sub { return $self->part_channel(@_) }, "part", 45);
|
||||||
$pbot->commands->register(sub { return $self->ack_die(@_) }, "die", 50);
|
$pbot->commands->register(sub { return $self->ack_die(@_) }, "die", 50);
|
||||||
$pbot->commands->register(sub { return $self->add_admin(@_) }, "addadmin", 60);
|
$pbot->commands->register(sub { return $self->adminadd(@_) }, "adminadd", 60);
|
||||||
$pbot->commands->register(sub { return $self->del_admin(@_) }, "deladmin", 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub login {
|
sub login {
|
||||||
@ -64,7 +66,7 @@ sub logout {
|
|||||||
return "/msg $nick Good-bye, $nick.";
|
return "/msg $nick Good-bye, $nick.";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub add_admin {
|
sub adminadd {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($from, $nick, $user, $host, $arguments) = @_;
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
||||||
|
|
||||||
@ -72,22 +74,21 @@ sub add_admin {
|
|||||||
|
|
||||||
if(not defined $name or not defined $channel or not defined $hostmask or not defined $level
|
if(not defined $name or not defined $channel or not defined $hostmask or not defined $level
|
||||||
or not defined $password) {
|
or not defined $password) {
|
||||||
return "/msg $nick Usage: addadmin name channel hostmask level password";
|
return "/msg $nick Usage: adminadd name channel hostmask level password";
|
||||||
}
|
}
|
||||||
|
|
||||||
$self->{pbot}->{admins}->add_admin($name, $channel, $hostmask, $level, $password);
|
$self->{pbot}->{admins}->add_admin($name, $channel, $hostmask, $level, $password);
|
||||||
$self->{pbot}->{admins}->save_admins;
|
|
||||||
return "Admin added.";
|
return "Admin added.";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub del_admin {
|
sub adminrem {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($from, $nick, $user, $host, $arguments) = @_;
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
||||||
|
|
||||||
my ($channel, $hostmask) = split / /, $arguments, 2;
|
my ($channel, $hostmask) = split / /, $arguments, 2;
|
||||||
|
|
||||||
if(not defined $channel or not defined $hostmask) {
|
if(not defined $channel or not defined $hostmask) {
|
||||||
return "/msg $nick Usage: deladmin channel hostmask";
|
return "/msg $nick Usage: adminrem channel hostmask";
|
||||||
}
|
}
|
||||||
|
|
||||||
if($self->{pbot}->{admins}->remove_admin($channel, $hostmask)) {
|
if($self->{pbot}->{admins}->remove_admin($channel, $hostmask)) {
|
||||||
@ -97,6 +98,31 @@ sub del_admin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
return "Usage: adminset <channel> <hostmask> <key> <value>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $self->{pbot}->admins->admins->set($channel, $hostmask, $key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
return "Usage: adminunset <channel> <hostmask> <key>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $self->{pbot}->admins->admins->unset($channel, $hostmask, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
sub join_channel {
|
sub join_channel {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($from, $nick, $user, $host, $arguments) = @_;
|
my ($from, $nick, $user, $host, $arguments) = @_;
|
||||||
|
@ -11,6 +11,8 @@ use strict;
|
|||||||
use vars qw($VERSION);
|
use vars qw($VERSION);
|
||||||
$VERSION = $PBot::PBot::VERSION;
|
$VERSION = $PBot::PBot::VERSION;
|
||||||
|
|
||||||
|
use PBot::DualIndexHashObject;
|
||||||
|
|
||||||
use Carp ();
|
use Carp ();
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
@ -46,8 +48,7 @@ sub initialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$self->{admins} = {};
|
$self->{admins} = PBot::DualIndexHashObject->new(name => 'Admins', filename => $filename);
|
||||||
$self->{filename} = $filename;
|
|
||||||
$self->{export_path} = $export_path;
|
$self->{export_path} = $export_path;
|
||||||
$self->{export_site} = $export_site;
|
$self->{export_site} = $export_site;
|
||||||
$self->{export_timeout} = $export_timeout;
|
$self->{export_timeout} = $export_timeout;
|
||||||
@ -62,18 +63,20 @@ sub add_admin {
|
|||||||
$channel = lc $channel;
|
$channel = lc $channel;
|
||||||
$hostmask = lc $hostmask;
|
$hostmask = lc $hostmask;
|
||||||
|
|
||||||
${ $self->admins }{$channel}{$hostmask}{name} = $name;
|
$self->admins->hash->{$channel}->{$hostmask}->{name} = $name;
|
||||||
${ $self->admins }{$channel}{$hostmask}{level} = $level;
|
$self->admins->hash->{$channel}->{$hostmask}->{level} = $level;
|
||||||
${ $self->admins }{$channel}{$hostmask}{password} = $password;
|
$self->admins->hash->{$channel}->{$hostmask}->{password} = $password;
|
||||||
|
|
||||||
$self->{pbot}->logger->log("Adding new level $level admin: [$name] [$hostmask] for channel [$channel]\n");
|
$self->{pbot}->logger->log("Adding new level $level admin: [$name] [$hostmask] for channel [$channel]\n");
|
||||||
|
|
||||||
|
$self->save_admins;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub remove_admin {
|
sub remove_admin {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($channel, $hostmask) = @_;
|
my ($channel, $hostmask) = @_;
|
||||||
|
|
||||||
my $admin = delete ${ $self->admins }{$channel}{$hostmask};
|
my $admin = delete $self->admins->hash->{$channel}->{$hostmask};
|
||||||
if(defined $admin) {
|
if(defined $admin) {
|
||||||
$self->{pbot}->logger->log("Removed level $admin->{level} admin [$admin->{name}] [$hostmask] from channel [$channel]\n");
|
$self->{pbot}->logger->log("Removed level $admin->{level} admin [$admin->{name}] [$hostmask] from channel [$channel]\n");
|
||||||
$self->save_admins;
|
$self->save_admins;
|
||||||
@ -88,7 +91,7 @@ sub load_admins {
|
|||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $filename;
|
my $filename;
|
||||||
|
|
||||||
if(@_) { $filename = shift; } else { $filename = $self->filename; }
|
if(@_) { $filename = shift; } else { $filename = $self->admins->filename; }
|
||||||
|
|
||||||
if(not defined $filename) {
|
if(not defined $filename) {
|
||||||
Carp::carp "No admins path specified -- skipping loading of admins";
|
Carp::carp "No admins path specified -- skipping loading of admins";
|
||||||
@ -97,23 +100,24 @@ sub load_admins {
|
|||||||
|
|
||||||
$self->{pbot}->logger->log("Loading admins from $filename ...\n");
|
$self->{pbot}->logger->log("Loading admins from $filename ...\n");
|
||||||
|
|
||||||
open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n";
|
$self->admins->load;
|
||||||
my @contents = <FILE>;
|
|
||||||
close(FILE);
|
|
||||||
|
|
||||||
my $i = 0;
|
my $i = 0;
|
||||||
|
|
||||||
foreach my $line (@contents) {
|
foreach my $channel (keys %{ $self->admins->hash } ) {
|
||||||
chomp $line;
|
foreach my $hostmask (keys %{ $self->admins->hash->{$channel} }) {
|
||||||
$i++;
|
$i++;
|
||||||
|
|
||||||
my ($name, $channel, $hostmask, $level, $password) = split(/\s+/, $line, 5);
|
my $name = $self->admins->hash->{$channel}->{$hostmask}->{name};
|
||||||
|
my $level = $self->admins->hash->{$channel}->{$hostmask}->{level};
|
||||||
|
my $password = $self->admins->hash->{$channel}->{$hostmask}->{password};
|
||||||
|
|
||||||
if(not defined $name || not defined $channel || not defined $hostmask || not defined $level || not defined $password) {
|
if(not defined $name or not defined $level or not defined $password) {
|
||||||
Carp::croak "Syntax error around line $i of $filename\n";
|
Carp::croak "Syntax error around line $i of $filename\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$self->add_admin($name, $channel, $hostmask, $level, $password);
|
$self->{pbot}->logger->log("Adding new level $level admin: [$name] [$hostmask] for channel [$channel]\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$self->{pbot}->logger->log(" $i admins loaded.\n");
|
$self->{pbot}->logger->log(" $i admins loaded.\n");
|
||||||
@ -122,25 +126,9 @@ sub load_admins {
|
|||||||
|
|
||||||
sub save_admins {
|
sub save_admins {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $filename;
|
|
||||||
|
|
||||||
if(@_) { $filename = shift; } else { $filename = $self->filename; }
|
$self->admins->save;
|
||||||
|
$self->export_admins;
|
||||||
if(not defined $filename) {
|
|
||||||
Carp::carp "No admins path specified -- skipping saving of admins\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
open(FILE, "> $filename") or Carp::croak "Couldn't open $filename: $!\n";
|
|
||||||
|
|
||||||
foreach my $channel (sort keys %{ $self->{admins} }) {
|
|
||||||
foreach my $hostmask (sort keys %{ $self->{admins}->{$channel} }) {
|
|
||||||
my $admin = $self->{admins}->{$channel}{$hostmask};
|
|
||||||
next if $admin->{name} eq $self->{pbot}->botnick;
|
|
||||||
print FILE "$admin->{name} $channel $hostmask $admin->{level} $admin->{password}\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(FILE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub export_admins {
|
sub export_admins {
|
||||||
@ -160,10 +148,10 @@ sub find_admin {
|
|||||||
$hostmask = '.*' if not defined $hostmask;
|
$hostmask = '.*' if not defined $hostmask;
|
||||||
|
|
||||||
my $result = eval {
|
my $result = eval {
|
||||||
foreach my $channel_regex (keys %{ $self->{admins} }) {
|
foreach my $channel_regex (keys %{ $self->admins->hash }) {
|
||||||
if($from !~ m/^#/) {
|
if($from !~ m/^#/) {
|
||||||
# if not from a channel, make sure that nick portion of hostmask matches $from
|
# if not from a channel, make sure that nick portion of hostmask matches $from
|
||||||
foreach my $hostmask_regex (keys %{ $self->{admins}->{$channel_regex} }) {
|
foreach my $hostmask_regex (keys %{ $self->admins->hash->{$channel_regex} }) {
|
||||||
my $nick;
|
my $nick;
|
||||||
|
|
||||||
if($hostmask_regex =~ m/^([^!]+)!.*/) {
|
if($hostmask_regex =~ m/^([^!]+)!.*/) {
|
||||||
@ -172,11 +160,11 @@ sub find_admin {
|
|||||||
$nick = $hostmask_regex;
|
$nick = $hostmask_regex;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $self->{admins}{$channel_regex}{$hostmask_regex} if($from =~ m/$nick/i and $hostmask =~ m/$hostmask_regex/i);
|
return $self->admins->hash->{$channel_regex}->{$hostmask_regex} if($from =~ m/$nick/i and $hostmask =~ m/$hostmask_regex/i);
|
||||||
}
|
}
|
||||||
} elsif($from =~ m/$channel_regex/i) {
|
} elsif($from =~ m/$channel_regex/i) {
|
||||||
foreach my $hostmask_regex (keys %{ $self->{admins}->{$channel_regex} }) {
|
foreach my $hostmask_regex (keys %{ $self->admins->hash->{$channel_regex} }) {
|
||||||
return $self->{admins}{$channel_regex}{$hostmask_regex} if $hostmask =~ m/$hostmask_regex/i;
|
return $self->admins->hash->{$channel_regex}->{$hostmask_regex} if $hostmask =~ m/$hostmask_regex/i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,17 +184,16 @@ sub list {
|
|||||||
$text = "Admins: ";
|
$text = "Admins: ";
|
||||||
my $last_channel = "";
|
my $last_channel = "";
|
||||||
my $sep = "";
|
my $sep = "";
|
||||||
foreach my $channel (sort keys %{ $self->{pbot}->admins->admins }) {
|
foreach my $channel (sort keys %{ $self->{pbot}->admins->admins->hash }) {
|
||||||
if($last_channel ne $channel) {
|
if($last_channel ne $channel) {
|
||||||
print "texzt: [$text], sep: [$sep]\n";
|
|
||||||
$text .= $sep . "Channel " . ($channel eq ".*" ? "all" : $channel) . ": ";
|
$text .= $sep . "Channel " . ($channel eq ".*" ? "all" : $channel) . ": ";
|
||||||
$last_channel = $channel;
|
$last_channel = $channel;
|
||||||
$sep = "";
|
$sep = "";
|
||||||
}
|
}
|
||||||
foreach my $hostmask (sort keys %{ $self->{pbot}->admins->admins->{$channel} }) {
|
foreach my $hostmask (sort keys %{ $self->{pbot}->admins->admins->hash->{$channel} }) {
|
||||||
$text .= $sep;
|
$text .= $sep;
|
||||||
$text .= "*" if exists ${ $self->{pbot}->admins->admins }{$channel}{$hostmask}{loggedin};
|
$text .= "*" if exists $self->{pbot}->admins->admins->hash->{$channel}->{$hostmask}->{loggedin};
|
||||||
$text .= ${ $self->{pbot}->admins->admins }{$channel}{$hostmask}{name} . " (" . ${ $self->{pbot}->admins->admins }{$channel}{$hostmask}{level} . ")";
|
$text .= $self->{pbot}->admins->admins->hash->{$channel}->{$hostmask}->{name} . " (" . $self->{pbot}->admins->admins->hash->{$channel}->{$hostmask}->{level} . ")";
|
||||||
$sep = "; ";
|
$sep = "; ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ use warnings;
|
|||||||
# These are set automatically by the build/commit script
|
# These are set automatically by the build/commit script
|
||||||
use constant {
|
use constant {
|
||||||
BUILD_NAME => "PBot",
|
BUILD_NAME => "PBot",
|
||||||
BUILD_REVISION => 233,
|
BUILD_REVISION => 234,
|
||||||
BUILD_DATE => "2010-08-14",
|
BUILD_DATE => "2010-08-15",
|
||||||
};
|
};
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
@ -1,2 +1,17 @@
|
|||||||
pragma .* .*!.*@unaffiliated/pragma/.* 60 *
|
[#channel]
|
||||||
somename .* .*!example@xyzcorp.com 60 5ecret5@uce
|
<nick!user@host>
|
||||||
|
level: 10
|
||||||
|
name: example
|
||||||
|
password: s3cr3ts@uce
|
||||||
|
|
||||||
|
[.*]
|
||||||
|
<.*!.*@unaffiliated/pragma/.*>
|
||||||
|
level: 60
|
||||||
|
name: pragma
|
||||||
|
password: *
|
||||||
|
|
||||||
|
<pbot3!stdin@localhost>
|
||||||
|
level: 60
|
||||||
|
name: pbot3
|
||||||
|
password: admin
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user