diff --git a/README.md b/README.md index b4924fe..9603e92 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,14 @@ -# newcmd - +commands: +```python +{ +"cd": "go to a directory, argument is target directory", +"ls": "list the directory" +"createdir": "creates a directory, argument is name", +"createfile": "creates a file, argument is name", +"remove": "removes a file or a directory, argument is name", +"editfile": "edits a file, argument is name", +"showfile": "shows a file, argument is name", +"createacc": "creates a user, argument 1 is name, argument 2 is password, argument 3 is rights", +"removeacc": "removes a user, argument 1 is name +} +``` \ No newline at end of file diff --git a/main.py b/main.py index 17b296e..c76c255 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ try: import hashlib from simplecrypt import encrypt, decrypt from copy import deepcopy + import sys except ModuleNotFoundError: print("error: some modules are missing or not being found") print("done") @@ -42,7 +43,11 @@ def login(user, password): else: return False -def updateusersfile(users): +def updateusersfile(user): + global users + user1 = deepcopy(user) + user1["data"] = encrypt(user1["pass"], str(user1["data"]).encode()) + users[user1["name"]] = user1 usersfile = {} for i in range(len(users)): usersfile[i] = list(users.values())[i] @@ -50,7 +55,7 @@ def updateusersfile(users): file.write(str(usersfile)) file.close() -def createnewaccount(): +def createnewaccount(right): name = input("enter name for your new user: ") password = input("enter password for your new user: ") back = {} @@ -59,12 +64,82 @@ def createnewaccount(): h.update(password.encode()) h = h.hexdigest() back["pass"] = h - back["data"] = {} - user = deepcopy(back) - user["data"] = encrypt(h, str({"rights": "Admin"}).encode()) - usersdata[user["name"]] = user - updateusersfile(usersdata) + back["data"] = {"rights": right, "files": {}} + updateusersfile(back) return back + +def cd(command): + global currentdir + if currentdir == currentdir: + if len(command) != 1: + if command[1] != "..": + if command[1] in user["data"]["files"]: + currentdir = command[1] + else: + print("error in cd: directory '{}' not existing".format(command[1])) + else: + currentdir = "" + else: + print("error in cd: cd requires 1 argument") + +def ls(): + if currentdir != "": + back = str(user["data"]["files"][currentdir].keys()) + back = back.replace("dict_keys([", "") + back = back.replace("])", "") + print(back) + else: + back = str(user["data"]["files"].keys()) + back = back.replace("dict_keys([", "") + back = back.replace("])", "") + print(back) + +def createdir(command): + if len(command) != 1: + user["data"]["files"][command[1]] = {} + updateusersfile(user) + else: + print("error in createdir: createdir requires 1 argument") + +def remove(command): + if len(command) != 1: + if command[1] in user["data"]["files"]: + del user["data"]["files"][command[1]] + updateusersfile(user) + else: + print("error in removedir: directory {} is not existing".format(command[1])) + else: + print("error in removedir: removedir requires 1 argument") + +def createfile(command): + if len(command) != 1: + user["data"]["files"][command[1]] = "" + updateusersfile(user) + else: + print("error in createfile: createfile requires 1 argument") + +def editfile(command): + if len(command) != 1: + if currentdir != "": + if command[1] in user["data"]["files"][currentdir]: + towrite = sys.stdin.read() + while not "exit:editor" in towrite: + print(towrite) + towrite = towrite.replace("exit:editor", "") + user["data"]["files"][currentdir][command[1]] = towrite + else: + print("error in editfile: file {} not found".format(command[1])) + else: + if command[1] in user["data"]["files"]: + towrite = sys.stdin.readlines() + while not "exit:editor" in towrite: + print(towrite) + towrite = towrite.replace("exit:editor", "") + user["data"]["files"][command[1]] = towrite + else: + print("error in editfile: file {} not found".format(command[1])) + else: + print("error in editfile: editfile requires 1 argument") # login if userslen != 0: count = 0 @@ -79,5 +154,25 @@ if userslen != 0: back = login(list(usersdata.values())[userin-1], password) user = back else: - user = createnewaccount() + user = createnewaccount("Admin") +#main +currentdir = "" +while True: + command = input("{}: ".format(currentdir)) + command = command.split() + if len(command) != 0: + if command[0] == "cd": + cd(command) + elif command[0] == "ls": + ls() + elif command[0] == "createdir": + createdir(command) + elif command[0] == "remove": + remove(command) + elif command[0] == "createfile": + createfile(command) + elif command[0] == "editfile": + editfile(command) + else: + print("unknown command: {}".format(command[0]))