3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 03:02:47 +01:00

Updated BotAdmins to use DualIndexHashObject

This commit is contained in:
Pragmatic Software 2010-08-15 08:25:35 +00:00
parent 0f47100dc9
commit 7eea052b36
5 changed files with 88 additions and 60 deletions

View File

@ -40,8 +40,10 @@ sub initialize {
$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->ack_die(@_) }, "die", 50);
$pbot->commands->register(sub { return $self->add_admin(@_) }, "addadmin", 60);
$pbot->commands->register(sub { return $self->del_admin(@_) }, "deladmin", 60);
$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);
}
sub login {
@ -64,7 +66,7 @@ sub logout {
return "/msg $nick Good-bye, $nick.";
}
sub add_admin {
sub adminadd {
my $self = shift;
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
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}->save_admins;
return "Admin added.";
}
sub del_admin {
sub adminrem {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my ($channel, $hostmask) = split / /, $arguments, 2;
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)) {
@ -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 {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;

View File

@ -11,6 +11,8 @@ use strict;
use vars qw($VERSION);
$VERSION = $PBot::PBot::VERSION;
use PBot::DualIndexHashObject;
use Carp ();
sub new {
@ -46,8 +48,7 @@ sub initialize {
}
}
$self->{admins} = {};
$self->{filename} = $filename;
$self->{admins} = PBot::DualIndexHashObject->new(name => 'Admins', filename => $filename);
$self->{export_path} = $export_path;
$self->{export_site} = $export_site;
$self->{export_timeout} = $export_timeout;
@ -62,18 +63,20 @@ sub add_admin {
$channel = lc $channel;
$hostmask = lc $hostmask;
${ $self->admins }{$channel}{$hostmask}{name} = $name;
${ $self->admins }{$channel}{$hostmask}{level} = $level;
${ $self->admins }{$channel}{$hostmask}{password} = $password;
$self->admins->hash->{$channel}->{$hostmask}->{name} = $name;
$self->admins->hash->{$channel}->{$hostmask}->{level} = $level;
$self->admins->hash->{$channel}->{$hostmask}->{password} = $password;
$self->{pbot}->logger->log("Adding new level $level admin: [$name] [$hostmask] for channel [$channel]\n");
$self->save_admins;
}
sub remove_admin {
my $self = shift;
my ($channel, $hostmask) = @_;
my $admin = delete ${ $self->admins }{$channel}{$hostmask};
my $admin = delete $self->admins->hash->{$channel}->{$hostmask};
if(defined $admin) {
$self->{pbot}->logger->log("Removed level $admin->{level} admin [$admin->{name}] [$hostmask] from channel [$channel]\n");
$self->save_admins;
@ -88,7 +91,7 @@ sub load_admins {
my $self = shift;
my $filename;
if(@_) { $filename = shift; } else { $filename = $self->filename; }
if(@_) { $filename = shift; } else { $filename = $self->admins->filename; }
if(not defined $filename) {
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");
open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n";
my @contents = <FILE>;
close(FILE);
$self->admins->load;
my $i = 0;
foreach my $line (@contents) {
chomp $line;
foreach my $channel (keys %{ $self->admins->hash } ) {
foreach my $hostmask (keys %{ $self->admins->hash->{$channel} }) {
$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";
}
$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");
@ -122,25 +126,9 @@ sub load_admins {
sub save_admins {
my $self = shift;
my $filename;
if(@_) { $filename = shift; } else { $filename = $self->filename; }
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);
$self->admins->save;
$self->export_admins;
}
sub export_admins {
@ -160,10 +148,10 @@ sub find_admin {
$hostmask = '.*' if not defined $hostmask;
my $result = eval {
foreach my $channel_regex (keys %{ $self->{admins} }) {
foreach my $channel_regex (keys %{ $self->admins->hash }) {
if($from !~ m/^#/) {
# 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;
if($hostmask_regex =~ m/^([^!]+)!.*/) {
@ -172,11 +160,11 @@ sub find_admin {
$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) {
foreach my $hostmask_regex (keys %{ $self->{admins}->{$channel_regex} }) {
return $self->{admins}{$channel_regex}{$hostmask_regex} if $hostmask =~ m/$hostmask_regex/i;
foreach my $hostmask_regex (keys %{ $self->admins->hash->{$channel_regex} }) {
return $self->admins->hash->{$channel_regex}->{$hostmask_regex} if $hostmask =~ m/$hostmask_regex/i;
}
}
}

View File

@ -184,17 +184,16 @@ sub list {
$text = "Admins: ";
my $last_channel = "";
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) {
print "texzt: [$text], sep: [$sep]\n";
$text .= $sep . "Channel " . ($channel eq ".*" ? "all" : $channel) . ": ";
$last_channel = $channel;
$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 .= "*" if exists ${ $self->{pbot}->admins->admins }{$channel}{$hostmask}{loggedin};
$text .= ${ $self->{pbot}->admins->admins }{$channel}{$hostmask}{name} . " (" . ${ $self->{pbot}->admins->admins }{$channel}{$hostmask}{level} . ")";
$text .= "*" if exists $self->{pbot}->admins->admins->hash->{$channel}->{$hostmask}->{loggedin};
$text .= $self->{pbot}->admins->admins->hash->{$channel}->{$hostmask}->{name} . " (" . $self->{pbot}->admins->admins->hash->{$channel}->{$hostmask}->{level} . ")";
$sep = "; ";
}
}

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 233,
BUILD_DATE => "2010-08-14",
BUILD_REVISION => 234,
BUILD_DATE => "2010-08-15",
};
1;

View File

@ -1,2 +1,17 @@
pragma .* .*!.*@unaffiliated/pragma/.* 60 *
somename .* .*!example@xyzcorp.com 60 5ecret5@uce
[#channel]
<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