2015-09-07 07:52:39 +02:00
|
|
|
# File: Plugins.pm
|
2015-09-07 07:17:07 +02:00
|
|
|
#
|
2021-06-19 06:23:34 +02:00
|
|
|
# Purpose: Loads and manages external plugins.
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::Plugins;
|
|
|
|
use parent 'PBot::Core::Class';
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2015-09-07 07:17:07 +02:00
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-07-14 04:45:56 +02:00
|
|
|
|
|
|
|
# loaded plugins
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{plugins} = {};
|
2021-07-14 04:45:56 +02:00
|
|
|
|
|
|
|
# autoload plugins listed in `$data_dir/plugins_autoload` file
|
2021-06-05 22:20:03 +02:00
|
|
|
$self->autoload(%conf);
|
2020-05-04 22:21:35 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 07:17:07 +02:00
|
|
|
sub autoload {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-07-14 04:45:56 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return if $self->{pbot}->{registry}->get_value('plugins', 'noautoload');
|
2019-09-01 20:01:18 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $data_dir = $self->{pbot}->{registry}->get_value('general', 'data_dir');
|
2019-09-01 20:01:18 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Loading plugins ...\n");
|
2021-07-14 04:45:56 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $plugin_count = 0;
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $fh;
|
|
|
|
if (not open $fh, "<$data_dir/plugin_autoload") {
|
|
|
|
$self->{pbot}->{logger}->log("warning: file $data_dir/plugin_autoload does not exist; skipping autoloading of Plugins\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chomp(my @plugins = <$fh>);
|
|
|
|
close $fh;
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $plugin (sort @plugins) {
|
|
|
|
# do not load plugins that begin with a comment
|
|
|
|
next if $plugin =~ m/^\s*#/;
|
2020-05-18 18:54:55 +02:00
|
|
|
next if not length $plugin;
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2020-05-18 18:54:55 +02:00
|
|
|
$plugin = basename $plugin;
|
|
|
|
$plugin =~ s/.pm$//;
|
2020-02-15 23:38:32 +01:00
|
|
|
$plugin_count++ if $self->load($plugin, %conf);
|
|
|
|
}
|
2021-07-14 04:45:56 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("$plugin_count plugin" . ($plugin_count == 1 ? '' : 's') . " loaded.\n");
|
2015-09-07 07:17:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub load {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $plugin, %conf) = @_;
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->unload($plugin);
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return if $self->{pbot}->{registry}->get_value('plugins', 'disabled');
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
my $module = "PBot/Plugin/$plugin.pm";
|
2019-09-01 20:01:18 +02:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
$self->{pbot}->{refresher}->{refresher}->refresh_module($module);
|
2019-09-01 20:01:18 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $ret = eval {
|
|
|
|
$self->{pbot}->{logger}->log("Loading $plugin\n");
|
2021-07-21 21:43:30 +02:00
|
|
|
require "$module";
|
2021-07-14 04:45:56 +02:00
|
|
|
my $class = "PBot::Plugin::$plugin";
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{plugins}->{$plugin} = $class->new(pbot => $self->{pbot}, %conf);
|
2021-07-14 04:45:56 +02:00
|
|
|
$self->{pbot}->{refresher}->{refresher}->update_cache($module);
|
2020-02-15 23:38:32 +01:00
|
|
|
return 1;
|
|
|
|
};
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
if (my $exception = $@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error loading $plugin: $exception");
|
2020-02-15 23:38:32 +01:00
|
|
|
return 0;
|
2015-09-07 07:17:07 +02:00
|
|
|
}
|
2021-07-14 04:45:56 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $ret;
|
2015-09-07 07:17:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unload {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $plugin) = @_;
|
|
|
|
|
|
|
|
if (exists $self->{plugins}->{$plugin}) {
|
|
|
|
eval {
|
|
|
|
$self->{plugins}->{$plugin}->unload;
|
|
|
|
delete $self->{plugins}->{$plugin};
|
|
|
|
};
|
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
if (my $exception = $@) {
|
|
|
|
$self->{pbot}->{logger}->log("Warning: got error unloading plugin $plugin: $exception");
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
my $module = "PBot/Plugin/$plugin.pm";
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->unload_module($module);
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Plugin $plugin unloaded.\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2015-09-07 07:17:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|