// SPDX-FileCopyrightText: MailCraft contributors // SPDX-License-Identifier: MIT OR Apache-2.0 //! An Exchange server account kept in step through Exchange Web Services (#252), E1 and E6: mail //! down. //! //! As for IMAP, Microsoft Graph and JMAP, the engine never talks to a server: [`ews_mail_request`] //! (`sync.ewsRequest`) says what a run should read, a host that can reach the server does it //! (`mailcraft_ews::sync_mail`, on a background thread), and [`apply_ews_mail`] //! (`sync.applyEws`) takes in what it brought back, piece by piece. The same goes for messages //! fetched whole: [`ews_fetch_request`] (`sync.ewsFetchRequest`) and [`apply_ews_fetch`] //! (`sync.applyEwsFetch`). All four are the host's side of a synchronisation, never an agent's. //! //! **Mail down, and changes up.** A run first takes up what was done here (folder changes, message //! changes, drafts, what waits in the Outbox: the same pieces as a Microsoft account's, worked out //! from the difference with what the server last said, `graph_up`, `graph_drafts`, `graph_send`), //! then reads the server's folders (a delta from the sync state the server gave) and each folder's //! messages as headers and a preview from the folder's sync state. Sending is through Exchange //! Web Services alone ([`can_queue_mail`]); a send whose answer was lost is held, as a Microsoft //! account's is. Undo is a reverse change, which the next run sends. //! //! **What comes back is hostile** (a host may hand over anything): the model holds a piece to //! sizes ([`mailcraft_model::sync::EwsMailResult::check`]), then [`super::ews_down::bounded`] cuts //! its text to what is shown and passes over what has an id that is no id. A piece that had to be //! cut removes nothing, as a run that went wrong removes nothing. //! //! **Taking in** goes the way of a Graph run, by server id: the folder list makes and updates the //! folders; each page is a page of a folder's changes ([`super::ews_down::take_page`]), taken in with //! the sync state that goes on from it; a folder read afresh (the server lost its state) marks its //! messages gone first, and at the end of a run that went well, what the fresh read didn't bring is //! removed ([`super::graph_down::sweep`]). Messages are matched by the server's item id, which stays //! with a message while it is where it is. A mark the server changed comes down, but for one //! changed here since the server last said. No server id and no sync state is logged. use mailcraft_model::sync::{ ArchiveSeen, ChangeOutcome, EwsFetchRequest, EwsFolderState, EwsMailRequest, EwsMailResult, EwsResolveRequest, EwsScope, FolderChange, FolderChangeDone, GraphFetchResult, MAX_DRAFTS, MAX_EWS_STATE_BYTES, MAX_EWS_STATES, MAX_FOLDER_CHANGES, MAX_MESSAGE_CHANGES, MAX_RESOLVE_TEXT, MAX_RESOLVE_TEXTS, MessageOutcome, SendOutcome, SyncPhase, one_line, }; use mailcraft_model::{Account, AccountKind, EwsServer, FolderKind, Id, Mailbox}; use serde_json::{Value, json}; use super::graph_down::{self, Down}; use super::graph_mail::{self, Taken}; use super::graph_up::{self, Refusals}; use super::sync_progress::{self, Piece}; use super::{bad, ews_down, fail}; use crate::{CmdError, CmdResult, CommandSpec, Session, p}; /// What a piece is refused with when the account would keep more sync state than is taken in. /// Most folders a run names as unreadable in its lines (the rest are counted in the log). const MAX_BLOCKED_SHOWN: usize = 8; const STATES_TOO_LARGE: &str = "the result would make this account keep more sync state than is taken in"; /// Why an Exchange server account is not sent and received as an IMAP account is, in words for a /// log or a command's error. pub const NOT_IMAP: &str = "an Exchange server account is sent and received through Exchange Web Services, not as an IMAP account is"; pub fn specs() -> Vec { vec![ CommandSpec::new("sync.ewsRequest", "Send/Receive with Exchange Web Services", "Send / Receive", request) .pure() .params(r#"{"account"?: number, "light"?: bool (the Inbox and the folder on screen only), "inboxPass"?: bool (the Inbox alone), "background"?: bool} — for an Exchange server account: answers what a host should read there."#) .when(available), CommandSpec::new("sync.applyEws", "Apply Send/Receive with Exchange Web Services", "Send / Receive", apply) .pure() .params(r#"{"result": EwsMailResult} — takes in what a host read from an Exchange server account."#), CommandSpec::new("sync.ewsFetchRequest", "Download Exchange Messages in Full", "Send / Receive", fetch_request) .pure() .params(r#"{"account"?: number, "message"?: number (one message, at once), "most"?: number (default 10)} — answers which messages of an Exchange server account a host should fetch whole: the one asked for, or the next ones in the background (within the account's mail to keep offline and its Download messages in full)."#) .when(available), CommandSpec::new("sync.applyEwsFetch", "Apply Downloaded Exchange Messages", "Send / Receive", apply_fetch) .pure() .params(r#"{"result": GraphFetchResult} — takes in messages a host fetched whole from an Exchange server account."#), CommandSpec::new("account.archive.hide", "Hide Online Archive", "Folder Pane", hide_archive) .params(r#"{"account": number (the online archive, or the Exchange server account it belongs to)} — takes the account's online archive off the folder pane, and the server isn't asked about it again until account.archive.show; nothing of it was kept on this computer, and nothing on the server changes. Answers {"hidden": owner id}"#) .when(available), CommandSpec::new("account.archive.show", "Show Online Archive", "Folder Pane", show_archive) .params(r#"{"account": number (an Exchange server account)} — shows the account's online archive again: the next Send/Receive asks the server, and adds it when there is one. Answers {"shown": owner id}"#) .when(available), ] } /// The Exchange server account `v` names, or the one whose online archive it names (#1818). fn archive_holder(s: &Session, v: &Value) -> Result { let id = p::id(v, "account").ok_or_else(|| bad("`account` is required"))?; let a = s.mailbox.account(id).ok_or_else(|| failed!("no account with id {id}", id))?; let owner = if a.is_archive() { s.mailbox.archive_owner(id).map(|o| o.id) } else { Some(id) }; owner .filter(|o| s.mailbox.account(*o).is_some_and(|o| o.kind == AccountKind::Exchange && o.ews.is_some())) .ok_or_else(|| failed!("{name} isn't an Exchange server account", name = a.name)) } fn hide_archive(s: &mut Session, v: &Value) -> CmdResult { let owner = archive_holder(s, v)?; if let Some(server) = s.mailbox.account_mut(owner).ok().and_then(|a| a.ews.as_mut()) { server.archive_hidden = true; } if let Some(archive) = s.mailbox.archive_of(owner).map(|a| a.id) { s.mailbox.remove_archive(archive).map_err(|e| fail(e.to_string()))?; } if s.mailbox.folder(s.view.folder).is_none() { s.view.folder = s.mailbox.folder_of_kind(owner, FolderKind::Inbox).unwrap_or(0); s.sel.messages.clear(); } s.mailbox_changed(); Ok(json!({"hidden": owner})) } fn show_archive(s: &mut Session, v: &Value) -> CmdResult { let owner = archive_holder(s, v)?; if let Some(server) = s.mailbox.account_mut(owner).ok().and_then(|a| a.ews.as_mut()) { (server.archive_hidden, server.archive_absent) = (false, 0); } s.mailbox_changed(); Ok(json!({"shown": owner})) } /// Why the commands are out of reach, when they are: no account is an Exchange server account. fn available(s: &Session) -> Option<&'static str> { if s.mailbox.accounts.iter().any(|a| a.kind == AccountKind::Exchange) { None } else { Some("no account is an Exchange server account") } } /// The Exchange server account a command names, or the first there is. fn named_account(s: &Session, v: &Value) -> Result { p::id(v, "account") .or_else(|| s.mailbox.accounts.iter().find(|a| a.kind == AccountKind::Exchange).map(|a| a.id)) .ok_or_else(|| fail("no account is an Exchange server account")) } /// Whether `account` is an Exchange server account whose server can be asked (for its address /// list, People's Address Book). pub fn has_address_list(mb: &Mailbox, account: Id) -> bool { ews_account(mb, account).is_ok() } /// The account `account` and the server it is reached at, when it is an Exchange server account /// whose server settings may be used ([`EwsServer::problem`]: a mailbox file can say anything). /// An online archive is reached at its owner's server, with its owner's sign-in (#1818). fn ews_account(mb: &Mailbox, account: Id) -> Result<(&Account, &EwsServer), CmdError> { let a = mb.account(account).ok_or_else(|| failed!("no account with id {id}", id = account))?; if a.kind != AccountKind::Exchange { return Err(failed!("{name} isn't an Exchange server account", name = a.name)); } let holder = if a.is_archive() { mb.archive_owner(account).ok_or_else(|| failed!("the account {name} is the online archive of is gone", name = a.name))? } else { a }; let server = holder.ews.as_ref().filter(|server| server.problem().is_none()); let server = server.ok_or_else(|| failed!("{name} has no Exchange server address that can be used", name = a.name))?; Ok((a, server)) } /// The request for `account` with what every run says: where the server is, whose mailbox it is. fn base(s: &Session, account: Id) -> Result { let (a, server) = ews_account(&s.mailbox, account)?; Ok(EwsMailRequest { account, email: a.email.clone(), url: server.url.clone(), username: server.username.clone(), trusted: server.trusted.clone(), archive: a.is_archive(), ..Default::default() }) } /// What an Exchange run of `account` should read. The host adds the password, which is never here. /// /// - **Scope.** `light`: the Inbox and the folder on screen, for a run nobody waits on; otherwise /// every folder ([`EwsScope::All`]). /// - **The folder list** is read in a run that reads every folder, and in the first run of the /// account (none of its folders is the server's yet): from the state the server gave, so that /// only what changed comes. /// - **States.** Where each folder goes on from: its sync state, empty for a folder not read yet. /// - **What goes up** (the same in a light run): the changes of folders and of messages made here /// since the server said, the drafts saved here, and what waits in the Outbox, as for a Microsoft /// account ([`graph_mail::folder_changes`], [`graph_up::message_changes`]). The Inbox pass /// ([`ews_inbox_request`]) sends and changes nothing. pub fn ews_mail_request(s: &Session, account: Id, light: bool) -> Result { let mut request = base(s, account)?; let mb = &s.mailbox; // (Where the account's own folder list goes on from: an archive's is its own, #1818.) let own = mb.account(account).and_then(|a| a.ews.clone()).unwrap_or_default(); let listed = mb.folders.iter().any(|f| f.account == account && f.server.is_some()); let server_id = |folder: Id| mb.folder(folder).filter(|f| f.account == account).and_then(|f| f.server.as_ref()).map(|server| server.id.clone()); let inbox = mb.folder_of_kind(account, FolderKind::Inbox).and_then(server_id); // An online archive is read the way Outlook reads it (#1818): its folder list, and the mail of // the folder on screen when that is one of its folders (and of its Inbox); never all of it. request.scope = if light || request.archive { // (The Inbox is read anyway.) let on_screen = server_id(s.view.folder).filter(|id| Some(id) != inbox.as_ref()); EwsScope::Light { also: on_screen.into_iter().collect() } } else { EwsScope::All }; request.changes = graph_mail::folder_changes(mb, account, MAX_FOLDER_CHANGES).0; request.messages = graph_up::message_changes(s, account, MAX_MESSAGE_CHANGES).0; request.drafts = super::graph_drafts::draft_changes(s, account, MAX_DRAFTS).0; request.sends = super::graph_send::sends(s, account); request.list_folders = !light || !listed; // (A folder list read before the folder above each was asked for is read afresh, once.) request.hierarchy = if own.parents { own.hierarchy.clone() } else { String::new() }; // Whether the mailbox has an online archive is asked in a run that lists the folders (#1818), // and not while its owner has it hidden (account.archive.hide). request.probe_archive = request.list_folders && !request.archive && !own.archive_hidden; let wanted = |id: &str| match &request.scope { EwsScope::All => true, EwsScope::Light { also } => inbox.as_deref() == Some(id) || also.iter().any(|a| a == id), }; request.states = mb .folders .iter() .filter(|f| f.account == account) .filter_map(|f| f.server.as_ref()) .filter(|server| wanted(&server.id)) .take(MAX_EWS_STATES) .map(|server| EwsFolderState { folder: server.id.clone(), state: server.sync_state.clone() }) .collect(); // The conversation ids of mail listed before they were asked for, in a run of every folder (#795). if !light { request.threads = super::thread_ids::to_ask(mb, account); } Ok(request) } /// The run that reads the Inbox alone, from its sync state: the first thing a Send/Receive does for /// each account, so that new mail shows before the long read of the rest. None until the account's /// Inbox is the server's. pub fn ews_inbox_request(s: &Session, account: Id) -> Option { let mb = &s.mailbox; let inbox = mb.folder_of_kind(account, FolderKind::Inbox).and_then(|f| mb.folder(f)).and_then(|f| f.server.as_ref())?; let mut request = base(s, account).ok()?; request.inbox_pass = true; request.scope = EwsScope::Light { also: Vec::new() }; request.states = vec![EwsFolderState { folder: inbox.id.clone(), state: inbox.sync_state.clone() }]; Some(request) } fn request(s: &mut Session, v: &Value) -> CmdResult { let account = named_account(s, v)?; let mut request = if p::bool(v, "inboxPass").unwrap_or(false) { ews_inbox_request(s, account).ok_or_else(|| fail("the Inbox of this account hasn't been read yet"))? } else { ews_mail_request(s, account, p::bool(v, "light").unwrap_or(false))? }; request.background = p::bool(v, "background").unwrap_or(false); // A run begins: nothing lost in an earlier one is held against it. if !request.inbox_pass { s.ews_run_cut.remove(&account); } serde_json::to_value(request).map_err(|e| fail(e.to_string())) } /// Which messages of `account` a host should fetch whole: `message` at once (the owner opened it, /// or is replying to it), else the next `most` in the background: those that aren't here whole, /// within the account's mail to keep offline and its Download messages in full /// ([`graph_down::fetch_queue_passing_over`]). pub fn ews_fetch_request(s: &Session, account: Id, message: Option, most: usize) -> Result { let (a, server) = ews_account(&s.mailbox, account)?; let ids = match message { Some(id) => { let m = s .mailbox .message(id) .filter(|m| m.account == account) .ok_or_else(|| failed!("no message with id {id} in {name}", id, name = a.name))?; m.server .as_ref() .filter(|server| (graph_down::still_to_fetch(&s.mailbox, m) || s.asked_for_save(id)) && ews_down::is_id(&server.id)) .map(|server| vec![server.id.clone()]) .unwrap_or_default() } None => graph_down::fetch_queue_passing_over(&s.mailbox, account, most.clamp(1, 100), s.now(), &s.graph_unfetchable) .into_iter() .filter(|id| ews_down::is_id(id)) .collect(), }; Ok(EwsFetchRequest { account, email: a.email.clone(), url: server.url.clone(), username: server.username.clone(), trusted: server.trusted.clone(), ids, background: message.is_none(), }) } /// The names `texts` to look up in the address list of the account the message being written goes /// from (#1262), when that is an Exchange server account whose server may be used: each name one /// line, at most [`MAX_RESOLVE_TEXT`] characters, each once, at most [`MAX_RESOLVE_TEXTS`] of them. /// Nothing but the names goes to the server. pub fn ews_resolve_request(s: &Session, texts: &[String]) -> Result { let compose = s.compose.as_ref().ok_or_else(|| fail("no message is being written"))?; let account = s .mailbox .account(compose.account) .or(s.mailbox.default_account()) .map(|a| a.id) .ok_or_else(|| fail("add an account first (File › Add Account)"))?; ews_resolve_request_for(s, account, texts) } /// What the address list of the Exchange server account `account` is asked for `texts` (People's /// Address Book: what the owner searches for). pub fn ews_resolve_request_for(s: &Session, account: Id, texts: &[String]) -> Result { let (a, server) = ews_account(&s.mailbox, account)?; let mut asked: Vec = Vec::new(); for text in texts { let text = one_line(text, MAX_RESOLVE_TEXT); if !text.is_empty() && !asked.iter().any(|seen| seen.to_lowercase() == text.to_lowercase()) && asked.len() < MAX_RESOLVE_TEXTS { asked.push(text); } } Ok(EwsResolveRequest { account, email: a.email.clone(), url: server.url.clone(), username: server.username.clone(), trusted: server.trusted.clone(), texts: asked, }) } fn fetch_request(s: &mut Session, v: &Value) -> CmdResult { let account = named_account(s, v)?; let most = p::id(v, "most").and_then(|n| usize::try_from(n).ok()).unwrap_or(10); serde_json::to_value(ews_fetch_request(s, account, p::id(v, "message"), most)?).map_err(|e| fail(e.to_string())) } fn apply(s: &mut Session, v: &Value) -> CmdResult { // What comes back is hostile: read as the type says, or refused, whatever it holds. let result = v.get("result").cloned().ok_or_else(|| bad("`result` is required"))?; let result: EwsMailResult = serde_json::from_value(result).map_err(|_| fail("`result` isn't an Exchange Send/Receive result"))?; apply_ews_mail(s, result) } fn apply_fetch(s: &mut Session, v: &Value) -> CmdResult { let result = v.get("result").cloned().ok_or_else(|| bad("`result` is required"))?; let result: GraphFetchResult = serde_json::from_value(result).map_err(|_| fail("`result` isn't a fetch result"))?; apply_ews_fetch(s, result) } /// The account a result is for, when it may take it in: an Exchange server account, the one the /// run was for, and at the server the run read when the result says which (`url`). (An answer for /// another account, the mailbox having been replaced while the run was under way, would tie this /// one's folders to a stranger's server; so would an answer from the server an account named /// before its address was changed.) fn receiving(mb: &Mailbox, account: Id, email: &str, url: &str) -> Result { let (a, server) = ews_account(mb, account)?; if !email.is_empty() && !a.email.trim().eq_ignore_ascii_case(email.trim()) { return Err(failed!("this Send/Receive was for another account, not for {name}", name = a.name)); } if !url.is_empty() && server.url.trim() != url.trim() { return Err(failed!("this Send/Receive was for the server {name} named before", name = a.name)); } Ok(a.name.clone()) } /// Take in a piece of what an Exchange run brought back (pieces come in the order the run made /// them, each whole in itself): the folder list, then the pages of the folders, and at the end of a /// run that went well, the messages that a fresh read left out are removed. Not an undo step: the /// server is the source of truth, but for what was changed here since it last said. /// /// The piece is held to sizes first ([`mailcraft_model::sync::EwsMailResult::check`], then /// [`ews_down::bounded`]); a piece that had to be cut, a run that went wrong, was stopped, or was /// not a read of every folder removes nothing. /// /// Answers what it changed, with the keys a Graph run's answer has (`added`, `updated` and /// `removed` folders; `new`, `changed`, `gone` and `removedMessages` messages; `errors`, /// `busyFor`, `summary`), `authRefused` when the server turned the password down and `untrusted` /// (the certificate's fingerprint) when the run stopped at a certificate the owner is to be asked about. pub fn apply_ews_mail(s: &mut Session, result: EwsMailResult) -> CmdResult { let mut taken = apply_ews_news(s, result); let said = super::snooze::after(s); super::snooze::tell(&mut taken, said); taken } fn apply_ews_news(s: &mut Session, result: EwsMailResult) -> CmdResult { let account = result.account; let taken = take_in(s, result); // A piece that was refused is a piece lost: the end of its run removes nothing. if taken.is_err() { s.ews_run_cut.insert(account); } taken } /// What a run said of `owner`'s online archive (#1818): one that is there is added (unless the /// owner hid it); one the server said twice in a row is not there goes, with what was held of it /// (nothing of it is kept on the computer anyway). Whether the mailbox changed. fn take_archive(s: &mut Session, owner: Id, seen: ArchiveSeen) -> bool { let Some(server) = s.mailbox.account(owner).filter(|a| !a.is_archive()).and_then(|a| a.ews.clone()) else { return false }; let have = s.mailbox.archive_of(owner).map(|a| a.id); let absent = match seen { ArchiveSeen::Unknown => return false, ArchiveSeen::Present => 0, ArchiveSeen::Absent => server.archive_absent.saturating_add(1).min(2), }; let mut changed = false; if absent != server.archive_absent && let Some(server) = s.mailbox.account_mut(owner).ok().and_then(|a| a.ews.as_mut()) { server.archive_absent = absent; changed = true; } match (seen, have) { (ArchiveSeen::Present, None) if !server.archive_hidden && !server.cloud_archive => { changed |= s.mailbox.add_archive(owner).is_ok(); log::info!("ews: the mailbox has an online archive; it is shown"); } (ArchiveSeen::Absent, Some(archive)) if absent >= 2 => { changed |= s.mailbox.remove_archive(archive).is_ok(); log::info!("ews: the server says the mailbox has no online archive; it is no longer shown"); } _ => {} } changed } fn take_in(s: &mut Session, result: EwsMailResult) -> CmdResult { result.check().map_err(|why| failed!("the result {why}", why = mailcraft_i18n::Arg::words(why)))?; let account = result.account; let name = receiving(&s.mailbox, account, &result.email, &result.url)?; let (result, cut) = ews_down::bounded(result); if ews_down::state_bytes_after(&s.mailbox, account, &result) > MAX_EWS_STATE_BYTES { // (The sync states kept for one account are held to a total, as one is held to a size: a // piece that would go over is refused whole, and what it was for is read again.) return Err(fail(STATES_TOO_LARGE)); } if s.file_is_current() { s.note_saved(); } let mut taken = Taken::default(); let mut down = Down::of_run(s, account); // What became of what went up, as for a Microsoft account: the folders, the messages (a // refusal is said once per kind and reason), the drafts, and what was sent. let mut errors = result.errors.clone(); for done in &result.changes { if let Some(said) = graph_mail::change_done(&mut s.mailbox, account, &done.change, &done.outcome, s.interface_language, &mut taken) { errors.push(said); } } // A folder deleted here that the server refused to delete comes back only in a read of the // folder list afresh (#779): its state goes, and the run's folder piece, when the run gets // that far, sets it again. A run that stops first (a 429, called off, a read that failed) // leaves the next one to read the list afresh. let refused_delete = |done: &FolderChangeDone| { matches!( (&done.change, &done.outcome), (FolderChange::Delete { .. }, ChangeOutcome::Refused { .. } | ChangeOutcome::OwnFolder { .. } | ChangeOutcome::NotShown { .. }) ) }; if result.changes.iter().any(refused_delete) && let Some(server) = s.mailbox.account_mut(account).ok().and_then(|a| a.ews.as_mut()) { server.hierarchy.clear(); } let mut refusals = Refusals::default(); for done in &result.messages { if graph_up::message_done(&mut s.mailbox, account, done, &mut refusals) { match done.outcome { MessageOutcome::Done { .. } => taken.pushed += 1, _ => taken.updated += 1, } // (A moved message has an id of its own in its new folder.) s.graph_index = Default::default(); } } let mut refused = refusals.lines(); for done in &result.drafts { if super::graph_drafts::draft_done(&mut s.mailbox, account, done, s.interface_language, &mut refused) { taken.updated += 1; s.graph_index = Default::default(); } } let mut sent = 0; for done in &result.sent { let sent_at = s.now(); if super::graph_send::send_done_at(&mut s.mailbox, account, done, s.interface_language, &mut refused, Some(sent_at)) { sent += usize::from(matches!(done.outcome, SendOutcome::Sent)); taken.updated += 1; s.graph_index = Default::default(); } } errors.extend(refused.iter().cloned()); s.graph_said.extend(refused); if let Some(deltas) = &result.folders { let lost; (down.removed, lost) = ews_down::take_folders(&mut s.mailbox, account, deltas, &mut taken); // The folder list goes on from here (with the folders it brought, in one step), or from // its start when one of the server's own folders is missing here (#779). (A read from its // start past a folder it couldn't read goes on from where it ended too: what is deleted // after it comes in the deltas, #1108.) if (lost || !result.hierarchy.is_empty()) && let Some(server) = s.mailbox.account_mut(account).ok().and_then(|a| a.ews.as_mut()) { server.hierarchy = if lost { String::new() } else { result.hierarchy.clone() }; // A list read from its start has each folder's parent, and so has what goes on from it. server.parents |= !lost && deltas.afresh; } } let mut index = std::mem::take(&mut s.graph_index); // A page that doesn't follow the folder's state is left out ([`ews_down::take_page`]): what was // lost before it is not skipped over, and a run that left a page out removes nothing. let mut left_out = 0usize; for page in &result.pages { if !ews_down::take_page(&mut s.mailbox, &mut index, account, page, &mut down) { left_out = left_out.saturating_add(1); } } s.graph_index = index; down.end_piece(s, account, result.partial); // A rule moved new mail out of the account (#567): there is no going back past it // ([`super::graph_mail::apply_graph_mail`] says why). if down.left > 0 { s.forget_history(); } // The run's end: what its fresh reads left out goes, when every folder was read and it went // well (the host says `whole` of such a run only). // A page lost anywhere in the run, not in this piece alone, keeps the end from sweeping. if cut || left_out > 0 || !result.blocked.is_empty() { s.ews_run_cut.insert(account); } let run_cut = s.ews_run_cut.contains(&account); if !result.partial { s.ews_run_cut.remove(&account); } if result.whole && !result.partial && !run_cut { graph_down::sweep(&mut s.mailbox, account, None, &mut down); } // The conversations of mail listed before they were asked for (#795). let threaded = super::thread_ids::take(&mut s.mailbox, account, &result.threads); // What the server said of an online archive (#1818). let archived = result.archive.is_some_and(|seen| take_archive(s, account, seen)); if archived || taken != Taken::default() || down.took_anything() || !result.pages.is_empty() || threaded { s.mailbox_changed(); } // The view stays on a folder that is there. if s.mailbox.folder(s.view.folder).is_none() { s.view.folder = s.mailbox.folder_of_kind(account, FolderKind::Inbox).unwrap_or(0); s.sel.messages.clear(); } let lang = s.interface_language; if result.auth_refused { errors.insert(0, mailcraft_i18n::fmt(lang.t("The server turned down the sign-in for {name}: sign in again"), &[("name", &name)])); } if !result.untrusted.is_empty() { errors.insert(0, mailcraft_i18n::fmt(lang.t("The certificate of the server of {name} needs your say"), &[("name", &name)])); } // The folders that hold a change that can't be taken in stay where they were, and are named as // the owner knows them (no id, no state): one line each, up to a few. let blocked: Vec = result .blocked .iter() .filter_map(|id| s.mailbox.folder_by_server(account, id).and_then(|f| s.mailbox.folder(f)).map(|f| sync_progress::shown(&f.name))) .take(MAX_BLOCKED_SHOWN) .collect(); for folder in &blocked { errors.push(mailcraft_i18n::fmt(lang.t("{folder} couldn't be read from the server"), &[("folder", folder)])); } // Send/Receive Progress: where the run is, what it brought, what went wrong. let step = match (&result.progress, &result.folders) { (Some(progress), _) => { let name = s.mailbox.folder_by_server(account, &progress.folder).and_then(|f| s.mailbox.folder(f)).map(|f| f.name.as_str()); Some((SyncPhase::NewMail, sync_progress::shown(name.unwrap_or_default()), progress.read.min(progress.total), progress.total)) } (None, Some(_)) => Some((SyncPhase::Folders, String::new(), 0, 0)), (None, None) => None, }; let piece = Piece { step, folders: result.progress.as_ref().map(|p| (p.number, p.folders)), new: down.new, sent, problems: &errors, last: !result.partial, stopped: result.stopped, timings: Some(&result.timings), }; s.sync_progress.take(account, piece); let summary = errors.first().cloned().unwrap_or_else(|| "Up to date".to_string()); if !result.partial { // What the server refused in this run, said once, ahead of anything else. let refused = std::mem::take(&mut s.graph_said); let fresh: Vec = refused.into_iter().filter(|line| !errors.contains(line)).collect(); errors.splice(0..0, fresh); } if result.partial { // Under way: how far it has got. if let Some(progress) = &result.progress { s.sync_status = graph_down::progress_words(&s.mailbox, account, progress, lang); } } else { let said = match errors.first() { Some(first) => first.clone(), None if result.stopped => lang.t("Stopped; carries on from here next time").to_string(), None => lang.t("Up to date").to_string(), }; let t = s.local_now(); s.sync_status = format!("{said} — {}", s.time_said(t)); } Ok(json!({ "pushed": taken.pushed, "added": taken.added, "updated": taken.updated, "removed": taken.removed, "errors": errors, "busyFor": result.busy_for, "authRefused": result.auth_refused, "untrusted": result.untrusted, "unreadableFolders": blocked, "stopped": result.stopped, "new": down.new, "changed": down.changed, "gone": down.gone, "removedMessages": down.removed, "summary": summary, })) } /// Take in messages of an Exchange server account fetched whole, as a Microsoft account's are /// ([`graph_mail::apply_graph_fetch`]), once the result is held to sizes ([`ews_down::bounded_fetch`]). /// Not an undo step. Only for the account they were fetched for. /// /// A message the server said it no longer has stays as it is (the next read says whether it is /// gone), and the background doesn't ask for it again this session; nor for one the server /// wouldn't give, as for Graph. pub fn apply_ews_fetch(s: &mut Session, result: GraphFetchResult) -> CmdResult { let account = result.account; receiving(&s.mailbox, account, &result.email, "")?; let result = ews_down::bounded_fetch(result); let missing: Vec<(Id, String)> = result.missing.iter().map(|id| (account, id.clone())).collect(); let answer = graph_mail::apply_graph_fetch(s, result)?; s.graph_unfetchable.extend(missing); Ok(answer) } #[cfg(test)] #[path = "ews_mail_tests.rs"] mod tests; #[cfg(test)] #[path = "ews_up_tests.rs"] pub(crate) mod up_tests; #[cfg(test)] #[path = "ews_well_known_tests.rs"] mod well_known_tests;