// ======================================================================
// SABNZBD LIBRARY
// Contains functions for working with a remote SABnzbd+ installation.
// ======================================================================

var sab_data = {
	'user':   '',
	'pass':   '',
	'host':   '',
	'port':   0,
	'apikey': ''
};

function sab_init(host, port, user, pass, apikey) {
	sab_data['host'] = host;
	sab_data['port'] = port;
	sab_data['user'] = user;
	sab_data['pass'] = pass;
	sab_data['apikey'] = apikey;
}

function sab_api(request) {
	var apicall =
		"/sabnzbd/api?output=json&apikey="+sab_data['apikey']+"&"+
		request
	;
	var result = http_getAuth(
		sab_data['host'], sab_data['port'], apicall,
		sab_data['user'], sab_data['pass']
	);
	
	if (result.length && result.charAt(0) === '{') {
		return eval(result);
	} else {
		mudc_console("WARNING: Non-JSON response from SABnzbd server");
		return false;
	}
}

function sab_history() {
	return sab_api("mode=history&start=0&limit=100");
}

function sab_queue() {
	return sab_api("mode=queue");
}

function sab_addByUrl(url, category) {
	var request = "mode=addurl";
	if (category.length) request += "&category="+category;
	sab_api(request + "&name=" + http_escape(url));
	return true;
}

