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') {
|
|
|
|
Carp::croak("Options to ChanOps should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
|
|
|
my $pbot = delete $conf{pbot};
|
|
|
|
if(not defined $pbot) {
|
|
|
|
Carp::croak("Missing pbot reference to ChanOps");
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{pbot} = $pbot;
|
2014-05-17 22:08:19 +02:00
|
|
|
|
|
|
|
$self->{unban_timeout} = PBot::DualIndexHashObject->new(pbot => $pbot, name => 'Unban Timeouts', filename => $pbot->{registry}->get_value('general', 'data_dir') . '/unban_timeouts');
|
|
|
|
$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-05-18 02:27:57 +02:00
|
|
|
$self->{commands} = PBot::ChanOpCommands->new(pbot => $pbot);
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$pbot->{timer}->register(sub { $self->check_opped_timeouts }, 10);
|
|
|
|
$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;
|
|
|
|
|
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");
|
2010-04-03 21:49:41 +02:00
|
|
|
$self->{is_opped}->{$channel}{timeout} = gettimeofday + 300; # assume we're going to be opped
|
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) = @_;
|
|
|
|
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);
|
|
|
|
$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
|
|
|
|
2010-06-18 09:03:16 +02:00
|
|
|
$self->ban_user($mask, $channel);
|
2013-07-24 14:35:40 +02:00
|
|
|
$self->{unban_timeout}->hash->{$channel}->{$mask}{timeout} = gettimeofday + $length;
|
|
|
|
$self->{unban_timeout}->save;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$self->lose_ops($channel);
|
2012-08-24 00:50:07 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|