-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuiltIns.java
More file actions
153 lines (137 loc) · 5.21 KB
/
BuiltIns.java
File metadata and controls
153 lines (137 loc) · 5.21 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.seven.bii;
import java.io.File;
import java.lang.reflect.Array;
import java.util.Arrays;
import static com.mycompany.seven.bii.ZipOutput.Encrypt;
import static com.mycompany.seven.bii.ZipOutput.Decrypt;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.lingala.zip4j.exception.ZipException;
/**
*
* @author 7 Bii
*/
public class BuiltIns {
//User Details
private static String Username;
private static String Password;
public static String[][] twoDStringArray = {{"root","root","super"},{"maya","maya123","super"},{"shruti","shruti123", "user"}};
//Validate if builtin command
public static String builtInSort(String input) {
while (true){
//take input and check against list of commands
String [] parsedCommand = input.split(" ");
String[] listOfCommands = {"cd", "showDir", "login", "whoAmI", "logoff","chPass"};
boolean valid = Arrays.stream(listOfCommands).anyMatch(Array.get(parsedCommand, 0)::equals);
if ( !valid ){
return "false"; //if no command matches
}else{
//Match the found command
switch(parsedCommand[0]) {
case "cd":
return changeDirectory(input);
case "showDir":
String dir = System.getProperty("user.dir");
return dir;
case "login":
//splits login details
String loginResult = "";
String loginDetails = input.substring(input.indexOf(' ') + 1);
String details = loginDetails + " ";
String [] userPass = details.split(" ");
String user = userPass[0];
String pass = userPass[1];
System.out.println(login(user,pass)); //login method
//validate login status
if(login(user,pass) == true){
try {
//if true decrypt
Decrypt(Username, Password);
loginResult = "Login Successful. Hello " + Username;
} catch (ZipException ex) {
Logger.getLogger(BuiltIns.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
loginResult = "Login Failed";
}
return loginResult;
case "whoAmI":
return Username;
case "logoff":
{
try {
//encrypt user folder
Encrypt(Username, Password);
} catch (ZipException ex) {
Logger.getLogger(BuiltIns.class.getName()).log(Level.SEVERE, null, ex);
}
String validDir;
System.setProperty("user.dir", "/home/ntu-user/Users/"); //to go back to home directory
validDir = System.setProperty("user.dir", "/home/ntu-user/Users/");
}
Username = "guest";
return "Logged out. Guest Signed in";
}
}
}
}
//Validate user credentials
public static boolean login(String username, String password){
//Booleans
boolean loggedin = false;
boolean userbool = false;
boolean passbool = false;
//tmp passwords
String tmpuser = "";
String tmppass = "";
int row;
//Loop to check username
for(row = 0;row<3; row++){
tmpuser = twoDStringArray[row][0];
//If username found make userbool true
if(username.equals(tmpuser)){
userbool = true;
break;
}
}
//checks password to username
tmppass = twoDStringArray[row][1];
if(password.equals(tmppass)){
passbool = true;
}
//if username and password match
if(userbool == true && passbool == true){
Username = username;
Password = password;
loggedin = true;
}
return loggedin;
}
//change directory
private static String changeDirectory(String input) {
String validDir = null;
String dirInput = input.substring(input.indexOf(' ') + 1);
System.out.println(System.getProperty("user.dir"));
//goes to home directory if ..
if("..".equals(dirInput)){
System.setProperty("user.dir", "/home/ntu-user/Users/");
validDir = System.setProperty("user.dir", "/home/ntu-user/Users/");
}else{
//checks if valid directory
File tmpDir = new File("/home/ntu-user/Users/" + dirInput);
boolean exists = tmpDir.exists();
if(exists){
//if valid sets to working directory
validDir = System.setProperty("user.dir", "/home/ntu-user/Users/" + dirInput);
}else{
validDir = ("There is no directory called /home/ntu-user/Users/" + dirInput);
}
}
return validDir;
}
}