Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions dodo/pgp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
Gpg = None


INLINE_BEGIN = "-----BEGIN PGP MESSAGE-----\n"
INLINE_END = "\n-----END PGP MESSAGE-----"


class GpgError(Exception):
pass

Expand Down Expand Up @@ -148,3 +152,25 @@ def encrypt(msg: email.message.EmailMessage) -> email.message.EmailMessage:
filename='encrypted.asc', cte='7bit')
encrypted_mail.attach(pgp_encrypted_part)
return encrypted_mail


def maybe_decrypt_inline(contents: str) -> str:
stripped = contents.strip("\n")
if not (stripped.startswith(INLINE_BEGIN) and stripped.endswith(INLINE_END)):
return contents

ensure_gpg()
assert Gpg is not None # for mypy
decrypted = Gpg.decrypt(contents)
raise_for_status(decrypted)
return decrypted.data.decode()


def decrypt_inline_attachment(data: bytes) -> bytes:
ensure_gpg()
assert Gpg is not None # for mypy

assert data.startswith(INLINE_BEGIN.encode()) and data.endswith(INLINE_END.encode())
decrypted = Gpg.decrypt(data)
raise_for_status(decrypted)
return decrypted.data
5 changes: 4 additions & 1 deletion dodo/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from . import util
from . import keymap
from . import panel
from . import pgp_util

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -145,7 +146,9 @@ def requestStarted(self, request: QWebEngineUrlRequestJob) -> None:
if text is not None:
break
else:
text = util.simple_escape(util.body_text(self.message_json))
text = util.body_text(self.message_json)
text = pgp_util.maybe_decrypt_inline(text)
text = util.simple_escape(text)
text = util.colorize_text(text)
text = util.linkify(text)

Expand Down
11 changes: 9 additions & 2 deletions dodo/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from bleach.linkifier import Linker

from . import settings
from . import pgp_util

def clean_html2html(s: str) -> str:
"""Sanitize the given HTML string
Expand Down Expand Up @@ -341,13 +342,19 @@ def write_attachments(m: dict) -> Tuple[str, List[str]]:
check=True,
)
filename = part["filename"]
if not proc.stdout:
data = proc.stdout

if not data:
print(f"Ignoring attachment {filename}: Got empty contents from notmuch")
continue

if filename.endswith(".pgp"):
filename = os.path.splitext(filename)[0]
data = pgp_util.decrypt_inline_attachment(data)

p = os.path.join(temp_dir, sanitize_filename(filename))
with open(p, 'wb') as att:
att.write(proc.stdout)
att.write(data)
file_paths.append(p)

if len(file_paths) == 0:
Expand Down