Net/Mixin/Bot/IRCD.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 177 CC 1 SCAR 6.9
CC
1
Cov
0%
default_configuration
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use strict;
2 use warnings;
3  
4 package Bot::Net::Mixin::Bot::IRCD;
5  
6 use Bot::Net::Mixin;
7  
8 use Scalar::Util qw/ reftype /;
9  
10 =head1 NAME
11  
12 Bot::Net::Mixin::Bot::IRCD - mixin class for building IRC daemon bots
13  
14 =head1 SYNOPSIS
15  
16 # Build an Eliza chatbot directly on the server
17 use strict;
18 use warnings;
19  
20 package MyBotNet::Bot::ElizaOnServer;
21  
22 use Bot::Net::Bot;
23 use Bot::Net::Mixin::Bot::IRCD;
24 use Chatbot::Eliza; # available separately on CPAN
25  
26 on _start => run {
27 # Make it easy for other sessions to talk to us
28 get(KERNEL)->alias_set('eliza');
29 };
30  
31 on bot startup => run {
32 remember eliza => Chatbot::Eliza->new;
33 };
34  
35 on bot message_to_me => run {
36 my $event = getARG0;
37  
38 my $reply = recall('eliza')->transform( $event->message );
39 yield reply_to_sender => $message, $reply;
40 };
41  
42 on bot quit => run {
43 # Clear the session alias for clean shutdown
44 get(KERNEL)->alias_remove('eliza');
45 };
46  
47 1;
48  
49 Then in your server, you need something like this:
50  
51 use strict;
52 use warnings;
53  
54 package MyBotNet::Server::Main;
55  
56 use Bot::Net::Server;
57 use Bot::Net::Mixin::Server::IRC;
58  
59 use MyBotNet::Bot::ElizaOnServer;
60  
61 on _start => run {
62 remember eliza => MyBotNet::Bot::ElizaOnServer->setup;
63 };
64  
65 on server quit => run {
66  
67 # Clean up for clean shutdown
68 forget 'eliza';
69 };
70  
71 1;
72  
73 =head1 DESCRIPTION
74  
75 Some bots are best run direclty on the server itself. This is generally useful for handling channel or nick management services and other administrative tasks. A typical bot should run as a separate entity whenever possible. See L<Bot::Net::Mixin::Bot::IRC>.
76  
77 Unlike stand-alone bots, an IRC daemon bot cannot be run using the L<botnet> command. Instead, they must be installed by a specific server and then are run as part of server startup via the L<botnet> command.
78  
79 =head1 METHODS
80  
81 =head2 default_configuration PACKAGE
82  
83 Returns a bas configuration for an IRC daemon bot.
84  
85 =cut
86  
87 sub default_configuration {
88 my $class = shift;
89 my $package = shift;
90  
91 my $name = Bot::Net->short_name_for_bot($package);
92 $name =~ s/\W+/_/g;
93  
94 my $default_channel = Bot::Net->config->net('ApplicationName');
95 $default_channel =~ s/\W+/_/g;
96  
97 return {
98 alias => $name,
99  
100 spoofing => {
101 nick => $name,
102 ircname => $name,
103 },
104  
105 channels => [ '#'.$default_channel ],
106 };
107 }
108  
109 =head1 BOT STATES
110  
111 The following states are available for your bot to implement.
112  
113 =head2 on bot connected
114  
115 This is emitted as soon as the nick spoofing and initial channel setup has been setup.
116  
117 =head2 on bot message_to_group EVENT
118  
119 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:").
120  
121 The C<EVENT> is a L<Bot::Net::Message> object setup to contain the group message.
122  
123 =head2 on bot message_to_me EVENT
124  
125 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.
126  
127 The C<EVENT> is a L<Bot::Net::Message> object setup for either a group or private message depending on which occurred. If a public message the "MyNick:" part of the message will have been stripped.
128  
129 =head1 MIXIN STATES
130  
131 =head2 on _start
132  
133 Tells the server to spoof the bot's nick and registers to receive daemon events. It also sets up an alias for the session so that the server and other server bots may talk to your bot directly. The alias is set according to the "alias" configuration parameter.
134  
135 It ends by firing the L</on bot connected> state.
136  
137 =cut
138  
139 on _start => run {
140 my $alias = recall [ config => 'alias' ];
141 my $spoofed = recall [ config => 'spoofing' ];
142 my $channels = recall [ config => 'channels' ];
143  
144 get(KERNEL)->alias_set($alias) if $alias;
145  
146 recall('log')->info('Setting up nick spoofing as '
147 .$spoofed->{nick});
148 post ircd => register => 'all'; # TODO limit this to a subset
149  
150 # Make sure we are, in fact, ready when we report
151 call ircd => add_spoofed_nick => $spoofed;
152 recall('log')->info("BOT READY : nick $spoofed->{nick}");
153  
154 for my $channel (@{ $channels || [] }) {
155 recall('log')->info('Joining '.$channel);
156 post ircd => daemon_cmd_join => $spoofed->{nick}, $channel;
157 }
158  
159 yield 'bot_connected';
160 };
161  
162 =head2 on ircd_daemon_privmsg USERHOST, NICK, MESSAGE
163  
164 Handles IRC messasges sent directly to the bot. This then emits the additional bot state L</on bot message_to_me>. It will be passed a single argument, the L<Bot::Net::Message> containting the message received.
165  
166 =cut
167  
168 on ircd_daemon_privmsg => run {
169 my $userhost = get ARG0;
170 my $me = get ARG1;
171 my $message = get ARG2;
172  
173 my $my_nick = recall [ config => spoofing => 'nick' ];
174  
175 my ($nick, $host) = split /!/, $userhost;
176  
177 # Only respond to messages directly to me
178 if ($me eq $my_nick) {
179 my $event = Bot::Net::Message->new({
180 sender_nick => $nick,
181 sender_host => $host,
182 recipient_nicks => $me,
183 message => $message,
184 private => 1,
185 });
186  
187 yield bot_message_to_me => $event;
188 }
189 };
190  
191 =head2 ircd_daemon_public USERHOST, CHANNEL, MESSAGE
192  
193 Handles any public messages stated in a channel the bot is in. It will emit either a L</on bot message_to_group> state or L</on bot message_to_me> state depending on whether or not the message was prefixed with "MyNick:".
194  
195 Both events will receive a single argument, the L<Bot::Net::Message> representing the message sent. The message will have the "MyNick:" prefix stripped on teh L</on bot message_to_me> message.
196  
197 =cut
198  
199 on ircd_daemon_public => run {
200 my $userhost = get ARG0;
201 my $channel = get ARG1;
202 my $message = get ARG2;
203  
204 my $my_nick = recall [ config => spoofing => 'nick' ];
205 my $channels = recall [ config => 'channels' ];
206  
207 my ($nick, $host) = split /!/, $userhost;
208  
209 # FIXME This isn't a very good mechanism for determining if this spoofed
210 # nick is in the channel since it could have changed.
211  
212 # Am I in this channel?
213 if (grep { $_ eq $channel } @{ $channels || [] }) {
214 my $state;
215 if ($message =~ s/^\Q$my_nick\E:\s*//) {
216 $state = 'message_to_me';
217 }
218  
219 else {
220 $state = 'message_to_group';
221 }
222  
223 my $event = Bot::Net::Message->new({
224 sender_nick => $nick,
225 sender_host => $host,
226 recipient_groups => $channel,
227 message => $message,
228 public => 1,
229 });
230  
231 yield 'bot_' . $state => $event;
232 }
233 };
234  
235 =head2 send_to DESTINATION, MESSAGE
236  
237 This sends the given C<MESSAGE> to the given C<DESTINATION>.
238  
239 The C<DESTINATION> may be one of the following:
240  
241 =over
242  
243 =item C<#channel>
244  
245 The name of a channel to send to. In this case, no special modifications will be made to the message.
246  
247 =item C<nick>
248  
249 The name of a nick to send to. In this case, no special modifications will be made to the message.
250  
251 =item C<< [ #channel1, #channel2, nick1, nick2 ] >>
252  
253 A list of channels and nicks to send to. In this case, no special modifications will be made to the message.
254  
255 =item C<< { #channel1 => nick1, #channel2 => [ nick1, nick2 ] } >>
256  
257 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).
258  
259 =back
260  
261 If C<MESSAGE> contains new lines (\n), then the message will be broken up and sent in pieces.
262  
263 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.
264  
265 =cut
266  
267 on send_to => run {
268 my $to = get ARG0;
269 my $full_message = get ARG1;
270  
271 my $log = recall 'log';
272 my $my_nick = recall [ config => spoofing => 'nick' ];
273  
274 # Split the message up by newlines
275 my @messages = split /\n/, $full_message;
276  
277 # Normalize the destination by channels and nicks
278 my (%group_destinations, %private_destinations);
279  
280 # Given a single channel or nick
281 if (!defined reftype $to) {
282  
283 # Given a single channel
284 if ($to =~ /\#/) {
285 $group_destinations{$to} = [ ];
286 }
287  
288 # Given a single nick
289 else {
290 $private_destinations{$to}++;
291 }
292 }
293  
294 # Given an array of channels and/or nicks
295 elsif (reftype($to) eq 'ARRAY') {
296  
297 # For each channel/nick in the array...
298 for my $thing (@$to) {
299  
300 # Add channels to the group list
301 if ($thing =~ /\#/) {
302 $group_destinations{$thing} = [ ];
303 }
304  
305 # Add nicks to the private list
306 else {
307 $private_destinations{$thing}++;
308 }
309 }
310 }
311  
312 # Given a hash of channels => nicks
313 elsif (reftype($to) eq 'HASH') {
314  
315 # For each channel in the hash...
316 for my $channel (keys %$to) {
317 my $nicks = $to->{$channel};
318  
319 # If a single nick, wrap it in an array
320 if (!defined reftype($nicks)) {
321 $group_destinations{$channel} = [ $nicks ];
322 }
323  
324 # If an array, assume an array of nicks
325 elsif (reftype($nicks) eq 'ARRAY') {
326 $group_destinations{$channel} = $nicks;
327 }
328
329 # Wha?
330 else {
331 $log->error("send_to: Don't know what to do with $channel => $nicks, ignoring.");
332 }
333  
334 }
335 }
336  
337 # Wha?
338 else {
339 $log->error("send_to: Don't know what to do with $to, ignore.");
340 return;
341 }
342  
343 # Internal function for chunking the message
344 my $message_chunks = sub {
345 my $message = shift;
346 my @chunks;
347 while (length $message > 400) {
348 my $chunk = substr $message, 0, 400;
349 $message = substr $message, 400;
350 push @chunks, $chunk.'\\';
351 }
352 push @chunks, $message;
353 return @chunks;
354 };
355  
356 # Handle group messages
357 for my $channel (keys %group_destinations) {
358 my $nicks = $group_destinations{$channel};
359  
360 # For each message line
361 for my $message (@messages) {
362  
363 # Prepend any nicks we have
364 if (@$nicks) {
365 $message = join(',', @$nicks).': '.$message;
366 }
367  
368 # Post the message by chunks
369 post ircd => daemon_cmd_privmsg => $my_nick => $channel => $_
370 for $message_chunks->($message);
371 }
372 }
373  
374 # Send out the private messages
375 for my $nick (keys %private_destinations) {
376  
377 # For each message line, post it to the nick in chunks
378 for my $message (@messages) {
379 post ircd => daemon_cmd_privmsg => $my_nick => $nick => $_
380 for $message_chunks->($message);
381 }
382 }
383  
384 };
385  
386 =head2 on reply_to_sender EVENT, MESSAGE
387  
388 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>.
389  
390 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.
391  
392 =cut
393  
394 on reply_to_sender => run {
395 my $event = get ARG0;
396 my $message = get ARG1;
397  
398 if ($event->public) {
399 my %send_to = map { $_ => $event->sender_nick }
400 @{ $event->recipient_groups };
401 yield send_to => \%send_to => $message;
402 }
403  
404 else {
405 yield send_to => $event->sender_nick => $message;
406 }
407 };
408  
409 =head2 on reply_to_sender_privately EVENT, MESSAGE
410  
411 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.
412  
413 =cut
414  
415 on reply_to_sender_privately => run {
416 my $event = get ARG0;
417 my $message = get ARG1;
418  
419 yield send_to => $event->sender_nick => $message;
420 };
421  
422 =head2 on reply_to_sender_group EVENT, CHANNEL, MESSAGE
423  
424 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.
425  
426 =cut
427  
428 on reply_to_sender_group => run {
429 my $event = get ARG0;
430 my $channel = get ARG1;
431 my $message = get ARG2;
432  
433 yield send_to => { $channel => $event->sender_nick } => $message;
434 };
435  
436 =head2 on reply_to_general EVENT, MESSAGE
437  
438 This is similar to L</reply_to_sender>, except that if sent to a channel, the sender will not be identified by nick.
439  
440 =cut
441  
442 on reply_to_general => run {
443 my $event = get ARG0;
444 my $message = get ARG1;
445  
446 if ($event->public) {
447 yield send_to => $event->recipient_groups => $message;
448 }
449  
450 else {
451 yield send_to => $event->sender_nick => $message;
452 }
453 };
454  
455 =head2 on bot quit
456  
457 This causes the IRC client to close down the connection and quit.
458  
459 =cut
460  
461 on bot_quit => run {
462 recall('log')->warn("Stopping spoofed server-side IRC bot.");
463  
464 my $alias = recall [ config => 'alias' ];
465 my $my_nick = recall [ config => spoofing => 'nick' ];
466  
467 post ircd => del_spoofed_nick => $my_nick => 'Quitting.';
468 post ircd => unregister => 'all';
469  
470 get(KERNEL)->alias_remove($alias) if $alias;
471 };
472  
473 =head1 AUTHORS
474  
475 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
476  
477 =head1 COPYRIGHT AND LICENSE
478  
479 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
480  
481 This program is free software and may be modified and distributed under the same terms as Perl itself.
482  
483 =cut
484  
485 1;