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.
|
|
|
|
|
|
|
|
package PBot::ChanOps;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
use PBot::ChanOpCommands;
|
2010-03-22 08:33:44 +01:00
|
|
|
use Time::HiRes qw(gettimeofday);
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
2014-05-20 00:59:51 +02:00
|
|
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2014-05-20 00:59:51 +02:00
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to ChanOps");
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-20 00:59:51 +02:00
|
|
|
$self->{unban_timeout} = PBot::DualIndexHashObject->new(
|
|
|
|
pbot => $self->{pbot},
|
|
|
|
name => 'Unban Timeouts',
|
|
|
|
filename => $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/unban_timeouts'
|
|
|
|
);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
|
|
|
$self->{unban_timeout}->load;
|
|
|
|
|
2012-10-27 23:03:10 +02:00
|
|
|
$self->{op_commands} = {};
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{is_opped} = {};
|
2014-08-11 09:34:30 +02:00
|
|
|
$self->{op_requested} = {};
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-20 00:59:51 +02:00
|
|
|
$self->{commands} = PBot::ChanOpCommands->new(pbot => $self->{pbot});
|
|
|
|
|
|
|
|
$self->{pbot}->{registry}->add_default('text', 'general', 'deop_timeout', $conf{'deop_timeout'} // 300);
|
2014-05-18 02:27:57 +02:00
|
|
|
|
2014-05-20 00:59:51 +02:00
|
|
|
$self->{pbot}->{timer}->register(sub { $self->check_opped_timeouts }, 10);
|
|
|
|
$self->{pbot}->{timer}->register(sub { $self->check_unban_timeouts }, 10);
|
2010-03-23 19:24:02 +01:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
sub gain_ops {
|
|
|
|
my $self = shift;
|
|
|
|
my $channel = shift;
|
|
|
|
|
2014-08-11 09:34:30 +02:00
|
|
|
return if exists $self->{op_requested}->{$channel};
|
|
|
|
return if not exists $self->{pbot}->{channels}->{channels}->hash->{$channel} or not $self->{pbot}->{channels}->{channels}->hash->{$channel}{chanop};
|
|
|
|
|
2012-10-27 23:03:10 +02:00
|
|
|
if(not exists $self->{is_opped}->{$channel}) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{conn}->privmsg("chanserv", "op $channel");
|
2014-08-11 09:34:30 +02:00
|
|
|
$self->{op_requested}->{$channel} = scalar gettimeofday;
|
2010-03-22 08:33:44 +01:00
|
|
|
} else {
|
2012-10-27 23:03:10 +02:00
|
|
|
$self->perform_op_commands($channel);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lose_ops {
|
|
|
|
my $self = shift;
|
|
|
|
my $channel = shift;
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{conn}->privmsg("chanserv", "op $channel -" . $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 {
|
|
|
|
my ($self, $channel, $command) = @_;
|
2014-10-28 21:34:48 +01:00
|
|
|
return if not exists $self->{pbot}->{channels}->{channels}->hash->{$channel} or not $self->{pbot}->{channels}->{channels}->hash->{$channel}{chanop};
|
2014-04-19 12:38:16 +02:00
|
|
|
push @{ $self->{op_commands}->{$channel} }, $command;
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub perform_op_commands {
|
|
|
|
my $self = shift;
|
2012-10-27 23:03:10 +02:00
|
|
|
my $channel = shift;
|
2014-05-17 22:08:19 +02:00
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
2012-10-27 23:03:10 +02:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Performing op commands...\n");
|
2014-04-19 12:38:16 +02:00
|
|
|
while(my $command = shift @{ $self->{op_commands}->{$channel} }) {
|
2010-03-22 08:33:44 +01:00
|
|
|
if($command =~ /^mode (.*?) (.*)/i) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{conn}->mode($1, $2);
|
2014-08-11 09:34:30 +02:00
|
|
|
$self->{pbot}->{logger}->log(" executing mode [$1] [$2]\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
} elsif($command =~ /^kick (.*?) (.*?) (.*)/i) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{conn}->kick($1, $2, $3) unless $1 =~ /\Q$botnick\E/i;
|
|
|
|
$self->{pbot}->{logger}->log(" executing kick on $1 $2 $3\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Done.\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2010-06-18 09:03:16 +02:00
|
|
|
sub ban_user {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2010-05-30 04:02:29 +02:00
|
|
|
my ($mask, $channel) = @_;
|
2012-10-27 23:03:10 +02:00
|
|
|
|
2014-04-19 12:38:16 +02:00
|
|
|
$self->add_op_command($channel, "mode $channel +b $mask");
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->gain_ops($channel);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2010-06-18 09:03:16 +02:00
|
|
|
sub unban_user {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2010-05-30 04:02:29 +02:00
|
|
|
my ($mask, $channel) = @_;
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Unbanning $channel $mask\n");
|
2013-07-24 14:35:40 +02:00
|
|
|
if($self->{unban_timeout}->find_index($channel, $mask)) {
|
|
|
|
$self->{unban_timeout}->hash->{$channel}->{$mask}{timeout} = gettimeofday + 7200; # try again in 2 hours if unban doesn't immediately succeed
|
|
|
|
$self->{unban_timeout}->save;
|
|
|
|
}
|
2014-04-19 12:38:16 +02:00
|
|
|
$self->add_op_command($channel, "mode $channel -b $mask");
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->gain_ops($channel);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2010-06-18 09:03:16 +02:00
|
|
|
sub ban_user_timed {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2010-05-30 04:02:29 +02:00
|
|
|
my ($mask, $channel, $length) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2015-05-07 06:13:39 +02:00
|
|
|
$mask .= '!*@*' if $mask !~ m/[\$!@]/;
|
2010-06-18 09:03:16 +02:00
|
|
|
$self->ban_user($mask, $channel);
|
2015-05-07 06:13:39 +02:00
|
|
|
if ($length > 0) {
|
|
|
|
$self->{unban_timeout}->hash->{$channel}->{$mask}{timeout} = gettimeofday + $length;
|
|
|
|
$self->{unban_timeout}->save;
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 09:46:13 +01:00
|
|
|
sub join_channel {
|
|
|
|
my ($self, $channel) = @_;
|
|
|
|
|
|
|
|
$self->{pbot}->{event_dispatcher}->dispatch_event('pbot.join', { channel => $channel });
|
|
|
|
$self->{pbot}->{conn}->join($channel);
|
|
|
|
|
|
|
|
delete $self->{is_opped}->{$channel};
|
|
|
|
delete $self->{op_requested}->{$channel};
|
|
|
|
|
|
|
|
if (exists $self->{pbot}->{channels}->{channels}->hash->{$channel}
|
|
|
|
and exists $self->{pbot}->{channels}->{channels}->hash->{$channel}{permop}
|
|
|
|
and $self->{pbot}->{channels}->{channels}->hash->{$channel}{permop}) {
|
|
|
|
$self->gain_ops($channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub part_channel {
|
|
|
|
my ($self, $channel) = @_;
|
|
|
|
|
|
|
|
$self->{pbot}->{event_dispatcher}->dispatch_event('pbot.part', { channel => $channel });
|
|
|
|
$self->{pbot}->{conn}->part($channel);
|
|
|
|
|
|
|
|
delete $self->{is_opped}->{$channel};
|
|
|
|
delete $self->{op_requested}->{$channel};
|
|
|
|
}
|
|
|
|
|
2010-06-18 09:03:16 +02:00
|
|
|
sub check_unban_timeouts {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2010-06-18 12:46:23 +02:00
|
|
|
|
|
|
|
return if not $self->{pbot}->{joined_channels};
|
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
my $now = gettimeofday();
|
|
|
|
|
2013-07-24 14:35:40 +02:00
|
|
|
foreach my $channel (keys %{ $self->{unban_timeout}->hash }) {
|
|
|
|
foreach my $mask (keys %{ $self->{unban_timeout}->hash->{$channel} }) {
|
|
|
|
if($self->{unban_timeout}->hash->{$channel}->{$mask}{timeout} < $now) {
|
|
|
|
$self->unban_user($mask, $channel);
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
sub check_opped_timeouts {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2010-03-23 19:24:02 +01:00
|
|
|
my $now = gettimeofday();
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
foreach my $channel (keys %{ $self->{is_opped} }) {
|
|
|
|
if($self->{is_opped}->{$channel}{timeout} < $now) {
|
2014-12-18 09:46:13 +01:00
|
|
|
unless (exists $self->{pbot}->{channels}->{channels}->hash->{$channel}
|
|
|
|
and exists $self->{pbot}->{channels}->{channels}->hash->{$channel}{permop}
|
|
|
|
and $self->{pbot}->{channels}->{channels}->hash->{$channel}{permop}) {
|
|
|
|
$self->lose_ops($channel);
|
|
|
|
delete $self->{is_opped}->{$channel}; # assume chanserv is alive and deop will succeed
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
} else {
|
2012-08-24 00:50:07 +02:00
|
|
|
# my $timediff = $self->{is_opped}->{$channel}{timeout} - $now;
|
2014-05-18 22:09:05 +02:00
|
|
|
# $self->{pbot}->{logger}->log("deop $channel in $timediff seconds\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 09:34:30 +02:00
|
|
|
|
|
|
|
foreach my $channel (keys %{ $self->{op_requested} }) {
|
|
|
|
if ($now - $self->{op_requested}->{$channel} > 60 * 5) {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|