def test_delivery_default_user_destination(self):
        response = Response(self.mock_firefly, self.user_message, self.hostmask, self.user_destination)

        message1 = 'Hello world! Message 1'
        message2 = 'performs an action test 2'
        message3 = 'Hello world! Notice 3'

        response.add_message(message1)
        response.add_action(message2)
        response.add_notice(message3)

        mock_message = mock.Mock()
        mock_action  = mock.Mock()
        mock_notice  = mock.Mock()

        self.mock_firefly.msg      = mock_message
        self.mock_firefly.describe = mock_action
        self.mock_firefly.notice   = mock_notice

        self.assertEqual(response._destination, response.DEST_USER)
        self.assertIs(response.destination, self.hostmask)

        response.send()

        mock_message.assert_called_once_with(self.hostmask, message1)
        mock_action.assert_called_once_with(self.hostmask, message2)
        mock_notice.assert_called_once_with(self.hostmask, message3)
    def test_add_notices(self):
        response = Response(self.mock_firefly, self.channel_message, self.hostmask, self.channel_destination)

        message1 = 'Hello world! Notice 1'
        message2 = 'Hello world! Notice 2'
        message3 = 'Hello world! Notice 3'

        response.add_notice(message1)
        response.add_notice(message2)
        response.add_notice(message3)

        self.assertListEqual(response._messages, [
            ('notice', message1, None),
            ('notice', message2, None),
            ('notice', message3, None)
        ])
        self.assertListEqual(response.messages, [])
        self.assertListEqual(response.actions, [])
        self.assertListEqual(response.notices, [message1, message2, message3])
    def test_add_actions(self):
        response = Response(self.mock_firefly, self.channel_message, self.hostmask, self.channel_destination)

        message1 = 'performs an action test 1'
        message2 = 'performs an action test 2'
        message3 = 'performs an action test 3'

        response.add_action(message1)
        response.add_action(message2)
        response.add_action(message3)

        self.assertListEqual(response._messages, [
            ('action', message1, None),
            ('action', message2, None),
            ('action', message3, None)
        ])
        self.assertListEqual(response.messages, [])
        self.assertListEqual(response.actions, [message1, message2, message3])
        self.assertListEqual(response.notices, [])
Example #4
0
    def _fire_event(self, event_name, has_reply=False, is_command=False, **kwargs):
        """
        Fire an IRC event.

        @type   event_name: C{str}
        @param  event_name: Name of the event to fire, see firefly.irc for a list of event constants

        @type   has_reply:  bool
        @param  has_reply:  Indicates that this event triggered a language response before firing

        @type   is_command: bool
        @param  is_command: Indicates that this event triggered a command before firing

        @param  kwargs:     Arbitrary event arguments
        """
        self._log.info('Firing event: %s', event_name)
        events = self.registry.get_events(event_name)

        for cls, func, params in events:
            # Commands ok?
            if is_command and not params['command_ok']:
                self._log.info('Event is not responding to command triggers, skipping')
                continue

            # Replies ok?
            if has_reply and not params['reply_ok']:
                self._log.info('Event is not responding to language triggers, skipping')
                continue

            self._log.info('Firing event: %s (%s); Params: %s', str(cls), str(func), str(params))

            # response = func(cls, **kwargs)
            message = kwargs.get('message')
            response = Response(
                self,
                message,
                message.source if message else None,
                message.destination if message and message.destination.is_channel else None
            )

            func(cls, response, **kwargs)
            response.send()
    def test_delivery_mixed_destinations(self):
        response = Response(self.mock_firefly, self.channel_message, self.hostmask, self.channel_destination)

        message1 = 'Hello world! Default message 1'
        message2 = 'performs an action channel test 2'
        message3 = 'Hello world! User notice 3'

        response.add_message(message1)
        response.add_action(message2, response.DEST_CHANNEL)
        response.add_notice(message3, response.DEST_USER)

        mock_message = mock.Mock()
        mock_action  = mock.Mock()
        mock_notice  = mock.Mock()

        self.mock_firefly.msg      = mock_message
        self.mock_firefly.describe = mock_action
        self.mock_firefly.notice   = mock_notice

        response.send()

        mock_message.assert_called_once_with(self.channel_destination, message1)
        mock_action.assert_called_once_with(self.channel_destination, message2)
        mock_notice.assert_called_once_with(self.hostmask, message3)
    def test_delivery_timestamps(self):
        response = Response(self.mock_firefly, self.channel_message, self.hostmask, self.channel_destination)

        message1 = 'Hello world! Message 1'
        message2 = 'performs an action test 2'
        message3 = 'Hello world! Notice 3'

        response.add_message(message1)
        response.add_action(message2)
        response.add_notice(message3)

        mock_message = mock.Mock()
        mock_action  = mock.Mock()
        mock_notice  = mock.Mock()

        self.mock_firefly.msg      = mock_message
        self.mock_firefly.describe = mock_action
        self.mock_firefly.notice   = mock_notice

        response.send()

        self.assertIsInstance(response._delivered[0][2], arrow.Arrow)
        self.assertIsInstance(response._delivered[1][2], arrow.Arrow)
        self.assertIsInstance(response._delivered[2][2], arrow.Arrow)
    def test_send_messages(self):
        response = Response(self.mock_firefly, self.channel_message, self.hostmask, self.channel_destination)

        message1 = 'Hello world! Message 1'
        message2 = 'performs an action test 2'
        message3 = 'Hello world! Notice 3'

        response.add_message(message1)
        response.add_action(message2)
        response.add_notice(message3)

        mock_message = mock.Mock()
        mock_action  = mock.Mock()
        mock_notice  = mock.Mock()

        self.mock_firefly.msg      = mock_message
        self.mock_firefly.describe = mock_action
        self.mock_firefly.notice   = mock_notice

        response.send()

        mock_message.assert_called_once_with(self.channel_destination, message1)
        mock_action.assert_called_once_with(self.channel_destination, message2)
        mock_notice.assert_called_once_with(self.channel_destination, message3)
Example #8
0
    def _fire_command(self, plugin, name, cmd_args, message):
        """
        Fire an IRC command.

        @type   plugin:     str
        @param  plugin:     Name of the command plugin

        @type   name:       str
        @param  name:       Name of the command

        @type   cmd_args:   list of str
        @param  cmd_args:   List of command arguments

        @type   message:    Message
        @param  message:    Command message container
        """
        self._log.info('Firing command: %s %s (%s)', plugin, name, str(cmd_args))
        cls, func, argparse, params = self.registry.get_command(plugin, name)

        # Make sure we have permission
        perm = params['permission']
        user = self.auth.check(message.source)

        if (perm != 'guest') and not user:
            error = 'You must be registered and authenticated in order to use this command.'

            if self.server.public_errors:
                self.msg(message.destination, error)
            else:
                self.notice(message.source, error)

            return

        if (perm == 'admin') and not user.is_admin:
            error = 'You do not have permission to use this command.'

            if self.server.public_errors:
                self.msg(message.destination, error)
            else:
                self.notice(message.source, error)

            return

        # Execute the command
        try:
            response = Response(
                self, message, message.source, message.destination if message.destination.is_channel else None
            )

            func(argparse.parse_args(cmd_args), response)
            response.send()
        except ArgumentParserError as e:
            self._log.info('Argument parser error: %s', e.message)

            usage    = style(argparse.format_usage().strip(), bold=True)
            desc     = ' -- {desc}'.format(desc=argparse.description.strip()) if argparse.description else ''
            help_msg = '({usage}){desc}'.format(usage=usage, desc=desc)

            # If this command was sent in a query, return the error now
            if message.destination.is_user:
                self.msg(message.source, e.message)
                self.msg(message.source, help_msg)
                return

            # Otherwise, check if we should send the messages as a notice or channel message
            if self.server.public_errors:
                self.msg(message.destination, e.message)
                self.msg(message.destination, help_msg)
            else:
                self.notice(message.source, e.message)
                self.notice(message.source, help_msg)