#!/usr/bin/perl

name: MegaUpload
short: MU
uri: qr{mega(upload|porn|rotic)\.com/(../)?\?d=}
status: OK 2009-08-24

pre:
	use Image::Magick;

	my $mu_font_db = $main::data_path . "/data/mu_font_db.png";
	die "Font DB '$mu_font_db' does not exist\n" unless -r $mu_font_db;

start:
	( my $uri = $-{_uri} ) =~ s/megarotic/megaporn/;
	GET( $uri );
stage_first:
	$-{first_page} = $-{_referer};

	ERROR( "file not found" ) if
		/The file you are trying to access is temporarily unavailable/
			or /Unfortunately, the link you have clicked is not available/
			or /This file has expired due to inactivity/;
	m{<TD valign="middle" align="left" .*?width="500">(.*?)</TD>}s;
	my @f = map m#<font.*?>\s*(.*?)\s*</font>#g, $1;
	my $name = $f[1];
	$name =~ s/(.*)\.(\..{1,5})$/$1\0$2/; # mark truncated name
	INFO( iname => $name, asize => $f[5] );

	if ( /The file you're trying to download is password protected/ ) {
		ERROR( "password required" ) unless exists $-{_opts}->{mu_pass};

		GET_NEXT( stage_last, "",
			post => { filepassword => $-{_opts}->{mu_pass} } );
	}

	SEARCH(
		captcha_img => qr#<img src="(http://.*/gencap\.php\?[0-9a-f]+\.gif)"#,
		s2icode => qr#<INPUT type="hidden" name="captchacode" value="(.*?)"#,
		s2mevagar => qr#<INPUT type="hidden" name="megavar" value="(.*?)"#,
	);

	GET( $-{captcha_img} );

	my $captcha = captcha( \$_ );
	RESTART( 10, "Can't read captcha" ) unless defined $captcha;

	$-{_referer} = $-{first_page};
	GET( "", post => {
		captchacode => $-{s2icode},
		megavar => $-{s2mevagar},
		captcha => $captcha
	} );

stage_last:
	if ( /id="captchaform"/ ) {
		$self->print( "invalid captcha" );
		return $self->stage_first();
	}
	ERROR( "invalid password" )
		if /The file you're trying to download is password protected/;

	! /<a href="(.*?)".*IMG SRC=".*?but_dnld_regular.gif/;
	$-{file_uri} = $1;

	! /count=([0-9]+);/;
	WAIT( $1, "starting download" );

	DOWNLOAD( $-{file_uri} );


perl:
	
my %size = (
	A => 28, B => 22, C => 21, D => 27, E => 16,
	F => 16, G => 26, H => 26, K => 20, M => 38,
	N => 28, P => 21, Q => 30, R => 22, S => 18,
	T => 19, U => 26, V => 22, W => 40, X => 23,
	Y => 18, Z => 18
);

my @db;

sub read_db()
{
	my $dbf = new Image::Magick;
	$dbf->Read( $mu_font_db );
	foreach my $pos ( 0..3 ) {
		my @list = sort keys %size;
		@list = (1..9) if $pos == 3;

		my $height = 32;
		my $width = 40;
		my $left = $width * $pos;
		$width = 22 if $pos == 3;
		my $top = 0;
	
		my %db;
		foreach my $char ( @list ) {
			my $db = $dbf->Clone();
			$db->Crop( width => $width, height => $height, x => $left, y => $top );
			$db{$char} = $db;
			$top += 32;
		}
		push @db, \%db;
	}
}

sub get_char
{
	my ($src, $db, $width, $x) = @_;

	my $img = $src->Clone();
	$img->Crop( width => $width, height => 32, x => $x, y => 0 );
	$img->Extent( width => $width, height => 32, x => 0, y => 0 );

	my $min = 1;
	my $min_char = undef;
	foreach my $n ( keys %$db ) {
		my $x = $img->Compare( image => $db->{$n} );
		my ($e, $em) = $img->Get( 'error', 'mean-error' );
		if ( $em < $min ) {
			$min = $em;
			$min_char = $n;
		}
	}
	return $min_char;
}

sub captcha
{
	my $data_ref = shift;

	read_db() unless @db;

	open IMAGE, '>', '.captcha.gif';
	print IMAGE $$data_ref;
	close IMAGE;

	my $img = new Image::Magick;
	my $x = $img->Read( '.captcha.gif' );
	unlink '.captcha.gif';
	return if length $x;

	my ($width, $height) = $img->Get( 'columns', 'rows' );

	my $bg = new Image::Magick;
	$bg->Set( size => $width."x32" );
	$bg->Read( "xc:white" );
	$bg->Composite( image => $img );

	my @cap;
	push @cap, get_char( $bg, $db[0], 40, 0 );
	push @cap, get_char( $bg, $db[1], 40, $size{$cap[0]} - 6 );
	push @cap, get_char( $bg, $db[2], 40, $width - 56 );
	push @cap, get_char( $bg, $db[3], 22, $width - 22 );

	return join "", @cap;
}

# vim:ts=4:sw=4
