3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-22 20:09:43 +01:00

Add op and deop commands

This commit is contained in:
Pragmatic Software 2020-01-11 17:46:44 -08:00
parent af6ba85ce8
commit 3da8e22e4e
2 changed files with 133 additions and 2 deletions

View File

@ -47,6 +47,8 @@ sub initialize {
$pbot->{commands}->register(sub { return $self->kick_user(@_) }, "kick", 10); $pbot->{commands}->register(sub { return $self->kick_user(@_) }, "kick", 10);
$pbot->{commands}->register(sub { return $self->checkban(@_) }, "checkban", 0); $pbot->{commands}->register(sub { return $self->checkban(@_) }, "checkban", 0);
$pbot->{commands}->register(sub { return $self->checkmute(@_) }, "checkmute", 0); $pbot->{commands}->register(sub { return $self->checkmute(@_) }, "checkmute", 0);
$pbot->{commands}->register(sub { return $self->op_user(@_) }, "op", 10);
$pbot->{commands}->register(sub { return $self->deop_user(@_) }, "deop", 10);
$pbot->{commands}->register(sub { return $self->mode(@_) }, "mode", 40); $pbot->{commands}->register(sub { return $self->mode(@_) }, "mode", 40);
$pbot->{commands}->register(sub { return $self->invite(@_) }, "invite", 10); $pbot->{commands}->register(sub { return $self->invite(@_) }, "invite", 10);
} }
@ -61,7 +63,7 @@ sub invite {
# add current channel as default channel # add current channel as default channel
if ($stuff->{arglist}[0] !~ m/^#/) { if ($stuff->{arglist}[0] !~ m/^#/) {
if ($from =~ m/^#/) { if ($from =~ m/^#/) {
unshift @{$stuff->{arglist}}, $from; $self->{pbot}->{interpreter}->unshift_arg($stuff->{arglist}, $from);
} else { } else {
return "Usage from private message: invite <channel> <nick>"; return "Usage from private message: invite <channel> <nick>";
} }
@ -73,6 +75,120 @@ sub invite {
return ""; return "";
} }
sub op_user {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my $channel = $from;
my $result = '';
if ($channel !~ m/^#/) {
# from message
$channel = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist});
$result = 'Done.';
if (not defined $channel) {
return "Usage from message: op <channel> [nick]";
} elsif ($channel !~ m/^#/) {
return "$channel is not a channel. Usage from message: op <channel> [nick]";
}
}
$channel = lc $channel;
if (not $self->{pbot}->{chanops}->can_gain_ops($channel)) {
return "I am not configured as an OP for $channel. See `chanset` command for more information.";
}
# add $nick to $args if no argument
if (not $self->{pbot}->{interpreter}->arglist_size($stuff->{arglist})) {
$self->{pbot}->{interpreter}->unshift_arg($stuff->{arglist}, $nick);
}
my $max_modes = $self->{pbot}->{ircd}->{MODES} // 1;
my $mode = '+';
my $list = '';
my $i = 0;
foreach my $targets ($self->{pbot}->{interpreter}->unquoted_args($stuff->{arglist})) {
foreach my $target (split /,/, $targets) {
$mode .= 'o';
$list .= "$target ";
$i++;
if ($i >= $max_modes) {
my $args = "$channel $mode $list";
$stuff->{arglist} = $self->{pbot}->{interpreter}->make_args($args);
$self->mode($from, $nick, $user, $host, $args, $stuff);
$mode = '+';
$list = '';
$i = 0;
}
}
}
if ($i) {
my $args = "$channel $mode $list";
$stuff->{arglist} = $self->{pbot}->{interpreter}->make_args($args);
$self->mode($from, $nick, $user, $host, $args, $stuff);
}
return $result;
}
sub deop_user {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my $channel = $from;
my $result = '';
if ($channel !~ m/^#/) {
# from message
$channel = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist});
$result = 'Done.';
if (not defined $channel) {
return "Usage from message: deop <channel> [nick]";
} elsif ($channel !~ m/^#/) {
return "$channel is not a channel. Usage from message: deop <channel> [nick]";
}
}
$channel = lc $channel;
if (not $self->{pbot}->{chanops}->can_gain_ops($channel)) {
return "I am not configured as an OP for $channel. See `chanset` command for more information.";
}
# add $nick to $args if no argument
if (not $self->{pbot}->{interpreter}->arglist_size($stuff->{arglist})) {
$self->{pbot}->{interpreter}->unshift_arg($stuff->{arglist}, $nick);
}
my $max_modes = $self->{pbot}->{ircd}->{MODES} // 1;
my $mode = '-';
my $list = '';
my $i = 0;
foreach my $targets ($self->{pbot}->{interpreter}->unquoted_args($stuff->{arglist})) {
foreach my $target (split /,/, $targets) {
$mode .= 'o';
$list .= "$target ";
$i++;
if ($i >= $max_modes) {
my $args = "$channel $mode $list";
$stuff->{arglist} = $self->{pbot}->{interpreter}->make_args($args);
$self->mode($from, $nick, $user, $host, $args, $stuff);
$mode = '-';
$list = '';
$i = 0;
}
}
}
if ($i) {
my $args = "$channel $mode $list";
$stuff->{arglist} = $self->{pbot}->{interpreter}->make_args($args);
$self->mode($from, $nick, $user, $host, $args, $stuff);
}
return $result;
}
sub mode { sub mode {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_; my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
@ -83,7 +199,7 @@ sub mode {
# add current channel as default channel # add current channel as default channel
if ($stuff->{arglist}[0] !~ m/^#/) { if ($stuff->{arglist}[0] !~ m/^#/) {
if ($from =~ m/^#/) { if ($from =~ m/^#/) {
unshift @{$stuff->{arglist}}, $from; $self->{pbot}->{interpreter}->unshift_arg($stuff->{arglist}, $from);
} else { } else {
return "Usage from private message: mode <channel> <arguments>"; return "Usage from private message: mode <channel> <arguments>";
} }

View File

@ -678,6 +678,14 @@ sub arglist_size {
return @$args / 2; return @$args / 2;
} }
# unshifts new argument to front
sub unshift_arg {
my ($self, $args, $arg) = @_;
splice @$args, @$args / 2, 0, $arg; # add quoted argument
unshift @$args, $arg; # add first argument
return @$args;
}
# shifts first argument off array of arguments # shifts first argument off array of arguments
sub shift_arg { sub shift_arg {
my ($self, $args) = @_; my ($self, $args) = @_;
@ -686,6 +694,13 @@ sub shift_arg {
return shift @$args; return shift @$args;
} }
# returns list of unquoted arguments
sub unquoted_args {
my ($self, $args) = @_;
return undef if not @$args;
return @$args[0 .. @$args / 2 - 1];
}
# splits array of arguments into array with overflow arguments filling up last position # splits array of arguments into array with overflow arguments filling up last position
# split_args(qw/dog cat bird hamster/, 3) => ("dog", "cat", "bird hamster") # split_args(qw/dog cat bird hamster/, 3) => ("dog", "cat", "bird hamster")
sub split_args { sub split_args {