2015-09-07 07:52:39 +02:00
|
|
|
# File: Plugins.pm
|
2015-09-07 07:17:07 +02:00
|
|
|
# Author: pragma-
|
|
|
|
#
|
2015-09-07 07:52:39 +02:00
|
|
|
# Purpose: Loads and manages plugins.
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
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
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2015-09-07 07:17:07 +02:00
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{plugins} = {};
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_plug {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
my $plugin = $context->{arguments};
|
|
|
|
|
|
|
|
if (not length $plugin) { return "Usage: plug <plugin>"; }
|
|
|
|
|
|
|
|
if ($self->load($plugin)) { return "Loaded $plugin plugin."; }
|
|
|
|
else { return "Plugin $plugin failed to load."; }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_unplug {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
my $plugin = $context->{arguments};
|
|
|
|
|
|
|
|
if (not length $plugin) { return "Usage: unplug <plugin>"; }
|
|
|
|
|
|
|
|
if ($self->unload($plugin)) { return "Unloaded $plugin plugin."; }
|
|
|
|
else { return "Plugin $plugin is not loaded."; }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_replug {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) = @_;
|
|
|
|
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 $path = $self->{pbot}->{registry}->get_value('general', 'plugin_dir') // 'Plugins';
|
|
|
|
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");
|
|
|
|
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;
|
|
|
|
}
|
2019-12-22 00:26:01 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
$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
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $path = $self->{pbot}->{registry}->get_value('general', 'plugin_dir') // 'Plugins';
|
|
|
|
|
|
|
|
if (not grep { $_ eq $path } @INC) {
|
|
|
|
unshift @INC, $path;
|
|
|
|
}
|
2019-09-01 20:01:18 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{refresher}->{refresher}->refresh_module("$path/$plugin.pm");
|
2019-09-01 20:01:18 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $ret = eval {
|
|
|
|
require "$path/$plugin.pm";
|
2015-09-07 07:17:07 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($@) {
|
|
|
|
chomp $@;
|
|
|
|
$self->{pbot}->{logger}->log("Error loading $plugin: $@\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Loading $plugin\n");
|
|
|
|
my $class = "Plugins::$plugin";
|
|
|
|
$self->{plugins}->{$plugin} = $class->new(pbot => $self->{pbot}, %conf);
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->update_cache("$path/$plugin.pm");
|
|
|
|
return 1;
|
|
|
|
};
|
2015-09-07 07:17:07 +02:00
|
|
|
|
|
|
|
if ($@) {
|
2020-02-15 23:38:32 +01:00
|
|
|
chomp $@;
|
|
|
|
$self->{pbot}->{logger}->log("Error loading $plugin: $@\n");
|
|
|
|
return 0;
|
2015-09-07 07:17:07 +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};
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
chomp $@;
|
|
|
|
$self->{pbot}->{logger}->log("Warning: got error unloading plugin $plugin: $@\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
my $path = $self->{pbot}->{registry}->get_value('general', 'plugin_dir') // 'Plugins';
|
|
|
|
my $class = $path;
|
|
|
|
$class =~ s,[/\\],::,g;
|
|
|
|
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->unload_module($class . '::' . $plugin);
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->unload_subs("$path/$plugin.pm");
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Plugin $plugin unloaded.\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2015-09-07 07:17:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|