aboutsummaryrefslogtreecommitdiff
path: root/Plugin.pm
blob: 4aba6d23395c9e29b41fa343af69cff976501de8 (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
package Plugin;

use strict;
use warnings;

my $unload_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: $!";
	};
	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;