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;
|
|
|
|
|
|
|
|
use vars qw($VERSION);
|
|
|
|
$VERSION = $PBot::PBot::VERSION;
|
|
|
|
|
|
|
|
use Time::HiRes qw(gettimeofday);
|
2010-06-18 12:46:23 +02:00
|
|
|
use PBot::HashObject;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
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;
|
2010-06-23 08:51:39 +02:00
|
|
|
$self->{unban_timeout} = PBot::HashObject->new(pbot => $pbot, name => 'Unban Timeouts', filename => "$pbot->{data_dir}/unban_timeouts");
|
2012-10-27 23:03:10 +02:00
|
|
|
$self->{op_commands} = {};
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{is_opped} = {};
|
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
$pbot->timer->register(sub { $self->check_opped_timeouts }, 10);
|
2011-02-11 03:46:35 +01:00
|
|
|
$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}) {
|
2010-03-22 08:33:44 +01: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;
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->{pbot}->conn->privmsg("chanserv", "op $channel -" . $self->{pbot}->botnick);
|
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;
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("Performing op commands...\n");
|
2012-10-27 23:03:10 +02:00
|
|
|
foreach my $command (@{ $self->{op_commands}->{$channel} }) {
|
2010-03-22 08:33:44 +01:00
|
|
|
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$self->{pbot}->botnick\E/i;
|
|
|
|
$self->{pbot}->logger->log(" executing kick on $1 $2 $3\n");
|
|
|
|
}
|
2012-10-27 23:03:10 +02:00
|
|
|
shift(@{ $self->{op_commands}->{$channel} });
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
$self->{pbot}->logger->log("Done.\n");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
unshift @{ $self->{op_commands}->{$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) = @_;
|
2011-10-31 20:56:08 +01:00
|
|
|
$self->{pbot}->logger->log("Unbanning $mask\n");
|
|
|
|
delete $self->{unban_timeout}->hash->{$mask};
|
|
|
|
$self->{unban_timeout}->save_hash();
|
2012-10-27 23:03:10 +02:00
|
|
|
unshift @{ $self->{op_commands}->{$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);
|
2010-06-18 12:46:23 +02:00
|
|
|
$self->{unban_timeout}->hash->{$mask}{timeout} = gettimeofday + $length;
|
|
|
|
$self->{unban_timeout}->hash->{$mask}{channel} = $channel;
|
|
|
|
$self->{unban_timeout}->save_hash();
|
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();
|
|
|
|
|
2010-06-18 12:46:23 +02:00
|
|
|
foreach my $mask (keys %{ $self->{unban_timeout}->hash }) {
|
|
|
|
if($self->{unban_timeout}->hash->{$mask}{timeout} < $now) {
|
|
|
|
$self->unban_user($mask, $self->{unban_timeout}->hash->{$mask}{channel});
|
2010-03-22 08:33:44 +01:00
|
|
|
} else {
|
2012-08-24 00:50:07 +02:00
|
|
|
# my $timediff = $self->{unban_timeout}->hash->{$mask}{timeout} - $now;
|
|
|
|
# $self->{pbot}->logger->log("ban: $mask has $timediff seconds remaining\n");
|
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;
|
|
|
|
# $self->{pbot}->logger->log("deop $channel in $timediff seconds\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|