Beispiel #1
0
class ServerCommandHandler():
    """ServerCommandHandler"""
    def __init__(self, serverhandler):
        """Constructor"""
        self.srvhandler = ServerHandler()
        self.srvhandler = serverhandler

        self.commands = {
            'luarun': self.command_luarun,
            'rcon': self.command_rcon,
            'sendfile': self.command_sendfile,
        }

    def queue_command(self, serverip, arg_type, arg_command):
        """Queue Server Command"""
        self.srvhandler.servers[serverip].append([arg_type, arg_command])

    def command_luarun(self, arg_command, arg_input):
        """Luarun"""
        tosend = arg_input[len(arg_command) + 1:]
        self.queue_command(self.srvhandler.get_current_server(), arg_command,
                           tosend)
        print("luarun " + tosend + ">>" + self.srvhandler.get_current_server())

    def command_rcon(self, arg_command, arg_input):
        """Rcon"""
        tosend = arg_input[len(arg_command) + 1:]
        self.queue_command(self.srvhandler.get_current_server(), arg_command,
                           tosend)
        print("rcon " + tosend + ">>" + self.srvhandler.get_current_server())

    def command_sendfile(self, arg_command, arg_input):
        """SendFile"""
        tosend = arg_input[len(arg_command) + 1:]
        try:
            file_container = open('./includes/' + tosend + '.lua', 'r').read()
        except Exception:
            print("Error opening file: " + tosend + '.lua')
            return
        self.queue_command(self.srvhandler.get_current_server(), 'luarun',
                           file_container)
        print("luarun " + tosend + ">>" + self.srvhandler.get_current_server())
Beispiel #2
0
class InputHandler():
    """Class InputHandler"""

    def __init__(self, serverhandler, servercmdhandler):
        """Constructor"""
        self.srvhandler = ServerHandler()
        self.srvhandler = serverhandler

        self.cmdhandler = ServerCommandHandler(serverhandler)
        self.cmdhandler = servercmdhandler

    def handle_console_input(self, originalInput):
        """Main Input func. Todo: split"""
        cmd = originalInput.split(" ")

        if originalInput == "list":
            for k in self.srvhandler.servers.keys():
                online = self.srvhandler.server_time[k]['curtime'] + \
                    10 >= time.time()
                print(k + " | Online: " + str(online))
            return

        try:
            command = cmd[0]
            arg = cmd[1]

        except Exception:
            print("[1] No such command: " + originalInput)
            return

        if command == "addserver":
            with open('server.list', 'a') as file:
                if os.stat("server.list").st_size == 0:
                    file.write(arg)
                else:
                    file.write('\n' + arg)
            self.srvhandler.servers[arg] = []
            self.srvhandler.set_server_time(arg, 0)
            print('Successful added ' + arg + ' to the list')
            return
        if command == "selectserver":
            if arg not in self.srvhandler.servers:
                print("No such server found.")
                return
            self.srvhandler.set_current_server(arg)
            print("Successfully selected server: " +
                  self.srvhandler.get_current_server())
            return

        try:

            if self.cmdhandler.commands[command]:
                if self.srvhandler.get_current_server() == "nil":
                    print(
                        "No server selected. ",
                        "Select a server by using selectserver [ip]")
                    return
                self.cmdhandler.commands[command](command, originalInput)
                return
        except Exception:
            print("[2] Wrong syntax: " + command)

    def read_input(self):
        while True:
            original_input = input()
            self.handle_console_input(original_input)