//! Contact identity is the phone number — the port of //! `packages/import-core/src/phone.ts`. //! //! The canonical normalization is the store's (`protocol::phone`): a number //! written with its country code is E.164 through libphonenumber, so a trunk //! 0 after the code goes; anything else is its digits, 8–15 of them. It does //! NOT infer a country code, because a WRONG normalization files someone's //! messages under a contact that is not theirs — the one outcome worth failing //! over. A miss is harmless: the CLI asks the operator for the number instead //! of guessing. //! //! The result carries NO leading `+`: that is the shape stored in //! `phone_e164` and the shape the import envelope must carry. use crate::import::text::{js_trim, utf16_len}; pub use cotton_store::protocol::phone::{normalize_phone, normalize_phone_in}; use cotton_store::protocol::phone::{is_presentation, strip_presentation}; /// A `_chat.txt` sender field is a display name UNLESS the contact was /// unsaved, in which case WhatsApp writes the number. This decides which it /// is, without ever guessing a country code. /// /// The shape test runs on the TRIMMED sender and the canonical test on the /// raw one, as the TypeScript does — the trim only decides whether the field /// LOOKS like a number, and `normalize_phone` strips whitespace itself. pub fn sender_looks_like_phone(sender: &str) -> bool { let trimmed = js_trim(sender); looks_like_phone_shape(trimmed) && normalize_phone(sender).is_some() } /// JS `/^\+?[\d\s\-().]{8,20}$/`. Written out rather than compiled because the /// quantifier counts UTF-16 code units in JavaScript and every character the /// class admits is one unit wide, so a plain count is exact here. fn looks_like_phone_shape(trimmed: &str) -> bool { let body = trimmed.strip_prefix('+').unwrap_or(trimmed); let mut units = 0usize; for ch in body.chars() { if !(ch.is_ascii_digit() || is_presentation(ch)) { return false; } units += 1; } (8..=20).contains(&units) } /// OPT-IN national → international promotion (`--country-code 91`). /// /// WHY THIS EXISTS. The canonical normalizer keeps `09876543210` as /// `09876543210` and `9876543210` as `9876543210` — neither of which is the /// `919876543210` the same human gets when they sign into Cotton from the /// app. Two spellings of one person = two contacts = the superset-thread /// promise quietly broken. So the operator can state the archive's home /// country, and the report SHOUTS when a number would land un-promoted. /// /// Off by default and never inferred. `country_code` is `Option<&str>` and an /// EMPTY string counts as absent, because the TypeScript's guard is /// `if (!countryCode)` and `""` is falsy there. /// /// Rules (national-number-preserving, no lookup tables): /// * already `+`-prefixed, or already starting with the country code at a /// plausible length → untouched; /// * leading trunk `0` → stripped, country code prepended; /// * bare national number (≤ 10 digits) → country code prepended. pub fn promote_to_country(raw: &str, country_code: Option<&str>) -> String { let Some(code) = country_code.filter(|value| !value.is_empty()) else { return raw.to_string(); }; let cc = code.strip_prefix('+').unwrap_or(code); let trimmed = js_trim(raw); if trimmed.starts_with('+') { return trimmed.to_string(); } let digits = strip_presentation(trimmed); // JS `/^[0-9]+$/` — an empty string fails it, and so does anything with a // letter, so the raw value goes through untouched. if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { return raw.to_string(); } if digits.starts_with(cc) && digits.len() > cc.len() + 6 { return digits; } if digits.starts_with('0') { return format!("{cc}{}", digits.trim_start_matches('0')); } if digits.len() <= 10 { return format!("{cc}{digits}"); } digits } /// True when a canonical phone is missing a country code badly enough to be /// worth a report warning — it kept a trunk `0`, or it is short enough that no /// country code can be in front of it. pub fn looks_national_only(canonical: &str) -> bool { canonical.starts_with('0') || utf16_len(canonical) <= 10 } #[cfg(test)] #[path = "phone_tests.rs"] mod tests; /// A phone number as a person reads it: its `+`, the country code, then the /// national number grouped the way that country writes it ("+44 20 7946 0958", /// "+91 98765 43210") — never a run of twelve bare digits (walker 604bcce6: a /// call read "999555703930"). `None` for anything that is not a number with a /// country code once normalised (an email a FaceTime call came from, a short /// code, a national number with no code), which is then said as written. /// Display only: what is stored and matched stays `normalize_phone`'s. pub fn phone_words(raw: &str) -> Option { cotton_store::protocol::phone::international_words(&normalize_phone(raw)?) }