1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
function escapeHTML( html ) {
var trans = {
'&': '&',
'<': '<',
};
return (html + '').replace(/[&<]/g, function(c) { return trans[c]; });
}
function handlekeys(field, state, e)
{
var keycode;
if (window.event) {
keycode = window.event.keyCode;
} else if (e) {
keycode = e.which;
} else {
return true;
}
if (keycode == 13) {
state.handle('SEND');
return false;
} else {
return true;
}
}
function display(text) {
var div = document.createElement("div");
div.innerHTML = escapeHTML(text);
var cl = document.getElementById("terminal");
cl.appendChild(div);
div.scrollIntoView();
}
function change_prompt() {
var p = document.getElementById("prompt")
p.scrollIntoView();
p.focus();
}
function clear_prompt() {
change_prompt();
var prompt = document.getElementById("prompt");
prompt.value = "";
}
function prompt(text) {
clear_prompt();
display(escapeHTML(text));
change_prompt();
}
state = new FSM({
start: function(fsm, event) {
display("Connecting to the Mongrel2 BBS....");
fsm.trans('connecting');
BBS.init(fsm);
},
connecting: {
CONNECT: function(fsm, event) {
display("Connected to Mongrel2 BBS.");
BBS.send("connect");
fsm.trans('connected');
},
},
connected: {
PROMPT: function(fsm, event) {
prompt(event.msg);
fsm.last_pchar = escapeHTML(event.pchar);
},
EXIT: function(fsm, event) {
clear_prompt();
display(event.msg);
},
SCREEN: function(fsm, event) {
display(event.msg);
},
CLOSE: function(fsm, event) {
addMessage('<em>Disconnected, will reconnect...</em>');
fsm.trans('connecting');
},
SEND: function(fsm, event) {
var input = document.getElementById("prompt");
BBS.send(input.value);
display(fsm.last_pchar + input.value);
clear_prompt();
}
}
});
|