"""Pure mail identity and attachment extraction for a complete export. A preview cutoff does not belong here. Duplicate attachment names stay distinct. """ from __future__ import annotations import hashlib import re from dataclasses import dataclass, field from email.parser import BytesParser _UID_PREFIX = "uid:" class MailIdentityError(Exception): def __init__(self, message: str) -> None: super().__init__(message) self.message = message @dataclass(frozen=True) class AttachmentBytes: filename: str media_type: str data: bytes sha256: str @dataclass class ParsedMail: message_id: str subject: str sender: str recipient: str copy: str text: str attachments: list[AttachmentBytes] = field(default_factory=list) missing: list[str] = field(default_factory=list) def header_message_id(raw: bytes) -> str: message = BytesParser().parsebytes(raw) return str(message.get("Message-ID") or "").strip() def identities_match(found: str | None, expected: str | None) -> bool: """A stored uid fallback matches only a message that still has no Message-ID.""" expected_text = (expected or "").strip() found_text = (found or "").strip() if expected_text.casefold().startswith(_UID_PREFIX): return found_text == "" if not expected_text or not found_text: return False return _bare(expected_text) == _bare(found_text) def choose_message(candidate: bytes | None, alternates: list[bytes], expected: str) -> bytes: """Keep the stored message when it still matches. Otherwise accept one unambiguous hit.""" if candidate is not None and identities_match(header_message_id(candidate), expected): return candidate if (expected or "").strip().casefold().startswith(_UID_PREFIX): raise MailIdentityError("Die gespeicherte Nachricht lässt sich nicht eindeutig wiederfinden.") matches = [raw for raw in alternates if identities_match(header_message_id(raw), expected)] if len(matches) == 1: return matches[0] if len(matches) > 1: raise MailIdentityError("Die Nachrichtenidentität ist mehrdeutig. Es wurde keine E-Mail übernommen.") raise MailIdentityError("Die gespeicherte Nachricht lässt sich nicht eindeutig wiederfinden.") def parse_mail(raw: bytes) -> ParsedMail: message = BytesParser().parsebytes(raw) attachments: list[AttachmentBytes] = [] missing: list[str] = [] used: set[str] = set() index = 0 for part in message.walk(): if part.is_multipart(): continue filename = part.get_filename() disposition = (part.get_content_disposition() or "").lower() if not filename and disposition != "attachment": continue index += 1 label = _header_text(filename) if filename else f"anlage-{index}.bin" payload = part.get_payload(decode=True) if not isinstance(payload, (bytes, bytearray)): missing.append(label or f"Anlage {index}") continue name = _unique_filename(label or f"anlage-{index}.bin", used) data = bytes(payload) attachments.append( AttachmentBytes( filename=name, media_type=(part.get_content_type() or "application/octet-stream")[:120], data=data, sha256=hashlib.sha256(data).hexdigest(), ) ) text = _body_text(message) return ParsedMail( message_id=str(message.get("Message-ID") or "").strip(), subject=_header_text(message.get("Subject")) or "(ohne Betreff)", sender=_header_text(message.get("From")), recipient=_header_text(message.get("To")), copy=_header_text(message.get("Cc")), text=text, attachments=attachments, missing=missing, ) def _bare(value: str) -> str: return value.strip().strip("<>").strip().casefold() def _header_text(value) -> str: if value is None: return "" text = str(value).replace("\r", " ").replace("\n", " ") return re.sub(r"\s+", " ", text).strip() def _unique_filename(name: str, used: set[str]) -> str: cleaned = name.replace("\\", "/").split("/")[-1].strip() or "anlage.bin" cleaned = cleaned[:180] if cleaned not in used: used.add(cleaned) return cleaned stem, dot, ext = cleaned.rpartition(".") if not dot: stem, ext = cleaned, "" else: ext = f".{ext}" number = 2 while True: candidate = f"{stem} ({number}){ext}" if candidate not in used: used.add(candidate) return candidate number += 1 def _body_text(message) -> str: plain: list[str] = [] html: list[str] = [] parts = message.walk() if message.is_multipart() else [message] for part in parts: if part.is_multipart() or part.get_content_disposition() == "attachment" or part.get_filename(): continue kind = part.get_content_type() payload = part.get_payload(decode=True) if isinstance(payload, (bytes, bytearray)): charset = part.get_content_charset() or "utf-8" text = bytes(payload).decode(charset, errors="replace").strip() else: raw = part.get_payload() text = raw.strip() if isinstance(raw, str) else "" if kind == "text/plain": plain.append(text) elif kind == "text/html": html.append(_html_text(text)) body = "\n".join(chunk for chunk in plain if chunk).strip() if not body: body = "\n".join(chunk for chunk in html if chunk).strip() return body def _html_text(value: str) -> str: text = re.sub(r"(?is)<(script|style)\b.*?>.*?", " ", value) text = re.sub(r"(?i)", "\n", text) text = re.sub(r"(?i)

", "\n", text) text = re.sub(r"<[^>]+>", " ", text) return re.sub(r"[ \t]{2,}", " ", text).strip()