NAME

POE::Component::IRC::Plugin::BotCommand - A PoCo-IRC plugin which makes it easy to handle commands issued to your bot.

SYNOPSIS

use POE;
use POE::Component::Client::DNS;
use POE::Component::IRC;
use POE::Component::IRC::Plugin::BotCommand;

my @channels = ('#channel1', '#channel2');
my $dns = POE::Component::Client::DNS->spawn();
my $irc = POE::Component::IRC->spawn(
    nick   => 'YourBot',
    server => 'some.irc.server',
);

POE::Session->create(
    package_states => [
        main => [ qw(_start irc_001 irc_botcmd_slap irc_botcmd_lookup dns_response) ],
    ],
);

$poe_kernel->run();

sub _start {
    $irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new(
        Commands => {
            slap   => 'Takes one argument: a nickname to slap.',
            lookup => 'Takes two arguments: a record type (optional), and a host.',
        }
    ));
    $irc->yield(register => qw(001 botcmd_slap botcmd_lookup));
    $irc->yield(connect => { });
}

# join some channels
sub irc_001 {
    $irc->yield(join => $_) for @channels;
    return;
}

# the good old slap
sub irc_botcmd_slap {
    my $nick = (split /!/, $_[ARG0])[0];
    my ($channel, $arg) = @_[ARG1, ARG2];
    $irc->yield(ctcp => $channel, "ACTION slaps $arg");
    return;
}

# non-blocking dns lookup
sub irc_botcmd_lookup {
    my $nick = (split /!/, $_[ARG0])[0];
    my ($channel, $arg) = @_[ARG1, ARG2];
    my ($type, $host) = $arg =~ /^(?:(\w+) )?(\S+)/;
    
    my $res = $dns->resolve(
        event => 'dns_response',
        host => $host,
        type => $type,
        context => {
            channel => $channel,
            nick    => $nick,
        },
    );
    $poe_kernel->yield(dns_response => $res) if $res;
    return;
}

sub dns_response {
    my $res = $_[ARG0];
    my @answers = map { $_->rdatastr } $res->{response}->answer() if $res->{response};
    
    $irc->yield(
        'notice',
        $res->{context}->{channel},
        $res->{context}->{nick} . (@answers
            ? ": @answers"
            : ': no answers for "' . $res->{host} . '"')
    );

    return;
}

DESCRIPTION

POE::Component::IRC::Plugin::BotCommand is a POE::Component::IRC plugin. It provides you with a standard interface to define bot commands and lets you know when they are issued. Commands are accepted as channel messages. However, if someone does /msg YourBot help, they will receive a list of available commands, and information on how to use them.

METHODS

new

Four optional arguments:

'Commands', a hash reference, with your commands as keys, and usage information as values. If the usage string contains newlines, the component will send one message for each line.

'Addressed', requires users to address the bot by name in order to issue commands. Default is true.

'Prefix', if 'Addressed' is false, all commands must be prefixed with this string. Default is '!'. You can set it to '' to allow bare commands.

'Eat', set to true to make the plugin hide irc_public events from other plugins if they contain a valid command. Default is false.

Returns a plugin object suitable for feeding to POE::Component::IRC's plugin_add() method.

add

Adds a new command. Takes two arguments, the name of the command, and a string containing its usage information. Returns false if the command has already been defined, true otherwise.

remove

Removes a command. Takes one argument, the name of the command. Returns false if the command wasn't defined to begin with, true otherwise.

list

Takes no arguments. Returns a list of key/value pairs, the keys being the command names and the values being the usage strings.

OUTPUT

irc_botcmd_*

You will receive an event like this for every valid command issued. E.g. if 'slap' were a valid command, you would receive an irc_botcmd_slap event every time someone issued that command. ARG0 is the nick!hostmask of the user who issued the command. ARG1 is the name of the channel in which the command was issued. If the command was followed by any arguments, ARG2 will be a string containing them, otherwise ARG2 will be undefined.

AUTHOR

Hinrik Örn Sigurðsson, hinrik.sig@gmail.com