示例#1
0
    def signedOn(self):
        """Called once we've connected to a network"""
        # Give the factory access to the bot
        if self.factory is None:
            raise InternalError("Factory must be set on CardinalBot instance")

        self.logger.info("Signed on as %s" % self.nickname)

        # Give the factory the instance it created in case it needs to
        # interface for error handling or metadata retention.
        self.factory.cardinal = self

        # Setup PluginManager
        self.plugin_manager = PluginManager(self, self.factory.plugins,
                                            self.factory.blacklist)

        # Attempt to identify with NickServ, if a password was given
        if self.factory.password:
            self.logger.info("Attempting to identify with NickServ")
            self.msg("NickServ", "IDENTIFY %s" % (self.factory.password, ))

        # For servers that support it, set the bot mode
        #
        # FIXME: is it possible that the server renames us before this? if so,
        # the mode registration would fail erroneously
        self.send("MODE {} +B".format(self.nickname))

        # Attempt to join channels
        for channel in self.factory.channels:
            self.join(channel)

        # Set the uptime as now and grab the  boot time from the factory
        self.uptime = datetime.now()
        self.booted = self.factory.booted
示例#2
0
    def signedOn(self):
        """Called once we've connected to a network"""
        super().signedOn()

        self.logger.info("Signed on as %s" % self.nickname)

        # Give the factory the instance it created in case it needs to
        # interface for error handling or metadata retention.
        self.factory.cardinal = self

        # Setup PluginManager
        self.plugin_manager = PluginManager(self, self.factory.plugins,
                                            self.factory.blacklist)

        if self.factory.server_commands:
            self.logger.info("Sending server commands")
            for command in self.factory.server_commands:
                self.send(command)

        # Attempt to identify with NickServ, if a password was given
        if self.factory.password:
            self.logger.info("Attempting to identify with NickServ")
            self.msg("NickServ", "IDENTIFY %s" % (self.factory.password, ))

        # For servers that support it, set the bot mode
        self.send("MODE {} +B".format(self.nickname))

        # Attempt to join channels
        for channel in self.factory.channels:
            self.join(channel)

        # Set the uptime as now and grab the  boot time from the factory
        self.uptime = datetime.now()
        self.booted = self.factory.booted
示例#3
0
    def signedOn(self):
        """Called once we've connected to a network"""
        self.logger.info("Signed on as %s" % self.nickname)

        # Give the factory access to the bot
        if self.factory is None:
            raise InternalError("Factory must be set on CardinalBot instance")

        # Give the factory the instance it created in case it needs to
        # interface for error handling or metadata retention.
        self.factory.cardinal = self

        # Set the currently connected network
        self.network = self.factory.network

        # Attempt to identify with NickServ, if a password was given
        if self.factory.password:
            self.logger.info("Attempting to identify with NickServ")
            self.msg("NickServ", "IDENTIFY %s" % (self.factory.password, ))

        # Creates an instance of EventManager
        self.logger.debug("Creating new EventManager instance")
        self.event_manager = EventManager(self)

        # Register events
        try:
            self.event_manager.register("irc.invite", 2)
            self.event_manager.register("irc.privmsg", 3)
            self.event_manager.register("irc.notice", 3)
            self.event_manager.register("irc.nick", 2)
            self.event_manager.register("irc.mode", 3)
            self.event_manager.register("irc.topic", 3)
            self.event_manager.register("irc.join", 2)
            self.event_manager.register("irc.part", 3)
            self.event_manager.register("irc.kick", 4)
            self.event_manager.register("irc.quit", 2)
        except EventAlreadyExistsError:
            self.logger.error("Could not register core IRC events",
                              exc_info=True)

        # Create an instance of PluginManager, giving it an instance of ourself
        # to pass to plugins, as well as a list of initial plugins to load.
        self.logger.debug("Creating new PluginManager instance")
        self.plugin_manager = PluginManager(self, self.factory.plugins)

        # Attempt to join channels
        for channel in self.factory.channels:
            self.join(channel)

        # Set the uptime as now and grab the  boot time from the factory
        self.uptime = datetime.now()
        self.booted = self.factory.booted
示例#4
0
    def setup_method(self):
        mock_cardinal = self.cardinal = Mock(spec=CardinalBot)
        mock_cardinal.nickname = 'Cardinal'
        mock_cardinal.event_manager = self.event_manager = \
            EventManager(mock_cardinal)

        self.blacklist = {}

        self.plugin_manager = PluginManager(
            mock_cardinal, [],
            self.blacklist,
            _plugin_module_import_prefix='fake_plugins',
            _plugin_module_directory_suffix='cardinal/fixtures/fake_plugins')
示例#5
0
    def setup_method(self):
        mock_cardinal = self.cardinal = Mock(spec=CardinalBot)
        mock_cardinal.nickname = 'Cardinal'
        mock_cardinal.event_manager = self.event_manager = \
            EventManager(mock_cardinal)

        self.blacklist = {}

        plugins_directory = os.path.abspath(os.path.join(
            os.path.dirname(os.path.realpath(os.path.abspath(__file__))),
            '..',
            'cardinal/fixtures/fake_plugins',
        ))
        self.plugin_manager = PluginManager(
            mock_cardinal,
            [],
            self.blacklist,
            _plugin_module_import_prefix='fake_plugins',
            _plugin_module_directory=plugins_directory,
        )
示例#6
0
 def test_constructor(self):
     manager = PluginManager(Mock(), [], [])
     assert len(manager.plugins) == 0
示例#7
0
    def test_reload_for_error_in_constructor_succeeds(self):
        """Cardinal has a bug where constructor exceptions brick a plugin.

        Basically, the plugin won't be reloadable without a restart of the bot
        itself. This is obviously problematic for plugin development.

        This test aims to solve the problem and act as a regression test.
        """
        plugin = 'valid'
        with tempdir('cardinal_fixtures') as fixture_dir:
            with open(os.path.join(fixture_dir, '__init__.py'), 'w') as f:
                pass

            plugins_dir = os.path.join(fixture_dir, 'test_plugins')
            os.mkdir(plugins_dir)
            with open(os.path.join(plugins_dir, '__init__.py'), 'w') as f:
                pass

            plugin_dir = os.path.join(
                plugins_dir,
                plugin,
            )
            os.mkdir(plugin_dir)
            with open(os.path.join(plugin_dir, '__init__.py'), 'w') as f:
                pass

            # Write a plugin that will load successfully
            with open(
                os.path.join(
                    FIXTURE_PATH,
                    'fake_plugins',
                    'valid',
                    'plugin.py',
                ),
                'r'
            ) as f:
                valid_plugin = f.read()

            with open(os.path.join(plugin_dir, 'plugin.py'), 'w') as f:
                f.write(valid_plugin)

            # Make sure the fake_plugins dir is in the import path
            sys.path.insert(0, os.path.join(fixture_dir))
            try:
                # Load the plugin successfully
                plugin_manager = PluginManager(
                    self.cardinal,
                    [],
                    self.blacklist,
                    _plugin_module_import_prefix='test_plugins',
                    _plugin_module_directory=plugins_dir)

                self.assert_load_success(
                    [plugin],
                    plugin_manager=plugin_manager,
                )

                # Now overwrite with a plugin that will fail during load
                with open(os.path.join(plugin_dir, 'plugin.py'), 'w') as f:
                    f.write("""
class TestPlugin:
    def __init__(self):
        self.x = y  # this should raise


def setup():
    return TestPlugin()
""")

                # Verify the load failed
                self.assert_load_failed([plugin], plugin_manager)

                # Now back to a valid version
                with open(os.path.join(plugin_dir, 'plugin.py'), 'w') as f:
                    f.write(valid_plugin)

                # and then make sure the valid version does load successfully
                self.assert_load_success(
                    [plugin],
                    plugin_manager=plugin_manager,
                )
            finally:
                sys.path.pop(0)
示例#8
0
 def test_constructor_plugins_not_a_list_typeerror(self, plugins):
     with pytest.raises(TypeError):
         PluginManager(Mock(), plugins)
示例#9
0
 def test_constructor_loads_plugins(self, load):
     plugins = ['foo', 'bar']
     PluginManager(Mock(), plugins, [])
     load.assert_called_with(plugins)