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) = @_;
|
2011-02-12 06:30:44 +01:00
|
|
|
$self->{pbot}->logger->log("Connected!\n");
|
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];
|
|
|
|
|
2011-01-30 04:22:00 +01:00
|
|
|
$text =~ s/^\Q$self->{pbot}->{trigger}\E?(.*)/$self->{pbot}->{trigger}$1/;
|
2010-03-17 07:36:54 +01:00
|
|
|
$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
|
|
|
|
2011-02-12 06:30:44 +01:00
|
|
|
if($nick eq "NickServ" && $text =~ m/This nickname is registered/) {
|
|
|
|
$self->{pbot}->logger->log("Identifying with NickServ . . .\n");
|
|
|
|
$conn->privmsg("nickserv", "identify " . $self->pbot->identify_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($nick eq "NickServ" && $text =~ m/You are now identified/) {
|
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) = @_;
|
2011-02-13 06:07:02 +01:00
|
|
|
my ($nick, $user, $host) = ($event->nick, $event->user, $event->host);
|
2011-02-13 10:05:48 +01:00
|
|
|
my $mode_string = $event->{args}[0];
|
2010-03-17 07:36:54 +01:00
|
|
|
my $channel = $event->{to}[0];
|
|
|
|
$channel = lc $channel;
|
|
|
|
|
2011-02-13 10:05:48 +01:00
|
|
|
my ($mode, $modifier);
|
|
|
|
my $i = 0;
|
|
|
|
my $target;
|
2011-02-13 06:07:02 +01:00
|
|
|
|
2011-02-13 10:05:48 +01:00
|
|
|
while($mode_string =~ m/(.)/g) {
|
|
|
|
my $char = $1;
|
2011-02-13 06:07:02 +01:00
|
|
|
|
2011-02-13 10:05:48 +01:00
|
|
|
if($char eq '-' or $char eq '+') {
|
|
|
|
$modifier = $char;
|
|
|
|
next;
|
|
|
|
}
|
2011-02-13 06:07:02 +01:00
|
|
|
|
2011-02-13 10:05:48 +01:00
|
|
|
$mode = $modifier . $char;
|
|
|
|
$target = $event->{args}[++$i];
|
2011-02-13 06:07:02 +01:00
|
|
|
|
2011-02-13 10:05:48 +01:00
|
|
|
$self->{pbot}->logger->log("Got mode: source: $nick!$user\@$host, mode: $mode, target: " . (defined $target ? $target : "(undef)") . ", channel: $channel\n");
|
2011-02-13 06:07:02 +01:00
|
|
|
|
2011-02-13 10:05:48 +01:00
|
|
|
if($mode eq "-b" or $mode eq "+b" or $mode eq "-q" or $mode eq "+q") {
|
|
|
|
$self->{pbot}->bantracker->track_mode("$nick!$user\@$host", $mode, $target, $channel);
|
2011-02-13 06:07:02 +01:00
|
|
|
}
|
2011-02-13 10:05:48 +01:00
|
|
|
|
|
|
|
if(defined $target && $target eq $self->{pbot}->botnick) { # bot targeted
|
|
|
|
if($mode eq "+o") {
|
|
|
|
$self->{pbot}->logger->log("$nick opped me in $channel\n");
|
|
|
|
$self->{pbot}->chanops->{is_opped}->{$channel}{timeout} = gettimeofday + 300; # 5 minutes
|
2012-10-27 23:03:10 +02:00
|
|
|
$self->{pbot}->chanops->perform_op_commands($channel);
|
2011-02-13 10:05:48 +01:00
|
|
|
}
|
|
|
|
elsif($mode eq "-o") {
|
|
|
|
$self->{pbot}->logger->log("$nick removed my ops in $channel\n");
|
|
|
|
delete $self->{pbot}->chanops->{is_opped}->{$channel};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2011-02-13 10:05:48 +01:00
|
|
|
elsif($mode eq "+b") {
|
|
|
|
$self->{pbot}->logger->log("Got banned in $channel, attempting unban.");
|
|
|
|
$conn->privmsg("chanserv", "unban $channel");
|
|
|
|
}
|
2011-02-13 06:07:02 +01:00
|
|
|
}
|
2011-02-13 10:05:48 +01:00
|
|
|
else { # bot not targeted
|
|
|
|
if($mode eq "+b") {
|
|
|
|
if($nick eq "ChanServ") {
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif($mode eq "+e" && $channel eq $self->{pbot}->botnick) {
|
|
|
|
foreach my $chan (keys %{ $self->{pbot}->channels->channels->hash }) {
|
|
|
|
if($self->channels->channels->hash->{$chan}{enabled}) {
|
|
|
|
$self->{pbot}->logger->log("Joining channel: $chan\n");
|
|
|
|
$self->{pbot}->conn->join($chan);
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2011-02-13 06:07:02 +01:00
|
|
|
|
2011-02-13 10:05:48 +01: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
|
|
|
|
2012-10-27 23:03:10 +02:00
|
|
|
my $admin = $self->{pbot}->admins->find_admin($channel, "$nick!$user\@$host");
|
|
|
|
if(defined $admin and $admin->{loggedin}) {
|
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");
|
2012-10-27 23:03:10 +02:00
|
|
|
delete $admin->{loggedin};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub pbot {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{pbot};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|