Component/IRC/Plugin/PlugMan.pm

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