예제 #1
0
def test_hook_args(hook):
    bot = MockBot()
    if hook.type in (
            "irc_raw",
            "perm_check",
            "periodic",
            "on_start",
            "on_stop",
            "event",
            "on_connect",
    ):
        event = Event(bot=bot)
    elif hook.type == "command":
        event = CommandEvent(bot=bot,
                             hook=hook,
                             text="",
                             triggered_command="",
                             cmd_prefix=".")
    elif hook.type == "regex":
        event = RegexEvent(bot=bot, hook=hook, match=None)
    elif hook.type.startswith("on_cap"):
        event = CapEvent(bot=bot, cap="")
    elif hook.type == "post_hook":
        event = PostHookEvent(bot=bot)
    elif hook.type == "irc_out":
        event = IrcOutEvent(bot=bot)
    elif hook.type == "sieve":
        return
    else:  # pragma: no cover
        assert False, "Unhandled hook type '{}' in tests".format(hook.type)

    for arg in hook.required_args:
        assert hasattr(
            event,
            arg), "Undefined parameter '{}' for hook function".format(arg)
예제 #2
0
파일: irc.py 프로젝트: knappjf/CloudBot
    async def send(self, line, log=True):
        # make sure we are connected before sending
        if not self.connected:
            if self._connecting:
                await self._connected_future
            else:
                raise ValueError(
                    "Attempted to send data to a closed connection"
                )

        old_line = line
        filtered = bool(self.bot.plugin_manager.out_sieves)

        for out_sieve in self.bot.plugin_manager.out_sieves:
            event = IrcOutEvent(
                bot=self.bot, hook=out_sieve, conn=self.conn, irc_raw=line
            )

            ok, new_line = await self.bot.plugin_manager.internal_launch(
                out_sieve, event
            )
            if not ok:
                logger.warning(
                    "Error occurred in outgoing sieve, falling back to old behavior"
                )
                logger.debug("Line was: %s", line)
                filtered = False
                break

            line = new_line
            if line is not None and not isinstance(line, bytes):
                line = str(line)

            if not line:
                return

        if not filtered:
            # No outgoing sieves loaded or one of the sieves errored, fall back to old behavior
            line = old_line[:510] + "\r\n"
            line = line.encode("utf-8", "replace")

        if not isinstance(line, bytes):
            # the line must be encoded before we send it, one of the sieves didn't encode it, fall back to the default
            line = line.encode("utf-8", "replace")

        if log:
            logger.debug("[%s|out] >> %r", self.conn.name, line)

        self._transport.write(line)