From 803eaad493e97e00ab449d3c00066a85f5d83c47 Mon Sep 17 00:00:00 2001 From: askiiart Date: Thu, 29 Feb 2024 10:22:33 -0600 Subject: [PATCH] Accidentally applied the wrong stash, OOPS --- README.md | 7 ++++--- gpg-email-helper.py | 38 ++++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 87a187e..8ceaac3 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,11 @@ Or to add it in the GUI: With a filtering rule, Claws Mail will only run gpg-email-helper when it receives a new email (that contains a signature). To run it on all emails instead, you can add the rule to pre/post-processing instead, but can take upwards of a minute to load every time you open your inbox, can cause the POP3/IMAP connection to time out, and often just crashes Claws Mail. Instead, I'd just just running it manually if you want to import public keys from all your old emails. -In bash, run this to run gpg-email-helper on all files with a signature: +Run this to run gpg-email-helper on all files with a signature: -```bash -for item in $(ls $HOME/Mail/inbox/); do if [ $(grep -c -- "-----BEGIN PGP SIGNATURE-----" $HOME/Mail/inbox/$item) != 0 ]; then python3 {SCRIPT LOCATION}/gpg-email-helper.py $HOME/Mail/inbox/$item; fi; done +```sh +bash -c 'for item in $(ls $HOME/Mail/inbox/); do if [ $(grep -c -- "-----BEGIN PGP SIGNATURE-----" $HOME/Mail/inbox/$item) != +0 ]; then python3 ~/gpg-email-helper/gpg-email-helper.py $HOME/Mail/inbox/$item; fi; done' ``` ## Todo diff --git a/gpg-email-helper.py b/gpg-email-helper.py index 998b141..ae5d62b 100644 --- a/gpg-email-helper.py +++ b/gpg-email-helper.py @@ -3,26 +3,29 @@ import hkp4py import os import sys import asyncio -import subprocess -async def get_pubkey(server, fingerprint, log): + +def do_hkp_stuff(server, fingerprint, log): # 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. - #return hkp4py.KeyServer(f'hkp://{server}').search(f'0x{fingerprint}') - results = await subprocess.getstatusoutput(f'gpg --keyserver {server} --recv-key {fingerprint}') - log.write(f'{server}: {results}\n') - print(f'{server}: {results}') - return results + try: + pubkey = hkp4py.KeyServer(f'hkp://{server}').search(f'0x{fingerprint}') + log.write(f'{server}: {pubkey}\n') + if pubkey != None: + import_result = gpg.Context().key_import(pubkey[0].key.encode()) + log.write(f'Attempted to import from {server}: {import_result}\n') + else: + log.write(f'{server}: None\n') + except Exception as e: + log.write(f'{server}: [ERROR] {e}\n\n') def get_first_real_result(results): for s in results: s = list(s) for task in s: - try: + if task != None: return task.result() - except asyncio.exceptions.InvalidStateError: - pass return None @@ -110,17 +113,16 @@ async def main(log): #'pgp.mit.edu' # still in operation but very unreliable ] + # multithreads it + # TODO: make it stop after a thread gets the key tasks = [ - asyncio.create_task(get_pubkey(server, fingerprint, log)) + asyncio.create_task( + asyncio.to_thread(do_hkp_stuff, server, fingerprint, log) + ) for server in keyservers ] - #tasks = set(tasks) - results = await asyncio.gather(tasks) - print(1) - print(get_first_real_result(results)) - print(3) - print(get_first_real_result(results)) - + tasks = set(tasks) + await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) except Exception as e: log.write(f'[ERROR] {e}\n\n')