#!/bin/perl

# Submit an eDonkey download request to mldonkey
#
# Argument(s): An ed2k URI of the form:
#
# ed2k://|file|<filename>|<filesize>|<MD4-sum|
use LWP::UserAgent;

($#ARGV >= 0) || die "Usage: mldonkey_submit <ed2kURI> ...
";

$vars{'HTTPURL'} = "http://192.168.0.11:4080";
$vars{'HTTPUSER'} = "admin";
$vars{'HTTPPASS'} = "";

my $ua = LWP::UserAgent->new;

while (my $uri = shift @ARGV) {
	$_ = URI::Escape::uri_unescape($uri);
	if (/^ed2k:\/\/\|file\|[^|]+\|(\d+)\|([\dabcdef]+)\|$/) {
		my $size = $1;
		my $md4 = $2;
		my $req = HTTP::Request->new(
			GET => "$vars{'HTTPURL'}/submit?q=dllink+$uri"
		);
		if (($vars{'HTTPUSER'}) && ($vars{'HTTPPASS'})) {
			$req->authorization_basic($vars{'HTTPUSER'},
				$vars{'HTTPPASS'});
		}
		my $response = $ua->request($req);
		if (!($response->is_success)) {
			print $response->error_as_HTML;
			exit 1;
		}
	} else {
		print "Not an ed2k URI: $_
";
	}
}
