//! PEOPLE, NOTICES OR PROMOTIONS: the three groups of Senders and of the Mail //! tab's chips (SENDERS.md "DECIDED: Senders 3", owner, 24 Nov 2026). Written //! rules only, never AI: the same facts always give the same group. //! //! The person-or-machine rule ([`super::who`]) answers first. A person is //! **People**; junk is in no group (it shows only in Blocked). A machine is //! split, the first step that answers winning: //! //! 1. the owner said so on the sender's row: `service` is Notices, //! `promotions` is Promotions (his own act is above every rule); //! 2. the mail is about something of his (a code, a statement, a receipt, an //! order, a payment, a booking, a security or sign-in notice, a bill): a //! **Notice**, even when it carries an unsubscribe link, as a bank's alerts do; //! 3. Gmail filed it under Promotions: **Promotions**; under Updates, Social //! or Forums: a **Notice** (Gmail's own label, read by the Mac; Gmail //! accounts only); //! 4. it is marketing: a List-Unsubscribe header, bulk Precedence, a list's //! copy rather than one to him, an unsubscribe footer in the sender's own //! words, or offer words in the subject ("50% off", "sale", "deal"): //! **Promotions**; //! 5. otherwise a **Notice**: an automatic message about his own things. //! //! Whether he opens a sender's mail orders senders inside a group (Senders), //! never moves one between groups: a notice he never opens is still a notice. use std::sync::OnceLock; use regex::Regex; use super::who::{is_machine_text, own_words, who, Facts, Who, Why}; /// The three groups. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "lowercase")] pub enum Group { People, Notices, Promotions, } impl Group { pub fn as_str(self) -> &'static str { match self { Group::People => "people", Group::Notices => "notices", Group::Promotions => "promotions", } } pub fn parse(id: &str) -> Option { match id { "people" => Some(Group::People), "notices" => Some(Group::Notices), "promotions" => Some(Group::Promotions), _ => None, } } } /// Which step put a machine where it is: said in tests and, later, on a sender's row. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Because { /// A person, by the person-or-machine rule. Person, OwnerSaid, AboutHisThings, GmailSaid, Marketing, /// A machine nothing above marked as marketing. Automatic, } /// What the Mac keeps of the newest mail's headers (`cotton_emails` columns, CONTRACT "Senders 3: what the Mac /// keeps"). Each is none until the Mac has read that mail with this build; the rule then falls back to the words. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct Headers<'a> { /// `List-Unsubscribe` is present (any value). pub list_unsubscribe: bool, /// `Precedence`, lower-cased: `bulk`, `list`, `junk`. pub precedence: Option<&'a str>, /// Gmail's category for the mail: `promotions`, `updates`, `social`, `forums` (`primary` when none). pub gmail_category: Option<&'a str>, } /// The group of a sender, from the facts the person-or-machine rule reads and the headers the Mac kept. None for /// junk (blocked, or the owner said junk): it is in no group. pub fn group(facts: &Facts<'_>, headers: &Headers<'_>) -> Option<(Group, Because)> { let (kind, why) = who(facts); match kind { Who::Junk => return None, Who::Person => return Some((Group::People, Because::Person)), Who::Machine => {} } if why == Why::OwnerSaid || facts.owner_verdict == Some("promotions") { return Some(match facts.owner_verdict { Some("promotions") => (Group::Promotions, Because::OwnerSaid), _ => (Group::Notices, Because::OwnerSaid), }); } if facts.subject.is_some_and(is_about_his_things) { return Some((Group::Notices, Because::AboutHisThings)); } match headers.gmail_category { Some("promotions") => return Some((Group::Promotions, Because::GmailSaid)), Some("updates" | "social" | "forums") => return Some((Group::Notices, Because::GmailSaid)), _ => {} } let bulk = matches!(headers.precedence, Some("bulk" | "list" | "junk")); if headers.list_unsubscribe || bulk || facts.to_a_list || facts.subject.is_some_and(is_offer) || facts.text.is_some_and(has_unsubscribe) { return Some((Group::Promotions, Because::Marketing)); } Some((Group::Notices, Because::Automatic)) } fn pattern(cell: &'static OnceLock, source: &str) -> &'static Regex { cell.get_or_init(|| Regex::new(source).expect("a group pattern compiles")) } /// A subject about something of his: a code, a statement, a receipt or invoice, an order or delivery, a payment, a /// booking or ticket, an appointment, a bill, a security or sign-in notice, an account change. pub fn is_about_his_things(subject: &str) -> bool { static RE: OnceLock = OnceLock::new(); pattern( &RE, concat!( r"(?i)\b(otp|one[- ]time (password|code|pin)|verification code|security code|login code|passcode|2fa)\b", r"|\b(e-?)?statement\b|\breceipt\b|\binvoice\b|\bbill(ing)?\b|\bpayment\b|\btransaction\b|\b(debited|credited|refund(ed)?)\b", r"|\border\b.{0,40}\b(confirm|placed|received|shipped|dispatched|delivered|out for delivery|cancel)|\byour order\b|\bshipment\b|\bdelivery\b", r"|\bbooking\b|\breservation\b|\bitinerary\b|\bboarding pass\b|\be-?ticket\b|\bcheck-in\b|\bappointment\b", r"|\b(security|sign-?in|login|log-?in) (alert|notice|attempt|activity)\b|\bnew (sign-?in|login|device)\b|\bpassword\b|\bverify your\b", r"|\b(renewal|expir(es|ing|y)|due (date|on|soon)|overdue|suspended|has been (updated|changed))\b", r"|\balert\b", ), ) .is_match(subject) } /// A subject that sells: a discount, a sale, a deal, an offer, free shipping, a new collection, last chance. pub fn is_offer(subject: &str) -> bool { static RE: OnceLock = OnceLock::new(); pattern( &RE, concat!( r"(?i)\d{1,2}\s?% off|\bsale\b|\bdeals?\b|\boffers?\b|\bdiscount|\bcoupon|\bpromo(tion)?\b|\bfree shipping\b", r"|\blast chance\b|\blimited time\b|\bends (tonight|today|soon)\b|\bnew (arrivals|collection|in)\b|\bshop (now|the)\b", r"|\bblack friday\b|\bcyber monday\b|\bbuy (one|1|now)\b|\bexclusive\b|\bwebinar\b|\bnewsletter\b|\bdigest\b", ), ) .is_match(subject) } /// An unsubscribe line in the sender's own words: the one footer that marks a mailing (the other machine-made /// footers, "do not reply" or an automated-message line, are as much a bank's as a shop's). fn has_unsubscribe(text: &str) -> bool { static RE: OnceLock = OnceLock::new(); is_machine_text(text) && pattern(&RE, r"(?i)unsubscribe|\bopt[- ]?out\b|manage (your )?((e-?mail|communication|newsletter|subscription|marketing) )?(preferences|subscriptions?)|you('re| are) receiving this").is_match(own_words(text)) } #[cfg(test)] #[path = "group_tests.rs"] mod tests;