blib/lib/Bot/BasicBot/Pluggable.pm untested

filter: Statementstmt 0.0% 0 / 231 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 39 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 392 CC 36 SCAR 27.7
CC
36
Cov
0%
load
56.0
init
30.0
dispatch
30.0
CRAP 16.0
1 package Bot::BasicBot::Pluggable;
2 $Bot::BasicBot::Pluggable::VERSION = '1.11';
3 use warnings;
4 use strict;
5  
6 use 5.8.0;
7 use POE;
8 use Bot::BasicBot 0.60;
9 use Log::Log4perl;
10 use Log::Log4perl::Level;
11 use base qw( Bot::BasicBot );
12 use Data::Dumper;
13  
14 $Data::Dumper::Terse = 1;
15 $Data::Dumper::Indent = 0;
16  
17 use Module::Pluggable
18 sub_name => '_available',
19 search_path => 'Bot::BasicBot::Pluggable::Module',
20 except => 'Bot::BasicBot::Pluggable::Module::Base';
21 use Bot::BasicBot::Pluggable::Module;
22 use Bot::BasicBot::Pluggable::Store;
23 use File::Spec;
24 use Try::Tiny;
25  
26 sub init {
27 my $self = shift;
28 $self->init_logging();
29  
30 my $logger = Log::Log4perl->get_logger( ref $self );
31 $logger->info( 'Starting initialization of ' . ref $self );
32  
33 if ( !$self->store ) {
34 $logger->debug('Store not set, trying to load a store backend');
35 my $store;
36 for my $type (qw( DBI Deep Storable Memory )) {
37 $store = try {
38 $logger->debug("Trying to load store backend $type");
39 Bot::BasicBot::Pluggable::Store->new( { type => $type } );
40 };
41 if ($store) {
42 $logger->info("Loaded store backend $type");
43 last;
44 }
45 }
46 if ( !UNIVERSAL::isa( $store, 'Bot::BasicBot::Pluggable::Store' ) ) {
47 $logger->logdie("Couldn't load any default store type");
48 }
49 $self->store($store);
50 }
51 elsif ( !UNIVERSAL::isa( $self->store, "Bot::BasicBot::Pluggable::Store" ) )
52 {
53 $self->store( Bot::BasicBot::Pluggable::Store->new( $self->store ) );
54 }
55 return 1;
56 }
57  
58 sub init_logging {
59 my $self = shift;
60 my $logger = Log::Log4perl->get_logger( ref $self );
61 if ( $self->logconfig ) {
62 Log::Log4perl->init( $self->logconfig );
63 }
64 else {
65 my $loglevel = $self->loglevel;
66 Log::Log4perl::init( \ <<EOT );
67 log4perl.rootLogger=$loglevel,Screen
68 log4perl.appender.Screen = Log::Log4perl::Appender::Screen
69 log4perl.appender.Screen.stderr = 0
70 log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
71 log4perl.appender.Screen.layout.ConversionPattern = %-6p %d %m%n
72 EOT
73 }
74 }
75  
76 sub log {
77 my $self = shift;
78 my $logger = Log::Log4perl->get_logger( ref $self );
79 for my $log_entry (@_) {
80 chomp $log_entry;
81 $logger->warn($log_entry);
82 }
83 return;
84 }
85  
86 sub load {
87 my $self = shift;
88 my $module = shift;
89  
90 my $logger = Log::Log4perl->get_logger( ref $self );
91  
92 # it's safe to die here, mostly this call is eval'd.
93 $logger->logdie("Cannot load module without a name") unless $module;
94 $logger->logdie("Module $module already loaded") if $self->handler($module);
95  
96 # This is possible a leeeetle bit evil.
97 $logger->info("Loading module $module");
98 my $filename = $module;
99 $filename =~ s{::}{/}g;
100 my $file = "Bot/BasicBot/Pluggable/Module/$filename.pm";
101 $file = "./$filename.pm" if ( -e "./$filename.pm" );
102 $file = "./modules/$filename.pm" if ( -e "./modules/$filename.pm" );
103 $logger->debug("Loading module $module from file $file");
104 warn "Loading $module from $file";
105  
106 # force a reload of the file (in the event that we've already loaded it).
107 no warnings 'redefine';
108 delete $INC{$file};
109  
110 try { require $file } catch { die "Can't load $module: $_"; };
111  
112 # Ok, it's very evil. Don't bother me, I'm working.
113  
114 my $m = "Bot::BasicBot::Pluggable::Module::$module"->new(
115 Bot => $self,
116 Param => \@_
117 );
118  
119 $logger->logdie("->new didn't return an object") unless ( $m and ref($m) );
120 $logger->logdie( ref($m) . " isn't a $module" )
121 unless ref($m) =~ /\Q$module/;
122  
123 $self->add_handler( $m, $module );
124  
125 return $m;
126 }
127  
128 sub reload {
129 my $self = shift;
130 my $module = shift;
131 my $logger = Log::Log4perl->get_logger( ref $self );
132 $logger->logdie("Cannot reload module with a name") unless $module;
133 $self->remove_handler($module) if $self->handler($module);
134 return $self->load($module);
135 }
136  
137 sub unload {
138 my $self = shift;
139 my $module = shift;
140 my $logger = Log::Log4perl->get_logger( ref $self );
141 $logger->logdie("Need name") unless $module;
142 $logger->logdie("Not loaded") unless $self->handler($module);
143 $logger->info("Unloading module $module");
144 $self->remove_handler($module);
145 }
146  
147 sub module {
148 my $self = shift;
149 return $self->handler(@_);
150 }
151  
152 sub modules {
153 my $self = shift;
154 return $self->handlers(@_);
155 }
156  
157 sub available_modules {
158 my $self = shift;
159 my @local_modules =
160 map { substr( ( File::Spec->splitpath($_) )[2], 0, -3 ) } glob('./*.pm'),
161 glob('./modules/*.pm');
162 my @central_modules =
163 map {
164 my $mod = $_;
165 $mod =~ s/^Bot::BasicBot::Pluggable::Module:://;
166 $mod;
167 } $self->_available();
168 my @modules = sort @local_modules, @central_modules;
169 return @modules;
170 }
171  
172 # deprecated methods
173 sub handler {
174 my ( $self, $name ) = @_;
175 return $self->{handlers}{ lc($name) };
176 }
177  
178 sub handlers {
179 my $self = shift;
180 my @keys = sort {
181 my $xa = $self->handler($a);
182 my $xb = $self->handler($b);
183 (
184 ($xb->get('user_priority') || $xb->get('priority') || 0)
185 <=>
186 ($xa->get('user_priority') || $xa->get('priority') || 0)
187 ) || ($a cmp $b)
188 } keys( %{ $self->{handlers} } );
189 return @keys if wantarray;
190 return \@keys;
191 }
192  
193 sub add_handler {
194 my ( $self, $handler, $name ) = @_;
195 my $logger = Log::Log4perl->get_logger( ref $self );
196 $logger->logdie("Need a name for adding a handler") unless $name;
197 $logger->logdie("Can't load a handler with a duplicate name $name")
198 if $self->{handlers}{ lc($name) };
199 $self->{handlers}{ lc($name) } = $handler;
200 }
201  
202 sub remove_handler {
203 my ( $self, $name ) = @_;
204 my $logger = Log::Log4perl->get_logger( ref $self );
205 $logger->logdie("Need a name for removing a handler") unless $name;
206 $logger->logdie("Hander $name not defined")
207 unless $self->{handlers}{ lc($name) };
208 $self->{handlers}{ lc($name) }->stop();
209 delete $self->{handlers}{ lc($name) };
210 }
211  
212 sub store {
213 my $self = shift;
214 $self->{store_object} = shift if @_;
215 return $self->{store_object};
216 }
217  
218 sub loglevel {
219 my $self = shift;
220 $self->{loglevel} = shift if @_;
221 return ($self->{loglevel} and uc $self->{loglevel}) || 'WARN';
222 }
223  
224 sub logconfig {
225 my $self = shift;
226 $self->{logconfig} = shift if @_;
227 return $self->{logconfig};
228 }
229  
230 sub dispatch {
231 my ( $self, $method, @args ) = @_;
232 my $logger = Log::Log4perl->get_logger( ref $self );
233  
234 $logger->info("Dispatching $method");
235 for my $who ( $self->handlers ) {
236 ## Otherwise we would see tick every five seconds
237 if ( $method eq 'tick' ) {
238 $logger->trace("Trying to dispatch $method to $who");
239 }
240 else {
241 $logger->debug("Trying to dispatch $method to $who");
242 }
243 $logger->trace( "... with " . Dumper(@args) )
244 if $logger->is_trace && @args;
245  
246 next unless $self->handler($who)->can($method);
247 try {
248 $logger->trace(
249 "Dispatching $method to $who with " . Dumper(@args) )
250 if $logger->is_trace;
251 $self->handler($who)->$method(@args);
252 }
253 catch {
254 $logger->warn($_);
255 }
256 }
257 return;
258 }
259  
260 sub help {
261 my $self = shift;
262 my $mess = shift;
263 $mess->{body} =~ s/^help\s*//i;
264 my $logger = Log::Log4perl->get_logger( ref $self );
265  
266 unless ( $mess->{body} ) {
267 return
268 "Ask me for help about: "
269 . join( ", ", $self->handlers() )
270 . " (say 'help <modulename>').";
271 }
272 elsif ( $mess->{body} eq 'modules' ) {
273 return "These modules are available for loading: "
274 . join( ", ", $self->available_modules );
275 }
276 else {
277 if ( my $handler = $self->handler( $mess->{body} ) ) {
278 try {
279 return $handler->help($mess);
280 }
281 catch {
282 $logger->warn(
283 "Error calling help for handler $mess->{body}: $_");
284 }
285 }
286 else {
287 return "I don't know anything about '$mess->{body}'.";
288 }
289 }
290 }
291  
292 #########################################################
293 # the following routines are lifted from Bot::BasicBot: #
294 #########################################################
295 sub tick {
296 my $self = shift;
297 $self->dispatch('tick');
298 return 5;
299 }
300  
301 sub dispatch_priorities {
302 my ( $self, $event, $mess ) = @_;
303 my $response;
304 my $who;
305  
306 my $logger = Log::Log4perl->get_logger( ref $self );
307 $logger->info('Dispatching said event');
308  
309 for my $priority ( 0 .. 3 ) {
310 for my $handler ( $self->handlers ) {
311 my $response;
312 $logger->debug(
313 "Trying to dispatch said to $handler on priority $priority");
314 $logger->trace( '... with arguments ' . Dumper($mess) )
315 if $logger->is_trace and $mess;
316 try {
317 $response =
318 $self->handler($handler)->$event( $mess, $priority );
319 }
320 catch {
321 $logger->warn($_);
322 };
323 if ( $priority and $response ) {
324 $logger->debug("Response by $handler on $priority");
325 $logger->trace( 'Response is ' . Dumper($response) )
326 if $logger->is_trace;
327 return if $response eq '1';
328 $self->reply( $mess, $response );
329 return;
330 }
331 }
332 }
333 return;
334 }
335  
336 sub reply {
337 my ( $self, $mess, @other ) = @_;
338 $self->dispatch( 'replied', {%$mess}, @other );
339 if ( $mess->{reply_hook} ) {
340 return $mess->{reply_hook}->( $mess, @other );
341 }
342 else {
343 return $self->SUPER::reply( $mess, @other );
344 }
345 }
346  
347 BEGIN {
348 my @dispatchable_events = (
349 qw/
350 connected chanjoin chanpart userquit nick_change
351 topic kicked
352 /
353 );
354 my @priority_events = (qw/ said emoted /);
355 {
356 ## no critic qw(ProhibitNoStrict)
357 no strict 'refs';
358 for my $event (@dispatchable_events) {
359 *$event = sub {
360 shift->dispatch( $event, @_ );
361 };
362 }
363 for my $event (@priority_events) {
364 *$event = sub {
365 shift->dispatch_priorities( $event, @_ );
366 };
367 }
368 }
369 }
370  
371 1; # sigh.
372  
373 __END__
374  
375 =head1 NAME
376  
377 Bot::BasicBot::Pluggable - extended simple IRC bot for pluggable modules
378  
379 =head1 VERSION
380  
381 version 1.11
382  
383 =head1 SYNOPSIS
384  
385 =head2 Creating the bot module
386  
387 # with all defaults.
388 my $bot = Bot::BasicBot->new();
389  
390 # with useful options. pass any option
391 # that's valid for Bot::BasicBot.
392 my $bot = Bot::BasicBot::Pluggable->new(
393
394 channels => ["#bottest"],
395 server => "irc.example.com",
396 port => "6667",
397  
398 nick => "pluggabot",
399 altnicks => ["pbot", "pluggable"],
400 username => "bot",
401 name => "Yet Another Pluggable Bot",
402  
403 ignore_list => [qw(hitherto blech muttley)],
404  
405 );
406  
407 =head2 Running the bot (simple)
408  
409 There's a shell script installed to run the bot.
410  
411 $ bot-basicbot-pluggable --nick MyBot --server irc.perl.org
412  
413 Then connect to the IRC server, /query the bot, and set a password. See
414 L<Bot::BasicBot::Pluggable::Module::Auth> for further details.
415  
416 =head2 Running the bot (advanced)
417  
418 There are two useful ways to create a Pluggable bot. The simple way is:
419  
420 # Load some useful modules.
421 my $infobot_module = $bot->load("Infobot");
422 my $google_module = $bot->load("Google");
423 my $seen_module = $bot->load("Seen");
424  
425 # Set the Google key (see http://www.google.com/apis/).
426 $google_module->set("google_key", "some google key");
427  
428 $bot->run();
429  
430 The above lets you run a bot with a few modules, but not change those modules
431 during the run of the bot. The complex, but more flexible, way is as follows:
432  
433 # Load the Loader module.
434 $bot->load('Loader');
435  
436 # run the bot.
437 $bot->run();
438  
439 This is simpler but needs further setup once the bot is joined to a server. The
440 Loader module lets you talk to the bot in-channel and tell it to load and unload
441 other modules. The first one you'll want to load is the 'Auth' module, so that
442 other people can't load and unload modules without permission. Then you'll need
443 to log in as an admin and change the default password, per the following /query:
444  
445 !load Auth
446 !auth admin julia
447 !password julia new_password
448 !auth admin new_password
449  
450 Once you've done this, your bot is safe from other IRC users, and you can tell
451 it to load and unload other installed modules at any time. Further information
452 on module loading is in L<Bot::BasicBot::Pluggable::Module::Loader>.
453  
454 !load Seen
455 !load Google
456 !load Join
457  
458 The Join module lets you tell the bot to join and leave channels:
459  
460 <botname>, join #mychannel
461 <botname>, leave #someotherchannel
462  
463 The perldoc pages for the various modules will list other commands.
464  
465 =head1 DESCRIPTION
466  
467 Bot::BasicBot::Pluggable started as Yet Another Infobot replacement, but now
468 is a generalised framework for writing infobot-type bots that lets you keep
469 each specific function separate. You can have separate modules for factoid
470 tracking, 'seen' status, karma, googling, etc. Included default modules are
471 below. Use C<perldoc Bot::BasicBot::Pluggable::Module::<module name>> for help
472 on their individual terminology.
473  
474 Auth - user authentication and admin access.
475 DNS - host lookup (e.g. nslookup and dns).
476 Google - search Google for things.
477 Infobot - handles infobot-style factoids.
478 Join - joins and leaves channels.
479 Karma - tracks the popularity of things.
480 Loader - loads and unloads modules as bot commands.
481 Seen - tells you when people were last seen.
482 Title - gets the title of URLs mentioned in channel.
483 Vars - changes module variables.
484  
485 The way the Pluggable bot works is very simple. You create a new bot object
486 and tell it to load various modules (or, alternatively, load just the Loader
487 module and then interactively load modules via an IRC /query). The modules
488 receive events when the bot sees things happen and can, in turn, respond. See
489 C<perldoc Bot::BasicBot::Pluggable::Module> for the details of the module API.
490  
491 =head1 METHODS
492  
493 =over 4
494  
495 =item new(key => value, ...)
496  
497 Create a new Bot. Except of the additional attributes loglevel and
498 logconfig identical to the C<new> method in L<Bot::BasicBot>. Please
499 refer to their accessor for documentation.
500  
501 =item load($module)
502  
503 Load a module for the bot by name from C<./ModuleName.pm> or
504 C<./modules/ModuleName.pm> in that order if one of these files
505 exist, and falling back to C<Bot::BasicBot::Pluggable::Module::$module>
506 if not.
507  
508 =item reload($module)
509  
510 Reload the module C<$module> - equivalent to unloading it (if it's already
511 loaded) and reloading it. Will stomp the old module's namespace - warnings
512 are expected here. Not totally clean - if you're experiencing odd bugs, restart
513 the bot if possible. Works for minor bug fixes, etc.
514  
515 =item unload($module)
516  
517 Removes a module from the bot. It won't get events any more.
518  
519 =item module($module)
520  
521 Returns the handler object for the loaded module C<$module>. Used, e.g.,
522 to get the 'Auth' hander to check if a given user is authenticated.
523  
524 =item modules
525  
526 Returns a list of the names of all loaded modules as an array.
527  
528 =item available_modules
529  
530 Returns a list of all available modules whether loaded or not
531  
532 =item add_handler($handler_object, $handler_name)
533  
534 Adds a handler object with the given name to the queue of modules. There
535 is no order specified internally, so adding a module earlier does not
536 guarantee it'll get called first. Names must be unique.
537  
538 =item remove_handler($handler_name)
539  
540 Remove a handler with the given name.
541  
542 =item store
543  
544 Returns the bot's object store; see L<Bot::BasicBot::Pluggable::Store>.
545  
546 =item log
547  
548 Logs all of its argument to loglevel info. Please do not use this
549 function in new code, it's simple provided as fallback for old
550 modules.
551  
552 =item loglevel
553  
554 Returns the bots loglevel or sets it if an argument is supplied.
555 It expects trace, debug, info, warn, error or fatal as value.
556  
557 =item logconfig
558  
559 Returns the bot configuration file for logging. Please refer to
560 L<Log::Log4perl::Config> for the configurations files format. Setting
561 this to a different file after calling init() has no effect.
562  
563 Returns or set
564  
565 =item dispatch($method_name, $method_params)
566  
567 Call the named C<$method> on every loaded module with that method name.
568  
569 =item help
570  
571 Returns help for the ModuleName of message 'help ModuleName'. If no message
572 has been passed, return a list of all possible handlers to return help for.
573  
574 =item run
575  
576 Runs the bot. POE core gets control at this point; you're unlikely to get it back.
577  
578 =back
579  
580 =head1 BUGS
581  
582 During the C<make>, C<make test>, C<make install> process, POE will moan about
583 its kernel not being run. This is a C<Bot::BasicBot problem>, apparently.
584 Reloading a module causes warnings as the old module gets its namespace stomped.
585 Not a lot you can do about that. All modules must be in Bot::Pluggable::Module::
586 namespace. Well, that's not really a bug.
587  
588 =head1 REQUIREMENTS
589  
590 Bot::BasicBot::Pluggable is based on POE, and really needs the latest version.
591 Because POE is like that sometimes. You also need L<POE::Component::IRC>.
592 Oh, and L<Bot::BasicBot>. Some of the modules will need more modules, e.g.
593 Google.pm needs L<Net::Google>. See the module docs for more details.
594  
595 =head1 LICENSE
596  
597 This program is free software; you can redistribute it
598 and/or modify it under the same terms as Perl itself.
599  
600 =head1 AUTHOR
601  
602 David Precious (BIGPRESH) <davidp@preshweb.co.uk>
603  
604 I am merely the current maintainer; however, the AUTHOR heading is traditional.
605  
606 =head1 CONTRIBUTORS
607  
608 =over 2
609  
610 =item * Mario Domgoergen <mdom@cpan.org>
611  
612 =item * Tom Insam <tom@jerakeen.org>
613  
614 =item * David Precious <davidp@preshweb.co.uk>
615  
616 =item * Mike Eldridge <diz@cpan.org>
617  
618 =back
619  
620 =head1 CREDITS
621  
622 Bot::BasicBot was written initially by Mark Fowler, and worked on heavily by
623 Simon Kent, who was kind enough to apply some patches we needed for Pluggable.
624 Eventually. Oh, yeah, and I stole huge chunks of docs from the Bot::BasicBot
625 source too. I spent a lot of time in the mozbot code, and that has influenced
626 my ideas for Pluggable. Mostly to get round its awfulness.
627  
628 Various people helped with modules. Convert was almost ported from the
629 infobot code by blech. But not quite. Thanks for trying... blech has also put
630 a lot of effort into the chump.cgi & chump.tem files in the examples/ folder,
631 including some /inspired/ calendar evilness.
632  
633 And thanks to the rest of #2lmc who were my unwilling guinea pigs during
634 development. And who kept suggesting totally stupid ideas for modules that I
635 then felt compelled to go implement. Shout.pm owes its existence to #2lmc.
636  
637 Thanks to Mike Eldridge (DIZ), maintainer until 2016, for kindly transferring
638 maintainership to me, BIGPRESH, so I can keep this useful bot framework alive.
639  
640 =head1 SEE ALSO
641  
642 =over 2
643  
644 =item * L<POE>
645  
646 =item * L<POE::Component::IRC>
647  
648 =item * L<Bot::BasicBot>
649  
650 =item * Infobot: http://www.infobot.org/
651  
652 =item * Mozbot: http://www.mozilla.org/projects/mozbot/
653  
654 =back
655  
656 =cut