

/************************************
  perl-generated javascript file to
  handle the javascript login prompt.
  ***********************************/

/**
 * This file is used for both the engine and the prompt pages. Some functions
 * will be multi-purpose, while others are specific to one file or the other.
 **/

var Engine = parent.engineframe;
var Prompt = parent.promptframe;

/**
 * TTY state variables. Can be used in a bitmask. For TTY_NONE, the input
 * element will neither accept text nor display what the user has typed.
 * For TTY_READ, text will be processed but it will not be echoed to the
 * terminal. For TTY_ECHO the text will be echoed to the terminal but not
 * echoed, and TTY_BOTH interprets the text and echoes it.
 **/
var TTY_NONE = 0;
var TTY_READ = 1;
var TTY_ECHO = 2;
var TTY_BOTH = 3;

// set up the variables to hold swappable information. This is specific to the
// terminal page, and will likely not be used by the engine.
var inputState = TTY_NONE;
var cmdSpool = "";
var cmdPrompt = "";
var cmdString = "";
var uname = "";
var elem;

function setPrompt(str) { Prompt.cmdPrompt = str; }
function getPrompt() { return Prompt.cmdPrompt ; }

function setState(state) { Prompt.inputState = state; }
function getState() { return Prompt.inputState; }

// initialize the element (textarea). This is called on load of the prompt page.
function init(obj) {
//	alert ("init -- "+obj);
	Engine.location.href = "index-engine.pl";
	elem = obj;
	cmdSpool = "Welcome to Fsckme.com. \n";
	cmdSpool += "Commands may be entered at the command line here. Currently " +
		"not many commands are supported, and in any case access depends " +
		"on who you log in as. Try the username 'user' and password " +
		"'pass' if you want to try stuff out. \n" +
		"You could also humour me and type 'uptime'... \n\n";
	setState();
	doRefresh();
}

// a key has been pressed. We decide here what to do with it.
function checkWhich(evt) {
	// if the TTY is not waiting for input, we exit.
	if (getState() == TTY_NONE) {
//		alert ("State is " + TTY_NONE);
		return false;
	}
	evt = (evt) ? evt : (window.event) ? window.event : "";
	if (evt) {
		var thingPressed = "";
		if (evt.which) {
			thingPressed = evt.which;
		} else {
			if (elem.type == "textarea") {
				thingPressed = evt.keyCode;
			}
		}
		// thingPressed should now be equal to a number representing the key pressed.
		cmdKey(thingPressed);
	}
	return false;
}

// Here we figure out what to do with the keystroke
function cmdKey(key) {
//	alert ("cmdKey: " + key + "; State is " + getState() + " & TTY_ECHO = " + (getState() & TTY_ECHO));
	// if the TTY is not waiting for input, we exit.
	if (getState() == TTY_NONE) return false;
	// check that the character is a printable (127 is Delete)
	if (key >= 32 && key <= 255 && key != 127 && key != 191) {
		cmdString += String.fromCharCode(key)
	} else {
		switch (key) {
			case 8 :	//backspace
				cmdString = cmdString.substr(0,cmdString.length - 1)
				break
			case 9 :
				cmdstring += String.fromCharCode(key)
				break
			case 13:	//enter key
				setState(TTY_NONE);
				Engine.doCmd(cmdString);
				//setPrompt("");
				addLine("");
				cmdString = "";
				//return;
				break;
		}
	}
	doRefresh()
}

function doRefresh() {
//	alert (getState());
	allSpool = cmdSpool + ((getState() & TTY_READ > 0)?getPrompt():"") + ((getState() & TTY_ECHO > 0)?cmdString:"");
	var arrSpool = allSpool.split("\n");
//	alert (getState());
	Prompt.elem.value = cmdSpool + (((getState() & TTY_READ) > 0)?getPrompt():"") + (((getState() & TTY_ECHO) > 0)?cmdString:"");
}

function addLine(line) {
	line += (line)?"":"";
	cmdSpool = elem.value + line + "\n"
	//doRefresh()
}

function authenticate(u,p) {
	if (u == "uname" && p == "pass") {
		uname = u
		return true
	}
	uname = ""
	return false
}
