From e605917d7e2babaa0026a57ac8b266d4527df0a6 Mon Sep 17 00:00:00 2001 From: askiiart Date: Wed, 28 Feb 2024 10:11:43 -0600 Subject: [PATCH] Fix hkp imports --- gpg-email-helper.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/gpg-email-helper.py b/gpg-email-helper.py index 46a0d12..341f3f9 100644 --- a/gpg-email-helper.py +++ b/gpg-email-helper.py @@ -17,11 +17,11 @@ if 'gpg-email-helper' not in os.listdir(f'{XDG_DATA_HOME}'): with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log: log.write(f'\n\nRunning on {filename}\n') - # Untested as I'm missing a real email with this done properly - is this even supported by anything? if email.rfind('-----BEGIN PGP PUBLIC KEY BLOCK-----') != -1 and email.rfind('-----END PGP PUBLIC KEY BLOCK-----') != -1: public_key = email[email.rfind( '-----BEGIN PGP PUBLIC KEY BLOCK-----'):email.rfind('-----END PGP PUBLIC KEY BLOCK-----')+35] - public_key = public_key.replace('=3D', '=') # quoted-printable attachments have the equals sign escaped as =3D apparently + # quoted-printable attachments have the equals sign escaped as =3D apparently + public_key = public_key.replace('=3D', '=') log.write( f'Attempted to import key from email: {gpg.Context(armor=True).key_import(public_key.encode())}\n') @@ -82,11 +82,13 @@ with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log: pubkey = hkp4py.KeyServer( # This isn't perfect, it's a loose search # for example searching for 7C79FCBB8372E5DE5B17E09A90D4B9641E092971 on keyserver.ubuntu.com returns the wrong key. But it works 99% of the time. - f'hkp://{server}').search(fingerprint) + f'hkp://{server}').search(f'0x{fingerprint}') if pubkey != None: - log.write(f'{pubkey}\n') - gpg.Context().key_import(pubkey) - break + import_result = gpg.Context().key_import(pubkey[0].key.encode()) + log.write( + f'Attempted to import from {server}: {import_result}\n') + if type(import_result) == gpg.results.ImportResult: + break else: log.write('None\n') except Exception as e: