|
| 1 | +/* |
| 2 | +* | |
| 3 | +* || |
| 4 | +* |||||| ||||||||| |||||||| ||||||||| ||||||| ||| ||| ||||||| ||||||||| |||||| |||||||| |
| 5 | +* ||| || ||| || || ||| ||| || || ||| ||| |
| 6 | +* ||| |||||||| ||| |||||||| |||||| |||||||| |||||| |||||||| ||| ||| |
| 7 | +* ||| ||| ||| ||| ||| ||| ||| ||| ||| || || ||| ||| ||| ||| |
| 8 | +* |||||| ||| ||| ||| ||| ||| ||| ||| ||| || || ||| ||| |||||| ||| |
| 9 | +* || |
| 10 | +* | |
| 11 | +* |
| 12 | +* A Cross Platform OS Shell |
| 13 | +* Powered By Truncheon Core |
| 14 | +*/ |
| 15 | + |
1 | 16 | package Cataphract.API.Wraith;
|
2 | 17 |
|
| 18 | +import java.io.BufferedReader; |
3 | 19 | import java.io.Console;
|
4 | 20 | import java.io.File;
|
5 |
| - |
| 21 | +import java.io.FileReader; |
6 | 22 | import java.nio.file.Files;
|
7 | 23 | import java.nio.file.StandardCopyOption;
|
8 | 24 |
|
9 | 25 | import Cataphract.API.Anvil;
|
10 | 26 | import Cataphract.API.IOStreams;
|
| 27 | +import Cataphract.API.Dragon.Login; |
11 | 28 | import Cataphract.API.Minotaur.Cryptography;
|
12 |
| - |
| 29 | +import Cataphract.API.Minotaur.PolicyCheck; |
| 30 | + |
| 31 | +/** |
| 32 | +* A utility class for file management. |
| 33 | +* |
| 34 | +* @author DAK404 (https://github.com/DAK404) |
| 35 | +* @version 1.3.0 (12-August-2024, Cataphract) |
| 36 | +* @since 0.0.1 (Zen Quantum 0.0.1) |
| 37 | +*/ |
13 | 38 | public class FileManagement
|
14 | 39 | {
|
15 | 40 | private String _username = "";
|
@@ -212,19 +237,68 @@ private final void changeDirectory(String destination)throws Exception
|
212 | 237 |
|
213 | 238 | public void fileManagementLogic()throws Exception
|
214 | 239 | {
|
215 |
| - // AUTHENTICATION LOGIC OMITTED FOR THE MOMENT. |
216 |
| - if(login()) |
| 240 | + if (new PolicyCheck().retrievePolicyValue("filemgmt").equals("on") || new Login(_username).checkPrivilegeLogic()) |
| 241 | + { |
| 242 | + if(login()) |
| 243 | + { |
| 244 | + String inputValue = ""; |
| 245 | + do |
| 246 | + { |
| 247 | + inputValue = console.readLine(_name + "@" + _presentWorkingDirectory.replace(_username, _name) + "> "); |
| 248 | + grinchInterpreter(inputValue); |
| 249 | + } |
| 250 | + while(!inputValue.equalsIgnoreCase("exit")); |
| 251 | + } |
| 252 | + else |
| 253 | + IOStreams.printError("Invalid Credentials."); |
| 254 | + } |
| 255 | + else |
| 256 | + IOStreams.printError("Policy Management System - Permission Denied."); |
| 257 | + } |
| 258 | + |
| 259 | + public void fileManagementLogic(String scriptFileName)throws Exception |
| 260 | + { |
| 261 | + if ((new PolicyCheck().retrievePolicyValue("filemgmt").equals("on") && new PolicyCheck().retrievePolicyValue("script").equals("on")) || new Login(_username).checkPrivilegeLogic()) |
217 | 262 | {
|
218 |
| - String inputValue = ""; |
219 |
| - do |
| 263 | + if(scriptFileName == null || scriptFileName.equalsIgnoreCase("") || scriptFileName.startsWith(" ") || new File(scriptFileName).isDirectory() || ! (new File("./Users/Truncheon/" + _username + "/" + scriptFileName + ".fmx").exists())) |
220 | 264 | {
|
221 |
| - inputValue = console.readLine(_name + "@" + _presentWorkingDirectory.replace(_username, _name) + "> "); |
222 |
| - grinchInterpreter(inputValue); |
| 265 | + IOStreams.printError("Invalid Script File!"); |
| 266 | + } |
| 267 | + else |
| 268 | + { |
| 269 | + if(login()) |
| 270 | + { |
| 271 | + //Initialize a stream to read the given file. |
| 272 | + BufferedReader br = new BufferedReader(new FileReader(scriptFileName)); |
| 273 | + //Initialize a string to hold the contents of the script file being executed. |
| 274 | + String scriptLine; |
| 275 | + |
| 276 | + //Read the script file, line by line. |
| 277 | + while ((scriptLine = br.readLine()) != "<EndGrinch>") |
| 278 | + { |
| 279 | + //Check if the line is a comment or is blank in the script file and skip the line. |
| 280 | + if(scriptLine.startsWith("#") || scriptLine.equalsIgnoreCase("")) |
| 281 | + continue; |
| 282 | + |
| 283 | + //Check if End Script command is encountered, which will stop the execution of the script. |
| 284 | + else if(scriptLine.equalsIgnoreCase("End Script")) |
| 285 | + break; |
| 286 | + |
| 287 | + //Read the command in the script file, and pass it on to menuLogic(<command>) for it to be processed. |
| 288 | + grinchInterpreter(scriptLine); |
| 289 | + } |
| 290 | + |
| 291 | + //Close the streams, run the garbage collector and clean. |
| 292 | + br.close(); |
| 293 | + } |
| 294 | + else |
| 295 | + { |
| 296 | + IOStreams.printError("Invalid Credentials."); |
| 297 | + } |
223 | 298 | }
|
224 |
| - while(!inputValue.equalsIgnoreCase("exit")); |
225 | 299 | }
|
226 | 300 | else
|
227 |
| - IOStreams.printError("Invalid Credentials."); |
| 301 | + IOStreams.printError("Policy Management System - Permission Denied."); |
228 | 302 | }
|
229 | 303 |
|
230 | 304 | private void grinchInterpreter(String command)throws Exception
|
@@ -277,14 +351,14 @@ private void grinchInterpreter(String command)throws Exception
|
277 | 351 | if(commandArray.length < 2)
|
278 | 352 | IOStreams.printError("Invalid Syntax.");
|
279 | 353 | else
|
280 |
| - FileWrite.editFile(commandArray[1], _presentWorkingDirectory); |
| 354 | + new FileWrite(_username).editFile(commandArray[1], _presentWorkingDirectory); |
281 | 355 | break;
|
282 | 356 |
|
283 | 357 | case "read":
|
284 | 358 | if(commandArray.length < 2)
|
285 | 359 | IOStreams.printError("Invalid Syntax.");
|
286 | 360 | else
|
287 |
| - new FileRead().readUserFile(_presentWorkingDirectory + commandArray[1]); |
| 361 | + new FileRead(_username).readUserFile(_presentWorkingDirectory + commandArray[1]); |
288 | 362 | break;
|
289 | 363 |
|
290 | 364 | case "pwd":
|
|
0 commit comments