#!/usr/bin/perl

name: SecuredIn
short: L:SecuredIn
uri: qr{secured\.in/download-\d+-[0-9a-f]+\.html$}
slots: max
status: OK 2009-10-04

pre:
	use MIME::Base64;
	use Crypt::Blowfish;

	my $cipher = new Crypt::Blowfish "\0\0\0\0\0\0\0\0";
	sub cipher
	{
		my $cipher_string = shift;
		my $cipher_text = decode_base64( $cipher_string );
		my $iv = substr $cipher_text, 0, 8;

		my $decrypted = '';
		for ( my $pos = 8; $pos < length $cipher_text; $pos += 8 ) {
			my $part = substr $cipher_text, $pos, 8;
			my $d = $cipher->decrypt( $part );
			$decrypted .= $d ^ $iv;
			$iv = $part;
		}
		return $decrypted;
	}

start:
	GET( $-{_uri} );

	$-{cap_form} = $self->form( id => "frm_captcha" );
	GOTO stage_find_links unless $-{cap_form};

stage_captcha:
	my $h = $-{cap_form}->get( "captcha_hash" );
	my $img = "captcha-$h.jpg";

	GET( $img, keep_referer => 1 );

	CAPTCHA( "image/jpeg" );
	$-{cap_form}->set( captcha_key => $_ );

	GET( $-{cap_form}->post() );

	$-{cap_form} = $self->form( id => "frm_captcha" );
	GOTO stage_captcha if $-{cap_form};

stage_find_links:

	my @list;
	push @list, $1 while s/accessDownload\(\d+, \d+, '(\d+-[0-9a-f]+)', \d+\);//;

	ERROR("no links") unless @list;

	$-{list} = \@list;
	$-{outlist} = [];

stage_get_link:
	GET( "/ajax-handler.php",
		post => { cmd => "download", download_id => shift @{$-{list}} },
		keep_referer => 1 );

	my $uri = cipher( $_ );
	$uri =~ s/\s+$//;
	$uri =~ s/ /%20/g;
	push @{$-{outlist}}, $uri;

	GOTO stage_get_link if @{$-{list}};

	LINK( @{$-{outlist}} );

# vim:ts=4:sw=4
