POE::Component::IRC 4.93 review
DownloadPOE::Component::IRC is a fully event-driven IRC client module. SYNOPSIS # A simple Rot13 'encryption' bot use strict;
|
|
POE::Component::IRC is a fully event-driven IRC client module.
SYNOPSIS
# A simple Rot13 'encryption' bot
use strict;
use warnings;
use POE qw(Component::IRC);
my $nickname = 'Flibble' . $$;
my $ircname = 'Flibble the Sailor Bot';
my $ircserver = 'irc.blahblahblah.irc';
my $port = 6667;
my @channels = ( '#Blah', '#Foo', '#Bar' );
# We create a new PoCo-IRC object and component.
my $irc = POE::Component::IRC->spawn(
nick => $nickname,
server => $ircserver,
port => $port,
ircname => $ircname,
) or die "Oh noooo! $!";
POE::Session->create(
package_states => [
'main' => [ qw(_default _start irc_001 irc_public) ],
],
heap => { irc => $irc },
);
$poe_kernel->run();
exit 0;
sub _start {
my ($kernel,$heap) = @_[KERNEL,HEAP];
# We get the session ID of the component from the object
# and register and connect to the specified server.
my $irc_session = $heap->{irc}->session_id();
$kernel->post( $irc_session => register => 'all' );
$kernel->post( $irc_session => connect => { } );
undef;
}
sub irc_001 {
my ($kernel,$sender) = @_[KERNEL,SENDER];
# Get the component's object at any time by accessing the heap of
# the SENDER
my $poco_object = $sender->get_heap();
print "Connected to ", $poco_object->server_name(), "n";
# In any irc_* events SENDER will be the PoCo-IRC session
$kernel->post( $sender => join => $_ ) for @channels;
undef;
}
sub irc_public {
my ($kernel,$sender,$who,$where,$what) = @_[KERNEL,SENDER,ARG0,ARG1,ARG2];
my $nick = ( split /!/, $who )[0];
my $channel = $where->[0];
if ( my ($rot13) = $what =~ /^rot13 (.+)/ ) {
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
$kernel->post( $sender => privmsg => $channel => "$nick: $rot13" );
}
undef;
}
# We registered for all events, this will produce some debug info.
sub _default {
my ($event, $args) = @_[ARG0 .. $#_];
my @output = ( "$event: " );
foreach my $arg ( @$args ) {
if ( ref($arg) eq 'ARRAY' ) {
push( @output, "[" . join(" ,", @$arg ) . "]" );
} else {
push ( @output, "'$arg'" );
}
}
print STDOUT join ' ', @output, "n";
return 0;
}
POE::Component::IRC is a POE component (who'd have guessed?) which acts as an easily controllable IRC client for your other POE components and sessions. You create an IRC component and tell it what events your session cares about and where to connect to, and it sends back interesting IRC events when they happen. You make the client do things by sending it events. That's all there is to it. Cool, no?
[Note that using this module requires some familiarity with the details of the IRC protocol. I'd advise you to read up on the gory details of RFC 1459 before you get started. Keep the list of server numeric codes handy while you program. Needless to say, you'll also need a good working knowledge of POE, or this document will be of very little use to you.]
The POE::Component::IRC distribution has a docs/ folder with a collection of salient documentation including the pertinent RFCs.
POE::Component::IRC consists of a POE::Session that manages the IRC connection and dispatches 'irc_' prefixed events to interested sessions and an object that can be used to access additional information using methods.
Sessions register their interest in receiving 'irc_' events by sending 'register' to the component. One would usually do this in your _start handler. Your session will continue to receive events until you 'unregister'. The component will continue to stay around until you tell it not to with 'shutdown'.
The SYNOPSIS demonstrates a fairly basic bot.
Requirements:
Perl
POE::Component::IRC 4.93 search tags