/************************************
Web Command Console
Copyright (C) 2005  Brian Murray
************************************/

//var Commands = Array();

function go(command) {
this.main = function main(command) {
	if (command[1] && !command[2]) {
		var path = command[1].split('/');
		if (path[0] == "" && path[1] == "") {
			this.print("Poorly formatted URI\n");
			return 0;
		}
		if (! /^[a-zA-Z]*:$/.test(path[0])) {
			var temp = "http://";
			for (var i in path) temp += path[i] + "/";
			temp = temp.substr(0, temp.length - 1);
			this.loadURI(temp);
		} else {
			this.loadURI(command[1]);
		}
		return 1;
	} else { 
		this.print("Syntax: go <url>\n");
		return 0;
	}
}
this.exit = true;
}

function ps(command) {
this.main = function main(command) {
	var processes = this.psaux();
	this.printCodes("<table class='stdterm'><tr><th>PID</th><th>PPID</th><th>tty</th><th>Started</th><th>Command</th></tr>");
	for (var i = 0; i < processes.length; i++) {
		this.printCodes("<tr><td>");
		this.print("" + processes[i].pid);
		this.printCodes("</td><td>");
		this.print("" + processes[i].ppid);
		this.printCodes("</td><td>");
		this.print("" + processes[i].tty);
		this.printCodes("</td><td>");
		this.print("" + processes[i].date);
		this.printCodes("</td><td>");
		this.print("" + processes[i].command);
		this.printCodes("</td></tr>");
	}
	this.printCodes("</table>\n");

}
this.exit = true;
}
function google(command) {
this.main = function main(command) {
	if (!command[1]) {
		this.print("Syntax: google <keywords ...>\n");
		return 0;
	}
	var query = command[1];
	for (i = 2; i < command.length; i++) query += "+" + command[i];
	this.loadURI("http://www.google.ca/search?hl=en&q=" + query);
	return 1;
}
this.exit = true;
}
function man(command) {
this.main = function main(command) {
        var section = "all";
        var doc = "";
        if (command[3] || !command[1]) {
                this.print("Too Few Args\n");
                return 0;
        }
        if (command[2]) {
                section = command[1];
                doc = command[2];
        } else doc = command[1];
        this.loadURI("http://man.he.net/?topic=" + doc + "&section=" + section);
        return 1;
}
this.exit = true;
}
function php(command) {
this.main = function main(command) {
        if (!command[1] || command[2]) {
                this.print("Too Few Args\n");
                return 0;
        }
        this.loadURI("http://php.net/" + command[1]);
        return 1;
}
this.exit = true;
}

function reload(command) {
this.main = function main(command) {
        if (!command[1]) {
                this.print("Too Few Args\n");
                return 0;
	}
        if (command[1] == "hard")
                window.location.reload()
	else {
		this.print("Unknown reload mode\n");
	}
        return 1;
}
this.exit = true;
}

function escaped(command) {
this.main = function main(command) {
	if (!command[2]) {
		this.print("Too Few Args\n");
		return 0;
	}
	for (var i = command[1]; i <= command[2]; i++) {
		this.print(i + " : \\&#" + i + ";\n");
	}
        return 1;
}
this.exit = true;
}

function help(command) {
this.main = function main(command) {
	this.loadURI("http://game-sat.com/mod.php?name=Console&file=doc");
        return 1;
}
this.exit = true;
}

function logout(command) {
this.main = function main(command) {
	this.loadURI("/mod.php?name=User&file=register&logout=1");
	this.loadURI("/");
        return 1;
}
this.exit = true;
}

function login(command) {
	/* OLD!!! DONT USE */
	this.ctrlKey = false;
	this.password = "";
	var me = this;
this.signal = function signal(sig) {
	if (sig == "SIGKILL") {
		me.form = null;
	}

}
this.controlCode = function controlCode(ctrl, alt) {
	me.ctrlKey = ctrl;
}
this.stdin = function stdin(c) {
	me.password += c;
}
this.editCode = function editCode(code) {
	if (code == 8) 
		me.password = me.password.substr(0, me.password.length - 1);
	else if (code == 13) {
		me.print("\n");
		me.passbox.value = me.password;
		me.form.submit();
		me.loadURI("/");
		me.exit();
	}
}
this.main = function main(command) {
	if (!command[1]) {
		this.exit();
		return 0;
	}
	me.form = document.createElement('form');
	me.form.action = "/mod.php?name=User&file=register";
	me.form.method = "post";
	me.form.target = "master";
	var uname = document.createElement('input');
	uname.name = 'user';
	uname.value = command[1];
	me.form.appendChild(uname);
	me.passbox = document.createElement('input');
	me.passbox.name = "pass";
	me.form.appendChild(me.passbox);
	me.form.target = me.getiframe().id;
	me.print("Password: ");
	me.addapp(me.form);
	return 1;
}

}

function jterm(command) {
var me = this;
this.main = function main(command) {
	if (command[1]) {
		nc = new Array();
		var j = 0;
		for (var i = 1; i < command.length; i++) {
			nc[j] = command[i];
			j++;
		}
		me.buildTTY(nc);
		return true;
	} else
		me.print("Usage: " + command[0] + " <app> <args>\n");
	return false;
}
this.exit = true;
}

