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
|
|
|
|
2015-09-07 07:52:39 +02:00
|
|
|
package PBot::Plugins;
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::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
|
|
|
|
|
|
|
# plugin management bot commands
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_plug(@_) }, "plug", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_unplug(@_) }, "unplug", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_replug(@_) }, "replug", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_pluglist(@_) }, "pluglist", 0);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_plug {
|
|
|
|
my ($self, $context) = @_;
|
2021-07-14 04:45:56 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
my $plugin = $context->{arguments};
|
|
|
|
|
|
|
|
if (not length $plugin) { return "Usage: plug <plugin>"; }
|
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
if ($self->load($plugin)) {
|
|
|
|
return "Loaded $plugin plugin.";
|
|
|
|
} else {
|
|
|
|
return "Plugin $plugin failed to load.";
|
|
|
|
}
|
2020-05-04 22:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_unplug {
|
|
|
|
my ($self, $context) = @_;
|
2021-07-14 04:45:56 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
my $plugin = $context->{arguments};
|
|
|
|
|
|
|
|
if (not length $plugin) { return "Usage: unplug <plugin>"; }
|
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
if ($self->unload($plugin)) {
|
|
|
|
return "Unloaded $plugin plugin.";
|
|
|
|
} else {
|
|
|
|
return "Plugin $plugin is not loaded.";
|
|
|
|
}
|
2020-05-04 22:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_replug {
|
|
|
|
my ($self, $context) = @_;
|
2021-07-14 04:45:56 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
my $plugin = $context->{arguments};
|
|
|
|
|
|
|
|
if (not length $plugin) { return "Usage: replug <plugin>"; }
|
|
|
|
|
|
|
|
my $unload_result = $self->cmd_unplug($context);
|
|
|
|
my $load_result = $self->cmd_plug($context);
|
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
my $result;
|
2020-05-04 22:21:35 +02:00
|
|
|
$result .= "$unload_result " if $unload_result =~ m/^Unloaded/;
|
|
|
|
$result .= $load_result;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_pluglist {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
|
|
|
|
my @plugins = sort keys %{$self->{plugins}};
|
|
|
|
|
|
|
|
return "No plugins loaded." if not @plugins;
|
|
|
|
|
|
|
|
return scalar @plugins . ' plugin' . (@plugins == 1 ? '' : 's') . ' loaded: ' . join (', ', @plugins);
|
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 {
|
2021-07-14 04:45:56 +02:00
|
|
|
require "$module";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Loading $plugin\n");
|
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;
|