Manual talk:MediaWiki-Recent Changes-IRCBot
From MediaWiki.org
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)
#!/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]);
# Create the component that will represent an IRC network.
my ($irc) = POE::Component::IRC->spawn();
# Create the bot session. The new() call specifies the events the bot
# knows about and the functions that will handle those events.
POE::Session->create(
inline_states => {
_start => \&bot_start,
irc_001 => \&on_connect,
irc_public => \&on_public,
},
);
POE::Session->create(
inline_states => {
_start => \&server_start,
get_datagram => \&server_read,
}
);
$poe_kernel->run();
exit;
# UDP Server
sub server_start {
my $kernel = $_[KERNEL];
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 );
}
# IRC Server
# The bot session has started. Register this bot with the "magnet"
# IRC component. Select a nickname. Connect to a server.
sub bot_start {
my $kernel = $_[KERNEL];
my $heap = $_[HEAP];
my $session = $_[SESSION];
$irc->yield( register => "all" );
my $nick = 'WikiBot';
$irc->yield( connect =>
{ Nick => $nick,
Username => 'WikiBot',
Ircname => 'ChekMate WikiBot',
Server => 'irc.chekmate.org',
Port => '6667',
}
);
}
# The bot has successfully connected to a server. Join a channel.
sub on_connect {
$irc->yield( join => "#ChekMate" );
}
# The bot has received a public message. Parse it for commands, and
# respond to interesting things.
sub on_public {
my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ];
my $nick = ( split /!/, $who )[0];
my $channel = $where->[0];
my $ts = scalar localtime;
print " [$ts] <$nick:$channel> $msg\n";
# if ( my ($rot13) = $msg =~ /^rot13 (.+)/ ) {
# $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
# Send a response back to the server.
# $irc->yield( privmsg => "#ChekMate", $rot13 );
# }
}