From 17318815db29003a907b2c9def1855528513d8fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 16 Feb 2022 18:51:54 +0100 Subject: [PATCH] Take input when started as the main module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The encoding method that's used is kinda annoying because the data_dir tree is unreadable. Probably urlencode would have been better.. but now we'd need to migrate everything. This provides a quick way to get a JID converted to what we use as encoding. Signed-off-by: Maxime “pep” Buquet --- poezio_omemo/__init__.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) 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))