3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-18 09:59:37 +01:00
pbot/PBot/Plugins.pm

165 lines
3.6 KiB
Perl
Raw Normal View History

2015-09-07 07:52:39 +02:00
# File: Plugins.pm
# Author: pragma-
#
2015-09-07 07:52:39 +02:00
# Purpose: Loads and manages plugins.
2015-09-07 07:52:39 +02:00
package PBot::Plugins;
use warnings;
use strict;
use File::Basename;
use Carp ();
sub new {
if(ref($_[1]) eq 'HASH') {
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
}
my ($class, %conf) = @_;
my $self = bless {}, $class;
$self->initialize(%conf);
return $self;
}
sub initialize {
my ($self, %conf) = @_;
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
2015-09-07 07:52:39 +02:00
$self->{plugins} = {};
$self->{pbot}->{commands}->register(sub { $self->load_cmd(@_) }, "plug", 90);
$self->{pbot}->{commands}->register(sub { $self->unload_cmd(@_) }, "unplug", 90);
$self->{pbot}->{commands}->register(sub { $self->list_cmd(@_) }, "pluglist", 0);
2015-09-07 07:39:40 +02:00
$self->autoload(%conf);
}
sub autoload {
2015-09-07 07:39:40 +02:00
my ($self, %conf) = @_;
2015-09-07 07:52:39 +02:00
$self->{pbot}->{logger}->log("Loading plugins ...\n");
my $plugin_count = 0;
2015-09-07 07:52:39 +02:00
my @plugins = glob 'PBot/Plugins/*.pm';
2015-09-07 07:52:39 +02:00
foreach my $plugin (sort @plugins) {
$plugin = basename $plugin;
$plugin =~ s/.pm$//;
2015-09-07 07:52:39 +02:00
# do not load plugins that begin with an underscore
next if $plugin =~ m/^_/;
2015-09-07 07:52:39 +02:00
$plugin_count++ if $self->load($plugin, %conf)
}
2015-09-07 07:52:39 +02:00
$self->{pbot}->{logger}->log("$plugin_count plugin" . ($plugin_count == 1 ? '' : 's') . " loaded.\n");
}
sub load {
2015-09-07 07:52:39 +02:00
my ($self, $plugin, %conf) = @_;
2015-09-07 07:52:39 +02:00
$self->unload($plugin);
2015-09-07 07:52:39 +02:00
my $class = "PBot::Plugins::$plugin";
2015-09-07 07:52:39 +02:00
$self->{pbot}->{refresher}->{refresher}->refresh_module("PBot/Plugins/$plugin.pm");
my $ret = eval {
eval "require $class";
if ($@) {
chomp $@;
2015-09-07 07:52:39 +02:00
$self->{pbot}->{logger}->log("Error loading $plugin: $@\n");
return 0;
}
2015-09-07 07:52:39 +02:00
$self->{pbot}->{logger}->log("Loading $plugin\n");
2015-09-07 07:39:40 +02:00
my $mod = $class->new(pbot => $self->{pbot}, %conf);
2015-09-07 07:52:39 +02:00
$self->{plugins}->{$plugin} = $mod;
$self->{pbot}->{refresher}->{refresher}->update_cache("PBot/Plugins/$plugin.pm");
return 1;
};
if ($@) {
chomp $@;
2015-09-07 07:52:39 +02:00
$self->{pbot}->{logger}->log("Error loading $plugin: $@\n");
return 0;
}
return $ret;
}
sub unload {
2015-09-07 07:52:39 +02:00
my ($self, $plugin) = @_;
2015-09-07 07:52:39 +02:00
if (exists $self->{plugins}->{$plugin}) {
eval {
2015-09-07 07:52:39 +02:00
$self->{plugins}->{$plugin}->unload;
delete $self->{plugins}->{$plugin};
};
if ($@) {
chomp $@;
2015-09-07 07:52:39 +02:00
$self->{pbot}->{logger}->log("Warning: got error unloading plugin $plugin: $@\n");
}
$self->{pbot}->{refresher}->{refresher}->unload_module("PBot::Plugins::$plugin");
$self->{pbot}->{refresher}->{refresher}->unload_subs("PBot/Plugins/$plugin.pm");
2015-09-07 07:52:39 +02:00
$self->{pbot}->{logger}->log("Plugin $plugin unloaded.\n");
return 1;
} else {
return 0;
}
}
sub load_cmd {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
if (not length $arguments) {
return "Usage: plug <plugin>";
}
if ($self->load($arguments)) {
return "Loaded $arguments plugin.";
} else {
2015-09-07 07:22:37 +02:00
return "Plugin $arguments failed to load.";
}
}
sub unload_cmd {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
if (not length $arguments) {
return "Usage: unplug <plugin>";
}
if ($self->unload($arguments)) {
return "Unloaded $arguments plugin.";
} else {
2015-09-07 09:20:06 +02:00
return "Plugin $arguments not found.";
}
}
sub list_cmd {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
my $result = "Loaded plugins: ";
my $count = 0;
my $comma = '';
2015-09-07 07:52:39 +02:00
foreach my $plugin (sort keys $self->{plugins}) {
$result .= $comma . $plugin;
$count++;
$comma = ', ';
}
$result .= 'none' if $count == 0;
return $result;
}
1;