Example #1
0
    def on_message(self, element):
        """
        Handler for an XMPP message. This method handles logging channel messages (if it occurs
        on a public channel) as well as allowing the plugin manager to send the message to all
        registered plugins. Should the plugin manager yield a response, it will be sent back.

        :param message: A <message/> element, instance of `twisted.words.xish.domish.Element`
        """
        nick = self.parse_nick(element)
        channel = self.parse_channel(element)
        message = self.parse_message(element)

        # This will be empty for messages that were delayed
        if not message:
            return

        # If we don't ignore this, we'll get infinite replies
        if nick == self.nickname:
            return

        # Log the incoming message and notify message subscribers
        logger.debug('[<--] %s/%s - %s', channel, nick, message)
        is_public = self.is_public_channel(channel)

        # When we get a priv msg, the channel is our current nick, so we need to
        # respond to the user that is talking to us
        if is_public:
            # Only log convos on public channels
            self.log_channel_message(channel, nick, message)
        else:
            channel = nick

        # Some things should go first
        try:
            channel, nick, message = registry.preprocess(
                self, channel, nick, message)
        except (TypeError, ValueError):
            pass

        # if not message.has_response:
        responses = registry.process(self, channel, nick, message)

        if responses:
            message = u'\n'.join(responses)
            self.msg(channel, message)

            if is_public:
                self.log_channel_message(channel, self.nickname, message)

        # Update last message
        self.last_message[channel][nick] = message
Example #2
0
    def on_message(self, element):
        """
        Handler for an XMPP message. This method handles logging channel messages (if it occurs
        on a public channel) as well as allowing the plugin manager to send the message to all
        registered plugins. Should the plugin manager yield a response, it will be sent back.

        :param message: A <message/> element, instance of `twisted.words.xish.domish.Element`
        """
        nick = self.parse_nick(element)
        channel = self.parse_channel(element)
        message = self.parse_message(element)

        # This will be empty for messages that were delayed
        if not message:
            return

        # If we don't ignore this, we'll get infinite replies
        if nick == self.nickname:
            return

        # Log the incoming message and notify message subscribers
        logger.debug('[<--] %s/%s - %s', channel, nick, message)
        is_public = self.is_public_channel(channel)

        # When we get a priv msg, the channel is our current nick, so we need to
        # respond to the user that is talking to us
        if is_public:
            # Only log convos on public channels
            self.log_channel_message(channel, nick, message)
        else:
            channel = nick

        # Some things should go first
        try:
            channel, nick, message = registry.preprocess(self, channel, nick, message)
        except (TypeError, ValueError):
            pass

        # if not message.has_response:
        responses = registry.process(self, channel, nick, message)

        if responses:
            message = u'\n'.join(responses)
            self.msg(channel, message)

            if is_public:
                self.log_channel_message(channel, self.nickname, message)

        # Update last message
        self.last_message[channel][nick] = message
Example #3
0
    def privmsg(self, user, channel, message):
        """
        Handler for an IRC message. This method handles logging channel messages (if it occurs
        on a public channel) as well as allowing the plugin manager to send the message to all
        registered plugins. Should the plugin manager yield a response, it will be sent back
        over IRC.

        :param user: IRC user string of the form ``{nick}!~{user}@{host}``
        :param channel: the channel from which the message came
        :param message: the message contents
        """
        user = self.parse_nick(user)
        message = message.strip()

        # Log the incoming message and notify message subscribers
        logger.debug('[<--] %s/%s - %s', channel, user, message)
        is_public = self.is_public_channel(channel)

        # When we get a priv msg, the channel is our current nick, so we need to
        # respond to the user that is talking to us
        if is_public:
            # Only log convos on public channels
            self.log_channel_message(channel, user, message)
        else:
            channel = user

        # Some things should go first
        try:
            channel, user, message = registry.preprocess(
                self, channel, user, message)
        except (TypeError, ValueError):
            pass

        # if not message.has_response:
        responses = registry.process(self, channel, user, message)

        if responses:
            message = u'\n'.join(responses)
            self.msg(channel, message)

            if is_public:
                self.log_channel_message(channel, self.nickname, message)

        # Update last message
        self.last_message[channel][user] = message
Example #4
0
File: irc.py Project: bigjust/helga
    def privmsg(self, user, channel, message):
        """
        Handler for an IRC message. This method handles logging channel messages (if it occurs
        on a public channel) as well as allowing the plugin manager to send the message to all
        registered plugins. Should the plugin manager yield a response, it will be sent back
        over IRC.

        :param user: IRC user string of the form ``{nick}!~{user}@{host}``
        :param channel: the channel from which the message came
        :param message: the message contents
        """
        user = self.parse_nick(user)
        message = message.strip()

        # Log the incoming message and notify message subscribers
        logger.debug('[<--] %s/%s - %s', channel, user, message)
        is_public = self.is_public_channel(channel)

        # When we get a priv msg, the channel is our current nick, so we need to
        # respond to the user that is talking to us
        if is_public:
            # Only log convos on public channels
            self.log_channel_message(channel, user, message)
        else:
            channel = user

        # Some things should go first
        try:
            channel, user, message = registry.preprocess(self, channel, user, message)
        except (TypeError, ValueError):
            pass

        # if not message.has_response:
        responses = registry.process(self, channel, user, message)

        if responses:
            message = u'\n'.join(responses)
            self.msg(channel, message)

            if is_public:
                self.log_channel_message(channel, self.nickname, message)

        # Update last message
        self.last_message[channel][user] = message
Example #5
0
    def slack_message(self, data):
        """
        Handler for an incoming Slack message event. This method allows the
        plugin manager to send the message to all registered plugins. Should
        the plugin manager yield a response, it will be sent back over Slack.

        :param data: dict from JSON received in WebSocket message
        """
        # Look up the human-readable name for this user ID.
        user = self._get_user_name(data['user'])

        # If we don't ignore this, we'll get infinite replies
        if user == self.nickname:
            return

        channel = self._get_channel_name(data['channel'])

        if channel:
            # If this was a legit channel, prefix it with a hash for later consistency
            channel = u'#{}'.format(channel)

        # I'm not sure if 100% of all messages have a "text" value. Use a blank
        # string fallback to be safe.
        message = data.get('text', '')

        message = self._parse_incoming_message(message)

        # Log the incoming message
        logger.debug('[<--] %s/%s - %s', channel, user, message)

        # Some things should go first
        try:
            channel, user, message = registry.preprocess(
                self, channel, user, message)
        except (TypeError, ValueError):
            pass

        # Update last message
        self.last_message[channel][user] = message

        responses = registry.process(self, channel, user, message)

        if responses:
            return self.msg(channel, u'\n'.join(responses))
Example #6
0
    def slack_message(self, data):
        """
        Handler for an incoming Slack message event. This method allows the
        plugin manager to send the message to all registered plugins. Should
        the plugin manager yield a response, it will be sent back over Slack.

        :param data: dict from JSON received in WebSocket message
        """
        # Look up the human-readable name for this user ID.
        user = self._get_user_name(data['user'])

        # If we don't ignore this, we'll get infinite replies
        if user == self.nickname:
            return

        channel = self._get_channel_name(data['channel'])

        if channel:
            # If this was a legit channel, prefix it with a hash for later consistency
            channel = u'#{}'.format(channel)

        # I'm not sure if 100% of all messages have a "text" value. Use a blank
        # string fallback to be safe.
        message = data.get('text', '')

        message = self._parse_incoming_message(message)

        # Log the incoming message
        logger.debug('[<--] %s/%s - %s', channel, user, message)

        # Some things should go first
        try:
            channel, user, message = registry.preprocess(self, channel, user, message)
        except (TypeError, ValueError):
            pass

        # Update last message
        self.last_message[channel][user] = message

        responses = registry.process(self, channel, user, message)

        if responses:
            return self.msg(channel, u'\n'.join(responses))
Example #7
0
    def test_preprocess(self):
        plugins = [Mock(), Mock(), Mock()]

        # Raising an exception shouldn't affect the others
        plugins[0].preprocess.return_value = ('foo', 'bar', self.snowman)
        plugins[1].preprocess.side_effect = Exception
        plugins[2].preprocess.return_value = ('abc', 'def', 'ghi')

        with patch.object(registry, 'prioritized') as prio:
            prio.return_value = plugins

            # Return value is what the last plugin returns
            retval = registry.preprocess(None, '#bots', 'me', self.snowman)
            assert retval == ('abc', 'def', 'ghi')

            # Should receive what the first plugin changed
            plugins[2].preprocess.assert_called_with(None, 'foo', 'bar', self.snowman)

            # Exception raising preprocess should have at least been called
            assert plugins[1].preprocess.called
Example #8
0
    def test_preprocess(self):
        plugins = [Mock(), Mock(), Mock()]

        # Raising an exception shouldn't affect the others
        plugins[0].preprocess.return_value = ('foo', 'bar', self.snowman)
        plugins[1].preprocess.side_effect = Exception
        plugins[2].preprocess.return_value = ('abc', 'def', 'ghi')

        with patch.object(registry, 'prioritized') as prio:
            prio.return_value = plugins

            # Return value is what the last plugin returns
            retval = registry.preprocess(None, '#bots', 'me', self.snowman)
            assert retval == ('abc', 'def', 'ghi')

            # Should receive what the first plugin changed
            plugins[2].preprocess.assert_called_with(None, 'foo', 'bar',
                                                     self.snowman)

            # Exception raising preprocess should have at least been called
            assert plugins[1].preprocess.called