Add built-in `users` command

This commit is contained in:
Pragmatic Software 2020-02-09 19:29:32 -08:00
parent 55d7561479
commit 0848c5752f
2 changed files with 46 additions and 48 deletions

View File

@ -328,7 +328,7 @@ sub listcmd {
my ($from, $nick, $user, $host, $arguments) = @_;
my $text;
my $usage = "Usage: list <modules|commands|users>";
my $usage = "Usage: list <modules|commands>";
if (not defined $arguments) {
return $usage;
@ -358,37 +358,6 @@ sub listcmd {
}
return $text;
}
if ($arguments =~ /^users$/i) {
$text = "Users: ";
my $last_channel = "";
my $sep = "";
foreach my $channel (sort keys %{ $self->{users}->{users}->{hash} }) {
next if $from =~ m/^#/ and $channel ne $from and $channel ne '.*';
if ($last_channel ne $channel) {
$text .= $sep . ($channel eq ".*" ? "global" : $channel) . ": ";
$last_channel = $channel;
$sep = "";
}
foreach my $hostmask (sort { return 0 if $a eq '_name' or $b eq '_name'; $self->{users}->{users}->{hash}->{$channel}->{$a}->{name} cmp $self->{users}->{users}->{hash}->{$channel}->{$b}->{name} } keys %{ $self->{users}->{users}->{hash}->{$channel} }) {
next if $hostmask eq '_name';
$text .= $sep;
my $has_cap = 0;
foreach my $key (keys %{$self->{users}->{users}->{hash}->{$channel}->{$hostmask}}) {
next if $key eq '_name';
if ($self->{capabilities}->exists($key)) {
$has_cap = 1;
last;
}
}
$text .= '+' if $has_cap;
$text .= $self->{users}->{users}->{hash}->{$channel}->{$hostmask}->{name};
$sep = " ";
}
$sep = "; ";
}
return $text;
}
return $usage;
}
@ -426,7 +395,7 @@ sub evalcmd {
$self->{logger}->log("[$from] $nick!$user\@$host Evaluating [$arguments]\n");
my $ret;
my $ret = '';
my $result = eval $arguments;
if ($@) {
if (length $result) {
@ -436,6 +405,8 @@ sub evalcmd {
}
$ret =~ s/ at \(eval \d+\) line 1.//;
}
$result = 'Undefined.' if not defined $result;
$result = 'No output.' if not length $result;
return "/say $ret $result";
}

View File

@ -24,6 +24,7 @@ sub initialize {
$self->{pbot}->{commands}->register(sub { $self->userdel(@_) }, "userdel", 1);
$self->{pbot}->{commands}->register(sub { $self->userset(@_) }, "userset", 1);
$self->{pbot}->{commands}->register(sub { $self->userunset(@_) }, "userunset", 1);
$self->{pbot}->{commands}->register(sub { $self->users(@_) }, "users", 0);
$self->{pbot}->{commands}->register(sub { $self->mycmd(@_) }, "my", 0);
$self->{pbot}->{capabilities}->add('admin', 'can-useradd', 1);
@ -266,16 +267,13 @@ sub logout {
sub get_loggedin_user_metadata {
my ($self, $channel, $hostmask, $key) = @_;
my $user = $self->loggedin($channel, $hostmask);
if ($user) {
return $user->{lc $key};
}
return $user->{lc $key} if $user;
return undef;
}
sub logincmd {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
my $channel = $from;
return "Usage: login [channel] password" if not $arguments;
if ($arguments =~ m/^([^ ]+)\s+(.+)/) {
@ -299,28 +297,58 @@ sub logincmd {
sub logoutcmd {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
$from = $arguments if length $arguments;
my ($user_channel, $user_hostmask) = $self->find_user_account($from, "$nick!$user\@$host");
if (not defined $user_channel) {
return "/msg $nick You do not have a user account.";
}
return "/msg $nick You do not have a user account." if not defined $user_channel;
my $u = $self->{users}->{hash}->{$user_channel}->{$user_hostmask};
my $channel_text = $user_channel eq '.*' ? '' : " for $user_channel";
if (not $u->{loggedin}) {
return "/msg $nick You are not logged into $u->{name} ($user_hostmask)$channel_text.";
}
return "/msg $nick You are not logged into $u->{name} ($user_hostmask)$channel_text." if not $u->{loggedin};
$self->logout($user_channel, $user_hostmask);
return "/msg $nick Logged out of $u->{name} ($user_hostmask)$channel_text.";
}
sub users {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my $channel = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist});
$channel = $from if not defined $channel;
my $text = "Users: ";
my $last_channel = "";
my $sep = "";
foreach my $chan (sort keys %{ $self->{users}->{hash} }) {
next if $from =~ m/^#/ and $chan ne $channel and $chan ne '.*';
next if $from !~ m/^#/ and $channel =~ m/^#/ and $chan ne $channel;
if ($last_channel ne $chan) {
$text .= $sep . ($chan eq ".*" ? "global" : $chan) . ": ";
$last_channel = $chan;
$sep = "";
}
foreach my $hostmask (sort { return 0 if $a eq '_name' or $b eq '_name'; $self->{users}->{hash}->{$chan}->{$a}->{name} cmp $self->{users}->{hash}->{$chan}->{$b}->{name} } keys %{ $self->{users}->{hash}->{$chan} }) {
next if $hostmask eq '_name';
$text .= $sep;
my $has_cap = 0;
foreach my $key (keys %{$self->{users}->{hash}->{$chan}->{$hostmask}}) {
next if $key eq '_name';
if ($self->{pbot}->{capabilities}->exists($key)) {
$has_cap = 1;
last;
}
}
$text .= '+' if $has_cap;
$text .= $self->{users}->{hash}->{$chan}->{$hostmask}->{name};
$sep = " ";
}
$sep = "; ";
}
return $text;
}
sub useradd {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my ($name, $channel, $hostmask, $capabilities, $password) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 5);
$capabilities //= 'none';
@ -357,7 +385,6 @@ sub useradd {
sub userdel {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my ($channel, $hostmask) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2);
if (not defined $channel or not defined $hostmask) {