Cotton code
70 lines · 3.1 KB · RustRaw
//! WHEN A MESSAGE WAS SENT, as far as its headers can be believed.//!//! Copied from `cotton-server`'s `mailbox::mail_date` (that crate retires on//! its own).//!//! A `Date` header is the sender's claim, and parsers take it at its word://! the owner's mail held a Google notice stamped "Mon 16 Sep 2611" (14 Oct//! 2026), drawn on a slip as if it were real. RFC 5322 (§3.3, §4.3) reads a//! two-digit year as 1950–2049 and a three-digit one as 1900 plus it; no date//! a person's mail can carry is before 1900 or after 2100. A header outside//! that is a PARSE ERROR, never a date: the message is then dated by its//! mailbox's own receipt time (IMAP `INTERNALDATE`, Gmail's `internalDate`),//! and with neither it is not dated at all.use chrono::{DateTime, Datelike, Utc};/// The earliest and latest years a mail date may carry.pub const YEARS: std::ops::RangeInclusive<i32> = 1900..=2100;/// `ms` when it falls inside [`YEARS`], else `None`.#[must_use]pub fn believable(ms: i64) -> Option<i64> {    let at = DateTime::<Utc>::from_timestamp_millis(ms)?;    YEARS.contains(&at.year()).then_some(ms)}/// The header's date when it is believable, else the mailbox's receipt time/// when that is.#[must_use]pub fn sent_ms(header: Option<i64>, received: Option<i64>) -> Option<i64> {    header.and_then(believable).or_else(|| received.and_then(believable))}/// An IMAP `INTERNALDATE` (`"16-Sep-2024 18:03:30 +0000"`, RFC 3501 §9/// `date-time`, day space-padded) as milliseconds.#[must_use]pub fn internal_date_ms(text: &str) -> Option<i64> {    DateTime::parse_from_str(text.trim(), "%d-%b-%Y %H:%M:%S %z")        .ok()        .map(|at| at.timestamp_millis())}#[cfg(test)]mod tests {    use super::*;    fn ms(iso: &str) -> i64 {        DateTime::parse_from_rfc3339(iso).unwrap().timestamp_millis()    }    /// RULE: A mail date outside 1900–2100 is a parse error and never a date — the message is dated by its mailbox's receipt time, or not at all — because a Google notice stamped 2611 was drawn on a slip as real (14 Oct 2026).    #[test]    fn a_date_outside_the_believable_years_is_never_the_date() {        let sent = ms("2024-09-16T18:03:30Z");        let received = ms("2024-09-16T18:03:31Z");        assert_eq!(sent_ms(Some(sent), Some(received)), Some(sent), "a believable header is the date");        assert_eq!(sent_ms(Some(ms("2611-09-16T18:03:30Z")), Some(received)), Some(received));        assert_eq!(sent_ms(Some(ms("0611-09-16T18:03:30Z")), Some(received)), Some(received));        assert_eq!(sent_ms(Some(ms("1899-12-31T23:59:59Z")), None), None, "no date rather than a wrong one");        assert_eq!(sent_ms(None, Some(ms("2611-09-16T18:03:30Z"))), None);        assert_eq!(sent_ms(Some(ms("2100-12-31T23:59:59Z")), None), Some(ms("2100-12-31T23:59:59Z")));    }    #[test]    fn an_internal_date_reads_with_a_padded_day() {        assert_eq!(internal_date_ms("16-Sep-2024 18:03:30 +0000"), Some(ms("2024-09-16T18:03:30Z")));        assert_eq!(internal_date_ms(" 6-Sep-2024 18:03:30 +0530"), Some(ms("2024-09-06T12:33:30Z")));        assert_eq!(internal_date_ms("not a date"), None);    }}