Manual:MediaWiki-Recent Changes-IRCBot


 * Not to be confused with Manual:Wikibot, a configurable PHP-based bot framework.

Introduction
Modified the Manual:Simple IRC RC Bot created by Thrasher6670. This bot does not require ircii. It is completely self contained. It displays recent changes of your wiki to an IRC channel.

Note: Most up to date version of this extension can be found at: http://www.chekmate.org/wiki/index.php/Projects

Change History

 * 18:08, 3 August 2006 (UTC) - WikiBot.php published (Smcnaught)

Maintainer
Shannon McNaught (smcnaught) - I am also available on irc.chekmate.org #MediaWiki

Homepage
ChekMate Technical Focus Group

License
WikiBot.php - Displays recent changes of your wiki to an IRC channel

Copyright (C) 2006 Shannon McNaught

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Requirements
This Perl script also requires POE (and the IRC Component), which you may find was not packaged with your system. Not to worry! Very easy to install.

POE may be installed through the CPAN shell which you should find on your system.

% perl -MCPAN -e shell cpan> install POE cpan> install POE::Component::IRC

When CPAN is first run, it will ask you a series of questions, however the defaults seemed to work fine for me. However I did have a problem with some FTP sites not having the POE file, so I went for ones in the US and they had it.

You may find that POE fails to install with a message that says cannot install unless force is used as a number of tests have failed. You might want to check the seriousness of these errors, however its possible to force via the following syntax.

cpan> force install POE cpan> install POE::Component::IRC

LocalSettings.php
Add the following to your local settings file:

// IRC # post: 555666 $wgRC2UDPAddress = '127.0.0.1'; $wgRC2UDPPort = '51666'; $wgRC2UDPPrefix = "";

WikiBot.pl

 * 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 ); $message =~ /\[\[(.+)\]\]/s; $ircmessage = $1; $irc->yield( privmsg => "#ChekMate", $ircmessage );

}


 * 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 = 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)    }

Command
Run this command on the receiving computer:

./WikiBot.pl

Caveats
Currently, anyone can "hack" your bot by sending packets to the port you specify. This can be fixed with iptables.

However, in the long run, a more complex bot should be written to allow for restrictions and perhaps some more stuff.