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
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
BEGIN {
|
|
|
|
use Exporter;
|
|
|
|
our @ISA = 'Exporter';
|
|
|
|
our @EXPORT = qw($VERSION);
|
|
|
|
|
|
|
|
our $VERSION = PBot::VERSION::BUILD_NAME . " revision " . PBot::VERSION::BUILD_REVISION . " " . PBot::VERSION::BUILD_DATE;
|
2014-05-20 00:59:51 +02:00
|
|
|
print "$VERSION\n";
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
# unbuffer stdout
|
|
|
|
STDOUT->autoflush(1);
|
|
|
|
|
|
|
|
use Carp ();
|
|
|
|
use PBot::Logger;
|
2014-05-17 22:08:19 +02:00
|
|
|
use PBot::Registry;
|
2014-03-14 06:51:15 +01:00
|
|
|
use PBot::SelectHandler;
|
2010-03-17 07:36:54 +01:00
|
|
|
use PBot::StdinReader;
|
2011-01-22 09:35:31 +01:00
|
|
|
use PBot::IRC;
|
2014-11-01 01:15:21 +01:00
|
|
|
use PBot::EventDispatcher;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::IRCHandlers;
|
2010-03-23 19:24:02 +01:00
|
|
|
use PBot::Channels;
|
2011-02-13 06:07:02 +01:00
|
|
|
use PBot::BanTracker;
|
2014-11-15 02:18:33 +01:00
|
|
|
use PBot::NickList;
|
2011-01-25 00:56:55 +01:00
|
|
|
use PBot::LagChecker;
|
2014-05-13 12:15:52 +02:00
|
|
|
use PBot::MessageHistory;
|
2010-03-17 07:36:54 +01:00
|
|
|
use PBot::AntiFlood;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::Interpreter;
|
|
|
|
use PBot::Commands;
|
|
|
|
use PBot::ChanOps;
|
|
|
|
use PBot::Factoids;
|
|
|
|
use PBot::BotAdmins;
|
|
|
|
use PBot::IgnoreList;
|
2015-03-17 05:08:25 +01:00
|
|
|
use PBot::BlackList;
|
2010-03-22 08:33:44 +01:00
|
|
|
use PBot::Timer;
|
2015-06-16 04:55:46 +02:00
|
|
|
use PBot::Refresher;
|
2015-09-07 07:52:39 +02:00
|
|
|
use PBot::Plugins;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
2014-05-06 07:15:27 +02:00
|
|
|
Carp::croak("Options to PBot should be key/value pairs, not hash reference");
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
2014-05-17 00:11:31 +02:00
|
|
|
$self->register_signal_handlers;
|
2010-03-22 08:33:44 +01:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
# logger created first to allow other modules to log things
|
2014-05-20 00:59:51 +02:00
|
|
|
$self->{logger} = PBot::Logger->new(log_file => delete $conf{log_file}, %conf);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2014-05-20 00:59:51 +02:00
|
|
|
$self->{atexit} = PBot::Registerable->new(%conf);
|
|
|
|
$self->{timer} = PBot::Timer->new(timeout => 10, %conf);
|
|
|
|
$self->{commands} = PBot::Commands->new(pbot => $self, %conf);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2015-06-16 04:55:46 +02:00
|
|
|
$self->{refresher} = PBot::Refresher->new(pbot => $self);
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
my $config_dir = delete $conf{config_dir} // "$ENV{HOME}/pbot/config";
|
|
|
|
|
|
|
|
# registry created, but not yet loaded, to allow modules to create default values and triggers
|
2014-05-20 00:59:51 +02:00
|
|
|
$self->{registry} = PBot::Registry->new(pbot => $self, filename => delete $conf{registry_file} // "$config_dir/registry", %conf);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{registry}->add_default('text', 'general', 'config_dir', $config_dir);
|
|
|
|
$self->{registry}->add_default('text', 'general', 'data_dir', delete $conf{data_dir} // "$ENV{HOME}/pbot/data");
|
|
|
|
$self->{registry}->add_default('text', 'general', 'module_dir', delete $conf{module_dir} // "$ENV{HOME}/pbot/modules");
|
|
|
|
$self->{registry}->add_default('text', 'general', 'trigger', delete $conf{trigger} // '!');
|
|
|
|
|
2014-05-31 03:03:42 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'debug', delete $conf{irc_debug} // 0);
|
2014-08-11 09:34:30 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'show_motd', delete $conf{show_motd} // 1);
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'max_msg_len', delete $conf{max_msg_len} // 425);
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'ircserver', delete $conf{ircserver} // "irc.freenode.net");
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'port', delete $conf{port} // 6667);
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'SSL', delete $conf{SSL} // 0);
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'SSL_ca_file', delete $conf{SSL_ca_file} // 'none');
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'SSL_ca_path', delete $conf{SSL_ca_path} // 'none');
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'botnick', delete $conf{botnick} // "pbot3");
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'username', delete $conf{username} // "pbot3");
|
|
|
|
$self->{registry}->add_default('text', 'irc', 'ircname', delete $conf{ircname} // "http://code.google.com/p/pbot2-pl/");
|
2014-05-18 02:30:51 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'identify_password', delete $conf{identify_password} // 'none');
|
2014-08-11 09:34:30 +02:00
|
|
|
$self->{registry}->add_default('text', 'irc', 'log_default_handler', 1);
|
2014-05-18 22:09:05 +02:00
|
|
|
|
2015-07-06 02:48:27 +02:00
|
|
|
$self->{registry}->set_default('irc', 'SSL_ca_file', 'private', 1);
|
|
|
|
$self->{registry}->set_default('irc', 'SSL_ca_path', 'private', 1);
|
|
|
|
$self->{registry}->set_default('irc', 'identify_password', 'private', 1);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
|
|
|
$self->{registry}->add_trigger('irc', 'botnick', sub { $self->change_botnick_trigger(@_) });
|
2014-05-31 03:03:42 +02:00
|
|
|
$self->{registry}->add_trigger('irc', 'debug', sub { $self->irc_debug_trigger(@_) });
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{event_dispatcher} = PBot::EventDispatcher->new(pbot => $self, %conf);
|
2016-09-22 17:26:42 +02:00
|
|
|
$self->{irchandlers} = PBot::IRCHandlers->new(pbot => $self, %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{select_handler} = PBot::SelectHandler->new(pbot => $self, %conf);
|
|
|
|
$self->{stdin_reader} = PBot::StdinReader->new(pbot => $self, %conf);
|
|
|
|
$self->{admins} = PBot::BotAdmins->new(pbot => $self, filename => delete $conf{admins_file}, %conf);
|
|
|
|
$self->{bantracker} = PBot::BanTracker->new(pbot => $self, %conf);
|
|
|
|
$self->{lagchecker} = PBot::LagChecker->new(pbot => $self, %conf);
|
|
|
|
$self->{messagehistory} = PBot::MessageHistory->new(pbot => $self, filename => delete $conf{messagehistory_file}, %conf);
|
|
|
|
$self->{antiflood} = PBot::AntiFlood->new(pbot => $self, %conf);
|
|
|
|
$self->{ignorelist} = PBot::IgnoreList->new(pbot => $self, filename => delete $conf{ignorelist_file}, %conf);
|
2015-03-17 05:08:25 +01:00
|
|
|
$self->{blacklist} = PBot::BlackList->new(pbot => $self, filename => delete $conf{blacklist_file}, %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
$self->{irc} = PBot::IRC->new();
|
|
|
|
$self->{channels} = PBot::Channels->new(pbot => $self, filename => delete $conf{channels_file}, %conf);
|
|
|
|
$self->{chanops} = PBot::ChanOps->new(pbot => $self, %conf);
|
2016-09-22 17:26:42 +02:00
|
|
|
$self->{nicklist} = PBot::NickList->new(pbot => $self, %conf);
|
2014-12-28 01:44:15 +01:00
|
|
|
|
|
|
|
$self->{interpreter} = PBot::Interpreter->new(pbot => $self, %conf);
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{interpreter}->register(sub { return $self->{commands}->interpreter(@_); });
|
|
|
|
$self->{interpreter}->register(sub { return $self->{factoids}->interpreter(@_); });
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-18 02:27:57 +02:00
|
|
|
$self->{factoids} = PBot::Factoids->new(
|
2014-05-19 00:51:37 +02:00
|
|
|
pbot => $self,
|
|
|
|
filename => delete $conf{factoids_file},
|
|
|
|
export_path => delete $conf{export_factoids_path},
|
|
|
|
export_site => delete $conf{export_factoids_site},
|
2014-05-20 00:59:51 +02:00
|
|
|
%conf
|
2014-05-18 02:27:57 +02:00
|
|
|
);
|
2010-03-23 19:24:02 +01:00
|
|
|
|
2015-09-07 07:52:39 +02:00
|
|
|
$self->{plugins} = PBot::Plugins->new(pbot => $self, %conf);
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
# load registry entries from file to overwrite defaults
|
|
|
|
$self->{registry}->load;
|
|
|
|
|
|
|
|
# create implicit bot-admin account for bot
|
|
|
|
my $botnick = $self->{registry}->get_value('irc', 'botnick');
|
2015-05-12 06:28:36 +02:00
|
|
|
$self->{admins}->add_admin($botnick, '.*', "$botnick!stdin\@localhost", 90, 'admin', 1);
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{admins}->login($botnick, "$botnick!stdin\@localhost", 'admin');
|
2014-05-17 22:08:19 +02:00
|
|
|
|
|
|
|
# start timer
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{timer}->start();
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2015-01-11 00:56:43 +01:00
|
|
|
sub random_nick {
|
|
|
|
my @chars = ("A".."Z", "a".."z", "0".."9");
|
2015-02-14 13:01:09 +01:00
|
|
|
my $nick = $chars[rand @chars - 10]; # nicks cannot start with a digit
|
|
|
|
$nick .= $chars[rand @chars] for 1..9;
|
2015-01-11 00:56:43 +01:00
|
|
|
return $nick;
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-01-11 00:56:43 +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
|
|
|
if($self->{connected}) {
|
|
|
|
# TODO: disconnect, clean-up, etc
|
|
|
|
}
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$server = $self->{registry}->get_value('irc', 'ircserver') if not defined $server;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{logger}->log("Connecting to $server ...\n");
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2014-11-01 01:15:21 +01:00
|
|
|
while (not $self->{conn} = $self->{irc}->newconn(
|
2015-01-11 00:56:43 +01:00
|
|
|
Nick => random_nick,
|
2014-11-01 01:15:21 +01:00
|
|
|
Username => $self->{registry}->get_value('irc', 'username'),
|
|
|
|
Ircname => $self->{registry}->get_value('irc', 'ircname'),
|
|
|
|
Server => $server,
|
|
|
|
SSL => $self->{registry}->get_value('irc', 'SSL'),
|
|
|
|
SSL_ca_file => $self->{registry}->get_value('irc', 'SSL_ca_file'),
|
|
|
|
SSL_ca_path => $self->{registry}->get_value('irc', 'SSL_ca_path'),
|
|
|
|
Port => $self->{registry}->get_value('irc', 'port'))) {
|
2015-01-23 14:35:06 +01:00
|
|
|
$self->{logger}->log("$0: Can't connect to $server:" . $self->{registry}->get_value('irc', 'port') . ". Retrying in 15 seconds...\n");
|
2014-11-01 01:15:21 +01:00
|
|
|
sleep 15;
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$self->{connected} = 1;
|
|
|
|
|
2014-08-11 09:34:30 +02:00
|
|
|
#set up handlers for the IRC engine
|
|
|
|
$self->{conn}->add_default_handler(sub { $self->{irchandlers}->default_handler(@_) }, 1);
|
2014-11-01 01:15:21 +01:00
|
|
|
$self->{conn}->add_handler([ 251,252,253,254,255,302 ], sub { $self->{irchandlers}->on_init(@_) });
|
2014-08-11 09:34:30 +02:00
|
|
|
|
|
|
|
# ignore these events
|
2016-02-10 12:42:42 +01:00
|
|
|
$self->{conn}->add_handler(['whoisserver',
|
2014-08-11 09:34:30 +02:00
|
|
|
'whoiscountry',
|
|
|
|
'whoischannels',
|
|
|
|
'whoisidle',
|
|
|
|
'motdstart',
|
2014-11-01 01:15:21 +01:00
|
|
|
'endofmotd',
|
2014-08-11 09:34:30 +02:00
|
|
|
'away',
|
|
|
|
'endofbanlist'], sub {});
|
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;
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{irc}->do_one_loop();
|
2014-03-14 06:51:15 +01:00
|
|
|
$self->{select_handler}->do_select();
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub start {
|
|
|
|
my $self = shift;
|
2014-11-01 01:15:21 +01:00
|
|
|
while(1) {
|
|
|
|
$self->connect() if not $self->{connected};
|
|
|
|
$self->do_one_loop() if $self->{connected};
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2014-05-17 00:11:31 +02:00
|
|
|
sub register_signal_handlers {
|
|
|
|
my $self = shift;
|
|
|
|
$SIG{INT} = sub { $self->atexit; exit 0; };
|
|
|
|
}
|
|
|
|
|
|
|
|
sub atexit {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{atexit}->execute_all;
|
|
|
|
}
|
|
|
|
|
2014-05-31 03:03:42 +02:00
|
|
|
sub irc_debug_trigger {
|
|
|
|
my ($self, $section, $item, $newvalue) = @_;
|
|
|
|
$self->{irc}->debug($newvalue);
|
|
|
|
$self->{conn}->debug($newvalue) if $self->{connected};
|
|
|
|
}
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
sub change_botnick_trigger {
|
|
|
|
my ($self, $section, $item, $newvalue) = @_;
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{conn}->nick($newvalue) if $self->{connected};
|
2010-03-23 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
1;
|