//! MAIL THAT ARRIVES ON A PERSON'S OWN DOMAIN (22 Nov 2026; //! assets/design/chat/OWN-MAIL.md): the raw message Cloudflare's Email //! Routing hands the Worker → the very rows the Mac would write for it, so a //! message that reaches Cotton by both roads (the domain, and the Gmail box it //! is still forwarded to) lands on one `email` row. //! //! The Worker runs this as WASM (`mail_arrive`, `mail_attachment` in //! cotton-rules-wasm), and the phone's core runs it for a message the Worker //! kept only raw. Nothing here reads a clock or a database: the Worker hands //! in the time it received the message. //! //! What it answers: //! //! ```json //! { "email": { "id": "…", "data": { …the email row… } }, //! "thread": { "id": "…", "key": "imap:root@example.org", "data": { …a new thread's row… } }, //! "participants": [{ "id": "…", "data": { "thread_id", "email", "display_name", "role" } }], //! "messageId": "", "inReplyTo": "<…>" | null, "references": ["<…>"], //! "attachments": [{ "index": 0, "name": "menu.pdf", "mime": "application/pdf", "size": 7 }], //! "auth": { "spf": "pass", "dkim": "pass", "dmarc": "pass" } | null, //! "from": "ada@example.org", "subject": "Re: Lunch" } //! ``` use mail_parser::{HeaderValue, MessageParser}; use serde_json::{json, Map, Value}; use super::map::{Ids, Page}; use super::mime::{decode, GmailFacts, MailFlags, Skip}; use crate::fingerprint::email::strip_reply_prefixes; use crate::js; /// The authserv-id of the `Authentication-Results` Cloudflare prepends: only /// the first such header, from this id, is Cloudflare's own verdict. pub const CLOUDFLARE_AUTHSERV: &str = "mx.cloudflare.net"; /// What one arrival is about. #[derive(Debug, Clone)] pub struct Arriving<'a> { /// The person's database id: the namespace every mail id is made under. pub database: &'a str, /// The address it was delivered to (the envelope's RCPT TO), lower-cased. pub account: &'a str, /// When the Worker received it, in milliseconds: its date when the /// `Date` header is not believable. pub received: Option, /// What an IMAP read said of it, when Cotton fetched it from a mailbox (Post CONTRACT row 13) rather than /// receiving it on the person's own domain: its labels and flags then say its state, as the Mac's rows do. pub fetched: Option, } /// One message as Cotton's fetcher read it over IMAP: Gmail's ids and labels when the server is Gmail, its flags. #[derive(Debug, Clone, Default)] pub struct Fetched { pub msgid: Option, pub thrid: Option, /// `X-GM-LABELS` as IMAP spells them (`\Inbox`, `Receipts`); empty on a server without them. pub gm_labels: Vec, /// `FLAGS` (`\Seen`, `\Flagged`). pub flags: Vec, /// Whether it was read from the Inbox (a server without Gmail's labels). pub in_inbox: bool, } /// Why a message was kept aside unread, as a code the raw-mail list stores /// beside it (`/v1/mail/raw`): `refused: