IRC::Bot::Hangman::Command - Hangman commands' plugin engine


IRC-Bot-Hangman documentation Contained in the IRC-Bot-Hangman distribution.

Index


Code Index:

NAME

Top

IRC::Bot::Hangman::Command - Hangman commands' plugin engine

SYNOPSIS

Top

See IRC::Bot::Hangman

  use IRC::Bot::Hangman::Command;
  IRC::Bot::Hangman::Command->run( 'play' );

DESCRIPTION

Top

This module execute the commands by calling the right plugin

pre_process( robot )

Call pre_process on all plugins which have implemented pre_process()

post_process( robot )

Call post_process on all plugins which have implemented pre_process()

run( robot )

Execute a command if registered in a IRC::Bot::Hangman::Command plugin

AUTHOR

Top

Pierre Denis <pierre@itrelease.net>

http://www.itrelease.net/

COPYRIGHT

Top


IRC-Bot-Hangman documentation Contained in the IRC-Bot-Hangman distribution.
package IRC::Bot::Hangman::Command;
use warnings::register;
use strict;
use Carp qw( carp croak );
use Module::Find qw( useall );

our @PLUGINS = useall( 'IRC::Bot::Hangman::Command' );
our %COMMANDS     = map { %{ $_->can('commands')  ? $_->commands : {} } } @PLUGINS;
our @PRE_PROCESS  = map { $_->can('pre_process')  ? $_           : ()   } @PLUGINS;
our @POST_PROCESS = map { $_->can('post_process') ? $_           : ()   } @PLUGINS;



sub pre_process {
  my $class = shift;
  my $robot = shift;
  $class->_do_process( $robot, 'pre_process', \@PRE_PROCESS );
}

sub post_process {
  my $class = shift;
  my $robot = shift;
  $class->_do_process( $robot, 'post_process', \@POST_PROCESS );
}


sub _do_process {
  my $class   = shift;
  my $robot   = shift;
  my $cmd     = shift;
  my $classes = shift || [];
  foreach my $pclass ( @$classes ) {
    $pclass->$cmd( $robot );
  }
}


sub run {
  my $class  = shift;
  my $robot  = shift;

  $class->pre_process( $robot );

  my (@args) = split /[\s,.;]+/, ( $robot->input || '');
  my $cmd    = lc shift @args;
  if ( my $cmd_ref = $COMMANDS{$cmd} ) {
    $cmd_ref->( $robot, @args );
  }

  $class->post_process( $robot );
}


1;