//! `developer.fakeMac` — COTTON DEV ONLY: a Mac on Gmail answering this copy's mail outbox, for a hand walk of Senders //! end to end on an emulator with no Mac (SENDERS TRUE, coordinator, 24 Nov 2026). The real Cotton refuses it by name. //! What the real Mac does against a Gmail-like server is mac-import's, tested in `crates/cotton-mac/tests`; this //! answers each queued row as that Mac's answers are written (CONTRACT "DELETE FAST", "SENDERS TRUE"), in the ways //! Gmail makes it answer. //! //! ```text //! developer.fakeMac { //! older?: n the mailbox holds n more from each address than this phone (its 90 days); 10 by default //! recount?: n answered counts are counted again to their number plus n (mail came between ask and act) //! trash?: "all" | "partial" a move stops half way the first time (the next run goes on) //! archive?: "read_back" | "unchecked" | "gmail_keeps" Gmail ignores the label change: kept 2 (one mail: it) //! unsubscribe?: "one_click" | "trash_on_arrival" | "sender_refused" | "page" | "refuse" //! (as the Mac: a refused click or one led to a page falls back to Trash on arrival, its why in `error`) //! refuse?: [kind, …] these kinds are refused in the Mac's words //! inbox?: n his Inbox holds n more from each address than Comms holds of theirs; 0 by default //! one_click?: address first give that sender's invented mail a one-click unsubscribe (https List-Unsubscribe //! with List-Unsubscribe-Post), as a real sender's mail carries //! old_archive?: address first lay down a sender's Archive everything as build 8 left it: OK, no read-back, no count //! //! As Mac 10 does (CONTRACT "A mail's page and files, brought when he opens it"): a `mail_page` lands the mail's //! designed page in its `media` (`page.html`, an invented page of its own subject and words, with one picture from //! the web and one link) and every attachment not brought yet beside its name, and answers `sent` with `page` and //! `files`. A mail written as plain text (`developer.inventMail`'s `plain`) gets no page. //! //! As Mac 9 does (ACTS CHECK): a `sender_trash` or `sender_archive` with no counted set on it is counted and refused //! in the Mac's words for that count, the set kept on the row; his Try again moves exactly that set. //! } → { answered: [{ id, kind, to, status, … }] } //! ``` use serde_json::{json, Map, Value}; use cotton_tools::mcp::handlers::tables; use crate::handle::CottonHandle; use crate::methods::MethodError; const OUTBOX: &str = "outbox"; pub fn answer(handle: &CottonHandle, request: &Value) -> Result { if handle.flavor() != cotton_core::flavor::Flavor::Dev { return Err(MethodError::new("not_dev", "developer.fakeMac answers only in Cotton Dev")); } let older = request["older"].as_u64().unwrap_or(10); let text = |k: &str, or: &str| request[k].as_str().unwrap_or(or).to_string(); let (trash, archive, unsubscribe) = (text("trash", "all"), text("archive", "read_back"), text("unsubscribe", "one_click")); let refuse: Vec = request["refuse"].as_array().into_iter().flatten().filter_map(Value::as_str).map(str::to_string).collect(); let recount = request["recount"].as_u64(); let inbox_more = request["inbox"].as_u64().unwrap_or(0); let client = handle.client(); if let Some(address) = request["one_click"].as_str().map(|a| a.trim().to_lowercase()) { super::cotton_chat::write(&client, "developer.fakeMac.one_click", |ctx| { let ws = super::cotton_chat::ws(ctx); let ids = super::cotton_chat::rows(ctx, "SELECT id FROM cotton_emails WHERE workspace_id = ? AND lower(trim(from_email)) = ?", &[ws, rusqlite::types::Value::Text(address.clone())])?; for id in ids.iter().filter_map(|r| r.get("id").and_then(Value::as_str)) { let mut patch = Map::new(); patch.insert("list_unsubscribe".into(), json!("")); patch.insert("list_unsubscribe_post".into(), json!("List-Unsubscribe=One-Click")); cotton_tools::mcp::handlers::common::update_row(ctx, "cotton_emails", id, patch).map_err(|f| f.to_string())?; } Ok(Value::Null) })?; } if let Some(address) = request["old_archive"].as_str().map(|a| a.trim().to_lowercase()) { super::cotton_chat::write(&client, "developer.fakeMac.old", |ctx| { let n = held_from(ctx, &address)?; let data = json!({ "channel": "mail", "kind": "sender_archive", "to": address, "body": address, "status": "sent", "moved": n, "sent_at": now(&client) }); tables::create_produced_record(ctx, json!({ "table": OUTBOX, "data": data }).as_object().unwrap()).map_err(|f| f.to_string())?; Ok(Value::Null) })?; } super::cotton_chat::write(&client, "developer.fakeMac", |ctx| { let mut answered = Vec::new(); let mut page = 0u64; let mut rows = Vec::new(); loop { let ask = json!({ "table": OUTBOX, "where": { "channel": "mail" }, "limit": 500, "page": page }); let listed = tables::list_records(ctx, ask.as_object().unwrap_or(&Map::new())).map_err(|f| f.to_string())?; let records = listed["records"].as_array().cloned().unwrap_or_default(); rows.extend(records.iter().filter(|r| r.get("deleted_at").is_none_or(Value::is_null)).cloned()); match listed["next_page"].as_u64() { Some(next) if !records.is_empty() && next > page => page = next, _ => break, } } for row in rows { let data = &row["data"]; let (Some(id), Some(kind)) = (row["id"].as_str(), data["kind"].as_str()) else { continue }; let status = data["status"].as_str().unwrap_or("queued"); let to = data["to"].as_str().unwrap_or_default().trim().to_lowercase(); let held = held_from(ctx, &to)?; let whole = |k: &str| data[k].as_u64().or_else(|| data[k].as_str().and_then(|t| t.parse().ok())); let fixed = whole("uid_validity").unwrap_or(0) > 0 && whole("uid_below").unwrap_or(0) > 0; let in_inbox = held + inbox_more; // Mac 9: no counted set on the row, so it counts, keeps the set, and asks him first. let asks = |n: u64| json!({ "status": "failed", "count": n, "uid_validity": 7, "uid_below": 100_000, "error": crate::phone::chat_app::whatsapp_send::mac_asks(kind, n) }); let patch = if kind == "sender_count" && status == "sent" { // Counted again: mail came between his ask and his act. let Some(n) = recount else { continue }; json!({ "count": whole("count").unwrap_or(0) + n, "uid_below": whole("uid_below").unwrap_or(100_000) + n }) } else if status != "queued" && status != "sending" { continue; } else if refuse.iter().any(|k| k == kind) { json!({ "status": "failed", "error": "This mail server can't move mail without erasing it, so nothing was moved." }) } else { match kind { "sender_count" if data["target"].as_str() == Some("inbox") => json!({ "status": "sent", "count": in_inbox, "uid_validity": 7, "uid_below": 100_000, "error": null }), "sender_count" => json!({ "status": "sent", "count": held + older, "uid_validity": 7, "uid_below": 100_000, "error": null }), "sender_trash" if !fixed => asks(held + older), "sender_archive" if !fixed => asks(in_inbox), "sender_trash" => { let count = whole("count").unwrap_or(held + older); let moved = whole("moved").unwrap_or(0); if trash == "partial" && moved == 0 && count > 1 { json!({ "status": "sending", "moved": count / 2 }) } else { json!({ "status": "sent", "moved": count, "sent_at": now(&client), "error": null }) } } "sender_archive" | "archive" => { let n = if kind == "archive" { 1 } else { whole("count").unwrap_or_else(|| in_inbox.max(1)) }; match archive.as_str() { "unchecked" => json!({ "status": "sent", "moved": n, "sent_at": now(&client), "error": null }), "gmail_keeps" if kind == "archive" => json!({ "status": "failed", "moved": 0, "error": "Gmail kept it in your Inbox." }), "gmail_keeps" => json!({ "status": "failed", "moved": n.saturating_sub(2), "error": "Gmail kept 2 of them in your Inbox." }), _ => json!({ "status": "sent", "moved": n, "sent_external_id": "read_back", "sent_at": now(&client), "error": null }), } } "unsubscribe" => match unsubscribe.as_str() { "refuse" => json!({ "status": "failed", "error": "Their unsubscribe link didn't answer." }), "sender_refused" => json!({ "status": "sent", "sent_external_id": "trash_on_arrival", "sent_at": now(&client), "error": "the sender's server answered 500" }), "page" => json!({ "status": "sent", "sent_external_id": "trash_on_arrival", "sent_at": now(&client), "error": "the sender's server redirected the click to a page, so it was not taken" }), how => json!({ "status": "sent", "sent_external_id": how, "sent_at": now(&client), "error": null }), }, // Mac 10: the mail's designed page and its files, landed on its row. "mail_page" => crate::phone::chat_app::mail::dev_bring_page(ctx, client.layout(), data["target"].as_str().unwrap_or_default(), &now(&client))?, _ => json!({ "status": "sent", "sent_at": now(&client), "error": null }), } }; // What a page's answer says it kept is told back, not a column of the outbox. let mut written = patch.clone(); if let Some(fields) = written.as_object_mut() { fields.remove("page"); fields.remove("files"); } tables::update_produced_record(ctx, json!({ "table": OUTBOX, "id": id, "patch": written }).as_object().unwrap()).map_err(|f| f.to_string())?; let mut said = patch.clone(); said["id"] = json!(id); said["kind"] = json!(kind); said["to"] = json!(to); answered.push(said); } Ok(json!({ "answered": answered })) }) } fn now(client: &crate::shell::client::ClientController) -> String { super::cotton_chat::iso(super::cotton_chat::now_ms(client)) } /// How many from `address` this phone holds (its mail, binned or not). fn held_from(ctx: &mut cotton_tools::mcp::dispatch::Ctx<'_>, address: &str) -> Result { let ws = super::cotton_chat::ws(ctx); let found = super::cotton_chat::rows(ctx, "SELECT count(*) AS n FROM cotton_emails WHERE workspace_id = ? AND lower(trim(from_email)) = ?", &[ws, rusqlite::types::Value::Text(address.to_string())])?; Ok(found.first().and_then(|r| r.get("n")).and_then(Value::as_u64).unwrap_or(0)) }