r106981 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106980‎ | r106981 | r106982 >
Date:21:28, 21 December 2011
Author:overlordq
Status:deferred (Comments)
Tags:tools 
Comment:
Rename original file to make room for a tcp-based version, in the remote^Cventuality mediawiki uses tcp transmission.
Modified paths:
  • /trunk/tools/rcbot/rcbot-tcp.pl (added) (history)
  • /trunk/tools/rcbot/rcbot-udp.pl (added) (history)
  • /trunk/tools/rcbot/rcbot.pl (deleted) (history)

Diff [purge]

Index: trunk/tools/rcbot/rcbot.pl
@@ -1,136 +0,0 @@
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 -
Index: trunk/tools/rcbot/rcbot-tcp.pl
@@ -0,0 +1,133 @@
 2+#!/usr/bin/perl
 3+
 4+use strict;
 5+use warnings;
 6+use POE qw(Component::IRC Component::IRC::Plugin::Connector Component::Server::TCP);
 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 $port = 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::Component::Server::TCP->new(
 34+ Alias => 'tcpserver',
 35+ Port => $port,
 36+ ClientInput => \&server_read,
 37+);
 38+
 39+POE::Session->create(
 40+ package_states => [
 41+ 'main' => [
 42+ qw(
 43+ irc_start
 44+ irc_registered
 45+ irc_001
 46+ _default )
 47+ ],
 48+ ],
 49+ inline_states => {
 50+ _start => \&server_start,
 51+ },
 52+ heap => { config => $settings },
 53+);
 54+
 55+POE::Kernel->run();
 56+exit;
 57+
 58+sub irc_start {
 59+ my ( $kernel, $session ) = @_[ KERNEL, SESSION ];
 60+ $kernel->signal( $kernel, 'POCOIRC_REGISTER', $session->ID(), 'all' );
 61+ undef;
 62+}
 63+
 64+sub server_start {
 65+ my ( $kernel, $session ) = $_[KERNEL];
 66+
 67+ $kernel->yield('irc_start');
 68+ undef;
 69+}
 70+
 71+sub server_read {
 72+ my ( $kernel, $session, $heap, $message ) = @_[ KERNEL, SESSION, HEAP, ARG0 ];
 73+
 74+ my $remote_address = $heap->{remote_ip};
 75+ return unless defined $remote_address;
 76+
 77+ my $peer_port = $heap->{remote_port};
 78+
 79+ print "(server) $remote_address : $peer_port sent us $message\n" if $debug;
 80+
 81+ foreach my $alias ( keys %{$settings} ) {
 82+ my @channels = @{ $settings->{$alias}->{channels} };
 83+ $kernel->post( $alias => privmsg => $_ => $message ) for @channels;
 84+ }
 85+}
 86+
 87+sub irc_registered {
 88+ my ( $kernel, $heap, $sender, $irc_object ) = @_[ KERNEL, HEAP, SENDER, ARG0 ];
 89+ my $alias = $irc_object->session_alias();
 90+
 91+ print "PoCo registered\n" if $debug;
 92+ $heap->{senders}->{$alias} = $sender;
 93+
 94+ $heap->{connectors}->{$alias} = POE::Component::IRC::Plugin::Connector->new();
 95+ $irc_object->plugin_add( 'Connector' => $heap->{connectors}->{$alias} );
 96+
 97+ my %conn_hash = (
 98+ server => $alias,
 99+ port => $heap->{config}->{$alias}->{port},
 100+ );
 101+
 102+ $kernel->post( $sender, 'connect', \%conn_hash );
 103+ undef;
 104+}
 105+
 106+sub irc_001 {
 107+ my ( $kernel, $heap, $sender ) = @_[ KERNEL, HEAP, SENDER ];
 108+
 109+ my $poco_object = $sender->get_heap();
 110+ print "Connected to ", $poco_object->server_name(), "\n" if $debug;
 111+
 112+ my $alias = $poco_object->session_alias();
 113+ my @channels = @{ $heap->{config}->{$alias}->{channels} };
 114+ $kernel->post( $sender => join => $_ ) for @channels;
 115+
 116+ undef;
 117+}
 118+
 119+sub _default {
 120+ my ( $event, $args ) = @_[ ARG0 .. $#_ ];
 121+ my @output = ("$event: ");
 122+
 123+ return unless $debug;
 124+ foreach my $arg (@$args) {
 125+ if ( ref($arg) eq 'ARRAY' ) {
 126+ push( @output, "[" . join( " ,", @$arg ) . "]" );
 127+ } else {
 128+ push( @output, "'$arg'" );
 129+ }
 130+ }
 131+ print STDOUT join ' ', @output, "\n";
 132+ return 0;
 133+}
 134+
Index: trunk/tools/rcbot/rcbot-udp.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:13, 22 December 2011

What's the advantage of tcp version?

#Comment by OverlordQ (talk | contribs)   00:24, 23 December 2011

I vaugely remember some conversation in -tech(?) about how using UDP for the rcfeed is silly since you'll lose packets, and if somebody found a way to overload the network backend the rcfeed would horribly explode and all the anti-vandal patrolling would grind to a halt. So for giggles I made a TCP listener.

#Comment by Tim Starling (talk | contribs)   06:37, 30 December 2011

Yes, I said that.

Status & tagging log