Bot-Net

 view release on metacpan or  search on metacpan

lib/Bot/Net/Bot.pm  view on Meta::CPAN

            config => [ YAML => file => $config_file ]
        );
    }

    if (my $state_file = $brain->recall([ config => 'state_file' ])) {
        $brain->register_brain(
            state => [ DBM => file => $state_file ]
        );
    }

    $brain->remember([ 'name' ] => $name);
    $brain->remember([ 'log'  ] => $self->log);

    # Setup any mixins
    my $mixins = Bot::Net::Mixin::_mixins_for_package($class);
    for my $mixin (@$mixins) {
        
        # Don't setup this one
        next if $mixin->isa('Bot::Net::Bot');

        if (my $method = $mixin->can('setup')) {
            $method->($self, $brain);
        }
    }

    POE::Declarative->setup($self, $brain);
}

=head2 default_configuration PACKAGE

Returns a base configuration appropriate for all bots.

=cut

sub default_configuration {
    my $class   = shift;
    my $package = shift;

    my $filename = join '/', split /::/,
        Bot::Net->short_name_for_bot($package);

    return {
        state_file => 'var/bot/'.$filename.'.db',
    };
}

=head1 BOT STATES

=head2 on bot startup

Bots should implement this event to perform any startup tasks. This is bot-specific and mixins should not do anything with this event.

=head2 on bot quit MESSAGE

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...

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.)

=head2 on bot shtudown

This is called (synchronously) during teh L</on _stop> handler immediately before shutdown to handle any last second clean up.

=head1 MIXIN STATES

The base mixin handles the following states.

=head2 on _start

Performs a number of setup tasks. Including:

=over

=item *

Register to receive messages from the IRC component.

=item *

Connect to the IRC server.

=item *

When finished, it fires the L</on bot startup> event.

=back

=cut

on _start => run {
    my $self = get OBJECT;
    my $name = recall 'name';
    my $log  = recall 'log';

    $log->info("Starting bot $name...");

    yield bot 'startup';
};

=head2 on _default

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.

=cut

on _default => run {
    my $log = recall 'log';
    my ($event, $args) = @_[ ARG0 .. $#_ ];
    my (@output);

    my $arg_number = 0;
    foreach (@$args) {
        SWITCH: {
            if ( ref($_) eq 'ARRAY' ) {
                push ( @output, "[", join ( ", ", @$_ ), "]" );
                last SWITCH;
            }
            if ( ref($_) eq 'HASH' ) {
                push ( @output, "{", join ( ", ", %$_ ), "}" );
                last SWITCH;
            }
            unless ( defined $_ ) {
                $_ = '';
            }
            push ( @output, "'$_'" );
        }
        $arg_number++;
    }
    $log->debug("$event ". join( ' ', @output ));
    return 0;    # Don't handle signals.
};

=head2 on _stop

This calls (synchronously) the L</on bot shutdown> state, to handle any final clean up before quitting.

=cut

on _stop => run {
    call get(SESSION) => bot 'shutdown';
};


=head1 AUTHORS

Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>

=head1 COPYRIGHT AND LICENSE

Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.

This program is free software and may be modified and distributed under the same terms as Perl itself.

=cut



( run in 0.696 second using v1.01-cache-2.11-cpan-e1769b4cff6 )