2010-03-22 08:33:44 +01:00
|
|
|
# File: ChanOps.pm
|
2010-03-24 07:47:40 +01:00
|
|
|
# Author: pragma_
|
2010-03-22 08:33:44 +01:00
|
|
|
#
|
|
|
|
# Purpose: Provides channel operator status tracking and commands.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
package PBot::ChanOps;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::Class';
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
use PBot::ChanOpCommands;
|
2010-03-22 08:33:44 +01: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
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{op_commands} = {};
|
|
|
|
$self->{is_opped} = {};
|
|
|
|
$self->{op_requested} = {};
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{commands} = PBot::ChanOpCommands->new(pbot => $self->{pbot});
|
2014-05-20 00:59:51 +02:00
|
|
|
|
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
|
|
|
|
2020-03-06 22:28:06 +01:00
|
|
|
$self->{pbot}->{timer}->register(sub { $self->check_opped_timeouts }, 10, 'Check Opped Timeouts');
|
2020-04-29 06:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub track_mode {
|
|
|
|
my ($self, $source, $channel, $mode, $target) = @_;
|
|
|
|
|
2020-06-12 04:30:07 +02:00
|
|
|
$channel = defined $channel ? lc $channel : '';
|
|
|
|
$target = defined $target ? lc $target : '';
|
2020-04-29 06:33:49 +02:00
|
|
|
|
|
|
|
if ($target eq lc $self->{pbot}->{registry}->get_value('irc', 'botnick')) {
|
|
|
|
if ($mode eq '+o') {
|
|
|
|
$self->{pbot}->{logger}->log("$source opped me in $channel\n");
|
|
|
|
my $timeout = $self->{pbot}->{registry}->get_value($channel, 'deop_timeout') // $self->{pbot}->{registry}->get_value('general', 'deop_timeout');
|
|
|
|
$self->{is_opped}->{$channel}{timeout} = gettimeofday + $timeout;
|
|
|
|
delete $self->{op_requested}->{$channel};
|
|
|
|
$self->perform_op_commands($channel);
|
|
|
|
} elsif ($mode eq '-o') {
|
|
|
|
$self->{pbot}->{logger}->log("$source removed my ops in $channel\n");
|
|
|
|
delete $self->{is_opped}->{$channel};
|
|
|
|
} else {
|
|
|
|
$self->{pbot}->{logger}->log("ChanOps: $source performed unhandled mode '$mode' on me\n");
|
|
|
|
}
|
|
|
|
}
|
2010-03-23 19:24:02 +01:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2015-06-26 05:54:38 +02:00
|
|
|
sub can_gain_ops {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
|
|
|
$channel = lc $channel;
|
|
|
|
return
|
|
|
|
$self->{pbot}->{channels}->{channels}->exists($channel)
|
|
|
|
&& $self->{pbot}->{channels}->{channels}->get_data($channel, 'chanop')
|
|
|
|
&& $self->{pbot}->{channels}->{channels}->get_data($channel, 'enabled');
|
2015-06-26 05:54:38 +02:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub gain_ops {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $channel = shift;
|
|
|
|
$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
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
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
|
|
|
|
2020-02-15 23:38:32 +01: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
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$op_command =~ s/\$channel\b/$channel/g;
|
2019-12-26 14:47:18 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not exists $self->{is_opped}->{$channel}) {
|
|
|
|
$self->{pbot}->{conn}->privmsg($op_nick, $op_command);
|
|
|
|
$self->{op_requested}->{$channel} = scalar gettimeofday;
|
|
|
|
} else {
|
|
|
|
$self->perform_op_commands($channel);
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub lose_ops {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $channel = shift;
|
|
|
|
$channel = lc $channel;
|
|
|
|
$self->{pbot}->{conn}->mode($channel, '-o ' . $self->{pbot}->{registry}->get_value('irc', 'botnick'));
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 12:38:16 +02:00
|
|
|
sub add_op_command {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $command) = @_;
|
|
|
|
$channel = lc $channel;
|
|
|
|
return if not $self->can_gain_ops($channel);
|
|
|
|
push @{$self->{op_commands}->{$channel}}, $command;
|
2014-04-19 12:38:16 +02:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub perform_op_commands {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $channel = shift;
|
|
|
|
$channel = lc $channel;
|
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Performing op commands...\n");
|
|
|
|
while (my $command = shift @{$self->{op_commands}->{$channel}}) {
|
|
|
|
if ($command =~ /^mode (.*?) (.*)/i) {
|
|
|
|
$self->{pbot}->{conn}->mode($1, $2);
|
|
|
|
$self->{pbot}->{logger}->log(" executing mode $1 $2\n");
|
|
|
|
} elsif ($command =~ /^kick (.*?) (.*?) (.*)/i) {
|
|
|
|
$self->{pbot}->{conn}->kick($1, $2, $3) unless $1 =~ /^\Q$botnick\E$/i;
|
|
|
|
$self->{pbot}->{logger}->log(" executing kick on $1 $2 $3\n");
|
|
|
|
} elsif ($command =~ /^sl (.*)/i) {
|
|
|
|
$self->{pbot}->{conn}->sl($1);
|
|
|
|
$self->{pbot}->{logger}->log(" executing sl $1\n");
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Done.\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
unless ($self->{pbot}->{channels}->{channels}->exists($channel) and $self->{pbot}->{channels}->{channels}->get_data($channel, 'permop')) { $self->lose_ops($channel); }
|
|
|
|
}
|
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) {
|
|
|
|
if ($self->{pbot}->{channels}->{channels}->exists($channel) and $self->{pbot}->{channels}->{channels}->get_data($channel, 'enabled')) {
|
|
|
|
$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;
|