コード例 #1
0
ファイル: hangupsbot.py プロジェクト: onaclovtech/hangoutsbot
    def _on_connect(self):
        """handle connection/reconnection"""

        logger.debug("connected")

        plugins.tracking.set_bot(self)
        command.set_tracking(plugins.tracking)
        command.set_bot(self)

        self.tags = tagging.tags(self)
        self._handlers = handlers.EventHandler(self)
        handlers.handler.set_bot(self) # shim for handler decorator

        """
        monkeypatch plugins go heere
        # plugins.load(self, "monkeypatch.something")
        use only in extreme circumstances e.g. adding new functionality into hangups library
        """

        #self._user_list = yield from hangups.user.build_user_list(self._client)

        self._user_list, self._conv_list = (
            yield from hangups.build_user_conversation_list(self._client)
        )

        self.conversations = yield from permamem.initialise_permanent_memory(self)

        plugins.load(self, "commands.plugincontrol")
        plugins.load(self, "commands.basic")
        plugins.load(self, "commands.tagging")
        plugins.load(self, "commands.permamem")
        plugins.load(self, "commands.convid")
        plugins.load(self, "commands.loggertochat")
        plugins.load_user_plugins(self)

        self._conv_list.on_event.add_observer(self._on_event)
        self._client.on_state_update.add_observer(self._on_status_changes)

        logger.info("bot initialised")
コード例 #2
0
    def _on_connect(self, initial_data):
        """handle connection/reconnection"""

        logger.debug("connected")

        plugins.tracking.set_bot(self)
        command.set_tracking(plugins.tracking)
        command.set_bot(self)

        self.tags = tagging.tags(self)
        self._handlers = handlers.EventHandler(self)
        handlers.handler.set_bot(self)  # shim for handler decorator

        plugins.load(self, "monkeypatch.otr_support")

        self._user_list = yield from hangups.user.build_user_list(self._client,
                                                                  initial_data)

        self._conv_list = hangups.ConversationList(self._client,
                                                   initial_data.conversation_states,
                                                   self._user_list,
                                                   initial_data.sync_timestamp)

        self.conversations = yield from permamem.initialise_permanent_memory(self)

        plugins.load(self, "commands.plugincontrol")
        plugins.load(self, "commands.basic")
        plugins.load(self, "commands.tagging")
        plugins.load(self, "commands.permamem")
        plugins.load(self, "commands.convid")
        plugins.load(self, "commands.loggertochat")
        plugins.load_user_plugins(self)

        self._conv_list.on_event.add_observer(self._on_event)
        self._client.on_state_update.add_observer(self._on_status_changes)

        logger.info("bot initialised")
        yield from self.coro_send_message(CONTROL, _("Bot is back up"))
コード例 #3
0
    def _on_connect(self):
        """handle connection/reconnection"""

        logger.debug("connected")

        plugins.tracking.set_bot(self)
        command.set_tracking(plugins.tracking)
        command.set_bot(self)

        self.tags = tagging.tags(self)
        self._handlers = handlers.EventHandler(self)
        handlers.handler.set_bot(self)  # shim for handler decorator
        """
        monkeypatch plugins go heere
        # plugins.load(self, "monkeypatch.something")
        use only in extreme circumstances e.g. adding new functionality into hangups library
        """

        #self._user_list = yield from hangups.user.build_user_list(self._client)

        self._user_list, self._conv_list = (
            yield from hangups.build_user_conversation_list(self._client))

        self.conversations = yield from permamem.initialise_permanent_memory(
            self)

        plugins.load(self, "commands.plugincontrol")
        plugins.load(self, "commands.basic")
        plugins.load(self, "commands.tagging")
        plugins.load(self, "commands.permamem")
        plugins.load(self, "commands.convid")
        plugins.load(self, "commands.loggertochat")
        plugins.load_user_plugins(self)

        self._conv_list.on_event.add_observer(self._on_event)
        self._client.on_state_update.add_observer(self._on_status_changes)

        logger.info("bot initialised")
コード例 #4
0
    def _on_connect(self, initial_data):
        """handle connection/reconnection"""

        logger.debug("connected")

        plugins.tracking.set_bot(self)
        command.set_tracking(plugins.tracking)
        command.set_bot(self)

        self.tags = tagging.tags(self)
        self._handlers = handlers.EventHandler(self)
        handlers.handler.set_bot(self)  # shim for handler decorator

        plugins.load(self, "monkeypatch.otr_support")

        self._user_list = yield from hangups.user.build_user_list(
            self._client, initial_data)

        self._conv_list = hangups.ConversationList(
            self._client, initial_data.conversation_states, self._user_list,
            initial_data.sync_timestamp)

        self.conversations = yield from permamem.initialise_permanent_memory(
            self)

        plugins.load(self, "commands.plugincontrol")
        plugins.load(self, "commands.basic")
        plugins.load(self, "commands.tagging")
        plugins.load(self, "commands.permamem")
        plugins.load(self, "commands.convid")
        plugins.load(self, "commands.loggertochat")
        plugins.load_user_plugins(self)

        self._conv_list.on_event.add_observer(self._on_event)
        self._client.on_state_update.add_observer(self._on_status_changes)

        logger.info("bot initialised")
コード例 #5
0
    def run(self):
        """Connect to Hangouts and run bot"""
        def _login():
            """Login to Google account

            Authenticate with saved cookies or prompt for user credentials

            Returns:
                dict, a dict of cookies to authenticate at Google
            """
            try:
                return hangups.get_auth_stdin(self._cookies_path)

            except hangups.GoogleAuthError as err:
                logger.error("LOGIN FAILED: %s", repr(err))
                return False

        cookies = _login()
        if not cookies:
            logger.critical("Valid login required, exiting")
            sys.exit(1)

        # Start asyncio event loop
        loop = asyncio.get_event_loop()

        # initialise pluggable framework
        sinks.start(self)

        # initialise plugin and command registration
        plugins.tracking.set_bot(self)
        command.set_bot(self)

        # retries for the hangups longpolling request
        max_retries_longpolling = (self._max_retries
                                   if self._max_retries > 5 else 5)

        # Connect to Hangouts
        # If we are forcefully disconnected, try connecting again
        while self.__retry < self._max_retries:
            self.__retry += 1
            try:
                # (re)create Hangups client
                self._client = hangups.Client(cookies, max_retries_longpolling)
                self._client.on_connect.add_observer(self._on_connect)
                self._client.on_disconnect.add_observer(
                    lambda: logger.warning("Event polling stopped"))
                self._client.on_reconnect.add_observer(
                    lambda: logger.warning("Event polling continued"))

                loop.run_until_complete(self._client.connect())
            except SystemExit:
                raise
            except:                                 # pylint:disable=bare-except
                logger.exception("low-level error")
            else:
                logger.critical("bot is exiting")
                sys.exit(0)

            finally:
                logger.info("bot started unloading")
                loop.run_until_complete(self.__stop())
                loop.run_until_complete(plugins.unload_all(self))

                self.memory.flush()
                self.config.flush()
                logger.info("bot unloaded")

            if self.__retry == self._max_retries:
                # the final retry failed, do not delay the exit
                break

            delay = self.__retry * 5
            logger.info("Waiting %s seconds", delay)
            task = asyncio.ensure_future(asyncio.sleep(delay))

            # a KeyboardInterrupt should cancel the delay task instead
            self.stop = task.cancel

            try:
                loop.run_until_complete(task)
            except asyncio.CancelledError:
                return

            # restore the functionality to stop the bot on KeyboardInterrupt
            self.stop = self._stop
            logger.info("Trying to connect again (try %s of %s)",
                        self.__retry, self._max_retries)

        logger.critical("Maximum number of retries reached! Exiting...")
        sys.exit(1)