def process_command(self, command_string): command, args = util.parse_command(command_string) if command == 'who': self.who() elif command == 'last': try: number = int(args[0]) self.last(number) except Exception: self.send_line('error:args:{}'.format(command)) elif command == 'broadcast': message_string = ' '.join(args) self.broadcast(message_string) elif command == 'send': try: usernames = set( args[0] if isinstance(args[0], list) else [args[0]]) message_string = ' '.join(args[1:]) self.send(message_string, usernames) except Exception: self.send_line('error:args:{}'.format(command)) elif command == 'logout': self.send_line('goodbye') self.user.logout() self.log('{} logged out'.format(self.user.username)) elif command == 'fetch': self.send_line('fetching') else: self.send_line('error:command:{}'.format(command))
def connect_to_user(self, username): try: self.server_socket = socket.socket() self.server_socket.connect((self.server, self.server_port)) self.server_socket.sendall(pickle.dumps("CONNECT " + username)) self.server_socket.shutdown(1) response = self._recv() self.server_socket.close() if len(response) == 2: self.udp_remote = response else: r, args = util.parse_command(response) if r == "ERROR" and args[0] == "E03": print(Fore.YELLOW + "This username does not exist") except OSError as e: print(Fore.RED + "\nImpossible to connect: Server not found") print(Fore.RED + self.server + ":" + str(self.server_port)) print(Fore.RED + str(e)) except ValueError: print(Fore.RED + "Internal server error") self._run = False
def process_command(self, command_string): command, args = util.parse_command(command_string) if command == 'who': self.who() elif command == 'last': try: number = int(args[0]) self.last(number) except Exception: self.send_line('error:args:{}'.format(command)) elif command == 'broadcast': message_string = ' '.join(args) self.broadcast(message_string) elif command == 'send': try: usernames = set( args[0] if isinstance(args[0], list) else [args[0]] ) message_string = ' '.join(args[1:]) self.send(message_string, usernames) except Exception: self.send_line('error:args:{}'.format(command)) elif command == 'logout': self.send_line('goodbye') self.user.logout() self.log('{} logged out'.format(self.user.username)) elif command == 'fetch': self.send_line('fetching') else: self.send_line('error:command:{}'.format(command))
def _handle(self, client, ip): request = self._recv(client) command, args = util.parse_command(request) commands = { "AVAILABLE": self._handle_command_available, "CONNECT": self._handle_connect_request, "USERLIST": self._handle_userlist_request, "REMOVE": self._remove, } if command in commands: commands[command](client, args) else: self._invalid_request(client)
def run(self): r = "" while not r == "OK": response = self.send_available_to_server() # If server is unavailable if response is None: print("Exiting...") sys.exit(1) try: r, args = util.parse_command(response) except ValueError: print(Fore.RED + "Internal server error") print("Exiting...") sys.exit(1) if r == "ERROR" and args[0] == "E02": print(Fore.YELLOW + "This username \"%s\" is already taken, please choose another" % self.username) self.ask_username() threading.Thread(target=self._udp_listen).start() while self._run: line = sys.stdin.readline().rstrip() # Skip if empty line if len(line) == 0: continue # If line begins with a colon it's considered a command if line[0] == ":" and len(line) > 2: command, args = util.parse_command(line[1:]) self._handle_command(command, args) else: self._udp_send(line)
def handle_command(self, command: str, channel, condition=None): """ Executes bot command if the command is known """ # Default response is help text for the user default_response = "Not sure what you mean." # Finds and executes the given command, filling in response response = "" # Default timestamp timestamp = datetime.datetime.now() # This is where you start to implement more commands! # for handler in task_handlers: # if command.startswith(handler.TASK_COMMAND): # handler.handle_request(command, condition) if command.startswith("next"): response = "next task is {}.".format(self.show_next()) elif command.startswith("done"): commands = command.split(' ') if len(commands) > 1: if is_int(commands[1]): try: target_task = self.task_manager.get_task_by_index(int(commands[1])) response = "{} is done! Well done!\n".format(target_task.description) self.task_manager.done_task(target_task, timestamp) self.task_manager.update_task_list() response += self.show_next() except ValueError as e: response = e.args[0] else: try: self.task_manager.done_task_by_name(commands[1], timestamp) self.task_manager.update_task_list() response = "{} is done! Well done!\n".format(commands[1]) response += self.show_next() except ValueError as e: response = e.args[0] else: self.task_manager.done_task_by_index(0, timestamp) self.task_manager.update_task_list() response = "{} is done! Well done!\n".format(self.displayed_task.description) response += self.show_next() elif command.startswith("postpone"): self.task_manager.postpone(self.displayed_task) response = "postponed {}.\n".format(self.displayed_task.description) response += self.show_next() elif command.startswith("adddaily"): commands = command.split(' ') if len(commands) > 1: if len(commands) > 2 and is_int(commands[2]): frequency = int(commands[2]) else: frequency = 5 gen = {'task_type': 'Unscheduled', 'last_done': datetime.datetime(2018, 9, 16, 12, 30, 0), 'frequency': frequency, 'task': {'priority': 4, 'due': datetime.datetime(2018, 1, 10, 10, 0, 0), 'time_needed': 15, 'description': commands[1], 'time_slot': 'any', 'cancellable': False, 'position': 'home'}} try: self.task_manager.insert_generator(gen) response = "A task {} is added!".format(commands[1]) except: response = "Failed to add task. Something wrong!" elif command.startswith("top"): commands = command.split(' ') length = 10 if len(commands) > 1: try: length = int(commands[1]) except ValueError: length = 10 tasks = self.task_manager.top(length) response = "task list:\n" for index, task in enumerate(tasks): response += "{} {}: {}, {}\n".format(index, task.description, task.priority, task.due.date()) elif command.startswith("task"): print(command) dummy, args = parse_command(['task'], command) print(args) self.task_manager.add_task(args[0]) # Sends the response back to the channel self.slack_client.api_call( "chat.postMessage", channel=channel, text=response or default_response#, # attachments=BUTTON_JSON['attachments'] )