User:CO/IRC

From Meta, a Wikimedia project coordination wiki
#!/usr/bin/perl

use strict;
use POE;
use POE::Component::IRC;

my $nickname = "KiloBot";
my $ircname = "KiloBot";
my $ircserver = "zelazny.freenode.net";
my $port = 6667;

my @channels = ( '#wikipedia-SCV' );
my $settings = {
        'irc.freenode.net' => {port => 6667, channels => [
'#wikipedia-SCV'], },
        'irc.wikimedia.org' => {port => 6668, channels => [
'#en.wikipedia'], },
};

foreach $server (keys %{$settings}) {
        POE::Component::IRC->spawn(
                alias=>$server,
                nick=>$nickname,
                ircname=>$ircname,
        ) || die "Could not create IRC: $!";
}

POE::Session->create(
        package_states => [ 'main'=> [ "_default", "_start", "irc_001",
"irc_public", "irc_ctcp_action", "irc_msg",
"irc_registered"], ],
        heap=>{config => $settings},
);

  $poe_kernel->run();
  exit 0;

  sub _start {
        ($kernel,$session) = @_[KERNEL,SESSION];

        # We get the session ID of the component from the object
	# and register and connect to the specified server.
	$kernel->signal( $kernel, 'POCOIRC_REGISTER', $session->ID(),
'all' );
        $kernel->post( 'irc.freenode.net' => privmsg =>
'nickserv' => "identify password");
        undef;
  }

  sub irc_registered {
    my ($kernel,$heap,$sender,$irc_object) =
@_[KERNEL,HEAP,SENDER,ARG0];

    my $alias = $irc_object->session_alias();

    my %conn_hash = (
        server => $alias,
        port   => $heap->{config}->{ $alias }->{port},
    );

    # In any irc_* events SENDER will be the PoCo-IRC session
    $kernel->post( $sender, 'connect', \%conn_hash );

    undef;
  }

sub irc_001 {
        ($kernel,$heap,$sender) = @_[KERNEL,HEAP,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";
    my $alias = $poco_object->session_alias();
    my @channels = @{ $heap->{config}->{ $alias }->{channels} };
        # In any irc_* events SENDER will be the PoCo-IRC session
	$kernel->post( $sender => join => $_ ) for @channels;
        undef;
}

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

sub irc_ctcp_action {
        ($kernel,$sender,$who,$where,$what) = @_[KERNEL,SENDER,ARG0,ARG1,ARG2];
        my $nick = ( split /!/, $who )[0];
        my $channel = $where->[0];
        if ($nick =~ /Wikihermit/ and $what =~ /pets_KiloBot/) {
                {$kernel->post( $sender => action => $channel => "purrs" )}
        }
}

sub irc_public {
        ($kernel,$sender,$who,$where,$what) = @_[KERNEL,SENDER,ARG0,ARG1,ARG2];
        my $nick = ( split /!/, $who )[0];
        my $channel = $where->[0];


        if ( $what =~ /Wikipedia:Suspected[\s_]copyright[\s_]violations/i and
$who=~/rc/i){
                sleep 2;
                $kernel->post( 'irc.freenode.net' => privmsg =>
'#wikipedia-SCV' => "New edit: $what" );
                print "See it\n";
        }
	elsif ( substr($what, 0, 4) eq "!run" and $who=~/Wikihermit/i ) {
		$what=~s/exit//gi;
		$val=eval substr($what, 5, length($what)-1);
		$kernel->post( $sender => privmsg => $channel => "$nick: $what = $val" );
	}
	elsif ( $what eq "KiloBot: DIE") {
		if (lc($nick) eq 'wikihermit') {die;} else {$kernel->post( 
'irc.freenode.net' => privmsg => $channel => "$nick: IMPOSTER!!!" )}
	}
        undef;
}

sub irc_msg {
        my ($kernel,$sender,$who,$me,$what) = @_[KERNEL,SENDER,ARG0,ARG1,ARG2];
        if ($what=~/(#wikipedia-SCV) (.*)/i) {
		$text=$2;
                $kernel->post( 'irc.freenode.net' => privmsg => "#wikipedia-SCV"
=> $text );
        }
}