The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POE::Component::IRC::State - a fully event-driven IRC client module with channel/nick tracking.

SYNOPSIS

  # A simple Rot13 'encryption' bot

  use strict;
  use warnings;
  use POE qw(Component::IRC::State);

  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::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) ],
        ],
        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];

    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;
  }

  # 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;
  }

DESCRIPTION

POE::Component::IRC::State is a sub-class of POE::Component::IRC which tracks IRC state entities such as nicks and channels. See the documentation for POE::Component::IRC for general usage. This document covers the extra methods that POE::Component::IRC::State provides.

The component tracks channels and nicks, so that it always has a current snapshot of what channels it is on and who else is on those channels. The returned object provides methods to query the collected state.

METHODS

All of the POE::Component::IRC methods are supported, plus the following:

channels

Takes no parameters. Returns a hashref, keyed on channel name and whether the bot is operator, halfop or has voice on that channel.

        foreach my $channel ( keys %{ $irc->channels() } ) {
                $irc->yield( 'privmsg' => $channel => 'm00!' );
        }

If the component happens to not be on any channels an empty hashref is returned.

nicks

Takes no parameters. Returns a list of all the nicks, including itself, that it knows about. If the component happens to be on no channels then an empty list is returned.

channel_list

Expects a channel as parameter. Returns a list of all nicks on the specified channel. If the component happens to not be on that channel an empty list will be returned.

is_operator

Expects a nick as parameter. Returns 1 if the specified nick is an IRC operator or 0 otherwise. If the nick does not exist in the state then a 0 will be returned.

is_channel_mode_set

Expects a channel and a single mode flag [A-Za-z]. Returns 1 if that mode is set on the channel, 0 otherwise.

channel_modes

Expects a channel as parameter. Returns channel modes or undef.

channel_limit

Expects a channel as parameter. Returns the channel limit or undef.

channel_key

Expects a channel as parameter. Returns the channel key or undef.

is_channel_member

Expects a channel and a nickname as parameters. Returns 1 if the specified nick is on the specified channel or 0 otherwise. If either channel or nick does not exist in the state then a 0 will be returned.

is_channel_owner

Expects a channel and a nickname as parameters. Returns 1 if the specified nick is an owner on the specified channel or 0 otherwise. If either channel or nick does not exist in the state then a 0 will be returned.

is_channel_admin

Expects a channel and a nickname as parameters. Returns 1 if the specified nick is an admin on the specified channel or 0 otherwise. If either channel or nick does not exist in the state then a 0 will be returned.

is_channel_operator

Expects a channel and a nickname as parameters. Returns 1 if the specified nick is an operator on the specified channel or 0 otherwise. If either channel or nick does not exist in the state then a 0 will be returned.

is_channel_halfop

Expects a channel and a nickname as parameters. Returns 1 if the specified nick is a half-op on the specified channel or 0 otherwise. If either channel or nick does not exist in the state then a 0 will be returned.

has_channel_voice

Expects a channel and a nickname as parameters. Returns 1 if the specified nick has voice on the specified channel or 0 otherwise. If either channel or nick does not exist in the state then a 0 will be returned.

nick_long_form

Expects a nickname. Returns the long form of that nickname, ie. <nick>!<user>@<host> or undef if the nick is not in the state.

nick_channels

Expects a nickname. Returns a list of the channels that that nickname and the component are on. An empty list will be returned if the nickname does not exist in the state.

nick_info

Expects a nickname. Returns a hashref containing similar information to that returned by WHOIS. Returns an undef if the nickname doesn't exist in the state. The hashref contains the following keys: 'Nick', 'User', 'Host', 'Userhost', 'Real', 'Server' and, if applicable, 'IRCop'.

ban_mask

Expects a channel and a ban mask, as passed to MODE +b-b. Returns a list of nicks on that channel that match the specified ban mask or an empty list if the channel doesn't exist in the state or there are no matches.

channel_ban_list

Expects a channel as a parameter. Returns a hashref containing the banlist if the channel is in the state, undef if not. The hashref keys are the entries on the list, each with the keys 'SetBy' and 'SetAt'. These keys will hold the nick!hostmask of the user who set the entry (or just the nick if it's all the ircd gives us), and the time at which it was set respectively.

channel_invex_list

Expects a channel as a parameter. Returns a hashref containing the invite exception list if the channel is in the state, undef if not. The hashref keys are the entries on the list, each with the keys 'SetBy' and 'SetAt'. These keys will hold the nick!hostmask of the user who set the entry (or just the nick if it's all the ircd gives us), and the time at which it was set respectively.

channel_except_list

Expects a channel as a parameter. Returns a hashref containing the ban exception list if the channel is in the state, undef if not. The hashref keys are the entries on the list, each with the keys 'SetBy' and 'SetAt'. These keys will hold the nick!hostmask of the user who set the entry (or just the nick if it's all the ircd gives us), and the time at which it was set respectively.

channel_topic

Expects a channel as a parameter. Returns a hashref containing topic information if the channel is in the state, undef if not. The hashref contains the following keys: 'Value', 'SetBy', 'SetAt'. These keys will hold the topic itself, the nick!hostmask of the user who set it (or just the nick if it's all the ircd gives us), and the time at which it was set respectively.

nick_channel_modes

Expects a channel and a nickname as parameters. Returns the modes of the specified nick on the specified channel (ie. qaohv). If the nick is not on the channel in the state, undef will be returned.

OUTPUT

As well as all the usual POE::Component::IRC 'irc_*' events, there are the following events you can register for:

irc_chan_sync

Sent whenever the component has completed synchronising a channel that it has joined. ARG0 is the channel name and ARG1 is the time in seconds that the channel took to synchronise.

irc_chan_sync_invex

Sent whenever the component has completed synchronising a channel's INVEX ( invite list ). Usually triggered by the component being opped on a channel. ARG0 is the channel.

irc_chan_sync_excepts

Sent whenever the component has completed synchronising a channel's EXCEPTS ( ban exemption list ). Usually triggered by the component being opped on a channel. ARG0 is the channel.

irc_nick_sync

Sent whenever the component has completed synchronising a user who has joined a channel the component is on. ARG0 is the user's nickname and ARG1 the channel they have joined.

irc_chan_mode

This is almost identical to irc_mode, except that it's sent once for each individual mode with it's respective argument if it has one (ie. the banmask if it's +b or -b). However, this event is only sent for channel modes.

The following two 'irc_*' events are the same as their POE::Component::IRC counterparts, with the additional parameters:

irc_quit

ARG2 contains an arrayref of channel names that are common to the quitting client and the component.

irc_nick

ARG2 contains an arrayref of channel names that are common to the nick changing client and the component.

irc_kick

Additional parameter ARG4 contains the full nick!user@host of the kicked individual.

CAVEATS

The component gathers information by registering for 'irc_quit', 'irc_nick', 'irc_join', 'irc_part', 'irc_mode', 'irc_kick' and various numeric replies. When the component is asked to join a channel, when it joins it will issue a 'WHO #channel' and a 'MODE #channel'. These will solicit between them the numerics, 'irc_352' and 'irc_324'. You may want to ignore these. When someone joins a channel the bot is on, it issues a 'WHO nick'.

Currently, whenever the component sees a topic or channel list change, it will use time() for the SetAt value and the full address of the user who set it for the SetBy value. When an ircd gives us it's record of such changes, it will use it's own time (obviously) and may only give us the nickname of the user, rather than their full address. Thus, if our time() and the ircd's time do not match, or the ircd uses the nickname only, ugly inconsistencies can develop. This leaves the SetAt and SetBy values at best, inaccurate, and you should use them with this in mind (for now, at least).

AUTHOR

Chris Williams <chris@bingosnet.co.uk>

With contributions from the Kinky Black Goat.

LICENCE

This module may be used, modified, and distributed under the same terms as Perl itself. Please see the license that came with your Perl distribution for details.

SEE ALSO

POE::Component::IRC