// SPDX-FileCopyrightText: MailCraft contributors // SPDX-License-Identifier: MIT OR Apache-2.0 //! Mail down through Microsoft Graph (R3.2; R3 spec S5, S9–S11, S15). //! //! A folder's mail arrives as pages of its message delta, each taken in by itself with the link //! that goes on from it, so that the mailbox file never holds a page without its link, nor a link //! past what it has (S10). Everything is matched by server id (S5): a page that comes twice //! changes nothing. //! //! - **A new message** comes as its headers and preview, not **fetched**. Once its folder's first //! round has ended, it is sorted by the Safe and Blocked Senders lists, and mail arriving in the //! Inbox then goes through the rules (#567), on every page of a later round. //! - **A message the server has changed** takes the server's read, flag, categories and //! importance, but for a property changed here since the server last said (ADR 0003): that //! goes up with R3.3, and nothing changed here is overwritten by coming down. //! - **A message whose read state alone the server says changed** (its id and `isRead`, as //! Exchange Online says a message read or unread elsewhere, #148) takes it, as a message changed //! there does: but for a message whose read state was changed here since. //! - **A message the server says left a folder** (`@removed`: deleted, or moved out) is marked //! gone and kept. Seen in another folder, it has moved there, with what was fetched of it. Only //! once every folder's delta reached its end in a run is what is still gone removed here. //! - **A folder listed afresh** (its link was no longer taken, S11) marks all its messages gone //! first: what the complete new listing brings is kept, and the rest goes at the end of the run. use std::collections::{HashMap, HashSet}; use mailcraft_model::sync::{GraphMessage, GraphProgress, MessagePage, ServerMessage, merge_categories}; use mailcraft_model::time::Timestamp; use mailcraft_model::{AccountKind, Body, FlagStatus, FolderKind, Id, Mailbox, Message, OfflineWindow}; /// Where each message a server knows is, by its server id: kept between pages, as finding one /// otherwise means a walk through every message of the mailbox. #[derive(Debug, Default)] pub struct ServerIndex { /// By account and server id: each mailbox names its messages by itself, so two mailboxes /// (an account and a shared mailbox opened through it) may use the same id for two messages. ids: HashMap<(Id, String), Id>, /// How many messages the mailbox had when this was last right: anything else that added or /// removed one makes it walk again. count: usize, } impl ServerIndex { fn rebuild(&mut self, mb: &Mailbox) { self.ids = mb.messages.values().filter_map(|m| m.server.as_ref().map(|s| ((m.account, s.id.clone()), m.id))).collect(); self.count = mb.messages.len(); } /// The message of `account` the server calls `id`. pub(crate) fn find(&mut self, mb: &Mailbox, account: Id, id: &str) -> Option { let is = |m: Id| mb.message(m).is_some_and(|m| m.account == account && m.server.as_ref().is_some_and(|s| s.id == id)); if self.count != mb.messages.len() { self.rebuild(mb); } let key = (account, id.to_string()); match self.ids.get(&key).copied() { Some(m) if is(m) => Some(m), // Out of date (a message was changed by other means): look again, once. Some(_) => { self.rebuild(mb); self.ids.get(&key).copied().filter(|m| is(*m)) } None => None, } } fn note(&mut self, mb: &Mailbox, account: Id, id: &str, m: Id) { self.ids.insert((account, id.to_string()), m); self.count = mb.messages.len(); } } /// What taking in pages did. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct Down { pub new: usize, pub changed: usize, pub gone: usize, pub removed: usize, /// New messages a rule moved out of the account as they arrived (#567). pub left: usize, /// The clock the rules run by on what arrives ([`Session::rule_clock`](crate::Session::rule_clock)); /// without one, this computer's. pub clock: Option, /// The `Message-ID`s of the mail an account had before a run's new mail /// ([`super::account::message_ids`]), found once for the run when new mail comes into the /// Inbox or Junk E-mail, and kept for its later pieces ([`Down::of_run`]). had: Option<(Id, HashSet)>, } impl Down { /// Down for a run of `s`, whose rules run by its clock. pub fn of(s: &crate::Session) -> Down { Down { clock: Some(s.rule_clock()), ..Default::default() } } /// [`Down::of`] for a piece of `account`'s run, with what the account had before the run's /// new mail as an earlier piece of the run found it ([`crate::Session::run_had`]). pub(crate) fn of_run(s: &mut crate::Session, account: Id) -> Down { let had = s.run_had.remove(&account).map(|had| (account, had)); Down { had, ..Down::of(s) } } /// Keep what the account had for the run's next piece, unless this one was its last. pub(crate) fn end_piece(&mut self, s: &mut crate::Session, account: Id, partial: bool) { match self.had.take() { Some((of, had)) if partial && of == account => { s.run_had.insert(account, had); } _ => { s.run_had.remove(&account); } } } /// Whether taking in the pages did anything: the clock is no part of that. pub fn took_anything(&self) -> bool { self.new + self.changed + self.gone + self.removed + self.left > 0 } /// Whether `account` had a message with the `Message-ID` `id` before the run's new mail: one /// moved into the Inbox on another device, to which an Exchange server gives a new id (#566). fn had(&mut self, mb: &Mailbox, account: Id, id: &str) -> bool { if id.is_empty() { return false; } if self.had.as_ref().is_none_or(|(a, _)| *a != account) { self.had = Some((account, super::account::message_ids(mb, account))); } self.had.as_ref().is_some_and(|(_, had)| had.contains(id)) } fn rule_clock(&self) -> mailcraft_model::RuleClock { let system = || mailcraft_model::RuleClock { now: mailcraft_time::now(), zone: mailcraft_time::Zone::system(), due_hour: 17 }; self.clock.unwrap_or_else(system) } } /// Take in one page of a folder's delta, with its link. A page for a folder this mailbox doesn't /// have (yet) is passed over, and so is its link: it comes again. pub fn take_page(mb: &mut Mailbox, index: &mut ServerIndex, account: Id, page: &MessagePage, down: &mut Down) { let Some(folder) = mb.folder_by_server(account, &page.folder) else { return }; // The Safe and Blocked Senders lists sort what arrives once the folder's first round has // ended (#504), on every page of a later round (#567): never its first download, nor a round // read afresh. (A folder of a file from before #567 has only `caught_up` to say so.) let sorting = !page.afresh && mb.folder(folder).and_then(|f| f.server.as_ref()).is_some_and(|s| s.round_ended || s.caught_up); // Then the rules (#567), on what arrives in the Inbox and only when the lists sort. Never in a // shared mailbox: the rules, like the lists, are the owner's. let inbox = mb.folder(folder).is_some_and(|f| f.kind == FolderKind::Inbox); let junk = mb.folder(folder).is_some_and(|f| f.kind == FolderKind::Junk); // (Of a Microsoft or Exchange server account alone, as the repair that puts right what an // earlier version kept: a JMAP server's Sent folder is read through here too.) let in_sent = mb.folder(folder).is_some_and(|f| f.kind == FolderKind::Sent); let sent_items = in_sent && mb.keeps_drafts_on_server(account); let ruling = sorting && mb.account(account).is_some_and(|a| !a.is_shared()); // The server's copies of messages being moved out of the account are not new mail. let mut moving_out: HashSet = mb.messages.values().filter_map(|m| m.moving_out.as_ref().filter(|out| out.account == account).map(|out| out.id.clone())).collect(); if page.afresh { for m in mb.messages.values_mut() { if m.account == account && m.server.as_ref().is_some_and(|s| s.folder == page.folder && !s.gone) && let Some(s) = std::sync::Arc::make_mut(m).server.as_mut() { s.gone = true; } // Being moved out of the account: still in that folder only if the listing has it (#451). // Until the listing has ended, it may be in a page to come (#30): `listing`. if m.moving_out.as_ref().is_some_and(|out| out.account == account && out.folder == page.folder && !out.left) && let Some(out) = std::sync::Arc::make_mut(m).moving_out.as_mut() { (out.left, out.listing) = (true, true); } } } for id in &page.removed { let Some(m) = index.find(mb, account, id) else { // Being moved out of the account, and gone from where it was: a refused move has // nothing to go back to there, unless it turns up in another folder (#451). if moving_out.contains(id) && let Some(m) = self::moving_out(mb, account, id) && let Ok(m) = mb.message_mut(m) && let Some(out) = m.moving_out.as_mut().filter(|out| out.folder == page.folder) { (out.left, out.listing) = (true, false); } continue; }; if let Ok(m) = mb.message_mut(m) { // A draft sent here, gone from Drafts there: Exchange has filed it, and a listing of it // as a draft outside Sent Items is no longer one from before that (#1160). if m.sent_here && !in_sent { m.sent_here = false; } if let Some(s) = m.server.as_mut().filter(|s| s.folder == page.folder && !s.gone) { s.gone = true; down.gone += 1; } } } for there in &page.read { let Some(m) = index.find(mb, account, &there.id) else { continue }; if let Ok(m) = mb.message_mut(m) && let Some(s) = m.server.as_mut().filter(|s| s.folder == page.folder) { let was = s.read; s.read = there.read; if m.read == was && m.read != there.read { m.read = there.read; down.changed += 1; } } } for there in &page.messages { if there.server.id.is_empty() { continue; } match index.find(mb, account, &there.server.id) { Some(m) => { if update(mb, account, m, folder, &page.folder, there) { down.changed += 1; } } None if moving_out.contains(&there.server.id) => { // Moved on there by another program while being moved out: it is in this folder now. if let Some(m) = self::moving_out(mb, account, &there.server.id) && let Ok(m) = mb.message_mut(m) && let Some(out) = m.moving_out.as_mut() { (out.folder, out.left, out.listing) = (page.folder.clone(), false, false); } } None => { let mut message = new_message(folder, &page.folder, there); // New to this mailbox and in Sent Items, it was sent: Exchange files the copy of a // message sent as MIME (`sendMail`, how a message never saved as a draft goes; // #611) under an id this mailbox has never seen, with `isDraft` still true, and it // would be shown as a draft for ever. A draft this mailbox knows that turns up in // Sent Items (moved there by the owner, which Outlook keeps as a draft) is sent // only when the server says so (`update`), and is a draft again when moved back. if sent_items { message.draft = false; } // Mail the account had, moved into the Inbox or Junk E-mail on another device, // where an Exchange server gives it a new id: the lists sort it as any new mail, // and the rules file it as found (#566). let moved_in = sorting && (inbox || junk) && down.had(mb, account, &message.message_id); if let Ok(m) = mb.add_message(message) { index.note(mb, account, &there.server.id, m); // By the Safe and Blocked Senders lists first (#504), then the rules: mail a // list sends to Junk E-mail isn't the rules' to file, and a safe sender's taken // out of Junk E-mail goes through them as any mail arriving in the Inbox does. // A move goes up as the owner's own does (R3.3). let sorted = sorting && mb.sort_junk(m); if ruling && inbox != sorted { if moved_in { mb.apply_rules_found(m, &down.rule_clock()); } else { mb.apply_rules(m, &down.rule_clock()); } // Out of the account: should the page list it again, that is the same // message (#606 review). if mb.message(m).is_some_and(|m| m.account != account) { moving_out.insert(there.server.id.clone()); down.left += 1; } } down.new += 1; } } } } // A listing afresh that ended without a copy being moved out: it left that folder (#30). if page.caught_up { for m in mb.messages.values_mut() { if m.moving_out.as_ref().is_some_and(|out| out.account == account && out.folder == page.folder && out.listing) && let Some(out) = std::sync::Arc::make_mut(m).moving_out.as_mut() { out.listing = false; } } } // Listed afresh as it was to be (MB3.16): the new listing goes on from here. Only the first // page of the listing that request began says so; a page of an earlier one, or one that // followed a link, never meets a request made after it. if page.afresh && let Ok(f) = mb.folder_mut(folder) && let Some(s) = f.server.as_mut().filter(|s| s.relist && s.relist_gen == page.relist_gen) { s.relist = false; } if !page.link.is_empty() && let Ok(f) = mb.folder_mut(folder) && let Some(s) = f.server.as_mut() { s.delta = page.link.clone(); s.caught_up = page.caught_up; // Ended before this page, or by it; a page read afresh begins again. s.round_ended = sorting || page.caught_up; // The counts the link was got with, or none (#276): a new link never keeps the old one's. s.counts = page.counts; } } /// A message the server has and this mailbox hasn't: its headers and preview. pub(crate) fn new_message(folder: Id, server_folder: &str, there: &GraphMessage) -> Message { let server = ServerMessage { folder: server_folder.to_string(), gone: false, ..there.server.clone() }; Message { folder, message_id: there.message_id.trim().trim_start_matches('<').trim_end_matches('>').to_string(), from: there.from.clone(), to: there.to.clone(), cc: there.cc.clone(), subject: there.subject.clone(), date: there.sent, received: there.received, body: Body { text: there.preview.clone(), html: None }, read: server.read, answered: server.answered, forwarded: server.forwarded, flag: server.flag, flag_due: server.flag_due, importance: server.importance, categories: server.categories.clone(), draft: there.draft.unwrap_or(false), server: Some(server), unfetched: true, attachments_on_server: there.has_attachments, headers_cut: there.cut, categories_cut: there.categories_cut, ..Default::default() } } /// Bring a message this mailbox has in step with what the server says of it now. Whether /// anything changed. fn update(mb: &mut Mailbox, account: Id, id: Id, folder: Id, server_folder: &str, there: &GraphMessage) -> bool { let Some(m) = mb.message(id) else { return false }; let before = m.clone(); let last = m.server.clone().unwrap_or_default(); // A draft the server has changed (its change key differs from what was last seen of it). let changed_there = m.draft && !last.change_key.is_empty() && !there.server.change_key.is_empty() && last.change_key != there.server.change_key; if changed_there && m.server_draft.as_ref().is_some_and(|d| d.pending) { // Changed here too: both are kept (S26). The server's is a draft of its own, unless the // server says it is one no longer: sent meanwhile and filed in Sent Items (#611), it is // the sent message, and only what was changed here stays a draft. let mut copy = new_message(folder, server_folder, there); copy.draft = there.draft.unwrap_or(true); // The server's own version, a draft on its word: so kept through a save and the repair // of sent copies an earlier version took for drafts, wherever the server has it. if copy.draft { copy.server_draft = Some(mailcraft_model::sync::ServerDraft { theirs: true, ..Default::default() }); } return super::graph_drafts::keep_twice(mb, id, Some(copy)).is_some(); } // Where the server last had it, as this mailbox has that folder: a message still there here // follows the server; one moved here since stays where it was put (that move goes up, R3.3). let was_here = mb.folder_by_server(account, &last.folder); // A draft sent here, listed as a draft outside Sent Items: Exchange hasn't filed it yet, or // this listing was made before it did (#1160). It is the message sent, in Sent Items here: it // doesn't follow, and once the server has listed it in Sent Items such a listing says nothing. let in_sent = mb.folder(folder).is_some_and(|f| f.kind == FolderKind::Sent); let unfiled = m.sent_here && !in_sent && there.draft == Some(true); if unfiled && !last.gone && was_here.and_then(|was| mb.folder(was)).is_some_and(|was| was.kind == FolderKind::Sent) { return false; } let follows = !unfiled && (last.gone || was_here.is_none_or(|was| m.folder == was)); let in_drafts = mb.folder(folder).is_some_and(|f| f.kind == FolderKind::Drafts); // Only of a JMAP account: a Microsoft server keeps a follow-up's dates and "complete" itself. let flag_bit_alone = mb.account(account).is_some_and(|a| a.kind == AccountKind::Jmap); let Ok(m) = mb.message_mut(id) else { return false }; let now = &there.server; if m.read == last.read { m.read = now.read; } // The marks a JMAP server keeps as keywords, as the read state (a Graph server says neither: // they stay false on both sides, and what is set here stays). if m.answered == last.answered { m.answered = now.answered; } if m.forwarded == last.forwarded { m.forwarded = now.forwarded; } // The flag is its status and its due date: changed here, both stay as they are. if flag_bit_alone { // A JMAP server keeps `$flagged` and nothing else, so a due date or a "complete" here is // no change it was ever told of, and no reason to hold its word off: whether the flag was // changed here is told by that bit alone. Unchanged here and changed there, the server's // state is taken, without a due date; in every other case what is here stays, with what // only this computer keeps of it. let flagged = |flag: FlagStatus| flag == FlagStatus::Flagged; if flagged(m.flag) == flagged(last.flag) && flagged(now.flag) != flagged(last.flag) { m.flag = now.flag; m.flag_due = now.flag_due; } } else if m.flag == last.flag && m.flag_due == last.flag_due { m.flag = now.flag; m.flag_due = now.flag_due; } // Changed on both sides: what was added and taken away here goes onto the server's list (S17). if m.categories == last.categories { m.categories = now.categories.clone(); } else if now.categories != last.categories { m.categories = merge_categories(&last.categories, &m.categories, &now.categories); } if m.importance == last.importance { m.importance = now.importance; } // (Its categories are the server's, and what a listing left out of them stays left out; its // recipients and subject are the listing's only until it is fetched.) m.categories_cut |= there.categories_cut; if m.unfetched { m.headers_cut |= there.cut; } if follows && m.folder != folder { m.folder = folder; // Moved on another device before it is here whole, and back too: the rules that waited on // it let it alone, as after a move here ([`Mailbox::move_message`], #566). m.rules_waiting = None; } if m.unfetched { m.attachments_on_server = there.has_attachments; } // Sent, the draft is filed in Sent Items under its id: no longer a draft (#611), when the // server says so and not merely leaves it out. One saved here that waits to go up stays one. // What was sent may not be what was last seen of the draft: it is fetched afresh. let sent = m.draft && there.draft == Some(false) && !m.server_draft.as_ref().is_some_and(|d| d.pending); if sent { m.draft = false; m.server_draft = None; } // And the other way: one taken for sent because it was new to this mailbox in Sent Items (a // draft another program moved there, or in an account added fresh) is a draft again once the // server lists it in Drafts and says so; what it is now is fetched afresh. Only one that is // where the server has it and was never a draft here: a draft sent here waits in the Outbox // with the server's id and its own draft state until the send clears, and the server may list // it in Drafts meanwhile (the send refused or its answer lost, a listing afresh, another // program touching the draft); it stays the message queued, not a [Draft] in the Outbox. let drafted = !m.draft && in_drafts && follows && m.server_draft.is_none() && there.draft == Some(true); if drafted { m.draft = true; } if changed_there || sent || drafted { // Changed elsewhere only: the server's version, fetched afresh. // (Its recipients and subject are the listing's now, and so is whether it left some out. // What else is here of it is the version downloaded before, kept until the new one comes, // and no word on what it is now: no rule goes by it meanwhile, [`Message::stale`], #566.) (m.subject, m.to, m.cc, m.unfetched) = (there.subject.clone(), there.to.clone(), there.cc.clone(), true); m.headers_cut = there.cut; m.stale = true; m.attachments_on_server = there.has_attachments; if let Some(sd) = m.server_draft.as_mut() { sd.synced = None; } } // A delta never says a size (#300): the last one known, of the whole message when it was // fetched, stays; but not for a draft changed elsewhere, sent or a draft again, whose new // version is still to be fetched and may be smaller than the background fetch's limit, which // its old size is held against, and is no size a rule may go by (#566). let size = if now.size > 0 || changed_there || sent || drafted { now.size } else { last.size }; // The conversation id, as the size: one a listing doesn't say (a JMAP server's marks alone) // is the last one known (#795). let (thread, thread_asked, thread_tries) = if now.thread.is_empty() { (last.thread.clone(), last.thread_asked, last.thread_tries) } else { (now.thread.clone(), false, 0) }; m.server = Some(ServerMessage { folder: server_folder.to_string(), gone: false, size, thread, thread_asked, thread_tries, ..now.clone() }); *m != before } /// Every folder's delta reached its end: what is still gone from every folder is gone here too. pub fn sweep(mb: &mut Mailbox, account: Id, since: Option, down: &mut Down) { // But for a draft saved here that waits to go up: it is made on the server anew (S26); and // for what waits in the Outbox, or is held there (R3.4.2): it stays, for the owner to see. let outbox = mb.folder_of_kind(account, mailcraft_model::FolderKind::Outbox); for m in mb.messages.values_mut() { let keeps = (m.draft && m.server_draft.as_ref().is_some_and(|d| d.pending)) || Some(m.folder) == outbox; let waits = m.account == account && keeps && m.server.as_ref().is_some_and(|s| s.gone); if waits { let m = std::sync::Arc::make_mut(m); m.server = None; if let Some(sd) = m.server_draft.as_mut() { sd.synced = None; } } } // Put back after a refused copy, and gone from the server since: kept here instead (#451). let returned: Vec = mb.messages.values().filter(|m| m.account == account && m.returned > 0 && m.server.as_ref().is_some_and(|s| s.gone)).map(|m| m.id).collect(); for id in returned { super::graph_place::kept_instead(mb, id); } // A message outside the window of a device that lists only that (`since`: where the window // starts) is marked gone by a listing afresh (MB3.16), though the server has it: with a change // here that hasn't gone up, it stays (as the server's, so that the change goes up) and loses // nothing. Without a window, or inside it, gone is gone: the server deleted or moved it. let unsent: Vec = mb .messages .values() .filter(|m| { let received = if m.received != 0 { m.received } else { m.date }; m.account == account && since.is_some_and(|since| received != 0 && received < since) && m.server.as_ref().is_some_and(|s| s.gone) && super::graph_up::has_unsent(mb, account, m) }) .map(|m| m.id) .collect(); for id in unsent { if let Some(m) = mb.messages.get_mut(&id) && let Some(server) = std::sync::Arc::make_mut(m).server.as_mut() { server.gone = false; } } let before = mb.messages.len(); mb.messages.retain(|_, m| !(m.account == account && m.server.as_ref().is_some_and(|s| s.gone))); down.removed += before - mb.messages.len(); // What was put back after a refused copy and is still there once every folder was read (or // passed over, its counts the same): one run nearer to being the server's message as any other. for m in mb.messages.values_mut() { if m.account == account && m.returned > 0 { std::sync::Arc::make_mut(m).returned -= 1; } } } /// The message being moved out of `account` whose server copy is `id`. pub fn moving_out(mb: &Mailbox, account: Id, id: &str) -> Option { mb.messages.values().find(|m| m.moving_out.as_ref().is_some_and(|out| out.account == account && out.id == id)).map(|m| m.id) } /// What the status bar says while a run is under way. pub fn progress_words(mb: &Mailbox, account: Id, progress: &GraphProgress, lang: mailcraft_i18n::Lang) -> String { let name = mb.folder_by_server(account, &progress.folder).and_then(|f| mb.folder(f)).map(|f| f.name.clone()).unwrap_or_default(); let name: String = name.chars().filter(|c| !c.is_control()).take(60).collect(); let (read, total) = (progress.read.min(progress.total).to_string(), progress.total.to_string()); let (number, folders) = (progress.number.to_string(), progress.folders.to_string()); let template = if progress.total > 0 { lang.t("Receiving {name}: {read} of {total} · folder {number} of {folders}") } else { lang.t("Receiving {name} · folder {number} of {folders}") }; mailcraft_i18n::fmt(template, &[("name", &name), ("read", &read), ("total", &total), ("number", &number), ("folders", &folders)]) } /// The next messages of `account` to fetch whole in the background (R3 spec S12): not fetched, /// not gone, no larger than the account's "Download messages in full", within its mail to keep /// offline at `now` (#143: older mail is fetched when it is opened), newest first; `most` of /// them at most, by server id. pub fn fetch_queue(mb: &Mailbox, account: Id, most: usize, now: Timestamp) -> Vec { fetch_queue_passing_over(mb, account, most, now, &HashSet::new()) } /// [`fetch_queue`] without the messages in `passed_over` (by account and server id): those the /// server wouldn't give whole this session. pub fn fetch_queue_passing_over(mb: &Mailbox, account: Id, most: usize, now: Timestamp, passed_over: &HashSet<(Id, String)>) -> Vec { let skip = |id: &str| passed_over.contains(&(account, id.to_string())); // Messages being moved out of the account first, whatever their size: the move ends only // once they are whole here (S19). let mut out: Vec = mb .messages .values() .filter(|m| (still_to_fetch(mb, m) || wants_from_source(m)) && !m.incomplete) .filter_map(|m| m.moving_out.as_ref().filter(|out| out.account == account && !skip(&out.id)).map(|out| out.id.clone())) .take(most) .collect(); out.extend(fetch_queue_of_account(mb, account, most.saturating_sub(out.len()), now, &skip)); out } fn fetch_queue_of_account(mb: &Mailbox, account: Id, most: usize, now: Timestamp, skip: &dyn Fn(&str) -> bool) -> Vec { let limit = keeps_whole(mb, account); let window = OfflineWindow::of(mb, now); let mut waiting: Vec<(i64, &str)> = mb .messages .values() .filter(|m| m.account == account && still_to_fetch(mb, m) && window.within(m)) .filter_map(|m| { m.server.as_ref().filter(|s| !s.gone && limit.is_none_or(|limit| s.size <= limit) && !skip(&s.id)).map(|s| (m.received, s.id.as_str())) }) .collect(); let most = most.min(waiting.len()); if most == 0 { return Vec::new(); } // The newest `most`, without sorting them all. if most < waiting.len() { waiting.select_nth_unstable_by(most - 1, |a, b| b.0.cmp(&a.0)); waiting.truncate(most); } waiting.sort_by_key(|w| std::cmp::Reverse(w.0)); waiting.into_iter().map(|(_, id)| id.to_string()).collect() } /// How many messages of `account` are kept in full here, and how many of them are still to be /// fetched ([`full_counts`]). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct FullCounts { /// Messages from its server kept in full here, fetched or waiting to be: those within its /// mail to keep offline (#143). pub all: usize, /// Of those, the ones not fetched yet. pub waiting: usize, /// Of those waiting, the ones larger than its "Download messages in full" (they wait until /// they are opened). pub large: usize, } /// [`FullCounts`] of `account` at `now`, in one pass over the mailbox. Older mail isn't counted: /// it stays as its headers and preview (#143). pub fn full_counts(mb: &Mailbox, account: Id, now: Timestamp) -> FullCounts { let limit = keeps_whole(mb, account); let window = OfflineWindow::of(mb, now); let mut counts = FullCounts::default(); for m in mb.messages.values().filter(|m| m.account == account && (m.server.is_some() || m.unfetched) && window.within(m)) { counts.all = counts.all.saturating_add(1); if still_to_fetch(mb, m) { let size = m.server.as_ref().map_or(0, |s| s.size); counts.waiting = counts.waiting.saturating_add(1); counts.large = counts.large.saturating_add(usize::from(limit.is_some_and(|limit| size > limit))); } } counts } /// Take in a message fetched whole: its text, attachments and headers from its MIME, with what /// the mailbox keeps of it (where it is, its marks, its server state) as it was. Whether there /// was such a message. pub fn take_fetched(mb: &mut Mailbox, index: &mut ServerIndex, account: Id, id: &str, mime: &[u8]) -> bool { take_fetched_as(mb, index, account, id, mime).is_some() } /// [`take_fetched`], answering which message it was. pub fn take_fetched_as(mb: &mut Mailbox, index: &mut ServerIndex, account: Id, id: &str, mime: &[u8]) -> Option { let here = index.find(mb, account, id).or_else(|| moving_out(mb, account, id))?; // Only while it is still what it was when it was asked for (AU3-16): headers only, no change // of the owner's waiting to go up, not queued to be sent. An answer that comes after it was // fetched another way, edited or sent is dropped, so that it never undoes what was done here. if !mb.message(here).is_some_and(|m| still_to_fetch(mb, m) || wants_from_source(m)) { return None; } let mut read = crate::mime::parse_fetched(mime); // What came brought no original (#30): the server's copy isn't protected either. let brought = read.original.is_some(); // Within what the mailbox keeps of originals in all (#30), counting the one it replaces. let replaced = mb.message(here).and_then(|m| m.original.as_ref()).map_or(0, mailcraft_model::Original::size); // (No room for it: how much room it needs, which there must be before it is asked again.) let mut no_room = 0; let needs = read.original.as_ref().map_or(0, |o| o.size().saturating_sub(replaced)); if read.original.is_some() && !mb.room_for_original(needs) { read.original = None; no_room = needs.max(1); } let m = mb.message_mut(here).ok()?; // Check again asked for the bytes it came as alone (#30), or a move asked the copy it came // from for them: nothing else of it changes. Unless the server has had another version of it // since ([`Message::stale`], #566): what came is then taken whole, as any fetch's answer is, // and not left the old version with no fetch to come. if std::mem::take(&mut m.original_wanted) { m.original_asked |= !brought; if no_room > 0 { m.original_no_room = no_room; } if !m.stale { // (A copy that brings none, one put on the server from its parts, takes nothing away.) if !m.parts_removed && read.original.is_some() { m.original = read.original; } (m.unfetched, m.attachments_on_server) = (false, false); return Some(here); } } // What a listing left out of its recipients and subject comes with it whole, but for one of // them the download doesn't give, which stays as the listing said (#566). let listed = (read.to.is_empty() && !m.to.is_empty()) || (read.cc.is_empty() && !m.cc.is_empty()) || (read.subject.is_empty() && !m.subject.is_empty()); let listing_cut = m.headers_cut && listed; m.message_id = read.message_id; m.in_reply_to = read.in_reply_to; m.references = read.references; m.reply_to = read.reply_to; m.bcc = read.bcc; if !read.from.email.is_empty() { m.from = read.from; } if !read.to.is_empty() { m.to = read.to; } if !read.cc.is_empty() { m.cc = read.cc; } if !read.subject.is_empty() { m.subject = read.subject; } if read.date != 0 { m.date = read.date; } m.body = read.body; m.hidden_text = read.hidden_text; m.attachments = read.attachments; // The bytes it came as, when it is Digitally signed or Encrypted (#30): with its files from // the same download, so nothing removed here before comes back in one and not the other. // A copy that brings none (one put on the server from its parts, as Microsoft Graph puts a // message) leaves the one kept here: it is the message as it came, and what Save As and a // move to another server write. if read.original.is_some() { m.original = read.original; } m.parts_removed = false; (m.headers, m.header_block, m.fresh_stamps) = (read.headers, read.header_block, read.fresh_stamps); // (What a listing left out of its categories stays left out: they are the server's still, // [`Message::categories_cut`].) m.headers_cut = read.headers_cut || listing_cut; // Fetched to go on to another account's server: this server's stamps stay behind (#781). if m.account != account { mailcraft_model::auth_results::forget_stamps(m); } // What it is now is all of it: nothing the mailbox kept on disk of its preview is its body. m.kept = None; m.unfetched = false; m.stale = false; m.attachments_on_server = false; // Its size is that of the MIME brought (#300), which the fetch caps and which is measured // here: kept for when it is back to its preview, and taken anew at every fetch, so that a // draft changed elsewhere and fetched again doesn't keep the size it had. if let Some(server) = m.server.as_mut() { server.size = mime.len() as u64; } // A draft as the server has it now: what a change made here is worked out from. if m.draft { let state = mailcraft_model::sync::DraftState::of(m); let sd = m.server_draft.get_or_insert_with(Default::default); if !sd.pending { sd.synced = Some(state); } } Some(here) } /// The copies being moved out of `account` that a listing afresh of their folder hadn't come to /// when a complete listing of the folders left that folder out (#30): no page of it will come, so /// they went with it. What waited for the bytes it came as from one goes made of its parts, and /// says so (at most [`crate::cmd::imap_again::MAX_AGAIN`] are said). pub(crate) fn gone_with_folders(s: &mut crate::Session, account: Id, listing: &mailcraft_model::sync::FolderListing) -> Vec { if !listing.complete { return Vec::new(); } let listed: HashSet<&str> = listing.folders.iter().map(|f| f.id.as_str()).collect(); let gone: Vec = s .mailbox .messages .values() .filter(|m| m.moving_out.as_ref().is_some_and(|out| out.account == account && out.listing && !listed.contains(out.folder.as_str()))) .map(|m| m.id) .collect(); let mut said = Vec::new(); for id in gone { if let Ok(m) = s.mailbox.message_mut(id) && let Some(out) = m.moving_out.as_mut() { out.listing = false; } let Some(m) = s.mailbox.message(id) else { continue }; if said.len() < crate::cmd::imap_again::MAX_AGAIN && !m.may_release() && !s.original_readable(m, true) { let subject: String = m.subject.chars().filter(|c| !c.is_control()).take(80).collect(); said.push(format!( "\"{subject}\": the copy it was moved out of went with its folder on the server, so it goes made of its parts, without the bytes it came as" )); } } crate::cmd::mail::check_again::settle(&mut s.mailbox); said } /// Whether `m`, being moved out of a Microsoft account, asks the copy it came from there for the /// bytes it came as (#30, [`crate::cmd::mail::check_again::ask_source`]): fetched for them alone, /// all it has here staying as it is. pub(crate) fn wants_from_source(m: &Message) -> bool { m.original_wanted && !m.unfetched && m.moving_out.as_ref().is_some_and(|out| !out.left && !out.id.is_empty()) } /// Whether `m` is one to fetch whole, and may take what was fetched: it is still only its headers /// here, has no change of the owner's waiting to go up, and isn't in an Outbox. Only such a message /// is asked for, so that one whose answer would be dropped is never asked for again and again. pub(crate) fn still_to_fetch(mb: &Mailbox, m: &Message) -> bool { let queued = mb.folder(m.folder).is_some_and(|f| f.kind == FolderKind::Outbox); let edited = m.server_draft.as_ref().is_some_and(|d| d.pending); m.unfetched && !edited && !queued } /// Back to its headers and `preview` (#143): what [`take_fetched`] brought in goes, and the /// message is not fetched again, as it was when it came down. Everything else stays as it is /// (the bytes a protected message came as, #30, too): /// where it is, its marks, the server's state of it, what waits to go up, its headers. It wears /// the paperclip still when it had attachments; opening it fetches it whole again. pub fn thin_message(m: &mut Message, preview: String) { m.attachments_on_server = m.has_attachments(); m.body = Body { text: preview, html: None }; // What the sender hid goes with the text it came from: search and agents don't see it // for a message that is no longer here in full. m.hidden_text = String::new(); m.attachments.clear(); m.kept = None; // The bytes a Digitally signed or Encrypted message came as stay (#30): the server's copy // may be one put there from its parts, which brings none back. m.original_wanted = false; m.unfetched = true; } /// Once a mailbox (#185): the messages of Microsoft accounts fetched whole whose subject, names /// or text hold U+FFFD are marked not fetched, so that the background fetches them again and /// reads them with the character sets of today. Mail fetched before Windows-1250 and the rest /// were read was kept with U+FFFD where a character was (the MIME itself isn't kept). Their /// headers, preview, attachments and everything done to them here stay as they are until the /// new copy comes; [`take_fetched`] then writes the text over. Drafts, a message being moved /// out of its account, and one the server no longer has are left alone. Only what is in /// memory is looked at: the text of older mail kept on disk isn't read for this. How many were /// marked; none once the mailbox has been looked through ([`Mailbox::redecoded`]). pub fn refetch_unreadable(mb: &mut Mailbox) -> usize { if mb.redecoded { return 0; } const MARK: char = char::REPLACEMENT_CHARACTER; let graph: HashSet = mb.accounts.iter().filter(|a| a.kind == mailcraft_model::AccountKind::Microsoft).map(|a| a.id).collect(); // Without a Microsoft account there is nothing to look for, and the mailbox stays as it is. if graph.is_empty() { return 0; } mb.redecoded = true; let unreadable = |m: &Message| { let names = std::iter::once(&m.from).chain(&m.to).chain(&m.cc).chain(&m.reply_to).map(|a| a.name.as_str()); let texts = [m.subject.as_str(), m.body.text.as_str(), m.body.html.as_deref().unwrap_or("")]; names.chain(texts).any(|text| text.contains(MARK)) }; let mut marked = 0usize; for m in mb.messages.values_mut() { let whole_from_server = graph.contains(&m.account) && !m.unfetched && !m.incomplete && !m.draft && m.server_draft.is_none() && m.moving_out.is_none() && m.server.as_ref().is_some_and(|s| !s.gone && !s.id.is_empty()); if whole_from_server && unreadable(m) { let m = std::sync::Arc::make_mut(m); m.attachments_on_server = m.has_attachments(); m.unfetched = true; // Fetched whole: what comes is taken whole, original and all, not for the original // alone (#30). m.original_wanted = false; marked = marked.saturating_add(1); } } marked } /// Once a mailbox (#793): the messages of Microsoft and Exchange server accounts that look as /// 0.5.0 up to #793 put back a message whose delete for good the server refused (no text, or a /// file of no bytes, with nothing kept on disk for them: [`Message::looks_hollow`]) are marked /// not fetched, so that they are fetched whole again from the server, which has them, before /// anything moves or copies them anywhere (an empty copy would be made there and the original /// deleted). One being moved out of such an account is fetched from its original there. Drafts, /// and messages no server has, are left alone. How many were marked; none once the mailbox has /// been looked through ([`Mailbox::unhollowed`]). pub fn refetch_hollow(mb: &mut Mailbox) -> usize { if mb.unhollowed { return 0; } let servers: HashSet = mb .accounts .iter() .filter(|a| matches!(a.kind, mailcraft_model::AccountKind::Microsoft | mailcraft_model::AccountKind::Exchange)) .map(|a| a.id) .collect(); if servers.is_empty() { return 0; } mb.unhollowed = true; let mut marked = 0usize; for m in mb.messages.values_mut() { let on_server = servers.contains(&m.account) && m.server.as_ref().is_some_and(|s| !s.gone && !s.id.is_empty()); let original = m.moving_out.as_ref().is_some_and(|o| servers.contains(&o.account) && !o.left && !o.id.is_empty()); let candidate = !m.unfetched && !m.incomplete && !m.draft && m.server_draft.is_none() && m.held.is_empty() && (on_server || original); if candidate && m.looks_hollow() { let m = std::sync::Arc::make_mut(m); m.attachments_on_server = m.has_attachments(); m.unfetched = true; // Fetched whole: what comes is taken whole, original and all, not for the original // alone (#30). m.original_wanted = false; marked = marked.saturating_add(1); } } marked } /// How large a message of `account` is fetched whole in the background: its own Download /// messages in full, and for a shared mailbox its owner's (#70: it keeps as much mail as its /// owner's account does; there is no setting of its own). fn keeps_whole(mb: &Mailbox, account: Id) -> Option { let a = mb.owner_of(account).or(mb.account(account))?; a.download_in_full.limit() } #[cfg(test)] #[path = "graph_down_size_tests.rs"] mod size_tests; #[cfg(test)] #[path = "graph_down_flag_tests.rs"] mod flag_tests; #[cfg(test)] #[path = "graph_sent_not_draft_tests.rs"] mod sent_not_draft_tests; #[cfg(test)] mod tests { use mailcraft_model::sync::ServerMessage; use mailcraft_model::{AccountKind, DownloadInFull, FlagStatus, Folder, FolderKind, Mailbox, Message}; use super::*; fn mailbox() -> (Mailbox, Id, Id) { let mut mb = Mailbox::with_local_account("avery@mail.example", "Avery"); let account = mb.accounts[0].id; mb.accounts[0].kind = AccountKind::Microsoft; let folder = mb.alloc_id(); mb.folders.push(Folder { id: folder, account, name: "Inbox".into(), kind: FolderKind::Inbox, ..Default::default() }); for (n, size) in [(1, 1_000), (2, 30 << 20), (3, 0), (4, 2_000)] { let server = ServerMessage { id: format!("AAkA{n}"), folder: "F".into(), size, ..Default::default() }; let m = Message { folder, received: n * 60, unfetched: n != 4, server: Some(server), subject: format!("Preview {n}"), ..Default::default() }; mb.add_message(m).unwrap(); } (mb, account, folder) } #[test] fn the_newest_not_fetched_within_the_limit_come_first() { let (mut mb, account, _) = mailbox(); // Number 4 is fetched; number 2 is larger than 10 MB; number 3 didn't say its size. assert_eq!(fetch_queue(&mb, account, 10, 0), ["AAkA3", "AAkA1"]); assert_eq!(fetch_queue(&mb, account, 1, 0), ["AAkA3"]); assert_eq!(full_counts(&mb, account, 0), FullCounts { all: 4, waiting: 3, large: 1 }); mb.accounts[0].download_in_full = DownloadInFull::Everything; assert_eq!(fetch_queue(&mb, account, 10, 0), ["AAkA3", "AAkA2", "AAkA1"]); assert_eq!(full_counts(&mb, account, 0), FullCounts { all: 4, waiting: 3, large: 0 }); } #[test] fn a_fetched_message_keeps_what_was_done_to_it_here() { let (mut mb, account, folder) = mailbox(); let id = mb.messages.values().find(|m| m.subject == "Preview 1").unwrap().id; { let m = mb.message_mut(id).unwrap(); m.read = true; m.flag = FlagStatus::Flagged; } let mime = b"From: Kit \r\nTo: avery@mail.example\r\nSubject: The whole of it\r\nMessage-ID: \r\nContent-Type: text/plain\r\n\r\nAll of the text.\r\n"; let mut index = ServerIndex::default(); assert!(take_fetched(&mut mb, &mut index, account, "AAkA1", mime)); assert!(!take_fetched(&mut mb, &mut index, account, "AAkA9", mime)); let m = mb.message(id).unwrap(); assert_eq!((m.subject.as_str(), m.body.text.trim(), m.from.email.as_str()), ("The whole of it", "All of the text.", "kit@example.org")); assert!(!m.unfetched && m.read && m.flag == FlagStatus::Flagged && m.folder == folder && m.server.as_ref().unwrap().id == "AAkA1"); } /// R6.2 S5: the server's "has attachments" is kept on a message not fetched yet, so that it /// wears the paperclip and `has:attachment` finds it; once fetched, its own files say. #[test] fn a_message_not_fetched_yet_has_the_attachments_the_server_said() { let (mut mb, account, folder) = mailbox(); mb.folder_mut(folder).unwrap().server = Some(mailcraft_model::ServerFolder { id: "F".into(), ..Default::default() }); let page = |id: &str, has_attachments: bool| MessagePage { folder: "F".into(), messages: vec![GraphMessage { server: ServerMessage { id: id.into(), ..Default::default() }, subject: "Quarterly figures".into(), has_attachments, ..Default::default() }], ..Default::default() }; let mut index = ServerIndex::default(); take_page(&mut mb, &mut index, account, &page("AAkB1", true), &mut Down::default()); let id = mb.messages.values().find(|m| m.subject == "Quarterly figures").unwrap().id; let m = mb.message(id).unwrap(); assert!(m.unfetched && m.attachments.is_empty() && m.attachments_on_server && m.has_attachments()); let query = mailcraft_model::search::Query::parse("has:attachment"); assert!(query.matches(m, 0)); assert!(!query.matches(mb.messages.values().find(|m| m.subject == "Preview 1").unwrap(), 0)); // Written to the mailbox file and read back; a file from before has none. let json = serde_json::to_string(m).unwrap(); assert!(json.contains(r#""attachmentsOnServer":true"#), "{json}"); assert_eq!(&serde_json::from_str::(&json).unwrap(), m); assert!(!serde_json::from_str::(r#"{"id": 9, "unfetched": true}"#).unwrap().has_attachments()); // The server says it has none after all (its files were taken off): neither does it here. take_page(&mut mb, &mut index, account, &page("AAkB1", false), &mut Down::default()); assert!(!mb.message(id).unwrap().has_attachments()); take_page(&mut mb, &mut index, account, &page("AAkB1", true), &mut Down::default()); assert!(mb.message(id).unwrap().has_attachments()); // Fetched whole, without files: its own attachments say. let mime = b"From: Kit \r\nSubject: Quarterly figures\r\nContent-Type: text/plain\r\n\r\nNo files.\r\n"; assert!(take_fetched(&mut mb, &mut index, account, "AAkB1", mime)); let m = mb.message(id).unwrap(); assert!(!m.unfetched && !m.attachments_on_server && !m.has_attachments() && m.folder == folder); } #[test] fn a_message_fetched_whole_is_not_given_back_what_was_kept_on_disk_of_its_preview() { let (mut mb, account, _) = mailbox(); let id = mb.messages.values().find(|m| m.subject == "Preview 1").unwrap().id; // Its preview's HTML is on disk, not in memory (R6.1). let stale = mailcraft_model::ContentRef { sha256: [7; 32], len: 40 }; mb.message_mut(id).unwrap().kept = Some(Box::new(mailcraft_model::KeptBody { html: Some(stale), ..Default::default() })); let mime = b"From: Kit \r\nSubject: Plain\r\nContent-Type: text/plain\r\n\r\nOnly text.\r\n"; assert!(take_fetched(&mut mb, &mut ServerIndex::default(), account, "AAkA1", mime)); let m = mb.message(id).unwrap(); assert!(m.kept.is_none() && !m.body_is_kept() && m.body.html.is_none(), "{:?}", m.kept); } /// A message in Windows-1250 (#185): the sender's name, the subject and the text with the /// characters Windows-1250 has and Windows-1252 hasn't (đ, ž) or has elsewhere (’, –). pub(crate) const CENTRAL_EUROPEAN: &[u8] = b"From: =?windows-1250?Q?Tarik_Gvo=9E=F0ar?= \r\n\ To: Avery \r\nSubject: =?windows-1250?Q?Introduction_=96_Josh?=\r\nMessage-ID: \r\n\ MIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary=\"b\"\r\n\r\n--b\r\n\ Content-Type: text/plain; charset=windows-1250\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nHi Avery, we=92ve met.\r\n--b\r\n\ Content-Type: text/html; charset=windows-1250\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n\

Hi Avery, we=92ve met.

\r\n--b--\r\n"; #[test] fn mail_kept_with_unreadable_characters_is_fetched_again_once() { let (mut mb, account, folder) = mailbox(); // As it was kept before Windows-1250 was read: U+FFFD where đ, ž, ’ and – were. let broken = |n: u32, draft: bool| { let server = ServerMessage { id: format!("AAkC{n}"), folder: "F".into(), size: 2_000, ..Default::default() }; Message { folder, received: 9_000 + i64::from(n), server: Some(server), draft, from: mailcraft_model::Address::new("Tarik Gvo\u{FFFD}\u{FFFD}ar", "tarik@example.org"), subject: "Introduction \u{FFFD} Josh".into(), body: Body { text: "Hi Avery, we\u{FFFD}ve met.".into(), html: Some("

Hi Avery, we\u{FFFD}ve met.

".into()) }, read: true, flag: FlagStatus::Flagged, categories: vec!["Blue category".into()], ..Default::default() } }; let id = mb.add_message(broken(1, false)).unwrap(); // A draft is the owner's own: written over, what was written here would be lost. let draft = mb.add_message(broken(2, true)).unwrap(); // Only its body has one, and the body only in HTML. let mut html_only = broken(3, false); (html_only.from.name, html_only.subject, html_only.body.text) = ("Tarik".into(), "Plain".into(), "Plain".into()); let html_only = mb.add_message(html_only).unwrap(); let count = mb.messages.len(); assert_eq!(refetch_unreadable(&mut mb), 2); assert!(mb.redecoded); let m = mb.message(id).unwrap(); // Nothing disappears meanwhile: its headers and text are as they were until the new copy comes. assert!(m.unfetched && m.subject.starts_with("Introduction") && m.body.html.is_some()); assert!(mb.message(html_only).unwrap().unfetched && !mb.message(draft).unwrap().unfetched); assert!(fetch_queue(&mb, account, 10, 0).contains(&"AAkC1".to_string())); // Opened again: not looked through again. assert_eq!(refetch_unreadable(&mut mb), 0); let mut index = ServerIndex::default(); assert!(take_fetched(&mut mb, &mut index, account, "AAkC1", CENTRAL_EUROPEAN)); let m = mb.message(id).unwrap(); assert_eq!((m.from.name.as_str(), m.subject.as_str()), ("Tarik Gvožđar", "Introduction – Josh")); assert_eq!(m.body.text.trim(), "Hi Avery, we’ve met."); assert!(m.body.html.as_deref().unwrap().contains("we’ve met")); assert!(!m.unfetched && m.read && m.flag == FlagStatus::Flagged && m.categories == ["Blue category"] && m.folder == folder); assert_eq!(mb.messages.len(), count, "no copy"); // Its source is broken (bytes that aren't UTF-8 in a message that says it is): fetched // again it is as before, and nothing fetches it once more, after a save and an open too. let bad = b"From: Kit \r\nSubject: Still \xff\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nBroken \xff text.\r\n"; assert!(take_fetched(&mut mb, &mut index, account, "AAkC3", bad)); assert!(mb.message(html_only).unwrap().body.text.contains('\u{FFFD}')); let json = serde_json::to_string(&mb).unwrap(); assert!(json.contains(r#""redecoded":true"#)); let mut again: Mailbox = serde_json::from_str(&json).unwrap(); assert_eq!(refetch_unreadable(&mut again), 0); assert!(again.messages.values().all(|m| !m.unfetched || m.subject.starts_with("Preview"))); // A file from before has not been looked through. assert!(!serde_json::from_str::("{}").unwrap().redecoded); } #[test] fn mail_fetched_again_keeps_its_size_in_the_list() { // As a delta brings it: size 0, "not said" (#273). Marked to be fetched again for an // unreadable character, it keeps its body until the new copy comes, and the list says // that body's size meanwhile, not none. let (mut mb, _, folder) = mailbox(); let server = ServerMessage { id: "AAkC9".into(), folder: "F".into(), size: 0, ..Default::default() }; let text = format!("Hi Avery, we\u{FFFD}ve met. {}", "x".repeat(4000)); let whole = Message { folder, server: Some(server), body: Body { text, html: None }, ..Default::default() }; let id = mb.add_message(whole).unwrap(); assert_eq!(refetch_unreadable(&mut mb), 1); let m = mb.message(id).unwrap(); assert!(m.unfetched); assert_eq!(m.list_size(), Some(m.size())); assert!(m.size() > 4000); } } #[cfg(test)] #[path = "graph_down_hidden_tests.rs"] mod hidden_tests;