I'm using POE::Component::IRC::State to write a multipurpose IRC bot in Perl. I figured I'd start by basing it off the example code given in the module's documentation, removing what I didn't need (the rot13 stuff) and adding what I did need.
According to the module's documentation, ARG2 passed to the irc_nick handler should include an arrayref of all the channels in which the nickname change that generated the event was observed by the module. To check this, I used Data:
umper to display the contents of ARG2. It displays an empty set, to wit:
Is there something blatantly obvious I'm missing here?
Code:
#!/usr/bin/perl
use warnings;
use strict;
use POE qw(Component::IRC::State);
use Data::Dumper;
my $nickname = 'Shostakovich';
my $ircname = 'Dmitri Dmitrievich Shostakovich';
my $ircserver = 'irc.outwardhosting.com';
my $port = 6667;
my @channels = ( '#bottest' );
my $irc = POE::Component::IRC::State->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 irc_nick) ],
],
heap => { irc => $irc },
);
$poe_kernel->run();
exit 0;
sub irc_nick {
my ($sender, $newnick, $where) = @_[SENDER,ARG1,ARG2];
print Dumper($where);
}
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];
my $poco_object = $sender->get_heap();
if ( my ($rot13) = $what =~ /^rot13 (.+)/ ) {
# Only operators can issue a rot13 command to us.
return unless $poco_object->is_channel_operator( $channel, $nick );
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
$kernel->post( $sender => privmsg => $channel => "$nick: $rot13" );
}
undef;
}
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;
}