Net/Mixin/Bot/IRC.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 185 CC 1 SCAR 6.9
CC
1
Cov
0%
setup
2.0
default_configuration
2.0
BEGIN
2.0
CRAP 2.0
1 use strict;
2 use warnings;
3  
4 package Bot::Net::Mixin::Bot::IRC;
5  
6 use Bot::Net::Mixin;
7  
8 use POE qw/ Component::IRC::State /;
9 use Scalar::Util qw/ reftype /;
10  
11 =head1 NAME
12  
13 Bot::Net::Mixin::Bot::IRC - mixin class for building IRC bots
14  
15 =head1 SYNOPSIS
16  
17 # Build an Eliza chatbot for IRC
18 use strict;
19 use warnings;
20 package MyBotNet::Bot::Eliza;
21  
22 use Bot::Net::Bot;
23 use Bot::Net::Mixin::Bot::IRC;
24 use Chatbot::Eliza; # available separately on CPAN
25  
26 on bot startup => run {
27 remember eliza => Chatbot::Eliza->new;
28 };
29  
30 on bot message_to_me => run {
31 my $event = get ARG0;
32  
33 my $reply = recall('eliza')->transform( $event->message );
34 yield reply_to_sender => $message, $reply;
35 };
36  
37 1;
38  
39 =head1 DESCRIPTION
40  
41 This is the mixin-class for L<Bot::Net> IRC bots. You "inherit" all the features of this mixin by using the class:
42  
43 use Bot::Net::Bot; # define a Bot::Net bot
44 use Bot::Net::Mixin::Bot::IRC; # become an IRC bot
45  
46 =head1 METHODS
47  
48 =head2 setup
49  
50 Adds the IRC component to the bot's memory on setup.
51  
52 =cut
53  
54 sub setup {
55 my $self = shift;
56 my $brain = shift;
57  
58 $brain->remember(
59 [ 'irc' ] => POE::Component::IRC::State->spawn( alias => 'irc' )
60 );
61 }
62  
63 =head2 default_configuration PACKAGE
64  
65 Returns a base configuration for an IRC bot.
66  
67 =cut
68  
69 sub default_configuration {
70 my $class = shift;
71 my $package = shift;
72  
73 my $name = Bot::Net->short_name_for_bot($package);
74 $name =~ s/\W+/_/g;
75  
76 my $default_channel = Bot::Net->config->net('ApplicationName');
77 $default_channel =~ s/\W+/_/g;
78  
79 return {
80 auto_connect => 1,
81  
82 irc_connect => {
83 nick => $name,
84 username => lc $name,
85 ircname => Bot::Net->short_name_for_bot($package),
86  
87 server => 'localhost',
88 port => 6667,
89  
90 flood => 1,
91 },
92
93 channels => [ '#'.$default_channel ],
94 };
95 }
96  
97 =head1 BOT STATES
98  
99 The following states are avaiable for your bot to implement.
100  
101 =head2 on bot connect
102  
103 This is automatically yielded by L</on _start> unless the C<auto_connect> configuration option is set to "0". If it is set to "0", you may emit this state to cause the bot to connect to the IRC server.
104  
105 =head2 on bot connected
106  
107 This state is emitted as soon as the bot has connected to the server and the server has passed back a message. This means it is now safe to communicate with the server.
108  
109 =head2 on bot message_to_group EVENT
110  
111 This state occurs when the bot observes a message sent to a channel it is in, but the message is not directed at the bot (i.e., is not prefixed with "MyNick:").
112  
113 The C<EVENT> is a L<Bot::Net::Message> object setup to contain the group message.
114  
115 =head2 on bot message_to_me EVENT
116  
117 This state occurs when either the bot observes a message sent to a channel that it is in that has been prefixed with "MyNick:" or a private message was sent directly to the bot.
118  
119 The C<EVENT> is a L<Bot::Net::Message> object setup for either a group or private message depending on which occurred. If a pblic message the "MyNick:" part of the message will have been stripped.
120  
121 =head1 MIXIN STATES
122  
123 =head2 on _start
124  
125 Sets up the IRC client.
126  
127 =cut
128  
129 on _start => run {
130 post irc => register => 'all';
131  
132 my $auto_connect = recall [ config => 'auto_connect' ];
133 if (not defined $auto_connect or $auto_connect) {
134 yield 'bot_connect';
135 }
136 };
137  
138 =head2 on bot connect
139  
140 Tells the IRC component to connect to the IRC server using the configuration stored in the C<irc_connect> option of the configuration.
141  
142 =cut
143  
144 on bot_connect => run {
145 my $config = recall [ config => 'irc_connect' ] ;
146 call irc => connect => $config;
147 };
148  
149 =head2 on irc_001
150  
151 Connects to the channels that the bot has been configured to join. This then fires the L</on bot connected> event which indicates that it is now safe to issue IRC commands, if your bot needs to do so.
152  
153 =cut
154  
155 on irc_001 => run {
156 my $self = get OBJECT;
157 my $log = recall 'log';
158  
159 my $channels = recall [ config => 'channels' ];
160 for my $channel (@$channels) {
161 $log->info("Joining $channel...");
162 post irc => join => $channel;
163 }
164  
165 yield 'bot_connected';
166  
167 # Report readiness (helpful for testing)
168 my $config = recall [ config => 'irc_connect' ] ;
169 recall('log')->info(
170 "BOT READY : nick $config->{nick} server $config->{server} "
171 ."port $config->{port}");
172 };
173  
174 =head2 on irc_disconnected
175  
176 =head2 on irc_error
177  
178 =head2 on irc_socketerr
179  
180 Handles bot disconnections. If the bot becomes disconnected it will automatically attempt to reconnect until the server returns or lets the bot back. It will attempt to do so in a way that will not cause it to be blocked for flooding.
181  
182 =cut
183  
184 on [ qw( irc_disconnected irc_error irc_socketerr ) ] => run {
185 # TODO XXX FIXME Add support for notifying the bot that the connection has
186 # been lost and then notifying the bot again when the connection is
187 # re-established.
188  
189 delay attempt_reconnect => 60;
190 };
191  
192 =head2 on attempt_reconnect
193  
194 This state is invoked by L</on irc_disconnected> and related states. This tells the IRC client to attempt ot reconnect. This will be called repeatedly (on a delay) until a connection is reestablished.
195  
196 =cut
197  
198 on attempt_reconnect => run {
199 post irc => 'connect';
200 };
201  
202 =head2 irc_msg USERHOST, ME, MESSAGE
203  
204 Handles IRC messages sent directly to the bot. This emits:
205  
206 =over
207  
208 =item bot message_to_me
209  
210 See L</bot message_to_me>.
211  
212 =back
213  
214 =cut
215  
216 on irc_msg => run {
217 my $userhost = get ARG0;
218 my $me = get ARG1;
219 my $message = get ARG2;
220  
221 my ($nick, $host) = split /!/, $userhost;
222  
223 my $event = Bot::Net::Message->new({
224 sender_nick => $nick,
225 sender_host => $host,
226 recipient_nicks => $me,
227 message => $message,
228 private => 1,
229 });
230  
231 yield bot_message_to_me => $event;
232 };
233  
234 =head2 irc_public USERHOST, CHANNEL, MESSAGE
235  
236 Handles IRC messages sent to a public channel. This then emits additional bot states:
237  
238 =over
239  
240 =item bot message_to_group
241  
242 Reports that a message was sent to a channel the bot is in. See L</bot message_to_group>.
243  
244 =item bot message_to_me
245  
246 Reports that a messages was sent to me publically in a channel. See L</bot message_to_me>.
247  
248 =back
249  
250 =cut
251  
252 on irc_public => run {
253 my $userhost = get ARG0;
254 my $channel = get ARG1;
255 my $message = get ARG2;
256  
257 my $my_nick = recall [ config => irc_connect => 'nick' ];
258  
259 my $state;
260 if ($message =~ s/^\Q$my_nick\E:\s*//) {
261 $state = 'message_to_me';
262 }
263  
264 else {
265 $state = 'message_to_group';
266 }
267  
268 my ($nick, $host) = split /!/, $userhost;
269  
270 my $event = Bot::Net::Message->new({
271 sender_nick => $nick,
272 sender_host => $host,
273 recipient_groups => $channel,
274 message => $message,
275 public => 1,
276 });
277  
278 yield 'bot_' . $state => $event;
279 };
280  
281 =head2 send_to DESTINATION, MESSAGE
282  
283 This sends the given C<MESSAGE> to the given C<DESTINATION>.
284  
285 The C<DESTINATION> may be one of the following:
286  
287 =over
288  
289 =item C<#channel>
290  
291 The name of a channel to send to. In this case, no special modifications will be made to the message.
292  
293 =item C<nick>
294  
295 The name of a nick to send to. In this case, no special modifications will be made to the message.
296  
297 =item C<< [ #channel1, #channel2, nick1, nick2 ] >>
298  
299 A list of channels and nicks to send to. In this case, no special modifications will be made to the message.
300  
301 =item C<< { #channel1 => nick1, #channel2 => [ nick1, nick2 ] } >>
302  
303 A hash of channels pointing to nicks. The nicks may be either a single nick or an array of nicks. In this case, the messages will have the given nick (or nicks) prepended to every message sent (except continuations).
304  
305 =back
306  
307 If C<MESSAGE> contains new lines (\n), then the message will be broken up and sent in pieces.
308  
309 If any message that would be sent to the server approaches the 512 byte limit on IRC messages, the line will be broken. The broken line will have a backslash (\) appended at the break point of the message to signal the recipient that the line was broken. A line may be broken multiple times.
310  
311 =cut
312  
313 on send_to => run {
314 my $to = get ARG0;
315 my $full_message = get ARG1;
316  
317 my $log = recall 'log';
318  
319 # Split the message up by newlines
320 my @messages = split /\n/, $full_message;
321  
322 # Normalize the destination by channels and nicks
323 my (%group_destinations, %private_destinations);
324  
325 # Given a single channel or nick
326 if (!defined reftype $to) {
327  
328 # Given a single channel
329 if ($to =~ /\#/) {
330 $group_destinations{$to} = [ ];
331 }
332  
333 # Given a single nick
334 else {
335 $private_destinations{$to}++;
336 }
337 }
338  
339 # Given an array of channels and/or nicks
340 elsif (reftype($to) eq 'ARRAY') {
341  
342 # For each channel/nick in the array...
343 for my $thing (@$to) {
344  
345 # Add channels to the group list
346 if ($thing =~ /\#/) {
347 $group_destinations{$thing} = [ ];
348 }
349  
350 # Add nicks to the private list
351 else {
352 $private_destinations{$thing}++;
353 }
354 }
355 }
356  
357 # Given a hash of channels => nicks
358 elsif (reftype($to) eq 'HASH') {
359  
360 # For each channel in the hash...
361 for my $channel (keys %$to) {
362 my $nicks = $to->{$channel};
363  
364 # If a single nick, wrap it in an array
365 if (!defined reftype($nicks)) {
366 $group_destinations{$channel} = [ $nicks ];
367 }
368  
369 # If an array, assume an array of nicks
370 elsif (reftype($nicks) eq 'ARRAY') {
371 $group_destinations{$channel} = $nicks;
372 }
373
374 # Wha?
375 else {
376 $log->error("send_to: Don't know what to do with $channel => $nicks, ignoring.");
377 }
378  
379 }
380 }
381  
382 # Wha?
383 else {
384 $log->error("send_to: Don't know what to do with $to, ignore.");
385 return;
386 }
387  
388 # Internal function for chunking the message
389 my $message_chunks = sub {
390 my $message = shift;
391 my @chunks;
392 while (length $message > 400) {
393 my $chunk = substr $message, 0, 400;
394 $message = substr $message, 400;
395 push @chunks, $chunk.'\\';
396 }
397 push @chunks, $message;
398 return @chunks;
399 };
400  
401 # Handle group messages
402 for my $channel (keys %group_destinations) {
403 my $nicks = $group_destinations{$channel};
404  
405 # For each message line
406 for my $message (@messages) {
407  
408 # Prepend any nicks we have
409 if (@$nicks) {
410 $message = join(',', @$nicks).': '.$message;
411 }
412  
413 # Post the message by chunks
414 post irc => privmsg => $channel => $_
415 for $message_chunks->($message);
416 }
417 }
418  
419 # Send out the private messages
420 for my $nick (keys %private_destinations) {
421  
422 # For each message line, post it to the nick in chunks
423 for my $message (@messages) {
424 post irc => privmsg => $nick => $_
425 for $message_chunks->($message);
426 }
427 }
428  
429 };
430  
431 =head2 on reply_to_sender EVENT, MESSAGE
432  
433 Sends the C<MESSAGE> back to the nick that sent the given C<EVENT>. The C<EVENT> should be a L<Bot::Net::Message> object. The C<MESSAGE> may be broken up and chunked as specified in L</send_to>.
434  
435 This method will reply back to a user in all the channels it received the message in if the C<EVENT> was sent to a channel (or group of channels) or will send privately back if the C<EVENT> was private.
436  
437 =cut
438  
439 on reply_to_sender => run {
440 my $event = get ARG0;
441 my $message = get ARG1;
442  
443 if ($event->public) {
444 my %send_to = map { $_ => $event->sender_nick }
445 @{ $event->recipient_groups };
446 yield send_to => \%send_to => $message;
447 }
448  
449 else {
450 yield send_to => $event->sender_nick => $message;
451 }
452 };
453  
454 =head2 on reply_to_sender_privately EVENT, MESSAGE
455  
456 Sends the C<MESSAGE> back to the nick that sent the given C<EVENT>, but via a private message directly to their nick, even if the original C<EVENT> took place as a channel message.
457  
458 =cut
459  
460 on reply_to_sender_privately => run {
461 my $event = get ARG0;
462 my $message = get ARG1;
463  
464 yield send_to => $event->sender_nick => $message;
465 };
466  
467 =head2 on reply_to_sender_group EVENT, CHANNEL, MESSAGE
468  
469 Sends the C<MESSAGE> back to the nick that sent the given C<EVENT>, but via the public group name in the C<CHANNEL> argument.
470  
471 =cut
472  
473 on reply_to_sender_group => run {
474 my $event = get ARG0;
475 my $channel = get ARG1;
476 my $message = get ARG2;
477  
478 yield send_to => { $channel => $event->sender_nick } => $message;
479 };
480  
481 =head2 on reply_to_general EVENT, MESSAGE
482  
483 This is similar to L</reply_to_sender>, except that if sent to a channel, the sender will not be identified by nick.
484  
485 =cut
486  
487 on reply_to_general => run {
488 my $event = get ARG0;
489 my $message = get ARG1;
490  
491 if ($event->public) {
492 yield send_to => $event->recipient_groups => $message;
493 }
494  
495 else {
496 yield send_to => $event->sender_nick => $message;
497 }
498 };
499  
500 =head2 on bot quit
501  
502 This causes the IRC client to close down the connection and quit.
503  
504 =cut
505  
506 on bot_quit => run {
507 my $message = get ARG0;
508  
509 recall('log')->warn("Quitting the IRC connection.");
510 post irc => quit => ($message || 'Quitting.');
511  
512 post irc => unregister => 'all';
513 forget 'irc';
514 };
515  
516 =head1 AUTHORS
517  
518 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
519  
520 =head1 COPYRIGHT AND LICENSE
521  
522 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
523  
524 This program is free software and may be modified and distributed under the same terms as Perl itself.
525  
526 =cut
527  
528 1;