package captchabot;
use strict;
use IO::Socket;
use MIME::Base64;
sub sock
{
my $sock;
do {
$sock = new IO::Socket::INET
(
PeerAddr => 'captchabot.com',
PeerPort => 80,
PeerProto => 'tcp',
TimeOut => 10
) or print "Can't connect\n";
} while (!$sock);
return $sock;
}
sub new
{
my $self={};
bless $self;
$self->{'url'}="http://captchabot.com/xmlrpc/axmlrpc.php";
$self->{'host'}="captchabot.com";
$self->{'Username'}="";
$self->{'Password'}="";
$self->{'id'}=0;
return $self;
}
sub body_callback
{
my ($chunk,$context)=@_;
push @{$context}, $chunk;
return length($chunk); # OK
}
sub hdr_callback
{
my ($chunk,$context)=@_;
push @{$context}, $chunk;
return length($chunk); # OK
}
sub Username
{
my $self=shift;
my ($Username)=@_;
$self->{'Username'}=$Username;
}
sub Password
{
my $self=shift;
my ($Password)=@_;
$self->{'Password'}=$Password;
}
sub Recognize
{
my $self=shift;
my ($file, $language)=@_;
die "Unable to open file $file: $!" unless open(IN, "$file");
my $contents="";
while (<IN>) {$contents.=$_;}
close(IN);
my $converted=encode_base64($contents);
my $request="<methodCall><methodName>ocr_server::analyze</methodName><params>";
$request.="<param><base64>$converted</base64></param>";
$request.="<param><string>$self->{'Username'}</string></param>";
$request.="<param><string>$self->{'Password'}</string></param>";
$request.="<param><int>".$language."</int></param>";
$request.="</params></methodCall>";
my $header=
"POST $self->{'url'} HTTP/1.1\n".
"Host: $self->{'host'}\n".
"MIME-Version: 1.0".
"Content-type: multipart/mixed; boundary=----doc\n".
"Accept: text/xml\n".
"Cache-Control: no-cache\n".
"Connection: close\n".
"Content-length: ".length($request)."\n\n".$request;
my @body;
my @hdr;
my $sock = sock();
print $sock $header;
read($sock,my $answ,10000);
$_=$answ;
if (/\<int\>(\d+)\<\/int\>/)
{
$self->{'id'}=$1;
} else
{
print "Unable to find ID\n";
}
my $ret=undef;
if (/\<string\>([\w| ]+)\<\/string\>/)
{
$ret = $1;
}
else
{
print "Unable to find CAPTCHA\n";
}
undef $sock;
return $ret;
}
sub Report
{
my $self=shift;
my ($result)=@_;
my $request="<methodCall><methodName>ocr_server::ver</methodName><params><param><string>";
$request.=($result)?"yes":"no";
$request.="</string></param><param><int>$self->{'id'}</int></param></params></methodCall>";
my $header=
"POST $self->{'url'} HTTP/1.1\n".
"Host: $self->{'host'}\n".
"MIME-Version: 1.0".
"Content-type: multipart/mixed; boundary=----doc\n".
"Accept: text/xml\n".
"Cache-Control: no-cache\n".
"Connection: close\n".
"Content-length: ".length($request)."\n\n".$request;
my @body;
my @hdr;
my $sock = sock();
print $sock $header;
read($sock,my $answ,10000);
undef $sock;
return 1;
}
1;