Component/IRC/Plugin/PlugMan.pm

filter: Statementstmt 76.6% 108 / 141 Branchbran 48.2% 27 / 56 Conditioncond 54.5% 6 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 81.8% 18 / 22 Podpod 55.5% 5 / 9 totaltotal 67.0% 165 / 246 CC 35 SCAR 17.0
CC
35
Cov
68%
S_msg
12.0
load
11.2
reload
8.4
CRAP 5.4
« Component/IRC/Plugin/Logger.pm ↑ Summary Component/IRC/Plugin/NickReclaim.pm »
1 package POE::Component::IRC::Plugin::PlugMan;
2 5 $POE::Component::IRC::Plugin::PlugMan::VERSION = '6.95';
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use IRC::Utils qw( matches_mask parse_user );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
10 # Turn on the debugger's symbol source tracing
11 5 $^P |= 0x10;
12  
13 # Work around bug in pre-5.8.7 perl where turning on $^P
14 # causes caller() to be confused about eval {}'s in the stack.
15 # (See http://rt.perl.org/rt3/Ticket/Display.html?id=35059 for more info.)
16 5 ▶ eval 'sub DB::sub' if $] < 5.008007;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.008007" 0 5 5
17 }
18  
19 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
20 4 my ($package) = shift;
21 4 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 4 4
22 4 my %args = @_;
23 4 $args{ lc $_ } = delete $args{ $_ } for keys %args;
24 4 return bless \%args, $package;
25 }
26  
27 ##########################
28 # Plugin related methods #
29 ##########################
30  
31 4 ▶sub PCI_register {
Subroutine PCI_register: called CC=1 SCAR=1.1
Pod undocumented
32 4 my ($self, $irc) = @_;
33  
34 4 $self->{irc} = $irc;
35 4 $irc->plugin_register( $self, 'SERVER', qw(public msg) );
36  
37 $self->{commands} = {
38 2 ▶ PLUGIN_ADD => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
39 2 my ($self, $method, $recipient, @cmd) = @_;
40 2 ▶ my $msg = $self->load(@cmd) ? 'Done.' : 'Nope';
Branch: 1 branch
BranchTrueFalseTotal
$self->load(@cmd) ? : 2 0 2
41 2 $self->{irc}->yield($method => $recipient => $msg);
42 },
43 1 ▶ PLUGIN_DEL => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
44 1 my ($self, $method, $recipient, @cmd) = @_;
45 1 ▶ my $msg = $self->unload(@cmd) ? 'Done.' : 'Nope';
Branch: 1 branch
BranchTrueFalseTotal
$self->unload(@cmd) ? : 1 0 1
46 1 $self->{irc}->yield($method => $recipient => $msg);
47 },
48 2 ▶ PLUGIN_RELOAD => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
49 2 my ($self, $method, $recipient, @cmd) = @_;
50 2 ▶ my $msg = $self->reload(@cmd) ? 'Done.' : 'Nope';
Branch: 1 branch
BranchTrueFalseTotal
$self->reload(@cmd) ? : 2 0 2
51 2 $self->{irc}->yield($method => $recipient => $msg);
52 },
53 0 ▶ PLUGIN_LIST => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
54 0 my ($self, $method, $recipient, @cmd) = @_;
55 0 my @aliases = keys %{ $self->{irc}->plugin_list() };
56 0 ▶ my $msg = @aliases
Branch: 1 branch
BranchTrueFalseTotal
@aliases ? : 0 0 0
57 ? 'Plugins [ ' . join(', ', @aliases ) . ' ]'
58 : 'No plugins loaded.';
59 0 $self->{irc}->yield($method => $recipient => $msg);
60 },
61 0 ▶ PLUGIN_LOADED => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
62 0 my ($self, $method, $recipient, @cmd) = @_;
63 0 my @aliases = $self->loaded();
64 0 ▶ my $msg = @aliases
Branch: 1 branch
BranchTrueFalseTotal
@aliases ? : 0 0 0
65 ? 'Managed Plugins [ ' . join(', ', @aliases ) . ' ]'
66 : 'No managed plugins loaded.';
67 0 $self->{irc}->yield($method => $recipient => $msg);
68 },
69 4 };
70  
71 4 return 1;
72 }
73  
74 4 ▶sub PCI_unregister {
Subroutine PCI_unregister: called CC=1 SCAR=0.0
Pod undocumented
75 4 my ($self, $irc) = @_;
76 4 delete $self->{irc};
77 4 return 1;
78 }
79  
80 6 ▶sub S_public {
Subroutine S_public: called CC=4 SCAR=13.9
Pod undocumented
81 6 my ($self, $irc) = splice @_, 0 , 2;
82 6 my $who = ${ $_[0] };
83 6 my $channel = ${ $_[1] }->[0];
84 6 my $what = ${ $_[2] };
85 6 my $me = $irc->nick_name();
86  
87 6 my ($command) = $what =~ m/^\s*\Q$me\E[:,;.!?~]?\s*(.*)$/i;
88 6 ▶ return PCI_EAT_NONE if !$command || !$self->_authed($who, $channel);
Branch: 1 branch
BranchTrueFalseTotal
unless $command and $self->_authed($who, $channel) 1 5 6
Condition66%2/3
$command and $self->_authed($who, $channel)
!ll&&!rl&&r
015
MC/DC50%1/2
$command and $self->_authed($who, $channel)
$command $self->_authed($who, $channel)
Truth table66%2/3
A and B
A= $commandB= $self->_authed($who, $channel)
ABresult
0X0
100
111
89  
90 5 my (@cmd) = split(/ +/, $command);
91 5 my $cmd = uc (shift @cmd);
92  
93 5 ▶ if (defined $self->{commands}->{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"commands"}{$cmd}) 5 0 5
94 5 $self->{commands}->{$cmd}->($self, 'privmsg', $channel, @cmd);
95 }
96  
97 5 return PCI_EAT_NONE;
98 }
99  
100 0 ▶sub S_msg {
Subroutine S_msg: not called CC=3 SCAR=24.8
Pod undocumented
101 0 my ($self, $irc) = splice @_, 0 , 2;
102 0 my $who = ${ $_[0] };
103 0 my $nick = parse_user($who);
104 0 my $channel = ${ $_[1] }->[0];
105 0 my $command = ${ $_[2] };
106 0 my (@cmd) = split(/ +/,$command);
107 0 my $cmd = uc (shift @cmd);
108  
109 0 ▶ return PCI_EAT_NONE if !$self->_authed($who, $channel);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_authed($who, $channel) 0 0 0
110  
111 0 ▶ if (defined $self->{commands}->{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"commands"}{$cmd}) 0 0 0
112 0 $self->{commands}->{$cmd}->($self, 'notice', $nick, @cmd);
113 }
114  
115 0 return PCI_EAT_NONE;
116 }
117  
118 ###############################
119 # Plugin manipulation methods #
120 ###############################
121  
122 7 ▶sub load {
Subroutine load: called CC=9 SCAR=24.2
Pod documented
123 7 my ($self, $desc, $plugin) = splice @_, 0, 3;
124 7 ▶ return if !$desc || !$plugin;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc and $plugin 0 7 7
Condition33%1/3
$desc and $plugin
!ll&&!rl&&r
007
MC/DC0%0/2
$desc and $plugin
$desc $plugin
Truth table33%1/3
A and B
A= $descB= $plugin
ABresult
0X0
100
111
125  
126 7 my $object;
127 7 ▶ my $module = ref $plugin || $plugin;
Condition66%2/3
ref $plugin || $plugin
l!l&&r!l&&!r
160
MC/DC0%0/2
ref $plugin || $plugin
ref $plugin $plugin
Truth table66%2/3
A || B
A= ref $pluginB= $plugin
ABresult
1X1
011
000
128 7 ▶ if (! ref $plugin){
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $plugin) { } 6 1 7
129 6 ▶ $module .= '.pm' if $module !~ /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /\.pm$/ 6 0 6
130 6 $module =~ s/::/\//g;
131  
132 6 eval "require $plugin";
133 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
134 0 my $error = $@;
135 0 delete $INC{$module};
136 0 $self->_unload_subs($plugin);
137 0 die $error;
138 }
139  
140 6 $object = $plugin->new( @_ );
141 6 ▶ return if !$object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 6 6
142 } else {
143 1 $object = $plugin;
144 1 $plugin = ref $object;
145 }
146  
147 7 my $args = [ @_ ];
148 7 $self->{plugins}->{ $desc }->{module} = $module;
149 7 $self->{plugins}->{ $desc }->{plugin} = $plugin;
150  
151 7 my $return = $self->{irc}->plugin_add( $desc, $object );
152 7 ▶ if ( $return ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return) { } 7 0 7
153 # Stash away arguments for use later by _reload.
154 7 $self->{plugins}->{ $desc }->{args} = $args;
155 }
156 else {
157 # Cleanup
158 0 delete $self->{plugins}->{ $desc };
159 }
160  
161 7 return $return;
162 }
163  
164 6 ▶sub unload {
Subroutine unload: called CC=3 SCAR=11.1
Pod documented
165 6 my ($self, $desc) = splice @_, 0, 2;
166 6 ▶ return if !$desc;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc 0 6 6
167  
168 6 my $plugin = $self->{irc}->plugin_del( $desc );
169 6 ▶ return if !$plugin;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin 0 6 6
170 6 my $module = $self->{plugins}->{ $desc }->{module};
171 6 my $file = $self->{plugins}->{ $desc }->{plugin};
172 6 delete $INC{$module};
173 6 delete $self->{plugins}->{ $desc };
174 6 $self->_unload_subs($file);
175 6 return 1;
176 }
177  
178 6 ▶sub _unload_subs {
Subroutine _unload_subs: called CC=4 SCAR=14.0
179 6 my $self = shift;
180 6 ▶ my $file = shift || return;
Condition50%1/2
shift() || (return)
l!l
60
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
181  
182 6 for my $sym ( grep { index( $_, "$file:" ) == 0 } keys %DB::sub ) {
183 48 eval { undef &$sym };
184 48 ▶ warn "$sym: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 48 48
185 48 delete $DB::sub{$sym};
186 }
187  
188 6 return 1;
189 }
190  
191 3 ▶sub reload {
Subroutine reload: called CC=7 SCAR=21.3
Pod documented
192 3 my ($self, $desc) = splice @_, 0, 2;
193 3 ▶ return if !defined $desc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc 0 3 3
194  
195 3 my $plugin_state = $self->{plugins}->{ $desc };
196 3 ▶ return if !$plugin_state;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin_state 0 3 3
197 3 ▶ warn "Unloading plugin $desc\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 3 3
198 3 ▶ return if !$self->unload( $desc );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->unload($desc) 0 3 3
199  
200 0 ▶ warn "Loading plugin $desc " . $plugin_state->{plugin} . ' [ ' . join(', ',@{ $plugin_state->{args} }) . " ]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 3 3
201 3 ▶ return if !$self->load( $desc, $plugin_state->{plugin}, @{ $plugin_state->{args} } );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->load($desc, $plugin_state->{"plugin"}, @{$$plugin_state{"args"};}) 0 3 3
202 3 return 1;
203 }
204  
205 0 ▶sub loaded {
Subroutine loaded: not called CC=1 SCAR=6.9
Pod documented
206 0 my $self = shift;
207 0 return keys %{ $self->{plugins} };
208 }
209  
210 6 ▶sub _authed {
Subroutine _authed: called CC=3 SCAR=11.4
211 6 my ($self, $who, $chan) = @_;
212  
213 6 ▶ return $self->{auth_sub}->($self->{irc}, $who, $chan) if $self->{auth_sub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auth_sub"} 3 3 6
214 3 ▶ return 1 if matches_mask($self->{botowner}, $who);
Branch: 1 branch
BranchTrueFalseTotal
if matches_mask($self->{"botowner"}, $who) 3 0 3
215 0 return;
216 }
217  
218 5 1;
219  
220 =encoding utf8
221  
222 =head1 NAME
223  
224 POE::Component::IRC::Plugin::PlugMan - A PoCo-IRC plugin that provides plugin
225 management services.
226  
227 =head1 SYNOPSIS
228  
229 use strict;
230 use warnings;
231 use POE qw(Component::IRC::State);
232 use POE::Component::IRC::Plugin::PlugMan;
233  
234 my $botowner = 'somebody!*@somehost.com';
235 my $irc = POE::Component::IRC::State->spawn();
236  
237 POE::Session->create(
238 package_states => [
239 main => [ qw(_start irc_plugin_add) ],
240 ],
241 );
242  
243 sub _start {
244 $irc->yield( register => 'all' );
245 $irc->plugin_add( 'PlugMan' => POE::Component::IRC::Plugin::PlugMan->new( botowner => $botowner ) );
246 return;
247 }
248  
249 sub irc_plugin_add {
250 my ($desc, $plugin) = @_[ARG0, ARG1];
251  
252 if ($desc eq 'PlugMan') {
253 $plugin->load( 'Connector', 'POE::Component::IRC::Plugin::Connector' );
254 }
255 return;
256 }
257  
258 =head1 DESCRIPTION
259  
260 POE::Component::IRC::Plugin::PlugMan is a POE::Component::IRC plugin management
261 plugin. It provides support for 'on-the-fly' loading, reloading and unloading
262 of plugin modules, via object methods that you can incorporate into your own
263 code and a handy IRC interface.
264  
265 =head1 METHODS
266  
267 =head2 C<new>
268  
269 Takes two optional arguments:
270  
271 B<'botowner'>, an IRC mask to match against for people issuing commands via the
272 IRC interface;
273  
274 B<'auth_sub'>, a sub reference which will be called to determine if a user
275 may issue commands via the IRC interface. Overrides B<'botowner'>. It will be
276 called with three arguments: the IRC component object, the nick!user@host and
277 the channel name as arguments. It should return a true value if the user is
278 authorized, a false one otherwise.
279  
280 B<'debug'>, set to a true value to see when stuff goes wrong;
281  
282 Not setting B<'botowner'> or B<'auth_sub'> effectively disables the IRC
283 interface.
284  
285 If B<'botowner'> is specified the plugin checks that it is being loaded into a
286 L<POE::Component::IRC::State|POE::Component::IRC::State> or sub-class and will
287 fail to load otherwise.
288  
289 Returns a plugin object suitable for feeding to
290 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
291  
292 =head2 C<load>
293  
294 Loads a managed plugin.
295  
296 Takes two mandatory arguments, a plugin descriptor and a plugin package name.
297 Any other arguments are used as options to the loaded plugin constructor.
298  
299 $plugin->load( 'Connector', 'POE::Component::IRC::Plugin::Connector', delay, 120 );
300  
301 Returns true or false depending on whether the load was successfully or not.
302  
303 =head2 C<unload>
304  
305 Unloads a managed plugin.
306  
307 Takes one mandatory argument, a plugin descriptor.
308  
309 $plugin->unload( 'Connector' );
310  
311 Returns true or false depending on whether the unload was successfully or not.
312  
313 =head2 C<reload>
314  
315 Unloads and loads a managed plugin, with applicable plugin options.
316  
317 Takes one mandatory argument, a plugin descriptor.
318  
319 $plugin->reload( 'Connector' );
320  
321 =head2 C<loaded>
322  
323 Takes no arguments.
324  
325 $plugin->loaded();
326  
327 Returns a list of descriptors of managed plugins.
328  
329 =head1 INPUT
330  
331 An IRC interface is enabled by specifying a "botowner" mask to
332 L<C<new>|/new>. Commands may be either invoked via a PRIVMSG directly to
333 your bot or in a channel by prefixing the command with the nickname of your
334 bot. One caveat, the parsing of the irc command is very rudimentary (it
335 merely splits the line on spaces).
336  
337 =head2 C<plugin_add>
338  
339 Takes the same arguments as L<C<load>|/load>.
340  
341 =head2 C<plugin_del>
342  
343 Takes the same arguments as L<C<unload>|/unload>.
344  
345 =head2 C<plugin_reload>
346  
347 Takes the same arguments as L<C<reload>|/reload>.
348  
349 =head2 C<plugin_loaded>
350  
351 Returns a list of descriptors of managed plugins.
352  
353 =head2 C<plugin_list>
354  
355 Returns a list of descriptors of *all* plugins loaded into the current PoCo-IRC
356 component.
357  
358 =head1 AUTHOR
359  
360 Chris 'BinGOs' Williams
361  
362 =head1 SEE ALSO
363  
364 L<POE::Component::IRC::State|POE::Component::IRC::State>
365  
366 L<POE::Component::IRC::Plugin|POE::Component::IRC::Plugin>
367  
368 =cut
« Component/IRC/Plugin/Logger.pm ↑ Summary Component/IRC/Plugin/NickReclaim.pm »