2010-03-22 08:33:44 +01:00
|
|
|
# File: ChanOps.pm
|
|
|
|
#
|
2021-08-02 00:53:58 +02:00
|
|
|
# Purpose: Manages channel operator status and command queues.
|
|
|
|
#
|
|
|
|
# Unless the `permop` metadata is set, PBot remains unopped until an OP-related
|
|
|
|
# command is queued. When a command is queued, PBot will request OP status.
|
|
|
|
# Until PBot gains Op status, new OP commands will be added to the queue. Once
|
|
|
|
# PBot gains OP status, all queued commands are invoked and then after a
|
|
|
|
# timeout PBot will remove its OP status.
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::ChanOps;
|
|
|
|
use parent 'PBot::Core::Class';
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
use Time::HiRes qw(gettimeofday);
|
2020-02-01 07:46:19 +01:00
|
|
|
use Time::Duration qw(concise duration);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
$self->{op_commands} = {}; # OP command queue
|
|
|
|
$self->{op_requested} = {}; # channels PBot has requested OP
|
|
|
|
$self->{is_opped} = {}; # channels PBot is currently OP
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# default de-OP timeout
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'general', 'deop_timeout', 300);
|
2014-05-18 02:27:57 +02:00
|
|
|
|
2021-06-22 02:26:24 +02:00
|
|
|
# TODO: enqueue OP events as needed instead of naively checking every 10 seconds
|
|
|
|
$self->{pbot}->{event_queue}->enqueue(sub { $self->check_opped_timeouts }, 10, 'Check opped timeouts');
|
2020-04-29 06:33:49 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# returns true if PBot can gain OP status in $channel
|
2015-06-26 05:54:38 +02:00
|
|
|
sub can_gain_ops {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
|
|
|
return
|
2021-07-09 23:39:35 +02:00
|
|
|
$self->{pbot}->{channels}->{storage}->exists($channel)
|
|
|
|
&& $self->{pbot}->{channels}->{storage}->get_data($channel, 'chanop')
|
|
|
|
&& $self->{pbot}->{channels}->{storage}->get_data($channel, 'enabled');
|
2015-06-26 05:54:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# sends request to gain OP status in $channel
|
2010-03-22 08:33:44 +01:00
|
|
|
sub gain_ops {
|
2021-08-02 00:53:58 +02:00
|
|
|
my ($self, $channel) = @_;
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
2018-06-06 00:02:02 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return if exists $self->{op_requested}->{$channel};
|
|
|
|
return if not $self->can_gain_ops($channel);
|
2014-08-11 09:34:30 +02:00
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
if (not exists $self->{is_opped}->{$channel}) {
|
|
|
|
# not opped in channel, send request for ops
|
|
|
|
my $op_nick = $self->{pbot}->{registry}->get_value($channel, 'op_nick')
|
|
|
|
// $self->{pbot}->{registry}->get_value('general', 'op_nick')
|
|
|
|
// 'chanserv';
|
2019-12-26 14:47:18 +01:00
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
my $op_command = $self->{pbot}->{registry}->get_value($channel, 'op_command')
|
|
|
|
// $self->{pbot}->{registry}->get_value('general', 'op_command')
|
|
|
|
// "op $channel";
|
2019-12-26 14:47:18 +01:00
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
$op_command =~ s/\$channel\b/$channel/g;
|
2019-12-26 14:47:18 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{conn}->privmsg($op_nick, $op_command);
|
|
|
|
$self->{op_requested}->{$channel} = scalar gettimeofday;
|
|
|
|
} else {
|
2021-08-02 00:53:58 +02:00
|
|
|
# already opped, invoke op commands
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->perform_op_commands($channel);
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# removes OP status in $channel
|
2010-03-22 08:33:44 +01:00
|
|
|
sub lose_ops {
|
2021-08-02 00:53:58 +02:00
|
|
|
my ($self, $channel) = @_;
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
|
|
|
$self->{pbot}->{conn}->mode($channel, '-o ' . $self->{pbot}->{registry}->get_value('irc', 'botnick'));
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# adds a command to the OP command queue
|
2014-04-19 12:38:16 +02:00
|
|
|
sub add_op_command {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $command) = @_;
|
|
|
|
return if not $self->can_gain_ops($channel);
|
2021-08-02 00:53:58 +02:00
|
|
|
push @{$self->{op_commands}->{lc $channel}}, $command;
|
2014-04-19 12:38:16 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# invokes commands in OP command queue
|
2010-03-22 08:33:44 +01:00
|
|
|
sub perform_op_commands {
|
2021-08-02 00:53:58 +02:00
|
|
|
my ($self, $channel) = @_;
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
2021-08-02 00:53:58 +02:00
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Performing op commands in $channel:\n");
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
|
|
|
|
|
|
|
while (my $command = shift @{$self->{op_commands}->{$channel}}) {
|
|
|
|
if ($command =~ /^mode (.*?) (.*)/i) {
|
|
|
|
$self->{pbot}->{logger}->log(" executing mode $1 $2\n");
|
2021-08-02 00:53:58 +02:00
|
|
|
$self->{pbot}->{conn}->mode($1, $2);
|
|
|
|
}
|
|
|
|
elsif ($command =~ /^kick (.*?) (.*?) (.*)/i) {
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log(" executing kick on $1 $2 $3\n");
|
2021-08-02 00:53:58 +02:00
|
|
|
$self->{pbot}->{conn}->kick($1, $2, $3) unless $1 =~ /^\Q$botnick\E$/i;
|
|
|
|
}
|
|
|
|
elsif ($command =~ /^sl (.*)/i) {
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log(" executing sl $1\n");
|
2021-08-02 00:53:58 +02:00
|
|
|
$self->{pbot}->{conn}->sl($1);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
2021-08-02 00:53:58 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Done.\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# manages OP-related timeouts
|
2010-03-23 19:24:02 +01:00
|
|
|
sub check_opped_timeouts {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $now = gettimeofday();
|
|
|
|
foreach my $channel (keys %{$self->{is_opped}}) {
|
|
|
|
if ($self->{is_opped}->{$channel}{timeout} < $now) {
|
2021-07-09 23:39:35 +02:00
|
|
|
unless ($self->{pbot}->{channels}->{storage}->exists($channel) and $self->{pbot}->{channels}->{storage}->get_data($channel, 'permop')) { $self->lose_ops($channel); }
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
2014-08-11 09:34:30 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $channel (keys %{$self->{op_requested}}) {
|
|
|
|
if ($now - $self->{op_requested}->{$channel} > 60 * 5) {
|
2021-07-09 23:39:35 +02:00
|
|
|
if ($self->{pbot}->{channels}->{storage}->exists($channel) and $self->{pbot}->{channels}->{storage}->get_data($channel, 'enabled')) {
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("5 minutes since OP request for $channel and no OP yet; trying again ...\n");
|
|
|
|
delete $self->{op_requested}->{$channel};
|
|
|
|
$self->gain_ops($channel);
|
|
|
|
} else {
|
|
|
|
$self->{pbot}->{logger}->log("Disregarding OP request for $channel (channel is disabled)\n");
|
|
|
|
delete $self->{op_requested}->{$channel};
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 09:34:30 +02:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|