Manual talk:MediaWiki-Recent Changes-IRCBot

irc component?

cpan> install POE::Component::IRC

85.157.173.47 20:57, 1 June 2009 (UTC)

The current WikiBot.pl file ends up messing up the data that is sent to it - namely, the bot only relays the title of the article that was edited. A fixed version of WikiBot.pl is below. FireCrotch 04:05, 12 June 2010 (UTC)


 * 1) !/usr/bin/perl

use warnings; use strict;

use POE; use IO::Socket::INET; use POE::Component::IRC;

use constant DATAGRAM_MAXLEN => 1024;

select((select(STDOUT), $|=1)[0]);

my ($irc) = POE::Component::IRC->spawn;
 * 1) Create the component that will represent an IRC network.

POE::Session->create(                    inline_states => {        _start     => \&bot_start,        irc_001    => \&on_connect,        irc_public => \&on_public,    },                     );
 * 1) Create the bot session.  The new call specifies the events the bot
 * 2) knows about and the functions that will handle those events.

POE::Session->create(   inline_states => {        _start       => \&server_start,        get_datagram => \&server_read,      } );

$poe_kernel->run;

exit;

sub server_start { my $kernel = $_[KERNEL];
 * 1) UDP Server

my $socket = IO::Socket::INET->new(       Proto     => 'udp',        LocalPort => 51666,    );

die "Couldn't create server socket: $!" unless $socket; $kernel->select_read( $socket, "get_datagram" ); }

sub server_read { my ( $kernel, $socket ) = @_[ KERNEL, ARG0 ]; my $ircmessage = ""; recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 ); $irc->yield( privmsg => "#ChekMate", $message );

}


 * 1) IRC Server

sub bot_start { my $kernel = $_[KERNEL]; my $heap   = $_[HEAP]; my $session = $_[SESSION];
 * 1) The bot session has started.  Register this bot with the "magnet"
 * 2) IRC component.  Select a nickname.  Connect to a server.

$irc->yield( register => "all" );

my $nick = 'WikiBot'; $irc->yield( connect =>                { Nick => $nick,            Username => 'WikiBot',            Ircname  => 'ChekMate WikiBot',            Server   => 'irc.chekmate.org',            Port     => '6667',               }                 ); }

sub on_connect { $irc->yield( join => "#ChekMate" ); }
 * 1) The bot has successfully connected to a server.  Join a channel.

sub on_public { my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ]; my $nick = ( split /!/, $who )[0]; my $channel = $where->[0];
 * 1) The bot has received a public message.  Parse it for commands, and
 * 2) respond to interesting things.

my $ts = scalar localtime; print " [$ts] <$nick:$channel> $msg\n";

# Send a response back to the server. }
 * 1)    if ( my ($rot13) = $msg =~ /^rot13 (.+)/ ) {
 * 2)        $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
 * 1)        $irc->yield( privmsg => "#ChekMate", $rot13 );
 * 2)    }