lib/Bot/BasicBot/Pluggable.pm

filter: Statementstmt 74.7% 166 / 222 Branchbran 48.6% 35 / 72 Conditioncond 51.8% 14 / 27 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 75.0% 36 / 48 Podpod 85.7% 18 / 21 totaltotal 67.3% 272 / 404 CC 54 SCAR 18.0
CC
54
Cov
67%
init
15.0
reload
12.0
unload
12.0
CRAP 6.0
« lib/Bot/BasicBot/Pluggable/Store.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Module/DNS.pm »
1 package Bot::BasicBot::Pluggable;
2 10 $Bot::BasicBot::Pluggable::VERSION = '1.11';
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Bot::BasicBot 0.60;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Log::Log4perl::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use base qw( Bot::BasicBot );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 $Data::Dumper::Terse = 1;
15 10 $Data::Dumper::Indent = 0;
16  
17 use Module::Pluggable
18 10 sub_name => '_available',
19 search_path => 'Bot::BasicBot::Pluggable::Module',
20 10 ▶ except => 'Bot::BasicBot::Pluggable::Module::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Bot::BasicBot::Pluggable::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use Bot::BasicBot::Pluggable::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 sub init {
27 8 ▶ my $self = shift;
Subroutine init: called CC=6 SCAR=27.1
Pod documented
28 8 $self->init_logging();
29  
30 8 my $logger = Log::Log4perl->get_logger( ref $self );
31 8 $logger->info( 'Starting initialization of ' . ref $self );
32  
33 8 ▶ if ( !$self->store ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->store) { } 0 8 8
elsif (not UNIVERSAL::isa($self->store, 'Bot::BasicBot::Pluggable::Store')) { } 5 3 8
34 0 $logger->debug('Store not set, trying to load a store backend');
35 0 my $store;
36 0 for my $type (qw( DBI Deep Storable Memory )) {
37 $store = try {
38 0 ▶ $logger->debug("Trying to load store backend $type");
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 Bot::BasicBot::Pluggable::Store->new( { type => $type } );
40 0 };
41 0 ▶ if ($store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) 0 0 0
42 0 $logger->info("Loaded store backend $type");
43 0 last;
44 }
45 }
46 0 ▶ if ( !UNIVERSAL::isa( $store, 'Bot::BasicBot::Pluggable::Store' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($store, "Bot::BasicBot::Pluggable::Store")) 0 0 0
47 0 $logger->logdie("Couldn't load any default store type");
48 }
49 0 $self->store($store);
50 }
51 elsif ( !UNIVERSAL::isa( $self->store, "Bot::BasicBot::Pluggable::Store" ) )
52 {
53 5 $self->store( Bot::BasicBot::Pluggable::Store->new( $self->store ) );
54 }
55 8 return 1;
56 }
57  
58 sub init_logging {
59 8 ▶ my $self = shift;
Subroutine init_logging: called CC=2 SCAR=7.2
Pod undocumented
60 8 my $logger = Log::Log4perl->get_logger( ref $self );
61 8 ▶ if ( $self->logconfig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logconfig) { } 0 8 8
62 0 Log::Log4perl->init( $self->logconfig );
63 }
64 else {
65 8 my $loglevel = $self->loglevel;
66 8 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 0 ▶ my $self = shift;
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
78 0 my $logger = Log::Log4perl->get_logger( ref $self );
79 0 for my $log_entry (@_) {
80 0 chomp $log_entry;
81 0 $logger->warn($log_entry);
82 }
83 0 return;
84 }
85  
86 sub load {
87 14 ▶ my $self = shift;
Subroutine load: called CC=8 SCAR=21.1
Pod documented
88 14 my $module = shift;
89  
90 14 my $logger = Log::Log4perl->get_logger( ref $self );
91  
92 # it's safe to die here, mostly this call is eval'd.
93 14 ▶ $logger->logdie("Cannot load module without a name") unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 14 14
94 14 ▶ $logger->logdie("Module $module already loaded") if $self->handler($module);
Branch: 1 branch
BranchTrueFalseTotal
if $self->handler($module) 0 14 14
95  
96 # This is possible a leeeetle bit evil.
97 14 $logger->info("Loading module $module");
98 14 my $filename = $module;
99 14 $filename =~ s{::}{/}g;
100 14 my $file = "Bot/BasicBot/Pluggable/Module/$filename.pm";
101 14 ▶ $file = "./$filename.pm" if ( -e "./$filename.pm" );
Branch: 1 branch
BranchTrueFalseTotal
if -e "./$filename.pm" 1 13 14
102 14 ▶ $file = "./modules/$filename.pm" if ( -e "./modules/$filename.pm" );
Branch: 1 branch
BranchTrueFalseTotal
if -e "./modules/$filename.pm" 1 13 14
103 14 $logger->debug("Loading module $module from file $file");
104 14 warn "Loading $module from $file";
105  
106 # force a reload of the file (in the event that we've already loaded it).
107 10 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 14 delete $INC{$file};
109  
110 14 ▶ try { require $file } catch { die "Can't load $module: $_"; };
Subroutine __ANON__: called CC=1 SCAR=0.0
111  
112 # Ok, it's very evil. Don't bother me, I'm working.
113  
114 14 my $m = "Bot::BasicBot::Pluggable::Module::$module"->new(
115 Bot => $self,
116 Param => \@_
117 );
118  
119 14 ▶ $logger->logdie("->new didn't return an object") unless ( $m and ref($m) );
Branch: 1 branch
BranchTrueFalseTotal
unless $m and ref $m 0 14 14
Condition33%1/3
$m and ref $m
!ll&&!rl&&r
0014
MC/DC0%0/2
$m and ref $m
$m ref $m
Truth table33%1/3
A and B
A= $mB= ref $m
ABresult
0X0
100
111
120 14 ▶ $logger->logdie( ref($m) . " isn't a $module" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref($m) =~ /\Q$module\E/ 0 14 14
121 unless ref($m) =~ /\Q$module/;
122  
123 14 $self->add_handler( $m, $module );
124  
125 14 return $m;
126 }
127  
128 sub reload {
129 0 ▶ my $self = shift;
Subroutine reload: not called CC=3 SCAR=24.8
Pod documented
130 0 my $module = shift;
131 0 my $logger = Log::Log4perl->get_logger( ref $self );
132 0 ▶ $logger->logdie("Cannot reload module with a name") unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
133 0 ▶ $self->remove_handler($module) if $self->handler($module);
Branch: 1 branch
BranchTrueFalseTotal
if $self->handler($module) 0 0 0
134 0 return $self->load($module);
135 }
136  
137 sub unload {
138 0 ▶ my $self = shift;
Subroutine unload: not called CC=3 SCAR=24.8
Pod documented
139 0 my $module = shift;
140 0 my $logger = Log::Log4perl->get_logger( ref $self );
141 0 ▶ $logger->logdie("Need name") unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
142 0 ▶ $logger->logdie("Not loaded") unless $self->handler($module);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->handler($module) 0 0 0
143 0 $logger->info("Unloading module $module");
144 0 $self->remove_handler($module);
145 }
146  
147 sub module {
148 54 ▶ my $self = shift;
Subroutine module: called CC=1 SCAR=0.0
Pod documented
149 54 return $self->handler(@_);
150 }
151  
152 sub modules {
153 0 ▶ my $self = shift;
Subroutine modules: not called CC=1 SCAR=6.9
Pod documented
154 0 return $self->handlers(@_);
155 }
156  
157 sub available_modules {
158 1 ▶ my $self = shift;
Subroutine available_modules: called CC=1 SCAR=0.0
Pod documented
159 my @local_modules =
160 1 map { substr( ( File::Spec->splitpath($_) )[2], 0, -3 ) } glob('./*.pm'),
161 glob('./modules/*.pm');
162 my @central_modules =
163 map {
164 1 my $mod = $_;
165 11 $mod =~ s/^Bot::BasicBot::Pluggable::Module:://;
166 11 $mod;
167 } $self->_available();
168 1 my @modules = sort @local_modules, @central_modules;
169 1 return @modules;
170 }
171  
172 # deprecated methods
173 sub handler {
174 1059 ▶ my ( $self, $name ) = @_;
Subroutine handler: called CC=1 SCAR=0.0
Pod documented
175 1059 return $self->{handlers}{ lc($name) };
176 }
177  
178 sub handlers {
179 961 ▶ my $self = shift;
Subroutine handlers: called CC=7 SCAR=23.4
Pod undocumented
180 my @keys = sort {
181 10 my $xa = $self->handler($a);
182 10 my $xb = $self->handler($b);
183 (
184 10 ▶ ($xb->get('user_priority') || $xb->get('priority') || 0)
Branch: 1 branch
BranchTrueFalseTotal
($xb->get("user_priority") || $xb->get("priority") || 0) <=> ($xa->get("user_priority") || $xa->get("priority") || 0) or $a cmp $b 10 0 10
Condition40%4/10
$xb->get('user_priority') || $xb->get('priority') || 0
l!l
010
$xb->get("user_priority") or $xb->get("priority")
l!l&&r!l&&!r
0010
$xa->get('user_priority') || $xa->get('priority') || 0
l!l
010
$xa->get("user_priority") or $xa->get("priority")
l!l&&r!l&&!r
0010
MC/DC0%0/2
$xb->get('user_priority') || $xb->get('priority') || 0
$xb->get("user_priority") $xb->get("priority")
MC/DC0%0/2
$xa->get('user_priority') || $xa->get('priority') || 0
$xa->get("user_priority") $xa->get("priority")
Truth table33%1/3
A or B
A= $xb->get("user_priority")B= $xb->get("priority")
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $xa->get("user_priority")B= $xa->get("priority")
ABresult
1X1
011
000
185 <=>
186 ($xa->get('user_priority') || $xa->get('priority') || 0)
187 ) || ($a cmp $b)
188 961 } keys( %{ $self->{handlers} } );
189 961 ▶ return @keys if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 961 0 961
190 0 return \@keys;
191 }
192  
193 sub add_handler {
194 14 ▶ my ( $self, $handler, $name ) = @_;
Subroutine add_handler: called CC=3 SCAR=11.3
Pod documented
195 14 my $logger = Log::Log4perl->get_logger( ref $self );
196 14 ▶ $logger->logdie("Need a name for adding a handler") unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 14 14
197 $logger->logdie("Can't load a handler with a duplicate name $name")
198 14 ▶ if $self->{handlers}{ lc($name) };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handlers"}{lc $name} 0 14 14
199 14 $self->{handlers}{ lc($name) } = $handler;
200 }
201  
202 sub remove_handler {
203 0 ▶ my ( $self, $name ) = @_;
Subroutine remove_handler: not called CC=3 SCAR=24.8
Pod documented
204 0 my $logger = Log::Log4perl->get_logger( ref $self );
205 0 ▶ $logger->logdie("Need a name for removing a handler") unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
206 $logger->logdie("Hander $name not defined")
207 0 ▶ unless $self->{handlers}{ lc($name) };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handlers"}{lc $name} 0 0 0
208 0 $self->{handlers}{ lc($name) }->stop();
209 0 delete $self->{handlers}{ lc($name) };
210 }
211  
212 sub store {
213 1335 ▶ my $self = shift;
Subroutine store: called CC=2 SCAR=6.9
Pod documented
214 1335 ▶ $self->{store_object} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 1322 1335
215 1335 return $self->{store_object};
216 }
217  
218 sub loglevel {
219 11 ▶ my $self = shift;
Subroutine loglevel: called CC=4 SCAR=13.9
Pod documented
220 11 ▶ $self->{loglevel} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 8 11
221 11 ▶ return ($self->{loglevel} and uc $self->{loglevel}) || 'WARN';
Condition80%4/5
$self->{'loglevel'} && uc $self->{'loglevel'} || 'WARN'
l!l
65
$self->{"loglevel"} and uc $self->{"loglevel"}
!ll&&!rl&&r
506
MC/DC50%1/2
$self->{'loglevel'} && uc $self->{'loglevel'} || 'WARN'
$self->{"loglevel"} uc $self->{"loglevel"}
Truth table66%2/3
A and B
A= $self->{"loglevel"}B= uc $self->{"loglevel"}
ABresult
111
0X0
100
222 }
223  
224 sub logconfig {
225 11 ▶ my $self = shift;
Subroutine logconfig: called CC=2 SCAR=6.9
Pod documented
226 11 ▶ $self->{logconfig} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 8 11
227 11 return $self->{logconfig};
228 }
229  
230 sub dispatch {
231 214 ▶ my ( $self, $method, @args ) = @_;
Subroutine dispatch: called CC=6 SCAR=23.0
Pod documented
232 214 my $logger = Log::Log4perl->get_logger( ref $self );
233  
234 214 $logger->info("Dispatching $method");
235 214 for my $who ( $self->handlers ) {
236 ## Otherwise we would see tick every five seconds
237 216 ▶ if ( $method eq 'tick' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'tick') { } 0 216 216
238 0 $logger->trace("Trying to dispatch $method to $who");
239 }
240 else {
241 216 $logger->debug("Trying to dispatch $method to $who");
242 }
243 216 ▶ $logger->trace( "... with " . Dumper(@args) )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace and @args 0 216 216
Condition33%1/3
$logger->is_trace and @args
!ll&&!rl&&r
21600
MC/DC0%0/2
$logger->is_trace and @args
$logger->is_trace @args
Truth table33%1/3
A and B
A= $logger->is_traceB= @args
ABresult
0X0
100
111
244 if $logger->is_trace && @args;
245  
246 216 ▶ next unless $self->handler($who)->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->handler($who)->can($method) 216 0 216
247 try {
248 0 ▶ $logger->trace(
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 0 0 0
249 "Dispatching $method to $who with " . Dumper(@args) )
250 if $logger->is_trace;
251 0 $self->handler($who)->$method(@args);
252 }
253 catch {
254 0 ▶ $logger->warn($_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 }
256 0 }
257 214 return;
258 }
259  
260 sub help {
261 1 ▶ my $self = shift;
Subroutine help: called CC=4 SCAR=16.0
Pod documented
262 1 my $mess = shift;
263 1 $mess->{body} =~ s/^help\s*//i;
264 1 my $logger = Log::Log4perl->get_logger( ref $self );
265  
266 1 ▶ unless ( $mess->{body} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mess->{'body'}) { } 1 0 1
if (do { $mess->{'body'} eq 'modules' }) { } 0 1 1
267 return
268 0 "Ask me for help about: "
269 . join( ", ", $self->handlers() )
270 . " (say 'help <modulename>').";
271 }
272 elsif ( $mess->{body} eq 'modules' ) {
273 0 return "These modules are available for loading: "
274 . join( ", ", $self->available_modules );
275 }
276 else {
277 1 ▶ if ( my $handler = $self->handler( $mess->{body} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $self->handler($mess->{'body'})) { } 1 0 1
278 try {
279 1 ▶ return $handler->help($mess);
Subroutine __ANON__: called CC=1 SCAR=0.0
280 }
281 catch {
282 0 ▶ $logger->warn(
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 "Error calling help for handler $mess->{body}: $_");
284 }
285 1 }
286 else {
287 0 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 0 ▶ my $self = shift;
Subroutine tick: not called CC=1 SCAR=6.9
Pod documented
297 0 $self->dispatch('tick');
298 0 return 5;
299 }
300  
301 sub dispatch_priorities {
302 230 ▶ my ( $self, $event, $mess ) = @_;
Subroutine dispatch_priorities: called CC=9 SCAR=22.3
Pod undocumented
303 230 my $response;
304 my $who;
305  
306 230 my $logger = Log::Log4perl->get_logger( ref $self );
307 230 $logger->info('Dispatching said event');
308  
309 230 for my $priority ( 0 .. 3 ) {
310 747 for my $handler ( $self->handlers ) {
311 754 my $response;
312 754 $logger->debug(
313 "Trying to dispatch said to $handler on priority $priority");
314 754 ▶ $logger->trace( '... with arguments ' . Dumper($mess) )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace and $mess 0 754 754
Condition33%1/3
$logger->is_trace and $mess
!ll&&!rl&&r
75400
MC/DC0%0/2
$logger->is_trace and $mess
$logger->is_trace $mess
Truth table33%1/3
A and B
A= $logger->is_traceB= $mess
ABresult
0X0
100
111
315 if $logger->is_trace and $mess;
316 try {
317 754 ▶ $response =
Subroutine __ANON__: called CC=1 SCAR=0.0
318 $self->handler($handler)->$event( $mess, $priority );
319 }
320 catch {
321 0 ▶ $logger->warn($_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 754 };
323 754 ▶ if ( $priority and $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priority and $response) 213 541 754
Condition100%3/3
$priority and $response
!ll&&!rl&&r
232309213
MC/DC100%2/2
$priority and $response
$priority $response
Truth table100%3/3
A and B
A= $priorityB= $response
ABresult
0X0
100
111
324 213 $logger->debug("Response by $handler on $priority");
325 213 ▶ $logger->trace( 'Response is ' . Dumper($response) )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 0 213 213
326 if $logger->is_trace;
327 213 ▶ return if $response eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $response eq 1 1 212 213
328 212 $self->reply( $mess, $response );
329 212 return;
330 }
331 }
332 }
333 17 return;
334 }
335  
336 sub reply {
337 214 ▶ my ( $self, $mess, @other ) = @_;
Subroutine reply: called CC=2 SCAR=7.4
Pod documented
338 214 $self->dispatch( 'replied', {%$mess}, @other );
339 214 ▶ if ( $mess->{reply_hook} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->{'reply_hook'}) { } 214 0 214
340 214 return $mess->{reply_hook}->( $mess, @other );
341 }
342 else {
343 0 return $self->SUPER::reply( $mess, @other );
344 }
345 }
346  
347 BEGIN {
348 10 ▶ my @dispatchable_events = (
Subroutine BEGIN: called CC=3 SCAR=11.0
349 qw/
350 connected chanjoin chanpart userquit nick_change
351 topic kicked
352 /
353 );
354 10 my @priority_events = (qw/ said emoted /);
355 {
356 ## no critic qw(ProhibitNoStrict)
357 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
358 10 for my $event (@dispatchable_events) {
359 *$event = sub {
360 0 ▶ shift->dispatch( $event, @_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
361 70 };
362 }
363 10 for my $event (@priority_events) {
364 *$event = sub {
365 230 ▶ shift->dispatch_priorities( $event, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
366 20 };
367 }
368 }
369 }
370  
371 10 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
« lib/Bot/BasicBot/Pluggable/Store.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Module/DNS.pm »