Release govoplan-mail v0.1.27: stabilize credentials, folder encoding and transport progress
Module Package Release / publish-packages (push) Successful in 11s
Module Package Release / publish-packages (push) Successful in 11s
This commit is contained in:
@@ -5,12 +5,14 @@ from contextvars import ContextVar
|
||||
from dataclasses import dataclass
|
||||
from email.message import EmailMessage
|
||||
from email.utils import formatdate, make_msgid
|
||||
from threading import get_ident
|
||||
from typing import Any, Iterator
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from govoplan_core.core.mail import NotificationMailDeliveryRequest
|
||||
from govoplan_core.core.modules import ModuleContext
|
||||
from govoplan_mail.backend.config import ImapConfig
|
||||
from govoplan_mail.backend.mail_profiles import (
|
||||
MailProfileError,
|
||||
_assert_campaign_inherits_profile_credentials,
|
||||
@@ -38,6 +40,7 @@ from govoplan_mail.backend.recovery import (
|
||||
)
|
||||
from govoplan_mail.backend.sending.imap import (
|
||||
ImapAppendError,
|
||||
ImapBatchSession,
|
||||
ImapConfigurationError,
|
||||
append_message_to_sent,
|
||||
)
|
||||
@@ -72,6 +75,78 @@ class CampaignSmtpDeliveryResult:
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CampaignImapAppendResult:
|
||||
folder: str
|
||||
connection_sequence: int = 1
|
||||
session_reused: bool = False
|
||||
reconnect_count: int = 0
|
||||
|
||||
|
||||
class CampaignImapBatchState:
|
||||
"""Lazy transport reuse; authorization and recovery remain per message."""
|
||||
|
||||
def __init__(self, *, tenant_id: str, campaign_id: str):
|
||||
self.tenant_id = tenant_id
|
||||
self.campaign_id = campaign_id
|
||||
self._session: ImapBatchSession | None = None
|
||||
self._binding: tuple[Any, ...] | None = None
|
||||
self._previous_connections = 0
|
||||
self._previous_reconnects = 0
|
||||
self._closed = False
|
||||
self._owner_thread = get_ident()
|
||||
|
||||
@property
|
||||
def connection_count(self) -> int:
|
||||
return self._previous_connections + (self._session.connection_count if self._session else 0)
|
||||
|
||||
@property
|
||||
def reconnect_count(self) -> int:
|
||||
return self._previous_reconnects + (self._session.reconnect_count if self._session else 0)
|
||||
|
||||
def assert_scope(self, *, tenant_id: str, campaign_id: str) -> None:
|
||||
if (
|
||||
self._closed or get_ident() != self._owner_thread
|
||||
or tenant_id != self.tenant_id or campaign_id != self.campaign_id
|
||||
):
|
||||
raise ImapConfigurationError("The IMAP batch does not match this campaign scope")
|
||||
|
||||
def session_for(self, config: ImapConfig, *, binding: tuple[Any, ...]) -> ImapBatchSession:
|
||||
if self._closed:
|
||||
raise ImapConfigurationError("The IMAP batch is closed")
|
||||
if self._session is not None and (
|
||||
self._binding != binding or not self._session.matches_config(config)
|
||||
):
|
||||
self._release_session()
|
||||
if self._session is None:
|
||||
self._session = ImapBatchSession(config)
|
||||
self._binding = binding
|
||||
return self._session
|
||||
|
||||
def _release_session(self) -> None:
|
||||
if self._session is not None:
|
||||
self._previous_connections += self._session.connection_count
|
||||
self._previous_reconnects += self._session.reconnect_count
|
||||
self._session.close()
|
||||
self._session = None
|
||||
|
||||
def close(self) -> None:
|
||||
self._closed = True
|
||||
self._release_session()
|
||||
|
||||
|
||||
_ACTIVE_IMAP_BATCH: ContextVar[CampaignImapBatchState | None] = ContextVar(
|
||||
"govoplan_mail_active_imap_batch", default=None,
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def campaign_imap_batch(*, tenant_id: str, campaign_id: str) -> Iterator[CampaignImapBatchState]:
|
||||
"""Open no connection until an individual append passes its current checks."""
|
||||
state = CampaignImapBatchState(tenant_id=tenant_id, campaign_id=campaign_id)
|
||||
token = _ACTIVE_IMAP_BATCH.set(state)
|
||||
try:
|
||||
yield state
|
||||
finally:
|
||||
_ACTIVE_IMAP_BATCH.reset(token)
|
||||
state.close()
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
@@ -155,6 +230,7 @@ def _authorized_campaign_profile(
|
||||
campaign_id: str,
|
||||
profile_id: str,
|
||||
selection: dict[str, str | None] | None = None,
|
||||
credential_protocol: str | None = None,
|
||||
):
|
||||
profile = ensure_mail_profile_allowed_for_campaign(
|
||||
session,
|
||||
@@ -164,7 +240,7 @@ def _authorized_campaign_profile(
|
||||
require_active=True,
|
||||
)
|
||||
policy = effective_mail_profile_policy(session, tenant_id=tenant_id, campaign_id=campaign_id)
|
||||
_assert_campaign_inherits_profile_credentials(profile, policy, selection)
|
||||
_assert_campaign_inherits_profile_credentials(profile, policy, selection, protocol=credential_protocol)
|
||||
return profile
|
||||
|
||||
|
||||
@@ -343,6 +419,7 @@ def campaign_smtp_batch(
|
||||
campaign_id=campaign_id,
|
||||
profile_id=profile_id,
|
||||
selection=selection,
|
||||
credential_protocol="smtp",
|
||||
)
|
||||
except MailProfileError:
|
||||
raise
|
||||
@@ -444,6 +521,7 @@ def send_campaign_email_bytes(
|
||||
campaign_id=campaign_id,
|
||||
profile_id=profile_id,
|
||||
selection=selection,
|
||||
credential_protocol="smtp",
|
||||
)
|
||||
except MailProfileError:
|
||||
raise
|
||||
@@ -593,6 +671,9 @@ def append_campaign_message_to_sent(
|
||||
recovery_resource_type: str | None = None,
|
||||
recovery_resource_id: str | None = None,
|
||||
) -> CampaignImapAppendResult:
|
||||
batch = _ACTIVE_IMAP_BATCH.get()
|
||||
if batch is not None:
|
||||
batch.assert_scope(tenant_id=tenant_id, campaign_id=campaign_id)
|
||||
selection = _selection_payload(
|
||||
profile_id=profile_id,
|
||||
smtp_server_id=smtp_server_id,
|
||||
@@ -607,6 +688,7 @@ def append_campaign_message_to_sent(
|
||||
campaign_id=campaign_id,
|
||||
profile_id=profile_id,
|
||||
selection=selection,
|
||||
credential_protocol="imap",
|
||||
)
|
||||
except MailProfileError:
|
||||
raise
|
||||
@@ -681,6 +763,15 @@ def append_campaign_message_to_sent(
|
||||
)
|
||||
except MailProfileError:
|
||||
raise MailProfileError("Appending to Sent is blocked by the effective Mail policy.") from None
|
||||
batch_session = None
|
||||
if batch is not None:
|
||||
batch_session = batch.session_for(
|
||||
imap,
|
||||
binding=(
|
||||
tenant_id, campaign_id, profile_id, smtp_server_id, smtp_credential_id,
|
||||
imap_server_id, imap_credential_id, smtp_revision, imap_revision, folder,
|
||||
),
|
||||
)
|
||||
try:
|
||||
recovery = begin_provider_effect_recovery(
|
||||
kind="imap-append",
|
||||
@@ -701,7 +792,12 @@ def append_campaign_message_to_sent(
|
||||
outcome_unknown=True,
|
||||
)
|
||||
try:
|
||||
result = append_message_to_sent(message_bytes, imap_config=imap, folder=folder)
|
||||
if batch_session is None:
|
||||
result = append_message_to_sent(message_bytes, imap_config=imap, folder=folder)
|
||||
else:
|
||||
result = append_message_to_sent(
|
||||
message_bytes, imap_config=imap, folder=folder, batch_session=batch_session,
|
||||
)
|
||||
except ImapAppendError as exc:
|
||||
sanitized = _sanitized_imap_error(exc)
|
||||
if recovery is not None:
|
||||
@@ -728,11 +824,18 @@ def append_campaign_message_to_sent(
|
||||
try:
|
||||
recovery.succeed_imap(folder=result.folder)
|
||||
except Exception:
|
||||
if batch is not None:
|
||||
batch.close()
|
||||
raise ImapAppendError(
|
||||
"IMAP APPEND returned success, but durable recovery evidence could not be finalized.",
|
||||
outcome_unknown=True,
|
||||
) from None
|
||||
return CampaignImapAppendResult(folder=result.folder)
|
||||
return CampaignImapAppendResult(
|
||||
folder=result.folder,
|
||||
connection_sequence=batch.connection_count if batch else getattr(result, "connection_sequence", 1),
|
||||
session_reused=getattr(result, "session_reused", False),
|
||||
reconnect_count=batch.reconnect_count if batch else getattr(result, "reconnect_count", 0),
|
||||
)
|
||||
|
||||
|
||||
class MailCampaignCapability:
|
||||
@@ -745,6 +848,7 @@ class MailCampaignCapability:
|
||||
mail_profile_id_from_campaign_json = staticmethod(mail_profile_id_from_campaign_json)
|
||||
campaign_profile_delivery_summary = staticmethod(campaign_profile_delivery_summary)
|
||||
campaign_smtp_batch = staticmethod(campaign_smtp_batch)
|
||||
campaign_imap_batch = staticmethod(campaign_imap_batch)
|
||||
send_campaign_email_bytes = staticmethod(send_campaign_email_bytes)
|
||||
append_campaign_message_to_sent = staticmethod(append_campaign_message_to_sent)
|
||||
wait_for_rate_limit = staticmethod(wait_for_rate_limit)
|
||||
|
||||
@@ -517,9 +517,11 @@ def _credential_line(policy: dict[str, Any]) -> str:
|
||||
smtp_inherit = bool((policy.get("smtp_credentials") or {}).get("inherit", True))
|
||||
imap_inherit = bool((policy.get("imap_credentials") or {}).get("inherit", True))
|
||||
return (
|
||||
f"Credential inheritance: SMTP {'inherits' if smtp_inherit else 'requires local credentials'}; "
|
||||
f"IMAP {'inherits' if imap_inherit else 'requires local credentials'}. "
|
||||
"Campaign delivery is available only for protocols that inherit credentials from the selected Mail profile."
|
||||
f"Credential selection: SMTP {'allows a profile default or explicit Mail credential' if smtp_inherit else 'requires an explicit Mail credential'}; "
|
||||
f"IMAP {'allows a profile default or explicit Mail credential' if imap_inherit else 'requires an explicit Mail credential'}. "
|
||||
"Select the authorized server and credential in Campaign Mail settings when explicit selection is required. "
|
||||
"Secrets remain in Mail. Policy administrators configure each protocol under Credential selection; "
|
||||
"ancestor lower-level locks cannot be overridden."
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -197,6 +197,9 @@ GERMAN_STRUCTURED_TRANSLATIONS: dict[str, dict[str, Any]] = {'mail.bounce-proces
|
||||
'seine mailbox lesen.'],
|
||||
'steps': ['Öffnen Sie Mail und wählen Sie ein autorisiertes IMAP- '
|
||||
'oder JMAP-fähiges Profil.',
|
||||
'Neuladen rechts aktualisiert den gesamten aktuellen Kontext; '
|
||||
'Postfachwerkzeuge enthält gezielte Aktualisierungen und '
|
||||
'berechtigungsabhängige Rückläuferdiagnosen.',
|
||||
'Wählen Sie einen Ordner aus, überprüfen Sie das '
|
||||
'Live/Cache-Synchronisationslabel und stellen Sie den '
|
||||
'begrenzten Nachrichtenindex auf die Seite; JMAP-Suchen '
|
||||
|
||||
@@ -1430,19 +1430,26 @@ def _assert_campaign_inherits_profile_credentials(
|
||||
profile: MailServerProfile,
|
||||
policy: EffectiveMailProfilePolicy,
|
||||
selection: Mapping[str, str | None] | None = None,
|
||||
*,
|
||||
protocol: str | None = None,
|
||||
) -> None:
|
||||
for protocol in ("smtp", "imap"):
|
||||
if not _profile_has_transport(profile, protocol):
|
||||
# Authoring and complete transport summaries validate both protocols.
|
||||
# An effect capability can require only the protocol it actually receives
|
||||
# and uses; an SMTP call does not carry the campaign's IMAP selection.
|
||||
if protocol is not None and protocol not in {"smtp", "imap"}:
|
||||
raise MailProfileError("Credential policy protocol must be smtp or imap")
|
||||
for selected_protocol in ((protocol,) if protocol is not None else ("smtp", "imap")):
|
||||
if protocol is None and not _profile_has_transport(profile, selected_protocol):
|
||||
continue
|
||||
explicit_credential = (
|
||||
selection or {}
|
||||
).get(f"{protocol}_credential_id")
|
||||
).get(f"{selected_protocol}_credential_id")
|
||||
if (
|
||||
not _credential_policy_for_protocol(policy, protocol).inherit
|
||||
not _credential_policy_for_protocol(policy, selected_protocol).inherit
|
||||
and not explicit_credential
|
||||
):
|
||||
raise MailProfileError(
|
||||
f"Campaign delivery cannot use the selected profile because the effective {protocol.upper()} "
|
||||
f"Campaign delivery cannot use the selected profile because the effective {selected_protocol.upper()} "
|
||||
"credential policy requires an explicit credential selection for this campaign."
|
||||
)
|
||||
|
||||
|
||||
@@ -450,7 +450,7 @@ POP3_PROVIDER = ExternalProviderDeclaration(
|
||||
manifest = ModuleManifest(
|
||||
id="mail",
|
||||
name="Mail",
|
||||
version="0.1.26",
|
||||
version="0.1.27",
|
||||
required_capabilities=(CAPABILITY_AUTH_PRINCIPAL_RESOLVER, CAPABILITY_AUTH_PERMISSION_EVALUATOR),
|
||||
optional_dependencies=("campaigns", "addresses", "calendar", "postbox", "search"),
|
||||
provides_interfaces=(
|
||||
@@ -1059,7 +1059,7 @@ manifest = ModuleManifest(
|
||||
id="mail.profiles-and-policy",
|
||||
title="Mail profiles and policy hierarchy",
|
||||
summary="Mail sending and mailbox access use reusable SMTP/IMAP/JMAP profiles governed by an effective system, tenant, owner, and campaign policy.",
|
||||
body="The active policy decides whether users can only choose approved profiles or whether user, group, and campaign scopes may define additional reusable profiles. SMTP, IMAP, and JMAP host allow/deny rules are inherited independently. JMAP Session discovery fails closed when its advertised API origin differs unless an administrator explicitly allows that origin. Runtime documentation adds the current tenant posture when the actor may read mail profile policy.",
|
||||
body="The active policy decides whether users can only choose approved profiles or whether user, group, and campaign scopes may define additional reusable profiles. SMTP, IMAP, and JMAP host allow/deny rules are inherited independently. JMAP Session discovery fails closed when its advertised API origin differs unless an administrator explicitly allows that origin. Runtime documentation adds the current tenant posture when the actor may read mail profile policy. In the Mail profile policy editor, Credential selection separately controls SMTP and IMAP: allow a profile default or require an explicit authorized Mail-owned server and credential reference in Campaign Mail settings. Both choices keep secrets in Mail. Inherit policy leaves the local choice unset and follows the parent; it is not the same as allowing default credentials. System values are concrete, while lower scopes show local, effective, and source-path values. Allow override sets the matching allow_lower_level_limits protocol key; an ancestor lock cannot be changed or re-enabled below that scope. Campaign policy has no lower-level override controls. Scope-write permission and an unlocked workflow remain required. A failed write preserves the draft for explicit retry; a successful write followed by a dependent refresh failure remains saved and asks only to reload the display.",
|
||||
layer="configured",
|
||||
documentation_types=("admin",),
|
||||
audience=("tenant_admin", "mail_admin", "campaign_admin"),
|
||||
@@ -1090,7 +1090,17 @@ manifest = ModuleManifest(
|
||||
"oder ob auf Personen-, Gruppen- und Campaign-Ebene zusätzliche wiederverwendbare Profile angelegt werden können. "
|
||||
"Host-Freigaben und -Sperren für SMTP, IMAP und JMAP werden unabhängig vererbt. Ein von der JMAP-Session "
|
||||
"angekündigter API-Ursprung muss bei abweichendem Ursprung ausdrücklich freigegeben sein. "
|
||||
"Die Laufzeitdokumentation ergänzt die aktuelle Lage des Mandanten, wenn die handelnde Person die Mail-Profilrichtlinie lesen darf."
|
||||
"Die Laufzeitdokumentation ergänzt die aktuelle Lage des Mandanten, wenn die handelnde Person die Mail-Profilrichtlinie lesen darf. "
|
||||
"Im Mail-Profilrichtlinieneditor steuert Auswahl der Zugangsdaten SMTP und IMAP getrennt: Standard-Zugangsdaten "
|
||||
"des Profils zulassen oder ausdrückliche berechtigte Mail-Server- und Zugangsdatenverweise in den Mail-Einstellungen "
|
||||
"der Kampagne verlangen. Geheimnisse bleiben bei beiden Optionen in Mail. Richtlinie erben lässt den lokalen Wert "
|
||||
"offen und übernimmt die übergeordnete Entscheidung; dies ist nicht dasselbe wie Standard-Zugangsdaten zu erlauben. "
|
||||
"Systemwerte sind konkret; darunter werden lokale und wirksame Werte samt Richtlinienpfad angezeigt. Überschreiben "
|
||||
"zulassen setzt den passenden Protokollschlüssel in allow_lower_level_limits. Eine übergeordnete Sperre kann darunter "
|
||||
"weder geändert noch aufgehoben werden. Kampagnenrichtlinien haben keine Freigabe für weitere untere Bereiche. "
|
||||
"Schreibberechtigung für den jeweiligen Bereich und ein entsperrter Arbeitsablauf bleiben erforderlich. Ein fehlgeschlagener "
|
||||
"Schreibvorgang erhält den Entwurf für einen ausdrücklichen Wiederholungsversuch. Scheitert nach erfolgreichem Speichern "
|
||||
"nur die Aktualisierung abhängiger Daten, bleibt die Richtlinie gespeichert; lediglich die Anzeige muss neu geladen werden."
|
||||
),
|
||||
}
|
||||
},
|
||||
@@ -1111,7 +1121,8 @@ manifest = ModuleManifest(
|
||||
title="Map standard folders for an IMAP profile",
|
||||
summary="Store Inbox, Sent, Drafts, Trash, Archive, and Junk mappings on the reusable Mail profile and populate them from bounded IMAP discovery.",
|
||||
body=(
|
||||
"Folder names are profile/server metadata, not Campaign settings. An authorized profile administrator may enter names directly or use folder discovery; empty mappings retain automatic behavior. Existing imap.sent_folder values are presented and persisted as the Sent mapping, while the compatibility field remains synchronized for consumers that still read it. A Campaign-specific Sent-folder override remains authoritative for that Campaign and is not rewritten by profile discovery. Folder discovery lists provider-visible names without creating, renaming, moving, or deleting remote folders."
|
||||
"Folder names are profile/server metadata, not Campaign settings. An authorized profile administrator may enter names directly or use folder discovery; empty mappings retain automatic behavior. Existing imap.sent_folder values are presented and persisted as the Sent mapping, while the compatibility field remains synchronized for consumers that still read it. A Campaign-specific Sent-folder override remains authoritative for that Campaign and is not rewritten by profile discovery. Folder discovery lists provider-visible names without creating, renaming, moving, or deleting remote folders. "
|
||||
"Enter readable Unicode names such as Entwürfe, not modified UTF-7 wire names such as Entw&APw-rfe. Discovery decodes names before detecting folder roles; mailbox reads, status checks, and Sent APPEND encode and quote them for the active connection. Previously saved wire names are resolved against the same account's live folder list, with an exact readable-name match taking precedence over a legacy alias. Rediscover and save to replace old encoded configuration values explicitly; reads do not rewrite profiles. Invalid provider encodings fail explicitly instead of silently addressing a replacement folder."
|
||||
),
|
||||
layer="configured",
|
||||
documentation_types=("admin", "user"),
|
||||
@@ -1140,7 +1151,13 @@ manifest = ModuleManifest(
|
||||
"und gespeichert, während das Kompatibilitätsfeld für ältere Verbraucher synchron bleibt. Eine Campaign-spezifische "
|
||||
"Abweichung für den Gesendet-Ordner bleibt für diese Campaign maßgeblich und wird von der Profilerkennung nicht verändert. "
|
||||
"Die Erkennung listet nur die beim Anbieter sichtbaren Namen auf und legt keine externen Ordner an, benennt sie nicht um, "
|
||||
"verschiebt sie nicht und löscht sie nicht."
|
||||
"verschiebt sie nicht und löscht sie nicht. Lesbare Unicode-Namen wie Entwürfe eingeben, nicht die "
|
||||
"Modified-UTF-7-Übertragungsform Entw&APw-rfe. Die Erkennung dekodiert Namen vor der Rollenzuordnung; "
|
||||
"Postfachzugriffe, Statusabfragen und Gesendet-APPEND kodieren und maskieren sie für die aktive Verbindung. "
|
||||
"Früher gespeicherte Übertragungsformen werden anhand der aktuellen Ordnerliste desselben Kontos aufgelöst; "
|
||||
"ein exakt passender lesbarer Name hat Vorrang vor einem alten Alias. Erneute Erkennung und ausdrückliches "
|
||||
"Speichern ersetzen alte kodierte Konfigurationswerte; Lesezugriffe schreiben Profile nicht um. Fehlerhafte "
|
||||
"Anbieterkodierungen führen zu einer klaren Fehlermeldung statt unbemerkt einen Ersatzordner anzusprechen."
|
||||
),
|
||||
}
|
||||
},
|
||||
@@ -1213,7 +1230,7 @@ manifest = ModuleManifest(
|
||||
id="mail.profile-ownership-and-consumers",
|
||||
title="Mail owns transport profiles and credentials",
|
||||
summary="Other modules select authorized Mail profiles by stable identifier; they do not copy SMTP/IMAP settings or secrets.",
|
||||
body="Mail encrypts credentials, tests connections, evaluates profile scope and policy, and performs revision-gated transport effects without returning resolved configuration. Random persisted revisions rotate when normalized transport or account identity changes, while password-only rotation remains transparent to built business intent. Campaign stores only server.mail_profile_id plus sanitized delivery evidence. Campaign-local transport fields are rejected, and legacy campaign records fail closed until an explicit profile migration preserves the source audit record and updates an editable version.",
|
||||
body="Mail encrypts credentials, tests connections, evaluates profile scope and policy, and performs revision-gated transport effects without returning resolved configuration. Random persisted revisions rotate when normalized transport or account identity changes, while password-only rotation remains transparent to built business intent. Campaign stores only server.mail_profile_id plus sanitized delivery evidence. Campaign-local transport fields are rejected, and legacy campaign records fail closed until an explicit profile migration preserves the source audit record and updates an editable version. The shared credential editor resolves server names from authorized Mail metadata on opening, without a page refresh or reading secrets. Typing does not reload this catalogue; reopening refreshes it and retries temporary metadata failures. Inactive servers retain their status, and deleted or unauthorized selected references stay visible as unavailable rather than being removed. A displayed label never grants server-use permission.",
|
||||
layer="available",
|
||||
documentation_types=("admin", "user"),
|
||||
audience=("mail_user", "mail_admin", "campaign_manager", "campaign_sender"),
|
||||
@@ -1247,7 +1264,12 @@ manifest = ModuleManifest(
|
||||
"Passwortrotation bleibt für bereits aufgebauten fachlichen Willen transparent. Campaign speichert nur "
|
||||
"server.mail_profile_id und bereinigte Zustellnachweise. Campaign-lokale Transportfelder werden abgelehnt; "
|
||||
"ältere Campaign-Datensätze bleiben gesperrt, bis eine ausdrückliche Profilmigration den ursprünglichen "
|
||||
"Auditdatensatz bewahrt und eine bearbeitbare Version aktualisiert."
|
||||
"Auditdatensatz bewahrt und eine bearbeitbare Version aktualisiert. Der gemeinsame Zugangsdateneditor "
|
||||
"löst Servernamen beim Öffnen aus berechtigten Mail-Metadaten auf, ohne die Seite neu zu laden oder "
|
||||
"Geheimnisse auszulesen. Beim Tippen wird dieser Katalog nicht erneut geladen; erneutes Öffnen aktualisiert "
|
||||
"ihn und wiederholt vorübergehend fehlgeschlagene Metadatenabfragen. Inaktive Server behalten ihre "
|
||||
"Kennzeichnung. Gelöschte oder nicht berechtigte ausgewählte Verweise bleiben als nicht verfügbar sichtbar "
|
||||
"und werden nicht entfernt. Eine angezeigte Bezeichnung erteilt niemals die Berechtigung zur Servernutzung."
|
||||
),
|
||||
}
|
||||
},
|
||||
@@ -1323,10 +1345,14 @@ manifest = ModuleManifest(
|
||||
id="mail.workflow.read-mailbox",
|
||||
title="Read a permitted mailbox without changing it",
|
||||
summary="Choose an IMAP- or JMAP-enabled profile, browse folders, search or page messages, and inspect bounded content through the read-only mailbox surface.",
|
||||
body="Mailbox access requires both mailbox-read and profile-use authority for a profile visible in the actor's scope. IMAP retains its existing bounded list behavior. JMAP adds capability discovery, server-side text search, query-state cursors, and bounded Email/changes synchronization; an expired state explicitly requires a full refresh. Lists expose provider read/unread flags and provenance without mutating them. Message HTML is isolated and sanitized, while attachment metadata, unavailable content, and provider failures remain explicit. Listing folders or messages must not mark mail read, move it, delete it, or expose unbounded content.",
|
||||
body=(
|
||||
"Mailbox access requires both mailbox-read and profile-use authority for a profile visible in the actor's scope. IMAP retains its existing bounded list behavior. IMAP folder names appear as readable Unicode, including umlauts and literal ampersands, and are encoded for the active connection when opened. Refresh an already-loaded folder list after an upgrade; folder discovery does not rename remote folders. Folder icons expand or collapse the tree; labels select without changing expansion. Selecting a synthetic parent grouping highlights that group and clears message selection without opening an invented provider folder. JMAP adds capability discovery, server-side text search, query-state cursors, and bounded Email/changes synchronization; an expired state explicitly requires a full refresh. Lists expose provider read/unread flags and provenance without mutating them. Message HTML is isolated and sanitized, while attachment metadata, unavailable content, and provider failures remain explicit. Listing folders or messages must not mark mail read, move it, delete it, or expose unbounded content. "
|
||||
"The persistent workspace header keeps the profile selector, Mailbox tools, Help, and one right-aligned Reload available even without a profile or message selection. Reload first rechecks authorized profiles, then refreshes the current folder catalogue and bounded message page together; a selected message is reread only if still present. IMAP retains the current page; JMAP starts a fresh cursor chain on page one while retaining the search. A selected synthetic grouping refreshes only the folder catalogue, not an invented mailbox. Folder expansion is retained. Failed refreshes preserve usable loaded data with an explicit error and retry action, never present failure as an empty mailbox, and ignore late reads from a previous profile or tenant. Mailbox tools groups the optional profile-only, folder-only, and message-only refreshes separately from Bounce status. The latter remains visible with a permission explanation when bounce-read/manage authority is absent. These controls do not grant profile administration rights, run SMTP delivery, append mail, or change read/unread flags. "
|
||||
"Mailbox context reads bypass browser response reuse; bounded server-side indexes and their provenance remain governed by Mail. A failed pagination request restores the page and size that belong to the retained rows. Dismissing or changing the preview while Reload is pending takes precedence over its remembered selection. On narrow screens, scroll vertically through folders, messages, and preview inside the mailbox workspace; its action header remains visible."
|
||||
),
|
||||
layer="configured",
|
||||
documentation_types=("user",),
|
||||
audience=("mail_user",),
|
||||
documentation_types=("user", "admin"),
|
||||
audience=("mail_user", "mail_admin"),
|
||||
order=42,
|
||||
conditions=(
|
||||
DocumentationCondition(
|
||||
@@ -1345,12 +1371,29 @@ manifest = ModuleManifest(
|
||||
"summary": "Ein IMAP- oder JMAP-fähiges Profil auswählen, Ordner durchsuchen und begrenzte Nachrichteninhalte in der nur lesbaren Postfachoberfläche prüfen.",
|
||||
"body": (
|
||||
"Der Postfachzugriff erfordert sowohl Leseberechtigung für das Postfach als auch Nutzungsberechtigung für ein "
|
||||
"im Bereich der handelnden Person sichtbares Profil. IMAP behält sein bisheriges Verhalten; JMAP ergänzt "
|
||||
"im Bereich der handelnden Person sichtbares Profil. IMAP zeigt lesbare Unicode-Ordnernamen mit Umlauten "
|
||||
"und kaufmännischen Und-Zeichen; beim Öffnen werden sie für die aktive Verbindung kodiert. Eine bereits "
|
||||
"geladene Ordnerliste nach einem Update neu laden; die Erkennung benennt keine externen Ordner um. "
|
||||
"Ordnersymbole klappen den Baum auf oder zu; Beschriftungen wählen aus, ohne die Aufklappstellung zu ändern. "
|
||||
"Eine künstliche übergeordnete Gruppe wird hervorgehoben und leert die Nachrichtenauswahl, ohne einen erfundenen Anbieterordner zu öffnen. "
|
||||
"IMAP behält sein bisheriges Verhalten; JMAP ergänzt "
|
||||
"serverseitige Suche, zustandsgebundene Seitennavigation und begrenzte inkrementelle Änderungen. Listen zeigen die vom Anbieter gelieferten Gelesen-/Ungelesen-Kennzeichen "
|
||||
"und die Herkunft aus Livezugriff, Zwischenspeicher oder Aktualisierung, ohne diese Zustände zu verändern. HTML-Inhalte "
|
||||
"werden isoliert und bereinigt; Anlagen, Inline-Verweise, nicht verfügbare Inhalte und Providerfehler bleiben ausdrücklich sichtbar. "
|
||||
"Das Auflisten von Ordnern oder Nachrichten darf keine Nachricht als gelesen markieren, verschieben oder löschen und keine "
|
||||
"unbegrenzten Inhalte offenlegen."
|
||||
"unbegrenzten Inhalte offenlegen. "
|
||||
"Die dauerhafte Arbeitsbereichsleiste enthält Profilauswahl, Postfachwerkzeuge, Hilfe und genau einmal Neuladen rechts, auch ohne Profil- oder Nachrichtenauswahl. "
|
||||
"Neuladen prüft zuerst die berechtigten Profile und aktualisiert anschließend den aktuellen Ordnerkatalog und die begrenzte Nachrichtenseite gemeinsam. "
|
||||
"Eine noch vorhandene ausgewählte Nachricht wird erneut gelesen. IMAP behält die Seite; JMAP beginnt mit der bestehenden Suche eine neue Cursorfolge auf Seite eins. "
|
||||
"Bei einer ausgewählten künstlichen Gruppe wird nur der Ordnerkatalog gelesen, kein erfundenes Postfach. Aufklappstellungen bleiben erhalten. "
|
||||
"Fehlgeschlagene Aktualisierungen erhalten nutzbare geladene Daten mit ausdrücklicher Fehlermeldung und Wiederholungsmöglichkeit; sie erscheinen nicht als leeres Postfach. "
|
||||
"Verspätete Antworten eines vorherigen Profils oder Mandanten werden verworfen. Postfachwerkzeuge trennt gezieltes Aktualisieren von Profilen, Ordnern und Nachrichten vom Rückläuferstatus. "
|
||||
"Bei fehlender Rückläufer-Lese- oder Verwaltungsberechtigung bleibt dieser Einstieg mit Erklärung sichtbar und deaktiviert. "
|
||||
"Diese Aktionen vergeben keine Profilverwaltungsrechte, versenden oder hängen keine Nachrichten an und ändern keine Gelesen-/Ungelesen-Kennzeichen. "
|
||||
"Postfachkontext-Lesezugriffe umgehen die Wiederverwendung von Browserantworten; begrenzte serverseitige Indizes und deren Herkunft bleiben unter Kontrolle von Mail. "
|
||||
"Fehlgeschlagene Seitenwechsel stellen die zu den erhaltenen Zeilen gehörige Seite und Seitengröße wieder her. "
|
||||
"Das Schließen oder Wechseln der Vorschau während Neuladen hat Vorrang vor der zuvor gemerkten Auswahl. "
|
||||
"Auf schmalen Bildschirmen werden Ordner, Nachrichten und Vorschau innerhalb des Postfacharbeitsbereichs vertikal gescrollt; seine Aktionsleiste bleibt sichtbar."
|
||||
),
|
||||
}
|
||||
},
|
||||
@@ -1358,13 +1401,14 @@ manifest = ModuleManifest(
|
||||
"kind": "workflow",
|
||||
"route": "/mail",
|
||||
"screen": "Mail",
|
||||
"help_contexts": ["mail.list", "mail.mailbox"],
|
||||
"help_contexts": ["mail.list", "mail.mailbox", "mail.mailbox.reload", "mail.mailbox.tools"],
|
||||
"prerequisites": [
|
||||
"An active visible profile has IMAP or JMAP configured.",
|
||||
"You may both use that profile and read its mailbox.",
|
||||
],
|
||||
"steps": [
|
||||
"Open Mail and choose an authorized IMAP- or JMAP-enabled profile.",
|
||||
"Use the right-aligned Reload for the complete current context; Mailbox tools contains optional targeted refreshes and permission-aware bounce diagnostics.",
|
||||
"Select a folder, review the live/cached synchronization label, and page its bounded message index; JMAP searches run at the provider.",
|
||||
"Use the provider-derived read/unread indicator, then open only the message needed for the task.",
|
||||
"Switch between safe plain-text and isolated HTML views as needed, and review attachment or unavailable-content details before closing the preview.",
|
||||
@@ -1431,7 +1475,11 @@ manifest = ModuleManifest(
|
||||
id="mail.reference.campaign-delivery-contract",
|
||||
title="Integrate Campaign through the Mail delivery contract",
|
||||
summary="Campaign freezes a Mail profile reference and opaque revision; Mail re-authorizes, revision-checks, resolves credentials, and performs the effect in one call.",
|
||||
body="The mail.campaign_delivery 0.2 contract never returns decrypted credentials or resolved SMTP/IMAP configuration. Mail compares the expected random transport revision before decrypting protocol-specific credentials and returns only bounded sanitized outcomes. Synchronous batches authorize the complete recipient set and preflight DNS, egress, connectivity, TLS, and authentication before the first effect. Mail reuses the bounded connection when deployment policy permits, health-checks it before reuse, and reconnects before the next message when a stale connection is detected. A connection loss after DATA begins remains outcome-unknown and is never replayed. Systemic authentication, sender, or connectivity failures carry stable reason codes so Campaign pauses remaining queued work and shows connection, reconnect, failure, and pause progress. Campaign owns ordinary recipient jobs; report messages use Mail's encrypted idempotent delivery-command and attempt ledger. Every current SMTP and Sent-folder attempt passes a stable effect identifier into a Mail-owned Core recovery operation before provider contact. Effect-start evidence prevents blind redelivery, unknown outcomes require explicit reconciliation, and raw recipient refusals require Mail diagnostic authority. Mail outbox dispatch and retention scans are partitioned by tenant entitlement, so disabling Mail leaves accepted commands and evidence untouched for operator resolution.",
|
||||
body=(
|
||||
"The mail.campaign_delivery 0.2 contract never returns decrypted credentials or resolved SMTP/IMAP configuration. Mail compares the expected random transport revision before decrypting protocol-specific credentials and returns only bounded sanitized outcomes. Synchronous batches authorize the complete recipient set and preflight DNS, egress, connectivity, TLS, and authentication before the first effect. Mail reuses the bounded connection when deployment policy permits, health-checks it before reuse, and reconnects before the next message when a stale connection is detected. A connection loss after DATA begins remains outcome-unknown and is never replayed. Systemic authentication, sender, or connectivity failures carry stable reason codes so Campaign pauses remaining queued work and shows connection, reconnect, failure, and pause progress. Campaign owns ordinary recipient jobs; report messages use Mail's encrypted idempotent delivery-command and attempt ledger. Every current SMTP and Sent-folder attempt passes a stable effect identifier into a Mail-owned Core recovery operation before provider contact. Effect-start evidence prevents blind redelivery, unknown outcomes require explicit reconciliation, and raw recipient refusals require Mail diagnostic authority. Mail outbox dispatch and retention scans are partitioned by tenant entitlement, so disabling Mail leaves accepted commands and evidence untouched for operator resolution."
|
||||
" Runtime credential-selection checks are protocol-scoped: SMTP batch and single-message delivery enforce SMTP policy; Sent-folder append enforces IMAP policy. A valid explicit SMTP selection must not fail merely because the SMTP call does not carry an unrelated explicit IMAP credential, and vice versa. Full campaign authoring validation and complete profile summaries still check both configured protocols. Missing explicit credentials for the selected protocol, inactive or unauthorized bindings and stale transport revisions remain blocked before credential decryption or provider contact. A successful Mail connection test proves only that selected connection/login, not later campaign authorization or recipient acceptance. Correcting this runtime check changes no saved policy, credentials, build/review evidence, or delivery status and does not send messages automatically."
|
||||
" The optional campaign_imap_batch context reuses a bounded authenticated IMAP connection for sequential APPENDs, never MULTIAPPEND or parallel effects. It opens lazily after each message's current authorization, both frozen revisions, IMAP-only credential resolution and recovery checks; permissions and recovery evidence are never cached. Connection-local Sent-folder discovery preserves the provider's original Unicode wire names. Different campaign/tenant scopes are rejected, and changed profile, references, folder or resolved credentials cannot reuse the prior connection. Defaults are 100 messages or 300 seconds per connection, an idle NOOP after 30 seconds, and one extra connection attempt before APPEND. GOVOPLAN_IMAP_BATCH_REUSE can disable reuse; MAX_MESSAGES, MAX_AGE_SECONDS, IDLE_HEALTH_CHECK_SECONDS and RECONNECT_ATTEMPTS with the same prefix bound deployment behavior as documented in the handbook. There is no automatic APPEND replay after transmission starts; unknown outcomes require mailbox reconciliation without SMTP resend. Accepted recovery-evidence finalization failure closes the batch; logout failure does not reverse acceptance. Per-message connection/reuse/reconnect counters expose no secrets. Older optional Mail providers retain single-message behavior."
|
||||
),
|
||||
layer="available",
|
||||
documentation_types=("admin", "user"),
|
||||
audience=("integrator", "campaign_manager", "campaign_sender", "release_reviewer"),
|
||||
@@ -1467,6 +1515,8 @@ manifest = ModuleManifest(
|
||||
"Wiederzustellung; unbekannte Ergebnisse erfordern ausdrückliche Abstimmung, und rohe Empfängerablehnungen erfordern Mail-Diagnoseberechtigung. "
|
||||
"Mail-Ausgangsverarbeitung und Aufbewahrungsläufe werden nach Mandantenberechtigung partitioniert. Das Deaktivieren von Mail lässt bereits "
|
||||
"angenommene Befehle und Nachweise daher zur Klärung durch den Betrieb unverändert bestehen."
|
||||
" Zur Laufzeit gelten Zugangsdaten-Auswahlregeln protokollbezogen: SMTP-Stapel und einzelne SMTP-Zustellungen prüfen die SMTP-Richtlinie; die Ablage im Gesendet-Ordner prüft die IMAP-Richtlinie. Eine gültige ausdrückliche SMTP-Auswahl darf nicht daran scheitern, dass der reine SMTP-Aufruf keine getrennte IMAP-Zugangsdatenkennung übergibt; umgekehrt gilt dasselbe. Vollständige Kampagnenvalidierung und Profilzusammenfassungen prüfen weiterhin beide konfigurierten Protokolle. Fehlende ausdrückliche Zugangsdaten des genutzten Protokolls, inaktive oder unberechtigte Bindungen und veraltete Transportrevisionen bleiben vor Entschlüsselung oder Providerkontakt gesperrt. Ein erfolgreicher Mail-Verbindungstest belegt nur diese Verbindung und Anmeldung, nicht spätere Kampagnenberechtigung oder Empfängerannahme. Die Korrektur verändert keine gespeicherte Richtlinie, Zugangsdaten, Build-/Prüfnachweise oder Zustellzustände und versendet nicht automatisch."
|
||||
" Der optionale Kontext campaign_imap_batch verwendet eine begrenzte authentifizierte IMAP-Verbindung für sequenzielle APPENDs erneut, niemals MULTIAPPEND oder parallele Wirkungen. Er öffnet erst nach der aktuellen Berechtigungsprüfung, Prüfung beider eingefrorenen Revisionen, alleiniger Auflösung der IMAP-Zugangsdaten und Wiederherstellungsprüfung jeder Nachricht; Berechtigungen und Nachweise werden nicht zwischengespeichert. Die verbindungslokale Gesendet-Ordner-Erkennung erhält die ursprünglichen Unicode-Übertragungsnamen des Providers. Andere Mandanten oder Kampagnen werden abgelehnt; geänderte Profile, Referenzen, Ordner oder aufgelöste Zugangsdaten dürfen die bisherige Verbindung nicht weiterverwenden. Standardwerte sind 100 Nachrichten oder 300 Sekunden je Verbindung, NOOP nach 30 Sekunden Leerlauf und ein zusätzlicher Verbindungsversuch vor APPEND. GOVOPLAN_IMAP_BATCH_REUSE kann die Wiederverwendung deaktivieren; MAX_MESSAGES, MAX_AGE_SECONDS, IDLE_HEALTH_CHECK_SECONDS und RECONNECT_ATTEMPTS mit demselben Präfix begrenzen das Betriebsverhalten gemäß Handbuch. Nach Übertragungsbeginn wird APPEND niemals automatisch wiederholt; unbekannte Ergebnisse erfordern Postfachabgleich ohne erneuten SMTP-Versand. Ein Fehler beim Abschluss des Annahmenachweises schließt den Stapel; ein Abmeldefehler macht die Annahme nicht rückgängig. Verbindungs-, Wiederverwendungs- und Neuverbindungszähler enthalten keine Geheimnisse. Ältere optionale Mail-Anbieter behalten das Einzelaufrufverhalten."
|
||||
),
|
||||
}
|
||||
},
|
||||
@@ -1475,7 +1525,7 @@ manifest = ModuleManifest(
|
||||
"route": "/campaigns/{campaign_id}/mail-settings",
|
||||
"screen": "Campaign Mail settings",
|
||||
"section": "Mail-owned profile and transport boundary",
|
||||
"verification": "Prove stale revisions fail before credential decryption, batch preflight fails before DATA, two messages reuse one healthy connection, a stale connection reconnects before the next message, post-DATA disconnect is never replayed, systemic failures pause remaining jobs, provider details are sanitized, and the interface/version gate passes.",
|
||||
"verification": "Prove stale revisions fail before credential decryption, batch preflight fails before DATA, two messages reuse one healthy connection, a stale connection reconnects before the next message, post-DATA disconnect is never replayed, systemic failures pause remaining jobs, provider details are sanitized, and the interface/version gate passes. For IMAP, prove one login and folder discovery for sequential APPENDs, count/age rotation, changed authorization or revisions block the next message before decryption, per-message recovery remains independent, unknown APPEND is never replayed, and nested/scoped contexts clean up safely.",
|
||||
"related_topic_ids": [
|
||||
"mail.profile-ownership-and-consumers",
|
||||
"campaigns.mail-profile-user-journey",
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import imaplib
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
import ssl
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
from email import policy
|
||||
from email.message import EmailMessage
|
||||
from email.parser import BytesParser
|
||||
from typing import Any
|
||||
from threading import Lock
|
||||
from typing import Any, Iterator
|
||||
|
||||
from govoplan_core.security.outbound_http import (
|
||||
OutboundHttpError,
|
||||
@@ -212,6 +217,9 @@ class ImapAppendResult:
|
||||
folder: str
|
||||
bytes_appended: int
|
||||
response: str | None = None
|
||||
connection_sequence: int = 1
|
||||
session_reused: bool = False
|
||||
reconnect_count: int = 0
|
||||
|
||||
|
||||
def _require_imap_config(config: ImapConfig) -> tuple[str, int]:
|
||||
@@ -276,7 +284,75 @@ def _unquote_imap_token(value: str) -> str:
|
||||
return value
|
||||
|
||||
|
||||
def _extract_mailbox_name(list_response_line: bytes | str) -> tuple[str, set[str]] | None:
|
||||
def _encode_mailbox_name(name: str) -> str:
|
||||
"""Encode a Unicode mailbox using RFC 3501 section 5.1.3 modified UTF-7."""
|
||||
|
||||
result: list[str] = []
|
||||
pending: list[str] = []
|
||||
|
||||
def flush() -> None:
|
||||
if pending:
|
||||
encoded = base64.b64encode("".join(pending).encode("utf-16-be"))
|
||||
result.append("&" + encoded.decode("ascii").rstrip("=").replace("/", ",") + "-")
|
||||
pending.clear()
|
||||
|
||||
for char in name:
|
||||
if " " <= char <= "~":
|
||||
flush()
|
||||
result.append("&-" if char == "&" else char)
|
||||
else:
|
||||
pending.append(char)
|
||||
flush()
|
||||
return "".join(result)
|
||||
|
||||
|
||||
def _decode_mailbox_name(name: str, *, utf8_enabled: bool = False) -> str:
|
||||
"""Decode mailbox names only, never message bodies or arbitrary IMAP text.
|
||||
|
||||
UTF8=ACCEPT changes mailbox names to UTF-8 (RFC 6855 section 3); an
|
||||
advertised capability alone does not activate that mode. Invalid provider
|
||||
names fail explicitly rather than silently selecting a replacement name.
|
||||
"""
|
||||
|
||||
if utf8_enabled:
|
||||
return name
|
||||
result: list[str] = []
|
||||
position = 0
|
||||
try:
|
||||
name.encode("ascii")
|
||||
while position < len(name):
|
||||
if name[position] != "&":
|
||||
result.append(name[position])
|
||||
position += 1
|
||||
continue
|
||||
end = name.find("-", position + 1)
|
||||
if end < 0:
|
||||
raise ValueError("unterminated modified UTF-7 shift")
|
||||
encoded = name[position + 1:end]
|
||||
if not encoded:
|
||||
result.append("&")
|
||||
else:
|
||||
if not re.fullmatch(r"[A-Za-z0-9+,]+", encoded):
|
||||
raise ValueError("invalid modified UTF-7 alphabet")
|
||||
raw = base64.b64decode(encoded.replace(",", "/") + "=" * (-len(encoded) % 4), validate=True)
|
||||
decoded = raw.decode("utf-16-be")
|
||||
if any(" " <= char <= "~" for char in decoded):
|
||||
raise ValueError("modified UTF-7 encodes a printable ASCII character")
|
||||
canonical = base64.b64encode(raw).decode("ascii").rstrip("=").replace("/", ",")
|
||||
if canonical != encoded:
|
||||
raise ValueError("non-canonical modified UTF-7 base64")
|
||||
result.append(decoded)
|
||||
position = end + 1
|
||||
except (ValueError, UnicodeError, binascii.Error) as exc:
|
||||
raise ImapAppendError("IMAP server returned an invalid mailbox name encoding", temporary=False) from exc
|
||||
return "".join(result)
|
||||
|
||||
|
||||
def _extract_wire_mailbox_name(
|
||||
list_response_line: bytes | str | tuple[bytes, bytes] | None,
|
||||
*,
|
||||
utf8_enabled: bool = False,
|
||||
) -> tuple[str, set[str]] | None:
|
||||
r"""Best-effort parser for IMAP LIST response lines.
|
||||
|
||||
RFC 3501 LIST responses contain attributes, hierarchy delimiter, then mailbox
|
||||
@@ -292,7 +368,18 @@ def _extract_mailbox_name(list_response_line: bytes | str) -> tuple[str, set[str
|
||||
blindly taking the last quoted value.
|
||||
"""
|
||||
|
||||
line = _decode_item(list_response_line).strip()
|
||||
if list_response_line is None:
|
||||
return None
|
||||
literal = None
|
||||
if isinstance(list_response_line, tuple):
|
||||
list_response_line, literal = list_response_line
|
||||
try:
|
||||
line = (
|
||||
list_response_line.decode("utf-8" if utf8_enabled else "ascii")
|
||||
if isinstance(list_response_line, bytes) else list_response_line
|
||||
).strip()
|
||||
except UnicodeError as exc:
|
||||
raise ImapAppendError("IMAP server returned an invalid mailbox name encoding", temporary=False) from exc
|
||||
match = re.match(
|
||||
r'^\((?P<flags>[^)]*)\)\s+'
|
||||
r'(?P<delimiter>"(?:[^"\\]|\\.)*"|NIL|[^\s]+)\s+'
|
||||
@@ -303,6 +390,14 @@ def _extract_mailbox_name(list_response_line: bytes | str) -> tuple[str, set[str
|
||||
if match:
|
||||
flags = {part.lower() for part in match.group("flags").split()}
|
||||
mailbox = _unquote_imap_token(match.group("mailbox"))
|
||||
if literal is not None:
|
||||
literal_size = re.fullmatch(r"\{(\d+)\+?\}", match.group("mailbox"))
|
||||
if not literal_size or int(literal_size.group(1)) != len(literal):
|
||||
raise ImapAppendError("IMAP server returned an invalid mailbox name literal", temporary=False)
|
||||
try:
|
||||
mailbox = literal.decode("utf-8" if utf8_enabled else "ascii")
|
||||
except UnicodeError as exc:
|
||||
raise ImapAppendError("IMAP server returned an invalid mailbox name encoding", temporary=False) from exc
|
||||
if mailbox:
|
||||
return mailbox, flags
|
||||
return None
|
||||
@@ -318,6 +413,38 @@ def _extract_mailbox_name(list_response_line: bytes | str) -> tuple[str, set[str
|
||||
return None
|
||||
|
||||
|
||||
def _extract_mailbox_name(
|
||||
list_response_line: bytes | str | tuple[bytes, bytes] | None,
|
||||
*,
|
||||
utf8_enabled: bool = False,
|
||||
) -> tuple[str, set[str]] | None:
|
||||
extracted = _extract_wire_mailbox_name(list_response_line, utf8_enabled=utf8_enabled)
|
||||
if extracted is None:
|
||||
return None
|
||||
name, flags = extracted
|
||||
return _decode_mailbox_name(name, utf8_enabled=utf8_enabled), flags
|
||||
|
||||
|
||||
def _parsed_mailbox_listing(client: imaplib.IMAP4, data: list[Any]) -> list[tuple[str, set[str]]]:
|
||||
utf8_enabled = getattr(client, "utf8_enabled", False) is True
|
||||
wire_names: dict[str, str] = {}
|
||||
parsed: list[tuple[str, set[str]]] = []
|
||||
for item in data:
|
||||
extracted = _extract_wire_mailbox_name(item, utf8_enabled=utf8_enabled)
|
||||
if extracted is None:
|
||||
continue
|
||||
wire_name, flags = extracted
|
||||
name = _decode_mailbox_name(wire_name, utf8_enabled=utf8_enabled)
|
||||
if name in wire_names and wire_names[name] != wire_name:
|
||||
raise ImapAppendError("IMAP server returned ambiguous mailbox name encodings", temporary=False)
|
||||
wire_names[name] = wire_name
|
||||
parsed.append((name, flags))
|
||||
# Connection-local only: never reuse names across users, profiles or modes.
|
||||
client._govoplan_mailbox_names = wire_names # type: ignore[attr-defined]
|
||||
client._govoplan_mailbox_names_utf8 = utf8_enabled # type: ignore[attr-defined]
|
||||
return parsed
|
||||
|
||||
|
||||
_STANDARD_FOLDER_FLAGS: dict[str, tuple[str, ...]] = {
|
||||
"inbox": ("\\inbox",),
|
||||
"sent": ("\\sent", "\\sentmail"),
|
||||
@@ -366,13 +493,7 @@ def discover_sent_folder(client: imaplib.IMAP4) -> str | None:
|
||||
if typ != "OK" or not data:
|
||||
return None
|
||||
|
||||
parsed: list[tuple[str, set[str]]] = []
|
||||
for item in data:
|
||||
extracted = _extract_mailbox_name(item)
|
||||
if extracted:
|
||||
parsed.append(extracted)
|
||||
|
||||
return _detect_sent_folder(parsed)
|
||||
return _detect_sent_folder(_parsed_mailbox_listing(client, data))
|
||||
|
||||
|
||||
def _effective_sent_folder(*, config: ImapConfig, requested_folder: str | None, client: imaplib.IMAP4) -> str:
|
||||
@@ -452,14 +573,9 @@ def _list_imap_folders_on_client(
|
||||
if typ != "OK":
|
||||
raise ImapAppendError(f"IMAP folder listing failed: {data!r}", temporary=True)
|
||||
|
||||
parsed: list[tuple[str, set[str]]] = []
|
||||
parsed = _parsed_mailbox_listing(client, data or [])
|
||||
folders: list[ImapMailboxInfo] = []
|
||||
for item in data or []:
|
||||
extracted = _extract_mailbox_name(item)
|
||||
if not extracted:
|
||||
continue
|
||||
name, flags = extracted
|
||||
parsed.append((name, flags))
|
||||
for name, flags in parsed:
|
||||
message_count, unseen_count = (
|
||||
(None, None)
|
||||
if not include_status or _has_folder_flag(flags, "noselect")
|
||||
@@ -622,13 +738,40 @@ def _has_folder_flag(flags: set[str], flag: str) -> bool:
|
||||
return any(item.casefold().lstrip("\\") == wanted for item in flags)
|
||||
|
||||
|
||||
def _quote_mailbox_name(name: str) -> str:
|
||||
return "\"" + name.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
|
||||
def _quote_mailbox_name(name: str, *, client: imaplib.IMAP4 | None = None) -> str:
|
||||
if any(ord(char) < 32 or 127 <= ord(char) <= 159 or char in "\u2028\u2029" for char in name):
|
||||
raise ImapConfigurationError("IMAP mailbox names must not contain control characters")
|
||||
utf8_enabled = getattr(client, "utf8_enabled", False) is True
|
||||
wire_names = getattr(client, "_govoplan_mailbox_names", None)
|
||||
if getattr(client, "_govoplan_mailbox_names_utf8", None) is not utf8_enabled:
|
||||
wire_names = None
|
||||
if (
|
||||
client is not None
|
||||
and not utf8_enabled
|
||||
and wire_names is None
|
||||
and name.isascii()
|
||||
and re.search(r"&[A-Za-z0-9+,]*-", name)
|
||||
):
|
||||
# Older configurations saved LIST's wire representation. Resolve those
|
||||
# against this authenticated connection, without guessing or rewriting
|
||||
# configuration. A genuine literal name always wins an ambiguous alias.
|
||||
typ, data = client.list()
|
||||
if typ != "OK":
|
||||
raise ImapAppendError("IMAP folder listing failed while resolving a saved folder name", temporary=True)
|
||||
_parsed_mailbox_listing(client, data or [])
|
||||
wire_names = client._govoplan_mailbox_names # type: ignore[attr-defined]
|
||||
if isinstance(wire_names, dict) and name in wire_names:
|
||||
wire_name = wire_names[name]
|
||||
elif not utf8_enabled and isinstance(wire_names, dict) and name in wire_names.values():
|
||||
wire_name = name
|
||||
else:
|
||||
wire_name = name if utf8_enabled else _encode_mailbox_name(name)
|
||||
return "\"" + wire_name.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
|
||||
|
||||
|
||||
def _imap_folder_status(client: imaplib.IMAP4, folder: str) -> tuple[int | None, int | None]:
|
||||
try:
|
||||
typ, data = client.status(_quote_mailbox_name(folder), "(MESSAGES UNSEEN)")
|
||||
typ, data = client.status(_quote_mailbox_name(folder, client=client), "(MESSAGES UNSEEN)")
|
||||
except Exception:
|
||||
return None, None
|
||||
if typ != "OK":
|
||||
@@ -787,7 +930,7 @@ def _parse_fetch_parts_with_sequence(data: list[Any] | tuple[Any, ...] | None) -
|
||||
|
||||
|
||||
def _select_readonly(client: imaplib.IMAP4, folder: str) -> tuple[int, str | None]:
|
||||
typ, data = client.select(_quote_mailbox_name(folder), readonly=True)
|
||||
typ, data = client.select(_quote_mailbox_name(folder, client=client), readonly=True)
|
||||
if typ != "OK":
|
||||
raise ImapAppendError(f"IMAP folder {folder!r} could not be opened read-only: {data!r}", temporary=False)
|
||||
selected_count = _decode_item(data[0] if data else None).strip()
|
||||
@@ -1253,69 +1396,231 @@ def list_imap_uids_since(
|
||||
_log_imap_cleanup_failure("listing watcher UIDs", cleanup_exc)
|
||||
|
||||
|
||||
def _batch_env_int(name: str, default: int, *, minimum: int, maximum: int) -> int:
|
||||
try:
|
||||
value = int(os.environ.get(name, str(default)))
|
||||
except ValueError:
|
||||
return default
|
||||
return min(maximum, max(minimum, value))
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ImapBatchPolicy:
|
||||
reuse_connections: bool = True
|
||||
max_messages_per_connection: int = 100
|
||||
max_connection_age_seconds: int = 300
|
||||
idle_health_check_seconds: int = 30
|
||||
reconnect_attempts: int = 1
|
||||
|
||||
@classmethod
|
||||
def from_environment(cls) -> ImapBatchPolicy:
|
||||
return cls(
|
||||
reuse_connections=os.environ.get("GOVOPLAN_IMAP_BATCH_REUSE", "true").strip().lower()
|
||||
not in {"0", "false", "no", "off"},
|
||||
max_messages_per_connection=_batch_env_int(
|
||||
"GOVOPLAN_IMAP_BATCH_MAX_MESSAGES", 100, minimum=1, maximum=10000,
|
||||
),
|
||||
max_connection_age_seconds=_batch_env_int(
|
||||
"GOVOPLAN_IMAP_BATCH_MAX_AGE_SECONDS", 300, minimum=1, maximum=3600,
|
||||
),
|
||||
idle_health_check_seconds=_batch_env_int(
|
||||
"GOVOPLAN_IMAP_BATCH_IDLE_HEALTH_CHECK_SECONDS", 30, minimum=0, maximum=3600,
|
||||
),
|
||||
reconnect_attempts=_batch_env_int(
|
||||
"GOVOPLAN_IMAP_BATCH_RECONNECT_ATTEMPTS", 1, minimum=0, maximum=5,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class ImapBatchSession:
|
||||
"""A bounded, sequential transport session, never an APPEND retry queue.
|
||||
|
||||
Callers must authorize every message before invoking append. A failed APPEND
|
||||
is never replayed here, even when the next independent message reconnects.
|
||||
Folder discovery and its original wire names belong to this connection only.
|
||||
"""
|
||||
|
||||
def __init__(self, imap_config: ImapConfig, *, policy: ImapBatchPolicy | None = None):
|
||||
self._host, self._port = _require_imap_config(imap_config)
|
||||
self._config = imap_config.model_copy(deep=True)
|
||||
self.policy = policy or ImapBatchPolicy.from_environment()
|
||||
self._client: imaplib.IMAP4 | None = None
|
||||
self._mock_connected = False
|
||||
self._closed = False
|
||||
self._in_use = Lock()
|
||||
self._connection_count = 0
|
||||
self._connection_attempt_count = 0
|
||||
self._messages_on_connection = 0
|
||||
self._opened_at = 0.0
|
||||
self._last_used_at = 0.0
|
||||
self._folders: dict[str | None, tuple[str, str | bytes]] = {}
|
||||
|
||||
@property
|
||||
def connection_count(self) -> int:
|
||||
return self._connection_count
|
||||
|
||||
@property
|
||||
def reconnect_count(self) -> int:
|
||||
return max(0, self._connection_attempt_count - 1)
|
||||
|
||||
def matches_config(self, config: ImapConfig) -> bool:
|
||||
return config == self._config
|
||||
|
||||
def __enter__(self) -> ImapBatchSession:
|
||||
if self._closed:
|
||||
raise ImapConfigurationError("The IMAP batch session is closed")
|
||||
return self
|
||||
|
||||
def __exit__(self, *_args: Any) -> None:
|
||||
self.close()
|
||||
|
||||
def _disconnect(self) -> None:
|
||||
client, self._client = self._client, None
|
||||
self._mock_connected = False
|
||||
self._folders.clear()
|
||||
self._messages_on_connection = 0
|
||||
if client is not None:
|
||||
try:
|
||||
client.logout()
|
||||
except Exception as exc:
|
||||
_log_imap_cleanup_failure("closing append batch", exc)
|
||||
try:
|
||||
# IMAP close() closes the selected mailbox, not the socket.
|
||||
client.shutdown()
|
||||
except Exception as cleanup_exc:
|
||||
_log_imap_cleanup_failure("shutting down append batch", cleanup_exc)
|
||||
|
||||
def close(self) -> None:
|
||||
self._closed = True
|
||||
self._disconnect()
|
||||
|
||||
@contextmanager
|
||||
def _exclusive_append(self) -> Iterator[None]:
|
||||
if not self._in_use.acquire(blocking=False):
|
||||
raise ImapConfigurationError("An IMAP batch only supports sequential APPENDs")
|
||||
try:
|
||||
if self._closed:
|
||||
raise ImapConfigurationError("The IMAP batch session is closed")
|
||||
yield
|
||||
finally:
|
||||
self._in_use.release()
|
||||
|
||||
def _prepare_connection(self) -> None:
|
||||
now = time.monotonic()
|
||||
if self._client is not None or self._mock_connected:
|
||||
if (
|
||||
not self.policy.reuse_connections
|
||||
or self._messages_on_connection >= self.policy.max_messages_per_connection
|
||||
or now - self._opened_at >= self.policy.max_connection_age_seconds
|
||||
):
|
||||
self._disconnect()
|
||||
elif self._client is not None and now - self._last_used_at >= self.policy.idle_health_check_seconds:
|
||||
try:
|
||||
typ, _data = self._client.noop()
|
||||
if typ != "OK":
|
||||
self._disconnect()
|
||||
except (OSError, imaplib.IMAP4.error):
|
||||
self._disconnect()
|
||||
if self._client is not None or self._mock_connected:
|
||||
return
|
||||
for attempt in range(self.policy.reconnect_attempts + 1):
|
||||
self._connection_attempt_count += 1
|
||||
try:
|
||||
if is_mock_imap_host(self._config.host):
|
||||
self._mock_connected = True
|
||||
else:
|
||||
self._client = _open_imap(self._config)
|
||||
self._connection_count += 1
|
||||
self._opened_at = self._last_used_at = time.monotonic()
|
||||
return
|
||||
except (OSError, imaplib.IMAP4.abort):
|
||||
# Connecting/authenticating has not issued APPEND. Never retry
|
||||
# an authentication rejection or any error from APPEND itself.
|
||||
if attempt >= self.policy.reconnect_attempts:
|
||||
raise
|
||||
|
||||
def append(self, message_bytes: bytes, *, folder: str | None = None) -> ImapAppendResult:
|
||||
with self._exclusive_append():
|
||||
return self._append(message_bytes, folder=folder)
|
||||
|
||||
def _append(self, message_bytes: bytes, *, folder: str | None) -> ImapAppendResult:
|
||||
append_started = False
|
||||
try:
|
||||
self._prepare_connection()
|
||||
reused = self._messages_on_connection > 0
|
||||
if self._mock_connected:
|
||||
if consume_fail_next_imap():
|
||||
raise ImapAppendError("Mock IMAP configured to fail the next append", temporary=False)
|
||||
target_folder = folder or (
|
||||
self._config.sent_folder if self._config.sent_folder != "auto" else "Sent"
|
||||
) or "Sent"
|
||||
record = record_imap_append(message_bytes, folder=target_folder, imap_host=self._config.host)
|
||||
response = f"mock append stored as {record.id}"
|
||||
else:
|
||||
client = self._client
|
||||
assert client is not None
|
||||
if folder not in self._folders:
|
||||
target_folder = _effective_sent_folder(
|
||||
config=self._config, requested_folder=folder, client=client,
|
||||
)
|
||||
self._folders[folder] = (target_folder, _quote_mailbox_name(target_folder, client=client))
|
||||
target_folder, mailbox_argument = self._folders[folder]
|
||||
internal_date = imaplib.Time2Internaldate(time.time())
|
||||
append_started = True
|
||||
typ, data = client.append(mailbox_argument, "\\Seen", internal_date, message_bytes)
|
||||
if typ != "OK":
|
||||
raise ImapAppendError(
|
||||
f"IMAP APPEND failed for folder {target_folder!r}: {data!r}", temporary=False,
|
||||
)
|
||||
response = "; ".join(_decode_item(item) for item in (data or [])) or None
|
||||
self._messages_on_connection += 1
|
||||
self._last_used_at = time.monotonic()
|
||||
return ImapAppendResult(
|
||||
host=self._host,
|
||||
port=self._port,
|
||||
security=self._config.security.value,
|
||||
folder=target_folder,
|
||||
bytes_appended=len(message_bytes),
|
||||
response=response,
|
||||
connection_sequence=self.connection_count,
|
||||
session_reused=reused,
|
||||
reconnect_count=self.reconnect_count,
|
||||
)
|
||||
except (ImapAppendError, ImapConfigurationError):
|
||||
self._disconnect()
|
||||
raise
|
||||
except (OSError, socket.timeout, imaplib.IMAP4.abort) as exc:
|
||||
self._disconnect()
|
||||
raise ImapAppendError(
|
||||
f"IMAP append failed: {exc}", temporary=not append_started, outcome_unknown=append_started,
|
||||
) from exc
|
||||
except imaplib.IMAP4.error as exc:
|
||||
self._disconnect()
|
||||
raise ImapAppendError(
|
||||
f"IMAP append failed: {exc}", temporary=False, outcome_unknown=append_started,
|
||||
) from exc
|
||||
except Exception:
|
||||
self._disconnect()
|
||||
raise
|
||||
|
||||
|
||||
def append_message_to_sent(
|
||||
message_bytes: bytes,
|
||||
*,
|
||||
imap_config: ImapConfig,
|
||||
folder: str | None = None,
|
||||
batch_session: ImapBatchSession | None = None,
|
||||
) -> ImapAppendResult:
|
||||
"""Append a sent MIME message to the configured IMAP Sent folder.
|
||||
"""APPEND one MIME message; SMTP remains authoritative and independent.
|
||||
|
||||
The SMTP send remains authoritative. APPEND is a separate best-effort step
|
||||
and should not be used to decide whether an email was sent.
|
||||
An explicitly scoped batch may reuse its authenticated connection. Neither
|
||||
mode retries an APPEND after transmission has started.
|
||||
"""
|
||||
|
||||
host, port = _require_imap_config(imap_config)
|
||||
if is_mock_imap_host(imap_config.host):
|
||||
if consume_fail_next_imap():
|
||||
raise ImapAppendError("Mock IMAP configured to fail the next append", temporary=False)
|
||||
target_folder = folder or (imap_config.sent_folder if imap_config.sent_folder and imap_config.sent_folder != "auto" else "Sent")
|
||||
record = record_imap_append(message_bytes, folder=target_folder, imap_host=imap_config.host)
|
||||
return ImapAppendResult(
|
||||
host=host,
|
||||
port=port,
|
||||
security=imap_config.security.value,
|
||||
folder=target_folder,
|
||||
bytes_appended=len(message_bytes),
|
||||
response=f"mock append stored as {record.id}",
|
||||
)
|
||||
|
||||
client: imaplib.IMAP4 | None = None
|
||||
append_started = False
|
||||
try:
|
||||
client = _open_imap(imap_config)
|
||||
target_folder = _effective_sent_folder(config=imap_config, requested_folder=folder, client=client)
|
||||
internal_date = imaplib.Time2Internaldate(time.time())
|
||||
append_started = True
|
||||
typ, data = client.append(_quote_mailbox_name(target_folder), "\\Seen", internal_date, message_bytes)
|
||||
if typ != "OK":
|
||||
raise ImapAppendError(f"IMAP APPEND failed for folder {target_folder!r}: {data!r}", temporary=False)
|
||||
response = "; ".join(_decode_item(item) for item in (data or [])) or None
|
||||
return ImapAppendResult(
|
||||
host=host,
|
||||
port=port,
|
||||
security=imap_config.security.value,
|
||||
folder=target_folder,
|
||||
bytes_appended=len(message_bytes),
|
||||
response=response,
|
||||
)
|
||||
except ImapAppendError:
|
||||
raise
|
||||
except (OSError, socket.timeout, imaplib.IMAP4.abort) as exc:
|
||||
raise ImapAppendError(
|
||||
f"IMAP append failed: {exc}",
|
||||
temporary=not append_started,
|
||||
outcome_unknown=append_started,
|
||||
) from exc
|
||||
except imaplib.IMAP4.error as exc:
|
||||
raise ImapAppendError(
|
||||
f"IMAP append failed: {exc}",
|
||||
temporary=False,
|
||||
outcome_unknown=append_started,
|
||||
) from exc
|
||||
finally:
|
||||
if client is not None:
|
||||
try:
|
||||
client.logout()
|
||||
except Exception as cleanup_exc:
|
||||
_log_imap_cleanup_failure("appending sent message", cleanup_exc)
|
||||
if batch_session is not None:
|
||||
if not batch_session.matches_config(imap_config):
|
||||
raise ImapConfigurationError("The IMAP batch configuration does not match this message")
|
||||
return batch_session.append(message_bytes, folder=folder)
|
||||
with ImapBatchSession(
|
||||
imap_config, policy=ImapBatchPolicy(reuse_connections=False, reconnect_attempts=0),
|
||||
) as single:
|
||||
return single.append(message_bytes, folder=folder)
|
||||
|
||||
Reference in New Issue
Block a user