diff --git a/PBot/BotAdminCommands.pm b/PBot/BotAdminCommands.pm index db9e8392..9f0764a0 100644 --- a/PBot/BotAdminCommands.pm +++ b/PBot/BotAdminCommands.pm @@ -41,6 +41,7 @@ sub initialize { $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); } sub login { @@ -48,7 +49,7 @@ sub login { my ($from, $nick, $user, $host, $arguments) = @_; 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); @@ -58,7 +59,7 @@ sub login { sub logout { my $self = shift; 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"); return "/msg $nick Good-bye, $nick."; } @@ -66,13 +67,34 @@ sub logout { sub add_admin { my $self = shift; 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 { my $self = shift; 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 { diff --git a/PBot/BotAdmins.pm b/PBot/BotAdmins.pm index 8ae94d49..991ce2d0 100644 --- a/PBot/BotAdmins.pm +++ b/PBot/BotAdmins.pm @@ -57,22 +57,31 @@ sub initialize { sub add_admin { my $self = shift; - my ($channel, $hostmask, $level, $password) = @_; + my ($name, $channel, $hostmask, $level, $password) = @_; $channel = lc $channel; $hostmask = lc $hostmask; - ${ $self->admins}{$channel}{$hostmask}{level} = $level; - ${ $self->admins}{$channel}{$hostmask}{password} = $password; + ${ $self->admins }{$channel}{$hostmask}{name} = $name; + ${ $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 { my $self = shift; 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 { @@ -98,13 +107,13 @@ sub load_admins { chomp $line; $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"; } - $self->add_admin($channel, $hostmask, $level, $password); + $self->add_admin($name, $channel, $hostmask, $level, $password); } $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"; 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 { @@ -133,66 +153,30 @@ sub export_admins { 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 { - my ($self, $channel_search, $hostmask_search) = @_; + my ($self, $from, $hostmask) = @_; - $channel_search = '.*' if not defined $channel_search; - $hostmask_search = '.*' if not defined $hostmask_search; + $from = $self->{pbot}->botnick if not defined $from; + $hostmask = '.*' if not defined $hostmask; my $result = eval { - foreach my $channel (keys %{ $self->{admins} }) { - if($channel_search =~ m/$channel/i) { - foreach my $hostmask (keys %{ $self->{admins}->{$channel} }) { - if($hostmask_search =~ m/$hostmask/i) { - return $self->{admins}{$channel}{$hostmask}; + foreach my $channel_regex (keys %{ $self->{admins} }) { + 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} }) { + 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) { $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) { @@ -235,9 +219,9 @@ sub login { $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 { @@ -248,4 +232,36 @@ sub logout { 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; diff --git a/PBot/FactoidCommands.pm b/PBot/FactoidCommands.pm index 18f21332..664fcedc 100644 --- a/PBot/FactoidCommands.pm +++ b/PBot/FactoidCommands.pm @@ -131,9 +131,21 @@ sub list { if($arguments =~ /^admins$/i) { $text = "Admins: "; - foreach my $admin (sort { ${ $self->{pbot}->admins->admins }{$b}{level} <=> ${ $self->{pbot}->admins->admins }{$a}{level} } keys %{ $self->{pbot}->admins->admins }) { - $text .= "*" if exists ${ $self->{pbot}->admins->admins }{$admin}{login}; - $text .= "$admin (" . ${ $self->{pbot}->admins->admins }{$admin}{level} . ") "; + my $last_channel = ""; + my $sep = ""; + 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; } diff --git a/PBot/PBot.pm b/PBot/PBot.pm index c0cdb27e..46cef2b1 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -108,8 +108,8 @@ sub initialize { ); $self->admins->load_admins(); - $self->admins->add_admin('.*', "$botnick!stdin\@localhost", 50, 'admin'); - $self->admins->login('.*', "$botnick!stdin\@localhost", "admin"); + $self->admins->add_admin($botnick, '.*', "$botnick!stdin\@localhost", 60, 'admin'); + $self->admins->login('pbot3', "$botnick!stdin\@localhost", 'admin'); $self->{factoids} = PBot::Factoids->new( pbot => $self, diff --git a/admins b/admins index 2f89ba21..e570426c 100644 --- a/admins +++ b/admins @@ -1 +1 @@ -.* .*!example@xyzcorp.com 50 5ecret5@uce +somename .* .*!example@xyzcorp.com 60 5ecret5@uce