#!/usr/bin/env python3 from server.authentication import Authetication from executor import Executor class Server: def __init__(self, homeDirectory: str = "", sessionTimeout: int = 120, availableServer: bool = True, currentDirectory: str = ""): self.isAuthenticated = False self.homeDirectory = homeDirectory self.sessionTimeout = sessionTimeout self.availableServer = availableServer if currentDirectory == "": self.currentDirectory = homeDirectory else: self.currentDirectory = currentDirectory self.executor = Executor(homeDirectory) def login(self, username: str, password: str) -> bool: auth = Authetication() self.isAuthenticated = True home_directory = auth.login(username, password) if not home_directory: return False else: return True def logout(self) -> None: self.isAuthenticated = False self.availableServer = False self.homeDirectory = "" self.currentDirectory = "" def parseCommand(self, command: str) -> str: parsedCommand = command.split(" ") if len(parsedCommand) > 3: return "ERROR" # TODO: Ez igy gecironda, ezt meg kene csinalni rendesen. elif len(parsedCommand) == 1: return self.execute(parsedCommand[0]) elif len(parsedCommand) == 2: return self.execute(parsedCommand[0], parsedCommand[1]) elif len(parsedCommand) == 3: return self.execute(parsedCommand[0], parsedCommand[1], parsedCommand[2]) def execute(self, command: str, firstParam: str = "", secondParam: str = "") -> str: if self.homeDirectory == "" or self.currentDirectory == "" or self.executor.currentDirectory == "" or self.executor.baseDir == "": raise Exception("Directories must not be empty string. Did the user log in?") if command == "MKD": if secondParam != "": return "ERROR" else: return self.executor.createDirectory(firstParam) elif command == "RMD": if secondParam != "": return "ERROR" else: return self.executor.removeDirectory(firstParam) elif command == "GWD": if secondParam != "" or firstParam != "": return "ERROR" else: return self.executor.getCurrentDirectory() elif command == "CWD": if secondParam != "": return "ERROR" else: return self.executor.setCurrentDirectory(firstParam) elif command == "LST": if secondParam != "" or firstParam != "": return "ERROR" else: return self.executor.listCurrentDirectoryContent() elif command == "RMF": if secondParam != "": return "ERROR" else: return self.executor.removeFileInCurrentDirectory(firstParam) elif command == "UPL": if secondParam != "": return "ERROR" else: # TODO pass elif command == "DNL": if secondParam != "": return "ERROR" else: # TODO pass else: return "ERROR"