3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 18:39:35 +01:00
pbot/PBot/PBot.pm
Pragmatic Software d955bfa06c Add centralized configuration registry module
Allows changing of bot configuration values without needing to restart
bot instance or needing to edit pbot.pl script.

Registry will initially be populated with default values from pbot.pl,
but if a registry file exists then the registry values will take
precedence over the pbot.pl values. For instance, if you regset the
bot trigger to '%' then the trigger will be '%' even if pbot.pl has '!'
or something else explicitly set.

Some registry items can have trigger hooks associated with them.  For
instance, the irc->botnick registry entry has a change_botnick_trigger
associated with it which changes the IRC nick on the server when a new
value is set via regset/regadd.

Tons of other fixes and improvements throughout.
2014-05-17 20:08:19 +00:00

369 lines
12 KiB
Perl

# File: PBot.pm
# Author: pragma_
#
# Purpose: IRC Bot (3rd generation)
# $Id$
package PBot::PBot;
use strict;
use warnings;
use PBot::VERSION;
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;
print "PBot version $VERSION\n";
}
# unbuffer stdout
STDOUT->autoflush(1);
use Carp ();
use PBot::Logger;
use PBot::Registry;
use PBot::SelectHandler;
use PBot::StdinReader;
use PBot::IRC;
use PBot::IRCHandlers;
use PBot::Channels;
use PBot::BanTracker;
use PBot::LagChecker;
use PBot::MessageHistory;
use PBot::AntiFlood;
use PBot::Interpreter;
use PBot::Commands;
use PBot::ChanOps;
use PBot::ChanOpCommands;
use PBot::Factoids;
use PBot::FactoidCommands;
use PBot::BotAdmins;
use PBot::BotAdminCommands;
use PBot::IgnoreList;
use PBot::IgnoreListCommands;
use PBot::Quotegrabs;
# no PBot::QuotegrabsCommands (bundled inside PBot::Quotegrabs for a change)
use PBot::Timer;
sub new {
if(ref($_[1]) eq 'HASH') {
Carp::croak("Options to PBot should be key/value pairs, not hash reference");
}
my ($class, %conf) = @_;
my $self = bless {}, $class;
$self->initialize(%conf);
$self->register_signal_handlers;
return $self;
}
sub initialize {
my ($self, %conf) = @_;
# logger created first to allow other modules to log things
$self->{logger} = PBot::Logger->new(log_file => delete $conf{log_file});
$self->{atexit} = PBot::Registerable->new();
$self->{timer} = PBot::Timer->new(timeout => 10);
$self->{commands} = PBot::Commands->new(pbot => $self);
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
$self->{registry} = PBot::Registry->new(pbot => $self, filename => delete $conf{registry_file} // "$config_dir/registry");
$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} // '!');
$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}->set('irc', 'SSL_ca_file', 'private', 1);
$self->{registry}->add_default('text', 'irc', 'SSL_ca_path', delete $conf{SSL_ca_path} // 'none');
$self->{registry}->set('irc', 'SSL_ca_path', 'private', 1);
$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/");
$self->{registry}->add_default('text', 'irc', 'identify_password', delete $conf{identify_password} // "");
$self->{registry}->set('irc', 'identify_password', 'private', 1);
$self->{registry}->add_trigger('irc', 'botnick', sub { $self->change_botnick_trigger(@_) });
$self->{registry}->add_default('text', 'antiflood', 'max_join_flood', delete $conf{max_join_flood} // 4);
$self->{registry}->add_default('text', 'antiflood', 'max_chat_flood', delete $conf{max_chat_flood} // 4);
$self->{registry}->add_default('text', 'antiflood', 'max_enter_flood', delete $conf{max_enter_flood} // 4);
$self->{registry}->add_default('text', 'antiflood', 'max_nick_flood', delete $conf{max_nick_flood} // 3);
$self->{registry}->add_default('text', 'antiflood', 'enter_abuse_max_lines', delete $conf{enter_abuse_max_lines} // 4);
$self->{registry}->add_default('text', 'antiflood', 'enter_abuse_max_seconds', delete $conf{enter_abuse_max_seconds} // 20);
$self->{registry}->add_default('text', 'antiflood', 'enter_abuse_max_offenses', delete $conf{enter_abuse_max_offenses} // 3);
$self->{registry}->add_default('text', 'messagehistory', 'max_messages', delete $conf{max_messages} // 32);
$self->{select_handler} = PBot::SelectHandler->new(pbot => $self);
$self->{stdin_reader} = PBot::StdinReader->new(pbot => $self);
$self->{admins} = PBot::BotAdmins->new(pbot => $self, filename => delete $conf{admins_file});
$self->admins->load_admins();
$self->{factoids} = PBot::Factoids->new(
pbot => $self,
filename => delete $conf{factoids_file},
export_path => delete $conf{export_factoids_path},
export_site => delete $conf{export_factoids_site},
);
$self->{bantracker} = PBot::BanTracker->new(pbot => $self);
$self->{lagchecker} = PBot::LagChecker->new(pbot => $self);
$self->{messagehistory} = PBot::MessageHistory->new(pbot => $self, filename => delete $conf{messagehistory_file});
$self->{antiflood} = PBot::AntiFlood->new(pbot => $self);
$self->{ignorelist} = PBot::IgnoreList->new(pbot => $self, filename => delete $conf{ignorelist_file});
$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);
$self->{irc} = PBot::IRC->new();
$self->{irchandlers} = PBot::IRCHandlers->new(pbot => $self);
$self->{channels} = PBot::Channels->new(pbot => $self, filename => delete $conf{channels_file});
$self->{chanops} = PBot::ChanOps->new(pbot => $self);
$self->{chanopcmds} = PBot::ChanOpCommands->new(pbot => $self);
$self->{quotegrabs} = PBot::Quotegrabs->new(
pbot => $self,
filename => delete $conf{quotegrabs_file},
export_path => delete $conf{export_quotegrabs_path},
export_site => delete $conf{export_quotegrabs_site},
);
# 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');
$self->admins->add_admin($botnick, '.*', "$botnick!stdin\@localhost", 60, 'admin', 1);
$self->admins->login($botnick, "$botnick!stdin\@localhost", 'admin');
# start timer
$self->timer->start();
}
# TODO: add disconnect subroutine
sub connect {
my ($self, $server) = @_;
if($self->{connected}) {
# TODO: disconnect, clean-up, etc
}
$server = $self->{registry}->get_value('irc', 'ircserver') if not defined $server;
$self->logger->log("Connecting to $server ...\n");
$self->conn($self->irc->newconn(
Nick => $self->{registry}->get_value('irc', 'botnick'),
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')))
or Carp::croak "$0: Can't connect to IRC server.\n";
$self->{connected} = 1;
#set up default handlers for the IRC engine
$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('kick' , sub { $self->irchandlers->on_kick(@_) });
$self->conn->add_handler('quit' , sub { $self->irchandlers->on_departure(@_) });
$self->conn->add_handler('nick' , sub { $self->irchandlers->on_nickchange(@_) });
$self->conn->add_handler('pong' , sub { $self->lagchecker->on_pong(@_) });
$self->conn->add_handler('whoisaccount' , sub { $self->antiflood->on_whoisaccount(@_) });
$self->conn->add_handler('banlist' , sub { $self->bantracker->on_banlist_entry(@_) });
$self->conn->add_handler('endofnames' , sub { $self->bantracker->get_banlist(@_) });
# freenode quietlist
$self->conn->add_handler(728 , sub { $self->bantracker->on_quietlist_entry(@_) });
}
#main loop
sub do_one_loop {
my $self = shift;
# process IRC events
$self->irc->do_one_loop();
# process SelectHandler
$self->{select_handler}->do_select();
}
sub start {
my $self = shift;
if(not $self->{connected}) {
$self->connect();
}
while(1) {
$self->do_one_loop();
}
}
sub register_signal_handlers {
my $self = shift;
$SIG{INT} = sub { $self->atexit; exit 0; };
}
sub atexit {
my $self = shift;
$self->{atexit}->execute_all;
}
sub change_botnick_trigger {
my ($self, $section, $item, $newvalue) = @_;
if($self->{connected}) {
$self->conn->nick($newvalue);
}
}
#-----------------------------------------------------------------------------------
# Getters/Setters
#-----------------------------------------------------------------------------------
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 ignorelist {
my $self = shift;
if(@_) { $self->{ignorelist} = shift; }
return $self->{ignorelist};
}
sub bantracker {
my $self = shift;
if(@_) { $self->{bantracker} = shift; }
return $self->{bantracker};
}
sub lagchecker {
my $self = shift;
if(@_) { $self->{lagchecker} = shift; }
return $self->{lagchecker};
}
sub antiflood {
my $self = shift;
if(@_) { $self->{antiflood} = shift; }
return $self->{antiflood};
}
sub quotegrabs {
my $self = shift;
if(@_) { $self->{quotegrabs} = shift; }
return $self->{quotegrabs};
}
sub chanops {
my $self = shift;
if(@_) { $self->{chanops} = shift; }
return $self->{chanops};
}
1;