Net/Bot.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 134 CC 5 SCAR 23.7
CC
5
Cov
0%
setup
30.0
import
2.0
bot
2.0
CRAP 10.8
1 use strict;
2 use warnings;
3  
4 package Bot::Net::Bot;
5  
6 use Bot::Net::Mixin;
7  
8 use Bot::Net::Message;
9 use Scalar::Util qw/ reftype /;
10  
11 require Exporter;
12 push our @ISA, 'Exporter';
13  
14 our @EXPORT = (
15 # Re-export POE::Session constants
16 qw/ OBJECT SESSION KERNEL HEAP STATE SENDER CALLER_FILE CALLER_LINE
17 CALLER_STATE ARG0 ARG1 ARG2 ARG3 ARG4 ARG5 ARG6 ARG7 ARG8 ARG9 /,
18  
19 # Re-export POE::Declarative
20 @POE::Declarative::EXPORT,
21
22 # Re-export Data::Remember
23 qw/ remember remember_these recall recall_and_update
24 forget forget_when brain /,
25  
26 # Add in our own subs
27 qw/ bot setup /,
28 );
29  
30 =head1 NAME
31  
32 Bot::Net::Bot - the base class for all Bot::Net bots
33  
34 =head1 SYNOPSIS
35  
36 # An example for building an Eliza-based chatbot
37 use strict;
38 use warnings;
39  
40 use Bot::Net::Bot;
41 use Bot::Net::Mixin::Bot::IRC;
42 use Chatbot::Eliza; # available separately on CPAN
43  
44 on bot startup => run {
45 remember eliza => Chatbot::Eliza->new;
46 };
47  
48 on bot message_to_me => run {
49 my $message = get ARG0;
50  
51 my $reply = recall('eliza')->transform( $message->text );
52 yield reply_to_sender, $message, $reply;
53 };
54  
55 1;
56  
57 =head1 DESCRIPTION
58  
59 This is the primary mixin-class for all L<Bot::Net> bots. You "inherit" all the features of this mixin by using the class:
60  
61 use Bot::Net::Bot; # This is a bot class now
62  
63 Some things to know about how L<Bot::Net> bots work:
64  
65 =over
66  
67 =item *
68  
69 There is a one-to-one relationship between packages and bot instances. If you want two bots that do the same thing, you will need two different packages. Fortunately, it's easy to clone a bot:
70  
71 package MyBotNet::Bot::Chatbot;
72 use Bot::Net::Bot;
73  
74 # define some state handlers...
75
76 package MyBotNet::Bot::Chatbot::Larry;
77 use MyBotNet::Bot::Chatbot;
78  
79 package MyBotNet::Bot::Chatbot::Bob;
80 use MyBotNet::Bot::Chatbot;
81  
82 This defines three bots that all do the same thing--in this case, we probably only intend to invoke Larry and Bob, but you can do whatever you like.
83  
84 =item *
85  
86 TODO FIXME XXX Implement these things...
87  
88 Make sure you use the C<botnet> command to help you out in this process.
89  
90 bin/botnet bot create Chatbot
91 bin/botnet bot create Chatbot::Larry Chatbot
92 bin/botnet bot create Chatbot::Bob Chatbot
93  
94 This will create the scaffolding required to setup the classes mentioned in the previous bullet. You can then configure them to run:
95  
96 bin/botnet bot host Chatbot::Larry ServerA
97 bin/botnet bot host Chatbot::Bob ServerB
98  
99 =back
100  
101 =head1 METHODS
102  
103 =head2 import
104  
105 Custom exporter for this mixin.
106  
107 =cut
108  
109 sub import {
110 my $class = shift;
111  
112 $class->export_to_level(1, undef);
113 $class->export_poe_declarative_to_level;
114  
115 my $package = caller;
116 no strict 'refs';
117 push @{ $package . '::ISA' }, qw/ Bot::Net::Object /;
118 }
119  
120 =head2 bot
121  
122 This is a helper for L<POE::Declarative>. That lets you prefix "bot_" to your POE states. For example:
123  
124 on bot message_to_me => run { ... };
125  
126 is the same as:
127  
128 on bot_message_to_me => run { ... };
129  
130 It can also be used to yield messages:
131  
132 yield bot 'startup';
133  
134 You may choose to use it or not.
135  
136 =cut
137  
138 sub bot($) { 'bot_'.shift }
139  
140 =head1 setup
141  
142 This method is called to tell the bot to startup. It finds all the mixins that have been added into the class and calls the L</setup> method for each.
143  
144 =cut
145  
146 sub setup {
147 my $class = shift;
148 my $self = bless {}, $class;
149  
150 my $name = Bot::Net->short_name_for_bot($class);
151 my $config_file = Bot::Net->config->bot_file($name);
152  
153 my $config;
154 {
155 no strict 'refs';
156 $config = ${ $class . '::CONFIG' } || $config_file;
157 }
158  
159 my $brain = brain->new_heap( Hybrid => [] => 'Memory' );
160  
161 # Configuration is defined in the package itself (mostly for testing)
162 if (ref $config) {
163 $brain->remember( [ 'config' ] => $config );
164 }
165  
166 # Use the YAML config file
167 else {
168 -f $config_file
169 or die qq{Bot startup failed, }
170 .qq{no configuration found for $name: $config_file};
171  
172 $brain->register_brain(
173 config => [ YAML => file => $config_file ]
174 );
175 }
176  
177 if (my $state_file = $brain->recall([ config => 'state_file' ])) {
178 $brain->register_brain(
179 state => [ DBM => file => $state_file ]
180 );
181 }
182  
183 $brain->remember([ 'name' ] => $name);
184 $brain->remember([ 'log' ] => $self->log);
185  
186 # Setup any mixins
187 my $mixins = Bot::Net::Mixin::_mixins_for_package($class);
188 for my $mixin (@$mixins) {
189
190 # Don't setup this one
191 next if $mixin->isa('Bot::Net::Bot');
192  
193 if (my $method = $mixin->can('setup')) {
194 $method->($self, $brain);
195 }
196 }
197  
198 POE::Declarative->setup($self, $brain);
199 }
200  
201 =head2 default_configuration PACKAGE
202  
203 Returns a base configuration appropriate for all bots.
204  
205 =cut
206  
207 sub default_configuration {
208 my $class = shift;
209 my $package = shift;
210  
211 my $filename = join '/', split /::/,
212 Bot::Net->short_name_for_bot($package);
213  
214 return {
215 state_file => 'var/bot/'.$filename.'.db',
216 };
217 }
218  
219 =head1 BOT STATES
220  
221 =head2 on bot startup
222  
223 Bots should implement this event to perform any startup tasks. This is bot-specific and mixins should not do anything with this event.
224  
225 =head2 on bot quit MESSAGE
226  
227 Bots may emit this state to ask the protocol client and all resources attached to the bot to close. The C<MESSAGE> parameter allows you to pass a human readable message that can be passed on as part of the protocol quit or logged or whatever...
228  
229 If all mixins are implemented correctly, this should very quickly result in the bot entering the L</on _stop> state and L</on bot shutdown>. (If not, the bot may be stuck in a sort of zombie state unable to die.)
230  
231 =head2 on bot shtudown
232  
233 This is called (synchronously) during teh L</on _stop> handler immediately before shutdown to handle any last second clean up.
234  
235 =head1 MIXIN STATES
236  
237 The base mixin handles the following states.
238  
239 =head2 on _start
240  
241 Performs a number of setup tasks. Including:
242  
243 =over
244  
245 =item *
246  
247 Register to receive messages from the IRC component.
248  
249 =item *
250  
251 Connect to the IRC server.
252  
253 =item *
254  
255 When finished, it fires the L</on bot startup> event.
256  
257 =back
258  
259 =cut
260  
261 on _start => run {
262 my $self = get OBJECT;
263 my $name = recall 'name';
264 my $log = recall 'log';
265  
266 $log->info("Starting bot $name...");
267  
268 yield bot 'startup';
269 };
270  
271 =head2 on _default
272  
273 Performs logging of unhandled events. All these logs are put into the DEBUG log, so they won't show up unless DEBUG logging is enabled in your L<Log::Log4perl> configuration.
274  
275 =cut
276  
277 on _default => run {
278 my $log = recall 'log';
279 my ($event, $args) = @_[ ARG0 .. $#_ ];
280 my (@output);
281  
282 my $arg_number = 0;
283 foreach (@$args) {
284 SWITCH: {
285 if ( ref($_) eq 'ARRAY' ) {
286 push ( @output, "[", join ( ", ", @$_ ), "]" );
287 last SWITCH;
288 }
289 if ( ref($_) eq 'HASH' ) {
290 push ( @output, "{", join ( ", ", %$_ ), "}" );
291 last SWITCH;
292 }
293 unless ( defined $_ ) {
294 $_ = '';
295 }
296 push ( @output, "'$_'" );
297 }
298 $arg_number++;
299 }
300 $log->debug("$event ". join( ' ', @output ));
301 return 0; # Don't handle signals.
302 };
303  
304 =head2 on _stop
305  
306 This calls (synchronously) the L</on bot shutdown> state, to handle any final clean up before quitting.
307  
308 =cut
309  
310 on _stop => run {
311 call get(SESSION) => bot 'shutdown';
312 };
313  
314  
315 =head1 AUTHORS
316  
317 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
318  
319 =head1 COPYRIGHT AND LICENSE
320  
321 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
322  
323 This program is free software and may be modified and distributed under the same terms as Perl itself.
324  
325 =cut