-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
161 lines (153 loc) · 5.24 KB
/
script.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const fs = require("fs");
const bcrypt = require('bcryptjs');
const { fetch } = "node-fetch";
var user;
var cmds = [
{
short_name: "su",
long_name: "Switch User",
description: "Allows you to switch the current user to another.",
params: "${USER}",
execute: async function(user, user_name){
login(user_name);
}
},
{
short_name: "au",
long_name: "Add User",
description: "Allows you to add a user.",
params: "${NEW USER} ${NEW PASSWORD}",
execute: async function(user, new_user_name, new_user_password){
bcrypt.hash(new_user_password, 10)
.then(async(hashed_password) => {
data = {
name: new_user_name,
password: hashed_password
}
await fs.writeFile(`./users/${new_user_name}.json`, JSON.stringify(data), (err) => {});
});
open_terminal();
}
},
{
short_name: "vv",
long_name: "View Verse",
description: "Allows you to view a verse.",
params: "${VERSE}",
execute: async function(user, verse1, verse2, verse3){
var request = require("request");
var url = `https://bible-api.com/${verse1}%20${verse2}?translation=kjv`;
if(verse3 != (undefined && null && "" && " ")){
var url = `https://bible-api.com/${verse1}%20${verse2}:${verse3}?translation=kjv`;
}
let verse_json;
request({ url: url, json: true }, function (error, response, body) {
if (!error && response.statusCode === 200) {
verse_json = body;
console.log(`${verse_json.reference} - ${verse_json.verses[0].text.trim()}`);
open_terminal();
}
else{
console.log("Failed to get verse.");
open_terminal();
}
});
}
},
{
short_name: "help",
long_name: "Help",
description: "Gives details on a command.",
params: "${CMD}",
execute: async function(user, cmd_name){
let cmd_data;
cmds.forEach((cmd) => {
if(cmd.short_name == cmd_name) cmd_data = cmd;
});
console.log(`\nHere is info on the ${cmd_data.long_name} command.\nShort Name: ${cmd_data.short_name}\nDescription: ${cmd_data.description}\n`)
open_terminal();
}
}
]
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
//cmds[1].execute("", "root", "RootPassword");
login();
function open_terminal(){
let question = "Use";
cmds.forEach((cmd, index) => {
if(index != cmds.length - 1){
question += ` "${cmd.short_name} ${cmd.params}",`
}
else{
question += ` or "${cmd.short_name} ${cmd.params}".`
}
})
question += "\n";
readline.question(question, data => {
cmd_valid = false;
data = data.split(" ");
let params = [];
data.forEach((sect, index) => {
if(index != 0){
params.push(sect);
}
})
cmds.forEach((cmd, index) => {
if(data[0] == cmd.short_name){
cmd_valid = true;
cmd.execute(user, ...params);
}
else if(index == cmds.length-1 && cmd_valid == false){
throw new Error(`No commands found with short_name of ${data[0]}.`);
}
})
cmd_valid = false;
});
}
async function login(user_name){
if(user_name){
try{
var user_data = require(`./users/${user_name.split(" ")[0]}.json`);
readline.question(`Enter the password for ${user_name}.\n`, password_data => {
bcrypt.compare(password_data, user_data.password)
.then(do_match => {
if(do_match){
user = data;
open_terminal();
}
else{
console.log("Invaild Password.\n")
login(user_name);
}
})
})
} catch(err){
console.log(err);
}
}
else{
readline.question("Which user do you want to use?\n", async(data) => {
try{
var user_data = require(`./users/${data.split(" ")[0]}.json`);
readline.question(`Enter the password for ${data}.\n`, password_data => {
bcrypt.compare(password_data, user_data.password)
.then(do_match => {
if(do_match){
user = data;
open_terminal();
}
else{
console.log("Invaild Password.\n")
login();
}
})
})
} catch(err){
console.log(err);
}
});
}
}