Manual:MediaWiki-Recent Changes-IRCBot

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

Introduction[edit]

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[edit]

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

Maintainer[edit]

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

Homepage[edit]

ChekMate Technical Focus Group

License[edit]

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.

Installation[edit]

Requirements[edit]

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[edit]

Add the following to your local settings file:

// IRC port #: 51666
$wgRCFeeds['irc'] = array(
    'formatter' => 'IRCColourfulRCFeedFormatter',
    'uri' => 'udp://localhost:51666',
    'add_interwiki_prefix' => false,
    'omit_bots' => true,
);

WikiBot.pl[edit]

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

}



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

Command[edit]

Run this command on the receiving computer:

./WikiBot.pl

Caveats[edit]

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.