2010-03-17 07:36:54 +01:00
|
|
|
# File: IRCHandlers.pm
|
2010-03-22 08:33:44 +01:00
|
|
|
# Author: pragma_
|
2010-03-17 07:36:54 +01:00
|
|
|
#
|
|
|
|
# Purpose: Subroutines to handle IRC events
|
|
|
|
|
|
|
|
package PBot::IRCHandlers;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
use vars qw($VERSION);
|
|
|
|
$VERSION = $PBot::PBot::VERSION;
|
|
|
|
|
|
|
|
use Carp();
|
|
|
|
use Time::HiRes qw(gettimeofday);
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
|
|
|
Carp::croak("Options to IRCHandlers should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
|
|
|
my $pbot = delete $conf{pbot};
|
|
|
|
Carp::croak("Missing pbot parameter to IRCHandlers") if not defined $pbot;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot} = $pbot;
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
# IRC related subroutines
|
|
|
|
#################################################
|
|
|
|
|
|
|
|
sub on_connect {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn) = @_;
|
|
|
|
$self->{pbot}->logger->log("Connected! Identifying with NickServ . . .\n");
|
|
|
|
$conn->privmsg("nickserv", "identify " . $self->pbot->identify_password);
|
2010-03-17 07:36:54 +01:00
|
|
|
$conn->{connected} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_disconnect {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
|
|
|
$self->{pbot}->logger->log("Disconnected, attempting to reconnect...\n");
|
|
|
|
$conn->connect();
|
|
|
|
if(not $conn->connected) {
|
2010-03-17 07:36:54 +01:00
|
|
|
sleep(5);
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->on_disconnect($self, $conn, $event);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_init {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
my (@args) = ($event->args);
|
|
|
|
shift (@args);
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("*** @args\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_public {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
my $from = $event->{to}[0];
|
|
|
|
my $nick = $event->nick;
|
|
|
|
my $user = $event->user;
|
|
|
|
my $host = $event->host;
|
|
|
|
my $text = $event->{args}[0];
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->pbot->interpreter->process_line($from, $nick, $user, $host, $text);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_msg {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
my ($nick, $host) = ($event->nick, $event->host);
|
|
|
|
my $text = $event->{args}[0];
|
|
|
|
|
|
|
|
$text =~ s/^!?(.*)/\!$1/;
|
|
|
|
$event->{to}[0] = $nick;
|
|
|
|
$event->{args}[0] = $text;
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->on_public($conn, $event);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_notice {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
my ($nick, $host) = ($event->nick, $event->host);
|
|
|
|
my $text = $event->{args}[0];
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("Received NOTICE from $nick $host '$text'\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
if($nick eq "NickServ" && $text =~ m/You are now identified/i) {
|
2010-06-18 05:21:36 +02:00
|
|
|
foreach my $chan (keys %{ $self->{pbot}->channels->channels->hash }) {
|
|
|
|
if($self->{pbot}->channels->channels->hash->{$chan}{enabled}) {
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->{pbot}->logger->log("Joining channel: $chan\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
$conn->join($chan);
|
|
|
|
}
|
|
|
|
}
|
2010-06-18 12:46:23 +02:00
|
|
|
$self->{pbot}->{joined_channels} = 1;
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_action {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->on_public($conn, $event);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_mode {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-03-17 07:36:54 +01:00
|
|
|
my ($nick, $host) = ($event->nick, $event->host);
|
|
|
|
my $mode = $event->{args}[0];
|
|
|
|
my $target = $event->{args}[1];
|
|
|
|
my $channel = $event->{to}[0];
|
|
|
|
$channel = lc $channel;
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("Got mode: nick: $nick, host: $host, mode: $mode, target: " . (defined $target ? $target : "") . ", channel: $channel\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
if(defined $target && $target eq $self->{pbot}->botnick) { # bot targeted
|
2010-03-17 07:36:54 +01:00
|
|
|
if($mode eq "+o") {
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("$nick opped me in $channel\n");
|
2010-03-23 19:24:02 +01:00
|
|
|
if(exists $self->{pbot}->chanops->{is_opped}->{$channel}) {
|
|
|
|
$self->{pbot}->logger->log("erm, I was already opped?\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->{pbot}->chanops->{is_opped}->{$channel}{timeout} = gettimeofday + 300; # 5 minutes
|
|
|
|
$self->{pbot}->chanops->perform_op_commands();
|
2010-03-17 07:36:54 +01:00
|
|
|
} elsif($mode eq "-o") {
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("$nick removed my ops in $channel\n");
|
2010-03-23 19:24:02 +01:00
|
|
|
if(not exists $self->{pbot}->chanops->{is_opped}->{$channel}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("warning: erm, I wasn't opped?\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2010-03-23 19:24:02 +01:00
|
|
|
delete $self->{pbot}->chanops->{is_opped}->{$channel};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
} else { # bot not targeted
|
|
|
|
if($mode eq "+b") {
|
|
|
|
if($nick eq "ChanServ") {
|
2010-06-18 12:46:23 +02:00
|
|
|
$self->{pbot}->chanops->{unban_timeout}->hash->{$target}{timeout} = gettimeofday + 3600 * 2; # 2 hours
|
|
|
|
$self->{pbot}->chanops->{unban_timeout}->hash->{$target}{channel} = $channel;
|
|
|
|
$self->{pbot}->chanops->{unban_timeout}->save_hash();
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2010-03-23 19:24:02 +01:00
|
|
|
} elsif($mode eq "+e" && $channel eq $self->{pbot}->botnick) {
|
2010-06-18 05:21:36 +02:00
|
|
|
foreach my $chan (keys %{ $self->{pbot}->channels->channels->hash }) {
|
|
|
|
if($self->channels->channels->hash->{$chan}{enabled}) {
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->{pbot}->logger->log("Joining channel: $chan\n");
|
|
|
|
$self->{pbot}->conn->join($chan);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-18 12:46:23 +02:00
|
|
|
$self->{pbot}->{joined_channels} = 1;
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_join {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-06-05 08:07:15 +02:00
|
|
|
my ($nick, $user, $host, $channel) = ($event->nick, $event->user, $event->host, $event->to);
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
#$self->{pbot}->logger->log("$nick!$user\@$host joined $channel\n");
|
2010-06-10 22:26:09 +02:00
|
|
|
$self->{pbot}->antiflood->check_flood($channel, $nick, $user, $host, "JOIN", 4, 60 * 30, $self->{pbot}->antiflood->{FLOOD_JOIN});
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_departure {
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($self, $conn, $event) = @_;
|
2010-06-10 22:26:09 +02:00
|
|
|
my ($nick, $user, $host, $channel, $args) = ($event->nick, $event->user, $event->host, $event->to, $event->args);
|
|
|
|
|
|
|
|
my $text = uc $event->type;
|
|
|
|
$text .= " $args";
|
|
|
|
|
2010-06-12 07:50:38 +02:00
|
|
|
$self->{pbot}->antiflood->check_flood($channel, $nick, $user, $host, $text, 4, 60 * 30, $self->{pbot}->antiflood->{FLOOD_JOIN});
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
=cut
|
2010-03-17 07:36:54 +01:00
|
|
|
if(exists $admins{$nick} && exists $admins{$nick}{login}) {
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->{pbot}->logger->log("Whoops, $nick left while still logged in.\n");
|
|
|
|
$self->{pbot}->logger->log("Logged out $nick.\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
delete $admins{$nick}{login};
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
=cut
|
|
|
|
}
|
|
|
|
|
|
|
|
sub pbot {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{pbot};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|