r106385 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106384‎ | r106385 | r106386 >
Date:22:15, 15 December 2011
Author:overlordq
Status:ok (Comments)
Tags:
Comment:
Commit combined udp server/irc client forwarder to repo
Modified paths:
  • /trunk/tools/rcbot (added) (history)
  • /trunk/tools/rcbot/rcbot.pl (added) (history)

Diff [purge]

Index: trunk/tools/rcbot/rcbot.pl
@@ -0,0 +1,136 @@
 2+#!/usr/bin/perl
 3+
 4+use strict;
 5+use warnings;
 6+use POE qw(Component::IRC Component::IRC::Plugin::Connector);
 7+use IO::Socket::INET;
 8+
 9+use constant DATAGRAM_MAXLEN => 1024;
 10+
 11+my $nickname = 'rcbot' . $$;
 12+my $ircname = 'RCBot for a Wiki';
 13+my $UDPport = 8675; #UDP Port to listen on
 14+my $debug = 0; #Only set if you really really mean it
 15+my $spam = 0; #For High-Volume wikis, to keep up with RC Feed, set to 1.
 16+ #NOTE: May require special ircd configuration or bot may
 17+ #be disconnected for flooding.
 18+
 19+my $settings = {
 20+ 'irc.yourserver.com' => { port => 6667, channels => ['#WPFeed'], },
 21+ 'irc.otherserver.com' => { port => 6667, channels => ['#OtherChan'], },
 22+};
 23+
 24+foreach my $server ( keys %{$settings} ) {
 25+ POE::Component::IRC->spawn(
 26+ alias => $server,
 27+ nick => $nickname,
 28+ ircname => $ircname,
 29+ Flood => $spam,
 30+ );
 31+}
 32+
 33+POE::Session->create(
 34+ package_states => [
 35+ 'main' => [
 36+ qw(
 37+ irc_start
 38+ irc_registered
 39+ irc_001
 40+ _default )
 41+ ],
 42+ ],
 43+ inline_states => {
 44+ _start => \&server_start,
 45+ get_datagram => \&server_read,
 46+ },
 47+ heap => { config => $settings },
 48+);
 49+
 50+POE::Kernel->run();
 51+exit;
 52+
 53+sub irc_start {
 54+ my ( $kernel, $session ) = @_[ KERNEL, SESSION ];
 55+ $kernel->signal( $kernel, 'POCOIRC_REGISTER', $session->ID(), 'all' );
 56+ undef;
 57+}
 58+
 59+sub server_start {
 60+ my ( $kernel, $session ) = $_[KERNEL];
 61+
 62+ my $socket = IO::Socket::INET->new(
 63+ Proto => 'udp',
 64+ LocalPort => $UDPport,
 65+ );
 66+
 67+ die "Couldn't create server socket: $!" unless $socket;
 68+ $kernel->select_read( $socket, "get_datagram" );
 69+ $kernel->yield('irc_start');
 70+ undef;
 71+}
 72+
 73+sub server_read {
 74+ my ( $kernel, $heap, $socket ) = @_[ KERNEL, HEAP, ARG0 ];
 75+
 76+ my $remote_address = recv( $socket, my $message = "", DATAGRAM_MAXLEN, 0 );
 77+ return unless defined $remote_address;
 78+
 79+ my ( $peer_port, $peer_addr ) = unpack_sockaddr_in($remote_address);
 80+ my $human_addr = inet_ntoa($peer_addr);
 81+ print "(server) $human_addr : $peer_port sent us $message\n" if $debug;
 82+
 83+ my $servers = $heap->{senders};
 84+ foreach my $alias ( keys %{$servers} ) {
 85+ my @channels = @{ $heap->{config}->{$alias}->{channels} };
 86+ $kernel->post( $alias => privmsg => $_ => $message ) for @channels;
 87+ }
 88+}
 89+
 90+sub irc_registered {
 91+ my ( $kernel, $heap, $sender, $irc_object ) = @_[ KERNEL, HEAP, SENDER, ARG0 ];
 92+ my $alias = $irc_object->session_alias();
 93+
 94+ print "PoCo registered\n" if $debug;
 95+ $heap->{senders}->{$alias} = $sender;
 96+
 97+ $heap->{connectors}->{$alias} = POE::Component::IRC::Plugin::Connector->new();
 98+ $irc_object->plugin_add( 'Connector' => $heap->{connectors}->{$alias} );
 99+
 100+ my %conn_hash = (
 101+ server => $alias,
 102+ port => $heap->{config}->{$alias}->{port},
 103+ );
 104+
 105+ $kernel->post( $sender, 'connect', \%conn_hash );
 106+ undef;
 107+}
 108+
 109+sub irc_001 {
 110+ my ( $kernel, $heap, $sender ) = @_[ KERNEL, HEAP, SENDER ];
 111+
 112+ my $poco_object = $sender->get_heap();
 113+ print "Connected to ", $poco_object->server_name(), "\n" if $debug;
 114+
 115+ my $alias = $poco_object->session_alias();
 116+ my @channels = @{ $heap->{config}->{$alias}->{channels} };
 117+ $kernel->post( $sender => join => $_ ) for @channels;
 118+
 119+ undef;
 120+}
 121+
 122+sub _default {
 123+ my ( $event, $args ) = @_[ ARG0 .. $#_ ];
 124+ my @output = ("$event: ");
 125+
 126+ return unless $debug;
 127+ foreach my $arg (@$args) {
 128+ if ( ref($arg) eq 'ARRAY' ) {
 129+ push( @output, "[" . join( " ,", @$arg ) . "]" );
 130+ } else {
 131+ push( @output, "'$arg'" );
 132+ }
 133+ }
 134+ print STDOUT join ' ', @output, "\n";
 135+ return 0;
 136+}
 137+

Comments

#Comment by Nikerabbit (talk | contribs)   07:26, 16 December 2011

Finally :)

Status & tagging log