package captchabot use strict use IO Socket use MIME Base64 sub sock m

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;