diff --git a/poezio_omemo/__init__.py b/poezio_omemo/__init__.py index f90473c..c0ef929 100644 --- a/poezio_omemo/__init__.py +++ b/poezio_omemo/__init__.py @@ -28,10 +28,18 @@ from slixmpp.exceptions import IqError, IqTimeout from slixmpp_omemo import PluginCouldNotLoad, MissingOwnKey, NoAvailableSession from slixmpp_omemo import UndecidedException, UntrustedException, EncryptionPrepareException import slixmpp_omemo +from pathlib import Path log = logging.getLogger(__name__) +def jid_as_path(jid: JID) -> Path: + """Ensure JID in folder names don't contain illegal chars for the FS""" + jid_str = jid.bare.encode('utf-8') + digest = hashlib.sha256(jid_str).digest() + return Path(base64.b32encode(digest).decode('US-ASCII')) + + class Plugin(E2EEPlugin): """OMEMO (XEP-0384) Plugin""" @@ -70,12 +78,11 @@ class Plugin(E2EEPlugin): self.info = lambda i: self.api.information(i, 'Info') - # Ensure folder names don't contain illegal chars for the FS - jid_str = self.core.xmpp.boundjid.bare.encode('utf-8') - digest = hashlib.sha256(jid_str).digest() - hashed_jid = base64.b32encode(digest).decode('US-ASCII') - - data_dir = os.path.join(DATA_HOME, 'omemo', hashed_jid) + data_dir = os.path.join( + DATA_HOME, + 'omemo', + jid_as_path(self.core.xmpp.boundjid), + ) try: # Raise exception if folder exists so that we don't chmod again. @@ -228,3 +235,8 @@ class Plugin(E2EEPlugin): return None return None + + +if __name__ == '__main__': + jid = JID(input('JID: ')) + print(jid_as_path(jid))