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
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2015-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# 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;
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub initialize($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
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub autoload($self, %conf) {
|
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
|
|
|
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") {
|
2021-07-27 21:01:42 +02:00
|
|
|
$self->{pbot}->{logger}->log("Plugins: autoload: file $data_dir/plugin_autoload does not exist; skipping autoloading of Plugins\n");
|
2020-02-15 23:38:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
chomp(my @plugins = <$fh>);
|
|
|
|
close $fh;
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2021-07-27 21:01:42 +02:00
|
|
|
$self->{pbot}->{logger}->log("Loading plugins:\n");
|
|
|
|
|
|
|
|
$conf{quiet} = 1;
|
|
|
|
|
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$//;
|
2021-07-27 21:01:42 +02:00
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log(" $plugin\n");
|
|
|
|
|
2021-07-31 21:05:48 +02:00
|
|
|
if ($self->load($plugin, %conf)) {
|
2021-08-02 00:53:58 +02:00
|
|
|
$plugin_count++;
|
2021-07-31 21:05:48 +02:00
|
|
|
} else {
|
|
|
|
die "Plugin $plugin failed to autoload. You may remove it from $data_dir/plugin_autoload.\n";
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub load($self, $plugin, %conf) {
|
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 {
|
2021-07-27 21:01:42 +02:00
|
|
|
$self->{pbot}->{logger}->log("Loading $plugin\n") unless $conf{quiet};
|
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
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub unload($self, $plugin) {
|
2020-02-15 23:38:32 +01:00
|
|
|
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;
|