Esempio n. 1
0
def call(name, args, return_id=None):
    """
    Call a function callback by name

    :param name: Name of callback
    :param args: Arguments to pass to callback (checked by `utils.check_args` first)
    :param return_id: Write a return value to the script with this ID (optional)
    :return: Return value of callback
    """

    engine.debug('Calling callback {}'.format(name))

    global _callbacks
    if name in _callbacks:
        callback = _callbacks[name]
        if utils.check_args(callback, args):
            return_value = callback(*args)

            # send return value
            if return_id:
                return_message = {'value': return_value, 'id': return_id}
                engine.debug('Return sync: {} {}'.format(
                    return_id, return_value))
                engine.write('return', return_message)

            return True
        else:
            syntax = '{}{}'.format(name, utils.signature(callback))
            raise RuntimeError(
                "{} is an invalid number of arguments for callback '{}'. Syntax: {}"
                .format(len(args), name, syntax))
            return False
    else:
        engine.debug('Tried to call unknown callback: {}'.format(name))
        return False
Esempio n. 2
0
def _handle_message(content):
    """
    Handle a bot message
    """

    global _command_callbacks

    parts = shlex.split(content)

    if not parts:
        # empty
        return

    command = parts[0]
    args = parts[1:]

    if command in _command_callbacks:
        # call the callback
        callback = _command_callbacks[command]
        if utils.check_args(callback, args):
            callback(*args)
        else:
            # bad arguments
            syntax = '{}{}'.format(command, utils.signature(callback))
            aggressor.say("Bad arguments. Syntax: {}".format(command, syntax))
    else:
        # unrecognized
        aggressor.say('Unrecognized command: ' + command)
Esempio n. 3
0
    def alias_callback(*args):
        # first argument is bid
        bid = int(args[0])

        # see above
        quote_replacement = quote_replacement_

        # check arguments
        if not utils.check_args(callback, args):
            syntax = '{} {}'.format(name, utils.signature_command(callback, trim=1))
            aggressor.berror(bid, "Syntax: " + syntax)
            engine.error("Invalid number of arguments passed to alias '{}'. Syntax: {}".format(name, syntax))
            return

        # handle the quote replacement character
        if not quote_replacement:
            global _default_quote_replacement
            quote_replacement = _default_quote_replacement

        if quote_replacement:
            args = [arg.replace(quote_replacement, '"') for arg in args]

        try:
            # run the alias callback
            #engine.debug('calling callback for alias {}'.format(name))
            callback(*args)
        except Exception as e:
            # print exception summaries to the beacon log. raise the
            # Exception again so the full traceback can get printed to the
            # Script Console
            aggressor.berror(bid,
                "Caught Python exception while executing alias '{}': {}\n    See Script Console for more details.".format(name, str(e)))
            raise e
Esempio n. 4
0
 def alias_callback(*args):
     bid = int(args[0])
     if utils.check_args(callback, args):
         try:
             engine.debug('calling callback for alias {}'.format(name))
             callback(*args)
         except Exception as e:
             aggressor.berror(
                 bid,
                 "Caught Python exception while executing alias '{}': {}\n    See Script Console for more details."
                 .format(name, str(e)))
             raise e
     else:
         syntax = '{}{}'.format(name, utils.signature(callback, trim=1))
         aggressor.berror(bid, "Syntax: " + syntax)
         engine.error(
             "Invalid number of arguments passed to alias '{}'. Syntax: {}".
             format(name, syntax))
Esempio n. 5
0
def call(name, args):
    """
    Call a function callback by name

    :param name: Name of callback
    :param args: Arguments to pass to callback (checked by `utils.check_args` first)
    """

    global _callbacks
    if name in _callbacks:
        callback = _callbacks[name]
        if utils.check_args(callback, args):
            callback(*args)
        else:
            syntax = '{}{}'.format(name, utils.signature(callback))
            engine.error("{} is an invalid number of arguments for callback '{}'. syntax: {}".format(len(args), name, syntax))
    else:
        engine.debug('unknown callback {}'.format(name))
Esempio n. 6
0
    def command_callback(*args):
        # see above
        quote_replacement = quote_replacement_

        # check arguments
        if not utils.check_args(callback, args):
            syntax = '{} {}'.format(name, utils.signature_command(callback))
            engine.error("Syntax: " + syntax)
            return

        # handle the quote replacement character
        if not quote_replacement:
            global _default_quote_replacement
            quote_replacement = _default_quote_replacement

        if quote_replacement:
            args = [arg.replace(quote_replacement, '"') for arg in args]

        #engine.debug('calling callback for command {}'.format(name))
        callback(*args)