Lade Bilder aus E-Mails auf Wunsch über den Server nach und verkleinere den Kopf der Leseansicht auf dem Handy.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
075525ee17
commit
a3f328832e
11 files changed
+365
-11
No files matched your search
+18
-5
@@ -70,6 +70,8 @@ class ParsedMail:
|
||||
attachments: list[AttachmentBytes] = field(default_factory=list)
|
||||
missing: list[str] = field(default_factory=list)
|
||||
html: str = ""
|
||||
html_source: str = ""
|
||||
remote_images: list[str] = field(default_factory=list)
|
||||
notices: list[str] = field(default_factory=list)
|
||||
text_truncated: bool = False
|
||||
document_on: str | None = None
|
||||
@@ -168,7 +170,9 @@ def parse_mail(raw: bytes) -> ParsedMail:
|
||||
simplified = "\n".join(chunk for chunk in plain if chunk).strip()
|
||||
rendered = ""
|
||||
if html_parts:
|
||||
rendered = sanitize_html("\n".join(html_parts), _inline_images(attachments))
|
||||
source = "\n".join(html_parts)
|
||||
remote_found: list[str] = []
|
||||
rendered = sanitize_html(source, _inline_images(attachments), found=remote_found)
|
||||
if not simplified:
|
||||
simplified = simplified_text("\n".join(html_parts))
|
||||
if missing:
|
||||
@@ -183,6 +187,8 @@ def parse_mail(raw: bytes) -> ParsedMail:
|
||||
attachments=attachments,
|
||||
missing=missing,
|
||||
html=rendered,
|
||||
html_source=source if html_parts else "",
|
||||
remote_images=remote_found if html_parts else [],
|
||||
notices=notices,
|
||||
document_on=_message_date(message.get("Date")),
|
||||
)
|
||||
@@ -235,8 +241,11 @@ def simplified_text(value: str) -> str:
|
||||
return text.strip()
|
||||
|
||||
|
||||
def sanitize_html(value: str, images: dict[str, str] | None = None) -> str:
|
||||
"""Keep a formatted reading. Scripts, active content and remote loads stay out."""
|
||||
def sanitize_html(value: str, images: dict[str, str] | None = None, *, remote=None, found: list[str] | None = None) -> str:
|
||||
"""Keep a formatted reading. Scripts and active content stay out.
|
||||
|
||||
Pictures from other servers stay out too, unless `remote` turns an address into an inline picture.
|
||||
`found` collects the addresses that were met."""
|
||||
images = images or {}
|
||||
|
||||
class Cleaner(HTMLParser):
|
||||
@@ -252,7 +261,7 @@ def sanitize_html(value: str, images: dict[str, str] | None = None) -> str:
|
||||
return
|
||||
if self.skip or name not in _ALLOWED_TAGS:
|
||||
return
|
||||
rendered = _clean_attrs(name, attrs, images)
|
||||
rendered = _clean_attrs(name, attrs, images, remote, found)
|
||||
if rendered is None:
|
||||
return
|
||||
self.parts.append(f"<{name}{rendered}>")
|
||||
@@ -299,7 +308,7 @@ def _safe_style(value: str) -> str:
|
||||
return "; ".join(kept)
|
||||
|
||||
|
||||
def _clean_attrs(tag: str, attrs, images: dict[str, str]) -> str | None:
|
||||
def _clean_attrs(tag: str, attrs, images: dict[str, str], remote=None, found: list[str] | None = None) -> str | None:
|
||||
kept: list[str] = []
|
||||
for key, value in attrs:
|
||||
name = (key or "").lower()
|
||||
@@ -332,6 +341,10 @@ def _clean_attrs(tag: str, attrs, images: dict[str, str]) -> str | None:
|
||||
if source.lower().startswith("cid:"):
|
||||
token = source[4:].strip().strip("<>")
|
||||
source = images.get(token.casefold(), "")
|
||||
elif re.match(r"https://", source, re.IGNORECASE):
|
||||
if found is not None and source not in found:
|
||||
found.append(source)
|
||||
source = remote(source) if remote is not None else ""
|
||||
if not _DATA_IMAGE.match(source):
|
||||
continue
|
||||
kept.append(f' src="{source}"')
|
||||
|
||||
Reference in new issue
Block a user