2021-07-23 16:24:30 +02:00
|
|
|
# File: Plugins.pm
|
|
|
|
#
|
|
|
|
# Purpose: Registers commands for loading and unloading plugins.
|
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2015-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-23 16:24:30 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package PBot::Core::Commands::Plugins;
|
|
|
|
|
|
|
|
use PBot::Imports;
|
|
|
|
use parent 'PBot::Core::Class';
|
|
|
|
|
|
|
|
use File::Basename;
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub initialize($self, %conf) {
|
2021-07-23 16:24:30 +02:00
|
|
|
# plugin management bot commands
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub cmd_plug($self, $context) {
|
2021-07-23 16:24:30 +02:00
|
|
|
my $plugin = $context->{arguments};
|
|
|
|
|
|
|
|
if (not length $plugin) { return "Usage: plug <plugin>"; }
|
|
|
|
|
|
|
|
if ($self->{pbot}->{plugins}->load($plugin)) {
|
|
|
|
return "Loaded $plugin plugin.";
|
|
|
|
} else {
|
|
|
|
return "Plugin $plugin failed to load.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub cmd_unplug($self, $context) {
|
2021-07-23 16:24:30 +02:00
|
|
|
my $plugin = $context->{arguments};
|
|
|
|
|
|
|
|
if (not length $plugin) { return "Usage: unplug <plugin>"; }
|
|
|
|
|
|
|
|
if ($self->{pbot}->{plugins}->unload($plugin)) {
|
|
|
|
return "Unloaded $plugin plugin.";
|
|
|
|
} else {
|
|
|
|
return "Plugin $plugin is not loaded.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub cmd_replug($self, $context) {
|
2021-07-23 16:24:30 +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);
|
|
|
|
|
|
|
|
my $result;
|
|
|
|
$result .= "$unload_result " if $unload_result =~ m/^Unloaded/;
|
|
|
|
$result .= $load_result;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub cmd_pluglist($self, $context) {
|
2021-07-23 16:24:30 +02:00
|
|
|
my @plugins = sort keys %{$self->{pbot}->{plugins}->{plugins}};
|
|
|
|
|
|
|
|
return "No plugins loaded." if not @plugins;
|
|
|
|
|
|
|
|
return scalar @plugins . ' plugin' . (@plugins == 1 ? '' : 's') . ' loaded: ' . join (', ', @plugins);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|