LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2007, 10:42 AM   #1
Kurt M. Weber
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 335

Rep: Reputation: 36
POE::Component::IRC::State and nickname changes


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:
Code:
$VAR1 = [];
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;
}

Last edited by Kurt M. Weber; 11-10-2007 at 10:45 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: State by state, Microsoft responds to creeping threat LXer Syndicated Linux News 0 05-01-2007 07:16 AM
POE Curses sci3ntist Programming 0 04-12-2007 03:36 AM
Poe sci3ntist Programming 0 03-25-2007 08:32 AM
Why/How did you pick the nickname you did? Gill Bates General 121 12-30-2005 11:41 PM
how did you choose ur LQ nickname? da_zombie General 21 11-08-2004 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration