Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions src/xkcd_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function randomChoice(items) {
return items[getRandomInt(0, items.length-1)];
}

var User = "guest";

var xkcd = {
latest: null,
last: null,
Expand Down Expand Up @@ -61,6 +63,12 @@ var xkcdDisplay = TerminalShell.commands['display'] = function(terminal, path) {
terminal.print("Time travel mode not enabled.");
return;
}

if (num == 404) {
terminal.print("Error 404: Not Found");
return;
}

} else {
num = xkcd.last.num;
}
Expand Down Expand Up @@ -91,12 +99,20 @@ var xkcdDisplay = TerminalShell.commands['display'] = function(terminal, path) {
};

TerminalShell.commands['next'] = function(terminal) {
xkcdDisplay(terminal, xkcd.last.num+1);
if (xkcd.last.num != 403) {
xkcdDisplay(terminal, xkcd.last.num+1);
} else {
xkcdDisplay(terminal, 405);
}
};

TerminalShell.commands['previous'] =
TerminalShell.commands['prev'] = function(terminal) {
xkcdDisplay(terminal, xkcd.last.num-1);
if (xkcd.last.num != 405) {
xkcdDisplay(terminal, xkcd.last.num-1);
} else {
xkcdDisplay(terminal, 403);
}
};

TerminalShell.commands['first'] = function(terminal) {
Expand Down Expand Up @@ -126,21 +142,40 @@ TerminalShell.commands['sudo'] = function(terminal) {
if (cmd_args.join(' ') == 'make me a sandwich') {
terminal.print('Okay.');
} else {
var cmd_name = cmd_args.shift();
cmd_args.unshift(terminal);
cmd_args.push('sudo');
if (TerminalShell.commands.hasOwnProperty(cmd_name)) {
this.sudo = true;
this.commands[cmd_name].apply(this, cmd_args);
delete this.sudo;
} else if (!cmd_name) {
terminal.print('sudo what?');
if (User = "root") {
var cmd_name = cmd_args.shift();
cmd_args.unshift(terminal);
cmd_args.push('sudo');
if (TerminalShell.commands.hasOwnProperty(cmd_name)) {
this.sudo = true;
this.commands[cmd_name].apply(this, cmd_args);
delete this.sudo;
} else if (!cmd_name) {
terminal.print('sudo what?');
} else {
terminal.print('sudo: '+cmd_name+': command not found');
}
} else {
terminal.print('sudo: '+cmd_name+': command not found');
terminal.print(User+' is not in the sudoers file.');
terminal.print('This incident will be reported.');
xkcdDisplay(terminal, 838);
}
}
};

TerminalShell.commands['su'] = function(terminal, user) {

if (!user) {
user = "root";
}

terminal.print("Password: ");
User = user;
delete user;
Terminal.config.prompt = User+'@xkcd:/$ ';
document.getElementById('title').innerHTML = User+'@xkcd';
}

TerminalShell.filters.push(function (terminal, cmd) {
if (/!!/.test(cmd)) {
var newCommand = cmd.replace('!!', this.lastCommand);
Expand Down