From 05e5bbfb1025ccc0362c59185f7ce0be5bdcc75b Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 23 Feb 2022 16:26:25 +0200 Subject: [PATCH] Log warnings if crypto dependencies aren't installed --- maubot/client.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/maubot/client.py b/maubot/client.py index 5495c5b..9601712 100644 --- a/maubot/client.py +++ b/maubot/client.py @@ -34,12 +34,16 @@ try: from mautrix.crypto import OlmMachine, StateStore as CryptoStateStore, PgCryptoStore from mautrix.util.async_db import Database as AsyncDatabase + class SQLStateStore(BaseSQLStateStore, CryptoStateStore): pass + + + crypto_import_error = None except ImportError as e: OlmMachine = CryptoStateStore = PgCryptoStore = AsyncDatabase = None SQLStateStore = BaseSQLStateStore - + crypto_import_error = e if TYPE_CHECKING: from .instance import PluginInstance @@ -96,7 +100,19 @@ class Client: @property def enable_crypto(self) -> bool: - return bool(OlmMachine and self.device_id and self.crypto_db) + if not self.device_id: + return False + elif not OlmMachine: + global crypto_import_error + self.log.warning("Client has device ID, but encryption dependencies not installed", + exc_info=crypto_import_error) + # Clear the stack trace after it's logged once to avoid spamming logs + crypto_import_error = None + return False + elif not self.crypto_db: + self.log.warning("Client has device ID, but crypto database is not prepared") + return False + return True def _prepare_crypto(self) -> None: self.crypto_store = PgCryptoStore(account_id=self.id, pickle_key="mau.crypto",