diff --git a/PBot/BotAdminCommands.pm b/PBot/BotAdminCommands.pm index accf1e88..8befc59b 100644 --- a/PBot/BotAdminCommands.pm +++ b/PBot/BotAdminCommands.pm @@ -163,13 +163,7 @@ sub ack_die { my $self = shift; my ($from, $nick, $user, $host, $arguments) = @_; $self->{pbot}->logger->log("$nick!$user\@$host made me exit.\n"); - - # TODO: move all of those to an registerable atexit handler - $self->{pbot}->factoids->save_factoids; - $self->{pbot}->ignorelist->save_ignores; - $self->{pbot}->{quotegrabs}->{database}->end(); - $self->{pbot}->{messagehistory}->{database}->end(); - + $self->{pbot}->atexit(); $self->{pbot}->conn->privmsg($from, "Good-bye.") if defined $from; $self->{pbot}->conn->quit("Departure requested."); exit 0; diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 669edab4..8d6a8e1d 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -46,6 +46,8 @@ sub initialize { $self->{pbot} = $pbot; $self->{factoidmodulelauncher} = PBot::FactoidModuleLauncher->new(pbot => $pbot); + + $self->{pbot}->{atexit}->register(sub { $self->save_factoids; return; }); } sub load_factoids { diff --git a/PBot/MessageHistory.pm b/PBot/MessageHistory.pm index 3a9cd81a..0c91f2b9 100644 --- a/PBot/MessageHistory.pm +++ b/PBot/MessageHistory.pm @@ -45,6 +45,8 @@ sub initialize { $self->{MSG_NICKCHANGE} = 3; # CHANGED NICK $self->{pbot}->commands->register(sub { $self->recall_message(@_) }, "recall", 0); + + $self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; }); } sub get_message_account { diff --git a/PBot/PBot.pm b/PBot/PBot.pm index 54945f79..4b88a585 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -60,9 +60,9 @@ sub new { } my ($class, %conf) = @_; - my $self = bless {}, $class; $self->initialize(%conf); + $self->register_signal_handlers; return $self; } @@ -108,6 +108,8 @@ sub initialize { $self->{commands} = PBot::Commands->new(pbot => $self); $self->{timer} = PBot::Timer->new(timeout => 10); + $self->{atexit} = PBot::Registerable->new(); + $self->{select_handler} = PBot::SelectHandler->new(pbot => $self); $self->{stdin_reader} = PBot::StdinReader->new(pbot => $self); @@ -235,6 +237,16 @@ sub start { } } +sub register_signal_handlers { + my $self = shift; + $SIG{INT} = sub { $self->atexit; exit 0; }; +} + +sub atexit { + my $self = shift; + $self->{atexit}->execute_all; +} + #----------------------------------------------------------------------------------- # Getters/Setters #----------------------------------------------------------------------------------- diff --git a/PBot/Quotegrabs.pm b/PBot/Quotegrabs.pm index 8993d28b..e36e9fe0 100644 --- a/PBot/Quotegrabs.pm +++ b/PBot/Quotegrabs.pm @@ -45,6 +45,8 @@ sub initialize { #$self->{database} = PBot::Quotegrabs_Hashtable->new(pbot => $self->{pbot}, filename => $self->{filename}); $self->{database}->begin(); + $self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; }); + #------------------------------------------------------------------------------------- # The following could be in QuotegrabsCommands.pm, or they could be kept in here? #------------------------------------------------------------------------------------- diff --git a/PBot/Registerable.pm b/PBot/Registerable.pm index 5650b5d4..7e8903b4 100644 --- a/PBot/Registerable.pm +++ b/PBot/Registerable.pm @@ -19,7 +19,6 @@ sub new { } my ($class, %conf) = @_; - my $self = bless {}, $class; $self->initialize(%conf); return $self; @@ -27,7 +26,6 @@ sub new { sub initialize { my $self = shift; - $self->{handlers} = []; } @@ -67,7 +65,6 @@ sub register { Carp::croak("Must pass subroutine reference to register()"); } - # TODO: Check if subref already exists in handlers? my $ref = { subref => $subref }; push @{ $self->{handlers} }, $ref; diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 969bdf2b..e9674d9a 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 580, + BUILD_REVISION => 581, BUILD_DATE => "2014-05-16", };