blib/lib/Bot/BasicBot.pm

filter: Statementstmt 10.8% 46 / 425 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 16.0% 14 / 87 Podpod 66.1% 45 / 68 totaltotal 13.4% 105 / 780 CC 94 SCAR 34.8
CC
94
Cov
8%
irc_received_state
182.0
say
132.0
forkit
56.0
CRAP 32.3
↑ Summary
1 package Bot::BasicBot;
2 2 our $AUTHORITY = 'cpan:HINRIK';
3 2 $Bot::BasicBot::VERSION = '0.90';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IRC::Utils qw(decode_irc);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use POE::Kernel;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POE::Wheel::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use POE::Component::IRC::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use POE::Component::IRC::Plugin::Connector;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Text::Wrap ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 our @EXPORT = qw(say emote);
21  
22 sub new {
23 0 ▶ my $class = shift;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
24 0 my $self = bless {}, $class;
25  
26 0 $self->{IRCNAME} = 'wanna'.int(rand(100000));
27 0 $self->{ALIASNAME} = 'pony'.int(rand(100000));
28  
29 # call the set methods
30 0 my %args = @_;
31 0 for my $method (keys %args) {
32 0 ▶ if ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 0 0 0
33 0 $self->$method($args{$method});
34 }
35 else {
36 0 $self->{$method} = $args{$method};
37 #croak "Invalid argument '$method'";
38 }
39 }
40 0 ▶ $self->{charset} = 'utf8' if !defined $self->{charset};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"charset"} 0 0 0
41  
42 0 ▶ $self->init or die "init did not return a true value - dying";
Branch: 1 branch
BranchTrueFalseTotal
$self->init or die "init did not return a true value - dying" 0 0 0
43  
44 0 return $self;
45 }
46  
47 sub run {
48 0 ▶ my $self = shift;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
49  
50 # create the callbacks to the object states
51 0 POE::Session->create(
52 object_states => [
53 $self => {
54 _start => "start_state",
55 die => "die_state",
56  
57 irc_001 => "irc_001_state",
58 irc_msg => "irc_said_state",
59 irc_public => "irc_said_state",
60 irc_ctcp_action => "irc_emoted_state",
61 irc_notice => "irc_noticed_state",
62  
63 irc_disconnected => "irc_disconnected_state",
64 irc_error => "irc_error_state",
65  
66 irc_join => "irc_chanjoin_state",
67 irc_part => "irc_chanpart_state",
68 irc_kick => "irc_kicked_state",
69 irc_nick => "irc_nick_state",
70 irc_quit => "irc_quit_state",
71  
72 fork_close => "fork_close_state",
73 fork_error => "fork_error_state",
74  
75 irc_366 => "names_done_state",
76  
77 irc_332 => "topic_raw_state",
78 irc_topic => "topic_state",
79  
80 irc_shutdown => "shutdown_state",
81  
82 tick => "tick_state",
83 }
84 ]
85 );
86  
87 # and say that we want to recive said messages
88 0 $poe_kernel->post($self->{IRCNAME}, 'register', 'all');
89  
90 # run
91 0 ▶ $poe_kernel->run() if !$self->{no_run};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_run"} 0 0 0
92 0 return;
93 }
94  
95 0 ▶sub init { return 1; }
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
96  
97 0 ▶sub said { return }
Subroutine said: not called CC=1 SCAR=6.9
Pod documented
98  
99 sub emoted {
100 0 ▶ return shift->said(@_);
Subroutine emoted: not called CC=1 SCAR=6.9
Pod documented
101 }
102  
103 sub noticed {
104 0 ▶ return shift->said(@_);
Subroutine noticed: not called CC=1 SCAR=6.9
Pod documented
105 }
106  
107 0 ▶sub chanjoin { return }
Subroutine chanjoin: not called CC=1 SCAR=6.9
Pod documented
108  
109 0 ▶sub chanpart { return }
Subroutine chanpart: not called CC=1 SCAR=6.9
Pod documented
110  
111 0 ▶sub got_names { return }
Subroutine got_names: not called CC=1 SCAR=6.9
Pod documented
112  
113 0 ▶sub topic { return }
Subroutine topic: not called CC=1 SCAR=6.9
Pod documented
114  
115 0 ▶sub nick_change { return }
Subroutine nick_change: not called CC=1 SCAR=6.9
Pod documented
116  
117 0 ▶sub kicked { return }
Subroutine kicked: not called CC=1 SCAR=6.9
Pod documented
118  
119 0 ▶sub tick { return 0; }
Subroutine tick: not called CC=1 SCAR=6.9
Pod documented
120  
121 0 ▶sub help { return "Sorry, this bot has no interactive help." }
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
122  
123 0 ▶sub connected { return }
Subroutine connected: not called CC=1 SCAR=6.9
Pod documented
124  
125 sub userquit {
126 0 ▶ my ($self, $mess) = @_;
Subroutine userquit: not called CC=1 SCAR=6.9
Pod documented
127 0 return;
128 }
129  
130 sub schedule_tick {
131 0 ▶ my $self = shift;
Subroutine schedule_tick: not called CC=2 SCAR=17.9
Pod documented
132 0 ▶ my $time = shift || 5;
Condition0%0/2
shift() || 5
l!l
00
MC/DC0%0/1
shift() || 5
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
133 0 $poe_kernel->delay('tick', $time);
134 0 return;
135 }
136  
137 sub forkit {
138 0 ▶ my $self = shift;
Subroutine forkit: not called CC=7 SCAR=40.3
Pod documented
139 0 my $args;
140  
141 0 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
142 0 $args = shift;
143 }
144 else {
145 0 my %args = @_;
146 0 $args = \%args;
147 }
148  
149 0 ▶ return if !$args->{run};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"run"} 0 0 0
150  
151 0 ▶ $args->{handler} = $args->{handler} || "_fork_said";
Condition0%0/2
$args->{'handler'} || '_fork_said'
l!l
00
MC/DC0%0/1
$args->{'handler'} || '_fork_said'
$args->{'handler'}
Truth table0%0/2
A
A= $args->{'handler'}
Aresult
11
00
152 0 ▶ $args->{arguments} = $args->{arguments} || [];
Condition0%0/2
$args->{'arguments'} || []
l!l
00
MC/DC0%0/1
$args->{'arguments'} || []
$args->{'arguments'}
Truth table0%0/2
A
A= $args->{'arguments'}
Aresult
11
00
153  
154 #install a new handler in the POE kernel pointing to
155 # $self->{$args{handler}}
156 0 ▶ $poe_kernel->state( $args->{handler}, $args->{callback} || $self );
Condition0%0/3
$args->{'callback'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'callback'} || $self
$args->{'callback'} $self
Truth table0%0/3
A || B
A= $args->{'callback'}B= $self
ABresult
1X1
011
000
157  
158 0 my $run;
159 0 ▶ if (ref($args->{run}) =~ /^CODE/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($args->{'run'}) =~ /^CODE/) { } 0 0 0
160 $run = sub {
161 0 ▶ $args->{run}->($args->{body}, @{ $args->{arguments} })
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 0 };
163 }
164 else {
165 0 $run = $args->{run};
166 }
167  
168 0 my $wheel = POE::Wheel::Run->new(
169 Program => $run,
170 StdoutFilter => POE::Filter::Line->new(),
171 StderrFilter => POE::Filter::Line->new(),
172 StdoutEvent => "$args->{handler}",
173 StderrEvent => "fork_error",
174 CloseEvent => "fork_close"
175 );
176  
177 # Use a signal handler to reap dead processes
178 0 $poe_kernel->sig_child($wheel->PID, "got_sigchld");
179  
180 # store the wheel object in our bot, so we can retrieve/delete easily
181  
182 $self->{forks}{ $wheel->ID } = {
183 wheel => $wheel,
184 args => {
185 channel => $args->{channel},
186 who => $args->{who},
187 address => $args->{address}
188 }
189 0 };
190 0 return;
191 }
192  
193 sub _fork_said {
194 0 ▶ my ($self, $body, $wheel_id) = @_[OBJECT, ARG0, ARG1];
Subroutine _fork_said: not called CC=1 SCAR=6.9
195 0 chomp $body; # remove newline necessary to move data;
196  
197 # pick up the default arguments we squirreled away earlier
198 0 my $args = $self->{forks}{$wheel_id}{args};
199 0 $args->{body} = $body;
200  
201 0 $self->say($args);
202 0 return;
203 }
204  
205 sub say {
206 # If we're called without an object ref, then we're handling saying
207 # stuff from inside a forked subroutine, so we'll freeze it, and toss
208 # it out on STDOUT so that POE::Wheel::Run's handler can pick it up.
209 0 ▶ if (!ref $_[0]) {
Subroutine say: not called CC=11 SCAR=48.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 0 0
210 0 print $_[0], "\n";
211 0 return 1;
212 }
213  
214 # Otherwise, this is a standard object method
215  
216 0 my $self = shift;
217 0 my $args;
218 0 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
219 0 $args = shift;
220 }
221 else {
222 0 my %args = @_;
223 0 $args = \%args;
224 }
225  
226 0 my $body = $args->{body};
227  
228 # add the "Foo: bar" at the start
229 0 ▶ if ($args->{channel} ne "msg" && defined $args->{address}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"channel"} ne "msg" and defined $args->{"address"}) 0 0 0
Condition0%0/3
$args->{"channel"} ne "msg" and defined $args->{"address"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"channel"} ne "msg" and defined $args->{"address"}
$args->{"channel"} ne "msg" defined $args->{"address"}
Truth table0%0/3
A and B
A= $args->{"channel"} ne "msg"B= defined $args->{"address"}
ABresult
0X0
100
111
230 0 $body = "$args->{who}: $body";
231 }
232  
233 # work out who we're going to send the message to
234 0 ▶ my $who = $args->{channel} eq "msg" ? $args->{who} : $args->{channel};
Branch: 1 branch
BranchTrueFalseTotal
$args->{'channel'} eq 'msg' ? : 0 0 0
235  
236 0 ▶ if (!defined $who || !defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $who and defined $body) 0 0 0
Condition0%0/3
defined $who and defined $body
!ll&&!rl&&r
000
MC/DC0%0/2
defined $who and defined $body
defined $who defined $body
Truth table0%0/3
A and B
A= defined $whoB= defined $body
ABresult
0X0
100
111
237 0 $self->log("Can't send a message without target and body\n"
238 . " called from "
239 . ( [caller]->[0] )
240 . " line "
241 . ( [caller]->[2] ) . "\n"
242 . " who = '$who'\n body = '$body'\n");
243 0 return;
244 }
245  
246 # if we have a long body, split it up..
247 0 local $Text::Wrap::columns = 300;
248 0 local $Text::Wrap::unexpand = 0; # no tabs
249 0 my $wrapped = Text::Wrap::wrap('', '..', $body); # =~ m!(.{1,300})!g;
250 # I think the Text::Wrap docs lie - it doesn't do anything special
251 # in list context
252 0 my @bodies = split /\n+/, $wrapped;
253  
254 # Allows to override the default "PRIVMSG". Used by notice()
255 my $irc_command = defined $args->{irc_command}
256 0 ▶ && $args->{irc_command} eq 'notice'
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'irc_command'} && $args->{'irc_command'} eq 'notice' ? : 0 0 0
Condition0%0/3
defined $args->{"irc_command"} and $args->{"irc_command"} eq "notice"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"irc_command"} and $args->{"irc_command"} eq "notice"
defined $args->{"irc_command"} $args->{"irc_command"} eq "notice"
Truth table0%0/3
A and B
A= defined $args->{"irc_command"}B= $args->{"irc_command"} eq "notice"
ABresult
0X0
100
111
257 ? 'notice'
258 : 'privmsg';
259  
260 # post an event that will send the message
261 0 for my $body (@bodies) {
262 0 my ($enc_who, $enc_body) = $self->charset_encode($who, $body);
263 #warn "$enc_who => $enc_body\n";
264 $poe_kernel->post(
265 $self->{IRCNAME},
266 0 $irc_command,
267 $enc_who,
268 $enc_body,
269 );
270 }
271  
272 0 return;
273 }
274  
275 sub emote {
276 # If we're called without an object ref, then we're handling emoting
277 # stuff from inside a forked subroutine, so we'll freeze it, and
278 # toss it out on STDOUT so that POE::Wheel::Run's handler can pick
279 # it up.
280 0 ▶ if (!ref $_[0]) {
Subroutine emote: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 0 0
281 0 print $_[0], "\n";
282 0 return 1;
283 }
284  
285 # Otherwise, this is a standard object method
286  
287 0 my $self = shift;
288 0 my $args;
289 0 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
290 0 $args = shift;
291 }
292 else {
293 0 my %args = @_;
294 0 $args = \%args;
295 }
296  
297 0 my $body = $args->{body};
298  
299 # Work out who we're going to send the message to
300 my $who = $args->{channel} eq "msg"
301 ? $args->{who}
302 0 ▶ : $args->{channel};
Branch: 1 branch
BranchTrueFalseTotal
$args->{'channel'} eq 'msg' ? : 0 0 0
303  
304 # post an event that will send the message
305 # if there's a better way of sending actions i'd love to know - jw
306 # me too; i'll look at it in v0.5 - sb
307  
308 $poe_kernel->post(
309 $self->{IRCNAME},
310 0 'ctcp',
311 $self->charset_encode($who, "ACTION $body"),
312 );
313 0 return;
314 }
315  
316 sub notice {
317 0 ▶ if (!ref $_[0]) {
Subroutine notice: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 0 0
318 0 print $_[0], "\n";
319 0 return 1;
320 }
321  
322 0 my $self = shift;
323 0 my $args;
324 0 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
325 0 $args = shift;
326 }
327 else {
328 0 my %args = @_;
329 0 $args = \%args;
330 }
331  
332 # Don't modify '$args' hashref in-place, or we might
333 # make all subsequent calls into notices
334 return $self->say(
335 0 %{ $args },
336 irc_command => 'notice'
337 );
338  
339 }
340  
341 sub pocoirc {
342 0 ▶ my $self = shift;
Subroutine pocoirc: not called CC=1 SCAR=6.9
Pod documented
343 0 return $self->{IRCOBJ};
344 }
345  
346 sub reply {
347 0 ▶ my $self = shift;
Subroutine reply: not called CC=1 SCAR=6.9
Pod documented
348 0 my ($mess, $body) = @_;
349 0 my %hash = %$mess;
350 0 $hash{body} = $body;
351 0 return $self->say(%hash);
352 }
353  
354 sub channel_data {
355 0 ▶ my $self = shift;
Subroutine channel_data: not called CC=5 SCAR=34.0
Pod documented
356 0 ▶ my $channel = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $channel = shift() or return 0 0 0
357 0 my $irc = $self->{IRCOBJ};
358 0 my $channels = $irc->channels();
359 0 ▶ return if !exists $channels->{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $channels->{$channel} 0 0 0
360  
361 return {
362 map {
363 0 ▶ $_ => {
Condition0%0/4
$irc->is_channel_operator($channel, $_) || 0
l!l
00
$irc->has_channel_voice($channel, $_) || 0
l!l
00
MC/DC0%0/1
$irc->is_channel_operator($channel, $_) || 0
$irc->is_channel_operator($channel, $_)
MC/DC0%0/1
$irc->has_channel_voice($channel, $_) || 0
$irc->has_channel_voice($channel, $_)
Truth table0%0/2
A
A= $irc->is_channel_operator($channel, $_)
Aresult
11
00
Truth table0%0/2
A
A= $irc->has_channel_voice($channel, $_)
Aresult
11
00
364 op => $irc->is_channel_operator($channel, $_) || 0,
365 voice => $irc->has_channel_voice($channel, $_) || 0,
366 }
367 } $irc->channel_list($channel)
368 };
369 }
370  
371 sub server {
372 0 ▶ my $self = shift;
Subroutine server: not called CC=3 SCAR=24.8
Pod documented
373 0 ▶ $self->{server} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
374 0 ▶ return $self->{server} || "irc.perl.org";
Condition0%0/2
$self->{'server'} || 'irc.perl.org'
l!l
00
MC/DC0%0/1
$self->{'server'} || 'irc.perl.org'
$self->{'server'}
Truth table0%0/2
A
A= $self->{'server'}
Aresult
11
00
375 }
376  
377 sub port {
378 0 ▶ my $self = shift;
Subroutine port: not called CC=3 SCAR=24.8
Pod documented
379 0 ▶ $self->{port} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
380 0 ▶ return $self->{port} || "6667";
Condition0%0/2
$self->{'port'} || '6667'
l!l
00
MC/DC0%0/1
$self->{'port'} || '6667'
$self->{'port'}
Truth table0%0/2
A
A= $self->{'port'}
Aresult
11
00
381 }
382  
383 sub password {
384 0 ▶ my $self = shift;
Subroutine password: not called CC=3 SCAR=24.8
Pod documented
385 0 ▶ $self->{password} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
386 0 ▶ return $self->{password} || undef;
Condition0%0/2
$self->{'password'} || undef
l!l
00
MC/DC0%0/1
$self->{'password'} || undef
$self->{'password'}
Truth table0%0/2
A
A= $self->{'password'}
Aresult
11
00
387 }
388  
389 sub ssl {
390 0 ▶ my $self = shift;
Subroutine ssl: not called CC=3 SCAR=24.8
Pod documented
391 0 ▶ $self->{ssl} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
392 0 ▶ return $self->{ssl} || 0;
Condition0%0/2
$self->{'ssl'} || 0
l!l
00
MC/DC0%0/1
$self->{'ssl'} || 0
$self->{'ssl'}
Truth table0%0/2
A
A= $self->{'ssl'}
Aresult
11
00
393 }
394  
395 sub localaddr {
396 0 ▶ my $self = shift;
Subroutine localaddr: not called CC=3 SCAR=24.8
Pod documented
397 0 ▶ $self->{localaddr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
398 0 ▶ return $self->{localaddr} || 0;
Condition0%0/2
$self->{'localaddr'} || 0
l!l
00
MC/DC0%0/1
$self->{'localaddr'} || 0
$self->{'localaddr'}
Truth table0%0/2
A
A= $self->{'localaddr'}
Aresult
11
00
399 }
400  
401 sub useipv6 {
402 0 ▶ my $self = shift;
Subroutine useipv6: not called CC=3 SCAR=24.8
Pod documented
403 0 ▶ $self->{useipv6} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
404 0 ▶ return $self->{useipv6} || 0;
Condition0%0/2
$self->{'useipv6'} || 0
l!l
00
MC/DC0%0/1
$self->{'useipv6'} || 0
$self->{'useipv6'}
Truth table0%0/2
A
A= $self->{'useipv6'}
Aresult
11
00
405 }
406  
407 sub nick {
408 0 ▶ my $self = shift;
Subroutine nick: not called CC=3 SCAR=24.8
Pod documented
409 0 ▶ $self->{nick} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
410 0 ▶ return $self->{nick} if defined $self->{nick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"nick"} 0 0 0
411 0 return _random_nick();
412 }
413  
414 sub _random_nick {
415 0 ▶ my @things = ( 'a' .. 'z' );
Subroutine _random_nick: not called CC=1 SCAR=6.9
416 0 return join '', ( map { @things[ rand @things ] } 0 .. 4 ), "bot";
417 }
418  
419 sub alt_nicks {
420 0 ▶ my $self = shift;
Subroutine alt_nicks: not called CC=4 SCAR=30.0
Pod documented
421 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
422 # make sure we copy
423 0 ▶ my @args = ( ref $_[0] eq "ARRAY" ) ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
424 0 $self->{alt_nicks} = \@args;
425 }
426 0 ▶ return @{ $self->{alt_nicks} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"alt_nicks"} or [] 0 0 0
427 }
428  
429 sub username {
430 0 ▶ my $self = shift;
Subroutine username: not called CC=3 SCAR=24.8
Pod documented
431 0 ▶ $self->{username} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
432 0 ▶ return defined $self->{username} ? $self->{username} : $self->nick;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'username'} ? : 0 0 0
433 }
434  
435 sub name {
436 0 ▶ my $self = shift;
Subroutine name: not called CC=3 SCAR=24.8
Pod documented
437 0 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
438 0 ▶ return defined $self->{name} ? $self->{name} : $self->nick . " bot";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'name'} ? : 0 0 0
439 }
440  
441 sub channels {
442 0 ▶ my $self = shift;
Subroutine channels: not called CC=4 SCAR=30.0
Pod documented
443 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
444 # make sure we copy
445 0 ▶ my @args = ( ref $_[0] eq "ARRAY" ) ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
446 0 $self->{channels} = \@args;
447 }
448 0 ▶ return @{ $self->{channels} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"channels"} or [] 0 0 0
449 }
450  
451 sub quit_message {
452 0 ▶ my $self = shift;
Subroutine quit_message: not called CC=3 SCAR=24.8
Pod documented
453 0 ▶ $self->{quit_message} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
454 0 ▶ return defined $self->{quit_message} ? $self->{quit_message} : "Bye";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'quit_message'} ? : 0 0 0
455 }
456  
457 sub ignore_list {
458 0 ▶ my $self = shift;
Subroutine ignore_list: not called CC=4 SCAR=30.0
Pod documented
459 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
460 # make sure we copy
461 0 ▶ my @args = ( ref $_[0] eq "ARRAY" ) ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
462 0 $self->{ignore_list} = \@args;
463 }
464 0 ▶ return @{ $self->{ignore_list} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ignore_list"} or [] 0 0 0
465 }
466  
467 sub charset {
468 0 ▶ my $self = shift;
Subroutine charset: not called CC=2 SCAR=17.9
Pod documented
469 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
470 0 $self->{charset} = shift;
471 }
472 0 return $self->{charset};
473 }
474  
475 sub flood {
476 0 ▶ my $self = shift;
Subroutine flood: not called CC=2 SCAR=17.9
Pod documented
477 0 ▶ $self->{flood} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
478 0 return $self->{flood};
479 }
480  
481 sub no_run {
482 0 ▶ my $self = shift;
Subroutine no_run: not called CC=2 SCAR=17.9
Pod documented
483 0 ▶ $self->{no_run} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
484 0 return $self->{no_run};
485 }
486  
487 sub start_state {
488 0 ▶ my ($self, $kernel, $session) = @_[OBJECT, KERNEL, SESSION];
Subroutine start_state: not called CC=1 SCAR=6.9
Pod undocumented
489 0 $kernel->sig('DIE', 'die');
490 0 $self->{session} = $session;
491  
492 # Make an alias for our session, to keep it from getting GC'ed.
493 0 $kernel->alias_set($self->{ALIASNAME});
494 0 $kernel->delay('tick', 30);
495  
496 $self->{IRCOBJ} = POE::Component::IRC::State->spawn(
497 alias => $self->{IRCNAME},
498 0 );
499 $self->{IRCOBJ}->plugin_add(
500 0 'Connector',
501 POE::Component::IRC::Plugin::Connector->new(),
502 );
503 0 $kernel->post($self->{IRCNAME}, 'register', 'all');
504  
505 $kernel->post(
506 $self->{IRCNAME},
507 0 'connect',
508 {
509 Nick => $self->nick,
510 Server => $self->server,
511 Port => $self->port,
512 Password => $self->password,
513 UseSSL => $self->ssl,
514 Flood => $self->flood,
515 LocalAddr => $self->localaddr,
516 useipv6 => $self->useipv6,
517 $self->charset_encode(
518 Nick => $self->nick,
519 Username => $self->username,
520 Ircname => $self->name,
521 ),
522 },
523 );
524  
525 0 return;
526 }
527  
528 sub die_state {
529 0 ▶ my ($kernel, $self, $ex) = @_[KERNEL, OBJECT, ARG1];
Subroutine die_state: not called CC=1 SCAR=6.9
Pod undocumented
530 0 warn $ex->{error_str};
531 0 $self->{IRCOBJ}->yield('shutdown');
532 0 $kernel->sig_handled();
533 0 return;
534 }
535  
536 sub irc_001_state {
537 0 ▶ my ($self, $kernel) = @_[OBJECT, KERNEL];
Subroutine irc_001_state: not called CC=2 SCAR=17.9
Pod undocumented
538  
539 # ignore all messages from ourselves
540 $kernel->post(
541 $self->{IRCNAME},
542 0 'ignore',
543 $self->charset_encode($self->nick),
544 );
545  
546 # connect to the channel
547 0 for my $channel ($self->channels) {
548 0 $self->log("Trying to connect to '$channel'\n");
549 $kernel->post(
550 $self->{IRCNAME},
551 0 'join',
552 $self->charset_encode($channel),
553 );
554 }
555  
556 0 $self->schedule_tick(5);
557 0 $self->connected();
558 0 return;
559 }
560  
561 sub irc_disconnected_state {
562 0 ▶ my ($self, $kernel, $server) = @_[OBJECT, KERNEL, ARG0];
Subroutine irc_disconnected_state: not called CC=1 SCAR=6.9
Pod undocumented
563 0 $self->log("Lost connection to server $server.\n");
564 0 return;
565 }
566  
567 sub irc_error_state {
568 0 ▶ my ($self, $err, $kernel) = @_[OBJECT, ARG0, KERNEL];
Subroutine irc_error_state: not called CC=1 SCAR=6.9
Pod undocumented
569 0 $self->log("Server error occurred! $err\n");
570 0 return;
571 }
572  
573 sub irc_kicked_state {
574 0 ▶ my ($self, $kernel, $heap, $session) = @_[OBJECT, KERNEL, HEAP, SESSION];
Subroutine irc_kicked_state: not called CC=1 SCAR=6.9
Pod undocumented
575 0 my ($nickstring, $channel, $kicked, $reason) = @_[ARG0..$#_];
576 0 my $nick = $self->nick_strip($nickstring);
577 0 $_[OBJECT]->_remove_from_channel( $channel, $kicked );
578 0 $self->kicked(
579 {
580 channel => $channel,
581 who => $nick,
582 kicked => $kicked,
583 reason => $reason,
584 }
585 );
586 0 return;
587 }
588  
589 sub irc_join_state {
590 0 ▶ my ($self, $nick) = @_[OBJECT, ARG0];
Subroutine irc_join_state: not called CC=1 SCAR=6.9
Pod undocumented
591 0 return;
592 }
593  
594 sub irc_nick_state {
595 0 ▶ my ($self, $nick, $newnick) = @_[OBJECT, ARG0, ARG1];
Subroutine irc_nick_state: not called CC=1 SCAR=6.9
Pod undocumented
596 0 $nick = $self->nick_strip($nick);
597 0 $self->nick_change($nick, $newnick);
598 0 return;
599 }
600  
601 sub irc_quit_state {
602 0 ▶ my ($self, $kernel, $session) = @_[OBJECT, KERNEL, SESSION];
Subroutine irc_quit_state: not called CC=1 SCAR=6.9
Pod undocumented
603 0 my ($nick, $message) = @_[ARG0..$#_];
604  
605 0 $nick = $self->nick_strip($nick);
606 0 $self->userquit({ who => $nick, body => $message });
607 0 return;
608 }
609  
610 sub irc_said_state {
611 0 ▶ irc_received_state( 'said', 'say', @_ );
Subroutine irc_said_state: not called CC=1 SCAR=6.9
Pod undocumented
612 0 return;
613 }
614  
615 sub irc_emoted_state {
616 0 ▶ irc_received_state( 'emoted', 'emote', @_ );
Subroutine irc_emoted_state: not called CC=1 SCAR=6.9
Pod undocumented
617 0 return;
618 }
619  
620 sub irc_noticed_state {
621 0 ▶ irc_received_state( 'noticed', 'emote', @_ );
Subroutine irc_noticed_state: not called CC=1 SCAR=6.9
Pod undocumented
622 0 return;
623 }
624  
625 sub irc_received_state {
626 0 ▶ my $received = shift;
Subroutine irc_received_state: not called CC=13 SCAR=52.0
Pod undocumented
627 0 my $respond = shift;
628 0 my ($self, $nick, $to, $body) = @_[OBJECT, ARG0, ARG1, ARG2];
629  
630 0 ($nick, $to, $body) = $self->charset_decode($nick, $to, $body);
631  
632 0 my $return;
633 0 my $mess = {};
634  
635 # pass the raw body through
636 0 $mess->{raw_body} = $body;
637  
638 # work out who it was from
639 0 $mess->{who} = $self->nick_strip($nick);
640 0 $mess->{raw_nick} = $nick;
641  
642 # right, get the list of places this message was
643 # sent to and work out the first one that we're
644 # either a memeber of is is our nick.
645 # The IRC protocol allows messages to be sent to multiple
646 # targets, which is pretty clever. However, noone actually
647 # /does/ this, so we can get away with this:
648  
649 0 my $channel = $to->[0];
650 0 ▶ if (lc($channel) eq lc($self->nick)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $channel eq lc $self->nick) { } 0 0 0
651 0 $mess->{channel} = "msg";
652 0 $mess->{address} = "msg";
653 }
654 else {
655 0 $mess->{channel} = $channel;
656 }
657  
658 # okay, work out if we're addressed or not
659  
660 0 $mess->{body} = $body;
661 0 ▶ if ($mess->{channel} ne "msg") {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->{"channel"} ne "msg") 0 0 0
662 0 my $own_nick = $self->nick;
663  
664 0 ▶ if ($mess->{body} =~ s/^(\Q$own_nick\E)\s*[:,-]?\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->{"body"} =~ s/^(\Q$own_nick\E)\s*[:,-]?\s*//i) 0 0 0
665 0 $mess->{address} = $1;
666 }
667  
668 0 for my $alt_nick ($self->alt_nicks) {
669 0 ▶ last if $mess->{address};
Branch: 1 branch
BranchTrueFalseTotal
if $mess->{"address"} 0 0 0
670 0 ▶ if ($mess->{body} =~ s/^(\Q$alt_nick\E)\s*[:,-]?\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->{"body"} =~ s/^(\Q$alt_nick\E)\s*[:,-]?\s*//i) 0 0 0
671 0 $mess->{address} = $1;
672 }
673 }
674 }
675  
676 # strip off whitespace before and after the message
677 0 $mess->{body} =~ s/^\s+//;
678 0 $mess->{body} =~ s/\s+$//;
679  
680 # check if someone was asking for help
681 0 ▶ if ($mess->{address} && $mess->{body} =~ /^help/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->{"address"} and $mess->{"body"} =~ /^help/i) 0 0 0
Condition0%0/3
$mess->{"address"} and $mess->{"body"} =~ /^help/i
!ll&&!rl&&r
000
MC/DC0%0/2
$mess->{"address"} and $mess->{"body"} =~ /^help/i
$mess->{"address"} $mess->{"body"} =~ /^help/i
Truth table0%0/3
A and B
A= $mess->{"address"}B= $mess->{"body"} =~ /^help/i
ABresult
0X0
100
111
682 0 ▶ $mess->{body} = $self->help($mess) or return;
Branch: 1 branch
BranchTrueFalseTotal
$mess->{"body"} = $self->help($mess) or return 0 0 0
683 0 $self->say($mess);
684 0 return;
685 }
686  
687 # okay, call the said/emoted method
688 0 $return = $self->$received($mess);
689  
690 ### what did we get back?
691  
692 # nothing? Say nothing then
693 0 ▶ return if !defined $return;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $return 0 0 0
694  
695 # a string? Say it how we were addressed then
696 0 ▶ if (!ref $return && length $return) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $return and length $return) 0 0 0
Condition0%0/3
not ref $return and length $return
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $return and length $return
not ref $return length $return
Truth table0%0/3
not(A) and B
A= not ref $returnB= length $return
ABresult
0X0
100
111
697 0 $mess->{body} = $return;
698 0 $self->$respond($mess);
699 0 return;
700 }
701 }
702  
703 sub irc_chanjoin_state {
704 0 ▶ my $self = $_[OBJECT];
Subroutine irc_chanjoin_state: not called CC=3 SCAR=24.8
Pod undocumented
705 0 my ($channel, $nick) = @_[ ARG1, ARG0 ];
706 0 $nick = $_[OBJECT]->nick_strip($nick);
707  
708 0 ▶ if ($self->nick eq $nick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nick eq $nick) 0 0 0
709 0 my @channels = $self->channels;
710 0 ▶ push @channels, $channel unless grep { $_ eq $channel } @channels;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $channel;} @channels 0 0 0
711 0 $self->channels(\@channels);
712 }
713 0 irc_chan_received_state('chanjoin', 'say', @_);
714 0 return;
715 }
716  
717 sub irc_chanpart_state {
718 0 ▶ my $self = $_[OBJECT];
Subroutine irc_chanpart_state: not called CC=2 SCAR=17.9
Pod undocumented
719 0 my ($channel, $nick) = @_[ ARG1, ARG0 ];
720 0 $nick = $_[OBJECT]->nick_strip($nick);
721  
722 0 ▶ if ($self->nick eq $nick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nick eq $nick) 0 0 0
723 0 my @channels = $self->channels;
724 0 @channels = grep { $_ ne $channel } @channels;
725 0 $self->channels(\@channels);
726 }
727 0 irc_chan_received_state('chanpart', 'say', @_);
728 0 return;
729 }
730  
731 sub irc_chan_received_state {
732 0 ▶ my $received = shift;
Subroutine irc_chan_received_state: not called CC=3 SCAR=24.8
Pod undocumented
733 0 my $respond = shift;
734 0 my ($self, $nick, $channel) = @_[OBJECT, ARG0, ARG1];
735  
736 0 my $return;
737 0 my $mess = {};
738 0 $mess->{who} = $self->nick_strip($nick);
739 0 $mess->{raw_nick} = $nick;
740  
741 0 $mess->{channel} = $channel;
742 0 $mess->{body} = $received; #chanjoin or chanpart
743 0 $mess->{address} = "chan";
744  
745 # okay, call the chanjoin/chanpart method
746 0 $return = $self->$received($mess);
747  
748 ### what did we get back?
749  
750 # nothing? Say nothing then
751 0 ▶ return if !defined $return;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $return 0 0 0
752  
753 # a string? Say it how we were addressed then
754 0 ▶ if (!ref $return) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $return) 0 0 0
755 0 $mess->{body} = $return;
756 0 $self->$respond($mess);
757 0 return;
758 }
759 }
760  
761  
762 sub fork_close_state {
763 0 ▶ my ($self, $wheel_id) = @_[OBJECT, ARG0];
Subroutine fork_close_state: not called CC=1 SCAR=6.9
Pod undocumented
764 0 delete $self->{forks}{$wheel_id};
765 0 return;
766 }
767  
768 ▶sub fork_error_state { }
Subroutine fork_error_state: not called CC=1 SCAR=0.0
Pod undocumented
769  
770 sub tick_state {
771 0 ▶ my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
Subroutine tick_state: not called CC=2 SCAR=17.9
Pod undocumented
772 0 my $delay = $self->tick();
773 0 ▶ $self->schedule_tick($delay) if $delay;
Branch: 1 branch
BranchTrueFalseTotal
if $delay 0 0 0
774 0 return;
775 }
776  
777 sub names_done_state {
778 0 ▶ my ($self, $kernel, $server, $message) = @_[OBJECT, KERNEL, ARG0, ARG1];
Subroutine names_done_state: not called CC=1 SCAR=6.9
Pod undocumented
779 0 my ($channel) = split /\s/, $message;
780 0 $self->got_names(
781 {
782 channel => $channel,
783 names => $self->channel_data($channel),
784 }
785 );
786 0 return;
787 }
788  
789 sub topic_raw_state {
790 0 ▶ my ($self, $kernel, $server, $raw) = @_[OBJECT, KERNEL, ARG0, ARG1];
Subroutine topic_raw_state: not called CC=1 SCAR=6.9
Pod undocumented
791 0 my ($channel, $topic) = split / :/, $raw, 2;
792 0 $self->topic(
793 {
794 channel => $channel,
795 who => undef,
796 topic => $topic,
797 }
798 );
799 0 return;
800 }
801  
802 sub topic_state {
803 0 ▶ my ($self, $kernel, $nickraw, $channel, $topic)
Subroutine topic_state: not called CC=1 SCAR=6.9
Pod undocumented
804 = @_[OBJECT, KERNEL, ARG0, ARG1, ARG2];
805 0 my $nick = $self->nick_strip($nickraw);
806 0 $self->topic(
807 {
808 channel => $channel,
809 who => $nick,
810 topic => $topic,
811 }
812 );
813 0 return;
814 }
815  
816 sub shutdown_state {
817 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine shutdown_state: not called CC=2 SCAR=17.9
Pod undocumented
818 0 $kernel->delay('tick');
819 0 $kernel->alias_remove($self->{ALIASNAME});
820 0 for my $fork (values %{ $self->{forks} }) {
821 0 $fork->{wheel}->kill();
822 }
823 0 return;
824 }
825  
826 sub AUTOLOAD {
827 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
828 0 our $AUTOLOAD;
829 0 $AUTOLOAD =~ s/.*:://;
830 $poe_kernel->post(
831 $self->{IRCNAME},
832 0 $AUTOLOAD,
833 $self->charset_encode(@_),
834 );
835 0 return;
836 }
837  
838 # so it won't get AUTOLOADed
839 0 ▶sub DESTROY { return }
Subroutine DESTROY: not called CC=1 SCAR=6.9
840  
841 sub log {
842 0 ▶ my $self = shift;
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
843 0 for (@_) {
844 0 my $log_entry = $_;
845 0 chomp $log_entry;
846 0 print STDERR "$log_entry\n";
847 }
848 0 return;
849 }
850  
851 sub ignore_nick {
852 0 ▶ local $_ = undef;
Subroutine ignore_nick: not called CC=1 SCAR=6.9
Pod documented
853 0 my $self = shift;
854 0 my $nick = shift;
855 0 return grep { $nick eq $_ } @{ $self->{ignore_list} };
856 }
857  
858 sub nick_strip {
859 0 ▶ my $self = shift;
Subroutine nick_strip: not called CC=2 SCAR=17.9
Pod documented
860 0 ▶ my $combined = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
861 0 my ($nick) = $combined =~ m/(.*?)!/;
862 0 return $nick;
863 }
864  
865 sub charset_decode {
866 0 ▶ my $self = shift;
Subroutine charset_decode: not called CC=5 SCAR=34.0
Pod documented
867  
868 0 my @r;
869 0 for (@_) {
870 0 ▶ if (ref($_) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 0 0 0
elsif (ref $_ eq 'HASH') { } 0 0 0
elsif (ref $_) { } 0 0 0
871 0 push @r, [ $self->charset_decode(@$_) ];
872 }
873 elsif (ref($_) eq "HASH") {
874 0 push @r, { $self->charset_decode(%$_) };
875 }
876 elsif (ref($_)) {
877 0 die "Can't decode object $_\n";
878 }
879 else {
880 0 push @r, decode_irc($_);
881 }
882 }
883 #warn Dumper({ decoded => \@r });
884 0 return @r;
885 }
886  
887 sub charset_encode {
888 0 ▶ my $self = shift;
Subroutine charset_encode: not called CC=5 SCAR=34.0
Pod documented
889  
890 0 my @r;
891 0 for (@_) {
892 0 ▶ if (ref($_) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 0 0 0
elsif (ref $_ eq 'HASH') { } 0 0 0
elsif (ref $_) { } 0 0 0
893 0 push @r, [ $self->charset_encode(@$_) ];
894 }
895 elsif (ref($_) eq "HASH") {
896 0 push @r, { $self->charset_encode(%$_) };
897 }
898 elsif (ref($_)) {
899 0 die "Can't encode object $_\n";
900 }
901 else {
902 0 push @r, encode($self->charset, $_);
903 }
904 }
905 #warn Dumper({ encoded => \@r });
906 0 return @r;
907 }
908  
909 2 1;
910  
911 =head1 NAME
912  
913 Bot::BasicBot - simple irc bot baseclass
914  
915 =head1 SYNOPSIS
916  
917 #!/usr/bin/perl
918 use strict;
919 use warnings;
920  
921 # Subclass Bot::BasicBot to provide event-handling methods.
922 package UppercaseBot;
923 use base qw(Bot::BasicBot);
924  
925 sub said {
926 my $self = shift;
927 my $arguments = shift; # Contains the message that the bot heard.
928  
929 # The bot will respond by uppercasing the message and echoing it back.
930 $self->say(
931 channel => $arguments->{channel},
932 body => uc $arguments->{body},
933 );
934  
935 # The bot will shut down after responding to a message.
936 $self->shutdown('I have done my job here.');
937 }
938  
939 # Create an object of your Bot::BasicBot subclass and call its run method.
940 package main;
941  
942 my $bot = UppercaseBot->new(
943 server => 'irc.example.com',
944 port => '6667',
945 channels => ['#bottest'],
946 nick => 'UppercaseBot',
947 name => 'John Doe',
948 ignore_list => [ 'laotse', 'georgeburdell' ],
949 );
950 $bot->run();
951  
952 =head1 DESCRIPTION
953  
954 Basic bot system designed to make it easy to do simple bots, optionally
955 forking longer processes (like searches) concurrently in the background.
956  
957 There are several examples of bots using Bot::BasicBot in the examples/
958 folder in the Bot::BasicBot tarball.
959  
960 A quick summary, though - You want to define your own package that
961 subclasses Bot::BasicBot, override various methods (documented below),
962 then call L<C<new>|/new> and L<C<run>|/run> on it.
963  
964 =head1 STARTING THE BOT
965  
966 =head2 C<new>
967  
968 Creates a new instance of the class. Key/value pairs may be passed
969 which will have the same effect as calling the method of that name
970 with the value supplied. Returns a Bot::BasicBot object, that you can
971 call 'run' on later.
972  
973 eg:
974  
975 my $bot = Bot::BasicBot->new( nick => 'superbot', channels => [ '#superheroes' ] );
976  
977 =head2 C<run>
978  
979 Runs the bot. Hands the control over to the POE core.
980  
981 =head1 STOPPING THE BOT
982  
983 To shut down the bot cleanly, use the L<C<shutdown>|/shutdown> method,
984 which will (through L<C<AUTOLOAD>|/AUTOLOAD>) send an
985 L<event|POE::Component::IRC/shutdown> of the same name to
986 POE::Component::IRC, so it takes the same arguments:
987  
988 $bot->shutdown( $bot->quit_message() );
989  
990 =head1 METHODS TO OVERRIDE
991  
992 In your Bot::BasicBot subclass, you want to override some of the following
993 methods to define how your bot works. These are all object methods - the
994 (implicit) first parameter to all of them will be the bot object.
995  
996 =head2 C<init>
997  
998 called when the bot is created, as part of new(). Override to provide
999 your own init. Return a true value for a successful init, or undef if
1000 you failed, in which case new() will die.
1001  
1002 =head2 C<said>
1003  
1004 This is the main method that you'll want to override in your subclass -
1005 it's the one called by default whenever someone says anything that we
1006 can hear, either in a public channel or to us in private that we
1007 shouldn't ignore.
1008  
1009 You'll be passed a hashref that contains the arguments described below.
1010 Feel free to alter the values of this hash - it won't be used later on.
1011  
1012 =over 4
1013  
1014 =item who
1015  
1016 Who said it (the nick that said it)
1017  
1018 =item raw_nick
1019  
1020 The raw IRC nick string of the person who said it. Only really useful
1021 if you want more security for some reason.
1022  
1023 =item channel
1024  
1025 The channel in which they said it. Has special value "msg" if it was in
1026 a message. Actually, you can send a message to many channels at once in
1027 the IRC spec, but no-one actually does this so this is just the first
1028 one in the list.
1029  
1030 =item body
1031  
1032 The body of the message (i.e. the actual text)
1033  
1034 =item address
1035  
1036 The text that indicates how we were addressed. Contains the string
1037 "msg" for private messages, otherwise contains the string off the text
1038 that was stripped off the front of the message if we were addressed,
1039 e.g. "Nick: ". Obviously this can be simply checked for truth if you
1040 just want to know if you were addressed or not.
1041  
1042 =back
1043  
1044 You should return what you want to say. This can either be a simple
1045 string (which will be sent back to whoever was talking to you as a
1046 message or in public depending on how they were talking) or a hashref
1047 that contains values that are compatible with say (just changing
1048 the body and returning the structure you were passed works very well.)
1049  
1050 Returning undef will cause nothing to be said.
1051  
1052 =head2 C<emoted>
1053  
1054 This is a secondary method that you may wish to override. It gets called
1055 when someone in channel 'emotes', instead of talking. In its default
1056 configuration, it will simply pass anything emoted on channel through to
1057 the C<said> handler.
1058  
1059 C<emoted> receives the same data hash as C<said>.
1060  
1061 =head2 C<noticed>
1062  
1063 This is like C<said>, except for notices instead of normal messages.
1064  
1065 =head2 C<chanjoin>
1066  
1067 Called when someone joins a channel. It receives a hashref argument
1068 similar to the one received by said(). The key 'who' is the nick of the
1069 user who joined, while 'channel' is the channel they joined.
1070  
1071 This is a do-nothing implementation, override this in your subclass.
1072  
1073 =head2 C<chanpart>
1074  
1075 Called when someone parts a channel. It receives a hashref argument
1076 similar to the one received by said(). The key 'who' is the nick of the
1077 user who parted, while 'channel' is the channel they parted.
1078  
1079 This is a do-nothing implementation, override this in your subclass.
1080  
1081 =head2 C<got_names>
1082  
1083 Whenever we have been given a definitive list of 'who is in the channel',
1084 this function will be called. It receives a hash reference as an argument.
1085 The key 'channel' will be the channel we have information for, 'names' is a
1086 hashref where the keys are the nicks of the users, and the values are more
1087 hashes, containing the two keys 'op' and 'voice', indicating if the user is
1088 a chanop or voiced respectively.
1089  
1090 The reply value is ignored.
1091  
1092 Normally, I wouldn't override this method - instead, just use the L<names>
1093 call when you want to know who's in the channel. Override this only if you
1094 want to be able to do something as soon as possible. Also be aware that
1095 the names list can be changed by other events - kicks, joins, etc, and this
1096 method won't be called when that happens.
1097  
1098 =head2 C<topic>
1099  
1100 Called when the topic of the channel changes. It receives a hashref
1101 argument. The key 'channel' is the channel the topic was set in, and 'who'
1102 is the nick of the user who changed the channel, 'topic' will be the new
1103 topic of the channel.
1104  
1105 =head2 C<nick_change>
1106  
1107 When a user changes nicks, this will be called. It receives two arguments:
1108 the old nickname and the new nickname.
1109  
1110 =head2 C<kicked>
1111  
1112 Called when a user is kicked from the channel. It receives a hashref which
1113 will look like this:
1114  
1115 {
1116 channel => "#channel",
1117 who => "nick",
1118 kicked => "kicked",
1119 reason => "reason",
1120 }
1121  
1122 The reply value is ignored.
1123  
1124 =head2 C<tick>
1125  
1126 This is an event called every regularly. The function should return the
1127 amount of time until the tick event should next be called. The default
1128 tick is called 5 seconds after the bot starts, and the default
1129 implementation returns '0', which disables the tick. Override this and
1130 return non-zero values to have an ongoing tick event.
1131  
1132 Use this function if you want the bot to do something periodically, and
1133 don't want to mess with 'real' POE things.
1134  
1135 Call the L<schedule_tick> event to schedule a tick event without waiting
1136 for the next tick.
1137  
1138 =head2 C<help>
1139  
1140 This is the other method that you should override. This is the text
1141 that the bot will respond to if someone simply says help to it. This
1142 should be considered a special case which you should not attempt to
1143 process yourself. Saying help to a bot should have no side effects
1144 whatsoever apart from returning this text.
1145  
1146 =head2 C<connected>
1147  
1148 An optional method to override, gets called after we have connected
1149 to the server
1150  
1151 =head2 C<userquit>
1152  
1153 Receives a hashref which will look like:
1154  
1155 {
1156 who => "nick that quit",
1157 body => "quit message",
1158 }
1159  
1160 =head1 BOT METHODS
1161  
1162 There are a few methods you can call on the bot object to do things. These
1163 are as follows:
1164  
1165 =head2 C<schedule_tick>
1166  
1167 Takes an integer as an argument. Causes the L<tick> event to be called
1168 after that many seconds (or 5 seconds if no argument is provided). Note
1169 that if the tick event is due to be called already, this will override it.
1170 You can't schedule multiple future events with this funtction.
1171  
1172 =head2 C<forkit>
1173  
1174 This method allows you to fork arbitrary background processes. They
1175 will run concurrently with the main bot, returning their output to a
1176 handler routine. You should call C<forkit> in response to specific
1177 events in your C<said> routine, particularly for longer running
1178 processes like searches, which will block the bot from receiving or
1179 sending on channel whilst they take place if you don't fork them.
1180  
1181 Inside the subroutine called by forkit, you can send output back to the
1182 channel by printing lines (followd by C<\n>) to STDOUT. This has the same
1183 effect as calling L<C<< Bot::BasicBot->say >>|say>.
1184  
1185 C<forkit> takes the following arguments:
1186  
1187 =over 4
1188  
1189 =item run
1190  
1191 A coderef to the routine which you want to run. Bear in mind that the
1192 routine doesn't automatically get the text of the query - you'll need
1193 to pass it in C<arguments> (see below) if you want to use it at all.
1194  
1195 Apart from that, your C<run> routine just needs to print its output to
1196 C<STDOUT>, and it will be passed on to your designated handler.
1197  
1198 =item handler
1199  
1200 Optional. A method name within your current package which we can
1201 return the routine's data to. Defaults to the built-in method
1202 C<say_fork_return> (which simply sends data to channel).
1203  
1204 =item callback
1205  
1206 Optional. A coderef to execute in place of the handler. If used, the value
1207 of the handler argument is used to name the POE event. This allows using
1208 closures and/or having multiple simultanious calls to forkit with unique
1209 handler for each call.
1210  
1211 =item body
1212  
1213 Optional. Use this to pass on the body of the incoming message that
1214 triggered you to fork this process. Useful for interactive proceses
1215 such as searches, so that you can act on specific terms in the user's
1216 instructions.
1217  
1218 =item who
1219  
1220 The nick of who you want any response to reach (optional inside a
1221 channel.)
1222  
1223 =item channel
1224  
1225 Where you want to say it to them in. This may be the special channel
1226 "msg" if you want to speak to them directly
1227  
1228 =item address
1229  
1230 Optional. Setting this to a true value causes the person to be
1231 addressed (i.e. to have "Nick: " prepended to the front of returned
1232 message text if the response is going to a public forum.
1233  
1234 =item arguments
1235  
1236 Optional. This should be an anonymous array of values, which will be
1237 passed to your C<run> routine. Bear in mind that this is not
1238 intelligent - it will blindly spew arguments at C<run> in the order
1239 that you specify them, and it is the responsibility of your C<run>
1240 routine to pick them up and make sense of them.
1241  
1242 =back
1243  
1244 =head2 C<say>
1245  
1246 Say something to someone. Takes a list of key/value pairs as arguments.
1247 You should pass the following arguments:
1248  
1249 =over 4
1250  
1251 =item who
1252  
1253 The nick of who you are saying this to (optional inside a channel.)
1254  
1255 =item channel
1256  
1257 Where you want to say it to them in. This may be the special channel
1258 "msg" if you want to speak to them directly
1259  
1260 =item body
1261  
1262 The body of the message. I.e. what you want to say.
1263  
1264 =item address
1265  
1266 Optional. Setting this to a true value causes the person to be
1267 addressed (i.e. to have "Nick: " prepended to the front of the message
1268 text if this message is going to a pulbic forum.
1269  
1270 =back
1271  
1272 You can also make non-OO calls to C<say>, which will be interpreted as
1273 coming from a process spawned by C<forkit>. The routine will serialise
1274 any data it is sent, and throw it to STDOUT, where L<POE::Wheel::Run> can
1275 pass it on to a handler.
1276  
1277 =head2 C<emote>
1278  
1279 C<emote> will return data to channel, but emoted (as if you'd said "/me
1280 writes a spiffy new bot" in most clients). It takes the same arguments
1281 as C<say>, listed above.
1282  
1283 =head2 C<notice>
1284  
1285 C<notice> will send a IRC notice to the channel. This is typically used by
1286 bots to not break the IRC conversations flow. The message will appear as:
1287  
1288 -nick- message here
1289  
1290 It takes the same arguments as C<say>, listed above. Example:
1291  
1292 $bot->notice(
1293 channel => '#bot_basicbot_test',
1294 body => 'This is a notice'
1295 );
1296  
1297 =head2 C<reply>
1298  
1299 Takes two arguments, a hashref containing information about an incoming
1300 message, and a reply message. It will reply in a privmsg if the incoming
1301 one was a privmsg, in channel if not, and with prefixes if the incoming
1302 one was prefixed. Mostly a shortcut method - it's roughly equivalent to
1303  
1304 $mess->{body} = $body;
1305 $self->say($mess);
1306  
1307 =head2 C<pocoirc>
1308  
1309 Takes no arguments. Returns the underlying
1310 L<POE::Component::IRC::State|POE::Component::IRC::State> object used by
1311 Bot::BasicBot. Useful for accessing various state methods and for posting
1312 commands to the component. For example:
1313  
1314 # get the list of nicks in the channel #someplace
1315 my @nicks = $bot->pocoirc->channel_list("#someplace");
1316  
1317 # join the channel #otherplace
1318 $bot->pocoirc->yield('join', '#otherplace');
1319  
1320 =head2 C<channel_data>
1321  
1322 Takes a channel names as a parameter, and returns a hash of hashes. The
1323 keys are the nicknames in the channel, the values are hashes containing
1324 the keys "voice" and "op", indicating whether these users are voiced or
1325 opped in the channel. This method is only here for backwards compatability.
1326 You'll probably get more use out of
1327 L<POE::Component::IRC::State|POE::Component::IRC::State>'s methods (which
1328 this method is merely a wrapper for). You can access the
1329 POE::Component::IRC::State object through Bot::BasicBot's C<pocoirc>
1330 method.
1331  
1332 =head1 ATTRIBUTES
1333  
1334 Get or set methods. Changing most of these values when connected
1335 won't cause sideffects. e.g. changing the server will not
1336 cause a disconnect and a reconnect to another server.
1337  
1338 Attributes that accept multiple values always return lists and
1339 either accept an arrayref or a complete list as an argument.
1340  
1341 The usual way of calling these is as keys to the hash passed to the
1342 'new' method.
1343  
1344 =head2 C<server>
1345  
1346 The server we're going to connect to. Defaults to
1347 "irc.perl.org".
1348  
1349 =head2 C<port>
1350  
1351 The port we're going to use. Defaults to "6667"
1352  
1353 =head2 C<password>
1354  
1355 The server password for the server we're going to connect to. Defaults to
1356 undef.
1357  
1358 =head2 C<ssl>
1359  
1360 A boolean to indicate whether or not the server we're going to connect to
1361 is an SSL server. Defaults to 0.
1362  
1363 =head2 C<localaddr>
1364  
1365 The local address to use, for multihomed boxes. Defaults to undef (use whatever
1366 source IP address the system deigns is appropriate).
1367  
1368 =head2 C<useipv6>
1369  
1370 A boolean to indicate whether IPv6 should be used. Defaults to undef (use
1371 IPv4).
1372  
1373 =head2 C<nick>
1374  
1375 The nick we're going to use. Defaults to five random letters
1376 and numbers followed by the word "bot"
1377  
1378 =head2 C<alt_nicks>
1379  
1380 Alternate nicks that this bot will be known by. These are not nicks
1381 that the bot will try if it's main nick is taken, but rather other
1382 nicks that the bot will recognise if it is addressed in a public
1383 channel as the nick. This is useful for bots that are replacements
1384 for other bots...e.g, your bot can answer to the name "infobot: "
1385 even though it isn't really.
1386  
1387 =head2 C<username>
1388  
1389 The username we'll claim to have at our ip/domain. By default this
1390 will be the same as our nick.
1391  
1392 =head2 C<name>
1393  
1394 The name that the bot will identify itself as. Defaults to
1395 "$nick bot" where $nick is the nick that the bot uses.
1396  
1397 =head2 C<channels>
1398  
1399 The channels we're going to connect to.
1400  
1401 =head2 C<quit_message>
1402  
1403 The quit message. Defaults to "Bye".
1404  
1405 =head2 C<ignore_list>
1406  
1407 The list of irc nicks to ignore B<public> messages from (normally
1408 other bots.) Useful for stopping bot cascades.
1409  
1410 =head2 C<charset>
1411  
1412 IRC has no defined character set for putting high-bit chars into channel.
1413 This attribute sets the encoding to be used for outgoing messages. Defaults
1414 to 'utf8'.
1415  
1416 =head2 C<flood>
1417  
1418 Set to '1' to disable the built-in flood protection of POE::Compoent::IRC
1419  
1420 =head2 C<no_run>
1421  
1422 Tells Bot::BasicBot to B<not> run the L<POE kernel|POE::Kernel> at the end
1423 of L<C<run>|/run>, in case you want to do that yourself.
1424  
1425 =head1 OTHER METHODS
1426  
1427 =head2 C<AUTOLOAD>
1428  
1429 Bot::BasicBot implements AUTOLOAD for sending arbitrary states to the
1430 underlying L<POE::Component::IRC|POE::Component::IRC> component. So for a
1431 C<$bot> object, sending
1432  
1433 $bot->foo("bar");
1434  
1435 is equivalent to
1436  
1437 $poe_kernel->post(BASICBOT_ALIAS, "foo", "bar");
1438  
1439 =head2 C<log>
1440  
1441 Logs the message. This method merely prints to STDERR - If you want smarter
1442 logging, override this method - it will have simple text strings passed in
1443 @_.
1444  
1445 =head2 C<ignore_nick>
1446  
1447 Takes a nick name as an argument. Return true if this nick should be
1448 ignored. Ignores anything in the ignore list
1449  
1450 =head2 C<nick_strip>
1451  
1452 Takes a nick and hostname (of the form "nick!hostname") and
1453 returns just the nick
1454  
1455 =head2 C<charset_decode>
1456  
1457 Converts a string of bytes from IRC (uses
1458 L<C<decode_irc>|IRC::Utils/decode_irc> from L<IRC::Utils|IRC::Utils>
1459 internally) and returns a Perl string.
1460  
1461 It can also takes a list (or arrayref or hashref) of strings, and return
1462 a list of strings
1463  
1464 =head2 C<charset_encode>
1465  
1466 Converts a list of perl strings into a list of byte sequences, using
1467 the bot's charset. See L<C<charset_decode>|/charset_decode>.
1468  
1469 =head1 HELP AND SUPPORT
1470  
1471 If you have any questions or issues, you can drop by in #poe or
1472 #bot-basicbot @ irc.perl.org, where I (Hinrik) am usually around.
1473  
1474 =head1 AUTHOR
1475  
1476 Tom Insam E<lt>tom@jerakeen.orgE<gt>
1477  
1478 This program is free software; you can redistribute it
1479 and/or modify it under the same terms as Perl itself.
1480  
1481 =head1 CREDITS
1482  
1483 The initial version of Bot::BasicBot was written by Mark Fowler,
1484 and many thanks are due to him.
1485  
1486 Nice code for dealing with emotes thanks to Jo Walsh.
1487  
1488 Various patches from Tom Insam, including much improved rejoining,
1489 AUTOLOAD stuff, better interactive help, and a few API tidies.
1490  
1491 Maintainership for a while was in the hands of Simon Kent
1492 E<lt>simon@hitherto.netE<gt>. Don't know what he did. :-)
1493  
1494 I (Tom Insam) recieved patches for tracking joins and parts from Silver,
1495 sat on them for two months, and have finally applied them. Thanks, dude.
1496 He also sent me changes for the tick event API, which made sense.
1497  
1498 In November 2010, maintainership moved to Hinrik E<Ouml>rn
1499 SigurE<eth>sson (L<hinrik.sig@gmail.com>).
1500  
1501 In April 2017, maintainership moved to David Precious
1502 (L<davidp@preshweb.co.uk>).
1503  
1504 =head1 SEE ALSO
1505  
1506 L<POE>, L<POE::Component::IRC>
1507  
1508 Possibly Infobot, at http://www.infobot.org
1509  
1510 =cut
↑ Summary