diff --git a/PBot/PBot.pm b/PBot/PBot.pm index 170fd6aa..4922e893 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -49,6 +49,7 @@ use PBot::Quotegrabs; use PBot::Timer; use PBot::AntiAway; use PBot::AntiKickAutoRejoin; +use PBot::Refresher; sub new { if(ref($_[1]) eq 'HASH') { @@ -72,6 +73,8 @@ sub initialize { $self->{timer} = PBot::Timer->new(timeout => 10, %conf); $self->{commands} = PBot::Commands->new(pbot => $self, %conf); + $self->{refresher} = PBot::Refresher->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 diff --git a/PBot/Refresher.pm b/PBot/Refresher.pm new file mode 100644 index 00000000..3035bcbd --- /dev/null +++ b/PBot/Refresher.pm @@ -0,0 +1,59 @@ +# File: Refresher.pm +# Author: pragma_ +# +# Purpose: Refreshes/reloads module subroutines. Does not refresh/reload +# module member data, only subroutines. + +package PBot::Refresher; + +use warnings; +use strict; + +use feature 'switch'; + +use Module::Refresh; +use Carp (); + +sub new { + if(ref($_[1]) eq 'HASH') { + Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference"); + } + + my ($class, %conf) = @_; + + my $self = bless {}, $class; + $self->initialize(%conf); + return $self; +} + +sub initialize { + my ($self, %conf) = @_; + + my $pbot = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); + $self->{pbot} = $pbot; + + $self->{refresher} = Module::Refresh->new; + + $pbot->{commands}->register(sub { return $self->refresh(@_) }, "refresh", 90); +} + +sub refresh { + my ($self, $from, $nick, $user, $host, $arguments) = @_; + + eval { + if (not $arguments) { + $self->{pbot}->{logger}->log("Refreshing all modified modules\n"); + $self->{refresher}->refresh; + } else { + $self->{pbot}->{logger}->log("Refreshing module $arguments\n"); + if ($self->{refresher}->refresh_module_if_modified($arguments)) { + $self->{pbot}->{logger}->log("Refreshed module.\n"); + } else { + $self->{pbot}->{logger}->log("Module had no changes; not refreshed.\n"); + } + } + }; + $self->{pbot}->{logger}->log("Error refreshing: $@\n") if $@; +} + +1; diff --git a/doc/MODULES b/doc/MODULES index 5000892b..9af10f8f 100644 --- a/doc/MODULES +++ b/doc/MODULES @@ -14,6 +14,7 @@ LWP::Simple LWP::UserAgent LWP::UserAgent::WithCache Math::Units +Module::Refresh Net::Dict Proc::ProcessTable SOAP::Lite