summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis M. Hoffmann <jannis@fehcom.de>2023-03-13 23:12:59 +0100
committerJannis M. Hoffmann <jannis@fehcom.de>2023-03-13 23:12:59 +0100
commitf542a632433cb935e969a2d3c3d2f012fa9e4541 (patch)
tree90cb6dcbd838608f62f28520ec422496709b52ba
parenta365c5e671bcd631b21149a09895594b032c5119 (diff)
Graceful fail for non-existing mail in raw_mail
Encode unknown content types as base64
-rwxr-xr-xscript/qmauth.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/script/qmauth.py b/script/qmauth.py
index 3cad208..1dcc8e3 100755
--- a/script/qmauth.py
+++ b/script/qmauth.py
@@ -28,12 +28,12 @@ import logging
import re
from argparse import ArgumentParser
+from base64 import b64encode
from datetime import datetime
-from functools import cache
from glob import glob
from itertools import islice
from mailbox import Maildir, MaildirMessage
-from os import environ, getpid, path, setuid, stat
+from os import environ, getpid, path, setuid
from pathlib import Path
from pwd import getpwnam
from sys import exit as sysexit, stdout
@@ -219,7 +219,8 @@ def _get_body(mail):
ret = mail.get_content()
if ret.isascii():
return ret.decode(encoding='ascii')
- raise ValueError(f"unsupported content type in leaf part {mail.get_content_type()}")
+ else:
+ return b64encode(ret).decode(encoding='ascii')
if (mctype := mail.get_content_maintype()) == 'message':
msg = mail.get_content()
@@ -235,7 +236,7 @@ def _get_body(mail):
}
for part in mail.iter_parts():
head = _get_mime_head_info(part)
- if not head['content_disposition'] == 'attachment':
+ if head['content_disposition'] != 'attachment':
body = _get_body(part)
else:
body = None
@@ -279,11 +280,15 @@ def _descent(xx):
def raw_mail(f, subfolder, mid, path):
if subfolder:
f = f.get_folder(subfolder)
+
+ msg = f.get(mid, None)
+ if not msg:
+ raise QMAuthError("no such message", mid=mid)
pth = [int(seg) for seg in path.split('.')] if path else []
mail = {
'head': {"content_maintype": "message", "content_subtype": "rfc822"},
- 'body': f[mid],
+ 'body': msg,
}
for n in pth: