2010-03-17 07:36:54 +01:00
|
|
|
# File: PBot.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: IRC Bot (3rd generation)
|
|
|
|
|
2010-06-05 00:55:03 +02:00
|
|
|
# $Id$
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
package PBot::PBot;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2010-06-05 06:34:14 +02:00
|
|
|
use PBot::VERSION;
|
2010-06-05 02:30:03 +02:00
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
use vars qw($VERSION);
|
2010-06-05 06:34:14 +02:00
|
|
|
$VERSION = PBot::VERSION::BUILD_NAME . " revision " . PBot::VERSION::BUILD_REVISION . " " . PBot::VERSION::BUILD_DATE;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
# unbuffer stdout
|
|
|
|
STDOUT->autoflush(1);
|
|
|
|
|
|
|
|
use Carp ();
|
|
|
|
use PBot::Logger;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
use PBot::StdinReader;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2011-01-22 09:35:31 +01:00
|
|
|
use PBot::IRC;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::IRCHandlers;
|
2010-03-23 19:24:02 +01:00
|
|
|
use PBot::Channels;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2011-02-13 06:07:02 +01:00
|
|
|
use PBot::BanTracker;
|
|
|
|
|
2011-01-25 00:56:55 +01:00
|
|
|
use PBot::LagChecker;
|
2010-03-17 07:36:54 +01:00
|
|
|
use PBot::AntiFlood;
|
2011-01-25 00:56:55 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::Interpreter;
|
|
|
|
use PBot::Commands;
|
|
|
|
|
|
|
|
use PBot::ChanOps;
|
2010-03-23 19:24:02 +01:00
|
|
|
use PBot::ChanOpCommands;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
use PBot::Factoids;
|
|
|
|
use PBot::FactoidCommands;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::BotAdmins;
|
|
|
|
use PBot::BotAdminCommands;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::IgnoreList;
|
|
|
|
use PBot::IgnoreListCommands;
|
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
use PBot::Quotegrabs;
|
|
|
|
# no PBot::QuotegrabsCommands (bundled inside PBot::Quotegrabs for a change)
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::Timer;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
|
|
|
Carp::croak("Options to Logger should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
my $log_file = delete $conf{log_file};
|
2010-06-18 12:46:23 +02:00
|
|
|
|
2011-01-30 04:22:00 +01:00
|
|
|
$self->{conf_dir} = delete $conf{conf_dir} // "$ENV{HOME}/pbot/config";
|
|
|
|
$self->{data_dir} = delete $conf{data_dir} // "$ENV{HOME}/pbot/data";
|
|
|
|
$self->{module_dir} = delete $conf{module_dir} // "$ENV{HOME}/pbot/modules";
|
2010-06-18 12:46:23 +02:00
|
|
|
|
2011-01-30 04:22:00 +01:00
|
|
|
$self->{ircserver} = delete $conf{ircserver} // "irc.freenode.net";
|
|
|
|
$self->{botnick} = delete $conf{botnick} // "pbot3";
|
|
|
|
$self->{username} = delete $conf{username} // "pbot3";
|
|
|
|
$self->{ircname} = delete $conf{ircname} // "http://code.google.com/p/pbot2-pl/";
|
|
|
|
$self->{identify_password} = delete $conf{identify_password} // "";
|
2011-01-26 11:13:06 +01:00
|
|
|
|
2011-01-30 04:22:00 +01:00
|
|
|
$self->{max_msg_len} = delete $conf{max_msg_len} // 430;
|
|
|
|
$self->{MAX_FLOOD_MESSAGES} = delete $conf{MAX_FLOOD_MESSAGES} // 4;
|
2012-08-01 11:57:43 +02:00
|
|
|
$self->{MAX_NICK_MESSAGES} = delete $conf{MAX_NICK_MESSAGES} // 32;
|
2011-01-30 04:22:00 +01:00
|
|
|
|
|
|
|
$self->{trigger} = delete $conf{trigger} // '!';
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
my $channels_file = delete $conf{channels_file};
|
|
|
|
my $admins_file = delete $conf{admins_file};
|
2010-04-06 20:38:27 +02:00
|
|
|
my $ignorelist_file = delete $conf{ignorelist_file};
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
my $factoids_file = delete $conf{factoids_file};
|
|
|
|
my $export_factoids_path = delete $conf{export_factoids_path};
|
|
|
|
my $export_factoids_site = delete $conf{export_factoids_site};
|
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
my $quotegrabs_file = delete $conf{quotegrabs_file};
|
|
|
|
my $export_quotegrabs_path = delete $conf{export_quotegrabs_path};
|
|
|
|
my $export_quotegrabs_site = delete $conf{export_quotegrabs_site};
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{logger} = PBot::Logger->new(log_file => $log_file);
|
|
|
|
$self->{commands} = PBot::Commands->new(pbot => $self);
|
|
|
|
$self->{timer} = PBot::Timer->new(timeout => 10);
|
2010-03-23 19:24:02 +01:00
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{admins} = PBot::BotAdmins->new(pbot => $self, filename => $admins_file);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$self->admins->load_admins();
|
2011-01-26 10:40:08 +01:00
|
|
|
$self->admins->add_admin($self->{botnick}, '.*', "$self->{botnick}!stdin\@localhost", 60, 'admin');
|
|
|
|
$self->admins->login($self->{botnick}, "$self->{botnick}!stdin\@localhost", 'admin');
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$self->{factoids} = PBot::Factoids->new(
|
|
|
|
pbot => $self,
|
|
|
|
filename => $factoids_file,
|
|
|
|
export_path => $export_factoids_path,
|
|
|
|
export_site => $export_factoids_site,
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->factoids->load_factoids() if defined $factoids_file;
|
2011-01-26 10:40:08 +01:00
|
|
|
$self->factoids->add_factoid('text', '.*', $self->{botnick}, 'version', "/say $VERSION");
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2011-02-13 06:07:02 +01:00
|
|
|
$self->{bantracker} = PBot::BanTracker->new(pbot => $self);
|
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{lagchecker} = PBot::LagChecker->new(pbot => $self);
|
|
|
|
$self->{antiflood} = PBot::AntiFlood->new(pbot => $self);
|
2010-04-06 20:38:27 +02:00
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{ignorelist} = PBot::IgnoreList->new(pbot => $self, filename => $ignorelist_file);
|
2010-04-06 20:38:27 +02:00
|
|
|
$self->{ignorelist}->load_ignores() if defined $ignorelist_file;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$self->interpreter(PBot::Interpreter->new(pbot => $self));
|
|
|
|
$self->interpreter->register(sub { return $self->commands->interpreter(@_); });
|
|
|
|
$self->interpreter->register(sub { return $self->factoids->interpreter(@_); });
|
|
|
|
|
|
|
|
$self->{botadmincmds} = PBot::BotAdminCommands->new(pbot => $self);
|
|
|
|
$self->{factoidcmds} = PBot::FactoidCommands->new(pbot => $self);
|
|
|
|
$self->{ignorelistcmds} = PBot::IgnoreListCommands->new(pbot => $self);
|
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{irc} = PBot::IRC->new();
|
|
|
|
$self->{irchandlers} = PBot::IRCHandlers->new(pbot => $self);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{channels} = PBot::Channels->new(pbot => $self, filename => $channels_file);
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->channels->load_channels() if defined $channels_file;
|
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{chanops} = PBot::ChanOps->new(pbot => $self);
|
|
|
|
$self->{chanopcmds} = PBot::ChanOpCommands->new(pbot => $self);
|
2010-03-23 19:24:02 +01:00
|
|
|
|
2010-06-18 12:46:23 +02:00
|
|
|
$self->{chanops}->{unban_timeout}->load_hash();
|
|
|
|
|
2011-01-26 11:13:06 +01:00
|
|
|
$self->{quotegrabs} = PBot::Quotegrabs->new(
|
2010-03-23 19:24:02 +01:00
|
|
|
pbot => $self,
|
|
|
|
filename => $quotegrabs_file,
|
|
|
|
export_path => $export_quotegrabs_path,
|
|
|
|
export_site => $export_quotegrabs_site,
|
|
|
|
);
|
|
|
|
|
2011-01-26 10:40:08 +01:00
|
|
|
$self->quotegrabs->add_quotegrab($self->{botnick}, "#pbot2", 0, "pragma_", "Who's a bot?");
|
2010-03-23 19:24:02 +01:00
|
|
|
$self->quotegrabs->load_quotegrabs() if defined $quotegrabs_file;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$self->timer->start();
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
# TODO: add disconnect subroutine
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
sub connect {
|
|
|
|
my ($self, $server) = @_;
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$server = $self->ircserver if not defined $server;
|
|
|
|
|
|
|
|
if($self->{connected}) {
|
|
|
|
# TODO: disconnect, clean-up, etc
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->logger->log("Connecting to $server ...\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->conn($self->irc->newconn(
|
|
|
|
Nick => $self->{botnick},
|
|
|
|
Username => $self->{username},
|
|
|
|
Ircname => $self->{ircname},
|
2010-03-17 07:36:54 +01:00
|
|
|
Server => $server,
|
2010-03-22 08:33:44 +01:00
|
|
|
Port => $self->{port}))
|
|
|
|
or Carp::croak "$0: Can't connect to IRC server.\n";
|
|
|
|
|
|
|
|
$self->{connected} = 1;
|
|
|
|
|
|
|
|
#set up default handlers for the IRC engine
|
2011-02-13 06:07:02 +01:00
|
|
|
$self->conn->add_handler([ 251,252,253,254,302,255 ], sub { $self->irchandlers->on_init(@_) });
|
|
|
|
$self->conn->add_handler(376 , sub { $self->irchandlers->on_connect(@_) });
|
|
|
|
$self->conn->add_handler('disconnect' , sub { $self->irchandlers->on_disconnect(@_) });
|
|
|
|
$self->conn->add_handler('notice' , sub { $self->irchandlers->on_notice(@_) });
|
|
|
|
$self->conn->add_handler('caction' , sub { $self->irchandlers->on_action(@_) });
|
|
|
|
$self->conn->add_handler('public' , sub { $self->irchandlers->on_public(@_) });
|
|
|
|
$self->conn->add_handler('msg' , sub { $self->irchandlers->on_msg(@_) });
|
|
|
|
$self->conn->add_handler('mode' , sub { $self->irchandlers->on_mode(@_) });
|
|
|
|
$self->conn->add_handler('part' , sub { $self->irchandlers->on_departure(@_) });
|
|
|
|
$self->conn->add_handler('join' , sub { $self->irchandlers->on_join(@_) });
|
|
|
|
$self->conn->add_handler('quit' , sub { $self->irchandlers->on_departure(@_) });
|
|
|
|
$self->conn->add_handler('pong' , sub { $self->lagchecker->on_pong(@_) });
|
|
|
|
$self->conn->add_handler('whoisaccount' , sub { $self->antiflood->on_whoisaccount(@_) });
|
2011-12-11 23:56:57 +01:00
|
|
|
$self->conn->add_handler('banlist' , sub { $self->bantracker->on_banlist_entry(@_) });
|
|
|
|
# freenode quietlist
|
|
|
|
$self->conn->add_handler(728 , sub { $self->bantracker->on_quietlist_entry(@_) });
|
2011-02-13 06:07:02 +01:00
|
|
|
$self->conn->add_handler('endofnames' , sub { $self->bantracker->get_banlist(@_) });
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#main loop
|
|
|
|
sub do_one_loop {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
# process IRC events
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->irc->do_one_loop();
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
# process STDIN events
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->check_stdin();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if(not defined $self->{connected} or $self->{connected} == 0) {
|
|
|
|
$self->connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
$self->do_one_loop();
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub check_stdin {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
my $input = PBot::StdinReader::check_stdin();
|
|
|
|
|
|
|
|
return if not defined $input;
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
$self->logger->log("---------------------------------------------\n");
|
|
|
|
$self->logger->log("Read '$input' from STDIN\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
my ($from, $text);
|
|
|
|
|
|
|
|
if($input =~ m/^~([^ ]+)\s+(.*)/) {
|
|
|
|
$from = $1;
|
2011-01-30 04:22:00 +01:00
|
|
|
$text = "$self->{trigger}$2";
|
2010-03-17 07:36:54 +01:00
|
|
|
} else {
|
|
|
|
$from = undef;
|
2011-01-30 04:22:00 +01:00
|
|
|
$text = "$self->{trigger}$input";
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
return $self->interpreter->process_line($from, $self->{botnick}, "stdin", "localhost", $text);
|
|
|
|
}
|
|
|
|
|
2011-01-26 10:40:08 +01:00
|
|
|
#-----------------------------------------------------------------------------------
|
2011-01-25 00:56:55 +01:00
|
|
|
# Getters/Setters
|
2011-01-26 10:40:08 +01:00
|
|
|
#-----------------------------------------------------------------------------------
|
2011-01-25 00:56:55 +01:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub irc {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{irc};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub logger {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{logger} = shift; }
|
|
|
|
return $self->{logger};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub channels {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{channels} = shift; }
|
|
|
|
return $self->{channels};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub factoids {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{factoids} = shift; }
|
|
|
|
return $self->{factoids};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub timer {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{timer} = shift; }
|
|
|
|
return $self->{timer};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub conn {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{conn} = shift; }
|
|
|
|
return $self->{conn};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub irchandlers {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{irchandlers} = shift; }
|
|
|
|
return $self->{irchandlers};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub interpreter {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{interpreter} = shift; }
|
|
|
|
return $self->{interpreter};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub admins {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{admins} = shift; }
|
|
|
|
return $self->{admins};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub commands {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{commands} = shift; }
|
|
|
|
return $self->{commands};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub botnick {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{botnick} = shift; }
|
|
|
|
return $self->{botnick};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub identify_password {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{identify_password} = shift; }
|
|
|
|
return $self->{identify_password};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub max_msg_len {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{max_msg_len} = shift; }
|
|
|
|
return $self->{max_msg_len};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub module_dir {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{module_dir} = shift; }
|
|
|
|
return $self->{module_dir};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub ignorelist {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{ignorelist} = shift; }
|
|
|
|
return $self->{ignorelist};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2011-02-13 06:07:02 +01:00
|
|
|
sub bantracker {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{bantracker} = shift; }
|
|
|
|
return $self->{bantracker};
|
|
|
|
}
|
|
|
|
|
2011-01-25 00:56:55 +01:00
|
|
|
sub lagchecker {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{lagchecker} = shift; }
|
|
|
|
return $self->{lagchecker};
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub antiflood {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{antiflood} = shift; }
|
|
|
|
return $self->{antiflood};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2010-03-23 19:24:02 +01:00
|
|
|
sub quotegrabs {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{quotegrabs} = shift; }
|
|
|
|
return $self->{quotegrabs};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub chanops {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{chanops} = shift; }
|
|
|
|
return $self->{chanops};
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub ircserver {
|
|
|
|
my $self = shift;
|
|
|
|
if(@_) { $self->{ircserver} = shift; }
|
|
|
|
return $self->{ircserver};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|