From f542a632433cb935e969a2d3c3d2f012fa9e4541 Mon Sep 17 00:00:00 2001 From: "Jannis M. Hoffmann" Date: Mon, 13 Mar 2023 23:12:59 +0100 Subject: Graceful fail for non-existing mail in raw_mail Encode unknown content types as base64 --- script/qmauth.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'script/qmauth.py') 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: -- cgit v1.2.3