3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 10:29:30 +01:00

Admins management improved. Admins now save and load, and can be added and removed.

This commit is contained in:
Pragmatic Software 2010-03-29 12:30:35 +00:00
parent f5bfe2dfda
commit cd73f1104e
5 changed files with 126 additions and 76 deletions

View File

@ -41,6 +41,7 @@ sub initialize {
$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->add_admin(@_) }, "addadmin", 60);
$pbot->commands->register(sub { return $self->del_admin(@_) }, "deladmin", 60);
} }
sub login { sub login {
@ -48,7 +49,7 @@ sub login {
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
if($self->{pbot}->admins->loggedin($from, "$nick!$user\@$host")) { if($self->{pbot}->admins->loggedin($from, "$nick!$user\@$host")) {
return "/msg $nick You are already logged in."; return "/msg $nick You are already logged into channel $from.";
} }
my $result = $self->{pbot}->admins->login($from, "$nick!$user\@$host", $arguments); my $result = $self->{pbot}->admins->login($from, "$nick!$user\@$host", $arguments);
@ -58,7 +59,7 @@ sub login {
sub logout { sub logout {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
return "/msg $nick Uh, you aren't logged in." if(not $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host")); 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"); $self->{pbot}->admins->logout($from, "$nick!$user\@$host");
return "/msg $nick Good-bye, $nick."; return "/msg $nick Good-bye, $nick.";
} }
@ -66,13 +67,34 @@ sub logout {
sub add_admin { sub add_admin {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
return "/msg $nick Coming soon.";
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) {
return "/msg $nick Usage: addadmin 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 del_admin {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
return "/msg $nick Coming soon.";
my ($channel, $hostmask) = split / /, $arguments, 2;
if(not defined $channel or not defined $hostmask) {
return "/msg $nick Usage: deladmin channel hostmask";
}
if($self->{pbot}->{admins}->remove_admin($channel, $hostmask)) {
return "Admin removed.";
} else {
return "No such admin found.";
}
} }
sub join_channel { sub join_channel {

View File

@ -57,22 +57,31 @@ sub initialize {
sub add_admin { sub add_admin {
my $self = shift; my $self = shift;
my ($channel, $hostmask, $level, $password) = @_; my ($name, $channel, $hostmask, $level, $password) = @_;
$channel = lc $channel; $channel = lc $channel;
$hostmask = lc $hostmask; $hostmask = lc $hostmask;
${ $self->admins}{$channel}{$hostmask}{level} = $level; ${ $self->admins }{$channel}{$hostmask}{name} = $name;
${ $self->admins}{$channel}{$hostmask}{password} = $password; ${ $self->admins }{$channel}{$hostmask}{level} = $level;
${ $self->admins }{$channel}{$hostmask}{password} = $password;
$self->{pbot}->logger->log("Adding new level $level admin: [$hostmask] for channel [$channel]\n"); $self->{pbot}->logger->log("Adding new level $level admin: [$name] [$hostmask] for channel [$channel]\n");
} }
sub remove_admin { sub remove_admin {
my $self = shift; my $self = shift;
my ($channel, $hostmask) = @_; my ($channel, $hostmask) = @_;
delete ${ $self->admins }{$channel}{$hostmask}; my $admin = delete ${ $self->admins }{$channel}{$hostmask};
if(defined $admin) {
$self->{pbot}->logger->log("Removed level $admin->{level} admin [$admin->{name}] [$hostmask] from channel [$channel]\n");
$self->save_admins;
return 1;
} else {
$self->{pbot}->logger->log("Attempt to remove non-existent admin [$hostmask] from channel [$channel]\n");
return 0;
}
} }
sub load_admins { sub load_admins {
@ -98,13 +107,13 @@ sub load_admins {
chomp $line; chomp $line;
$i++; $i++;
my ($channel, $hostmask, $level, $password) = split(/\s+/, $line, 4); my ($name, $channel, $hostmask, $level, $password) = split(/\s+/, $line, 5);
if(not defined $channel || not defined $hostmask || not defined $level || not defined $password) { if(not defined $name || not defined $channel || not defined $hostmask || not defined $level || 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($channel, $hostmask, $level, $password); $self->add_admin($name, $channel, $hostmask, $level, $password);
} }
$self->{pbot}->logger->log(" $i admins loaded.\n"); $self->{pbot}->logger->log(" $i admins loaded.\n");
@ -121,6 +130,17 @@ sub save_admins {
Carp::carp "No admins path specified -- skipping saving of admins\n"; Carp::carp "No admins path specified -- skipping saving of admins\n";
return; 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 {
@ -133,66 +153,30 @@ sub export_admins {
return; return;
} }
sub interpreter {
my $self = shift;
my ($from, $nick, $user, $host, $count, $keyword, $arguments, $tonick) = @_;
my $result;
my $pbot = $self->{pbot};
return undef;
}
sub export_path {
my $self = shift;
if(@_) { $self->{export_path} = shift; }
return $self->{export_path};
}
sub export_timeout {
my $self = shift;
if(@_) { $self->{export_timeout} = shift; }
return $self->{export_timeout};
}
sub logger {
my $self = shift;
if(@_) { $self->{logger} = shift; }
return $self->{logger};
}
sub export_site {
my $self = shift;
if(@_) { $self->{export_site} = shift; }
return $self->{export_site};
}
sub admins {
my $self = shift;
return $self->{admins};
}
sub filename {
my $self = shift;
if(@_) { $self->{filename} = shift; }
return $self->{filename};
}
sub find_admin { sub find_admin {
my ($self, $channel_search, $hostmask_search) = @_; my ($self, $from, $hostmask) = @_;
$channel_search = '.*' if not defined $channel_search; $from = $self->{pbot}->botnick if not defined $from;
$hostmask_search = '.*' if not defined $hostmask_search; $hostmask = '.*' if not defined $hostmask;
my $result = eval { my $result = eval {
foreach my $channel (keys %{ $self->{admins} }) { foreach my $channel_regex (keys %{ $self->{admins} }) {
if($channel_search =~ m/$channel/i) { if($from !~ m/^#/) {
foreach my $hostmask (keys %{ $self->{admins}->{$channel} }) { # if not from a channel, make sure that nick portion of hostmask matches $from
if($hostmask_search =~ m/$hostmask/i) { foreach my $hostmask_regex (keys %{ $self->{admins}->{$channel_regex} }) {
return $self->{admins}{$channel}{$hostmask}; my $nick;
if($hostmask_regex =~ m/^([^!]+)!.*/) {
$nick = $1;
} else {
$nick = $hostmask_regex;
} }
return $self->{admins}{$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;
} }
} }
} }
@ -225,7 +209,7 @@ sub login {
if(not defined $admin) { if(not defined $admin) {
$self->{pbot}->logger->log("Attempt to login non-existent [$channel][$hostmask] failed\n"); $self->{pbot}->logger->log("Attempt to login non-existent [$channel][$hostmask] failed\n");
return "You do not have an account."; return "You do not have an account in $channel.";
} }
if($admin->{password} ne $password) { if($admin->{password} ne $password) {
@ -235,9 +219,9 @@ sub login {
$admin->{loggedin} = 1; $admin->{loggedin} = 1;
$self->{pbot}->logger->log("$hostmask logged-in in $channel\n"); $self->{pbot}->logger->log("$hostmask logged into $channel\n");
return "Logged in."; return "Logged into $channel.";
} }
sub logout { sub logout {
@ -248,4 +232,36 @@ sub logout {
delete $admin->{loggedin} if defined $admin; delete $admin->{loggedin} if defined $admin;
} }
sub export_path {
my $self = shift;
if(@_) { $self->{export_path} = shift; }
return $self->{export_path};
}
sub export_timeout {
my $self = shift;
if(@_) { $self->{export_timeout} = shift; }
return $self->{export_timeout};
}
sub export_site {
my $self = shift;
if(@_) { $self->{export_site} = shift; }
return $self->{export_site};
}
sub admins {
my $self = shift;
return $self->{admins};
}
sub filename {
my $self = shift;
if(@_) { $self->{filename} = shift; }
return $self->{filename};
}
1; 1;

View File

@ -131,9 +131,21 @@ sub list {
if($arguments =~ /^admins$/i) { if($arguments =~ /^admins$/i) {
$text = "Admins: "; $text = "Admins: ";
foreach my $admin (sort { ${ $self->{pbot}->admins->admins }{$b}{level} <=> ${ $self->{pbot}->admins->admins }{$a}{level} } keys %{ $self->{pbot}->admins->admins }) { my $last_channel = "";
$text .= "*" if exists ${ $self->{pbot}->admins->admins }{$admin}{login}; my $sep = "";
$text .= "$admin (" . ${ $self->{pbot}->admins->admins }{$admin}{level} . ") "; foreach my $channel (sort keys %{ $self->{pbot}->admins->admins }) {
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} }) {
$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} . ")";
$sep = "; ";
}
} }
return $text; return $text;
} }

View File

@ -108,8 +108,8 @@ sub initialize {
); );
$self->admins->load_admins(); $self->admins->load_admins();
$self->admins->add_admin('.*', "$botnick!stdin\@localhost", 50, 'admin'); $self->admins->add_admin($botnick, '.*', "$botnick!stdin\@localhost", 60, 'admin');
$self->admins->login('.*', "$botnick!stdin\@localhost", "admin"); $self->admins->login('pbot3', "$botnick!stdin\@localhost", 'admin');
$self->{factoids} = PBot::Factoids->new( $self->{factoids} = PBot::Factoids->new(
pbot => $self, pbot => $self,

2
admins
View File

@ -1 +1 @@
.* .*!example@xyzcorp.com 50 5ecret5@uce somename .* .*!example@xyzcorp.com 60 5ecret5@uce