2010-12-23 22:39:54 +01:00
|
|
|
/*****************************************************************
|
|
|
|
* *
|
|
|
|
* blash - An AJAX CMS for browsing your web site like a shell *
|
|
|
|
* *
|
|
|
|
* by BlackLight <blacklight@autistici.org>, (C) 2010 *
|
|
|
|
* Web: http://0x00.ath.cx *
|
|
|
|
* Released under GPL licence v.3 *
|
|
|
|
* *
|
|
|
|
*****************************************************************/
|
|
|
|
|
2010-12-21 22:47:39 +01:00
|
|
|
var shell = null;
|
|
|
|
|
|
|
|
function blash ()
|
|
|
|
{
|
|
|
|
/************ ATTRIBUTES **************/
|
|
|
|
/** Object containing the parsed JSON configuration object */
|
|
|
|
this.json = {};
|
|
|
|
|
|
|
|
/** Shell window object */
|
|
|
|
this.window = document.getElementById ( "blashWindow" );
|
|
|
|
|
2010-12-22 02:28:45 +01:00
|
|
|
/** Array containing the codes of the shell commands */
|
|
|
|
this.commands = new Array();
|
|
|
|
|
2010-12-21 22:47:39 +01:00
|
|
|
/** Escape sequences to be parsed in the prompt text */
|
|
|
|
this.promptSequences = new Array();
|
|
|
|
|
|
|
|
/** Array containing the history of given commands */
|
|
|
|
this.history = new Array();
|
|
|
|
|
|
|
|
/** Index to the current history element */
|
|
|
|
this.history_index = -1;
|
|
|
|
|
|
|
|
/** Current path */
|
|
|
|
this.path = '/';
|
|
|
|
|
|
|
|
/** Element containing the output of the command */
|
|
|
|
this.cmdOut = document.getElementById ( "blashCmdOut" );
|
|
|
|
|
|
|
|
/** Element containing the text of the prompt */
|
|
|
|
this.promptText = document.getElementById ( "promptText" );
|
|
|
|
|
|
|
|
/** Input field used as prompt */
|
|
|
|
this.prompt = document.getElementsByName ( "blashPrompt" )[0];
|
|
|
|
|
|
|
|
/** Counter of the open <span> tags when replacing the colours in the command prompt */
|
|
|
|
this.__open_spans = 0;
|
2010-12-23 15:45:53 +01:00
|
|
|
|
|
|
|
/** Check if this is the first command given in this session (for fixing <br/> stuff) */
|
|
|
|
this.__first_cmd = true;
|
2010-12-21 22:47:39 +01:00
|
|
|
/**************************************/
|
|
|
|
|
2010-12-22 02:28:45 +01:00
|
|
|
this.loadCommand = function ( cmd )
|
|
|
|
{
|
|
|
|
var cmd_file = window.location.href;
|
|
|
|
cmd_file = cmd_file.replace ( /\/([a-zA-Z\.]+)$/, '/commands/' + cmd + ".json" );
|
|
|
|
|
|
|
|
var http = new XMLHttpRequest();
|
|
|
|
http.open ( "GET", cmd_file, true );
|
|
|
|
|
|
|
|
http.onreadystatechange = function ()
|
|
|
|
{
|
|
|
|
if ( http.readyState == 4 && http.status == 200 )
|
|
|
|
{
|
|
|
|
shell.commands.push ( eval ( '(' + http.responseText + ')' ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
http.send ( null );
|
|
|
|
}
|
|
|
|
|
2010-12-21 22:47:39 +01:00
|
|
|
this.prompt.focus();
|
|
|
|
|
|
|
|
var json_config = window.location.href;
|
|
|
|
json_config = json_config.replace ( /\/([a-zA-Z\.]+)$/, '/blash.json' );
|
|
|
|
|
|
|
|
var http = new XMLHttpRequest();
|
|
|
|
http.open ( "GET", json_config, true );
|
|
|
|
|
|
|
|
http.onreadystatechange = function ()
|
|
|
|
{
|
|
|
|
if ( http.readyState == 4 && http.status == 200 )
|
|
|
|
{
|
|
|
|
shell.json = eval ( '(' + http.responseText + ')' );
|
|
|
|
|
|
|
|
shell.promptText.innerHTML = ( shell.json.promptText ) ? shell.json.promptText : "[%n@%m %W] $ ";
|
|
|
|
shell.promptText.innerHTML = shell.unescapePrompt ( promptText.innerHTML, shell.json.promptSequences );
|
|
|
|
|
|
|
|
if ( shell.json.banner.length > 0 )
|
|
|
|
{
|
|
|
|
var banner = document.createElement ( "span" );
|
|
|
|
banner.setAttribute ( "id", "banner" );
|
|
|
|
banner.innerHTML = shell.json.banner;
|
|
|
|
shell.window.insertBefore ( banner, shell.promptText );
|
|
|
|
}
|
2010-12-22 02:28:45 +01:00
|
|
|
|
|
|
|
for ( var i in shell.json.commands )
|
|
|
|
{
|
|
|
|
shell.loadCommand ( shell.json.commands[i] );
|
|
|
|
}
|
2010-12-21 22:47:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
http.send ( null );
|
2010-12-22 02:28:45 +01:00
|
|
|
|
2010-12-21 22:47:39 +01:00
|
|
|
this.getKey = function ( e )
|
|
|
|
{
|
|
|
|
var evt = ( window.event ) ? window.event : e;
|
|
|
|
var key = ( evt.charCode ) ? evt.charCode : evt.keyCode;
|
|
|
|
|
|
|
|
if ( key == 13 || key == 10 )
|
|
|
|
{
|
|
|
|
if ( this.prompt.value.length != 0 )
|
|
|
|
{
|
|
|
|
this.prompt.value.match ( /^([^\s]+)\s*(.*)$/ );
|
|
|
|
var cmd = RegExp.$1;
|
|
|
|
var arg = RegExp.$2;
|
|
|
|
var cmd_found = false;
|
|
|
|
this.history.push ( this.prompt.value );
|
|
|
|
this.history_index = -1;
|
|
|
|
|
2010-12-22 02:28:45 +01:00
|
|
|
for ( i=0; i < this.commands.length && !cmd_found; i++ )
|
2010-12-21 22:47:39 +01:00
|
|
|
{
|
2010-12-22 02:28:45 +01:00
|
|
|
if ( this.commands[i].name == cmd )
|
2010-12-21 22:47:39 +01:00
|
|
|
{
|
|
|
|
cmd_found = true;
|
2010-12-22 02:28:45 +01:00
|
|
|
var out = this.commands[i].action ( arg );
|
2010-12-21 22:47:39 +01:00
|
|
|
|
|
|
|
if ( out.length > 0 )
|
|
|
|
{
|
|
|
|
this.cmdOut.innerHTML = out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !cmd_found )
|
|
|
|
{
|
|
|
|
this.cmdOut.innerHTML = this.json.shellName + ": command not found: " + cmd + '<br/>';
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = this.prompt.value;
|
|
|
|
var out = this.cmdOut.innerHTML;
|
|
|
|
|
2010-12-23 01:28:38 +01:00
|
|
|
var text = ( shell.json.promptText ) ? shell.json.promptText : "[%n@%m %W] $ ";
|
|
|
|
text = shell.unescapePrompt ( text, shell.json.promptSequences );
|
|
|
|
|
2010-12-21 22:47:39 +01:00
|
|
|
this.window.removeChild ( this.prompt );
|
|
|
|
this.window.removeChild ( this.cmdOut );
|
2010-12-23 15:45:53 +01:00
|
|
|
|
|
|
|
if ( this.__first_cmd )
|
|
|
|
{
|
|
|
|
this.window.innerHTML += value + '<br/>' + out + text;
|
|
|
|
this.__first_cmd = false;
|
|
|
|
} else {
|
|
|
|
if ( out )
|
|
|
|
{
|
|
|
|
if ( out.match ( /^\s*<br.?>\s*/ ))
|
|
|
|
{
|
|
|
|
out = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.window.innerHTML += value + '<br/>' + out + text;
|
|
|
|
}
|
2010-12-21 22:47:39 +01:00
|
|
|
|
|
|
|
this.prompt = document.createElement ( 'input' );
|
|
|
|
this.prompt.setAttribute ( 'name', 'blashPrompt' );
|
|
|
|
this.prompt.setAttribute ( 'type', 'text' );
|
|
|
|
this.prompt.setAttribute ( 'class', 'promptInput' );
|
|
|
|
this.prompt.setAttribute ( 'autocomplete', 'off' );
|
2010-12-23 01:28:38 +01:00
|
|
|
this.prompt.setAttribute ( 'onkeydown', 'shell.getKey ( event )' );
|
|
|
|
this.prompt.setAttribute ( 'onkeyup', 'this.focus()' );
|
2010-12-23 18:10:50 +01:00
|
|
|
this.prompt.setAttribute ( 'onblur', 'return false' );
|
2010-12-21 22:47:39 +01:00
|
|
|
|
|
|
|
this.cmdOut = document.createElement ( 'div' );
|
|
|
|
this.cmdOut.setAttribute ( 'id', 'blashCmdOut' );
|
|
|
|
this.cmdOut.setAttribute ( 'class', 'blashCmdOut' );
|
|
|
|
this.cmdOut.innerHTML = '<br/>';
|
|
|
|
|
|
|
|
this.window.appendChild ( this.prompt );
|
|
|
|
this.window.appendChild ( this.cmdOut );
|
|
|
|
this.prompt.focus();
|
|
|
|
}
|
|
|
|
} else if ( key == 38 || key == 40 ) {
|
|
|
|
if ( key == 38 )
|
|
|
|
{
|
|
|
|
if ( this.history_index < 0 )
|
|
|
|
{
|
|
|
|
this.history_index = this.history.length - 1;
|
|
|
|
this.prompt.value = this.history[ this.history_index ];
|
|
|
|
} else if ( this.history_index == 0 ) {
|
|
|
|
// We're already on the first history element
|
|
|
|
} else {
|
|
|
|
this.history_index--;
|
|
|
|
this.prompt.value = this.history[ this.history_index ];
|
|
|
|
}
|
|
|
|
} else if ( key == 40 ) {
|
|
|
|
if ( this.history_index < 0 )
|
|
|
|
{
|
|
|
|
// We're already on the last element, don't do anything
|
|
|
|
} else if ( this.history_index == this.history.length - 1 ) {
|
|
|
|
this.prompt.value = '';
|
|
|
|
} else {
|
|
|
|
this.history_index++;
|
|
|
|
this.prompt.value = this.history[ this.history_index ];
|
2010-12-21 23:22:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prompt.focus();
|
|
|
|
} else if ( key == 9 ) {
|
2010-12-23 01:28:38 +01:00
|
|
|
this.prompt.focus();
|
|
|
|
|
2010-12-22 00:43:46 +01:00
|
|
|
if ( this.prompt.value.match ( /\s(.*)$/ ))
|
2010-12-21 23:22:22 +01:00
|
|
|
{
|
2010-12-22 00:43:46 +01:00
|
|
|
var arg = RegExp.$1;
|
|
|
|
var path = arg;
|
|
|
|
var dirs = new Array();
|
2010-12-21 23:22:22 +01:00
|
|
|
|
2010-12-22 00:43:46 +01:00
|
|
|
for ( var i in this.json.directories )
|
|
|
|
{
|
|
|
|
if ( arg.match ( /^[^\/]/ ) )
|
|
|
|
{
|
|
|
|
path = this.path + '/' + arg;
|
|
|
|
path = path.replace ( /\/+/g, '/' );
|
|
|
|
}
|
|
|
|
|
|
|
|
var re = new RegExp ( '^' + path + '[^/]*$' );
|
|
|
|
|
|
|
|
if ( this.json.directories[i].path.match ( re ))
|
|
|
|
{
|
|
|
|
dirs.push ({
|
|
|
|
'name' : this.json.directories[i].path,
|
|
|
|
'type' : this.json.directories[i].type,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-23 14:25:43 +01:00
|
|
|
if ( dirs.length == 1 ) {
|
2010-12-22 00:43:46 +01:00
|
|
|
this.prompt.value = this.prompt.value.replace ( arg, dirs[0].name + (( dirs[0].type == 'directory' ) ? '/' : '' ));
|
|
|
|
} else {
|
|
|
|
this.cmdOut.innerHTML = '';
|
|
|
|
|
|
|
|
for ( var i in dirs )
|
|
|
|
{
|
|
|
|
this.cmdOut.innerHTML += "<br/>\n" + dirs[i].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-12-21 23:22:22 +01:00
|
|
|
var cmds = new Array();
|
|
|
|
|
2010-12-22 02:28:45 +01:00
|
|
|
for ( var i in this.commands )
|
2010-12-21 23:22:22 +01:00
|
|
|
{
|
|
|
|
var re = new RegExp ( '^' + this.prompt.value );
|
|
|
|
|
2010-12-22 02:28:45 +01:00
|
|
|
if ( this.commands[i].name.match ( re ))
|
2010-12-21 23:22:22 +01:00
|
|
|
{
|
2010-12-22 02:28:45 +01:00
|
|
|
cmds.push ( this.commands[i].name );
|
2010-12-21 23:22:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( cmds.length == 0 )
|
|
|
|
{
|
|
|
|
this.cmdOut.innerHTML = '<br/>Sorry, no matches for `' + this.prompt.value + "'";
|
|
|
|
} else if ( cmds.length == 1 ) {
|
|
|
|
this.prompt.value = cmds[0] + ' ';
|
|
|
|
} else {
|
|
|
|
this.cmdOut.innerHTML = '';
|
|
|
|
|
|
|
|
for ( var i in cmds )
|
|
|
|
{
|
2010-12-22 00:43:46 +01:00
|
|
|
this.cmdOut.innerHTML += "<br/>\n" + cmds[i];
|
2010-12-21 23:22:22 +01:00
|
|
|
}
|
2010-12-21 22:47:39 +01:00
|
|
|
}
|
|
|
|
}
|
2010-12-23 01:28:38 +01:00
|
|
|
|
|
|
|
this.prompt.focus();
|
2010-12-23 18:10:50 +01:00
|
|
|
setTimeout ( function() { shell.prompt.focus(); }, 1 );
|
|
|
|
|
|
|
|
if ( this.prompt.setSelectionRange )
|
|
|
|
{
|
|
|
|
this.prompt.setSelectionRange ( this.prompt.value.length, this.prompt.value.length );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-12-21 22:47:39 +01:00
|
|
|
}
|
2010-12-22 00:43:46 +01:00
|
|
|
|
|
|
|
this.prompt.focus();
|
2010-12-21 22:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.unescapePrompt = function ( prompt, sequences )
|
|
|
|
{
|
|
|
|
var re = new RegExp ( "([^\]?)#\{([0-9]+)\}" );
|
|
|
|
|
|
|
|
while ( prompt.match ( re ))
|
|
|
|
{
|
|
|
|
if ( this.__open_spans > 0 )
|
|
|
|
{
|
|
|
|
prompt = prompt.replace ( re, RegExp.$1 + "</span><span style=\"color: #" + RegExp.$2 + "\">" );
|
|
|
|
} else {
|
|
|
|
prompt = prompt.replace ( re, RegExp.$1 + "<span style=\"color: #" + RegExp.$2 + "\">" );
|
|
|
|
this.__open_spans++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.__open_spans > 0 )
|
|
|
|
{
|
|
|
|
prompt = prompt + "</span>";
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i=0; i < sequences.length; i++ )
|
|
|
|
{
|
|
|
|
prompt = this.unescapePromptSequence ( prompt, sequences[i].sequence, sequences[i].text(), sequences[i].default_text );
|
|
|
|
}
|
|
|
|
|
|
|
|
return prompt;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.unescapePromptSequence = function ( prompt, sequence, text, default_text )
|
|
|
|
{
|
|
|
|
var re = new RegExp ( "([^\]?)" + sequence, "g" );
|
2010-12-23 01:28:38 +01:00
|
|
|
prompt.replace ( /%W/g, this.path );
|
2010-12-21 22:47:39 +01:00
|
|
|
|
|
|
|
if ( prompt.match ( re ))
|
|
|
|
{
|
|
|
|
prompt = prompt.replace ( re, (( text ) ? RegExp.$1 + text : RegExp.$1 + default_text ));
|
|
|
|
}
|
|
|
|
|
|
|
|
return prompt;
|
|
|
|
}
|
2010-12-23 01:28:38 +01:00
|
|
|
|
|
|
|
this.expandPath = function ( arg )
|
|
|
|
{
|
|
|
|
if ( arg.match ( /^[^\/]/ ))
|
|
|
|
{
|
|
|
|
arg = this.path + '/' + arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = arg.replace ( /\/+/, '/' );
|
2010-12-23 14:25:43 +01:00
|
|
|
|
|
|
|
if ( arg != '/' )
|
|
|
|
{
|
|
|
|
arg = arg.replace ( /\/*$/, '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( arg.match ( /^(.+?\/?\.\.)/ ))
|
|
|
|
{
|
|
|
|
var part = RegExp.$1;
|
|
|
|
|
|
|
|
if ( arg.match ( /^(.+?)\/?\.\./ ))
|
|
|
|
{
|
|
|
|
if ( RegExp.$1 == '/' )
|
|
|
|
{
|
|
|
|
arg = arg.replace ( part, '/' );
|
|
|
|
} else {
|
|
|
|
part.match ( /^(.*)\/[^\/]*\/\.\..*$/ );
|
|
|
|
var sup = RegExp.$1;
|
|
|
|
arg = arg.replace ( part, sup );
|
|
|
|
|
|
|
|
if ( arg.length == 0 )
|
|
|
|
{
|
|
|
|
arg = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-23 01:28:38 +01:00
|
|
|
return arg;
|
|
|
|
}
|
2010-12-21 22:47:39 +01:00
|
|
|
}
|
|
|
|
|