| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Bot::Net::Mixin; | ||
| 5 | use base qw/ Bot::Net::Object POE::Declarative::Mixin /; | ||
| 6 | |||
| 7 | use Data::Remember POE => 'Memory'; | ||
| 8 | use POE; | ||
| 9 | use POE::Declarative; | ||
| 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 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Bot::Net::Mixin - build complex objects my mixing components | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | # Define your own mixin | ||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | |||
| 37 | package MyBotNet::Mixin::Bot::Counter; | ||
| 38 | use base qw/ Bot::Net::Mixin /; | ||
| 39 | |||
| 40 | use Bot::Net::Mixin::Bot::Command; | ||
| 41 | |||
| 42 | # Add a counter command to a bot | ||
| 43 | |||
| 44 | sub setup { | ||
| 45 | my $self = shift; | ||
| 46 | my $brain = shift; | ||
| 47 | |||
| 48 | $brain->remember( [ 'counter' ] => 0 ); | ||
| 49 | } | ||
| 50 | |||
| 51 | on bot command next => run { | ||
| 52 | my $event = get ARG0; | ||
| 53 | |||
| 54 | my $counter = recall 'counter' + 1; | ||
| 55 | remember counter => $counter; | ||
| 56 | |||
| 57 | yield reply_to_sender => $event => $counter; | ||
| 58 | }; | ||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | This is the base class for all L<Bot::Net> mixins. It basically provides for a way of cataloging which mixins a class has added, tools for mixin setup, and magic for pulling mixin stuff into the importing package. | ||
| 65 | |||
| 66 | =head1 METHODS | ||
| 67 | |||
| 68 | =head2 import | ||
| 69 | |||
| 70 | Exports anything the module lists in it's C<@EXPORT> variable, if such a method exists. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub import { | ||
| 75 | my $self = shift; | ||
| 76 | my $caller = caller; | ||
| 77 | |||
| 78 | $self->export_to_level(1, undef); | ||
| 79 | |||
| 80 | # Sombody else is creating a new sweet custom mixin | ||
| 81 | if ($self eq __PACKAGE__) { | ||
| 82 | no strict 'refs'; | ||
| 83 | push @{ $caller . '::ISA' }, __PACKAGE__; | ||
| 84 | } | ||
| 85 | |||
| 86 | # Sombody is using somebody else's sweet custom mixin | ||
| 87 | else { | ||
| 88 | my $mixins = Bot::Net::Mixin::_mixins_for_package($caller); | ||
| 89 | push @$mixins, $self; | ||
| 90 | |||
| 91 | $self->export_poe_declarative_to_level(1); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _mixins_for_package { | ||
| 96 | my $package = shift; | ||
| 97 | |||
| 98 | no strict 'refs'; | ||
| 99 | return ${ $package . '::_BOT_NET_MIXINS' } ||= []; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 default_configuration PACKAGE | ||
| 103 | |||
| 104 | This is used to build the default configuration file when creating a new bot or server using the L<botnet> script and used by L<Bot::Net::Test> to help build the base test bot or test server configuration. | ||
| 105 | |||
| 106 | A mixin should override this to return a reference to a hash containing any default values it recommends or requires for bot configuration. It will be called as a class level method and passed the name of the C<PACKAGE> that the mixin is being used to build: | ||
| 107 | |||
| 108 | MyBotNet::Mixin::Bot::PasteBot->default_configuration('MyBotNet::Bot::FancyPaster'); | ||
| 109 | |||
| 110 | =head1 MIXIN IMPLEMENTATIONS | ||
| 111 | |||
| 112 | A mixin may implement whatever POE states it wishes to using the L<POE::Declarative> interface. Those states will be imported into the calling package. | ||
| 113 | |||
| 114 | If a mixin needs to perform any setup prior to L<POE::Kernel> startup, it may be do so by implementing a C<setup> method. It will be passed two arguments, the C<$self> variable for the class stored with the session and the L<Data::Remember> brain object that will be stored in the heap of the session being created. | ||
| 115 | |||
| 116 | sub setup { | ||
| 117 | my $self = shift; | ||
| 118 | my $brain = shift; | ||
| 119 | |||
| 120 | # Using the internal brain interface of Data::Remember, make sure to | ||
| 121 | # be careful to use only array-based ques! | ||
| 122 | $brain->remember( [ 'foo' ] => 'bar' ); | ||
| 123 | } | ||
| 124 | |||
| 125 | In general, however, your mixins can probably get away with performing initial setup in the C<_start> state. | ||
| 126 | |||
| 127 | =head1 AUTHORS | ||
| 128 | |||
| 129 | Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >> | ||
| 130 | |||
| 131 | =head1 COPYRIGHT AND LICENSE | ||
| 132 | |||
| 133 | Copyright 2007 Boomer Consulting, Inc. All Rights Reserved. | ||
| 134 | |||
| 135 | This program is free software and may be modified and distributed under the same terms as Perl itself. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | 1; |