3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 22:09:26 +01:00

Move command modules out of PBot.pm and into their respective modules

E.g., FactoidCommands.pm is now loaded inside Factoids.pm, etc.

Misc reformatting/refactoring of PBot.pm
This commit is contained in:
Pragmatic Software 2014-05-18 00:27:57 +00:00
parent d955bfa06c
commit e07df35cbf
7 changed files with 66 additions and 91 deletions

View File

@ -362,7 +362,7 @@ sub check_flood {
my $length = $channel_data->{offenses} ** $channel_data->{offenses} * $channel_data->{offenses} * 30;
$self->{pbot}->{ignorelistcmds}->ignore_user("", "floodcontrol", "", "", "$nick!$user\@$host $channel $length");
$self->{pbot}->ignorelist->{commands}->ignore_user("", "floodcontrol", "", "", "$nick!$user\@$host $channel $length");
$length = duration($length);
$self->{pbot}->logger->log("$nick msg flood offense " . $channel_data->{offenses} . " earned $length ignore\n");
$self->{pbot}->conn->privmsg($nick, "You have used too many commands in too short a time period, you have been ignored for $length.");

View File

@ -9,12 +9,13 @@ use warnings;
use strict;
use PBot::DualIndexHashObject;
use PBot::BotAdminCommands;
use Carp ();
sub new {
if(ref($_[1]) eq 'HASH') {
Carp::croak("Options to BotAdmins should be key/value pairs, not hash reference");
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
}
my ($class, %conf) = @_;
@ -27,16 +28,11 @@ sub new {
sub initialize {
my ($self, %conf) = @_;
my $filename = delete $conf{filename};
my $export_path = delete $conf{export_path};
my $export_site = delete $conf{export_site};
my $pbot = delete $conf{pbot};
if(not defined $pbot) {
Carp::croak("Missing pbot reference to BotAdmins");
}
my $filename = delete $conf{filename};
my $export_path = delete $conf{export_path};
my $export_site = delete $conf{export_site};
my $export_timeout = delete $conf{export_timeout};
if(not defined $export_timeout) {
if(defined $export_path) {
$export_timeout = 300; # every 5 minutes
@ -45,12 +41,14 @@ sub initialize {
}
}
$self->{admins} = PBot::DualIndexHashObject->new(name => 'Admins', filename => $filename);
$self->{export_path} = $export_path;
$self->{export_site} = $export_site;
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
$self->{admins} = PBot::DualIndexHashObject->new(name => 'Admins', filename => $filename);
$self->{commands} = PBot::BotAdminCommands->new(pbot => $self->{pbot});
$self->{export_path} = $export_path;
$self->{export_site} = $export_site;
$self->{export_timeout} = $export_timeout;
$self->{pbot} = $pbot;
$self->load_admins;
}
sub add_admin {

View File

@ -8,6 +8,7 @@ package PBot::ChanOps;
use warnings;
use strict;
use PBot::ChanOpCommands;
use Time::HiRes qw(gettimeofday);
sub new {
@ -38,6 +39,8 @@ sub initialize {
$self->{op_commands} = {};
$self->{is_opped} = {};
$self->{commands} = PBot::ChanOpCommands->new(pbot => $pbot);
$pbot->timer->register(sub { $self->check_opped_timeouts }, 10);
$pbot->timer->register(sub { $self->check_unban_timeouts }, 10);
}

View File

@ -14,6 +14,7 @@ use Carp ();
use POSIX qw(strftime);
use PBot::PBot qw($VERSION);
use PBot::FactoidCommands;
use PBot::FactoidModuleLauncher;
use PBot::DualIndexHashObject;
@ -32,17 +33,18 @@ sub new {
sub initialize {
my ($self, %conf) = @_;
my $filename = delete $conf{filename};
my $filename = delete $conf{filename};
my $export_path = delete $conf{export_path};
my $export_site = delete $conf{export_site};
my $pbot = delete $conf{pbot} // Carp::croak("Missing pbot reference to Factoids");
$self->{factoids} = PBot::DualIndexHashObject->new(name => 'Factoids', filename => $filename);
$self->{factoids} = PBot::DualIndexHashObject->new(name => 'Factoids', filename => $filename);
$self->{export_path} = $export_path;
$self->{export_site} = $export_site;
$self->{pbot} = $pbot;
$self->{pbot} = $pbot;
$self->{commands} = PBot::FactoidCommands->new(pbot => $pbot);
$self->{factoidmodulelauncher} = PBot::FactoidModuleLauncher->new(pbot => $pbot);
$self->{pbot}->{atexit}->register(sub { $self->save_factoids; return; });
@ -341,7 +343,7 @@ sub interpreter {
# if a non-nick argument was supplied, e.g., a sentence using the bot's nick, don't say anything
return "" if length $arguments and $arguments !~ /^[^.+-, ]{1,20}$/;
my $matches = $self->{pbot}->{factoidcmds}->factfind($from, $nick, $user, $host, quotemeta $original_keyword);
my $matches = $self->{commands}->factfind($from, $nick, $user, $host, quotemeta $original_keyword);
# found factfind matches
if($matches !~ m/^No factoids/) {

View File

@ -8,6 +8,7 @@ package PBot::IgnoreList;
use warnings;
use strict;
use PBot::IgnoreListCommands;
use Time::HiRes qw(gettimeofday);
sub new {
@ -28,9 +29,11 @@ sub initialize {
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to Channels");
$self->{filename} = delete $conf{filename};
$self->{ignore_list} = {};
$self->{ignore_list} = {};
$self->{ignore_flood_counter} = {};
$self->{last_timestamp} = {};
$self->{last_timestamp} = {};
$self->{commands} = PBot::IgnoreListCommands->new(pbot => $self->{pbot});
$self->load_ignores;
@ -146,7 +149,7 @@ sub check_ignore {
}
if(exists $self->{ignore_flood_counter}->{$channel} and $self->{ignore_flood_counter}->{$channel} > 5) {
$self->{pbot}->{ignorelistcmds}->ignore_user("", "floodcontrol", "", "", ".* $channel 300");
$self->{commands}->ignore_user("", "floodcontrol", "", "", ".* $channel 300");
$self->{ignore_flood_counter}->{$channel} = 0;
if($channel =~ /^#/) {
$pbot->conn->me($channel, "has been overwhelmed.");
@ -183,7 +186,7 @@ sub check_ignore_timeouts {
next if($self->{ignore_list}->{$hostmask}{$channel} == -1); #permanent ignore
if($self->{ignore_list}->{$hostmask}{$channel} < $now) {
$self->{pbot}->{ignorelistcmds}->unignore_user("", "floodcontrol", "", "", "$hostmask $channel");
$self->{commands}->unignore_user("", "floodcontrol", "", "", "$hostmask $channel");
if($hostmask eq ".*") {
$self->{pbot}->conn->me($channel, "awakens.");
}

View File

@ -26,40 +26,23 @@ 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 {
@ -89,43 +72,53 @@ sub initialize {
# 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', '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}->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/");
$self->{registry}->add_default('text', 'irc', 'identify_password', delete $conf{identify_password} // "");
$self->{registry}->set('irc', 'SSL_ca_file', 'private', 1);
$self->{registry}->set('irc', 'SSL_ca_path', 'private', 1);
$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', '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->{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->{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->{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->{admins} = PBot::BotAdmins->new(pbot => $self, filename => delete $conf{admins_file});
$self->admins->load_admins();
$self->interpreter(PBot::Interpreter->new(pbot => $self));
$self->interpreter->register(sub { return $self->commands->interpreter(@_); });
$self->interpreter->register(sub { return $self->factoids->interpreter(@_); });
$self->{factoids} = PBot::Factoids->new(
pbot => $self,
@ -134,31 +127,7 @@ sub initialize {
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(
$self->{quotegrabs} = PBot::Quotegrabs->new(
pbot => $self,
filename => delete $conf{quotegrabs_file},
export_path => delete $conf{export_quotegrabs_path},

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 582,
BUILD_REVISION => 583,
BUILD_DATE => "2014-05-17",
};