aboutsummaryrefslogtreecommitdiff
path: root/Plugin.pm
blob: 1f3d0803b478f1725d4c915b58301ed537c82630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package Plugin;

use strict;
use warnings;

my $load_callback;

sub set_load_callback {
	($load_callback) = @_;
};

sub load_plugin {
	my ($logger, $config, $module) = @_;
	(my $path = $module) =~ s,::,/,g;

	return "$module is already loaded, no changes made" if grep {$_ eq $module} @{$config->{active_plugins}};

	eval {
		require $path . ".pm";
		push @{$config->{active_plugins}}, $module;
	} or do {
		chomp $@;
		$logger->($@);
		return "Cannot load $module: $!";
	};
	$load_callback->($module);
	return undef;
}

sub unload_plugin {
	my ($logger, $config, $module) = @_;

	return "$module is not loaded, no changes made" unless grep {$_ eq $module} @{$config->{active_plugins}};

	my @new_plugins = grep {$_ ne $module} @{$config->{active_plugins}};
	$config->{active_plugins} = \@new_plugins;
	return undef;
}
1;