#!/usr/bin/perl
use lib '/usr/lib/perl5/site_perl/5.8.3';

use Crypt::CBC;
use MIME::Base64;
use CGI ':standard';

my $key = 'pqrdqqdesrstdsds';
# IV must be exactly 16 bytes long
my $IV = pack "H32", 0;
my $cipher = Crypt::CBC->new({'key' => $key,
			      'cipher' => 'Anubis',
			      'iv' => $IV,
			      'regenerate_key' => 1,
			      'padding' => 'standard',
			      'prepend_iv' => 0
			      });

print header(),
    start_html(-title=>'CAPTCHA',
	       -author=>'mario.storti@gmail.com'),
    h1('Protecting e-mail addresses with CAPTCHA'),
    a({-href=>"/twiki/bin/view/Cimec/CaptchaScript"},"Want to learn more? "
      ."What is CAPTCHA?"),p,
    " The solicited e-mail address is being converted to a blurred bitmap. ",
    "This process avoid spambots to grab email addresses from web pages for",
    " sending spam. ", b("This process may take a few seconds, please wait...");

my $crypto_mail = param('crypto_mail');
$decoded = decode_base64($crypto_mail);
$plaintext  = $cipher->decrypt($decoded);
$plaintext =~ s/_/\\_/g;

open LOG,">>/tmp/deco.log";
printf LOG "%s $plaintext\n",scalar localtime(time);
close LOG;

my $pid = $$;
open TEX,">tex$pid.tex";
/`/;
print TEX <<EOM;
\\documentclass{article}
\\usepackage{amsmath}
\\pagestyle{empty} 
\\begin{document}
{\\boxed{\\text{ \\Huge\\bfseries\\ttfamily 
      \\fontsize{50}{20} $plaintext}}}
\\end{document}
EOM
/`/;
close TEX;
system "/bin/bash deco.sh tex$pid &> ./deco-sh.log";
# system "/bin/bash deco.sh tex$pid";

print hr,b("Done. The e-mail address is:"),p,
img({src=>"/~mstorti/captcha/jpeg/tex$pid.jpeg"}),end_html;