def execute(command, user): """ Attempt to execute a command for the specified user with given arguments, returning a message to be sent to the user after the command has finished :param command: function, function to execute :type command: function :param user: string, specified user :type user: str :return: response to an executed command :rtype: str :raises: :ValueError: if command does not exist """ # Determine the name of the command and arguments from the full # command (taking into account aliases). name = '' args = '' for command_name in _CMD_NAMES: if bool(re.match(command_name, command, re.I)): name = command_name args = command[len(command_name):] else: try: for command_alias in _CONFIG[command_name + '_aliases']: if bool(re.match(command_alias, command, re.I)): name = command_name args = command[len(command_alias):] break except KeyError: pass # No aliases defined, that's fine - keep looking if name: break # If no command found, assume we're executing the default command if not name: name = _CONFIG['default_command'] args = command # Verify this command is installed if name not in Command.commands: raise ValueError(name + ' command must be installed by calling Command(\'' + name + '\', <handler>)') # Verify the user has permission to execute this command if not cm.has_permission(user, name): return _CONFIG['unauthorized_response'].format(cmd=name, user=user) # Check to see if the command issued should be throttled if cm.is_throttle_exceeded(name, user): return _CONFIG['throttle_limit_reached_response'].format(cmd=name, user=user) # Execute the command return Command.commands[name].execute(user, args.strip())
def cmd_help(*args): """ Returns a list of available commands for the requesting user. :param args: [specified user, arguments for command] :type args: list :return: list of available commands that the current user has permission to execute :rtype: str """ user = args[0] help_msg = "Commands:\n" for cmd in _CMD_NAMES: if cm.has_permission(user, cmd): cmd_description = cm.sms()[cmd + '_description'] if cmd_description: help_msg += cmd_description + "\n" return help_msg