From 9b941ccb6665e73fe4dfd20acfe6a7092709f84a Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Wed, 7 Oct 2026 13:06:05 -0500 Subject: [PATCH 01/88] Show a saved-file notice, save from the reader, and offer a folder for .eml - A card above the status bar names the saved file and its folder, with Open folder... and a dismiss button. It hides after eight seconds and stays while hovered. The status-bar notice is unchanged. - The reader toolbar has a Save as .eml button, and a right-click on the message text offers Save as .eml and Save as .eml to folder... - The message menu offers Save as .eml to folder... beside Save as .eml. - mail.exportEml takes an optional directory: an absolute path in normal form. A chosen folder must already exist and pass the same ownership checks as Downloads, and it is never created. Downloads is unchanged. - attachment.sh folder asks omarchy-file-select --directory (the desktop portal) or zenity for one folder, out of process like the file picker. The standalone app does not offer folder rows. --- Makefile | 1 + backend-api.json | 16 +- scripts/attachment.sh | 26 +++- src/backend/mail.rs | 5 +- src/mail/export.rs | 115 +++++++++++++- src/mail/types.rs | 32 +++- tests/test_attachment.sh | 34 +++++ ui/App.qml | 2 + ui/Service.qml | 9 +- ui/account/MailAccount.qml | 12 +- ui/account/MessageActions.js | 33 +++- ui/components/EmlSavedToast.qml | 185 +++++++++++++++++++++++ ui/components/Icons.js | 2 +- ui/components/MessageMenu.qml | 18 ++- ui/components/MessageReader.qml | 36 +++++ ui/components/TextMenu.qml | 30 +++- ui/keys/Keymap.js | 2 +- ui/tests/qml/tst_eml_saved_toast.qml | 112 ++++++++++++++ ui/tests/qml/tst_message_menu_export.qml | 33 +++- ui/tests/qml/tst_reader_mode.qml | 3 + ui/tests/qml/tst_reader_text_menu.qml | 63 +++++++- ui/tests/test_eml_export.js | 46 ++++++ ui/tests/test_icons.js | 4 + 23 files changed, 797 insertions(+), 22 deletions(-) create mode 100644 ui/components/EmlSavedToast.qml create mode 100644 ui/tests/qml/tst_eml_saved_toast.qml diff --git a/Makefile b/Makefile index 9c7690b..9a57d8c 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryContr ui/components/LabelPicker.qml \ ui/components/UndoSendToast.qml \ ui/components/DraftSavedToast.qml \ + ui/components/EmlSavedToast.qml \ ui/components/SearchBar.qml \ ui/components/AppMenu.qml \ ui/components/AccountSwitcher.qml \ diff --git a/backend-api.json b/backend-api.json index 07f7250..fdc4dda 100644 --- a/backend-api.json +++ b/backend-api.json @@ -1361,6 +1361,19 @@ "equals": { "message": "mail_account_unknown" } + }, + { + "name": "mail.exportEml refuses a folder that is not an absolute plain path", + "method": "mail.exportEml", + "params": { + "account": "imap:sender@example.org", + "id": "1:INBOX", + "directory": "../Downloads" + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } } ], "unreleased": { @@ -1375,7 +1388,8 @@ "mail.exportEml rejects unknown fields", "mail.exportEml rejects a malformed UID before provider work", "mail.exportEml rejects controlled filename hints", - "mail.exportEml rejects account controls before normalization" + "mail.exportEml rejects account controls before normalization", + "mail.exportEml refuses a folder that is not an absolute plain path" ] } } diff --git a/scripts/attachment.sh b/scripts/attachment.sh index d0e69d7..60ab0f9 100755 --- a/scripts/attachment.sh +++ b/scripts/attachment.sh @@ -4,6 +4,7 @@ # attachment.sh read # attachment.sh clipboard # attachment.sh pick +# attachment.sh folder # attachment.sh forget # # One JSON object on stdout. Quickshell's Process.write() never closes stdin, @@ -279,6 +280,26 @@ pick_files() { emit_paths "$list" } +# One folder, through the same out-of-process chooser, for Save as .eml. +pick_folder() { + list= + if command -v omarchy-file-select >/dev/null 2>&1; then + list=$(omarchy-file-select --title 'Save .eml to' --directory 2>/dev/null || true) + elif command -v zenity >/dev/null 2>&1; then + list=$(zenity --file-selection --directory --title='Save .eml to' 2>/dev/null || true) + else + fail_json "No folder picker is available" + fi + list=$(printf '%s\n' "$list" | sed -n '1p') + # The backend takes a folder only in its plain form, so no trailing slash. + while [ "${#list}" -gt 1 ] && [ "${list%/}" != "$list" ]; do list=${list%/}; done + case "$list" in + /*) ;; + *) fail_json "cancelled" ;; + esac + emit_paths "$list" +} + command=${1:-} case "$command" in read) @@ -292,12 +313,15 @@ case "$command" in pick) pick_files ;; + folder) + pick_folder + ;; forget) [ -n "${2:-}" ] && [ -n "${3:-}" ] || fail_json "That file is not a draft attachment" forget_file "$2" "$3" ;; *) - printf '%s\n' 'usage: attachment.sh read | clipboard | pick | forget ' >&2 + printf '%s\n' 'usage: attachment.sh read | clipboard | pick | folder | forget ' >&2 exit 2 ;; esac diff --git a/src/backend/mail.rs b/src/backend/mail.rs index 505a3e4..ed95bbc 100644 --- a/src/backend/mail.rs +++ b/src/backend/mail.rs @@ -394,7 +394,10 @@ impl Session { "mail.exportEml" => { let request = ExportRequest::try_from(params)?; let refusals = crate::account::refusals_readonly(&request.account.id)?; - let downloads = crate::platform::dirs::AppDirs::discover()?.downloads; + let downloads = match &request.directory { + Some(chosen) => chosen.clone(), + None => crate::platform::dirs::AppDirs::discover()?.downloads, + }; let control = crate::mail::export::ExportControl::new(); crate::mail::export::export_with( request, diff --git a/src/mail/export.rs b/src/mail/export.rs index 3958e43..96d60f3 100644 --- a/src/mail/export.rs +++ b/src/mail/export.rs @@ -1,4 +1,5 @@ -//! Save one message's original bytes as `.eml` in the user's Downloads folder. +//! Save one message's original bytes as `.eml` in the user's Downloads folder, +//! or in a folder the user picked. //! //! The bytes come from the provider's raw-message operation (`imap.rawMessage`) //! and are written unchanged. The `mail.exportEml` RPC contract is fixed; this @@ -323,6 +324,10 @@ async fn run_export( let downloads = downloads.to_path_buf(); let deadline_std = deadline.into_std(); let cancel = control.cancel.clone(); + // Downloads is created on first use. A folder the user picked must already + // exist and pass the same ownership checks: a picker answer is never a + // reason to create directories. + let create = request.directory.is_none(); // Storage is synchronous create/write/fsync, so it runs on a blocking // worker rather than a Tokio worker. The destination directory is anchored // to a descriptor before the deadline is observed, so a path swap after the @@ -330,7 +335,7 @@ async fn run_export( // point: a cancellation or an expired deadline observed before it leaves no // file; after it the write completes and success is returned. let path = tokio::task::spawn_blocking(move || { - let dir = crate::platform::private_fs::directories(&downloads, &[], true) + let dir = crate::platform::private_fs::directories(&downloads, &[], create) .map_err(|_| "mail_export_write_failed")? .ok_or("mail_export_write_failed")?; write_unique_in(&downloads, &dir, &filename, &bytes, deadline_std, &cancel) @@ -532,6 +537,52 @@ mod tests { dir } + #[cfg(unix)] + #[test] + fn a_chosen_folder_must_be_an_absolute_plain_path() { + if crate::mail::tests::isolated() { + return; + } + let fixture = crate::mail::tests::account_fixture(json!({ + "version": 1, + "activeId": "imap:owner@example.org", + "accounts": [{"provider": "imap", "email": "owner@example.org", + "imap": {"username": "owner@example.org"}}] + })); + let before = crate::mail::tests::fixture_tree(&fixture.root); + let parse = |directory: Value| { + ExportRequest::try_from(&json!({ + "account": "imap:owner@example.org", "id": "1:INBOX", "directory": directory + })) + .map(|request| request.directory) + }; + assert_eq!( + parse(json!("/home/owner/Mail")), + Ok(Some(PathBuf::from("/home/owner/Mail"))) + ); + for refused in [ + json!(""), + json!("Mail"), + json!("./Mail"), + json!("/home/owner/../other"), + json!("/home/owner/./Mail"), + json!("/home/owner//Mail"), + json!("/home/owner/Mail/"), + json!("/home/owner/Mail\n"), + json!("/home/owner/\u{0}Mail"), + json!(null), + json!(7), + ] { + assert_eq!(parse(refused.clone()), Err("invalid_params"), "{refused}"); + } + let request = ExportRequest::try_from(&json!({ + "account": "imap:owner@example.org", "id": "1:INBOX" + })) + .unwrap(); + assert_eq!(request.directory, None, "no folder means Downloads"); + assert_eq!(crate::mail::tests::fixture_tree(&fixture.root), before); + } + #[test] fn export_requires_an_explicit_account_without_creating_state() { if crate::mail::tests::isolated() { @@ -771,6 +822,7 @@ mod tests { }, id: "42:INBOX".into(), suggested_name: "Project update".into(), + directory: None, } } @@ -1113,6 +1165,65 @@ mod tests { fs::remove_dir_all(dir).unwrap(); } + // A picked folder receives the file, and one that is missing is refused + // rather than created: only Downloads is made on first use. + #[tokio::test] + async fn a_chosen_folder_is_written_to_but_never_created() { + let dir = scratch("chosen"); + let limits = ExportLimits::new(); + let adapter = Fake { + payload: json!({"bytes": 4, "data": STANDARD.encode(b"body")}), + }; + let mut chosen = request(); + chosen.directory = Some(dir.clone()); + let saved = export_with( + chosen, + &adapter, + &dir, + &Value::Null, + &limits, + &ExportControl::new(), + ) + .await + .unwrap(); + assert_eq!( + Path::new(saved["path"].as_str().unwrap()), + dir.join("Project update.eml") + ); + assert_eq!(fs::read(dir.join("Project update.eml")).unwrap(), b"body"); + + let missing = dir.join("not-there"); + let mut chosen = request(); + chosen.directory = Some(missing.clone()); + assert_eq!( + export_with( + chosen, + &adapter, + &missing, + &Value::Null, + &limits, + &ExportControl::new() + ) + .await, + Err("mail_export_write_failed") + ); + assert!(!missing.exists(), "a picked folder is never created"); + + let downloads = dir.join("Downloads"); + let made = export_with( + request(), + &adapter, + &downloads, + &Value::Null, + &limits, + &ExportControl::new(), + ) + .await; + assert!(made.is_ok(), "{made:?}"); + assert!(downloads.join("Project update.eml").is_file()); + fs::remove_dir_all(dir).unwrap(); + } + // A swap of the destination directory after it is anchored must not // redirect the file: the handle still names the original directory. #[cfg(unix)] diff --git a/src/mail/types.rs b/src/mail/types.rs index 10adbc2..bfb6f95 100644 --- a/src/mail/types.rs +++ b/src/mail/types.rs @@ -381,13 +381,15 @@ pub struct ExportRequest { pub account: Account, pub id: String, pub suggested_name: String, + /// A folder the user picked. `None` saves to Downloads. + pub directory: Option, } impl TryFrom<&Value> for ExportRequest { type Error = &'static str; fn try_from(value: &Value) -> Result { - let object = params_object(value, &["account", "id", "suggestedName"])?; + let object = params_object(value, &["account", "id", "suggestedName", "directory"])?; let wanted = object .get("account") .and_then(Value::as_str) @@ -404,14 +406,42 @@ impl TryFrom<&Value> for ExportRequest { if suggested_name.chars().any(char::is_control) { return Err("invalid_params"); } + let directory = export_directory(object.get("directory"))?; Ok(Self { account, id, suggested_name, + directory, }) } } +/// A chosen destination is an absolute, already normalized folder path. The +/// writer still anchors it by descriptor and refuses a missing or unsafe one; +/// this only rejects what can never name a folder before any work starts. +fn export_directory(value: Option<&Value>) -> Result, &'static str> { + let Some(value) = value else { + return Ok(None); + }; + let text = value.as_str().ok_or("invalid_params")?; + let path = std::path::Path::new(text); + // `components()` quietly drops an inner `.`, a doubled or trailing + // separator, so the path must also be exactly its own normal form. + let normal: PathBuf = path.components().collect(); + if text.is_empty() + || text.len() > 4096 + || text.chars().any(char::is_control) + || !path.is_absolute() + || normal.as_os_str() != path.as_os_str() + || path + .components() + .any(|part| matches!(part, std::path::Component::ParentDir)) + { + return Err("invalid_params"); + } + Ok(Some(path.to_path_buf())) +} + /// Export needs an explicit account. An empty or unknown id is refused and /// never falls back to the registry's active account, unlike `resolve_account`. fn resolve_export_account(wanted: &str) -> Result { diff --git a/tests/test_attachment.sh b/tests/test_attachment.sh index 4b89566..ce0862b 100755 --- a/tests/test_attachment.sh +++ b/tests/test_attachment.sh @@ -219,6 +219,40 @@ answer=$(sh "$script" pick) check "a cancelled picker is cancelled, not an error" "$(json_field error "$answer")" "cancelled" unset OMAMAIL_PICK_OUT OMAMAIL_PICK_EXIT +# A folder for Save as .eml: the chooser is asked for a directory, and only +# its first absolute answer is returned. +cat > "$work/bin/omarchy-file-select" <<'STUB' +#!/bin/sh +printf '%s\n' "$*" > "$OMAMAIL_PICK_ARGS" +printf '%s\n' "$OMAMAIL_PICK_OUT" +exit "${OMAMAIL_PICK_EXIT:-0}" +STUB +chmod +x "$work/bin/omarchy-file-select" +OMAMAIL_PICK_ARGS=$work/pick-args +OMAMAIL_PICK_OUT=$(printf '%s\n' "/home/ada/Mail archive" /home/ada/other) +export OMAMAIL_PICK_ARGS OMAMAIL_PICK_OUT +answer=$(sh "$script" folder) +check "folder says ok" "$(json_field ok "$answer")" "True" +picked=$(printf '%s' "$answer" | python3 -c 'import json,sys; print("\n".join(json.load(sys.stdin)["paths"]))') +check "folder returns only the first folder" "$picked" "/home/ada/Mail archive" +case "$(cat "$OMAMAIL_PICK_ARGS")" in + *--directory*) ok "folder asks the chooser for a directory" ;; + *) bad "folder asks the chooser for a directory" ;; +esac +OMAMAIL_PICK_OUT=/home/ada/Mail/ +answer=$(sh "$script" folder) +picked=$(printf '%s' "$answer" | python3 -c 'import json,sys; print(json.load(sys.stdin)["paths"][0])') +check "a trailing slash is dropped" "$picked" "/home/ada/Mail" +OMAMAIL_PICK_OUT=relative/folder +answer=$(sh "$script" folder) +check "a relative folder is refused as cancelled" "$(json_field error "$answer")" "cancelled" +OMAMAIL_PICK_EXIT=1 +OMAMAIL_PICK_OUT= +export OMAMAIL_PICK_EXIT OMAMAIL_PICK_OUT +answer=$(sh "$script" folder) +check "a cancelled folder chooser is cancelled" "$(json_field error "$answer")" "cancelled" +unset OMAMAIL_PICK_OUT OMAMAIL_PICK_EXIT OMAMAIL_PICK_ARGS + # forget only deletes files this script wrote into the compose dir. printf 'keep\n' > "$work/outside.txt" sh "$script" forget "$work/compose" "$work/outside.txt" >/dev/null 2>&1 || true diff --git a/ui/App.qml b/ui/App.qml index fcee5b3..3c54d36 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -2426,6 +2426,8 @@ Item { panelFontFamily: root.fontFamily } + EmlSavedToast { app: root; clearance: reader.visible ? reader.footerHeight : 0 } + // --------------------------------------------------------- status bar Item { diff --git a/ui/Service.qml b/ui/Service.qml index ebaa919..050093e 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -537,12 +537,12 @@ Item { return true } - function chooseFiles(callback) { + function chooseFiles(callback, folder) { if (platform && typeof platform.chooseFiles === "function") - return platform.chooseFiles(callback) + return !folder && platform.chooseFiles(callback) var request = hostProcessComponent.createObject(root, { operation: "result", callback: callback, - command: [root.pluginDir + "/scripts/attachment.sh", "pick"] + command: [root.pluginDir + "/scripts/attachment.sh", folder ? "folder" : "pick"] }) if (!request) { if (typeof callback === "function") callback(({ok:false,error:"No file picker is available"})) @@ -2250,6 +2250,8 @@ Item { function canExportEmlFor(id) { return MessageActions.canExportEmlFor(root, id) } function accountForMessage(id) { return MessageActions.accountForMessage(root, id) } function exportEmlFor(accountId, id) { return MessageActions.exportEmlFor(root, accountId, id) } + function exportEmlToFolder(accountId, id) { return MessageActions.exportEmlToFolder(root, accountId, id) } + signal emlSaved(var result) function exportEml(id) { return MessageActions.exportEml(root, id) } function exportFromView(view, cursorId) { return MessageActions.exportFromView(root, view, cursorId) } function markAllRead() { return MessageActions.markAllRead(root) } @@ -2683,6 +2685,7 @@ Item { onReplySent: function(sendId) { root.replySent(String(sendId || "")) } onReplyFailed: function(sendId) { root.forwardReplyFailure(index, sendId) } onMonitoredMigrated: function(ids) { root.setMonitoredIds(index, ids) } + onEmlSaved: function(result) { root.emlSaved(result) } // What a merged list is made of, and everything a merged list says // about itself. `recount` is not enough and is deliberately not used: diff --git a/ui/account/MailAccount.qml b/ui/account/MailAccount.qml index e92e2b8..54e24ec 100644 --- a/ui/account/MailAccount.qml +++ b/ui/account/MailAccount.qml @@ -1998,7 +1998,8 @@ Item { return runNativeAction([], "markRead", false, false, true) } - // Save the message's original bytes as .eml in Downloads. One request per + // Save the message's original bytes as .eml in Downloads, or in a folder + // the user picked. One request per // mailbox at a time, and the notice names the file the backend actually // wrote. The id is this mailbox's own, resolved by the service. property bool exportingEml: false @@ -2011,8 +2012,11 @@ Item { return "" } - function exportEml(id) { + signal emlSaved(var result) + + function exportEml(id, directory) { var target = String(id || "") + var folder = String(directory || "") if (!ready || !backend || target === "") return false if (!canExportEml) { fail("This mailbox cannot save messages as .eml") @@ -2024,6 +2028,7 @@ Item { } clearNotice() var parameters = {account: accountId, id: target, suggestedName: subjectForEml(target)} + if (folder !== "") parameters.directory = folder exportingEml = true exportingEmlId = target // The busy state is visible from the moment the work starts, not only when @@ -2033,10 +2038,11 @@ Item { root.exportingEml = false root.exportingEmlId = "" if (error) { - root.fail(MessageActions.exportErrorText(error)) + root.fail(MessageActions.exportErrorText(error, folder)) return } root.note(MessageActions.exportSavedNotice(result)) + root.emlSaved(result) }) return true } diff --git a/ui/account/MessageActions.js b/ui/account/MessageActions.js index 0b402ad..a05a382 100644 --- a/ui/account/MessageActions.js +++ b/ui/account/MessageActions.js @@ -35,7 +35,7 @@ function accountForMessage(service, id) { // message, captured by a caller that must keep them across an account // switch. A non-unified view refuses when the captured mailbox is no longer // the one on screen: 42:INBOX is a different message in the next account. -function exportEmlFor(service, accountId, id) { +function exportEmlFor(service, accountId, id, directory) { var target = String(id || "") if (target === "") return false if (!service.backendCanExportEml) { @@ -59,7 +59,19 @@ function exportEmlFor(service, accountId, id) { owner.fail("This mailbox cannot save messages as .eml") return false } - return owner.exportEml(target) + return owner.exportEml(target, directory) +} +// The same export into a folder the user picks first. Only the desktop plugin +// has the out-of-process chooser, so callers offer it where `standalone` is +// false. The boundary above runs again once the chooser answers, so an +// account switch while it was open still refuses. +function exportEmlToFolder(service, accountId, id) { + if (String(id || "") === "" || !service || service.standalone || !service.backendCanExportEml) return false + return service.chooseFiles(function(answer) { + var folder = answer && answer.ok && answer.paths ? String(answer.paths[0] || "") : "" + if (folder !== "") exportEmlFor(service, accountId, id, folder) + else if (answer && answer.error !== "cancelled") service.fail(String(answer.error || "No folder picker is available")) + }, true) } function exportEml(service, id) { if (!canExportEmlFor(service, id)) { @@ -104,6 +116,19 @@ function exportSavedNotice(result) { return at > 0 ? "Saved " + name + " to " + path.substring(0, at) : "Saved " + (name || "the message as .eml") } +// What the saved-file toast shows: the name, and the folder it went to with +// the home directory shortened, so the folder is the part that stays visible. +function exportSavedFile(result, home) { + var path = String(result && result.path || "") + var at = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + var folder = at > 0 ? path.substring(0, at) : "" + var root = String(home || "") + var shown = root !== "" && (folder === root || folder.indexOf(root + "/") === 0) + ? "~" + folder.substring(root.length) : folder + return { name: String(result && result.filename || "") || path.substring(at + 1), + folder: folder, shownFolder: shown } +} + // The backend refuses with a code; the status line says what happened. var EXPORT_ERRORS = { mail_export_too_large: "This message is larger than 25 MB, so it was not saved", @@ -116,8 +141,10 @@ var EXPORT_ERRORS = { request_cancelled: "Saving the message as .eml was cancelled", backend_needs_update: "Saving .eml needs a newer Omamail backend" } -function exportErrorText(error) { +function exportErrorText(error, directory) { var code = String(error && error.message || error || "") + if (code === "mail_export_write_failed" && String(directory || "") !== "") + return "Could not write the .eml file to " + directory return EXPORT_ERRORS.hasOwnProperty(code) ? EXPORT_ERRORS[code] : "Could not save the message as .eml" } diff --git a/ui/components/EmlSavedToast.qml b/ui/components/EmlSavedToast.qml new file mode 100644 index 0000000..3117dc5 --- /dev/null +++ b/ui/components/EmlSavedToast.qml @@ -0,0 +1,185 @@ +import QtQuick +import Quickshell +import qs.Commons +import qs.Ui +import "../account/MessageActions.js" as MessageActions + +// Says that a Save as .eml finished, and where the file went. The status line +// says it too, but a caption at the window's edge was easy to miss for a file +// written somewhere on disk; this card names the file and the folder, and +// offers to open that folder. It listens to the service itself, so the window +// only places it. +Rectangle { + id: root + + // The window, for its service and theme colours. One property rather than + // seven, because App.qml sits against the repository's file-size ceiling. + required property var app + readonly property var service: app ? app.service : null + readonly property color textColor: app ? app.foreground : Color.foreground + readonly property color dimColor: app ? app.dim : textColor + readonly property color accentColor: app ? app.accent : Color.accent + readonly property color popupBackgroundColor: app ? app.popupBackground : Qt.rgba(0, 0, 0, 1) + readonly property color popupBorderColor: app ? app.popupBorder : textColor + readonly property string panelFontFamily: app ? app.fontFamily : Style.font.family + // The widest the card may grow; the name and folder elide inside it. + property real maximumWidth: Style.space(440) + // Long enough to read a path and reach the button, short enough not to + // linger over the next message. + property int timeout: 8000 + + // The file the backend wrote, which can carry a " (2)" the subject did not. + property string filename: "" + // The folder as shown, home shortened to ~, and the real one to open. + property string folderLabel: "" + property string folder: "" + readonly property bool shown: filename !== "" + + function show(result) { + var saved = MessageActions.exportSavedFile(result, Quickshell.env("HOME")) + filename = saved.name + folder = saved.folder + folderLabel = saved.shownFolder + if (shown) hideTimer.restart() + } + function dismiss() { + hideTimer.stop() + filename = "" + folder = "" + folderLabel = "" + } + function openFolder() { + var target = folder + dismiss() + if (target !== "" && root.service && typeof root.service.openExternal === "function") + root.service.openExternal(target) + } + + Connections { + target: root.service + ignoreUnknownSignals: true + function onEmlSaved(result) { root.show(result) } + } + + Timer { + id: hideTimer + interval: root.timeout + onTriggered: root.dismiss() + } + + readonly property real chromeWidth: mark.width + openButton.width + closeButton.width + + content.spacing * 3 + Style.space(18) + + // How much of the window's foot is taken above the status line: the + // reader's toolbar while a message is open. The card is raised past it, + // because that toolbar's right end holds the very save button that brought + // the card up, and a notice must not cover the control it reports on. + property real clearance: 0 + + // Placed by itself, at the right end where the draft and send toasts sit, + // because the window file has no room left to spare. + anchors.right: parent ? parent.right : undefined + anchors.rightMargin: Style.space(16) + anchors.bottom: parent ? parent.bottom : undefined + anchors.bottomMargin: Style.space(28) + Math.max(0, clearance) + Style.space(12) + z: 80 + visible: shown + implicitWidth: Math.min(maximumWidth, + Math.max(nameText.implicitWidth, folderText.implicitWidth) + chromeWidth) + implicitHeight: Math.max(content.implicitHeight + Style.space(14), Style.space(48)) + width: implicitWidth + height: implicitHeight + radius: Style.cornerRadius + color: Qt.rgba(popupBackgroundColor.r, popupBackgroundColor.g, + popupBackgroundColor.b, 1) + border.width: Style.normalBorderWidth + border.color: popupBorderColor + + Accessible.role: Accessible.AlertMessage + Accessible.name: "Saved " + root.filename + " in " + root.folderLabel + + // Hovering holds the card, so it does not leave from under the pointer. + HoverHandler { + onHoveredChanged: if (hovered) hideTimer.stop(); else if (root.shown) hideTimer.restart() + } + + Row { + id: content + anchors.left: parent.left + anchors.leftMargin: Style.space(12) + anchors.right: parent.right + anchors.rightMargin: Style.space(6) + anchors.verticalCenter: parent.verticalCenter + spacing: Style.space(10) + + ActionIcon { + id: mark + anchors.verticalCenter: parent.verticalCenter + name: "check" + color: root.accentColor + iconSize: Style.font.icon + width: Style.font.icon + height: Style.font.icon + } + + Column { + anchors.verticalCenter: parent.verticalCenter + width: Math.max(0, content.width - mark.width - openButton.width + - closeButton.width - content.spacing * 3) + spacing: Style.space(2) + + Text { + id: nameText + objectName: "eml-saved-name" + width: parent.width + text: "Saved " + root.filename + textFormat: Text.PlainText + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + elide: Text.ElideMiddle + } + Text { + id: folderText + objectName: "eml-saved-folder" + width: parent.width + visible: root.folderLabel !== "" + text: "in " + root.folderLabel + textFormat: Text.PlainText + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + // The end of a path is the part that tells folders apart. + elide: Text.ElideLeft + } + } + + Button { + id: openButton + objectName: "eml-saved-open-folder" + anchors.verticalCenter: parent.verticalCenter + visible: root.folder !== "" + text: "Open folder..." + foreground: root.accentColor + accent: root.accentColor + bordered: true + fontFamily: root.panelFontFamily + fontSize: Style.font.caption + onClicked: root.openFolder() + } + + IconButton { + id: closeButton + objectName: "eml-saved-close" + anchors.verticalCenter: parent.verticalCenter + iconName: "close" + iconSize: Style.font.iconSmall + size: Style.space(22) + tooltipText: "Dismiss" + foreground: root.dimColor + hoverColor: root.textColor + fontFamily: root.panelFontFamily + onClicked: root.dismiss() + } + } +} diff --git a/ui/components/Icons.js b/ui/components/Icons.js index 1d70f8d..5d60db7 100644 --- a/ui/components/Icons.js +++ b/ui/components/Icons.js @@ -19,7 +19,7 @@ var GLYPHS = { replyAll: 0xF0F1F, // reply-all-outline forward: 0xF0932, // share-outline — the mail-forward arrow, not "next" archive: 0xF120E, // archive-outline - download: 0xF0DA9, // download-outline — keeping the file, not opening it + download: 0xF0B8F, // download-outline — keeping the file, not opening it trash: 0xF0A7A, // trash-can-outline — the can, not the bin spam: 0xF0CE6, // alert-octagon-outline unread: 0xF01F0, // email-outline diff --git a/ui/components/MessageMenu.qml b/ui/components/MessageMenu.qml index 681aa3c..2c8114a 100644 --- a/ui/components/MessageMenu.qml +++ b/ui/components/MessageMenu.qml @@ -36,7 +36,7 @@ Item { property string nativeId: "" readonly property var menuRows: [continueRow, replyRow, replyAllRow, forwardRow, archiveRow, unarchiveRow, moveRow, - trashRow, spamRow, readRow, starRow, exportRow, browserRow, aiRow] + trashRow, spamRow, readRow, starRow, exportRow, exportFolderRow, browserRow, aiRow] // Whether this message is archived — out of the inbox and not somewhere // that has its own verb. Read off the summary the menu was opened on rather // than asked of the service, because the menu is about one message. IMAP @@ -156,6 +156,15 @@ Item { root.service.exportEmlFor(account, id) } + // The same, into a folder the user picks before anything is fetched. + function exportMessageToFolder() { + var account = root.ownerAccountId + var id = root.nativeId + menu.close() + if (root.service && typeof root.service.exportEmlToFolder === "function") + root.service.exportEmlToFolder(account, id) + } + QQC.Popup { id: menu width: Style.space(200) @@ -279,6 +288,13 @@ Item { // browser does. onActivated: root.exportMessage() } + MenuRow { + id: exportFolderRow + objectName: "message-menu-export-eml-folder" + visible: exportRow.visible && root.service.standalone !== true + text: "Save as .eml to folder..." + onActivated: root.exportMessageToFolder() + } MenuSeparatorLine { width: menu.width - menu.leftPadding - menu.rightPadding diff --git a/ui/components/MessageReader.qml b/ui/components/MessageReader.qml index e2533eb..dafea2e 100644 --- a/ui/components/MessageReader.qml +++ b/ui/components/MessageReader.qml @@ -117,6 +117,18 @@ Item { }) } + // Save as .eml from the reader: the message on screen, to Downloads or to a + // folder picked first. The owner is captured now, before any chooser opens. + readonly property bool canSaveEml: !!service && !!summary && !isDraft + && typeof service.canExportEmlFor === "function" && service.canExportEmlFor(selectedId) + readonly property bool canSaveEmlToFolder: canSaveEml && service.standalone !== true + function saveEml(chooseFolder) { + if (!canSaveEml) return false + var id = selectedId + if (!chooseFolder) return service.exportEml(id) + return service.exportEmlToFolder(service.accountForMessage(id), service.sourceIdFor(id)) + } + function scrollBy(steps) { var maximum = Math.max(0, bodyFlick.contentHeight - bodyFlick.height) bodyFlick.contentY = Math.max(0, Math.min(maximum, @@ -748,6 +760,9 @@ Item { // The toolbar sits on its own ground rather than on whatever happens to be // scrolled behind it. Transparent, the message ran underneath the icons and // through the rule, which read as text overlapping the controls. + // What the toolbar takes from the bottom, for a card that must sit above it. + readonly property real footerHeight: footerBackdrop.visible ? footerBackdrop.height : 0 + Rectangle { id: footerBackdrop anchors.left: parent.left @@ -1001,6 +1016,7 @@ Item { y: actionsRow.stacked ? messageActions.height + Style.space(4) : 0 implicitWidth: modeTrack.width + (openWebButton.visible ? Style.space(6) + openWebButton.width : 0) + + (saveEmlButton.visible ? Style.space(6) + saveEmlButton.width : 0) implicitHeight: Math.max(modeTrack.height, openWebButton.visible ? openWebButton.height : 0) width: implicitWidth @@ -1061,6 +1077,23 @@ Item { fontFamily: root.panelFontFamily onClicked: if (root.service && root.summary) root.service.openInBrowser(root.selectedId) } + // Takes the message out of the window as a file, like the browser + // button takes it out to the web. A click saves to Downloads; the + // body's right-click menu and the list's menu also offer a folder. + IconButton { + id: saveEmlButton + objectName: "reader-save-eml-button" + visible: root.canSaveEml + x: (openWebButton.visible ? openWebButton.x + openWebButton.width : modeTrack.width) + + Style.space(6) + y: modeTrack.y + height: modeTrack.height + iconName: "download" + tooltipText: "Save as .eml to Downloads · ctrl+shift+s" + foreground: root.dimColor; hoverColor: root.textColor + fontFamily: root.panelFontFamily + onClicked: root.saveEml(false) + } } } } @@ -1107,6 +1140,9 @@ Item { if (root.service && typeof root.service.copyText === "function") root.service.copyText(text) } onOpenLinkRequested: function(url) { root.openLink(url) } + canSaveEml: root.canSaveEml + canSaveEmlToFolder: root.canSaveEmlToFolder + onSaveEmlRequested: function(chooseFolder) { root.saveEml(chooseFolder) } } ImagePopover { diff --git a/ui/components/TextMenu.qml b/ui/components/TextMenu.qml index 750857e..140ca23 100644 --- a/ui/components/TextMenu.qml +++ b/ui/components/TextMenu.qml @@ -29,7 +29,12 @@ Item { property int cursorIndex: -1 readonly property bool opened: menu.opened readonly property bool hasSelection: !!target && String(target.selectedText || "") !== "" - readonly property var menuRows: [cutRow, copyRow, pasteRow, selectAllRow, openLinkRow, copyLinkRow] + // The reader's body offers the message itself as a file too. Off in the + // composer, which has no saved message yet. + property bool canSaveEml: false + property bool canSaveEmlToFolder: false + readonly property var menuRows: [cutRow, copyRow, pasteRow, selectAllRow, openLinkRow, copyLinkRow, + saveEmlRow, saveEmlFolderRow] readonly property alias cutRow: cutRow readonly property alias copyRow: copyRow @@ -37,12 +42,15 @@ Item { readonly property alias selectAllRow: selectAllRow readonly property alias openLinkRow: openLinkRow readonly property alias copyLinkRow: copyLinkRow + readonly property alias saveEmlRow: saveEmlRow + readonly property alias saveEmlFolderRow: saveEmlFolderRow signal copyRequested(string text) // Paste is the owner's: a compose form tries the clipboard for an image // before it pastes text, and only it knows how. signal pasteRequested(var target) signal openLinkRequested(string url) + signal saveEmlRequested(bool chooseFolder) anchors.fill: parent z: 50 @@ -175,6 +183,26 @@ Item { text: "Copy URL" onActivated: { var url = root.link; menu.close(); root.copyRequested(url) } } + + MenuSeparatorLine { + visible: root.canSaveEml + width: menu.width - menu.leftPadding - menu.rightPadding + lineColor: root.textColor + } + MenuRow { + id: saveEmlRow + objectName: "text-menu-save-eml" + visible: root.canSaveEml + text: "Save as .eml" + onActivated: { menu.close(); root.saveEmlRequested(false) } + } + MenuRow { + id: saveEmlFolderRow + objectName: "text-menu-save-eml-folder" + visible: root.canSaveEml && root.canSaveEmlToFolder + text: "Save as .eml to folder..." + onActivated: { menu.close(); root.saveEmlRequested(true) } + } } } diff --git a/ui/keys/Keymap.js b/ui/keys/Keymap.js index 218066e..7df5240 100644 --- a/ui/keys/Keymap.js +++ b/ui/keys/Keymap.js @@ -77,7 +77,7 @@ var BINDINGS = [ hint: { list: "trash", reader: "trash" } }, { id: "star", keys: ["s"], contexts: MAIL, group: "Acting", label: "Star or unstar" }, - // Save the original message bytes to Downloads. + // Save the original message bytes to Downloads. The menus also offer a folder. { id: "exportEml", keys: ["Ctrl+Shift+S"], contexts: MAIL, group: "Acting", label: "Save as .eml" }, // `v` because that is the key Gmail moves a message with, and issue #58 asks diff --git a/ui/tests/qml/tst_eml_saved_toast.qml b/ui/tests/qml/tst_eml_saved_toast.qml new file mode 100644 index 0000000..03e3ac2 --- /dev/null +++ b/ui/tests/qml/tst_eml_saved_toast.qml @@ -0,0 +1,112 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../../components" as Omamail + +// The saved-file card: it appears when the service says a .eml was written, +// names the file and the folder, opens that folder on request, and goes away +// on its own or when dismissed. +Item { + id: window + width: 900 + height: 600 + + QtObject { + id: fakeService + property var opened: [] + signal emlSaved(var result) + function openExternal(target) { opened = opened.concat([String(target)]); return true } + } + + // Stands in for App.qml: the card reads the service and the theme off it. + QtObject { + id: fakeApp + property var service: fakeService + property color foreground: Qt.rgba(1, 1, 1, 1) + property color dim: Qt.rgba(0.6, 0.6, 0.6, 1) + property color accent: Qt.rgba(1, 0.5, 0, 1) + property color popupBackground: Qt.rgba(0.1, 0.1, 0.1, 1) + property color popupBorder: Qt.rgba(0.4, 0.4, 0.4, 1) + property string fontFamily: "monospace" + } + + Omamail.EmlSavedToast { + id: toast + app: fakeApp + timeout: 300 + } + + TestCase { + name: "EmlSavedToast" + when: windowShown + + function named(item, name) { + if (!item) return null + if (item.objectName === name) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var hit = named(values[i], name) + if (hit) return hit + } + return null + } + + function saved(path, filename) { + fakeService.emlSaved({ ok: true, path: path, filename: filename, bytes: 10 }) + wait(10) + } + + function init() { + toast.clearance = 0 + toast.dismiss() + fakeService.opened = [] + } + + function test_nothing_shows_until_a_file_is_saved() { + compare(toast.visible, false) + } + + function test_a_save_names_the_file_and_the_folder() { + saved("/srv/mail/Project update (2).eml", "Project update (2).eml") + compare(toast.visible, true) + compare(named(toast, "eml-saved-name").text, "Saved Project update (2).eml") + compare(named(toast, "eml-saved-folder").text, "in /srv/mail") + verify(toast.x + toast.width <= window.width, "the card stays inside the window") + verify(toast.y + toast.height <= window.height) + } + + function test_the_card_sits_above_the_reader_toolbar() { + saved("/srv/mail/Note.eml", "Note.eml") + var resting = toast.y + toast.clearance = 44 + verify(toast.y + toast.height <= window.height - 28 - 44, + "the card clears the status line and the toolbar above it") + compare(toast.y, resting - 44) + toast.clearance = 0 + } + + function test_open_folder_opens_the_folder_and_closes_the_card() { + saved("/srv/mail/Note.eml", "Note.eml") + var button = named(toast, "eml-saved-open-folder") + verify(button && button.visible) + button.clicked() + compare(fakeService.opened, ["/srv/mail"], "the folder, never the file itself") + compare(toast.visible, false) + } + + function test_dismiss_and_timeout_both_hide_it() { + saved("/srv/mail/Note.eml", "Note.eml") + named(toast, "eml-saved-close").clicked() + compare(toast.visible, false) + saved("/srv/mail/Note.eml", "Note.eml") + compare(toast.visible, true) + tryCompare(toast, "visible", false, 2000, "it leaves on its own") + } + + function test_a_second_save_replaces_the_first() { + saved("/srv/mail/One.eml", "One.eml") + saved("/srv/other/Two.eml", "Two.eml") + compare(named(toast, "eml-saved-name").text, "Saved Two.eml") + compare(toast.folder, "/srv/other") + } + } +} diff --git a/ui/tests/qml/tst_message_menu_export.qml b/ui/tests/qml/tst_message_menu_export.qml index d58e805..46925fa 100644 --- a/ui/tests/qml/tst_message_menu_export.qml +++ b/ui/tests/qml/tst_message_menu_export.qml @@ -35,6 +35,13 @@ Item { var at = String(id).indexOf("/") return at > 0 ? String(id).substring(at + 1) : String(id) } + property bool folderable: true + property var chosen: [] + readonly property bool standalone: !folderable + function exportEmlToFolder(accountId, id) { + chosen = chosen.concat([{ account: String(accountId), id: String(id) }]) + return true + } function exportEmlFor(accountId, id) { exported = exported.concat([{ account: String(accountId), id: String(id) }]) return true @@ -59,6 +66,7 @@ Item { // exportRow sits after star, before the browser and AI rows. function exportRow() { return menu.menuRows[11] } + function exportFolderRow() { return menu.menuRows[12] } function summary() { return { id: "42:INBOX", subject: "Project update", unread: false, @@ -93,7 +101,11 @@ Item { wait(20) } - function cleanup() { menu.close() } + function cleanup() { + menu.close() + fakeService.chosen = [] + fakeService.folderable = true + } function test_the_row_appears_only_when_the_service_says_it_can() { fakeService.exportable = true @@ -115,6 +127,25 @@ Item { compare(menu.opened, false, "the menu closes on choosing") } + function test_the_folder_row_routes_the_owner_and_native_id() { + fakeService.exportable = true + showMember(fakeService.ada + "/17:INBOX") + compare(exportFolderRow().visible, true) + compare(String(exportFolderRow().text), "Save as .eml to folder...") + exportFolderRow().activated() + compare(fakeService.chosen, [{ account: fakeService.ada, id: "17:INBOX" }]) + compare(fakeService.exported, [], "the Downloads path is not taken as well") + compare(menu.opened, false) + } + + function test_the_folder_row_needs_a_chooser() { + fakeService.exportable = true + fakeService.folderable = false + show() + compare(exportRow().visible, true) + compare(exportFolderRow().visible, false) + } + // A stop on the conversation rail is one message, and its id is the // composed member id. The menu keeps the account and the member's native // id, so the export reaches that member rather than the list's root row. diff --git a/ui/tests/qml/tst_reader_mode.qml b/ui/tests/qml/tst_reader_mode.qml index bd00456..d9444ac 100644 --- a/ui/tests/qml/tst_reader_mode.qml +++ b/ui/tests/qml/tst_reader_mode.qml @@ -223,6 +223,9 @@ Item { compare(web.height, track.height, "Open Web has the same toolbar control box, not only the same centre") + verify(reader.footerHeight >= toolbar.height, + "the reader reports the whole toolbar, for cards that must sit above it") + reader.width = 300 tryCompare(toolbar, "stacked", true) verify(track.y >= 0 && web.y >= 0) diff --git a/ui/tests/qml/tst_reader_text_menu.qml b/ui/tests/qml/tst_reader_text_menu.qml index 6cfdee0..cfac93c 100644 --- a/ui/tests/qml/tst_reader_text_menu.qml +++ b/ui/tests/qml/tst_reader_text_menu.qml @@ -4,8 +4,8 @@ import qs.Commons import "../../components" as Omamail import "../../message/Html.js" as Html -// A right-click on the message body: Copy for what is selected, and Open and -// Copy URL when the click lands on a link. +// A right-click on the message body: Copy for what is selected, Open and Copy +// URL when the click lands on a link, and the message itself as a .eml file. Item { width: 900 height: 600 @@ -55,6 +55,19 @@ Item { function getMessage() {} function showRemoteImages() {} + // Save as .eml from the reader: the toolbar button and the body menu. + property bool exportable: true + property bool folderable: true + property var exported: [] + function canExportEmlFor(id) { return exportable } + readonly property bool standalone: !folderable + function exportEml(id) { exported = exported.concat([{ id: String(id), folder: false }]); return true } + function accountForMessage(id) { return "imap:reader@example.com" } + function sourceIdFor(id) { return "native-" + id } + function exportEmlToFolder(account, id) { + exported = exported.concat([{ account: String(account), id: String(id), folder: true }]) + return true + } function copyText(text) { copied = copied.concat([String(text)]); return true } function openExternal(url) { opened = opened.concat([String(url)]); return true } } @@ -130,6 +143,9 @@ Item { body().deselect() mailService.copied = [] mailService.opened = [] + mailService.exported = [] + mailService.exportable = true + mailService.folderable = true } function cleanup() { menu().close() } @@ -168,6 +184,49 @@ Item { compare(mailService.copied, ["https://example.com/board"]) } + function test_the_body_menu_saves_the_message_as_eml() { + var edit = body() + var point = pointOf(edit, "Person 1") + mouseClick(edit, point.x, point.y, Qt.RightButton) + wait(20) + compare(menu().saveEmlRow.visible, true) + compare(menu().saveEmlFolderRow.visible, true) + menu().saveEmlRow.activated() + compare(mailService.exported, [{ id: "message-9", folder: false }]) + compare(menu().opened, false) + + mouseClick(edit, point.x, point.y, Qt.RightButton) + wait(20) + menu().saveEmlFolderRow.activated() + compare(mailService.exported[1], { account: "imap:reader@example.com", + id: "native-message-9", folder: true }, "the owner and native id, captured now") + } + + function test_the_rows_follow_what_the_service_can_do() { + mailService.folderable = false + var edit = body() + var point = pointOf(edit, "Person 1") + mouseClick(edit, point.x, point.y, Qt.RightButton) + wait(20) + compare(menu().saveEmlRow.visible, true) + compare(menu().saveEmlFolderRow.visible, false, "no chooser, no folder row") + menu().close() + mailService.exportable = false + mouseClick(edit, point.x, point.y, Qt.RightButton) + wait(20) + compare(menu().saveEmlRow.visible, false) + } + + function test_the_toolbar_button_saves_to_downloads() { + var button = named(reader, "reader-save-eml-button") + verify(button, "the reader toolbar has a save button") + compare(button.visible, true) + button.clicked() + compare(mailService.exported, [{ id: "message-9", folder: false }]) + mailService.exportable = false + compare(button.visible, false, "hidden where export is not possible") + } + function test_open_link_opens_it_outside() { var edit = body() var point = pointOf(edit, "Open the board") diff --git a/ui/tests/test_eml_export.js b/ui/tests/test_eml_export.js index 5dbaa33..d971c1b 100644 --- a/ui/tests/test_eml_export.js +++ b/ui/tests/test_eml_export.js @@ -93,4 +93,50 @@ for (const code of ["mail_export_write_failed", "mail_export_message_missing", " assert.strictEqual(actions.exportErrorText({ message: "mail_tls_failed" }), "Could not save the message as .eml") assert.strictEqual(actions.exportErrorText({ message: "constructor" }), "Could not save the message as .eml") assert.strictEqual(actions.exportErrorText(null), "Could not save the message as .eml") +// A picked folder rides the same dispatch boundary, captured owner and all. +const folderCalls = [] +const desk = account("imap:desk@example.org", "imap") +desk.exportEml = (nativeId, directory) => { folderCalls.push({ id: nativeId, directory }); return true } +let chooserAnswer = null +const plugin = { current: desk, unified: false, standalone: false, selectedId: "", + backendCanExportEml: true, failures: [], fail: text => plugin.failures.push(text), + findAccount: id => id === desk.accountId ? desk : null, + hostForId: () => plugin.current, sourceIdFor: id => id, + chooseFiles: (done, folder) => { assert.strictEqual(folder, true); done(chooserAnswer); return true } } +plugin.canExportEmlFor = id => actions.canExportEmlFor(plugin, id) +chooserAnswer = { ok: true, paths: ["/home/ada/Mail archive"] } +assert.strictEqual(actions.exportEmlToFolder(plugin, desk.accountId, "9:INBOX"), true) +assert.deepStrictEqual(folderCalls.pop(), { id: "9:INBOX", directory: "/home/ada/Mail archive" }) +chooserAnswer = { ok: false, error: "cancelled" } +actions.exportEmlToFolder(plugin, desk.accountId, "9:INBOX") +assert.strictEqual(folderCalls.length, 0, "a cancelled chooser saves nothing") +assert.deepStrictEqual(plugin.failures, [], "and says nothing") +chooserAnswer = { ok: false, error: "No folder picker is available" } +actions.exportEmlToFolder(plugin, desk.accountId, "9:INBOX") +assert.deepStrictEqual(plugin.failures, ["No folder picker is available"]) +// An account switch while the chooser was open refuses, as a stale menu does. +chooserAnswer = { ok: true, paths: ["/home/ada/Mail archive"] } +plugin.chooseFiles = done => { plugin.current = bob; done(chooserAnswer); return true } +plugin.findAccount = id => [desk, bob].find(a => a.accountId === id) || null +actions.exportEmlToFolder(plugin, desk.accountId, "9:INBOX") +assert.strictEqual(folderCalls.length, 0, "the folder answer cannot reach another mailbox") +plugin.current = desk +plugin.standalone = true +assert.strictEqual(actions.exportEmlToFolder(plugin, desk.accountId, "9:INBOX"), false, + "the standalone app has no out-of-process chooser") +plugin.standalone = false +plugin.backendCanExportEml = false +assert.strictEqual(actions.exportEmlToFolder(plugin, desk.accountId, "9:INBOX"), false, + "no chooser opens for a backend that cannot export") + +assert.deepStrictEqual(JSON.parse(JSON.stringify(actions.exportSavedFile({ filename: "Project update (2).eml", + path: "/home/ada/Downloads/Project update (2).eml" }, "/home/ada"))), + { name: "Project update (2).eml", folder: "/home/ada/Downloads", shownFolder: "~/Downloads" }) +assert.strictEqual(actions.exportSavedFile({ path: "/home/adam/x.eml" }, "/home/ada").shownFolder, + "/home/adam", "a sibling of home is not shortened") +assert.strictEqual(actions.exportSavedFile({ path: "/srv/mail/x.eml" }, "").shownFolder, "/srv/mail") +assert.strictEqual(actions.exportErrorText({ message: "mail_export_write_failed" }, "/home/ada/Mail"), + "Could not write the .eml file to /home/ada/Mail", "a picked folder is named in the failure") +assert.strictEqual(actions.exportErrorText({ message: "mail_export_write_failed" }), + "Could not write the .eml file to Downloads") console.log("eml export API gate and account routing tests passed") diff --git a/ui/tests/test_icons.js b/ui/tests/test_icons.js index 00503a3..afd0e91 100644 --- a/ui/tests/test_icons.js +++ b/ui/tests/test_icons.js @@ -26,6 +26,10 @@ assert.strictEqual(icons.glyph("unread", true), icons.glyph("unread", false), "n assert.notStrictEqual(icons.glyph("forward"), icons.glyph("chevronRight")) assert.strictEqual(icons.glyph("sent").codePointAt(0), 0xF10DD, "the Sent mailbox uses the balanced email-send-outline glyph") +// Save buttons draw download-outline. 0xF0DA9, used before, is +// badge-account-alert-outline: a badge with an exclamation mark. +assert.strictEqual(icons.glyph("download").codePointAt(0), 0xF0B8F, + "download is the download-outline glyph") // Every icon a view asks for by name is defined. Scans the QML for the // literal names handed to ActionIcon, IconButton, IconTextButton and From bc4b97db9805aae4c7f34f8962c12883dad45a00 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 9 Oct 2026 21:32:14 +0800 Subject: [PATCH 02/88] chore: Wait for a quiet second in the settings watch settle test (#322) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `SettingsTest::watchSettlesAfterDirectoryMutation` sampled the change count 500 ms after the first event and required no further change in the next second. FSEvents may split the test's own three mutations (a sibling write, a directory mode change and a file mode change) into batches spaced further apart on a busy runner, so the [v0.10.8 Release run](https://github.com/huacnlee/omamail/actions/runs/36874896519) failed on macOS with 5 changes against a sampled 2; rerunning the job passed. Five is exactly two directory events over two watched paths plus one file event, which means late delivery of expected events rather than a watch loop. The test now waits until a full second passes without a change, bounded at 10 seconds, then keeps the same assertions. A watch that never goes quiet still fails. ## Verification - Built `tst_settings` on Linux and ran it three times through ctest; it passed every time. - Temporarily made `FileStore`'s directory handler create a new file in the watched directory, an endless loop. The test failed after the 10 second bound (1,383,671 changes against 1,243,847). With the change reverted, the full suite passed again (12 passed, 1 skipped). - macOS behavior is exercised by this PR's CI. **Security: PASS** — only a native test's waiting changes; no runtime code or security boundary is touched. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 --- app/tests/native/tst_settings.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/tests/native/tst_settings.cpp b/app/tests/native/tst_settings.cpp index 350e097..11850d5 100644 --- a/app/tests/native/tst_settings.cpp +++ b/app/tests/native/tst_settings.cpp @@ -2,6 +2,7 @@ #include "file_store.h" #include +#include #include #include #include @@ -260,9 +261,17 @@ void SettingsTest::watchSettlesAfterDirectoryMutation() QVERIFY(QFile::setPermissions(present, QFileDevice::ReadOwner | QFileDevice::WriteOwner)); #endif QTRY_VERIFY_WITH_TIMEOUT(!changed.isEmpty(), 2000); - QTest::qWait(500); - const int settled = changed.count(); - QTest::qWait(1000); + // FSEvents delivers these three mutations in as many batches as it + // likes, and a busy runner can space them well apart. Wait for a quiet + // second rather than assuming when the last one lands; the loop never + // went quiet. + QElapsedTimer settling; + settling.start(); + int settled = -1; + while (settled != changed.count() && settling.elapsed() < 10000) { + settled = changed.count(); + QTest::qWait(1000); + } QCOMPARE(changed.count(), settled); // A handful for two coalesced events over two watched paths; the loop // produced hundreds and was still going. From a4d0b78d49ebffbf570f54557325b625911977e1 Mon Sep 17 00:00:00 2001 From: Arthur Augusto <59454155+KitsuneSemCalda@users.noreply.github.com> Date: Fri, 9 Oct 2026 10:57:21 -0300 Subject: [PATCH 03/88] chore: List Omamail in OmaStore with an omastore.toml (#328) This adds an `omastore.toml` at the root so Omamail's standalone desktop app shows up in [OmaStore](https://github.com/KitsuneForgering/OmaStore), an app store for Omarchy. OmaStore only lists repositories that have this file, so the file is your opt-in. It doesn't change the plugin, the build or the release. Most of the file is there because the release carries two kinds of Linux tarball. Without a manifest, the store's heuristic would see `omamail-linux-x86_64.tar.gz` (the plugin backend) and `omamail-app-linux-x86_64.tar.gz` side by side and could pick the backend, then launch `bin/omamail` as if it were the app. So the file names the standalone tarball and its Qt host, `omamail.app/bin/omamail-app`, the same executable `install.sh` links. The aarch64 entry names `omamail-app-linux-aarch64.tar.gz`, which no release has yet. That's on purpose. Until a release has it, OmaStore treats the app as not installable on ARM instead of installing `omamail-linux-aarch64.tar.gz` (the plugin backend) in its place. If an ARM build of the standalone app is ever published under that name, it starts working with no further change. (Refusing a declared asset that is missing is new in OmaStore; this release prompted it, and the release that has it is still on its way.) The rest is display data: the icon the standalone package installs (`app/resources/icons/omamail.svg`), four screenshots from `docs/images/` and the categories from the bundled `omamail.desktop` (`Network`, `Email`, `Office`). OmaStore checks the file against `SHA256SUMS`, extracts it into the user's `$HOME` without running anything from it and writes its own menu entry. Every field is optional and documented [here](https://github.com/KitsuneForgering/OmaStore/blob/master/docs/authors.md). Change or drop any of them as you like. ## Verification `make validate` green on this commit: Rust tests, node tests, `tests/test_source.sh`, the QML suites (0 failures) and qmllint, then `omarchy plugin validate .` and `git diff --check`. The new file is not code, so no test was added. There is no behavior in this repository for one to fail against. The OmaStore side was checked with its own tools in a throwaway `$HOME`. `omastore lint-manifest .` is clean. `omastore check --manifest` reports the icon, the 4 screenshots and the `SHA256SUMS` checksum, and v0.10.8 as installable on x86_64. A real `omastore install` of v0.10.8 picked `omamail-app-linux-x86_64.tar.gz`, verified it, linked `omamail-app` and wrote a menu entry, and `omastore uninstall` then removed everything it had created. A fresh agent with no context from writing the change reviewed the diff. It checked that every path exists, that the x86_64 asset is the standalone bundle and not the plugin backend, that `exec` matches what `install.sh` links, and that the categories match the bundled `.desktop`. It also checked that the new root file trips nothing in `tests/test_source.sh` or `omarchy plugin validate`. It found nothing wrong. --- omastore.toml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 omastore.toml diff --git a/omastore.toml b/omastore.toml new file mode 100644 index 0000000..a8f6a53 --- /dev/null +++ b/omastore.toml @@ -0,0 +1,23 @@ +kind = "app" +name = "Omamail" +summary = "Native mail and calendar app for Gmail, Outlook, HEY, JMAP and IMAP, with multiple accounts and keyboard navigation." +categories = ["Network", "Email", "Office"] +icon = "app/resources/icons/omamail.svg" +screenshots = [ + "docs/images/full-mail.webp", + "docs/images/full-calendar.webp", + "docs/images/full-compose.webp", + "docs/images/mini-list.webp", +] + +# The release also ships the Omarchy plugin backend (omamail-linux-*.tar.gz); +# OmaStore installs the standalone desktop app and launches its Qt host. +[linux.x86_64] +asset = "omamail-app-linux-x86_64.tar.gz" +exec = "omamail.app/bin/omamail-app" + +# Not published yet: until it is, the app is not installable on ARM rather than +# getting the plugin backend in its place. +[linux.aarch64] +asset = "omamail-app-linux-aarch64.tar.gz" +exec = "omamail.app/bin/omamail-app" From 19148cd711f5ca53b0fac6628acec7d00a43a9bc Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 11 Oct 2026 12:28:54 +0800 Subject: [PATCH 04/88] docs: Prioritize product necessity and design in PR reviews --- .agents/skills/review-prs/SKILL.md | 14 +++++++++++++- AGENTS.md | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.agents/skills/review-prs/SKILL.md b/.agents/skills/review-prs/SKILL.md index 0dcaa88..74c6e31 100644 --- a/.agents/skills/review-prs/SKILL.md +++ b/.agents/skills/review-prs/SKILL.md @@ -1,12 +1,22 @@ --- name: review-prs -description: Review GitHub PRs sequentially in dedicated worktrees, assess completeness, tests, performance and security, fix small issues, and prepare or publish maintainer reviews and release-note descriptions when authorized. +description: Review GitHub PRs to judge whether their new or adjusted capabilities belong in the product and assess their design, with technical and security validation, dedicated worktrees, and authorized maintainer reviews. --- # Review PRs Accept optional PR numbers, URLs, or a repository/queue filter. Examples: `$review-prs 42 57`, `$review-prs https://github.com/owner/repo/pull/42`, or `$review-prs review all open non-draft PRs in this repository`. +## Primary purpose: product necessity and design + +The maintainer primarily reviews PRs to decide whether their new or adjusted capabilities are necessary to introduce and whether the design is appropriate. Lead every PR report with what it changes and the problem it aims to solve, including passing PRs and PRs skipped as already reviewed, superseded or closed. Do not reduce the report to a technical verdict. + +For each change, assess fit with established project goals, who benefits, overlap with existing behavior, simpler alternatives, and interface and maintenance cost. Recommend introducing it, deferring it, or leaving it out, with a concrete reason. Evaluate adjusted workflows as well as entirely new features; restoring an established behavior may be a necessary fix, but its design still needs review. The maintainer owns the product decision. + +Assess product and interaction design separately from implementation quality: workflow and scope, discoverability, defaults, information hierarchy, consistency, keyboard and focus behavior, and loading, empty, error and cancellation states. For non-UI changes, assess the affected command, configuration or integration workflow. Explain material tradeoffs and distinguish observed design evidence from inference; missing screenshots or hands-on evidence limit the design verdict. + +Technical completeness, tests, performance and security support the product and design decision and remain required approval gates. Passing checks does not by itself justify introducing a capability. Keep product recommendations, design findings and technical blockers distinct. + ## Scope and authority - Resolve the repository from explicit arguments or the current checkout. If that is ambiguous, ask only for the repository. With explicit PR numbers or URLs, review only those PRs, deduplicated and in supplied order; never add other candidates. Without PR targets, inspect open PRs and their review/discussion history, order by creation time oldest to newest, and select at most the oldest three open non-draft PRs needing review or re-review. A PR needing re-review has a new head or an explicit re-review request since the prior completed review; skip unchanged PRs already reviewed by this reviewer and explain the skip. Paginate listings/history before selection so an API page does not silently omit older PRs. @@ -36,6 +46,7 @@ Continue useful technical investigation while the product decision is pending, b | Area | Evidence to seek | | --- | --- | +| Product necessity and design | Explain the change and purpose, assess fit and alternatives, recommend introduce/defer/leave out, and evaluate the affected workflow and interaction states with actual evidence. | | Functional completeness | Map promised behavior and acceptance criteria to implementation. Check integration, error/empty/loading states, compatibility and affected providers/platforms. Flag missing pieces and regressions. | | Tests | Verify meaningful assertions for new behavior, failure paths and boundaries. Run repository-required checks and proportionate targeted regressions. Distinguish tests run locally, CI-only evidence and untested environments. Passing tests do not prove completeness. | | Performance | Check algorithmic cost, realistic input size, UI blocking, request amplification, pagination, cache behavior, resource growth, cancellation and timeouts. Measure plausible hot-path regressions; distinguish measured results from inference. | @@ -104,6 +115,7 @@ Make every progress update and final review easy to scan. Do not mix product dec - Put key names, action ids, commands, SHAs and short identifiers in inline code. - Use fenced code blocks only for multiline commands, logs, patches or exact text the user may copy; never use a code block merely as decoration. - Lead with the outcome. State blockers and required user choices separately at the end. +- For every PR, explain what changes and why before the verdict; include the product recommendation and design assessment, even when technical checks pass. Keep skipped PR explanations proportionate. - Preserve the brief one-sentence style for a published passing review, but use the structured format in the user-facing batch report. Passing review or publishing APPROVE is not merge authorization. Wait for explicit user confirmation of the selected PRs; silence is not approval. If the user already explicitly authorized merging these specific PRs in this session, honor that authorization without asking again. Resolve the merge method from the user's instructions or repository conventions/allowed methods, and include it in the concrete proposal when confirmation is needed. diff --git a/AGENTS.md b/AGENTS.md index bc89be8..f553b61 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,6 +44,13 @@ Read the relevant document before changing its area; these contain constraints t - Preserve account/draft ownership, human approval of AI proposals, and normal outbox dispatch. Model text must not supply routing or permission to send mail. - Every review states **Security: PASS / BLOCK / NOT VERIFIED** for the affected boundaries. Missing evidence or a security failure blocks approval/release. Security regressions must verify the forbidden effect did not happen, not merely that an error was returned; follow `docs/SECURITY-BOUNDARIES.md`. +## Review priorities + +- PR review primarily decides whether a new or adjusted capability belongs in Omamail and whether its product and interaction design is appropriate. Begin each PR report by explaining what changes, the problem it aims to solve, and who benefits. +- Assess necessity, fit with project goals, overlap with existing workflows, simpler alternatives, and interface and maintenance costs. Give a clear recommendation to introduce, defer, or leave out the change, with reasons; the maintainer owns the product decision. +- Evaluate design independently of implementation quality: workflow, discoverability, defaults, information hierarchy, keyboard and focus behavior, consistency, and loading, empty, error and cancellation states. State when screenshots or hands-on evidence are missing rather than treating tests as design evidence. +- Functional correctness, tests, performance and security support this product and design assessment and remain approval gates. Passing technical checks alone does not justify adding a capability. Explain the change and purpose for every PR, including passing and skipped PRs. + ## Repository and pull requests - Keep runtime assets, source, tests and maintained documentation in the tree. Keep planning notes and screenshots outside it. Upload PR screenshots to GitHub attachments; tracked files must stay under 128 KiB. From a52850714fe932e5e99157b89d14198392240b64 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 11 Oct 2026 07:09:56 +0200 Subject: [PATCH 05/88] Unlock a locked Secret Service collection before creating an item (#302) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What broke On a machine that logs in without a typed password (SDDM autologin, so PAM never hands gnome-keyring the login password), Gmail sign-in finishes Google's side and then fails with *"Could not save the Google session to the keyring. Please try again"*. `~/.local/state/omamail/diagnostics/errors.json` records `auth.poll` → `auth_keyring_failed`. ### Reproduction 1. Log in through SDDM autologin so the default Secret Service collection starts locked each session. It may have a blank password, in which case unlocking needs no prompt. 2. Add a Gmail mailbox and complete the Google consent screen. 3. The browser page says "Sign-in received", and the token exchange plus `users/me/profile` succeed, but the app then reports the keyring error. Root cause: `Operation::Put` checked `collection.is_locked()` and returned `Error::Unavailable` instead of unlocking the collection. The refresh token is stored during sign-in, before the UI starts any mailbox request, so the whole sign-in reports failure after Google has already returned a token — with no way for the user to act on it. ## Fix Unlock the default collection when it is locked, then continue to `create_item`: ```rust if collection.is_locked().await.map_err(|_| Fault::Transport)? { collection.unlock().await.map_err(|_| Fault::Transport)?; } ``` A blank-password collection unlocks immediately with no prompt; a password-protected one prompts inside the operation's single deadline (the same way `find` already unlocks a locked item). Refusing was the only path that could not succeed. ## Test Adds a `with_locked_collection()` native D-Bus fixture (item missing, default collection locked) and `credentials_native_linux_a_locked_collection_is_unlocked_before_a_write`, asserting the write reaches `Unlock` and then `CreateItem`. Before the fix the collection was refused, so neither was reached. All native credential tests pass: ``` cargo test --locked --lib credentials_native_linux -- --ignored test result: ok. 10 passed; 0 failed ``` --- src/credentials/secret_service.rs | 10 ++++- src/credentials/secret_service_tests.rs | 53 ++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/credentials/secret_service.rs b/src/credentials/secret_service.rs index 054748e..8fa991f 100644 --- a/src/credentials/secret_service.rs +++ b/src/credentials/secret_service.rs @@ -311,8 +311,16 @@ async fn execute( .get_default_collection() .await .map_err(|_| Fault::Transport)?; + // A blank-password collection is locked again by every fresh + // login, and unlocking it is an answer the daemon gives over a + // connection that is still good; a password-protected one + // prompts inside this operation's single deadline. Refusing + // instead made Google sign-in fail with a generic keyring + // error after Google had already handed back a token, because + // the refresh token is stored before the UI starts any + // mailbox request. if collection.is_locked().await.map_err(|_| Fault::Transport)? { - return Err(Fault::Answer(Error::Unavailable)); + collection.unlock().await.map_err(|_| Fault::Transport)?; } // Keep older grants intact until the current-scope item exists. collection diff --git a/src/credentials/secret_service_tests.rs b/src/credentials/secret_service_tests.rs index 22769ff..3642cc4 100644 --- a/src/credentials/secret_service_tests.rs +++ b/src/credentials/secret_service_tests.rs @@ -28,6 +28,9 @@ struct Service { /// Reports the item as present but locked, which the daemon answers over a /// connection that is still good. locked: Arc, + /// Reports the default collection as locked, the state a fresh login + /// leaves a blank-password keyring in. + collection_locked: Arc, unlock_denial: bool, unlocks: Arc, /// Counts negotiated sessions, which is what tells a reused connection from @@ -93,6 +96,7 @@ impl Service { )); } self.locked.store(false, Ordering::SeqCst); + self.collection_locked.store(false, Ordering::SeqCst); Ok((objects, path("/"))) } } @@ -101,12 +105,13 @@ impl Service { /// write the daemon commits and the client never hears about. struct Collection { creates: Arc, + locked: Arc, } #[zbus::interface(name = "org.freedesktop.Secret.Collection")] impl Collection { #[zbus(property)] fn locked(&self) -> bool { - false + self.locked.load(Ordering::SeqCst) } fn create_item( &self, @@ -148,6 +153,7 @@ struct Fixture { searches: Arc, unlocks: Arc, creates: Arc, + collection_locked: Arc, _daemon: Daemon, } impl Fixture { @@ -163,6 +169,13 @@ impl Fixture { fn with_refused_unlock() -> Self { Self::build(false, false, true, true, 0) } + /// A missing item in a locked default collection: the state a fresh login + /// leaves behind, where a write has nothing to set and must create. + fn with_locked_collection() -> Self { + let fixture = Self::build(false, true, false, false, 0); + fixture.collection_locked.store(true, Ordering::SeqCst); + fixture + } fn build( denial: bool, missing: bool, @@ -193,6 +206,7 @@ impl Fixture { let searches = Arc::new(AtomicUsize::new(0)); let unlocks = Arc::new(AtomicUsize::new(0)); let locked = Arc::new(AtomicBool::new(locked)); + let collection_locked = Arc::new(AtomicBool::new(false)); let failures = Arc::new(AtomicUsize::new(failures)); let creates = Arc::new(AtomicUsize::new(0)); let runtime = tokio::runtime::Builder::new_multi_thread() @@ -211,6 +225,7 @@ impl Fixture { denial, missing, locked, + collection_locked: collection_locked.clone(), unlock_denial, unlocks: unlocks.clone(), sessions: sessions.clone(), @@ -225,6 +240,7 @@ impl Fixture { "/org/freedesktop/secrets/collection/default", Collection { creates: creates.clone(), + locked: collection_locked.clone(), }, ) .unwrap() @@ -246,6 +262,7 @@ impl Fixture { searches, unlocks, creates, + collection_locked, _daemon: daemon, } } @@ -517,6 +534,40 @@ fn credentials_native_linux_silently_unlocks_a_locked_item() { assert_eq!(fixture.searches.load(Ordering::SeqCst), 1); } +/// A fresh login leaves a blank-password default collection locked. A write +/// must unlock it and carry on rather than refuse: the refresh token is stored +/// during sign-in, before any mailbox request, so refusing here is what made +/// Gmail sign-in fail with a generic keyring error after Google had already +/// handed back a token. The daemon counts the calls, so this asserts the +/// unlock reached it and the write then reached CreateItem. +#[test] +#[ignore = "requires native dbus-daemon; mandatory in the Linux credential gate"] +fn credentials_native_linux_a_locked_collection_is_unlocked_before_a_write() { + let fixture = Fixture::with_locked_collection(); + let shared = Shared::new().unwrap(); + + assert!(matches!( + run_on( + &shared, + &key(), + Operation::Put(b"synthetic"), + || fixture.client(), + DEADLINE + ), + Err(Error::Unavailable) + )); + assert_eq!( + fixture.unlocks.load(Ordering::SeqCst), + 1, + "the locked collection was refused instead of unlocked" + ); + assert_eq!( + fixture.creates.load(Ordering::SeqCst), + 1, + "the write never reached CreateItem" + ); +} + /// One operation costs the caller one deadline. Opening the session and running /// the operation share a single budget, so a connect that never answers cannot /// be waited for once and then waited for again. From bc918cd93e021281badbbf604e5c1c5b88dfc587 Mon Sep 17 00:00:00 2001 From: art-request Date: Sun, 11 Oct 2026 01:36:04 -0400 Subject: [PATCH 06/88] Fix moving IMAP Junk messages to Inbox in single and unified views (#303) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IMAP Junk messages now offer **Move to Inbox**, so mistakenly classified mail can return to the Inbox even when the provider has no report-spam action or its folder picker excludes system folders. This restores the existing `unarchive` operation; no backend method or new user setting is added. The menu checks the selected message’s owning account through `hostForId`, including qualified IDs in the unified view. The unified service’s aggregate capability cannot decide a single message’s spam semantics: Gmail Spam keeps its existing actions when another mailbox lacks spam reporting. Missing owners do not enable the recovery action. The component is shared by the plugin and standalone hosts. ## Verification On `144ccfa`, Linux/Qt 6.11.2: `make validate` passed, including Rust, JavaScript, shell, QML, standalone composition, lint, plugin validation and whitespace. The menu suite passed 20 tests; an independent mixed-account fixture passed 18. The mixed Gmail regression failed on the earlier PR and passes with the owner-capability correction. Synthetic inputs only; no live mailbox or keyring was used. Security: PASS for the affected menu and existing account-routing scope. ## Release Notes - Restore Move to Inbox for IMAP Junk messages, including the unified mailbox view, while preserving Gmail Spam behavior. ## Maintainer UI evidence Production MessageMenu component captures with synthetic mail, Qt 6.11.2, 640×480, scale 1, fixed reproducible light/dark semantic palettes. These are focused component renders using production standalone UI compatibility imports, not the installed desktop or live accounts. Each BEFORE/AFTER pair uses the same dimensions, palette, popup position and selected row. IMAP Junk now offers Move to Inbox instead of Archive; Gmail Spam retains its existing actions. Unified-mailbox captures resolve the selected message owner: IMAP gets Move to Inbox, while Gmail retains Archive. Current reviewed component at `144ccfa` has the same SHA256 as the captured source. **AFTER DARK GMAIL** ![after-dark-gmail](https://github.com/user-attachments/assets/e973c88c-a54a-4e72-aedc-ecfffc62ee9d) **AFTER DARK IMAP** ![after-dark-imap](https://github.com/user-attachments/assets/82687088-8d10-4076-bb8d-c48cbd05f000) **AFTER DARK UNIFIED GMAIL** ![after-dark-unified-gmail](https://github.com/user-attachments/assets/7c9adfee-77a3-447a-a7ca-36d41cd23301) **AFTER DARK UNIFIED IMAP** ![after-dark-unified-imap](https://github.com/user-attachments/assets/f5b65b2b-983c-4cac-81b8-df938ac658bc) **AFTER LIGHT GMAIL** ![after-light-gmail](https://github.com/user-attachments/assets/9daab0cc-561f-439f-ab94-a1ee08001fa5) **AFTER LIGHT IMAP** ![after-light-imap](https://github.com/user-attachments/assets/07c2fd6f-b8d4-4082-8a7c-f0da8fe7394e) **AFTER LIGHT UNIFIED GMAIL** ![after-light-unified-gmail](https://github.com/user-attachments/assets/e61151ef-3347-4598-9666-504eb9864242) **AFTER LIGHT UNIFIED IMAP** ![after-light-unified-imap](https://github.com/user-attachments/assets/88a58396-1720-40ed-b27b-c606be4f9ea7) **BEFORE DARK GMAIL** ![before-dark-gmail](https://github.com/user-attachments/assets/93adce08-8ee4-4a41-b4b1-78cfc7352796) **BEFORE DARK IMAP** ![before-dark-imap](https://github.com/user-attachments/assets/5d562841-d9e8-4059-8ac9-da50d417d7c3) **BEFORE DARK UNIFIED GMAIL** ![before-dark-unified-gmail](https://github.com/user-attachments/assets/d96d71c3-c525-4559-a850-02b2265da1e5) **BEFORE DARK UNIFIED IMAP** ![before-dark-unified-imap](https://github.com/user-attachments/assets/bc380543-8b3e-4c87-ae8b-a9d225c9f49f) **BEFORE LIGHT GMAIL** ![before-light-gmail](https://github.com/user-attachments/assets/006bd49b-1baa-4ba0-9239-11f54962b33d) **BEFORE LIGHT IMAP** ![before-light-imap](https://github.com/user-attachments/assets/4b4d46f0-4e64-408e-9bb2-328aadc8c0de) **BEFORE LIGHT UNIFIED GMAIL** ![before-light-unified-gmail](https://github.com/user-attachments/assets/d225dec3-dd7f-4563-9b04-9be48254b669) **BEFORE LIGHT UNIFIED IMAP** ![before-light-unified-imap](https://github.com/user-attachments/assets/8701b163-840f-4f8a-a08b-9f15e389e203) --------- Co-authored-by: art-request Co-authored-by: Jason Lee --- ui/components/MessageMenu.qml | 9 ++- ui/tests/qml/tst_message_menu_inbox.qml | 73 ++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/ui/components/MessageMenu.qml b/ui/components/MessageMenu.qml index fc4ec45..7fdadaa 100644 --- a/ui/components/MessageMenu.qml +++ b/ui/components/MessageMenu.qml @@ -35,11 +35,16 @@ Item { // that has its own verb. Read off the summary the menu was opened on rather // than asked of the service, because the menu is about one message. IMAP // synthesises all four of these from the folder, so one rule serves both - // kinds of provider. + // kinds of provider. Spam counts as "its own verb" only where a spam verb + // exists: an IMAP Junk folder has no report-spam inverse to offer instead, + // so a Junk message there is the one spam case "Move to Inbox" still means. + readonly property var messageHost: !root.service ? null + : typeof root.service.hostForId === "function" + ? root.service.hostForId(root.messageId) : root.service readonly property bool archived: !!root.summary && root.summary.inInbox === false && root.summary.inTrash !== true - && root.summary.inSpam !== true + && (root.summary.inSpam !== true || (root.messageHost && root.messageHost.canReportSpam === false)) && root.summary.isSent !== true && root.summary.isDraft !== true diff --git a/ui/tests/qml/tst_message_menu_inbox.qml b/ui/tests/qml/tst_message_menu_inbox.qml index ba894ea..e3f9a38 100644 --- a/ui/tests/qml/tst_message_menu_inbox.qml +++ b/ui/tests/qml/tst_message_menu_inbox.qml @@ -24,6 +24,8 @@ Item { property bool hasLabels: true property string rawLabelId: "" property var messages: [] + property var hosts: null + function hostForId(id) { return hosts ? hosts[id] || null : fakeService } } Omamail.MessageMenu { @@ -68,7 +70,11 @@ Item { wait(20) } - function cleanup() { menu.close() } + function cleanup() { + menu.close() + fakeService.hosts = null + fakeService.canReportSpam = true + } function archivedMessage() { return { id: "m1", subject: "filed", unread: false, starred: false, @@ -108,7 +114,10 @@ Item { } // The four that have their own verb or their own place. Adding INBOX to any - // of them is not what "move to the inbox" means. + // of them is not what "move to the inbox" means — except Junk without a + // spam verb: an IMAP account has no report-spam to offer instead, so the + // row is the only way out, and `actionPlan("unarchive")` already moves to + // INBOX from anywhere. function test_it_is_not_offered_where_it_would_lie_data() { return [ { tag: "spam", field: "inSpam" }, @@ -126,6 +135,66 @@ Item { compare(unarchiveRow().visible, false) } + // Junk without a spam verb is the exception: IMAP has no report-spam to + // offer instead, and `unarchive` moves to INBOX from anywhere. + function test_junk_without_a_spam_verb_offers_it() { + fakeService.canReportSpam = false + var summary = archivedMessage() + summary.inSpam = true + show(summary) + compare(menu.archived, true, "Junk with no spam verb is where Move to Inbox means") + compare(unarchiveRow().visible, true) + compare(unarchiveRow().text, "Move to Inbox") + unarchiveRow().activated() + compare(actionSpy.signalArguments[0][0], "unarchive") + fakeService.canReportSpam = true + } + + // Gmail keeps its spam verb, so there the original rule stands. + function test_junk_with_a_spam_verb_still_hides_it() { + fakeService.canReportSpam = true + var summary = archivedMessage() + summary.inSpam = true + show(summary) + compare(menu.archived, false) + compare(unarchiveRow().visible, false) + } + + function test_mixed_mailboxes_use_the_messages_own_spam_capability_data() { + return [ + { tag: "gmail", id: "gmail:owner:spam", capability: true, offered: false }, + { tag: "imap", id: "imap:owner:Junk:42", capability: false, offered: true } + ] + } + + function test_mixed_mailboxes_use_the_messages_own_spam_capability(row) { + fakeService.canReportSpam = false + var hosts = ({}) + hosts[row.id] = { canReportSpam: row.capability } + fakeService.hosts = hosts + var summary = archivedMessage() + summary.id = row.id + summary.inSpam = true + show(summary) + compare(menu.archived, row.offered) + compare(unarchiveRow().visible, row.offered) + if (row.offered) { + unarchiveRow().activated() + compare(actionSpy.signalArguments[0][0], "unarchive") + compare(actionSpy.signalArguments[0][1], row.id) + } + } + + function test_missing_owner_does_not_offer_a_spam_recovery_action() { + fakeService.canReportSpam = false + fakeService.hosts = ({}) + var summary = archivedMessage() + summary.inSpam = true + show(summary) + compare(menu.archived, false) + compare(unarchiveRow().visible, false) + } + // ------------------------------------------------------- what it says function test_from_a_label_it_says_the_label_goes_too() { From b0504b97fb610204b64d2fe13c46926660c24321 Mon Sep 17 00:00:00 2001 From: eliasfaltin <36010212+eliasfaltin@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:03:40 +0800 Subject: [PATCH 07/88] Fix theme colors on Qt 6.12 (#346) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem Qt 6.12 adds a `Color` type to the `QtQuick` module. It shadows the `Color` palette singleton from `qs.Commons`. Every unqualified `Color.*` read now returns `undefined`, so the plugin and the standalone app render with no theme colours. The shell log shows the fault on Qt 6.12.0: ``` omamail/ui/BarWidget.qml[116:11]: Unable to assign [undefined] to QColor omamail/ui/BarWidget.qml[179:-1]: TypeError: Cannot read property 'text' of undefined omamail/ui/components/GmailIcon.qml[105:-1]: TypeError: Cannot read property 'background' of undefined ``` ## Fix Each file that reads the palette now imports `qs.Commons as Commons` and uses `Commons.Color`, including `target: Commons.Color` in `app/qml/Main.qml`. This is the rule Omarchy applies to its own shell in basecamp/omarchy#14553. The unqualified `qs.Commons` import stays for `Style`, `Border`, and `Util`. The change covers `ui/`, the standalone `app/qml/` host and its `qs.Commons`/`qs.Ui` modules, and the QML tests. `tests/test_source.sh` now looks for `markColor: Commons.Color.accent`. The alias import works on every Qt 6 version, so Qt 6.11 keeps working. The palette fix exposed one older bug, fixed in `3baf5f7`. In `app/qml/styles/Omamail/ComboBox.qml`, `parent` in the chevron's `Connections` handler resolves to the ComboBox, not the `Canvas`. Each palette change raised `requestPaint is not a function` and left the chevron in its old colour. The handler now calls the Canvas by id. ## Qt 6.12 theme evidence Standalone app (`make app-build`) on Arch, Qt 6.12.0, Omarchy theme, empty profile (setup page). Before: `main` at `a528507`; the app log has 1155 `undefined`/`TypeError` lines. After: PR head `3baf5f7`; the log has none. | Before (Standalone app, setup page) | After (Standalone app, setup page) | |:---:|:---:| | ![Before](https://raw.githubusercontent.com/eliasfaltin/omamail-upstream/0e7595e99738936831c0372680f6766bd34236a4/qt612-before.png) | ![After](https://raw.githubusercontent.com/eliasfaltin/omamail-upstream/0e7595e99738936831c0372680f6766bd34236a4/qt612-after.png) | ## Verification On Arch with Qt 6.12.0: - Synced with `main` at `a528507` (merge commit `e5c17bd`); head is `3baf5f7`. On that head: `make test-qml` 1155 passed, 3 failed (the same `LabelOwnership` failures as `main`); `make test-app-qml` passes; `bash tests/test_source.sh` passes. - `grep` finds no unqualified `Color` reference left in `ui/` or `app/` QML. The remaining matches are a string literal and a comment. - `make test-qml`: 1154 passed, 4 failed before; 1155 passed, 3 failed after. `NotificationOpen::test_new_notifications_follow_theme_without_bar_foreground` fails before and passes after. The 3 `LabelOwnership` failures (`reloadLabels is not a function`) fail the same way on `main` and are unrelated. - `make test-app-qml`: `tst_standalone_composition` failed 2 of 30 before (palette compares returned `undefined`) and passes after. - `bash tests/test_source.sh` passes. ## Release Notes - Restore theme colors in the plugin and standalone app on Qt 6.12. ## Maintainer supplemental screenshot evidence Matching before/after captures cover shared plugin components (bar icon, unread badge, action icons, default and selected buttons; text, number and toggle controls; dropdown closed and open) and the standalone provider picker, using the active Omarchy palette at scale 1 on Qt 6.11.2. Appearance is intentionally identical on this runtime: these captures demonstrate preservation of Qt 6.11 appearance, not the Qt 6.12 broken/restored state. Independent Qt 6.12 runtime screenshots remain unavailable; the author reports that regression separately. Synthetic data and production component source are used. After captures include the author’s `3baf5f7` Canvas repaint fix and match the affected widget and theme code in final head `dacd864`; the final integration also includes unrelated MessageMenu updates, so equivalence of the whole UI tree is not claimed. The open-dropdown screenshots are matching 313×56 crops of the actual expanded popup content; the plugin and standalone screenshots show matching full-window views. A separate native SignalSpy regression covers chevron repaint when the theme changes; these screenshots do not substitute for that assertion. **AFTER CONTROLS POPUP** ![after-controls-popup](https://github.com/user-attachments/assets/cf4ae09e-d4df-4540-b33d-ecf5eaf8baa9) **AFTER CONTROLS** ![after-controls](https://github.com/user-attachments/assets/21b83dee-0b08-4f62-9642-759c1da2cfba) **AFTER PLUGIN** ![after-plugin](https://github.com/user-attachments/assets/2fd0f190-d727-4b42-9a80-1bb9ecb025c3) **AFTER STANDALONE** ![after-standalone](https://github.com/user-attachments/assets/6949823f-4733-441a-9b90-d5259d5fef4d) **BEFORE CONTROLS POPUP** ![before-controls-popup](https://github.com/user-attachments/assets/9ec4cc2d-9b80-41d0-ac8e-99f735450415) **BEFORE CONTROLS** ![before-controls](https://github.com/user-attachments/assets/490e2975-6825-4ef8-9d1e-2c371a7ddbc2) **BEFORE PLUGIN** ![before-plugin](https://github.com/user-attachments/assets/841782bc-3f01-4c65-b570-2065db621b44) **BEFORE STANDALONE** ![before-standalone](https://github.com/user-attachments/assets/c479ebf0-0ec2-4f84-8183-87b286977278) --------- Co-authored-by: Jason Lee --- app/qml/Main.qml | 5 +- app/qml/StandaloneShell.qml | 3 +- app/qml/imports/Quickshell/FloatingWindow.qml | 3 +- app/qml/imports/qs/Commons/Style.qml | 9 +- app/qml/imports/qs/Ui/BarIconButton.qml | 3 +- app/qml/imports/qs/Ui/BorderSurface.qml | 3 +- app/qml/imports/qs/Ui/Button.qml | 5 +- app/qml/imports/qs/Ui/Dropdown.qml | 9 +- app/qml/imports/qs/Ui/NumberField.qml | 7 +- app/qml/imports/qs/Ui/PanelActionButton.qml | 3 +- app/qml/imports/qs/Ui/PanelSectionHeader.qml | 3 +- app/qml/imports/qs/Ui/PanelSeparator.qml | 5 +- app/qml/imports/qs/Ui/PanelToolTip.qml | 5 +- app/qml/imports/qs/Ui/TextField.qml | 7 +- app/qml/imports/qs/Ui/ToggleSwitch.qml | 9 +- app/qml/styles/Omamail/ComboBox.qml | 8 +- app/qml/styles/Omamail/ScrollBar.qml | 9 +- app/qml/styles/Omamail/ToolTip.qml | 7 +- app/tests/qml/tst_host_contract.qml | 155 +++++++++--------- app/tests/qml/tst_standalone_composition.qml | 43 ++--- tests/test_source.sh | 2 +- ui/App.qml | 15 +- ui/BarWidget.qml | 17 +- ui/Service.qml | 9 +- ui/components/ActionIcon.qml | 3 +- ui/components/GmailIcon.qml | 7 +- ui/components/IconButton.qml | 5 +- ui/components/IconTextButton.qml | 5 +- ui/components/ProviderLogo.qml | 3 +- ui/tests/qml/tst_calendar_settings.qml | 9 +- ui/tests/qml/tst_event_suggestions.qml | 9 +- ui/tests/qml/tst_load_more.qml | 9 +- ui/tests/qml/tst_message_list_press.qml | 9 +- ui/tests/qml/tst_notification_open.qml | 13 +- ui/tests/qml/tst_setup_name_field.qml | 17 +- ui/tests/qml/tst_sidebar_fold.qml | 9 +- 36 files changed, 239 insertions(+), 203 deletions(-) diff --git a/app/qml/Main.qml b/app/qml/Main.qml index e93c847..9dcc2b1 100644 --- a/app/qml/Main.qml +++ b/app/qml/Main.qml @@ -4,6 +4,7 @@ import QtQuick.Window import Quickshell import Omamail.Native import qs.Commons +import qs.Commons as Commons import "../../ui" as Omamail import "../../ui/settings/Appearance.js" as Appearance @@ -140,7 +141,7 @@ ApplicationWindow { Binding { target: Quickshell; property: "nativeHost"; value: root.nativeHost } Binding { target: Quickshell; property: "fileStore"; value: root.nativeFileStore } Binding { - target: Color + target: Commons.Color property: "preferredAppearance" value: Appearance.paletteFor(mailService.appearance) } @@ -211,7 +212,7 @@ ApplicationWindow { // The palette's own border role rather than the control border: a // control's edge is the foreground at 40%, which on a light palette is a // dark line around the whole window. - border.color: Color.border + border.color: Commons.Color.border antialiasing: false } diff --git a/app/qml/StandaloneShell.qml b/app/qml/StandaloneShell.qml index c34d572..f3c32f3 100644 --- a/app/qml/StandaloneShell.qml +++ b/app/qml/StandaloneShell.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Dialogs import qs.Commons +import qs.Commons as Commons QtObject { id: root @@ -24,7 +25,7 @@ QtObject { reopen: !!host && !!host.capabilities && host.capabilities.reopen === true, // An installed Omarchy theme is the palette; the appearance setting only // has something to decide where the fallback palettes are in use. - appearance: !Color.hasOmarchyTheme + appearance: !Commons.Color.hasOmarchyTheme }) property var fileCallback: null diff --git a/app/qml/imports/Quickshell/FloatingWindow.qml b/app/qml/imports/Quickshell/FloatingWindow.qml index 59546ff..53fe420 100644 --- a/app/qml/imports/Quickshell/FloatingWindow.qml +++ b/app/qml/imports/Quickshell/FloatingWindow.qml @@ -1,10 +1,11 @@ import QtQuick import qs.Commons +import qs.Commons as Commons Item { property size minimumSize: Qt.size(0, 0) property string title: "" - property color color: Color.background + property color color: Commons.Color.background width: parent ? parent.width : implicitWidth height: parent ? parent.height : implicitHeight } diff --git a/app/qml/imports/qs/Commons/Style.qml b/app/qml/imports/qs/Commons/Style.qml index 6868551..fc1b417 100644 --- a/app/qml/imports/qs/Commons/Style.qml +++ b/app/qml/imports/qs/Commons/Style.qml @@ -1,5 +1,6 @@ pragma Singleton import QtQuick +import qs.Commons as Commons QtObject { id: root @@ -30,8 +31,8 @@ QtObject { // standalone tooltip the same pause, including controls that use the // platform style directly. readonly property int tooltipDelay: 400 - readonly property color normalBorderColor: normalBorderFor(Color.foreground, Color.accent) - readonly property color selectedAccentFill: selectedFillFor(Color.foreground, Color.accent) + readonly property color normalBorderColor: normalBorderFor(Commons.Color.foreground, Commons.Color.accent) + readonly property color selectedAccentFill: selectedFillFor(Commons.Color.foreground, Commons.Color.accent) readonly property var font: ({ family: metrics.font.family, iconFamily: iconFont.status === FontLoader.Ready ? iconFont.name : metrics.font.family, @@ -88,8 +89,8 @@ QtObject { var role = token.toLowerCase() if (role === "foreground" || role === "text") return foreground if (role === "accent") return accent - if (role === "urgent") return Color.urgent - if (role === "background") return Color.background + if (role === "urgent") return Commons.Color.urgent + if (role === "background") return Commons.Color.background if (role === "transparent") return Qt.rgba(0, 0, 0, 0) if (role === "hover" || role === "hover-cursor" || role === "inherit") return fallback return token.charAt(0) === "#" ? token : fallback diff --git a/app/qml/imports/qs/Ui/BarIconButton.qml b/app/qml/imports/qs/Ui/BarIconButton.qml index 62a01b9..4dafcc5 100644 --- a/app/qml/imports/qs/Ui/BarIconButton.qml +++ b/app/qml/imports/qs/Ui/BarIconButton.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Controls as QQC import qs.Commons +import qs.Commons as Commons Item { id: root @@ -11,7 +12,7 @@ Item { property real slotSize: Style.space(24) property real opticalSize: Style.space(20) property bool active: false - property color foreground: Color.foreground + property color foreground: Commons.Color.foreground signal pressed(int button) signal wheelMoved(int delta) diff --git a/app/qml/imports/qs/Ui/BorderSurface.qml b/app/qml/imports/qs/Ui/BorderSurface.qml index 336c9f5..1c3e2a9 100644 --- a/app/qml/imports/qs/Ui/BorderSurface.qml +++ b/app/qml/imports/qs/Ui/BorderSurface.qml @@ -1,9 +1,10 @@ import QtQuick import qs.Commons +import qs.Commons as Commons Rectangle { property var borderSpec: Border.flat(Style.normalBorderColor, Style.normalBorderWidth) - color: Style.withAlpha(Color.background, 0) + color: Style.withAlpha(Commons.Color.background, 0) border.color: borderSpec && borderSpec.color !== undefined ? borderSpec.color : Style.normalBorderColor border.width: borderSpec && borderSpec.width !== undefined diff --git a/app/qml/imports/qs/Ui/Button.qml b/app/qml/imports/qs/Ui/Button.qml index cda7908..c45f509 100644 --- a/app/qml/imports/qs/Ui/Button.qml +++ b/app/qml/imports/qs/Ui/Button.qml @@ -1,14 +1,15 @@ import QtQuick import QtQuick.Controls.Basic as QQC import qs.Commons +import qs.Commons as Commons Rectangle { id: root property string text: "" property string tooltipText: "" - property color foreground: Color.foreground - property color accent: Color.accent + property color foreground: Commons.Color.foreground + property color accent: Commons.Color.accent property color background: Style.normalFillFor(foreground, accent) property bool bordered: false property bool selected: false diff --git a/app/qml/imports/qs/Ui/Dropdown.qml b/app/qml/imports/qs/Ui/Dropdown.qml index a64fd01..3e7be8c 100644 --- a/app/qml/imports/qs/Ui/Dropdown.qml +++ b/app/qml/imports/qs/Ui/Dropdown.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Controls as QQC import qs.Commons +import qs.Commons as Commons Item { id: root @@ -8,10 +9,10 @@ Item { property string label: "" property string value: "" property var options: [] - property color foreground: Color.foreground - property color background: Color.background - property color popupBorder: Color.popups.border - property color accent: Color.accent + property color foreground: Commons.Color.foreground + property color background: Commons.Color.background + property color popupBorder: Commons.Color.popups.border + property color accent: Commons.Color.accent property string fontFamily: Style.font.family property int rowHeight: Style.spacing.controlHeight property int popupRowHeight: Style.spacing.popupRowHeight diff --git a/app/qml/imports/qs/Ui/NumberField.qml b/app/qml/imports/qs/Ui/NumberField.qml index 890b7b6..7f295da 100644 --- a/app/qml/imports/qs/Ui/NumberField.qml +++ b/app/qml/imports/qs/Ui/NumberField.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Controls as QQC import qs.Commons +import qs.Commons as Commons Item { id: root @@ -10,8 +11,8 @@ Item { property int from: 0 property int to: 100 property int stepSize: 1 - property color foreground: Color.foreground - property color accent: Color.accent + property color foreground: Commons.Color.foreground + property color accent: Commons.Color.accent property string fontFamily: Style.font.family property real fontSize: Style.font.bodySmall property real fieldWidth: Style.space(80) @@ -48,7 +49,7 @@ Item { editable: true font.family: root.fontFamily font.pixelSize: root.fontSize - palette.window: Color.background + palette.window: Commons.Color.background palette.windowText: root.foreground palette.highlight: root.accent onValueModified: { diff --git a/app/qml/imports/qs/Ui/PanelActionButton.qml b/app/qml/imports/qs/Ui/PanelActionButton.qml index ce3abdf..23197c2 100644 --- a/app/qml/imports/qs/Ui/PanelActionButton.qml +++ b/app/qml/imports/qs/Ui/PanelActionButton.qml @@ -1,13 +1,14 @@ import QtQuick import QtQuick.Controls.Basic as QQC import qs.Commons +import qs.Commons as Commons Item { id: root property string iconText: "" property string tooltipText: "" - property color foreground: Color.foreground + property color foreground: Commons.Color.foreground property color hoverColor: foreground property string fontFamily: Style.font.family property real fontSize: Style.font.bodySmall diff --git a/app/qml/imports/qs/Ui/PanelSectionHeader.qml b/app/qml/imports/qs/Ui/PanelSectionHeader.qml index 2728caa..33ff8fd 100644 --- a/app/qml/imports/qs/Ui/PanelSectionHeader.qml +++ b/app/qml/imports/qs/Ui/PanelSectionHeader.qml @@ -1,8 +1,9 @@ import QtQuick import qs.Commons +import qs.Commons as Commons Text { - property color foreground: Color.foreground + property color foreground: Commons.Color.foreground property string fontFamily: Style.font.family color: foreground font.family: fontFamily diff --git a/app/qml/imports/qs/Ui/PanelSeparator.qml b/app/qml/imports/qs/Ui/PanelSeparator.qml index 59e8b67..42be822 100644 --- a/app/qml/imports/qs/Ui/PanelSeparator.qml +++ b/app/qml/imports/qs/Ui/PanelSeparator.qml @@ -1,11 +1,12 @@ import QtQuick import qs.Commons +import qs.Commons as Commons Item { - property color foreground: Color.foreground + property color foreground: Commons.Color.foreground implicitHeight: Math.max(1, Style.normalBorderWidth) Rectangle { anchors.fill: parent - color: Style.normalBorderFor(parent.foreground, Color.accent) + color: Style.normalBorderFor(parent.foreground, Commons.Color.accent) } } diff --git a/app/qml/imports/qs/Ui/PanelToolTip.qml b/app/qml/imports/qs/Ui/PanelToolTip.qml index 8107d0e..c2bd099 100644 --- a/app/qml/imports/qs/Ui/PanelToolTip.qml +++ b/app/qml/imports/qs/Ui/PanelToolTip.qml @@ -1,12 +1,13 @@ import QtQuick import QtQuick.Controls as QQC import qs.Commons +import qs.Commons as Commons QQC.ToolTip { property string fontFamily: Style.font.family delay: Style.tooltipDelay font.family: fontFamily font.pixelSize: Style.font.caption - palette.window: Color.popups.background - palette.windowText: Color.popups.text + palette.window: Commons.Color.popups.background + palette.windowText: Commons.Color.popups.text } diff --git a/app/qml/imports/qs/Ui/TextField.qml b/app/qml/imports/qs/Ui/TextField.qml index dce663e..afdd0dc 100644 --- a/app/qml/imports/qs/Ui/TextField.qml +++ b/app/qml/imports/qs/Ui/TextField.qml @@ -1,20 +1,21 @@ import QtQuick import QtQuick.Controls.Basic as QQC import qs.Commons +import qs.Commons as Commons QQC.TextField { id: root property bool password: false - property color foreground: Color.foreground - property color accent: Color.accent + property color foreground: Commons.Color.foreground + property color accent: Commons.Color.accent property real verticalPadding: Style.spacing.inputPaddingY property real horizontalPadding: Style.spacing.controlPaddingX color: foreground selectionColor: Style.selectionFillFor(foreground, accent) selectedTextColor: foreground - placeholderTextColor: Style.mutedColorFor(foreground, Color.background) + placeholderTextColor: Style.mutedColorFor(foreground, Commons.Color.background) echoMode: password ? TextInput.Password : TextInput.Normal topPadding: verticalPadding bottomPadding: verticalPadding diff --git a/app/qml/imports/qs/Ui/ToggleSwitch.qml b/app/qml/imports/qs/Ui/ToggleSwitch.qml index 4362b5b..7600461 100644 --- a/app/qml/imports/qs/Ui/ToggleSwitch.qml +++ b/app/qml/imports/qs/Ui/ToggleSwitch.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Controls as QQC import qs.Commons +import qs.Commons as Commons Item { id: root @@ -10,8 +11,8 @@ Item { property bool interactive: true property bool cursorRing: false property bool hasCursor: false - property color foreground: Color.foreground - property color accent: Color.accent + property color foreground: Commons.Color.foreground + property color accent: Commons.Color.accent signal toggled() onCheckedChanged: { @@ -28,7 +29,7 @@ Item { checked: root.checked enabled: root.interactive && !root.busy focusPolicy: root.interactive ? Qt.StrongFocus : Qt.NoFocus - palette.window: Color.background + palette.window: Commons.Color.background palette.windowText: root.foreground palette.highlight: root.accent onToggled: { @@ -53,7 +54,7 @@ Item { height: parent.height running: root.busy visible: running - palette.window: Color.background + palette.window: Commons.Color.background palette.windowText: root.foreground palette.highlight: root.accent } diff --git a/app/qml/styles/Omamail/ComboBox.qml b/app/qml/styles/Omamail/ComboBox.qml index c18278d..3e7a50f 100644 --- a/app/qml/styles/Omamail/ComboBox.qml +++ b/app/qml/styles/Omamail/ComboBox.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Templates as T import qs.Commons +import qs.Commons as Commons // The shell's Dropdown, drawn here: a platform ComboBox brings its own // up/down arrows and its own type size, which read as a foreign control in a @@ -28,6 +29,7 @@ T.ComboBox { } indicator: Canvas { + id: chevron objectName: "omamail-combobox-chevron" x: control.width - width - Style.spacing.controlPaddingX y: (control.height - height) / 2 @@ -46,7 +48,7 @@ T.ComboBox { } Connections { target: control - function onSemanticForegroundChanged() { parent.requestPaint() } + function onSemanticForegroundChanged() { chevron.requestPaint() } } } @@ -87,9 +89,9 @@ T.ComboBox { } background: Rectangle { - color: Color.popups.background + color: Commons.Color.popups.background border.width: 1 - border.color: Color.popups.border + border.color: Commons.Color.popups.border radius: Style.cornerRadius } } diff --git a/app/qml/styles/Omamail/ScrollBar.qml b/app/qml/styles/Omamail/ScrollBar.qml index 9ffeaad..8201519 100644 --- a/app/qml/styles/Omamail/ScrollBar.qml +++ b/app/qml/styles/Omamail/ScrollBar.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Templates as T import qs.Commons +import qs.Commons as Commons T.ScrollBar { id: control @@ -19,10 +20,10 @@ T.ScrollBar { implicitHeight: control.interactive ? Style.space(8) : Style.space(3) radius: Style.cornerRadius color: control.pressed - ? Style.pressedFillFor(Color.foreground, Color.accent) + ? Style.pressedFillFor(Commons.Color.foreground, Commons.Color.accent) : (control.hovered - ? Style.selectedFillFor(Color.foreground, Color.accent) - : Style.hoverFillFor(Color.foreground, Color.accent)) + ? Style.selectedFillFor(Commons.Color.foreground, Commons.Color.accent) + : Style.hoverFillFor(Commons.Color.foreground, Commons.Color.accent)) opacity: control.policy === T.ScrollBar.AlwaysOn || (control.active && control.size < 1.0) ? 1 : 0 @@ -33,6 +34,6 @@ T.ScrollBar { visible: control.policy === T.ScrollBar.AlwaysOn || (control.active && control.size < 1.0) radius: Style.cornerRadius - color: Style.normalFillFor(Color.foreground, Color.accent) + color: Style.normalFillFor(Commons.Color.foreground, Commons.Color.accent) } } diff --git a/app/qml/styles/Omamail/ToolTip.qml b/app/qml/styles/Omamail/ToolTip.qml index 33fb2b7..5fffc53 100644 --- a/app/qml/styles/Omamail/ToolTip.qml +++ b/app/qml/styles/Omamail/ToolTip.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Templates as T import qs.Commons +import qs.Commons as Commons T.ToolTip { id: control @@ -45,16 +46,16 @@ T.ToolTip { contentItem: Text { objectName: "omamail-tooltip-label" text: control.text - color: Color.popups.text + color: Commons.Color.popups.text font.family: Style.font.family font.pixelSize: Style.font.caption } background: Rectangle { objectName: "omamail-tooltip-background" - color: Color.popups.background + color: Commons.Color.popups.background radius: Style.cornerRadius border.width: Style.normalBorderWidth - border.color: Color.popups.border + border.color: Commons.Color.popups.border } } diff --git a/app/tests/qml/tst_host_contract.qml b/app/tests/qml/tst_host_contract.qml index 50b1a87..d5f4d72 100644 --- a/app/tests/qml/tst_host_contract.qml +++ b/app/tests/qml/tst_host_contract.qml @@ -3,6 +3,7 @@ import QtTest import Quickshell import Quickshell.Io import qs.Commons +import qs.Commons as Commons import qs.Ui import "../../../ui" as Omamail import "../../../ui/components" as Components @@ -99,7 +100,7 @@ TestCase { visible: false Button { id: button; text: "Save"; tooltipText: "Save help" } TextField { id: textField; password: true } - BorderSurface { id: surface; borderSpec: Border.controlSpec("normal", Color.foreground, Color.accent) } + BorderSurface { id: surface; borderSpec: Border.controlSpec("normal", Commons.Color.foreground, Commons.Color.accent) } NumberField { id: numberField; label: "Count"; from: 1; to: 10; value: 4 } PanelActionButton { id: actionButton; iconText: "+" } PanelSeparator { id: separator } @@ -113,17 +114,17 @@ TestCase { host.reset() Quickshell.nativeHost = host Quickshell.fileStore = host - Color.reload() + Commons.Color.reload() } function test_panel_tooltip_uses_shared_hover_delay() { compare(toolTip.delay, Style.tooltipDelay) - compare(String(toolTip.palette.window), String(Color.popups.background)) - compare(String(toolTip.palette.windowText), String(Color.popups.text)) + compare(String(toolTip.palette.window), String(Commons.Color.popups.background)) + compare(String(toolTip.palette.windowText), String(Commons.Color.popups.text)) } function test_tooltip_draws_an_opaque_nonzero_surface_behind_its_text() { - Color.applySystemAppearance("light") + Commons.Color.applySystemAppearance("light") toolTip.visible = true tryCompare(toolTip, "opened", true, Style.tooltipDelay + 1000) verify(toolTip.width > toolTip.contentItem.implicitWidth) @@ -134,8 +135,8 @@ TestCase { compare(toolTip.background.opacity, 1) compare(toolTip.background.color.a, 1) verify(toolTip.background.z < toolTip.contentItem.z) - compare(String(toolTip.background.color), String(Color.popups.background)) - compare(String(toolTip.background.color), String(Color.background)) + compare(String(toolTip.background.color), String(Commons.Color.popups.background)) + compare(String(toolTip.background.color), String(Commons.Color.background)) toolTip.visible = false tryCompare(toolTip, "opened", false) } @@ -169,17 +170,17 @@ TestCase { } function test_semantic_theme_comes_from_a_valid_system_palette() { - verify(Color.foreground.valid) - verify(Color.background.valid) - verify(Color.accent.valid) - verify(Color.urgent.valid) - verify(Color.popups.background.valid) - compare(String(Color.popups.background), String(Color.background)) - verify(Color.popups.border.valid) + verify(Commons.Color.foreground.valid) + verify(Commons.Color.background.valid) + verify(Commons.Color.accent.valid) + verify(Commons.Color.urgent.valid) + verify(Commons.Color.popups.background.valid) + compare(String(Commons.Color.popups.background), String(Commons.Color.background)) + verify(Commons.Color.popups.border.valid) verify(Style.normalBorderColor.valid) verify(Style.selectedAccentFill.valid) verify(Style.space(8) > 0) - verify(Style.mutedColorFor(Color.foreground, Color.background).valid) + verify(Style.mutedColorFor(Commons.Color.foreground, Commons.Color.background).valid) compare(Style.cornerRadius, 0) compare(Style.font.body, 12) compare(Style.font.caption, 10) @@ -211,9 +212,9 @@ TestCase { "/fixture/home/.config/omarchy/current/theme/colors.toml": "background='#ffffff'\nforeground='#000000'\naccent='#abcdef'\nred='#990000'\nyellow='#996600'\ngreen='#006600'" }) - verify(Color.reload()) - compare(String(Color.background), "#111111") - compare(String(Color.accent), "#123456") + verify(Commons.Color.reload()) + compare(String(Commons.Color.background), "#111111") + compare(String(Commons.Color.accent), "#123456") } function test_legacy_theme_is_used_only_without_state_current() { @@ -221,9 +222,9 @@ TestCase { "/fixture/home/.config/omarchy/current/theme/colors.toml": "background='#ffffff'\nforeground='#000000'\naccent='#205ea6'\nred='#af3029'\nyellow='#855b00'\ngreen='#526600'" }) - verify(Color.reload()) - compare(String(Color.background), "#ffffff") - compare(String(Color.accent), "#205ea6") + verify(Commons.Color.reload()) + compare(String(Commons.Color.background), "#ffffff") + compare(String(Commons.Color.accent), "#205ea6") } function test_invalid_current_theme_falls_back_atomically() { @@ -233,70 +234,70 @@ TestCase { "/fixture/home/.config/omarchy/current/theme/colors.toml": "background='#ffffff'\nforeground='#000000'\naccent='#abcdef'\nred='#990000'\nyellow='#996600'\ngreen='#006600'" }) - Color.applySystemAppearance("dark") - verify(!Color.reload()) - compare(String(Color.background), "#1a1b26") - compare(String(Color.foreground), "#a9b1d6") - compare(String(Color.accent), "#7aa2f7") - compare(String(Color.urgent), "#f7768e") + Commons.Color.applySystemAppearance("dark") + verify(!Commons.Color.reload()) + compare(String(Commons.Color.background), "#1a1b26") + compare(String(Commons.Color.foreground), "#a9b1d6") + compare(String(Commons.Color.accent), "#7aa2f7") + compare(String(Commons.Color.urgent), "#f7768e") } function test_system_scheme_switches_complete_fallback_but_not_omarchy_theme() { host.files = ({}) - Color.reload() - verify(!Color.hasOmarchyTheme) - verify(Color.applySystemAppearance("light")) - compare(Color.dark, false) - compare(String(Color.background), "#fffcf0") - compare(String(Color.foreground), "#100f0f") - compare(String(Color.accent), "#205ea6") - compare(String(Color.surface), "#f2f0e5") - compare(String(Color.inset), "#e6e4d9") - compare(String(Color.border), "#b7b5ac") - - verify(Color.applySystemAppearance("dark")) - compare(Color.dark, true) - compare(String(Color.background), "#1a1b26") + Commons.Color.reload() + verify(!Commons.Color.hasOmarchyTheme) + verify(Commons.Color.applySystemAppearance("light")) + compare(Commons.Color.dark, false) + compare(String(Commons.Color.background), "#fffcf0") + compare(String(Commons.Color.foreground), "#100f0f") + compare(String(Commons.Color.accent), "#205ea6") + compare(String(Commons.Color.surface), "#f2f0e5") + compare(String(Commons.Color.inset), "#e6e4d9") + compare(String(Commons.Color.border), "#b7b5ac") + + verify(Commons.Color.applySystemAppearance("dark")) + compare(Commons.Color.dark, true) + compare(String(Commons.Color.background), "#1a1b26") var path = "/fixture/home/.local/state/omarchy/current/theme/colors.toml" host.files[path] = "mode='light'\nbackground='#ffffff'\nforeground='#111111'\naccent='#123456'\nred='#990000'\nyellow='#996600'\ngreen='#006600'" - verify(Color.reload()) - verify(Color.hasOmarchyTheme) - compare(String(Color.accent), "#123456") - verify(!Color.applySystemAppearance("dark")) - compare(String(Color.accent), "#123456") - compare(Color.dark, false) + verify(Commons.Color.reload()) + verify(Commons.Color.hasOmarchyTheme) + compare(String(Commons.Color.accent), "#123456") + verify(!Commons.Color.applySystemAppearance("dark")) + compare(String(Commons.Color.accent), "#123456") + compare(Commons.Color.dark, false) } function test_preferred_appearance_overrides_the_desktop_but_not_omarchy() { host.files = ({}) - Color.preferredAppearance = "" - Color.reload() - verify(!Color.hasOmarchyTheme) - Color.preferredAppearance = "dark" - compare(Color.dark, true) - compare(String(Color.background), "#1a1b26") - Color.preferredAppearance = "light" - compare(Color.dark, false) - compare(String(Color.background), "#fffcf0") + Commons.Color.preferredAppearance = "" + Commons.Color.reload() + verify(!Commons.Color.hasOmarchyTheme) + Commons.Color.preferredAppearance = "dark" + compare(Commons.Color.dark, true) + compare(String(Commons.Color.background), "#1a1b26") + Commons.Color.preferredAppearance = "light" + compare(Commons.Color.dark, false) + compare(String(Commons.Color.background), "#fffcf0") // The desktop's scheme only decides when nothing is preferred. - verify(Color.applySystemAppearance("dark")) - compare(Color.dark, true) - compare(Color.fallbackAppearance(), "light") - Color.reload() - compare(Color.dark, false) + verify(Commons.Color.applySystemAppearance("dark")) + compare(Commons.Color.dark, true) + compare(Commons.Color.fallbackAppearance(), "light") + Commons.Color.reload() + compare(Commons.Color.dark, false) var path = "/fixture/home/.local/state/omarchy/current/theme/colors.toml" host.files[path] = "mode='dark'\nbackground='#010203'\nforeground='#fefefe'\naccent='#123456'\nred='#990000'\nyellow='#996600'\ngreen='#006600'" - verify(Color.reload()) - verify(Color.hasOmarchyTheme) - Color.preferredAppearance = "light" - compare(Color.dark, true) - compare(String(Color.background), "#010203") - Color.preferredAppearance = "" + verify(Commons.Color.reload()) + verify(Commons.Color.hasOmarchyTheme) + Commons.Color.preferredAppearance = "light" + compare(Commons.Color.dark, true) + compare(String(Commons.Color.background), "#010203") + Commons.Color.preferredAppearance = "" } function test_theme_change_reloads_the_active_palette() { @@ -304,14 +305,14 @@ TestCase { host.files = ({}) host.files[path] = "background='#111111'\nforeground='#eeeeee'\naccent='#123456'\nred='#cc3333'\nyellow='#cccc33'\ngreen='#33cc33'" - verify(Color.reload()) + verify(Commons.Color.reload()) verify(host.watched[path]) host.files[path] = "background='#222222'\nforeground='#ffffff'\naccent='#654321'\nred='#dd4444'\nyellow='#dddd44'\ngreen='#44dd44'" host.changed(path) - compare(String(Color.background), "#222222") - compare(String(Color.accent), "#654321") + compare(String(Commons.Color.background), "#222222") + compare(String(Commons.Color.accent), "#654321") } function test_shell_theme_and_user_override_apply_atomically() { @@ -329,16 +330,16 @@ TestCase { "[font]\nbase-size=16\n[spacing]\ncontrol-height=38\n" + "[controls]\nnormal-fill-alpha=0.2" - verify(Color.reload()) + verify(Commons.Color.reload()) compare(Style.font.body, 17) compare(Style.font.caption, 13) compare(Style.spacing.controlHeight, 38) compare(Style.space(10), 15) - fuzzyCompare(Style.normalFillFor(Color.foreground, Color.accent).a, 0.2, 0.001) - compare(String(Style.selectedStateColor(Color.foreground, Color.accent)), "#336699") - compare(String(Color.popups.text), "#336699") - fuzzyCompare(Color.popups.background.a, 0.8, 0.001) - fuzzyCompare(Color.popups.border.a, 0.3, 0.001) + fuzzyCompare(Style.normalFillFor(Commons.Color.foreground, Commons.Color.accent).a, 0.2, 0.001) + compare(String(Style.selectedStateColor(Commons.Color.foreground, Commons.Color.accent)), "#336699") + compare(String(Commons.Color.popups.text), "#336699") + fuzzyCompare(Commons.Color.popups.background.a, 0.8, 0.001) + fuzzyCompare(Commons.Color.popups.border.a, 0.3, 0.001) verify(host.watched[themeRoot + "/shell.toml"]) verify(host.watched[userShell]) } @@ -351,7 +352,7 @@ TestCase { "background='#101010'\nforeground='#eeeeee'\naccent='#336699'\nred='#cc3333'\nyellow='#cccc33'\ngreen='#33cc33'" host.files[themeRoot + "/shell.toml"] = "[font]\nbase-size=14\n[spacing]\ncontrol-height=30" host.files[userShell] = "[font]\nbase-size=18\n[spacing]\ncontrol-height=44" - verify(Color.reload()) + verify(Commons.Color.reload()) compare(Style.font.body, 18) compare(Style.spacing.controlHeight, 44) diff --git a/app/tests/qml/tst_standalone_composition.qml b/app/tests/qml/tst_standalone_composition.qml index 61a05d2..c62b14e 100644 --- a/app/tests/qml/tst_standalone_composition.qml +++ b/app/tests/qml/tst_standalone_composition.qml @@ -3,6 +3,7 @@ import QtQuick.Controls import QtTest import Quickshell import qs.Commons +import qs.Commons as Commons import "../../qml" as Standalone import "../../../ui/providers" as Providers import "../../../ui/components" as Components @@ -47,10 +48,10 @@ TestCase { Components.CalendarSettings { service: calendarFixture controller: calendarFixture - textColor: Color.foreground - dimColor: Style.mutedColorFor(Color.foreground, Color.background) - accentColor: Color.accent - urgentColor: Color.accent + textColor: Commons.Color.foreground + dimColor: Style.mutedColorFor(Commons.Color.foreground, Commons.Color.background) + accentColor: Commons.Color.accent + urgentColor: Commons.Color.accent panelFontFamily: Style.font.family } } @@ -59,18 +60,18 @@ TestCase { id: styledSwitch visible: false checked: true - palette.window: Color.background - palette.windowText: Color.foreground - palette.highlight: Color.accent + palette.window: Commons.Color.background + palette.windowText: Commons.Color.foreground + palette.highlight: Commons.Color.accent } SpinBox { id: styledSpinBox visible: false editable: true - palette.window: Color.background - palette.windowText: Color.foreground - palette.highlight: Color.accent + palette.window: Commons.Color.background + palette.windowText: Commons.Color.foreground + palette.highlight: Commons.Color.accent } ToolTip { @@ -206,15 +207,15 @@ TestCase { verify(findChild(styledSpinBox, "omamail-spinbox-decrement")) verify(findChild(styledSpinBox, "omamail-spinbox-increment")) compare(background.radius, Style.cornerRadius) - compare(String(styledSpinBox.palette.windowText), String(Color.foreground)) + compare(String(styledSpinBox.palette.windowText), String(Commons.Color.foreground)) compare(styledToolTip.delay, Style.tooltipDelay) var tooltipLabel = findChild(styledToolTip, "omamail-tooltip-label") var tooltipBackground = findChild(styledToolTip, "omamail-tooltip-background") verify(tooltipLabel) verify(tooltipBackground) - compare(String(tooltipLabel.color), String(Color.popups.text)) - compare(String(tooltipBackground.color), String(Color.popups.background)) - compare(String(tooltipBackground.border.color), String(Color.popups.border)) + compare(String(tooltipLabel.color), String(Commons.Color.popups.text)) + compare(String(tooltipBackground.color), String(Commons.Color.popups.background)) + compare(String(tooltipBackground.border.color), String(Commons.Color.popups.border)) compare(tooltipBackground.radius, Style.cornerRadius) } @@ -257,8 +258,8 @@ TestCase { verify(composition.app.standaloneWindowChrome) var windowBorder = findChild(composition, "standalone-window-border") verify(windowBorder) - compare(String(windowBorder.border.color), String(Color.border)) - verify(String(Color.border) !== String(composition.app.borderColor)) + compare(String(windowBorder.border.color), String(Commons.Color.border)) + verify(String(Commons.Color.border) !== String(composition.app.borderColor)) compare(windowBorder.border.width, composition.app.borderWidth / Math.max(1, Screen.devicePixelRatio)) verify(windowBorder.visible) @@ -317,13 +318,13 @@ TestCase { findChild(composition, "appearance-dark").clicked() compare(host.settings.appearance, "Dark") compare(composition.service.appearance, "Dark") - compare(Color.dark, true) - compare(String(Color.preferredAppearance), "dark") + compare(Commons.Color.dark, true) + compare(String(Commons.Color.preferredAppearance), "dark") findChild(composition, "appearance-light").clicked() - compare(Color.dark, false) - compare(String(Color.background), "#fffcf0") + compare(Commons.Color.dark, false) + compare(String(Commons.Color.background), "#fffcf0") findChild(composition, "appearance-system").clicked() - compare(String(Color.preferredAppearance), "") + compare(String(Commons.Color.preferredAppearance), "") var appMenu = findChild(composition, "app-menu") verify(appMenu) compare(appMenu.canQuit, true) diff --git a/tests/test_source.sh b/tests/test_source.sh index 72b5fea..2cee5e8 100755 --- a/tests/test_source.sh +++ b/tests/test_source.sh @@ -224,7 +224,7 @@ grep -q 'bar ? bar\.barForeground' BarWidget.qml \ || fail "the bar icon must follow bar.barForeground in transparent mode" grep -q 'markColor: root.accent' App.qml \ || fail "the Omamail header M must use the active theme accent" -grep -q 'markColor: Color.accent' BarWidget.qml \ +grep -q 'markColor: Commons.Color.accent' BarWidget.qml \ || fail "the bar M must use the active theme accent" # IconTextButton has no separate hover glyph colour. Assigning one makes the diff --git a/ui/App.qml b/ui/App.qml index 7e105ae..6fe4427 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -4,6 +4,7 @@ import QtQuick.Window import Quickshell import Quickshell.Io import qs.Commons +import qs.Commons as Commons import qs.Ui import "account/Model.js" as Model @@ -106,13 +107,13 @@ Item { onTriggered: root.saveComposeRecovery() } - readonly property color foreground: Color.foreground - readonly property color background: Color.background - readonly property color accent: Color.accent - readonly property color urgent: Color.urgent - readonly property color danger: Color.urgent - readonly property color popupBackground: Color.popups.background - readonly property color popupBorder: Color.popups.border + readonly property color foreground: Commons.Color.foreground + readonly property color background: Commons.Color.background + readonly property color accent: Commons.Color.accent + readonly property color urgent: Commons.Color.urgent + readonly property color danger: Commons.Color.urgent + readonly property color popupBackground: Commons.Color.popups.background + readonly property color popupBorder: Commons.Color.popups.border // Shared themed border for mail and standalone window chrome. readonly property color borderColor: Style.normalBorderColor readonly property int borderWidth: Style.normalBorderWidth diff --git a/ui/BarWidget.qml b/ui/BarWidget.qml index 808707d..1bfe934 100644 --- a/ui/BarWidget.qml +++ b/ui/BarWidget.qml @@ -1,6 +1,7 @@ import QtQuick import Quickshell import qs.Commons +import qs.Commons as Commons import qs.Ui import "components" import "bar" @@ -70,7 +71,7 @@ BarWidget { // `barForeground` belongs to qs.Ui.Panel, not to BarWidget: reading it here // yields undefined, and assigning undefined to a colour leaves the icon // unpainted. The bar itself is the source. - readonly property color foreground: bar ? bar.barForeground : Color.foreground + readonly property color foreground: bar ? bar.barForeground : Commons.Color.foreground property bool previewOpen: false property bool popoutSwitchClosing: false @@ -168,7 +169,7 @@ BarWidget { anchors.centerIn: parent iconSize: Style.space(12) color: button.glyphColor - markColor: Color.accent + markColor: Commons.Color.accent // The dot is simply whether unread mail is waiting. It used to mean // "something arrived since you last looked", which was a different // question from the one anyone asks of a mail icon, and it could not @@ -204,7 +205,7 @@ BarWidget { id: openIndicator readonly property bool vertical: !!root.bar && root.bar.vertical visible: button.windowOpen - color: Color.accent + color: Commons.Color.accent radius: Math.min(width, height) / 2 width: vertical ? Style.space(2) : Style.space(10) height: vertical ? Style.space(10) : Style.space(2) @@ -231,11 +232,11 @@ BarWidget { width: parent ? parent.width : 0 messages: root.gmail ? root.gmail.barMessages : [] events: root.gmail ? root.gmail.barEvents : [] - textColor: Color.popups.text - backgroundColor: Color.popups.background - accentColor: Color.accent - dimColor: Qt.rgba(Color.popups.text.r, Color.popups.text.g, - Color.popups.text.b, 0.62) + textColor: Commons.Color.popups.text + backgroundColor: Commons.Color.popups.background + accentColor: Commons.Color.accent + dimColor: Qt.rgba(Commons.Color.popups.text.r, Commons.Color.popups.text.g, + Commons.Color.popups.text.b, 0.62) panelFontFamily: Style.font.family contentDirection: root.gmail ? root.gmail.contentDirection : "" onMessageRequested: function(accountId, messageId) { diff --git a/ui/Service.qml b/ui/Service.qml index ed659c0..d1f9eae 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -3,6 +3,7 @@ import "compose/Recipients.js" as AgentRecipients import Quickshell import Quickshell.Io import qs.Commons +import qs.Commons as Commons import "account" import "calendar" import "agent" @@ -2636,8 +2637,8 @@ Item { CalendarReminders { service: root pluginDir: root.pluginDir - notificationForeground: Color.foreground - notificationAccent: Color.accent + notificationForeground: Commons.Color.foreground + notificationAccent: Commons.Color.accent } } } @@ -2667,8 +2668,8 @@ Item { return index < accounts.length ? accounts[index] : null } - notificationForeground: Color.foreground - notificationAccent: Color.accent + notificationForeground: Commons.Color.foreground + notificationAccent: Commons.Color.accent pluginDir: root.pluginDir accountId: entry ? entry.id : "" backend: root.backend diff --git a/ui/components/ActionIcon.qml b/ui/components/ActionIcon.qml index bee6338..cdd4dab 100644 --- a/ui/components/ActionIcon.qml +++ b/ui/components/ActionIcon.qml @@ -1,5 +1,6 @@ import QtQuick import qs.Commons +import qs.Commons as Commons import "Icons.js" as Icons // One of the app's icons, by name. @@ -16,7 +17,7 @@ Item { id: root property string name: "" - property color color: Color.foreground + property color color: Commons.Color.foreground // Omamail keeps the envelope in the foreground and gives its M the active // theme accent. Provider artwork uses ProviderLogo instead of this mark. property color markColor: color diff --git a/ui/components/GmailIcon.qml b/ui/components/GmailIcon.qml index c327f2f..430aa3b 100644 --- a/ui/components/GmailIcon.qml +++ b/ui/components/GmailIcon.qml @@ -1,5 +1,6 @@ import QtQuick import qs.Commons +import qs.Commons as Commons import qs.Ui // The mark, drawn rather than rasterised from an SVG: the bar slot is about @@ -12,9 +13,9 @@ Item { id: root property real iconSize: Style.font.icon - property color color: Color.foreground + property color color: Commons.Color.foreground property color markColor: color - property color badgeColor: Color.urgent + property color badgeColor: Commons.Color.urgent // A dot, not a count: the bar says "something arrived", the tooltip says // how much, and the window says what. property bool dot: false @@ -102,6 +103,6 @@ Item { anchors.rightMargin: -parent.width * 0.06 anchors.top: parent.top anchors.topMargin: -parent.height * 0.04 - borderSpec: Border.flat(Color.popups.background, 1) + borderSpec: Border.flat(Commons.Color.popups.background, 1) } } diff --git a/ui/components/IconButton.qml b/ui/components/IconButton.qml index 74f0f7e..dd4c1d6 100644 --- a/ui/components/IconButton.qml +++ b/ui/components/IconButton.qml @@ -1,5 +1,6 @@ import QtQuick import qs.Commons +import qs.Commons as Commons import qs.Ui // A drawn icon on the kit's shared hover/cursor surface. qs.Ui's @@ -10,9 +11,9 @@ Item { property string iconName: "" property string tooltipText: "" - property color foreground: Color.foreground + property color foreground: Commons.Color.foreground property color hoverColor: foreground - property color accent: Color.accent + property color accent: Commons.Color.accent property bool filled: false property bool hasCursor: false // Held down for as long as a menu this button opened is on screen. A trigger diff --git a/ui/components/IconTextButton.qml b/ui/components/IconTextButton.qml index 22156b7..5baed50 100644 --- a/ui/components/IconTextButton.qml +++ b/ui/components/IconTextButton.qml @@ -1,5 +1,6 @@ import QtQuick import qs.Commons +import qs.Commons as Commons import qs.Ui // A bordered button carrying a drawn icon beside its label. @@ -14,8 +15,8 @@ Rectangle { property string iconName: "" property string text: "" property string tooltipText: "" - property color foreground: Color.foreground - property color accent: Color.accent + property color foreground: Commons.Color.foreground + property color accent: Commons.Color.accent property bool bordered: true // Reserve the normal border but leave the field clear until interaction. property bool outline: false diff --git a/ui/components/ProviderLogo.qml b/ui/components/ProviderLogo.qml index 74148f0..4e21d98 100644 --- a/ui/components/ProviderLogo.qml +++ b/ui/components/ProviderLogo.qml @@ -1,5 +1,6 @@ import QtQuick import qs.Commons +import qs.Commons as Commons import qs.Ui // The mark of the service a setup page is about. @@ -29,7 +30,7 @@ Item { // and no brand this could put a name to. Left empty where a missing mark // should simply take no room. property string fallbackIcon: "" - property color fallbackColor: Color.foreground + property color fallbackColor: Commons.Color.foreground readonly property bool present: logo !== "" || fallbackIcon !== "" readonly property bool drawn: logo !== "" diff --git a/ui/tests/qml/tst_calendar_settings.qml b/ui/tests/qml/tst_calendar_settings.qml index cefa595..950e31d 100644 --- a/ui/tests/qml/tst_calendar_settings.qml +++ b/ui/tests/qml/tst_calendar_settings.qml @@ -1,6 +1,7 @@ import QtQuick 2.15 import QtTest 1.3 import qs.Commons +import qs.Commons as Commons import "../../components" as Omamail Item { @@ -74,10 +75,10 @@ Item { width: parent.width service: mailService calendarController: calendarController - textColor: Color.foreground - dimColor: Color.foreground - accentColor: Color.accent - urgentColor: Color.accent + textColor: Commons.Color.foreground + dimColor: Commons.Color.foreground + accentColor: Commons.Color.accent + urgentColor: Commons.Color.accent panelFontFamily: "monospace" } diff --git a/ui/tests/qml/tst_event_suggestions.qml b/ui/tests/qml/tst_event_suggestions.qml index 76924a3..5223fd4 100644 --- a/ui/tests/qml/tst_event_suggestions.qml +++ b/ui/tests/qml/tst_event_suggestions.qml @@ -1,6 +1,7 @@ import QtQuick 2.15 import QtTest 1.3 import qs.Commons +import qs.Commons as Commons import "../.." as Omamail import "../../components" as Mail import "../../account/Accounts.js" as Accounts @@ -66,10 +67,10 @@ Item { Mail.EventSuggestionCard { id: card width: 500 - textColor: Color.foreground - accentColor: Color.accent - dimColor: Color.foreground - dimmerColor: Color.foreground + textColor: Commons.Color.foreground + accentColor: Commons.Color.accent + dimColor: Commons.Color.foreground + dimmerColor: Commons.Color.foreground panelFontFamily: "monospace" } SignalSpy { id: added; target: card; signalName: "addRequested" } diff --git a/ui/tests/qml/tst_load_more.qml b/ui/tests/qml/tst_load_more.qml index f768bb6..409efb8 100644 --- a/ui/tests/qml/tst_load_more.qml +++ b/ui/tests/qml/tst_load_more.qml @@ -1,6 +1,7 @@ import QtQuick 2.15 import QtTest 1.3 import qs.Commons +import qs.Commons as Commons import "../../components" as Mail // The next page comes as the list is scrolled to its foot: once per foot, @@ -37,10 +38,10 @@ Item { scroller: flick width: flick.width service: fakeService - textColor: Color.foreground - accentColor: Color.accent - dimColor: Color.foreground - urgentColor: Color.accent + textColor: Commons.Color.foreground + accentColor: Commons.Color.accent + dimColor: Commons.Color.foreground + urgentColor: Commons.Color.accent panelFontFamily: "monospace" } } diff --git a/ui/tests/qml/tst_message_list_press.qml b/ui/tests/qml/tst_message_list_press.qml index 3af0b83..644db69 100644 --- a/ui/tests/qml/tst_message_list_press.qml +++ b/ui/tests/qml/tst_message_list_press.qml @@ -1,6 +1,7 @@ import QtQuick 2.15 import QtTest 1.3 import qs.Commons +import qs.Commons as Commons import "../../components" as Mail // A press on a row survives the list being told its messages again. @@ -33,10 +34,10 @@ Item { id: list width: parent.width service: fakeService - textColor: Color.foreground - accentColor: Color.accent - dimColor: Color.foreground - urgentColor: Color.accent + textColor: Commons.Color.foreground + accentColor: Commons.Color.accent + dimColor: Commons.Color.foreground + urgentColor: Commons.Color.accent panelFontFamily: "monospace" property var activated: [] onMessageActivated: function(id) { activated.push(id) } diff --git a/ui/tests/qml/tst_notification_open.qml b/ui/tests/qml/tst_notification_open.qml index 41163b4..9a20efc 100644 --- a/ui/tests/qml/tst_notification_open.qml +++ b/ui/tests/qml/tst_notification_open.qml @@ -2,6 +2,7 @@ import QtQuick import QtTest import Quickshell import qs.Commons +import qs.Commons as Commons import "../.." as Omamail import "../../account" as Account import "BackendFixture.js" as BackendFixture @@ -126,18 +127,18 @@ Item { function test_new_notifications_follow_theme_without_bar_foreground() { var account = service.findAccount(second) var firstNotice = notification(account, [{ id: "one" }]) - compare(firstNotice.command[2], String(Color.foreground)) - compare(firstNotice.command[3], String(Color.accent)) - var previous = Color.foreground + compare(firstNotice.command[2], String(Commons.Color.foreground)) + compare(firstNotice.command[3], String(Commons.Color.accent)) + var previous = Commons.Color.foreground try { - Color.foreground = Qt.rgba(0.1, 0.2, 0.3, 1) + Commons.Color.foreground = Qt.rgba(0.1, 0.2, 0.3, 1) var nextNotice = notification(account, [{ id: "two" }]) - compare(nextNotice.command[2], String(Color.foreground)) + compare(nextNotice.command[2], String(Commons.Color.foreground)) verify(firstNotice.command[2] !== nextNotice.command[2]) finish(firstNotice, "") finish(nextNotice, "") } finally { - Color.foreground = previous + Commons.Color.foreground = previous } } diff --git a/ui/tests/qml/tst_setup_name_field.qml b/ui/tests/qml/tst_setup_name_field.qml index a502132..0278cdc 100644 --- a/ui/tests/qml/tst_setup_name_field.qml +++ b/ui/tests/qml/tst_setup_name_field.qml @@ -1,6 +1,7 @@ import QtQuick 2.15 import QtTest 1.3 import qs.Commons +import qs.Commons as Commons import "../../components" as Mail import "../.." as Omamail import "../../account/Accounts.js" as Accounts @@ -69,10 +70,10 @@ Item { id: gmailPage width: 600 service: gmailService - textColor: Color.foreground - dimColor: Color.foreground - dangerColor: Color.accent - accentColor: Color.accent + textColor: Commons.Color.foreground + dimColor: Commons.Color.foreground + dangerColor: Commons.Color.accent + accentColor: Commons.Color.accent panelFontFamily: "monospace" } @@ -91,10 +92,10 @@ Item { id: page width: 600 service: fakeService - textColor: Color.foreground - dimColor: Color.foreground - dangerColor: Color.accent - accentColor: Color.accent + textColor: Commons.Color.foreground + dimColor: Commons.Color.foreground + dangerColor: Commons.Color.accent + accentColor: Commons.Color.accent panelFontFamily: "monospace" } diff --git a/ui/tests/qml/tst_sidebar_fold.qml b/ui/tests/qml/tst_sidebar_fold.qml index 2189d80..3312f90 100644 --- a/ui/tests/qml/tst_sidebar_fold.qml +++ b/ui/tests/qml/tst_sidebar_fold.qml @@ -1,6 +1,7 @@ import QtQuick 2.15 import QtTest 1.3 import qs.Commons +import qs.Commons as Commons import "../../components" as Mail // The chevron folds; it does not also open. Two tap handlers sit one inside @@ -25,9 +26,9 @@ Item { width: 220 height: 600 service: fake - textColor: Color.foreground - accentColor: Color.accent - dimColor: Color.foreground + textColor: Commons.Color.foreground + accentColor: Commons.Color.accent + dimColor: Commons.Color.foreground panelFontFamily: "monospace" slots: [{ kind: "mailbox", key: "inbox" }] } @@ -101,7 +102,7 @@ Item { verify(chip.y < chip.parent.height / 4, "hint stays at the top") verify(chip.height < chip.parent.height / 2, "hint is smaller than the row icon") verify(named(sidebar, "mailbox-entry-icon", [])[0].visible, "modifier preserves mailbox icon") - verify(chip.color.a < Style.selectedFillFor(Color.foreground, Color.accent).a, + verify(chip.color.a < Style.selectedFillFor(Commons.Color.foreground, Commons.Color.accent).a, "a shortcut hint must not use the much heavier selected-state fill") } } From a2762b7b3e6945a4391dd910c60bdc44b5bb4b1f Mon Sep 17 00:00:00 2001 From: nerdislb Date: Sun, 11 Oct 2026 09:31:59 +0200 Subject: [PATCH 08/88] Fix VTIMEZONE offsets for zones that ship retired rules (#326) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit iCloud sends a zone's whole history in every VTIMEZONE: retired `RRULE`s that end in an `UNTIL`, and one-off transitions gathered into `RDATE` lists. `transitionIn` ignored both. A rule kept recurring after its `UNTIL`, so Europe/Vienna's 1981–1995 late-September fall-back won every autumn: an event created on an iPhone at 15:30 on 7 October showed at 16:30, and every iCloud event in that zone was an hour late from late September to the last Sunday of October. `RDATE`s were not read at all, so a zone that has given up summer time (Mexico City, Moscow, Istanbul, …) fell back to whichever entry headed the list, often local mean time from the 1800s. A VTIMEZONE is now read once into a plan kept on the parsed zone. Recurring rules keep their parsed `RRULE`; bare-date rules keep every `DTSTART`/`RDATE`, grouped by year, so a rule that takes effect twice in one year (Casablanca's Ramadan in January and December 2029) counts both. A rule stops at its `UNTIL`. `UNTIL` is UTC while the transition is a wall-clock reading, so the transition is moved to UTC by the rule's `TZOFFSETFROM` before comparing; compared naively, the last transition a zone east of UTC made under a rule falls after its own `UNTIL`. A zone that carries a history (an `UNTIL`, an `RDATE` list, or more than one standard/daylight pair) is also asked about each rule's final year, so it stays where its last change left it. A lone pair of bare dates is still the Exchange case the existing `STANDARD`-first fallback is written for, and keeps it. The candidates a reading chooses between depend only on its year, so they are cached per year on the plan. Of the transitions from earlier years only the latest is kept, so an invitation carrying tens of thousands of `RDATE`s stays bounded: reading every `RDATE` on every call, as an earlier draft of this change did, let a ~460 KB invitation freeze the UI for close to a minute. ## Verification `make validate` green on this commit (on Arch, `shasum` lives in `/usr/bin/core_perl`, which had to be added to `PATH` for `tests/test_source.sh`). New cases in `ui/tests/test_calendar.js`: Europe/Vienna with its retired autumn rules, America/Mexico_City after summer time was abolished, Europe/Moscow with its 2014 change only as an `RDATE`, and Africa/Casablanca with two transitions of one rule in 2029. Against the unfixed code the Vienna case fails (`early October is still summer time`); the Mexico City, Moscow, 1995 east-of-UTC `UNTIL` and Casablanca cases each fail against the intermediate versions that lacked the respective part. A sweep over 418 ICU zones, 2018–2030 on the 5th and 20th of each month, against VTIMEZONEs generated in iCloud's shape (`RRULE` with `UNTIL` for recurring rules, `RDATE` lists for the rest) and compared with `Intl`: 0 zones worse, 288 better, mismatching readings 40,390 → 382 (the remainder is mostly the generator approximating Apple's grouping). Hostile input: a ~480 KB zone with 15,000 `RDATE`s per rule, queried for every day over 300 years, takes 1.0 s / 16 MB heap (original 7.0 s / 12 MB); with the `RDATE`s in the queried year itself 0.4 s (original 0.8 s); 3,200 `RRULE` sub-components over 28 years 1.4 s (original did not finish in 200 s). A single offset lookup on iCloud's Europe/Vienna block: 7 µs (original 302 µs). Live: an iCloud calendar on Europe/Vienna in the Omarchy bar, where events created on an iPhone between late September and late October showed an hour late before and at their time after. A fresh agent reviewed the diff in four rounds. It found the naive `UTC`/wall-clock `UNTIL` comparison, a regression for zones that abolished summer time, unread `RDATE` lists, a second same-year transition being lost, the per-call `RDATE` re-read (UI freeze on a crafted invitation) and unbounded per-year cache growth; those iterations addressed the cases tested above. Review of the current head still found a gap for sparse past/future RDATE histories, and the per-year candidate cache has no eviction. Its last round also noted that the "before every transition" fallback in `zoneOffsetMinutes` is unreachable, as it was before this change; it is left as it was. ## Release Notes - Calendar events from iCloud no longer show an hour late from late September to the end of October in zones such as Europe/Vienna and Europe/Berlin. - Events in zones that have abolished summer time, such as Mexico City or Moscow, show at the right time all year. --------- Co-authored-by: Jason Lee --- ui/message/Calendar.js | 195 ++++++++++++++++++++++++++++++-------- ui/tests/test_calendar.js | 157 ++++++++++++++++++++++++++++++ 2 files changed, 314 insertions(+), 38 deletions(-) diff --git a/ui/message/Calendar.js b/ui/message/Calendar.js index 2579f7b..582562d 100644 --- a/ui/message/Calendar.js +++ b/ui/message/Calendar.js @@ -382,20 +382,100 @@ function parseRuleParts(value) { return parts } +// A VTIMEZONE's sub-components, read once. Recurrence expansion asks for an +// offset on every day of a series and an invitation can carry thousands of +// RDATEs, so nothing below re-reads a property after this. The plan lives on +// the parsed zone, so a zone timezoneDocument reuses is read only once. +// +// STANDARD first, and that ordering is load-bearing: a zone whose rules this +// cannot place — a lone pair of bare dates outside the years looked at, which +// is what an Exchange zone looks like from here — falls back to rules[0], and +// being an hour behind in winter is a better wrong answer than being an hour +// ahead all year. +function zonePlan(timezone) { + if (timezone.transitionPlan) return timezone.transitionPlan + var rules = [] + var kinds = ["STANDARD", "DAYLIGHT"] + for (var k = 0; k < kinds.length; k++) { + var found = childrenNamed(timezone, kinds[k]) + for (var i = 0; i < found.length; i++) { + var offset = parseOffsetMinutes(textOf(found[i], "TZOFFSETTO")) + if (offset === null) continue + rules.push(zoneRule(found[i], offset)) + } + } + // A zone that carries its history — retired rules, RDATE lists, more than + // one standard/daylight pair — is also asked about each rule's final year, + // so one that has given up summer time stays where its last transition left + // it. A lone pair of bare dates is the Exchange case above and keeps its + // fallback. + var history = rules.length > 2 + for (var q = 0; q < rules.length && !history; q++) { + if (rules[q].until || rules[q].listed) history = true + } + timezone.transitionPlan = { rules: rules, history: history, byYear: {} } + return timezone.transitionPlan +} + +// One STANDARD or DAYLIGHT block. A recurring one keeps its parsed RRULE; one +// without keeps every date it takes effect on — its DTSTART and each RDATE, +// which is how iCloud gathers a zone's one-off transitions, so the last of +// them can sit in an RDATE decades after the DTSTART — grouped by year. +function zoneRule(component, offset) { + var rule = { + offset: offset, + from: parseOffsetMinutes(textOf(component, "TZOFFSETFROM")), + start: parseDateValue(textOf(component, "DTSTART")), + parts: null, until: null, dates: {}, dateYears: [], listed: false, lastYear: null + } + if (!rule.start) return rule + var recurrence = property(component, "RRULE") + if (recurrence) { + rule.parts = parseRuleParts(recurrence.value) + rule.until = rule.parts.UNTIL ? parseDateValue(rule.parts.UNTIL) : null + rule.lastYear = rule.until ? rule.until.year : null + return rule + } + var dates = [rule.start] + var extra = properties(component, "RDATE") + rule.listed = extra.length > 0 + for (var i = 0; i < extra.length; i++) { + var values = String(extra[i].value || "").split(",") + for (var j = 0; j < values.length; j++) { + var value = parseDateValue(values[j]) + if (value) dates.push(value) + } + } + for (var d = 0; d < dates.length; d++) { + var year = dates[d].year + if (!rule.dates[year]) { + rule.dates[year] = [] + rule.dateYears.push(year) + } + rule.dates[year].push(naiveKey(dates[d])) + if (rule.lastYear === null || year > rule.lastYear) rule.lastYear = year + } + rule.dateYears.sort(function(a, b) { return a - b }) + return rule +} + // When one of a VTIMEZONE's rules takes effect, in the year asked about. The // rules that matter are the ones real zones are written with: a yearly BYMONTH // with an ordinal BYDAY, a yearly BYMONTH with a BYMONTHDAY, or no rule at all -// — a single dated transition, which is what a fixed-offset zone has. -function transitionIn(rule, year) { - var start = parseDateValue(textOf(rule.component, "DTSTART")) - if (!start) return null - var recurrence = property(rule.component, "RRULE") - if (!recurrence) { - return start.year === year ? naiveKey(start) : null - } - - var parts = parseRuleParts(recurrence.value) +// — dated transitions, one for a fixed-offset zone, a list for a history. +function transitionsIn(rule, year) { + if (!rule.start) return [] + if (!rule.parts) return rule.dates[year] || [] + var at = recurringTransition(rule, year) + return at === null ? [] : [at] +} + +function recurringTransition(rule, year) { + var start = rule.start + var parts = rule.parts + var until = rule.until if (String(parts.FREQ || "").toUpperCase() !== "YEARLY") return null + if (until && year > until.year + 1) return null var month = Math.floor(Number(parts.BYMONTH) || start.month) if (month < 1 || month > 12) return null @@ -414,7 +494,63 @@ function transitionIn(rule, year) { } if (day < 1 || day > daysInMonth(year, month)) return null - return Date.UTC(year, month - 1, day, start.hour, start.minute, start.second) + var at = Date.UTC(year, month - 1, day, start.hour, start.minute, start.second) + // A rule stops at its UNTIL. Zones keep their retired rules — iCloud ships + // Vienna's 1981–1995 late-September fall-back to this day — and without + // this one of those wins every autumn. UNTIL is normally UTC while `at` is + // the wall clock the rule left behind, so it is moved to UTC by the offset + // in force before the transition; compared naively, the last transition a + // zone east of UTC made under the rule would fall after its own UNTIL. A + // bare date includes its whole day. + if (until) { + var reading = until.utc && rule.from !== null ? at - rule.from * 60000 : at + var limit = naiveKey(until) + (until.dateOnly ? 86400000 - 1 : 0) + if (reading > limit) return null + } + return at +} + +// The transitions a reading in `year` has to choose between. Every one from +// an earlier year is behind the reading, so of those only the latest is kept; +// the year's own follow it. Listed histories use the nearest occupied past +// year. Recurring rules use the year before and, for a zone with a history, +// their final year and the ones either side — UNTIL +// is UTC, so the last transition can sit in the year before or after the one +// UNTIL names. Later years cannot be behind a reading in this one. +function transitionsAround(plan, year) { + if (plan.byYear[year]) return plan.byYear[year] + var latest = null + var within = [] + for (var r = 0; r < plan.rules.length; r++) { + var rule = plan.rules[r] + var years = [year - 1] + // Listed histories can have decades between dates and more dates still + // ahead. Find the latest occupied year before this one, not merely the + // previous year or the final year in the list. The parsed year index is + // sorted once; repeated recurrence lookups need only a binary search. + if (plan.history && !rule.parts) { + var low = 0 + var high = rule.dateYears.length + while (low < high) { + var middle = Math.floor((low + high) / 2) + if (rule.dateYears[middle] < year) low = middle + 1 + else high = middle + } + years = low > 0 ? [rule.dateYears[low - 1]] : [] + } + var last = plan.history ? rule.lastYear : null + if (rule.parts && last !== null && last < year - 1) years.push(last - 1, last, last + 1) + for (var y = 0; y < years.length; y++) { + var earlier = transitionsIn(rule, years[y]) + for (var e = 0; e < earlier.length; e++) { + if (latest === null || earlier[e] >= latest.at) latest = { at: earlier[e], offset: rule.offset } + } + } + var found = transitionsIn(rule, year) + for (var f = 0; f < found.length; f++) within.push({ at: found[f], offset: rule.offset }) + } + plan.byYear[year] = latest ? [latest].concat(within) : within + return plan.byYear[year] } // The offset a zone was on at a given wall-clock reading. @@ -427,32 +563,17 @@ function transitionIn(rule, year) { // no reading of that hour is more correct than another. function zoneOffsetMinutes(timezone, fields) { if (!timezone) return null - var rules = [] - // STANDARD first, and that ordering is load-bearing: a zone whose rules this - // cannot place — no RRULE and dates outside the years looked at, which is - // what an RDATE-driven Exchange zone looks like from here — falls back to - // rules[0], and being an hour behind in winter is a better wrong answer than - // being an hour ahead all year. - var kinds = ["STANDARD", "DAYLIGHT"] - for (var k = 0; k < kinds.length; k++) { - var found = childrenNamed(timezone, kinds[k]) - for (var i = 0; i < found.length; i++) { - var offset = parseOffsetMinutes(textOf(found[i], "TZOFFSETTO")) - if (offset === null) continue - rules.push({ component: found[i], offset: offset }) - } - } + var plan = zonePlan(timezone) + var rules = plan.rules if (rules.length === 0) return null if (rules.length === 1) return rules[0].offset var wanted = naiveKey(fields) var best = null - for (var year = fields.year - 1; year <= fields.year + 1; year++) { - for (var r = 0; r < rules.length; r++) { - var at = transitionIn(rules[r], year) - if (at === null || at > wanted) continue - if (best === null || at >= best.at) best = { at: at, offset: rules[r].offset } - } + var candidates = transitionsAround(plan, fields.year) + for (var c = 0; c < candidates.length; c++) { + if (candidates[c].at > wanted) continue + if (best === null || candidates[c].at >= best.at) best = candidates[c] } if (best) return best.offset @@ -460,12 +581,10 @@ function zoneOffsetMinutes(timezone, fields) { // the one the earliest transition moved away from. var earliest = null for (var s = 0; s < rules.length; s++) { - var opening = transitionIn(rules[s], fields.year - 1) - if (opening === null) continue - if (earliest === null || opening < earliest.at) { - earliest = { - at: opening, - offset: parseOffsetMinutes(textOf(rules[s].component, "TZOFFSETFROM")) + var openings = transitionsIn(rules[s], fields.year - 1) + for (var o = 0; o < openings.length; o++) { + if (earliest === null || openings[o] < earliest.at) { + earliest = { at: openings[o], offset: rules[s].from } } } } diff --git a/ui/tests/test_calendar.js b/ui/tests/test_calendar.js index 2730272..f41bff3 100644 --- a/ui/tests/test_calendar.js +++ b/ui/tests/test_calendar.js @@ -247,6 +247,163 @@ assert.strictEqual(calendar.formatWhen(twoDays), assert.strictEqual(fixed.start.resolved, true) } +// iCloud ships a zone's whole history, retired rules included. Vienna fell +// back in late September from 1981 to 1995; that rule's UNTIL has to end it, +// or every autumn reads as standard time a month early and an event made on +// an iPhone shows an hour late from late September to the end of October. +{ + const vienna = [ + "BEGIN:VTIMEZONE", "TZID:Europe/Vienna", + "BEGIN:STANDARD", "DTSTART:18930401T000000", + "TZOFFSETFROM:+010521", "TZOFFSETTO:+0100", "END:STANDARD", + "BEGIN:STANDARD", "DTSTART:19170917T030000", + "RRULE:FREQ=YEARLY;UNTIL=19180916T010000Z;BYMONTH=9;BYDAY=3MO", + "TZOFFSETFROM:+0200", "TZOFFSETTO:+0100", "END:STANDARD", + "BEGIN:STANDARD", "DTSTART:19461006T030000", + "RRULE:FREQ=YEARLY;UNTIL=19481003T010000Z;BYMONTH=10;BYDAY=1SU", + "TZOFFSETFROM:+0200", "TZOFFSETTO:+0100", "END:STANDARD", + "BEGIN:DAYLIGHT", "DTSTART:19810329T020000", + "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU", + "TZOFFSETFROM:+0100", "TZOFFSETTO:+0200", "END:DAYLIGHT", + "BEGIN:STANDARD", "DTSTART:19810927T030000", + "RRULE:FREQ=YEARLY;UNTIL=19950924T010000Z;BYMONTH=9;BYDAY=-1SU", + "TZOFFSETFROM:+0200", "TZOFFSETTO:+0100", "END:STANDARD", + "BEGIN:STANDARD", "DTSTART:19961027T030000", + "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU", + "TZOFFSETFROM:+0200", "TZOFFSETTO:+0100", "END:STANDARD", + "END:VTIMEZONE" + ] + const at = (local) => calendar.invitationFrom([ + "BEGIN:VCALENDAR", "VERSION:2.0", "METHOD:REQUEST"].concat(vienna, [ + "BEGIN:VEVENT", "UID:u8", "SUMMARY:Appointment", + "DTSTART;TZID=Europe/Vienna:" + local, + "END:VEVENT", "END:VCALENDAR"]).join("\r\n")).start + assert.strictEqual(at("20261007T153000").ms, Date.UTC(2026, 9, 7, 13, 30, 0), + "early October is still summer time; the retired September rule has ended") + assert.strictEqual(at("20261024T120000").ms, Date.UTC(2026, 9, 24, 10, 0, 0)) + assert.strictEqual(at("20261026T120000").ms, Date.UTC(2026, 9, 26, 11, 0, 0), + "the rule still in force ends summer time on the last Sunday of October") + assert.strictEqual(at("20260701T120000").ms, Date.UTC(2026, 6, 1, 10, 0, 0)) + assert.strictEqual(at("20260115T120000").ms, Date.UTC(2026, 0, 15, 11, 0, 0)) + // UNTIL=19950924T010000Z is the 03:00 summer-time transition itself. Read + // as a wall clock it would land after its own UNTIL and drop out, leaving + // October 1995 on summer time. + assert.strictEqual(at("19951010T120000").ms, Date.UTC(1995, 9, 10, 11, 0, 0), + "a rule's last transition is the one its UTC UNTIL names") +} + +// Once every summer-time rule has ended, the zone stays where the last one +// left it. Mexico City dropped summer time in 2022; years later it is on +// -0600 all year, not on the 1922 entry that heads the list. +{ + const mexico = [ + "BEGIN:VTIMEZONE", "TZID:America/Mexico_City", + "BEGIN:STANDARD", "DTSTART:19220101T000000", + "TZOFFSETFROM:-063636", "TZOFFSETTO:-0700", "END:STANDARD", + "BEGIN:DAYLIGHT", "DTSTART:20020407T020000", + "RRULE:FREQ=YEARLY;UNTIL=20220403T080000Z;BYMONTH=4;BYDAY=1SU", + "TZOFFSETFROM:-0600", "TZOFFSETTO:-0500", "END:DAYLIGHT", + "BEGIN:STANDARD", "DTSTART:20021027T020000", + "RRULE:FREQ=YEARLY;UNTIL=20221030T070000Z;BYMONTH=10;BYDAY=-1SU", + "TZOFFSETFROM:-0500", "TZOFFSETTO:-0600", "END:STANDARD", + "END:VTIMEZONE" + ] + const at = (local) => calendar.invitationFrom([ + "BEGIN:VCALENDAR", "VERSION:2.0", "METHOD:REQUEST"].concat(mexico, [ + "BEGIN:VEVENT", "UID:u9", "SUMMARY:Appointment", + "DTSTART;TZID=America/Mexico_City:" + local, + "END:VEVENT", "END:VCALENDAR"]).join("\r\n")).start + assert.strictEqual(at("20260701T120000").ms, Date.UTC(2026, 6, 1, 18, 0, 0), + "no summer time after the rules ended") + assert.strictEqual(at("20260115T120000").ms, Date.UTC(2026, 0, 15, 18, 0, 0), + "the last transition's offset, not the zone's first entry") + assert.strictEqual(at("20220701T120000").ms, Date.UTC(2022, 6, 1, 17, 0, 0), + "the final summer under the rules still counts") +} + +// iCloud gathers a zone's one-off transitions into one sub-component per +// offset pair, so the last change can be an RDATE decades after its DTSTART. +// Moscow went to +0400 for good in 2011 and back to +0300 in 2014 — the +// second only an RDATE of a component that starts in 1930. +{ + const moscow = [ + "BEGIN:VTIMEZONE", "TZID:Europe/Moscow", + "BEGIN:DAYLIGHT", "DTSTART:19960331T020000", + "RRULE:FREQ=YEARLY;UNTIL=20100328T000000Z;BYMONTH=3;BYDAY=-1SU", + "TZOFFSETFROM:+0300", "TZOFFSETTO:+0400", "END:DAYLIGHT", + "BEGIN:STANDARD", "DTSTART:19961027T030000", + "RRULE:FREQ=YEARLY;UNTIL=20101031T000000Z;BYMONTH=10;BYDAY=-1SU", + "TZOFFSETFROM:+0400", "TZOFFSETTO:+0300", "END:STANDARD", + "BEGIN:STANDARD", "DTSTART:20110327T020000", + "TZOFFSETFROM:+0300", "TZOFFSETTO:+0400", "END:STANDARD", + "BEGIN:STANDARD", "DTSTART:19300621T000000", + "RDATE:19910929T030000,20141026T020000", + "TZOFFSETFROM:+0400", "TZOFFSETTO:+0300", "END:STANDARD", + "END:VTIMEZONE" + ] + const at = (local) => calendar.invitationFrom([ + "BEGIN:VCALENDAR", "VERSION:2.0", "METHOD:REQUEST"].concat(moscow, [ + "BEGIN:VEVENT", "UID:u10", "SUMMARY:Appointment", + "DTSTART;TZID=Europe/Moscow:" + local, + "END:VEVENT", "END:VCALENDAR"]).join("\r\n")).start + assert.strictEqual(at("20260701T120000").ms, Date.UTC(2026, 6, 1, 9, 0, 0), + "the 2014 return to +0300 is read from its RDATE") + assert.strictEqual(at("20130115T120000").ms, Date.UTC(2013, 0, 15, 8, 0, 0), + "between the two, +0400 all year") +} + +// One rule can take effect twice in a year. Casablanca leaves +0100 for +// Ramadan, and in 2029 Ramadan starts in January and again in December, so +// the January date has to count although the December one is still ahead. +{ + const casablanca = [ + "BEGIN:VTIMEZONE", "TZID:Africa/Casablanca", + "BEGIN:STANDARD", "DTSTART:20280123T030000", + "RDATE:20290114T030000,20291230T030000", + "TZOFFSETFROM:+0100", "TZOFFSETTO:+0000", "END:STANDARD", + "BEGIN:DAYLIGHT", "DTSTART:20280305T020000", + "RDATE:20290218T020000", + "TZOFFSETFROM:+0000", "TZOFFSETTO:+0100", "END:DAYLIGHT", + "END:VTIMEZONE" + ] + const at = (local) => calendar.invitationFrom([ + "BEGIN:VCALENDAR", "VERSION:2.0", "METHOD:REQUEST"].concat(casablanca, [ + "BEGIN:VEVENT", "UID:u11", "SUMMARY:Appointment", + "DTSTART;TZID=Africa/Casablanca:" + local, + "END:VEVENT", "END:VCALENDAR"]).join("\r\n")).start + assert.strictEqual(at("20290120T120000").ms, Date.UTC(2029, 0, 20, 12, 0, 0), + "the January start of Ramadan counts though December's is later") + assert.strictEqual(at("20290601T120000").ms, Date.UTC(2029, 5, 1, 11, 0, 0)) + assert.strictEqual(at("20291231T120000").ms, Date.UTC(2029, 11, 31, 12, 0, 0)) +} + +// Sparse RDATE histories must keep the nearest past transition even when a +// later listed date lies ahead of the reading and the previous year is empty. +{ + const sparse = [ + "BEGIN:VTIMEZONE", "TZID:Custom/Sparse", + "BEGIN:STANDARD", "DTSTART:19900101T030000", + "RDATE:20180101T030000", + "TZOFFSETFROM:+0200", "TZOFFSETTO:+0100", "END:STANDARD", + "BEGIN:DAYLIGHT", "DTSTART:20000101T020000", + "RDATE:20400101T020000,20200101T020000", + "TZOFFSETFROM:+0100", "TZOFFSETTO:+0200", "END:DAYLIGHT", + "END:VTIMEZONE" + ] + const at = (local) => calendar.invitationFrom([ + "BEGIN:VCALENDAR", "VERSION:2.0", "METHOD:REQUEST"].concat(sparse, [ + "BEGIN:VEVENT", "UID:sparse", "SUMMARY:Appointment", + "DTSTART;TZID=Custom/Sparse:" + local, + "END:VEVENT", "END:VCALENDAR"]).join("\r\n")).start + assert.strictEqual(at("20150115T120000").ms, Date.UTC(2015, 0, 15, 10), + "a future RDATE must not hide the component's earlier DTSTART") + assert.strictEqual(at("20260115T120000").ms, Date.UTC(2026, 0, 15, 10), + "the nearest past RDATE remains in force across empty years") + assert.strictEqual(at("20200101T015959").ms, Date.UTC(2020, 0, 1, 0, 59, 59)) + assert.strictEqual(at("20200101T020000").ms, Date.UTC(2020, 0, 1, 0), + "the current year's transition still takes effect at its wall-clock boundary") +} + // ------------------------------------------------------------- formatting assert.strictEqual(calendar.formatWhen(invite), From f94e897fd7501ea861f090cd653b98afe817e924 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 11 Oct 2026 15:32:14 +0800 Subject: [PATCH 09/88] Fix Outlook Graph consent for AADSTS70000 (#316) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mise ~/.config/mise/config.toml tools: gh@2.102.0 mise ~/.config/mise/config.toml tools: gh@2.102.0 Fixes #306. A consumer Outlook grant can still refresh mail while Microsoft refuses its first Graph exchange with AADSTS70000. Treat that specific Graph refusal as a candidate for missing consent, then probe the exact same refresh token for the mail resource under the existing per-account rotation lock. Only a successful, scoped mail response becomes `auth_consent_required`; genuinely invalid grants, temporary failures and malformed responses retain their distinct outcomes. Keep a refresh-token rotation returned by the probe. Both exchanges share a 20-second network budget. Accept Microsoft’s exact short Graph scope names after consent, without allowing mail/Graph scope aliases to cross resources. Decode Outlook RPC errors using their `{code, message}` envelope. This small adapter correction overlaps the Outlook portion of #284; the unrelated Gmail changes and open review findings in that PR are not included. Calendar discovery and native calendar requests now report missing Graph consent to the original mailbox's auth object. The callback is bound to its account, client, session, tenant and Graph grant generation, so an old response cannot change a replacement session or undo a completed consent flow. The setup page can then offer the existing **Allow Microsoft Graph...** button. Its existing second, Graph-only device-code flow retains the returned refresh token; no combined mail/Graph request is introduced. Outlook grant replacement and logout use the existing account-bound `auth.store`/`auth.clear` methods, which serialize with native refresh rotation. Once a credential operation owns that lane, it keeps the lock until any in-progress native write finishes, even if an outer calendar deadline cancels its caller. No mailbox operation is detached. ## Verification - Rust 1.98.1: all 650 unit/integration checks pass (576 unit tests and 74 integration tests; 11 explicit native/environment tests remain ignored by the standard command). The 14 focused credential checks pass, including same-refresh-token mail proof, genuine revocation, transient/malformed responses, token rotation, Graph-only short scopes, the shared network deadline, concurrent Graph-grant replacement, and outer cancellation during a blocked native write. The 70000 classification, RPC/calendar routing, serialized-store routing, short-scope, network-deadline and outer-cancellation reproductions were observed failing before their respective fixes. - Qt 6.11.2: all 1,092 main QML checks pass. The new 17-check suite exercises real OutlookAuth/CalendarController/setup components through synthetic RPC responses: calendar listing/discovery, consent-button visibility, the second Graph code, refresh-token storage, other errors and stale account/client/tenant/grant responses. - The additional native Qt checks pass: 6 Outlook HTTP, 64 sidebar, and the HTML resource-isolation test. `make test-js test-shell-portable`, full `make qml-check` (with environment import warnings), and whitespace checks pass. - `make validate` reaches the unchanged `tests/test_imap_ordering.sh` mismatch on this Debian host’s libcurl 8.14.1; pristine main fails the same fixture. The remaining standalone host gate cannot start because CMake is absent. Full manual Omarchy/standalone-host QA is not claimed. - Strict all-target Clippy reports five pre-existing `collapsible_if` / `needless_return` findings in untouched calendar, credential-store, IMAP and mail-test code. All-target Clippy passes with only those two pre-existing lint classes allowed. - Fresh independent review found the short-scope, store-interleaving and cancellation issues; the final delta has no remaining actionable findings. Security: **PASS**, scoped to the affected auth and credential-persistence boundaries. Provider and credential-store fixtures are synthetic. No real Microsoft account or native user keyring was accessed. Fixed credential destinations, TLS/redirect policy and separate resource scopes remain unchanged. This is not a live-provider interoperability test or a broader authentication audit. **Backend delivery:** public methods, response shapes and the existing `auth_consent_required` meaning are unchanged, so no new API gate is required. The installed backend pin remains 0.10.7. The native 70000/rotation fixes require a subsequent backend release; this PR does not publish or replace release assets. ## UI evidence Comparable synthetic production-component captures with GitHub-hosted attachments are included below, at 640×740 per side and scale 1 on Qt 6.11.2. Before, the signed-in mailbox has no Graph consent action. After, **Allow Microsoft Graph...** appears and starts the separate Graph code while mail remains signed in. The code and failure states are also covered. Dark uses the active Osaka Jade palette; light is a labeled fixed test palette. Full manual native-host and live Microsoft-account QA are not claimed. ## Release Notes - Offer Microsoft Graph consent for personal Outlook accounts whose mail sign-in still works, and keep the refresh token returned by the separate Graph sign-in. ## Maintainer validation on current head Merged current main `b0504b9` into this branch as `36cd1bf`, preserving the accepted Outlook repair. The merge required no new conflict resolutions or behavior changes. On Rust 1.98.1 / Qt 6.11.2, 20 credential regressions and 17 real Outlook-component QML checks pass; these include the empty-tenant/default-consumers rotation lane, dual-clock token cache expiry, revoked/malformed grants, and cancellation during native credential persistence. JavaScript and QML lint pass after integration; existing environment import warnings remain non-fatal. Synthetic fixtures were used throughout. Security: PASS for the affected Outlook auth and credential-persistence boundaries. Full local `make validate` remains unperformed on this integrated head because existing test scripts create temporary files under `/tmp`, which the current task instructions prohibit; no `TMPDIR` override was used. Required remote checks for this newly pushed SHA are pending and must pass before merge. ## Maintainer supplemental screenshot evidence Each comparison shows BEFORE on the left and AFTER on the right, 640×740 each side, scale 1, Qt 6.11.2. Production OutlookAuth/OutlookSetupPage use unmodified standalone controls; only transport responses, address, client, tokens and device code are synthetic. Dark uses the active Osaka Jade palette; light is an explicitly fixed test palette rather than the active desktop. Ordinary signed-in mailbox UI remains unchanged. After a Graph consent refusal, the account remains signed in and now offers Allow Microsoft Graph...; the separate Graph-only device-code flow shows a browser link and Cancel. A device-flow error preserves mail sign-in, explains the refusal and permits retry. All four states are covered in both palettes. Source is reviewed head `36cd1bf` versus main `b0504b9`. **DARK CONSENT BUTTON** ![dark-consent-button](https://github.com/user-attachments/assets/6d03caf8-1df4-4cab-a62b-459e92887faf) **DARK DEVICE CODE** ![dark-device-code](https://github.com/user-attachments/assets/6a57ea59-095d-4154-b8ca-9da802670d5d) **DARK DEVICE ERROR** ![dark-device-error](https://github.com/user-attachments/assets/77058458-58c2-45d7-9f43-7ec20124d962) **DARK SIGNED IN** ![dark-signed-in](https://github.com/user-attachments/assets/d962f58e-c0f3-4608-b3d5-06fee5df6055) **LIGHT CONSENT BUTTON** ![light-consent-button](https://github.com/user-attachments/assets/fceb87b4-dbcf-4d82-bb72-354139958c60) **LIGHT DEVICE CODE** ![light-device-code](https://github.com/user-attachments/assets/011e51dd-248d-4c45-88b5-6f8495c3199a) **LIGHT DEVICE ERROR** ![light-device-error](https://github.com/user-attachments/assets/984fc42e-d74c-41b9-81b4-c4bbff72f552) **LIGHT SIGNED IN** ![light-signed-in](https://github.com/user-attachments/assets/d1a36180-6e19-4173-a714-5206b1866803) Co-authored-by: Codex --- src/auth/credentials.rs | 737 +++++++++++++++++-- ui/calendar/CalendarController.qml | 15 +- ui/providers/OutlookAuth.qml | 38 +- ui/tests/qml/tst_outlook_backend_consent.qml | 254 +++++++ 4 files changed, 977 insertions(+), 67 deletions(-) create mode 100644 ui/tests/qml/tst_outlook_backend_consent.qml diff --git a/src/auth/credentials.rs b/src/auth/credentials.rs index f80f2da..4c308a5 100644 --- a/src/auth/credentials.rs +++ b/src/auth/credentials.rs @@ -185,7 +185,7 @@ async fn access_token_with( return Err("auth_provider_invalid"); } valid_account(provider, account)?; - let scope = scope(resource)?; + scope(resource)?; let owned = account.to_owned(); let entry = tokio::task::spawn_blocking(move || { if read_only { @@ -203,34 +203,120 @@ async fn access_token_with( let tenant = stored_tenant(&entry); let url = destination(&json!({"provider":"outlook", "endpoint":"token", "tenant":tenant}))?; let lock = account_tokens(account, client_id, tenant).await?; - // One refresh at a time per mailbox, shared by mail and Graph resources: - // rotating a refresh token must not race another resource's exchange. - let mut tokens = lock.lock().await; - if let Some(token) = cached_token( - &tokens, - resource, - std::time::Instant::now(), - std::time::SystemTime::now(), - ) { - return Ok(token); - } - let key = outlook_key(client_id, account); - let refresh_secret = store::get(key.clone()).await.map_err(store_error)?; - let refresh = text_secret(&refresh_secret)?; - let reply = post( - client()?, - &url, - callback::form(&[ + let account = account.to_owned(); + let client_id = client_id.to_owned(); + let resource = resource.to_owned(); + in_outlook_lane(lock, move |mut tokens| async move { + // One refresh at a time per mailbox, shared by mail and Graph resources: + // rotating a refresh token must not race another resource's exchange. + if let Some(token) = cached_token( + &tokens, + &resource, + std::time::Instant::now(), + std::time::SystemTime::now(), + ) { + return Ok(token); + } + let key = outlook_key(&client_id, &account); + let refresh_secret = store::get(key.clone()).await.map_err(store_error)?; + let refresh = text_secret(&refresh_secret)?; + let http = client()?; + let (access, lifetime) = refresh_outlook_with( + &client_id, + refresh, + &resource, + |body| post(http, &url, body), + |token| { + let key = key.clone(); + async move { persist_outlook_rotation(refresh, &token, key, read_only).await } + }, + ) + .await?; + tokens.insert( + resource.to_owned(), + ( + access.to_owned(), + TokenExpiry::after( + Duration::from_secs(lifetime), + std::time::Instant::now(), + std::time::SystemTime::now(), + ), + ), + ); + Ok(access.to_owned()) + }) + .await +} + +// The account lock remains held across every exchange and refresh-token store. +// Inject only the transport and store so provider fixtures never touch a keyring. +async fn refresh_outlook_with( + client_id: &str, + refresh: &str, + resource: &str, + mut request: Request, + mut persist: Persist, +) -> Result<(String, u64), &'static str> +where + Request: FnMut(String) -> RequestFuture, + RequestFuture: std::future::Future>, + Persist: FnMut(Value) -> PersistFuture, + PersistFuture: std::future::Future>, +{ + let scope = scope(resource)?; + // Both network exchanges share one budget below the UI's RPC timeout. + // Do not time out a keyring write: its blocking native operation must + // finish while the caller still owns the account rotation lock. + let deadline = tokio::time::Instant::now() + Duration::from_secs(20); + let reply = tokio::time::timeout_at( + deadline, + request(callback::form(&[ ("client_id", client_id), ("grant_type", "refresh_token"), ("refresh_token", refresh), ("scope", scope), - ]), + ])), ) - .await?; + .await + .map_err(|_| "auth_timeout")??; let token: Value = serde_json::from_str(reply["body"].as_str().ok_or("auth_invalid_response")?) .map_err(|_| "auth_invalid_response")?; if reply["status"] != 200 { + // Consumers can refuse the first Graph grant with 70000 while this + // refresh token still works for mail. A cached mail access token is + // not evidence: verify this exact grant, under the same rotation lock. + if resource == "graph" && token["error"] == "invalid_grant" && aadsts_code(&token, 70000) { + let mail_scope = self::scope("mail")?; + let mail_reply = tokio::time::timeout_at( + deadline, + request(callback::form(&[ + ("client_id", client_id), + ("grant_type", "refresh_token"), + ("refresh_token", refresh), + ("scope", mail_scope), + ])), + ) + .await + .map_err(|_| "auth_timeout")??; + let mail: Value = + serde_json::from_str(mail_reply["body"].as_str().ok_or("auth_invalid_response")?) + .map_err(|_| "auth_invalid_response")?; + if mail_reply["status"] != 200 { + return Err(if mail["error"] == "invalid_grant" { + "auth_signed_out" + } else { + "auth_refresh_failed" + }); + } + outlook_access(&mail)?; + // Even a scope refusal may rotate the grant. Keep the successful + // mail probe's rotation before reporting missing Graph consent. + persist(mail.clone()).await?; + if !outlook_scopes(&mail, mail_scope) { + return Err("auth_refresh_failed"); + } + return Err("auth_consent_required"); + } if token["error_codes"] .as_array() .is_some_and(|codes| codes.iter().any(|v| *v == 65001)) @@ -245,39 +331,70 @@ async fn access_token_with( } return Err("auth_refresh_failed"); } - let access = token["access_token"] + let access = outlook_access(&token)?; + persist(token.clone()).await?; + if !outlook_scopes(&token, scope) { + return Err("auth_consent_required"); + } + let lifetime = token["expires_in"] + .as_u64() + .filter(|n| *n <= 86400) + .unwrap_or(0); + Ok((access.to_owned(), lifetime)) +} + +fn aadsts_code(token: &Value, code: u64) -> bool { + token["error_codes"] + .as_array() + .is_some_and(|codes| codes.iter().any(|value| value.as_u64() == Some(code))) + || token["error_description"] + .as_str() + .is_some_and(|description| { + description + .split_whitespace() + .any(|word| word == format!("AADSTS{code}:")) + }) +} + +fn outlook_access(token: &Value) -> Result<&str, &'static str> { + token["access_token"] .as_str() .filter(|s| { !s.is_empty() && s.len() <= 16384 && !s.chars().any(|c| c.is_whitespace() || c.is_control()) }) - .ok_or("auth_invalid_response")?; - persist_outlook_rotation(refresh, &token, key, read_only).await?; + .ok_or("auth_invalid_response") +} + +fn outlook_scopes(token: &Value, scope: &str) -> bool { let granted = token["scope"].as_str().unwrap_or(""); - if scope + scope .split_whitespace() .filter(|s| s.starts_with("https://")) - .any(|required| !granted.split_whitespace().any(|s| s == required)) - { - return Err("auth_consent_required"); - } - let lifetime = token["expires_in"] - .as_u64() - .filter(|n| *n <= 86400) - .unwrap_or(0); - tokens.insert( - resource.to_owned(), - ( - access.to_owned(), - TokenExpiry::after( - Duration::from_secs(lifetime), - std::time::Instant::now(), - std::time::SystemTime::now(), - ), - ), - ); - Ok(access.to_owned()) + .all(|required| { + granted.split_whitespace().any(|s| { + s == required + || required + .strip_prefix("https://graph.microsoft.com/") + .is_some_and(|short| s == short) + }) + }) +} + +async fn in_outlook_lane(lane: AccountTokens, operation: F) -> Result +where + T: Send + 'static, + F: FnOnce(tokio::sync::OwnedMutexGuard) -> Fut + Send + 'static, + Fut: std::future::Future> + Send + 'static, +{ + // Waiting is cancellable. Once admitted, finish under the owned guard: + // native keyring writes use spawn_blocking and cannot be cancelled when + // a calendar caller's outer deadline drops its authentication future. + let guard = lane.lock_owned().await; + tokio::spawn(operation(guard)) + .await + .map_err(|_| "auth_refresh_failed")? } async fn account_tokens( @@ -314,23 +431,26 @@ pub(super) async fn change_outlook(params: &Value, clear: bool) -> Result {} - Err(error) => return Err(store_error(error)), + let token = token.to_owned(); + in_outlook_lane(lock, move |mut tokens| async move { + if clear { + match store::delete(key).await { + Ok(()) | Err(StoreError::Missing) => {} + Err(error) => return Err(store_error(error)), + } + } else { + store::put( + key, + Secret::new(token.as_bytes().to_vec()).map_err(store_error)?, + ) + .await + .map_err(store_error)?; } - } else { - store::put( - key, - Secret::new(token.as_bytes().to_vec()).map_err(store_error)?, - ) - .await - .map_err(store_error)?; - } - tokens.clear(); - Ok(json!({"saved":!clear,"cleared":clear})) + tokens.clear(); + Ok(json!({"saved":!clear,"cleared":clear})) + }) + .await } /// Discard cached credentials without opening a second refresh lane. @@ -458,6 +578,503 @@ where #[cfg(test)] mod tests { use super::*; + fn reply(status: u16, token: Value) -> Value { + json!({"status": status, "body": token.to_string()}) + } + + fn mail_grant() -> Value { + json!({"access_token":"synthetic-mail", "expires_in":3600, + "refresh_token":"synthetic-rotated", "scope":scope("mail").unwrap()}) + } + + // This is Microsoft's consumer-account refusal, not proof the grant died. + fn unconsented_graph() -> Value { + json!({"error":"invalid_grant", "error_codes":[70000], + "error_description":"AADSTS70000: The request was denied because one or more scopes requested are unauthorized or expired. The user must first sign in and grant the client application access to the requested scope."}) + } + + #[tokio::test] + async fn graph_70000_probes_the_same_grant_for_mail_before_offering_consent() { + let requests = std::sync::Mutex::new(Vec::new()); + let rotations = std::sync::Mutex::new(Vec::new()); + let mut replies = std::collections::VecDeque::from([ + reply(400, unconsented_graph()), + reply(200, mail_grant()), + ]); + let answer = refresh_outlook_with( + "synthetic-client", + "synthetic-original", + "graph", + |body| { + requests.lock().unwrap().push(body); + let answer = replies.pop_front().expect("only one mail probe is allowed"); + async move { Ok(answer) } + }, + |token| { + rotations.lock().unwrap().push(token); + async { Ok(()) } + }, + ) + .await; + assert_eq!(answer, Err("auth_consent_required")); + let requests = requests.lock().unwrap(); + assert_eq!(requests.len(), 2); + let graph = &requests[0]; + let mail = &requests[1]; + for body in [graph, mail] { + assert!(body.contains("refresh_token=synthetic-original")); + assert!(body.contains("client_id=synthetic-client")); + } + assert!(graph.contains("graph.microsoft.com")); + assert!(!graph.contains("outlook.office.com")); + assert!(mail.contains("outlook.office.com")); + assert!(!mail.contains("graph.microsoft.com")); + assert_eq!( + rotations.lock().unwrap()[0]["refresh_token"], + "synthetic-rotated" + ); + } + + #[tokio::test] + async fn graph_70000_needs_a_successful_mail_probe_not_a_cached_session() { + let mut malformed = mail_grant(); + malformed["access_token"] = json!("synthetic\ninvalid"); + let mut partial = mail_grant(); + partial["scope"] = json!("https://outlook.office.com/SMTP.Send"); + for (probe, expected, stores) in [ + ( + Ok(reply( + 400, + json!({"error":"invalid_grant","error_codes":[70000]}), + )), + "auth_signed_out", + 0, + ), + ( + Ok(reply( + 400, + json!({"error":"invalid_grant","error_codes":[700082]}), + )), + "auth_signed_out", + 0, + ), + ( + Ok(reply(401, json!({"error":"invalid_client"}))), + "auth_refresh_failed", + 0, + ), + ( + Ok(reply(503, json!({"error":"temporarily_unavailable"}))), + "auth_refresh_failed", + 0, + ), + (Err("auth_timeout"), "auth_timeout", 0), + ( + Ok(json!({"status":200,"body":"not-json"})), + "auth_invalid_response", + 0, + ), + (Ok(reply(200, malformed)), "auth_invalid_response", 0), + (Ok(reply(200, partial)), "auth_refresh_failed", 1), + ] { + let mut replies = + std::collections::VecDeque::from([Ok(reply(400, unconsented_graph())), probe]); + let mut requests = 0; + let mut writes = 0; + let result = refresh_outlook_with( + "synthetic", + "original", + "graph", + |_| { + requests += 1; + let answer = replies.pop_front().expect("probe must not retry"); + async move { answer } + }, + |_| { + writes += 1; + async { Ok(()) } + }, + ) + .await; + assert_eq!(result, Err(expected)); + assert_eq!(requests, 2); + assert_eq!(writes, stores); + } + } + + #[tokio::test] + async fn only_graph_70000_adds_a_mail_probe() { + for (resource, response, expected) in [ + ("mail", unconsented_graph(), "auth_signed_out"), + ( + "graph", + json!({"error":"invalid_grant","error_codes":[700082]}), + "auth_signed_out", + ), + ( + "graph", + json!({"error":"invalid_client","error_codes":[70000]}), + "auth_refresh_failed", + ), + ( + "graph", + json!({"error":"temporarily_unavailable"}), + "auth_refresh_failed", + ), + ( + "graph", + json!({"error":"invalid_grant","error_codes":[65001]}), + "auth_consent_required", + ), + ( + "graph", + json!({"error":"invalid_grant","error_description":"AADSTS700000: not the same code"}), + "auth_signed_out", + ), + ] { + let mut requests = 0; + let result = refresh_outlook_with( + "synthetic", + "original", + resource, + |_| { + requests += 1; + assert_eq!(requests, 1, "unrelated refusals must not probe mail"); + let answer = reply(400, response.clone()); + async move { Ok(answer) } + }, + |_| async { panic!("a failed response cannot store a token") }, + ) + .await; + assert_eq!(result, Err(expected)); + } + } + + #[tokio::test] + async fn graph_70000_description_and_rotation_failure_are_preserved() { + let mut refusal = unconsented_graph(); + refusal.as_object_mut().unwrap().remove("error_codes"); + let mut replies = + std::collections::VecDeque::from([reply(400, refusal), reply(200, mail_grant())]); + let result = refresh_outlook_with( + "synthetic", + "original", + "graph", + |_| { + let answer = replies.pop_front().unwrap(); + async move { Ok(answer) } + }, + |_| async { Err("auth_keyring_failed") }, + ) + .await; + assert_eq!(result, Err("auth_keyring_failed")); + assert!(replies.is_empty()); + } + + #[tokio::test(start_paused = true)] + async fn graph_probe_shares_the_original_network_deadline() { + let started = tokio::time::Instant::now(); + let mut calls = 0; + let result = refresh_outlook_with( + "synthetic", + "original", + "graph", + |_| { + calls += 1; + let response = if calls == 1 { + reply(400, unconsented_graph()) + } else { + reply(200, mail_grant()) + }; + async move { + tokio::time::sleep(Duration::from_secs(12)).await; + Ok(response) + } + }, + |_| async { panic!("a timed-out probe must not rotate the grant") }, + ) + .await; + assert_eq!(result, Err("auth_timeout")); + assert_eq!(calls, 2); + assert!(started.elapsed() <= Duration::from_secs(20)); + } + + #[tokio::test] + async fn device_grant_store_waits_for_an_older_probe_rotation() { + if crate::mail::tests::isolated() { + return; + } + let account = "outlook:consent-race@example.org"; + let _fixture = crate::mail::tests::account_fixture(json!({"version":1, + "activeId":account,"accounts":[{"provider":"outlook","email":"consent-race@example.org", + "clientId":"synthetic-client","imap":{"tenant":""}}]})); + let key = outlook_key("synthetic-client", account); + struct RecordingStore { + key: CredentialKey, + writes: std::sync::Arc>>>, + } + impl store::CredentialStore for RecordingStore { + fn get(&self, key: &CredentialKey) -> Result { + assert_eq!(key, &self.key); + Secret::new(self.writes.lock().unwrap().last().unwrap().clone()) + } + fn put(&self, key: &CredentialKey, value: &[u8]) -> Result<(), StoreError> { + assert_eq!(key, &self.key); + self.writes.lock().unwrap().push(value.to_vec()); + Ok(()) + } + fn delete(&self, _: &CredentialKey) -> Result<(), StoreError> { + panic!("grant replacement cannot delete a credential") + } + } + let writes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); + let _store = store::tests::isolated_store(RecordingStore { + key: key.clone(), + writes: writes.clone(), + }); + let lane = account_tokens(account, "synthetic-client", "consumers") + .await + .unwrap(); + let mut probe = lane.lock().await; + probe.insert( + "graph".into(), + ( + "old-graph".into(), + TokenExpiry::after( + Duration::from_secs(3600), + std::time::Instant::now(), + std::time::SystemTime::now(), + ), + ), + ); + let mut device = tokio::spawn(async move { + Session::default() + .call( + "auth.store", + &json!({"accountId":account, + "clientId":"synthetic-client","token":"graph-device-refresh"}), + ) + .await + }); + assert!( + tokio::time::timeout(Duration::from_millis(30), &mut device) + .await + .is_err(), + "device store must wait for the older refresh lane" + ); + assert!(writes.lock().unwrap().is_empty()); + persist_outlook_rotation( + "original", + &json!({"refresh_token":"mail-probe-rotation"}), + key.clone(), + false, + ) + .await + .unwrap(); + drop(probe); + assert_eq!(device.await.unwrap().unwrap()["saved"], true); + assert_eq!( + *writes.lock().unwrap(), + vec![ + b"mail-probe-rotation".to_vec(), + b"graph-device-refresh".to_vec() + ] + ); + assert!( + lane.lock().await.is_empty(), + "device grant invalidates cached resource tokens" + ); + assert_eq!( + store::get(key).await.unwrap().as_slice(), + b"graph-device-refresh" + ); + } + + #[tokio::test] + async fn outer_cancellation_cannot_release_a_lane_during_native_rotation() { + if crate::mail::tests::isolated() { + return; + } + let account = "outlook:cancelled-probe@example.org"; + let _fixture = crate::mail::tests::account_fixture(json!({"version":1, + "activeId":account,"accounts":[{"provider":"outlook","email":"cancelled-probe@example.org", + "clientId":"synthetic-client","imap":{"tenant":"consumers"}}]})); + let key = outlook_key("synthetic-client", account); + let writes = std::sync::Arc::new(std::sync::Mutex::new(Vec::>::new())); + let release = + std::sync::Arc::new((std::sync::Mutex::new(false), std::sync::Condvar::new())); + let finished = std::sync::Arc::new(tokio::sync::Notify::new()); + let (started, started_rx) = tokio::sync::oneshot::channel(); + struct BlockedStore { + key: CredentialKey, + writes: std::sync::Arc>>>, + release: std::sync::Arc<(std::sync::Mutex, std::sync::Condvar)>, + started: std::sync::Mutex>>, + finished: std::sync::Arc, + } + impl store::CredentialStore for BlockedStore { + fn get(&self, _: &CredentialKey) -> Result { + panic!("no native get") + } + fn delete(&self, _: &CredentialKey) -> Result<(), StoreError> { + panic!("no native delete") + } + fn put(&self, key: &CredentialKey, value: &[u8]) -> Result<(), StoreError> { + assert_eq!(key, &self.key); + if value == b"mail-probe-rotation" { + self.started + .lock() + .unwrap() + .take() + .unwrap() + .send(()) + .unwrap(); + let (state, notify) = &*self.release; + let mut state = state.lock().unwrap(); + while !*state { + state = notify.wait(state).unwrap(); + } + } + self.writes.lock().unwrap().push(value.to_vec()); + if value == b"mail-probe-rotation" { + self.finished.notify_one(); + } + Ok(()) + } + } + let _store = store::tests::isolated_store(BlockedStore { + key: key.clone(), + writes: writes.clone(), + release: release.clone(), + started: std::sync::Mutex::new(Some(started)), + finished: finished.clone(), + }); + let lane = account_tokens(account, "synthetic-client", "consumers") + .await + .unwrap(); + let older = tokio::spawn(in_outlook_lane(lane, move |tokens| async move { + let result = persist_outlook_rotation( + "original", + &json!({"refresh_token":"mail-probe-rotation"}), + key, + false, + ) + .await; + drop(tokens); + result + })); + started_rx.await.unwrap(); + older.abort(); + assert!(older.await.unwrap_err().is_cancelled()); + let mut device = tokio::spawn(async move { + Session::default() + .call( + "auth.store", + &json!({"accountId":account, + "clientId":"synthetic-client","token":"graph-device-refresh"}), + ) + .await + }); + let early = tokio::time::timeout(Duration::from_millis(30), &mut device).await; + let stayed_locked = early.is_err(); + let (state, notify) = &*release; + *state.lock().unwrap() = true; + notify.notify_all(); + finished.notified().await; + let stored = match early { + Ok(result) => result, + Err(_) => device.await, + }; + assert_eq!(stored.unwrap().unwrap()["saved"], true); + assert!( + stayed_locked, + "caller cancellation must not release the native-write lane" + ); + assert_eq!( + *writes.lock().unwrap(), + vec![ + b"mail-probe-rotation".to_vec(), + b"graph-device-refresh".to_vec() + ] + ); + } + + #[tokio::test] + async fn graph_refresh_after_device_consent_accepts_short_resource_scopes() { + let mut calls = 0; + let result = refresh_outlook_with( + "synthetic", + "graph-device-refresh", + "graph", + |body| { + calls += 1; + assert!(body.contains("refresh_token=graph-device-refresh")); + let response = reply( + 200, + json!({"access_token":"graph-token", "expires_in":3600, + "refresh_token":"graph-rotated", "scope":"Mail.Send Calendars.ReadWrite"}), + ); + async move { Ok(response) } + }, + |token| { + assert_eq!(token["refresh_token"], "graph-rotated"); + async { Ok(()) } + }, + ) + .await; + assert_eq!(calls, 1); + assert_eq!(result, Ok(("graph-token".into(), 3600))); + } + + #[test] + fn scope_abbreviations_do_not_cross_resource_boundaries() { + for granted in [ + "", + "Mail.Send", + "Calendars.ReadWrite", + "https://outlook.office.com/Mail.Send Calendars.ReadWrite", + "https://other.example/Mail.Send Calendars.ReadWrite", + ] { + assert!(!outlook_scopes( + &json!({"scope": granted}), + scope("graph").unwrap() + )); + } + assert!(!outlook_scopes( + &json!({"scope":"IMAP.AccessAsUser.All SMTP.Send"}), + scope("mail").unwrap() + )); + assert!(!outlook_scopes( + &json!({"scope":"Mail.Send Calendars.ReadWrite"}), + scope("mail").unwrap() + )); + } + + #[tokio::test] + async fn successful_graph_refresh_never_requests_mail() { + let mut calls = 0; + let result = refresh_outlook_with( + "synthetic", + "original", + "graph", + |_| { + calls += 1; + let response = reply( + 200, + json!({"access_token":"graph-token", "expires_in":3600, + "refresh_token":"graph-rotated", "scope":scope("graph").unwrap()}), + ); + async move { Ok(response) } + }, + |token| { + assert_eq!(token["refresh_token"], "graph-rotated"); + async { Ok(()) } + }, + ) + .await; + assert_eq!(calls, 1); + assert_eq!(result, Ok(("graph-token".into(), 3600))); + } #[test] fn stored_tenant_treats_a_missing_and_an_empty_field_alike() { assert_eq!(stored_tenant(&json!({"imap":{"tenant":""}})), "consumers"); diff --git a/ui/calendar/CalendarController.qml b/ui/calendar/CalendarController.qml index 535cf13..8c69ca8 100644 --- a/ui/calendar/CalendarController.qml +++ b/ui/calendar/CalendarController.qml @@ -4,6 +4,7 @@ import Quickshell.Io import qs.Commons import "Calendar.js" as Calendar import "Sources.js" as Sources +import "../providers/MicrosoftOAuth.js" as Microsoft Item { id: root @@ -215,6 +216,13 @@ Item { return count } + function microsoftConsentHandler(accountId) { + var host = accountId && service && typeof service.findAccount === "function" ? service.findAccount(accountId) : null + if (host && host.providerId === "outlook" && host.auth + && typeof host.auth.graphConsentHandler === "function") return host.auth.graphConsentHandler() + return function() { return false } + } + function discoveryFailure(error) { // JSON-RPC codes are numeric; the backend's stable reason is its message. // Match only known reasons and never display the raw diagnostic. @@ -244,13 +252,14 @@ Item { discoveringCalendars = true discoveringAccountId = wanted discoveryError = "" + var notifyConsent = microsoftConsentHandler(wanted) service.backend.call("calendar.discover", { accountId: wanted }, function(result, error) { if (serial !== root.discoverySerial || root.discoveringAccountId !== wanted) return root.discoveringCalendars = false root.discoveringAccountId = "" if (error || !result || String(result.accountId || "") !== wanted || !Array.isArray(result.calendars) || !root.discoverableAccount(wanted)) { - root.discoveryError = root.discoveryFailure(error) + root.discoveryError = notifyConsent(error) ? Microsoft.graphConsentMessage() : root.discoveryFailure(error) root.discoveryFinished(false, root.discoveryError, 0) return } @@ -580,10 +589,12 @@ Item { var params = fields || {} params.source = source params.operation = operation + var notifyConsent = microsoftConsentHandler(source && source.kind === "microsoft" ? source.accountId : "") service.backend.call("calendar.request", params, function(result, error) { var reason = "" if (error) { - reason = error.message === "calendar_conflict" ? "This event changed elsewhere. Refresh and try again." + reason = notifyConsent(error) ? Microsoft.graphConsentMessage() + : error.message === "calendar_conflict" ? "This event changed elsewhere. Refresh and try again." : Calendar.nativeRequestError(String(source && source.kind || "")) } callback(result, reason) diff --git a/ui/providers/OutlookAuth.qml b/ui/providers/OutlookAuth.qml index 0ddeb38..766fa27 100644 --- a/ui/providers/OutlookAuth.qml +++ b/ui/providers/OutlookAuth.qml @@ -167,6 +167,22 @@ Item { } } + // Native calendar work resolves its own tokens. Bind its consent result + // before it starts, so an old request cannot change a replacement session + // or undo the Graph device flow that completed while it was in flight. + function graphConsentHandler() { + var context = sessionContext() + var requestedTenant = tenant + var grants = graphGrants + return function(error) { + if (!root || !root.isCurrent(context) || !root.sessionEnabled + || root.tenant !== requestedTenant || root.graphGrants !== grants + || !error || error.message !== "auth_consent_required") return false + root.graphConsentNeeded = true + return true + } + } + function withGraphToken(callback) { if (typeof callback !== "function") return if (!sessionEnabled) { @@ -478,8 +494,9 @@ Item { keyringJob = next.shift() keyringJobs = next var job = keyringJob - if (!platform || (job.kind === "store" && typeof platform.credentialPut !== "function") - || (job.kind === "clear" && typeof platform.credentialDelete !== "function")) { + var nativeStore = backend && typeof backend.call === "function" + if (!nativeStore && (!platform || (job.kind === "store" && typeof platform.credentialPut !== "function") + || (job.kind === "clear" && typeof platform.credentialDelete !== "function"))) { job.token = "" keyringJob = null if (isCurrent(job.context)) lastError = "Could not update the saved Microsoft session" @@ -492,7 +509,17 @@ Item { if (!ok && root.isCurrent(job.context)) root.lastError = "Could not update the saved Microsoft session" root.runKeyringJob() } - if (job.kind === "store") + if (nativeStore) { + // Device grants and logout share the backend's account refresh lane. + // A mail/Graph refresh already in flight must finish its rotation before + // this newer grant replaces it, or the old grant can overwrite consent. + var fields = { accountId: job.context.accountId, clientId: job.context.clientId } + if (job.kind === "store") fields.token = job.token + backend.call(job.kind === "store" ? "auth.store" : "auth.clear", fields, function(result, error) { + done(!error && !!result && (job.kind === "store" ? result.saved === true : result.cleared === true), + error ? String(error.message || "") : "") + }) + } else if (job.kind === "store") platform.credentialPut("outlook-refresh-token", job.context.accountId, job.context.clientId, job.token, done) else @@ -529,8 +556,9 @@ Item { if (endpoint === "token" && body.indexOf("grant_type=refresh_token") >= 0 && accountId) { backend.call("auth.token", { provider: "outlook", accountId: accountId, resource: body.indexOf("graph.microsoft.com") >= 0 ? "graph" : "mail" }, function(result, error) { - var code = error === "auth_signed_out" ? "invalid_grant" : "temporarily_unavailable" - var failure = error === "auth_consent_required" + var reason = error ? String(error.message || "") : "" + var code = reason === "auth_signed_out" ? "invalid_grant" : "temporarily_unavailable" + var failure = reason === "auth_consent_required" ? { error: "interaction_required", error_codes: [65001] } : { error: code } finish({ status: error ? 400 : 200, body: JSON.stringify(error ? failure : result) }, "") }) diff --git a/ui/tests/qml/tst_outlook_backend_consent.qml b/ui/tests/qml/tst_outlook_backend_consent.qml new file mode 100644 index 0000000..6115bed --- /dev/null +++ b/ui/tests/qml/tst_outlook_backend_consent.qml @@ -0,0 +1,254 @@ +import QtQuick +import QtTest +import "../../providers" as Providers +import "../../providers/MicrosoftOAuth.js" as Microsoft +import "../../components" as Components +import "../../calendar" as Calendar + +// Exercise the real postForm RPC adapter. The provider fixture supplies the +// backend's {code, message} error envelope, not a raw Microsoft response. +Item { + QtObject { + id: fixture + property bool ready: true + property var calls: [] + property var writes: [] + property var deletes: [] + property string failure: "auth_consent_required" + property bool hold: false + property var pending: null + function call(method, params, callback) { + calls = calls.concat([{method: method, params: params}]) + if (method === "auth.store") { + fixture.credentialPut("outlook-refresh-token", params.accountId, params.clientId, params.token, function() {}) + callback({saved: true}, null) + return + } + if (method === "auth.clear") { + fixture.credentialDelete("outlook-refresh-token", params.accountId, params.clientId, function() {}) + callback({cleared: true}, null) + return + } + if (method === "calendar.request" || method === "calendar.discover") { + if (hold) { pending = callback; return } + callback(null, {code: -32000, message: failure}) + return + } + if (method === "auth.token") { + if (hold) { pending = callback; return } + if (params.resource === "graph") { + callback(null, {code: -32000, message: failure}) + return + } + callback({access_token: "mail-token", expires_in: 3600, scope: Microsoft.SCOPES.join(" ")}, null) + return + } + if (method === "auth.form" && params.endpoint === "device") { + callback({status: 200, body: JSON.stringify({device_code: "synthetic-device", + user_code: "SYNTHETIC", verification_uri: "https://microsoft.com/devicelogin", expires_in: 900, interval: 5})}, null) + return + } + if (method === "auth.form" && params.endpoint === "token") { + callback({status: 200, body: JSON.stringify({access_token: "graph-token", + refresh_token: "graph-refresh-token", expires_in: 3600, scope: Microsoft.GRAPH_SCOPES.join(" ")})}, null) + return + } + callback({}, null) + } + function credentialPut(kind, accountId, clientId, secret, callback) { + writes = writes.concat([{kind: kind, accountId: accountId, clientId: clientId, secret: secret}]) + callback(true, "") + } + function credentialDelete(kind, accountId, clientId, callback) { + deletes = deletes.concat([{kind: kind, accountId: accountId, clientId: clientId}]) + callback(true, "") + } + } + Component { + id: authFactory + Providers.OutlookAuth { + pluginDir: "/tmp/omamail-test" + accountId: "outlook:alice@example.test" + configuredEmail: "alice@example.test" + configuredClientId: "12345678-1234-4abc-9def-1234567890ab" + backend: fixture + platform: fixture + } + } + Component { + id: serviceFactory + QtObject { + property var auth + property var backend: fixture + property bool backendCanDiscoverCalendars: true + property var accountSummaries: [{id: "outlook:alice@example.test", calendarProvider: "microsoft", signedIn: true}] + function findAccount(accountId) { + return accountId === auth.accountId ? {providerId: "outlook", auth: auth} : null + } + property string accountAddress: "alice@example.test" + property bool backendCanCheckMicrosoftConnection: true + function cancelSignIn() { auth.cancelLogin() } + function configureCurrentAccountAndSignInOAuth(values) { auth.beginLogin() } + } + } + Component { + id: pageFactory + Components.OutlookSetupPage { + textColor: Qt.rgba(0, 0, 0, 1) + dimColor: Qt.rgba(0.5, 0.5, 0.5, 1) + dangerColor: Qt.rgba(1, 0, 0, 1) + accentColor: Qt.rgba(0, 0, 1, 1) + panelFontFamily: "Sans" + width: 500 + } + } + Component { + id: calendarFactory + Calendar.CalendarController { pluginDir: "/tmp/omamail-test" } + } + TestCase { + name: "OutlookBackendConsent" + function fresh() { + fixture.hold = false + fixture.pending = null + fixture.failure = "auth_consent_required" + var auth = createTemporaryObject(authFactory, parent) + wait(1) + auth.cancelLogin() + auth.accessToken = "mail-token" + auth.accessTokenExpiresAt = Date.now() + 3600000 + auth.loggedIn = true + auth.savedSessionPresent = true + fixture.calls = []; fixture.writes = []; fixture.deletes = [] + return auth + } + function test_backend_consent_offers_graph_code_and_keeps_its_refresh_token() { + var auth = fresh() + var service = createTemporaryObject(serviceFactory, parent, {auth: auth}) + var page = createTemporaryObject(pageFactory, parent, {service: service}) + var button = findChild(page, "outlook-sign-in") + compare(button.visible, false) + var error = "" + auth.withGraphToken(function(token, reason) { error = reason }) + compare(fixture.calls.length, 1) + compare(fixture.calls[0].method, "auth.token") + compare(fixture.calls[0].params.resource, "graph") + compare(auth.graphConsentNeeded, true) + compare(auth.loggedIn, true) + compare(auth.accessToken, "mail-token") + compare(fixture.deletes.length, 0) + verify(error.indexOf("Graph needs its own consent") >= 0, error) + compare(button.visible, true) + compare(button.text, "Allow Microsoft Graph...") + button.clicked() + compare(auth.devicePurpose, "graph") + compare(auth.graphRoundBusy, true) + var body = decodeURIComponent(fixture.calls[1].params.body) + verify(body.indexOf("graph.microsoft.com") >= 0) + verify(body.indexOf("offline_access") >= 0) + verify(body.indexOf("outlook.office.com") < 0) + auth.pollDeviceCode() + compare(auth.graphConsentNeeded, false) + compare(auth.graphRoundBusy, false) + compare(auth.graphAccessToken, "graph-token") + compare(auth.accessToken, "mail-token") + compare(auth.loggedIn, true) + compare(button.visible, false) + compare(fixture.writes.length, 1) + compare(fixture.writes[0].secret, "graph-refresh-token") + compare(fixture.writes[0].kind, "outlook-refresh-token") + compare(fixture.writes[0].accountId, auth.accountId) + compare(fixture.writes[0].clientId, auth.clientId) + compare(fixture.deletes.length, 0) + } + function test_graph_device_grant_uses_the_serialized_backend_store() { + var auth = fresh() + auth.acceptGraphSignIn({accessToken: "graph-token", refreshToken: "device-graph-refresh", + expiresIn: 3600, scope: Microsoft.GRAPH_SCOPES.join(" ")}) + compare(fixture.calls.length, 1) + compare(fixture.calls[0].method, "auth.store") + compare(fixture.calls[0].params.accountId, auth.accountId) + compare(fixture.calls[0].params.clientId, auth.clientId) + compare(fixture.calls[0].params.token, "device-graph-refresh") + compare(fixture.writes[0].secret, "device-graph-refresh") + } + function test_logout_uses_the_same_serialized_backend_lane() { + var auth = fresh() + auth.logout() + compare(fixture.calls[fixture.calls.length - 1].method, "auth.clear") + compare(fixture.deletes.length, 1) + } + function test_native_calendar_requests_offer_consent_data() { + return [{tag: "listing", operation: "list"}, {tag: "discovery", operation: "discover"}] + } + function test_native_calendar_requests_offer_consent(data) { + var auth = fresh() + var service = createTemporaryObject(serviceFactory, parent, {auth: auth}) + var controller = createTemporaryObject(calendarFactory, parent, {service: service}) + var error = "" + if (data.operation === "discover") { + verify(controller.discoverAccountCalendars(auth.accountId)) + error = controller.discoveryError + } else { + controller.nativeRequest({kind: "microsoft", accountId: auth.accountId}, "list", {}, + function(result, reason) { error = reason }) + } + compare(auth.graphConsentNeeded, true) + compare(auth.loggedIn, true) + compare(auth.accessToken, "mail-token") + compare(fixture.deletes.length, 0) + verify(error.indexOf("Allow Microsoft Graph") >= 0, error) + } + function test_native_calendar_late_consent_is_bound_to_the_session_data() { + return [{tag: "account", change: "account"}, {tag: "client", change: "client"}, + {tag: "tenant", change: "tenant"}, {tag: "logout", change: "logout"}, + {tag: "new grant", change: "grant"}] + } + function test_native_calendar_late_consent_is_bound_to_the_session(data) { + var auth = fresh() + var service = createTemporaryObject(serviceFactory, parent, {auth: auth}) + var controller = createTemporaryObject(calendarFactory, parent, {service: service}) + fixture.hold = true + controller.nativeRequest({kind: "microsoft", accountId: auth.accountId}, "list", {}, function() {}) + var pending = fixture.pending + if (data.change === "account") auth.accountId = "outlook:bob@example.test" + if (data.change === "client") auth.configuredClientId = "87654321-4321-4abc-9def-1234567890ab" + if (data.change === "tenant") auth.entrySettings = {tenant: "organizations"} + if (data.change === "logout") auth.logout() + if (data.change === "grant") auth.acceptGraphToken({accessToken: "new-graph", expiresIn: 3600}) + pending(null, {code: -32000, message: "auth_consent_required"}) + compare(auth.graphConsentNeeded, false) + } + function test_other_backend_refusals_do_not_offer_graph_consent_data() { + return [{tag: "revoked", reason: "auth_signed_out"}, + {tag: "transient", reason: "auth_refresh_failed"}, + {tag: "malformed", reason: "auth_invalid_response"}, + {tag: "keyring", reason: "auth_keyring_failed"}] + } + function test_other_backend_refusals_do_not_offer_graph_consent(data) { + var auth = fresh() + fixture.failure = data.reason + var error = "" + auth.withGraphToken(function(token, reason) { error = reason }) + compare(auth.graphConsentNeeded, false) + compare(auth.loggedIn, true) + compare(fixture.deletes.length, 0) + verify(error !== "") + if (data.reason === "auth_signed_out") verify(error.indexOf("Sign in again") >= 0, error) + } + function test_late_consent_after_account_change_does_not_touch_the_new_session() { + var auth = fresh() + fixture.hold = true + var answers = [] + auth.withGraphToken(function(token, reason) { answers.push({token: token, reason: reason}) }) + var pending = fixture.pending + auth.accountId = "outlook:bob@example.test" + pending(null, {code: -32000, message: "auth_consent_required"}) + compare(auth.graphConsentNeeded, false) + compare(answers.length, 1) + compare(answers[0].token, "") + compare(fixture.writes.length, 0) + compare(fixture.deletes.length, 0) + } + } +} From 4bdcec470db6f380714bdbad2811f1aa4b02b7cb Mon Sep 17 00:00:00 2001 From: latentoperator Date: Sun, 11 Oct 2026 03:33:43 -0500 Subject: [PATCH 10/88] Fix calendar times to follow the system clock format (#323) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Daily user of Omamail and would love to help: The calendar used a hard-coded 24-hour clock for all displayed times. With `LANG=en_US.UTF-8` (Qt's short time format `h:mm AP`), a 3 PM meeting appeared as `15:00` in month cells, week blocks, the hour rail, agenda, event details, reminder lists and reminder notifications, while the rest of the desktop showed `3:00 PM`. Calendar labels now use three small helpers in `Calendar.js` (`timeLabel`, `timeRangeLabel`, `dateTimeLabel`) that format times with `Qt.locale().timeFormat(Locale.ShortFormat)`. This preserves the locale's hour cycle, separator (`15.05` in Finnish), and AM/PM text and spacing. The surrounding date formats (`ddd, MMM d ·`, `d MMM`) and all-day lane are unchanged. The longer 12-hour labels also need a little more room. Week blocks measure the localized range and show only the start time when the range does not fit. The hour rail sizes itself using the locale's labels for 0:59 and 12:59, and month-overflow ranges wrap instead of overflowing. Time entry in the event composer stays `HH:mm`. The mail reader, agent and bar-preview clocks are unchanged and would need separate changes. A rule in `tests/test_source.sh` prevents hard-coded `hh:mm`/`HH:mm` display formats from returning to the calendar files, with the composer's validation message as the only exception. ## Verification Author validation on the original `15270ba` head: `make validate` passed on Omarchy with Qt 6.11.2: the QML suites passed 1158, 6 and 64 tests, with 0 failures; `qml-check` passes, and `omarchy plugin validate` and `git diff --check` are clean. The JS, shell, QML and lint targets also pass on v0.10.8. `ui/tests/test_calendar_time.js` covers 12- and 24-hour clocks, midnight, noon, a non-colon separator, ranges and multi-day labels. It fails against the unfixed code with `'15:00' !== '3:00 PM'` and passes with this change. The new `test_source.sh` rule fails on v0.10.8 at `calendar/CalendarReminders.qml:52` and passes here. Running the actual `Calendar.js` in a QML runtime produces `3:00 PM–4:30 PM` for `en_US` and `15:00–16:30` for `C`, `en_GB` and `de_DE`. Manual checks used synthetic events in a separate Quickshell window, with Catppuccin (dark) and Catppuccin Latte (light), full screen and at 716×768. These covered day, week, month, agenda, event details and the reminder notification. The before/after screenshots below use matching size, theme and scale. At 716×768, week blocks show only the start time where the range no longer fits. A narrow month cell that already placed the time after the title now elides it sooner (`Lunch · 11:3…`), using the existing title elision behavior. The notification displays `3:00 PM` while the panel displays `3:00PM`, even though both use the same string: the locale inserts a narrow no-break space (U+202F), which the two fonts render at different widths. A fresh agent reviewed the diff and found no blocking or important issues. It noted three things: bare `Qt.` calls in a `.pragma library` file, source-rule globs that cover only the current calendar files, and an hour-rail width based on two probe labels. `qmllint` reports nothing for `Calendar.js`, and the runtime check exercised the `Qt.` calls. Scrolled to the top of the week view, `12:00AM` through `9:00AM` fit in the rail with room to spare; the remaining hour labels are no wider and fit as well. ### Screenshots Each image is before (v0.10.8) on the left and after on the right, at the same size, theme and scale, with synthetic events. #### Catppuccin (dark), full screen **Week** ![Week before and after](https://github.com/user-attachments/assets/c3dd60c5-da55-4022-87be-01408bd92899) **Day** ![Day before and after](https://github.com/user-attachments/assets/f3a51d4b-592a-4538-8c30-4d7e9a2f5147) **Month** ![Month before and after](https://github.com/user-attachments/assets/bb5214c5-ca00-46ee-8d0d-bea0c3382338) **Agenda** ![Agenda before and after](https://github.com/user-attachments/assets/abc3ca47-8aa3-4511-9d38-21ffec95e119) **Event detail** ![Event detail before and after](https://github.com/user-attachments/assets/26883c43-8c4c-43e0-ae07-ca372602903d) **Reminder notification** ![Reminder notification before and after](https://github.com/user-attachments/assets/75543925-cf6c-43fb-b1ff-2b436064204d) #### Catppuccin (dark), 716×768 **Week** ![Week before and after](https://github.com/user-attachments/assets/15f9f2ca-985d-42f5-a797-2a496773f4bf) **Day** ![Day before and after](https://github.com/user-attachments/assets/7734227e-4d1c-4095-9835-0b83c4da7821) **Month** ![Month before and after](https://github.com/user-attachments/assets/29382a95-6f13-4ca3-8f2b-7c444f0d1866) **Agenda** ![Agenda before and after](https://github.com/user-attachments/assets/be3633c1-b1bb-4782-a1b6-4b56b30a3c8b) **Event detail** ![Event detail before and after](https://github.com/user-attachments/assets/a9a67446-039b-4800-b20e-86611133b3e6) #### Catppuccin Latte (light) **Week, full screen** ![Week, full screen before and after](https://github.com/user-attachments/assets/db1a0ba0-cbf6-47a7-8fdc-ee74a0959c7b) **Month, 716×768** ![Month, 716×768 before and after](https://github.com/user-attachments/assets/57013e84-cb6c-4fac-a0a0-58e4a393f6a4) #### Top of the day **Week scrolled to midnight, 980×720** ![Week scrolled to midnight, 980×720 before and after](https://github.com/user-attachments/assets/e55ff1e1-9beb-4049-8b50-343590cd1a0c) ## Release Notes - Calendar times and reminders now follow your system's 12-hour or 24-hour clock format. ## Maintainer review validation Final reviewed head `ca23d43a28f57d041499f107c957026fb42d5f78` includes current main `b0504b9` through a normal merge without conflicts. On Omarchy with Qt 6.11.2 and Node 26, `make validate` passed on this exact head: Rust unit/integration tests, the complete JavaScript and shell suites, actual libcurl behavior, QML (1163 shared, 6 Outlook, 64 sidebar tests; no failures or skips), the native resource-request regression, standalone composition, qmllint, plugin validation and whitespace checks. The focused calendar time/feed tests and source display-format guard also passed. Security: PASS for the affected display-only boundaries; sender-controlled labels remain plain text, and no credential, routing or remote-resource policy changes are introduced. UI evidence: PASS for the affected views after the supplementary comparisons below. Product scope is accepted. Required GitHub checks for the new head must still finish before approval or merge. ## Maintainer supplementary UI evidence The comparisons below cover reminder inbox, month overflow popup, timed-event tooltip, and create/move/resize drag previews. BEFORE is left and AFTER is right; each half is 716×768 at scale 1 using Qt 6.11.2, identical synthetic events and fixed dark/light semantic palettes. These are production-component renders using test compatibility imports, not complete installed-desktop or live-account screenshots. `12` uses en_US: labels change from 24-hour times to AM/PM. `24` uses locale C: labels retain 24-hour formatting. Date formatting and reminder actions remain unchanged; drag and tooltip labels follow the locale helpers. The supplemental production-component captures were rerendered against current-main base `b0504b9` and final head `ca23d43`. All 48 single-side images are pixel-identical to the attached originals below the revision-caption strip, so the original attachment URLs remain accurate. Existing author screenshots above are retained. **12 DARK CREATE PREVIEW** ![12-dark-create-preview](https://github.com/user-attachments/assets/0724c388-7efe-4052-8935-43b8985f9048) **12 DARK MOVE PREVIEW** ![12-dark-move-preview](https://github.com/user-attachments/assets/a999657f-041a-45d8-94a6-8a8252f3de25) **12 DARK OVERFLOW** ![12-dark-overflow](https://github.com/user-attachments/assets/92126de6-5428-49e8-902f-38ffc6cf22ad) **12 DARK REMINDERS** ![12-dark-reminders](https://github.com/user-attachments/assets/dcf7c08e-37c3-424b-924c-6a68a1eb18d1) **12 DARK RESIZE PREVIEW** ![12-dark-resize-preview](https://github.com/user-attachments/assets/58339f38-90ca-40b2-96ac-b8fdf82d91c4) **12 DARK TOOLTIP** ![12-dark-tooltip](https://github.com/user-attachments/assets/ebb15e69-becf-4806-bec9-0bb4a680c94b) **12 LIGHT CREATE PREVIEW** ![12-light-create-preview](https://github.com/user-attachments/assets/6e0e546e-4809-47b0-97cc-ac21ee779c72) **12 LIGHT MOVE PREVIEW** ![12-light-move-preview](https://github.com/user-attachments/assets/50c98f0d-8272-4e7c-8eba-7bf617001aaf) **12 LIGHT OVERFLOW** ![12-light-overflow](https://github.com/user-attachments/assets/fc45fdf2-c7e3-4692-99db-39bbfde5fd19) **12 LIGHT REMINDERS** ![12-light-reminders](https://github.com/user-attachments/assets/dd7af6c3-8bbe-4255-902b-2a355bba43bc) **12 LIGHT RESIZE PREVIEW** ![12-light-resize-preview](https://github.com/user-attachments/assets/546d42fb-10dc-45e0-8b62-d606d0f97b00) **12 LIGHT TOOLTIP** ![12-light-tooltip](https://github.com/user-attachments/assets/786efffa-a8c8-469d-bfe2-6083c04b1869) **24 DARK CREATE PREVIEW** ![24-dark-create-preview](https://github.com/user-attachments/assets/7d4d178c-a0b5-4c05-9672-7e49f5634daa) **24 DARK MOVE PREVIEW** ![24-dark-move-preview](https://github.com/user-attachments/assets/886ab2a3-a554-466c-80c6-40179b74f686) **24 DARK OVERFLOW** ![24-dark-overflow](https://github.com/user-attachments/assets/141ea02d-c6a0-4eb2-aee1-b947bb874c95) **24 DARK REMINDERS** ![24-dark-reminders](https://github.com/user-attachments/assets/6f8dfdf5-17d3-4757-9ee6-05dae64d490c) **24 DARK RESIZE PREVIEW** ![24-dark-resize-preview](https://github.com/user-attachments/assets/1f10ba00-0150-4f4e-a037-b565788fcbf6) **24 DARK TOOLTIP** ![24-dark-tooltip](https://github.com/user-attachments/assets/4e58b183-05c6-498a-a1eb-71409d03b160) **24 LIGHT CREATE PREVIEW** ![24-light-create-preview](https://github.com/user-attachments/assets/1b59b7f4-ec2d-4966-b332-6fb39039238d) **24 LIGHT MOVE PREVIEW** ![24-light-move-preview](https://github.com/user-attachments/assets/0740129d-bc38-48e6-93a8-08b80828531e) **24 LIGHT OVERFLOW** ![24-light-overflow](https://github.com/user-attachments/assets/cecd773c-06bc-4ecb-ade3-809ee8f9ef6b) **24 LIGHT REMINDERS** ![24-light-reminders](https://github.com/user-attachments/assets/444ff44a-4bcf-4b39-9e91-86f6ca3a9ef8) **24 LIGHT RESIZE PREVIEW** ![24-light-resize-preview](https://github.com/user-attachments/assets/080b3d96-fc4d-4789-bf10-2148ffb96534) **24 LIGHT TOOLTIP** ![24-light-tooltip](https://github.com/user-attachments/assets/b228932b-0954-454e-9fff-3d0a1e591f7e) Co-authored-by: Chris Gray <216301595+latentoperator@users.noreply.github.com> Co-authored-by: Jason Lee --- Makefile | 1 + tests/test_source.sh | 17 ++++++ ui/calendar/Calendar.js | 17 +++++- ui/calendar/CalendarReminders.qml | 4 +- ui/components/CalendarEventDetail.qml | 8 +-- ui/components/CalendarReminderPanel.qml | 4 +- ui/components/CalendarView.qml | 18 ++++--- ui/components/WeekCalendarView.qml | 41 ++++++++++----- ui/tests/test_calendar_feed.js | 10 +++- ui/tests/test_calendar_time.js | 69 +++++++++++++++++++++++++ 10 files changed, 161 insertions(+), 28 deletions(-) create mode 100644 ui/tests/test_calendar_time.js diff --git a/Makefile b/Makefile index a93e249..8d05989 100644 --- a/Makefile +++ b/Makefile @@ -140,6 +140,7 @@ test-js: node ui/tests/test_gmail_api.js node ui/tests/test_message.js node ui/tests/test_calendar.js + node ui/tests/test_calendar_time.js node ui/tests/test_calendar_cache.js node ui/tests/test_calendar_feed.js node ui/tests/test_calendar_sources.js diff --git a/tests/test_source.sh b/tests/test_source.sh index 2cee5e8..4e5a4d9 100755 --- a/tests/test_source.sh +++ b/tests/test_source.sh @@ -61,6 +61,23 @@ for file in "${JS_FILES[@]}"; do fi done +# Calendar display clocks use the locale's short format. The composer's +# HH:mm entry convention and its validation error are deliberately separate. +python3 - <<'PY_CALENDAR_TIME' +import re +from pathlib import Path +files = sorted(Path("calendar").glob("*.qml")) + sorted(Path("calendar").glob("*.js")) +files += sorted(Path("components").glob("*Calendar*.qml")) +for path in files: + for number, line in enumerate(path.read_text().splitlines(), 1): + if re.match(r"\s*//", line): + continue + for match in re.finditer(r'"([^"\n]*(?:hh|HH):mm[^"\n]*)"', line): + if path == Path("calendar/Calendar.js") and match.group(1) == "Enter valid dates (YYYY-MM-DD) and times (HH:mm)": + continue + raise SystemExit("test_source.sh: %s:%d hard-codes a calendar clock; use the locale short time format" % (path, number)) +PY_CALENDAR_TIME + # 3. Nothing may name a colour inside a JS library either: colours are passed # in from QML, which is the only place that can read the theme. # Html.js is the one exception, and a narrow one: PAPER and INK are the sheet a diff --git a/ui/calendar/Calendar.js b/ui/calendar/Calendar.js index 2da6823..20a3f9c 100644 --- a/ui/calendar/Calendar.js +++ b/ui/calendar/Calendar.js @@ -1241,10 +1241,23 @@ function nowOffset(day, firstHour, lastHour, hourHeight, nowMs) { return (minutes - first) / 60 * Number(hourHeight) } -function timeLabel(timeMs) { +// QML supplies Qt.locale().timeFormat(Locale.ShortFormat); keep the complete +// pattern so Qt also owns localized separators and AM/PM text. Node tests +// supply the Qt formatting boundary without needing the QML engine. +function timeLabel(timeMs, localeFormat) { var time = new Date(Number(timeMs)) if (!isFinite(time.getTime())) return "" - return two(time.getHours()) + ":" + two(time.getMinutes()) + return time.toLocaleTimeString(Qt.locale(), localeFormat) +} + +function timeRangeLabel(startMs, endMs, localeFormat, separator) { + return timeLabel(startMs, localeFormat) + (separator === undefined ? "–" : separator) + + timeLabel(endMs, localeFormat) +} + +function dateTimeLabel(timeMs, dateFormat, localeFormat, separator) { + return Qt.formatDate(new Date(Number(timeMs)), dateFormat) + + (separator === undefined ? " " : separator) + timeLabel(timeMs, localeFormat) } // The same offset for the week as a whole, so the time rail can label the line diff --git a/ui/calendar/CalendarReminders.qml b/ui/calendar/CalendarReminders.qml index cfba82a..83a62c5 100644 --- a/ui/calendar/CalendarReminders.qml +++ b/ui/calendar/CalendarReminders.qml @@ -1,9 +1,11 @@ import QtQuick import Quickshell.Io import "Reminders.js" as Reminders +import "Calendar.js" as Calendar Item { id: root + readonly property string timeFormat: Qt.locale().timeFormat(Locale.ShortFormat) required property var service required property string pluginDir required property string notificationForeground @@ -49,7 +51,7 @@ Item { var notice = reminderInbox.receive(record) // The process budget limits desktop waiters, never the actionable inbox. if (waiters >= 16) return - var body = Qt.formatDateTime(new Date(record.start), "ddd, MMM d · hh:mm") + var body = Calendar.dateTimeLabel(record.start, "ddd, MMM d", root.timeFormat, " · ") var process = notification.createObject(root, { record: notice, command: ["python3", pluginDir + "/scripts/notify-mail.py", "--calendar", notificationForeground, notificationAccent, "--", record.title, body] }) diff --git a/ui/components/CalendarEventDetail.qml b/ui/components/CalendarEventDetail.qml index 20a22c5..075af2c 100644 --- a/ui/components/CalendarEventDetail.qml +++ b/ui/components/CalendarEventDetail.qml @@ -8,6 +8,7 @@ import "../message/Html.js" as Html Rectangle { id: root + readonly property string timeFormat: Qt.locale().timeFormat(Locale.ShortFormat) required property var controller required property var event @@ -89,10 +90,9 @@ Rectangle { } var startDay = Qt.formatDate(start, "dddd, d MMMM yyyy") if (start.toDateString() === end.toDateString()) - return startDay + " · " + Qt.formatTime(start, "HH:mm") + "–" - + Qt.formatTime(end, "HH:mm") - return startDay + " · " + Qt.formatTime(start, "HH:mm") + " – " - + Qt.formatDate(end, "dddd, d MMMM yyyy") + " · " + Qt.formatTime(end, "HH:mm") + return startDay + " · " + Calendar.timeRangeLabel(start.getTime(), end.getTime(), root.timeFormat) + return startDay + " · " + Calendar.timeLabel(start.getTime(), root.timeFormat) + " – " + + Calendar.dateTimeLabel(end.getTime(), "dddd, d MMMM yyyy", root.timeFormat, " · ") } CalendarPalette { diff --git a/ui/components/CalendarReminderPanel.qml b/ui/components/CalendarReminderPanel.qml index 49a1c1e..f1ce10f 100644 --- a/ui/components/CalendarReminderPanel.qml +++ b/ui/components/CalendarReminderPanel.qml @@ -1,9 +1,11 @@ import QtQuick import QtQuick.Controls import qs.Commons +import "../calendar/Calendar.js" as Calendar Column { id: root + readonly property string timeFormat: Qt.locale().timeFormat(Locale.ShortFormat) required property var service required property color textColor required property color dimColor @@ -59,7 +61,7 @@ Column { Text { width: parent.width text: String(entry.modelData.title || "Event") + " · " - + Qt.formatDateTime(new Date(entry.modelData.start), "ddd HH:mm") + + Calendar.dateTimeLabel(entry.modelData.start, "ddd", root.timeFormat) textFormat: Text.PlainText wrapMode: Text.Wrap color: root.textColor diff --git a/ui/components/CalendarView.qml b/ui/components/CalendarView.qml index fe7b921..abd6ec7 100644 --- a/ui/components/CalendarView.qml +++ b/ui/components/CalendarView.qml @@ -8,6 +8,7 @@ import "../keys/Keymap.js" as Keymap Item { id: root + readonly property string timeFormat: Qt.locale().timeFormat(Locale.ShortFormat) required property var controller required property color textColor @@ -827,8 +828,7 @@ Item { var event = monthEvent.eventData if (!event) return "" var time = event.start && !event.start.allDay - ? Calendar.two(new Date(event.start.ms).getHours()) + ":" - + Calendar.two(new Date(event.start.ms).getMinutes()) + " " : "" + ? Calendar.timeLabel(event.start.ms, root.timeFormat) + " " : "" var title = String(event.summary || "Untitled event") return monthEvent.width < Style.space(140) ? title + (time ? " · " + time.trim() : "") : time + title } @@ -923,7 +923,7 @@ Item { anchors.leftMargin: Style.space(spanBar.modelData.continuesBefore ? 18 : 6) anchors.rightMargin: Style.space(spanBar.modelData.continuesAfter ? 18 : 6) verticalAlignment: Text.AlignVCenter - text: (spanBar.eventData.start.allDay ? "" : Calendar.timeLabel(spanBar.eventData.start.ms) + " ") + text: (spanBar.eventData.start.allDay ? "" : Calendar.timeLabel(spanBar.eventData.start.ms, root.timeFormat) + " ") + String(spanBar.eventData.summary || "Untitled event") textFormat: Text.PlainText elide: Text.ElideRight @@ -1040,8 +1040,9 @@ Item { } Text { width: parent.width - text: Qt.formatDateTime(new Date(agendaRow.modelData.start.ms), agendaRow.modelData.start.allDay ? "ddd, MMM d" : "ddd, MMM d · hh:mm") - + (agendaRow.modelData.start.allDay ? " · All day" : "") + text: agendaRow.modelData.start.allDay + ? Qt.formatDate(new Date(agendaRow.modelData.start.ms), "ddd, MMM d") + " · All day" + : Calendar.dateTimeLabel(agendaRow.modelData.start.ms, "ddd, MMM d", root.timeFormat, " · ") textFormat: Text.PlainText color: root.dimColor font.family: root.panelFontFamily @@ -1170,9 +1171,12 @@ Item { y: overflowTitle.y + overflowTitle.height + Style.space(2) width: overflowTitle.width text: overflowEvent.modelData.start.allDay ? "All day" - : Qt.formatDateTime(new Date(overflowEvent.modelData.start.ms), Calendar.spansMultipleDays(overflowEvent.modelData) ? "d MMM HH:mm" : "HH:mm") - + " – " + Qt.formatDateTime(new Date(overflowEvent.modelData.end.ms), Calendar.spansMultipleDays(overflowEvent.modelData) ? "d MMM HH:mm" : "HH:mm") + : Calendar.spansMultipleDays(overflowEvent.modelData) + ? Calendar.dateTimeLabel(overflowEvent.modelData.start.ms, "d MMM", root.timeFormat) + + " – " + Calendar.dateTimeLabel(overflowEvent.modelData.end.ms, "d MMM", root.timeFormat) + : Calendar.timeRangeLabel(overflowEvent.modelData.start.ms, overflowEvent.modelData.end.ms, root.timeFormat, " – ") textFormat: Text.PlainText + wrapMode: Text.Wrap color: root.dimColor font.family: root.panelFontFamily font.pixelSize: Style.font.caption diff --git a/ui/components/WeekCalendarView.qml b/ui/components/WeekCalendarView.qml index 44dafa7..2a51d42 100644 --- a/ui/components/WeekCalendarView.qml +++ b/ui/components/WeekCalendarView.qml @@ -5,6 +5,7 @@ import "../calendar/Calendar.js" as Calendar Item { id: root + readonly property string timeFormat: Qt.locale().timeFormat(Locale.ShortFormat) required property var controller required property var days @@ -39,13 +40,28 @@ Item { } } - readonly property real timeRailWidth: Math.max(Style.space(52), allDayLabelMetrics.advanceWidth + Style.space(12)) + readonly property real timeRailWidth: Math.max(Style.space(52), + allDayLabelMetrics.advanceWidth + Style.space(12), + midnightTimeMetrics.advanceWidth + Style.space(12), + noonTimeMetrics.advanceWidth + Style.space(12), nowLabel.implicitWidth + Style.space(12)) TextMetrics { id: allDayLabelMetrics font.family: root.panelFontFamily font.pixelSize: Style.font.caption text: "all-day" } + TextMetrics { + id: midnightTimeMetrics + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + text: Calendar.timeLabel(new Date(2000, 0, 1, 0, 59).getTime(), root.timeFormat) + } + TextMetrics { + id: noonTimeMetrics + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + text: Calendar.timeLabel(new Date(2000, 0, 1, 12, 59).getTime(), root.timeFormat) + } readonly property var weekdayNames: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] readonly property var hourRange: Calendar.weekHourRange( controller ? controller.events : [], days, 7, 19) @@ -248,8 +264,8 @@ Item { contentItem: Text { text: String(allDayEvent.eventData.summary || "Untitled event") + (allDayEvent.eventData.start.allDay ? "" : "\n" - + Qt.formatDateTime(new Date(allDayEvent.eventData.start.ms), "ddd d MMM, HH:mm") - + " – " + Qt.formatDateTime(new Date(allDayEvent.eventData.end.ms), "ddd d MMM, HH:mm")) + + Calendar.dateTimeLabel(allDayEvent.eventData.start.ms, "ddd d MMM", root.timeFormat, ", ") + + " – " + Calendar.dateTimeLabel(allDayEvent.eventData.end.ms, "ddd d MMM", root.timeFormat, ", ")) textFormat: Text.PlainText color: root.textColor font.family: root.panelFontFamily @@ -298,7 +314,7 @@ Item { anchors.leftMargin: Style.space(4) anchors.top: parent.top anchors.topMargin: index === 0 ? Style.space(2) : -implicitHeight / 2 - text: Calendar.two(root.firstHour + index) + ":00" + text: Calendar.timeLabel(new Date(2000, 0, 1, root.firstHour + index).getTime(), root.timeFormat) color: root.dimColor font.family: root.panelFontFamily font.pixelSize: Style.font.caption @@ -332,7 +348,7 @@ Item { Text { id: nowLabel anchors.centerIn: parent - text: Calendar.timeLabel(root.nowMs) + text: Calendar.timeLabel(root.nowMs, root.timeFormat) color: root.backgroundColor font.family: root.panelFontFamily font.pixelSize: Style.font.caption @@ -389,7 +405,7 @@ Item { y: Calendar.eventTop({start:{ms:first}}, dayColumn.modelData, root.firstHour, timeline.hourHeight), width: dayColumn.width, height: (last - first) / 3600000 * timeline.hourHeight, - label: Qt.formatTime(new Date(first), "HH:mm") + "–" + Qt.formatTime(new Date(last), "HH:mm") } + label: Calendar.timeRangeLabel(first, last, root.timeFormat) } } onPressed: function(mouse) { initialY = mouse.y; selectTo(mouse.y) } onPositionChanged: function(mouse) { if (pressed) selectTo(mouse.y) } @@ -453,8 +469,8 @@ Item { Text { width: parent.width visible: eventBlock.height >= Style.space(38) - text: Qt.formatTime(new Date(eventBlock.eventData.start.ms), "HH:mm") - + (width >= timeMetrics.advanceWidth ? "–" + Qt.formatTime(new Date(eventBlock.eventData.end.ms), "HH:mm") : "") + text: width >= timeMetrics.advanceWidth ? timeMetrics.text + : Calendar.timeLabel(eventBlock.eventData.start.ms, root.timeFormat) elide: Text.ElideRight color: root.dimColor font.family: root.panelFontFamily @@ -464,7 +480,7 @@ Item { id: timeMetrics font.family: root.panelFontFamily font.pixelSize: Style.font.caption - text: "00:00–00:00" + text: Calendar.timeRangeLabel(eventBlock.eventData.start.ms, eventBlock.eventData.end.ms, root.timeFormat) } } } @@ -504,7 +520,7 @@ Item { root.dragPreview = { x: root.timeRailWidth + (edge === "" ? column : initialColumn) * dayColumn.width, y: Calendar.eventTop(previewEvent, previewDay, root.firstHour, timeline.hourHeight), width: dayColumn.width, height: Calendar.eventHeight(previewEvent, previewDay, timeline.hourHeight), - label: Qt.formatTime(new Date(proposed.start), "HH:mm") + "–" + Qt.formatTime(new Date(proposed.end), "HH:mm") } + label: Calendar.timeRangeLabel(proposed.start, proposed.end, root.timeFormat) } } onReleased: { root.dragPreview = null @@ -543,8 +559,7 @@ Item { delay: 500 contentItem: Text { text: String(eventBlock.eventData.summary || "Untitled event") + "\n" - + Qt.formatTime(new Date(eventBlock.eventData.start.ms), "HH:mm") + "–" - + Qt.formatTime(new Date(eventBlock.eventData.end.ms), "HH:mm") + + Calendar.timeRangeLabel(eventBlock.eventData.start.ms, eventBlock.eventData.end.ms, root.timeFormat) textFormat: Text.PlainText color: root.textColor font.family: root.panelFontFamily @@ -613,8 +628,10 @@ Item { Text { anchors.top: parent.top anchors.left: parent.left + anchors.right: parent.right anchors.margins: Style.space(5) text: root.dragPreview ? root.dragPreview.label || "" : "" + elide: Text.ElideRight textFormat: Text.PlainText color: root.textColor font.family: root.panelFontFamily diff --git a/ui/tests/test_calendar_feed.js b/ui/tests/test_calendar_feed.js index fc597eb..9b80c64 100644 --- a/ui/tests/test_calendar_feed.js +++ b/ui/tests/test_calendar_feed.js @@ -168,7 +168,15 @@ const springDay = feed.weekDays(new Date(2026, 2, 8, 12).getTime(), 0)[0] const springHalfThree = new Date(2026, 2, 8, 3, 30).getTime() assert.strictEqual(feed.nowOffset(springDay, 0, 24, 60, springHalfThree), 210, "spring-forward now follows the wall-clock hour") -assert.strictEqual(feed.timeLabel(springHalfThree), "03:30", +feed.Qt = { locale: function() { return "test-system-locale" }, formatTime: function(date, locale, format) { + assert.strictEqual(locale, "test-system-locale") + assert.strictEqual(format, "h:mm AP") + assert.strictEqual(date.getHours(), 3, "the marker's label uses the same local wall-clock hour") + assert.strictEqual(date.getMinutes(), 30) + return "3:30 AM" +} } +require("vm").runInContext("Date.prototype.toLocaleTimeString = function(locale, format) { return Qt.formatTime(this, locale, format) }", feed) +assert.strictEqual(feed.timeLabel(springHalfThree, "h:mm AP"), "3:30 AM", "the tested marker position and its label use the same local time") const springEvent = { start: { ms: springHalfThree, allDay: false }, diff --git a/ui/tests/test_calendar_time.js b/ui/tests/test_calendar_time.js new file mode 100644 index 0000000..9f83814 --- /dev/null +++ b/ui/tests/test_calendar_time.js @@ -0,0 +1,69 @@ +const assert = require("assert") +const vm = require("vm") +const { load } = require("./load") + +const calendar = load("calendar/Calendar.js") +const locale = { name: "test-system-locale" } +const formats = { + "h:mm AP": new Intl.DateTimeFormat("en-US", { hour: "numeric", minute: "2-digit", hourCycle: "h12" }), + "HH:mm": new Intl.DateTimeFormat("en-GB", { hour: "2-digit", minute: "2-digit", hourCycle: "h23" }), + "HH.mm": new Intl.DateTimeFormat("fi-FI", { hour: "2-digit", minute: "2-digit", hourCycle: "h23" }) +} + +// Supply only Qt's formatting boundary. The module must pass the locale's +// format through intact; Intl supplies real locale clocks without a Qt host. +calendar.Qt = { + locale() { return locale }, + formatTime(date, suppliedLocale, format) { + assert.strictEqual(suppliedLocale, locale, "localize AM/PM using the system locale") + assert.ok(formats[format], "use the supplied locale format") + return formats[format].format(date) + }, + formatDate(date, format) { + const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] + const weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][date.getDay()] + const labels = { + "d MMM": date.getDate() + " " + months[date.getMonth()], + "ddd": weekday, + "ddd, MMM d": weekday + ", " + months[date.getMonth()] + " " + date.getDate(), + "ddd d MMM": weekday + " " + date.getDate() + " " + months[date.getMonth()] + } + assert.ok(labels[format], "use the date label's existing shape") + return labels[format] + } +} +// QML extends Date with a (locale, format) overload absent from Node. +vm.runInContext("Date.prototype.toLocaleTimeString = function(locale, format) { return Qt.formatTime(this, locale, format) }", calendar) +function at(hour, minute = 0, day = 1) { + return new Date(2026, 9, day, hour, minute).getTime() +} + +assert.strictEqual(calendar.timeLabel(at(15), "h:mm AP"), "3:00 PM", "an afternoon needs AM/PM in a 12-hour locale") +assert.strictEqual(calendar.timeLabel(at(15), "HH:mm"), "15:00") +assert.strictEqual(calendar.timeLabel(at(0), "h:mm AP"), "12:00 AM") +assert.strictEqual(calendar.timeLabel(at(12), "h:mm AP"), "12:00 PM") +assert.strictEqual(calendar.timeLabel(at(0), "HH:mm"), "00:00") +assert.strictEqual(calendar.timeLabel(at(12), "HH:mm"), "12:00") +assert.strictEqual(calendar.timeLabel(at(3, 5), "h:mm AP"), "3:05 AM") +assert.strictEqual(calendar.timeLabel(at(15, 5), "HH.mm"), "15.05", "keep the locale's separator") +assert.strictEqual(calendar.timeLabel(NaN, "h:mm AP"), "") + +for (const [format, start, end] of [["h:mm AP", "3:00 PM", "4:30 PM"], ["HH:mm", "15:00", "16:30"]]) { + assert.strictEqual(calendar.timeRangeLabel(at(15), at(16, 30), format), start + "–" + end) + assert.strictEqual(calendar.timeRangeLabel(at(15), at(16, 30), format, " – "), start + " – " + end) + assert.strictEqual(calendar.dateTimeLabel(at(15), "d MMM", format), "1 Oct " + start) + assert.strictEqual(calendar.dateTimeLabel(at(15), "d MMM", format, " · "), "1 Oct · " + start) + assert.strictEqual(calendar.dateTimeLabel(at(15), "ddd, MMM d", format, " · "), "Thu, Oct 1 · " + start, + "agenda rows and desktop reminders retain their date prefix") + assert.strictEqual(calendar.dateTimeLabel(at(15), "ddd", format), "Thu " + start, + "reminder inbox labels remain compact") + assert.strictEqual(calendar.dateTimeLabel(at(15), "d MMM", format) + " – " + + calendar.dateTimeLabel(at(16, 30, 2), "d MMM", format), "1 Oct " + start + " – 2 Oct " + end) + assert.strictEqual(calendar.dateTimeLabel(at(15), "ddd d MMM", format, ", ") + " – " + + calendar.dateTimeLabel(at(16, 30, 2), "ddd d MMM", format, ", "), "Thu 1 Oct, " + start + " – Fri 2 Oct, " + end, + "timed multi-day events in the all-day lane retain both dates") +} +assert.strictEqual(calendar.timeRangeLabel(at(23, 30), at(0, 15, 2), "h:mm AP"), "11:30 PM–12:15 AM") +assert.strictEqual(calendar.timeRangeLabel(at(11, 30), at(12, 15), "h:mm AP"), "11:30 AM–12:15 PM") + +console.log("test_calendar_time.js: all assertions passed") From 4d5f0a21ac22086862186f3e00d91669000f539d Mon Sep 17 00:00:00 2001 From: lim000 Date: Sun, 11 Oct 2026 16:38:02 +0800 Subject: [PATCH 11/88] Fix IMAP list timeouts on slow servers with large mailboxes (#332) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #331. ## Summary An `imap.*` call is wrapped in a fixed 22-second budget, and `ui/backend/Backend.qml` gives every frame 30 seconds before it stops the backend process. Both numbers assume a server that answers a command in well under a second, on a mailbox small enough that a listing is a handful of commands. Neither holds for Yahoo. From a Chinese connection it answers in roughly half a second per command, and a 10 000-message INBOX costs nine of them: `SELECT`, a `UID FETCH 1:* (UID)` snapshot, then one `UID FETCH <4 096 UIDs> (UID INTERNALDATE)` window per 4 096 messages. Measured on the reported account: `imap.list` alone took 13.6–15.7 s, and the panel's `mail.list` (that plus `imap.messages`) took 22.02 s — the budget exactly — and answered `request_timed_out`. The Inbox then showed no mail at all, on every refresh, while the same plugin listed a 163 mailbox of 7 844 messages normally in 1.7 s. The budget is the wrong shape for the job: a call is a conversation whose length is the mailbox's, so a fixed per-call allowance measures the mailbox's patience rather than detecting a hang. ## What changes - `src/providers/imap/mod.rs`: the three whole-call budgets become one named `CALL_BUDGET` (120 s), and every reply read gets `COMMAND_BUDGET` (30 s) of its own. A server that accepts a command and then goes quiet still fails quickly — it no longer has to consume the whole conversation's allowance to be recognised as hung. - `src/providers/imap/idle.rs`: the IDLE session's `STEP` is that same per-reply budget, spelled as the one constant instead of a second copy of the number. - `ui/backend/Backend.qml`: an `imap.*` frame gets 180 s, more than the backend's own call budget, so the process is not killed while the backend is still waiting on a server that is still answering. Every other frame keeps the 30 s it had. ## Verification Reproduced against the reported account (Yahoo, 10 000-message INBOX, ~0.5 s per command), swapping only the pinned backend binary: | Backend | `omamail list --account imap:
` | | --- | --- | | v0.10.8 | 22.02 s, `request_timed_out`; a second run returned the page in 19.1 s | | this change | 14.2 s, 16.7 s, 35.4 s, 42.3 s — every run returned the newest 25 messages, including same-day mail | Rust 1.98.1: `cargo test --locked --features integration-test-credentials` passes — 599 lib tests (11 ignored), 33 CLI tests, and every integration target (accounts 1, attachment_pdf 16, backend_lifetime 1, gmail 5, hey 4, message 1, message_charset 7, platform_native 11, protocol 4, stdio 3, upload 4). The `providers::imap` module alone is 75 tests. The regression test fails without the change: with `src/providers/imap/mod.rs` restored to main, `a_slow_server_that_answers_every_command_still_pages` fails at 22.00 s — the old budget, exactly — and passes with it. `a_command_that_is_never_answered_ends_the_call` passes on both revisions, because what it pins is the hang guard the per-reply budget keeps, not the size of a budget. Both new Rust tests spend their seconds for real, about 27 s and 30 s, running in parallel with the rest of the suite. The budget they have to outlast is a wall-clock 22 s, and a paused clock cannot carry the loopback fixture: with `start_paused` the runtime advances to the client's own timer before the fixture's replies arrive, which the first version of this test did. If the cost is unwelcome, they are the two tests to make opt-in, not the fix. JavaScript: `make test-js` passes, including the new block in `ui/tests/test_backend_queue.js`, which drives the production `Backend.qml` functions and reports `ERR_ASSERTION` against the unmodified file. `make qml-check`, `omarchy plugin validate .` and `git diff --check` pass. Not verified here: a running Quickshell panel, and a mailbox other than the reported account — the measurements above come from `omamail` CLI calls against that mailbox, which reproduce the failure at the provider boundary where both tests sit. `make validate` includes stages needing CMake and the Omarchy/Quickshell tooling this host lacks; its Rust, JS and portable-shell stages are what ran above. ## What this does not change - The interactive search keeps its own tighter 12 s step budget in `read/search.rs`. A streamed search is supposed to answer quickly or degrade; a listing that must produce a page is a different promise. - `src/backend/reader.rs` (25 s) and the frame timeout in `src/backend/protocol.rs` (25 s, Gmail's IPC deadline against its own rate limiting) are untouched. An IMAP body read is one command exchange plus parsing, and widening Gmail's documented deadline is not this fix. - No protocol, validation, credential or provider-selection behavior changes. ## Security PASS for the affected boundary. The change adds and widens `tokio::time::timeout` guards around exchanges that already ran inside one; it reads no new input, logs nothing, holds no credential and opens no connection the previous budgets did not. The longer allowance extends how long a hostile or broken server can hold a backend task, bounded per reply by `COMMAND_BUDGET` and per call by `CALL_BUDGET`, with the frontend's frame deadline above both. ## Release Notes - Fix Inbox listings timing out, and showing no mail, on IMAP servers that answer slowly or hold a large mailbox. Co-authored-by: Hermes Co-authored-by: Hermes Co-authored-by: Jason Lee --- src/providers/imap/idle.rs | 9 +-- src/providers/imap/mod.rs | 35 ++++++++++-- src/providers/imap/read/tests.rs | 98 ++++++++++++++++++++++++++++++++ ui/backend/Backend.qml | 6 ++ ui/tests/test_backend_queue.js | 14 +++++ 5 files changed, 154 insertions(+), 8 deletions(-) diff --git a/src/providers/imap/idle.rs b/src/providers/imap/idle.rs index 944364c..1f4d68a 100644 --- a/src/providers/imap/idle.rs +++ b/src/providers/imap/idle.rs @@ -5,8 +5,8 @@ //! INBOX changed, and the check that follows reads what changed through the //! ordinary path, so nothing here interprets a mailbox. use super::{ - LIMIT, Result, Wire, advertises, command, connect, literal_length, login, resolve_account, - write, + COMMAND_BUDGET, LIMIT, Result, Wire, advertises, command, connect, literal_length, login, + resolve_account, write, }; use serde_json::{Value, json}; use std::{ @@ -18,8 +18,9 @@ use std::{ use tokio::{io::AsyncBufReadExt, sync::Notify, time::Instant}; /// The budget for everything from connecting to examining INBOX, and again for -/// each later command exchange. -const STEP: Duration = Duration::from_secs(22); +/// each later command exchange — the same allowance `command` gives one reply, +/// because that is what each of these is. +const STEP: Duration = COMMAND_BUDGET; /// How often the wall clock is compared with the monotonic one, which stops /// while the machine sleeps; the same check as the JMAP event stream's. const RESUME_CHECK: Duration = Duration::from_secs(30); diff --git a/src/providers/imap/mod.rs b/src/providers/imap/mod.rs index 6707fd6..3080b0b 100644 --- a/src/providers/imap/mod.rs +++ b/src/providers/imap/mod.rs @@ -19,6 +19,20 @@ use tokio_rustls::{ rustls::{ClientConfig, RootCertStore, pki_types::ServerName}, }; const LIMIT: usize = 32 * 1024 * 1024; +/// How long one reply may take: the command is written, then read to its +/// tagged completion. A hang guard, not a speed test — a 4 096-UID date batch +/// is a quarter of a megabyte, and a server that charges a second a round trip +/// is still working. +const COMMAND_BUDGET: Duration = Duration::from_secs(30); +/// How long one `imap.*` call may take. A call is a conversation — connect, +/// login, SELECT, then one command per 4 096-UID window — so its cost is the +/// server's per-command cost multiplied by the mailbox's size. Yahoo charges +/// roughly half a second per command and a 10 000-message INBOX costs nine of +/// them; a loaded server multiplies that. The budget has to clear the honest +/// worst case, because what it exists to stop is a call that never ends. +/// `ui/backend/Backend.qml` holds the matching frame deadline, so the process +/// is not killed while a slow server is still answering. +const CALL_BUDGET: Duration = Duration::from_secs(120); type Result = std::result::Result; trait Socket: AsyncRead + AsyncWrite + Unpin + Send {} impl Socket for T {} @@ -122,10 +136,23 @@ async fn response(w: &mut Wire, tag: &str, continuation: bool) -> Result .await?; Ok(out) } +/// Read one reply under `COMMAND_BUDGET`: a server that takes a command and +/// then goes quiet fails the call here, rather than holding it until the +/// conversation as a whole runs out of time. +async fn response_each( + w: &mut Wire, + tag: &str, + continuation: bool, + visit: impl FnMut(&[u8]) -> Result<()>, +) -> Result<()> { + tokio::time::timeout(COMMAND_BUDGET, records(w, tag, continuation, visit)) + .await + .unwrap_or(Err("request_timed_out")) +} /// Visit complete IMAP response records, keeping literals attached to their /// protocol record. UID inventories can retain only numbers instead of a full /// response and a second, much larger parsed syntax tree. -async fn response_each( +async fn records( w: &mut Wire, tag: &str, continuation: bool, @@ -394,7 +421,7 @@ pub(crate) async fn planned_action_availability( account: &str, refusals: Value, ) -> Result { - tokio::time::timeout(Duration::from_secs(22), async { + tokio::time::timeout(CALL_BUDGET, async { let params = resolve_account( &json!({"accountId":account,"readOnly":true}), "imap.folders", @@ -442,7 +469,7 @@ pub(crate) async fn execute_planned_action( }) .collect::>()?; let mut completed = Vec::new(); - let result = tokio::time::timeout(Duration::from_secs(22), async { + let result = tokio::time::timeout(CALL_BUDGET, async { let params = resolve_account(params, method).await?; credentials(¶ms)?; mutation::call_planned( @@ -488,7 +515,7 @@ pub async fn call(method: &str, p: &Value) -> Result { } async fn call_inner(method: &str, p: &Value) -> Result { let sent = std::sync::atomic::AtomicBool::new(false); - match tokio::time::timeout(Duration::from_secs(22), async { + match tokio::time::timeout(CALL_BUDGET, async { let resolved = resolve_account(p, method).await?; execute(method, &resolved, &sent).await }) diff --git a/src/providers/imap/read/tests.rs b/src/providers/imap/read/tests.rs index c1243ab..824124c 100644 --- a/src/providers/imap/read/tests.rs +++ b/src/providers/imap/read/tests.rs @@ -260,3 +260,101 @@ async fn original_query_controls_are_rejected_before_connecting() { .is_err() ); } +/// A 10 000-message INBOX on a server that answers every command in three +/// seconds: nine replies, twenty-seven seconds, and nothing wrong with either +/// side. The call budget has to clear that, because what it exists to stop is a +/// call that never ends rather than one that is merely slow. Real seconds, on +/// purpose: the budget this replaces is a wall-clock 22 s, and there is no way +/// to spend more than that without spending it. +#[tokio::test] +async fn a_slow_server_that_answers_every_command_still_pages() { + const SLOW: Duration = Duration::from_secs(3); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + let peer = tokio::spawn(async move { + let (stream, _) = listener.accept().await.unwrap(); + let mut w: Wire = BufReader::new(Box::new(stream)); + write(&mut w, b"* OK ready\r\n").await.unwrap(); + let mut replies = 0u32; + loop { + let request = String::from_utf8(line(&mut w).await.unwrap()).unwrap(); + replies += 1; + // Well inside one command's budget, and well outside the call's + // old one once the conversation has run its length. + tokio::time::sleep(SLOW).await; + let mut response = String::new(); + let last = if request.starts_with("O1 LOGIN") { + response.push_str("O1 OK login\r\n"); + false + } else if request.starts_with("O1 CAPABILITY") { + response.push_str("* CAPABILITY IMAP4rev1\r\nO1 OK caps\r\n"); + false + } else if request.starts_with("O1 LIST") { + response.push_str("* LIST () \"/\" INBOX\r\nO1 OK folders\r\n"); + false + } else if request == "O1 SELECT \"INBOX\"\r\n" { + response.push_str("O1 OK selected\r\n"); + false + } else if let Some(fetch) = request.strip_prefix("O1 UID FETCH ") { + let (set, fields) = fetch.trim_end().split_once(' ').unwrap(); + let dated = fields.contains("INTERNALDATE"); + let ids: Vec = if set == "1:*" { + (1..=9000).collect() + } else { + set.split(',').map(|id| id.parse().unwrap()).collect() + }; + for uid in &ids { + response.push_str(&if dated { + format!("* {uid} FETCH (UID {uid} INTERNALDATE \"21-Sep-2026 12:00:00 +0000\")\r\n") + } else { + format!("* {uid} FETCH (UID {uid})\r\n") + }); + } + response.push_str("O1 OK fetched\r\n"); + // The snapshot, then one date window per 4 096 UIDs. + dated && ids.last() == Some(&9000) + } else { + panic!("unexpected command: {request}"); + }; + write(&mut w, response.as_bytes()).await.unwrap(); + if last { + break; + } + } + replies + }); + let mut p = params(port); + p["query"] = json!("folder:INBOX"); + p["limit"] = json!(3); + let listed = super::super::call("imap.list", &p).await.unwrap(); + assert_eq!( + listed["page"]["ids"], + json!(["9000:INBOX", "8999:INBOX", "8998:INBOX"]) + ); + let replies = peer.await.unwrap(); + assert!(replies >= 9, "the conversation has to be the long one"); + assert!( + SLOW * replies > Duration::from_secs(22), + "the test only means something while the conversation outlasts the budget it replaced" + ); +} +/// The other half of the same budget: a server that takes a command and then +/// says nothing is a hang, and it has to end the call long before the whole +/// conversation's allowance would. Thirty real seconds, for the reason above. +#[tokio::test] +async fn a_command_that_is_never_answered_ends_the_call() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + let peer = tokio::spawn(async move { + let (stream, _) = listener.accept().await.unwrap(); + let mut w: Wire = BufReader::new(Box::new(stream)); + write(&mut w, b"* OK ready\r\n").await.unwrap(); + assert!(line(&mut w).await.unwrap().starts_with(b"O1 LOGIN")); + tokio::time::sleep(Duration::from_secs(3600)).await; + }); + assert_eq!( + super::super::call("imap.list", ¶ms(port)).await, + Err("request_timed_out") + ); + peer.abort(); +} diff --git a/ui/backend/Backend.qml b/ui/backend/Backend.qml index 115e86d..b902a27 100644 --- a/ui/backend/Backend.qml +++ b/ui/backend/Backend.qml @@ -124,6 +124,12 @@ Item { var entry = queued.shift() var next = Object.assign({}, pending) var timeout = entry.operation === "agent.context" ? 65000 : 30000 + // An IMAP frame is a whole conversation — connect, login, SELECT, then + // one command per 4 096-UID window — on a server that charges about + // half a second per command, so it outlives the backend's own call + // budget (CALL_BUDGET in src/providers/imap/mod.rs) instead of killing + // the process while the server is still answering. + if (entry.operation.indexOf("imap.") === 0) timeout = 180000 entry.deadline = Date.now() + timeout next[entry.id] = entry pending = next diff --git a/ui/tests/test_backend_queue.js b/ui/tests/test_backend_queue.js index e50f406..5bfa406 100644 --- a/ui/tests/test_backend_queue.js +++ b/ui/tests/test_backend_queue.js @@ -232,6 +232,20 @@ for(const reason of ['cancel','append failure']) { reply(sent[0],{}) assert.deepStrictEqual(sent[64].params,{accountId:'one@example.org',message:{subject:'original'}}) } +// A slow IMAP server is not a hung backend: it charges about half a second for +// every command and a large mailbox needs dozens of them, so an imap frame has +// to outlive the backend's own call budget instead of being killed at the 30 s +// every other request gets. +{ + const {backend,sent}=bridge() + backend.call('imap.list',{accountId:'one@example.org'},()=>{}) + backend.call('providers.list',{},()=>{}) + const imap=backend.pending[sent[0].id].deadline-Date.now() + const other=backend.pending[sent[1].id].deadline-Date.now() + assert(imap>120000,'an imap frame outlives the backend call budget') + assert(other<=30000,'every other frame keeps the short deadline') + assert.strictEqual(sent.length,2) +} // Failure fanout is still complete if a receiver has been destroyed or throws. for(const shutdown of [false,true]) { const {backend,sent,reply}=bridge() From 9c7a042b609ec752065c6be6cb1cb9275cfc3f6f Mon Sep 17 00:00:00 2001 From: Kevin Verelst Date: Sun, 11 Oct 2026 10:38:28 +0200 Subject: [PATCH 12/88] Add a sender name for IMAP and Outlook mailboxes (#333) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IMAP and Outlook mailboxes sent mail with an empty From display name, so recipients saw only the bare address (or their client's guess from its local part). Gmail, JMAP and HEY report their own identities with names; IMAP and Outlook have no server that does, and Omamail had nowhere to keep one. This adds that place: a "Your name" for IMAP and Outlook mailboxes, asked for when the mailbox is added and editable afterwards in Settings → Mailboxes. The name is stored as a top-level `senderName` on the account entry in `accounts.json`, not inside `imap`, because Outlook's IMAP settings are rebuilt from the address on every save and would drop anything extra. Both send paths read it: the composer names the primary row of the send-as list with it (`Aliases.withSenderName`, so a rename shows in the composer at once), and the backend's `configured_identities()` names the primary identity for `mail.send`, so agent sends and composer sends agree. Aliases keep their own names. The UI and the backend clean the value the same way (control characters become a space, runs of spaces collapse, trimmed), so a hand-edited entry cannot become a second header line, and no stray character makes agent sends fail where composer sends succeed. Outgoing quoting and RFC 2047 encoding are unchanged. Because `mail.send` only names the sender from this revision on, this is backend API 7. Every UI surface — the Settings field, the setup-page field and the composer's naming — is gated on `apiVersion >= 7`; on an older backend the fields are hidden and the stored value is left unused rather than having the composer and agent sends disagree. The standalone app's `bundledApiVersion` moves from 5 to the released 6 so it accepts this checkout's API 7 backend (one step past the pin); left at 5, `make app-run` — and a standalone build of the next release — refused its own backend as incompatible. While here, the two names on these pages are told apart: the account label is now "Mailbox name" (it never leaves Omamail) and the new field is "Your name", with the hint "shown to recipients as the sender". These follow what other mail clients call the same fields (Thunderbird and Outlook ask for "Your name"). The label field's behaviour is unchanged. `ui/Service.qml` sits at the 128 KiB source cap, so making room was part of the change: the six account setters now share a `commitAccounts()` helper, `configureAccount` copies its fields through one key list, and the setup page's `accountName` / `accountAddress` / new `accountSenderName` share an `editingEntry()` lookup. No behaviour changes in those refactors; `tst_setup_name_field` and the existing account tests cover them. ## Verification `make validate` green on this commit (Rust unit and integration suites, JS, 1170 QML tests, source regressions, qmllint). `python3 tests/test_backend_api.py` passes the API 7 contract, including the new unreleased case "mail send dry run names the IMAP sender"; the released view was checked against an upstream-main build at API 6. The released-view gate against the actual published 0.10.8 binary was not run locally; CI runs it. New tests. The Rust test, the API contract case and the composer test were each watched failing with the fix removed; the rest assert fields and storage that do not exist without the change: - `src/backend/mail.rs` `configured_identities_name_the_primary_address_with_the_sender_name`: the primary identity carries the trimmed, single-line name; aliases keep theirs. - `ui/tests/qml/tst_composer_sender_name.qml`: IMAP and Outlook name the composer's primary row from API 7 (not on 6, kept on 8, dropped when disconnected); Gmail keeps its server's name. Fails with the API gate removed. - `ui/tests/qml/tst_service_backend_path.qml`: `backendCanNameSender` is fixed at API 7 across releases. - `ui/tests/qml/tst_settings_name.qml` and `tst_setup_name_field.qml`: the Settings and setup fields show the stored name, save on blur/Enter/save/sign-in, are absent for server-named mailboxes and on older backends, and leave the stored name alone when hidden. - `ui/tests/test_accounts.js`, `test_aliases.js`: storage, cleaning and alias naming. By hand, in the Omarchy plugin on a real IMAP mailbox: set the name in Settings → Mailboxes, sent a message, and the recipient saw the name beside the address; then checked the setup-page field and the renamed labels. Screenshots below are the standalone app (`make app-run` from `main` and from this branch) on a throwaway home with synthetic accounts, 1376×850 at scale 1, in tokyo-night (dark) and flexoki-light (light). A fresh agent reviewed the diff against `AGENTS.md`. Security: PASS for the From header / outgoing MIME, persisted settings and the backend API boundary. Its findings, all addressed in this commit: the composer named the sender on an API 6 backend while the field was hidden (now gated on API 7, and the doc says so); no QML test exercised the API gate or the composer path (added above); the backend only trimmed a hand-edited name, so a control character made agent sends fail where composer sends succeeded (now cleaned like the UI). It also noted, unverified, that Exchange may replace the display name with the directory name for work accounts sending through Graph. ### Screenshots | | Before | After | | --- | --- | --- | | IMAP setup, dark |imap-tokyo-night-before | imap-tokyo-night-after | | IMAP setup, light | imap-flexoki-light-before | imap-flexoki-light-after | | Outlook setup, dark | outlook-tokyo-night-before | outlook-tokyo-night-after | | Outlook setup, light | outlook-flexoki-light-before | outlook-flexoki-light-after | | Settings → Mailboxes, dark | settings-tokyo-night-before | settings-tokyo-night-after | | Settings → Mailboxes, light | settings-flexoki-light-before | settings-flexoki-light-after | Visible differences: the setup pages gain "Your name (optional) — shown to recipients as the sender" (IMAP: between the mailbox name and the address; Outlook: above the address), and the IMAP mailbox name placeholder reads "Mailbox name (optional) — how it is listed in Omamail". In Settings → Mailboxes, "Name" becomes "Mailbox name", and an IMAP or Outlook mailbox gets a "Your name" field with its explanation. ## Release Notes - IMAP and Outlook mailboxes can now send with your name beside the address: set "Your name" when adding the mailbox or in Settings → Mailboxes. - The mailbox's own label is now called "Mailbox name", to tell it apart from the name recipients see. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016Fmayb5WTWTWzhCw7TRUKx ## Maintainer review validation Reviewed `e05d96b` on macOS/Qt 6.11.2. API 7 contract and released API 6 view against the checksum-verified actual v0.10.8 macOS standalone backend pass. JS/lint/native resource checks pass; shared QML: 1156 passed, 0 failed, 14 skipped. A 22-case synthetic IMAP/Outlook API/MIME probe found no extra headers, unintended recipients or file writes. `make test-app-qml` fails at app/tests/qml/tst_standalone_composition.qml:302: it still expects baseline API 5 after this PR changes it to 6 (29 passed, 1 failed). Update the assertion. Composer From-row and shared Gmail/JMAP setup-label screenshots are needed. Product acceptance is pending. Full local validation remains incomplete: selecting Python 3.14 fixes the unchanged runtime fixture failures from the worktree shell using Python 3.9, but Linux-only public_http tests cannot run on macOS without os.pidfd_open. No required remote CI checks are reported. ## Maintainer supplemental screenshot evidence Six matching production-component captures cover Gmail setup, JMAP setup and the composer From popup at 1376×850, scale 1, with the same Tokyo Night palette and synthetic data. Shared setup copy distinguishes Mailbox name from sender name; the composer primary identity subtitle changes from the mailbox label Work to Ada Lovelace, while alias labels and addresses remain unchanged. Production components use standalone UI compatibility adapters; these are controlled component renders rather than installed-desktop or real-account screenshots. Captured UI corresponds to reviewed head `aa3ecb7`. **COMPOSEVIEW AFTER** ![ComposeView-after](https://github.com/user-attachments/assets/ff630a98-3006-47b8-875f-f1df5b2fd4ff) **COMPOSEVIEW BEFORE** ![ComposeView-before](https://github.com/user-attachments/assets/5887499d-9cb1-4470-8537-0cbb4ac26210) **JMAPSETUPPAGE AFTER** ![JmapSetupPage-after](https://github.com/user-attachments/assets/1e4e63ad-7975-4a24-8927-cd558bee5e0e) **JMAPSETUPPAGE BEFORE** ![JmapSetupPage-before](https://github.com/user-attachments/assets/cf6f87b4-dc19-4bdb-a3b5-930fa40b7177) **SETUPPAGE AFTER** ![SetupPage-after](https://github.com/user-attachments/assets/e71f25a2-767a-4043-bbad-c22a3212ab27) **SETUPPAGE BEFORE** ![SetupPage-before](https://github.com/user-attachments/assets/e17ccd40-ba8f-4e29-b711-397ea6e01221) --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Jason Lee --- Makefile | 1 + app/src/resource_check.cpp | 2 +- app/tests/native/tst_resources.cpp | 4 +- app/tests/qml/tst_standalone_composition.qml | 2 +- backend-api.json | 30 +++++- docs/BACKEND-RUNTIME.md | 4 + src/backend/mail.rs | 99 ++++++++++++++++++-- src/backend/mod.rs | 2 +- tests/test_backend_api.py | 2 + ui/Service.qml | 72 ++++++-------- ui/account/Accounts.js | 30 ++++++ ui/account/Aliases.js | 23 +++++ ui/account/MailAccount.qml | 15 ++- ui/components/AccountNameField.qml | 2 +- ui/components/ImapSetupPage.qml | 17 +++- ui/components/OutlookSetupPage.qml | 14 ++- ui/components/SenderNameField.qml | 24 +++++ ui/components/SettingsPage.qml | 76 +++++++++++++-- ui/tests/qml/tst_composer_sender_name.qml | 74 +++++++++++++++ ui/tests/qml/tst_service_backend_path.qml | 17 ++++ ui/tests/qml/tst_settings_name.qml | 86 ++++++++++++++++- ui/tests/qml/tst_setup_name_field.qml | 96 +++++++++++++++++++ ui/tests/test_accounts.js | 35 +++++++ ui/tests/test_aliases.js | 18 ++++ 24 files changed, 669 insertions(+), 76 deletions(-) create mode 100644 ui/components/SenderNameField.qml create mode 100644 ui/tests/qml/tst_composer_sender_name.qml diff --git a/Makefile b/Makefile index 8d05989..bdf4b83 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryContr ui/components/InviteCard.qml \ ui/components/SwitcherSearch.qml \ ui/components/AccountNameField.qml \ + ui/components/SenderNameField.qml \ ui/components/ReaderBlankSlate.qml \ ui/components/ReaderSkeleton.qml \ ui/components/ComposeView.qml \ diff --git a/app/src/resource_check.cpp b/app/src/resource_check.cpp index 48af195..b5f3e7b 100644 --- a/app/src/resource_check.cpp +++ b/app/src/resource_check.cpp @@ -365,7 +365,7 @@ bool runSmokeTest(const ResourcePaths &paths, const QString &readyFile, QString || !protocol.isDouble() || protocol.toInt() != 1 || !apiVersion.isDouble() || apiVersion.toDouble() != static_cast(apiVersion.toInt()) - || apiVersion.toInt() != 6) { + || apiVersion.toInt() != 7) { if (error) *error = QStringLiteral("Bundled backend system.info does not match this app"); stopProcess(backend); return false; diff --git a/app/tests/native/tst_resources.cpp b/app/tests/native/tst_resources.cpp index 2503753..ba9c30a 100644 --- a/app/tests/native/tst_resources.cpp +++ b/app/tests/native/tst_resources.cpp @@ -164,7 +164,7 @@ void ResourcesTest::smokeTestLoadsQmlHandshakesAndWritesReadyFile() QVERIFY(ready.open(QIODevice::ReadOnly)); const QJsonObject value = QJsonDocument::fromJson(ready.readAll()).object(); QCOMPARE(value.value(QStringLiteral("version")).toString(), QStringLiteral(OMAMAIL_APP_VERSION)); - QCOMPARE(value.value(QStringLiteral("apiVersion")).toInt(), 6); + QCOMPARE(value.value(QStringLiteral("apiVersion")).toInt(), 7); } void ResourcesTest::smokeRejectsMismatchedBackendApi() @@ -344,7 +344,7 @@ int main(int argc, char *argv[]) result = {{QStringLiteral("name"), QStringLiteral("omamail")}, {QStringLiteral("version"), QStringLiteral(OMAMAIL_APP_VERSION)}, {QStringLiteral("protocol"), 1}, - {QStringLiteral("apiVersion"), fixture == QByteArrayLiteral("stale-api") ? 5 : 6}}; + {QStringLiteral("apiVersion"), fixture == QByteArrayLiteral("stale-api") ? 6 : 7}}; } else { result = {{QStringLiteral("quitReady"), true}}; } diff --git a/app/tests/qml/tst_standalone_composition.qml b/app/tests/qml/tst_standalone_composition.qml index c62b14e..a8d4ce2 100644 --- a/app/tests/qml/tst_standalone_composition.qml +++ b/app/tests/qml/tst_standalone_composition.qml @@ -300,7 +300,7 @@ TestCase { compare(composition.service.settings.maxMessages, 17) compare(composition.service.notifyNewMail, false) compare(composition.service.backendRuntime.bundled, true) - compare(composition.service.backendRuntime.requiredApiVersion, 5, + compare(composition.service.backendRuntime.requiredApiVersion, 6, "the bundled handshake must require this checkout's API") compare(composition.service.backendRuntime.canInstall, false) compare(composition.service.backendRuntime.executable, host.backendPath) diff --git a/backend-api.json b/backend-api.json index 69783b8..aa043c5 100644 --- a/backend-api.json +++ b/backend-api.json @@ -1,5 +1,5 @@ { - "apiVersion": 6, + "apiVersion": 7, "releasedApiVersion": 6, "protocolVersion": 1, "methods": [ @@ -527,6 +527,30 @@ "attachments": [] } }, + { + "name": "mail send dry run names the IMAP sender", + "method": "mail.send", + "params": { + "account": "imap:named@example.org", + "to": [ + "recipient@example.org" + ], + "subject": "Contract preview", + "body": "Hello" + }, + "equals": { + "dryRun": true, + "executed": false, + "accountId": "imap:named@example.org", + "from": "Jane Example ", + "to": [ + "recipient@example.org" + ], + "subject": "Contract preview", + "body": "Hello", + "attachments": [] + } + }, { "name": "mail list rejects unknown fields", "method": "mail.list", @@ -1267,6 +1291,8 @@ ], "unreleased": { "methods": [], - "cases": [] + "cases": [ + "mail send dry run names the IMAP sender" + ] } } diff --git a/docs/BACKEND-RUNTIME.md b/docs/BACKEND-RUNTIME.md index e218e70..47266a5 100644 --- a/docs/BACKEND-RUNTIME.md +++ b/docs/BACKEND-RUNTIME.md @@ -102,6 +102,10 @@ The isolated provider launch preserves model/authentication configuration while Proposal cards show their saved From, To and optional Cc, Bcc and Reply-To. If routing in the corresponding open composer changes, direct card sending is disabled and rechecked at dispatch. Use this version applies subject/body without replacing the edited recipients; the normal composer Send then reviews and sends the current routing. +## API 7 sender name for IMAP and Outlook + +An IMAP or Outlook account entry may carry a top-level `senderName`. `mail.send` uses it, trimmed, as the display name of the mailbox's own address in the From header; aliases keep their own names. An empty or missing value sends the bare address, as before. Starting at API 7 the UI offers the name as "Your name" in Settings → Mailboxes and on the IMAP and Outlook setup pages, and the composer names the From address with it. On an older backend the fields are hidden and the composer leaves the stored value unused, so composer and agent sends both carry the bare address rather than disagreeing. + ## API 6 account-wide IMAP search IMAP and Outlook text searches, and IMAP address searches, resolve to `search:`. IMAP All mail resolves to `search:ALL`; folder queries remain folder-scoped. The IMAP UI keeps Archive on API 5 and exposes All mail starting at API 6. diff --git a/src/backend/mail.rs b/src/backend/mail.rs index 3eeb1d9..4317990 100644 --- a/src/backend/mail.rs +++ b/src/backend/mail.rs @@ -7,6 +7,29 @@ use std::{future::Future, pin::Pin}; #[path = "mail_action_tests.rs"] mod action_tests; +// The send-as list of a mailbox whose identities live in its account entry +// rather than on a server: the mailbox's own address first, under the sender +// name the account was given, then its configured aliases. The name is the +// account's, not the server settings', because nothing a server says sets it. +fn configured_identities(settings: &Value) -> Result { + let email = settings["email"].as_str().filter(|value| !value.is_empty()).or_else(|| settings["imap"]["username"].as_str()).ok_or("mail_send_sender_unavailable")?; + let name = sender_name(settings["senderName"].as_str().unwrap_or("")); + let aliases = settings["imap"]["aliases"].as_array().cloned().unwrap_or_default(); + if aliases.len() > 1024 { return Err("mail_send_identities_invalid"); } + let default = aliases.iter().any(|alias| alias["isDefault"] == true); + let mut rows = vec![json!({"email":email,"displayName":name,"isPrimary":true,"isDefault":!default})]; + rows.extend(aliases); + Ok(json!(rows)) +} + +// One header line, cleaned the way `Accounts.senderNameText` cleans it, so a +// hand-edited entry names agent sends exactly as it names composer sends +// instead of failing them. +fn sender_name(raw: &str) -> String { + let line: String = raw.chars().map(|c| if c.is_ascii_control() { ' ' } else { c }).collect(); + line.split(' ').filter(|word| !word.is_empty()).collect::>().join(" ").trim().to_string() +} + struct ProviderList<'a> { session: &'a Session, } @@ -44,13 +67,7 @@ impl crate::mail::send::IdentityLookup for ProviderIdentities<'_> { let account = account.clone(); tokio::task::spawn_blocking(move || { let settings = crate::auth::settings_readonly(account.provider.id(), &account.id)?; - let email = settings["email"].as_str().filter(|value| !value.is_empty()).or_else(|| settings["imap"]["username"].as_str()).ok_or("mail_send_sender_unavailable")?; - let aliases = settings["imap"]["aliases"].as_array().cloned().unwrap_or_default(); - if aliases.len() > 1024 { return Err("mail_send_identities_invalid"); } - let default = aliases.iter().any(|alias| alias["isDefault"] == true); - let mut rows = vec![json!({"email":email,"displayName":"","isPrimary":true,"isDefault":!default})]; - rows.extend(aliases); - Ok(json!(rows)) + configured_identities(&settings) }).await.map_err(|_| "worker_failed")? } } @@ -614,6 +631,74 @@ mod tests { sync::{Arc, Mutex}, }; + #[test] + fn configured_identities_name_the_primary_address_with_the_sender_name() { + let rows = configured_identities(&json!({"email":"me@example.org","senderName":" Jane Example ", + "imap":{"aliases":[{"email":"alias@example.org","displayName":"Alias","isDefault":true}]}})).unwrap(); + assert_eq!(rows, json!([ + {"email":"me@example.org","displayName":"Jane Example","isPrimary":true,"isDefault":false}, + {"email":"alias@example.org","displayName":"Alias","isDefault":true}])); + let unnamed = configured_identities(&json!({"imap":{"username":"me@example.org"}})).unwrap(); + assert_eq!(unnamed, json!([{"email":"me@example.org","displayName":"","isPrimary":true,"isDefault":true}])); + let edited = configured_identities(&json!({"email":"me@example.org","senderName":"Jane\r\nBcc: x@example.org\u{7f}\tExample"})).unwrap(); + assert_eq!(edited[0]["displayName"], "Jane Bcc: x@example.org Example", "one header line, as the UI stores it"); + } + + #[test] + fn configured_sender_names_cannot_change_the_mime_envelope() { + use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; + use mailparse::MailHeaderMap; + for provider in [crate::mail::Provider::Imap, crate::mail::Provider::Outlook] { + for name in [ + "Jane\r\nBcc: attacker@example.org", "Jane\rBcc: attacker@example.org", + "Jane\nBcc: attacker@example.org", "Jane\0Bcc: attacker@example.org", + "Jane\tExample\u{7f}", "田中 太郎", "Jane \"Quoted\" \\ Example", + "Jane\r\n", "Jane\n", "Jane\0", + ] { + let rows = configured_identities(&json!({"email":"me@example.org","senderName":name, + "imap":{"aliases":[{"email":"alias@example.org","displayName":"Alias \"名\" \\","isDefault":false}]}})).unwrap(); + for from in ["me@example.org", "alias@example.org"] { + let request = crate::mail::SendRequest { + account: crate::mail::Account { id: format!("{}:me@example.org", provider.id()), provider }, + from: from.into(), to: vec!["recipient@example.org".into()], cc: vec![], bcc: vec![], + subject: "Synthetic preview".into(), body: "Line one\nLine two\n".into(), + attachments: vec![], execute: false, send_id: None, + }; + let prepared = crate::mail::send::prepare(&request, &rows).unwrap(); + assert_eq!(prepared.preview()["executed"], false); + assert_eq!(prepared.preview()["to"], json!(["recipient@example.org"])); + let payload = prepared.payload(0, "synthetic-name-boundary").unwrap(); + let bytes = URL_SAFE_NO_PAD.decode(payload["raw"].as_str().unwrap()).unwrap(); + let parsed = mailparse::parse_mail(&bytes).unwrap(); + assert_eq!(parsed.headers.get_all_values("From").len(), 1, "{provider:?}: {name:?}"); + assert_eq!(parsed.headers.get_all_values("To"), ["recipient@example.org"]); + assert!(parsed.headers.get_all_values("Bcc").is_empty(), "{name:?}"); + assert!(parsed.headers.get_all_values("Cc").is_empty()); + let addresses = mailparse::addrparse(&parsed.headers.get_first_value("From").unwrap()).unwrap(); + assert_eq!(addresses.len(), 1); + match &addresses[0] { + mailparse::MailAddr::Single(address) => assert_eq!(address.addr, from), + _ => panic!("sender name created a group"), + } + assert_eq!(parsed.get_body().unwrap(), "Line one\nLine two\n"); + } + } + // Aliases keep their own names: malformed alias metadata must be + // refused before any payload exists, rather than being cleaned as + // though it were the account's primary sender name. + for name in ["Alias\r\nBcc: attacker@example.org", "Alias\0", "Alias\n", "=?UTF-8?B?WA==?="] { + let rows = configured_identities(&json!({"email":"me@example.org","senderName":"Safe", + "imap":{"aliases":[{"email":"alias@example.org","displayName":name,"isDefault":true}]}})).unwrap(); + let request = crate::mail::SendRequest { + account: crate::mail::Account { id: format!("{}:me@example.org", provider.id()), provider }, + from: "alias@example.org".into(), to: vec!["recipient@example.org".into()], cc: vec![], bcc: vec![], + subject: "Synthetic preview".into(), body: "Body".into(), attachments: vec![], execute: false, send_id: None, + }; + assert!(crate::mail::send::prepare(&request, &rows).is_err(), "{name:?}"); + } + } + } + struct ReaderProjectionAdapter { opened: Value, calls: Arc>>, diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 7e91dd4..ec4d192 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -479,7 +479,7 @@ pub fn dispatch(method: &str, params: &Value) -> Result { match method { "system.info" => Ok(json!({ "name": "omamail", "version": env!("CARGO_PKG_VERSION"), - "protocol": 1, "apiVersion": 6, "methods": methods::available(), + "protocol": 1, "apiVersion": 7, "methods": methods::available(), "capabilities": {"agent": cfg!(all(feature = "agent", unix))} })), "system.quit" => Ok(json!({"quitReady": true})), diff --git a/tests/test_backend_api.py b/tests/test_backend_api.py index d8e920c..911e2d4 100644 --- a/tests/test_backend_api.py +++ b/tests/test_backend_api.py @@ -324,6 +324,8 @@ def main(): {'email': 'contract@example.org'}, {'provider': 'imap', 'email': 'sender@example.org', 'imap': {'username': 'sender@example.org'}}, + {'provider': 'imap', 'email': 'named@example.org', 'senderName': 'Jane Example', + 'imap': {'username': 'named@example.org'}}, {'provider': 'hey', 'email': 'sender@example.org'}, {'provider': 'outlook', 'email': 'sender@example.org'}] registry.write_text(json.dumps({'version': 1, 'activeId': 'contract@example.org', diff --git a/ui/Service.qml b/ui/Service.qml index d1f9eae..6c1f6d9 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -59,7 +59,7 @@ Item { pluginDir: root.pluginDir bundledExecutable: root.standalone ? String(root.platform.backendPath || "") : "" bundledVersion: root.standalone ? root.version : "" - bundledApiVersion: root.standalone ? 5 : 0 + bundledApiVersion: root.standalone ? 6 : 0 bundledMode: root.standalone developmentExecutable: root.standalone ? "" : (Quickshell.env("OMAMAIL_BIN") || "") onValidated: Qt.callLater(rustBackend.reconcileProcess) @@ -104,6 +104,8 @@ Item { readonly property bool backendCanCheckMicrosoftConnection: backend.ready && backend.apiVersion >= 5 readonly property bool backendCanDiscoverCalendars: backend.ready && backend.apiVersion >= 5 readonly property bool backendCanGoogleCalendars: backend.ready && backend.apiVersion >= 6 + // Older backends send the bare address. + readonly property bool backendCanNameSender: backend.ready && backend.apiVersion >= 7 readonly property bool calendarRemindersEnabled: !settings || settings.calendarRemindersEnabled !== false readonly property int calendarSnoozeMinutes: Math.max(1, Math.min(1440, Math.floor(Number(settings && settings.calendarSnoozeMinutes) || 5))) @@ -1014,13 +1016,9 @@ Item { var entry = {} for (var key in accounts[index]) entry[key] = accounts[index][key] - if (raw.provider !== undefined) entry.provider = raw.provider - if (raw.email !== undefined) entry.email = raw.email - if (raw.clientId !== undefined) entry.clientId = raw.clientId - if (raw.clientSecret !== undefined) entry.clientSecret = raw.clientSecret - if (raw.imap !== undefined) entry.imap = raw.imap - if (raw.jmap !== undefined) entry.jmap = raw.jmap - if (raw.label !== undefined) entry.label = raw.label + var keys = ["provider", "email", "clientId", "clientSecret", "imap", "jmap", "label", "senderName"] + for (var k = 0; k < keys.length; k++) + if (raw[keys[k]] !== undefined) entry[keys[k]] = raw[keys[k]] // Never rebuild through `add`: a colliding id replaces the *other* row // and drops this one, which is how re-authing Proton deleted iCloud. @@ -1079,11 +1077,8 @@ Item { function toggleMonitored(labelId, accountId) { var owner = labelOwner(accountId) if (!owner) return - var next = Accounts.toggleMonitored(accountList, owner.accountId, labelId) - if (Accounts.serialize(next) === Accounts.serialize(accountList)) return - accountList = next - saveAccounts() - owner.labelActions.refreshMonitored() + if (commitAccounts(Accounts.toggleMonitored(accountList, owner.accountId, labelId))) + owner.labelActions.refreshMonitored() } // The watched ids an account's rename or move left behind, written to its @@ -1091,32 +1086,21 @@ Item { function setMonitoredIds(index, ids) { var account = accountAt(index) if (!account) return - var next = Accounts.setMonitored(accountList, account.accountId, ids) - if (Accounts.serialize(next) === Accounts.serialize(accountList)) return - accountList = next - saveAccounts() - } - - function setAccountLabel(id, text) { - var next = Accounts.setLabel(accountList, id, text) - if (Accounts.serialize(next) === Accounts.serialize(accountList)) return - accountList = next - saveAccounts() + commitAccounts(Accounts.setMonitored(accountList, account.accountId, ids)) } - function setAccountSignatureHtml(id, html) { - var next = Accounts.setSignatureHtml(accountList, id, html) - if (Accounts.serialize(next) === Accounts.serialize(accountList)) return + // Writes only when the edit changed something. + function commitAccounts(next) { + if (Accounts.serialize(next) === Accounts.serialize(accountList)) return false accountList = next saveAccounts() + return true } - function setAccountSignature(id, text) { - var next = Accounts.setSignature(accountList, id, text) - if (Accounts.serialize(next) === Accounts.serialize(accountList)) return - accountList = next - saveAccounts() - } + function setAccountLabel(id, text) { commitAccounts(Accounts.setLabel(accountList, id, text)) } + function setAccountSenderName(id, text) { commitAccounts(Accounts.setSenderName(accountList, id, text)) } + function setAccountSignatureHtml(id, html) { commitAccounts(Accounts.setSignatureHtml(accountList, id, html)) } + function setAccountSignature(id, text) { commitAccounts(Accounts.setSignature(accountList, id, text)) } function saveAccounts(opts) { if (!accountsLoaded) return @@ -1445,6 +1429,8 @@ Item { // The name as it was typed, empty when none was, so a field editing // it shows what is there rather than the address standing in for it. label: String(accounts[i].label || ""), + provider: String(accounts[i].provider || ""), + senderName: String(accounts[i].senderName || ""), signature: String(accounts[i].signature || ""), signatureHtml: String(accounts[i].signatureHtml || "") }) @@ -1752,20 +1738,17 @@ Item { root.sendIdentities = result.identities || [] }) } - // The name the entry being edited was given, or "" for none: what the - // setup page's name field shows. `accountLabel` cannot say, because it - // falls through to the address's local part. - readonly property string accountName: { - var accounts = accountList ? accountList.accounts : [] - var index = editingIndex() - return index >= 0 && index < accounts.length ? String(accounts[index].label || "") : "" - } - - readonly property string accountAddress: { + // The name the entry being edited was given, and the rest the setup page + // shows for it, "" for none. `accountLabel` cannot say, because it falls + // through to the address's local part. + function editingEntry() { var accounts = accountList ? accountList.accounts : [] var index = editingIndex() - return index >= 0 && index < accounts.length ? String(accounts[index].email || "") : "" + return index >= 0 && index < accounts.length ? accounts[index] : ({}) } + readonly property string accountName: String(editingEntry().label || "") + readonly property string accountSenderName: String(editingEntry().senderName || "") + readonly property string accountAddress: String(editingEntry().email || "") readonly property int inboxUnread: unified ? Number(unifiedSnapshot.totalUnread || 0) : (current ? current.inboxUnread : 0) readonly property var messages: unified @@ -2681,6 +2664,7 @@ Item { providerId: entry ? entry.provider : Provider.DEFAULT_ID imapSettings: entry ? entry.imap : null jmapSettings: entry ? entry.jmap : null + senderName: entry ? String(entry.senderName || "") : "" // Only a Gmail account has a client-keyed refresh token to inherit, and // only the first one may claim it. mayAdoptLegacyToken: index === 0 && (!entry || entry.provider === "gmail") diff --git a/ui/account/Accounts.js b/ui/account/Accounts.js index dc796f5..44432b4 100644 --- a/ui/account/Accounts.js +++ b/ui/account/Accounts.js @@ -212,6 +212,10 @@ function makeAccount(account) { imap: makeImapSettings(raw.imap), jmap: makeJmapSettings(raw.jmap), label: trimmed(raw.label), + // The name recipients see beside this mailbox's own address. Only the + // mailboxes `namesSender` admits read it; the others take theirs from + // the server. + senderName: senderNameText(raw.senderName), signature: trimmed(raw.signature), // The signature as markup, imported from a file and rebuilt by // `Signature.js` before it is stored: never the file's own bytes. Sent as @@ -553,6 +557,32 @@ function setLabel(list, id, text) { return next } +// Whether this mailbox's sender name is ours to set. IMAP and Outlook have no +// server that names the sender, so the account entry is the only place a name +// can come from; Gmail, JMAP and HEY report their own identities with names. +// The backend makes the same distinction when it builds the From header. +function namesSender(provider) { + var id = String(provider || "") + return id === "imap" || id === "outlook" +} + +// One header line, so a pasted line break or tab becomes a space instead of a +// name the send would refuse. +function senderNameText(value) { + return trimmed(String(value === undefined || value === null ? "" : value) + .replace(/[\u0000-\u001f\u007f]+/g, " ").replace(/ {2,}/g, " ")) +} + +function setSenderName(list, id, text) { + var next = copyList(list) + var at = indexOfId(next.accounts, id) + if (at < 0) return next + var entry = makeAccount(next.accounts[at]) + entry.senderName = senderNameText(text) + next.accounts[at] = entry + return next +} + // The sign-off belongs to the mailbox rather than to the window. Two accounts // are two identities, and one signature under both is wrong for whichever it // was not written for — so it sits beside the label, which is the other thing diff --git a/ui/account/Aliases.js b/ui/account/Aliases.js index 76ccf01..13c6579 100644 --- a/ui/account/Aliases.js +++ b/ui/account/Aliases.js @@ -224,3 +224,26 @@ function sendAsList(address, aliases) { } return list } + +// The same list with the mailbox's own address under the account's sender +// name, which is what the backend writes into the From header for it. Only +// the primary row: an alias was given its own name, or none, where it was +// typed. A copy, so a list a provider handed over is not edited in place. +function withSenderName(rows, name) { + var list = Array.isArray(rows) ? rows : [] + var wanted = trimmed(name) + if (wanted === "") return list + var out = [] + for (var i = 0; i < list.length; i++) { + var row = list[i] + if (row && row.isPrimary === true) { + var named = {} + for (var key in row) named[key] = row[key] + named.displayName = wanted + out.push(named) + } else { + out.push(row) + } + } + return out +} diff --git a/ui/account/MailAccount.qml b/ui/account/MailAccount.qml index 6137c71..036189d 100644 --- a/ui/account/MailAccount.qml +++ b/ui/account/MailAccount.qml @@ -12,6 +12,7 @@ import "../message/Unsubscribe.js" as Unsub import "../message/Outbox.js" as Outbox import "Model.js" as Model import "Accounts.js" as Accounts +import "Aliases.js" as Aliases import "../providers/Registry.js" as Provider import "../providers/ImapProtocol.js" as Imap import "../providers/OAuth.js" as OAuth @@ -52,6 +53,8 @@ Item { // Server settings for an IMAP account, straight off the account entry. Unused // by the others, and normalised before anything can dial one. property var imapSettings: null + // The account's sender name, for the providers whose From name it sets. + property string senderName: "" // The same for a JMAP account, and the same rule: what is on the entry is // what a hand edit could have written, so it is normalised before anything // sends a credential to it. @@ -397,9 +400,15 @@ Item { property var profile: null readonly property string accountEmail: profile ? String(profile.email || "") : "" readonly property var availableSendAsAliases: { - if (sendAsAliases.length > 0) return sendAsAliases - if (accountEmail === "") return [] - return [{ email: accountEmail, displayName: "", isPrimary: true, isDefault: true }] + var rows = sendAsAliases.length > 0 ? sendAsAliases + : (accountEmail === "" ? [] + : [{ email: accountEmail, displayName: "", isPrimary: true, isDefault: true }]) + // Named here rather than in the provider's own list so that a rename + // shows in the composer at once, without reloading the send-as list. + // Only on a backend that names agent sends too (API 7), so the composer + // and `mail.send` never disagree about who a message is from. + var named = Accounts.namesSender(providerId) && !!backend && backend.ready && backend.apiVersion >= 7 + return named ? Aliases.withSenderName(rows, senderName) : rows } // The address this mailbox answers as when nothing more specific applies. // The profile is authoritative once it has loaded; until then the address the diff --git a/ui/components/AccountNameField.qml b/ui/components/AccountNameField.qml index 71fade8..0b586d6 100644 --- a/ui/components/AccountNameField.qml +++ b/ui/components/AccountNameField.qml @@ -12,7 +12,7 @@ TextField { property var service: null objectName: "account-name-field" - placeholderText: "Name (optional) — how this mailbox is listed, e.g. Work" + placeholderText: "Mailbox name (optional) — how it is listed in Omamail, e.g. Work" function syncFromStore() { text = service ? String(service.accountName || "") : "" } function value() { return String(text || "").trim() } diff --git a/ui/components/ImapSetupPage.qml b/ui/components/ImapSetupPage.qml index 75449e2..9f9c979 100644 --- a/ui/components/ImapSetupPage.qml +++ b/ui/components/ImapSetupPage.qml @@ -70,6 +70,7 @@ Column { function syncFromStore() { nameField.syncFromStore() + senderField.syncFromStore() if (!auth || !auth.settings) return var settings = auth.settings addressField.text = service ? service.accountAddress : "" @@ -127,7 +128,8 @@ Column { provider: "imap", email: address, imap: check.settings, - label: nameField.value() + label: nameField.value(), + senderName: senderField.value() }) } @@ -145,7 +147,8 @@ Column { provider: "imap", email: address, imap: check.settings, - label: nameField.value() + label: nameField.value(), + senderName: senderField.value() }, passwordField.text) } @@ -223,6 +226,16 @@ Column { foreground: root.textColor font.family: root.panelFontFamily font.pixelSize: Style.font.bodySmall + onAccepted: (senderField.available ? senderField : addressField).forceActiveFocus() + } + + SenderNameField { + id: senderField + service: root.service + width: parent.width + foreground: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall onAccepted: addressField.forceActiveFocus() } diff --git a/ui/components/OutlookSetupPage.qml b/ui/components/OutlookSetupPage.qml index a4defd9..db96ece 100644 --- a/ui/components/OutlookSetupPage.qml +++ b/ui/components/OutlookSetupPage.qml @@ -68,7 +68,8 @@ Column { email: address, clientId: clientId, clientSecret: "", - imap: imap + imap: imap, + senderName: senderField.value() }) } @@ -113,6 +114,7 @@ Column { function syncFromStore() { if (!service) return + senderField.syncFromStore() addressField.text = String(service.accountAddress || "") if (auth && auth.configuredClientId) clientIdField.text = String(auth.configuredClientId) @@ -185,6 +187,16 @@ Column { width: parent.width spacing: Style.space(10) + SenderNameField { + id: senderField + service: root.service + width: parent.width + foreground: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + onAccepted: addressField.forceActiveFocus() + } + TextField { id: addressField objectName: "outlook-address-field" diff --git a/ui/components/SenderNameField.qml b/ui/components/SenderNameField.qml new file mode 100644 index 0000000..de8f4a9 --- /dev/null +++ b/ui/components/SenderNameField.qml @@ -0,0 +1,24 @@ +import QtQuick +import qs.Commons +import qs.Ui + +// The name recipients see beside this mailbox's address, asked for where the +// mailbox is set up so its first message is not sent nameless. Only on the +// pages for mailboxes whose server does not name the sender +// (`Accounts.namesSender`), and only on a backend that writes it. +TextField { + id: root + + property var service: null + readonly property bool available: !!service && !!service.backendCanNameSender + + objectName: "sender-name-field" + visible: available + placeholderText: "Your name (optional) — shown to recipients as the sender" + + function syncFromStore() { text = service ? String(service.accountSenderName || "") : "" } + // Left out of the form while unavailable, so the entry keeps what it has. + function value() { return available ? String(text || "").trim() : undefined } + + Component.onCompleted: syncFromStore() +} diff --git a/ui/components/SettingsPage.qml b/ui/components/SettingsPage.qml index 130876f..167abd0 100644 --- a/ui/components/SettingsPage.qml +++ b/ui/components/SettingsPage.qml @@ -5,6 +5,7 @@ import qs.Ui import "../message/Direction.js" as Direction import "../settings/Appearance.js" as Appearance import "../message/Html.js" as Html +import "../account/Accounts.js" as Accounts // Where mailboxes are managed. // @@ -88,25 +89,43 @@ Column { service.setAccountLabel(selectedNameAccountId, nameEdit.text) } + // Whether the selected mailbox takes its From name from here. The others + // are named by their server, and a field that changed nothing on the sent + // message would be a promise the page could not keep. So would one on a + // backend too old to write the name. + readonly property bool senderNameEditable: { + var entry = signatureAccount(selectedNameAccountId) + return !!(service && service.backendCanNameSender && entry + && Accounts.namesSender(entry.provider)) + } + + function saveSenderName() { + if (service && senderNameEditable) + service.setAccountSenderName(selectedNameAccountId, senderNameEdit.text) + } + + function showNameAccount(next) { + selectedNameAccountId = next ? String(next.id || "") : "" + nameEdit.text = next ? String(next.label || "") : "" + senderNameEdit.text = next ? String(next.senderName || "") : "" + } + function selectNameAccount(id) { var next = signatureAccount(id) if (!next || String(next.id || "") === selectedNameAccountId) return saveName() - selectedNameAccountId = String(next.id || "") - nameEdit.text = String(next.label || "") + saveSenderName() + showNameAccount(next) } function ensureNameAccount() { if (signatureAccounts.length === 0) { - selectedNameAccountId = "" - nameEdit.text = "" + showNameAccount(null) return } if (signatureAccount(selectedNameAccountId)) return var activeId = service ? String(service.activeAccountId || "") : "" - var next = signatureAccount(activeId) || signatureAccounts[0] - selectedNameAccountId = String(next.id || "") - nameEdit.text = String(next.label || "") + showNameAccount(signatureAccount(activeId) || signatureAccounts[0]) } // The imported markup for the selected mailbox, and the import in flight. @@ -1168,7 +1187,7 @@ Column { Text { width: parent.width - text: "Name" + text: "Mailbox name" color: root.textColor font.family: root.panelFontFamily font.pixelSize: Style.font.bodySmall @@ -1206,6 +1225,47 @@ Column { font.pixelSize: Style.font.caption wrapMode: Text.WordWrap } + + Item { + width: parent.width + implicitHeight: Style.space(4) + visible: root.senderNameEditable + } + + Text { + width: parent.width + visible: root.senderNameEditable + text: "Your name" + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + } + + TextField { + id: senderNameEdit + objectName: "settings-sender-name-editor" + visible: root.senderNameEditable + width: parent.width + foreground: root.textColor + accent: root.accentColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + + onActiveFocusChanged: if (!activeFocus) root.saveSenderName() + onAccepted: root.saveSenderName() + } + + Text { + width: parent.width + visible: root.senderNameEditable + textFormat: Text.PlainText + text: "Who your mail is from, as recipients see it beside this mailbox's " + + "address. Leave it empty to send the address alone." + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } } Column { diff --git a/ui/tests/qml/tst_composer_sender_name.qml b/ui/tests/qml/tst_composer_sender_name.qml new file mode 100644 index 0000000..163163f --- /dev/null +++ b/ui/tests/qml/tst_composer_sender_name.qml @@ -0,0 +1,74 @@ +import QtQuick +import QtTest +import "../../account" as Accounts +import "../../backend" as BackendModule + +// The composer's From row names an IMAP or Outlook mailbox's own address with +// the entry's sender name, and only on a backend that names agent sends the +// same way (API 7). A server-named mailbox keeps the name its server gave. +Item { + Component { + id: backendFactory + BackendModule.Backend { + executable: "/synthetic/runtime/bin/omamail" + expectedVersion: "0.8.2" + expectedApiVersion: 1 + launchEnabled: false + } + } + Component { + id: accountFactory + Accounts.MailAccount { + pluginDir: "/synthetic/plugin" + configuredEmail: "ada@example.org" + senderName: "Ada Lovelace" + active: false + windowOpen: false + } + } + TestCase { + name: "ComposerSenderName" + + function connected(apiVersion) { + var backend = createTemporaryObject(backendFactory, parent) + backend.launchEnabled = true + backend.connected = true + backend.protocolInfo = ({ apiVersion: apiVersion }) + return backend + } + + function primary(account) { + var rows = account.availableSendAsAliases + for (var i = 0; i < rows.length; i++) if (rows[i].isPrimary) return rows[i] + return null + } + + function test_imap_and_outlook_name_the_primary_address_from_api_7() { + var providers = ["imap", "outlook"] + for (var i = 0; i < providers.length; i++) { + var backend = connected(6) + var account = createTemporaryObject(accountFactory, parent, + { backend: backend, providerId: providers[i] }) + account.profile = ({ email: "ada@example.org" }) + compare(backend.ready, true) + compare(primary(account).displayName, "", + providers[i] + ": API 6 cannot name agent sends, so the composer does not either") + backend.protocolInfo = ({ apiVersion: 7 }) + compare(primary(account).displayName, "Ada Lovelace", providers[i]) + backend.latestApiVersion = 8 + backend.protocolInfo = ({ apiVersion: 8 }) + compare(primary(account).displayName, "Ada Lovelace", providers[i] + ": a later API keeps it") + backend.connected = false + compare(primary(account).displayName, "", providers[i] + ": nothing is promised offline") + } + } + + function test_a_server_named_mailbox_keeps_its_own_name() { + var account = createTemporaryObject(accountFactory, parent, + { backend: connected(7), providerId: "gmail" }) + account.profile = ({ email: "ada@example.org" }) + compare(account.availableSendAsAliases.length, 1) + compare(account.availableSendAsAliases[0].displayName, "") + } + } +} diff --git a/ui/tests/qml/tst_service_backend_path.qml b/ui/tests/qml/tst_service_backend_path.qml index c496cdc..b656e49 100644 --- a/ui/tests/qml/tst_service_backend_path.qml +++ b/ui/tests/qml/tst_service_backend_path.qml @@ -41,6 +41,23 @@ Item { compare(service.backendCanCheckMicrosoftConnection, false) compare(service.backendCanDiscoverCalendars, false) } + function test_sender_name_requirement_is_fixed_across_releases() { + var service = createTemporaryObject(factory, parent) + verify(service !== null) + var backend = service.backend + backend.launchEnabled = true + backend.connected = true + backend.protocolInfo = ({ apiVersion: 6 }) + compare(service.backendCanNameSender, false, "published API 6 ignores a stored sender name") + backend.protocolInfo = ({ apiVersion: 7 }) + compare(service.backendCanNameSender, true) + backend.latestApiVersion = 8 + backend.protocolInfo = ({ apiVersion: 8 }) + compare(service.backendCanNameSender, true, + "a later API must not take the sender name away") + backend.connected = false + compare(service.backendCanNameSender, false) + } function test_calendar_requests_obey_the_connected_api() { var service = createTemporaryObject(factory, parent) verify(service !== null) diff --git a/ui/tests/qml/tst_settings_name.qml b/ui/tests/qml/tst_settings_name.qml index ce6a963..c045590 100644 --- a/ui/tests/qml/tst_settings_name.qml +++ b/ui/tests/qml/tst_settings_name.qml @@ -28,9 +28,14 @@ Item { property string activeAccountId: "a@example.org" property var accountSummaries: [] property var accountSignatures: [ - { id: "a@example.org", email: "a@example.org", label: "", signature: "" }, - { id: "b@example.net", email: "b@example.net", label: "Work", signature: "" } + { id: "a@example.org", email: "a@example.org", label: "", signature: "", + provider: "imap", senderName: "" }, + { id: "b@example.net", email: "b@example.net", label: "Work", signature: "", + provider: "gmail", senderName: "" }, + { id: "c@example.com", email: "c@example.com", label: "", signature: "", + provider: "outlook", senderName: "Jane Example" } ] + property bool backendCanNameSender: true property int undoSendSeconds: 10 property bool unifiedCalendarView: false property bool notifyNewMail: true @@ -52,6 +57,16 @@ Item { nameWrites += 1 } + property string senderNamedId: "" + property string senderNamedText: "" + property int senderNameWrites: 0 + + function setAccountSenderName(id, text) { + senderNamedId = String(id || "") + senderNamedText = String(text || "") + senderNameWrites += 1 + } + function setAccountSignature(_id, _text) {} function setUndoSendSeconds(_value) {} function setUnifiedCalendarView(_value) {} @@ -92,11 +107,26 @@ Item { return find("settings-name-editor") } + // Laid out before it is clicked: switching mailboxes in `init` can show + // or hide the field, and the column places it on the next polish. + function senderField() { + var field = find("settings-sender-name-editor") + if (field && field.visible) waitForPolish(field.parent) + return field + } + function init() { + fakeService.backendCanNameSender = true + page.selectNameAccount("a@example.org") + // The fake service keeps nothing it is given, so the fields go back to + // the stored values rather than carrying one test's typing into the next. + page.showNameAccount(page.signatureAccount("a@example.org")) fakeService.namedId = "" fakeService.namedText = "" fakeService.nameWrites = 0 - page.selectNameAccount("a@example.org") + fakeService.senderNamedId = "" + fakeService.senderNamedText = "" + fakeService.senderNameWrites = 0 } function test_signature_import_waits_for_native_result_and_keeps_original_account() { @@ -181,5 +211,55 @@ Item { "what was typed belongs to the mailbox it was typed for") compare(fakeService.namedText, "x") } + + // ---------------------------------------------------------- sender name + + // An IMAP mailbox has no server to name its sender, so the name is set + // here, and typed the way a person types it. + function test_an_imap_mailbox_takes_a_sender_name() { + var field = senderField() + verify(field, "the sender name field has to exist") + verify(field.visible, "an IMAP mailbox's From name is set here") + compare(field.text, "") + mouseClick(field, field.width / 2, field.height / 2) + verify(field.activeFocus, "a click must put the cursor in the field") + keyClick(Qt.Key_J) + keyClick(Qt.Key_O) + compare(fakeService.senderNameWrites, 0, "nothing is written per keystroke") + keyClick(Qt.Key_Return) + compare(fakeService.senderNameWrites, 1) + compare(fakeService.senderNamedId, "a@example.org") + compare(fakeService.senderNamedText, "jo") + } + + // Leaving for another mailbox saves the name for the one it was typed for. + function test_moving_away_saves_the_sender_name_where_it_was_typed() { + var field = senderField() + mouseClick(field, field.width / 2, field.height / 2) + keyClick(Qt.Key_Z) + page.selectNameAccount("c@example.com") + compare(fakeService.senderNamedId, "a@example.org") + compare(fakeService.senderNamedText, "z") + compare(field.text, "Jane Example", "an Outlook mailbox shows its own stored name") + verify(field.visible) + } + + // A server-named mailbox would ignore the field, so there is none. + function test_a_server_named_mailbox_has_no_sender_name_field() { + page.selectNameAccount("b@example.net") + verify(!senderField().visible, "Gmail names the sender itself") + compare(page.senderNameEditable, false) + page.saveSenderName() + compare(fakeService.senderNameWrites, 0, "nothing is written for it either") + } + + // A backend older than API 7 sends the bare address whatever is stored, so + // the field waits for one that writes the name. + function test_an_older_backend_hides_the_sender_name_field() { + fakeService.backendCanNameSender = false + verify(!senderField().visible) + fakeService.backendCanNameSender = true + verify(senderField().visible) + } } } diff --git a/ui/tests/qml/tst_setup_name_field.qml b/ui/tests/qml/tst_setup_name_field.qml index 0278cdc..c5e4b08 100644 --- a/ui/tests/qml/tst_setup_name_field.qml +++ b/ui/tests/qml/tst_setup_name_field.qml @@ -28,6 +28,8 @@ Item { property var auth: fakeAuth property string accountAddress: "" property string accountName: "Work" + property string accountSenderName: "Ada Lovelace" + property bool backendCanNameSender: false property var saved: null property var signedIn: null function configureCurrentAccount(values) { saved = values } @@ -99,6 +101,27 @@ Item { panelFontFamily: "monospace" } + QtObject { + id: outlookService + property var auth: null + property string accountAddress: "ada@hotmail.com" + property string accountSenderName: "" + property bool backendCanNameSender: true + property var saved: null + function configureCurrentAccount(values) { saved = values } + } + + Mail.OutlookSetupPage { + id: outlookPage + width: 600 + service: outlookService + textColor: Color.foreground + dimColor: Color.foreground + dangerColor: Color.accent + accentColor: Color.accent + panelFontFamily: "monospace" + } + TestCase { name: "SetupNameField" when: windowShown @@ -113,6 +136,16 @@ Item { return find(page) } + function outlookField(name) { + function find(item) { + if (item.objectName === name) return item + var kids = item.children || [] + for (var i = 0; i < kids.length; i++) { var f = find(kids[i]); if (f) return f } + return null + } + return find(outlookPage) + } + function gmailField(name) { function find(item) { if (item.objectName === name) return item @@ -190,5 +223,68 @@ Item { page.save() compare(fakeService.saved.label, "", "empty is no name, which puts the address back") } + + // The sender name is asked for beside the mailbox name, and the two stay + // apart: one is how Omamail lists the mailbox, the other what recipients + // see. A backend too old to write it gets no field and no value, so the + // entry keeps whatever it already had. + function test_imap_setup_names_the_sender_only_on_a_backend_that_writes_it() { + var sender = field("sender-name-field") + verify(sender !== null) + verify(!sender.visible, "hidden while the backend cannot name the sender") + field("imap-address-field").text = "ada@icloud.com" + page.save() + compare(fakeService.saved.senderName, undefined, "and left out of the form") + + fakeService.backendCanNameSender = true + verify(sender.visible) + compare(sender.text, "Ada Lovelace", "the name the entry has is what the field shows") + verify(sender.placeholderText.indexOf("Your name") === 0) + verify(field("account-name-field").placeholderText.indexOf("Mailbox name") === 0) + sender.text = " Ada King " + field("account-name-field").text = "Home" + page.save() + compare(fakeService.saved.senderName, "Ada King", "trimmed, with the rest of the form") + compare(fakeService.saved.label, "Home", "beside the mailbox name, not in place of it") + page.signIn() + compare(fakeService.signedIn.senderName, "Ada King", "and on the way to a sign-in") + fakeService.backendCanNameSender = false + page.syncFromStore() + } + + function test_outlook_setup_names_the_sender() { + var sender = outlookField("sender-name-field") + verify(sender !== null) + verify(sender.visible) + sender.text = "Ada Lovelace" + outlookField("outlook-address-field").text = "ada@hotmail.com" + outlookField("outlook-client-id-field").text = "00000000-0000-0000-0000-000000000000" + outlookPage.save() + verify(outlookService.saved !== null) + compare(outlookService.saved.provider, "outlook") + compare(outlookService.saved.senderName, "Ada Lovelace") + } + + // The setup form's value reaches the entry through the same save as the + // rest of it, cleaned the way the settings field's is. + function test_configuring_an_account_stores_its_sender_name() { + var list = Accounts.emptyList() + list = Accounts.add(list, { email: "ada@example.com", provider: "imap", clientId: "", clientSecret: "", + imap: { imapHost: "imap.example.com", imapPort: 993, smtpHost: "smtp.example.com", smtpPort: 465, + username: "ada@example.com", aliases: [], insecure: false }, label: "", signature: "" }) + list = Accounts.setActive(list, "imap:ada@example.com") + realService.activeIndex = -1 + realService.accountList = list + realService.accountsLoaded = true + wait(0) + realService.refreshCurrent() + tryCompare(realService, "activeAccountId", "imap:ada@example.com") + realService.configureCurrentAccount({ senderName: "Ada\nLovelace" }) + compare(String(realService.accountList.accounts[0].senderName || ""), "Ada Lovelace") + compare(realService.accountSenderName, "Ada Lovelace", "which is what the field reads back") + realService.configureCurrentAccount({ label: "Work" }) + compare(String(realService.accountList.accounts[0].senderName || ""), "Ada Lovelace", + "a save without it keeps the name") + } } } diff --git a/ui/tests/test_accounts.js b/ui/tests/test_accounts.js index a9b1e41..d029b9e 100644 --- a/ui/tests/test_accounts.js +++ b/ui/tests/test_accounts.js @@ -862,3 +862,38 @@ assert.strictEqual(frozen(accounts.replaceAt(cidActive, -1, account("x@example.c assert.strictEqual(accounts.setSignatureHtml(rich, "me@gmail.com", "").accounts[0].signatureHtml, "") assert.strictEqual(accounts.load(accounts.serialize(rich)).accounts[0].signatureHtml, "

Ada

") } + +// ------------------------------------------------------------ sender name + +// The name recipients see beside an IMAP or Outlook mailbox's address. It is +// stored on the entry, so it has to survive the rebuild every write does. +{ + const imap = accounts.add(accounts.emptyList(), + { email: "me@example.org", provider: "imap", imap: { username: "me@example.org" } }) + const id = imap.accounts[0].id + assert.strictEqual(imap.accounts[0].senderName, "", "no name until one is given") + + const named = accounts.setSenderName(imap, id, " Jane Example ") + assert.strictEqual(named.accounts[0].senderName, "Jane Example") + assert.strictEqual(frozen(imap), frozen(accounts.add(accounts.emptyList(), + { email: "me@example.org", provider: "imap", imap: { username: "me@example.org" } })), + "the input list is left alone") + assert.strictEqual(accounts.load(accounts.serialize(named)).accounts[0].senderName, "Jane Example", + "a saved name comes back from the file") + assert.strictEqual(accounts.setLabel(named, id, "Personal").accounts[0].senderName, "Jane Example", + "naming the mailbox does not forget the sender name") + assert.strictEqual(accounts.setSenderName(named, id, " ").accounts[0].senderName, "", + "empty clears it, so the address goes out alone") + + // One header line: a pasted break or tab becomes a space, never a header + // the send would refuse. + assert.strictEqual(accounts.senderNameText("Jane\r\nExample\t\u0007"), "Jane Example") +} + +// Only the mailboxes with no server to name the sender take it from here. +assert.strictEqual(accounts.namesSender("imap"), true) +assert.strictEqual(accounts.namesSender("outlook"), true) +assert.strictEqual(accounts.namesSender("gmail"), false) +assert.strictEqual(accounts.namesSender("jmap"), false) +assert.strictEqual(accounts.namesSender("hey"), false) +assert.strictEqual(accounts.namesSender(undefined), false) diff --git a/ui/tests/test_aliases.js b/ui/tests/test_aliases.js index 2a3a272..1cf7c71 100644 --- a/ui/tests/test_aliases.js +++ b/ui/tests/test_aliases.js @@ -104,4 +104,22 @@ deepEqual(aliases.sendAsList("me@icloud.com", null), [ { email: "me@icloud.com", displayName: "", isPrimary: true, isDefault: true } ]) +// ------------------------------------------------------------ sender name + +// The account's sender name goes on the mailbox's own address and nowhere +// else: an alias keeps the name it was typed with, or none. +{ + const rows = aliases.sendAsList("me@icloud.com", '"Work Desk" , other@icloud.com') + const before = JSON.stringify(rows) + deepEqual(aliases.withSenderName(rows, " Jane Example "), [ + { email: "me@icloud.com", displayName: "Jane Example", isPrimary: true, isDefault: true }, + { email: "work@icloud.com", displayName: "Work Desk", isPrimary: false, isDefault: false }, + { email: "other@icloud.com", displayName: "", isPrimary: false, isDefault: false } + ]) + assert.strictEqual(JSON.stringify(rows), before, "the provider's list is not edited in place") + // No name is the list as it was: the address goes out alone. + assert.strictEqual(aliases.withSenderName(rows, " "), rows) + deepEqual(aliases.withSenderName(null, "Jane"), []) +} + console.log("Aliases.js ok") From 1c0065b175379072e983a27c211007f6d573a2ba Mon Sep 17 00:00:00 2001 From: Orpheus Date: Sun, 11 Oct 2026 14:08:42 +0530 Subject: [PATCH 13/88] Fix opening attachments on JMAP mailboxes (#339) Closes #327. On a JMAP mailbox, opening or saving an attachment failed with "That attachment could not be opened". The diagnostics log showed `attachment_data_invalid` from `attachment.store`. The cause is in `JmapClient.getAttachment`. It gave the callback of `nativeRequest` straight to the caller. `nativeRequest` calls back with `result.data`. For `jmap.attachment`, that value is the object `{data, size}`, not the base64 string. `MailAccount.loadAttachments` then wrote `String(data)`, which is `"[object Object]"`. The backend refused that text. The IMAP, Gmail and HEY clients already give their callers the base64 string. The fix makes the JMAP client do the same. All callers of `api.getAttachment` use the same function: opening, saving, and the calendar invitation reader. The fix in the client covers all of them. ## Verification - New test: `test_attachment_callback_receives_the_base64_string_not_the_wire_object` in `ui/tests/qml/tst_jmap_request_queue.qml`. It failed before the fix (`Actual: [object Object]`, `Expected: aGk`). It passes after the fix. I watched both results. - The full QML suite passed: 1159 passed, 0 failed (`tests/run_qml_native.py` with `-input ui/tests/qml`). - `make test-js` passed. `qmllint` on `ui/providers/JmapClient.qml` gave no output. - I did not run the full `make validate`. I did not run the macOS and Windows native gates. - I did not test this against a live Fastmail account. - No fresh-agent review was done. No screenshot is included, because no visible layout changed. ## Release Notes - Attachments on JMAP mailboxes, such as Fastmail, now open and save. Before, they failed with "That attachment could not be opened". Co-authored-by: Jason Lee --- ui/providers/JmapClient.qml | 5 ++++- ui/tests/qml/tst_jmap_request_queue.qml | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ui/providers/JmapClient.qml b/ui/providers/JmapClient.qml index c0cd6a4..8831474 100644 --- a/ui/providers/JmapClient.qml +++ b/ui/providers/JmapClient.qml @@ -151,7 +151,10 @@ Item { }, existingHandle) } function getMessage(id, full, callback) { return nativeRequest("jmap.read", { id: String(id || ""), full: full === true }, callback) } - function getAttachment(messageId, attachmentId, callback) { return nativeRequest("jmap.attachment", { attachmentId: String(attachmentId || "") }, callback) } + function getAttachment(messageId, attachmentId, callback) { + return nativeRequest("jmap.attachment", { attachmentId: String(attachmentId || "") }, + function(result, error) { if (typeof callback === "function") callback(error || !result ? "" : String(result.data || ""), error) }) + } function getLabels(callback) { return nativeRequest("jmap.labels", {}, callback) } function getLabelCounts(id, callback) { return nativeRequest("jmap.labelCounts", { id: String(id || "") }, callback) } function modifyMessage(id, added, removed, callback) { return batchModify([id], added, removed, callback) } diff --git a/ui/tests/qml/tst_jmap_request_queue.qml b/ui/tests/qml/tst_jmap_request_queue.qml index 257a634..67be80b 100644 --- a/ui/tests/qml/tst_jmap_request_queue.qml +++ b/ui/tests/qml/tst_jmap_request_queue.qml @@ -67,5 +67,24 @@ Item { compare(f.api.mailboxList.length, 0) compare(f.api.inFlight, 0) } + function test_attachment_callback_receives_the_base64_string_not_the_wire_object() { + var f = setup() + var got = [] + var before = Transports.transports(f.api) + f.api.getAttachment("m1", "blob1", function(data, error) { got.push([data, error]) }) + var request = Transports.newSince(f.api, before)[0] + compare(request.method, "jmap.attachment") + Transports.complete(request, {data: "aGk", size: 2}) + compare(got.length, 1) + compare(got[0][0], "aGk") + compare(got[0][1], "") + + before = Transports.transports(f.api) + f.api.getAttachment("m1", "blob2", function(data, error) { got.push([data, error]) }) + Transports.reply(Transports.newSince(f.api, before)[0], null, {message: "attachment_data_invalid"}) + compare(got.length, 2) + compare(got[1][0], "") + verify(got[1][1] !== "") + } } } From 2ba5c69a2953fe54a5e9146dcb721e4e7370986d Mon Sep 17 00:00:00 2001 From: Beast1ng <91227656+Beast1ng@users.noreply.github.com> Date: Sun, 11 Oct 2026 10:38:56 +0200 Subject: [PATCH 14/88] Act on a selection spanning mailboxes in the unified view (#340) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Reproduction 1. Add two accounts (reproduced with two IMAP accounts on the same Zoho server) and turn on unified mailboxes. 2. In the merged inbox, tick two or more messages. 3. Press `d`, mark read, star or archive. Nothing happens to the messages. A notice briefly reads "Acting on several messages needs one mailbox on screen", which is easy to miss, so it looks like batch actions are broken. The same action on one message works. ## Cause `Service.actMany` refuses every batch when `unified` is true, on the grounds that a batch is one mailbox's request. But a merged row's id already names its account (`Unified.accountOf`), which is exactly how `Service.act` routes a single-row action to the right host. ## Fix In the unified view, `actMany` splits the ticked ids by owning account (`Unified.groupByAccount`, which translates each one back to the id its mailbox issued) and calls each host's own `actMany`. Each mailbox still gets one batch request. The non-unified path is unchanged. A selection spanning two accounts shows one confirmation note per account. I left that as is rather than merging the notes. ## Tests - `ui/tests/test_unified.js` covers `groupByAccount`: a selection across two mailboxes, IMAP ids whose folder contains a space, and an id that names no account. It fails without the fix. - `node ui/tests/test_unified.js` passes, and so does `make test-qml` (1228 passed, 0 failed). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Jason Lee --- Makefile | 3 +- ui/App.qml | 32 +++++-- ui/Service.qml | 18 ++-- ui/account/BatchAction.qml | 5 +- ui/account/MailAccount.qml | 39 ++++++-- ui/account/Unified.js | 20 +++++ ui/account/UnifiedBatchAction.qml | 67 ++++++++++++++ ui/account/UnifiedBatchSelection.qml | 40 +++++++++ ui/tests/qml/tst_batch_actions.qml | 89 +++++++++++++++++- ui/tests/qml/tst_unified_batch.qml | 130 +++++++++++++++++++++++++++ ui/tests/test_unified.js | 15 ++++ 11 files changed, 428 insertions(+), 30 deletions(-) create mode 100644 ui/account/UnifiedBatchAction.qml create mode 100644 ui/account/UnifiedBatchSelection.qml create mode 100644 ui/tests/qml/tst_unified_batch.qml diff --git a/Makefile b/Makefile index bdf4b83..e6fb285 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,10 @@ QMLLINT := /usr/lib/qt6/bin/qmllint .DEFAULT_GOAL := test QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml \ ui/tests/compatibility/tst_published_agent.qml \ + ui/tests/qml/tst_unified_batch.qml \ ui/backend/Backend.qml ui/backend/Runtime.qml ui/diagnostics/Diagnostics.qml \ ui/components/BackendSetup.qml ui/components/OmamailLogo.qml \ - ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ + ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/UnifiedBatchAction.qml ui/account/UnifiedBatchSelection.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ ui/cache/CacheStore.qml ui/cache/BodyCache.qml \ ui/providers/AuthManager.qml ui/providers/GmailApiClient.qml \ ui/providers/OutlookAuth.qml \ diff --git a/ui/App.qml b/ui/App.qml index 6fe4427..5904634 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -8,6 +8,7 @@ import qs.Commons as Commons import qs.Ui import "account/Model.js" as Model +import "account" as Account import "account/Accounts.js" as Accounts import "account/Navigation.js" as Nav import "compose/Recovery.js" as Recovery @@ -240,9 +241,7 @@ Item { return true } - // Acting on the selection acts on every ticked row at once, then drops the - // selection: the rows it named have moved or changed, and a selection that - // outlived the action would be one keystroke from repeating it. + Account.UnifiedBatchSelection { id: batchSelection; app: root; service: root.service } function actOnChecked(action) { if (!service || checkedIds.length === 0) return false // Never through another mailbox: a switch that reached the service by @@ -258,7 +257,11 @@ Item { // The open message going with the selection closes the reader, the way // acting on it alone does: it is about to leave this list. var wasOpen = currentView === "reader" && ids.indexOf(service.selectedId) >= 0 - if (!service.actMany(ids, action)) return false + var scope = batchSelection.scope + var completion = service.unified ? function(result) { + Qt.callLater(function() { batchSelection.restore(result.failedIds, scope) }) + } : undefined + if (!service.actMany(ids, action, completion)) return false checkedIds = [] if (!leaves) return true if (wasOpen) { @@ -528,8 +531,7 @@ Item { else close() } - // How a message is read is a preference and survives; the heavy-document - // override is a per-message decision about one specific message and does not. + // The rich-document override belongs to one message. function openMessage(id) { if (!service) return pendingComposeMode = "" @@ -2590,9 +2592,6 @@ Item { } } - // The right of the status line carries one of two things: what the - // window most needs to say, or — when it has nothing to report — what - // the keyboard can do from where you are standing. readonly property bool hasNotice: root.notice !== "" || (!!root.service && (root.service.actionStatus !== "" || root.service.lastError !== "")) @@ -2640,6 +2639,21 @@ Item { font.family: root.fontFamily font.pixelSize: Style.font.caption elide: Text.ElideRight + HoverHandler { id: noticeHover } + ToolTip { + visible: notice.visible && noticeHover.hovered && notice.truncated + delay: 500 + width: Math.min(Style.space(600), window.width - Style.space(32)) + contentItem: Text { + text: notice.text + textFormat: Text.PlainText + wrapMode: Text.Wrap + color: root.foreground + font.family: root.fontFamily + font.pixelSize: Style.font.caption + } + background: Rectangle { color: root.popupBackground; border.color: root.popupBorder } + } } KeyHints { diff --git a/ui/Service.qml b/ui/Service.qml index 6c1f6d9..d02543d 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -2022,13 +2022,14 @@ Item { readonly property int sendSecondsRemaining: pendingSendHost ? pendingSendHost.sendSecondsRemaining : 0 readonly property string lastError: unified - ? (unifiedSnapshotError || unifiedSnapshot.error || "") : (current ? current.lastError : "") + ? (unifiedBatchAction.error || unifiedSnapshotError || unifiedSnapshot.error || "") : (current ? current.lastError : "") // The last thing a mailbox said it did, and the undo that goes with it. Read // from `current` alone, archiving a row from a non-active mailbox in a merged // list produced no confirmation and no undo affordance at all — so the most // recent of them wins, which is the one the press just produced. readonly property string actionStatus: { if (!unified) return current ? current.actionStatus : "" + if (unifiedBatchAction.status !== "") return unifiedBatchAction.status var _epoch = listEpoch var newest = "" var at = -1 @@ -2235,10 +2236,12 @@ Item { return host ? host.refuseUnavailableAction(action) : true } function act(id, action, quiet, memberOnly) { + unifiedBatchAction.clear() var host = hostForId(id) return host ? host.act(sourceIdFor(id), action, quiet, memberOnly) : false } function toggleStar(id) { + unifiedBatchAction.clear() var host = hostForId(id) if (host) host.toggleStar(sourceIdFor(id)) } @@ -2249,15 +2252,10 @@ Item { } eachHost(function(host) { host.markAllRead() }) } - // Several ticked rows at once. A merged list draws rows from several - // mailboxes, and a batch is one mailbox's request, so it is refused there - // the way a move is: the rule every unavailable action follows. - function actMany(ids, action) { - if (unified) { - fail("Acting on several messages needs one mailbox on screen") - return false - } - return current ? current.actMany(ids, action) : false + UnifiedBatchAction { id: unifiedBatchAction; service: root } + function actMany(ids, action, completion) { + if (!unified) return current ? current.actMany(ids, action, completion) : false + return unifiedBatchAction.run(ids, action, completion) } // The mailbox the From address belongs to, which compose already names: // `sendIdentities` spans every account and carries the id, so a unified diff --git a/ui/account/BatchAction.qml b/ui/account/BatchAction.qml index c9154b5..1d7e542 100644 --- a/ui/account/BatchAction.qml +++ b/ui/account/BatchAction.qml @@ -6,7 +6,8 @@ QtObject { required property var account required property var intents - function run(ids, action) { - return account.runNativeAction(Array.isArray(ids) ? ids : [], action, false, false, false) + function run(ids, action, completion) { + return account.runNativeAction(Array.isArray(ids) ? ids : [], action, + typeof completion === "function", false, false, completion) } } diff --git a/ui/account/MailAccount.qml b/ui/account/MailAccount.qml index 036189d..90b0c15 100644 --- a/ui/account/MailAccount.qml +++ b/ui/account/MailAccount.qml @@ -439,6 +439,7 @@ Item { property string pendingActionQuery: "" property var deferredListLoad: null property var queuedActions: [] + property var actionCompletions: [] property bool sending: false readonly property alias sendQueue: sendQueue readonly property int sendPendingCount: sendQueue.parked.length @@ -1736,12 +1737,12 @@ Item { // Network sends keep their order; native intent preparation proceeds while // a previous send is in flight, so a slow server never blocks the next edit. - function queueAction(messageId, action, actionQuery, quiet, memberOnly, dispatch, discard, token, sourceLabelId) { + function queueAction(messageId, action, actionQuery, quiet, memberOnly, dispatch, discard, token, sourceLabelId, independent) { var queued = queuedActions.slice() for (var i = queued.length - 1; i >= 0; i--) { var previous = queued[i] if (previous.id !== messageId) continue - if (previous.action === action && previous.cacheKey === actionQuery + if (!independent && previous.action === action && previous.cacheKey === actionQuery && previous.quiet === quiet && previous.memberOnly === memberOnly && previous.sourceLabelId === sourceLabelId) { discard(previous.token) @@ -1798,9 +1799,18 @@ Item { return runNativeAction([String(id || "")], action, quiet === true, memberOnly === true, false) } - function runNativeAction(ids, action, quiet, memberOnly, allRead) { + function runNativeAction(ids, action, quiet, memberOnly, allRead, completion) { if (!ready || !backend || (!allRead && ids.length === 0) || (allRead && messages.length === 0)) return false if (refuseUnavailableAction(action)) return false + var reported = false + var cancel = function() { finish(ids, "Account signed out") } + function finish(failedIds, error) { + if (reported) return + reported = true + root.actionCompletions = root.actionCompletions.filter(function(callback) { return callback !== cancel }) + if (typeof completion === "function") completion({failedIds: failedIds || [], error: String(error || "")}) + } + if (typeof completion === "function") actionCompletions = actionCompletions.concat([cancel]) var account = accountId var actionQuery = cacheKey var estimate = resultEstimate @@ -1830,6 +1840,7 @@ Item { if (root.accountId === account && preparationEpoch === intents.epoch) root.actionPreparations = Math.max(0, root.actionPreparations - 1) if (error || !prepared) { + finish(ids, error || "Could not prepare the action") if (root.accountId === account && preparationEpoch === intents.epoch) { root.fail(error || "Could not prepare the action") if (root.actionPreparations === 0 && root.pendingAction === "") { @@ -1841,6 +1852,7 @@ Item { return } if (prepared.refused) { + finish(ids, "This mailbox cannot perform that action") if (root.accountId === account && preparationEpoch === intents.epoch) { root.refuseUnavailableAction(action) if (root.actionPreparations === 0 && root.pendingAction === "") { @@ -1852,8 +1864,9 @@ Item { } var targets = prepared.targets || [] var rows = prepared.rows || [] - if (targets.length === 0) return + if (targets.length === 0) { finish(ids, "Those messages are no longer available"); return } if (root.accountId !== account || !root.ready || intents.generation !== prepared.generation) { + finish(ids, "Account changed") intents.settle(account, actionQuery, prepared.token, rows, function() {}, prepared.generation) return } @@ -1865,7 +1878,10 @@ Item { function done(payload, failure, failedIds) { var failed = failure ? (Array.isArray(failedIds) ? failedIds : rows) : [] intents.settle(account, actionQuery, prepared.token, failed, function(settled, settleError) { - if (root.accountId !== account || intents.generation !== prepared.generation) return + if (root.accountId !== account || intents.generation !== prepared.generation) { + finish(ids, "Account changed") + return + } root.pendingAction = "" root.pendingActionQuery = "" var message = failure && Array.isArray(failedIds) && rows.length > 1 @@ -1887,6 +1903,7 @@ Item { else root.note(rows.length > 1 ? Model.batchNote(rows.length, root.actionLabel(action)) : root.actionLabel(action)) } root.refreshCounts() + finish(settleError ? ids : failed, message) root.runQueuedAction() if (root.resumeDeferredListLoad(actionQuery, message)) return if (root.cacheKey === actionQuery && (interrupted || invalidates || message !== "")) @@ -1924,7 +1941,7 @@ Item { } if (root.pendingAction !== "") root.queueAction(ids.join(","), action, actionQuery, quiet, memberOnly, dispatch, function(intoToken) { intents.coalesce(account, actionQuery, prepared.token, intoToken, prepared.generation) - }, prepared.token, parameters.sourceLabelId) + }, prepared.token, parameters.sourceLabelId, typeof completion === "function") else dispatch() }) return true @@ -2003,7 +2020,7 @@ Item { return runNativeAction([], "markRead", false, false, true) } - function actMany(ids, action) { return batchAction.run(ids, action) } + function actMany(ids, action, completion) { return batchAction.run(ids, action, completion) } BatchAction { id: batchAction @@ -2585,6 +2602,7 @@ Item { function signOut() { intents.clear() + cancelActionCompletions() queuedActions = [] actionPreparations = 0 pendingAction = "" @@ -2608,6 +2626,13 @@ Item { clearSelection() } + function cancelActionCompletions() { + var callbacks = actionCompletions + actionCompletions = [] + for (var i = 0; i < callbacks.length; i++) callbacks[i]() + } + Component.onDestruction: cancelActionCompletions() + // ------------------------------------------------------------- lifecycle onWindowOpenChanged: { diff --git a/ui/account/Unified.js b/ui/account/Unified.js index 5afcaa8..8b77f23 100644 --- a/ui/account/Unified.js +++ b/ui/account/Unified.js @@ -246,6 +246,26 @@ function sourceIdOf(id) { return splitUnifiedId(id).id } +// Ticked merged rows as one batch per mailbox: each account with the ids it +// issued, in the order its first row was ticked. A batch is one mailbox's +// request, so a selection spanning two of them is two batches. An id that +// names no account is dropped rather than guessed at. +function groupByAccount(ids) { + var values = Array.isArray(ids) ? ids : [] + var groups = [] + var at = ({}) + for (var i = 0; i < values.length; i++) { + var parts = splitUnifiedId(values[i]) + if (parts.accountId === "") continue + if (!(parts.accountId in at)) { + at[parts.accountId] = groups.length + groups.push({ accountId: parts.accountId, ids: [] }) + } + groups[at[parts.accountId]].ids.push(parts.id) + } + return groups +} + // The row itself, for the reader: a merged row carries the account it came // from and the account's own copy does not. function rowOf(messages, id) { diff --git a/ui/account/UnifiedBatchAction.qml b/ui/account/UnifiedBatchAction.qml new file mode 100644 index 0000000..180d182 --- /dev/null +++ b/ui/account/UnifiedBatchAction.qml @@ -0,0 +1,67 @@ +import QtQuick +import "Unified.js" as Unified + +// Account-local transactions report one result for the user's whole selection. +QtObject { + id: root + required property var service + property int serial: 0 + property string status: "" + property string error: "" + readonly property string scope: JSON.stringify([service.unified, service.activeAccountId, service.mailboxKey, service.rawQuery]) + onScopeChanged: clear() + property Timer noticeTimer: Timer { interval: 5000; onTriggered: root.status = "" } + function clear() { serial++; status = ""; error = ""; noticeTimer.stop() } + + function run(ids, action, completion) { + var groups = Unified.groupByAccount(ids) + if (groups.length === 0) return false + clear() + var generation = serial + var originalScope = scope + var remaining = groups.length + var failed = [] + var notes = new Array(groups.length) + var total = 0 + var label = "Done" + var acted = false + for (var n = 0; n < groups.length; n++) total += groups[n].ids.length + function replyFor(group, index, host) { + var answered = false + return function(result) { + if (answered) return + answered = true + var reportedFailures = result && Array.isArray(result.failedIds) ? result.failedIds : group.ids + var localFailures = [] + // A reply cannot name another account or a row outside this request. + for (var f = 0; f < reportedFailures.length; f++) { + var id = reportedFailures[f] + if (group.ids.indexOf(id) >= 0 && localFailures.indexOf(id) < 0) { + localFailures.push(id) + failed.push(Unified.unifiedId(group.accountId, id)) + } + } + var count = group.ids.length - localFailures.length + var name = group.accountId + notes[index] = name + ": " + Math.max(0, count) + " completed, " + localFailures.length + " failed" + if (--remaining !== 0) return + if (generation === serial && service.unified && scope === originalScope) { + status = label + ": " + (total - failed.length) + " of " + total + + " selected across " + groups.length + " mailboxes. " + notes.join("; ") + error = failed.length > 0 ? status : "" + service.note(status) + noticeTimer.restart() + } + if (typeof completion === "function") completion({failedIds: failed}) + } + } + for (var i = 0; i < groups.length; i++) { + var host = service.findAccount(groups[i].accountId) + if (host && typeof host.actionLabel === "function") label = host.actionLabel(action) + var reply = replyFor(groups[i], i, host) + if (host && host.actMany(groups[i].ids, action, reply)) acted = true + else reply({failedIds: groups[i].ids}) + } + return acted + } +} diff --git a/ui/account/UnifiedBatchSelection.qml b/ui/account/UnifiedBatchSelection.qml new file mode 100644 index 0000000..f786299 --- /dev/null +++ b/ui/account/UnifiedBatchSelection.qml @@ -0,0 +1,40 @@ +import QtQuick +import "Model.js" as Model + +// Rollback may reach the merged list after the account's completion callback. +QtObject { + id: root + required property var app + required property var service + property var pending: [] + property int epoch: 0 + readonly property string viewKey: service + ? JSON.stringify([service.unified, service.activeAccountId, service.mailboxKey, service.rawQuery]) : "" + readonly property string scope: service + ? JSON.stringify([viewKey, epoch]) : "" + onViewKeyChanged: { epoch++; pending = [] } + + function restore(ids, originalScope) { + if (!service || !service.unified || scope !== originalScope) return + pending = Array.isArray(ids) ? ids.slice() : [] + apply() + } + + function apply() { + if (!service || pending.length === 0) return + var checks = app.checkedIds.slice() + var waiting = [] + for (var i = 0; i < pending.length; i++) { + var id = pending[i] + if (Model.indexById(service.messages, id) < 0) waiting.push(id) + else if (checks.indexOf(id) < 0) checks.push(id) + } + app.checkedIds = checks + pending = waiting + } + + property Connections changes: Connections { + target: root.service + function onMessagesChanged() { Qt.callLater(root.apply) } + } +} diff --git a/ui/tests/qml/tst_batch_actions.qml b/ui/tests/qml/tst_batch_actions.qml index aeafc96..0cc643c 100644 --- a/ui/tests/qml/tst_batch_actions.qml +++ b/ui/tests/qml/tst_batch_actions.qml @@ -3,7 +3,9 @@ import QtTest 1.3 import "../.." as Omamail import "BackendFixture.js" as BackendFixture import "NativeIntentFixture.js" as NativeIntentFixture +import "ModelFixture.js" as NativeModel import "../../account/Accounts.js" as Accounts +import "../../account/Unified.js" as Unified // The batch's two boundaries, driven against a client the test controls. // @@ -92,7 +94,7 @@ Item { name: "BatchActions" when: windowShown - function initTestCase() { BackendFixture.markReady(mailService); NativeIntentFixture.install(mailService) } + function initTestCase() { NativeModel.install(mailService); NativeIntentFixture.install(mailService) } readonly property string ada: "ada@example.com" readonly property string bob: "bob@example.com" @@ -135,11 +137,96 @@ Item { function init() { record.reset() + mailService.setUnifiedMailboxes(false) app.checkedIds = [] app.checkedAccountId = "" app.resetNavigation() } + function test_unified_trash_restores_failed_checks_and_reports_all_accounts() { + seed([entry(ada), entry(bob)], "imap:" + ada) + var first = mailService.accountAt(0) + var second = mailService.accountAt(1) + first.messages = [row("1:INBOX"), row("2:INBOX"), row("3:INBOX")] + second.messages = [row("1:INBOX"), row("4:INBOX")] + mailService.setUnifiedMailboxes(true) + var failed = Unified.unifiedId(first.accountId, "2:INBOX") + var ids = [Unified.unifiedId(first.accountId, "1:INBOX"), failed, + Unified.unifiedId(second.accountId, "1:INBOX")] + tryVerify(function() { return mailService.messages.length === 5 }) + app.checkedAccountId = mailService.activeAccountId + app.checkedIds = ids + app.cursorId = ids[0] + record.refuse = "2:INBOX" + record.hold = true + verify(app.actOnChecked("trash")) + compare(app.checkedIds, []) + tryVerify(function() { return record.trashed.length === 3 }) + compare(record.trashed.filter(function(id) { return id === "1:INBOX" }).length, 2, + "the identical local id reaches both owning accounts") + record.finish() + tryCompare(app, "checkedIds", [failed]) + tryVerify(function() { return mailService.lastError.indexOf("2 of 3 selected across 2 mailboxes") >= 0 }) + verify(mailService.lastError.indexOf(ada + ": 1 completed, 1 failed") >= 0) + verify(mailService.lastError.indexOf(bob + ": 1 completed, 0 failed") >= 0) + compare(first.messages.map(function(message) { return message.id }), ["2:INBOX", "3:INBOX"]) + compare(second.messages.map(function(message) { return message.id }), ["4:INBOX"]) + } + + function test_batch_completion_reports_a_whole_batch_failure_once() { + seed([entry(ada)], "imap:" + ada) + var account = mailService.accountAt(0) + account.messages = [row("1:INBOX"), row("2:INBOX")] + record.refuseBatch = true + var answers = [] + verify(account.actMany(["1:INBOX", "2:INBOX"], "markRead", function(result) { answers.push(result) })) + tryVerify(function() { return answers.length === 1 }) + compare(answers[0].failedIds, ["1:INBOX", "2:INBOX"]) + verify(answers[0].error !== "") + compare(account.messages.length, 2) + } + + function test_queued_batches_keep_every_completion_callback() { + seed([entry(ada)], "imap:" + ada) + var account = mailService.accountAt(0) + account.messages = [row("1:INBOX"), row("2:INBOX")] + record.hold = true + var answers = [] + verify(account.actMany(["1:INBOX", "2:INBOX"], "markRead", function(result) { answers.push(result) })) + tryCompare(account, "pendingAction", "markRead") + for (var i = 0; i < 2; i++) + verify(account.actMany(["1:INBOX", "2:INBOX"], "markRead", function(result) { answers.push(result) })) + tryVerify(function() { return account.queuedActions.length === 2 }) + record.finish() + tryVerify(function() { return answers.length === 3 }) + for (var a = 0; a < answers.length; a++) compare(answers[a].failedIds, []) + compare(record.batches.length, 3) + } + function test_sign_out_finishes_queued_batches_without_dispatching_them() { + seed([entry(ada)], "imap:" + ada) + var account = mailService.accountAt(0) + account.messages = [row("1:INBOX"), row("2:INBOX")] + record.hold = true + var answers = [] + verify(account.actMany(["1:INBOX", "2:INBOX"], "markRead", function(result) { answers.push(result) })) + tryCompare(account, "pendingAction", "markRead") + verify(account.actMany(["1:INBOX", "2:INBOX"], "markRead", function(result) { answers.push(result) })) + tryVerify(function() { return account.queuedActions.length === 1 }) + compare(record.batches.length, 1) + account.signOut() + compare(answers.length, 2) + compare(account.actionCompletions.length, 0) + compare(account.queuedActions.length, 0) + for (var i = 0; i < answers.length; i++) { + compare(answers[i].failedIds, ["1:INBOX", "2:INBOX"]) + compare(answers[i].error, "Account signed out") + } + record.finish() + wait(30) + compare(answers.length, 2, "late replies cannot finish the same batch again") + compare(record.batches.length, 1, "the queued mutation never reached the provider") + } + function having(item, accept) { if (accept(item)) return item var children = item.children || [] diff --git a/ui/tests/qml/tst_unified_batch.qml b/ui/tests/qml/tst_unified_batch.qml new file mode 100644 index 0000000..12936d8 --- /dev/null +++ b/ui/tests/qml/tst_unified_batch.qml @@ -0,0 +1,130 @@ +import QtQuick +import QtTest +import "../../account" as Account +import "../../account/Unified.js" as Unified + +Item { + QtObject { + id: service + property bool unified: true + property string activeAccountId: "a@example.org" + property string mailboxKey: "inbox" + property string rawQuery: "" + property var messages: [] + property var hosts: ({}) + property string actionStatus: "" + function findAccount(id) { return hosts[id] || null } + function note(text) { actionStatus = text } + } + QtObject { id: app; property var checkedIds: [] } + Account.UnifiedBatchAction { id: batch; service: service } + Account.UnifiedBatchSelection { id: selection; app: app; service: service } + TestCase { + name: "UnifiedBatchCompletion" + property var calls: [] + property var replies: ({}) + property var completed: [] + function key(account, id) { return Unified.unifiedId(account + "@example.org", id) } + function host(account, accepts) { + return {configuredEmail: account + "@example.org", + actionLabel: function(action) { return action === "trash" ? "Moved to trash" : "Marked read" }, + actMany: function(ids, action, callback) { + calls = calls.concat([{account: account, ids: ids.slice(), action: action}]) + if (!accepts) return false + replies[account] = callback + return true + }} + } + function init() { + service.unified = true + service.activeAccountId = "a@example.org" + service.mailboxKey = "inbox" + service.rawQuery = "" + service.messages = [] + service.actionStatus = "" + service.hosts = {"a@example.org": host("a", true), "b@example.org": host("b", true)} + calls = []; replies = ({}); completed = []; app.checkedIds = []; selection.pending = [] + } + function run(ids) { return batch.run(ids, "trash", function(result) { completed.push(result) }) } + function test_success_waits_for_every_account_and_reports_the_whole_selection() { + verify(run([key("a", "42:INBOX"), key("b", "42:Sent Items"), key("a", "43:INBOX")])) + compare(calls.length, 2) + compare(calls[0].ids, ["42:INBOX", "43:INBOX"]) + compare(calls[1].ids, ["42:Sent Items"]) + replies.b({failedIds: []}) + compare(service.actionStatus, "") + compare(completed.length, 0) + replies.a({failedIds: []}) + compare(completed.length, 1) + compare(completed[0].failedIds, []) + verify(service.actionStatus.indexOf("3 of 3 selected across 2 mailboxes") >= 0) + verify(service.actionStatus.indexOf("a@example.org: 2 completed, 0 failed") >= 0) + verify(service.actionStatus.indexOf("b@example.org: 1 completed, 0 failed") >= 0) + replies.a({failedIds: ["42:INBOX"]}) + compare(completed.length, 1, "duplicate completion cannot change the result") + } + function test_failure_keeps_account_identity_and_ignores_unrequested_rows() { + verify(run([key("a", "42:INBOX"), key("b", "42:INBOX")])) + replies.a({failedIds: ["42:INBOX", "42:INBOX", "other", key("b", "42:INBOX")]}) + replies.b({failedIds: []}) + compare(completed[0].failedIds, [key("a", "42:INBOX")]) + verify(service.actionStatus.indexOf("1 of 2 selected") >= 0) + verify(service.actionStatus.indexOf("a@example.org: 0 completed, 1 failed") >= 0) + } + function test_signed_out_or_missing_account_is_counted_without_guessing_a_host() { + service.hosts = {"a@example.org": host("a", false)} + compare(run([key("a", "42:INBOX"), key("b", "42:INBOX")]), false) + compare(calls.length, 1) + compare(completed[0].failedIds, [key("a", "42:INBOX"), key("b", "42:INBOX")]) + verify(service.actionStatus.indexOf("0 of 2 selected") >= 0) + } + function test_mailbox_change_does_not_show_an_old_batch_note() { + verify(run([key("a", "42:INBOX")])) + service.mailboxKey = "unread" + replies.a({failedIds: []}) + compare(service.actionStatus, "") + compare(completed.length, 1) + } + function test_failed_checks_wait_for_rollback_then_leave_user_selection_alone() { + var failed = key("a", "42:INBOX") + var scope = selection.scope + selection.restore([failed], scope) + compare(app.checkedIds, []) + compare(selection.pending, [failed]) + service.messages = [{id: failed}] + tryCompare(app, "checkedIds", [failed]) + compare(selection.pending, []) + app.checkedIds = [] + service.messages = [{id: failed}, {id: key("b", "1:INBOX")}] + wait(0) + compare(app.checkedIds, [], "later reloads cannot recheck a row the user unchecked") + } + function test_failed_checks_never_cross_a_query_or_account_change() { + var scope = selection.scope + selection.restore([key("a", "42:INBOX")], scope) + service.rawQuery = "different search" + compare(selection.pending, []) + service.messages = [{id: key("a", "42:INBOX")}] + selection.restore([key("a", "42:INBOX")], scope) + wait(0) + compare(app.checkedIds, []) + } + function test_turning_unified_off_and_on_invalidates_old_completions() { + var scope = selection.scope + var failed = key("a", "42:INBOX") + verify(run([failed])) + selection.restore([failed], scope) + service.unified = false + compare(selection.pending, []) + service.unified = true + service.messages = [{id: failed}] + replies.a({failedIds: ["42:INBOX"]}) + selection.restore([failed], scope) + wait(0) + compare(app.checkedIds, []) + compare(service.actionStatus, "") + compare(batch.status, "") + compare(batch.error, "") + } + } +} diff --git a/ui/tests/test_unified.js b/ui/tests/test_unified.js index 6b8d399..4e70667 100644 --- a/ui/tests/test_unified.js +++ b/ui/tests/test_unified.js @@ -50,6 +50,21 @@ assert.strictEqual(unified.accountOf("me@example.org" + SEP + "42"), "me@example assert.strictEqual(unified.sourceIdOf("me@example.org" + SEP + "42"), "42") assert.strictEqual(unified.accountOf("42"), "") +// A selection spanning two mailboxes is one batch for each, in each one's own +// ids — the IMAP folder intact — and an id naming no account is dropped. +deepEqual(unified.groupByAccount([ + unified.unifiedId("a@example.org", "1"), + unified.unifiedId("b@example.net", "7:INBOX"), + "42", + unified.unifiedId("a@example.org", "2"), + unified.unifiedId("b@example.net", "8:Sent Items") +]), [ + { accountId: "a@example.org", ids: ["1", "2"] }, + { accountId: "b@example.net", ids: ["7:INBOX", "8:Sent Items"] } +]) +deepEqual(unified.groupByAccount([]), []) +deepEqual(unified.groupByAccount(null), []) + // ---------------------------------------------------------------- merging const sources = [ From cf8088258f0866c8a434e9f776139bd524d8eae4 Mon Sep 17 00:00:00 2001 From: Guido Jouret <116769228+gjouret@users.noreply.github.com> Date: Sun, 11 Oct 2026 10:39:16 +0200 Subject: [PATCH 15/88] Fix empty quotes in replies and forwards started from the message list (#342) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replying to or forwarding a message from the list produces a draft whose quote is empty: the "On …, … wrote:" line followed by a bare `>` for a reply, and no original text for a forward. `MailAccount.select()` sets `selectedMessage` from the listed row at once and clears `selectedBody` until the detail read lands. `startCompose` in `ui/App.qml` held a reply only while `selectedMessage` was missing, so for any listed row the hold never applied and `compose.begin` ran with an empty body. The comment above `pendingComposeMode` already describes the intended behavior ("Held until the fetch lands instead"); the row summary added for fast reader paint is what defeated it. The draft is now held until the detail read has painted (`selectedMessage` present, `detailLoading` false, `detailPainted` true), the same condition `resumeHeldDraft` and `editDraft` already use. The existing `onSelectedBodyChanged` and `onSelectedMessageChanged` handlers resume it. ## Reproduction 1. Open a mailbox with at least two messages and leave the reader on one of them, or on none. 2. Put the cursor on a different row and press the reply or forward key, or use the row's context menu Reply / Forward. Replying on a row that is only previewed does the same. 3. Before: the draft opens immediately with an empty quote. 4. After: the draft opens once the message body has loaded, with the body quoted. Replying from a message that is already open and loaded was not affected. ## Validation - `make validate` passed on integrated head `9258358b1c9c71792e03c9f2ce0367ba2f3bac20`: Rust, JavaScript, shell/source, native QML, no-remote-resource Qt regression, standalone app QML composition, qmllint, plugin validation and diff check. - The AppNavigation regression verifies that a listed summary alone does not open the composer and that the draft opens when detail arrives. Existing compose/navigation cancellation checks pass. - Real-backend component capture assertions confirm that reply, reply-all and forward drafts lack the original body before the fix and contain it afterward. Both matching capture suites pass, with the loading state and resulting draft shown below. - Synchronized with main `b0504b97fb610204b64d2fe13c46926660c24321` by a normal merge. No conflicts or expanded product scope. - Security: PASS for the affected held-compose and quote-input boundary. Validation uses synthetic mail and an isolated real backend; it does not send messages. - Not repeated: hands-on validation in the installed plugin or a live mailbox. Screenshots are controlled component captures with local Qt compatibility imports, as described below. ## Release Notes - Fix replies, replies to all recipients and forwards started from the message list opening with an empty quote. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Maintainer supplemental screenshot evidence Matching before/after captures cover reply, reply-all and forward during detail loading and after draft creation. Dimensions are 1200×800, scale 1, Qt 6.11.2, with the active Osaka Jade palette, identical synthetic data and fonts. Before opens a draft before detail arrives and omits the original body; after retains the reader loading skeleton until completion and then opens a draft with the original body quoted. Captures exercise production App/ComposeView and an isolated real backend message.composeText using synthetic mail; local Qt compatibility imports supply normal input controls. These are controlled component captures, not installed-plugin screenshots. The current synchronized head is `9258358b1c9c71792e03c9f2ce0367ba2f3bac20`; the captures were repeated against that head and current main after the theme-qualifier merge. **AFTER FORWARD DRAFT** ![after-forward-draft](https://github.com/user-attachments/assets/9d6eabd8-b970-4a24-bf30-45c4df0eeded) **AFTER FORWARD LOADING** ![after-forward-loading](https://github.com/user-attachments/assets/a9e93b44-9cc5-4fcc-b52d-24ef5a4c6e70) **AFTER REPLY DRAFT** ![after-reply-draft](https://github.com/user-attachments/assets/4fa92f7d-0b69-4b49-9175-e40ca4bf4fc1) **AFTER REPLY LOADING** ![after-reply-loading](https://github.com/user-attachments/assets/01aa406e-0c50-48a4-a61b-1d29cdbcadea) **AFTER REPLYALL DRAFT** ![after-replyAll-draft](https://github.com/user-attachments/assets/c38e2415-19c9-46e2-92b3-0f48fb414208) **AFTER REPLYALL LOADING** ![after-replyAll-loading](https://github.com/user-attachments/assets/94f35b05-ac70-45ea-9dd3-8b31cfc8dfb4) **BEFORE FORWARD DRAFT** ![before-forward-draft](https://github.com/user-attachments/assets/669ecf85-5e77-459a-a5d3-ac3901fd35eb) **BEFORE FORWARD LOADING** ![before-forward-loading](https://github.com/user-attachments/assets/d4d817cf-9eb2-4af1-95b5-4d69708e7722) **BEFORE REPLY DRAFT** ![before-reply-draft](https://github.com/user-attachments/assets/6350b4a7-e8c9-43a5-aab5-15b0e2de7779) **BEFORE REPLY LOADING** ![before-reply-loading](https://github.com/user-attachments/assets/5705e203-1e47-49d5-8426-2a380c2115c8) **BEFORE REPLYALL DRAFT** ![before-replyAll-draft](https://github.com/user-attachments/assets/d9012775-5de0-47ac-9ce8-0b1088367788) **BEFORE REPLYALL LOADING** ![before-replyAll-loading](https://github.com/user-attachments/assets/3f087fd1-072a-4719-a11b-aabb782e4725) Co-authored-by: Claude Opus 5.5 Co-authored-by: Jason Lee --- ui/App.qml | 10 ++++++++-- ui/tests/qml/tst_app_compose_pending.qml | 2 +- ui/tests/qml/tst_app_navigation.qml | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ui/App.qml b/ui/App.qml index 5904634..fa9232c 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -674,11 +674,17 @@ Item { return currentView === "reader" ? "reader" : "list" } + // A listed row supplies the summary the moment it is selected, before the + // body is read; a draft begun on the summary alone quotes nothing. + function detailReady() { + return !!service.selectedMessage && !service.detailLoading && service.detailPainted + } + function startCompose(mode) { if (!service) return pendingDraftId = "" var next = String(mode || "new") - if (next !== "new" && !service.selectedMessage) { + if (next !== "new" && !detailReady()) { pendingComposeMode = next return } @@ -705,7 +711,7 @@ Item { } function resumeHeldCompose() { - if (pendingComposeMode === "" || !service || !service.selectedMessage) return + if (pendingComposeMode === "" || !service || !detailReady()) return var mode = pendingComposeMode pendingComposeMode = "" startCompose(mode) diff --git a/ui/tests/qml/tst_app_compose_pending.qml b/ui/tests/qml/tst_app_compose_pending.qml index 2f94e8d..93e26a0 100644 --- a/ui/tests/qml/tst_app_compose_pending.qml +++ b/ui/tests/qml/tst_app_compose_pending.qml @@ -378,7 +378,7 @@ Item { mailService.attachmentCallback = null mailService.mailboxKey = "inbox" mailService.detailLoading = false - mailService.detailPainted = false + mailService.detailPainted = true mailService.selectedId = "message-1" mailService.selectedBody = ({ text: "Original body", source: "plain" }) mailService.selectedAttachments = [] diff --git a/ui/tests/qml/tst_app_navigation.qml b/ui/tests/qml/tst_app_navigation.qml index 4fec74d..be10306 100644 --- a/ui/tests/qml/tst_app_navigation.qml +++ b/ui/tests/qml/tst_app_navigation.qml @@ -449,6 +449,23 @@ Item { compare(mailService.count("saveDraft"), 0, "an untouched reply leaves without saving") } + function test_a_reply_raised_from_the_list_waits_for_the_body() { + app.cursorId = "message-1" + app.composeFromCursor("reply") + // What selecting a listed row does: the summary at once, the body later. + mailService.selectedMessage = ({ + id: "message-1", subject: "One", + from: ({ email: "sender@example.com", display: "Sender" }), to: [], cc: [] + }) + wait(50) + compare(app.composing, false, "the summary alone does not begin the draft") + + mailService.land() + tryCompare(app, "composing", true) + app.back() + tryCompare(app, "composing", false) + } + function test_the_shortcut_sheet_closes_before_the_page_under_it() { app.openMessage("message-1") app.runShortcut("help", "") From ca80a522c53d3bd8521be607a2eab827ac3c0d4e Mon Sep 17 00:00:00 2001 From: Guido Jouret <116769228+gjouret@users.noreply.github.com> Date: Sun, 11 Oct 2026 10:39:33 +0200 Subject: [PATCH 16/88] Fix messages falling back to plain text once their pictures load (#343) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A message that draws correctly in Original (or Reader) view drops to plain text a moment later, once its remote pictures have loaded, with the "too heavy" notice. The markup did not change; only the pictures arrived. `sanitize` and the reader's `render` decide `tooHeavy` from the length of the serialised document. With remote pictures approved, each one is carried in that document as a data URI, so the 120,000-character bound, which exists for markup Qt lays out on the GUI thread, is spent on picture bytes. One large picture is enough: the first render (no picture data yet) is under the bound, the second (after `public.image` returns) is far over it. The decision now leaves out the characters of `img` sources that are data URIs. The reported `complexity` is unchanged, so the JavaScript parity corpus still matches; tags, tables and table depth are bounded as before, and each raster is subject to the existing size check (8 MiB). `maxImages` limits fetched remote pictures; inline data images are not counted by that option. Review identified that excluding all data-image source bytes also weakens the existing aggregate rendering guard; that boundary still needs repair and validation. ## Reproduction Seen on a PayPal marketing mail with "always show images" on: 1. Open a message whose sanitised markup is small (here 47,908 characters, 241 tags, no tables) and that has one large remote picture (here a PNG of 2,347,290 characters as a data URI). 2. Before: the Original view draws, then is replaced by the plain-text view as soon as the pictures load. Measured length after embedding: 2,429,678. 3. After: the Original view stays, with its pictures. ## Validation - New `approved_image_bytes_do_not_make_a_small_message_too_heavy` in `src/message/html/tests.rs` fails without the change (`length` 400,056 for a three-tag message, `tooHeavy` true) and passes with it. It also asserts that 130,000 characters of markup are still refused. - A first attempt that changed `complexity.length` itself broke `actual_javascript_sanitizer_and_reader_corpus_matches`, which is why only the decision changed. - `cargo test --locked --features integration-test-credentials`: 688 passed, 0 failed, 11 ignored. - The real message, run through `sanitize` with all nine fetched pictures: `tooHeavy` true before, false after, for both the sanitised document and the reader document. - A backend built with this change is installed in my plugin (0.10.8 checkout). - Not run: `make test-js`, `make test-shell`, `make test-qml`. No QML or JavaScript is changed. ## Screenshots None attached. No view or layout changes. ## Release Notes - Fix messages with a large picture falling back to plain text once their pictures load. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Maintainer supplemental screenshot evidence Matching Original and Reader captures use production MessageReader and native backend outputs, identical synthetic mail, 900×600, scale 1, Qt 6.11.2 and the active Osaka Jade palette. Before, an embedded picture reaching the old URI-length threshold triggers the heavy-rendering refusal and falls back to plain body. After, the heading, embedded picture and paragraph remain rich while the selected Original/Reader mode is preserved. No real mail or remote image resources are used. After source is reviewed head `f1e6a7d`. **AFTER ORIGINAL** ![after-original](https://github.com/user-attachments/assets/091db44e-b38f-4ef0-8889-0b9946895266) **AFTER READER** ![after-reader](https://github.com/user-attachments/assets/34a27bba-bbb2-49ec-84b9-81582311e764) **BEFORE ORIGINAL** ![before-original](https://github.com/user-attachments/assets/7f88602f-be51-4f8e-9109-21f2fc5a28c5) **BEFORE READER** ![before-reader](https://github.com/user-attachments/assets/6d930b63-a805-4636-84c4-8223c3a75604) --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Jason Lee --- docs/MESSAGE-RENDERING.md | 1 + src/message/html/mod.rs | 67 ++++++++++++++++++++++++++++-- src/message/html/reader.rs | 8 ++-- src/message/html/tests.rs | 83 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 7 deletions(-) diff --git a/docs/MESSAGE-RENDERING.md b/docs/MESSAGE-RENDERING.md index 32a4948..faa63e7 100644 --- a/docs/MESSAGE-RENDERING.md +++ b/docs/MESSAGE-RENDERING.md @@ -12,6 +12,7 @@ Required when touching message HTML, images, direction or outgoing MIME. - Hidden-content detection must not treat container `font-size:0`, unclipped `max-height:0`, or `mso-hide:all` as universally hidden. Child font sizes, overflow and Outlook-only semantics matter. - Reading-mode links allow `mailto:` and public-host HTTP(S). Refused addresses leave their labels visible. - Never let Qt fetch original remote image URLs. With images enabled, fetch through `scripts/image_fetch.py` and `public_http.py`: checked public DNS, pinned connections, no redirects, size/deadline bounds, and supported raster signatures matching the declared type. Return only successful images as data URIs; omit pending/refused sources. Exclude tracking pixels and hidden images. +- Sanitized and reading documents share the same policy: `maxImages` bounds all rendered inline, CID and remote images, and each document admits at most 16 MiB of embedded data URI bytes. Image data does not count toward the separate 120,000-character markup/layout refusal. Refused images never reach Qt. - Remote images default off. “Always show” grants the standing preference; Settings revokes it. See [security boundaries](SECURITY-BOUNDARIES.md). ## Direction and MIME diff --git a/src/message/html/mod.rs b/src/message/html/mod.rs index 708c827..0317830 100644 --- a/src/message/html/mod.rs +++ b/src/message/html/mod.rs @@ -243,6 +243,8 @@ pub(super) struct Images<'a> { loadable: usize, sources: Vec, pub limit: usize, + admitted: usize, + embedded_bytes: usize, } impl<'a> Images<'a> { pub fn new(options: &'a Value) -> Self { @@ -253,6 +255,8 @@ impl<'a> Images<'a> { loadable: 0, sources: vec![], limit: bounded_option(options, "maxImages", 24, 256), + admitted: 0, + embedded_bytes: 0, } } pub fn prepared(&self, source: &str) -> String { @@ -267,13 +271,35 @@ impl<'a> Images<'a> { pub fn allowed(&self) -> bool { self.options.get("allowRemoteImages") == Some(&Value::Bool(true)) } + // Count every rendered image, including CID and sender-embedded data. The + // markup length budget no longer counts base64, so it must have its own + // shared budget before either reader hands those bytes to Qt. + fn admit(&mut self, source: &str) -> bool { + const MAX_EMBEDDED_BYTES: usize = 16 * 1024 * 1024; + let bytes = if source + .get(..5) + .is_some_and(|head| head.eq_ignore_ascii_case("data:")) + { + source.len() + } else { + 0 + }; + if self.admitted >= self.limit || bytes > MAX_EMBEDDED_BYTES - self.embedded_bytes { + self.blocked += 1; + return false; + } + self.admitted += 1; + self.embedded_bytes += bytes; + true + } fn keep(&mut self, n: &mut Node) -> bool { if !n.has("src") { return true; } let source = n.attr("src").to_owned(); match image_kind(&source) { - "inline" | "none" => true, + "inline" => self.admit(&source), + "none" => true, "remote" => { if tracking(n) { self.blocked += 1; @@ -283,10 +309,13 @@ impl<'a> Images<'a> { self.loadable += 1 } let prepared = self.prepared(&source); - if !self.allowed() || self.kept >= self.limit || prepared.is_empty() { + if !self.allowed() || prepared.is_empty() { self.blocked += 1; return false; } + if !self.admit(&prepared) { + return false; + } n.set("src", &prepared); self.kept += 1; true @@ -390,6 +419,38 @@ pub fn too_heavy(size: &Value) -> bool { || size["tables"].as_u64().unwrap_or(0) > 60 || size["tableDepth"].as_u64().unwrap_or(0) > 4 } +// The length bounds the markup Qt has to lay out. The bytes of a picture +// carried as a data URI are not markup, and counting them turned a small +// message into the plain-text refusal the moment its pictures loaded. +pub fn too_heavy_to_draw(n: &Node, size: &Value) -> bool { + fn embedded(n: &Node) -> u64 { + n.children + .iter() + .filter(|c| c.kind != "text") + .map(|c| { + let source = c.attr("src"); + let own = if c.name == "img" + && source + .get(..5) + .is_some_and(|head| head.eq_ignore_ascii_case("data:")) + { + source.encode_utf16().count() as u64 + } else { + 0 + }; + own + embedded(c) + }) + .sum() + } + let mut drawn = size.clone(); + drawn["length"] = json!( + size["length"] + .as_u64() + .unwrap_or(0) + .saturating_sub(embedded(n)) + ); + too_heavy(&drawn) +} pub fn read_plain(n: &Node) -> Value { fn walk(n: &Node, text: &mut String, images: &mut Vec) { for c in &n.children { @@ -458,7 +519,7 @@ pub fn sanitize(source: &str, options: &Value) -> Result { collapse(&mut root); let html = serialize(&root)?; let size = measure(&root, &html); - let heavy = too_heavy(&size); + let heavy = too_heavy_to_draw(&root, &size); Ok( json!({"html":html,"blockedImages":images.blocked,"images":images.kept,"remoteImages":images.loadable,"remoteImageSources":images.sources,"complexity":size,"tooHeavy":heavy,"plainText":plain,"reader":reader,"document":root}), ) diff --git a/src/message/html/reader.rs b/src/message/html/reader.rs index c379e8d..0df7c2e 100644 --- a/src/message/html/reader.rs +++ b/src/message/html/reader.rs @@ -219,8 +219,8 @@ fn image(n: &Node, s: &mut State, ctx: &mut Images<'_>) { } else { source.into() }; - if kind == "inline" - || (kind == "remote" && ctx.allowed() && ctx.kept < ctx.limit && !rendered.is_empty()) + if (kind == "inline" || (kind == "remote" && ctx.allowed() && !rendered.is_empty())) + && ctx.admit(&rendered) { if kind == "remote" { ctx.kept += 1 @@ -240,7 +240,7 @@ fn image(n: &Node, s: &mut State, ctx: &mut Images<'_>) { s.pending = false; return; } - if kind == "remote" { + if kind == "remote" && (!ctx.allowed() || rendered.is_empty()) { ctx.blocked += 1 } let alt = source_space(&tree::escape_text(&decode(n.attr("alt")))); @@ -702,6 +702,6 @@ pub fn render(n: &Node, options: &Value) -> Result { let html = serialize(&document)?; let size = measure(&document, &html); Ok( - json!({"html":html,"empty":document.children.is_empty(),"document":document,"images":ctx.kept,"blockedImages":ctx.blocked,"tooHeavy":too_heavy(&size),"complexity":size}), + json!({"html":html,"empty":document.children.is_empty(),"document":document,"images":ctx.kept,"blockedImages":ctx.blocked,"tooHeavy":too_heavy_to_draw(&document, &size),"complexity":size}), ) } diff --git a/src/message/html/tests.rs b/src/message/html/tests.rs index 59873db..81e5817 100644 --- a/src/message/html/tests.rs +++ b/src/message/html/tests.rs @@ -307,3 +307,86 @@ fn attribute_names_and_nonanchor_urls_cannot_create_resource_markup() { assert_eq!(result["remoteImageSources"], json!([])); } } + +// A picture the reader approved arrives as a data URI in the document. Its +// bytes are not markup Qt has to lay out, so one large picture must not turn a +// small message into the plain-text refusal the moment its pictures load. +#[test] +fn approved_image_bytes_do_not_make_a_small_message_too_heavy() { + use base64::{Engine as _, engine::general_purpose::STANDARD}; + let mut png = b"\x89PNG\r\n\x1a\n".to_vec(); + png.resize(300_000, 0); + let data = format!("data:image/png;base64,{}", STANDARD.encode(&png)); + let source = "

Hello

Bye

"; + let blocked = sanitize( + source, + &json!({"allowRemoteImages":false,"withReader":true}), + ) + .unwrap(); + assert_eq!(blocked["tooHeavy"], false); + let shown = sanitize( + source, + &json!({"allowRemoteImages":true,"withReader":true, + "remoteImageData":{"https://cdn.example.com/hero.png":data}}), + ) + .unwrap(); + assert!(shown["html"].as_str().unwrap().contains("{}

", "x".repeat(130_000)); + assert_eq!(sanitize(&markup, &json!({})).unwrap()["tooHeavy"], true); +} + +#[test] +fn inline_and_remote_images_share_the_count_budget() { + let data = "DATA:image/png;base64,iVBORw0KGgoA"; + let source = format!( + "

Body

{}", + format!("inline").repeat(1000) + ); + let result = sanitize( + &source, + &json!({"maxImages":1,"withReader":true, + "allowRemoteImages":true,"remoteImageData":{"https://cdn.example.com/remote":data}}), + ) + .unwrap(); + for value in [&result, &result["reader"]] { + let html = value["html"].as_str().unwrap(); + assert_eq!(html.matches("".repeat(3); + let result = sanitize( + &source, + &json!({"withReader":true,"allowRemoteImages":true, + "remoteImageData":{"https://cdn.example.com/hero":data}}), + ) + .unwrap(); + for value in [&result, &result["reader"]] { + let html = value["html"].as_str().unwrap(); + assert_eq!(html.matches(" Date: Sun, 11 Oct 2026 10:39:48 +0200 Subject: [PATCH 17/88] Read the HTML part when the plain alternative contains markup (#344) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some senders put the markup itself in the `text/plain` alternative of a `multipart/alternative` message. `prepare_content` takes the plain part whenever it is not empty, so the plain-text view of such a message, and anything else built on its text, is a page of tags and style rules. A plain part is now treated as markup, and the HTML part read instead, only when all of these hold: there is an HTML part; the trimmed text opens with a comment, a doctype or a whole opening tag (`html`, `head`, `body`, `div`, `table`, `style`, `center`, `span`, `p`); it ends with `>`; and it contains a closing tag. Text outside this heuristic, such as a quoted address, stays the sender's text, and a message with no HTML part is unchanged. Review found that a legitimate sentence beginning with an opening tag and ending with its closing tag also matches the heuristic and loses its plain alternative; that false-positive case still needs to be addressed. ## Reproduction Seen on a PayPal marketing mail whose `text/plain` part (92,470 bytes) begins `\n

Send money easily

\n", + "

Send money easily

", + "

Send money easily

", + "

Send money easily

", + "

Send money easily

", + "

Send money easily

", + ] { + let body = &super::content::prepare(&message(markup, Some(html)), 0).unwrap()["body"]; + assert_eq!(body["source"], "html", "{markup}"); + assert_eq!(body["text"], "Send money easily", "{markup}"); + } + for text in [ + " wrote:\n> see

in the docs", + "
is a block element, is not.\nRegards", + "Send money easily", + "
is a block element; close it with
", + "

See the docs for

", + " is the root element; close it with ", + " contains metadata; close it with ", + " contains visible content; close it with ", + " contains content ", + "
is closed with
", + "
code
is an example ending with

", + ] { + let body = &super::content::prepare(&message(text, Some(html)), 0).unwrap()["body"]; + assert_eq!(body["source"], "plain", "{text}"); + assert_eq!(body["text"], text, "{text}"); + } + // With no HTML part there is nothing better to read. + let only = "

Send money easily

"; + let body = &super::content::prepare(&message(only, None), 0).unwrap()["body"]; + assert_eq!(body["source"], "plain"); + assert_eq!(body["text"], only); +} From 958643362ed6e61ea3441b47232782388e83e720 Mon Sep 17 00:00:00 2001 From: Farzad Hayat Date: Sun, 11 Oct 2026 18:39:59 +1000 Subject: [PATCH 18/88] Fix IMAP sign-in with pasted password line breaks (#345) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #341. Pasting an IMAP app password with a trailing line break no longer fails with a generic mail error. Sign-in removes only a final CR/LF suffix, preserves valid password characters exactly, and reports an inline error when an unsupported control character remains. The resulting secret is used for both verification and keyring storage. Embedded CR/LF, tab, NUL, other C0 controls and DEL are refused before either operation. Spaces, quotes, backslashes and Unicode remain unchanged. This prevents silent cleanup from verifying and saving a different password. Existing saved-secret reads retain their byte-preserving behavior. ## Verification - Full `make validate` passed locally on the repaired head `d6544c8`, including Rust unit/integration suites, JavaScript, shell regressions, the full QML suite, controlled resource/HTTP tests, standalone composition, qmllint and plugin validation. - Current head `5e89fc2` normally integrates `main` at `b0504b9`. The IMAP implementation and credential regressions are unchanged by that integration; focused auth (76), credential RPC (8) and setup-form QML checks (5), standalone composition and qmllint all passed on the resulting head. - Password cases cover final CR/LF/CRLF suffixes, every embedded C0 control and DEL, trailing nonnewline controls, empty-after-cleanup input, and exact preservation of valid secrets. Refused input emits no verification request and causes no credential-store write. - That repaired regression produces 65 failures against the original blanket-cleanup implementation, demonstrating that it silently changed and accepted unsupported secrets. - The author reported a live-mailbox sign-in on the original version under Omarchy 4.0.4-1. Maintainer verification uses synthetic credentials and controlled fixtures. ## Release Notes - Accept pasted IMAP passwords with a trailing line break while preserving the entered secret. - Explain unsupported password control characters before attempting sign-in. ## Maintainer supplemental screenshot evidence Matching synthetic IMAP setup screenshots use the active Omarchy palette at 900×820, scale 1, Qt 6.11.2. Before shows a generic controlled backend refusal; after reports a specific unsupported control-character error immediately. The fixture invokes production ImapAuth and ImapSetupPage with synthetic settings and a controlled verification refusal, using no real credentials or mailbox. The captured IMAP auth/setup behavior remains unchanged in final reviewed head `5e89fc2`; the base integration adds namespace-qualified theme references from #346. **AFTER** ![after](https://github.com/user-attachments/assets/276c8e62-cdfb-4375-9bad-0d8165b303cd) **BEFORE** ![before](https://github.com/user-attachments/assets/ad725472-ed8c-41cc-bbf8-179c61dc1c44) --------- Co-authored-by: Jason Lee --- ui/providers/ImapAuth.qml | 9 +- ui/tests/qml/tst_credential_rpc.qml | 4 +- .../qml/tst_imap_credential_control_bytes.qml | 122 ++++++++++++++++++ 3 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 ui/tests/qml/tst_imap_credential_control_bytes.qml diff --git a/ui/providers/ImapAuth.qml b/ui/providers/ImapAuth.qml index 89adc79..d539ce3 100644 --- a/ui/providers/ImapAuth.qml +++ b/ui/providers/ImapAuth.qml @@ -175,7 +175,14 @@ Item { lastError = "Missing " + missingTools.join(", ") return false } - var value = String(secret || "") + // Pasting from a line can include its trailing line break. Remove only + // that suffix; silently changing controls inside a password would verify + // and save a different secret from the one the user entered. + var value = String(secret || "").replace(/[\r\n]+$/, "") + if (/[\x00-\x1f\x7f]/.test(value)) { + lastError = "The password contains a control character. Enter it again" + return false + } if (value === "") { lastError = "Enter the password for this mailbox" return false diff --git a/ui/tests/qml/tst_credential_rpc.qml b/ui/tests/qml/tst_credential_rpc.qml index f7f0f17..88eef00 100644 --- a/ui/tests/qml/tst_credential_rpc.qml +++ b/ui/tests/qml/tst_credential_rpc.qml @@ -98,11 +98,11 @@ Item { function test_store_and_delete_are_data_not_process_arguments() { var auth = createTemporaryObject(imapFactory, root) verify(auth) - verify(auth.signIn("$(touch /tmp/never-credential)\r\n")) + verify(auth.signIn("$(touch /tmp/never-credential)")) auth.completeSignIn(true, "") compare(credentials.writes.length, 1) compare(credentials.writes[0].kind, "imap-password") - compare(credentials.writes[0].secret, "$(touch /tmp/never-credential)\r\n") + compare(credentials.writes[0].secret, "$(touch /tmp/never-credential)") auth.logout() compare(credentials.deletes, [{kind:"imap-password",accountId:"imap:one@example.org",clientId:""}]) } diff --git a/ui/tests/qml/tst_imap_credential_control_bytes.qml b/ui/tests/qml/tst_imap_credential_control_bytes.qml new file mode 100644 index 0000000..4d82913 --- /dev/null +++ b/ui/tests/qml/tst_imap_credential_control_bytes.qml @@ -0,0 +1,122 @@ +import QtQuick +import QtTest +import "../../providers" as Providers + +Item { + id: root + + QtObject { + id: credentials + property bool backendCanStoreCredentials: true + property var writes: [] + function reset() { writes = [] } + function credentialGet(kind, accountId, clientId, callback) { + return true + } + function credentialPut(kind, accountId, clientId, secret, callback) { + writes = writes.concat([{kind:kind,accountId:accountId,clientId:clientId,secret:secret}]) + callback(true, "") + return true + } + function credentialDelete(kind, accountId, clientId, callback) { + callback(true, "") + return true + } + } + + Component { + id: imapFactory + Providers.ImapAuth { + pluginDir: "/synthetic" + platform: credentials + accountId: "imap:one@example.org" + settings: ({imapHost:"imap.example.org",imapPort:993,smtpHost:"smtp.example.org", + smtpPort:465,username:"one@example.org",aliases:[],insecure:false}) + } + } + + TestCase { + name: "ImapCredentialControlBytes" + + function init() { credentials.reset() } + + function test_a_pasted_password_loses_only_its_trailing_line_break_data() { + return [ + {tag: "CR", suffix: "\r"}, + {tag: "LF", suffix: "\n"}, + {tag: "CRLF", suffix: "\r\n"}, + {tag: "several line breaks", suffix: "\r\n\n"} + ] + } + + function test_a_pasted_password_loses_only_its_trailing_line_break(data) { + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + var sent = "" + auth.verifyRequested.connect(function(settings, value) { sent = value }) + verify(auth.signIn("app-password " + data.suffix)) + compare(sent, "one@example.org:app-password ") + auth.completeSignIn(true, "") + compare(credentials.writes.length, 1) + compare(credentials.writes[0].secret, "app-password ") + } + + function test_controls_are_refused_without_verification_or_storage_data() { + var cases = [] + for (var byte = 0; byte < 32; byte++) { + cases.push({tag: "embedded-" + byte, value: "before" + String.fromCharCode(byte) + "after"}) + if (byte !== 10 && byte !== 13) + cases.push({tag: "trailing-" + byte, value: "password" + String.fromCharCode(byte)}) + } + cases.push({tag: "embedded-DEL", value: "before\u007fafter"}) + cases.push({tag: "trailing-DEL", value: "password\u007f"}) + cases.push({tag: "internal-CRLF", value: "before\r\nafter\r\n"}) + return cases + } + + function test_controls_are_refused_without_verification_or_storage(data) { + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + var verifications = 0 + auth.verifyRequested.connect(function(settings, value) { verifications++ }) + verify(!auth.signIn(data.value)) + compare(auth.lastError, "The password contains a control character. Enter it again") + compare(verifications, 0) + compare(credentials.writes.length, 0) + compare(auth.pendingPassword, "") + compare(auth.loginBusy, false) + compare(auth.password, "") + } + + function test_clean_passwords_are_unchanged_data() { + return [ + {tag: "plain", value: "app-password"}, + {tag: "spaces", value: " leading and trailing "}, + {tag: "Unicode", value: "päss密碼🔑"}, + {tag: "quotes and backslashes", value: "a\"'b\\c:$()"} + ] + } + + function test_clean_passwords_are_unchanged(data) { + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + var sent = "" + auth.verifyRequested.connect(function(settings, value) { sent = value }) + verify(auth.signIn(data.value)) + compare(sent, "one@example.org:" + data.value) + auth.completeSignIn(true, "") + compare(credentials.writes.length, 1) + compare(credentials.writes[0].secret, data.value) + } + + function test_only_line_breaks_do_not_verify_or_store() { + var auth = createTemporaryObject(imapFactory, root) + var verifications = 0 + auth.verifyRequested.connect(function(settings, value) { verifications++ }) + verify(!auth.signIn("\r\n")) + compare(auth.lastError, "Enter the password for this mailbox") + compare(verifications, 0) + compare(credentials.writes.length, 0) + } + } +} From 6d0a7e7b9fcd2b0ff489f62d13de597fb486ae42 Mon Sep 17 00:00:00 2001 From: taisau Date: Sun, 11 Oct 2026 02:17:59 -0700 Subject: [PATCH 19/88] Copy the subject line from the reader header (#262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What changes A right-click on the reader-header subject line opens a one-row menu, **Copy subject**, and copies the subject as written to the clipboard — use cases like ticket IDs, order numbers and other verbatim strings want the subject whole, and today it had to be selected by hand or retyped. The menu is `ui/components/SubjectMenu.qml`, in the AddressMenu shape: a `QQC.Popup`, one `MenuActionRow`, keyboard navigation through the same `Menu.js` helpers (J/K/arrows, Enter/O), `closePolicy` outside-click/Escape. Copy is a signal the host routes — `MessageReader` hands it to `service.copyText`, the path the body's `TextMenu` already uses, so the text reaches `wl-copy` as a single argv entry with no shell in between. The reader owns the menu rather than App, the reason the body's menu was moved into the reader: it is tested with the summary it reads. The subject `Text` gains a `RightButton` `TapHandler` beside the ones the From and To lines already carry. The menu anchors to the line's own lower edge rather than the click point, and an empty or whitespace-only subject refuses to open, the way `AddressMenu.openAt` refuses a line that held no addresses. ## Verification `make validate` green on the commit sent, run against **v0.10.7** (`a3b7616`) as the base: QML `1166 passed, 0 failed`, the Rust suite `597 passed, 0 failed` plus the standalone composition test, and `qmllint`, `omarchy plugin validate .` and `git diff --check` are clean. The QML count is up from `997` at the previous base because 0.10.7 adds its own cases. The branch was rebased onto 0.10.7 after the original review. The rebase applied the four-file payload without a conflict, so the diff is byte-identical to what was reviewed — same `Makefile` line, the same 27 added lines in `MessageReader.qml`, and both new files. What changed underneath it is `MessageReader.qml` itself, which 0.10.7 rewrites (+84/-25 across `afcd736`, `af8a7ae`); the `SubjectMenu` block sits at the end of the file and the `subjectLine` `TapHandler` in the reader header, and the 6 cases below pass against that rewritten reader. `ui/tests/qml/tst_reader_subject_menu.qml` (6 cases) covers the right-click opening the menu, the single row named for what it copies, that copy routes the exact subject through the service and closes, that an empty or whitespace-only subject opens nothing and copies nothing, and that the menu lands at the line's edge rather than under the pointer. The two cases added after review were each watched failing with their fix reverted (`6 passed, 2 failed`) and pass with it (`8 passed, 0 failed`). A fresh, context-free agent reviewed the diff against `AGENTS.md`. It found no blocker — security verdict **PASS**: the subject reaches `wl-copy` through the same no-shell `service.copyText` channel the body's Copy already uses — and three findings, all answered: - The popup anchored to the click point; it now anchors to the trigger line's own lower edge (`MessageReader.qml`), per the Popups rule. - The empty-subject guard was exact-match, so a whitespace-only subject still opened the menu; it now refuses when the subject trims to empty, while still copying the text verbatim. - The subject line holds no selected style while its menu is open. Left as it is: the neighbouring From, To and body triggers have the same omission, and aligning all four is a change of its own. Driven live on the running shell: right-click a subject → **Copy subject** → paste elsewhere. ## Release Notes - Right-click a message's subject to copy it. --------- Co-authored-by: taisau Co-authored-by: Jason Lee --- Makefile | 6 +- app/qml/imports/Quickshell/Quickshell.qml | 12 +- app/tests/qml/tst_host_contract.qml | 10 + tests/test_clipboard.py | 43 ++++ ui/Service.qml | 4 +- ui/components/MessageReader.qml | 63 ++++-- ui/components/SubjectMenu.qml | 119 +++++++++++ .../qml/imports/Quickshell/Quickshell.qml | 6 +- ui/tests/qml/tst_clipboard_arguments.qml | 41 ++++ ui/tests/qml/tst_reader_subject_menu.qml | 187 ++++++++++++++++++ 10 files changed, 471 insertions(+), 20 deletions(-) create mode 100644 tests/test_clipboard.py create mode 100644 ui/components/SubjectMenu.qml create mode 100644 ui/tests/qml/tst_clipboard_arguments.qml create mode 100644 ui/tests/qml/tst_reader_subject_menu.qml diff --git a/Makefile b/Makefile index e6fb285..d71e08d 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ QMLLINT := /usr/lib/qt6/bin/qmllint .DEFAULT_GOAL := test QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml \ ui/tests/compatibility/tst_published_agent.qml \ - ui/tests/qml/tst_unified_batch.qml \ + ui/tests/qml/tst_unified_batch.qml ui/tests/qml/tst_clipboard_arguments.qml \ + ui/tests/qml/tst_reader_subject_menu.qml \ ui/backend/Backend.qml ui/backend/Runtime.qml ui/diagnostics/Diagnostics.qml \ ui/components/BackendSetup.qml ui/components/OmamailLogo.qml \ ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/UnifiedBatchAction.qml ui/account/UnifiedBatchSelection.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ @@ -58,7 +59,7 @@ QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryContr ui/components/LabelMenu.qml \ ui/components/LabelMovePicker.qml \ ui/components/NamePrompt.qml \ - ui/components/AddressMenu.qml ui/components/TextMenu.qml \ + ui/components/AddressMenu.qml ui/components/TextMenu.qml ui/components/SubjectMenu.qml \ ui/components/ComposeAgent.qml \ ui/agent/AgentRunner.qml ui/agent/AgentContext.qml \ ui/agent/EventSuggester.qml ui/components/EventSuggestionCard.qml \ @@ -194,6 +195,7 @@ test-shell-portable: python3 tests/test_contacts.py python3 tests/test_qml_names.py python3 tests/test_qml_text_format.py + python3 tests/test_clipboard.py bash tests/test_source.sh bash tests/test_service_source.sh bash tests/test_config_store.sh diff --git a/app/qml/imports/Quickshell/Quickshell.qml b/app/qml/imports/Quickshell/Quickshell.qml index 4f02ce7..544de73 100644 --- a/app/qml/imports/Quickshell/Quickshell.qml +++ b/app/qml/imports/Quickshell/Quickshell.qml @@ -14,11 +14,15 @@ QtObject { } function execDetached(command) { - if (!nativeHost || !Array.isArray(command) || command.length !== 2) return false - if (command[0] === "xdg-open" && typeof nativeHost.openExternal === "function") + if (!nativeHost || !Array.isArray(command)) return false + if (command.length === 2 && command[0] === "xdg-open" && typeof nativeHost.openExternal === "function") return !!nativeHost.openExternal(String(command[1])) - if (command[0] === "wl-copy" && typeof nativeHost.setClipboard === "function") - return !!nativeHost.setClipboard(String(command[1])) + if (command[0] === "wl-copy" && typeof nativeHost.setClipboard === "function") { + if (command.length === 3 && command[1] === "--") + return !!nativeHost.setClipboard(String(command[2])) + if (command.length === 2) + return !!nativeHost.setClipboard(String(command[1])) + } return false } diff --git a/app/tests/qml/tst_host_contract.qml b/app/tests/qml/tst_host_contract.qml index d5f4d72..b112d17 100644 --- a/app/tests/qml/tst_host_contract.qml +++ b/app/tests/qml/tst_host_contract.qml @@ -157,6 +157,16 @@ TestCase { compare(host.copied.length, 1) } + function test_clipboard_option_terminator_keeps_text_verbatim() { + var subjects = ["--version", "--clear", "-p", "田中 \"quoted\" \\ body\nline two", ""] + for (var i = 0; i < subjects.length; i++) + verify(Quickshell.execDetached(["wl-copy", "--", subjects[i]])) + compare(host.copied, subjects) + verify(!Quickshell.execDetached(["wl-copy", "--type", "text/html"])) + verify(!Quickshell.execDetached(["wl-copy", "--", "text", "extra"])) + compare(host.copied, subjects) + } + function test_notification_process_routes_to_native_host() { var process = createTemporaryObject(notificationProcessComponent, testCase) verify(process) diff --git a/tests/test_clipboard.py b/tests/test_clipboard.py new file mode 100644 index 0000000..e50bc3c --- /dev/null +++ b/tests/test_clipboard.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""Verify Service's real wl-copy arguments cannot execute clipboard options.""" +import json +import os +from pathlib import Path +import shutil +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[1] +runner = shutil.which("qmltestrunner6") or shutil.which("qmltestrunner") +if not runner and Path("/usr/lib/qt6/bin/qmltestrunner").exists(): + runner = "/usr/lib/qt6/bin/qmltestrunner" +clipboard = shutil.which("wl-copy") +if not runner or not clipboard: + print("Clipboard transport regression SKIP: requires QML test tooling and wl-copy") + raise SystemExit(0) + +with tempfile.TemporaryDirectory(prefix="omamail-clipboard-") as directory: + env = dict(os.environ, XDG_RUNTIME_DIR=directory, + XDG_CONFIG_HOME=directory, XDG_DATA_HOME=directory, + XDG_CACHE_HOME=directory, XDG_STATE_HOME=directory, + WAYLAND_DISPLAY="unavailable-review-display", + QT_QPA_PLATFORM="offscreen", QT_QUICK_BACKEND="software", + QT_QPA_PLATFORMTHEME="", GSETTINGS_BACKEND="memory") + result = subprocess.run([runner, "-input", str(ROOT / "ui/tests/qml/tst_clipboard_arguments.qml"), + "-import", str(ROOT / "ui/tests/qml/imports")], + env=env, text=True, capture_output=True, timeout=30) + assert result.returncode == 0, result.stdout + result.stderr + commands = [json.loads(line.split("OMAMAIL_CLIPBOARD_ARGUMENTS:", 1)[1]) + for line in result.stdout.splitlines() if "OMAMAIL_CLIPBOARD_ARGUMENTS:" in line] + assert len(commands) == 7, result.stdout + for command in commands: + result = subprocess.run(command, env=env, text=True, capture_output=True, timeout=5) + assert result.returncode != 0, (command, result.stdout, result.stderr) + assert result.stdout == "", (command, result.stdout) + assert "Failed to connect to a Wayland server" in result.stderr, (command, result.stderr) + # Negative control: the vulnerable argument list executes --version and + # succeeds, even though this display cannot accept clipboard writes. + vulnerable = subprocess.run([clipboard, "--version"], env=env, + text=True, capture_output=True, timeout=5) + assert vulnerable.returncode == 0 and "wl-clipboard" in vulnerable.stdout +print("Clipboard transport regression PASS; no clipboard connection or option action") diff --git a/ui/Service.qml b/ui/Service.qml index d02543d..c3ded4f 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -501,7 +501,9 @@ Item { var text = String(value === undefined || value === null ? "" : value) if (platform && typeof platform.setClipboard === "function") return platform.setClipboard(text) - return Quickshell.execDetached(["wl-copy", text]) + // NUL cannot cross argv without truncating the sender's text. + if (text.indexOf("\0") >= 0) return false + return Quickshell.execDetached(["wl-copy", "--", text]) } function configPath(name) { diff --git a/ui/components/MessageReader.qml b/ui/components/MessageReader.qml index e2533eb..2bfd57a 100644 --- a/ui/components/MessageReader.qml +++ b/ui/components/MessageReader.qml @@ -369,19 +369,44 @@ Item { anchors.topMargin: backBar.visible ? Style.space(14) : 0 spacing: Style.space(4) - Text { + Item { width: parent.width - // A stranger wrote this. Qt's default AutoText switches a string that - // looks like markup into rich text, and rich text with an in it is - // a fetch — the same beacon the message body is stripped of. - textFormat: Text.PlainText - text: root.summary ? root.summary.subject : "" - color: root.textColor - font.family: root.panelFontFamily - font.pixelSize: Style.font.subtitle - font.bold: true - wrapMode: Text.WordWrap - horizontalAlignment: root.subjectAlignment + height: subjectLine.implicitHeight + Rectangle { + objectName: "reader-subject-selection" + anchors.fill: parent + radius: Style.cornerRadius + visible: subjectLine.selected + color: Qt.rgba(root.textColor.r, root.textColor.g, root.textColor.b, 0.08) + } + Text { + id: subjectLine + objectName: "reader-subject" + width: parent.width + // A stranger wrote this. Qt's default AutoText switches a string that + // looks like markup into rich text, and rich text with an in it is + // a fetch — the same beacon the message body is stripped of. + textFormat: Text.PlainText + text: root.summary ? root.summary.subject : "" + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.subtitle + font.bold: true + wrapMode: Text.WordWrap + horizontalAlignment: root.subjectAlignment + + readonly property bool selected: subjectMenu.opened + TapHandler { + acceptedButtons: Qt.RightButton + onTapped: function(eventPoint) { + if (!root.summary) return + // Anchor to the line's own lower edge, not the pointer, so the menu + // lands in the same place however the line was pressed. + var scene = subjectLine.mapToGlobal(0, subjectLine.height) + subjectMenu.openAt(root.summary.subject, scene.x, scene.y) + } + } + } } Text { @@ -1109,6 +1134,20 @@ Item { onOpenLinkRequested: function(url) { root.openLink(url) } } + // The subject's own menu: one row, the subject as written. Beside the + // body's menu for the same reason that menu lives here. + SubjectMenu { + id: subjectMenu + objectName: "reader-subject-menu" + textColor: root.textColor + popupBackgroundColor: root.popupBackgroundColor + popupBorderColor: root.popupBorderColor + panelFontFamily: root.panelFontFamily + onCopyRequested: function(subject) { + if (root.service && typeof root.service.copyText === "function") root.service.copyText(subject) + } + } + ImagePopover { id: imagePopover textColor: root.textColor diff --git a/ui/components/SubjectMenu.qml b/ui/components/SubjectMenu.qml new file mode 100644 index 0000000..6c0a595 --- /dev/null +++ b/ui/components/SubjectMenu.qml @@ -0,0 +1,119 @@ +import QtQuick +import QtQuick.Controls as QQC +import qs.Commons +import qs.Ui +import "Menu.js" as Menu + +// The menu a right-click on the reader's subject opens: the subject as +// written, copied whole. One row, the way AddressMenu is one pattern — Copy +// goes out as a signal because the host owns the platform clipboard. The +// empty subject refuses an open, the way AddressMenu refuses a line that +// held no addresses. +Item { + id: root + + required property color textColor + required property color popupBackgroundColor + required property color popupBorderColor + required property string panelFontFamily + + // The subject the click landed on, set at open time rather than bound. + property string subject: "" + property int cursorIndex: -1 + readonly property bool opened: menu.opened + readonly property var menuRows: [copyRow] + readonly property alias copyRow: copyRow + // Exposed so a test can assert where the menu landed, the way copyRow is. + readonly property alias popup: menu + + signal copyRequested(string subject) + + anchors.fill: parent + z: 50 + + property real anchorX: 0 + property real anchorY: 0 + + function openAt(text, sceneX, sceneY) { + subject = String(text || "") + // A subject of only spaces is one the reader cannot show anything for; it + // is refused like an empty one. The text is still copied verbatim, so a + // real subject keeps the spacing it was sent with. + if (subject.trim() === "") return + var local = root.mapFromGlobal(sceneX, sceneY) + anchorX = local.x + anchorY = local.y + menu.open() + } + + function place() { + if (!menu.visible) return + var tall = menu.height > 0 ? menu.height : menu.implicitHeight + var placed = Menu.position(anchorX, anchorY, menu.width, tall, root.width, root.height) + menu.x = placed.x + menu.y = placed.y + } + + function selectableRows() { + var values = [] + for (var i = 0; i < menuRows.length; i++) values.push({ + selectable: true, visible: menuRows[i].visible, enabled: menuRows[i].enabled + }) + return values + } + function moveCursor(step) { cursorIndex = Menu.nextSelectable(selectableRows(), cursorIndex, step) } + function runCursor() { if (cursorIndex >= 0) menuRows[cursorIndex].activated() } + function close() { menu.close() } + + QQC.Popup { + id: menu + width: Style.space(180) + implicitHeight: rows.implicitHeight + Style.space(8) + padding: Style.space(4) + modal: false + focus: true + closePolicy: QQC.Popup.CloseOnEscape | QQC.Popup.CloseOnPressOutside + onHeightChanged: root.place() + onOpened: { + root.cursorIndex = Menu.firstSelectable(root.selectableRows()) + root.place() + } + background: Rectangle { + radius: Style.cornerRadius + color: root.popupBackgroundColor + border.width: 1 + border.color: root.popupBorderColor + } + + contentItem: Column { + id: rows + spacing: Style.space(2) + + focus: true + Keys.onPressed: function(event) { + if (event.key === Qt.Key_J || event.key === Qt.Key_Down) { + root.moveCursor(1); event.accepted = true + } else if (event.key === Qt.Key_K || event.key === Qt.Key_Up) { + root.moveCursor(-1); event.accepted = true + } else if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter + || event.key === Qt.Key_O) { + root.runCursor(); event.accepted = true + } + } + + MenuRow { + id: copyRow + text: "Copy subject" + onActivated: { var subject = root.subject; menu.close(); root.copyRequested(subject) } + } + } + } + + component MenuRow: MenuActionRow { + width: menu.width - menu.leftPadding - menu.rightPadding + textColor: root.textColor + panelFontFamily: root.panelFontFamily + collection: root.menuRows + cursorIndex: root.cursorIndex + } +} diff --git a/ui/tests/qml/imports/Quickshell/Quickshell.qml b/ui/tests/qml/imports/Quickshell/Quickshell.qml index 4293058..461683b 100644 --- a/ui/tests/qml/imports/Quickshell/Quickshell.qml +++ b/ui/tests/qml/imports/Quickshell/Quickshell.qml @@ -5,7 +5,11 @@ QtObject { property var notifications: [] signal notificationActivated(string accountId, string messageId) function env(name) { return "" } - function execDetached(command) {} + property var detachedCommands: [] + function execDetached(command) { + detachedCommands = detachedCommands.concat([command.slice()]) + return true + } function showNotification(id, title, body, accountId, messageId) { notifications = notifications.concat([{ id: String(id), title: String(title), body: String(body), diff --git a/ui/tests/qml/tst_clipboard_arguments.qml b/ui/tests/qml/tst_clipboard_arguments.qml new file mode 100644 index 0000000..1a4aae5 --- /dev/null +++ b/ui/tests/qml/tst_clipboard_arguments.qml @@ -0,0 +1,41 @@ +import QtQuick +import QtTest +import Quickshell +import "../.." as Omamail + +Item { + Component { id: factory; Omamail.Service {} } + TestCase { + name: "ClipboardArguments" + function init() { Quickshell.detachedCommands = [] } + function test_sender_text_cannot_be_a_clipboard_option() { + var service = createTemporaryObject(factory, parent) + verify(service) + var values = ["--version", "--clear", "-p", "--type=text/html", + "田中 \"quoted\" \\ body\nline two", "", "trailing\r\n"] + for (var i = 0; i < values.length; i++) { + verify(service.copyText(values[i])) + var command = Quickshell.detachedCommands[i] + compare(command, ["wl-copy", "--", values[i]]) + // The transport regression consumes the command actually emitted by + // Service, then runs wl-copy against an unavailable display. + console.log("OMAMAIL_CLIPBOARD_ARGUMENTS:" + JSON.stringify(command)) + } + } + function test_nul_is_refused_before_any_process_starts() { + var service = createTemporaryObject(factory, parent) + verify(service) + verify(!service.copyText("--clear\0text")) + compare(Quickshell.detachedCommands, []) + } + function test_native_clipboard_receives_text_without_process_arguments() { + var service = createTemporaryObject(factory, parent) + verify(service) + var copied = [] + service.platform = {setClipboard: function(text) { copied.push(text); return true }} + verify(service.copyText("--clear")) + compare(copied, ["--clear"]) + compare(Quickshell.detachedCommands, []) + } + } +} diff --git a/ui/tests/qml/tst_reader_subject_menu.qml b/ui/tests/qml/tst_reader_subject_menu.qml new file mode 100644 index 0000000..bd6310f --- /dev/null +++ b/ui/tests/qml/tst_reader_subject_menu.qml @@ -0,0 +1,187 @@ +import QtQuick 2.15 +import QtTest 1.3 +import qs.Commons +import "../../components" as Omamail +import "../../message/Html.js" as Html + +// A right-click on the reader's subject: one row, the subject as written, +// copied through the host. +Item { + width: 900 + height: 600 + + readonly property string senderHtml: '

Person 1 moved 4 cards.

' + + readonly property var rendered: Html.sanitize(senderHtml, ({ withReader: true })) + + QtObject { + id: mailService + + property var copied: [] + + property string selectedId: "message-9" + property var selectedMessage: ({ + id: "message-9", + subject: "Activity on Sprint board", + from: ({ display: "Boards", email: "boards@example.com" }), + to: [({ display: "Reader", email: "reader@example.com" })], + fullTime: "24 August 2026", + starred: false + }) + property bool detailLoading: false + property bool detailPainted: true + property bool selectedHasHtml: rendered.html !== "" + property var selectedDocument: rendered.document + property var selectedReaderDocument: rendered.reader.document + property bool selectedReaderTooHeavy: rendered.reader.tooHeavy + property bool selectedReaderEmpty: rendered.reader.empty + property int selectedRemoteImages: rendered.remoteImages + property bool remoteImagesAllowed: false + property bool selectedTooHeavy: rendered.tooHeavy + property string unsubscribeLabel: "" + property string unsubscribeDetail: "" + property bool unsubscribing: false + property var selectedBody: ({ text: "Person 1 moved 4 cards.", source: "html" }) + property var selectedImages: [] + property var selectedInvite: null + property string selectedResponse: "" + property bool canRespondToInvite: false + property bool rsvpSending: false + property bool canArchive: true + property bool canOpenOnWeb: false + property var selectedAttachments: [] + + function getMessage() {} + function showRemoteImages() {} + function copyText(text) { copied = copied.concat([String(text)]); return true } + function openExternal(url) { return true } + } + + Omamail.MessageReader { + id: reader + width: 900 + height: 600 + service: mailService + textColor: Qt.rgba(1, 1, 1, 1) + backgroundColor: Qt.rgba(0.06, 0.06, 0.06, 1) + accentColor: Qt.rgba(1, 0.5, 0, 1) + linkColor: Qt.rgba(0.3, 0.7, 1, 1) + dimColor: Qt.rgba(0.67, 0.67, 0.67, 1) + popupBackgroundColor: Qt.rgba(0.13, 0.13, 0.13, 1) + popupBorderColor: Qt.rgba(0.47, 0.47, 0.47, 1) + leadingBoundaryOverlap: 0 + dimmerColor: Qt.rgba(0.47, 0.47, 0.47, 1) + panelFontFamily: "monospace" + bodyMode: "reader" + onBodyModeRequested: function(mode) { reader.bodyMode = mode } + } + + TestCase { + name: "ReaderSubjectMenu" + when: windowShown + + function named(item, name) { + if (!item) return null + if (item.objectName === name) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var hit = named(values[i], name) + if (hit) return hit + } + return null + } + + function subjectLine() { + var line = named(reader, "reader-subject") + verify(line, "the reader draws its subject in a Text") + return line + } + + function menu() { + var found = named(reader, "reader-subject-menu") + verify(found, "the reader owns a subject menu") + return found + } + + function init() { + menu().close() + mailService.selectedMessage.subject = "Activity on Sprint board" + mailService.copied = [] + } + + function cleanup() { menu().close() } + + function test_a_right_click_on_the_subject_opens_the_menu() { + mouseClick(subjectLine(), 30, subjectLine().height / 2, Qt.RightButton) + tryVerify(function() { return menu().opened }, 1000, "the menu opens") + wait(20) + compare(menu().copyRow.enabled, true) + } + + function test_copy_copies_the_subject_through_the_service() { + mouseClick(subjectLine(), 30, subjectLine().height / 2, Qt.RightButton) + tryVerify(function() { return menu().opened }, 1000, "the menu opens") + wait(20) + menu().copyRow.activated() + tryVerify(function() { return menu().opened === false }, 1000, "the menu closes") + compare(mailService.copied, ["Activity on Sprint board"]) + } + + function test_the_menu_holds_one_row_named_for_what_it_copies() { + mouseClick(subjectLine(), 30, subjectLine().height / 2, Qt.RightButton) + tryVerify(function() { return menu().opened }, 1000, "the menu opens") + wait(20) + compare(menu().menuRows.length, 1) + compare(menu().copyRow.text, "Copy subject") + } + + function test_an_empty_subject_does_not_open_the_menu() { + mailService.selectedMessage.subject = "" + mouseClick(subjectLine(), 30, subjectLine().height / 2, Qt.RightButton) + wait(20) + compare(menu().opened, false) + compare(mailService.copied, []) + } + + function test_a_whitespace_only_subject_does_not_open_the_menu() { + mailService.selectedMessage.subject = " " + mouseClick(subjectLine(), 30, subjectLine().height / 2, Qt.RightButton) + wait(20) + compare(menu().opened, false) + compare(mailService.copied, []) + } + + function test_the_subject_stays_selected_until_the_menu_closes() { + compare(subjectLine().selected, false) + mouseClick(subjectLine(), 30, subjectLine().height / 2, Qt.RightButton) + tryVerify(function() { return menu().opened }) + compare(subjectLine().selected, true) + var selection = named(reader, "reader-subject-selection") + verify(selection.visible) + verify(selection.color.a > 0) + compare(selection.height, subjectLine().height) + keyClick(Qt.Key_Escape) + tryVerify(function() { return !menu().opened }) + compare(subjectLine().selected, false) + compare(selection.visible, false) + } + + function test_keyboard_activation_copies_the_snapshot_verbatim() { + var subject = " --version \\ <工单> \"quoted\"\nline two " + mailService.selectedMessage.subject = subject + mouseClick(subjectLine(), 30, subjectLine().height / 2, Qt.RightButton) + tryVerify(function() { return menu().opened }) + keyClick(Qt.Key_Return) + tryVerify(function() { return !menu().opened }) + compare(mailService.copied, [subject]) + } + + function test_the_menu_anchors_to_the_line_not_the_pointer() { + mouseClick(subjectLine(), 120, subjectLine().height / 2, Qt.RightButton) + tryVerify(function() { return menu().opened }, 1000, "the menu opens") + wait(20) + compare(menu().popup.x, subjectLine().mapToItem(menu().popup.parent, 0, 0).x, + "the menu anchors to the line's edge, not the click point") + } + } +} From c8d57c325933342a32dbd7f617589c4e9737029f Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 11 Oct 2026 17:40:06 +0800 Subject: [PATCH 20/88] chore: Fix release source size and Windows Rust test coverage (#352) Two existing issues prevent a fully verified release: `App.qml` exceeds the 128 KiB source-size ceiling, and the Windows release job omits the complete standalone Rust suite. Extract reply/forward startup and deferred resume into `compose/PendingCompose.qml`, bringing `App.qml` from 131,279 to 130,345 bytes. App retains ownership of pending state, and the controller receives the composer and assistant surfaces explicitly. Replies still wait for painted message detail and retain the existing assistant conversation handoff. The module is included in lint inputs. Run the complete standalone Rust suite in the Windows release job before building the backend. PowerShell checks its exit code and stops on failure, so packaging cannot continue after failed tests. Add a workflow regression requiring this complete suite on each native release platform and the Windows failure guard. This matches the native release requirements in `docs/BACKEND-RUNTIME.md`. Verification: - Full `make validate` passed for the source extraction (`92d553d`) with Qt 6.11.2, including 1,283 QML cases, standalone composition, source-size checks, lint, plugin validation, and whitespace checks; the focused compose suite passed all 99 cases. - After incorporating current main and #262, the combined QML suite passed all 1,298 cases, with Outlook HTTP, sidebar, native HTML resource-denial, and lint checks passing. Source-size and real clipboard transport regressions passed. - All 13 release workflow contract cases, 26 backend release cases, release-source checks, and publish-script tests passed. The workflow regression fails with the Windows test invocation removed. - The local standalone backend API 7 contract passed. Windows native release results will be established by the authoritative release job; this PR does not claim those results in advance. Security: PASS for the affected compose state and release failure guard. The extraction preserves existing app-owned account/message inputs; the workflow adds test coverage without changing publication permissions, secrets, or build destinations. --- .github/workflows/release.yml | 2 ++ Makefile | 2 +- app/tests/test_release_workflow.py | 12 +++++++++ ui/App.qml | 41 +++++++----------------------- ui/compose/PendingCompose.qml | 41 ++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 33 deletions(-) create mode 100644 ui/compose/PendingCompose.qml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ffcbfa9..fdd5d30 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -295,6 +295,8 @@ jobs: QT_QUICK_CONTROLS_FALLBACK_STYLE: Basic run: | $ErrorActionPreference = "Stop" + cargo test --locked --no-default-features --features standalone,integration-test-credentials + if ($LASTEXITCODE -ne 0) { throw "standalone Rust tests failed" } cargo build --release --locked --no-default-features --features standalone if ($LASTEXITCODE -ne 0) { throw "standalone backend build failed" } cmake -S app -B build/app -G "Visual Studio 17 2022" -A x64 -DOMAMAIL_BACKEND="$PWD/target/release/omamail.exe" diff --git a/Makefile b/Makefile index d71e08d..c862068 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ QMLLINT := /usr/lib/qt6/bin/qmllint .DEFAULT_GOAL := test -QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml \ +QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml ui/compose/PendingCompose.qml \ ui/tests/compatibility/tst_published_agent.qml \ ui/tests/qml/tst_unified_batch.qml ui/tests/qml/tst_clipboard_arguments.qml \ ui/tests/qml/tst_reader_subject_menu.qml \ diff --git a/app/tests/test_release_workflow.py b/app/tests/test_release_workflow.py index 76e4cc9..9afd3a5 100644 --- a/app/tests/test_release_workflow.py +++ b/app/tests/test_release_workflow.py @@ -102,6 +102,18 @@ def test_each_native_job_runs_backend_qt_qml_package_installer_and_live_smoke_ga self.assertIn("package-release.sh", block) self.assertIn('--host "$PWD/build/app/omamail-app"', block) + def test_every_native_release_runs_the_complete_standalone_rust_suite(self): + command = "cargo test --locked --no-default-features --features standalone,integration-test-credentials" + for job in APP_JOBS: + with self.subTest(job=job): + block = job_block(self.workflow, job) + self.assertIn(command, block) + self.assertLess(block.index(command), block.index("cargo build --release")) + if "windows" in job: + # A failed cargo invocation must stop PowerShell before it + # can build or publish an archive. + self.assertIn(command + '\n if ($LASTEXITCODE -ne 0) { throw "standalone Rust tests failed" }', block) + def test_pull_requests_build_and_exercise_each_production_standalone_archive(self): forbidden = ("gh release create", "gh release edit", "publish-backend.sh") for job, (runner, target, archive) in CI_APP_JOBS.items(): diff --git a/ui/App.qml b/ui/App.qml index fa9232c..dadb1fa 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -12,6 +12,7 @@ import "account" as Account import "account/Accounts.js" as Accounts import "account/Navigation.js" as Nav import "compose/Recovery.js" as Recovery +import "compose" as Compose import "keys/Keymap.js" as Keymap import "providers/Registry.js" as Provider import "agent/Agent.js" as Agent @@ -674,33 +675,14 @@ Item { return currentView === "reader" ? "reader" : "list" } - // A listed row supplies the summary the moment it is selected, before the - // body is read; a draft begun on the summary alone quotes nothing. - function detailReady() { - return !!service.selectedMessage && !service.detailLoading && service.detailPainted - } - - function startCompose(mode) { - if (!service) return - pendingDraftId = "" - var next = String(mode || "new") - if (next !== "new" && !detailReady()) { - pendingComposeMode = next - return - } - pendingComposeMode = "" - var keepAssistant = agentPrompt.opened - var conversation = keepAssistant ? agentPrompt.job : null - compose.begin(next, service.selectedMessage, service.selectedBody.text, - service.selectedAttachments) - if (keepAssistant) { - if ((next === "reply" || next === "replyAll") && conversation - && String(conversation.accountId) === compose.accountId - && String(conversation.messageId) === compose.replyMessageId) - compose.agentParentJobId = String(conversation.id) - composeAgent.open() - } + Compose.PendingCompose { + id: pendingCompose + app: root + composer: compose + assistant: root.agentPrompt + composeAssistant: root.composeAgent } + function startCompose(mode) { pendingCompose.start(mode) } // A mailto: URL, or the blank draft `compose: true` asks for. The window is // already open when this runs — summon delivers the payload to open(). @@ -710,12 +692,7 @@ Item { compose.beginDraft(draft) } - function resumeHeldCompose() { - if (pendingComposeMode === "" || !service || !detailReady()) return - var mode = pendingComposeMode - pendingComposeMode = "" - startCompose(mode) - } + function resumeHeldCompose() { pendingCompose.resume() } function resumeHeldDraft() { if (pendingDraftId === "" || !service) return diff --git a/ui/compose/PendingCompose.qml b/ui/compose/PendingCompose.qml new file mode 100644 index 0000000..9fb98b4 --- /dev/null +++ b/ui/compose/PendingCompose.qml @@ -0,0 +1,41 @@ +import QtQuick + +// A reply waits for the body, not merely the summary supplied by the list. +QtObject { + required property var app + required property var composer + required property var assistant + required property var composeAssistant + readonly property var service: app.service + function detailReady() { + return !!service && !!service.selectedMessage && !service.detailLoading && service.detailPainted + } + function start(mode) { + if (!service) return + app.pendingDraftId = "" + var next = String(mode || "new") + if (next !== "new" && !detailReady()) { + app.pendingComposeMode = next + return + } + app.pendingComposeMode = "" + var keepAssistant = assistant.opened + var conversation = keepAssistant ? assistant.job : null + composer.begin(next, service.selectedMessage, service.selectedBody.text, + service.selectedAttachments) + if (keepAssistant) { + if ((next === "reply" || next === "replyAll") && conversation + && String(conversation.accountId) === composer.accountId + && String(conversation.messageId) === composer.replyMessageId) + composer.agentParentJobId = String(conversation.id) + composeAssistant.open() + } + } + + function resume() { + if (app.pendingComposeMode === "" || !detailReady()) return + var mode = app.pendingComposeMode + app.pendingComposeMode = "" + start(mode) + } +} From 5db17740f99feb1252ba136db20d9fb67899cd91 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 11 Oct 2026 20:11:10 +0800 Subject: [PATCH 21/88] chore: Publish v0.11.0 and repair Windows backend runtime (#353) Publish Omamail v0.11.0 from current main, including the merged subject-copying and release-validation repairs. This release advances the published backend to API 7, enabling sender names for IMAP and Outlook accounts while retaining the fixed API gates in QML. Stop the Windows persistent backend promptly when its response pipe closes, including while stdin remains open. Give the Windows backend entry thread an 8 MiB stack to avoid command and persistent-dispatch stack overflows. Restore Windows OAuth admission and default MIME boundary generation by using the same operating-system cryptographic random provider already used by IMAP cursors. Correct the native test fixtures for Windows filenames, absolute paths, UTF-8 Python transport, and connection-refusal latency. Isolate Windows CLI integration children from native user directories with a debug-only test feature; production builds ignore that hint. Give synthetic storage its required owner-only protected ACL and compare owner/DACL and timestamps in no-write assertions. Hostile attachment display names and the assertions against unwanted writes, sends, and credential-bearing requests remain covered. Release CI builds the two static Linux backends and all three standalone desktop packages from one source commit. It verifies native security contracts, backend APIs, package installation and rollback, and smoke tests before publishing the complete immutable asset set. It downloads and verifies the public assets before committing `backend-version` and the folded API metadata on this branch. Merge only after that pin commit and the checks for its exact revision pass. ## Validation Release source `70c295b` passed both static Linux backend jobs and the complete macOS, Linux and Windows standalone jobs in https://github.com/huacnlee/omamail/actions/runs/38136130747 (attempt 2). Native credential, private-file/IPC, process, network, QML, API, installation/rollback and smoke checks completed before publication. Windows passed 497 library tests, all 33 CLI tests and all three stdio regressions. The authoritative workflow published all ten immutable assets, verified their public downloads, then committed only `backend-version` and the folded API metadata as `88bdf6a`. The pin is now 0.11.0 and released API 7, with no unreleased API entries. Exact-pin PR checks are running before merge. Local Linux CLI integration tests (33) and MIME parity tests (9) passed; full local `make validate` is still running. Security: PASS for the affected release boundaries on all three native platform runners. Standalone archives retain the documented unsigned/unnotarized distribution limitations. ## Release Notes - Add a sender name for IMAP and Outlook mailboxes and support selections across accounts in the unified view. - Right-click a message's subject to copy it in full, including text beginning with a dash. - Fix Windows backend stack overflows, OAuth sign-in, and outgoing MIME generation. - Update the Omarchy backend and standalone desktop applications to v0.11.0. --------- Co-authored-by: Omamail Release <41898282+github-actions[bot]@users.noreply.github.com> --- Cargo.lock | 2 +- Cargo.toml | 2 +- app/CMakeLists.txt | 2 +- backend-api.json | 6 +- backend-version | 2 +- manifest.json | 2 +- src/auth/callback.rs | 13 ++-- src/backend/mail_action_tests.rs | 7 +- src/backend/stdio.rs | 60 +++++++++++++++-- src/calendar/discovery_runtime_tests.rs | 1 + src/compose/tests.rs | 3 +- src/main.rs | 16 +++++ src/message/compose.rs | 6 +- src/platform/dirs.rs | 21 ++++++ src/providers/gmail_http_runtime_tests.rs | 7 +- tests/cli.rs | 79 ++++++++++++++++++++--- tests/cli/search.rs | 6 +- tests/platform_native.rs | 22 +++++++ 18 files changed, 224 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 815ba2c..8c4573b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1320,7 +1320,7 @@ dependencies = [ [[package]] name = "omamail" -version = "0.10.8" +version = "0.11.0" dependencies = [ "base64", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 47cf702..9aa7b1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "omamail" -version = "0.10.8" +version = "0.11.0" edition = "2024" publish = false diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index c1e3f40..a4695bc 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.21) -project(omamail-app VERSION 0.10.8 LANGUAGES CXX) +project(omamail-app VERSION 0.11.0 LANGUAGES CXX) if(WIN32) enable_language(RC) diff --git a/backend-api.json b/backend-api.json index aa043c5..24bdb28 100644 --- a/backend-api.json +++ b/backend-api.json @@ -1,6 +1,6 @@ { "apiVersion": 7, - "releasedApiVersion": 6, + "releasedApiVersion": 7, "protocolVersion": 1, "methods": [ "system.info", @@ -1291,8 +1291,6 @@ ], "unreleased": { "methods": [], - "cases": [ - "mail send dry run names the IMAP sender" - ] + "cases": [] } } diff --git a/backend-version b/backend-version index 1a46c7f..d9df1bb 100644 --- a/backend-version +++ b/backend-version @@ -1 +1 @@ -0.10.8 +0.11.0 diff --git a/manifest.json b/manifest.json index a5434af..3746eff 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "id": "omamail", "name": "Omamail", - "version": "0.10.8", + "version": "0.11.0", "author": "huacnlee", "license": "MIT", "homepage": "https://huacnlee.github.io/omamail/", diff --git a/src/auth/callback.rs b/src/auth/callback.rs index 42acd96..5cbee81 100644 --- a/src/auth/callback.rs +++ b/src/auth/callback.rs @@ -2,7 +2,7 @@ use super::*; use base64::Engine; use sha2::{Digest, Sha256}; -use std::{collections::HashMap, io::Read}; +use std::collections::HashMap; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, net::TcpListener, @@ -15,8 +15,9 @@ pub(super) struct Flows(Mutex Result { let mut bytes = [0; 32]; - std::fs::File::open("/dev/urandom") - .and_then(|mut f| f.read_exact(&mut bytes)) + rustls::crypto::ring::default_provider() + .secure_random + .fill(&mut bytes) .map_err(|_| "auth_random_failed")?; Ok(base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)) } @@ -320,7 +321,7 @@ mod tests { let address = listener.local_addr().unwrap(); assert!(!listener_closes(address, Duration::from_millis(60)).await); drop(listener); - assert!(listener_closes(address, Duration::from_secs(1)).await); + assert!(listener_closes(address, Duration::from_secs(5)).await); } #[tokio::test] @@ -343,7 +344,9 @@ mod tests { // connection refusal within a bound, not closure at one scheduling // instant. The separate live-listener regression prevents this probe // from treating a timeout or a successful connection as closure. - assert!(listener_closes(address, Duration::from_secs(1)).await); + // Windows can take more than a second to report connection refusal; + // the probe still requires refusal, never a timeout or a successful connect. + assert!(listener_closes(address, Duration::from_secs(5)).await); } #[tokio::test] diff --git a/src/backend/mail_action_tests.rs b/src/backend/mail_action_tests.rs index a3218b5..af3c409 100644 --- a/src/backend/mail_action_tests.rs +++ b/src/backend/mail_action_tests.rs @@ -481,7 +481,12 @@ async fn production_jmap_send_preview_only_reads_identity_and_preserves_storage( } let fixture = account_fixture(json!({"version":1,"activeId":ACCOUNT, "accounts":[{"provider":"jmap","email":"user@example.test"}]})); - let attachment = fixture.root.join("quote\\工\".txt"); + // Keep the hostile display name separate from a Windows-valid filename. + let attachment = fixture.root.join(if cfg!(windows) { + "attachment-工.txt" + } else { + "quote\\工\".txt" + }); fs::write(&attachment, b"synthetic attachment").unwrap(); fs::create_dir_all(fixture.state.join("omamail")).unwrap(); fs::write(fixture.state.join("omamail/outbox.json"), b"[]\n").unwrap(); diff --git a/src/backend/stdio.rs b/src/backend/stdio.rs index 677b26f..ff91e21 100644 --- a/src/backend/stdio.rs +++ b/src/backend/stdio.rs @@ -55,10 +55,62 @@ impl Read for Input { } #[cfg(windows)] { - // Windows anonymous pipes do not support poll(2). A failed stdout - // will be observed before the next request; otherwise the blocking - // stdin read is exactly the backend protocol's required wait. - return io::stdin().read(bytes); + use std::os::windows::io::AsRawHandle; + use windows_sys::Win32::{ + Foundation::ERROR_BROKEN_PIPE, + Storage::FileSystem::{FILE_TYPE_PIPE, GetFileType, ReadFile}, + System::Pipes::PeekNamedPipe, + }; + let mut input = io::stdin(); + let handle = input.as_raw_handle(); + if unsafe { GetFileType(handle) } != FILE_TYPE_PIPE { + return input.read(bytes); + } + // We are the sole reader. Read only bytes already available so an + // open, idle stdin cannot conceal a failed response pipe forever. + loop { + if self.failed.load(Ordering::Acquire) { + return Err(io::Error::new(io::ErrorKind::BrokenPipe, "output closed")); + } + let mut available = 0; + if unsafe { + PeekNamedPipe( + handle, + std::ptr::null_mut(), + 0, + std::ptr::null_mut(), + &mut available, + std::ptr::null_mut(), + ) + } == 0 + { + let error = io::Error::last_os_error(); + return if error.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) { + Ok(0) + } else { + Err(error) + }; + } + if available == 0 { + std::thread::sleep(std::time::Duration::from_millis(10)); + continue; + } + let count = bytes.len().min(available as usize) as u32; + let mut read = 0; + if unsafe { + ReadFile( + handle, + bytes.as_mut_ptr(), + count, + &mut read, + std::ptr::null_mut(), + ) + } == 0 + { + return Err(io::Error::last_os_error()); + } + return Ok(read as usize); + } } #[cfg(unix)] loop { diff --git a/src/calendar/discovery_runtime_tests.rs b/src/calendar/discovery_runtime_tests.rs index 1ad1e1c..9521db1 100644 --- a/src/calendar/discovery_runtime_tests.rs +++ b/src/calendar/discovery_runtime_tests.rs @@ -24,6 +24,7 @@ impl reqwest::dns::Resolve for LoopbackDns { impl Peer { fn new() -> Self { let mut process = Command::new("python3") + .env("PYTHONUTF8", "1") .arg(concat!( env!("CARGO_MANIFEST_DIR"), "/src/calendar/discovery_tls_test.py" diff --git a/src/compose/tests.rs b/src/compose/tests.rs index 0454e28..fc06594 100644 --- a/src/compose/tests.rs +++ b/src/compose/tests.rs @@ -263,7 +263,8 @@ fn attachment_metadata_never_opens_paths_and_invalid_paths_write_nothing() { } #[test] fn js_oracle_parity_for_normalized_drafts() { - let fixtures = json!([saved("hello"),saved(" "),{"version":1,"active":true,"returnView":"calendar","draft":{"to":"a@example.org","draftAttachments":[{"path":"/tmp/synthetic","data":"discard","filename":"a","size":12}],"replyRecipients":[{"email":"a@example.org"}]},"parked":[{"subject":"parked","accountId":"other"},{}]}, {"version":1,"active":false}, {"version":2,"active":true}]); + let path = std::env::temp_dir().join("synthetic"); + let fixtures = json!([saved("hello"),saved(" "),{"version":1,"active":true,"returnView":"calendar","draft":{"to":"a@example.org","draftAttachments":[{"path":path,"data":"discard","filename":"a","size":12}],"replyRecipients":[{"email":"a@example.org"}]},"parked":[{"subject":"parked","accountId":"other"},{}]}, {"version":1,"active":false}, {"version":2,"active":true}]); let script = r#"const fs=require('fs'),vm=require('vm');const ctx={};vm.createContext(ctx);vm.runInContext(fs.readFileSync('ui/tests/oracles/Recovery.js','utf8').replace(/^\.pragma library\s*/,''),ctx);const fixtures=JSON.parse(fs.readFileSync(0,'utf8'));process.stdout.write(JSON.stringify(fixtures.map(v=>ctx.parse(JSON.stringify(v)))));"#; use std::process::{Command, Stdio}; let mut child = Command::new("node") diff --git a/src/main.rs b/src/main.rs index d247d70..58f1fa9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,19 @@ fn main() { + #[cfg(windows)] + { + // Windows reserves only 1 MiB for the executable's initial thread. + // The shared CLI/backend dispatch futures need the same stack budget + // as the Unix entry point, including in unoptimized native test builds. + let result = std::thread::Builder::new() + .name("omamail-main".into()) + .stack_size(8 * 1024 * 1024) + .spawn(omamail::cli::run) + .expect("start backend entry thread") + .join(); + if let Err(panic) = result { + std::panic::resume_unwind(panic); + } + } + #[cfg(not(windows))] omamail::cli::run(); } diff --git a/src/message/compose.rs b/src/message/compose.rs index f557985..11b8307 100644 --- a/src/message/compose.rs +++ b/src/message/compose.rs @@ -8,7 +8,6 @@ use base64::{ }; use chrono::{DateTime, Datelike, Local}; use serde_json::{Value, json}; -use std::io::Read; type Result = std::result::Result; fn field<'a>(v: &'a Value, key: &str) -> &'a str { v[key].as_str().unwrap_or("") @@ -136,8 +135,9 @@ fn boundary(value: &str) -> Result { return Ok(stated); } let mut random = [0; 16]; - std::fs::File::open("/dev/urandom") - .and_then(|mut f| f.read_exact(&mut random)) + rustls::crypto::ring::default_provider() + .secure_random + .fill(&mut random) .map_err(|_| "random_unavailable")?; Ok(format!("=_Omamail_{}", URL_SAFE_NO_PAD.encode(random))) } diff --git a/src/platform/dirs.rs b/src/platform/dirs.rs index 24b8a44..bc0e75e 100644 --- a/src/platform/dirs.rs +++ b/src/platform/dirs.rs @@ -65,6 +65,13 @@ impl AppDirs { { return Ok(override_dirs.dirs); } + #[cfg(all(windows, feature = "integration-test-credentials"))] + if let Some(root) = integration_root()? { + return Self::from_roots( + root.join("config"), root.join("cache"), root.join("state"), + root.join("runtime"), root.join("downloads"), + ); + } Self::discover_with(|name| std::env::var_os(name), &std::env::temp_dir()) } pub fn home() -> Result { @@ -76,6 +83,10 @@ impl AppDirs { { return Ok(override_dirs.home); } + #[cfg(all(windows, feature = "integration-test-credentials"))] + if let Some(root) = integration_root()? { + return absolute(root.join("home")); + } #[cfg(windows)] { return known_folder(&windows_sys::Win32::UI::Shell::FOLDERID_Profile); @@ -173,6 +184,16 @@ impl AppDirs { self.config.join(APP_DIRECTORY) } } +// CLI integration children cannot use the unit-test in-memory override. This +// is compiled only with the test credential feature, which is forbidden in +// release builds by credentials/mod.rs. Production keeps native Known Folders. +#[cfg(all(windows, feature = "integration-test-credentials"))] +fn integration_root() -> Result, &'static str> { + std::env::var_os("OMAMAIL_INTEGRATION_HOME") + .map(|root| absolute(PathBuf::from(root))) + .transpose() +} + fn absolute(path: PathBuf) -> Result { if !path.is_absolute() || path diff --git a/src/providers/gmail_http_runtime_tests.rs b/src/providers/gmail_http_runtime_tests.rs index 3cb0b64..fee5924 100644 --- a/src/providers/gmail_http_runtime_tests.rs +++ b/src/providers/gmail_http_runtime_tests.rs @@ -143,7 +143,12 @@ async fn mail_send_preview_reads_google_identity_without_mutation_or_local_write std::fs::write(directory.join("sentinel"), b"unchanged existing state").unwrap(); } std::fs::write(fixture.state.join("omamail/outbox.json"), b"[]\n").unwrap(); - let attachment = fixture.root.join("quote\\工\".txt"); + // Keep the hostile display name separate from a Windows-valid filename. + let attachment = fixture.root.join(if cfg!(windows) { + "attachment-工.txt" + } else { + "quote\\工\".txt" + }); std::fs::write(&attachment, b"private attachment bytes").unwrap(); let before = fixture_tree(&fixture.root); let body = json!({"access_token":"synthetic-access-token","expires_in":3600, diff --git a/tests/cli.rs b/tests/cli.rs index d70f087..71a3dc5 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -56,8 +56,10 @@ fn no_arguments_print_help_without_starting_a_gui() { #[test] fn serve_runs_the_persistent_backend() { + let fixture = mail_list_fixture(9, false); let mut child = Command::new(env!("CARGO_BIN_EXE_omamail")) .arg("serve") + .env("OMAMAIL_INTEGRATION_HOME", &fixture.0) .env_remove("HOME") .env_remove("XDG_CONFIG_HOME") .env_remove("XDG_CACHE_HOME") @@ -74,7 +76,7 @@ fn serve_runs_the_persistent_backend() { .unwrap(); drop(child.stdin.take()); let output = child.wait_with_output().unwrap(); - assert!(output.status.success()); + assert!(output.status.success(), "{output:?}"); assert!(output.stderr.is_empty()); let replies: Vec = output .stdout @@ -112,6 +114,7 @@ fn call_in_empty_home(method: &str, params: &[u8]) -> Output { std::fs::create_dir_all(&home).unwrap(); let mut child = Command::new(env!("CARGO_BIN_EXE_omamail")) .args(["call", method, "--json"]) + .env("OMAMAIL_INTEGRATION_HOME", &home) .env("XDG_CONFIG_HOME", &home) .env("HOME", &home) .stdin(Stdio::piped()) @@ -143,6 +146,13 @@ fn mail_list_fixture(imap_port: u16, keyring_succeeds: bool) -> MailListFixture )); let config = config_root(&root).join("omamail"); fs::create_dir_all(&config).unwrap(); + #[cfg(windows)] + { + // These synthetic ancestors are created by std::fs, so unlike the + // native private-directory creator they inherit the temp directory ACL. + set_mode(&root, 0o700); + set_mode(&config_root(&root), 0o700); + } fs::write( config.join("accounts.json"), serde_json::json!({ @@ -220,6 +230,7 @@ fn assert_no_runtime_storage(root: &Path) { fn call_mail_list(root: &Path, account: &str) -> Output { let mut child = Command::new(env!("CARGO_BIN_EXE_omamail")) .args(["call", "mail.list", "--json"]) + .env("OMAMAIL_INTEGRATION_HOME", root) .env("XDG_CONFIG_HOME", root.join("config")) .env("HOME", root.join("home")) .env("PATH", root.join("bin")) @@ -253,6 +264,7 @@ fn call_mail_list(root: &Path, account: &str) -> Output { fn call_mail_read(root: &Path, account: &str, id: &str) -> Output { let mut child = Command::new(env!("CARGO_BIN_EXE_omamail")) .args(["call", "mail.read", "--json"]) + .env("OMAMAIL_INTEGRATION_HOME", root) .env("XDG_CONFIG_HOME", root.join("config")) .env("HOME", root.join("home")) .env("PATH", root.join("bin")) @@ -288,7 +300,21 @@ fn set_mode(path: &Path, mode: u32) { fs::set_permissions(path, fs::Permissions::from_mode(mode)).unwrap(); } #[cfg(windows)] -fn set_mode(_: &Path, _: u32) {} +fn set_mode(path: &Path, _: u32) { + // Native private storage requires a protected DACL and this user's owner + // SID. A no-op Unix-mode stub does not make an inherited Windows file private. + let user = std::env::var("USERNAME").unwrap(); + let grant = if path.is_dir() { + format!("{user}:(OI)(CI)F") + } else { + format!("{user}:F") + }; + for arguments in [vec!["/setowner".to_owned(), user.clone()], + vec!["/inheritance:r".to_owned(), "/grant:r".to_owned(), grant]] { + let output = Command::new("icacls").arg(path).args(arguments).output().unwrap(); + assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr)); + } +} #[cfg(unix)] fn metadata(path: &Path) -> (u64, (i64, i64), (i64, i64)) { @@ -301,8 +327,26 @@ fn metadata(path: &Path) -> (u64, (i64, i64), (i64, i64)) { } #[cfg(windows)] fn metadata(path: &Path) -> (u64, (i64, i64), (i64, i64)) { + use std::hash::{Hash, Hasher}; + use std::os::windows::{ffi::OsStrExt, fs::MetadataExt}; + use windows_sys::Win32::Security::{GetFileSecurityW, DACL_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION}; let metadata = fs::metadata(path).unwrap(); - (metadata.len(), (0, 0), (0, 0)) + let name: Vec = path.as_os_str().encode_wide().chain(Some(0)).collect(); + let flags = DACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION; + let mut needed = 0; + unsafe { GetFileSecurityW(name.as_ptr(), flags, std::ptr::null_mut(), 0, &mut needed) }; + assert!(needed > 0); + let mut descriptor = vec![0u8; needed as usize]; + assert_ne!(unsafe { GetFileSecurityW(name.as_ptr(), flags, descriptor.as_mut_ptr().cast(), needed, &mut needed) }, 0); + descriptor.truncate(needed as usize); + let mut hash = std::collections::hash_map::DefaultHasher::new(); + descriptor.hash(&mut hash); + // Capture bytes separately in fixture_snapshot, and here preserve native + // timestamps, attributes, owner and DACL: a same-size write or permission + // repair must not pass a read-only regression merely because length stayed put. + (metadata.len(), + (metadata.last_write_time() as i64, metadata.creation_time() as i64), + (metadata.file_attributes() as i64, hash.finish() as i64)) } type FixtureSnapshot = Vec<( @@ -406,7 +450,11 @@ async fn malformed_final_imap_action_id_prevents_all_network_and_cache_changes() let mut mutations = 0; loop { let mut line = String::new(); - if reader.read_line(&mut line).await.unwrap() == 0 { + if match reader.read_line(&mut line).await { + Ok(n) => n == 0, + Err(error) if error.kind() == std::io::ErrorKind::ConnectionReset => true, + Err(error) => panic!("synthetic IMAP read failed: {error}"), + } { break; } let response = if line.starts_with("O1 LOGIN ") { @@ -779,6 +827,7 @@ fn pretty_call_errors_go_to_stderr() { fn root_mail(root: &Path, args: &[&str], body: &[u8]) -> Output { let mut child = Command::new(env!("CARGO_BIN_EXE_omamail")) .args(args) + .env("OMAMAIL_INTEGRATION_HOME", root) .env("XDG_CONFIG_HOME", root.join("config")) .env("XDG_CACHE_HOME", root.join("cache")) .env("XDG_STATE_HOME", root.join("state")) @@ -952,7 +1001,11 @@ fn root_mail_errors_use_stable_envelopes_and_never_fall_back_accounts() { #[test] fn send_previews_repeatable_inputs_and_strict_bounded_utf8_without_writes() { let fixture = mail_list_fixture(9, false); - let attachment = fixture.0.join("quoted\\工\".txt"); + let attachment = fixture.0.join(if cfg!(windows) { + "attachment-工.txt" + } else { + "quoted\\工\".txt" + }); fs::write(&attachment, b"private attachment").unwrap(); let second = fixture.0.join("second.txt"); fs::write(&second, b"two").unwrap(); @@ -984,7 +1037,7 @@ fn send_previews_repeatable_inputs_and_strict_bounded_utf8_without_writes() { serde_json::json!({"ok":true,"result":{ "dryRun":true,"executed":false,"accountId":"imap:imap@example.org","from":"imap@example.org", "to":["one@example.org","two@example.org"],"cc":["three@example.org"],"bcc":["four@example.org"], - "subject":"Unicode 工","body":body,"attachments":[{"name":"quoted\\工\".txt","size":18},{"name":"second.txt","size":3}] + "subject":"Unicode 工","body":body,"attachments":[{"name":attachment.file_name().unwrap().to_str().unwrap(),"size":18},{"name":"second.txt","size":3}] }}) ); for body in [ @@ -1072,6 +1125,7 @@ fn only_send_consumes_stdin_and_human_previews_escape_untrusted_text() { ] { let mut child = Command::new(env!("CARGO_BIN_EXE_omamail")) .args(args) + .env("OMAMAIL_INTEGRATION_HOME", &fixture.0) .env("XDG_CONFIG_HOME", fixture.0.join("config")) .env("HOME", fixture.0.join("home")) .env("PATH", fixture.0.join("bin")) @@ -1181,6 +1235,7 @@ async fn real_smtp_send(generic: bool, acknowledge: bool, owner: bool, crash: bo let mut desktop = if owner { let mut child = tokio::process::Command::new(env!("CARGO_BIN_EXE_omamail")) .arg("serve") + .env("OMAMAIL_INTEGRATION_HOME", &fixture.0) .env("XDG_CONFIG_HOME", fixture.0.join("config")) .env("XDG_STATE_HOME", fixture.0.join("state")) .env("XDG_CACHE_HOME", fixture.0.join("cache")) @@ -1455,7 +1510,11 @@ async fn imap_action_preview_requires_discovered_destinations_and_execution_uses let mut moves = 0; loop { let mut line = String::new(); - if reader.read_line(&mut line).await.unwrap() == 0 { + if match reader.read_line(&mut line).await { + Ok(n) => n == 0, + Err(error) if error.kind() == std::io::ErrorKind::ConnectionReset => true, + Err(error) => panic!("synthetic IMAP read failed: {error}"), + } { break; } let response = if line.starts_with("O1 LOGIN ") { @@ -1573,7 +1632,11 @@ async fn imap_archive_keeps_confirmed_folder_results_when_later_select_refuses() let mut commands = Vec::new(); loop { let mut line = String::new(); - if reader.read_line(&mut line).await.unwrap() == 0 { + if match reader.read_line(&mut line).await { + Ok(n) => n == 0, + Err(error) if error.kind() == std::io::ErrorKind::ConnectionReset => true, + Err(error) => panic!("synthetic IMAP read failed: {error}"), + } { break; } let response = if line.starts_with("O1 LOGIN ") { diff --git a/tests/cli/search.rs b/tests/cli/search.rs index 4bb021e..76a0e86 100644 --- a/tests/cli/search.rs +++ b/tests/cli/search.rs @@ -23,7 +23,11 @@ async fn search_pages_survive_process_exit_and_deletion_of_the_cursor_message() }; loop { let mut cmd = String::new(); - if reader.read_line(&mut cmd).await.unwrap() == 0 { + if match reader.read_line(&mut cmd).await { + Ok(n) => n == 0, + Err(error) if error.kind() == std::io::ErrorKind::ConnectionReset => true, + Err(error) => panic!("synthetic IMAP read failed: {error}"), + } { break; } let mut data = String::new(); diff --git a/tests/platform_native.rs b/tests/platform_native.rs index f6e1888..61c51f9 100644 --- a/tests/platform_native.rs +++ b/tests/platform_native.rs @@ -44,3 +44,25 @@ fn windows_process_command_line_query_reads_live_child_arguments() { "queried command line omitted the child marker" ); } + +#[cfg(all(windows, not(feature = "integration-test-credentials")))] +#[test] +fn production_directories_ignore_integration_root_hint_without_writes() { + use omamail::platform::dirs::AppDirs; + let root = std::env::temp_dir().canonicalize().unwrap() + .join(format!("omamail-forbidden-integration-root-{}", std::process::id())); + if std::env::var_os("OMAMAIL_DIRECTORY_PROBE_CHILD").is_some() { + let hint = std::path::PathBuf::from(std::env::var_os("OMAMAIL_INTEGRATION_HOME").unwrap()); + assert!(!AppDirs::discover().unwrap().config.starts_with(&hint)); + assert!(!AppDirs::home().unwrap().starts_with(&hint)); + assert!(!hint.exists(), "the production directory lookup must not write to the hint"); + return; + } + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "production_directories_ignore_integration_root_hint_without_writes"]) + .env("OMAMAIL_DIRECTORY_PROBE_CHILD", "1") + .env("OMAMAIL_INTEGRATION_HOME", &root) + .output().unwrap(); + assert!(output.status.success(), "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + assert!(!root.exists()); +} From 3848414224923ba67276b31fe9b620d223b9dfe5 Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 18:41:37 -0500 Subject: [PATCH 22/88] Remove personal dictionary storage and keep session-only spelling ignore --- Makefile | 2 +- README.md | 2 +- app/qml/StandaloneShell.qml | 2 +- app/tests/qml/tst_standalone_composition.qml | 7 -- docs/KEYS.md | 2 +- scripts/config-store.sh | 4 +- tests/test_config_store.sh | 13 --- ui/Service.qml | 7 +- ui/components/ComposeView.qml | 12 -- ui/components/SettingsPage.qml | 19 +-- ui/components/TextMenu.qml | 10 +- ui/compose/PersonalWords.qml | 85 -------------- ui/compose/SpellcheckAdapter.qml | 14 --- ui/compose/Spelling.js | 22 ---- ui/compose/SpellingState.qml | 6 - ui/tests/qml/tst_service_settings.qml | 117 ------------------- ui/tests/qml/tst_spellcheck_adapter.qml | 6 +- ui/tests/qml/tst_spellcheck_composer.qml | 48 -------- ui/tests/qml/tst_spellcheck_menu.qml | 15 +-- ui/tests/test_spelling.js | 12 -- 20 files changed, 16 insertions(+), 389 deletions(-) delete mode 100644 ui/compose/PersonalWords.qml diff --git a/Makefile b/Makefile index 4116c50..43c1b94 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ QMLLINT := /usr/lib/qt6/bin/qmllint .DEFAULT_GOAL := test QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml ui/compose/PendingCompose.qml \ - ui/compose/SpellcheckAdapter.qml ui/compose/PersonalWords.qml ui/compose/SpellingState.qml \ + ui/compose/SpellcheckAdapter.qml ui/compose/SpellingState.qml \ ui/tests/compatibility/tst_published_agent.qml \ ui/tests/qml/tst_unified_batch.qml ui/tests/qml/tst_clipboard_arguments.qml \ ui/tests/qml/tst_reader_subject_menu.qml \ diff --git a/README.md b/README.md index bab0f61..4d32c5c 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Each installer verifies the release package before replacing an existing version Composer body spellchecking uses KDE Sonnet and a local Hunspell dictionary. On Arch, install `sonnet` and `hunspell-en_us`. On Debian and Ubuntu releases that provide Qt 6 Sonnet, install `qml6-module-org-kde-sonnet`, `sonnet6-plugins`, and `hunspell-en-us`. Restart Omamail after installing a dictionary. -Spelling checks default to US English and can be turned off in Settings. Without the Sonnet QML module or the selected dictionary, composing remains available and Settings explains what is missing. Omamail does not install system packages. Personal words are saved in Omamail's own `spelling.json`; Add to dictionary does not change a global Hunspell dictionary. +Spelling checks default to US English and can be turned off in Settings. Without the Sonnet QML module or the selected dictionary, composing remains available and Settings explains what is missing. Omamail does not install system packages. Ignore for this session stays in memory; there is no personal dictionary yet. ## Run from source diff --git a/app/qml/StandaloneShell.qml b/app/qml/StandaloneShell.qml index 888f461..f3c32f3 100644 --- a/app/qml/StandaloneShell.qml +++ b/app/qml/StandaloneShell.qml @@ -85,7 +85,7 @@ QtObject { } function writeConfig(name, text, callback) { - var allowed = ["credentials.json", "window.json", "window-size.json", "calendars.json", "spelling.json"] + var allowed = ["credentials.json", "window.json", "window-size.json", "calendars.json"] if (allowed.indexOf(String(name || "")) < 0 || !fileStore || typeof fileStore.write !== "function") { if (typeof callback === "function") callback(false, "Invalid configuration file") diff --git a/app/tests/qml/tst_standalone_composition.qml b/app/tests/qml/tst_standalone_composition.qml index 199ecc3..a8d4ce2 100644 --- a/app/tests/qml/tst_standalone_composition.qml +++ b/app/tests/qml/tst_standalone_composition.qml @@ -660,13 +660,6 @@ TestCase { verify(composition.shell.writeConfig("calendars.json", "{}", function(ok) { called = ok })) verify(called) compare(host.files["/fixture/config/omamail/calendars.json"], "{}") - // The personal spelling dictionary goes through the same host boundary; a - // name the shell refuses silently loses an added word at restart. - called = false - verify(composition.shell.writeConfig("spelling.json", "{\"words\":[\"blorptar\"]}", - function(ok) { called = ok })) - verify(called) - compare(host.files["/fixture/config/omamail/spelling.json"], "{\"words\":[\"blorptar\"]}") verify(!composition.shell.writeConfig("../outside", "secret", function() {})) verify(!Object.prototype.hasOwnProperty.call(host.files, "/fixture/config/omamail/../outside")) } diff --git a/docs/KEYS.md b/docs/KEYS.md index adb6470..c791a5a 100644 --- a/docs/KEYS.md +++ b/docs/KEYS.md @@ -47,7 +47,7 @@ readonly property string keyContext: | `page` | Setup or settings | `Escape`, and the modified keys | | `calendar` | The calendar month | Calendar navigation and the modified keys | -`Ctrl+.` opens spelling suggestions for the word at the caret when the composer body has focus. Right-click a word for the same corrections, Ignore for this session, and Add to dictionary. Fcitx5 commonly uses `Ctrl+.` to toggle punctuation width and may consume it before Omamail receives it; use right-click or change the input method binding in that case. There is no application menu-key or `Shift+F10` binding. +`Ctrl+.` opens spelling suggestions for the word at the caret when the composer body has focus. Right-click a word for the same corrections and Ignore for this session. Fcitx5 commonly uses `Ctrl+.` to toggle punctuation width and may consume it before Omamail receives it; use right-click or change the input method binding in that case. There is no application menu-key or `Shift+F10` binding. `Ctrl+,` opens Settings from every context, including a focused draft field. Back returns to the previous screen with the draft intact. diff --git a/scripts/config-store.sh b/scripts/config-store.sh index 8235566..4ae64f9 100755 --- a/scripts/config-store.sh +++ b/scripts/config-store.sh @@ -14,9 +14,9 @@ set -eu name=${1:-} case "$name" in - credentials.json|accounts.json|window.json|calendars.json|compose.json|spelling.json) ;; + credentials.json|accounts.json|window.json|calendars.json|compose.json) ;; *) - printf '%s\n' 'usage: config-store.sh credentials.json|accounts.json|window.json|calendars.json|compose.json|spelling.json' >&2 + printf '%s\n' 'usage: config-store.sh credentials.json|accounts.json|window.json|calendars.json|compose.json' >&2 exit 2 ;; esac diff --git a/tests/test_config_store.sh b/tests/test_config_store.sh index 708fb37..d80cbe1 100755 --- a/tests/test_config_store.sh +++ b/tests/test_config_store.sh @@ -56,19 +56,6 @@ done printf '%s\n' '{"zoom":1}' | "$store" window.json >/dev/null grep -q '"zoom":1' "$XDG_CONFIG_HOME/omamail/window.json" -# The app-owned personal spelling dictionary is one of those files. The service -# writes it through the same helper, so a name the writer refused would mean an -# added word never reached disk while the UI said it had. -printf '%s\n' '{"words":["blorptar"]}' | "$store" spelling.json >/dev/null -grep -q '"blorptar"' "$XDG_CONFIG_HOME/omamail/spelling.json" -if stat -c '%a' "$XDG_CONFIG_HOME/omamail/spelling.json" >/dev/null 2>&1; then - spelling_mode=$(stat -c '%a' "$XDG_CONFIG_HOME/omamail/spelling.json") -else - spelling_mode=$(stat -f '%Lp' "$XDG_CONFIG_HOME/omamail/spelling.json") -fi -[ "$spelling_mode" = 600 ] \ - || { echo 'config-store.sh: the personal dictionary must stay owner-only' >&2; exit 1; } - # An unknown name, an empty payload, and the permissions the files are kept at. if printf '%s\n' 'x' | "$store" secrets.json >/dev/null 2>&1; then echo 'config-store.sh: an unknown file name must be refused' >&2 diff --git a/ui/Service.qml b/ui/Service.qml index 33d3fdc..7e0ae38 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -488,7 +488,7 @@ Item { } function writeConfig(name, text, callback) { - var allowed = ["credentials.json", "window.json", "calendars.json", "spelling.json"] + var allowed = ["credentials.json", "window.json", "calendars.json"] if (allowed.indexOf(String(name || "")) < 0) { if (typeof callback === "function") callback(false, "Invalid configuration file") return false @@ -696,13 +696,8 @@ Item { readonly property alias spellingLanguage: spelling.language readonly property alias spellingAvailable: spelling.available readonly property alias spellingStatus: spelling.status - readonly property alias spellingWordStore: spelling.wordStore - readonly property alias spellingPersonalWords: spelling.words - readonly property alias spellingPersonalWordsError: spelling.error function setSpellingEnabled(value) { persistSetting("spellingEnabled", value !== false) } function setSpellingLanguage(value) { persistSetting("spellingLanguage", String(value || "en_US")) } - function addPersonalWord(word) { spelling.wordStore.add(word) } - function removePersonalWord(word) { spelling.wordStore.remove(word) } // ---------------------------------------------------------- the accounts diff --git a/ui/components/ComposeView.qml b/ui/components/ComposeView.qml index 36b5345..715798e 100644 --- a/ui/components/ComposeView.qml +++ b/ui/components/ComposeView.qml @@ -45,7 +45,6 @@ DropArea { // that instantiate it bare. A caller may still assign over the binding. property bool spellingEnabled: Spelling.decodeSettings(service ? { spellingEnabled: service.spellingEnabled } : null).enabled property string spellingLanguage: Spelling.decodeSettings(service ? { spellingLanguage: service.spellingLanguage } : null).language - property var spellingPersonalWords: service ? service.spellingPersonalWords : [] // Bumped on every body change so a menu opened before an edit can tell that // the text under its saved position moved. property int bodyRevision: 0 @@ -140,12 +139,6 @@ DropArea { return true } - // App-owned personal words: owned and persisted by the service; - // applied to the adapter and rechecked whenever the list changes. - onSpellingPersonalWordsChanged: { - if (spellcheckLoader.item) spellcheckLoader.item.personalWords = spellingPersonalWords - refreshSpellingRanges() - } // Drafts parked for their send's undo window, oldest first, each beside // the name of the send it belongs to. The timer owns them while the // visible composer stays free for the next message. @@ -2077,7 +2070,6 @@ DropArea { if (!item) return item.document = bodyEdit.textDocument item.enabled = root.spellingEnabled - item.personalWords = root.spellingPersonalWords root.refreshSpellingRanges() } } @@ -2314,9 +2306,5 @@ DropArea { if (root.spellingAdapter) root.spellingAdapter.ignoreForSession(word) root.refreshSpellingRanges() } - onSpellingAddToDictionary: function(word) { - if (root.service && typeof root.service.addPersonalWord === "function") - root.service.addPersonalWord(word) - } } } diff --git a/ui/components/SettingsPage.qml b/ui/components/SettingsPage.qml index 79087b8..84bdaf9 100644 --- a/ui/components/SettingsPage.qml +++ b/ui/components/SettingsPage.qml @@ -859,8 +859,8 @@ Column { Text { width: parent.width - text: "Underline misspelled words in the message body with corrections, " - + "Ignore, and Add to dictionary. Uses the local Hunspell engine; " + text: "Underline misspelled words in the message body with corrections " + + "and Ignore for this session. Uses the local Hunspell engine; " + "nothing is sent anywhere." color: root.dimColor font.family: root.panelFontFamily @@ -889,21 +889,6 @@ Column { textFormat: Text.PlainText } - // The personal word list is the one spelling write that can be refused - // by the host. The word stays in memory for the session, but a failed - // save has to say so rather than quietly vanish at restart. - Text { - objectName: "spellingPersonalWordsError" - width: parent.width - visible: !!root.service && String(root.service.spellingPersonalWordsError || "") !== "" - text: "Personal words could not be saved: " - + (root.service ? root.service.spellingPersonalWordsError : "") - color: root.accentColor - font.family: root.panelFontFamily - font.pixelSize: Style.font.caption - wrapMode: Text.WordWrap - textFormat: Text.PlainText - } } Text { diff --git a/ui/components/TextMenu.qml b/ui/components/TextMenu.qml index 37cd828..d7b3c0c 100644 --- a/ui/components/TextMenu.qml +++ b/ui/components/TextMenu.qml @@ -45,7 +45,7 @@ Item { readonly property bool hasSelection: !!target && String(target.selectedText || "") !== "" readonly property var menuRows: [ suggestion0, suggestion1, suggestion2, suggestion3, suggestion4, - ignoreRow, addRow, cutRow, copyRow, pasteRow, selectAllRow, openLinkRow, copyLinkRow + ignoreRow, cutRow, copyRow, pasteRow, selectAllRow, openLinkRow, copyLinkRow ] readonly property alias cutRow: cutRow @@ -55,7 +55,6 @@ Item { readonly property alias openLinkRow: openLinkRow readonly property alias copyLinkRow: copyLinkRow readonly property alias ignoreWordRow: ignoreRow - readonly property alias addToDictionaryRow: addRow signal copyRequested(string text) // Paste is the owner's: a compose form tries the clipboard for an image @@ -64,7 +63,6 @@ Item { signal openLinkRequested(string url) signal spellingCorrect(string replacement) signal spellingIgnore(string word) - signal spellingAddToDictionary(string word) anchors.fill: parent z: 50 @@ -201,12 +199,6 @@ Item { text: "Ignore for this session" onActivated: { var word = root.spellingWord; menu.close(); root.spellingIgnore(word) } } - MenuRow { - id: addRow - visible: root.spellingActionsVisible - text: "Add to dictionary" - onActivated: { var word = root.spellingWord; menu.close(); root.spellingAddToDictionary(word) } - } MenuSeparatorLine { visible: root.spellingVisible width: menu.width - menu.leftPadding - menu.rightPadding diff --git a/ui/compose/PersonalWords.qml b/ui/compose/PersonalWords.qml deleted file mode 100644 index f7fafac..0000000 --- a/ui/compose/PersonalWords.qml +++ /dev/null @@ -1,85 +0,0 @@ -import QtQuick -import Quickshell.Io -import "Spelling.js" as Spelling - -// App-owned personal spelling words, persisted as spelling.json beside the -// window state and applied by the composer. Never written to a global -// dictionary. -// -// The write is a queue rather than a one-shot. A second word added while the -// first write is still in flight must reach disk too, and an add that lands -// before the initial file read finishes must survive the read. -Item { - id: store - visible: false - - // The service owns both file boundaries: configPath() and writeConfig(). - required property var service - - property var words: [] - property bool loaded: false - property bool writing: false - property bool pending: false - property string error: "" - - // Replace the list. The file load uses `merge` below; this is the explicit - // reset form and what the tests drive. - function apply(raw) { - words = Spelling.normalizePersonalWords(raw) - loaded = true - if (pending) save() - } - - // A load that arrives after the user has already added a word must union, - // not replace: the in-memory additions are this session's intent and the - // disk copy is yesterday's. - function merge(raw) { - words = Spelling.mergePersonalWords(words, raw) - loaded = true - if (pending) save() - } - - function save() { - // Before the load completes, remember that there is something to write so - // the load can flush it rather than dropping it. - if (!loaded || writing) { pending = true; return } - writing = true - pending = false - service.writeConfig("spelling.json", JSON.stringify({ words: words }), function(ok, failure) { - store.writing = false - // The write boundaries can refuse the file (a host allowlist that has - // not been told about it, a full disk): keep the failure where the - // Settings page can say so instead of pretending the word saved. - store.error = ok ? "" : String(failure || "Could not save personal words") - if (store.pending) store.save() - }) - } - - function add(word) { - var value = String(word || "") - if (value === "" || words.indexOf(value) >= 0) return - var next = words.slice() - next.push(value) - words = next - save() - } - - function remove(word) { - var value = String(word || "") - var at = words.indexOf(value) - if (at < 0) return - var next = words.slice() - next.splice(at, 1) - words = next - save() - } - - FileView { - path: store.service.configPath("spelling.json") - printErrors: false - // Merge rather than replace: a word added before the read returned is this - // session's and must not be undone by yesterday's file. - onLoaded: store.merge(text()) - onLoadFailed: store.merge("") - } -} diff --git a/ui/compose/SpellcheckAdapter.qml b/ui/compose/SpellcheckAdapter.qml index 39b9118..7998782 100644 --- a/ui/compose/SpellcheckAdapter.qml +++ b/ui/compose/SpellcheckAdapter.qml @@ -27,10 +27,6 @@ Item { // QQuickItem.enabled (which qmllint rejects). The owning Loader is inactive // when spelling is off, so this is a runtime toggle within a loaded adapter. property string language: "en_US" - // App-owned words to treat as correct. Persistence belongs to the - // settings layer in Service.qml; this property only applies them. - property var personalWords: [] - // Availability is about the *requested* language, and it is computed after // `setCurrentLanguage` rather than bound to `spellCheckerFound`. // @@ -56,7 +52,6 @@ Item { Component.onDestruction: Session.unsubscribe(adapter) onEnabledChanged: applySettings() onLanguageChanged: applySettings() - onPersonalWordsChanged: applyPersonalWords() // Returns { word, misspelled, suggestions }. Call this for the position of a // click or the caret; it also primes applyCorrection for the same position. @@ -106,15 +101,6 @@ Item { // leaves the prior one in place, which is the no-dictionary answer. requestedLanguageAvailable = highlighter.spellCheckerFound && highlighter.currentLanguage === language - if (enabled) applyPersonalWords() - } - - function applyPersonalWords() { - if (!available) return - for (var i = 0; i < personalWords.length; i++) { - var word = String(personalWords[i]) - if (word !== "") highlighter.ignoreWord(word) - } Session.changed() } diff --git a/ui/compose/Spelling.js b/ui/compose/Spelling.js index 7eb1842..ab7755a 100644 --- a/ui/compose/Spelling.js +++ b/ui/compose/Spelling.js @@ -35,28 +35,6 @@ function isWordChar(character) { function isApostrophe(character) { return character === "'" || character === "\u2019" } -function normalizePersonalWords(raw) { - var parsed = null - try { parsed = JSON.parse(String(raw || "")) } catch (e) { parsed = null } - var list = (parsed && Array.isArray(parsed.words)) ? parsed.words : [] - var next = [] - for (var i = 0; i < list.length; i++) { - var word = list[i] - if (typeof word !== "string") continue - if (word !== "" && next.indexOf(word) < 0) next.push(word) - } - return next -} - -function mergePersonalWords(current, raw) { - var onDisk = normalizePersonalWords(raw) - var next = current.slice() - for (var i = 0; i < onDisk.length; i++) { - if (next.indexOf(onDisk[i]) < 0) next.push(onDisk[i]) - } - return next -} - function decodeSettings(settings) { var values = settings || ({}) return { diff --git a/ui/compose/SpellingState.qml b/ui/compose/SpellingState.qml index d617812..1862353 100644 --- a/ui/compose/SpellingState.qml +++ b/ui/compose/SpellingState.qml @@ -14,12 +14,6 @@ Item { && probe.item !== null && probe.item.available readonly property string status: probe.status === Loader.Error ? "no-module" : (probe.item ? probe.item.status : "loading") - readonly property alias wordStore: personalWords - readonly property alias words: personalWords.words - readonly property alias error: personalWords.error - - PersonalWords { id: personalWords; service: root.service } - // Sonnet enumerates dictionaries once per process. Installing one requires // restarting Omamail; a new adapter alone cannot discover it. Loader { id: probe; source: "SpellcheckAdapter.qml" } diff --git a/ui/tests/qml/tst_service_settings.qml b/ui/tests/qml/tst_service_settings.qml index 696ab29..2c8510e 100644 --- a/ui/tests/qml/tst_service_settings.qml +++ b/ui/tests/qml/tst_service_settings.qml @@ -19,32 +19,9 @@ Item { } } - // The personal dictionary write is asynchronous and may fail. This stands in - // for the host writer so a test can hold the reply and complete it later. - QtObject { - id: configPlatform - property var writes: [] - property var callbacks: [] - function writeConfig(name, text, callback) { - writes = writes.concat([{ name: String(name), text: String(text) }]) - callbacks = callbacks.concat([callback]) - return true - } - function confirm(index, ok, error) { - var cb = callbacks[index] - if (typeof cb !== "function") return false - var next = callbacks.slice() - next[index] = null - callbacks = next - cb(ok, error) - return true - } - } - Omamail.Service { id: mailService shell: shellStore - platform: configPlatform manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-test" }) } @@ -57,14 +34,6 @@ Item { TestCase { name: "ServiceSettings" - function init() { - configPlatform.writes = [] - configPlatform.callbacks = [] - mailService.spellingWordStore.writing = false - mailService.spellingWordStore.pending = false - mailService.spellingWordStore.error = "" - } - function test_scoped_manifest_resolves_bundled_helpers_without_private_metadata() { verify(scopedMailService.pluginDir !== "") verify(scopedMailService.pluginDir.indexOf("%") < 0, @@ -210,92 +179,6 @@ Item { compare(shellStore.updatedEntry.spellingLanguage, "en_GB") } - function test_spelling_personal_words_add_remove_and_dedupe() { - mailService.spellingWordStore.apply("") - compare(mailService.spellingPersonalWords.length, 0) - mailService.addPersonalWord("blorptar") - mailService.addPersonalWord("blorptar") - mailService.addPersonalWord("floobert") - compare(mailService.spellingPersonalWords.length, 2) - compare(mailService.spellingPersonalWords[0], "blorptar") - mailService.removePersonalWord("blorptar") - compare(mailService.spellingPersonalWords.length, 1) - compare(mailService.spellingPersonalWords[0], "floobert") - } - - function test_spelling_personal_words_parse_dedupe_and_ignore_junk() { - mailService.spellingWordStore.apply('{"words":["a","b","a","","c",42,null]}') - compare(mailService.spellingPersonalWords.length, 3) - compare(mailService.spellingPersonalWords[0], "a") - compare(mailService.spellingPersonalWords[1], "b") - compare(mailService.spellingPersonalWords[2], "c") - } - - // Two words added while the first write is still in flight: the second is - // queued and written after the first completes, so disk ends with both. - function test_spelling_personal_words_queue_changes_during_a_write() { - mailService.spellingWordStore.apply("") - configPlatform.writes = [] - configPlatform.callbacks = [] - mailService.addPersonalWord("alpha") - compare(configPlatform.writes.length, 1) - compare(JSON.parse(configPlatform.writes[0].text).words.length, 1) - mailService.addPersonalWord("beta") - compare(configPlatform.writes.length, 1, "the second add waits for the in-flight write") - compare(configPlatform.confirm(0, true, ""), true) - compare(configPlatform.writes.length, 2, "the queued snapshot is written once the first completes") - compare(JSON.parse(configPlatform.writes[1].text).words.length, 2) - compare(configPlatform.confirm(1, true, ""), true) - compare(mailService.spellingPersonalWordsError, "") - } - - // A refused write is recorded rather than swallowed, and the next change - // asks the host again. - function test_spelling_personal_words_retry_after_a_failed_write() { - mailService.spellingWordStore.apply("") - configPlatform.writes = [] - configPlatform.callbacks = [] - mailService.addPersonalWord("alpha") - compare(configPlatform.confirm(0, false, "disk full"), true) - compare(mailService.spellingPersonalWordsError, "disk full") - mailService.addPersonalWord("beta") - compare(configPlatform.writes.length, 2, "a later change retries the failed write") - compare(configPlatform.confirm(1, true, ""), true) - compare(mailService.spellingPersonalWordsError, "") - } - - // Write then read: what a completed write put in the file is what a later - // read restores, which is the restart guarantee the old path never had. - function test_spelling_personal_words_round_trip_through_a_restart() { - mailService.spellingWordStore.apply("") - configPlatform.writes = [] - configPlatform.callbacks = [] - mailService.addPersonalWord("blorptar") - compare(configPlatform.confirm(0, true, ""), true) - var saved = configPlatform.writes[0].text - // A restart reads the file back with no words in memory. - mailService.spellingWordStore.apply(saved) - compare(mailService.spellingPersonalWords.length, 1) - compare(mailService.spellingPersonalWords[0], "blorptar") - } - - // A word added before the initial read returns is the session's, not the - // file's: the later load unions the two instead of replacing memory. - function test_spelling_personal_words_merge_a_late_load() { - mailService.spellingWordStore.apply("") - configPlatform.writes = [] - configPlatform.callbacks = [] - mailService.spellingWordStore.loaded = false - mailService.addPersonalWord("alpha") - compare(configPlatform.writes.length, 0, "nothing is written before the load completes") - mailService.spellingWordStore.merge('{"words":["beta","alpha"]}') - compare(mailService.spellingPersonalWords.length, 2) - verify(mailService.spellingPersonalWords.indexOf("alpha") >= 0) - verify(mailService.spellingPersonalWords.indexOf("beta") >= 0) - compare(configPlatform.writes.length, 1, "the deferred add is flushed after the load") - compare(configPlatform.confirm(0, true, ""), true) - } - // The probe loads Sonnet through the same adapter the composer uses; it // settles into one concrete status on any machine rather than hanging. function test_spelling_availability_settles_to_a_known_status() { diff --git a/ui/tests/qml/tst_spellcheck_adapter.qml b/ui/tests/qml/tst_spellcheck_adapter.qml index 52dfa56..4b80a14 100644 --- a/ui/tests/qml/tst_spellcheck_adapter.qml +++ b/ui/tests/qml/tst_spellcheck_adapter.qml @@ -117,7 +117,7 @@ Item { compare(ranges[0].end, 11) } - function test_personal_words_are_treated_as_correct() { + function test_session_ignore_treats_a_word_as_correct() { var a = adapter() if (!a) return // A word unique to this test: ignore is process-global and irreversible, @@ -125,9 +125,9 @@ Item { // order. edit.text = "zqxjw zqxjw" compare(a.inspect(0).misspelled, true) - a.personalWords = ["zqxjw"] + a.ignoreForSession("zqxjw") compare(a.inspect(0).misspelled, false) - // Applying personal words never disables the checker for other words. + // Ignoring never disables the checker for other words. compare(a.available, true) edit.text = root.sample compare(a.inspect(edit.text.indexOf("wrod")).misspelled, true) diff --git a/ui/tests/qml/tst_spellcheck_composer.qml b/ui/tests/qml/tst_spellcheck_composer.qml index 5d88f82..3c969b2 100644 --- a/ui/tests/qml/tst_spellcheck_composer.qml +++ b/ui/tests/qml/tst_spellcheck_composer.qml @@ -23,18 +23,12 @@ Item { ] property string accountEmail: "me@example.com" property string activeAccountId: "me@example.com" - property var spellingPersonalWords: [] function preferredSendAs(_r) { return null } function switchTo(_id) { return true } function refreshRecipientContacts() {} function send(_f) { return true } function copyText(_t) { return true } function clipboardAttachment(_dir, callback) { callback({ ok: false, error: "no-image" }); return true } - function addPersonalWord(word) { - if (word && spellingPersonalWords.indexOf(word) < 0) { - var words = spellingPersonalWords.slice(); words.push(word); spellingPersonalWords = words - } - } } Omamail.KeyRouter { @@ -50,7 +44,6 @@ Item { onKeyPressed: function(event) { keyRouter.routeKeyEvent(event) } anchors.fill: parent service: mailService - spellingPersonalWords: mailService.spellingPersonalWords textColor: Qt.rgba(1, 1, 1, 1) errorColor: Qt.rgba(1, 1, 1, 1) backgroundColor: Qt.rgba(0.06, 0.06, 0.06, 1) @@ -125,30 +118,6 @@ Item { } } - function test_personal_words_preserve_focused_draft_selection_and_undo() { - var body = named(compose, "compose-body-editor") - tryVerify(function() { return compose.spellingAdapter !== null || compose.spellingStatus === "no-module" }, 3000) - if (!compose.spellingAvailable) { skip("spelling unavailable (" + compose.spellingStatus + ")"); return } - body.text = "the zqxjpersonalreviewhere here " - body.forceActiveFocus() - body.select(4, 9) - compose.userModified = false - compose.bodyWasEdited = false - var before = body.text - var selected = body.selectedText - var revision = compose.bodyRevision - var undoAvailable = body.canUndo - mailService.addPersonalWord("zqxjpersonalreviewhere") - wait(250) - compare(body.text, before) - compare(body.selectedText, selected) - compare(body.canUndo, undoAvailable) - compare(compose.userModified, false) - compare(compose.bodyWasEdited, false) - compare(compose.bodyRevision, revision) - compare(compose.spellingRanges.length, 0) - } - function test_a_correction_changes_the_body_and_undo_restores_it() { var body = named(compose, "compose-body-editor") verify(body) @@ -216,23 +185,6 @@ Item { compare(compose.spellingRanges.length, 0) } - function test_add_to_dictionary_stops_marking_the_word() { - var body = named(compose, "compose-body-editor") - tryVerify(function() { return compose.spellingAdapter !== null || compose.spellingStatus === "no-module" }, 3000) - if (!compose.spellingAvailable) { skip("spelling unavailable"); return } - body.text = "the blorptar here " - wait(250) - compare(compose.spellingRanges.length, 1) - var box = body.positionToRectangle(body.text.indexOf("blorptar")) - mouseClick(body, box.x + 2, box.y + box.height / 2, Qt.RightButton) - wait(20) - var menu = named(compose, "compose-text-menu") - compare(menu.spellingWord, "blorptar") - menu.addToDictionaryRow.activated() - wait(250) - compare(compose.spellingRanges.length, 0) - } - function test_ctrl_period_opens_suggestions_at_the_caret_word() { var body = named(compose, "compose-body-editor") tryVerify(function() { return compose.spellingAdapter !== null || compose.spellingStatus === "no-module" }, 3000) diff --git a/ui/tests/qml/tst_spellcheck_menu.qml b/ui/tests/qml/tst_spellcheck_menu.qml index 08dba3e..dd7f3d3 100644 --- a/ui/tests/qml/tst_spellcheck_menu.qml +++ b/ui/tests/qml/tst_spellcheck_menu.qml @@ -3,7 +3,7 @@ import QtTest import "../../components" as Omamail // The right-click / Ctrl+. spelling menu on the composer body — the word -// it targets, the correction/ignore/dictionary actions it offers, and the +// it targets, the correction/ignore actions it offers, and the // guards that keep a stale or destroyed draft from being rewritten. The // underlines and the adapter contract are covered by tst_spellcheck_adapter // and tst_spellcheck_composer; here the point is the menu wiring. @@ -24,18 +24,12 @@ Item { ] property string accountEmail: "me@example.com" property string activeAccountId: "me@example.com" - property var spellingPersonalWords: [] function preferredSendAs(_r) { return null } function switchTo(_id) { return true } function refreshRecipientContacts() {} function send(_f) { return true } function copyText(_t) { return true } function clipboardAttachment(_dir, callback) { callback({ ok: false, error: "no-image" }); return true } - function addPersonalWord(word) { - if (word && spellingPersonalWords.indexOf(word) < 0) { - var words = spellingPersonalWords.slice(); words.push(word); spellingPersonalWords = words - } - } } Omamail.KeyRouter { @@ -51,7 +45,6 @@ Item { onKeyPressed: function(event) { keyRouter.routeKeyEvent(event) } anchors.fill: parent service: mailService - spellingPersonalWords: mailService.spellingPersonalWords textColor: Qt.rgba(1, 1, 1, 1) errorColor: Qt.rgba(1, 1, 1, 1) backgroundColor: Qt.rgba(0.06, 0.06, 0.06, 1) @@ -203,7 +196,7 @@ Item { compare(editor.text.indexOf("wrod"), before.indexOf("wrod")) } - function test_no_suggestions_offers_ignore_and_add_but_no_rows() { + function test_no_suggestions_offers_ignore_but_no_rows() { var editor = body() editor.text = "a zxqjklv here " wait(250) @@ -213,7 +206,6 @@ Item { compare(m.spellingMisspelled, true) compare(m.spellingSuggestions.length, 0) verify(m.ignoreWordRow.visible, "Ignore is offered without suggestions") - verify(m.addToDictionaryRow.visible, "Add to dictionary is offered without suggestions") m.ignoreWordRow.activated() wait(250) compare(compose.spellingRanges.length, 0) @@ -233,7 +225,7 @@ Item { compare(editor.text, "a " + suggestion + " here") } - function test_keyboard_opens_ignore_and_add_without_suggestions() { + function test_keyboard_opens_ignore_without_suggestions() { var editor = body() editor.text = "the zxqjklv here " editor.cursorPosition = 8 @@ -243,7 +235,6 @@ Item { tryCompare(menu(), "opened", true) compare(menu().spellingMisspelled, true) verify(menu().ignoreWordRow.visible) - verify(menu().addToDictionaryRow.visible) menu().close() } diff --git a/ui/tests/test_spelling.js b/ui/tests/test_spelling.js index ab0b666..99ee7f1 100644 --- a/ui/tests/test_spelling.js +++ b/ui/tests/test_spelling.js @@ -29,18 +29,6 @@ test("Ranges keep UTF-16 offsets, apostrophes, combining marks and repeated word deepEqual(rules.misspelledRanges("'' ’ 😀 ", wrong), []) }) -test("Personal words reject malformed data and merge late disk reads with session additions", () => { - const rules = spelling() - for (const raw of ["", "{", "null", "[]", '{"words":{}}']) { - deepEqual(rules.normalizePersonalWords(raw), []) - } - deepEqual(rules.normalizePersonalWords('{"words":["a","b","a","",42,null,{},"café"]}'), ["a", "b", "café"]) - const current = ["session", "shared"] - deepEqual(rules.mergePersonalWords(current, '{"words":["disk","shared","disk"]}'), ["session", "shared", "disk"]) - assert.deepEqual(current, ["session", "shared"], "a merge does not mutate the session snapshot") - deepEqual(rules.mergePersonalWords(current, "invalid"), current) -}) - test("Settings default to enabled en_US and only explicit false disables checking", () => { const rules = spelling() for (const settings of [null, {}, { spellingEnabled: "no" }, { spellingEnabled: 0 }]) { From 62eea313b4bad647a0b8f496a5bc56b291540351 Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 18:43:00 -0500 Subject: [PATCH 23/88] Fix spelling language resolution with validated system-locale defaults --- README.md | 4 ++-- manifest.json | 10 +++++----- ui/Service.qml | 2 +- ui/components/ComposeView.qml | 3 ++- ui/components/SettingsPage.qml | 21 ++++++++++++++++++--- ui/compose/SpellcheckAdapter.qml | 9 +++++---- ui/compose/Spelling.js | 17 +++++++++++++++-- ui/compose/SpellingState.qml | 3 ++- ui/settings/Settings.js | 2 +- ui/tests/qml/tst_service_settings.qml | 13 ++++++++++++- ui/tests/qml/tst_spellcheck_composer.qml | 2 +- ui/tests/qml/tst_spellcheck_menu.qml | 1 + ui/tests/test_settings.js | 2 +- ui/tests/test_spelling.js | 22 +++++++++++++++++++--- 14 files changed, 85 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 4d32c5c..5249ef0 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ Each installer verifies the release package before replacing an existing version ### Optional spelling support -Composer body spellchecking uses KDE Sonnet and a local Hunspell dictionary. On Arch, install `sonnet` and `hunspell-en_us`. On Debian and Ubuntu releases that provide Qt 6 Sonnet, install `qml6-module-org-kde-sonnet`, `sonnet6-plugins`, and `hunspell-en-us`. Restart Omamail after installing a dictionary. +Composer body spellchecking uses KDE Sonnet and a local Hunspell dictionary. On Arch, install `sonnet` and the matching `hunspell-` package, such as `hunspell-en_us` or `hunspell-de`. On Debian and Ubuntu releases that provide Qt 6 Sonnet, install `qml6-module-org-kde-sonnet`, `sonnet6-plugins`, and the matching dictionary, such as `hunspell-en-us`. Restart Omamail after installing a dictionary. -Spelling checks default to US English and can be turned off in Settings. Without the Sonnet QML module or the selected dictionary, composing remains available and Settings explains what is missing. Omamail does not install system packages. Ignore for this session stays in memory; there is no personal dictionary yet. +Only the message body is checked, not the subject. Spelling uses the system language by default (`Qt.locale().name`, for example `en_US` or `de_DE`) and can be turned off in Settings. The advanced `spellingLanguage` setting is empty for system language, or an installed dictionary code to override it; malformed values are treated as system language and never passed to Sonnet. There is no silent fallback to English: without the Sonnet QML module or the requested dictionary, composing remains available and Settings names what is missing. Omamail does not install system packages. Checks and corrections stay local; nothing leaves the machine. Ignore for this session stays in memory; there is no personal dictionary yet. ## Run from source diff --git a/manifest.json b/manifest.json index 24b8079..334d160 100644 --- a/manifest.json +++ b/manifest.json @@ -51,7 +51,7 @@ "openOnClick": "Window", "showBarIcon": true, "spellingEnabled": true, - "spellingLanguage": "en_US", + "spellingLanguage": "", "suggestEvents": false, "aiAgent": "System default", "aiModel": "" @@ -206,14 +206,14 @@ "type": "boolean", "label": "Spell-check while composing", "defaultValue": true, - "description": "Underline misspelled words in the message body with corrections, Ignore, and Add to dictionary. Uses the local Hunspell engine; nothing is sent anywhere." + "description": "Underline misspelled body words with corrections and Ignore for this session. Uses local dictionaries; nothing is sent anywhere." }, { "key": "spellingLanguage", "type": "string", - "label": "Spelling dictionary language", - "defaultValue": "en_US", - "description": "The Hunspell dictionary code used for spell-checking, for example en_US. Requires the matching hunspell dictionary package to be installed." + "label": "Spelling dictionary language override", + "defaultValue": "", + "description": "Empty uses the system language. Advanced override: an installed Hunspell dictionary code such as en_US or de_DE. No fallback dictionary is used." } ] } diff --git a/ui/Service.qml b/ui/Service.qml index 7e0ae38..6d3b871 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -697,7 +697,7 @@ Item { readonly property alias spellingAvailable: spelling.available readonly property alias spellingStatus: spelling.status function setSpellingEnabled(value) { persistSetting("spellingEnabled", value !== false) } - function setSpellingLanguage(value) { persistSetting("spellingLanguage", String(value || "en_US")) } + function setSpellingLanguage(value) { persistSetting("spellingLanguage", String(value || "")) } // ---------------------------------------------------------- the accounts diff --git a/ui/components/ComposeView.qml b/ui/components/ComposeView.qml index 715798e..1a7d52f 100644 --- a/ui/components/ComposeView.qml +++ b/ui/components/ComposeView.qml @@ -44,7 +44,8 @@ DropArea { // one is wired, and falls back to the defaults for the tests // that instantiate it bare. A caller may still assign over the binding. property bool spellingEnabled: Spelling.decodeSettings(service ? { spellingEnabled: service.spellingEnabled } : null).enabled - property string spellingLanguage: Spelling.decodeSettings(service ? { spellingLanguage: service.spellingLanguage } : null).language + property string spellingLanguage: Spelling.resolveLanguage( + service ? service.spellingLanguage : "", Qt.locale().name) // Bumped on every body change so a menu opened before an edit can tell that // the text under its saved position moved. property int bodyRevision: 0 diff --git a/ui/components/SettingsPage.qml b/ui/components/SettingsPage.qml index 84bdaf9..ebbf1e1 100644 --- a/ui/components/SettingsPage.qml +++ b/ui/components/SettingsPage.qml @@ -4,6 +4,7 @@ import qs.Commons import qs.Ui import "../message/Direction.js" as Direction import "../settings/Appearance.js" as Appearance +import "../compose/Spelling.js" as Spelling import "../message/Html.js" as Html import "../account/Accounts.js" as Accounts @@ -869,8 +870,22 @@ Column { textFormat: Text.PlainText } + Text { + objectName: "spellingLanguagePolicy" + width: parent.width + text: "Checking spelling in " + + Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) + + (root.service && Spelling.decodeSettings(root.service.settings).language !== "" + ? " (set in settings)." : " (system language).") + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + textFormat: Text.PlainText + } + // Availability is a runtime fact, separate from the enabled request: - // a machine without Sonnet or the en_US dictionary still composes. + // a machine without Sonnet or its language dictionary still composes. // Sonnet lists installed dictionaries once per process, so a dictionary // installed while Omamail runs is only found after a restart. Text { @@ -879,9 +894,9 @@ Column { visible: !!root.service && !root.service.spellingAvailable text: root.service && root.service.spellingStatus === "no-module" ? "Spell-checking is unavailable: install the Sonnet QML module, then restart Omamail." - : "The " + (root.service ? root.service.spellingLanguage : "en_US") + : "The " + Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) + " dictionary is not installed. Install the matching Hunspell " - + "dictionary package (for example hunspell-en_us), then restart Omamail." + + "dictionary package (hunspell-, for example hunspell-de), then restart Omamail." color: root.accentColor font.family: root.panelFontFamily font.pixelSize: Style.font.caption diff --git a/ui/compose/SpellcheckAdapter.qml b/ui/compose/SpellcheckAdapter.qml index 7998782..b54e6c4 100644 --- a/ui/compose/SpellcheckAdapter.qml +++ b/ui/compose/SpellcheckAdapter.qml @@ -26,7 +26,8 @@ Item { // non-visual, so this keeps the fixed interface name without shadowing // QQuickItem.enabled (which qmllint rejects). The owning Loader is inactive // when spelling is off, so this is a runtime toggle within a loaded adapter. - property string language: "en_US" + property string language: "" + readonly property string resolvedLanguage: Spelling.resolveLanguage(language, Qt.locale().name) // Availability is about the *requested* language, and it is computed after // `setCurrentLanguage` rather than bound to `spellCheckerFound`. // @@ -51,7 +52,7 @@ Item { } Component.onDestruction: Session.unsubscribe(adapter) onEnabledChanged: applySettings() - onLanguageChanged: applySettings() + onResolvedLanguageChanged: applySettings() // Returns { word, misspelled, suggestions }. Call this for the position of a // click or the caret; it also primes applyCorrection for the same position. @@ -96,11 +97,11 @@ Item { // The request is attempted even when the previous one reverted: a later // successful call is the only way back from a missing dictionary, and // guarding on the prior result would leave the switch unrecoverable. - highlighter.setCurrentLanguage(language) + if (resolvedLanguage !== "") highlighter.setCurrentLanguage(resolvedLanguage) // Sonnet reports the language it accepted. A request it could not serve // leaves the prior one in place, which is the no-dictionary answer. requestedLanguageAvailable = highlighter.spellCheckerFound - && highlighter.currentLanguage === language + && resolvedLanguage !== "" && highlighter.currentLanguage === resolvedLanguage Session.changed() } diff --git a/ui/compose/Spelling.js b/ui/compose/Spelling.js index ab7755a..069643d 100644 --- a/ui/compose/Spelling.js +++ b/ui/compose/Spelling.js @@ -1,6 +1,6 @@ .pragma library -// Spelling presentation rules, shared by the optional adapter and its stores. +// Spelling presentation rules shared by the composer and optional adapter. // Positions are UTF-16 offsets, matching Qt's text document API. function misspelledRanges(text, isMisspelled) { var ranges = [] @@ -35,11 +35,24 @@ function isWordChar(character) { function isApostrophe(character) { return character === "'" || character === "\u2019" } +// Persisted dictionary names are untrusted. Do not trim: trailing controls +// and whitespace must be rejected before a value can reach Sonnet. +function validLanguage(value) { + return typeof value === "string" && value.length <= 24 + && /^[A-Za-z]{2,3}([_-][A-Za-z0-9]{2,8}){0,2}$/.test(value) + && !/[\r\n]/.test(value) +} + +function resolveLanguage(setting, systemLocaleName) { + if (validLanguage(setting)) return setting + return validLanguage(systemLocaleName) ? systemLocaleName : "" +} + function decodeSettings(settings) { var values = settings || ({}) return { enabled: values.spellingEnabled !== false, - language: String(values.spellingLanguage || "en_US") + language: validLanguage(values.spellingLanguage) ? values.spellingLanguage : "" } } diff --git a/ui/compose/SpellingState.qml b/ui/compose/SpellingState.qml index 1862353..80040ce 100644 --- a/ui/compose/SpellingState.qml +++ b/ui/compose/SpellingState.qml @@ -9,7 +9,8 @@ Item { visible: false required property var service readonly property bool enabledRequest: Spelling.decodeSettings(service.settings).enabled - readonly property string language: Spelling.decodeSettings(service.settings).language + readonly property string languageOverride: Spelling.decodeSettings(service.settings).language + readonly property string language: Spelling.resolveLanguage(languageOverride, Qt.locale().name) readonly property bool available: probe.status === Loader.Ready && probe.item !== null && probe.item.available readonly property string status: probe.status === Loader.Error diff --git a/ui/settings/Settings.js b/ui/settings/Settings.js index 5a3b8c1..8ab6125 100644 --- a/ui/settings/Settings.js +++ b/ui/settings/Settings.js @@ -19,7 +19,7 @@ var DEFAULTS = ({ showBarIcon: true, unifiedMailboxes: false, spellingEnabled: true, - spellingLanguage: "en_US", + spellingLanguage: "", suggestEvents: false, aiAgent: "System default", aiModel: "" diff --git a/ui/tests/qml/tst_service_settings.qml b/ui/tests/qml/tst_service_settings.qml index 2c8510e..09ba2c2 100644 --- a/ui/tests/qml/tst_service_settings.qml +++ b/ui/tests/qml/tst_service_settings.qml @@ -164,7 +164,7 @@ Item { function test_spelling_defaults_on_and_persists_changes() { mailService.applySettings({}) compare(mailService.spellingEnabled, true) - compare(mailService.spellingLanguage, "en_US") + compare(mailService.spellingLanguage, Qt.locale().name) mailService.setSpellingEnabled(false) compare(mailService.spellingEnabled, false) @@ -179,6 +179,17 @@ Item { compare(shellStore.updatedEntry.spellingLanguage, "en_GB") } + function test_hostile_spelling_language_is_resolved_before_the_adapter() { + for (var i = 0; i < 5; i++) { + var values = ["../x", "en_US\n", "en_US\u0000", "éé", "x".repeat(500)] + mailService.applySettings({ spellingLanguage: values[i] }) + compare(mailService.spellingLanguage, Qt.locale().name) + } + mailService.setSpellingLanguage("") + compare(shellStore.updatedEntry.spellingLanguage, "") + compare(mailService.spellingLanguage, Qt.locale().name) + } + // The probe loads Sonnet through the same adapter the composer uses; it // settles into one concrete status on any machine rather than hanging. function test_spelling_availability_settles_to_a_known_status() { diff --git a/ui/tests/qml/tst_spellcheck_composer.qml b/ui/tests/qml/tst_spellcheck_composer.qml index 3c969b2..c2cb6b5 100644 --- a/ui/tests/qml/tst_spellcheck_composer.qml +++ b/ui/tests/qml/tst_spellcheck_composer.qml @@ -106,7 +106,7 @@ Item { var component = Qt.createComponent("../../compose/SpellcheckAdapter.qml") compare(component.status, Component.Ready) var secondDoc = Qt.createQmlObject('import QtQuick; TextEdit { text: "zqxjremotereviewhere " }', compose) - var second = component.createObject(compose, { document: secondDoc.textDocument }) + var second = component.createObject(compose, { document: secondDoc.textDocument, language: "en_US" }) try { verify(second.available) second.ignoreForSession("zqxjremotereviewhere") diff --git a/ui/tests/qml/tst_spellcheck_menu.qml b/ui/tests/qml/tst_spellcheck_menu.qml index dd7f3d3..0e82c7b 100644 --- a/ui/tests/qml/tst_spellcheck_menu.qml +++ b/ui/tests/qml/tst_spellcheck_menu.qml @@ -77,6 +77,7 @@ Item { function init() { compose.parent.forceActiveFocus() compose.begin("new", null, "", []) + compose.spellingLanguage = "en_US" tryVerify(function() { return compose.spellingAdapter !== null || compose.spellingStatus === "no-module" }, 3000) if (!compose.spellingAvailable) skip("spelling unavailable (" + compose.spellingStatus + ")") } diff --git a/ui/tests/test_settings.js b/ui/tests/test_settings.js index 81d703c..8419e79 100644 --- a/ui/tests/test_settings.js +++ b/ui/tests/test_settings.js @@ -12,7 +12,7 @@ for (const key of ["spellingEnabled", "spellingLanguage"]) { deepEqual(settings.normalize(null), settings.DEFAULTS) assert.equal(settings.normalize({ spellingEnabled: false }).spellingEnabled, false) assert.equal(settings.normalize({ aiAgent: "Claude", calendarSnoozeMinutes: 10 }).aiAgent, "Claude") -assert.equal(settings.normalize({ spellingLanguage: null, aiModel: undefined }).spellingLanguage, "en_US") +assert.equal(settings.normalize({ spellingLanguage: null, aiModel: undefined }).spellingLanguage, "") assert.equal(settings.normalize({ aiModel: undefined }).aiModel, "") assert.equal(settings.DEFAULTS.aiAgent, "System default", "normalization does not mutate defaults") deepEqual(bridge.settings({ spellingEnabled: false, spellingLanguage: "en_GB", secret: "synthetic" }, settings.DEFAULTS), { diff --git a/ui/tests/test_spelling.js b/ui/tests/test_spelling.js index 99ee7f1..339b808 100644 --- a/ui/tests/test_spelling.js +++ b/ui/tests/test_spelling.js @@ -29,13 +29,29 @@ test("Ranges keep UTF-16 offsets, apostrophes, combining marks and repeated word deepEqual(rules.misspelledRanges("'' ’ 😀 ", wrong), []) }) -test("Settings default to enabled en_US and only explicit false disables checking", () => { +test("Settings default to enabled system language and only explicit false disables checking", () => { const rules = spelling() for (const settings of [null, {}, { spellingEnabled: "no" }, { spellingEnabled: 0 }]) { - deepEqual(rules.decodeSettings(settings), { enabled: true, language: "en_US" }) + deepEqual(rules.decodeSettings(settings), { enabled: true, language: "" }) } deepEqual(rules.decodeSettings({ spellingEnabled: false, spellingLanguage: "en_GB" }), { enabled: false, language: "en_GB" }) - deepEqual(rules.decodeSettings({ spellingLanguage: "" }), { enabled: true, language: "en_US" }) + deepEqual(rules.decodeSettings({ spellingLanguage: "" }), { enabled: true, language: "" }) +}) + +test("Language uses the system locale unless a short dictionary code overrides it", () => { + const rules = spelling() + for (const valid of ["en", "en_US", "de-DE", "eng_Latn_US", "sr_Latn", "es_419"]) { + assert.equal(rules.validLanguage(valid), true) + assert.equal(rules.resolveLanguage(valid, "de_DE"), valid) + } + for (const hostile of [null, 42, {}, "", "C", " en_US", "en_US ", "en_US\n", + "en_US\r\n", "en_US\0", "../x", "en/US", "en\\US", "en..US", "éé", "a".repeat(500)]) { + assert.equal(rules.validLanguage(hostile), false, JSON.stringify(hostile)) + assert.equal(rules.resolveLanguage(hostile, "de_DE"), "de_DE") + deepEqual(rules.decodeSettings({ spellingLanguage: hostile }), { enabled: true, language: "" }) + } + assert.equal(rules.resolveLanguage("", "zz_ZZ"), "zz_ZZ", "no silent English fallback") + assert.equal(rules.resolveLanguage("", "C"), "", "an unsupported system locale never reaches Sonnet") }) test("Underlines cover every visual line of a word and descending caret positions", () => { From e8c8edf7a83afc547153e4f3d5ad76c93ec9f30a Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:01:57 -0500 Subject: [PATCH 24/88] Fix long-body spelling with viewport windows and budgeted underline slices --- README.md | 2 +- ui/components/ComposeView.qml | 128 ++++++++++++++------- ui/components/SettingsPage.qml | 4 +- ui/compose/SpellcheckAdapter.qml | 12 +- ui/compose/Spelling.js | 135 +++++++++++++++++++---- ui/compose/SpellingSession.js | 5 +- ui/tests/qml/tst_service_settings.qml | 7 +- ui/tests/qml/tst_spellcheck_composer.qml | 86 +++++++++++++++ ui/tests/test_spelling.js | 78 +++++++++++++ 9 files changed, 388 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 5249ef0..d29c381 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Each installer verifies the release package before replacing an existing version Composer body spellchecking uses KDE Sonnet and a local Hunspell dictionary. On Arch, install `sonnet` and the matching `hunspell-` package, such as `hunspell-en_us` or `hunspell-de`. On Debian and Ubuntu releases that provide Qt 6 Sonnet, install `qml6-module-org-kde-sonnet`, `sonnet6-plugins`, and the matching dictionary, such as `hunspell-en-us`. Restart Omamail after installing a dictionary. -Only the message body is checked, not the subject. Spelling uses the system language by default (`Qt.locale().name`, for example `en_US` or `de_DE`) and can be turned off in Settings. The advanced `spellingLanguage` setting is empty for system language, or an installed dictionary code to override it; malformed values are treated as system language and never passed to Sonnet. There is no silent fallback to English: without the Sonnet QML module or the requested dictionary, composing remains available and Settings names what is missing. Omamail does not install system packages. Checks and corrections stay local; nothing leaves the machine. Ignore for this session stays in memory; there is no personal dictionary yet. +Only completed words in the message body are checked, not the subject or quoted reply lines whose first non-space character is `>`. Checking is limited to the visible viewport plus one viewport of context on each side, with a 20,000-character window, at most 400 visible underline segments, and 8 ms / 12-token work slices. Tokens longer than 128 UTF-16 units are skipped; lines indented by more than 128 spaces or tabs are conservatively skipped. Scrolling checks the newly visible text. Corrections remain available on demand without waiting for the underline pass. Spelling uses the system language by default (`Qt.locale().name`, for example `en_US` or `de_DE`) and can be turned off in Settings. The advanced `spellingLanguage` setting is empty for system language, or an installed dictionary code to override it; malformed values are treated as system language and never passed to Sonnet. There is no silent fallback to English: without the Sonnet QML module or the requested dictionary, composing remains available and Settings names what is missing. Omamail does not install system packages. Checks and corrections stay local; nothing leaves the machine. Ignore for this session stays in memory; there is no personal dictionary yet. ## Run from source diff --git a/ui/components/ComposeView.qml b/ui/components/ComposeView.qml index 1a7d52f..98ae83e 100644 --- a/ui/components/ComposeView.qml +++ b/ui/components/ComposeView.qml @@ -79,33 +79,89 @@ DropArea { signal keyPressed(var event) - // A word is marked only once it is finished (something follows it), so this - // only has to keep up with the document; the completeness rule is what defers - // the underline while someone is still typing a word. + property var spellingPass: null + property var spellingWindow: ({ start: 0, end: 0 }) + property int spellingGeneration: 0 + readonly property int spellingDelegateCount: spellingSegments.count + + function cancelSpellingPass() { + spellingGeneration += 1 + spellingPass = null + spellingSlice.stop() + spellingSegments.clear() + spellingRanges = [] + } + + function scheduleSpellingLayout() { + cancelSpellingPass() + spellingScrollDebounce.restart() + } + function refreshSpellingRanges() { + cancelSpellingPass() var adapter = spellingAdapter - spellingRanges = (adapter && spellingAvailable && spellingEnabled) - ? adapter.misspelledRanges(bodyEdit.text) : [] + if (!adapter || !spellingAvailable || !spellingEnabled || bodyFlick.height <= 0) return + var text = bodyEdit.text + var top = Math.max(0, bodyFlick.contentY) + var bottom = top + bodyFlick.height + var visibleStart = Math.min(bodyEdit.positionAt(0, top), bodyEdit.positionAt(bodyEdit.width, top)) + var visibleEnd = Math.max(bodyEdit.positionAt(0, bottom), bodyEdit.positionAt(bodyEdit.width, bottom)) + var marginTop = Math.max(0, top - bodyFlick.height) + var marginBottom = bottom + bodyFlick.height + var start = Math.min(bodyEdit.positionAt(0, marginTop), bodyEdit.positionAt(bodyEdit.width, marginTop)) + var end = Math.max(bodyEdit.positionAt(0, marginBottom), bodyEdit.positionAt(bodyEdit.width, marginBottom)) + spellingWindow = Spelling.wordWindow(text, start, end) + spellingPass = Spelling.createPass(text, spellingWindow, visibleStart, visibleEnd, bodyRevision) + spellingPass.generation = spellingGeneration + spellingSlice.restart() + } + + function processSpellingSlice() { + var pass = spellingPass + var adapter = spellingAdapter + if (!pass || !adapter || pass.revision !== bodyRevision || pass.generation !== spellingGeneration) { + cancelSpellingPass() + return + } + Spelling.advancePass(pass, function(word) { return adapter.isMisspelled(word) }, + function() { return Date.now() }, function(range) { + var segments = Spelling.underlineSegments(range.start, range.end, + function(position) { return bodyEdit.positionToRectangle(position) }, + bodyEdit.leftPadding, bodyEdit.width - bodyEdit.rightPadding) + var before = spellingSegments.count + for (var i = 0; i < segments.length && spellingSegments.count < Spelling.MAX_UNDERLINE_RANGES; i++) { + // Margin words warm the checker cache; only viewport geometry paints. + var segment = segments[i] + if (segment.y >= bodyFlick.contentY && segment.y < bodyFlick.contentY + bodyFlick.height) + spellingSegments.append({ segmentX: segment.x, segmentY: segment.y, segmentWidth: segment.width }) + } + return spellingSegments.count > before + }) + if (pass.revision !== bodyRevision || pass.generation !== spellingGeneration) { + cancelSpellingPass() + return + } + spellingRanges = pass.ranges.slice() + if (!pass.done) spellingSlice.restart() } + + onBodyRevisionChanged: cancelSpellingPass() + onSpellingLayoutKeyChanged: scheduleSpellingLayout() onSpellingAvailableChanged: refreshSpellingRanges() onSpellingEnabledChanged: refreshSpellingRanges() - // A valid-to-valid language switch leaves `spellingAvailable` true, so the - // ranges would keep the old dictionary's underlines. Re-check after the - // adapter has applied the new language (the debounce lets that land first). - onSpellingLanguageChanged: spellingDebounce.restart() + onSpellingLanguageChanged: { cancelSpellingPass(); spellingDebounce.restart() } - Timer { - id: spellingDebounce - interval: 120 - repeat: false - onTriggered: root.refreshSpellingRanges() - } + ListModel { id: spellingSegments } + Timer { id: spellingSlice; interval: 1; onTriggered: root.processSpellingSlice() } + Timer { id: spellingScrollDebounce; interval: 40; onTriggered: root.refreshSpellingRanges() } + Timer { id: spellingDebounce; interval: 120; onTriggered: root.refreshSpellingRanges() } // Right-click: the word under the pointer, kept for the menu's spelling // section. Only the body is checked; another field clears the section. function prepareSpellingMenu(editor, x, y) { var adapter = spellingAdapter - if (!adapter || !spellingAvailable || editor !== bodyEdit) { + if (!adapter || !spellingAvailable || editor !== bodyEdit + || Spelling.quotedAt(bodyEdit.text, editor.positionAt(x, y))) { textMenu.spellingPosition = -1 textMenu.spellingWord = "" textMenu.spellingMisspelled = false @@ -127,6 +183,7 @@ DropArea { var adapter = spellingAdapter if (!bodyEdit.activeFocus || !spellingEnabled || !adapter || !spellingAvailable) return false var position = bodyEdit.cursorPosition + if (Spelling.quotedAt(bodyEdit.text, position)) return false var info = adapter.inspect(position) if (!info.misspelled) return false textMenu.spellingPosition = position @@ -1977,6 +2034,8 @@ DropArea { anchors.topMargin: Style.space(12) contentWidth: width contentHeight: bodyEdit.height + onContentYChanged: root.scheduleSpellingLayout() + onHeightChanged: root.scheduleSpellingLayout() clip: true boundsBehavior: Flickable.StopAtBounds ScrollBar.vertical: ScrollBar { policy: ScrollBar.AsNeeded } @@ -2029,30 +2088,17 @@ DropArea { id: spellingUnderlines anchors.fill: parent Repeater { - model: root.spellingRanges - delegate: Item { - required property var modelData - anchors.fill: parent - readonly property var segments: { - // Qt reads layout inputs internally; name them to refresh after - // a width or font change even when the ranges are unchanged. - root.spellingLayoutKey - return Spelling.underlineSegments(modelData.start, modelData.end, - function(position) { return bodyEdit.positionToRectangle(position) }, - bodyEdit.leftPadding, bodyEdit.width - bodyEdit.rightPadding) - } - Repeater { - model: segments - delegate: Rectangle { - required property var modelData - objectName: "spelling-underline" - x: modelData.x - y: modelData.y - width: modelData.width - height: 1 - color: root.errorColor - } - } + model: spellingSegments + delegate: Rectangle { + required property real segmentX + required property real segmentY + required property real segmentWidth + x: segmentX + y: segmentY + width: segmentWidth + objectName: "spelling-underline" + height: 1 + color: root.errorColor } } } @@ -2086,7 +2132,7 @@ DropArea { Connections { target: spellcheckLoader.item - function onCheckerChanged() { spellingDebounce.restart() } + function onCheckerChanged() { root.cancelSpellingPass(); spellingDebounce.restart() } } Rectangle { diff --git a/ui/components/SettingsPage.qml b/ui/components/SettingsPage.qml index ebbf1e1..9372503 100644 --- a/ui/components/SettingsPage.qml +++ b/ui/components/SettingsPage.qml @@ -874,7 +874,7 @@ Column { objectName: "spellingLanguagePolicy" width: parent.width text: "Checking spelling in " - + Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) + + (Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) || Qt.locale().name) + (root.service && Spelling.decodeSettings(root.service.settings).language !== "" ? " (set in settings)." : " (system language).") color: root.dimColor @@ -894,7 +894,7 @@ Column { visible: !!root.service && !root.service.spellingAvailable text: root.service && root.service.spellingStatus === "no-module" ? "Spell-checking is unavailable: install the Sonnet QML module, then restart Omamail." - : "The " + Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) + : "The " + (Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) || Qt.locale().name) + " dictionary is not installed. Install the matching Hunspell " + "dictionary package (hunspell-, for example hunspell-de), then restart Omamail." color: root.accentColor diff --git a/ui/compose/SpellcheckAdapter.qml b/ui/compose/SpellcheckAdapter.qml index b54e6c4..7cb943a 100644 --- a/ui/compose/SpellcheckAdapter.qml +++ b/ui/compose/SpellcheckAdapter.qml @@ -45,6 +45,15 @@ Item { readonly property string status: !enabled ? "disabled" : (available ? "ready" : "no-dictionary") + property var verdictCache: Spelling.createCache() + function resetCache() { verdictCache = Spelling.createCache() } + function isMisspelled(word) { + if (!available || !enabled) return false + return Spelling.cachedVerdict(verdictCache, word, function(value) { + return highlighter.isWordMisspelled(value) + }) + } + signal checkerChanged() Component.onCompleted: { Session.subscribe(adapter) @@ -89,11 +98,12 @@ Item { function misspelledRanges(text) { if (!available || !enabled) return [] return Spelling.misspelledRanges(text, function(word) { - return highlighter.isWordMisspelled(word) + return adapter.isMisspelled(word) }) } function applySettings() { + resetCache() // The request is attempted even when the previous one reverted: a later // successful call is the only way back from a missing dictionary, and // guarding on the prior result would leave the switch unrecoverable. diff --git a/ui/compose/Spelling.js b/ui/compose/Spelling.js index 069643d..1a135a9 100644 --- a/ui/compose/Spelling.js +++ b/ui/compose/Spelling.js @@ -2,29 +2,124 @@ // Spelling presentation rules shared by the composer and optional adapter. // Positions are UTF-16 offsets, matching Qt's text document API. -function misspelledRanges(text, isMisspelled) { - var ranges = [] - if (!text) return ranges +var MAX_WINDOW_CHARACTERS = 20000 +var MAX_UNDERLINE_RANGES = 400 +var MAX_WORD_CHARACTERS = 128 +var MAX_CACHE_WORDS = 4096 +var SLICE_MILLISECONDS = 8 +var MAX_SLICE_WORDS = 12 +var MAX_SLICE_CHARACTERS = 512 + +// Do not split a token at a window edge or traverse an arbitrarily long word. +// If expansion cannot fit the cap, skip that token rather than check a fragment. +function wordWindow(text, start, end) { var length = text.length - var index = 0 - while (index < length) { - if (!isWordChar(text.charAt(index))) { index += 1; continue } - var start = index - while (index < length && isWordChar(text.charAt(index))) index += 1 - var end = index - // Nothing follows: this is the word still being typed. Earlier finished - // words have already been collected, so stop. - if (end >= length) break - var wordStart = start - var wordEnd = end - while (wordStart < wordEnd && isApostrophe(text.charAt(wordStart))) wordStart += 1 - while (wordEnd > wordStart && isApostrophe(text.charAt(wordEnd - 1))) wordEnd -= 1 - if (wordEnd > wordStart) { - var word = text.substring(wordStart, wordEnd) - if (isMisspelled(word)) ranges.push({ start: wordStart, end: wordEnd }) + var from = Math.max(0, Math.min(length, Math.floor(start))) + var to = Math.max(from, Math.min(length, Math.ceil(end))) + var walked = 0 + while (from > 0 && isWordChar(text.charAt(from - 1)) && walked < MAX_WORD_CHARACTERS) { + from -= 1; walked += 1 + } + if (from > 0 && isWordChar(text.charAt(from - 1))) { + from = Math.max(0, Math.min(length, Math.floor(start))) + while (from < length && isWordChar(text.charAt(from)) && from - start < MAX_WORD_CHARACTERS) from += 1 + } + to = Math.min(to, from + MAX_WINDOW_CHARACTERS) + var originalEnd = to + while (to < length && isWordChar(text.charAt(to)) + && to - originalEnd < MAX_WORD_CHARACTERS && to - from < MAX_WINDOW_CHARACTERS) to += 1 + if (to < length && isWordChar(text.charAt(to))) { + while (to > from && isWordChar(text.charAt(to - 1))) to -= 1 + } + return { start: from, end: to } +} + +// Find the containing paragraph with the native string search, then examine +// only its bounded indentation. No quoted words reach the dictionary checker. +function quotedAt(text, position) { + var at = text.lastIndexOf("\n", Math.max(0, position) - 1) + 1 + var first = at + while (at < text.length && (text.charAt(at) === " " || text.charAt(at) === "\t") + && at - first < MAX_WORD_CHARACTERS) at += 1 + return text.charAt(at) === ">" || text.charAt(at) === " " || text.charAt(at) === "\t" +} + +function createPass(text, window, visibleStart, visibleEnd, revision) { + return { text: text.substring(window.start, window.end), offset: window.start, + index: 0, documentLength: text.length, quoted: quotedAt(text, window.start), + lineStart: false, visibleStart: visibleStart, visibleEnd: visibleEnd, + revision: revision, ranges: [], skipLong: window.start > 0 + && isWordChar(text.charAt(window.start - 1)) && isWordChar(text.charAt(window.start)), + done: window.end <= window.start } +} + +function nextWord(pass, limit) { + var text = pass.text + while (pass.index < text.length && pass.index < limit) { + var ch = text.charAt(pass.index) + if (ch === "\n") { + pass.index += 1; pass.lineStart = true; pass.quoted = false; pass.skipLong = false; continue + } + if (pass.lineStart) { + if (ch === " " || ch === "\t") { pass.index += 1; continue } + pass.quoted = ch === ">"; pass.lineStart = false + } + if (pass.skipLong) { + if (isWordChar(ch)) { pass.index += 1; continue } + pass.skipLong = false + } + if (pass.quoted || !isWordChar(ch)) { pass.index += 1; continue } + var start = pass.index + while (pass.index < text.length && isWordChar(text.charAt(pass.index)) + && pass.index - start <= MAX_WORD_CHARACTERS) pass.index += 1 + var end = pass.index + if (pass.offset + end >= pass.documentLength) continue // unfinished final word + if (end - start > MAX_WORD_CHARACTERS) { pass.skipLong = true; continue } + while (start < end && isApostrophe(text.charAt(start))) start += 1 + while (end > start && isApostrophe(text.charAt(end - 1))) end -= 1 + if (end > start) return { start: pass.offset + start, end: pass.offset + end, + word: text.substring(start, end) } + } + return null +} + +// The injected clock makes the time rule deterministic in tests. `paint` runs +// inside the same slice so native geometry and delegate creation share its budget. +function advancePass(pass, isMisspelled, clock, paint) { + var began = clock() + var count = 0 + var limit = pass.index + MAX_SLICE_CHARACTERS + while (!pass.done && count < MAX_SLICE_WORDS && clock() - began < SLICE_MILLISECONDS) { + var range = nextWord(pass, limit) + if (!range) { pass.done = pass.index >= pass.text.length; break } + count += 1 + if (isMisspelled(range.word) && range.end > pass.visibleStart && range.start < pass.visibleEnd) { + var stored = { start: range.start, end: range.end } + if (paint && paint(stored) === false) continue + pass.ranges.push(stored) + if (pass.ranges.length >= MAX_UNDERLINE_RANGES) pass.done = true } } - return ranges + return pass.done +} + +function createCache() { return { words: ({}), size: 0 } } +function cachedVerdict(cache, word, check) { + // Prefixing keeps arbitrary words such as __proto__ away from object keys. + var key = "$" + word + if (Object.prototype.hasOwnProperty.call(cache.words, key)) return cache.words[key] + if (cache.size >= MAX_CACHE_WORDS) { cache.words = ({}); cache.size = 0 } + var verdict = check(word) + cache.words[key] = verdict; cache.size += 1 + return verdict +} + +// Small-text helper for non-visual callers; production uses incremental passes. +function misspelledRanges(text, isMisspelled) { + var window = wordWindow(text, 0, text.length) + var pass = createPass(text, window, window.start, window.end, 0) + while (!pass.done) advancePass(pass, isMisspelled, function() { return 0 }, null) + return pass.ranges } function isWordChar(character) { diff --git a/ui/compose/SpellingSession.js b/ui/compose/SpellingSession.js index 65982a2..2a587d9 100644 --- a/ui/compose/SpellingSession.js +++ b/ui/compose/SpellingSession.js @@ -13,5 +13,8 @@ function unsubscribe(adapter) { function changed() { var current = adapters.slice() - for (var i = 0; i < current.length; i++) current[i].checkerChanged() + for (var i = 0; i < current.length; i++) { + current[i].resetCache() + current[i].checkerChanged() + } } diff --git a/ui/tests/qml/tst_service_settings.qml b/ui/tests/qml/tst_service_settings.qml index 09ba2c2..5c6a366 100644 --- a/ui/tests/qml/tst_service_settings.qml +++ b/ui/tests/qml/tst_service_settings.qml @@ -2,6 +2,7 @@ import QtQuick 2.15 import QtTest 1.3 import "../.." as Omamail import "../../account/Unified.js" as Unified +import "../../compose/Spelling.js" as Spelling Item { width: 400 @@ -164,7 +165,7 @@ Item { function test_spelling_defaults_on_and_persists_changes() { mailService.applySettings({}) compare(mailService.spellingEnabled, true) - compare(mailService.spellingLanguage, Qt.locale().name) + compare(mailService.spellingLanguage, Spelling.resolveLanguage("", Qt.locale().name)) mailService.setSpellingEnabled(false) compare(mailService.spellingEnabled, false) @@ -183,11 +184,11 @@ Item { for (var i = 0; i < 5; i++) { var values = ["../x", "en_US\n", "en_US\u0000", "éé", "x".repeat(500)] mailService.applySettings({ spellingLanguage: values[i] }) - compare(mailService.spellingLanguage, Qt.locale().name) + compare(mailService.spellingLanguage, Spelling.resolveLanguage("", Qt.locale().name)) } mailService.setSpellingLanguage("") compare(shellStore.updatedEntry.spellingLanguage, "") - compare(mailService.spellingLanguage, Qt.locale().name) + compare(mailService.spellingLanguage, Spelling.resolveLanguage("", Qt.locale().name)) } // The probe loads Sonnet through the same adapter the composer uses; it diff --git a/ui/tests/qml/tst_spellcheck_composer.qml b/ui/tests/qml/tst_spellcheck_composer.qml index c2cb6b5..311a821 100644 --- a/ui/tests/qml/tst_spellcheck_composer.qml +++ b/ui/tests/qml/tst_spellcheck_composer.qml @@ -1,6 +1,7 @@ import QtQuick import QtTest import "../../components" as Omamail +import "../../compose/Spelling.js" as Spelling // The real composer attaches the optional spelling adapter to its body // document, does not change the draft while checking, and unloads cleanly when @@ -78,6 +79,91 @@ Item { compose.contentDirection = "" compose.spellingEnabled = true compose.spellingLanguage = "en_US" + named(compose, "compose-body").contentY = 0 + } + + function readyAdapter() { + tryVerify(function() { return compose.spellingAdapter !== null || compose.spellingStatus === "no-module" }, 3000) + if (!compose.spellingAvailable) { skip("spelling unavailable (" + compose.spellingStatus + ")"); return null } + return compose.spellingAdapter + } + + function test_long_body_only_paints_visible_bounded_ranges_and_scrolls_to_end() { + if (!readyAdapter()) return + var body = named(compose, "compose-body-editor") + var flick = named(compose, "compose-body") + body.text = "mispelled ".repeat(10000) + flick.contentY = 0 + compose.refreshSpellingRanges() + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }, 5000) + verify(compose.spellingDelegateCount > 0) + verify(compose.spellingDelegateCount <= Spelling.MAX_UNDERLINE_RANGES) + verify(compose.spellingRanges.length <= Spelling.MAX_UNDERLINE_RANGES) + verify(compose.spellingWindow.end - compose.spellingWindow.start <= Spelling.MAX_WINDOW_CHARACTERS) + for (var i = 0; i < compose.spellingRanges.length; i++) { + var box = body.positionToRectangle(compose.spellingRanges[i].start) + verify(box.y >= flick.contentY && box.y < flick.contentY + flick.height) + } + verify(compose.spellingRanges[compose.spellingRanges.length - 1].end < body.text.length / 2) + flick.contentY = flick.contentHeight - flick.height + tryVerify(function() { + return compose.spellingRanges.length > 0 + && compose.spellingRanges[compose.spellingRanges.length - 1].end > 99000 + }, 5000) + verify(compose.spellingDelegateCount <= Spelling.MAX_UNDERLINE_RANGES) + verify(compose.spellingRanges[0].start > 90000) + } + + function test_revision_change_abandons_an_incomplete_pass_before_it_can_paint() { + var adapter = readyAdapter() + if (!adapter) return + var body = named(compose, "compose-body-editor") + body.text = "mispelled ".repeat(10000) + compose.refreshSpellingRanges() + compose.processSpellingSlice() + var oldPass = compose.spellingPass + verify(oldPass && !oldPass.done, "long work yields to the next tick") + body.insert(0, "correct ") + compare(compose.spellingDelegateCount, 0) + compare(compose.spellingRanges.length, 0) + compare(compose.spellingPass, null) + // Even an explicitly delivered stale continuation cannot paint. + compose.spellingPass = oldPass + compose.processSpellingSlice() + compare(compose.spellingDelegateCount, 0) + compare(compose.spellingPass, null) + } + + function test_quoted_reply_lines_never_reach_the_checker() { + var adapter = readyAdapter() + if (!adapter) return + var body = named(compose, "compose-body-editor") + body.text = ("> mispelled wrod \n").repeat(6000) + adapter.resetCache() + compose.refreshSpellingRanges() + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }, 5000) + compare(adapter.verdictCache.size, 0) + compare(compose.spellingDelegateCount, 0) + body.cursorPosition = 4 + body.forceActiveFocus() + compare(compose.openSpellingAtCaret(), false) + } + + function test_suggestions_inspect_a_word_outside_the_processed_window_on_demand() { + if (!readyAdapter()) return + var body = named(compose, "compose-body-editor") + body.text = "correct ".repeat(12500) + "mispelled " + named(compose, "compose-body").contentY = 0 + compose.refreshSpellingRanges() + var firstWindowEnd = compose.spellingWindow.end + body.cursorPosition = body.text.length - 5 + body.forceActiveFocus() + verify(body.cursorPosition > firstWindowEnd) + verify(compose.openSpellingAtCaret()) + var menu = named(compose, "compose-text-menu") + compare(menu.spellingWord, "mispelled") + verify(menu.spellingSuggestions.length > 0) + menu.close() } function test_body_adapter_attaches_and_checking_does_not_mutate() { diff --git a/ui/tests/test_spelling.js b/ui/tests/test_spelling.js index 339b808..88e91c7 100644 --- a/ui/tests/test_spelling.js +++ b/ui/tests/test_spelling.js @@ -54,6 +54,84 @@ test("Language uses the system locale unless a short dictionary code overrides i assert.equal(rules.resolveLanguage("", "C"), "", "an unsupported system locale never reaches Sonnet") }) +test("Windows expand whole words, preserve offsets and cap hostile tokens", () => { + const r = spelling() + deepEqual(r.wordWindow("hello mispelled here ", 8, 12), { start: 6, end: 15 }) + const text = "wrod ".repeat(20000) + const window = r.wordWindow(text, 0, text.length) + assert.ok(window.end - window.start <= r.MAX_WINDOW_CHARACTERS) + assert.equal(text[window.end - 1], " ") + const hostile = "a".repeat(100000) + " wrod " + const seen = [] + r.misspelledRanges(hostile, w => { seen.push(w); return true }) + assert.equal(seen.length, 0, "a huge token never reaches the native checker") + r.misspelledRanges("a".repeat(129) + " wrod ", w => { seen.push(w); return true }) + assert.deepEqual(seen, ["wrod"], "an over-budget token is skipped, not split") + deepEqual(r.misspelledRanges("a".repeat(129) + "\nwrod ", () => true), [{ start: 130, end: 134 }]) +}) + +test("Quoted lines skip lookup including an initial window in the middle of a quote", () => { + const r = spelling() + const text = "wrod \n > mispelled wrod \n\t> more wrod \nunquoted " + const seen = [] + const ranges = r.misspelledRanges(text, w => { seen.push(w); return true }) + assert.deepEqual(seen, ["wrod", "unquoted"]) + assert.equal(ranges.length, 2) + assert.equal(r.quotedAt(text, text.indexOf("mispelled")), true) + assert.equal(r.quotedAt(text, text.indexOf("unquoted")), false) + const window = r.wordWindow(text, text.indexOf("mispelled") + 2, text.indexOf("more") + 2) + const pass = r.createPass(text, window, 0, text.length, 1) + while (!pass.done) r.advancePass(pass, () => { throw Error("quote reached checker") }, () => 0) +}) + +test("Range, token, character and deterministic time budgets yield without losing work", () => { + const r = spelling() + const text = "wrod ".repeat(10000) + const window = r.wordWindow(text, 0, text.length) + const pass = r.createPass(text, window, 0, text.length, 7) + const paint = [] + r.advancePass(pass, () => true, () => 0, range => paint.push(range)) + assert.equal(pass.ranges.length, r.MAX_SLICE_WORDS) + assert.equal(pass.done, false) + let clock = 0 + const timed = r.createPass(text, window, 0, text.length, 7) + r.advancePass(timed, () => true, () => { clock += 4; return clock }) + assert.equal(timed.ranges.length, 1) + while (!pass.done) r.advancePass(pass, () => true, () => 0) + assert.equal(pass.ranges.length, r.MAX_UNDERLINE_RANGES) + const blank = r.createPass(" ".repeat(20000), { start: 0, end: 20000 }, 0, 20000, 0) + r.advancePass(blank, () => true, () => 0) + assert.equal(blank.done, false) + assert.ok(blank.index <= r.MAX_SLICE_CHARACTERS) + const narrow = r.createPass(text, window, 1000, 1100, 0) + while (!narrow.done) r.advancePass(narrow, () => true, () => 0) + assert.ok(narrow.ranges.length > 0) + assert.ok(narrow.ranges.every(x => x.start >= 1000 && x.end <= 1100)) +}) + +test("Cached verdicts avoid repeat queries, are bounded and invalidate with the session", () => { + const r = spelling() + const session = load("compose/SpellingSession.js") + let count = 0 + let cache = r.createCache() + const checker = () => { count++; return true } + assert.equal(r.cachedVerdict(cache, "__proto__", checker), true) + assert.equal(r.cachedVerdict(cache, "__proto__", checker), true) + assert.equal(count, 1) + let notifications = 0 + const adapter = { resetCache() { cache = r.createCache() }, checkerChanged() { notifications++ } } + session.subscribe(adapter) + session.changed() + assert.equal(notifications, 1) + r.cachedVerdict(cache, "__proto__", checker) + assert.equal(count, 2, "Ignore and language notifications discard stale verdicts") + for (let i = 0; i <= r.MAX_CACHE_WORDS; i++) r.cachedVerdict(cache, "w" + i, checker) + assert.ok(cache.size <= r.MAX_CACHE_WORDS) + session.unsubscribe(adapter) + session.changed() + assert.equal(notifications, 1) +}) + test("Underlines cover every visual line of a word and descending caret positions", () => { const rules = spelling() const positions = [ From 74917ec991547a7bf5d882310d065c3a3057bf06 Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:03:15 -0500 Subject: [PATCH 25/88] Add local spelling session and native settings refusal regressions --- app/tests/native/tst_settings.cpp | 14 ++++++++ ui/tests/qml/tst_spellcheck_adapter.qml | 27 +++++++++++++++ ui/tests/qml/tst_spellcheck_composer.qml | 44 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/app/tests/native/tst_settings.cpp b/app/tests/native/tst_settings.cpp index e20cb18..615539c 100644 --- a/app/tests/native/tst_settings.cpp +++ b/app/tests/native/tst_settings.cpp @@ -75,6 +75,20 @@ void SettingsTest::acceptsSpellingSettings() QVERIFY2(store.replace(settings, &error), qPrintable(error)); QCOMPARE(store.load(&error), settings); QVERIFY(error.isEmpty()); + auto systemLanguage = settings; + systemLanguage[QStringLiteral("spellingLanguage")] = QString(); + QVERIFY2(store.replace(systemLanguage, &error), qPrintable(error)); + QCOMPARE(store.load(&error), systemLanguage); + + QFile saved(path); + QVERIFY(saved.open(QIODevice::ReadOnly)); + const QByteArray original = saved.readAll(); + saved.close(); + auto unreviewed = systemLanguage; + unreviewed[QStringLiteral("unreviewedSpellingSetting")] = true; + QVERIFY(!store.replace(unreviewed, &error)); + QVERIFY(saved.open(QIODevice::ReadOnly)); + QCOMPARE(saved.readAll(), original); // refusal must not modify the saved settings } void SettingsTest::invalidJsonIsPreserved() diff --git a/ui/tests/qml/tst_spellcheck_adapter.qml b/ui/tests/qml/tst_spellcheck_adapter.qml index 4b80a14..a92c966 100644 --- a/ui/tests/qml/tst_spellcheck_adapter.qml +++ b/ui/tests/qml/tst_spellcheck_adapter.qml @@ -49,6 +49,33 @@ Item { return a } + function test_language_changes_discard_native_verdict_cache() { + var a = adapter() + if (!a) return + compare(a.isMisspelled("mispelled"), true) + verify(a.verdictCache.size > 0) + a.language = "zz_ZZ" + compare(a.available, false) + compare(a.verdictCache.size, 0) + a.language = "en_US" + compare(a.available, true) + compare(a.verdictCache.size, 0) + compare(a.isMisspelled("mispelled"), true) + } + + function test_hostile_override_never_becomes_the_native_dictionary_name() { + var a = adapter() + if (!a) return + try { + var values = ["../x", "en_US\r\n", "en_US\u0000", "éé", "x".repeat(500)] + for (var i = 0; i < values.length; i++) { + a.language = values[i] + verify(a.resolvedLanguage !== values[i]) + verify(a.resolvedLanguage === Qt.locale().name || a.resolvedLanguage === "") + } + } finally { a.language = "en_US" } + } + function test_ready_and_status() { var a = adapter() if (!a) return diff --git a/ui/tests/qml/tst_spellcheck_composer.qml b/ui/tests/qml/tst_spellcheck_composer.qml index 311a821..9ac3bd4 100644 --- a/ui/tests/qml/tst_spellcheck_composer.qml +++ b/ui/tests/qml/tst_spellcheck_composer.qml @@ -24,6 +24,12 @@ Item { ] property string accountEmail: "me@example.com" property string activeAccountId: "me@example.com" + property var configWrites: [] + function writeConfig(name, text, callback) { + configWrites = configWrites.concat([String(name)]) + if (typeof callback === "function") callback(false, "No filesystem writer in this fixture") + return false + } function preferredSendAs(_r) { return null } function switchTo(_id) { return true } function refreshRecipientContacts() {} @@ -166,6 +172,44 @@ Item { menu.close() } + function test_complete_local_spelling_session() { + if (!readyAdapter()) return + mailService.configWrites = [] + var body = named(compose, "compose-body-editor") + var menu = named(compose, "compose-text-menu") + body.forceActiveFocus() + var typed = "mispelled " + for (var i = 0; i < typed.length; i++) keyClick(typed.toUpperCase().charCodeAt(i), Qt.NoModifier, 0) + compare(body.text, typed) + tryVerify(function() { return compose.spellingRanges.length > 0 }) + body.cursorPosition = 3 + verify(compose.openSpellingAtCaret()) + compare(menu.spellingWord, "mispelled") + verify(menu.spellingSuggestions.length > 0) + menu.chooseSuggestion(0) + verify(body.text !== typed) + compare(compose.bodyWasEdited, true) + body.undo() + compare(body.text, typed) + body.insert(body.text.length, "zqxjlocalonlyspell ") + body.cursorPosition = body.text.indexOf("zqxjlocalonlyspell") + 2 + body.forceActiveFocus() + verify(compose.openSpellingAtCaret()) + menu.ignoreWordRow.activated() + compare(compose.spellingAdapter.inspect(body.cursorPosition).misspelled, false) + compose.spellingEnabled = false + tryVerify(function() { return compose.spellingAdapter === null }) + compare(compose.spellingDelegateCount, 0) + compose.spellingEnabled = true + tryVerify(function() { return compose.spellingAvailable }) + compose.spellingLanguage = "zz_ZZ" + tryCompare(compose, "spellingStatus", "no-dictionary") + compare(compose.spellingDelegateCount, 0) + compose.spellingLanguage = "en_US" + tryCompare(compose, "spellingStatus", "ready") + compare(mailService.configWrites.length, 0, "no spelling file writer was invoked") + } + function test_body_adapter_attaches_and_checking_does_not_mutate() { var body = named(compose, "compose-body-editor") verify(body, "the composer owns a body editor") From 9cf9dd2a0def830278ae16e0a81840fc1024773e Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 20:49:08 -0500 Subject: [PATCH 26/88] Skip email addresses and web links in composer spelling --- ui/components/ComposeView.qml | 1 + ui/compose/SpellcheckAdapter.qml | 4 ++ ui/compose/Spelling.js | 33 +++++++++- ui/tests/qml/tst_spellcheck_adapter.qml | 20 ++++++ ui/tests/qml/tst_spellcheck_composer.qml | 48 ++++++++++++++ ui/tests/test_spelling.js | 82 ++++++++++++++++++++++++ 6 files changed, 187 insertions(+), 1 deletion(-) diff --git a/ui/components/ComposeView.qml b/ui/components/ComposeView.qml index 98ae83e..2acd23f 100644 --- a/ui/components/ComposeView.qml +++ b/ui/components/ComposeView.qml @@ -2116,6 +2116,7 @@ DropArea { onLoaded: { if (!item) return item.document = bodyEdit.textDocument + item.editor = bodyEdit item.enabled = root.spellingEnabled root.refreshSpellingRanges() } diff --git a/ui/compose/SpellcheckAdapter.qml b/ui/compose/SpellcheckAdapter.qml index 7cb943a..7223783 100644 --- a/ui/compose/SpellcheckAdapter.qml +++ b/ui/compose/SpellcheckAdapter.qml @@ -22,6 +22,8 @@ Item { // highlighter by destroying the adapter (unload its Loader), not by clearing // this. property var document: null + // Read plain text only on inspection, not through a whole-body change binding. + property var editor: null // `enabled` is reused from Item rather than redeclared: the adapter is // non-visual, so this keeps the fixed interface name without shadowing // QQuickItem.enabled (which qmllint rejects). The owning Loader is inactive @@ -68,6 +70,8 @@ Item { function inspect(position) { if (!available || document === null) return { word: "", misspelled: false, suggestions: [] } var at = Math.max(0, Math.floor(position)) + if (editor !== null && Spelling.nonProseAt(editor.text, at)) + return { word: "", misspelled: false, suggestions: [] } var list = highlighter.suggestions(at, 5) return { word: String(highlighter.wordUnderMouse), diff --git a/ui/compose/Spelling.js b/ui/compose/Spelling.js index 1a135a9..460343d 100644 --- a/ui/compose/Spelling.js +++ b/ui/compose/Spelling.js @@ -5,6 +5,7 @@ var MAX_WINDOW_CHARACTERS = 20000 var MAX_UNDERLINE_RANGES = 400 var MAX_WORD_CHARACTERS = 128 +var MAX_CHUNK_CHARACTERS = 256 var MAX_CACHE_WORDS = 4096 var SLICE_MILLISECONDS = 8 var MAX_SLICE_WORDS = 12 @@ -44,8 +45,37 @@ function quotedAt(text, position) { return text.charAt(at) === ">" || text.charAt(at) === " " || text.charAt(at) === "\t" } +// A bounded chunk rule, not URL parsing: none of its word fragments are prose. +// Retain the document string in a pass so window edges cannot hide a prefix/@. +function nonProseAt(text, position) { + var at = Math.max(0, Math.min(text.length, Math.floor(position))) + if ((at === text.length || chunkDelimiter(text.charAt(at))) + && at > 0 && isWordChar(text.charAt(at - 1))) at -= 1 + if (at === text.length || chunkDelimiter(text.charAt(at))) return false + var from = at + var to = at + while (from > 0 && !chunkDelimiter(text.charAt(from - 1))) { + if (at - from >= MAX_CHUNK_CHARACTERS) return true + from -= 1 + } + while (to < text.length && !chunkDelimiter(text.charAt(to))) { + if (to - from >= MAX_CHUNK_CHARACTERS) return true + to += 1 + } + var chunk = text.substring(from, to) + if (/^(?:[A-Za-z][A-Za-z0-9+.-]*:\/\/|mailto:|www\.)/i.test(chunk)) return true + var email = chunk.indexOf("@") + while (email >= 0) { + if (isWordChar(chunk.charAt(email - 1)) && isWordChar(chunk.charAt(email + 1))) return true + email = chunk.indexOf("@", email + 1) + } + return false +} + +function chunkDelimiter(character) { return /[\s<>()\[\]"',;]/.test(character) } + function createPass(text, window, visibleStart, visibleEnd, revision) { - return { text: text.substring(window.start, window.end), offset: window.start, + return { documentText: text, text: text.substring(window.start, window.end), offset: window.start, index: 0, documentLength: text.length, quoted: quotedAt(text, window.start), lineStart: false, visibleStart: visibleStart, visibleEnd: visibleEnd, revision: revision, ranges: [], skipLong: window.start > 0 @@ -93,6 +123,7 @@ function advancePass(pass, isMisspelled, clock, paint) { var range = nextWord(pass, limit) if (!range) { pass.done = pass.index >= pass.text.length; break } count += 1 + if (nonProseAt(pass.documentText, range.start)) continue if (isMisspelled(range.word) && range.end > pass.visibleStart && range.start < pass.visibleEnd) { var stored = { start: range.start, end: range.end } if (paint && paint(stored) === false) continue diff --git a/ui/tests/qml/tst_spellcheck_adapter.qml b/ui/tests/qml/tst_spellcheck_adapter.qml index a92c966..d6158f1 100644 --- a/ui/tests/qml/tst_spellcheck_adapter.qml +++ b/ui/tests/qml/tst_spellcheck_adapter.qml @@ -25,6 +25,7 @@ Item { source: "../../compose/SpellcheckAdapter.qml" onLoaded: { item.document = edit.textDocument + item.editor = edit item.enabled = true item.language = "en_US" } @@ -83,6 +84,25 @@ Item { compare(a.status, "ready") } + function test_inspection_skips_non_prose_and_follows_edits() { + var a = adapter() + if (!a) return + var chunks = ["sam@example.com", "first.last+tag@sub.example.co.uk", + "https://x.y/recieve?teh=wrod#shedule", "www.x.y/wrod", "mailto:sam", "wrod-".repeat(10000)] + for (var i = 0; i < chunks.length; i++) { + edit.text = "<" + chunks[i] + "> mispelled " + var at = edit.text.indexOf("@") + if (at < 0) at = edit.text.indexOf("wrod") + if (at < 0) at = 1 + var info = a.inspect(at) + compare(info.misspelled, false) + compare(info.suggestions.length, 0) + compare(a.inspect(edit.text.indexOf("mispelled")).misspelled, true) + } + edit.text = "mispelled " + compare(a.inspect(2).misspelled, true, "non-prose does not poison later inspection") + } + function test_inspect_word_misspelling_and_suggestions() { var a = adapter() if (!a) return diff --git a/ui/tests/qml/tst_spellcheck_composer.qml b/ui/tests/qml/tst_spellcheck_composer.qml index 9ac3bd4..9e587d6 100644 --- a/ui/tests/qml/tst_spellcheck_composer.qml +++ b/ui/tests/qml/tst_spellcheck_composer.qml @@ -140,6 +140,54 @@ Item { compare(compose.spellingPass, null) } + function test_reply_addresses_and_links_are_not_underlined_but_adjacent_typos_are() { + if (!readyAdapter()) return + var body = named(compose, "compose-body-editor") + body.text = "On Fri, Sam Rivera wrote: shedule https://x.y/recieve?teh=wrod#shedule. " + compose.refreshSpellingRanges() + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + var typo = body.text.indexOf("shedule") + var address = body.text.indexOf("sam@example.com") + var link = body.text.indexOf("https:") + var found = false + for (var i = 0; i < compose.spellingRanges.length; i++) { + var range = compose.spellingRanges[i] + if (range.start === typo) found = true + verify(range.end <= address || range.start >= address + 15) + verify(range.end <= link) + } + verify(found, "a real prose typo on the same line still has an underline") + var before = body.text + var box = body.positionToRectangle(address + 1) + mouseClick(body, box.x + 2, box.y + box.height / 2, Qt.RightButton) + var menu = named(compose, "compose-text-menu") + tryCompare(menu, "opened", true) + compare(menu.spellingMisspelled, false) + compare(menu.spellingSuggestions.length, 0) + compare(menu.ignoreWordRow.visible, false) + menu.close() + body.cursorPosition = link + 14 + body.forceActiveFocus() + keyClick(Qt.Key_Period, Qt.ControlModifier) + wait(20) + compare(menu.opened, false, "Ctrl+. has no correction menu for a link") + compare(compose.openSpellingAtCaret(), false) + compare(body.text, before) + } + + function test_non_prose_never_warms_the_dictionary_cache() { + var adapter = readyAdapter() + if (!adapter) return + var body = named(compose, "compose-body-editor") + body.text = " https://x.y/recieve?teh=wrod#shedule www.x.y/wrod mailto:sam " + adapter.resetCache() + compose.refreshSpellingRanges() + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + compare(adapter.verdictCache.size, 0) + compare(compose.spellingRanges.length, 0) + compare(compose.spellingDelegateCount, 0) + } + function test_quoted_reply_lines_never_reach_the_checker() { var adapter = readyAdapter() if (!adapter) return diff --git a/ui/tests/test_spelling.js b/ui/tests/test_spelling.js index 88e91c7..7132205 100644 --- a/ui/tests/test_spelling.js +++ b/ui/tests/test_spelling.js @@ -29,6 +29,88 @@ test("Ranges keep UTF-16 offsets, apostrophes, combining marks and repeated word deepEqual(rules.misspelledRanges("'' ’ 😀 ", wrong), []) }) +test("Email chunks skip every token without dictionary calls", () => { + const r = spelling() + for (const address of ["sam@example.com", "first.last+tag@sub.example.co.uk", "élise@exemple.fr"]) { + for (const wrap of [["", ""], ["<", ">"], ["(", ")"], ["[", "]"], ['"', '"'], ["'", "'"], [",", ";"]]) { + const text = "shedule " + wrap[0] + address + wrap[1] + " recieve " + const seen = [] + const ranges = r.misspelledRanges(text, word => { seen.push(word); return true }) + assert.deepEqual(seen, ["shedule", "recieve"]) + deepEqual(ranges, [{ start: 0, end: 7 }, { start: text.indexOf("recieve"), end: text.indexOf("recieve") + 7 }]) + assert.equal(r.nonProseAt(text, text.indexOf(address)), true) + } + } +}) + +test("Link chunks skip schemes, mailto, www and misspelled paths/query/fragment", () => { + const r = spelling() + for (const link of ["http://x.y/recieve", "https://x.y/recieve?teh=wrod#shedule", + "ftp://x.y/wrod", "file:///recieve", "a+b.c-d://wrod", "HTTPS://x.y/wrod", + "www.x.y/recieve", "mailto:sam", "mailto:sam@example.com"]) { + for (const suffix of [" ", ". ", ", ", "; "]) { + const text = "shedule (" + link + ")" + suffix + "recieve " + const seen = [] + r.misspelledRanges(text, word => { seen.push(word); return true }) + assert.deepEqual(seen, ["shedule", "recieve"], text) + assert.equal(r.nonProseAt(text, text.indexOf(link)), true) + } + } + deepEqual(r.misspelledRanges("see https://x.y/recieve. ", w => w !== "see"), []) +}) + +test("Incomplete @ and non-link prefixes do not suppress prose", () => { + const r = spelling() + for (const text of ["@ noon ", "me@ ", "@me ", "me@.noon ", "1http://wrod ", "http:wrod "]) { + assert.equal(r.nonProseAt(text, text.indexOf(" ") - 1), false, text) + const seen = [] + r.misspelledRanges(text, word => { seen.push(word); return true }) + assert.ok(seen.length > 0, text) + } + const seen = [] + r.misspelledRanges("Thanks shedule, sam@example.com ", word => { seen.push(word); return true }) + assert.deepEqual(seen, ["Thanks", "shedule"]) +}) + +test("Non-prose spans survive window edges and caret ends", () => { + const r = spelling() + const text = "https://x.y/recieve?teh=wrod#shedule sam@example.com recieve " + for (const word of ["recieve", "teh", "wrod", "shedule", "sam", "example", "com"]) { + const at = text.indexOf(word) + for (const position of [at, at + word.length]) assert.equal(r.nonProseAt(text, position), true) + const window = r.wordWindow(text, at, at + word.length) + const pass = r.createPass(text, window, at, at + word.length, 0) + while (!pass.done) r.advancePass(pass, () => { throw Error("window fragment reached dictionary") }, () => 0) + } + assert.equal(r.nonProseAt(text, text.lastIndexOf("recieve")), false) +}) + +test("Overlong chunks are skipped with bounded scans and skipped words still yield", () => { + const r = spelling() + const cap = r.MAX_CHUNK_CHARACTERS + const chunk = "wrod-".repeat(20000) + for (const at of [0, 100, 50000, chunk.length - 1]) { + let reads = 0 + const input = { length: chunk.length, charAt: index => { reads++; return chunk.charAt(index) } } + assert.equal(r.nonProseAt(input, at), true) + assert.ok(reads <= cap + 8, "the cap bounds character reads even in a huge chunk") + } + const exact = "-".repeat(cap - 4) + "wrod" + assert.equal(r.nonProseAt(exact + " ", cap - 2), false) + assert.equal(r.nonProseAt("-" + exact + " ", cap - 1), true) + const text = chunk + " shedule " + const window = { start: chunk.length - 40, end: text.length } + const pass = r.createPass(text, window, 0, text.length, 0) + const seen = [] + while (!pass.done) r.advancePass(pass, w => { seen.push(w); return true }, () => 0) + assert.deepEqual(seen, ["shedule"]) + const repeated = "sam@example.com ".repeat(10000) + const bounded = r.createPass(repeated, r.wordWindow(repeated, 0, repeated.length), 0, repeated.length, 0) + r.advancePass(bounded, () => { throw Error("address reached dictionary") }, () => 0) + assert.equal(bounded.done, false) + assert.ok(bounded.index <= r.MAX_SLICE_CHARACTERS) +}) + test("Settings default to enabled system language and only explicit false disables checking", () => { const rules = spelling() for (const settings of [null, {}, { spellingEnabled: "no" }, { spellingEnabled: 0 }]) { From 98f313afff928e7ccf6beb2344b8bbce26b66ff8 Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 22:35:35 -0500 Subject: [PATCH 27/88] Fix spelling underlines disappearing during typing and scrolling --- ui/components/ComposeView.qml | 43 +++++++++++++--- ui/tests/qml/tst_spellcheck_composer.qml | 62 +++++++++++++++++++++++- 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/ui/components/ComposeView.qml b/ui/components/ComposeView.qml index 2acd23f..28990d0 100644 --- a/ui/components/ComposeView.qml +++ b/ui/components/ComposeView.qml @@ -88,8 +88,24 @@ DropArea { spellingGeneration += 1 spellingPass = null spellingSlice.stop() - spellingSegments.clear() - spellingRanges = [] + } + + // Keep only unchanged prefixes; native string comparisons avoid a body scan. + // Include the delimiter so typing at a word's end drops that word too. + function retainSpellingBeforeEdit(text, previous) { + var low = 0 + var high = spellingRanges.length + while (low < high) { + var mid = Math.floor((low + high) / 2) + var end = spellingRanges[mid].end + 1 + if (text.substring(0, end) === previous.substring(0, end)) low = mid + 1 + else high = mid + } + spellingRanges = spellingRanges.slice(0, low) + var keptEnd = low > 0 ? spellingRanges[low - 1].end : 0 + while (spellingSegments.count > 0 + && spellingSegments.get(spellingSegments.count - 1).wordEnd > keptEnd) + spellingSegments.remove(spellingSegments.count - 1) } function scheduleSpellingLayout() { @@ -100,7 +116,11 @@ DropArea { function refreshSpellingRanges() { cancelSpellingPass() var adapter = spellingAdapter - if (!adapter || !spellingAvailable || !spellingEnabled || bodyFlick.height <= 0) return + if (!adapter || !spellingAvailable || !spellingEnabled || bodyFlick.height <= 0) { + spellingSegments.clear() + spellingRanges = [] + return + } var text = bodyEdit.text var top = Math.max(0, bodyFlick.contentY) var bottom = top + bodyFlick.height @@ -113,6 +133,7 @@ DropArea { spellingWindow = Spelling.wordWindow(text, start, end) spellingPass = Spelling.createPass(text, spellingWindow, visibleStart, visibleEnd, bodyRevision) spellingPass.generation = spellingGeneration + spellingPass.segments = [] spellingSlice.restart() } @@ -128,21 +149,26 @@ DropArea { var segments = Spelling.underlineSegments(range.start, range.end, function(position) { return bodyEdit.positionToRectangle(position) }, bodyEdit.leftPadding, bodyEdit.width - bodyEdit.rightPadding) - var before = spellingSegments.count - for (var i = 0; i < segments.length && spellingSegments.count < Spelling.MAX_UNDERLINE_RANGES; i++) { + var before = pass.segments.length + for (var i = 0; i < segments.length && pass.segments.length < Spelling.MAX_UNDERLINE_RANGES; i++) { // Margin words warm the checker cache; only viewport geometry paints. var segment = segments[i] if (segment.y >= bodyFlick.contentY && segment.y < bodyFlick.contentY + bodyFlick.height) - spellingSegments.append({ segmentX: segment.x, segmentY: segment.y, segmentWidth: segment.width }) + pass.segments.push({ segmentX: segment.x, segmentY: segment.y, + segmentWidth: segment.width, wordEnd: range.end }) } - return spellingSegments.count > before + return pass.segments.length > before }) if (pass.revision !== bodyRevision || pass.generation !== spellingGeneration) { cancelSpellingPass() return } + if (!pass.done) { spellingSlice.restart(); return } + // Publish a complete view without an empty intermediate frame or model. + for (var i = 0; i < pass.segments.length; i++) spellingSegments.set(i, pass.segments[i]) + if (spellingSegments.count > pass.segments.length) + spellingSegments.remove(pass.segments.length, spellingSegments.count - pass.segments.length) spellingRanges = pass.ranges.slice() - if (!pass.done) spellingSlice.restart() } onBodyRevisionChanged: cancelSpellingPass() @@ -2066,6 +2092,7 @@ DropArea { // Sonnet can emit textChanged while rehighlighting an unchanged // document. Only a plain-text change edits the draft or its revision. if (text === lastPlainText) return + root.retainSpellingBeforeEdit(text, lastPlainText) lastPlainText = text root.noteDraftChanged() if (!root.settingBodyText && activeFocus) { diff --git a/ui/tests/qml/tst_spellcheck_composer.qml b/ui/tests/qml/tst_spellcheck_composer.qml index 9e587d6..944d79a 100644 --- a/ui/tests/qml/tst_spellcheck_composer.qml +++ b/ui/tests/qml/tst_spellcheck_composer.qml @@ -5,11 +5,18 @@ import "../../compose/Spelling.js" as Spelling // The real composer attaches the optional spelling adapter to its body // document, does not change the draft while checking, and unloads cleanly when -// disabled. Underlines are proven by the adapter contract test and the Sonnet -// probe; here the point is the wiring, not the highlighter. +// disabled. These tests also exercise the visible, bounded underline passes. Item { width: 900 height: 600 + property bool watchUnderlines: false + property int minimumUnderlines: 0 + Connections { + target: compose + function onSpellingDelegateCountChanged() { + if (watchUnderlines) minimumUnderlines = Math.min(minimumUnderlines, compose.spellingDelegateCount) + } + } QtObject { id: mailService @@ -78,6 +85,7 @@ Item { } function init() { + watchUnderlines = false compose.begin("new", null, "", []) // Reset the layout and the requested language: an earlier case may have // narrowed the composer or pointed at a dictionary that is not here. @@ -120,6 +128,56 @@ Item { verify(compose.spellingRanges[0].start > 90000) } + function test_typing_keeps_underlines_before_the_caret_until_replaced() { + if (!readyAdapter()) return + var body = named(compose, "compose-body-editor") + body.text = "mispelled here wrod here " + compose.refreshSpellingRanges() + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + compare(compose.spellingRanges.length, 2) + minimumUnderlines = compose.spellingDelegateCount + watchUnderlines = true + body.cursorPosition = body.text.indexOf("wrod") + 2 + body.forceActiveFocus() + keyClick(Qt.Key_X, Qt.NoModifier, 0) + compare(compose.spellingRanges.length, 1, "only the unchanged prefix is retained") + compare(compose.spellingRanges[0].start, 0) + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + verify(minimumUnderlines > 0, "underlines never disappear during typing or publication") + compare(compose.spellingRanges[0].start, 0) + } + + function test_scrolling_keeps_underlines_until_the_next_pass() { + if (!readyAdapter()) return + var body = named(compose, "compose-body-editor") + var flick = named(compose, "compose-body") + body.text = "mispelled here wrod here \n".repeat(100) + compose.refreshSpellingRanges() + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + var underline = named(compose, "spelling-underline") + var contentY = underline.y + minimumUnderlines = compose.spellingDelegateCount + watchUnderlines = true + flick.contentY += 20 + compare(underline.y, contentY, "segments stay in content coordinates") + verify(compose.spellingDelegateCount > 0, "the scroll step keeps the existing model") + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + verify(minimumUnderlines > 0, "scrolling and publication never empty the model") + } + + function test_removing_a_word_drops_its_underline() { + if (!readyAdapter()) return + var body = named(compose, "compose-body-editor") + body.text = "mispelled here wrod here " + compose.refreshSpellingRanges() + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + compare(compose.spellingRanges.length, 2) + body.remove(15, 20) + tryVerify(function() { return compose.spellingPass && compose.spellingPass.done }) + compare(compose.spellingRanges, [{ start: 0, end: 9 }]) + compare(compose.spellingDelegateCount, 1) + } + function test_revision_change_abandons_an_incomplete_pass_before_it_can_paint() { var adapter = readyAdapter() if (!adapter) return From a7a28db8433439d4617a08182a956539dc0dd881 Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 22:37:16 -0500 Subject: [PATCH 28/88] Fix disabled spelling loading and host-specific setup guidance --- ui/components/SettingsPage.qml | 18 +++++--- ui/compose/SpellingState.qml | 6 +-- ui/tests/qml/tst_service_settings.qml | 60 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/ui/components/SettingsPage.qml b/ui/components/SettingsPage.qml index 9372503..8dd0c6e 100644 --- a/ui/components/SettingsPage.qml +++ b/ui/components/SettingsPage.qml @@ -33,6 +33,8 @@ Column { signal openCalendarRequested() signal calendarSignInRequested(int index) + readonly property string spellingLanguage: Spelling.resolveLanguage( + service ? service.spellingLanguage : "", Qt.locale().name) readonly property var accounts: service ? service.accountSummaries : [] // A separate list on purpose: accountSummaries carries live mailbox state and // is replaced on a poll, which would rebuild the field being typed in. @@ -873,8 +875,10 @@ Column { Text { objectName: "spellingLanguagePolicy" width: parent.width - text: "Checking spelling in " - + (Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) || Qt.locale().name) + id: spellingLanguagePolicy + text: root.spellingLanguage === "" + ? "No system language is set; choose one with the spellingLanguage setting." + : "Checking spelling in " + root.spellingLanguage + (root.service && Spelling.decodeSettings(root.service.settings).language !== "" ? " (set in settings)." : " (system language).") color: root.dimColor @@ -891,10 +895,14 @@ Column { Text { objectName: "spellingNeedsSetup" width: parent.width - visible: !!root.service && !root.service.spellingAvailable - text: root.service && root.service.spellingStatus === "no-module" + visible: !!root.service && root.service.spellingEnabled !== false + && !root.service.spellingAvailable && root.service.spellingStatus !== "loading" + text: root.service && root.service.standalone === true + ? "Spell-checking is not available in this build." + : root.service && root.service.spellingStatus === "no-module" ? "Spell-checking is unavailable: install the Sonnet QML module, then restart Omamail." - : "The " + (Spelling.resolveLanguage(root.service ? root.service.spellingLanguage : "", Qt.locale().name) || Qt.locale().name) + : root.spellingLanguage === "" ? spellingLanguagePolicy.text + : "The " + root.spellingLanguage + " dictionary is not installed. Install the matching Hunspell " + "dictionary package (hunspell-, for example hunspell-de), then restart Omamail." color: root.accentColor diff --git a/ui/compose/SpellingState.qml b/ui/compose/SpellingState.qml index 80040ce..a478811 100644 --- a/ui/compose/SpellingState.qml +++ b/ui/compose/SpellingState.qml @@ -13,11 +13,11 @@ Item { readonly property string language: Spelling.resolveLanguage(languageOverride, Qt.locale().name) readonly property bool available: probe.status === Loader.Ready && probe.item !== null && probe.item.available - readonly property string status: probe.status === Loader.Error - ? "no-module" : (probe.item ? probe.item.status : "loading") + readonly property string status: !enabledRequest ? "disabled" : (probe.status === Loader.Error + ? "no-module" : (probe.item ? probe.item.status : "loading")) // Sonnet enumerates dictionaries once per process. Installing one requires // restarting Omamail; a new adapter alone cannot discover it. - Loader { id: probe; source: "SpellcheckAdapter.qml" } + Loader { id: probe; active: root.enabledRequest; source: "SpellcheckAdapter.qml" } Binding { target: probe.item property: "language" diff --git a/ui/tests/qml/tst_service_settings.qml b/ui/tests/qml/tst_service_settings.qml index 5c6a366..d4b3f6a 100644 --- a/ui/tests/qml/tst_service_settings.qml +++ b/ui/tests/qml/tst_service_settings.qml @@ -1,6 +1,7 @@ import QtQuick 2.15 import QtTest 1.3 import "../.." as Omamail +import "../../components" as Components import "../../account/Unified.js" as Unified import "../../compose/Spelling.js" as Spelling @@ -32,6 +33,18 @@ Item { manifest: ({ id: "omamail" }) } + Components.SettingsPage { + id: page + width: parent.width + service: mailService + calendarController: null + textColor: Qt.rgba(1, 1, 1, 1) + dimColor: Qt.rgba(0.6, 0.6, 0.6, 1) + accentColor: Qt.rgba(1, 0.5, 0, 1) + urgentColor: Qt.rgba(1, 0.5, 0, 1) + panelFontFamily: "sans" + } + TestCase { name: "ServiceSettings" @@ -191,9 +204,56 @@ Item { compare(mailService.spellingLanguage, Spelling.resolveLanguage("", Qt.locale().name)) } + function test_spelling_setup_guidance_data() { + return [ + { tag: "plugin-module", standalone: false, status: "no-module", enabled: true, message: "install the Sonnet QML module" }, + { tag: "plugin-dictionary", standalone: false, status: "no-dictionary", enabled: true, message: "The en_US dictionary is not installed" }, + { tag: "standalone-module", standalone: true, status: "no-module", enabled: true, message: "Spell-checking is not available in this build." }, + { tag: "standalone-dictionary", standalone: true, status: "no-dictionary", enabled: true, message: "Spell-checking is not available in this build." }, + { tag: "off", standalone: false, status: "disabled", enabled: false, message: "" }, + { tag: "loading", standalone: false, status: "loading", enabled: true, message: "" } + ] + } + function test_spelling_setup_guidance(data) { + try { + page.service = { standalone: data.standalone, spellingEnabled: data.enabled, + spellingStatus: data.status, spellingAvailable: false, spellingLanguage: "en_US", + settings: { spellingLanguage: "en_US" }, accountSummaries: [] } + var notice = findChild(page, "spellingNeedsSetup") + compare(notice.visible, data.message !== "") + if (data.message !== "") verify(notice.text.indexOf(data.message) >= 0) + } finally { page.service = mailService } + } + function test_unset_system_language_guidance() { + if (Spelling.validLanguage(Qt.locale().name)) { skip("requires C/POSIX system locale"); return } + mailService.applySettings({ spellingLanguage: "" }) + compare(mailService.spellingLanguage, "") + compare(findChild(page, "spellingLanguagePolicy").text, + "No system language is set; choose one with the spellingLanguage setting.") + compare(findChild(page, "spellingNeedsSetup").text.indexOf("The C dictionary"), -1) + } + + function test_spelling_off_unloads_the_probe_and_hides_setup_warning() { + mailService.applySettings({ spellingEnabled: false }) + compare(mailService.spellingStatus, "disabled") + compare(mailService.spellingAvailable, false) + var state = null + for (var i = 0; i < mailService.children.length; i++) { + if (mailService.children[i].enabledRequest !== undefined) state = mailService.children[i] + } + verify(state) + var probe = state.children[0] + compare(probe.active, false) + compare(probe.item, null) + compare(probe.status, Loader.Null) + compare(findChild(page, "spellingNeedsSetup").visible, false) + mailService.applySettings({}) + } + // The probe loads Sonnet through the same adapter the composer uses; it // settles into one concrete status on any machine rather than hanging. function test_spelling_availability_settles_to_a_known_status() { + mailService.applySettings({}) tryVerify(function() { var status = mailService.spellingStatus return status === "ready" || status === "no-dictionary" || status === "no-module" From afe3e8f033e1887182a043adf35fb3f170071b5b Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 22:37:16 -0500 Subject: [PATCH 29/88] Clarify Sonnet missing-dictionary language restoration --- ui/compose/SpellcheckAdapter.qml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ui/compose/SpellcheckAdapter.qml b/ui/compose/SpellcheckAdapter.qml index 7223783..4eed3e1 100644 --- a/ui/compose/SpellcheckAdapter.qml +++ b/ui/compose/SpellcheckAdapter.qml @@ -33,13 +33,9 @@ Item { // Availability is about the *requested* language, and it is computed after // `setCurrentLanguage` rather than bound to `spellCheckerFound`. // - // When the requested dictionary is missing, Sonnet keeps the prior language - // and leaves `spellCheckerFound` true, so asking for `zz_ZZ` while `en_US` is - // installed would still read as ready. The accepted language is - // `currentLanguage`; comparing it with the request is what says "no - // dictionary for this one". The backing property is assigned in - // `applySettings` because `currentLanguage` does not notify, so a binding - // would hold a stale answer after the revert. + // On a missing dictionary Sonnet sets spellCheckerFound false, restores the + // previous language and returns without emitting currentLanguageChanged. + // Read both properties imperatively after the call, not through a binding. property bool requestedLanguageAvailable: false readonly property bool available: requestedLanguageAvailable // "ready", "disabled", or "no-dictionary". A missing module is reported by From 8699f1c592c892be6b669f21bffcc9f7dbfa7e8e Mon Sep 17 00:00:00 2001 From: Chris Gray <216301595+latentoperator@users.noreply.github.com> Date: Sun, 11 Oct 2026 22:38:39 -0500 Subject: [PATCH 30/88] Remove redundant spelling probes and unused APIs --- ui/Service.qml | 1 - ui/compose/SpellcheckAdapter.qml | 9 -- ui/compose/Spelling.js | 10 +- ui/tests/qml/SpellcheckProbe.qml | 41 -------- ui/tests/qml/tst_service_settings.qml | 10 +- ui/tests/qml/tst_spellcheck_adapter.qml | 31 +----- ui/tests/qml/tst_spellcheck_composer.qml | 4 +- ui/tests/qml/tst_spellcheck_probe.qml | 115 ----------------------- ui/tests/test_spelling.js | 47 +++++---- 9 files changed, 40 insertions(+), 228 deletions(-) delete mode 100644 ui/tests/qml/SpellcheckProbe.qml delete mode 100644 ui/tests/qml/tst_spellcheck_probe.qml diff --git a/ui/Service.qml b/ui/Service.qml index 6d3b871..01a9699 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -697,7 +697,6 @@ Item { readonly property alias spellingAvailable: spelling.available readonly property alias spellingStatus: spelling.status function setSpellingEnabled(value) { persistSetting("spellingEnabled", value !== false) } - function setSpellingLanguage(value) { persistSetting("spellingLanguage", String(value || "")) } // ---------------------------------------------------------- the accounts diff --git a/ui/compose/SpellcheckAdapter.qml b/ui/compose/SpellcheckAdapter.qml index 4eed3e1..0ce49c7 100644 --- a/ui/compose/SpellcheckAdapter.qml +++ b/ui/compose/SpellcheckAdapter.qml @@ -93,15 +93,6 @@ Item { Session.changed() } - // Only completed words receive underlines; Sonnet supplies the decision, - // while the shared rule keeps positions and completeness testable without Qt. - function misspelledRanges(text) { - if (!available || !enabled) return [] - return Spelling.misspelledRanges(text, function(word) { - return adapter.isMisspelled(word) - }) - } - function applySettings() { resetCache() // The request is attempted even when the previous one reverted: a later diff --git a/ui/compose/Spelling.js b/ui/compose/Spelling.js index 460343d..0880175 100644 --- a/ui/compose/Spelling.js +++ b/ui/compose/Spelling.js @@ -63,7 +63,7 @@ function nonProseAt(text, position) { to += 1 } var chunk = text.substring(from, to) - if (/^(?:[A-Za-z][A-Za-z0-9+.-]*:\/\/|mailto:|www\.)/i.test(chunk)) return true + if (chunk.indexOf("://") >= 0 || /^(?:mailto:|www\.)/i.test(chunk)) return true var email = chunk.indexOf("@") while (email >= 0) { if (isWordChar(chunk.charAt(email - 1)) && isWordChar(chunk.charAt(email + 1))) return true @@ -145,14 +145,6 @@ function cachedVerdict(cache, word, check) { return verdict } -// Small-text helper for non-visual callers; production uses incremental passes. -function misspelledRanges(text, isMisspelled) { - var window = wordWindow(text, 0, text.length) - var pass = createPass(text, window, window.start, window.end, 0) - while (!pass.done) advancePass(pass, isMisspelled, function() { return 0 }, null) - return pass.ranges -} - function isWordChar(character) { // Letters/digits in common scripts and apostrophes within words. Surrogate // halves (such as emoji) delimit words without changing UTF-16 positions. diff --git a/ui/tests/qml/SpellcheckProbe.qml b/ui/tests/qml/SpellcheckProbe.qml deleted file mode 100644 index b2172ba..0000000 --- a/ui/tests/qml/SpellcheckProbe.qml +++ /dev/null @@ -1,41 +0,0 @@ -import QtQuick -import org.kde.sonnet as Sonnet - -// Probe component: the only file under tests/ that imports org.kde.sonnet. -// It is loaded through a Loader so a machine without the module reports a -// loader error to the probe instead of failing the whole test file to import. -// -// This isolates the Sonnet runtime behavior used by the production adapter. -Item { - id: probe - - // The TextEdit whose text document is checked. Set by the caller after the - // Loader has created the item. - property var editor: null - - readonly property bool available: highlighter.spellCheckerFound - readonly property string language: highlighter.currentLanguage - - // Whether underlines are drawn. Independent of the query functions. - property alias active: highlighter.active - property alias automatic: highlighter.automatic - - //Suggestions for the word at a UTF-16 character position in the document. - function suggestionsAt(position, max) { return highlighter.suggestions(position, max) } - function isMisspelled(word) { return highlighter.isWordMisspelled(word) } - function replaceWord(word, at) { highlighter.replaceWord(word, at) } - // Session-only: process-wide, never written to disk by Sonnet. - function ignore(word) { highlighter.ignoreWord(word) } - function setLanguage(language) { highlighter.setCurrentLanguage(language) } - - onEditorChanged: if (editor !== null) highlighter.document = editor.textDocument - - Sonnet.SpellcheckHighlighter { - id: highlighter - active: true - automatic: false - // misspelledColor is not set: it has no effect on the rendered underline - // (always Qt's fixed red spell-check underline), and this repository - // forbids literal colours in QML anyway. - } -} diff --git a/ui/tests/qml/tst_service_settings.qml b/ui/tests/qml/tst_service_settings.qml index d4b3f6a..6e9ebc4 100644 --- a/ui/tests/qml/tst_service_settings.qml +++ b/ui/tests/qml/tst_service_settings.qml @@ -188,7 +188,7 @@ Item { mailService.applySettings({ spellingEnabled: "no" }) compare(mailService.spellingEnabled, true) - mailService.setSpellingLanguage("en_GB") + mailService.persistSetting("spellingLanguage", "en_GB") compare(mailService.spellingLanguage, "en_GB") compare(shellStore.updatedEntry.spellingLanguage, "en_GB") } @@ -199,7 +199,7 @@ Item { mailService.applySettings({ spellingLanguage: values[i] }) compare(mailService.spellingLanguage, Spelling.resolveLanguage("", Qt.locale().name)) } - mailService.setSpellingLanguage("") + mailService.persistSetting("spellingLanguage", "") compare(shellStore.updatedEntry.spellingLanguage, "") compare(mailService.spellingLanguage, Spelling.resolveLanguage("", Qt.locale().name)) } @@ -269,15 +269,15 @@ Item { return status === "ready" || status === "no-dictionary" || status === "no-module" }, 3000) if (mailService.spellingStatus === "no-module") { skip("Sonnet is not installed"); return } - mailService.setSpellingLanguage("en_US") + mailService.persistSetting("spellingLanguage", "en_US") tryVerify(function() { return mailService.spellingStatus === "ready" || mailService.spellingStatus === "no-dictionary" }, 3000) if (!mailService.spellingAvailable) { skip("en_US dictionary is not installed"); return } - mailService.setSpellingLanguage("zz_ZZ") + mailService.persistSetting("spellingLanguage", "zz_ZZ") tryVerify(function() { return mailService.spellingStatus === "no-dictionary" }, 3000) compare(mailService.spellingAvailable, false) - mailService.setSpellingLanguage("en_US") + mailService.persistSetting("spellingLanguage", "en_US") tryVerify(function() { return mailService.spellingStatus === "ready" }, 3000) } } diff --git a/ui/tests/qml/tst_spellcheck_adapter.qml b/ui/tests/qml/tst_spellcheck_adapter.qml index d6158f1..bb7b157 100644 --- a/ui/tests/qml/tst_spellcheck_adapter.qml +++ b/ui/tests/qml/tst_spellcheck_adapter.qml @@ -88,7 +88,8 @@ Item { var a = adapter() if (!a) return var chunks = ["sam@example.com", "first.last+tag@sub.example.co.uk", - "https://x.y/recieve?teh=wrod#shedule", "www.x.y/wrod", "mailto:sam", "wrod-".repeat(10000)] + "https://x.y/recieve?teh=wrod#shedule", "Re:http://x.y/recieve", "see:https://x.y/wrod", + "www.x.y/wrod", "mailto:sam", "wrod-".repeat(10000)] for (var i = 0; i < chunks.length; i++) { edit.text = "<" + chunks[i] + "> mispelled " var at = edit.text.indexOf("@") @@ -116,6 +117,9 @@ Item { // A correct word is not reported as misspelled. var ok = a.inspect(edit.text.indexOf("sentence")) compare(ok.misspelled, false) + compare(ok.suggestions.length, 0) + compare(a.isMisspelled("don't"), false) + verify(a.inspect(edit.text.indexOf("mispelled")).suggestions.length > 0) } function test_correction_is_undo_safe_and_does_not_touch_other_words() { @@ -139,31 +143,6 @@ Item { compare(edit.text, root.sample) } - function test_misspelled_ranges_are_only_finished_words() { - var a = adapter() - if (!a) return - // Nothing follows the word: still being typed, so not marked yet. - compare(a.misspelledRanges("wrod").length, 0) - // A delimiter finished it. - compare(a.misspelledRanges("wrod ").length, 1) - // The trailing word is skipped; the finished one before it is not. - var one = a.misspelledRanges("a wrod and mispelled") - compare(one.length, 1) - compare(one[0].start, 2) - compare(one[0].end, 6) - compare(a.misspelledRanges("a wrod and mispelled ").length, 2) - } - - function test_misspelled_ranges_use_utf16_offsets() { - var a = adapter() - if (!a) return - // "the " is four units, the emoji is two more, so wrod starts at 7. - var ranges = a.misspelledRanges("the \uD83D\uDCE8 wrod ") - compare(ranges.length, 1) - compare(ranges[0].start, 7) - compare(ranges[0].end, 11) - } - function test_session_ignore_treats_a_word_as_correct() { var a = adapter() if (!a) return diff --git a/ui/tests/qml/tst_spellcheck_composer.qml b/ui/tests/qml/tst_spellcheck_composer.qml index 944d79a..8af6315 100644 --- a/ui/tests/qml/tst_spellcheck_composer.qml +++ b/ui/tests/qml/tst_spellcheck_composer.qml @@ -553,9 +553,7 @@ Item { compose.spellingLanguage = "en_GB" wait(250) - var live = JSON.stringify(compose.spellingRanges) - var actual = JSON.stringify(compose.spellingAdapter.misspelledRanges(body.text)) - compare(live, actual, "ranges recheck after a valid-to-valid switch") + compare(compose.spellingRanges.length, 0, "en_GB accepts all the British spellings") verify(compose.spellingRanges.length < usRanges, "en_GB accepts the British spellings") } } diff --git a/ui/tests/qml/tst_spellcheck_probe.qml b/ui/tests/qml/tst_spellcheck_probe.qml deleted file mode 100644 index 8f5fa1f..0000000 --- a/ui/tests/qml/tst_spellcheck_probe.qml +++ /dev/null @@ -1,115 +0,0 @@ -import QtQuick -import QtTest - -// Probe: proves the real Sonnet QML highlighter behaviour the spelling -// adapter depends on. It loads Sonnet through a Loader so this file has no -// mandatory org.kde.sonnet import, and skips (rather than fails) where Sonnet -// or the en_US dictionary is absent. -Item { - id: root - width: 800 - height: 220 - - // The surrogate pair in the emoji is two UTF-16 units, which is what makes - // the offset check below meaningful. - readonly property string sample: "ok \uD83D\uDCE8 wrod and mispelled; don't TEH. A correct sentence follows here." - - TextEdit { - id: edit - anchors.fill: parent - textFormat: TextEdit.PlainText - font.pixelSize: 18 - wrapMode: TextEdit.Wrap - text: root.sample - } - - Loader { - id: loader - source: "SpellcheckProbe.qml" - onLoaded: item.editor = edit - } - - TestCase { - name: "SpellcheckProbe" - when: windowShown - - function init() { edit.text = root.sample } - - // Returns the probe component, or null after asking to skip when Sonnet or - // the dictionary is unavailable. - function component() { - if (loader.status === Loader.Error || !loader.item) { - skip("org.kde.sonnet QML module unavailable (loader status " + loader.status + ")") - return null - } - var probe = loader.item - probe.setLanguage("en_US") - if (!probe.available || probe.language !== "en_US") { - skip("en_US dictionary unavailable (language " + probe.language + ", available " + probe.available + ")") - return null - } - return probe - } - - function test_availability_and_language() { - var probe = component() - if (!probe) return - compare(probe.available, true) - compare(probe.language, "en_US") - } - - function test_suggestions_and_utf16_offsets() { - var probe = component() - if (!probe) return - // "ok wrod": emoji occupies two UTF-16 units, so wrod starts at 6. - compare(edit.text.indexOf("wrod"), 6) - var atWrod = probe.suggestionsAt(6, 5) - verify(atWrod.length > 0) - compare(String(atWrod[0]), "word") - verify(probe.suggestionsAt(edit.text.indexOf("mispelled"), 5).length > 0) - // A correct word yields no suggestions. - compare(probe.suggestionsAt(edit.text.indexOf("sentence"), 5).length, 0) - } - - function test_is_word_misspelled() { - var probe = component() - if (!probe) return - compare(probe.isMisspelled("wrod"), true) - compare(probe.isMisspelled("mispelled"), true) - compare(probe.isMisspelled("sentence"), false) - compare(probe.isMisspelled("don't"), false) - } - - function test_correction_integrates_with_undo() { - var probe = component() - if (!probe) return - var before = edit.text - // replaceWord selects a length that only suggestions() has set, so the - // correction must be primed by asking for suggestions at the same - // position first. Without it Sonnet inserts instead of replacing. - probe.suggestionsAt(6, 5) - probe.replaceWord("word", 6) - verify(edit.text !== before) - compare(edit.text.indexOf("word and"), 6) - edit.undo() - compare(edit.text, before) - } - - function test_ignore_is_session_only() { - var probe = component() - if (!probe) return - compare(probe.isMisspelled("qqzzx"), true) - probe.ignore("qqzzx") - compare(probe.isMisspelled("qqzzx"), false) - } - - function test_disabling_does_not_mutate_text() { - var probe = component() - if (!probe) return - var before = edit.text - probe.active = false - compare(edit.text, before) - probe.active = true - } - } -} diff --git a/ui/tests/test_spelling.js b/ui/tests/test_spelling.js index 7132205..cee554e 100644 --- a/ui/tests/test_spelling.js +++ b/ui/tests/test_spelling.js @@ -4,29 +4,35 @@ const { load, deepEqual } = require("./load") // Load inside each test so the baseline demonstrates all four missing rules. function spelling() { return load("compose/Spelling.js") } +function collectRanges(rules, text, checker) { + const window = rules.wordWindow(text, 0, text.length) + const pass = rules.createPass(text, window, window.start, window.end, 0) + while (!pass.done) rules.advancePass(pass, checker, () => 0) + return pass.ranges +} test("Only completed body words are checked", () => { const rules = spelling() const seen = [] const checker = word => { seen.push(word); return true } - deepEqual(rules.misspelledRanges("mispelled", checker), []) + deepEqual(collectRanges(rules, "mispelled", checker), []) assert.equal(seen.length, 0, "the unfinished word never reaches the checker") for (const delimiter of [" ", ".", ",", "\n", "\t"]) { - deepEqual(rules.misspelledRanges("mispelled" + delimiter, checker), [{ start: 0, end: 9 }]) + deepEqual(collectRanges(rules, "mispelled" + delimiter, checker), [{ start: 0, end: 9 }]) } - deepEqual(rules.misspelledRanges("mispelled wrod", checker), [{ start: 0, end: 9 }]) - deepEqual(rules.misspelledRanges("", checker), []) + deepEqual(collectRanges(rules, "mispelled wrod", checker), [{ start: 0, end: 9 }]) + deepEqual(collectRanges(rules, "", checker), []) }) test("Ranges keep UTF-16 offsets, apostrophes, combining marks and repeated words", () => { const rules = spelling() const wrong = word => word !== "correct" const text = "📨 'wrod', correct wrod.\n" - deepEqual(rules.misspelledRanges(text, wrong), [{ start: 4, end: 8 }, { start: 19, end: 23 }]) - deepEqual(rules.misspelledRanges("‘can’t’ cafe\u0301 Ελληνικά русский ", wrong), [ + deepEqual(collectRanges(rules, text, wrong), [{ start: 4, end: 8 }, { start: 19, end: 23 }]) + deepEqual(collectRanges(rules, "‘can’t’ cafe\u0301 Ελληνικά русский ", wrong), [ { start: 1, end: 6 }, { start: 8, end: 13 }, { start: 14, end: 22 }, { start: 23, end: 30 } ]) - deepEqual(rules.misspelledRanges("'' ’ 😀 ", wrong), []) + deepEqual(collectRanges(rules, "'' ’ 😀 ", wrong), []) }) test("Email chunks skip every token without dictionary calls", () => { @@ -35,7 +41,7 @@ test("Email chunks skip every token without dictionary calls", () => { for (const wrap of [["", ""], ["<", ">"], ["(", ")"], ["[", "]"], ['"', '"'], ["'", "'"], [",", ";"]]) { const text = "shedule " + wrap[0] + address + wrap[1] + " recieve " const seen = [] - const ranges = r.misspelledRanges(text, word => { seen.push(word); return true }) + const ranges = collectRanges(r, text, word => { seen.push(word); return true }) assert.deepEqual(seen, ["shedule", "recieve"]) deepEqual(ranges, [{ start: 0, end: 7 }, { start: text.indexOf("recieve"), end: text.indexOf("recieve") + 7 }]) assert.equal(r.nonProseAt(text, text.indexOf(address)), true) @@ -47,28 +53,28 @@ test("Link chunks skip schemes, mailto, www and misspelled paths/query/fragment" const r = spelling() for (const link of ["http://x.y/recieve", "https://x.y/recieve?teh=wrod#shedule", "ftp://x.y/wrod", "file:///recieve", "a+b.c-d://wrod", "HTTPS://x.y/wrod", - "www.x.y/recieve", "mailto:sam", "mailto:sam@example.com"]) { + "www.x.y/recieve", "mailto:sam", "mailto:sam@example.com", "Re:http://x.y/recieve", "see:https://x.y/wrod", "1http://wrod"]) { for (const suffix of [" ", ". ", ", ", "; "]) { const text = "shedule (" + link + ")" + suffix + "recieve " const seen = [] - r.misspelledRanges(text, word => { seen.push(word); return true }) + collectRanges(r, text, word => { seen.push(word); return true }) assert.deepEqual(seen, ["shedule", "recieve"], text) assert.equal(r.nonProseAt(text, text.indexOf(link)), true) } } - deepEqual(r.misspelledRanges("see https://x.y/recieve. ", w => w !== "see"), []) + deepEqual(collectRanges(r, "see https://x.y/recieve. ", w => w !== "see"), []) }) test("Incomplete @ and non-link prefixes do not suppress prose", () => { const r = spelling() - for (const text of ["@ noon ", "me@ ", "@me ", "me@.noon ", "1http://wrod ", "http:wrod "]) { + for (const text of ["@ noon ", "me@ ", "@me ", "me@.noon ", "http:wrod "]) { assert.equal(r.nonProseAt(text, text.indexOf(" ") - 1), false, text) const seen = [] - r.misspelledRanges(text, word => { seen.push(word); return true }) + collectRanges(r, text, word => { seen.push(word); return true }) assert.ok(seen.length > 0, text) } const seen = [] - r.misspelledRanges("Thanks shedule, sam@example.com ", word => { seen.push(word); return true }) + collectRanges(r, "Thanks shedule, sam@example.com ", word => { seen.push(word); return true }) assert.deepEqual(seen, ["Thanks", "shedule"]) }) @@ -133,7 +139,10 @@ test("Language uses the system locale unless a short dictionary code overrides i deepEqual(rules.decodeSettings({ spellingLanguage: hostile }), { enabled: true, language: "" }) } assert.equal(rules.resolveLanguage("", "zz_ZZ"), "zz_ZZ", "no silent English fallback") - assert.equal(rules.resolveLanguage("", "C"), "", "an unsupported system locale never reaches Sonnet") + for (const locale of ["C", "POSIX", ""]) { + assert.equal(rules.resolveLanguage("", locale), "", "an unset system locale never reaches Sonnet") + assert.equal(rules.resolveLanguage("de_DE", locale), "de_DE") + } }) test("Windows expand whole words, preserve offsets and cap hostile tokens", () => { @@ -145,18 +154,18 @@ test("Windows expand whole words, preserve offsets and cap hostile tokens", () = assert.equal(text[window.end - 1], " ") const hostile = "a".repeat(100000) + " wrod " const seen = [] - r.misspelledRanges(hostile, w => { seen.push(w); return true }) + collectRanges(r, hostile, w => { seen.push(w); return true }) assert.equal(seen.length, 0, "a huge token never reaches the native checker") - r.misspelledRanges("a".repeat(129) + " wrod ", w => { seen.push(w); return true }) + collectRanges(r, "a".repeat(129) + " wrod ", w => { seen.push(w); return true }) assert.deepEqual(seen, ["wrod"], "an over-budget token is skipped, not split") - deepEqual(r.misspelledRanges("a".repeat(129) + "\nwrod ", () => true), [{ start: 130, end: 134 }]) + deepEqual(collectRanges(r, "a".repeat(129) + "\nwrod ", () => true), [{ start: 130, end: 134 }]) }) test("Quoted lines skip lookup including an initial window in the middle of a quote", () => { const r = spelling() const text = "wrod \n > mispelled wrod \n\t> more wrod \nunquoted " const seen = [] - const ranges = r.misspelledRanges(text, w => { seen.push(w); return true }) + const ranges = collectRanges(r, text, w => { seen.push(w); return true }) assert.deepEqual(seen, ["wrod", "unquoted"]) assert.equal(ranges.length, 2) assert.equal(r.quotedAt(text, text.indexOf("mispelled")), true) From 6710e39d871319742aa3650078fdaffb8364e29e Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 19 Oct 2026 00:02:12 +0800 Subject: [PATCH 31/88] docs: Clarify contribution priorities and review evidence (#379) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Omamail’s core product is substantially complete, so contribution guidance should favor well-tested fixes to existing functionality and explain the product cost of new features and providers. This updates CONTRIBUTING.md to require labeled, detailed before-and-after screenshots for every UI change and a detailed end-to-end recording for complex features or workflows, so the maintainer can assess the result without installing the app. The review guidance skips drafts, distinguishes low-risk objective fixes from product choices, and reserves context-menu changes, feature placement, subjective design tradeoffs, large features, and architectural direction for Jason’s confirmation. An objective correction to an established convention still needs evidence and regression checks. Approval never authorizes automated merging. ## Verification Documentation-only change; no runtime code, tests, release configuration, or deployment changes. - Read current CONTRIBUTING.md, AGENTS.md, and the Makefile. - Patch applicability against main 5db17740f99feb1252ba136db20d9fb67899cd91 passed with git apply --check. - Checked added text for trailing whitespace, preserved Markdown fences, and the repository’s 128 KiB file limit. - A fresh independent reviewer found two wording gaps: approval needed an explicit low-risk limit, and the objective exception needed to be limited to established conventions. Both were corrected. - make validate was not run: no working QML test runner or omarchy executable was available for this task. The qmltestrunner command is a qtchooser shim pointing to a missing runner. CI will be checked for the published commit; this PR remains a draft. - Security: PASS for this documentation-only diff; no runtime trust boundary is changed. Prepared with ChatGPT. --- CONTRIBUTING.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 41617fa..2ef4d01 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -39,6 +39,12 @@ it, described in terms of what it protects rather than what it opens. [docs/KEYS.md](docs/KEYS.md) the key design — read that one before touching a binding. +## Contribution priorities + +Omamail's core product is substantially complete. Prefer fixes and refinements to existing functionality: reproducible bugs, small focused fixes, wording corrections, and performance improvements that preserve the intended design and workflow. New features and email providers are considered cautiously; explain the unmet need, why existing workflows are insufficient, and the ongoing interface and maintenance cost before expanding scope. + +Discuss large features, new providers, and substantial architectural changes with Jason before implementation. Passing tests does not establish that a capability belongs in the product. + ## Project boundary Omamail has two hosts over one shared mail and calendar implementation: a [Quickshell plugin hosted by `omarchy-shell`][omarchy-shell], and the Qt host under `app/`. A shared behavior belongs under `ui/` or in the Rust backend; `ui/Service.qml`, `ui/BarWidget.qml`, and `ui/App.qml` remain the Omarchy entry points, while `app/qml/Main.qml` supplies the standalone composition and adapters. @@ -110,8 +116,9 @@ code once and watch it fail, and say so in the pull request. *Popups* sections are the same rules where they are specific to this codebase, and `tests/test_source.sh` enforces the part of them that grep can. - **Test it on a screen, by hand. This is required and nothing substitutes for it.** For the plugin, register your development checkout with the Omarchy shell and follow [the development runtime instructions](docs/BACKEND-RUNTIME.md); click the envelope in the bar and drive the change with the mouse and keyboard in a light theme and a dark one, at both window sizes. For the standalone host, run `make app-run` on each affected platform and repeat the applicable mail, calendar, notification, and window checks. The offscreen QML tests prove focus and routing; they cannot see that a control hangs four pixels out of its row. -- Put a screenshot in the pull request, and say which themes and sizes you - drove it in. +- **Every UI change requires clear, detailed visual evidence.** Attach labeled before-and-after screenshots of every changed view and relevant state, captured at matching window dimensions, theme, and scale. Explain what changed and identify the platforms, themes, and sizes tested. Screenshots must be legible enough to evaluate the actual layout, labels, and states; update them whenever the UI changes. +- **Complex features and workflows also require a detailed screen recording.** Show the complete path from entry to result, including the relevant keyboard and mouse interactions and loading, empty, error, cancellation, or recovery states. Explain the steps and expected behavior so Jason can understand and assess the change without installing or running the app. Screenshots alone are not sufficient for these changes. +- Use synthetic or redacted data in screenshots and recordings. Upload the evidence as PR attachments rather than committing it to the repository. Visual evidence complements the required hands-on testing and regression tests; it does not replace them. ## The pull request @@ -158,8 +165,7 @@ missing: - `make validate`, and that it was green on the commit you are sending. - The new test, and that you saw it fail against the unfixed code. -- What you drove by hand — for an interface change, the themes and sizes, with - a screenshot. +- What you drove by hand — for an interface change, the platforms, themes, and sizes, with the required before-and-after screenshots and, for complex features or workflows, a detailed recording. - That a fresh agent reviewed the diff, and what it found. "Nothing" is an answer; not having asked is not. @@ -172,6 +178,14 @@ document, an internal rename. ## Review +Draft pull requests are not reviewed or approved. The failing-test-only draft described above is a way to preserve a reproducible report; mark a completed change ready for review when it has the required implementation and evidence. + +Review existing-functionality bug fixes, small fixes, wording corrections, and performance improvements first. A reviewer may approve a reasonable, focused, low-risk change that preserves product intent once the current diff and discussion have been checked, the relevant tests and required checks pass on the latest revision, and the affected security boundaries satisfy [AGENTS.md](AGENTS.md). Missing verification must be reported, not inferred from an earlier green commit. + +UI, shortcuts, layout, and interaction changes need a separate product and design judgment. An objectively demonstrable correction to an established product convention, such as restoring the product's established Vim navigation conventions, may be approved after verifying the convention, checking for conflicts and regressions, and reviewing the required tests and visual evidence. Explain the evidence and recommendation. This exception does not cover new product choices: context-menu details or new menu actions, feature placement, subjective taste, and changes with multiple reasonable design tradeoffs require Jason's explicit confirmation before approval. Large features, product-direction changes, and substantial architectural changes also require deep review and product confirmation. When the distinction is unclear, ask rather than treating passing tests as a product decision. + +An approval is not permission to merge: Jason makes the final merge decision, and automated reviewers must never merge. + Answer a review comment by changing the code or by saying why the change would be wrong, and re-run `make validate` after every round — a fix made under review is a change like any other. Both answers are useful; silence and a From 3dd8e2f93d27d434e926bbc78130c312bccab0f7 Mon Sep 17 00:00:00 2001 From: Sergey Armodin Date: Sun, 18 Oct 2026 23:35:11 +0700 Subject: [PATCH 32/88] Fix BackendSync not re-registering a mail watch the backend no longer holds (#374) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stacked on #373 and #372; please review only the last commit. I will rebase as they merge so the squash carries only this change. #372 named this as a follow-up. `BackendSync.check()` discarded the `mail.check` reply. When the backend answered `mail_watch_unknown`, the UI still believed it was registered, and since `reconcile()` returns early for a matching key nothing ever sent `mail.watch` again: automatic checks and the unread badge stopped for that account until its settings changed, it was disabled, or the backend restarted. The backend removes a watch only on unwatch, replacement or shutdown, and a restart always passes through `ready` going false, so the trigger has to be an unwatch the delegate did not send. There is one. `Service.qml`'s `Instantiator` binds delegates to `accounts[index]`, so removing an account that is not last moves ids between delegates: with [A, B, C] → [B, C], delegate 0 moves A→B and delegate 1 moves B→C. If delegate 1 still had a watch for B in flight — ordinary at startup, during a retry, or after an interval change — #373 defers its unwatch for B until that watch answers, and that unwatch can then land after delegate 0 has registered B. Before #373 the immediate unwatch usually won that race but could orphan the watch instead. #373's guard is per `BackendSync`, so it cannot coordinate two delegates; the real fix belongs in Rust (list accounts under the operations lock, or a client generation on watch/unwatch), as noted there. Until then this self-heals: on `mail_watch_unknown` for the current epoch `BackendSync` clears its registration and reconciles, which sends a single `mail.watch` with no unwatch because the account is unchanged. A reply that arrives after the registration moved on is ignored, and other errors are still ignored. Recovery happens on the next `refresh()` (wake, ready, sign-in or a mail action), since that is what calls `check()`. It uses an error code that has existed since the first API revision, so no API revision or fixtures are involved. ## Verification `make validate` green on this commit. `ui/tests/qml/tst_backend_sync.qml` lets the mock answer `mail.check` with an error or hold its reply back, and adds three tests: a lost watch registers again, other check errors keep the registration, and a lost-watch reply for a watch that has since been replaced is ignored. Against #373's `BackendSync.qml` the first fails (the registration is kept); with the change 20/20 pass. Mutations removing the epoch guard, the error filter, the registration reset or the scheduled reconcile are each caught. Nothing visible changes in the interface beyond the badge recovering, so there are no screenshots, and I did not drive the account-removal race by hand on screen. Three fresh reviews of the diff (protocol correctness, test quality with mutation runs, and a maintainer/security pass against AGENTS.md) approved; the correctness and maintainer reviews traced the delegate-shift trigger above, and the test review's findings — a stray reconcile timer masking a mutant and the untested epoch guard — are applied. Security: PASS; it only re-registers the delegate's own account. ## Release Notes - Automatic mail checks and the unread count resume for an account whose background check was lost, for example after removing another account. --- ui/account/BackendSync.qml | 60 ++++++-- ui/tests/qml/tst_backend_sync.qml | 219 +++++++++++++++++++++++++++++- 2 files changed, 267 insertions(+), 12 deletions(-) diff --git a/ui/account/BackendSync.qml b/ui/account/BackendSync.qml index 4bbdc3b..5c24654 100644 --- a/ui/account/BackendSync.qml +++ b/ui/account/BackendSync.qml @@ -13,7 +13,14 @@ Item { property var registeredBackend: null property string registeredAccount: "" property string registrationKey: "" + // A check sent before mail.watch answers may fail with mail_watch_unknown and + // is not needed, as a new watch checks at once. + property bool registered: false property bool unregistering: false + // One registration request at a time: Rust lists accounts before it takes + // its operation lock, so a later watch or unwatch can overtake this one. An + // unwatch requested meanwhile is stored here and sent from the watch's reply. + property var watchRequest: null property int epoch: 0 property real sequence: 0 signal updated(var snapshot) @@ -30,23 +37,31 @@ Item { registeredBackend = null registeredAccount = "" registrationKey = "" + registered = false sequence = 0 epoch++ if (oldBackend && oldBackend.ready && oldAccount) { unregistering = true - oldBackend.call("mail.unwatch", { accountId: oldAccount }, function() { - if (!root) return - root.unregistering = false - root.scheduleReconcile() - }) + // The pending watch sends it on reply, even after this item is destroyed. + if (watchRequest && watchRequest.backend === oldBackend) watchRequest.unwatch = oldAccount + else oldBackend.call("mail.unwatch", { accountId: oldAccount }, unwatched) } } + function unwatched() { + if (!root) return + root.unregistering = false + root.scheduleReconcile() + } + + function wantedKey() { return JSON.stringify([accountId, query, intervalSec, pageSize]) } + function reconcile() { if (unregistering) return if (!enabled || !backend || !backend.ready || !accountId) { stop(); return } - var key = JSON.stringify([accountId, query, intervalSec, pageSize]) + var key = wantedKey() if (registeredBackend === backend && registrationKey === key) return + if (watchRequest && watchRequest.backend === backend) return retryTimer.stop() // Updating an existing watch is atomic in Rust; an unwatch racing a new // watch would otherwise remove the replacement. @@ -55,15 +70,31 @@ Item { registeredBackend = backend registeredAccount = accountId registrationKey = key + registered = false var mine = ++epoch + var request = { backend: backend, unwatch: "" } + // Captured so a deferred unwatch can still finish after destruction. + var onUnwatched = root.unwatched + watchRequest = request backend.call("mail.watch", { accountId: accountId, query: query, intervalSec: intervalSec, pageSize: pageSize }, function(result, error) { - if (mine !== root.epoch) return + if (root && root.watchRequest === request) root.watchRequest = null + if (request.unwatch) { + request.backend.call("mail.unwatch", { accountId: request.unwatch }, onUnwatched) + return + } + if (!root) return + if (mine !== root.epoch) { root.scheduleReconcile(); return } + // Settings changed while this watch was pending: register those instead. + var current = root.wantedKey() === key if (error) { root.registrationKey = "" - if (root.enabled && root.backend && root.backend.ready) retryTimer.restart() + if (!current) root.scheduleReconcile() + else if (root.enabled && root.backend && root.backend.ready) retryTimer.restart() return } - root.accept(result) + root.registered = true + if (current) root.accept(result) + else root.scheduleReconcile() }) } @@ -76,8 +107,15 @@ Item { function check() { reconcile() - if (!registeredBackend || !registeredBackend.ready || !registrationKey) return - registeredBackend.call("mail.check", { accountId: registeredAccount }, function() {}) + if (!registeredBackend || !registeredBackend.ready || !registered) return + var mine = epoch + registeredBackend.call("mail.check", { accountId: registeredAccount }, function(result, error) { + if (!root || mine !== root.epoch || !error || error.message !== "mail_watch_unknown") return + // The backend no longer holds this watch, so polling stopped: register again. + root.registrationKey = "" + root.registered = false + root.scheduleReconcile() + }) } onEnabledChanged: scheduleReconcile() diff --git a/ui/tests/qml/tst_backend_sync.qml b/ui/tests/qml/tst_backend_sync.qml index d4309dd..a608853 100644 --- a/ui/tests/qml/tst_backend_sync.qml +++ b/ui/tests/qml/tst_backend_sync.qml @@ -9,10 +9,19 @@ Item { property var calls: [] property var pendingStop: null property bool failWatch: false + property bool deferWatch: false + property string checkError: "" + property bool deferCheck: false + property var pendingChecks: [] + property var pendingWatches: [] signal notification(string method, var params) function call(method, params, callback) { - calls = calls.concat([{method:method, params:params}]) + calls = calls.concat([ready ? {method:method, params:params} : {method:method, params:params, refused:true}]) + if (!ready) { callback(null, {message:"Backend unavailable"}); return } if (method === "mail.unwatch") pendingStop = callback + else if (method === "mail.watch" && deferWatch) pendingWatches = pendingWatches.concat([callback]) + else if (method === "mail.check" && deferCheck) pendingChecks = pendingChecks.concat([callback]) + else if (method === "mail.check" && checkError) callback(null, {message:checkError}) else callback(null, method === "mail.watch" && failWatch ? {message:"temporary"} : null) } } @@ -22,6 +31,10 @@ Item { accountId: "a@example.org" query: "is:unread" } + Component { + id: syncComponent + Accounts.BackendSync { accountId: "b@example.org"; query: "is:unread" } + } SignalSpy { id: updates; target: sync; signalName: "updated" } TestCase { name: "BackendSync" @@ -29,10 +42,21 @@ Item { function init() { sync.enabled = false sync.reconcile() + var waiting = backend.pendingWatches + backend.pendingWatches = [] + // A test that failed early leaves its watch unanswered; settle it here + // without charging a throw to the next test. + for (var i = 0; i < waiting.length; i++) { + try { if (waiting[i]) waiting[i](null, {message:"reset"}) } catch (failure) {} + } if (backend.pendingStop) { var done = backend.pendingStop; backend.pendingStop = null; done() } backend.ready = true backend.calls = [] backend.failWatch = false + backend.deferWatch = false + backend.checkError = "" + backend.deferCheck = false + backend.pendingChecks = [] sync.accountId = "a@example.org" sync.intervalSec = 120 sync.pageSize = 25 @@ -96,5 +120,198 @@ Item { compare(backend.calls[backend.calls.length - 1].method, "mail.check") verify(sync.registrationKey !== "") } + function answerWatch(index, error) { + var answer = backend.pendingWatches[index] + backend.pendingWatches[index] = null + answer(error ? null : {accountId:"a@example.org",sequence:index + 1,estimate:0,messages:[],error:""}, error || null) + } + function test_check_waits_for_the_watch_to_answer() { + backend.deferWatch = true + sync.enabled = true + sync.check() + compare(backend.calls.length, 1) + compare(backend.calls[0].method, "mail.watch") + compare(backend.pendingWatches.length, 1) + answerWatch(0) + sync.check() + compare(backend.calls.length, 2) + compare(backend.calls[1].method, "mail.check") + } + function test_check_waits_for_a_reconnected_backend_to_register() { + sync.enabled = true + sync.reconcile() + backend.deferWatch = true + backend.ready = false + backend.ready = true + var count = backend.calls.length + compare(backend.calls[count - 1].method, "mail.watch") + sync.check() + compare(backend.calls.length, count) + answerWatch(0) + sync.check() + compare(backend.calls.length, count + 1) + compare(backend.calls[count].method, "mail.check") + } + function test_settings_changed_during_a_watch_wait_for_its_answer() { + backend.deferWatch = true + sync.enabled = true + sync.reconcile() + sync.pageSize = 50 + sync.reconcile() + sync.pageSize = 25 + sync.reconcile() + sync.pageSize = 50 + sync.reconcile() + compare(backend.calls.length, 1) + answerWatch(0) + var count = backend.calls.length + sync.check() + compare(backend.calls.length, count + 1) + compare(backend.calls[count].method, "mail.watch") + compare(backend.calls[count].params.pageSize, 50) + answerWatch(1) + sync.check() + compare(backend.calls.length, count + 2) + compare(backend.calls[count + 1].method, "mail.check") + } + function test_settings_changed_during_a_watch_apply_after_it() { + backend.deferWatch = true + sync.enabled = true + sync.reconcile() + sync.pageSize = 50 + wait(0) + compare(backend.calls.length, 1) + answerWatch(0) + compare(updates.count, 0) + tryVerify(function() { return backend.pendingWatches.length === 2 }) + compare(backend.calls[1].params.pageSize, 50) + } + function test_failed_watch_for_changed_settings_registers_them_at_once() { + backend.deferWatch = true + sync.enabled = true + sync.reconcile() + sync.pageSize = 50 + wait(0) + answerWatch(0, {message:"temporary"}) + tryVerify(function() { return backend.pendingWatches.length === 2 }, 1000) + compare(backend.calls[1].params.pageSize, 50) + } + function test_reconnect_during_a_watch_ignores_its_answer() { + backend.deferWatch = true + sync.enabled = true + sync.reconcile() + backend.ready = false + backend.ready = true + wait(0) + compare(backend.calls.length, 1) + answerWatch(0) + compare(updates.count, 0) + tryVerify(function() { return backend.pendingWatches.length === 2 }) + sync.check() + compare(backend.calls[backend.calls.length - 1].method, "mail.watch") + } + function test_backend_failure_releases_a_deferred_unwatch() { + backend.deferWatch = true + sync.enabled = true + sync.reconcile() + sync.enabled = false + sync.reconcile() + verify(sync.unregistering) + backend.ready = false + answerWatch(0, {message:"Backend unavailable"}) + verify(!sync.unregistering) + compare(backend.calls.length, 2) + compare(backend.calls[1].method, "mail.unwatch") + verify(backend.calls[1].refused) + backend.ready = true + sync.enabled = true + tryVerify(function() { return backend.pendingWatches.length === 2 }) + } + function test_disabling_during_a_watch_unwatches_after_its_answer() { + backend.deferWatch = true + sync.enabled = true + sync.reconcile() + sync.enabled = false + sync.reconcile() + compare(backend.calls.length, 1) + answerWatch(0) + compare(backend.calls.length, 2) + compare(backend.calls[1].method, "mail.unwatch") + compare(backend.calls[1].params.accountId, "a@example.org") + sync.enabled = true + sync.reconcile() + compare(backend.calls.length, 2) + var done = backend.pendingStop + backend.pendingStop = null + done() + tryVerify(function() { return backend.pendingWatches.length === 2 }) + } + function test_destroyed_during_a_watch_still_unwatches() { + backend.deferWatch = true + var item = syncComponent.createObject(null, {backend: backend, enabled: true}) + item.reconcile() + compare(backend.calls.length, 1) + item.destroy() + wait(0) + compare(backend.calls.length, 1) + answerWatch(0) + compare(backend.calls.length, 2) + compare(backend.calls[1].method, "mail.unwatch") + compare(backend.calls[1].params.accountId, "b@example.org") + var done = backend.pendingStop + backend.pendingStop = null + done() + } + function test_failed_pending_watch_registers_again_instead_of_checking() { + backend.deferWatch = true + sync.enabled = true + sync.reconcile() + answerWatch(0, {message:"temporary"}) + var count = backend.calls.length + sync.check() + compare(backend.calls.length, count + 1) + compare(backend.calls[count].method, "mail.watch") + } + function test_check_for_a_lost_watch_registers_again() { + sync.enabled = true + sync.reconcile() + wait(10) + backend.checkError = "mail_watch_unknown" + sync.check() + var count = backend.calls.length + compare(backend.calls[count - 1].method, "mail.check") + verify(!sync.registered) + tryVerify(function() { return backend.calls.length === count + 1 }, 1000) + compare(backend.calls[count].method, "mail.watch") + verify(sync.registered) + } + function test_other_check_errors_keep_the_watch() { + sync.enabled = true + sync.reconcile() + wait(10) + backend.checkError = "mail_network_failed" + sync.check() + var count = backend.calls.length + wait(10) + compare(backend.calls.length, count) + verify(sync.registered) + } + function test_lost_watch_reply_for_a_replaced_watch_is_ignored() { + sync.enabled = true + sync.reconcile() + wait(10) + backend.deferCheck = true + sync.check() + compare(backend.pendingChecks.length, 1) + sync.intervalSec = 60 + sync.reconcile() + var count = backend.calls.length + compare(backend.calls[count - 1].method, "mail.watch") + verify(sync.registered) + backend.pendingChecks[0](null, {message:"mail_watch_unknown"}) + wait(10) + compare(backend.calls.length, count) + verify(sync.registered) + } } } From bb2ed8414c193659582f86715504917ebc16fece Mon Sep 17 00:00:00 2001 From: Lesani <5182104+Lesani@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:35:25 +0200 Subject: [PATCH 33/88] Hide the calendar entry while no calendar is connected (#375) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What changes and who it helps A mailbox with no calendar behind it, such as plain IMAP without CalDAV and with nothing added under Settings → Calendars, still shows **Calendar** in the sidebar, in the window menu and under Alt+C / Ctrl+Shift+C. It opens an empty month that cannot hold an event, because every event write needs a source. People who keep their calendar elsewhere get a permanent dead entry and no way to turn it off. The window now offers the calendar only while at least one source exists across all accounts: a Google or Microsoft mailbox's own calendar, a discovered iCloud calendar, or a CalDAV calendar added in Settings. Nothing is removed for anyone who has one. - **Sidebar, window menu, both shortcuts:** hidden or inert while there is no source. A calendar opened from a bar notification is gated the same way. - **Adding one is unchanged:** the Settings form still adds a CalDAV calendar, and the entry appears as soon as one exists. - **Losing the last source:** a calendar that is open when its last source goes away returns to the mail list. - **Disabled calendars still count,** because the calendar is where they are turned back on. - **No flicker on start:** a source list that has not been read yet counts as having a source, so the entry never disappears and comes back while the file loads. No setting was added. The rule is one pure function, `Sources.hasNone(loaded, list)`, exposed as `noCalendars` on the calendar controller and the service, and read by `App.qml`'s new `calendarAvailable`. ### Reproduction 1. Configure only an IMAP mailbox with no CalDAV calendar (All-Inkl, for example). 2. Open Omamail. **Calendar** sits at the foot of the sidebar, and opening it shows an empty month with nothing to create an event in. ## Screenshots The sidebar is rendered offscreen with synthetic labels at matching size, scale and colours. The left image uses unmodified `main`. | Before (`main`) | After, calendar connected | After, none connected | | --- | --- | --- | | ![before](https://raw.githubusercontent.com/Lesani/omamail/pr-assets-hide-calendar/before.png) | ![after, connected](https://raw.githubusercontent.com/Lesani/omamail/pr-assets-hide-calendar/after-connected.png) | ![after, none](https://raw.githubusercontent.com/Lesani/omamail/pr-assets-hide-calendar/after-none.png) | There's no screenshot of the window menu: its Calendar row hides the same way the existing Quit and web-inbox rows do, and `tst_app_navigation` asserts that. ## Verification - `ui/tests/test_calendar_sources.js`: `hasNone` covers an empty list, an unread list, `null`, a disabled source and a signed-in Gmail account. - `tst_app_navigation`: an open calendar that loses its last source returns to the list. Neither shortcut opens it, `showCalendar()` refuses, the menu row is hidden, and connecting a source brings it back. - `tst_sidebar_fold`: the sidebar entry follows `calendarAvailable`. - All three new tests fail on `main` and pass here. - `tst_standalone_composition`'s calendar close-chord test now connects a source before it opens the calendar. - `make validate` passes locally (Qt 6.11.2, Arch, Omarchy 4.0.4), including `omarchy plugin validate .`. The one exception is `tests/test_clipboard.py`, which needs a live Wayland clipboard and fails the same way on unmodified `main` here. **Security: PASS.** This only changes which entry points are shown. No network request, credential, subprocess or persisted setting is touched. ## Release Notes - The Calendar entry, menu row and shortcuts are hidden while no calendar is connected. Add one in Settings to bring them back. 🤖 Greetings from [Claude](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- app/tests/qml/tst_standalone_composition.qml | 6 +++- ui/App.qml | 28 +++++++++---------- ui/Service.qml | 1 + ui/calendar/CalendarController.qml | 3 ++ ui/calendar/Sources.js | 10 +++++++ ui/components/AppMenu.qml | 3 ++ ui/components/MailboxSidebar.qml | 2 ++ ui/tests/qml/tst_app_navigation.qml | 29 ++++++++++++++++++++ ui/tests/qml/tst_sidebar_fold.qml | 8 ++++++ ui/tests/test_calendar_sources.js | 19 +++++++++++++ 10 files changed, 94 insertions(+), 15 deletions(-) diff --git a/app/tests/qml/tst_standalone_composition.qml b/app/tests/qml/tst_standalone_composition.qml index a8d4ce2..5551765 100644 --- a/app/tests/qml/tst_standalone_composition.qml +++ b/app/tests/qml/tst_standalone_composition.qml @@ -501,7 +501,11 @@ TestCase { var scope = focusScopeOf(composition) verify(scope) scope.enabled = true - composition.app.showCalendar() + // The window offers a calendar only once one is connected. + composition.app.service.calendarController.sourceList = ({ version: 1, sources: [{ + id: "caldav:calendar-example", kind: "caldav", name: "Example", + url: "https://calendar.example/", username: "me", enabled: true }] }) + verify(composition.app.showCalendar()) var router = keyRouter(composition) verify(router) router.context = "calendar" diff --git a/ui/App.qml b/ui/App.qml index dadb1fa..0daa95c 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -310,6 +310,10 @@ Item { readonly property string currentView: page === "reader" ? "reader" : ((page === "calendar" || page === "calendarDetail") ? "calendar" : "list") readonly property bool calendarVisible: currentView === "calendar" + // With no calendar connected anywhere the view cannot hold an event, so the + // window stops offering it; Settings still adds one. + readonly property bool calendarAvailable: !(service && service.noCalendars === true) + onCalendarAvailableChanged: if (!calendarAvailable && calendarVisible) backToList() readonly property bool showSettings: page === "settings" readonly property bool showPicker: page === "picker" readonly property bool showSetup: page === "setup" @@ -491,8 +495,7 @@ Item { if (payload.messageId) Qt.callLater(function() { root.openMessage(String(payload.messageId)) }) - if (payload.view === "calendar") { - showCalendar() + if (payload.view === "calendar" && showCalendar()) { Qt.callLater(function() { calendarView.showEvent(String(payload.eventId || ""), Number(payload.eventStart || 0)) }) @@ -611,8 +614,10 @@ Item { } function showCalendar() { + if (!calendarAvailable) return false navUntouched = false nav = Nav.replaceRoot(nav, "calendar") + return true } // Moving the cursor has to bring the row with it. The list is a Column in a @@ -1066,16 +1071,12 @@ Item { if (id === "switchAccount") return accountSwitcher.openCentered() if (id === "calendar") { if (calendarVisible) backToList() - else { - showCalendar() - calendarView.refresh() - } + else if (showCalendar()) calendarView.refresh() return } if (id === "mailView") return backToList() if (id === "calendarView") { - showCalendar() - calendarView.refresh() + if (showCalendar()) calendarView.refresh() return } if (id === "toggleSidebar") return toggleSidebar() @@ -1811,6 +1812,7 @@ Item { visible: !root.compact && !root.showPage && !root.composing collapsed: root.sidebarCollapsed calendarSelected: root.calendarVisible + calendarAvailable: root.calendarAvailable menuLabelPath: labelMenu.opened && !!root.service && labelMenu.accountId === root.service.activeAccountId ? labelMenu.labelPath : "" service: root.service @@ -1822,8 +1824,7 @@ Item { numbersVisible: focusScope.ctrlHeld onMailboxSelected: function(key) { root.goMailbox(key) } onCalendarRequested: { - root.showCalendar() - calendarView.refresh() + if (root.showCalendar()) calendarView.refresh() } // Not a search: the provider decides what selecting a label means, // and on IMAP it is a folder rather than a term to look for. @@ -2364,8 +2365,7 @@ Item { Qt.callLater(function() { if (root.service) root.service.signIn() }) } onOpenCalendarRequested: { - root.showCalendar() - calendarView.refresh() + if (root.showCalendar()) calendarView.refresh() } // Which kind first, then the form for it. onAddRequested: root.addMailbox() @@ -2669,6 +2669,7 @@ Item { id: appMenu objectName: "app-menu" anchors.fill: parent + calendarAvailable: root.calendarAvailable textColor: root.foreground popupBackgroundColor: root.popupBackground popupBorderColor: root.popupBorder @@ -2685,8 +2686,7 @@ Item { if (root.service) root.service.selectMailbox("inbox") } onCalendarRequested: { - root.showCalendar() - calendarView.refresh() + if (root.showCalendar()) calendarView.refresh() } onSetupRequested: root.openSettings() onSwitchAccountRequested: accountSwitcher.openCentered() diff --git a/ui/Service.qml b/ui/Service.qml index c3ded4f..0e446f0 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -1670,6 +1670,7 @@ Item { } readonly property alias calendarController: sharedCalendar + readonly property bool noCalendars: sharedCalendar.noCalendars readonly property var barEvents: Preview.upcomingEvents( barCalendar.events, Date.now(), 2) diff --git a/ui/calendar/CalendarController.qml b/ui/calendar/CalendarController.qml index 8c69ca8..68ecd47 100644 --- a/ui/calendar/CalendarController.qml +++ b/ui/calendar/CalendarController.qml @@ -74,6 +74,9 @@ Item { property bool eventWriting: false readonly property var availableSources: Sources.withMicrosoftAccounts(Sources.withGoogleAccounts( sourceList, service ? service.accountSummaries : []), service ? service.accountSummaries : []) + // Every account's calendars, not just this mailbox's: the entry hides only + // when no account has one and nothing was added in Settings. + readonly property bool noCalendars: Sources.hasNone(sourcesLoaded, availableSources) readonly property bool unifiedCalendarView: !!service && service.unifiedCalendarView === true readonly property var contextSources: unifiedCalendarView diff --git a/ui/calendar/Sources.js b/ui/calendar/Sources.js index 2039cd1..7c5f2ef 100644 --- a/ui/calendar/Sources.js +++ b/ui/calendar/Sources.js @@ -364,6 +364,16 @@ function forAccount(list, accountId) { return next } +// Whether the window should offer a calendar at all. Nothing connected means +// the view is an empty grid that cannot hold an event, since every write needs +// a source. A list that has not been read yet still counts as having one: the +// entry must not vanish and reappear while the file loads. A disabled source +// still counts, because the calendar is where it is turned back on. +function hasNone(loaded, list) { + return loaded === true + && !(list && Array.isArray(list.sources) && list.sources.length > 0) +} + function providerLabel(kind) { var value = trimmed(kind).toLowerCase() if (value === "google" || value === "gmail") return "Google" diff --git a/ui/components/AppMenu.qml b/ui/components/AppMenu.qml index ad00bc8..7244cb9 100644 --- a/ui/components/AppMenu.qml +++ b/ui/components/AppMenu.qml @@ -26,6 +26,7 @@ Item { // Only the standalone host owns the application process. A plugin menu // must never offer to terminate the shell that loaded it. property bool canQuit: false + property bool calendarAvailable: true readonly property bool opened: menu.opened // Positioned against the window rather than a button, and flipped when it @@ -146,7 +147,9 @@ Item { } MenuRow { id: calendarRow + objectName: "app-menu-calendar" text: "Calendar" + visible: root.calendarAvailable onActivated: { menu.close(); root.calendarRequested() } } MenuSeparatorLine { diff --git a/ui/components/MailboxSidebar.qml b/ui/components/MailboxSidebar.qml index 608ca17..a362603 100644 --- a/ui/components/MailboxSidebar.qml +++ b/ui/components/MailboxSidebar.qml @@ -21,6 +21,7 @@ Item { required property string panelFontFamily property bool collapsed: false property bool calendarSelected: false + property bool calendarAvailable: true property string menuLabelPath: "" signal mailboxSelected(string key) @@ -166,6 +167,7 @@ Item { x: Style.space(6) label: "Calendar" icon: "calendar" + visible: root.calendarAvailable selected: root.calendarSelected onActivated: root.calendarRequested() } diff --git a/ui/tests/qml/tst_app_navigation.qml b/ui/tests/qml/tst_app_navigation.qml index be10306..2d81038 100644 --- a/ui/tests/qml/tst_app_navigation.qml +++ b/ui/tests/qml/tst_app_navigation.qml @@ -107,6 +107,7 @@ Item { property var sendAsAliases: [] property var sendIdentities: [] property var calendarController: null + property bool noCalendars: false property var selectedBody: ({ text: "Original body", source: "plain" }) property var selectedMessage: null property var unavailableActions: [] @@ -268,6 +269,7 @@ Item { mailService.selectedId = "" mailService.selectedMessage = null mailService.unavailableActions = [] + mailService.noCalendars = false mailService.selectedBody = ({ text: "Original body", source: "plain" }) fakeAuth.credentialsPresent = false fakeShell.hidden = [] @@ -481,6 +483,33 @@ Item { compare(kinds(), "list,reader", "the key toggles") } + function test_no_connected_calendar_takes_the_calendar_out_of_the_window() { + app.runShortcut("calendarView", "") + compare(kinds(), "calendar") + mailService.noCalendars = true + compare(app.calendarAvailable, false) + compare(kinds(), "list", "a calendar that loses its last source closes") + + app.runShortcut("calendarView", "") + app.runShortcut("calendar", "") + compare(kinds(), "list", "neither shortcut opens an empty calendar") + compare(app.showCalendar(), false) + + var menu = named(app, "app-menu") + menu.openAt(40, 40) + wait(20) + var row = null + for (var i = 0; i < menu.menuRows.length; i++) + if (menu.menuRows[i].objectName === "app-menu-calendar") row = menu.menuRows[i] + verify(row, "the menu has a calendar row") + compare(row.visible, false, "and does not offer it") + menu.close() + + mailService.noCalendars = false + app.runShortcut("calendarView", "") + compare(kinds(), "calendar", "connecting one brings it back") + } + function test_the_calendar_is_the_other_root_and_back_on_a_root_closes() { app.runShortcut("calendarView", "") compare(kinds(), "calendar") diff --git a/ui/tests/qml/tst_sidebar_fold.qml b/ui/tests/qml/tst_sidebar_fold.qml index 3312f90..15c4bb1 100644 --- a/ui/tests/qml/tst_sidebar_fold.qml +++ b/ui/tests/qml/tst_sidebar_fold.qml @@ -64,6 +64,14 @@ Item { sidebar.collapsed = false } + function test_the_calendar_entry_follows_whether_one_is_connected() { + verify(textItem(sidebar, "Calendar").visible, "shown by default") + sidebar.calendarAvailable = false + var hidden = textItem(sidebar, "Calendar") + verify(hidden === null || !hidden.visible, "hidden with nothing connected") + sidebar.calendarAvailable = true + } + function test_the_chevron_folds_without_opening() { var folds = named(sidebar, "fold", []) compare(folds.length, 1, "only the parent carries a chevron") diff --git a/ui/tests/test_calendar_sources.js b/ui/tests/test_calendar_sources.js index 88860d3..17c9a2a 100644 --- a/ui/tests/test_calendar_sources.js +++ b/ui/tests/test_calendar_sources.js @@ -359,3 +359,22 @@ assert.strictEqual(namedMicrosoft.sources[0].name, "Work appointments", assert.strictEqual(sources.errorLabel({ id: "caldav:team", kind: "caldav" }, accounts), "caldav:team") assert.strictEqual(sources.errorLabel(null, accounts), "Calendar") } + +// The window hides its calendar only once the list is known to be empty. +{ + assert.strictEqual(sources.hasNone(true, sources.emptyList()), true, + "nothing connected anywhere hides the calendar") + assert.strictEqual(sources.hasNone(false, sources.emptyList()), false, + "an unread list keeps the entry rather than flashing it away") + assert.strictEqual(sources.hasNone(true, null), true) + const disabled = sources.add(sources.emptyList(), { + id: "caldav:off", kind: "caldav", name: "Off", + url: "https://calendar.example/off/", username: "me", enabled: false + }) + assert.strictEqual(sources.hasNone(true, disabled), false, + "a disabled calendar is still turned back on from the calendar") + const google = sources.withGoogleAccounts(sources.emptyList(), + [{ id: "one@gmail.com", email: "one@gmail.com", provider: "gmail", signedIn: true }]) + assert.strictEqual(sources.hasNone(true, google), false, + "a Google mailbox brings its own calendar") +} From 027efb6bafd4281d4c5a4bd710b17f37aa177244 Mon Sep 17 00:00:00 2001 From: Oliver Lukschander Date: Mon, 19 Oct 2026 03:58:22 +0200 Subject: [PATCH 34/88] Add Restore in Trash (menu, buttons, d) and Move to Inbox in Archive (buttons, e) (#359) --- docs/KEYS.md | 6 +- ui/App.qml | 18 +- ui/account/Model.js | 98 +++ ui/components/MessageList.qml | 64 +- ui/components/MessageMenu.qml | 72 ++- ui/components/MessageReader.qml | 24 +- ui/components/MessageRow.qml | 20 +- ui/components/ReaderBlankSlate.qml | 30 +- ui/keys/Keymap.js | 22 +- ui/tests/qml/tst_message_menu_inbox.qml | 124 ++++ ui/tests/qml/tst_trash_restore.qml | 786 ++++++++++++++++++++++++ ui/tests/test_keymap.js | 14 + ui/tests/test_model.js | 100 +++ 13 files changed, 1316 insertions(+), 62 deletions(-) create mode 100644 ui/tests/qml/tst_trash_restore.qml diff --git a/docs/KEYS.md b/docs/KEYS.md index f771a10..16da26d 100644 --- a/docs/KEYS.md +++ b/docs/KEYS.md @@ -118,8 +118,8 @@ used to exist, and they had. | `backToList` | `u`, `Left` | reader | Back to the list | | `nextMember` | `n` | reader | Next message in the conversation | | `previousMember` | `p` | reader | Previous message in the conversation | -| `archive` | `e` | mail | Archive | -| `trash` | `d` | mail | Move to trash | +| `archive` | `e` | mail | Archive (in Archive: move to Inbox) | +| `trash` | `d` | mail | Move to trash (in the trash: restore) | | `star` | `s` | mail | Star or unstar | | `moveToLabel` | `v` | mail | Move to | | `markRead` | `Shift+I` | mail | Mark read | @@ -177,6 +177,8 @@ In Drafts, `Enter`, `o` and `c` open the selected draft in the composer, with wh `Space` or `x` toggles the cursor row's selection in the list or reader context. Shift+click applies the clicked row's next checked state to the inclusive range from the cursor: an unchecked endpoint selects the range, and a checked endpoint clears it. Selections outside the range remain unchanged; the cursor then moves to the clicked row. +`e` and `d` are the archive and trash buttons, and where a button's action is already done for every row it will reach, the button is its inverse, and so is the key: in the trash they restore, and in Archive they move an archived message to the Inbox. The rows are the ones the control acts on: the ticked rows when it is about one of them (a key, a ticked row's menu or middle-click, the reader on a ticked message), else the cursor's row (before there is one, the listing's rows, which is what the hint row and the empty reader's key list describe), the row itself, or the row the open message belongs to. A mixed selection gets the key's own verb, the way `s` stars a mixed one. Restore is the provider's untrash, which every client has, so it is also the way out of the trash on HEY, which has no archive and no move. It is not called Move to Inbox because where it lands is the provider's: Gmail takes the TRASH label off and leaves the rest, the others move the message to the Inbox. `e` turns back only in Archive: on IMAP every folder but the Inbox reads as archived, and `e` in one of them still files the message in Archive. `Model.keyActionFor` is the rule. `MessageList` applies it over each control's rows, with App's own `selectionActive` deciding whether the ticked rows are reached, and App sends the keys and the hint row through its `keyActions`. A button's tooltip, on a row or in the reader, names `e` or `d` only when the key reaches the same rows and asks the same of them. Hovering does not move the cursor, so a row names its keys only when it is the cursor's row, or ticked while the selection is active. A message that is not one of the listing's rows, such as one a notification opened over the Trash, is judged by its own labels. + `n` and `p` walk the conversation rail beside the message on a provider whose listing collapses to conversations, opening the next and previous member in the same reader and stopping at the ends. The rail runs newest at the top, so `n` opens the stop below the open message, which is the older one, and `p` the stop above it, the newer — the keys follow the rail as it is drawn, the way `j` and `k` follow the list. They move the reader and nothing else. `j` and `k` move the list cursor and immediately open the representative of that row, leaving the conversation member selected with `n` or `p`. A message whose conversation has one member draws no rail, and both keys then do nothing. A right-click on a stop opens the same menu a row has — reply, archive, trash, spam, read, star — for that one message: the action reaches the member alone, where the same verb on the row reaches every counted member, and if the open message is the one taken out of the view the reader moves to the stop beside it, the newer one above or else the older below. The delayed-send toast does not create a keyboard context. The current screen keeps its normal keys while the toast is visible. A new draft, reply, or forward can open during the delay. The send button waits for the queued message, but every draft field remains editable. The toast button restores the queued message. `Alt+Z` does the same from every context. `Ctrl+Z` remains text undo while composing or searching. If another compose is open, Omamail saves it to the provider's Drafts storage before dropping its in-memory fallback. A failed save keeps that fallback. diff --git a/ui/App.qml b/ui/App.qml index 0daa95c..28f4cb9 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -1006,8 +1006,7 @@ Item { if (id === "backToList") return backToList() if (id === "nextMember") return stepMember(1) if (id === "previousMember") return stepMember(-1) - if (id === "archive") return actOnCursor("archive") - if (id === "trash") return actOnCursor("trash") + if (id === "archive" || id === "trash") return actOnCursor(list.keyActions[id]) // Through the same guard actOnCursor applies rather than around it: // starring with nothing selected used to call through with an empty id. // @@ -1947,6 +1946,7 @@ Item { panelFontFamily: root.fontFamily cursorId: root.cursorId checkedIds: root.checkedIds + selectionActive: root.selectionActive ctrlHeld: focusScope.ctrlHeld urgentColor: root.urgent onMessageActivated: function(id) { root.openMessage(id) } @@ -1954,9 +1954,9 @@ Item { onRowActionRequested: function(id, action) { root.actFromRow(id, action) } onCheckToggled: function(id) { root.toggleCheck(id) } onCheckRangeRequested: function(id) { root.checkRange(id) } - onMenuRequested: function(id, sceneX, sceneY) { + onMenuRequested: function(id, sceneX, sceneY, ids) { root.cursorId = id - rowMenu.openAt(id, sceneX, sceneY) + rowMenu.openAt(id, sceneX, sceneY, ids) } } } @@ -2034,6 +2034,8 @@ Item { bodyMode: root.bodyMode alwaysRenderHeavyMessages: !!root.service && root.service.alwaysRenderHeavyMessages contentDirection: root.service ? root.service.contentDirection : "" + actions: list.readerActions + keyActions: list.keyActions onBodyModeRequested: function(mode) { if (root.service) root.service.setBodyMode(mode) } @@ -2081,9 +2083,7 @@ Item { root.service.toggleStar(root.service.selectedId) return } - if (((root.service.conversationProjection || ({})).memberIds || []).indexOf(root.service.selectedId) < 0 - || root.cursorId === "") - root.cursorId = root.service.selectedId + root.cursorId = Model.readerTarget(root.service.conversationProjection, root.service.selectedId, root.cursorId) if (action === "moveToLabel") return root.openLabelPicker(outside) root.actOnCursor(action, outside) } @@ -2660,7 +2660,7 @@ Item { panelFontFamily: root.fontFamily hints: Keymap.hintsFor(focusScope.keyContext, root.service ? root.service.unavailableActions : [], - root.cursorId !== "") + root.cursorId !== "", list.keyActions) } } @@ -2981,8 +2981,6 @@ Item { var outside = root.checkedIds.indexOf(id) < 0 root.cursorId = id if (action === "moveToLabel") return root.openLabelPicker(outside) - if ((action === "star" || action === "unstar") && root.selectionActive && !outside) - return root.actOnChecked(Model.starActionFor(Model.summariesById(root.service.messages, root.checkedIds))) root.actOnCursor(action, outside) } // From a stop on the rail. A member is not a row, so the cursor stays diff --git a/ui/account/Model.js b/ui/account/Model.js index 335c28e..11854ac 100644 --- a/ui/account/Model.js +++ b/ui/account/Model.js @@ -538,6 +538,104 @@ function unavailableActions(capabilities) { return out } +// The mailbox the list is showing, or "" while it shows something else: a +// search reaches the whole account and a label view is somewhere else, +// whatever `mailboxKey` still says. `view` is anything with the service's +// `mailboxKey`, `searchQuery` and `rawQuery`. +function plainMailbox(view) { + var on = view || {} + if (String(on.searchQuery || "") !== "" || String(on.rawQuery || "") !== "") return "" + return String(on.mailboxKey || "") +} + +// Whether a message is in the trash. Its own TRASH label says so wherever the +// provider's resource carries one. A HEY posting carries no label for the box +// it was listed from, so the plain Trash listing is the other witness. +function inTrash(summary, view) { + if (!summary) return false + return summary.inTrash === true || plainMailbox(view) === "trash" +} + +// Whether a message is archived: out of the inbox and not somewhere that has +// its own verb. Spam counts as "its own verb" only where a spam verb exists — +// an IMAP Junk folder has no report-spam inverse to offer instead, so a Junk +// message there is the one spam case "Move to Inbox" still means. The verb is +// the owning mailbox's (`view.hostForId`), because a merged list holds both +// kinds, and an owner that cannot be found is not taken to lack one. +function isArchived(summary, view, id) { + if (!summary || summary.inInbox !== false || inTrash(summary, view)) return false + if (summary.isSent === true || summary.isDraft === true) return false + if (summary.inSpam !== true) return true + var host = !view ? null : typeof view.hostForId === "function" ? view.hostForId(id) : view + return !!host && host.canReportSpam === false +} + +// Whether every one of these rows is in the trash, or archived: the menu and +// `keyActionFor` ask over the rows an action will reach, never one of them. +function allInTrash(rows, view) { + var list = Array.isArray(rows) ? rows : [] + for (var i = 0; i < list.length; i++) if (!inTrash(list[i], view)) return false + return list.length > 0 +} + +function allArchived(rows, view) { + var list = Array.isArray(rows) ? rows : [] + for (var i = 0; i < list.length; i++) + if (!isArchived(list[i], view, list[i] ? list[i].id : "")) return false + return list.length > 0 +} + +// What the archive or trash slot asks for on the rows its action will reach. +// Where the slot's action is already done for all of them the slot is the way +// back: `d` restores messages in the trash, `e` moves archived messages in the +// Archive mailbox to the Inbox. Archived alone is not enough for `e`: on IMAP +// every folder but the Inbox reads as archived, and `e` in one of them files +// the message in Archive, which is worth keeping. Anything else is the slot's +// own action, so a mixed selection lands in one place rather than swapping +// every row — the rule `starActionFor` keeps for `s`. +function keyActionFor(action, rows, view) { + var verb = String(action || "") + if (verb === "trash") return allInTrash(rows, view) ? "untrash" : verb + if (verb === "archive" && plainMailbox(view) === "archive" && allArchived(rows, view)) return "unarchive" + return verb +} + +// The view a message that is not one of the listing's rows is judged in: the +// same mailboxes and their spam verbs, and no listing. The listing witnesses +// where its own rows are (`inTrash`, the Archive rule of `keyActionFor`), not +// where a message opened from elsewhere is, such as one a new-mail +// notification opened over the Trash. +function unlistedView(view) { + var on = view || {} + return { mailboxKey: "", searchQuery: "", rawQuery: "", canReportSpam: on.canReportSpam, + hostForId: function(id) { return typeof on.hostForId === "function" ? on.hostForId(id) : on } } +} + +// The row the reader's own buttons act on when the selection does not +// reach the open message: the cursor's row while the open message is a +// member of that row's conversation, else the open message itself. App moves +// the cursor there before acting, and the list draws the buttons from it. +function readerTarget(projection, selectedId, cursorId) { + var members = projection && Array.isArray(projection.memberIds) ? projection.memberIds : [] + var cursor = String(cursorId || "") + return members.indexOf(selectedId) >= 0 && cursor !== "" ? cursor : String(selectedId || "") +} + +// The words for the four verbs, for the menu, the row and the reader, so a +// verb reads the same wherever it is offered. Which verb each control offers +// is its own: the menu offers Move to Inbox for an archived message anywhere, +// the buttons and `e` only in Archive. Untrash is "Restore", not "Move to Inbox": +// Gmail's takes the TRASH label off and leaves the rest, so a sent message +// goes back to Sent, while the other clients move it to the Inbox. +function actionName(action, labelView) { + var verb = String(action || "") + if (verb === "archive") return "Archive" + if (verb === "trash") return "Move to trash" + if (verb === "untrash") return "Restore" + if (verb === "unarchive") return labelView === true ? "Move to Inbox and remove label" : "Move to Inbox" + return "" +} + // A row carrying a block, with its own flags read the way `Message.summarize` // reads them: unread and starred are "this message, *or* any counted member". // A representative that has been read while a reply has not is still an unread diff --git a/ui/components/MessageList.qml b/ui/components/MessageList.qml index d729038..ac22f34 100644 --- a/ui/components/MessageList.qml +++ b/ui/components/MessageList.qml @@ -37,7 +37,55 @@ Column { // A row's own star, archive and trash buttons. Routed up rather than // straight to the service so a ticked row's button means the selection. signal rowActionRequested(string id, string action) - signal menuRequested(string id, real sceneX, real sceneY) + + // `ids` are the rows a menu opened here will act on (`reachedIds`). + signal menuRequested(string id, real sceneX, real sceneY, var ids) + + // App's own answer to whether an action reaches the ticked rows + // (`App.selectionActive`: rows are ticked and the list is on screen). The + // list draws what App will do and does not decide it again. + property bool selectionActive: false + + // What the archive and trash slots ask for, by binding id, decided over the + // rows the action will reach and nothing else (`Model.keyActionFor`), so a + // control says what will happen to them. The rows are the ones App's + // handler for that control sends the action to: the ticked rows when the + // selection is active and the control is about one of them (or is `e`/`d`), + // else the cursor's row for the keys, the row itself for a row's own + // controls, and for the reader the row it was opened from. + readonly property var selectionActions: actionsFor(Model.summariesById( + root.service ? root.service.messages : [], root.checkedIds)) + // With no cursor yet, the next `j` lands on one of this listing's rows, so + // the keys are described by all of them: in the Trash `d` restores before a + // row is chosen (the hint row and the empty reader say so). + readonly property var keyActions: root.selectionActive ? selectionActions + : actionsFor(root.cursorId !== "" ? rowsOf(root.cursorId) : (root.service ? root.service.messages : [])) + // `keys` says whether `e` and `d` reach the same rows as the reader's + // buttons and ask the same of them, so its tooltips name a key only then. + // An open message that is not a listed row (one a notification opened) is + // judged by its own labels: the listing says nothing about where it is. + readonly property var readerActions: { + var open = root.service ? String(root.service.selectedId || "") : "" + if (reaches(open)) return ({ archive: selectionActions.archive, trash: selectionActions.trash, keys: true }) + var target = Model.readerTarget(root.service ? root.service.conversationProjection : null, open, root.cursorId) + var rows = rowsOf(target) + var actions = rows.length > 0 ? actionsFor(rows) + : actionsFor(root.service && root.service.selectedMessage ? [root.service.selectedMessage] : [], + Model.unlistedView(root.service)) + return ({ archive: actions.archive, trash: actions.trash, + keys: !root.selectionActive && target === root.cursorId + && actions.archive === keyActions.archive && actions.trash === keyActions.trash }) + } + + // Whether an action on `id` means the selection: App's `outside` test. + function reaches(id) { return root.selectionActive && root.checkedIds.indexOf(id) >= 0 } + function reachedIds(id) { return reaches(id) ? root.checkedIds.slice() : [id] } + function rowsOf(id) { return Model.summariesById(root.service ? root.service.messages : [], [id]) } + function actionsFor(rows, view) { + var on = view || root.service + return ({ archive: Model.keyActionFor("archive", rows, on), + trash: Model.keyActionFor("trash", rows, on) }) + } width: parent ? parent.width : 0 spacing: Style.space(2) @@ -86,6 +134,14 @@ Column { selectionActive: root.checkedIds.length > 0 ctrlHeld: root.ctrlHeld canArchive: root.service.canArchive + // A ticked row's buttons and middle-click mean the selection. + readonly property var slotActions: root.reaches(modelData.id) + ? root.selectionActions : root.actionsFor([modelData]) + archiveAction: slotActions.archive + trashAction: slotActions.trash + // Hovering does not move the cursor, so `e` and `d` reach this row only + // when it is the cursor's, or ticked while the selection is active. + namesKeys: root.selectionActive ? root.reaches(modelData.id) : root.cursorId === modelData.id conversations: Unified.rowIsConversation(modelData) contentDirection: root.service.contentDirection onActivated: root.messageActivated(modelData.id) @@ -93,10 +149,10 @@ Column { onCheckRangeRequested: root.checkRangeRequested(modelData.id) onAgentRequested: function(sceneX, sceneY) { root.agentRequested(modelData.id, sceneX, sceneY) } onStarToggled: root.rowActionRequested(modelData.id, "star") - onArchiveRequested: root.rowActionRequested(modelData.id, "archive") - onTrashRequested: root.rowActionRequested(modelData.id, "trash") + onArchiveRequested: root.rowActionRequested(modelData.id, archiveAction) + onTrashRequested: root.rowActionRequested(modelData.id, trashAction) onMenuRequested: function(sceneX, sceneY) { - root.menuRequested(modelData.id, sceneX, sceneY) + root.menuRequested(modelData.id, sceneX, sceneY, root.reachedIds(modelData.id)) } } } diff --git a/ui/components/MessageMenu.qml b/ui/components/MessageMenu.qml index 7fdadaa..c7a768f 100644 --- a/ui/components/MessageMenu.qml +++ b/ui/components/MessageMenu.qml @@ -29,24 +29,27 @@ Item { property real anchorY: 0 property int cursorIndex: -1 readonly property var menuRows: [continueRow, replyRow, replyAllRow, forwardRow, archiveRow, - unarchiveRow, moveRow, + unarchiveRow, untrashRow, moveRow, trashRow, spamRow, readRow, starRow, browserRow, aiRow] - // Whether this message is archived — out of the inbox and not somewhere - // that has its own verb. Read off the summary the menu was opened on rather - // than asked of the service, because the menu is about one message. IMAP - // synthesises all four of these from the folder, so one rule serves both - // kinds of provider. Spam counts as "its own verb" only where a spam verb - // exists: an IMAP Junk folder has no report-spam inverse to offer instead, - // so a Junk message there is the one spam case "Move to Inbox" still means. - readonly property var messageHost: !root.service ? null - : typeof root.service.hostForId === "function" - ? root.service.hostForId(root.messageId) : root.service - readonly property bool archived: !!root.summary - && root.summary.inInbox === false - && root.summary.inTrash !== true - && (root.summary.inSpam !== true || (root.messageHost && root.messageHost.canReportSpam === false)) - && root.summary.isSent !== true - && root.summary.isDraft !== true + // The rows a choice here will act on, as App's handler sends it: every + // ticked row when the menu was opened on one of them (the list says which, + // `MessageList.reachedIds`), else the one message. Restore, Move to Inbox + // and Star or Unstar are decided over all of them: Restore only when every + // one is in the trash, Unstar only when every one is starred. Mark read or + // unread follows the row the menu was opened on and reaches them all. Move + // to Inbox is offered for an archived message anywhere (`Model.isArchived`), + // where the row's button offers it only in Archive. IMAP synthesises the + // flags from the folder, so one rule serves both kinds of provider. + property var reachedIds: [] + readonly property var reached: { + if (!root.summary) return [] + if (root.memberOnly || root.reachedIds.length < 2) return [root.summary] + var rows = Model.summariesById(root.service ? root.service.messages : [], root.reachedIds) + return rows.length > 0 ? rows : [root.summary] + } + readonly property bool archived: Model.allArchived(root.reached, root.service) + readonly property bool trashed: Model.allInTrash(root.reached, root.service) + readonly property string starAction: Model.starActionFor(root.reached) // Whether the list it was opened from is a label's, and whether that label // is one a message can be taken out of — which is the same question @@ -81,13 +84,19 @@ Item { anchors.fill: parent z: 50 - function openAt(id, sceneX, sceneY) { + function openAt(id, sceneX, sceneY, ids) { root.memberOnly = false + // Copied by index: through a signal the list arrives as a sequence, not + // a JavaScript array. + var reached = [] + for (var i = 0; ids && i < ids.length; i++) reached.push(String(ids[i])) + root.reachedIds = reached openMenu(id, sceneX, sceneY) } function openForMember(id, sceneX, sceneY) { root.memberOnly = true + root.reachedIds = [] openMenu(id, sceneX, sceneY) } @@ -210,9 +219,21 @@ Item { MenuRow { id: unarchiveRow visible: (!root.service || root.service.canArchive) && root.archived - text: root.inLabelView ? "Move to Inbox and remove label" : "Move to Inbox" + text: Model.actionName("unarchive", root.inLabelView) onActivated: root.run("unarchive") } + // Out of the trash, by the verb every client has for it — which is why + // it needs no capability, and why it is offered on a provider with no + // archive and no move, where it is the only way back. Undoing a trash + // is not adding INBOX: `labelChangesFor` would leave TRASH on the + // message, which is why the row above is not offered here. "Restore" + // because where it lands is the provider's (`Model.actionName`). + MenuRow { + id: untrashRow + visible: root.trashed + text: Model.actionName("untrash") + onActivated: root.run("untrash") + } MenuRow { id: moveRow // A member of a conversation is not a row the picker can move on @@ -221,7 +242,14 @@ Item { text: "Move to..." onActivated: root.run("moveToLabel") } - MenuRow { id: trashRow; text: "Move to trash"; tone: root.urgentColor; onActivated: root.run("trash") } + // Not for a message already there: trashing it again moves nothing. + MenuRow { + id: trashRow + visible: !root.trashed + text: Model.actionName("trash") + tone: root.urgentColor + onActivated: root.run("trash") + } MenuRow { id: spamRow visible: !root.service || root.service.canReportSpam @@ -243,8 +271,8 @@ Item { MenuRow { id: starRow visible: !root.service || root.service.canStar - text: root.summary && root.summary.starred ? "Unstar" : "Star" - onActivated: root.run(root.summary && root.summary.starred ? "unstar" : "star") + text: root.starAction === "unstar" ? "Unstar" : "Star" + onActivated: root.run(root.starAction) } MenuSeparatorLine { diff --git a/ui/components/MessageReader.qml b/ui/components/MessageReader.qml index 2bfd57a..244ea71 100644 --- a/ui/components/MessageReader.qml +++ b/ui/components/MessageReader.qml @@ -6,6 +6,7 @@ import "../message/Direction.js" as Direction import "../message/Html.js" as Html import "../message/Message.js" as Mail import "../message/Mailto.js" as Mailto +import "../account/Model.js" as Model // The right column. The body goes through Qt's own rich text engine — a real // HTML renderer, not a browser — after Html.sanitize has removed what Qt would @@ -125,6 +126,18 @@ Item { readonly property var summary: service ? service.selectedMessage : null readonly property bool isDraft: !!summary && summary.isDraft === true + // What the archive and trash buttons ask for, by binding id. The window + // decides it over the rows the buttons' action will reach, which are not + // always the open message (`MessageList.readerActions`): the selection when + // the message is ticked, the conversation's row when a member is open. + // `keys` false means `e` and `d` reach other rows, so the tooltips leave + // them out rather than name a key that does something else. + property var actions: null + // What `e` and `d` ask for, for the empty reader's key list. + property var keyActions: null + readonly property string archiveAction: actions ? actions.archive : "archive" + readonly property string trashAction: actions ? actions.trash : "trash" + function keyHint(key) { return actions && actions.keys === false ? "" : " · " + key } // The id the service answers to, which is not always the one on the summary. // A list made of several mailboxes addresses a row by mailbox and id, and the @@ -277,6 +290,7 @@ Item { dimColor: root.dimColor dimmerColor: root.dimmerColor panelFontFamily: root.panelFontFamily + keyActions: root.keyActions } // Nothing known at all, which after the reader learned to open on the list's @@ -973,9 +987,10 @@ Item { x: actionGap.x + actionGap.width + messageActions.gap y: Math.round((parent.height - height) / 2) visible: !root.service || root.service.canArchive - iconName: "archive"; tooltipText: "Archive · e" + iconName: root.archiveAction === "unarchive" ? "inbox" : "archive" + tooltipText: Model.actionName(root.archiveAction) + root.keyHint("e") foreground: root.dimColor; hoverColor: root.textColor; fontFamily: root.panelFontFamily - onClicked: root.actionRequested("archive") + onClicked: root.actionRequested(root.archiveAction) } // Archive under a label, or move to a folder: the same picker `v` // opens, one press away in the reader too. @@ -999,9 +1014,10 @@ Item { ? archiveButton.x + archiveButton.width : actionGap.x + actionGap.width)) + messageActions.gap y: Math.round((parent.height - height) / 2) - iconName: "trash"; tooltipText: "Move to trash · d" + iconName: root.trashAction === "untrash" ? "undo" : "trash" + tooltipText: Model.actionName(root.trashAction) + root.keyHint("d") foreground: root.dimColor; hoverColor: root.textColor; fontFamily: root.panelFontFamily - onClicked: root.actionRequested("trash") + onClicked: root.actionRequested(root.trashAction) } } diff --git a/ui/components/MessageRow.qml b/ui/components/MessageRow.qml index 41dd05a..91998c6 100644 --- a/ui/components/MessageRow.qml +++ b/ui/components/MessageRow.qml @@ -24,6 +24,14 @@ Rectangle { ? String(root.summary.sourceLabel) : "" property bool canArchive: true + // What the archive and trash buttons ask for. The list asks + // `Model.keyActionFor`, the rule `e` and `d` follow, so in the trash the + // trash button restores, in Archive the archive button moves the message to + // the Inbox, and the key its tooltip names does the same. + property string archiveAction: "archive" + property string trashAction: "trash" + // Whether `e` and `d` act on this row, so its tooltips name them only then. + property bool namesKeys: true // Whether this row stands for a conversation rather than for one message. // Grouping is a panel rule gated on the provider's `conversations` // capability; a row is told, and asks nobody. @@ -114,8 +122,8 @@ Rectangle { var scene = mapToGlobal(event.x, event.y) root.menuRequested(scene.x, scene.y) } else if (event.button === Qt.MiddleButton) { - // Middle-click archives: the one triage action worth having without - // moving the pointer to a button. + // Middle-click is the archive button, without moving the pointer to + // it: Archive, or in Archive, Move to Inbox (`archiveAction`). root.archiveRequested() } else if (event.modifiers & Qt.ShiftModifier) { // Shift selects or clears the range from the cursor to here; @@ -298,8 +306,8 @@ Rectangle { // that is a move to a folder, and a server without one would have this // quietly do nothing. visible: root.hot && root.canArchive && !root.selectionMode - iconName: "archive" - tooltipText: "Archive · e" + iconName: root.archiveAction === "unarchive" ? "inbox" : "archive" + tooltipText: Model.actionName(root.archiveAction) + (root.namesKeys ? " · e" : "") foreground: root.dimColor hoverColor: root.textColor iconSize: Style.font.iconSmall @@ -311,8 +319,8 @@ Rectangle { IconButton { visible: root.hot && !root.selectionMode objectName: "message-trash" - iconName: "trash" - tooltipText: "Move to trash · d" + iconName: root.trashAction === "untrash" ? "undo" : "trash" + tooltipText: Model.actionName(root.trashAction) + (root.namesKeys ? " · d" : "") foreground: root.dimColor hoverColor: root.textColor iconSize: Style.font.iconSmall diff --git a/ui/components/ReaderBlankSlate.qml b/ui/components/ReaderBlankSlate.qml index f125aa7..c1614ea 100644 --- a/ui/components/ReaderBlankSlate.qml +++ b/ui/components/ReaderBlankSlate.qml @@ -42,15 +42,27 @@ Item { && (height - centeredColumn.height) / 2 >= versionLabel.implicitHeight + Style.space(12) + Style.space(12) - readonly property var keys: [ - { key: "j / k", action: "Move through the list" }, - { key: "Enter or o", action: "Open the selected message" }, - { key: "e", action: "Archive" }, - { key: "d", action: "Move to trash" }, - { key: Keymap.hintKeyFor(Keymap.byId("toggleCheck")), action: "Select or deselect a message" }, - { key: "r", action: "Reply" }, - { key: "c", action: "Compose" } - ] + // What `e` and `d` will ask for here, the decision the hint row reads + // (`MessageList.keyActions`): Restore in the trash, Move to Inbox in + // Archive. `e` is left out where the mailbox has no archive, as the hint + // row leaves it out (`unavailableActions`). + property var keyActions: null + readonly property var keys: { + var acts = root.keyActions || ({ archive: "archive", trash: "trash" }) + var missing = root.service && Array.isArray(root.service.unavailableActions) + ? root.service.unavailableActions : [] + var out = [ + { key: "j / k", action: "Move through the list" }, + { key: "Enter or o", action: "Open the selected message" } + ] + if (missing.indexOf("archive") < 0) out.push({ key: "e", action: Model.actionName(acts.archive) }) + out.push({ key: "d", action: Model.actionName(acts.trash) }) + return out.concat([ + { key: Keymap.hintKeyFor(Keymap.byId("toggleCheck")), action: "Select or deselect a message" }, + { key: "r", action: "Reply" }, + { key: "c", action: "Compose" } + ]) + } Column { id: centeredColumn diff --git a/ui/keys/Keymap.js b/ui/keys/Keymap.js index b178ba8..cc3c091 100644 --- a/ui/keys/Keymap.js +++ b/ui/keys/Keymap.js @@ -69,12 +69,19 @@ var BINDINGS = [ { id: "previousMember", keys: ["p"], contexts: ["reader"], group: "Moving", label: "Previous message in the conversation" }, + // Each is the button in its slot, and where that action is already done the + // button is its inverse: `d` restores a message in the trash and `e` moves + // one in Archive to the Inbox (`Model.keyActionFor`). Re-trashing moves + // nothing, so `d` was a key that did nothing there. `inverse` names that + // action and its hint, so the hint row says what the key will do. { id: "archive", keys: ["e"], contexts: MAIL, - group: "Acting", label: "Archive", - hint: { list: "archive", reader: "archive" } }, + group: "Acting", label: "Archive (in Archive: move to Inbox)", + hint: { list: "archive", reader: "archive" }, + inverse: { action: "unarchive", hint: "move to inbox" } }, { id: "trash", keys: ["d"], contexts: MAIL, - group: "Acting", label: "Move to trash", - hint: { list: "trash", reader: "trash" } }, + group: "Acting", label: "Move to trash (in the trash: restore)", + hint: { list: "trash", reader: "trash" }, + inverse: { action: "untrash", hint: "restore" } }, { id: "star", keys: ["s"], contexts: MAIL, group: "Acting", label: "Star or unstar" }, // `v` because that is the key Gmail moves a message with, and issue #58 asks @@ -407,15 +414,20 @@ function helpGroups(hidden) { // what a key means is a property of the application, and only whether it is on // offer depends on which mailbox is open. `hasSelection` makes the few hints // that act on a particular row contextual without changing their bindings. -function hintsFor(context, unavailable, hasSelection) { +// `actions` says what a binding with an `inverse` will ask for, by binding +// id; where that is the inverse, the hint names it rather than the key's own +// verb. +function hintsFor(context, unavailable, hasSelection, actions) { var out = [] var rows = bindingsFor(context) var missing = Array.isArray(unavailable) ? unavailable : [] + var asked = actions && typeof actions === "object" ? actions : {} for (var i = 0; i < rows.length; i++) { if (rows[i].hintNeedsSelection && hasSelection !== true) continue var unavailableId = rows[i].hintUnavailableId || rows[i].id if (missing.indexOf(unavailableId) >= 0) continue var text = hintTextFor(rows[i], context) + if (text !== "" && rows[i].inverse && asked[rows[i].id] === rows[i].inverse.action) text = rows[i].inverse.hint if (text !== "") out.push(({ key: hintKeyFor(rows[i]), label: text })) } return out diff --git a/ui/tests/qml/tst_message_menu_inbox.qml b/ui/tests/qml/tst_message_menu_inbox.qml index e3f9a38..d2259bb 100644 --- a/ui/tests/qml/tst_message_menu_inbox.qml +++ b/ui/tests/qml/tst_message_menu_inbox.qml @@ -23,6 +23,9 @@ Item { property bool canOpenOnWeb: false property bool hasLabels: true property string rawLabelId: "" + property string mailboxKey: "inbox" + property string searchQuery: "" + property string rawQuery: "" property var messages: [] property var hosts: null function hostForId(id) { return hosts ? hosts[id] || null : fakeService } @@ -74,6 +77,17 @@ Item { menu.close() fakeService.hosts = null fakeService.canReportSpam = true + fakeService.canArchive = true + fakeService.mailboxKey = "inbox" + fakeService.searchQuery = "" + } + + // A row by what it says, among those drawn: the rows added for the trash + // must not be found by a position that moves when a row is added. + function shown(text) { + for (var i = 0; i < menu.menuRows.length; i++) + if (menu.menuRows[i].visible && menu.menuRows[i].text === text) return menu.menuRows[i] + return null } function archivedMessage() { @@ -195,6 +209,116 @@ Item { compare(unarchiveRow().visible, false) } + // ------------------------------------------------- out of the trash + // + // Untrash is the verb every client has, so Restore needs no capability: it + // is offered where there is no archive and no move, and "Move to trash" is + // never offered for a message already there. + + function trashedMessage() { + var summary = archivedMessage() + summary.inTrash = true + summary.labelIds = ["TRASH"] + return summary + } + + function test_a_message_in_the_trash_offers_restore_and_not_trash() { + show(trashedMessage()) + verify(shown("Move to trash") === null, "a message in the trash is not moved to it again") + verify(shown("Move to Inbox") === null, "adding INBOX would leave TRASH on it") + var restore = shown("Restore") + verify(restore !== null) + restore.activated() + compare(actionSpy.count, 1) + compare(actionSpy.signalArguments[0][0], "untrash") + compare(actionSpy.signalArguments[0][1], "m1") + } + + // HEY: no archive, no move, and a posting that carries no TRASH label. + // The plain Trash listing is where it is, and nothing else here gets it out. + function test_without_archive_or_a_trash_label_the_trash_listing_offers_it() { + fakeService.canArchive = false + fakeService.mailboxKey = "trash" + var summary = archivedMessage() + summary.labelIds = [] + show(summary) + verify(shown("Move to trash") === null) + verify(shown("Archive") === null) + var restore = shown("Restore") + verify(restore !== null) + restore.activated() + compare(actionSpy.signalArguments[0][0], "untrash") + } + + // A search reaches the whole account, whatever mailbox the rail still has. + function test_a_search_from_the_trash_is_not_the_trash() { + fakeService.mailboxKey = "trash" + fakeService.searchQuery = "invoice" + var summary = archivedMessage() + summary.inInbox = true + summary.labelIds = ["INBOX"] + show(summary) + verify(shown("Restore") === null) + verify(shown("Move to trash") !== null) + } + + function test_elsewhere_it_is_move_to_trash() { + var summary = archivedMessage() + summary.inInbox = true + show(summary) + verify(shown("Restore") === null) + var trash = shown("Move to trash") + verify(trash !== null) + trash.activated() + compare(actionSpy.signalArguments[0][0], "trash") + } + + function test_the_keyboard_can_reach_restore_in_the_trash() { + show(trashedMessage()) + var steps = 0 + while (steps < menu.menuRows.length + && (menu.cursorIndex < 0 || menu.menuRows[menu.cursorIndex].text !== "Restore")) { + menu.moveCursor(1) + steps++ + } + compare(menu.menuRows[menu.cursorIndex].text, "Restore", "j steps onto the row rather than over it") + menu.runCursor() + compare(actionSpy.signalArguments[0][0], "untrash") + } + + // Opened on a ticked row, a choice reaches every ticked row, so each verb + // is decided over all of them: Restore only when every one is in the + // trash, Unstar only when every one is starred. + function test_a_ticked_row_decides_over_the_selection() { + var inbox = archivedMessage() + inbox.id = "m2" + inbox.inInbox = true + inbox.labelIds = ["INBOX"] + var trashed = trashedMessage() + trashed.starred = true + fakeService.messages = [trashed, inbox] + actionSpy.clear() + menu.openAt("m1", 100, 100, ["m1", "m2"]) + wait(20) + verify(shown("Restore") === null, "one of them is not in the trash") + verify(shown("Unstar") === null, "one of them is not starred") + verify(shown("Star") !== null) + var trash = shown("Move to trash") + verify(trash !== null) + trash.activated() + compare(actionSpy.signalArguments[0][0], "trash") + + var other = trashedMessage() + other.id = "m3" + other.starred = true + fakeService.messages = [trashed, other] + menu.openAt("m1", 100, 100, ["m1", "m3"]) + wait(20) + verify(shown("Restore") !== null, "every one is in the trash") + verify(shown("Unstar") !== null, "every one is starred") + menu.close() + } + // ------------------------------------------------------- what it says function test_from_a_label_it_says_the_label_goes_too() { diff --git a/ui/tests/qml/tst_trash_restore.qml b/ui/tests/qml/tst_trash_restore.qml new file mode 100644 index 0000000..e78612d --- /dev/null +++ b/ui/tests/qml/tst_trash_restore.qml @@ -0,0 +1,786 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../.." as Omamail +import "BackendFixture.js" as BackendFixture +import "ModelFixture.js" as NativeModel +import "NativeIntentFixture.js" as NativeIntentFixture +import "../../account/Accounts.js" as Accounts +import "../../account/Unified.js" as Unified + +// Restore in the trash and Move to Inbox in Archive, through the real Service, +// App and MailAccount, with the native intent engine deciding the optimistic +// change and a client the test controls recording what reaches the provider. +// +// HEY declares neither archive nor move, so a message in its Trash had no way +// out: the menu offered "Move to trash" again, `d` trashed it again, and the +// untrash every client implements was reachable from nothing. HEY's posting +// carries no TRASH label either, so the Trash listing itself says where it is. +// +// Move to Inbox in Archive comes from the same rule. Every control decides its +// verb over the rows its action will reach: a ticked row's menu or +// middle-click, `e` and `d` with rows ticked, and the reader on a ticked +// message all mean the selection, and the reader on a conversation member +// means the conversation's row. +Item { + width: 900 + height: 600 + + QtObject { + id: shellStore + function updateEntryInline(_id, _entry) {} + function hide(_id) {} + } + + QtObject { + id: record + property var trashed: [] + property var untrashed: [] + property var modified: [] + function reset() { trashed = []; untrashed = []; modified = [] } + } + + Component { + id: controlledClient + QtObject { + property var auth: null + property string email: "" + function handle() { return ({ aborted: false }) } + function later(fn) { Qt.callLater(fn); return handle() } + function trashMessage(id, callback) { + record.trashed = record.trashed.concat([String(id)]) + return later(function() { callback(null, "") }) + } + function untrashMessage(id, callback) { + record.untrashed = record.untrashed.concat([String(id)]) + return later(function() { callback(null, "") }) + } + function batchModify(ids, add, remove, callback) { + record.modified = record.modified.concat([{ ids: ids.slice().sort().join(","), + add: add.join(","), remove: remove.join(",") }]) + return later(function() { callback(null, "") }) + } + function modifyMessage(id, add, remove, callback) { return batchModify([id], add, remove, callback) } + function listMessages() { return handle() } + function getMessages() { return handle() } + function getMessage() { return handle() } + function getSummaries() { return handle() } + function getLabels(callback) { return later(function() { callback([], "") }) } + function getLabelCounts(id, callback) { return later(function() { callback({ id: id, unread: 0, total: 0, threadsUnread: 0 }, "") }) } + function getProfile(callback) { return later(function() { callback({ email: email }, "") }) } + function getSendAs(callback) { return later(function() { callback([], "") }) } + function getAttachment() { return handle() } + function saveDraft() { return handle() } + function deleteDraft() { return handle() } + function sendMessage() { return handle() } + function createLabel() { return handle() } + function renameLabel() { return handle() } + function deleteLabel() { return handle() } + function abortRequest(h) { if (h) h.aborted = true } + } + } + + Omamail.Service { + id: mailService + shell: shellStore + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-test" }) + } + + Omamail.App { id: app; service: mailService } + + TestCase { + name: "TrashRestore" + when: windowShown + + function initTestCase() { + // Every mailbox made here asks for its cached list. Unanswered, those + // requests fill the backend's 64 slots and the actions under test wait + // behind them. + BackendFixture.install(mailService).answers = {"cache.queryRestore": null} + NativeModel.install(mailService) + NativeIntentFixture.install(mailService) + } + + function entry(provider, email) { + var value = { email: email, provider: provider, clientSecret: "", label: "", signature: "", + clientId: provider === "outlook" ? "87654321-4321-4abc-9def-1234567890ab" : "" } + if (provider === "imap") value.imap = { imapHost: "imap.example.com", imapPort: 993, + smtpHost: "smtp.example.com", smtpPort: 465, username: email, aliases: [], insecure: false } + if (provider === "jmap") value.jmap = { sessionUrl: "https://api.example.org/jmap/session", + username: email, authScheme: "basic", accountId: "u1" } + return value + } + + // Ready an account the way each sign-in leaves it, with the recording client. + function ready(account, provider) { + account.clientOverride = controlledClient + account.auth.toolsChecked = true + account.auth.missingTools = [] + if (provider === "imap") { + account.auth.passwordChecked = true + account.auth.password = "test-password" + } else if (provider === "jmap") { + account.auth.secret = "app-password" + account.auth.secretChecked = true + } else if (provider === "gmail") { + account.auth.credentials = ({ clientId: "123-test.apps.googleusercontent.com", + clientSecret: "test", projectId: "test" }) + account.auth.refreshBusy = true + account.auth.loggedIn = true + } else { + account.auth.loggedIn = true + } + tryCompare(account, "ready", true) + } + + // Accounts on `mailboxKey`, holding `rows` each. A new address each time: + // the native ledger is one process for the whole file, and an action a + // replaced account left unsettled must not hold up the next one. + property int seeded: 0 + function seedMany(providers, mailboxKey, rowsEach) { + var list = Accounts.emptyList() + var emails = [] + for (var i = 0; i < providers.length; i++) { + seeded++ + emails.push("me" + seeded + "@example.com") + list = Accounts.add(list, entry(providers[i], emails[i])) + } + mailService.setUnifiedMailboxes(false) + mailService.activeIndex = -1 + mailService.accountList = list + mailService.accountsLoaded = true + wait(0) + mailService.refreshCurrent() + var accounts = [] + for (var h = 0; h < providers.length; h++) { + var account = mailService.accountAt(h) + verify(account !== null) + ready(account, providers[h]) + // A host can outlive the account it held, so nothing a previous test + // left on it is allowed to stand. + account.searchQuery = "" + account.rawQuery = "" + account.rawLabelId = "" + account.mailboxKey = mailboxKey + account.messages = rowsEach[h] + account.listLoaded = true + accounts.push(account) + } + return accounts + } + + function seed(provider, mailboxKey, rows) { return seedMany([provider], mailboxKey, [rows])[0] } + + // The same, merged: turning the merged view on moves every mailbox to the + // merged view's mailbox, so the rows are put back after it has. + function unified(providers, mailboxKey, rowsEach) { + var accounts = seedMany(providers, mailboxKey, rowsEach) + mailService.setUnifiedMailboxes(true) + mailService.selectMailbox(mailboxKey) + wait(0) + for (var i = 0; i < accounts.length; i++) { + accounts[i].mailboxKey = mailboxKey + accounts[i].messages = rowsEach[i] + accounts[i].listLoading = false + accounts[i].listLoaded = true + } + tryVerify(function() { return mailService.messages.length === accounts.length }) + compare(mailService.mailboxKey, mailboxKey) + return accounts + } + + // A message with these labels, and the flags the summary derives from them. + // A HEY posting in Trash has none: nothing in it says where it is. + function message(id, labels) { + var list = labels || [] + return ({ id: id, threadId: "", from: { email: "x@example.com", display: "X" }, subject: id, + snippet: "", time: "", date: "", unread: false, starred: list.indexOf("STARRED") >= 0, + inInbox: list.indexOf("INBOX") >= 0, inTrash: list.indexOf("TRASH") >= 0, + inSpam: false, isSent: false, isDraft: false, labelIds: list }) + } + + function named(item, objectName) { + if (!item) return null + if (item.objectName === objectName) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = named(values[i], objectName) + if (found) return found + } + return null + } + + function having(item, accept) { + if (!item) return null + if (accept(item)) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = having(values[i], accept) + if (found) return found + } + return null + } + + // The archive or trash button under `item`, by the key its tooltip names + // (`e`, `d`) rather than by a name the change gave it, so the same lookup + // finds the button before the change and after. + function slot(item, key) { + return having(item, function(candidate) { + return candidate.iconName !== undefined && candidate.visible + && String(candidate.tooltipText || "").slice(-4) === " · " + key + }) + } + + function reader() { + var found = having(app, function(item) { return item.forceRichAnyway !== undefined }) + verify(found !== null) + return found + } + + // The row drawing `id`, found by the summary it was handed. + function rowFor(id) { + return having(app, function(item) { + return item.summary !== undefined && item.trashRequested !== undefined + && !!item.summary && item.summary.id === id + }) + } + + // A popup's rows live under the overlay, not under the menu item, so they + // are found in the list the menu's own cursor walks, by what they say. + function menuRow(text) { + var menu = named(app, "rowMenu") + for (var i = 0; i < menu.menuRows.length; i++) + if (menu.menuRows[i].visible && menu.menuRows[i].text === text) return menu.menuRows[i] + return null + } + + function ids(account) { + return account.messages.map(function(message) { return message.id }).join(",") + } + + function hintLabels() { + var hints = named(app, "status-key-hints") + verify(hints !== null) + return hints.hints.map(function(hint) { return hint.key + " " + hint.label }) + } + + function tick(ids) { + app.checkedAccountId = mailService.activeAccountId + app.checkedIds = ids + verify(app.selectionActive) + } + + // The reader on `id`, in a window wide enough to keep the list beside it. + function openReader(account, id, summary) { + account.selectedId = id + account.selectedMessage = summary + app.pushEntry("reader", { id: id }) + wait(0) + verify(!app.compact) + verify(app.listOnScreen) + } + + function init() { + record.reset() + app.checkedIds = [] + app.checkedAccountId = "" + app.cursorId = "" + app.resetNavigation() + mailService.conversationProjection = ({ showsRail: false, stops: [], caption: "", navigation: {}, memberIds: [] }) + app.open() + } + + function cleanup() { + var menu = named(app, "rowMenu") + if (menu) menu.close() + app.close() + } + + // ------------------------------------------------- Restore in the trash + + function test_hey_trash_row_button_restores() { + var account = seed("hey", "trash", [message("1:2"), message("3:4")]) + compare(mailService.canArchive, false, "HEY has no archive") + compare(mailService.canMoveToLabel, false, "and no move") + var row = rowFor("1:2") + verify(row !== null) + app.cursorId = "1:2" + tryCompare(row, "hot", true) + var trash = slot(row, "d") + verify(trash !== null && trash.visible) + compare(trash.tooltipText, "Restore · d") + compare(trash.iconName, "undo") + trash.clicked() + tryCompare(record, "untrashed", ["1:2"]) + compare(record.trashed, [], "a message in the trash is not trashed again") + tryCompare(account, "actionPreparations", 0) + compare(ids(account), "3:4", "the row leaves the Trash") + tryCompare(mailService, "actionStatus", "Restored") + } + + function test_hey_trash_key_restores() { + var account = seed("hey", "trash", [message("1:2"), message("3:4")]) + app.cursorId = "1:2" + app.runShortcut("trash", "d") + tryCompare(record, "untrashed", ["1:2"]) + compare(record.trashed, []) + tryCompare(account, "actionPreparations", 0) + compare(ids(account), "3:4") + compare(app.cursorId, "3:4", "the cursor takes the place of the row that left") + verify(hintLabels().indexOf("d restore") >= 0, "the hint says what d does here: " + hintLabels()) + } + + function test_hey_selection_in_trash_restores_every_row() { + var account = seed("hey", "trash", [message("1:2"), message("3:4"), message("5:6")]) + app.cursorId = "1:2" + tick(["1:2", "5:6"]) + app.runShortcut("trash", "d") + tryVerify(function() { return record.untrashed.length === 2 }) + compare(record.untrashed.slice().sort(), ["1:2", "5:6"]) + compare(record.trashed, []) + tryCompare(account, "actionPreparations", 0) + compare(ids(account), "3:4") + } + + function test_hey_trash_menu_offers_restore_and_not_trash() { + seed("hey", "trash", [message("1:2")]) + rowFor("1:2").menuRequested(100, 100) + tryCompare(named(app, "rowMenu"), "opened", true) + verify(menuRow("Move to trash") === null, "never Move to trash for a message in the trash") + var restore = menuRow("Restore") + verify(restore !== null) + restore.activated() + tryCompare(record, "untrashed", ["1:2"]) + compare(record.trashed, []) + } + + function test_hey_reader_trash_button_restores() { + var account = seed("hey", "trash", [message("1:2"), message("3:4")]) + app.cursorId = "1:2" + openReader(account, "1:2", account.messages[0]) + var button = slot(reader(), "d") + verify(button !== null) + button.clicked() + tryCompare(record, "untrashed", ["1:2"]) + compare(record.trashed, []) + compare(button.tooltipText, "Restore · d") + } + + // Untrash reaches each provider's client the same way. + function test_trash_key_restores_on_every_provider_data() { + return [ + { tag: "gmail", provider: "gmail", id: "g1" }, + { tag: "imap", provider: "imap", id: "1:Trash" }, + { tag: "outlook", provider: "outlook", id: "1:Deleted Items" }, + { tag: "jmap", provider: "jmap", id: "j1" } + ] + } + + function test_trash_key_restores_on_every_provider(data) { + var account = seed(data.provider, "trash", [message(data.id, ["TRASH"]), message("other", ["TRASH"])]) + app.cursorId = data.id + app.runShortcut("trash", "d") + tryCompare(record, "untrashed", [data.id]) + compare(record.trashed, []) + tryCompare(account, "actionPreparations", 0) + compare(ids(account), "other") + } + + // A merged Trash: each row goes to the mailbox that owns it, HEY's + // unlabelled posting included. + function test_unified_trash_restores_through_the_owning_mailbox() { + var accounts = unified(["hey", "imap"], "trash", [[message("1:2")], [message("7:Trash", ["TRASH"])]]) + app.cursorId = Unified.unifiedId(accounts[0].accountId, "1:2") + app.runShortcut("trash", "d") + tryCompare(record, "untrashed", ["1:2"]) + compare(record.trashed, []) + tryVerify(function() { return accounts[0].messages.length === 0 }) + compare(accounts[1].messages.length, 1, "the other mailbox's row stays") + mailService.setUnifiedMailboxes(false) + } + + // ----------------------------------------- where nothing should change + + // Outside the Trash `d` trashes, and a search started from the Trash + // reaches the whole HEY account, so it is not the Trash. + function test_hey_elsewhere_d_trashes() { + var account = seed("hey", "inbox", [message("1:2", ["INBOX"])]) + app.cursorId = "1:2" + app.runShortcut("trash", "d") + tryCompare(record, "trashed", ["1:2"]) + compare(record.untrashed, []) + verify(hintLabels().indexOf("d trash") >= 0) + tryCompare(account, "actionPreparations", 0) + + record.reset() + account.mailboxKey = "trash" + account.searchQuery = "invoice" + account.messages = [message("7:8", ["INBOX"])] + app.cursorId = "7:8" + app.runShortcut("trash", "d") + tryCompare(record, "trashed", ["7:8"]) + compare(record.untrashed, [], "a search is not the Trash listing") + account.searchQuery = "" + } + + // `e` in the Inbox and in a folder of one's own still archives: on IMAP + // every folder but the Inbox reads as archived, and that is worth keeping. + function test_imap_e_archives_from_the_inbox_and_a_folder() { + var account = seed("imap", "inbox", [message("1:INBOX", ["INBOX"])]) + app.cursorId = "1:INBOX" + app.runShortcut("archive", "e") + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].remove, "INBOX") + compare(record.modified[0].add, "") + tryCompare(account, "actionPreparations", 0) + + record.reset() + account.rawQuery = "folder:Receipts" + account.messages = [message("3:Receipts")] + app.cursorId = "3:Receipts" + app.runShortcut("archive", "e") + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].ids, "3:Receipts") + compare(record.modified[0].add, "", "a folder is not Archive: e archives") + account.rawQuery = "" + } + + // ------------------------------------------- Move to Inbox in Archive + + function test_imap_archive_row_button_moves_to_inbox() { + var account = seed("imap", "archive", [message("1:Archive"), message("2:Archive")]) + var row = rowFor("1:Archive") + verify(row !== null) + app.cursorId = "1:Archive" + tryCompare(row, "hot", true) + var archive = slot(row, "e") + verify(archive !== null && archive.visible) + compare(archive.iconName, "inbox") + compare(archive.tooltipText, "Move to Inbox · e") + compare(slot(row, "d").tooltipText, "Move to trash · d", "the trash button is unchanged in Archive") + archive.clicked() + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].ids, "1:Archive") + compare(record.modified[0].add, "INBOX", "the existing unarchive: INBOX added") + tryCompare(account, "actionPreparations", 0) + compare(ids(account), "2:Archive", "on a folder provider the row leaves Archive") + } + + function test_imap_archive_key_moves_to_inbox() { + seed("imap", "archive", [message("1:Archive"), message("2:Archive")]) + app.cursorId = "1:Archive" + app.runShortcut("archive", "e") + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].add, "INBOX") + compare(record.modified[0].remove, "") + verify(hintLabels().indexOf("e move to inbox") >= 0, "the hint says what e does here: " + hintLabels()) + } + + function test_imap_archive_reader_button_moves_to_inbox() { + var account = seed("imap", "archive", [message("1:Archive"), message("2:Archive")]) + app.cursorId = "1:Archive" + openReader(account, "1:Archive", account.messages[0]) + var archive = slot(reader(), "e") + verify(archive !== null && archive.visible) + archive.clicked() + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].ids, "1:Archive") + compare(record.modified[0].add, "INBOX") + compare(archive.tooltipText, "Move to Inbox · e") + } + + function test_unified_archive_moves_to_inbox_through_the_owning_mailbox() { + var accounts = unified(["imap", "imap"], "archive", [[message("1:Archive")], [message("1:Archive")]]) + app.cursorId = Unified.unifiedId(accounts[1].accountId, "1:Archive") + app.runShortcut("archive", "e") + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].ids, "1:Archive") + compare(record.modified[0].add, "INBOX") + tryVerify(function() { return accounts[1].messages.length === 0 }) + compare(accounts[0].messages.length, 1, "the other mailbox's row stays") + mailService.setUnifiedMailboxes(false) + } + + // --------------------------------------- the rows the action reaches + + // Gmail's `in:anywhere` lists a message in the trash beside one in the + // Inbox. With both ticked, every control that reaches the selection + // trashes both, and says so: Restore would take a message that is not in + // the trash "out" of it. + function mixedGmailSelection() { + var account = seed("gmail", "inbox", [message("t1", ["TRASH"]), message("i1", ["INBOX"])]) + account.searchQuery = "in:anywhere invoice" + app.cursorId = "t1" + tick(["t1", "i1"]) + return account + } + + function test_reader_on_a_ticked_message_trashes_a_mixed_selection() { + var account = mixedGmailSelection() + openReader(account, "t1", account.messages[0]) + var button = slot(reader(), "d") + verify(button !== null) + button.clicked() + tryVerify(function() { return record.trashed.length === 2 }) + compare(record.untrashed, []) + compare(button.tooltipText, "Move to trash · d", "the reader's verb is the selection's") + account.searchQuery = "" + } + + function test_reader_on_a_ticked_message_restores_a_trashed_selection() { + var account = seed("gmail", "trash", [message("t1", ["TRASH"]), message("t2", ["TRASH"]), message("t3", ["TRASH"])]) + app.cursorId = "t1" + tick(["t1", "t2"]) + openReader(account, "t1", account.messages[0]) + var button = slot(reader(), "d") + compare(button.tooltipText, "Restore · d") + button.clicked() + tryVerify(function() { return record.untrashed.length === 2 }) + compare(record.untrashed.slice().sort(), ["t1", "t2"]) + compare(record.trashed, []) + } + + function test_menu_on_a_ticked_row_trashes_a_mixed_selection() { + var account = mixedGmailSelection() + rowFor("t1").menuRequested(100, 100) + tryCompare(named(app, "rowMenu"), "opened", true) + verify(menuRow("Restore") === null, "not every row it reaches is in the trash") + var trash = menuRow("Move to trash") + verify(trash !== null) + trash.activated() + tryVerify(function() { return record.trashed.length === 2 }) + compare(record.untrashed, []) + account.searchQuery = "" + } + + function test_d_trashes_a_mixed_selection() { + var account = mixedGmailSelection() + verify(hintLabels().indexOf("d trash") >= 0, "the hint names trash: " + hintLabels()) + app.runShortcut("trash", "d") + tryVerify(function() { return record.trashed.length === 2 }) + compare(record.untrashed, []) + account.searchQuery = "" + } + + // A menu on a ticked row stars or unstars the whole selection, and says + // which: Unstar only when every ticked row is starred. + function test_menu_on_a_ticked_row_names_the_selections_star() { + seed("gmail", "inbox", [message("s1", ["INBOX", "STARRED"]), message("s2", ["INBOX"])]) + app.cursorId = "s1" + tick(["s1", "s2"]) + rowFor("s1").menuRequested(100, 100) + tryCompare(named(app, "rowMenu"), "opened", true) + verify(menuRow("Unstar") === null, "one of them is not starred") + var star = menuRow("Star") + verify(star !== null) + star.activated() + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].ids, "s1,s2") + compare(record.modified[0].add, "STARRED") + } + + // JMAP lets a message be in the Inbox and Archive at once. Middle-click is + // the archive button, and on a ticked row it means the selection. + function test_middle_click_on_a_ticked_row_archives_a_mixed_selection() { + seed("jmap", "archive", [message("a1"), message("a2", ["INBOX"])]) + app.cursorId = "a1" + tick(["a1", "a2"]) + rowFor("a1").archiveRequested() + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].ids, "a1,a2") + compare(record.modified[0].remove, "INBOX", "archived, not moved to the Inbox") + compare(record.modified[0].add, "") + } + + // A conversation listed in Archive, with `n` on a member that is in the + // Inbox: `e` and the reader's button both act on the conversation's row, + // so both move it to the Inbox, and the button says so. + function test_reader_on_a_conversation_member_follows_the_row() { + var row = message("m1") + row.thread = { id: "c", count: 2, unread: false, flagged: false, memberIds: ["m1", "m2"] } + var account = seed("jmap", "archive", [row]) + app.cursorId = "m1" + openReader(account, "m2", message("m2", ["INBOX"])) + // The rail as the backend projects it, once the projection asked for on + // opening has landed. + wait(100) + mailService.conversationProjection = ({ showsRail: true, stops: [], caption: "", + navigation: {}, memberIds: ["m1", "m2"] }) + wait(50) + compare(mailService.conversationProjection.memberIds, ["m1", "m2"]) + var button = slot(reader(), "e") + verify(button !== null && button.visible) + compare(button.tooltipText, "Move to Inbox · e", "the button says what e does") + button.clicked() + tryVerify(function() { return record.modified.length === 1 }) + compare(record.modified[0].ids, "m1,m2", "the row's action reaches every member") + compare(record.modified[0].add, "INBOX") + } + + // With rows ticked, `d` reaches them, while the reader's buttons reach the + // open message when it is not one of them. The button then names no key: + // here `d` restores the ticked rows and the button trashes the open one. + function test_reader_names_no_key_that_reaches_other_rows() { + var account = seed("gmail", "inbox", [message("t1", ["TRASH"]), message("t2", ["TRASH"]), message("i1", ["INBOX"])]) + account.searchQuery = "in:anywhere invoice" + app.cursorId = "i1" + tick(["t1", "t2"]) + openReader(account, "i1", account.messages[2]) + var button = having(reader(), function(item) { + return item.iconName !== undefined && item.visible && item.tooltipText === "Move to trash" + }) + verify(button !== null, "the reader's trash button names no key") + app.runShortcut("trash", "d") + tryVerify(function() { return record.untrashed.length === 2 }) + compare(record.untrashed.slice().sort(), ["t1", "t2"], "d restores the ticked rows") + compare(record.trashed, []) + tryCompare(account, "actionPreparations", 0) + record.reset() + account.selectedId = "i1" + account.selectedMessage = message("i1", ["INBOX"]) + button.clicked() + tryCompare(record, "trashed", ["i1"]) + compare(record.untrashed, []) + account.searchQuery = "" + } + + // In a narrow window the reader replaces the list and the ticks are not + // on screen, so App acts on the open message alone. The list must decide + // with App's answer, not its own reading of the ticks: `d` and the + // reader's button here act on the open message whatever is ticked. + function compactReader(account, id, summary) { + var window = having(app, function(item) { return item.title === "Omamail" }) + verify(window !== null) + window.width = 600 + app.pushEntry("reader", { id: id }) + account.selectedId = id + account.selectedMessage = summary + wait(0) + verify(app.compact) + verify(!app.selectionActive, "the ticks are not on screen") + return window + } + + function test_compact_reader_d_acts_on_the_open_message() { + var account = seed("gmail", "inbox", [message("t1", ["TRASH"]), message("t2", ["TRASH"]), message("i1", ["INBOX"])]) + account.searchQuery = "in:anywhere invoice" + app.cursorId = "i1" + app.checkedAccountId = mailService.activeAccountId + app.checkedIds = ["t1", "t2"] + var window = compactReader(account, "i1", account.messages[2]) + app.runShortcut("trash", "d") + tryCompare(record, "trashed", ["i1"]) + compare(record.untrashed, [], "the ticked rows are not what d reaches here") + window.width = 980 + account.searchQuery = "" + } + + function test_compact_reader_button_acts_on_the_open_message() { + var account = seed("gmail", "inbox", [message("t1", ["TRASH"]), message("i1", ["INBOX"])]) + account.searchQuery = "in:anywhere invoice" + app.cursorId = "t1" + app.checkedAccountId = mailService.activeAccountId + app.checkedIds = ["t1", "i1"] + var window = compactReader(account, "t1", account.messages[0]) + var button = slot(reader(), "d") + verify(button !== null) + compare(button.tooltipText, "Restore · d", "the open message alone is in the trash") + button.clicked() + tryCompare(record, "untrashed", ["t1"]) + compare(record.trashed, []) + window.width = 980 + account.searchQuery = "" + } + + // A message a notification opened over the Trash is not one of its rows, + // so the listing says nothing about it: an Inbox message there is moved + // to the trash, and the reader's button and `d` both say and do that. + function test_a_message_opened_from_outside_the_trash_listing_is_trashed() { + var account = seed("gmail", "trash", [message("t1", ["TRASH"])]) + var outside = message("n1", ["INBOX"]) + account.previewMessages = [outside] + app.cursorId = "n1" + openReader(account, "n1", outside) + var button = slot(reader(), "d") + verify(button !== null, "the button names d, which reaches the same message") + compare(button.tooltipText, "Move to trash · d") + app.runShortcut("trash", "d") + tryCompare(record, "trashed", ["n1"]) + compare(record.untrashed, []) + tryCompare(account, "actionPreparations", 0) + record.reset() + account.previewMessages = [outside] + account.selectedId = "n1" + account.selectedMessage = outside + app.cursorId = "n1" + button.clicked() + tryCompare(record, "trashed", ["n1"]) + compare(record.untrashed, []) + } + + // Hovering does not move the cursor, so a row names `d` only when `d` + // reaches it: hovering a trashed row beside an Inbox cursor shows Restore + // without the key, while `d` trashes the cursor's row. + function test_a_row_names_d_only_when_d_reaches_it() { + var account = mixedGmailSelection() + app.checkedIds = [] + app.cursorId = "i1" + var trashed = rowFor("t1") + var restore = having(trashed, function(item) { return item.iconName === "undo" }) + verify(restore !== null) + compare(restore.tooltipText, "Restore", "d does not reach a row the pointer is only resting on") + compare(slot(rowFor("i1"), "d").tooltipText, "Move to trash · d") + app.runShortcut("trash", "d") + tryCompare(record, "trashed", ["i1"]) + compare(record.untrashed, []) + account.searchQuery = "" + } + + // A reload that takes the open message's row away moves the cursor to + // another row: `d` now reaches that row, so the reader's button, which + // still means the open message, names no key. + function test_reader_names_no_key_once_the_cursor_moved_off_its_row() { + var account = seed("gmail", "inbox", [message("r1", ["INBOX"]), message("r2", ["INBOX"])]) + app.cursorId = "r1" + openReader(account, "r1", account.messages[0]) + compare(slot(reader(), "d").tooltipText, "Move to trash · d") + account.messages = [message("r2", ["INBOX"])] + app.cursorId = "r2" + wait(0) + var button = having(reader(), function(item) { + return item.iconName === "trash" && item.visible + }) + verify(button !== null) + compare(button.tooltipText, "Move to trash", "d reaches r2 now, the button r1") + app.runShortcut("trash", "d") + tryCompare(record, "trashed", ["r2"]) + } + + // The empty reader lists the keys that do something here, and `e` and `d` + // are listed as what they will do: Restore in the trash, Move to Inbox in + // Archive, no `e` at all where there is no archive. + function blankSlateLines() { + var slate = having(app, function(item) { return item.keys !== undefined && item.showLegend !== undefined }) + verify(slate !== null) + return slate.keys.map(function(line) { return line.key + " " + line.action }) + } + + function test_empty_reader_lists_what_e_and_d_do_here() { + seed("hey", "trash", [message("1:2"), message("3:4")]) + var lines = blankSlateLines() + verify(lines.indexOf("d Restore") >= 0, "in the trash: " + lines) + verify(lines.indexOf("d Move to trash") < 0) + verify(lines.indexOf("e Archive") < 0, "HEY has no archive: " + lines) + verify(hintLabels().indexOf("d restore") >= 0, "the hint row agrees: " + hintLabels()) + + seed("imap", "archive", [message("1:Archive"), message("2:Archive")]) + lines = blankSlateLines() + verify(lines.indexOf("e Move to Inbox") >= 0, "in Archive: " + lines) + verify(lines.indexOf("d Move to trash") >= 0) + + seed("imap", "inbox", [message("1:INBOX", ["INBOX"])]) + lines = blankSlateLines() + verify(lines.indexOf("e Archive") >= 0, "in the Inbox: " + lines) + verify(lines.indexOf("d Move to trash") >= 0) + } + } +} diff --git a/ui/tests/test_keymap.js b/ui/tests/test_keymap.js index 3348c09..be124c7 100644 --- a/ui/tests/test_keymap.js +++ b/ui/tests/test_keymap.js @@ -278,6 +278,20 @@ deepEqual(selectedListHints.map(function (h) { return h.key + " " + h.label }), assert.ok(!keymap.hintsFor("list", ["move"], true).some(function (h) { return h.key === "v" }), "a provider without move does not offer the move hint") +// Where `e` or `d` is the way back, its hint says so rather than naming a verb +// the key will not perform (`Model.keyActionFor` decides which). +deepEqual(keymap.hintsFor("list", [], true, { archive: "archive", trash: "untrash" }).map(function (h) { return h.key + " " + h.label }), + ["j / k move", "e archive", "d restore", "v move to", "Space select", "c compose"], + "in the trash, d restores") +deepEqual(keymap.hintsFor("reader", [], true, { archive: "unarchive", trash: "trash" }).filter(function (h) { + return h.key === "e" || h.key === "d" }).map(function (h) { return h.key + " " + h.label }), + ["e move to inbox", "d trash"], "in Archive, e is the way back") +deepEqual(keymap.hintsFor("list", ["archive", "star", "move"], true, { archive: "unarchive", trash: "untrash" }).map(function (h) { return h.key }), + ["j / k", "d", "Space", "c"], "a provider with no archive still drops e, whatever it would ask for") +deepEqual(keymap.hintsFor("list", [], true, { archive: "archive", trash: "trash" }), selectedListHints, + "the keys' own verbs change nothing") +deepEqual(keymap.hintsFor("list", [], true, { archive: "untrash", trash: "unarchive" }), selectedListHints, + "a key's hint changes only for its own way back") const composeHints = keymap.hintsFor("compose") deepEqual(composeHints.map(function (h) { return h.label }), ["send", "close"], diff --git a/ui/tests/test_model.js b/ui/tests/test_model.js index 0fa00ef..2a13f8b 100644 --- a/ui/tests/test_model.js +++ b/ui/tests/test_model.js @@ -1763,3 +1763,103 @@ assert.strictEqual(model.monitoredNote([]), "") // A provider with no move verb is told so in the hints, the way archive is. deepEqual(model.unavailableActions({ archive: true, star: true, move: true }), []) deepEqual(model.unavailableActions({ archive: true, star: true }), ["move"]) + +// ------------------------------------------------------- the way back +// +// `e` and `d` are their buttons, and where the button's action is already done +// it is the way back. A message in the trash cannot be trashed again — that +// moved nothing — and on HEY, which has no archive and no move, untrash is the +// only way out of the trash at all. +{ + const plain = (key) => ({ mailboxKey: key, searchQuery: "", rawQuery: "" }) + const labelled = { id: "g1", inInbox: false, inTrash: true, labelIds: ["TRASH"] } + // A HEY posting says nothing about the box it was listed from. + const posting = { id: "1:2", inInbox: false, inTrash: false, labelIds: [] } + const inbox = { id: "i1", inInbox: true, inTrash: false, labelIds: ["INBOX"] } + const filed = { id: "a1", inInbox: false, inTrash: false, inSpam: false, + isSent: false, isDraft: false, labelIds: [] } + + assert.strictEqual(model.inTrash(labelled, plain("inbox")), true, "its own TRASH label says so anywhere") + assert.strictEqual(model.inTrash(posting, plain("trash")), true, "the plain Trash listing says so for HEY") + assert.strictEqual(model.inTrash(posting, plain("papertrail")), false) + assert.strictEqual(model.inTrash(posting, { mailboxKey: "trash", searchQuery: "invoice", rawQuery: "" }), false, + "a search reaches the whole account, whatever mailbox the rail still has") + assert.strictEqual(model.inTrash(posting, { mailboxKey: "trash", searchQuery: "", rawQuery: "label:Bills" }), false, + "a label view is somewhere else") + assert.strictEqual(model.inTrash(null, plain("trash")), false) + + assert.strictEqual(model.keyActionFor("trash", [labelled], plain("trash")), "untrash") + assert.strictEqual(model.keyActionFor("trash", [posting], plain("trash")), "untrash") + assert.strictEqual(model.keyActionFor("trash", [inbox], plain("inbox")), "trash") + assert.strictEqual(model.keyActionFor("trash", [labelled, inbox], plain("inbox")), "trash", + "a mixed selection lands in one place rather than swapping every row") + assert.strictEqual(model.keyActionFor("trash", [labelled, posting], plain("trash")), "untrash") + assert.strictEqual(model.keyActionFor("trash", [], plain("trash")), "trash", "no rows, no change") + + // `e` turns back only in Archive: on IMAP every folder but the Inbox reads as + // archived, and `e` there files the message in Archive. + assert.strictEqual(model.keyActionFor("archive", [filed], plain("archive")), "unarchive") + assert.strictEqual(model.keyActionFor("archive", [filed], { mailboxKey: "archive", searchQuery: "x", rawQuery: "" }), + "archive", "a search from Archive is not Archive") + assert.strictEqual(model.keyActionFor("archive", [filed], { mailboxKey: "inbox", searchQuery: "", rawQuery: "folder:Receipts" }), + "archive", "a folder of one's own still archives") + assert.strictEqual(model.keyActionFor("archive", [filed], plain("all")), "archive") + assert.strictEqual(model.keyActionFor("archive", [{ ...filed, inInbox: true }], plain("archive")), "archive", + "a message that is in the inbox as well can still be archived") + assert.strictEqual(model.keyActionFor("archive", [labelled], plain("trash")), "archive", + "the archive key is not the way out of the trash") + assert.strictEqual(model.keyActionFor("star", [labelled], plain("trash")), "star", "other keys are their own") + + // The menu's archived rule, now shared: spam is archived only where the + // owning mailbox has no spam verb, and an owner that cannot be found is not + // taken to lack one. + const junk = { ...filed, inSpam: true } + assert.strictEqual(model.isArchived(filed, plain("archive"), "a1"), true) + assert.strictEqual(model.isArchived(posting, plain("trash"), "1:2"), false, "in the trash is not archived") + assert.strictEqual(model.isArchived(junk, { canReportSpam: false }, "a1"), true) + assert.strictEqual(model.isArchived(junk, { canReportSpam: true }, "a1"), false) + assert.strictEqual(model.isArchived(junk, { hostForId: () => null }, "a1"), false) + assert.strictEqual(model.isArchived(junk, { hostForId: (id) => id === "a1" ? { canReportSpam: false } : null }, "a1"), true) + assert.strictEqual(model.isArchived({ ...filed, isSent: true }, plain("sent"), "a1"), false) + + // Over every row an action reaches: one that is not there is enough to say no. + assert.strictEqual(model.allInTrash([labelled, posting], plain("trash")), true) + assert.strictEqual(model.allInTrash([labelled, inbox], plain("inbox")), false) + assert.strictEqual(model.allInTrash([], plain("trash")), false, "nothing reached is nothing to restore") + assert.strictEqual(model.allArchived([filed, { ...filed, id: "a2" }], plain("archive")), true) + assert.strictEqual(model.allArchived([filed, { ...filed, id: "a2", inInbox: true }], plain("archive")), false) + assert.strictEqual(model.allArchived([], plain("archive")), false) + + // A message that is not a listed row is judged by its own labels: an Inbox + // message a notification opened over the Trash is trashed, not restored. + const opened = { id: "n1", inInbox: true, inTrash: false, labelIds: ["INBOX"] } + assert.strictEqual(model.keyActionFor("trash", [opened], plain("trash")), "untrash", + "as a row of the Trash listing it would count as in the trash") + assert.strictEqual(model.keyActionFor("trash", [opened], model.unlistedView(plain("trash"))), "trash") + assert.strictEqual(model.keyActionFor("trash", [labelled], model.unlistedView(plain("inbox"))), "untrash", + "its own TRASH label still says so") + assert.strictEqual(model.keyActionFor("archive", [filed], model.unlistedView(plain("archive"))), "archive") + const owner = { canReportSpam: false } + assert.strictEqual(model.isArchived({ ...filed, inSpam: true }, + model.unlistedView({ hostForId: () => owner }), "a1"), true, "the owning mailbox still decides spam") + + // The row the reader's buttons land on without a selection: the cursor's + // row for a conversation member, the open message otherwise — App moves the + // cursor there and the list draws from it. + const rail = { memberIds: ["m1", "m2"] } + assert.strictEqual(model.readerTarget(rail, "m2", "m1"), "m1") + assert.strictEqual(model.readerTarget(rail, "m2", ""), "m2", "no cursor: the open message") + assert.strictEqual(model.readerTarget(rail, "x9", "m1"), "x9", "not a member: the open message") + assert.strictEqual(model.readerTarget(null, "x9", "m1"), "x9") + + // Restore, not Move to Inbox: Gmail's untrash takes TRASH off and leaves the + // rest, so a sent message goes back to Sent. + assert.strictEqual(model.actionName("untrash"), "Restore") + assert.strictEqual(model.actionName("unarchive"), "Move to Inbox") + assert.strictEqual(model.actionName("unarchive", true), "Move to Inbox and remove label") + assert.strictEqual(model.actionName("trash"), "Move to trash") + assert.strictEqual(model.actionName("archive"), "Archive") + // An immediate action: no ellipsis. + for (const verb of ["archive", "unarchive", "trash", "untrash"]) + assert.ok(!/\.\.\.$/.test(model.actionName(verb)), verb + " opens nothing") +} From 4ea8864aefdfb3b6bd391f6835b0fd813f6ecde7 Mon Sep 17 00:00:00 2001 From: Oliver Lukschander Date: Mon, 19 Oct 2026 03:58:42 +0200 Subject: [PATCH 35/88] Fix display-name quoting in composed address headers and inserted contacts (#368) --- src/backend/mail.rs | 4 +- src/mail/send.rs | 10 +- src/mail/send_tests.rs | 18 ++ src/message/compose.rs | 119 ++++++-- src/message/content.rs | 5 + src/message/parity_tests.rs | 373 +++++++++++++++++++++++ ui/account/Accounts.js | 6 +- ui/account/Rsvp.qml | 16 +- ui/account/Unsubscribe.qml | 12 +- ui/compose/Recipients.js | 10 +- ui/message/Message.js | 139 +++++++-- ui/tests/qml/tst_calendar_attendance.qml | 11 + ui/tests/qml/tst_unsubscribe_backend.qml | 29 ++ ui/tests/test_accounts.js | 2 + ui/tests/test_calendar.js | 12 + ui/tests/test_message.js | 101 ++++++ ui/tests/test_recipients.js | 30 ++ ui/tests/test_unsubscribe.js | 14 + 18 files changed, 831 insertions(+), 80 deletions(-) diff --git a/src/backend/mail.rs b/src/backend/mail.rs index 4317990..2031f05 100644 --- a/src/backend/mail.rs +++ b/src/backend/mail.rs @@ -26,7 +26,7 @@ fn configured_identities(settings: &Value) -> Result { // hand-edited entry names agent sends exactly as it names composer sends // instead of failing them. fn sender_name(raw: &str) -> String { - let line: String = raw.chars().map(|c| if c.is_ascii_control() { ' ' } else { c }).collect(); + let line: String = raw.chars().map(|c| if c.is_control() { ' ' } else { c }).collect(); line.split(' ').filter(|word| !word.is_empty()).collect::>().join(" ").trim().to_string() } @@ -642,6 +642,8 @@ mod tests { assert_eq!(unnamed, json!([{"email":"me@example.org","displayName":"","isPrimary":true,"isDefault":true}])); let edited = configured_identities(&json!({"email":"me@example.org","senderName":"Jane\r\nBcc: x@example.org\u{7f}\tExample"})).unwrap(); assert_eq!(edited[0]["displayName"], "Jane Bcc: x@example.org Example", "one header line, as the UI stores it"); + let c1 = configured_identities(&json!({"email":"me@example.org","senderName":"Jane\u{85}Example\u{9f}"})).unwrap(); + assert_eq!(c1[0]["displayName"], "Jane Example", "a C1 control too, which the composer would refuse"); } #[test] diff --git a/src/mail/send.rs b/src/mail/send.rs index 4787890..f9af812 100644 --- a/src/mail/send.rs +++ b/src/mail/send.rs @@ -70,12 +70,10 @@ fn display(email: &str, name: &str) -> String { if name.is_empty() { return email.into(); } - let name = if name.chars().any(|c| "\"\\,;<>@():[]".contains(c)) { - format!("\"{}\"", name.replace('\\', "\\\\").replace('"', "\\\"")) - } else { - name.to_owned() - }; - format!("{name} <{email}>") + format!( + "{} <{email}>", + crate::message::compose::quoted_phrase(name, false) + ) } fn parsed(value: &str) -> Result> { diff --git a/src/mail/send_tests.rs b/src/mail/send_tests.rs index cb32be2..befd01a 100644 --- a/src/mail/send_tests.rs +++ b/src/mail/send_tests.rs @@ -91,6 +91,24 @@ fn encoded_header_words_cannot_change_the_previewed_envelope() { assert!(send::prepare(&request, &identities()).is_err()); } +#[test] +fn preview_quotes_names_with_the_composers_rule() { + let mut request = request(); + request.to = vec![ + "\"Dr. Jane\" , \"Team: eve@example.net;\" , Ann Lee " + .into(), + ]; + let preview = send::prepare(&request, &identities()).unwrap().preview(); + assert_eq!( + preview["to"], + json!([ + "\"Dr. Jane\" ", + "\"Team: eve@example.net;\" ", + "Ann Lee " + ]) + ); +} + #[test] fn actual_composed_sender_and_recipients_must_match_the_authorized_plan() { let fields = json!({"from":"alias@example.org","to":"to@example.org","cc":"cc@example.org","bcc":"bcc@example.org"}); diff --git a/src/message/compose.rs b/src/message/compose.rs index 11b8307..fbb669b 100644 --- a/src/message/compose.rs +++ b/src/message/compose.rs @@ -35,19 +35,27 @@ fn header(value: &str) -> Result { } Ok(out) } +// RFC 5322 specials: a display name holding one is one name only when quoted. +const SPECIALS: &[char] = &[ + '(', ')', '<', '>', '[', ']', ':', ';', '@', '\\', ',', '.', '"', +]; +/// A display name as a quoted-string, `"` and `\` escaped: `always` in a header, +/// and in address text a person reads only when the name holds a special. +pub(crate) fn quoted_phrase(value: &str, always: bool) -> String { + if value.is_empty() || (!always && !value.contains(SPECIALS)) { + return value.into(); + } + format!("\"{}\"", value.replace('\\', "\\\\").replace('"', "\\\"")) +} +/// A display name in a header: one encoded word when it is not ASCII or holds +/// `=?`, which readers decode even inside quotes; otherwise quoted. fn phrase(value: &str) -> Result { let value = header(value)?; let value = value.trim(); - if value.is_empty() { - return Ok(String::new()); - } - if !value.is_ascii() { + if !value.is_ascii() || value.contains("=?") { return Ok(format!("=?UTF-8?B?{}?=", STANDARD.encode(value))); } - Ok(format!( - "\"{}\"", - value.replace('\\', "\\\\").replace('"', "\\\"") - )) + Ok(quoted_phrase(value, true)) } fn fold(name: &str, value: &str) -> Result { let value = header(value)?; @@ -57,38 +65,83 @@ fn fold(name: &str, value: &str) -> Result { format!("{name}: =?UTF-8?B?{}?=", STANDARD.encode(value)) }) } +// An address that is one without angle brackets: a dot-atom on both sides of +// the `@`. Anything else keeps its brackets, so `` stays one. +fn plain_address(value: &str) -> bool { + let atom = |part: &str| { + !part.is_empty() + && part + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"!#$%&'*+-/=?^_`{|}~".contains(&b)) + }; + value + .split_once('@') + .is_some_and(|(local, domain)| local.split('.').all(atom) && domain.split('.').all(atom)) +} +fn addresses(list: &mailparse::MailAddrList) -> Vec { + list.iter() + .flat_map(|address| match address { + mailparse::MailAddr::Single(single) => vec![single.addr.clone()], + mailparse::MailAddr::Group(group) => { + group.addrs.iter().map(|s| s.addr.clone()).collect() + } + }) + .collect() +} fn address_line(name: &str, value: &str) -> Result { + // ASCII goes out as typed, comments and unquoted commas included. A list + // ASCII cannot carry, or with a name holding `=?`, is written again with + // each name through `phrase`, and that rewrite refuses what it could change + // the reading of: a group, an address that is not ASCII, or a line that + // names other addresses than the list did. let value = header(value)?; - if value.is_ascii() { - return Ok(format!("{name}: {value}")); - } - fn single(value: &mailparse::SingleInfo) -> Result { - let address = header(&value.addr)?; - let name = phrase(value.display_name.as_deref().unwrap_or(""))?; - Ok(if name.is_empty() { - address - } else { - format!("{name} <{address}>") + let parsed = mailparse::addrparse(&value); + let word = |name: &str| name.contains("=?"); + let words = parsed.as_ref().is_ok_and(|list| { + list.iter().any(|address| match address { + mailparse::MailAddr::Single(single) => { + word(single.display_name.as_deref().unwrap_or("")) + } + mailparse::MailAddr::Group(group) => { + word(&group.group_name) + || group + .addrs + .iter() + .any(|s| word(s.display_name.as_deref().unwrap_or(""))) + } }) + }); + if value.is_ascii() && !words { + return Ok(format!("{name}: {value}")); } - let parsed = mailparse::addrparse(&value).map_err(|_| "invalid_message_header")?; - let mut addresses = Vec::new(); + let parsed = parsed.map_err(|_| "invalid_message_header")?; + let mut written = Vec::new(); for address in parsed.iter() { - addresses.push(match address { - mailparse::MailAddr::Single(value) => single(value)?, - mailparse::MailAddr::Group(group) => format!( - "{}: {};", - phrase(&group.group_name)?, - group - .addrs - .iter() - .map(single) - .collect::>>()? - .join(", ") - ), + let mailparse::MailAddr::Single(single) = address else { + return Err("invalid_message_header"); + }; + if !single.addr.is_ascii() { + return Err("invalid_message_header"); + } + let phrase = phrase(single.display_name.as_deref().unwrap_or(""))?; + written.push(match (phrase.is_empty(), plain_address(&single.addr)) { + (true, true) => single.addr.clone(), + (true, false) => format!("<{}>", single.addr), + (false, _) => format!("{phrase} <{}>", single.addr), }); } - Ok(format!("{name}: {}", addresses.join(", "))) + let line = format!("{name}: {}", written.join(", ")); + let check = format!("{line}\r\n\r\n"); + let (headers, _) = + mailparse::parse_headers(check.as_bytes()).map_err(|_| "invalid_message_header")?; + let read = headers + .first() + .and_then(|header| mailparse::addrparse_header(header).ok()) + .ok_or("invalid_message_header")?; + if addresses(&read) != addresses(&parsed) { + return Err("invalid_message_header"); + } + Ok(line) } fn reference(value: &str) -> Result { Ok(header(value)? diff --git a/src/message/content.rs b/src/message/content.rs index e36bf0f..04aa05b 100644 --- a/src/message/content.rs +++ b/src/message/content.rs @@ -187,10 +187,15 @@ pub fn address(value: &str) -> Value { pub fn addresses(value: &str) -> Vec { let mut quotes = false; let mut angled = false; + let mut escaped = false; let mut start = 0; let mut result = Vec::new(); for (i, c) in value.char_indices() { + // Inside quotes a backslash escapes the next character, so `"O\"Neil, Pat"` + // stays one name. match c { + _ if escaped => escaped = false, + '\\' if quotes => escaped = true, '"' => quotes = !quotes, '<' => angled = true, '>' => angled = false, diff --git a/src/message/parity_tests.rs b/src/message/parity_tests.rs index b267790..1455cbe 100644 --- a/src/message/parity_tests.rs +++ b/src/message/parity_tests.rs @@ -174,3 +174,376 @@ fn outgoing_address_words_never_hide_address_structure() { } } } + +fn composed_headers(fields: &Value) -> (Vec, Value) { + let raw = URL_SAFE_NO_PAD + .decode(compose::build(fields).unwrap()["raw"].as_str().unwrap()) + .unwrap(); + let summary = content::prepare(&json!({"payload":parse(&raw).unwrap()}), 0).unwrap(); + (raw, summary["summary"].clone()) +} + +#[test] +fn display_names_with_specials_stay_one_name_in_every_address_header() { + use mailparse::MailHeaderMap; + let mut names: Vec<(String, String)> = "()<>[]:;@\\,.\"" + .chars() + .map(|c| format!("Ada {c} Lovelace")) + .chain( + [ + "Doe, Jane", + "Say \"hi\" \\ bye", + "ends with \\", + "\"", + "()<>[]:;@\\,.\"", + "Team: eve@example.net;", + "eve@example.net", + "Müller, Jörg", + "工, Lee ", + // Readers decode `=?` even inside quotes, so it leaves encoded. + "=?UTF-8?B?PGV2ZUBleGFtcGxlLm5ldD4=?=", + ] + .map(String::from), + ) + .map(|name| (name.clone(), name)) + .collect(); + for (typed, read) in [ + ("Ada\rBcc: eve@example.net", "Ada Bcc: eve@example.net"), + ("Ada\nLovelace", "Ada Lovelace"), + ("Ada\r\nLovelace", "Ada Lovelace"), + ] { + names.push((typed.into(), read.into())); + } + for (name, read) in &names { + // The To text as the composer's field holds it. + let typed = |email: &str| format!("{} <{email}>", compose::quoted_phrase(name, false)); + let fields = json!({"from":"me@example.org","fromName":name, + "to":format!("{}, ada@example.org", typed("to@example.org")), + "cc":typed("cc@example.org"),"bcc":typed("bcc@example.org"), + "replyTo":typed("reply@example.org"),"body":"body"}); + let (raw, summary) = composed_headers(&fields); + let (headers, _) = mailparse::parse_headers(&raw).unwrap(); + assert!( + headers.iter().all(|h| h.get_value_raw().is_ascii()), + "{name:?}" + ); + assert!(headers.get_all_headers("Bcc").len() == 1, "{name:?}"); + for (key, expected) in [ + ("From", vec!["me@example.org"]), + ("To", vec!["to@example.org", "ada@example.org"]), + ("Cc", vec!["cc@example.org"]), + ("Bcc", vec!["bcc@example.org"]), + ("Reply-To", vec!["reply@example.org"]), + ] { + let list = mailparse::addrparse_header(headers.get_first_header(key).unwrap()).unwrap(); + let singles: Vec<_> = list + .iter() + .map(|a| match a { + mailparse::MailAddr::Single(s) => s.clone(), + _ => panic!("{name:?} made a group in {key}"), + }) + .collect(); + assert_eq!( + singles.iter().map(|s| s.addr.as_str()).collect::>(), + expected, + "{name:?} {key}" + ); + assert_eq!( + singles[0].display_name.as_deref(), + Some(read.as_str()), + "{name:?} {key}" + ); + } + // And the reader this app draws a message with agrees. + assert_eq!(summary["from"]["name"], read.as_str(), "{name:?}"); + assert_eq!(summary["to"].as_array().unwrap().len(), 2, "{name:?}"); + assert_eq!(summary["to"][0]["name"], read.as_str(), "{name:?}"); + assert_eq!(summary["cc"][0]["name"], read.as_str(), "{name:?}"); + } + assert_eq!( + compose::build(&json!({"from":"me@example.org","fromName":"Ada\0Lovelace"})).unwrap_err(), + "invalid_message_header" + ); +} + +fn to_line(to: &str) -> Result { + use mailparse::MailHeaderMap; + let raw = URL_SAFE_NO_PAD + .decode( + compose::build(&json!({"to":to,"body":"b"}))?["raw"] + .as_str() + .unwrap(), + ) + .unwrap(); + let (headers, _) = mailparse::parse_headers(&raw).unwrap(); + Ok(String::from_utf8( + headers + .get_first_header("To") + .unwrap() + .get_value_raw() + .to_vec(), + ) + .unwrap()) +} + +#[test] +fn typed_ascii_lists_go_out_as_typed_and_rewrites_refuse_what_they_could_change() { + // A group in the middle, a nameless address with specials, a comment, an + // unquoted comma, a half-typed draft: all as typed. + for typed in [ + "ada@example.org, Team: eve@example.net; ", + ", ", + "Smith (Sales) , ann@example.org (Ann)", + "Doe, Jane ", + "bo", + ] { + assert_eq!(to_line(typed).unwrap(), typed); + } + // A list ASCII cannot carry, or with a name holding `=?`, is written again: + // a nameless address keeps its brackets unless it is a plain addr-spec. + assert_eq!( + to_line("Jörg , , ").unwrap(), + "=?UTF-8?B?SsO2cmc=?= , , c@example.org" + ); + assert_eq!( + to_line("=?utf-8?q?Team=3A_eve=40example=2Enet=3B?= ").unwrap(), + format!( + "=?UTF-8?B?{}?= ", + base64::engine::general_purpose::STANDARD + .encode("=?utf-8?q?Team=3A_eve=40example=2Enet=3B?=") + ) + ); + // And refuses a group, an address that is not ASCII or printable, and what + // it cannot read. + for typed in [ + "Jörg , Team: eve@example.net; ", + "Équipe: a@example.org, b@example.org;", + "Jörg , undisclosed-recipients:;", + "=?x?=: a@example.org;", + "x ", + "jörg@example.org", + "x ", + "x ", + "Müller, Jörg ", + // Rewritten, these would lose their first recipient: mailparse reads the + // address's `=?utf-8?q?` as an encoded word that runs into the next + // name, and the address turns into display text. + "=?utf-8?q?a?=b@example.org, Jörg ", + " =?utf-8?q?a?=b@example.org=?x?= ,=?utf-8?q?a?=Jörg ", + ] { + assert_eq!( + to_line(typed).unwrap_err(), + "invalid_message_header", + "{typed:?}" + ); + } +} + +#[test] +fn composed_lists_name_the_recipients_that_were_typed() { + // Random lists of specials, names that need rewriting and addresses: + // whenever a message is built, its To names exactly the addresses mailparse + // reads in the typed text, rewritten or not. + let pieces = [ + "\"", + "<", + ">", + "(", + ")", + "[", + "]", + ":", + ";", + "@", + "\\", + ",", + ".", + " ", + "a", + "é", + "Jörg ", + "=?x?= ", + "\"Müller, Jörg\" ", + "", + "b@example.org", + "", + "Team: ", + "(Sales) ", + ]; + let mut seed: u64 = 0x9e3779b97f4a7c15; + for _ in 0..200000 { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + let mut x = seed; + let typed: String = (0..(seed % 10) as usize + 1) + .map(|_| { + x = x + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + pieces[((x >> 33) as usize) % pieces.len()] + }) + .collect(); + let Ok(payload) = compose::build(&json!({"to":typed,"body":"b"})) else { + continue; + }; + let raw = URL_SAFE_NO_PAD + .decode(payload["raw"].as_str().unwrap()) + .unwrap(); + let (headers, _) = mailparse::parse_headers(&raw).unwrap(); + let before = format!("To: {typed}\r\n\r\n"); + let (typed_headers, _) = mailparse::parse_headers(before.as_bytes()).unwrap(); + if let Ok(expected) = envelope::addresses(&typed_headers, "To") { + assert_eq!( + envelope::addresses(&headers, "To"), + Ok(expected), + "{typed:?}" + ); + } + } +} + +#[test] +fn javascript_rewrites_name_the_typed_recipients() { + // `Message.buildRawMessage` has no RFC 5322 parser of its own, so its + // rewrites are judged by mailparse here: a line it writes is printable + // ASCII, and a rewritten one names exactly the typed recipients. + let pieces = [ + "\"", + "<", + ">", + "(", + ")", + "[", + "]", + ":", + ";", + "@", + "\\", + ",", + ".", + " ", + "a", + "é", + "Jörg ", + "=?x?= ", + "=?utf-8?q?a?=", + "\"Müller, Jörg\" ", + "", + "b@example.org", + "", + "Team: ", + "(Sales) ", + "\u{85}", + "\0", + ]; + let mut seed: u64 = 0x1234_5678_9abc_def1; + let inputs: Vec = (0..100000) + .map(|_| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + let mut x = seed; + (0..(seed % 10) as usize + 1) + .map(|_| { + x = x + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + pieces[((x >> 33) as usize) % pieces.len()] + }) + .collect() + }) + .collect(); + let script = r#"const M=require('./ui/tests/load.js').load('message/Message.js');const xs=JSON.parse(require('fs').readFileSync(0,'utf8'));process.stdout.write(JSON.stringify(xs.map(t=>{const r=M.buildRawMessage({to:t,body:'x'});return r===null?null:r.split('\r\n').find(l=>l.startsWith('To:'))})))"#; + let mut child = std::process::Command::new("node") + .args(["-e", script]) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + child + .stdin + .take() + .unwrap() + .write_all(json!(inputs).to_string().as_bytes()) + .unwrap(); + let output = child.wait_with_output().unwrap(); + assert!(output.status.success()); + let lines: Vec> = serde_json::from_slice(&output.stdout).unwrap(); + for (typed, line) in inputs.iter().zip(&lines) { + let Some(line) = line else { continue }; + assert!(line.bytes().all(|b| (0x20..0x7f).contains(&b)), "{typed:?}"); + if line == &format!("To: {typed}") { + continue; + } + let written = format!("{line}\r\n\r\n"); + let (headers, _) = mailparse::parse_headers(written.as_bytes()).unwrap(); + let before = format!("To: {typed}\r\n\r\n"); + let (typed_headers, _) = mailparse::parse_headers(before.as_bytes()).unwrap(); + if let Ok(expected) = envelope::addresses(&typed_headers, "To") { + assert_eq!( + envelope::addresses(&headers, "To"), + Ok(expected), + "{typed:?} -> {line:?}" + ); + } + } +} + +#[test] +fn picker_text_reads_as_one_address_in_mailparse() { + // The To text `Recipients.address` writes for a contact, read by the parser + // the backend writes the header with. + let mut names: Vec = "()<>[]:;@\\,.\"" + .chars() + .map(|c| format!("Ada {c} Lovelace")) + .collect(); + names.extend( + [ + "Doe, Jane", + "Say \"hi\" \\ bye", + "ends with \\", + "Team: eve@example.net;", + "Müller, Jörg", + "Jörg Müller", + "Jane Doe", + ] + .map(String::from), + ); + let script = r#"const R=require('./ui/tests/load.js').load('compose/Recipients.js');const names=JSON.parse(require('fs').readFileSync(0,'utf8'));process.stdout.write(JSON.stringify(names.map(n=>R.address({name:n,email:'dave@example.org'}))))"#; + let mut child = std::process::Command::new("node") + .args(["-e", script]) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + child + .stdin + .take() + .unwrap() + .write_all(json!(names).to_string().as_bytes()) + .unwrap(); + let output = child.wait_with_output().unwrap(); + assert!(output.status.success()); + let texts: Vec = serde_json::from_slice(&output.stdout).unwrap(); + for (name, text) in names.iter().zip(&texts) { + let list = mailparse::addrparse(text).unwrap(); + let one = list.extract_single_info().expect("one address"); + assert_eq!(one.addr, "dave@example.org", "{text}"); + assert_eq!(one.display_name.as_deref(), Some(name.as_str()), "{text}"); + let (raw, _) = composed_headers(&json!({"to":text})); + let (headers, _) = mailparse::parse_headers(&raw).unwrap(); + assert_eq!( + envelope::addresses(&headers, "To").unwrap(), + ["dave@example.org"] + ); + } +} + +#[test] +fn address_lists_honour_escapes_inside_quoted_names() { + let read = content::addresses("\"O\\\"Neil, Pat\" , ada@example.org"); + assert_eq!(read.len(), 2); + assert_eq!(read[0]["name"], "O\"Neil, Pat"); + assert_eq!(read[1]["email"], "ada@example.org"); +} diff --git a/ui/account/Accounts.js b/ui/account/Accounts.js index 44432b4..b01159a 100644 --- a/ui/account/Accounts.js +++ b/ui/account/Accounts.js @@ -566,11 +566,11 @@ function namesSender(provider) { return id === "imap" || id === "outlook" } -// One header line, so a pasted line break or tab becomes a space instead of a -// name the send would refuse. +// One header line, so a pasted line break, tab or other control character +// (C1 included) becomes a space instead of a name the send would refuse. function senderNameText(value) { return trimmed(String(value === undefined || value === null ? "" : value) - .replace(/[\u0000-\u001f\u007f]+/g, " ").replace(/ {2,}/g, " ")) + .replace(/[\u0000-\u001f\u007f-\u009f]+/g, " ").replace(/ {2,}/g, " ")) } function setSenderName(list, id, text) { diff --git a/ui/account/Rsvp.qml b/ui/account/Rsvp.qml index 4d636ff..a4e7238 100644 --- a/ui/account/Rsvp.qml +++ b/ui/account/Rsvp.qml @@ -123,10 +123,7 @@ QtObject { var messageId = account.selectedId var invited = account.selectedInvite var summary = account.selectedMessage - account.rsvpSending = true - account.clearNotice() - - account.api.sendMessage(Mail.buildSendPayload({ + var payload = Mail.buildSendPayload({ // The ATTENDEE line claims this address; the envelope has to agree, or a // strict organiser drops the reply as somebody answering for a third // party. Gmail fills a From in for itself, and the IMAP client puts the @@ -144,7 +141,16 @@ QtObject { // organiser reads as a second, unrelated mail. inReplyTo: summary ? summary.messageId : "", threadId: summary ? summary.threadId : "" - }), function(payload, error) { + }) + // Null when an address in it cannot be written into a header. + if (!payload) { + account.fail("An address in this answer cannot be written into a message") + return + } + account.rsvpSending = true + account.clearNotice() + + account.api.sendMessage(payload, function(sent, error) { account.rsvpSending = false if (error) { account.fail(error) diff --git a/ui/account/Unsubscribe.qml b/ui/account/Unsubscribe.qml index 28925a3..36f24b2 100644 --- a/ui/account/Unsubscribe.qml +++ b/ui/account/Unsubscribe.qml @@ -39,8 +39,7 @@ QtObject { account.fail("Sign in before account.unsubscribing") return } - account.unsubscribing = true - account.api.sendMessage(Mail.buildSendPayload({ + var payload = Mail.buildSendPayload({ // The address the newsletter was sent to. A list that only ever knew // an alias has no reason to act on a request from anywhere else. from: account.receivedAsAddress, @@ -49,7 +48,14 @@ QtObject { to: info.mail.to, subject: info.mail.subject, body: info.mail.body - }), function(payload, error) { + }) + // Null when an address in it cannot be written into a header. + if (!payload) { + account.fail("An address in this request cannot be written into a message") + return + } + account.unsubscribing = true + account.api.sendMessage(payload, function(sent, error) { account.unsubscribing = false if (error) { account.fail(error) diff --git a/ui/compose/Recipients.js b/ui/compose/Recipients.js index 2359324..6dba4da 100644 --- a/ui/compose/Recipients.js +++ b/ui/compose/Recipients.js @@ -1,4 +1,5 @@ .pragma library +.import "../message/Message.js" as Mail // A follow-up to our own message addresses its To list, not its author. // Only the draft owner's identities count as self; another mailbox on a @@ -109,13 +110,14 @@ function suggest(values, text, limit) { return out } +// A contact as To text. The name is often a stranger's, harvested from mail +// that arrived, and the backend writes this text into the header, so a name +// holding a special is quoted here or it stops being one name there. function address(contact) { var value = cleanContact(contact) if (!value) return "" - if (value.name === "") return value.email - var name = value.name.replace(/([\\"])/g, "\\$1") - if (/[,<>]/.test(name)) name = "\"" + name + "\"" - return name + " <" + value.email + ">" + var name = Mail.quotedPhrase(value.name, false) + return name === "" ? value.email : name + " <" + value.email + ">" } function accept(text, contact) { diff --git a/ui/message/Message.js b/ui/message/Message.js index e69a82a..7decb85 100644 --- a/ui/message/Message.js +++ b/ui/message/Message.js @@ -315,18 +315,24 @@ function parseAddress(value) { return { name: name, email: email, display: name || email } } -function parseAddressList(value) { +// The entries of an address list. A comma inside a quoted name or an angle +// address is not a separator, and inside quotes a backslash escapes the next +// character: `"O\"Neil, Pat"` is one name, not a quote that ends early. +function splitAddressList(value) { var raw = String(value || "") var entries = [] var current = "" var inQuotes = false var inAngle = false + var escaped = false for (var i = 0; i < raw.length; i++) { var character = raw.charAt(i) - if (character === "\"") inQuotes = !inQuotes + if (escaped) escaped = false + else if (character === "\\" && inQuotes) escaped = true + else if (character === "\"") inQuotes = !inQuotes else if (character === "<") inAngle = true else if (character === ">") inAngle = false - if (character === "," && !inQuotes && !inAngle) { + else if (character === "," && !inQuotes && !inAngle) { entries.push(current) current = "" continue @@ -334,6 +340,11 @@ function parseAddressList(value) { current += character } entries.push(current) + return entries +} + +function parseAddressList(value) { + var entries = splitAddressList(value) var result = [] for (var j = 0; j < entries.length; j++) { if (!entries[j].trim()) continue @@ -952,23 +963,50 @@ function headerSafe(value) { return String(value === undefined || value === null ? "" : value).replace(/[\r\n]+/g, " ") } -// A display name is a phrase, not a header value: an encoded word may not sit -// inside quotes, and an ASCII name carrying a comma or a dot is only legal -// quoted. `foldHeader` cannot do this, because it encodes the whole value as -// one word — `=?UTF-8?B?...?=` wrapped around `"Name" ` is not an address. +// A display name is a phrase, not a header value. Bare, a name holding one of +// RFC 5322's specials stops being one name: a comma ends it, a colon opens a +// group, a parenthesis a comment, so `Team: eve@example.net;` in front of an +// address adds a recipient. Control characters, line breaks among them, become +// spaces first, so no name reaches a header with one. +var NAME_SPECIALS = /[()<>\[\]:;@\\,."]/ + +function nameText(text) { + return String(text === undefined || text === null ? "" : text) + .replace(/[\u0000-\u001f\u007f-\u009f]+/g, " ").trim() +} + +// A name as a quoted-string, `"` and `\` escaped: `always` in a header, and in +// text a person edits, like the To field, only when the name holds a special, +// so `Jane Doe ` reads as it did. +function quotedPhrase(text, always) { + var value = nameText(text) + if (value === "" || (!always && !NAME_SPECIALS.test(value))) return value + return '"' + value.replace(/([\\"])/g, "\\$1") + '"' +} + +// A name in a header: one encoded word when it is not ASCII, or when it holds +// `=?`, which readers decode even inside quotes; otherwise quoted. An encoded +// word may not sit inside quotes, and `foldHeader` cannot do this, because it +// encodes the whole value as one word — `=?UTF-8?B?...?=` wrapped around +// `"Name" ` is not an address. function encodedPhrase(text) { - var value = headerSafe(text).trim() - if (value === "") return "" - if (!/^[\x20-\x7e]*$/.test(value)) + var value = nameText(text) + if (/[^\x20-\x7e]/.test(value) || value.indexOf("=?") >= 0) return "=?UTF-8?B?" + encodeBase64(value) + "?=" - return '"' + value.replace(/([\\"])/g, "\\$1") + '"' + return quotedPhrase(value, true) } -// Written by hand rather than through `foldHeader` for the reason above. The -// address still loses its line breaks, so a display name cannot smuggle a -// second header in either. -// One address as a header *value*: `"Name" `, or the bare address when -// there is no name to put in front of it. +// An address that is one without angle brackets: a dot-atom on both sides of +// the `@`. Anything else keeps its brackets, so `` +// stays one odd address rather than becoming two. +var ATOM = "[A-Za-z0-9!#$%&'*+\\-/=?^_\\x60{|}~]+" +var PLAIN_ADDRESS = new RegExp("^" + ATOM + "(\\." + ATOM + ")*@" + ATOM + "(\\." + ATOM + ")*$") +var PRINTABLE = /^[\x20-\x7e]*$/ +var ANGLED = /^([\s\S]*)<([^>]*)>$/ + +// One address as a header *value*: `"Name" `, the bare address when +// there is no name and it is plain, or `
` when it is not. The address +// loses its line breaks, so it cannot smuggle a second header in either. // // Split out from `fromHeader` because a provider composing a message rather // than parsing one needs the value without a field name — HEY's client builds a @@ -978,13 +1016,53 @@ function encodedPhrase(text) { function addressHeader(email, displayName) { var address = headerSafe(email).trim() var phrase = encodedPhrase(displayName) - return phrase === "" ? address : phrase + " <" + address + ">" + if (phrase !== "") return phrase + " <" + address + ">" + return address === "" || PLAIN_ADDRESS.test(address) ? address : "<" + address + ">" } function fromHeader(email, displayName) { return "From: " + addressHeader(email, displayName) } +// An address list as a header line, or null when it cannot be written. ASCII +// goes out as typed, comments and unquoted commas included, the way +// `message.compose` writes it. A list ASCII cannot carry, or holding `=?`, is +// written again entry by entry, split the way `parseAddressList` reads it: each +// name through `encodedPhrase`, each address through `addressHeader`. There is +// no RFC 5322 parser here to check the result against, so the rewrite takes +// only entries whose reading it cannot change, and refuses the list otherwise: +// a name that is quoted whole or holds no special (no comment, no group), an +// address of printable ASCII with no quote, comment, `<` or `=?`, and a bare +// entry only when it is a plain addr-spec. `foldHeader` once turned such a +// list into an encoded word that names nobody. +function addressListHeader(name, value) { + var text = headerSafe(value) + var entries = splitAddressList(text) + var words = false + for (var i = 0; i < entries.length; i++) { + var named = entries[i].trim().match(ANGLED) + if ((named ? named[1] : entries[i]).indexOf("=?") >= 0) words = true + } + if (PRINTABLE.test(text) && !words) return name + ": " + text + var out = [] + for (var j = 0; j < entries.length; j++) { + var entry = entries[j].trim() + if (entry === "") continue + var parts = entry.match(ANGLED) + if (!parts) { + if (!PLAIN_ADDRESS.test(entry) || entry.indexOf("=?") >= 0) return null + out.push(entry) + continue + } + var phrase = parts[1].trim() + var address = parts[2].trim() + if (/[()<>\[\]:;@\\,"]/.test(phrase) && !/^"(\\[\s\S]|[^"\\])*"$/.test(phrase)) return null + if (!/^[\x21-\x7e]+$/.test(address) || /["()\\<]|=\?/.test(address)) return null + out.push(addressHeader(address, unquote(phrase))) + } + return name + ": " + out.join(", ") +} + function foldHeader(name, value) { var text = headerSafe(value) // Any non-ASCII in a header has to go back out as an encoded word, or Gmail @@ -1300,13 +1378,21 @@ function buildRawMessage(fields) { // millisecond apart from the id that names it. var now = Date.now() var lines = [] - if (values.from) lines.push(fromHeader(values.from, values.fromName)) - lines.push(foldHeader("To", values.to || "")) - if (values.cc) lines.push(foldHeader("Cc", values.cc)) - if (values.bcc) lines.push(foldHeader("Bcc", values.bcc)) - // Where answers should go when that is not the sender: one header, folded - // like the rest, so a line break typed into it cannot start a second one. - if (values.replyTo) lines.push(foldHeader("Reply-To", values.replyTo)) + if (values.from) { + if (!PRINTABLE.test(headerSafe(values.from))) return null + lines.push(fromHeader(values.from, values.fromName)) + } + // Where answers should go when that is not the sender is one more list, + // written like the rest, so a line break typed into it cannot start a second + // header. A list that cannot be written refuses the whole message. + var lists = [["To", values.to || ""], ["Cc", values.cc], ["Bcc", values.bcc], + ["Reply-To", values.replyTo]] + for (var listIndex = 0; listIndex < lists.length; listIndex++) { + if (listIndex > 0 && !lists[listIndex][1]) continue + var line = addressListHeader(lists[listIndex][0], lists[listIndex][1]) + if (line === null) return null + lines.push(line) + } lines.push(foldHeader("Subject", values.subject || "")) var inReplyTo = referenceValue(values.inReplyTo) if (inReplyTo) { @@ -1389,8 +1475,11 @@ function buildRawMessage(fields) { return lines.join("\r\n") + "\r\n" } +// Null when `buildRawMessage` refuses the message. function buildSendPayload(fields) { - var payload = { raw: encodeBase64Url(buildRawMessage(fields)) } + var raw = buildRawMessage(fields) + if (raw === null) return null + var payload = { raw: encodeBase64Url(raw) } if (fields && fields.threadId) payload.threadId = String(fields.threadId) // The draft this message replaces, so a provider that can destroy it in the // same request as the send or the save does not leave the old copy behind. diff --git a/ui/tests/qml/tst_calendar_attendance.qml b/ui/tests/qml/tst_calendar_attendance.qml index d35f1de..eb14f9f 100644 --- a/ui/tests/qml/tst_calendar_attendance.qml +++ b/ui/tests/qml/tst_calendar_attendance.qml @@ -119,6 +119,17 @@ TestCase { verify(!action.fallbackAvailable) } + function test_reply_email_to_an_unwritable_organizer_address_is_refused() { + backend.apiVersion = 5 + owner.selectedInvite = {uid:"meeting",summary:"Synthetic meeting", + organizer:{email:"organizer\u0085@example.test"},attendees:[]} + action.run("accepted", true) + compare(owner.sent.length, 0, "no message is sent with that address in a header") + verify(!owner.rsvpSending) + verify(owner.failure !== "") + compare(owner.notice, "") + } + function test_success_is_cached_only_after_readback_confirmation() { action.run("accepted") compare(owner.cachedInvite, null) diff --git a/ui/tests/qml/tst_unsubscribe_backend.qml b/ui/tests/qml/tst_unsubscribe_backend.qml index e78b18f..14268c4 100644 --- a/ui/tests/qml/tst_unsubscribe_backend.qml +++ b/ui/tests/qml/tst_unsubscribe_backend.qml @@ -19,6 +19,18 @@ Item { property bool unsubscribing: false property string unsubscribeDone: "" property string error: "" + property bool ready: true + property bool canSend: true + property var selectedUnsubscribe: null + property string receivedAsAddress: "me@example.org" + property string receivedAsName: "" + property string ownAddress: "me@example.org" + property var sent: [] + property var api: ({sendMessage: function(payload, callback) { + mailbox.sent = mailbox.sent.concat([payload]) + callback({}, null) + }}) + function clearNotice() {} function fail(message) { error = message } } Account.Unsubscribe { id: action; account: mailbox } @@ -32,6 +44,23 @@ Item { mailbox.unsubscribing = false mailbox.unsubscribeDone = "" mailbox.error = "" + mailbox.sent = [] + } + // The list's mailto address is a stranger's; one that cannot be written + // into a header is refused before anything is sent. + function test_mail_to_an_unwritable_address_is_refused() { + mailbox.selectedUnsubscribe = {oneClick: false, postUrl: "", url: "", + mail: {to: "x", subject: "Unsubscribe", body: "Unsubscribe"}} + action.run() + compare(mailbox.sent.length, 0) + compare(mailbox.unsubscribing, false) + verify(mailbox.error !== "") + mailbox.error = "" + mailbox.selectedUnsubscribe = {oneClick: false, postUrl: "", url: "", + mail: {to: "list@example.org", subject: "Unsubscribe", body: "Unsubscribe"}} + action.run() + compare(mailbox.sent.length, 1) + compare(mailbox.unsubscribeDone, "Unsubscribe request sent to list@example.org") } function test_native_request_and_success() { action.postUnsubscribe("https://sender.example/unsubscribe?token=synthetic") diff --git a/ui/tests/test_accounts.js b/ui/tests/test_accounts.js index d029b9e..64b279b 100644 --- a/ui/tests/test_accounts.js +++ b/ui/tests/test_accounts.js @@ -888,6 +888,8 @@ assert.strictEqual(frozen(accounts.replaceAt(cidActive, -1, account("x@example.c // One header line: a pasted break or tab becomes a space, never a header // the send would refuse. assert.strictEqual(accounts.senderNameText("Jane\r\nExample\t\u0007"), "Jane Example") +assert.strictEqual(accounts.senderNameText("Jane\u0085Example\u009f"), "Jane Example", + "a C1 control is a control too, and the backend refuses a name with one") } // Only the mailboxes with no server to name the sender take it from here. diff --git a/ui/tests/test_calendar.js b/ui/tests/test_calendar.js index f41bff3..be571d4 100644 --- a/ui/tests/test_calendar.js +++ b/ui/tests/test_calendar.js @@ -571,6 +571,18 @@ assert.strictEqual(calendar.replyFields(invite, me, "declined", 0).subject, assert.strictEqual(calendar.replyFields(invite, me, "tentative", 0).subject, "Tentative: Weekly sync") assert.strictEqual(calendar.replyFields(null, me, "accepted", 0), null) +// The ORGANIZER address is the sender's and is not validated here. An answer to +// one that is not printable ASCII is refused when the mail is built. +{ + const message = load("message/Message.js") + for (const email of ["ada\u0000@example.com", "ada\u0085@example.com", "adä@example.com"]) { + const bad = Object.assign({}, invite, { organizer: Object.assign({}, invite.organizer, { email: email }) }) + const answered = calendar.replyFields(bad, me, "accepted", 0) + assert.strictEqual(answered.to, email) + assert.strictEqual(message.buildSendPayload({ from: me.email, fromName: me.name, to: answered.to, + subject: answered.subject, body: answered.body, calendar: answered.calendar }), null, JSON.stringify(email)) + } +} const noOrganizer = calendar.invitationFrom( GOOGLE_INVITE.replace("ORGANIZER;CN=Ada Lovelace:mailto:ada@example.com", "X-NOTHING:x")) diff --git a/ui/tests/test_message.js b/ui/tests/test_message.js index 8e8d5cc..7eb641f 100644 --- a/ui/tests/test_message.js +++ b/ui/tests/test_message.js @@ -146,6 +146,14 @@ assert.strictEqual(recipients.length, 3) assert.strictEqual(recipients[0].display, "Doe, Jane") assert.strictEqual(recipients[2].email, "carl@z.com") assert.strictEqual(message.parseAddressList("").length, 0) +// Inside quotes a backslash escapes the next character, so an escaped quote +// does not end the name and let its comma split the list. +{ + const escaped = message.parseAddressList('"O\\"Neil, Pat" , ada@example.org') + assert.strictEqual(escaped.length, 2) + assert.strictEqual(escaped[0].name, 'O"Neil, Pat') + assert.strictEqual(escaped[1].email, "ada@example.org") +} assert.strictEqual(message.formatAddressList(recipients, 2), "Doe, Jane, bob, +1") assert.strictEqual(message.formatAddressList(recipients, 5), "Doe, Jane, bob, Carl") @@ -869,6 +877,99 @@ assert.strictEqual(message.extractHtml({ .indexOf("In-Reply-To") < 0) } +// ---------------------------------------------------------- display names +// +// A name holding one of RFC 5322's specials is one name only quoted: bare, a +// comma ends it, a colon opens a group, a parenthesis a comment. Every name +// reaches a header through `encodedPhrase`, in From, To, Cc, Bcc and Reply-To. +const NAME_CASES = "()<>[]:;@\\,.\"".split("").map((c) => ["Ada " + c + " Lovelace"]) + .concat([ + ["Doe, Jane"], ['Say "hi" \\ bye'], ["ends with \\"], ['"'], + ["()<>[]:;@\\,.\""], ["Team: eve@example.net;"], ["eve@example.net"], + ["Müller, Jörg"], ["工, Lee "], + // Readers decode `=?` even inside quotes, so it leaves as an encoded word. + ["=?UTF-8?B?PGV2ZUBleGFtcGxlLm5ldD4=?="], + ["Ada\rBcc: eve@example.net", "Ada Bcc: eve@example.net"], + ["Ada\nLovelace", "Ada Lovelace"], ["Ada\r\nLovelace", "Ada Lovelace"], + ["Ada\u0000Lovelace", "Ada Lovelace"], ["Ada\u0085Lovelace", "Ada Lovelace"] + ]) +{ + const headers = (raw) => raw.split("\r\n\r\n")[0].split("\r\n") + for (const [name, expected = name] of NAME_CASES) { + // The To text as the composer's field holds it; the RSVP and unsubscribe + // senders hand `buildRawMessage` the same kind of text. + const typed = (email) => message.quotedPhrase(name, false) + " <" + email + ">" + const raw = message.buildRawMessage({ + from: "me@example.org", fromName: name, to: typed("to@example.org") + ", ada@example.org", + cc: typed("cc@example.org"), bcc: typed("bcc@example.org"), + replyTo: typed("reply@example.org"), body: "x" + }) + const lines = headers(raw) + assert.ok(lines.every((line) => /^[\x20-\x7e]+$/.test(line)), JSON.stringify(name)) + assert.deepStrictEqual(lines.slice(0, 5).map((line) => line.split(":")[0]), + ["From", "To", "Cc", "Bcc", "Reply-To"], JSON.stringify(name)) + const summary = message.summarize({ payload: message.parseRfc822(raw) }, new Date(0)) + for (const [address, email] of [[summary.from, "me@example.org"], + [summary.replyTo, "reply@example.org"]]) { + assert.strictEqual(address.email, email, JSON.stringify(name)) + assert.strictEqual(address.name, expected, JSON.stringify(name)) + } + for (const [list, email] of [[summary.to, "to@example.org"], [summary.cc, "cc@example.org"], + [summary.bcc, "bcc@example.org"]]) { + assert.strictEqual(list.length, email === "to@example.org" ? 2 : 1, JSON.stringify(name)) + assert.strictEqual(list[0].email, email, JSON.stringify(name)) + assert.strictEqual(list[0].name, expected, JSON.stringify(name)) + } + } + assert.strictEqual(message.fromHeader("me@example.org", "Doe, Jane"), + 'From: "Doe, Jane" ') + assert.strictEqual(message.encodedPhrase('Say "hi" \\ bye'), '"Say \\"hi\\" \\\\ bye"') + assert.strictEqual(message.encodedPhrase("=?x?="), "=?UTF-8?B?" + Buffer.from("=?x?=").toString("base64") + "?=") + assert.strictEqual(message.encodedPhrase("Ada\u0000Lovelace"), '"Ada Lovelace"') + assert.strictEqual(message.encodedPhrase(" \r\n "), "") + assert.strictEqual(message.addressHeader("me@example.org", "\u0000"), "me@example.org", + "a name that is only control characters is no name") + // An ASCII list goes out as typed, the way `message.compose` writes it: + // groups, comments, unquoted commas and odd bracketed addresses included. + const toLine = (to) => { + const raw = message.buildRawMessage({ to: to, body: "x" }) + return raw === null ? null : raw.split("\r\n").find((line) => line.startsWith("To:")) + } + const asTyped = "ada@example.org, Team: eve@example.net; , Smith (Sales) , " + + ", Doe, Jane " + assert.strictEqual(toLine(asTyped), "To: " + asTyped) + // A list ASCII cannot carry is written again entry by entry. A non-ASCII name + // no longer turns the whole list into one encoded word; a nameless address + // keeps its brackets unless it is a plain addr-spec, so it stays one address. + assert.strictEqual(toLine('"Müller, Jörg" , , ' + + ", , d@example.org"), + "To: =?UTF-8?B?" + Buffer.from("Müller, Jörg").toString("base64") + "?= , " + + ", , c@example.org, d@example.org") + assert.strictEqual(toLine('"=?x?=" '), "To: =?UTF-8?B?" + + Buffer.from("=?x?=").toString("base64") + "?= ", "a name holding =? is rewritten too") + // What that rewrite could change the reading of is refused, and the whole + // message with it: a group, a comment, an address holding `=?` or a stray + // `<`, and an address part that is not printable ASCII. + for (const to of ["Jörg , Team: eve@example.net; ", + "Équipe: a@example.org, b@example.org;", "Jörg , undisclosed-recipients:;", + "=?x?=: a@example.org;", "Jorg (CEO, Sälès) ", + "=?utf-8?q?a?=b@example.org, Jörg ", "Jörg <)", + "Jörg <=?utf-8?q?a?=b@example.org>", + "\"Müller, Jörg\" ", "x ", "x ", + "x ", "jörg@example.org", "a@example.org\u0000", "x \u0000"]) { + assert.strictEqual(message.buildRawMessage({ to: to, body: "x" }), null, JSON.stringify(to)) + assert.strictEqual(message.buildSendPayload({ to: to, body: "x" }), null, JSON.stringify(to)) + } + assert.strictEqual(message.buildRawMessage({ from: "me\u0085@example.org", to: "a@example.org", body: "x" }), null) + assert.strictEqual(message.addressHeader("a@example.org,eve@example.net", ""), "") + assert.strictEqual(message.addressHeader("a.b+c@example.org", ""), "a.b+c@example.org") + // Text the composer quoted only where a name needs it: a plain name stays as + // it reads. + assert.strictEqual(message.quotedPhrase("Jane Doe", false), "Jane Doe") + assert.strictEqual(message.quotedPhrase("Jörg Müller", false), "Jörg Müller") + assert.strictEqual(message.quotedPhrase("Doe, Jane", false), '"Doe, Jane"') +} + // ------------------------------------------------------------- Message-ID // // RFC 5322 asks every message for an id, and a relay told not to add missing diff --git a/ui/tests/test_recipients.js b/ui/tests/test_recipients.js index 1776f7e..ebbeb74 100644 --- a/ui/tests/test_recipients.js +++ b/ui/tests/test_recipients.js @@ -47,6 +47,36 @@ assert.strictEqual( "Jane Doe " ) +// A contact's name is often a stranger's, harvested from mail that arrived, +// and the backend writes the field's text into the header. A name holding a +// special is quoted, or `Team: eve@example.net;` in front of an address would +// be a group that sends a copy to eve. +{ + const message = load("message/Message.js") + // One special per name, so each one in the set is needed on its own. + const quoted = (name) => '"' + name.replace(/([\\"])/g, "\\$1") + '" ' + const cases = "()<>[]:;@\\,.\"".split("").map((c) => ["Ada " + c + " Lovelace", quoted("Ada " + c + " Lovelace")]) + cases.push( + ["Team: eve@example.net;", '"Team: eve@example.net;" '], + ['Say "hi" \\ bye', '"Say \\"hi\\" \\\\ bye" '], + ["Smith (Sales)", '"Smith (Sales)" '], + ["Dr. Dave", '"Dr. Dave" '], + ["eve@example.net", '"eve@example.net" '], + ["[ops]", '"[ops]" '], + ["Müller, Jörg", '"Müller, Jörg" '], + ["Jörg Müller", "Jörg Müller "], + ["Dave\u0000Lovelace", "Dave Lovelace "], + ["\u0000", "dave@example.org"]) + for (const [name, text] of cases) { + const written = recipients.append("ada@example.org", { name: name, email: "dave@example.org" }) + assert.strictEqual(written, "ada@example.org, " + text, JSON.stringify(name)) + const summary = message.summarize({ payload: message.parseRfc822( + message.buildRawMessage({ to: written, body: "x" })) }, new Date(0)) + deepEqual(summary.to.map((entry) => entry.email), + ["ada@example.org", "dave@example.org"], JSON.stringify(name)) + } +} + deepEqual(recipients.filter(contacts, "morgan"), [ { name: "Morgan Reed", email: "morgan@example.com" } ]) diff --git a/ui/tests/test_unsubscribe.js b/ui/tests/test_unsubscribe.js index 962511f..89af8a0 100644 --- a/ui/tests/test_unsubscribe.js +++ b/ui/tests/test_unsubscribe.js @@ -177,4 +177,18 @@ assert.strictEqual(unsub.fromMessage(null).available, false) assert.strictEqual(unsub.postBody(), "List-Unsubscribe=One-Click") assert.strictEqual(unsub.postContentType(), "application/x-www-form-urlencoded") +// A list's mailto address is a stranger's, and `parseMailto` lets these three +// through. The message built for them is refused: no NUL, C1 or raw UTF-8 byte +// reaches a header, and no encoded word that names nobody is sent instead. +{ + const message = load("message/Message.js") + for (const link of ["mailto:x%3Ca%00@list.example.org%3E", "mailto:x%3Ca%C2%85@list.example.org%3E", + "mailto:x%3Cu%C3%BC@list.example.org%3E"]) { + const info = unsub.parseMailto(link) + assert.ok(info, link) + assert.strictEqual(message.buildSendPayload({ from: "me@example.org", to: info.to, + subject: info.subject, body: info.body }), null, link) + } +} + console.log("test_unsubscribe.js ok") From 6416295f02286182feedeaf5d8a73ddc7c597308 Mon Sep 17 00:00:00 2001 From: Oliver Lukschander Date: Mon, 19 Oct 2026 04:07:35 +0200 Subject: [PATCH 36/88] Fix lists passed through QML signals or a delegate's modelData reading as empty on Qt 6.12 (#354) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Qt 6.12 changed what a QML signal hands its handler for a `var` argument. On 6.11.2 the handler gets the JavaScript value itself; on 6.12.0 an array arrives as a sequence object and an object as a reference object whose arrays are sequences, all the way down. A sequence still has a length, indices, `map` and the same JSON, but `Array.isArray` is false, and the readers here guard every list with `Array.isArray(x) ? x : []`, so they read these lists as empty. Property assignments, function arguments, `Qt.callLater` and backend replies are not affected, and no way of declaring the parameter avoids it. A delegate's `modelData` is such a reference object on both versions. What this breaks on Qt 6.12, each reproduced through the app's own objects: - Renaming, moving or deleting a watched label clears all of the account's watched labels, and the empty list is saved. - Outbox updates from the backend are ignored: "Sent" never appears, and a message that could not be sent is not reopened in the composer. - Background mail checks leave the bar's unread preview empty. - Clicking a sender or recipient in the reader opens no address menu. - The agent's "found 2 events in …" note stays empty. - Changing the guests of a Google event drops the replies of the guests it keeps. - Deleting a repeating event no longer says that the whole series goes. - A Microsoft (Graph) or CalDAV event opened from the week, day or agenda view, or from a multi-day bar or the overflow list of the month view, lists no guests (a Google one until its detail is read again). Only the timed events of the week and day view are new to 6.12; the others come from a delegate's `modelData` and list no guests on 6.11.2 either. `Plain.plain` (`ui/backend/Plain.js`, beside `Wire.js`) copies such a value back into plain arrays and objects; scalars, functions, Dates, QObjects and value types stay as they are, and copying stops after 64 levels. Each reader applies it once, where the value arrives: `Accounts.setMonitored`, `SendQueue`, `MailAccount`'s `BackendSync.onUpdated`, `AddressMenu.openAt`, `Agent.finishedNote`, `CalendarEventComposer.beginEdit`, `CalendarController.deleteRequest` and `CalendarView.activateEvent`. AGENTS.md gains the rule, so the next reader of a signal's list or a delegate's `modelData` starts from it. Alternative: two of the eight readers get their list through the backend's `notification` signal, which could deliver through callbacks instead; that is a larger change to `Backend.qml`, and the other six cross the app's own signals and delegates anyway. ## Verification `make validate` is green on the last commit (f6ebd63, Qt 6.12.0): 615 Rust library tests, the integration, node and shell suites, the QML suite (1314 passed, 0 failed), the standalone tests (6 and 64), `qmllint`, `omarchy plugin validate` and `git diff --check`. Every new test goes through the app's real signal or delegate (backend notifications as a line through `Backend.receive`) and fails on 6.12.0 with the eight readers as on `main`: - `tst_signal_lists.qml` (a failed send reopening its draft with the account's notice, a sent one saying "Sent", a mail update down to the bar's preview list, the address menu, the agent's note): 5 of 5 fail without the fix. - `tst_calendar_signal_events.qml` (edit, delete): 2 of 2 fail without the fix. - `tst_calendar_signal_crossings.qml` (a Graph event opened through the week view's `eventActivated` and from an agenda row): 2 of 2 fail without the fix. - `tst_label_ownership.qml`: 4 fail without the fix, the three that fail on `main` plus the new one. - The sequence assertion in `test_accounts.js` fails without the fix. `test_backend_plain.js` covers the helper: a sequence against an object with a `length` field, nesting, values left alone, and the depth bound on a cycle. On Qt 6.11.2 (the Arch packages unpacked, not installed) the whole QML suite passes with the fix (1314 passed, 0 failed). Without it, `tst_signal_lists.qml`, `tst_calendar_signal_events.qml` and `tst_label_ownership.qml` pass there, and the agenda test of `tst_calendar_signal_crossings.qml` fails: that is the `modelData` part of the calendar bullet above. The delete confirmation's `confirmed` signal hands `CalendarController.confirmDelete` a reference object too, but nothing it reads is a list: on 6.12.0 as it is, a delete through the dialog of a Graph event, a CalDAV event, a Google event without email to its guests and a whole Google series sends the right request (checked with a probe, not added as a test), so it is left alone. By hand, on Qt 6.12.0 in the standalone host built from this branch, against an iCloud (IMAP) mailbox: a right-click on the sender opens the address menu; a message sent to myself goes from "Sending in 7s · Undo" to "Message queued" to "Sent"; with two labels watched, renaming one keeps both watched. The failed send and the calendar views are covered by their tests, not by hand. A fresh agent reviewed the first commit with AGENTS.md: it confirmed the diagnosis with its own probe on 6.11.2 and 6.12.0, audited the 32 `var` signals, measured the copy at about 0.35 ms per realistic update, and asked for the rule in AGENTS.md and a stronger bar-preview assertion, both done. The calendar views, which the first version of this description left as a follow-up, are the second commit: one `Plain.plain` where the event arrives and its test, read through on its own before it was pushed. ## Release Notes - On Qt 6.12, a message that could not be sent opens again in the composer, renaming, moving or deleting a watched label no longer clears the account's other watched labels, and the bar previews unread mail again. - Also on Qt 6.12: the address menu in a message, the agent's count of events found, guests' replies when editing a Google event, and the warning when deleting a whole repeating series work again. - A Microsoft or CalDAV calendar event opened from the week, day, month or agenda view lists its guests. ## Contact Thanks for reading! Happy to answer questions or change anything: here, by email (oliver@lukschander.com), on X (@olukschander) or Discord (oliverlukschander). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 --- AGENTS.md | 1 + Makefile | 1 + ui/account/Accounts.js | 8 +- ui/account/MailAccount.qml | 6 +- ui/account/SendQueue.qml | 5 +- ui/agent/Agent.js | 7 +- ui/backend/Plain.js | 60 ++++ ui/calendar/CalendarController.qml | 6 +- ui/components/AddressMenu.qml | 6 +- ui/components/CalendarEventComposer.qml | 8 +- ui/components/CalendarView.qml | 6 +- .../qml/tst_calendar_signal_crossings.qml | 107 ++++++++ ui/tests/qml/tst_calendar_signal_events.qml | 170 ++++++++++++ ui/tests/qml/tst_label_ownership.qml | 10 + ui/tests/qml/tst_signal_lists.qml | 257 ++++++++++++++++++ ui/tests/test_accounts.js | 10 + ui/tests/test_backend_plain.js | 82 ++++++ 17 files changed, 741 insertions(+), 9 deletions(-) create mode 100644 ui/backend/Plain.js create mode 100644 ui/tests/qml/tst_calendar_signal_crossings.qml create mode 100644 ui/tests/qml/tst_calendar_signal_events.qml create mode 100644 ui/tests/qml/tst_signal_lists.qml create mode 100644 ui/tests/test_backend_plain.js diff --git a/AGENTS.md b/AGENTS.md index f553b61..36f329f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -20,6 +20,7 @@ Read the relevant document before changing its area; these contain constraints t - Group by module, not file type. Rust lives in `src/`, CLI handling in `src/cli/`, and dispatch in `src/backend/`. QML and its JavaScript live together under `ui/`. Rust unit tests live with their modules; UI tests live in `ui/tests/`; integration tests live in `tests/`. - Rust owns mail business logic and durable agent jobs. QML owns presentation and interaction state, communicating with the persistent backend over stdin/stdout rather than invoking CLI commands. See `docs/BACKEND.md` and `docs/ARCHITECTURE.md`. - QML JavaScript libraries start with `.pragma library` and use `var` and `function`, not `const`, `let`, arrows or template literals. Put testable presentation rules there. Tests load module paths, e.g. `load("cache/Cache.js")`; `ui/tests/load.js` resolves QML `.import` chains. +- A list or object received through a `var` signal argument (on Qt 6.12) or a delegate's `modelData` is a sequence or reference object, not a JavaScript array or object: `Array.isArray` says no and its keys come back sorted. A receiver that judges such a value, keeps it or writes to it takes `Plain.plain` (`ui/backend/Plain.js`) of it first; `ui/tests/qml/tst_signal_lists.qml` shows the pattern. - List new QML files in the Makefile's lint inputs and import other modules explicitly. - `Service.qml` is shell-constructed and must declare no required properties. The shell injects only `shell`, `manifest`, `pluginRegistry`, and `barWidgetRegistry`; settings arrive through the bar widget's `applySettings`. diff --git a/Makefile b/Makefile index c862068..af42e3c 100644 --- a/Makefile +++ b/Makefile @@ -121,6 +121,7 @@ test-js: node app/tests/test_shell_theme.js node ui/tests/test_bar_bridge.js node ui/tests/test_backend_wire.js + node ui/tests/test_backend_plain.js node ui/tests/test_backend_queue.js node ui/tests/test_backend_compatibility.js node ui/tests/test_backend_runtime.js diff --git a/ui/account/Accounts.js b/ui/account/Accounts.js index b01159a..48c87e7 100644 --- a/ui/account/Accounts.js +++ b/ui/account/Accounts.js @@ -1,6 +1,7 @@ .pragma library .import "Aliases.js" as Aliases +.import "../backend/Plain.js" as Plain // The list of Gmail accounts and which one the window is showing. One account // was the original design; several is a list plus a selection, and every rule @@ -519,13 +520,16 @@ function idList(value) { } // The watched list replaced whole: what a rename or a move leaves it as, -// once the ids it held have followed the folders they named. +// once the ids it held have followed the folders they named. They arrive +// through the account's `monitoredMigrated` signal, so they are made plain +// first: read as nothing, they emptied every watch on the account and wrote +// that to disk. function setMonitored(list, id, labelIds) { var next = copyList(list) var at = indexOfId(next.accounts, id) if (at < 0) return next var entry = makeAccount(next.accounts[at]) - entry.monitored = idList(labelIds) + entry.monitored = idList(Plain.plain(labelIds)) next.accounts[at] = entry return next } diff --git a/ui/account/MailAccount.qml b/ui/account/MailAccount.qml index 90b0c15..6a36b0e 100644 --- a/ui/account/MailAccount.qml +++ b/ui/account/MailAccount.qml @@ -16,6 +16,7 @@ import "Aliases.js" as Aliases import "../providers/Registry.js" as Provider import "../providers/ImapProtocol.js" as Imap import "../providers/OAuth.js" as OAuth +import "../backend/Plain.js" as Plain // One mailbox: its sign-in, its cache, its messages. Service.qml owns a set of // these and puts whichever is on screen in front of the views. @@ -2902,7 +2903,10 @@ Item { query: Provider.unreadQuery(root.providerId) intervalSec: root.refreshIntervalSec pageSize: root.maxMessages - onUpdated: function(snapshot) { + // Made plain: through the signal Qt 6.12 hands `messages` over as a + // sequence, which `summarizeResources` reads as none. + onUpdated: function(crossed) { + var snapshot = Plain.plain(crossed) if (snapshot.error) return root.summarizeResources(snapshot.messages || [], function(prepared, preparationError) { if (preparationError) return diff --git a/ui/account/SendQueue.qml b/ui/account/SendQueue.qml index 1524f98..20b414d 100644 --- a/ui/account/SendQueue.qml +++ b/ui/account/SendQueue.qml @@ -1,4 +1,5 @@ import QtQuick +import "../backend/Plain.js" as Plain // Rendering and callback correlation only. The backend owns undo deadlines, // serial delivery, cancellation and durable recovery after a restart. @@ -234,7 +235,9 @@ QtObject { function onReadyChanged() { if (queue.available()) { queue.revision = -1; queue.sync() } } - function onNotification(method, params) { if (method === "outbox.changed") queue.apply(params) } + // Made plain: through the signal Qt 6.12 hands `entries` over as a + // sequence, which `apply` reads as none. + function onNotification(method, params) { if (method === "outbox.changed") queue.apply(Plain.plain(params)) } } readonly property Connections authEvents: Connections { target: queue.account ? queue.account.auth : null diff --git a/ui/agent/Agent.js b/ui/agent/Agent.js index 13f9b64..6290c0c 100644 --- a/ui/agent/Agent.js +++ b/ui/agent/Agent.js @@ -1,5 +1,7 @@ .pragma library +.import "../backend/Plain.js" as Plain + function suggestsClear(text) { var value = String(text || "") return !/[\r\n]/.test(value) && /^\s*\/[a-z]*$/.test(value) @@ -145,7 +147,10 @@ function finishedNote(job) { // A look for events was not asked for by name: it says something only // when it found something, and nothing at all when it found nothing. if (isEventsJob(job)) { - var found = glyph === "done" && Array.isArray(job.events) ? job.events.length : 0 + // The job comes through the runner's `jobFinished` signal, which on Qt + // 6.12 hands `events` over as a sequence: made plain, it is a list again. + var events = Plain.plain(job.events) + var found = glyph === "done" && Array.isArray(events) ? events.length : 0 return found > 0 ? "The agent found " + (found === 1 ? "an event" : found + " events") + " in " + about : "" } if (glyph === "question") return "The agent has a question about " + about diff --git a/ui/backend/Plain.js b/ui/backend/Plain.js new file mode 100644 index 0000000..1bffe7d --- /dev/null +++ b/ui/backend/Plain.js @@ -0,0 +1,60 @@ +.pragma library + +// A value a QML signal handed over, made plain JavaScript again. +// +// Qt 6.12 carries a `var` signal argument through QVariant, so a JavaScript +// array reaches the handler as a sequence object and an object as a reference +// object, with every array and object inside it converted the same way. They +// read like the originals (a length, indices, `map`, `instanceof Array`, +// JSON), but `Array.isArray` says no to a sequence, so each +// `Array.isArray(x) ? x : []` in these libraries reads a list as empty. Qt +// 6.11 handed over the value itself. The backend's `notification` is such a +// signal, and so is every list the app passes from one object to another. +// +// Arrays and sequences come back as arrays, plain and reference objects as +// objects, all the way down. Anything else (a scalar, a function, a Date, a +// QObject, a value type such as a point or a url) is returned as it is. Past +// MAX_DEPTH levels a value is returned uncopied, so a cycle ends. +var MAX_DEPTH = 64 + +function plain(value) { return copied(value, 0) } + +function copied(value, depth) { + if (value === null || typeof value !== "object" || depth >= MAX_DEPTH) return value + var out + if (isList(value)) { + out = [] + for (var i = 0; i < value.length; i++) out.push(copied(value[i], depth + 1)) + return out + } + if (!isRecord(value)) return value + out = {} + for (var key in value) out[key] = copied(value[key], depth + 1) + return out +} + +// An array, or a sequence: built on Array.prototype, which is itself an +// array, so this holds for one made in another realm (the node tests) too. +function isList(value) { + if (Array.isArray(value)) return true + for (var proto = Object.getPrototypeOf(value); proto !== null; proto = Object.getPrototypeOf(proto)) + if (Array.isArray(proto)) return true + return false +} + +// An object literal, or the reference object Qt 6.12 makes of one. A QObject, +// a Date and a value type carry a tag or a prototype of their own. +// +// The reference object is known only by its tag, which is Qt's internal class +// name: nothing else tells it from a url, which also says `[object Object]` +// and has a prototype of its own, and copying a url key by key would empty +// it. If Qt renames the tag, objects stop being copied, the lists inside them +// arrive as sequences again, and the QML tests for those readers fail; the +// release workflow runs only the node tests, which build their stand-in with +// this tag, so it would not notice. +function isRecord(value) { + var tag = Object.prototype.toString.call(value) + if (tag === "[object V4ReferenceObject]") return true + var proto = Object.getPrototypeOf(value) + return tag === "[object Object]" && (proto === null || Object.getPrototypeOf(proto) === null) +} diff --git a/ui/calendar/CalendarController.qml b/ui/calendar/CalendarController.qml index 68ecd47..a0b38c1 100644 --- a/ui/calendar/CalendarController.qml +++ b/ui/calendar/CalendarController.qml @@ -5,6 +5,7 @@ import qs.Commons import "Calendar.js" as Calendar import "Sources.js" as Sources import "../providers/MicrosoftOAuth.js" as Microsoft +import "../backend/Plain.js" as Plain Item { id: root @@ -451,7 +452,10 @@ Item { } // What the delete confirmation asks: named, with a button per consequence. - function deleteRequest(sourceId, event) { + // The event comes through the views' `deleteRequested` signal, and on Qt + // 6.12 its recurrence arrives as a sequence that reads as no series. + function deleteRequest(sourceId, crossed) { + var event = Plain.plain(crossed) var request = Calendar.deleteRequest(event, findSource(String(sourceId || "")), service.backendCanGoogleCalendars === true) request.sourceId = String(sourceId || "") diff --git a/ui/components/AddressMenu.qml b/ui/components/AddressMenu.qml index feb7e62..a212acc 100644 --- a/ui/components/AddressMenu.qml +++ b/ui/components/AddressMenu.qml @@ -3,6 +3,7 @@ import QtQuick.Controls as QQC import qs.Commons import qs.Ui import "Menu.js" as Menu +import "../backend/Plain.js" as Plain // The menu an address opens on a right-click in the reader's header: copy // it, or find the mail from it or to it. A line with several addresses asks @@ -37,7 +38,10 @@ Item { function openAt(list, sceneX, sceneY) { var rows = [] var seen = {} - var given = Array.isArray(list) ? list : [] + // The list comes through the reader's `addressMenuRequested` signal, + // which on Qt 6.12 hands it over as a sequence: made plain, it is a list. + var given = Plain.plain(list) + if (!Array.isArray(given)) given = [] for (var i = 0; i < given.length; i++) { var email = String(given[i] && given[i].email ? given[i].email : "").trim() if (email === "" || seen[email]) continue diff --git a/ui/components/CalendarEventComposer.qml b/ui/components/CalendarEventComposer.qml index 5052991..7d57585 100644 --- a/ui/components/CalendarEventComposer.qml +++ b/ui/components/CalendarEventComposer.qml @@ -5,6 +5,7 @@ import qs.Commons import qs.Ui import "../calendar/Calendar.js" as Calendar import "../compose/Recipients.js" as Recipients +import "../backend/Plain.js" as Plain Rectangle { id: root @@ -290,7 +291,12 @@ Rectangle { return String(groups[0].calendars[0].id) } - function beginEdit(sourceId, event) { + // The event comes through the views' `editRequested` and the controller's + // `composeRequested` signals, and on Qt 6.12 its guests arrive as a + // sequence the update cannot match against: the guests it kept lost their + // answers. + function beginEdit(sourceId, crossed) { + var event = Plain.plain(crossed) advancedOptionsOpen = !!event && (event.transparency === "transparent" || event.visibility === "private" || event.visibility === "public") changeRecurrence = false diff --git a/ui/components/CalendarView.qml b/ui/components/CalendarView.qml index abd6ec7..f89ef70 100644 --- a/ui/components/CalendarView.qml +++ b/ui/components/CalendarView.qml @@ -4,6 +4,7 @@ import QtQuick.Layouts import QtQuick.Window import qs.Commons import "../calendar/Calendar.js" as Calendar +import "../backend/Plain.js" as Plain import "../keys/Keymap.js" as Keymap Item { @@ -205,8 +206,11 @@ Item { selectedEventId = Calendar.eventKey(values[index]) } - function activateEvent(event, alreadyDetailed) { + // The event comes from a row's `modelData`, or through the week view's `eventActivated` signal on Qt 6.12, as a + // reference object whose guests are a sequence the detail reads as nobody: made plain, they are a list again. + function activateEvent(crossed, alreadyDetailed) { monthOverflow.close() + var event = Plain.plain(crossed) if (!event) return var serial = ++detailReadSerial selectedEventId = Calendar.eventKey(event) diff --git a/ui/tests/qml/tst_calendar_signal_crossings.qml b/ui/tests/qml/tst_calendar_signal_crossings.qml new file mode 100644 index 0000000..68daf43 --- /dev/null +++ b/ui/tests/qml/tst_calendar_signal_crossings.qml @@ -0,0 +1,107 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../../calendar/Calendar.js" as Calendar +import "../../components" as Views + +// A calendar event that reaches the detail through the week view's `eventActivated` signal or an agenda row's +// `modelData`, which Qt 6.12 hands over as a reference object whose lists are sequences: `Array.isArray` +// refuses them, so the guests read as nobody. Each test goes through the app's own crossing. +Item { + width: 1000 + height: 700 + SystemPalette { id: palette } + + // A Microsoft 365 mailbox's Graph calendar in the views, with a stand-in controller that holds one meeting. + QtObject { id: viewService; property bool backendCanGoogleCalendars: false; property string calendarPalettePath: "" } + QtObject { + id: viewController + property var service: viewService + property var source: ({ id: "microsoft:outlook:jane@corp.example.test", kind: "microsoft", + accountId: "outlook:jane@corp.example.test", calendarId: "", readOnly: false }) + property var availableSources: ({ sources: [source] }) + property var events: Calendar.eventsFromGraph({ value: [{ id: "g1", iCalUId: "g1-uid", subject: "Review", + start: { dateTime: "2026-10-14T10:00:00", timeZone: "UTC" }, end: { dateTime: "2026-10-14T11:00:00", timeZone: "UTC" }, + organizer: { emailAddress: { address: "jane@corp.example.test", name: "Jane" } }, + attendees: [ + { emailAddress: { address: "one@corp.example.test", name: "One" }, status: { response: "accepted" } }, + { emailAddress: { address: "two@corp.example.test", name: "Two" }, status: { response: "declined" } }] }] }, + "microsoft:outlook:jane@corp.example.test") + property bool sourcesLoaded: true + property bool loading: false + property bool clockRunning: false + property bool eventWriting: false + property double nowMs: new Date(2026, 9, 12, 8).getTime() + property string lastError: "" + property string lastErrorKind: "" + function findSource(id) { return source } + function colorKeyFor(id) { return "accent" } + function refresh(start, end) {} + function nativeRequest(source, operation, fields, callback) {} + } + + Views.CalendarView { + id: view + anchors.fill: parent + controller: viewController + textColor: palette.text + backgroundColor: palette.base + accentColor: palette.highlight + urgentColor: palette.highlight + dimColor: palette.mid + calendarBorderColor: palette.mid + calendarTodayBackgroundColor: palette.alternateBase + calendarBorderWidth: 1 + panelFontFamily: "sans-serif" + } + + TestCase { + name: "CalendarSignalCrossings" + when: windowShown + + function find(item, test) { + if (item && test(item)) return item + var kids = item ? (item.children || []) : [] + for (var i = 0; i < kids.length; i++) { var found = find(kids[i], test); if (found) return found } + return null + } + + function init() { + view.closeDetail() + view.setView("week") + } + + function guestsLine() { + var line = find(view, function(item) { return typeof item.text === "string" && item.text.indexOf("Guests · ") === 0 }) + return line ? line.text : "" + } + + // The week view hands the event it was clicked on through `eventActivated`: the detail names its guests. + function test_an_event_opened_from_the_week_names_its_guests() { + var week = find(view, function(item) { + return item !== view && typeof item.eventActivated === "function" && typeof item.eventRescheduled === "function" + }) + verify(week !== null) + week.eventActivated(viewController.events[0]) + verify(view.detailOpen) + compare(view.detailEvent.summary, "Review") + tryVerify(function() { return guestsLine() === "Guests · 2" }, 2000, "the detail lists both guests") + verify(Array.isArray(view.detailEvent.attendees), "the open event's guests are a list") + } + + // An agenda row opens the event its `modelData` holds: the detail names its guests too. + function test_an_event_opened_from_the_agenda_names_its_guests() { + view.setView("agenda") + var row = null + tryVerify(function() { + row = find(view, function(item) { return item.modelData !== undefined && item.modelData !== null + && item.modelData.graphId === "g1" && item.ListView !== undefined && item.ListView.view !== null }) + return row !== null && row.visible && row.width > 0 + }, 2000, "the agenda lists the meeting") + // A row built a moment ago can miss the first press offscreen; the click is the row's own all the same. + tryVerify(function() { if (!view.detailOpen) mouseClick(row, 10, 10); return view.detailOpen }, 2000, + "the row opens its event") + tryVerify(function() { return guestsLine() === "Guests · 2" }, 2000, "the detail lists both guests") + verify(Array.isArray(view.detailEvent.attendees), "the open event's guests are a list") + } + } +} diff --git a/ui/tests/qml/tst_calendar_signal_events.qml b/ui/tests/qml/tst_calendar_signal_events.qml new file mode 100644 index 0000000..b85bd54 --- /dev/null +++ b/ui/tests/qml/tst_calendar_signal_events.qml @@ -0,0 +1,170 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../../components" as Views +import "../../calendar" as CalendarModule +import "../../calendar/Calendar.js" as Calendar + +// An event the detail hands on through `editRequested` and `deleteRequested` +// crosses two QML signals before the composer or the controller reads it, and +// Qt 6.12 hands its lists over as sequences, which `Array.isArray` refuses. +// Wired as App.qml wires them: the edit to the composer's `beginEdit`, the +// delete to the controller's `deleteRequest`. +Item { + width: 1000 + height: 700 + SystemPalette { id: palette } + + QtObject { id: viewService; property bool backendCanGoogleCalendars: true; property string calendarPalettePath: "" } + QtObject { + id: viewController + property var service: viewService + property var source: ({ id: "google:me@example.com", kind: "microsoft", calendarId: "primary", readOnly: false }) + property var availableSources: ({ sources: [source] }) + property var events: [{ sourceId: "google:me@example.com", googleId: "planning", uid: "planning", etag: "v1", + summary: "Planning", start: { ms: new Date(2026, 9, 1, 10).getTime() }, end: { ms: new Date(2026, 9, 1, 11).getTime() }, + organizer: { self: true }, recurrence: ["RRULE:FREQ=WEEKLY"], recurrenceLines: ["RRULE:FREQ=WEEKLY"], + attendees: [{ email: "one@example.test", responseStatus: "accepted", displayName: "One" }, + { email: "two@example.test", responseStatus: "declined" }] }] + property bool sourcesLoaded: true + property bool loading: false + property bool clockRunning: false + property bool eventWriting: false + property double nowMs: new Date(2026, 9, 1, 8).getTime() + property string lastError: "" + property string lastErrorKind: "" + function findSource(id) { return source } + function colorKeyFor(id) { return "accent" } + function refresh(start, end) {} + function nativeRequest(source, operation, fields, callback) {} + } + + QtObject { + id: composerService + property var recipientContacts: [] + property bool backendCanGoogleCalendars: true + function refreshRecipientContacts() {} + } + QtObject { + id: composerController + property var service: composerService + property bool creatingEvent: false + property bool eventWriting: false + property bool composerHeld: false + property string accountId: "" + property var updated: null + property var writableSourceGroups: [{ id: "google:me@example.com", providerLabel: "Google", accountLabel: "me@example.com", + calendars: [{ id: "google:me@example.com", name: "me@example.com", colorKey: "accent", kind: "google" }] }] + function findSource(id) { return writableSourceGroups[0].calendars[0] } + function updateEvent(sourceId, event, fields) { updated = { sourceId: sourceId, event: event, fields: fields }; return true } + function createEvent(sourceId, fields) { return false } + signal eventCreated(bool ok, string error) + signal eventUpdated(bool ok, string error) + signal composeRequested(var prefill) + signal composeEnded() + } + + QtObject { + id: controllerService + property bool backendCanDiscoverCalendars: false + property bool backendCanGoogleCalendars: true + property var backend: ({ ready: false, call: function() {} }) + property bool unifiedCalendarView: false + property var accountSummaries: [] + function withGoogleAccessToken(_accountId, callback) { callback("", "not used by this test") } + function writeConfig(name, payload, callback) {} + } + CalendarModule.CalendarController { + id: realController + service: controllerService + pluginDir: "/tmp/omamail-test" + accountId: "" + sourceList: ({ version: 1, sources: [] }) + } + + property var deleteConfirmation: null + + Views.CalendarView { + id: view + anchors.fill: parent + controller: viewController + textColor: palette.text + backgroundColor: palette.base + accentColor: palette.highlight + urgentColor: palette.highlight + dimColor: palette.mid + calendarBorderColor: palette.mid + calendarTodayBackgroundColor: palette.alternateBase + calendarBorderWidth: 1 + panelFontFamily: "sans-serif" + onEditRequested: function(sourceId, event) { composer.beginEdit(sourceId, event) } + onDeleteRequested: function(sourceId, event) { deleteConfirmation = realController.deleteRequest(sourceId, event) } + } + + Views.CalendarEventComposer { + id: composer + anchors.fill: parent + controller: composerController + textColor: palette.text + backgroundColor: palette.base + accentColor: palette.highlight + urgentColor: palette.highlight + dimColor: palette.mid + panelFontFamily: "sans-serif" + } + + TestCase { + name: "CalendarSignalEvents" + when: windowShown + + function detail() { + function find(item) { + if (item && item !== view && typeof item.editRequested === "function" + && typeof item.deleteRequested === "function" && item.event !== undefined) return item + var kids = item ? (item.children || []) : [] + for (var i = 0; i < kids.length; i++) { var found = find(kids[i]); if (found) return found } + return null + } + return find(view) + } + + function init() { + composer.close() + composerController.updated = null + deleteConfirmation = null + view.closeDetail() + view.activateEvent(viewController.events[0]) + } + + // Adding a guest keeps the answers the others already gave: the update + // matches the typed addresses against the event's guests, and a list it + // cannot read as one leaves every guest with an address and nothing else. + function test_an_edit_keeps_the_guests_answers() { + var shown = detail() + verify(shown !== null) + shown.editRequested(String(shown.event.sourceId), shown.event) + verify(composer.opened) + findChild(composer, "event-guests-field").text = "one@example.test, two@example.test, new@example.test" + composer.submit("all") + verify(composerController.updated !== null, "the composer saved") + var update = composerController.updated + verify(Array.isArray(update.event.attendees), "the edited event's guests are a list") + var built = Calendar.updateEvent(update.fields, update.event, Date.now()) + verify(built.ok, built.error) + var options = Calendar.googleOptions(built.google, update.fields, { kind: "google" }, update.event) + verify(options.ok, options.error) + compare(options.body.attendees.map(function(guest) { + return [guest.email, guest.responseStatus || "", guest.displayName || ""].join("|") + }), ["one@example.test|accepted|One", "two@example.test|declined|", "new@example.test||"]) + } + + // Deleting a repeating event says that the whole series goes. + function test_a_delete_names_the_series() { + var shown = detail() + verify(shown !== null) + shown.deleteRequested(String(shown.event.sourceId), shown.event) + verify(deleteConfirmation !== null) + compare(deleteConfirmation.message, "Every occurrence of this series will be deleted.") + verify(Array.isArray(deleteConfirmation.event.recurrence)) + } + } +} diff --git a/ui/tests/qml/tst_label_ownership.qml b/ui/tests/qml/tst_label_ownership.qml index e8a98bb..3f68099 100644 --- a/ui/tests/qml/tst_label_ownership.qml +++ b/ui/tests/qml/tst_label_ownership.qml @@ -212,6 +212,16 @@ Item { deepEqualIds(mailService.monitoredLabelIds, ["Jobs/2026"]) } + // The ids a rename leaves reach the entry through `monitoredMigrated`, a + // `var` signal argument, which Qt 6.12 hands over as a sequence rather + // than an array. They are the watched list all the same: none is lost. + function test_watched_ids_cross_the_migration_signal_whole() { + seed([entry(ada, ["Work/2026", "Receipts"])], adaId) + mailService.accountAt(0).monitoredMigrated(["Jobs/2026", "Receipts"]) + deepEqualIds(Accounts.find(mailService.accountList, adaId).monitored, ["Jobs/2026", "Receipts"]) + deepEqualIds(mailService.monitoredLabelIds, ["Jobs/2026", "Receipts"]) + } + // A name the list already has is refused before it reaches the server. function test_a_name_already_taken_is_refused() { seed([entry(ada)], adaId) diff --git a/ui/tests/qml/tst_signal_lists.qml b/ui/tests/qml/tst_signal_lists.qml new file mode 100644 index 0000000..1966417 --- /dev/null +++ b/ui/tests/qml/tst_signal_lists.qml @@ -0,0 +1,257 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../.." as Omamail +import "BackendFixture.js" as BackendFixture +import "../../account/Accounts.js" as Accounts + +// Lists that reach their reader through a QML signal. Qt 6.12 hands a `var` +// signal argument over as a sequence or a reference object, which +// `Array.isArray` refuses, so each of these read its list as empty. Every test +// emits through the app's own signal: the backend's `notification` (fed a +// line, as the process would), the reader's `addressMenuRequested`, and the +// agent runner's `jobFinished`. +Item { + width: 900 + height: 600 + + QtObject { + id: shellStore + function updateEntryInline(_id, _entry) {} + function hide(_id) {} + } + + QtObject { + id: record + property var renames: [] + property var deletes: [] + property var creates: [] + property var listing: ({}) + property string listingError: "" + property int inFlight: 0 + property int mostInFlight: 0 + property int counted: 0 + function reset() { renames = []; deletes = []; creates = []; listing = {}; listingError = ""; inFlight = 0; mostInFlight = 0; counted = 0 } + } + + Component { + id: controlledClient + QtObject { + property var auth: null + property string email: "" + function handle() { return ({ aborted: false }) } + function later(fn) { Qt.callLater(fn); return handle() } + function listMessages() { return handle() } + function getMessages() { return handle() } + function getMessage() { return handle() } + function trashMessage() { return handle() } + function untrashMessage() { return handle() } + function batchModify() { return handle() } + function modifyMessage() { return handle() } + function getLabels(callback) { + var mine = record.listing[email] || [] + var refused = record.listingError + record.listingError = "" + return later(function() { callback(refused !== "" ? [] : mine, refused) }) + } + function getLabelCounts(id, callback) { + record.inFlight++ + if (record.inFlight > record.mostInFlight) record.mostInFlight = record.inFlight + return later(function() { + record.inFlight-- + record.counted++ + callback({ id: id, unread: 1, total: 1, threadsUnread: 1 }, "") + }) + } + function getProfile(callback) { return later(function() { callback({ email: email }, "") }) } + function getSendAs(callback) { return later(function() { callback([], "") }) } + function getAttachment() { return handle() } + function saveDraft() { return handle() } + function deleteDraft() { return handle() } + function sendMessage() { return handle() } + function createLabel(name, callback) { + record.creates = record.creates.concat([email + ":" + name]) + return later(function() { callback(null, "") }) + } + function renameLabel(id, name, callback) { + record.renames = record.renames.concat([email + ":" + id + ">" + name]) + return later(function() { callback(null, "") }) + } + function deleteLabel(id, callback) { + record.deletes = record.deletes.concat([email + ":" + id]) + return later(function() { callback(null, "") }) + } + function abortRequest(h) { if (h) h.aborted = true } + } + } + + Omamail.Service { + id: mailService + shell: shellStore + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-test" }) + } + + Omamail.App { id: app; service: mailService } + + TestCase { + name: "SignalLists" + when: windowShown + property var listener + + function initTestCase() { listener = BackendFixture.markReady(mailService) } + + // A send one test left behind must not answer for the next. + function cleanup() { + var account = mailService.accountAt(0) + if (account) { + account.sendQueue.parked = [] + account.sendQueue.submitted = ({}) + account.sendQueue.revision = -1 + } + var compose = composeView() + if (compose) compose.reset() + app.clearComposeRecovery() + } + + function seed() { + var list = Accounts.add(Accounts.emptyList(), { + email: "ada@example.com", provider: "imap", clientId: "", clientSecret: "", + imap: { imapHost: "imap.example.com", imapPort: 993, smtpHost: "smtp.example.com", smtpPort: 465, + username: "ada@example.com", aliases: [], insecure: false }, + label: "", signature: "", monitored: [] }) + mailService.activeIndex = -1 + mailService.accountList = Accounts.setActive(list, "imap:ada@example.com") + mailService.accountsLoaded = true + wait(0) + mailService.refreshCurrent() + var account = mailService.accountAt(0) + account.clientOverride = controlledClient + account.auth.toolsChecked = true + account.auth.missingTools = [] + account.auth.passwordChecked = true + account.auth.password = "test-password" + tryCompare(account, "ready", true) + account.api.email = "ada@example.com" + return account + } + + // One line from the backend process, the way `Process` hands it over. + function notify(method, params) { + mailService.backend.receive(JSON.stringify({ jsonrpc: "2.0", method: method, params: params })) + } + + function find(item, test) { + if (item && test(item)) return item + var kids = item ? (item.children || []) : [] + for (var i = 0; i < kids.length; i++) { var found = find(kids[i], test); if (found) return found } + return null + } + + function composeView() { + var item = find(app, function(candidate) { return candidate.objectName === "compose-to-field" }) + while (item && typeof item.resumePendingSend !== "function") item = item.parent + return item + } + + function field(name) { + return find(composeView(), function(candidate) { return candidate.objectName === name }) + } + + // A message the backend could not send comes back: the parked draft opens + // again with its words, and the account says what happened. + function test_a_failed_send_reopens_its_draft() { + var account = seed() + mailService.applySettings({ undoSendSeconds: 10 }) + app.opened = false + app.loadComposeRecovery("") + app.clearComposeRecovery() + app.resetNavigation() + var compose = composeView() + verify(compose !== null) + compose.reset() + compose.opened = false + var before = listener.requests.length + app.startCompose("new") + field("compose-to-field").text = "person@example.com" + field("compose-body-editor").text = "Keep these words" + compose.submit() + tryCompare(compose, "parkedForSend", true) + var enqueue = null + tryVerify(function() { + var asked = listener.requests.slice(before).filter(function(r) { return r.method === "outbox.enqueue" }) + enqueue = asked.length ? asked[0] : null + return enqueue !== null + }, 1000, "the message goes to the outbox") + var now = Date.now() + var entry = { id: enqueue.params.sendId, state: "queued", order: enqueue.params.order, queuedAt: now, dueAt: now + 10000 } + BackendFixture.respond(mailService, enqueue, { snapshot: { accountId: account.accountId, revision: 2000, entries: [entry] } }) + tryCompare(account, "sendPendingCount", 1) + compare(compose.opened, false, "the draft is parked while it waits") + + entry.state = "failed" + notify("outbox.changed", { accountId: account.accountId, revision: 2001, entries: [entry] }) + tryCompare(compose, "opened", true, 1000, "the failed message's draft opens again") + compare(compose.parkedForSend, false) + compare(field("compose-body-editor").text, "Keep these words") + compare(account.lastError, "The message could not be sent. Your draft has been kept.") + } + + // The outbox says what it holds: a queued message is waiting, and a sent + // one is reported as sent. + function test_the_outbox_reaches_the_send_queue() { + var account = seed() + var now = Date.now() + notify("outbox.changed", { accountId: account.accountId, revision: 1000, entries: [ + { id: "send-1", state: "queued", order: 1, queuedAt: now, dueAt: now + 60000 }] }) + tryCompare(account, "sendPendingCount", 1, 1000, "the queued message is waiting") + notify("outbox.changed", { accountId: account.accountId, revision: 1001, entries: [ + { id: "send-1", state: "sent", order: 1, queuedAt: now, dueAt: now, result: {} }] }) + tryCompare(account, "sendPendingCount", 0) + compare(account.actionStatus, "Sent") + } + + // A background check's unread messages reach the bar's preview. + function test_a_mail_update_prepares_its_messages() { + var account = seed() + var info = mailService.backend.protocolInfo + mailService.backend.protocolInfo = { apiVersion: info.apiVersion, protocol: info.protocol, version: info.version, + methods: ["mail.watch"] } + tryCompare(account, "nativePolling", true) + var before = listener.requests.length + notify("mail.updated", { accountId: account.accountId, sequence: 1e12, estimate: 1, fingerprint: "one", error: "", + messages: [{ id: "unread-1", threadId: "unread-1", labelIds: ["INBOX", "UNREAD"], snippet: "Hello", + payload: { headers: [{ name: "Subject", value: "Hello" }] } }] }) + var asked = [] + tryVerify(function() { + asked = listener.requests.slice(before).filter(function(r) { return r.method === "message.summaries" }) + return asked.length === 1 + }, 1000, "the update's messages are sent to be summarised") + compare(asked[0].params.messages.length, 1) + compare(asked[0].params.messages[0].id, "unread-1") + tryVerify(function() { + return mailService.barMessages.some(function(message) { return message.id === "unread-1" }) + }, 1000, "and the bar previews it") + compare(account.previewMessages.length, 1) + } + + // Clicking a sender opens a menu of the addresses. + function test_the_address_menu_lists_the_addresses() { + seed() + var reader = find(app, function(item) { return typeof item.addressMenuRequested === "function" && item.summary !== undefined }) + var menu = find(app, function(item) { return item.objectName === "address-menu" }) + verify(reader !== null && menu !== null) + reader.addressMenuRequested([{ name: "Bob", email: "bob@example.com" }, { name: "Cy", email: "cy@example.com" }], 20, 20) + compare(menu.addresses.length, 2) + tryCompare(menu, "opened", true) + menu.close() + } + + // A look for events that found some says how many. + function test_the_agent_says_how_many_events_it_found() { + var account = seed() + account.note("") + mailService.agentRunner.jobFinished({ kind: "events", state: "done", subject: "Lunch", + accountId: account.accountId, events: [{ title: "Lunch" }, { title: "Dinner" }] }) + compare(account.actionStatus, "The agent found 2 events in “Lunch”") + } + } +} diff --git a/ui/tests/test_accounts.js b/ui/tests/test_accounts.js index 64b279b..6bb5e30 100644 --- a/ui/tests/test_accounts.js +++ b/ui/tests/test_accounts.js @@ -851,6 +851,16 @@ assert.strictEqual(frozen(accounts.replaceAt(cidActive, -1, account("x@example.c deepEqual(accounts.toggleMonitored(two, "me@gmail.com", " ").accounts[0].monitored, ["Label_7", "Label_9"]) deepEqual(accounts.load(accounts.serialize(two)).accounts[0].monitored, ["Label_7", "Label_9"], "and it is written to disk and read back") + // The ids a rename leaves come through a QML signal, and Qt 6.12 hands an + // array over as a sequence: a length, indices and Array.prototype up its + // chain, but `Array.isArray` says no. + const sequence = Object.assign(Object.create(Array.prototype), { 0: "Label_8", 1: "Label_9", length: 2 }) + assert.strictEqual(Array.isArray(sequence), false) + deepEqual(accounts.setMonitored(two, "me@gmail.com", sequence).accounts[0].monitored, ["Label_8", "Label_9"], + "a sequence replaces the watched list as an array does") + deepEqual(accounts.setMonitored(two, "me@gmail.com", ["Label_8"]).accounts[0].monitored, ["Label_8"]) + deepEqual(accounts.setMonitored(two, "me@gmail.com", "Label_8").accounts[0].monitored, [], + "a string is not a list") } diff --git a/ui/tests/test_backend_plain.js b/ui/tests/test_backend_plain.js new file mode 100644 index 0000000..be19054 --- /dev/null +++ b/ui/tests/test_backend_plain.js @@ -0,0 +1,82 @@ +const assert = require("assert") +const { load } = require("./load") +const Plain = load("backend/Plain.js") + +// What Qt 6.12 hands a signal handler: a sequence is indexable, has a length +// and Array.prototype further up its chain, but `Array.isArray` says no; a +// reference object says what it is in its tag. +const qt = { + sequence(items) { + const s = Object.create(Object.create(Array.prototype)) + items.forEach((item, i) => { s[i] = item }) + Object.defineProperty(s, "length", { value: items.length }) + return s + }, + reference(fields) { + return Object.assign(Object.create({ [Symbol.toStringTag]: "V4ReferenceObject" }), fields) + } +} + +{ + const crossed = qt.sequence(["a", "b"]) + assert.strictEqual(Array.isArray(crossed), false, "the stand-in is what fails the libraries' checks") + const list = Plain.plain(crossed) + assert.strictEqual(Array.isArray(list), true, "a sequence comes back as an array") + assert.deepStrictEqual(Array.from(list), ["a", "b"]) +} + +// Nested all the way down: a reference object holding a sequence of reference +// objects holding sequences, as a backend notification arrives. +{ + const snapshot = qt.reference({ accountId: "a", revision: 3, + entries: qt.sequence([qt.reference({ id: "s1", tags: qt.sequence(["x"]) })]) }) + const copy = Plain.plain(snapshot) + assert.strictEqual(Object.prototype.toString.call(copy), "[object Object]") + assert.strictEqual(Array.isArray(copy.entries), true) + assert.strictEqual(Array.isArray(copy.entries[0].tags), true) + assert.strictEqual(copy.entries[0].id, "s1") + assert.strictEqual(copy.revision, 3) + assert.strictEqual(copy.accountId, "a") +} + +// An object with a `length` field is an object, not a list. +{ + const copy = Plain.plain({ length: 2, 0: "a", name: "n" }) + assert.strictEqual(Array.isArray(copy), false) + assert.deepStrictEqual(Object.keys(copy).sort(), ["0", "length", "name"]) + assert.strictEqual(copy.length, 2) +} + +// Ordinary arrays and objects are copied too, and nothing else is touched. +{ + const nested = { list: [1, [2, { deep: ["d"] }]] } + const copy = Plain.plain(nested) + assert.notStrictEqual(copy, nested) + assert.strictEqual(JSON.stringify(copy), JSON.stringify(nested)) + const when = new Date(0) + const call = function() {} + for (const value of [null, undefined, 0, 1.5, "", "text", true, false, when, call]) + assert.strictEqual(Plain.plain(value), value) + const inside = Plain.plain({ when: when, call: call }) + assert.strictEqual(inside.when, when) + assert.strictEqual(inside.call, call) + const wrapped = Object.create({ kind: "value type" }) + assert.strictEqual(Plain.plain(wrapped), wrapped, "an object with a prototype of its own is not a record") +} + +// A cycle ends at the depth bound instead of recursing for ever. +{ + const loop = { name: "loop" } + loop.self = loop + let copy = Plain.plain(loop) + let depth = 0 + while (copy !== loop && depth < 1000) { copy = copy.self; depth++ } + assert.strictEqual(copy, loop, "the copy hands back the original past the bound") + assert.strictEqual(depth, Plain.MAX_DEPTH) + const deep = [] + let cursor = deep + for (let i = 0; i < 200; i++) { const next = []; cursor.push(next); cursor = next } + assert.strictEqual(Array.isArray(Plain.plain(deep)), true) +} + +console.log("test_backend_plain.js ok") From d4d80f162eacae9a4a6562f24b1a5d8b48987723 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 19 Oct 2026 10:08:34 +0800 Subject: [PATCH 37/88] Add Microsoft 365 with reviewed credential and send fixes (#381) --- AGENTS.md | 1 + Makefile | 4 + README.md | 4 +- app/src/resource_check.cpp | 2 +- app/tests/native/tst_resources.cpp | 4 +- app/tests/qml/tst_standalone_composition.qml | 2 +- backend-api.json | 223 ++++- docs/BACKEND-RUNTIME.md | 8 +- docs/BACKEND.md | 23 +- docs/MAILBOXES.md | 14 +- docs/SPEC.md | 3 + docs/providers/README.md | 4 +- docs/providers/m365.md | 21 + scripts/diagnostics.py | 6 + src/account/mod.rs | 4 +- src/account/tests.rs | 12 + src/agent/context.rs | 10 + src/auth/credentials.rs | 24 +- src/auth/credentials/m365.rs | 338 +++++++ src/auth/mod.rs | 21 +- src/backend/mail.rs | 53 ++ src/backend/methods.rs | 17 + src/backend/mod.rs | 9 +- src/backend/reader.rs | 16 +- src/backend/rpc.rs | 39 + src/cache/query.rs | 4 +- src/cache/query/tests.rs | 12 + src/calendar/discovery.rs | 15 +- src/credentials/mod.rs | 5 +- src/credentials/rpc.rs | 4 + src/credentials/tests.rs | 39 + src/mail/action.rs | 1 + src/mail/send.rs | 6 +- src/mail/types.rs | 3 + src/message/mod.rs | 7 +- src/outbox/delivery.rs | 5 + src/outbox/mod.rs | 7 +- src/providers/domain.rs | 27 +- src/providers/domain_queries.json | 10 + src/providers/m365/actions.rs | 214 +++++ src/providers/m365/cancel.rs | 124 +++ src/providers/m365/drafts.rs | 96 ++ src/providers/m365/graph.rs | 555 ++++++++++++ src/providers/m365/ids.rs | 76 ++ src/providers/m365/mailbox.rs | 468 ++++++++++ src/providers/m365/mod.rs | 76 ++ src/providers/m365/model.rs | 401 +++++++++ src/providers/m365/params.rs | 118 +++ src/providers/m365/read.rs | 210 +++++ src/providers/m365/send.rs | 109 +++ src/providers/m365/signin.rs | 532 +++++++++++ src/providers/m365/tests.rs | 724 +++++++++++++++ src/providers/m365/tests/actions.rs | 780 ++++++++++++++++ src/providers/m365/tests/drafts.rs | 268 ++++++ src/providers/m365/tests/mail.rs | 452 ++++++++++ src/providers/m365/tests/signin.rs | 894 +++++++++++++++++++ src/providers/m365/tests/unit.rs | 527 +++++++++++ src/providers/m365/tests/wire.rs | 498 +++++++++++ src/providers/m365/verify.rs | 82 ++ src/providers/mod.rs | 21 +- src/sync/mod.rs | 14 +- src/sync/preload.rs | 61 +- tests/cli.rs | 57 +- tests/test_backend_api.py | 7 +- tests/test_diagnostics.py | 2 +- tests/test_source.sh | 41 + ui/App.qml | 136 +-- ui/Service.qml | 2 +- ui/account/Accounts.js | 4 +- ui/account/MailAccount.qml | 37 +- ui/account/Model.js | 8 +- ui/backend/Backend.qml | 29 +- ui/calendar/Calendar.js | 21 +- ui/calendar/CalendarController.qml | 2 +- ui/calendar/Sources.js | 3 +- ui/components/M365SetupPage.qml | 402 +++++++++ ui/components/SetupPages.qml | 139 +++ ui/providers/M365.js | 62 ++ ui/providers/M365Auth.qml | 420 +++++++++ ui/providers/M365Client.qml | 231 +++++ ui/providers/M365Protocol.js | 248 +++++ ui/providers/NativeDomain.js | 31 + ui/providers/Registry.js | 16 +- ui/tests/oracles/providers/M365.js | 19 + ui/tests/oracles/providers/Registry.js | 3 +- ui/tests/qml/tst_backend.qml | 16 + ui/tests/qml/tst_calendar_m365.qml | 134 +++ ui/tests/qml/tst_m365_auth.qml | 669 ++++++++++++++ ui/tests/qml/tst_m365_client.qml | 306 +++++++ ui/tests/qml/tst_m365_sign_in.qml | 741 +++++++++++++++ ui/tests/qml/tst_m365_unified.qml | 104 +++ ui/tests/qml/tst_setup_pages.qml | 94 ++ ui/tests/test_calendar_feed.js | 6 + ui/tests/test_m365.js | 230 +++++ ui/tests/test_provider.js | 6 +- 95 files changed, 12341 insertions(+), 192 deletions(-) create mode 100644 docs/providers/m365.md create mode 100644 src/auth/credentials/m365.rs create mode 100644 src/providers/m365/actions.rs create mode 100644 src/providers/m365/cancel.rs create mode 100644 src/providers/m365/drafts.rs create mode 100644 src/providers/m365/graph.rs create mode 100644 src/providers/m365/ids.rs create mode 100644 src/providers/m365/mailbox.rs create mode 100644 src/providers/m365/mod.rs create mode 100644 src/providers/m365/model.rs create mode 100644 src/providers/m365/params.rs create mode 100644 src/providers/m365/read.rs create mode 100644 src/providers/m365/send.rs create mode 100644 src/providers/m365/signin.rs create mode 100644 src/providers/m365/tests.rs create mode 100644 src/providers/m365/tests/actions.rs create mode 100644 src/providers/m365/tests/drafts.rs create mode 100644 src/providers/m365/tests/mail.rs create mode 100644 src/providers/m365/tests/signin.rs create mode 100644 src/providers/m365/tests/unit.rs create mode 100644 src/providers/m365/tests/wire.rs create mode 100644 src/providers/m365/verify.rs create mode 100644 ui/components/M365SetupPage.qml create mode 100644 ui/components/SetupPages.qml create mode 100644 ui/providers/M365.js create mode 100644 ui/providers/M365Auth.qml create mode 100644 ui/providers/M365Client.qml create mode 100644 ui/providers/M365Protocol.js create mode 100644 ui/tests/oracles/providers/M365.js create mode 100644 ui/tests/qml/tst_calendar_m365.qml create mode 100644 ui/tests/qml/tst_m365_auth.qml create mode 100644 ui/tests/qml/tst_m365_client.qml create mode 100644 ui/tests/qml/tst_m365_sign_in.qml create mode 100644 ui/tests/qml/tst_m365_unified.qml create mode 100644 ui/tests/qml/tst_setup_pages.qml create mode 100644 ui/tests/test_m365.js diff --git a/AGENTS.md b/AGENTS.md index 36f329f..78b6b4e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,6 +11,7 @@ Read the relevant document before changing its area; these contain constraints t | HEY | [docs/providers/hey.md](docs/providers/hey.md) | | IMAP, SMTP and Proton Bridge | [docs/providers/imap.md](docs/providers/imap.md) | | JMAP discovery and streaming | [docs/providers/jmap.md](docs/providers/jmap.md) | +| Microsoft 365 over Graph | [docs/providers/m365.md](docs/providers/m365.md) | | Message HTML, images, direction and outgoing MIME | [docs/MESSAGE-RENDERING.md](docs/MESSAGE-RENDERING.md) | | Network requests, credentials, subprocesses and security reviews | [docs/SECURITY-BOUNDARIES.md](docs/SECURITY-BOUNDARIES.md) | | Backend APIs, binary pins and releases | [docs/BACKEND-RUNTIME.md](docs/BACKEND-RUNTIME.md) | diff --git a/Makefile b/Makefile index af42e3c..3beb913 100644 --- a/Makefile +++ b/Makefile @@ -10,11 +10,13 @@ QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryContr ui/cache/CacheStore.qml ui/cache/BodyCache.qml \ ui/providers/AuthManager.qml ui/providers/GmailApiClient.qml \ ui/providers/OutlookAuth.qml \ + ui/providers/M365Auth.qml ui/providers/M365Client.qml \ ui/providers/ImapAuth.qml ui/providers/ImapClient.qml \ ui/providers/HeyAuth.qml ui/providers/HeyClient.qml \ ui/providers/JmapAuth.qml ui/providers/JmapClient.qml ui/providers/JmapPush.qml \ ui/components/ImapSetupPage.qml \ ui/components/OutlookSetupPage.qml \ + ui/components/M365SetupPage.qml \ ui/components/JmapSetupPage.qml \ ui/components/HeySetupPage.qml \ ui/components/ProviderPicker.qml \ @@ -74,6 +76,7 @@ QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryContr ui/components/CalendarPalette.qml \ ui/components/ConfirmDeleteDialog.qml \ ui/components/SetupPage.qml \ + ui/components/SetupPages.qml \ ui/components/ShortcutHelp.qml \ ui/calendar/CalendarController.qml ui/calendar/CalendarCache.qml ui/calendar/CalendarReminders.qml ui/calendar/CalendarReminderInbox.qml \ ui/components/CalendarView.qml \ @@ -169,6 +172,7 @@ test-js: node ui/tests/test_aliases.js node ui/tests/test_menu.js node ui/tests/test_provider.js + node ui/tests/test_m365.js node ui/tests/test_imap.js node ui/tests/test_jmap.js node ui/tests/test_jmap_threads.js diff --git a/README.md b/README.md index 0243782..eb9132c 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ make app-run ## Features -- **Multiple mailboxes:** Gmail, Outlook, HEY, JMAP and IMAP/SMTP, including Fastmail, iCloud and self-hosted servers. +- **Multiple mailboxes:** Gmail, Outlook, Microsoft 365, HEY, JMAP and IMAP/SMTP, including Fastmail, iCloud and self-hosted servers. - **Mail and calendar:** read, search, compose, manage attachments and respond to meeting invitations. Available actions depend on your provider. - **Keyboard navigation:** `j`/`k` to move, `r` to reply, `c` to compose, `/` to search and `?` for all shortcuts. - **AI assistance in Omarchy:** ask about selected messages and review suggested drafts with Claude, OpenCode V2 or Codex. Follow your Omarchy default, or select an agent and optional model in Settings. An unsupported system default disables the AI action with a setup explanation. Conversations retain follow-ups and draft proposals; `/clear`, `/history` and `/diagnose` manage the chat. @@ -63,7 +63,7 @@ make app-run ## Add your mailbox -Choose a provider in Settings. Gmail needs a Google OAuth client; Outlook needs a Microsoft app registration. HEY uses the official [HEY CLI](https://github.com/basecamp/hey-cli). JMAP and IMAP usually use an app password or API token. +Choose a provider in Settings. Gmail needs a Google OAuth client; Outlook and Microsoft 365 need a Microsoft app registration. HEY uses the official [HEY CLI](https://github.com/basecamp/hey-cli). JMAP and IMAP usually use an app password or API token. See [mailbox setup](docs/MAILBOXES.md) for provider instructions and limitations, including Microsoft 365 and Proton Mail Bridge. diff --git a/app/src/resource_check.cpp b/app/src/resource_check.cpp index b5f3e7b..035e9bd 100644 --- a/app/src/resource_check.cpp +++ b/app/src/resource_check.cpp @@ -365,7 +365,7 @@ bool runSmokeTest(const ResourcePaths &paths, const QString &readyFile, QString || !protocol.isDouble() || protocol.toInt() != 1 || !apiVersion.isDouble() || apiVersion.toDouble() != static_cast(apiVersion.toInt()) - || apiVersion.toInt() != 7) { + || apiVersion.toInt() != 8) { if (error) *error = QStringLiteral("Bundled backend system.info does not match this app"); stopProcess(backend); return false; diff --git a/app/tests/native/tst_resources.cpp b/app/tests/native/tst_resources.cpp index ba9c30a..546242a 100644 --- a/app/tests/native/tst_resources.cpp +++ b/app/tests/native/tst_resources.cpp @@ -164,7 +164,7 @@ void ResourcesTest::smokeTestLoadsQmlHandshakesAndWritesReadyFile() QVERIFY(ready.open(QIODevice::ReadOnly)); const QJsonObject value = QJsonDocument::fromJson(ready.readAll()).object(); QCOMPARE(value.value(QStringLiteral("version")).toString(), QStringLiteral(OMAMAIL_APP_VERSION)); - QCOMPARE(value.value(QStringLiteral("apiVersion")).toInt(), 7); + QCOMPARE(value.value(QStringLiteral("apiVersion")).toInt(), 8); } void ResourcesTest::smokeRejectsMismatchedBackendApi() @@ -344,7 +344,7 @@ int main(int argc, char *argv[]) result = {{QStringLiteral("name"), QStringLiteral("omamail")}, {QStringLiteral("version"), QStringLiteral(OMAMAIL_APP_VERSION)}, {QStringLiteral("protocol"), 1}, - {QStringLiteral("apiVersion"), fixture == QByteArrayLiteral("stale-api") ? 6 : 7}}; + {QStringLiteral("apiVersion"), fixture == QByteArrayLiteral("stale-api") ? 7 : 8}}; } else { result = {{QStringLiteral("quitReady"), true}}; } diff --git a/app/tests/qml/tst_standalone_composition.qml b/app/tests/qml/tst_standalone_composition.qml index 5551765..e2cfac8 100644 --- a/app/tests/qml/tst_standalone_composition.qml +++ b/app/tests/qml/tst_standalone_composition.qml @@ -300,7 +300,7 @@ TestCase { compare(composition.service.settings.maxMessages, 17) compare(composition.service.notifyNewMail, false) compare(composition.service.backendRuntime.bundled, true) - compare(composition.service.backendRuntime.requiredApiVersion, 6, + compare(composition.service.backendRuntime.requiredApiVersion, 7, "the bundled handshake must require this checkout's API") compare(composition.service.backendRuntime.canInstall, false) compare(composition.service.backendRuntime.executable, host.backendPath) diff --git a/backend-api.json b/backend-api.json index 24bdb28..4d8f99d 100644 --- a/backend-api.json +++ b/backend-api.json @@ -1,5 +1,5 @@ { - "apiVersion": 7, + "apiVersion": 8, "releasedApiVersion": 7, "protocolVersion": 1, "methods": [ @@ -183,7 +183,24 @@ "jmap.request", "jmap.stream.open", "jmap.stream.poll", - "jmap.stream.close" + "jmap.stream.close", + "m365.signInBegin", + "m365.signInPoll", + "m365.signOut", + "m365.verify", + "m365.list", + "m365.messages", + "m365.read", + "m365.attachment", + "m365.modify", + "m365.trash", + "m365.untrash", + "m365.send", + "m365.saveDraft", + "m365.deleteDraft", + "m365.sendAs", + "m365.invalidate", + "m365.cancel" ], "contractCases": [ { @@ -1287,10 +1304,208 @@ "types": { "ticket": "string" } + }, + { + "name": "Microsoft 365 provider rules", + "method": "providers.snapshot", + "params": {}, + "equals": { + "m365.nativeSync": true, + "m365.capabilities.archive": true, + "m365.capabilities.spam": false, + "m365.capabilities.labels": false, + "m365.queries.archive": "role:archive", + "m365.queries.starred": "role:inbox flagged" + } + }, + { + "name": "Microsoft 365 text search is a text query", + "method": "providers.resolve", + "params": { + "provider": "m365", + "operation": "query", + "search": "invoice" + }, + "equals": { + "value": "text:invoice" + } + }, + { + "name": "Microsoft 365 answers one sender identity without a request", + "method": "m365.sendAs", + "params": { + "accountId": "m365:sender@example.org" + }, + "equals": { + "0.email": "sender@example.org", + "0.isDefault": true + } + }, + { + "name": "Microsoft 365 verify names a mailbox that is not saved", + "method": "m365.verify", + "params": { + "accountId": "m365:absent@example.org" + }, + "errorCode": -32000, + "equals": { + "message": "auth_account_missing" + } + }, + { + "name": "Microsoft 365 list refuses a forged page token", + "method": "m365.list", + "params": { + "accountId": "m365:sender@example.org", + "query": "role:inbox", + "limit": 5, + "pageToken": "forged" + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } + }, + { + "name": "Microsoft 365 messages refuse an id that is not its own", + "method": "m365.messages", + "params": { + "accountId": "m365:sender@example.org", + "ids": [ + "1:INBOX" + ], + "full": false + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } + }, + { + "name": "Microsoft 365 send refuses a body that is not canonical base64url", + "method": "m365.send", + "params": { + "accountId": "m365:sender@example.org", + "raw": "not base64url!" + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } + }, + { + "name": "Microsoft 365 sign-in needs an application id", + "method": "m365.signInBegin", + "params": { + "accountId": "m365:sender@example.org", + "address": "sender@example.org" + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } + }, + { + "name": "mail action rejects malformed Microsoft 365 IDs before preview", + "method": "mail.act", + "params": { + "account": "m365:sender@example.org", + "operation": "read", + "ids": [ + "1:INBOX" + ] + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } + }, + { + "name": "Microsoft 365 saves a draft only from canonical base64url", + "method": "m365.saveDraft", + "params": { + "accountId": "m365:sender@example.org", + "raw": "not base64url!" + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } + }, + { + "name": "Microsoft 365 deletes a draft only by an id of its own", + "method": "m365.deleteDraft", + "params": { + "accountId": "m365:sender@example.org", + "id": "1:Drafts" + }, + "errorCode": -32602, + "equals": { + "message": "Invalid params" + } + }, + { + "name": "Microsoft 365 calendar discovery asks the mailbox's own sign-in", + "method": "calendar.discover", + "params": { + "accountId": "m365:absent@example.org" + }, + "errorCode": -32000, + "equals": { + "message": "auth_account_missing" + } + }, + { + "name": "outbox accepts the Microsoft 365 provider", + "method": "outbox.enqueue", + "params": { + "accountId": "m365:sender@example.org", + "provider": "m365", + "payload": { + "raw": "eA", + "unexpected": true + } + }, + "errorCode": -32000, + "equals": { + "message": "outbox_invalid_payload" + } } ], "unreleased": { - "methods": [], - "cases": [] + "methods": [ + "m365.signInBegin", + "m365.signInPoll", + "m365.signOut", + "m365.verify", + "m365.list", + "m365.messages", + "m365.read", + "m365.attachment", + "m365.modify", + "m365.trash", + "m365.untrash", + "m365.send", + "m365.saveDraft", + "m365.deleteDraft", + "m365.sendAs", + "m365.invalidate", + "m365.cancel" + ], + "cases": [ + "Microsoft 365 provider rules", + "Microsoft 365 text search is a text query", + "Microsoft 365 answers one sender identity without a request", + "Microsoft 365 verify names a mailbox that is not saved", + "Microsoft 365 list refuses a forged page token", + "Microsoft 365 messages refuse an id that is not its own", + "Microsoft 365 send refuses a body that is not canonical base64url", + "Microsoft 365 sign-in needs an application id", + "mail action rejects malformed Microsoft 365 IDs before preview", + "Microsoft 365 saves a draft only from canonical base64url", + "Microsoft 365 deletes a draft only by an id of its own", + "Microsoft 365 calendar discovery asks the mailbox's own sign-in", + "outbox accepts the Microsoft 365 provider" + ] } } diff --git a/docs/BACKEND-RUNTIME.md b/docs/BACKEND-RUNTIME.md index 47266a5..f27b2af 100644 --- a/docs/BACKEND-RUNTIME.md +++ b/docs/BACKEND-RUNTIME.md @@ -108,7 +108,7 @@ An IMAP or Outlook account entry may carry a top-level `senderName`. `mail.send` ## API 6 account-wide IMAP search -IMAP and Outlook text searches, and IMAP address searches, resolve to `search:`. IMAP All mail resolves to `search:ALL`; folder queries remain folder-scoped. The IMAP UI keeps Archive on API 5 and exposes All mail starting at API 6. +IMAP and Outlook text searches, and IMAP address searches, resolve to `search:`. IMAP All mail resolves to `search:ALL`; folder queries remain folder-scoped. The IMAP UI keeps Archive on API 5 and exposes All mail starting at API 6. Microsoft 365 text searches resolve to `text:` (see its API 8 section below). `imap.list` and `imap.listContinue` return an empty `page` plus an opaque work `continuation` until the scan and page assembly have finished. Continue with the same account, query, request token and original `pageToken`, including when a later page needs a new scan. Partial scans are not a globally newest-first page. Expired or mismatched work continuations return `imap_search_expired`; refresh starts a new search. @@ -120,6 +120,12 @@ Each call advances at most four scan or page-verification folders, with at most Search includes aggregate All Mail destinations and excludes Trash/Junk membership from aggregate results. Cross-folder copies are collapsed using server-provided `EMAILID`/`X-GM-MSGID`. A generic server that advertises `\All` but no server identity is searched through its `\All` mailbox alone, because RFC 6154 defines it as every message in the store; scanning physical folders as well would only add copies that cannot be matched without reading them. Its Message-ID header and RFC822.SIZE nominate possible overlaps between All and Trash/Junk, then SHA-256 of complete message bytes verifies those candidates before excluding any aggregate result. Verification reads at most 64 candidates and 8 MiB per folder step with `BODY.PEEK[]` and retains only digests; a candidate larger than 8 MiB is not read and stays in the results. A server with neither `\All` nor a server identity preserves folder/UID copies, so overlapping folders may produce duplicate rows. A sender-written Message-ID, headers or size alone never suppress a result. Copies within one physical folder remain distinct. `mail.list` follows search continuations for at most 120 seconds; other IMAP listings still accept one continuation. +## Microsoft 365 mailboxes (API 8) + +The `m365.*` methods are the unreleased API 8 step: `m365.signInBegin`, `m365.signInPoll`, `m365.signOut`, `m365.verify`, `m365.list`, `m365.messages`, `m365.read`, `m365.attachment`, `m365.modify`, `m365.trash`, `m365.untrash`, `m365.send`, `m365.saveDraft`, `m365.deleteDraft`, `m365.sendAs`, `m365.invalidate` and `m365.cancel`. A feature gates on API 8 or newer; an older backend answers `backend_needs_update`. + +Queries are `role: [unseen] [flagged]` or `text:`, which Graph's `$search` reads as words (a text search leaves Trash and Junk out); a Flagged or Unread view lists the Inbox only. `m365.signInBegin` and `m365.signOut` take the Application (client) ID as `clientId`: the backend has no default. `m365.signInBegin` signs in at the tenant the address's domain names and, if Microsoft does not know the domain as a tenant, once more at `organizations`; refreshes ask the same way. `m365.signInPoll` asks Microsoft at most once per interval whatever its caller's rhythm, answers `pending`, `signedIn`, `declined`, `expired` or `failed`, and has stored the refresh token in the keyring before it says `signedIn`; no token or device code is ever returned. A poll whose token request or keyring save outlasts its 24 seconds answers `pending` with the warning `m365_exchange_running` while the exchange goes on, and the same `requestId` answers the outcome afterwards. `m365.signOut` ends the sign-ins under way of that account and `clientId` only. It and `m365.invalidate` wait at most 20 seconds for the mailbox's refresh lane, which a refresh holds while its keyring save waits to be unlocked, and then answer `m365_timeout`: a sign-out so answered may not have been done. `m365.list` answers `{page, rows}` (the rows are the header-only resources of the page, which `m365.messages` serves from memory for a minute) and `m365.cancel` ends a call made with its `requestToken`. `m365.saveDraft {accountId, raw, draftId?}` answers `{saved, warning, id}` like `imap.saveDraft` (plus the new draft's `id`), or fails with `m365_draft_unconfirmed` when Graph may have made the draft without saying so, and `m365.deleteDraft {accountId, id}` answers `{}` or refuses anything that is not a draft with `m365_not_a_draft`. Calendar requests for an `m365:` account take their Graph token from the same sign-in. + ## Released and unreleased: one step ahead of the pin Backends ship in batches, not per merge, so `main` may implement an API the pinned diff --git a/docs/BACKEND.md b/docs/BACKEND.md index cc7a588..9798ea3 100644 --- a/docs/BACKEND.md +++ b/docs/BACKEND.md @@ -70,7 +70,7 @@ Example request: Replies contain `jsonrpc: "2.0"`, the original `id`, and either `result` or an `error` object with a numeric code and static message. Clients should use string IDs to avoid QML number precision issues. Notifications omit `id` and receive no response, including on method failures. Explicit null IDs receive responses. Batches contain at most 128 entries and return only non-notification responses. Invalid envelopes return null IDs; unknown fields and duplicate envelope keys are refused. Frames are bounded to 1 MiB including the newline. Oversized or unterminated frames return an error and end the stream. Invalid complete frames allow the next request. Errors never include input bytes. -A Tokio runtime runs up to 32 concurrent frame futures behind a bounded 16-frame queue. Gmail uses a shared native reqwest/rustls connection pool, with asynchronous HTTP and Hickory DNS resolution, verified TLS, fixed Google HTTPS origins, no redirects or environment proxies, and a streamed 16 MiB response ceiling. OAuth refresh is coalesced per account; one account's refresh does not block another account. HTTP has a 10-second connection timeout and a 20-second whole-request timeout. Gmail IPC requests have a 25-second deadline including queue time. Expired queued frames never start domain operations. Gmail meters each user at 250 quota units per second and answers a burst past that with HTTP 403 `rateLimitExceeded` (or 429); the session paces every call per account at 200 units per second by its documented cost, so trashing a screen of conversations — one 5-unit call per message — streams out instead of failing, and a request Gmail still refuses for rate limiting is resent with exponential backoff (1 s, 2 s, 4 s plus jitter) inside its 20-second budget. A refused request never ran, so this applies to mutations too; a timed-out mutation is still never retried. Message mutations — `gmail.modify`, `gmail.batchModify`, `gmail.trash`, `gmail.untrash` — do not hold the request for any of that: they answer at once with `{"queued":true,"ticket":"…"}` and join an in-memory per-account queue (`src/providers/gmail_queue.rs`) that one task sends in order, paced by the same bucket, waiting a further 5, 10, 20 and 30 seconds when Gmail keeps refusing for rate limiting before giving up. The outcome is announced as a `gmail.settled` notification `{accountId, ticket, method, ok, error}`; `GmailApiClient.qml` keeps the request in flight until then and hands the outcome to the original callback, so the optimistic edit stays on screen and nothing is shown unless the queue finally fails. The CLI's `mail.act` waits on the ticket in process. Logout refuses what the queue has not yet sent; a send already on the wire settles on its real answer. Nothing in the queue survives the process. +A Tokio runtime runs up to 32 concurrent frame futures behind a bounded 16-frame queue. Gmail uses a shared native reqwest/rustls connection pool, with asynchronous HTTP and Hickory DNS resolution, verified TLS, fixed Google HTTPS origins, no redirects or environment proxies, and a streamed 16 MiB response ceiling. OAuth refresh is coalesced per account; one account's refresh does not block another account. HTTP has a 10-second connection timeout and a 20-second whole-request timeout. Gmail IPC requests have a 25-second deadline including queue time; a frame that reads a Microsoft 365 message body (`m365.read`, `m365.attachment`, `m365.messages` with `full`, `reader.open` of an `m365:` mailbox) may wait 85 seconds, since the provider gives the body a minute. Expired queued frames never start domain operations. Gmail meters each user at 250 quota units per second and answers a burst past that with HTTP 403 `rateLimitExceeded` (or 429); the session paces every call per account at 200 units per second by its documented cost, so trashing a screen of conversations — one 5-unit call per message — streams out instead of failing, and a request Gmail still refuses for rate limiting is resent with exponential backoff (1 s, 2 s, 4 s plus jitter) inside its 20-second budget. A refused request never ran, so this applies to mutations too; a timed-out mutation is still never retried. Message mutations — `gmail.modify`, `gmail.batchModify`, `gmail.trash`, `gmail.untrash` — do not hold the request for any of that: they answer at once with `{"queued":true,"ticket":"…"}` and join an in-memory per-account queue (`src/providers/gmail_queue.rs`) that one task sends in order, paced by the same bucket, waiting a further 5, 10, 20 and 30 seconds when Gmail keeps refusing for rate limiting before giving up. The outcome is announced as a `gmail.settled` notification `{accountId, ticket, method, ok, error}`; `GmailApiClient.qml` keeps the request in flight until then and hands the outcome to the original callback, so the optimistic edit stays on screen and nothing is shown unless the queue finally fails. The CLI's `mail.act` waits on the ticket in process. Logout refuses what the queue has not yet sent; a send already on the wire settles on its real answer. Nothing in the queue survives the process. Every TLS connection the backend opens — Gmail, JMAP, Microsoft, calendars, public HTTP and IMAP/SMTP — verifies the peer against the bundled Mozilla list plus the operating system's certificate store (`/etc/ssl/certs`, or `SSL_CERT_FILE` and `SSL_CERT_DIR` when set), so a mail server behind a private authority the system trusts is reachable, as it was under curl before the backend existed. reqwest's `rustls-tls-native-roots` feature does this for HTTPS; `src/tls/` builds the same union once for the tokio-rustls connections IMAP and SMTP make. An entry in the system store that rustls cannot parse is skipped, not fatal. @@ -79,7 +79,8 @@ the runtime has two async workers and at most eight blocking workers. The official HEY child processes use asynchronous pipes, output limits, deadlines and process-group cleanup. Other native providers enforce their own request deadlines. The 25-second RPC timeout currently wraps Gmail operations; expired -queued requests for every method are rejected before dispatch. An operation +queued requests for every method are rejected before dispatch (after 85 seconds +for a Microsoft 365 body read, 25 for everything else). An operation that has already submitted a mutation must not be automatically retried: a lost reply does not establish whether the provider applied it. @@ -182,8 +183,10 @@ multiple message IDs. Their previews retain `requestedIds` and show the resolved `targetIds`, including expanded conversation members where applicable. The complete ID list is validated against the provider's syntax before any provider lookup or mutation chunk. HEY uses numeric `posting:topic` IDs; -IMAP/Outlook use a positive 32-bit `UID:folder`. Validation never rewrites ID -spelling, folder bytes, or leading zeroes. A malformed later ID refuses the +IMAP/Outlook use a positive 32-bit `UID:folder`; Microsoft 365 uses the +lower-case base32 of Graph's immutable message id (8 to 156 bytes before +encoding). Validation never rewrites ID spelling, folder bytes, or leading +zeroes. A malformed later ID refuses the whole request without executing earlier valid IDs. Every state-changing command is a dry run unless `--execute` is present. @@ -197,7 +200,9 @@ must obtain any approval its own workflow requires before setting it. Archive and trash previews for IMAP and Outlook read the server's current LIST/SPECIAL-USE folders. Missing destinations are refused, and execution uses -the same discovered folder names without another destination lookup. +the same discovered folder names without another destination lookup. Microsoft +365 previews of the same actions read Graph's well-known folders; a token they +refresh does not save a rotated refresh token, and other actions ask nothing. `send` reads a UTF-8 body from stdin, up to 16 MiB. There is no body argument. Repeat `--to`, `--cc`, `--bcc`, or `--attach` as needed. Attachments require @@ -251,6 +256,7 @@ the seven root commands. | IMAP / SMTP | Async DNS, verified TLS/STARTTLS, authenticated connection reuse, bounded octet-aware literals, special-use folders, UID search windows and continuations, counts, MIME reads, attachment extraction, mutations, folder changes, drafts and submission. A dedicated INBOX IDLE connection per watched account wakes the background check. Rust validates the SMTP envelope and preserves confirmed-send results if filing the Sent copy fails. | `ImapClient.qml` is a presentation adapter using account-bound domain requests. Setup presentation remains in QML; cached-query interpretation runs in Rust, and old wire helpers remain as test references. | | Outlook | Native Microsoft OAuth and Graph sending; native IMAP handles mailbox reads with XOAUTH2. | Browser/device authorization still requires the user's interaction. | | JMAP | HTTPS session discovery and requests, bounded SSE parsing/reconnection, native query/mailbox/read/mutation/resource modules and background checks. Resource conversion preserves MIME depth bounds, charset and conversation-count semantics. | `JmapClient.qml` and `JmapPush.qml` pass account-bound domain requests and display returned state. Legacy JS helpers remain for presentation/configuration and parity tests. | +| Microsoft 365 | Device-code sign-in held in Rust with the refresh token in the keyring; Graph list, header rows, MIME reads up to 24 MiB, attachments, read/flag/archive/trash changes, drafts, `sendMail` and the background check, over one fixed host with bounded responses, deadlines and throttling waits. | `M365Client.qml` and `M365Auth.qml` pass account-bound requests and show the sign-in code; the calendar reads the same Graph token through Rust. | | HEY | Async execution of the official client, account identity checks, queries/paging, resource conversion, HTML feature negotiation, profile/labels/send-as, login lifecycle, supported actions, sends and drafts. | `hey` owns its OAuth credential and supported service interface. No private HEY endpoints are accessed and no unavailable capability is invented. | | Authentication | Native Google/Microsoft token networking, Google loopback callback, credential lookup/store/clear, fixed credential destinations and account-bound tokens. Gmail first-login identity is verified and its refresh token stored before the UI receives success. | Keyring access uses the system credential service; opening the browser remains a desktop interaction. | | Calendar | Native Google/Microsoft and CalDAV requests and pagination, Microsoft/iCloud account-calendar discovery, and origin checks before CalDAV credential access. | Calendar presentation, iCalendar interpretation and compose/RSVP state remain in UI modules where not explicitly migrated. | @@ -398,16 +404,17 @@ Preloading issues read-only requests and never marks messages read. `j`/`k` navigation opens the selected message explicitly, so its normal mark-read behavior is separate from preloading. -All five providers have native check paths: Gmail reads an unread query and a +All six providers have native check paths: Gmail reads an unread query and a small metadata sample; HEY uses its official unread listing; JMAP performs -`Email/query` and `Email/get`; IMAP and Outlook read mailbox status. The last +`Email/query` and `Email/get`; Microsoft 365 reads the Inbox's folder counts and +the newest unread rows over Graph; IMAP and Outlook read mailbox status. The last pair currently provide counts/fingerprints without message preview samples. The backend publishes `mail.updated` notifications containing the account, sequence, timestamp, count, available message samples and static error state. A failure preserves the last successful data. Native fingerprints also detect changed message samples when the count stays equal. The service updates desktop state from these events instead of using an open reader's timer to fetch mail. -Checks run on the watch interval. IMAP and Outlook watches also keep one dedicated connection per account idling on INBOX (RFC 2177). It is opened with `EXAMINE`, so watching never clears `\Recent`, and an untagged `EXISTS`, `EXPUNGE` or `FETCH` wakes the account's check instead of waiting for the interval; further changes within two seconds of the first wake it once. IDLE is ended and issued again every four minutes, which keeps a quiet connection alive through middleboxes and notices one that died silently; a wall-clock jump after the machine sleeps ends the session at once. A failed session reconnects after five seconds, doubling to five minutes with some jitter, reset once a session completes a refresh; a failed login or unavailable credential waits the full five minutes. Every session after the first asks for one check, because a change during the gap raised nothing. A server that ends IDLE on its own counts as a failed session rather than being idled again at once. Only a server that does not advertise IDLE ends the attempt, for as long as the watch lasts. Replacing, removing or shutting down the watch closes the connection, and polling continues in every case. JMAP's separate event stream supplies its push support; Gmail and HEY remain periodic. +Checks run on the watch interval. IMAP and Outlook watches also keep one dedicated connection per account idling on INBOX (RFC 2177). It is opened with `EXAMINE`, so watching never clears `\Recent`, and an untagged `EXISTS`, `EXPUNGE` or `FETCH` wakes the account's check instead of waiting for the interval; further changes within two seconds of the first wake it once. IDLE is ended and issued again every four minutes, which keeps a quiet connection alive through middleboxes and notices one that died silently; a wall-clock jump after the machine sleeps ends the session at once. A failed session reconnects after five seconds, doubling to five minutes with some jitter, reset once a session completes a refresh; a failed login or unavailable credential waits the full five minutes. Every session after the first asks for one check, because a change during the gap raised nothing. A server that ends IDLE on its own counts as a failed session rather than being idled again at once. Only a server that does not advertise IDLE ends the attempt, for as long as the watch lasts. Replacing, removing or shutting down the watch closes the connection, and polling continues in every case. JMAP's separate event stream supplies its push support; Gmail, HEY and Microsoft 365 (Graph change notifications need a public endpoint) remain periodic. The backend also observes account-registry revisions and publishes `accounts.changed` without including account settings or credentials. Shutdown diff --git a/docs/MAILBOXES.md b/docs/MAILBOXES.md index b64b472..5d2e04d 100644 --- a/docs/MAILBOXES.md +++ b/docs/MAILBOXES.md @@ -32,10 +32,21 @@ and only then is the IMAP row worth removing. **Outlook** signs in on Microsoft's own page and uses [Microsoft's supported OAuth route for IMAP and SMTP][microsoft-mail-oauth]. It works with Outlook.com, Hotmail, Live and MSN accounts; Omamail never asks for the Microsoft account password. Until Omamail ships a maintainer-owned public client, the setup page asks for an Application (client) ID from a one-time Microsoft Entra app registration. Make it a public client for personal Microsoft accounts; the sign-in asks for `IMAP.AccessAsUser.All`, `SMTP.Send`, `offline_access` and `openid` and shows the device code to enter in the Microsoft page it opens. Microsoft Graph — sending where the tenant has SMTP off, the calendar — is a second code for `Mail.Send` and `Calendars.ReadWrite`, asked for once, when Microsoft refuses the Graph exchange for want of consent: straight after the sign-in where the mailbox sends through Graph, else from the mailbox's settings (*Allow Microsoft Graph...*). -A **work or school** mailbox (Microsoft 365) is the same sign-in addressed to its own tenant: turn on *Work or school account* on the setup page, and register the client in that tenant, or as multi-tenant. Where the tenant has switched authenticated SMTP off — the common Microsoft 365 default, which fails a send with "SmtpClientAuthentication is disabled" — turn on *Send through Microsoft Graph*: the same message goes to Graph's `sendMail` with a token of Graph's own audience, obtained with the same refresh token, and Graph files the sent copy itself. That needs the `Mail.Send` permission on the registration. +A **work or school** mailbox (Microsoft 365) is the same sign-in addressed to its own tenant: turn on *Work or school account* on the setup page, and register the client in that tenant, or as multi-tenant. Where the tenant has switched authenticated SMTP off — the common Microsoft 365 default, which fails a send with "SmtpClientAuthentication is disabled" — turn on *Send through Microsoft Graph*: the same message goes to Graph's `sendMail` with a token of Graph's own audience, obtained with the same refresh token, and Graph files the sent copy itself. That needs the `Mail.Send` permission on the registration. Where the organization has turned IMAP off, or for one sign-in for mail and calendar instead of an IMAP one and a second code for Graph, choose **Microsoft 365** below instead. Before signing in, enable IMAP in Outlook.com: **Settings > Mail > Forwarding and IMAP > Let devices and apps use IMAP**, then save. Microsoft disables IMAP by default; OAuth consent alone does not enable mailbox access. See [Microsoft's IMAP setup instructions](https://support.microsoft.com/en-us/outlook/pop-imap-and-smtp-settings-for-outlook-com). +**Microsoft 365** is a work or school mailbox on Exchange Online, read and written through Microsoft Graph with one sign-in for mail and calendar: no IMAP, no SMTP and no second code. Choose it over Outlook for a work or school mailbox whose organization has turned IMAP off, or to have one sign-in rather than two. Microsoft issues tokens only to registered applications and Omamail has none of its own, so you register one once in [Microsoft Entra][entra-apps]: + +1. Choose *New registration*. Under *Supported account types* keep *Accounts in this organizational directory only (Single tenant)*, the portal's default, or pick *Accounts in any organizational directory (Multitenant)*. Leave the redirect URI empty: the sign-in uses a device code, which needs none. +2. Under *Authentication*, set *Allow public client flows* to *Yes*. +3. Under *API permissions*, add the delegated Microsoft Graph permissions `Mail.ReadWrite`, `Mail.Send`, `Calendars.ReadWrite`, `openid`, `profile`, `email` and `offline_access`. +4. Copy the *Application (client) ID* from the app's *Overview* page into the field on the setup page. + +Then choose **Add mailbox**, **Microsoft 365**, enter the work address and the Application (client) ID and press **Sign in with Microsoft...**. The window shows a code and opens Microsoft's page; enter the code there and sign in with the work account. Omamail signs in at the organization whose domain is the one after the `@` of your address, which is what a single-tenant registration needs; where that domain is not a domain of the organization (a vanity address, say), it asks once more without naming one, which only a multitenant registration answers. The account you sign in as may have another name than your address (an alias or a hybrid account): it is accepted when its email, preferred user name or UPN is the address you typed. If your organization does not let people approve apps themselves, an administrator has to approve the registration first (*Grant admin consent* on its API permissions page). A browser already signed in to another Microsoft account may offer that one: open the page in a private window instead, using the address shown beside the code. + +The rail is Outlook's: Inbox, Unread, Flagged, Drafts, Sent and, where the mailbox has the folders, Archive, Junk and Trash; Unread and Flagged list the Inbox only. Archive moves a message out of the Inbox into the Archive folder. What Microsoft 365 does not have here, the panel does not offer: no labels or folders of your own, no conversations and no "report spam". A saved draft goes to the mailbox's Drafts folder; one opened from there replaces its old copy when it is saved again, and is deleted once it is sent. Microsoft files the sent copy itself, and the calendar is the Microsoft one. Signing out, or removing the mailbox once you have confirmed it, deletes the saved sign-in from the keyring. + **HEY** needs no address and no password. HEY publishes no IMAP, no POP and no public API, so Omamail reads it through the [HEY CLI][hey-cli] client 37signals ship for exactly this — which means the sign-in, the token and the keyring entry @@ -121,4 +132,5 @@ and the mailto desktop file are yours to add and yours to remove. [Back to installation](../README.md#install) [hey-cli]: https://github.com/basecamp/hey-cli +[entra-apps]: https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade [microsoft-mail-oauth]: https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth diff --git a/docs/SPEC.md b/docs/SPEC.md index 540818e..6704e8a 100644 --- a/docs/SPEC.md +++ b/docs/SPEC.md @@ -20,6 +20,7 @@ Three plugin entry points (`manifest.kinds`): | Question | Decision | |---|---| | JMAP | **A fourth provider, after Gmail and HEY and ahead of IMAP.** "Any server that speaks JMAP": a protocol rather than a service, so there is no brand to draw and a mailbox here takes the themed envelope IMAP does, and no web front door either to link to or to open a message in. Signing in is an address and an app password or an API token — the auth scheme detected rather than asked, the server discovered from the address's domain unless one is typed — and the account id is `jmap:
`. It goes in front of IMAP because a server that speaks both is better read over JMAP: the server's own thread ids, a listing that collapses to conversations, a junk verb where the session proves the server learns from one, whole-account search, and one round trip for a batch. **A provider's capability list is a ceiling, not a promise.** An account may refuse one of them from what its own session and mailbox roles say, and `Registry.can` weighs those refusals beside the ceiling: a server with no Archive mailbox loses the Archive rail row, the archive button and the `e` hint, and says why rather than failing after the row has already moved. The transport is `scripts/jmap-transport.sh`, one curl script beside the IMAP one, and every signed-in account holds an event stream open whether or not the window is — a change on the server reaches the panel within about a second, with the two-minute poll left as a backstop. | +| Microsoft 365 | **A work or school mailbox on Exchange Online over Microsoft Graph, with one device-code sign-in for mail and calendar; after Outlook in the chooser.** Microsoft refuses a device code that names two resources, so the sign-in asks only Graph scopes (`Mail.ReadWrite`, `Mail.Send`, `Calendars.ReadWrite`, and `openid`, `profile`, `email` and `offline_access` for the sign-in itself) and its one token serves mail and the existing Graph calendar code: no IMAP, no SMTP, no second code. Omamail ships no registration of its own (Outlook's built-in ID is another registration's), so the setup page asks for the Application (client) ID of a public client the person registers once in Microsoft Entra and says how. The account id is `m365:
`; the refresh token lives in the keyring and the access token in the backend's memory, and no token or device code is in QML or `accounts.json` (the entry's `clientId` is a name, not a secret). **The ceiling is `archive`, `star`, `batch`, `search` and `send`.** The rail is Outlook's; Archive, Junk and Trash show when the mailbox has the folder. No labels, folders of your own, conversations or report-spam; drafts are kept in the mailbox's Drafts folder. The page warns that a browser signed in to another Microsoft account may offer that one and advises a private window. | | List granularity | **One row per conversation where the provider declares `conversations`, one row per message everywhere else.** Having a server-side thread id is not the same question as whether the listing collapses, which is why `conversations` is a capability of its own beside `threads`. Gmail has thread ids and still lists messages (`messages.list`): aggregating them costs an extra `threads.get` round trip per page and doubles the UI states. That objection does not apply to JMAP, where the query, the ids and the members arrive in one chained POST and a collapsed listing therefore costs no extra round trip at all; HEY's rows have always been conversations, since `hey threads` answers one entry per topic. A row above the seam stands for a conversation on those two and for a message on Gmail and IMAP, and the reader draws a rail of the members when there is more than one. | | Body rendering | **Three readings of one message, and reading mode is the default.** Reading mode discards the sender's presentation and rebuilds the message out of what it says — paragraphs, headings, lists, quotes, links, small data tables — in this window's type at a bounded measure; nothing but text, a checked `href`, a checked image source, and bounded numeric image dimensions crosses from the sender's document, so no sender markup reaches Qt at all. Original is the sender's own layout through Qt RichText, sanitised, kept for the receipts and tables whose layout is carrying something. Plain is the text. All three are built from one parse when the body arrives, so choosing between them costs neither a fetch nor a reparse. Remote images are blocked in every one of them until the reader asks; approved visible images are then fetched without redirects and with byte/time limits, and Qt receives only completed `data:` URIs, never pending remote sources or loading placeholders. The fetch still reports the read to the image host. A source aimed at loopback, a private address or a local file is never fetched at all. No browser engine: `QtWebEngineQuick::initialize()` must run before the host process's `QGuiApplication` is constructed, which a plugin loaded later cannot do. | | Sending | **Included.** Reply, reply-all, forward, and compose, plain-text body with quoted original. Requires the `gmail.send` scope. | @@ -56,6 +57,8 @@ Outlook.com and Hotmail use Microsoft's OAuth device-code flow for delegated IMA - Application ID → the account entry; it is public configuration and there is no client secret - Access token → process memory, handed to curl over stdin and used through its OAuth bearer option +Microsoft 365 uses the same device-code flow with Graph scopes only, so one code serves mail and calendar. The backend holds the flow: the user supplies the Application (client) ID of a public client, there is no client secret, the refresh token goes to the keyring keyed by account and client (the ID in lower case, however it was typed), and the access token stays in the backend's memory. The sign-in names the tenant after the domain of the typed address and retries once with `organizations` when Microsoft does not know that domain. + ## Features **Ship in v1** diff --git a/docs/providers/README.md b/docs/providers/README.md index ed25177..4f0b287 100644 --- a/docs/providers/README.md +++ b/docs/providers/README.md @@ -2,7 +2,7 @@ Required when changing provider adapters, account identity, capabilities or provider-dependent UI. -- Keep provider differences in descriptors/adapters and the registry, not higher-level views. Chooser order is Gmail, Outlook, HEY, JMAP, IMAP; prefer the specific protocol before the catch-all. +- Keep provider differences in descriptors/adapters and the registry, not higher-level views. Chooser order is Gmail, Outlook, Microsoft 365, HEY, JMAP, IMAP; prefer the specific protocol before the catch-all. - Auth and fetching adapters expose the same methods and callback shapes to `MailAccount`. - Every client returns Gmail's message-resource shape: headers, MIME tree and base64url bodies. RFC 822 adapters use `Message.parseRfc822`; HEY constructs the same shape from postings and topics. Conversation listings add the `thread` block consumed by `Message.threadOf`. - Detail reads update only fields they carry (`Model.detailSummary`); absence must not erase listing metadata. @@ -12,4 +12,4 @@ Required when changing provider adapters, account identity, capabilities or prov - Resolve web locations through `Registry.webMessageUrl` / `webBoxUrl`. - `Registry.mark` supplies the square icon and `logo` the lockup; preserve official artwork colors. Unbranded providers use the themed envelope. -Read the provider-specific [HEY](hey.md), [IMAP](imap.md) or [JMAP](jmap.md) notes when applicable, and [security boundaries](../SECURITY-BOUNDARIES.md) before touching transport or authentication. +Read the provider-specific [HEY](hey.md), [IMAP](imap.md), [JMAP](jmap.md) or [Microsoft 365](m365.md) notes when applicable, and [security boundaries](../SECURITY-BOUNDARIES.md) before touching transport or authentication. diff --git a/docs/providers/m365.md b/docs/providers/m365.md new file mode 100644 index 0000000..a1e3fc4 --- /dev/null +++ b/docs/providers/m365.md @@ -0,0 +1,21 @@ +# Microsoft 365 + +Required when touching `src/providers/m365`, its sign-in, its Graph requests or the Graph token that `calendar.request` and `calendar.discover` borrow. + +- A work or school mailbox over Microsoft Graph (`https://graph.microsoft.com/v1.0/me`), mail and calendar behind one device-code sign-in that asks only Graph scopes (`Mail.ReadWrite`, `Mail.Send`, `Calendars.ReadWrite`, plus `offline_access`, `openid`, `profile` and `email`). Microsoft refuses one code that names two resources, so mail never goes through IMAP and SMTP here. There is no built-in Application (client) ID: the person registers a public client (device flow allowed, no redirect URI) and `m365.signInBegin` and `m365.signOut` take its GUID as `clientId`, which the account entry keeps. +- The login authority is the domain after the `@` of the address, when it is a plain hostname (Microsoft takes a domain of an organization for its tenant), because a single-tenant registration, the portal's default, signs in only through its own tenant. If Microsoft answers the device-code request or a refresh with a tenant-not-found code (`AADSTS90002`, `AADSTS900023`), that request is made once more at `organizations`, which a multitenant registration answers; the polls of a sign-in stay at the authority that answered its device-code request. Nothing is stored: every refresh derives the authority the same way, and the account entry has no tenant key. +- A sign-in is accepted when the typed address is the id token's `email`, `preferred_username` or `upn` (case-insensitively): a sign-in name can differ from the mail address. If the token names accounts and none is the address, the sign-in is refused as `m365_wrong_account` with the sign-in name Microsoft reported; if it names nobody (or there is none), as `m365_auth_failed`. Either way nothing is kept. +- The Application (client) ID is one spelling everywhere Rust reads it: a GUID in lower case (`auth::m365::client_id`), for the login requests, the keyring item's `client-id` and the refresh lane, so an ID typed in capitals is the same app after the sign-in. The setup page saves it that way too. +- Tokens never leave Rust. The device code lives in the sign-in table, the refresh token in the keyring (kind `m365-refresh-token`, account and client id as attributes), the access token in `auth`'s in-memory cache. Refresh, single-flight, expiry and saving a rotated refresh token run in `auth/credentials/m365.rs`, in the per-mailbox lane Outlook's refresh also uses (`in_outlook_lane`); its scope, consent and authority rules are its own and Outlook's code is untouched. Every login-host request goes through `auth::m365::login` or `login_at`. A change must not give QML a token, put one in an error, a log or an argument, or let a refresh skip the keyring save. +- Hosts are fixed: Graph requests carry the bearer token only to `graph.microsoft.com` and are never redirected, a `@odata.nextLink` is followed only after `graph::next_link` judges it (https, that origin, a messages listing under `/v1.0/me/`) and a page token that carries one is judged before any request. A test build alone can point the clients at a local listener. +- Message ids are the lower-case base32 of Graph's immutable id (`Prefer: IdType="ImmutableId"`, so a move keeps it): one spelling, and a cache file name within 255 bytes. +- Every response, list, string and wait is bounded. Each request has 20 seconds of its own, waits for a slot and a token included (and, after Graph refused a token, the wait for the mailbox's refresh lane to forget it), and a call around several of them 25; a message body is up to 24 MiB and has 60 seconds of its own, so a slow link can finish it. The call, `reader.open`, the RPC frame and the desktop wait a little longer than that for a Microsoft 365 message and stay at 25 and 30 seconds for everything else. A message uploaded whole (a send, a draft) has a second more for every 256 KiB of its base64, up to what its caller waits: 24 seconds from the desktop, whose call must answer inside 30, and 49 from the outbox, which waits a minute for a delivery and the removal of its draft. Four requests per mailbox are in flight at most; a throttled read is repeated twice after `Retry-After` up to five seconds, a write only after a 429 and a send never. A change Graph answered with a 2xx was done: its answer (the whole message) is not read, and is no failure however long it is. +- A batch (`m365.modify`, `m365.trash`, `m365.untrash`, up to 1000 ids) is not dropped at its deadline. Every request in it ends by the call's 25 seconds, none is begun in the last fifth of them, and one already begun is waited for; so when time runs out the answer names what was done in `succeededIds` and what was never begun (with `m365_timeout`) or not confirmed in `failedIds`, and the window restores only those rows. In a combined change (marking read and archiving, say) a message whose first request ran but whose second could not begin before the deadline is reported failed with `m365_timeout` although its first change was applied: the next listing shows it. +- What `mail.act` plans asks Graph only for an action that moves mail (archive, trash), as IMAP's plans do, and refreshes its token without saving a rotated refresh token: a preview writes nothing, the keyring included. +- A send is `sendMail` with the MIME as base64, a Bcc header included. Microsoft documents nothing about Bcc inside the MIME; on one work tenant it was observed that the blind recipient got the message and the other copy had no Bcc header and did not name the blind address. What a failed send proves is `m365_send_not_attempted` (also a deadline that passed before the request was written), `m365_send_rejected` (and `m365_message_too_large`), which the outbox treats as "definitely not sent", or `m365_send_failed`, which it shows as unknown. +- How large a message these two routes take is not known. This provider was written from Microsoft's pages for sending and for creating a message in MIME format without a size limit for the base64 body to rely on, and none has been verified live: the owner's checks go through an RPC frame of at most 1 MiB, so nothing larger has been sent through Graph. Omamail therefore sets no limit of its own below the 16 MiB every provider's message stops at (`message::MAX_MESSAGE`). A 413, or Exchange's `ErrorMessageSizeExceeded` code in an error body, from `sendMail` or the draft `POST` is `m365_message_too_large`: Microsoft refused the message for its size, nothing was sent or saved, and the outbox treats it as definitely not sent. A larger message on a slow link is given a longer clock (above), never a second attempt. +- Drafts live in the mailbox. `m365.saveDraft {accountId, raw, draftId?}` is Microsoft's "create a message in MIME format": `POST /me/messages` with the MIME as base64 `text/plain`, which Graph keeps in Drafts, answered `{saved, warning, id}` (`id` the new draft's immutable id, empty if the answer did not say). With `draftId` the old copy is deleted only after the new one exists, within five more seconds or what is left of the call's 25, and a failure there is a `warning` on a saved draft. A save whose answer was lost after the request was written (the connection, a deadline, a 5xx) is `m365_draft_unconfirmed`: Graph may have made the draft, so the window says it may already be in Drafts, and the save is never repeated. `m365.deleteDraft {accountId, id}` reads the item first (`$select=isDraft,parentFolderId`) and refuses with `m365_not_a_draft`, sending nothing more, anything that is neither a draft nor in Drafts; then `DELETE /me/messages/{id}`. The outbox deletes the draft a sent message came from the same way. Microsoft's page for deleting a message does not say where the message goes; its page for `permanentDelete` says that action puts the item in the Purges folder of the dumpster, out of Outlook's reach. Observed on one work tenant: a draft created from MIME was listed in Drafts and passed the draft check, and a deleted one did not appear in Deleted Items. +- Messages read up to 24 MiB, each parsed with the shared `message::parse_up_to` behind a gate of two. +- Sign-in states are `pending`, `signedIn`, `declined`, `expired` and `failed` with an `error` code. A sign-out that lands while a grant is being saved leaves nothing saved: the save is checked again afterwards and undone. A poll waits for its token request and the keyring save at most 24 seconds (a keyring may be waiting to be unlocked) and then answers `pending` with the warning `m365_exchange_running` while the exchange goes on; the same request answers its outcome for five minutes after. That warning is the poll's own: a token request that failed in transit, its `m365_timeout` included, saved nothing. The window asks the same request again after such an answer or one that never came, and a sign-in that then ends without a mailbox (cancelled, declined, failed) is signed out, since the backend may have saved it. A late answer of a cancelled sign-in is not signed out while a newer one of the same app is under way, signed in, or saved with its check to be asked again: that one writes the same keyring item, which `m365.signOut` would delete, and a sign-out ends the sign-ins under way of the app it names. One of another app (the ID was changed in between) is signed out at once by its own ID, which leaves the newer sign-in alone. Signing out or removing the mailbox names every ID the window asked a sign-in with, so an older one still exchanging its code cannot save a grant afterwards. A sign-out waits for a refresh that holds the sign-in (a keyring waiting to be unlocked) at most 20 seconds and then answers `m365_timeout`, which the window reports as a sign-out that could not be completed. +- A keyring that is locked when a saved sign-in is restored (`auth_keyring_failed`) passes: the check is asked again on the retry schedule and the window says to unlock the keyring, not to sign in again. +- Limits that are not bugs: Unread and Flagged list the Inbox only; and the Graph token the calendar borrows is the mailbox's one sign-in, so it carries the mail permissions as well as `Calendars.ReadWrite`. diff --git a/scripts/diagnostics.py b/scripts/diagnostics.py index d141938..ac14ea0 100644 --- a/scripts/diagnostics.py +++ b/scripts/diagnostics.py @@ -92,6 +92,12 @@ jmap_stream_missing jmap_stream_poll_pending jmap_submission_failed jmap_submission_unconfirmed jmap_timeout jmap_transport_unavailable jmap_unauthorized jmap_unsupported_sort jmap_update_failed jmap_upload_failed +m365_access_denied m365_attachment_not_found m365_auth_failed m365_bad_response m365_cancelled +m365_consent_required m365_draft_unconfirmed m365_folder_unavailable m365_invalid_request +m365_invalid_response m365_message_not_found m365_message_too_large m365_network_failed +m365_not_a_draft m365_request_limit m365_response_too_large m365_send_failed m365_send_not_attempted +m365_send_rejected m365_server_error m365_signed_out m365_tenant_invalid m365_throttled +m365_timeout m365_tls_failed m365_wrong_account mail_account_invalid mail_account_unknown mail_action_destination_unavailable mail_action_invalid_change mail_action_invalid_target mail_action_target_limit mail_action_target_unknown mail_action_unavailable mail_auth_failed mail_check_timeout diff --git a/src/account/mod.rs b/src/account/mod.rs index 47c23ee..4babb2d 100644 --- a/src/account/mod.rs +++ b/src/account/mod.rs @@ -50,7 +50,7 @@ fn text(value: &Value) -> &str { value.as_str().unwrap_or("").trim() } -fn valid_email(value: &str) -> bool { +pub(crate) fn valid_email(value: &str) -> bool { let Some((local, domain)) = value.split_once('@') else { return false; }; @@ -78,7 +78,7 @@ pub fn summarize(bytes: &[u8]) -> Result { let declared = text(&entry["provider"]).to_lowercase(); let provider = match declared.as_str() { "" | "gmail" => "gmail", - "outlook" | "hey" | "jmap" | "imap" => declared.as_str(), + "outlook" | "hey" | "jmap" | "imap" | "m365" => declared.as_str(), _ => continue, }; let mut email = text(&entry["email"]); diff --git a/src/account/tests.rs b/src/account/tests.rs index 1fc5267..b7857b4 100644 --- a/src/account/tests.rs +++ b/src/account/tests.rs @@ -21,6 +21,18 @@ fn registry_identity_repair_and_secret_projection() { } } +#[test] +fn a_microsoft_365_mailbox_is_listed_by_its_own_id_without_its_application_id() { + let source = json!({"version":1,"activeId":"m365:a@example.org","accounts":[ + {"provider":"m365","email":"A@Example.org","label":"Work","clientId":"00000000-1111-2222-3333-444444444444"} + ]}); + let result = summarize(source.to_string().as_bytes()).unwrap(); + assert_eq!(result["accounts"][0]["id"], "m365:a@example.org"); + assert_eq!(result["accounts"][0]["provider"], "m365"); + assert_eq!(result["activeId"], "m365:a@example.org"); + assert!(!result.to_string().contains("00000000")); +} + #[test] fn corrupt_registry_is_an_error_not_empty_success() { assert_eq!(summarize(b"secret"), Err("accounts_invalid")); diff --git a/src/agent/context.rs b/src/agent/context.rs index 9703077..a26da0c 100644 --- a/src/agent/context.rs +++ b/src/agent/context.rs @@ -326,6 +326,7 @@ fn validate_ids(provider: &str, ids: &[String]) -> Result<()> { .bytes() .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'), "jmap" => id.len() <= 1024, + "m365" => crate::providers::m365::validate_message_id(id).is_ok(), _ => false, }; if !valid { @@ -501,6 +502,15 @@ mod tests { json!({"accountId":"imap:test@example.org","requestId":"request-1","ids":["1:INBOX"],"summaries":[{"id":"1:INBOX","subject":"First"}],"prompt":"Read","folder":"inbox"}) } #[test] + fn m365_context_accepts_only_its_own_message_ids() { + let id = crate::providers::m365::encode_id_for_test("AAMkAGI20001AAA="); + assert!(validate_ids("m365", std::slice::from_ref(&id)).is_ok()); + for bad in ["", "1:INBOX", "IFAU222BI5ETEMBQGAYUCQKBHU", "a/b"] { + assert!(validate_ids("m365", &[bad.to_owned()]).is_err(), "{bad}"); + } + assert!(validate_ids("imap", &[id]).is_err()); + } + #[test] fn payload_matches_original_js_for_one_and_many_messages() { let rows = [ json!({"id":"1:INBOX","subject":"مرحبا","from":{"name":"Ada","email":"ada@example.org"},"to":[{"name":"Bob","email":"bob@example.org"}],"messageId":"","bodyText":"First body\nمرحبا"}), diff --git a/src/auth/credentials.rs b/src/auth/credentials.rs index 4c308a5..0c0bddc 100644 --- a/src/auth/credentials.rs +++ b/src/auth/credentials.rs @@ -4,6 +4,8 @@ use crate::credentials::{ self as store, CredentialKey, CredentialKind, Error as StoreError, Secret, }; +pub(crate) mod m365; + pub fn settings(provider: &str, account: &str) -> Result { settings_with(provider, account, crate::account::raw_registry()?) } @@ -13,7 +15,7 @@ pub fn settings_readonly(provider: &str, account: &str) -> Result Result { - if !["gmail", "outlook", "imap", "jmap"].contains(&provider) + if !["gmail", "outlook", "imap", "jmap", "m365"].contains(&provider) || account.is_empty() || account.len() > 1024 || account.chars().any(char::is_control) @@ -181,6 +183,9 @@ async fn access_token_with( resource: &str, read_only: bool, ) -> Result { + if provider == "m365" { + return m365::access_token(account, read_only).await; + } if provider != "outlook" { return Err("auth_provider_invalid"); } @@ -453,9 +458,24 @@ pub(super) async fn change_outlook(params: &Value, clear: bool) -> Result &'static str { + if account.starts_with("m365:") { + "m365" + } else { + "outlook" + } +} + +/// The Graph token of a mailbox for the calendar: an Outlook mailbox asks its Graph resource of its +/// own refresh token, a Microsoft 365 one has the one sign-in that covers mail and calendar. +pub async fn graph_token(account: &str) -> Result { + access_token(graph_provider(account), account, "graph").await +} + /// Discard cached credentials without opening a second refresh lane. pub async fn invalidate(account: &str) -> Result<(), &'static str> { - valid_account("outlook", account)?; + valid_account(graph_provider(account), account)?; if let Some(accounts) = TOKENS.get() { let prefix = format!("{}:", account.to_lowercase()); let locks: Vec<_> = accounts diff --git a/src/auth/credentials/m365.rs b/src/auth/credentials/m365.rs new file mode 100644 index 0000000..43266e4 --- /dev/null +++ b/src/auth/credentials/m365.rs @@ -0,0 +1,338 @@ +//! Microsoft 365 sign-in tokens: one Graph refresh token per mailbox, refreshed in the lane Outlook's +//! are (one refresh at a time, a rotated token saved before it is used), with its own scope, consent +//! rule and login authority. Nothing here changes what Outlook does. +use super::*; + +/// What one sign-in asks for: Graph permissions only, so one device code covers mail and calendar +/// (Microsoft refuses a code that names two resources). `email` puts the address in the id token. +pub(crate) const SCOPE: &str = "openid profile email offline_access https://graph.microsoft.com/Mail.ReadWrite https://graph.microsoft.com/Mail.Send https://graph.microsoft.com/Calendars.ReadWrite"; +const GRAPH: &str = "https://graph.microsoft.com/"; +const ORGANIZATIONS: &str = "organizations"; +/// Both attempts of one login request share this, below the 30 seconds the desktop waits. +const LOGIN_BUDGET: Duration = Duration::from_secs(20); + +/// An Application (client) ID as every request, keyring item and refresh lane of this provider names +/// it: a GUID, in lower case. The portal shows it in lower case; a person may still type it in upper. +pub(crate) fn client_id(text: &str) -> Option { + let groups: Vec<&str> = text.split('-').collect(); + let sound = groups.len() == 5 + && groups + .iter() + .zip([8, 4, 4, 4, 12]) + .all(|(group, len)| group.len() == len && group.bytes().all(|b| b.is_ascii_hexdigit())); + sound.then(|| text.to_ascii_lowercase()) +} + +/// Whether `granted` (a token response's `scope`) holds every Graph permission of `SCOPE`. Microsoft +/// writes a permission with or without the resource prefix and in either case. +pub(crate) fn permissions_granted(granted: &str) -> bool { + let bare = |scope: &'_ str| { + scope + .strip_prefix(GRAPH) + .unwrap_or(scope) + .to_ascii_lowercase() + }; + SCOPE + .split_whitespace() + .filter_map(|scope| scope.strip_prefix(GRAPH)) + .all(|asked| { + granted + .split_whitespace() + .any(|scope| bare(scope) == asked.to_ascii_lowercase()) + }) +} + +/// The tenant a login request for `address` goes to: the domain after `@` (Microsoft takes a domain +/// of an organization for its tenant) when it is a plain hostname, else `organizations`. A single-tenant +/// registration signs in only through its own tenant, so the portal's default needs this. +fn authority(address: &str) -> String { + let label = |label: &str| { + (1..=63).contains(&label.len()) + && !label.starts_with('-') + && !label.ends_with('-') + && label + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-') + }; + match address.rsplit_once('@') { + Some((_, domain)) + if domain.len() <= 253 && domain.contains('.') && domain.split('.').all(label) => + { + domain.to_ascii_lowercase() + } + _ => ORGANIZATIONS.into(), + } +} + +/// Microsoft says the tenant does not exist (or is no name at all): nobody has that domain. +pub(crate) fn tenant_unknown(body: &Value) -> bool { + body["error"] == "invalid_tenant" || aadsts_code(body, 90002) || aadsts_code(body, 900023) +} + +/// Microsoft says consent was never given (65001) or needs an administrator (90094). +pub(crate) fn consent_needed(body: &Value) -> bool { + aadsts_code(body, 65001) || aadsts_code(body, 90094) +} + +/// A form request to the login host as it answered: status and JSON body. +pub(crate) async fn login_at( + authority: &str, + endpoint: &str, + fields: &[(&str, &str)], +) -> Result<(u16, Value), &'static str> { + // The one place a login URL is made, and where the tenant is judged to be one plain name. + let url = destination(&json!({"provider":"outlook", "endpoint":endpoint, "tenant":authority}))?; + let reply = post_login(&url, callback::form(fields)).await?; + let status = reply["status"].as_u64().ok_or("auth_invalid_response")?; + let body = serde_json::from_str(reply["body"].as_str().ok_or("auth_invalid_response")?) + .map_err(|_| "auth_invalid_response")?; + Ok((status as u16, body)) +} + +/// A login request for `address`: to its domain's tenant, and if Microsoft does not know that +/// domain, once more to `organizations` (a registration for any directory takes people whose +/// domain is not their tenant's own). Answers the authority that answered, so a flow can keep to it. +pub(crate) async fn login( + address: &str, + endpoint: &str, + fields: &[(&str, &str)], +) -> Result<(String, u16, Value), &'static str> { + let domain = authority(address); + tokio::time::timeout(LOGIN_BUDGET, async { + let (status, body) = login_at(&domain, endpoint, fields).await?; + if status == 200 || domain == ORGANIZATIONS || !tenant_unknown(&body) { + return Ok((domain, status, body)); + } + let (status, body) = login_at(ORGANIZATIONS, endpoint, fields).await?; + Ok((ORGANIZATIONS.into(), status, body)) + }) + .await + .map_err(|_| "auth_timeout")? +} + +/// A test build can send login requests to a local listener (`http://127.0.0.1:`); nothing in +/// a release build, a setting or the environment can name another host. +#[cfg(test)] +pub(crate) static TEST_LOGIN: std::sync::Mutex> = std::sync::Mutex::new(None); + +async fn post_login(url: &str, body: String) -> Result { + #[cfg(test)] + let local = TEST_LOGIN.lock().unwrap_or_else(|e| e.into_inner()).clone(); + #[cfg(test)] + if let Some(local) = local { + // The production client but for the HTTPS requirement. + let client = builder().build().map_err(|_| "auth_transport_failed")?; + let url = url.replacen("https://login.microsoftonline.com", &local, 1); + return post(&client, &url, body).await; + } + post(client()?, url, body).await +} + +fn key(client: &str, account: &str) -> CredentialKey { + CredentialKey { + provider: "m365".into(), + account_id: account.to_lowercase(), + kind: CredentialKind::M365RefreshToken { + client_id: client.into(), + }, + } +} + +/// The account's lane (see `in_outlook_lane`): the same for every caller of one mailbox and client. +async fn lane(account: &str, client: &str) -> Result { + account_tokens(account, client, "m365").await +} + +/// A Graph access token from the mailbox's refresh token, cached until it nears expiry. +pub(super) async fn access_token(account: &str, read_only: bool) -> Result { + valid_account("m365", account)?; + let owned = account.to_owned(); + let entry = tokio::task::spawn_blocking(move || { + if read_only { + settings_readonly("m365", &owned) + } else { + settings("m365", &owned) + } + }) + .await + .map_err(|_| "auth_account_invalid")??; + let client = entry["clientId"].as_str().ok_or("auth_client_missing")?; + let client = client_id(client).ok_or("auth_client_invalid")?; + let lane = lane(account, &client).await?; + let account = account.to_owned(); + in_outlook_lane(lane, move |mut tokens| async move { + let now = (std::time::Instant::now(), std::time::SystemTime::now()); + if let Some(token) = cached_token(&tokens, "graph", now.0, now.1) { + return Ok(token); + } + let key = key(&client, &account); + let secret = store::get(key.clone()).await.map_err(store_error)?; + let refresh = text_secret(&secret)?; + let address = account.strip_prefix("m365:").unwrap_or(&account); + let fields = [ + ("client_id", client.as_str()), + ("grant_type", "refresh_token"), + ("refresh_token", refresh), + ("scope", SCOPE), + ]; + let (_, status, token) = login(address, "token", &fields).await?; + if status != 200 { + return Err(if consent_needed(&token) { + "auth_consent_required" + } else if token["error"] == "invalid_grant" { + "auth_signed_out" + } else { + "auth_refresh_failed" + }); + } + let access = outlook_access(&token)?; + persist_outlook_rotation(refresh, &token, key, read_only).await?; + if !permissions_granted(token["scope"].as_str().unwrap_or("")) { + return Err("auth_consent_required"); + } + let lifetime = token["expires_in"] + .as_u64() + .filter(|n| *n <= 86400) + .unwrap_or(0); + let expiry = TokenExpiry::after( + Duration::from_secs(lifetime), + std::time::Instant::now(), + std::time::SystemTime::now(), + ); + tokens.insert("graph".into(), (access.to_owned(), expiry)); + Ok(access.to_owned()) + }) + .await +} + +/// Keeps (`Some`) or deletes (`None`) the mailbox's refresh token under the lane its refreshes use, so +/// a rotation cannot land after the change. The account entry may not exist yet: a sign-in comes first. +pub(crate) async fn change( + account: &str, + client: &str, + token: Option<&str>, +) -> Result<(), &'static str> { + change_while(account, client, token, || true) + .await + .map(|_| ()) +} + +/// Saves a device grant only while its flow is current. The checks, save and any rollback own one +/// refresh lane, so a cancelled grant can never delete a newer grant queued behind its keyring write. +pub(crate) async fn save_grant( + account: &str, + client: &str, + token: &str, + current: impl Fn() -> bool + Send + 'static, +) -> Result { + change_while(account, client, Some(token), current).await +} + +async fn change_while( + account: &str, + client: &str, + token: Option<&str>, + current: impl Fn() -> bool + Send + 'static, +) -> Result { + valid_account("m365", account)?; + let client = &client_id(client).ok_or("auth_client_invalid")?; + let secret = token + .map(|token| { + if token.is_empty() || token.len() > 16384 || token.chars().any(char::is_control) { + return Err("auth_secret_invalid"); + } + Secret::new(token.as_bytes().to_vec()).map_err(store_error) + }) + .transpose()?; + let lane = lane(account, client).await?; + let key = key(client, account); + in_outlook_lane(lane, move |mut tokens| async move { + if !current() { + return Ok(false); + } + if let Some(secret) = secret { + store::put(key.clone(), secret).await.map_err(store_error)?; + tokens.clear(); + if current() { + return Ok(true); + } + // A cancellation during the native write is undone before another grant or refresh + // can take this lane. Comparing token bytes would not protect a reused token value. + } + match store::delete(key).await { + Ok(()) | Err(StoreError::Missing) => {} + Err(error) => return Err(store_error(error)), + } + tokens.clear(); + Ok(false) + }) + .await +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_login_goes_to_the_domain_of_the_address_when_that_is_a_plain_hostname() { + for (address, expected) in [ + ("a@example.org", "example.org"), + ("a@Sub.Example.ORG", "sub.example.org"), + ("a@xn--bcher-kva.example", "xn--bcher-kva.example"), + ("a@localhost", "organizations"), + ("a@exa_mple.org", "organizations"), + ("a@a..org", "organizations"), + ("a@-a.org", "organizations"), + ("a@a-.org", "organizations"), + ("a@b\u{fc}cher.example", "organizations"), + ("a@a/b.org", "organizations"), + ("a@a.org.", "organizations"), + ("no-at-sign", "organizations"), + ] { + assert_eq!(authority(address), expected, "{address}"); + } + for long in [ + format!("a@{}.org", "a".repeat(64)), + format!("a@{}", vec!["aa"; 130].join(".")), + ] { + assert_eq!(authority(&long), "organizations"); + } + } + + #[test] + fn a_grant_is_judged_by_the_graph_permissions_in_either_spelling() { + assert!(SCOPE.contains("email") && !SCOPE.contains("outlook.office.com")); + let all = "Mail.ReadWrite Mail.Send Calendars.ReadWrite openid profile"; + assert!(permissions_granted(all)); + assert!(permissions_granted(&all.to_uppercase())); + assert!(permissions_granted(&SCOPE.replace("openid", ""))); + for short in [ + "", + "openid", + "Mail.ReadWrite Mail.Send", + "Mail.Read Mail.Send Calendars.ReadWrite", + "https://outlook.office.com/Mail.ReadWrite Mail.Send Calendars.ReadWrite", + ] { + assert!(!permissions_granted(short), "{short}"); + } + } + + #[test] + fn microsofts_numbers_say_consent_or_tenant_and_its_words_are_not_read() { + assert!(consent_needed(&json!({"error_codes":[65001]}))); + assert!(consent_needed( + &json!({"error_description":"AADSTS90094: needs admin"}) + )); + assert!(tenant_unknown(&json!({"error":"invalid_tenant"}))); + assert!(tenant_unknown(&json!({"error_codes":[90002]}))); + assert!(tenant_unknown(&json!({"error_codes":[900023]}))); + for body in [ + json!({}), + json!({"error":"invalid_grant"}), + json!({"error_codes":[50194]}), + json!({"error_description":"secret AADSTS65001 words"}), + ] { + assert!(!tenant_unknown(&body) && !consent_needed(&body), "{body}"); + } + } +} diff --git a/src/auth/mod.rs b/src/auth/mod.rs index 9d808ad..7e0c467 100644 --- a/src/auth/mod.rs +++ b/src/auth/mod.rs @@ -5,7 +5,10 @@ use std::{sync::OnceLock, time::Duration}; mod callback; mod credentials; mod graph; -pub use credentials::{access_token, access_token_readonly, password, settings, settings_readonly}; +pub use credentials::{ + access_token, access_token_readonly, graph_token, password, settings, settings_readonly, +}; +pub(crate) use credentials::{invalidate, m365}; #[derive(Default)] pub struct Session { @@ -15,16 +18,20 @@ pub struct Session { static CLIENT: OnceLock> = OnceLock::new(); const LIMIT: usize = 256 * 1024; +fn builder() -> reqwest::ClientBuilder { + reqwest::Client::builder() + .hickory_dns(true) + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .connect_timeout(Duration::from_secs(10)) + .timeout(Duration::from_secs(20)) +} + fn client() -> Result<&'static reqwest::Client, &'static str> { CLIENT .get_or_init(|| { - reqwest::Client::builder() + builder() .https_only(true) - .hickory_dns(true) - .no_proxy() - .redirect(reqwest::redirect::Policy::none()) - .connect_timeout(Duration::from_secs(10)) - .timeout(Duration::from_secs(20)) .build() .map_err(|_| "auth_transport_failed") }) diff --git a/src/backend/mail.rs b/src/backend/mail.rs index 2031f05..0564e6a 100644 --- a/src/backend/mail.rs +++ b/src/backend/mail.rs @@ -63,6 +63,7 @@ impl crate::mail::send::IdentityLookup for ProviderIdentities<'_> { let checked = crate::providers::hey_access::checked_params(&json!({"accountId":account.id,"program":crate::providers::hey_access::program()?})).await?; crate::providers::hey::call("hey.sendAs", &checked).await } + Provider::M365 => Box::pin(m365_call("m365.sendAs", ¶ms)).await, Provider::Imap | Provider::Outlook => { let account = account.clone(); tokio::task::spawn_blocking(move || { @@ -108,6 +109,9 @@ impl MutationAdapter for ProviderMutation<'_> { params["program"] = json!(crate::providers::hey_access::program()?); let checked = crate::providers::hey_access::checked_params(¶ms).await?; crate::providers::hey_actions::call(method, &checked).await + } else if method.starts_with("m365.") { + // Detached like IMAP: a caller that gives up cannot leave a half-sent batch behind. + m365_call(method, ¶ms).await } else { let method = method.to_owned(); let context = context.clone(); @@ -176,6 +180,10 @@ pub(crate) async fn mutate_plan( 1 } else if matches!(plan.account.provider, Provider::Imap | Provider::Outlook) { 500 + } else if plan.account.provider == Provider::M365 { + // Graph changes one message per request, four at a time, and a call has a deadline: a + // smaller batch leaves less unconfirmed when it fires. + 50 } else { 1000 }; @@ -194,6 +202,7 @@ pub(crate) async fn mutate_plan( } Provider::Jmap if plan.operation == "trash" => "jmap.trash", Provider::Imap | Provider::Outlook if plan.operation == "trash" => "imap.trash", + Provider::M365 if plan.operation == "trash" => "m365.trash", provider => { params["addLabelIds"] = json!(plan.add_label_ids); params["removeLabelIds"] = json!(plan.remove_label_ids); @@ -201,6 +210,7 @@ pub(crate) async fn mutate_plan( Provider::Gmail => "gmail.batchModify", Provider::Jmap => "jmap.batchModify", Provider::Imap | Provider::Outlook => "imap.modify", + Provider::M365 => "m365.modify", Provider::Hey => unreachable!(), } } @@ -272,6 +282,20 @@ impl crate::mail::action::ActionLookup for AccountActionLookup<'_> { .await .map_err(|_| "worker_failed")?; } + // Microsoft 365 asks the mailbox, read-only like IMAP, only for an action that moves mail: + // whether it has an Archive and a Trash decides what may be planned. + if account.provider == Provider::M365 + && crate::account::model::action_mailbox(action).is_some() + && (capability.is_empty() + || crate::providers::can(account.provider.id(), capability, &refusals)) + { + let account = account.clone(); + return tokio::spawn(async move { + crate::providers::m365::planned_action_availability(&account.id, refusals).await + }) + .await + .map_err(|_| "worker_failed")?; + } let mailboxes = ["archive", "trash", "spam"] .iter() .map(|mailbox| { @@ -335,6 +359,14 @@ async fn imap_call(method: &str, params: &Value) -> Result .map_err(|_| "worker_failed")? } +async fn m365_call(method: &str, params: &Value) -> Result { + let method = method.to_owned(); + let params = params.clone(); + tokio::spawn(async move { crate::providers::m365::call(&method, ¶ms).await }) + .await + .map_err(|_| "worker_failed")? +} + impl Session { pub(crate) async fn finish_cli_send(&self, result: &mut Value) -> Result<(), &'static str> { result["outbox"] = self @@ -434,6 +466,7 @@ impl Session { Provider::Outlook | Provider::Imap => { Box::pin(self.imap_list(¶ms, request.limit)).await } + Provider::M365 => Box::pin(self.m365_list(¶ms, request.limit)).await, } } @@ -517,6 +550,26 @@ impl Session { })) } + /// `m365.list` answers the page and its header-only rows in one reply, so there is no second + /// per-id request as in the Gmail and IMAP paths. + async fn m365_list(&self, params: &Value, limit: u16) -> Result { + let reply = Box::pin(m365_call( + "m365.list", + &json!({ + "accountId":params["accountId"],"query":params["query"],"limit":limit, + "pageToken":params["pageToken"], + }), + )) + .await?; + let page = &reply["page"]; + Ok(json!({ + "ids":page["ids"], + "messages":summaries(reply["rows"].as_array().ok_or("m365_invalid_response")?)?, + "nextPageToken":page["nextPageToken"].as_str().unwrap_or(""), + "estimate":page["estimate"].as_u64().unwrap_or(0), + })) + } + async fn imap_list(&self, params: &Value, limit: u16) -> Result { let mut request = json!({ "accountId":params["accountId"],"query":params["query"],"limit":limit, diff --git a/src/backend/methods.rs b/src/backend/methods.rs index 878e645..28ca76a 100644 --- a/src/backend/methods.rs +++ b/src/backend/methods.rs @@ -181,6 +181,23 @@ pub const ALL: &[&str] = &[ "jmap.stream.open", "jmap.stream.poll", "jmap.stream.close", + "m365.signInBegin", + "m365.signInPoll", + "m365.signOut", + "m365.verify", + "m365.list", + "m365.messages", + "m365.read", + "m365.attachment", + "m365.modify", + "m365.trash", + "m365.untrash", + "m365.send", + "m365.saveDraft", + "m365.deleteDraft", + "m365.sendAs", + "m365.invalidate", + "m365.cancel", ]; pub fn available() -> Vec<&'static str> { diff --git a/src/backend/mod.rs b/src/backend/mod.rs index ec4d192..e405127 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -207,6 +207,9 @@ impl Session { if method.starts_with("jmap.") { return Box::pin(self.jmap.call(method, params)).await; } + if method.starts_with("m365.") { + return Box::pin(crate::providers::m365::call(method, params)).await; + } if method.starts_with("imap.") || method == "smtp.send" { return Box::pin(crate::providers::imap::call(method, params)).await; } @@ -322,12 +325,10 @@ impl Session { .await?, ), Some("microsoft") => Some( - crate::auth::access_token( - "outlook", + crate::auth::graph_token( params["source"]["accountId"] .as_str() .ok_or("invalid_params")?, - "graph", ) .await?, ), @@ -479,7 +480,7 @@ pub fn dispatch(method: &str, params: &Value) -> Result { match method { "system.info" => Ok(json!({ "name": "omamail", "version": env!("CARGO_PKG_VERSION"), - "protocol": 1, "apiVersion": 7, "methods": methods::available(), + "protocol": 1, "apiVersion": 8, "methods": methods::available(), "capabilities": {"agent": cfg!(all(feature = "agent", unix))} })), "system.quit" => Ok(json!({"quitReady": true})), diff --git a/src/backend/reader.rs b/src/backend/reader.rs index d8814fd..c1c22b5 100644 --- a/src/backend/reader.rs +++ b/src/backend/reader.rs @@ -190,6 +190,13 @@ impl Session { .cloned() .ok_or("reader_message_missing")? } + "m365" => { + crate::providers::m365::call( + "m365.read", + &json!({"accountId":account,"id":id,"full":true}), + ) + .await? + } _ => return Err("reader_provider_unknown"), }; if value["id"] != id || !value["payload"].is_object() { @@ -291,6 +298,13 @@ impl Session { key, live: job.live.clone(), }; + // A Microsoft 365 message is up to 24 MiB and is allowed a minute for the body. Every other + // provider stays on the 25 seconds a listing uses. + let allowance = if account.starts_with("m365:") { + Duration::from_secs(80) + } else { + Duration::from_secs(25) + }; let operation = async { registered(&account).await?; job.current()?; @@ -346,7 +360,7 @@ impl Session { .await .map_err(|_| "worker_failed")? }; - let result = tokio::select! {result=tokio::time::timeout(Duration::from_secs(25),operation)=>result.unwrap_or(Err("reader_timeout")),_=job.cancelled.notified()=>Err("reader_cancelled")}; + let result = tokio::select! {result=tokio::time::timeout(allowance,operation)=>result.unwrap_or(Err("reader_timeout")),_=job.cancelled.notified()=>Err("reader_cancelled")}; if result.is_err() { *job.live.lock().map_err(|_| "session_failed")? = false; } diff --git a/src/backend/rpc.rs b/src/backend/rpc.rs index df9f6eb..5fe0a44 100644 --- a/src/backend/rpc.rs +++ b/src/backend/rpc.rs @@ -29,6 +29,19 @@ fn empty_params() -> Value { json!({}) } +fn body_allowance(request: &Request) -> std::time::Duration { + let params = &request.params; + let body = match request.method.as_str() { + "m365.read" | "m365.attachment" => true, + "m365.messages" => params["full"] == true, + "reader.open" => params["accountId"] + .as_str() + .is_some_and(|id| id.starts_with("m365:")), + _ => false, + }; + std::time::Duration::from_secs(if body { 60 } else { 0 }) +} + pub fn error(id: Value, code: i32, message: &str) -> Value { json!({"jsonrpc":"2.0", "id":id, "error":{"code":code,"message":message}}) } @@ -108,6 +121,9 @@ async fn one( { return (Some(error(Value::Null, -32600, "Invalid Request")), false); } + // A Microsoft 365 message body is allowed a minute inside the provider, so + // its frame may wait that much longer before it counts as expired. + let deadline = deadline + body_allowance(&request); // An expired queued request must not start a side effect: timeout_at polls // its future before checking the clock. let result = if tokio::time::Instant::now() >= deadline { @@ -145,6 +161,29 @@ mod tests { use super::*; use std::time::Duration; + #[tokio::test] + async fn only_a_microsoft_365_body_read_may_wait_past_the_frame_deadline() { + let session = crate::backend::Session::default(); + let late = tokio::time::Instant::now() - Duration::from_secs(1); + // Started, and refused for its parameters before anything is asked of anybody. + for params in [ + r#""m365.read","params":{}"#, + r#""m365.messages","params":{"full":true}"#, + ] { + let frame = format!(r#"{{"jsonrpc":"2.0","id":1,"method":{params}}}"#); + let (response, _) = handle(frame.as_bytes(), &session, late).await; + assert_eq!(response.unwrap()["error"]["message"], "Invalid params"); + } + for params in [ + r#""m365.list","params":{}"#, + r#""m365.messages","params":{"full":false}"#, + ] { + let frame = format!(r#"{{"jsonrpc":"2.0","id":1,"method":{params}}}"#); + let (response, _) = handle(frame.as_bytes(), &session, late).await; + assert_eq!(response.unwrap()["error"]["message"], "request_timed_out"); + } + } + #[tokio::test] async fn expired_upload_request_does_not_reserve_capacity() { let session = crate::backend::Session::default(); diff --git a/src/cache/query.rs b/src/cache/query.rs index f9173f1..412f565 100644 --- a/src/cache/query.rs +++ b/src/cache/query.rs @@ -168,7 +168,7 @@ fn eligible(provider: &str, query: &str, row: &Value) -> bool { if provider == "hey" { return true; } - if !matches!(provider, "gmail" | "jmap") { + if !matches!(provider, "gmail" | "jmap" | "m365") { return false; } if row["labelIds"].as_array().is_some_and(|labels| { @@ -449,6 +449,8 @@ impl QueryCache { "jmap" } else if account.starts_with("hey:") { "hey" + } else if account.starts_with("m365:") { + "m365" } else { "gmail" }; diff --git a/src/cache/query/tests.rs b/src/cache/query/tests.rs index a29fe80..d530c99 100644 --- a/src/cache/query/tests.rs +++ b/src/cache/query/tests.rs @@ -14,6 +14,18 @@ fn conservative_search_terms_and_provider_scope() { assert_eq!(query_key(" in:inbox ", 25), "in:inbox|25"); } #[test] +fn m365_pages_answer_search_like_jmap_roles_and_never_from_trash_or_junk() { + let mut store = store::empty(); + store["queries"] = json!({ + "role:inbox|25":{"at":3,"summaries":[row("in","needle",10,json!(["INBOX"]))]}, + "role:trash|25":{"at":2,"summaries":[row("gone","needle",20,json!(["TRASH"]))]}, + "role:junk|25":{"at":1,"summaries":[row("spam","needle",30,json!(["SPAM"]))]} + }); + let found = search(&store, "needle", "m365"); + assert_eq!(found.len(), 1); + assert_eq!(found[0]["id"], "in"); +} +#[test] fn newest_copy_owns_scope_and_older_copy_can_supply_search_fields() { let mut store = store::empty(); store["queries"] = json!({"in:trash|25":{"at":20,"summaries":[row("id","",20,json!(["TRASH"]))]},"in:inbox|25":{"at":10,"summaries":[row("id","needle",10,json!([]))]}}); diff --git a/src/calendar/discovery.rs b/src/calendar/discovery.rs index cee8d80..7413912 100644 --- a/src/calendar/discovery.rs +++ b/src/calendar/discovery.rs @@ -26,7 +26,9 @@ fn account_id(params: &Value) -> Result<&str, &'static str> { .get("accountId") .and_then(Value::as_str) .filter(|value| { - (value.starts_with("outlook:") || value.starts_with("imap:")) + (value.starts_with("outlook:") + || value.starts_with("imap:") + || value.starts_with("m365:")) && value.len() <= 1024 && !value.chars().any(|c| c.is_control() || c.is_whitespace()) }) @@ -77,8 +79,9 @@ pub(super) async fn response_body(mut response: Response) -> Result Result { - crate::auth::settings("outlook", account)?; - let token = crate::auth::access_token("outlook", account, "graph").await?; + // A Microsoft 365 mailbox keeps its Graph token under its own sign-in (one code for mail and + // calendar); an Outlook one asks its Graph resource of the Outlook refresh token. + let token = crate::auth::graph_token(account).await?; microsoft_with_client(super::client()?, account, &token).await } @@ -599,7 +602,7 @@ async fn icloud(account: &str) -> Result { pub async fn discover(params: &Value) -> Result { let account = account_id(params)?.to_owned(); tokio::time::timeout(std::time::Duration::from_secs(27), async { - if account.starts_with("outlook:") { + if account.starts_with("outlook:") || account.starts_with("m365:") { microsoft(&account).await } else { icloud(&account).await @@ -655,6 +658,10 @@ mod tests { account_id(&json!({"accountId":"outlook:person@example.org"})), Ok("outlook:person@example.org") ); + assert_eq!( + account_id(&json!({"accountId":"m365:person@example.org"})), + Ok("m365:person@example.org") + ); for value in [ json!({}), json!({"accountId":"gmail:person@example.org"}), diff --git a/src/credentials/mod.rs b/src/credentials/mod.rs index f155758..eedaa62 100644 --- a/src/credentials/mod.rs +++ b/src/credentials/mod.rs @@ -27,6 +27,7 @@ pub enum CredentialKind { OutlookRefreshToken { client_id: String }, ImapPassword, JmapSecret, + M365RefreshToken { client_id: String }, CalendarPassword, } @@ -153,6 +154,7 @@ impl CredentialKey { CredentialKind::OutlookRefreshToken { .. } => ("outlook", "outlook-refresh-token"), CredentialKind::ImapPassword => ("imap", "imap-password"), CredentialKind::JmapSecret => ("jmap", "jmap-secret"), + CredentialKind::M365RefreshToken { .. } => ("m365", "m365-refresh-token"), CredentialKind::CalendarPassword => ("caldav", "calendar-password"), }; if self.provider != provider { @@ -177,7 +179,8 @@ impl CredentialKey { attrs.insert("account".into(), self.account_id.to_lowercase()); } if let CredentialKind::GoogleRefreshToken { client_id } - | CredentialKind::OutlookRefreshToken { client_id } = &self.kind + | CredentialKind::OutlookRefreshToken { client_id } + | CredentialKind::M365RefreshToken { client_id } = &self.kind { metadata(client_id)?; attrs.insert("client-id".into(), client_id.clone()); diff --git a/src/credentials/rpc.rs b/src/credentials/rpc.rs index 484ed67..26e4afb 100644 --- a/src/credentials/rpc.rs +++ b/src/credentials/rpc.rs @@ -211,6 +211,10 @@ mod tests { json!({"kind":"calendar-password","accountId":"source\0id"}), json!({"kind":"google-refresh-token","accountId":"a@example.org"}), json!({"kind":"google-refresh-token","accountId":"a@example.org","clientId":"client","nativeAttributes":[]}), + // A Microsoft 365 refresh token never crosses this interface: the provider reads and + // writes it in Rust, and QML has no kind to name it by. + json!({"kind":"m365-refresh-token","accountId":"m365:a@example.org","clientId":"client"}), + json!({"kind":"m365-refresh-token","accountId":"m365:a@example.org"}), ] { assert!(matches!( parse("credentials.get", ¶ms), diff --git a/src/credentials/tests.rs b/src/credentials/tests.rs index 1536d25..c9d6d43 100644 --- a/src/credentials/tests.rs +++ b/src/credentials/tests.rs @@ -298,3 +298,42 @@ fn credentials_native_linux_retains_legacy_attribute_scopes() { ); cleanup.0.delete().unwrap(); } + +#[test] +fn m365_refresh_tokens_are_their_own_kind_bound_to_account_and_client() { + let m365 = |provider: &str, account: &str, client: &str| { + let kind = CredentialKind::M365RefreshToken { + client_id: client.into(), + }; + key(provider, account, kind) + }; + let good = m365("m365", "m365:A@example.org", "client"); + let attributes = good.attributes().unwrap(); + assert_eq!(attributes.get("kind").unwrap(), "m365-refresh-token"); + assert_eq!(attributes.get("account").unwrap(), "m365:a@example.org"); + assert_eq!(attributes.get("client-id").unwrap(), "client"); + // Neither another client's item nor Outlook's is this one. + let other = m365("m365", "m365:a@example.org", "other") + .native_id() + .unwrap(); + let outlook = key( + "outlook", + "outlook:a@example.org", + CredentialKind::OutlookRefreshToken { + client_id: "client".into(), + }, + ); + let id = good.native_id().unwrap(); + assert!(id != other && id != outlook.native_id().unwrap()); + for bad in [ + m365("outlook", "m365:a@example.org", "client"), + m365("m365", "outlook:a@example.org", "client"), + m365("m365", "m365:a b@example.org", "client"), + m365("m365", "m365:a@example.org", ""), + ] { + assert!( + matches!(NativeStore.get(&bad), Err(Error::InvalidKey)), + "{bad:?}" + ); + } +} diff --git a/src/mail/action.rs b/src/mail/action.rs index f8a200c..9d62712 100644 --- a/src/mail/action.rs +++ b/src/mail/action.rs @@ -86,6 +86,7 @@ pub(crate) fn validate_message_id(provider: Provider, id: &str) -> Result<(), &' Provider::Jmap => crate::providers::jmap::validate_action_id(id), Provider::Hey => crate::providers::hey_actions::message_id(id).map(|_| ()), Provider::Imap | Provider::Outlook => crate::providers::imap::message_id(id).map(|_| ()), + Provider::M365 => crate::providers::m365::validate_message_id(id), } .map_err(|_| "invalid_params") } diff --git a/src/mail/send.rs b/src/mail/send.rs index f9af812..e6274c5 100644 --- a/src/mail/send.rs +++ b/src/mail/send.rs @@ -307,8 +307,10 @@ pub(super) fn validate_size( ) -> Result<()> { if (provider == Provider::Hey && encoded_len > 16 * 1024 * 1024 * 4 / 3) || (provider == Provider::Jmap && encoded_len > 24 * 1024 * 1024) - || (matches!(provider, Provider::Imap | Provider::Outlook) - && decoded_len > crate::message::MAX_MESSAGE) + || (matches!( + provider, + Provider::Imap | Provider::Outlook | Provider::M365 + ) && decoded_len > crate::message::MAX_MESSAGE) { return Err("message_too_large"); } diff --git a/src/mail/types.rs b/src/mail/types.rs index 4bcbfa7..2aa7454 100644 --- a/src/mail/types.rs +++ b/src/mail/types.rs @@ -12,6 +12,7 @@ pub enum Provider { Hey, Jmap, Imap, + M365, } impl Provider { @@ -22,6 +23,7 @@ impl Provider { Self::Hey => "hey", Self::Jmap => "jmap", Self::Imap => "imap", + Self::M365 => "m365", } } } @@ -36,6 +38,7 @@ impl TryFrom<&str> for Provider { "hey" => Ok(Self::Hey), "jmap" => Ok(Self::Jmap), "imap" => Ok(Self::Imap), + "m365" => Ok(Self::M365), _ => Err("mail_provider_unknown"), } } diff --git a/src/message/mod.rs b/src/message/mod.rs index ee654ee..02d0652 100644 --- a/src/message/mod.rs +++ b/src/message/mod.rs @@ -15,7 +15,12 @@ pub const MAX_MESSAGE: usize = 16 * 1024 * 1024; mod tests; pub fn parse(raw: &[u8]) -> Result { - if raw.len() > MAX_MESSAGE { + parse_up_to(raw, MAX_MESSAGE) +} + +/// `parse` for a provider whose transport carries more than `MAX_MESSAGE` and bounds the message itself. +pub fn parse_up_to(raw: &[u8], limit: usize) -> Result { + if raw.len() > limit { return Err("message_too_large"); } let parsed = mailparse::parse_mail(raw).map_err(|_| "invalid_message")?; diff --git a/src/outbox/delivery.rs b/src/outbox/delivery.rs index f9d44c5..a36f4b3 100644 --- a/src/outbox/delivery.rs +++ b/src/outbox/delivery.rs @@ -173,6 +173,8 @@ pub async fn send( "gmail" => gmail.call("gmail.send", ¶ms).await?, "imap" | "outlook" => crate::providers::imap::call("imap.send", ¶ms).await?, "jmap" => jmap.call("jmap.send", ¶ms).await?, + // The outbox waits longer than the desktop, and a large message may use that to upload. + "m365" => Box::pin(crate::providers::m365::send_queued(¶ms)).await?, "hey" => { params["program"] = json!(crate::providers::hey_access::program()?); let raw = raw.to_owned(); @@ -204,6 +206,9 @@ pub async fn send( "imap" | "outlook" => crate::providers::imap::call("imap.deleteDraft", ¶ms) .await .map(|_| ()), + "m365" => Box::pin(crate::providers::m365::call("m365.deleteDraft", ¶ms)) + .await + .map(|_| ()), "jmap" if answer["draftRemoved"] == true => Ok(()), _ => Err("outbox_draft_cleanup_unavailable"), } diff --git a/src/outbox/mod.rs b/src/outbox/mod.rs index 500be45..15b72bd 100644 --- a/src/outbox/mod.rs +++ b/src/outbox/mod.rs @@ -370,7 +370,7 @@ impl Inner { match method { "outbox.enqueue" => { let provider = text(params, "provider")?; - if !["gmail", "outlook", "imap", "jmap", "hey"].contains(&provider) { + if !["gmail", "outlook", "imap", "jmap", "hey", "m365"].contains(&provider) { return Err("outbox_invalid_provider"); } let payload = params @@ -610,6 +610,11 @@ fn definitely_refused(error: &str) -> bool { | "gmail_invalid_params" | "gmail_send_rejected" | "jmap_send_rejected" + // Microsoft 365: its explicit refusal, a failure before the request that could create + // the message was written, and an oversize message refused before any I/O. + | "m365_send_rejected" + | "m365_send_not_attempted" + | "m365_message_too_large" | "smtp_recipient_refused" ) } diff --git a/src/providers/domain.rs b/src/providers/domain.rs index 703f08a..36bca33 100644 --- a/src/providers/domain.rs +++ b/src/providers/domain.rs @@ -69,7 +69,7 @@ fn search(id: &str, text: &str) -> String { match id { "imap" | "outlook" => imap_search(text), "hey" => format!("search:{text}"), - "jmap" => format!("text:{text}"), + "jmap" | "m365" => format!("text:{text}"), _ => text.into(), } } @@ -136,6 +136,7 @@ pub fn resolve(p: &Value) -> Result { match id.as_str() { "imap" | "outlook" => format!("folder:{}", quoted(text)), "jmap" => format!("mailbox:{text}"), + "m365" => String::new(), _ => format!("label:{text}"), } } @@ -183,7 +184,7 @@ mod tests { use super::*; #[test] fn control_bytes_are_rejected_before_any_query_or_url_is_returned() { - for id in ["gmail", "outlook", "hey", "jmap", "imap"] { + for id in ["gmail", "outlook", "hey", "jmap", "m365", "imap"] { for suffix in ["\r", "\n", "\r\n", "\0", "\t", "\u{7f}"] { for operation in ["labelQuery", "addressQuery", "webMessageUrl", "webBoxUrl"] { assert_eq!( @@ -231,6 +232,28 @@ mod tests { } } #[test] + fn m365_speaks_the_role_query_language_and_has_no_labels_or_web_urls() { + for (mailbox, expected) in [ + ("inbox", "role:inbox"), + ("unread", "role:inbox unseen"), + ("starred", "role:inbox flagged"), + ("archive", "role:archive"), + ("spam", "role:junk"), + ("trash", "role:trash"), + ("missing", "role:inbox"), + ] { + let params = json!({"provider":"m365","operation":"query","mailbox":mailbox}); + assert_eq!(resolve(¶ms).unwrap()["value"], expected, "{mailbox}"); + } + let search = + json!({"provider":"m365","operation":"query","mailbox":"trash","search":" hi "}); + assert_eq!(resolve(&search).unwrap()["value"], "text:hi"); + for operation in ["labelQuery", "addressQuery", "webMessageUrl", "webBoxUrl"] { + let params = json!({"provider":"m365","operation":operation,"value":"x"}); + assert_eq!(resolve(¶ms).unwrap()["value"], "", "{operation}"); + } + } + #[test] fn generated_ui_descriptor_matches_native_authority() { let source = include_str!("../../ui/providers/NativeDomain.js"); let value: Value = diff --git a/src/providers/domain_queries.json b/src/providers/domain_queries.json index 606107c..2d43f42 100644 --- a/src/providers/domain_queries.json +++ b/src/providers/domain_queries.json @@ -39,6 +39,16 @@ "spam": "role:junk", "trash": "role:trash" }, + "m365": { + "inbox": "role:inbox", + "unread": "role:inbox unseen", + "starred": "role:inbox flagged", + "sent": "role:sent", + "drafts": "role:drafts", + "archive": "role:archive", + "spam": "role:junk", + "trash": "role:trash" + }, "imap": { "all": "search:ALL", "inbox": "folder:INBOX", diff --git a/src/providers/m365/actions.rs b/src/providers/m365/actions.rs new file mode 100644 index 0000000..9145f4c --- /dev/null +++ b/src/providers/m365/actions.rs @@ -0,0 +1,214 @@ +//! Changing mail: `m365.modify`, `m365.trash`, `m365.untrash`, and what `mail.act` plans around them. +//! +//! Labels map as: remove/add `UNREAD` is mark read/unread, add/remove `STARRED` sets/clears the flag, +//! add `INBOX` moves to the Inbox and remove `INBOX` archives (a move to the `archive` folder); trash +//! moves to Deleted Items, untrash to the Inbox. Any other label is never ignored: every id lands in +//! `failedIds` with `invalid_params` and nothing is sent. A message gets its property update (one +//! `PATCH` for read state and flag together) before its move, and is moved only if the update worked. +//! +//! The answer `{succeededIds, failedIds, error}` names every requested id once, in request order. +//! `failedIds` means "not confirmed", never "not applied": an unknown outcome is not retried and the +//! next listing is the truth. Mutations are never cancelled, so a batch always answers for every id: +//! the call's deadline is checked before each request, and a request already begun is waited for +//! (every request ends by the deadline at the latest). A message whose first request could not begin +//! in time fails with `m365_timeout` and nothing sent. In a combined change (mark read and archive, +//! say) a message whose update ran but whose move could not begin in time fails with `m365_timeout` +//! too, its update applied: only a message whose whole change was made is reported as done. + +use futures_util::StreamExt as _; +use serde_json::{Map, Value, json}; +use tokio::time::Instant; + +use super::cancel::{self, guarded}; +use super::graph::Graph; +use super::model::Role; +use super::{ids, params, read}; + +const MAX_IDS: usize = 1000; +const INVALID: &str = "invalid_params"; +const NO_FOLDER: &str = "m365_folder_unavailable"; +const LATE: &str = "m365_timeout"; +pub(super) const ARCHIVE_REFUSAL: &str = "No Archive folder was found in this mailbox"; + +/// What a request does to each message. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(super) struct Plan { + pub read: Option, + pub flag: Option, + pub to: Option, +} + +/// The plan a label change asks for, and whether it names a label with no meaning here. An empty +/// change, or one asking for a state and its opposite, is `invalid_params`. +pub(super) fn plan(add: &[&str], remove: &[&str]) -> Result<(Plan, bool), &'static str> { + fn set(slot: &mut Option, value: T) -> Result<(), &'static str> { + match slot { + Some(old) if *old != value => Err(INVALID), + _ => { + *slot = Some(value); + Ok(()) + } + } + } + if add.is_empty() && remove.is_empty() { + return Err(INVALID); + } + let (mut plan, mut unsupported) = (Plan::default(), false); + for label in add { + match *label { + "UNREAD" => set(&mut plan.read, false)?, + "STARRED" => set(&mut plan.flag, true)?, + "INBOX" => set(&mut plan.to, Role::Inbox)?, + _ => unsupported = true, + } + } + for label in remove { + match *label { + "UNREAD" => set(&mut plan.read, true)?, + "STARRED" => set(&mut plan.flag, false)?, + // Leaving the Inbox with no other place named is archiving. + "INBOX" => set(&mut plan.to, Role::Archive)?, + _ => unsupported = true, + } + } + Ok((plan, unsupported)) +} + +pub(super) async fn run(method: &str, request: &Value) -> Result { + let (account, ids, plan, unsupported) = match method { + "m365.modify" => { + let map = params::object( + request, + &["accountId", "ids", "addLabelIds", "removeLabelIds"], + )?; + let (plan, unsupported) = plan( + ¶ms::labels(map, "addLabelIds")?, + ¶ms::labels(map, "removeLabelIds")?, + )?; + ( + params::account(map)?, + params::message_ids(map, MAX_IDS)?, + plan, + unsupported, + ) + } + "m365.trash" | "m365.untrash" => { + let map = params::object(request, &["accountId", "ids"])?; + let to = if method == "m365.trash" { + Role::Trash + } else { + Role::Inbox + }; + let plan = Plan { + to: Some(to), + ..Plan::default() + }; + ( + params::account(map)?, + params::message_ids(map, MAX_IDS)?, + plan, + false, + ) + } + _ => return Err("method_not_found"), + }; + if unsupported { + return Ok(reply(&ids, &vec![Err(INVALID); ids.len()])); + } + // Not dropped at a deadline, which would lose what was done: every request ends by `end`, and + // none is begun with less than a fifth of the call left. + let limit = cancel::limit(cancel::DEADLINE); + let end = Instant::now() + limit; + let last_start = end - limit / 5; + let graph = Graph::new(account).until(end); + read::forget(account, Some(&ids)); + let graph = &graph; + if let Some(role) = plan.to + && !graph.roles(false).await?.contains_key(&role) + { + // The mailbox has nowhere to put them: nothing is sent, every id says so. + return Ok(reply(&ids, &vec![Err(NO_FOLDER); ids.len()])); + } + let outcomes: Vec<_> = futures_util::stream::iter(ids.clone()) + .map(move |id| async move { change(graph, &id, plan, last_start).await }) + .buffered(4) + .collect() + .await; + Ok(reply(&ids, &outcomes)) +} + +/// One message's update and move, each begun only before `last_start`. +async fn change( + graph: &Graph, + id: &str, + plan: Plan, + last_start: Instant, +) -> Result<(), &'static str> { + let graph_id = ids::decode(id)?; + let in_time = || { + if Instant::now() < last_start { + Ok(()) + } else { + Err(LATE) + } + }; + if plan.read.is_some() || plan.flag.is_some() { + in_time()?; + graph.update(&graph_id, plan.read, plan.flag).await?; + } + if let Some(role) = plan.to { + in_time()?; + graph.move_to(&graph_id, role).await?; + } + Ok(()) +} + +fn reply(ids: &[String], outcomes: &[Result<(), &'static str>]) -> Value { + let (mut succeeded, mut failed, mut error) = (Vec::new(), Vec::new(), None); + for (id, outcome) in ids.iter().zip(outcomes) { + match outcome { + Ok(()) => succeeded.push(id), + Err(code) => { + failed.push(id); + error.get_or_insert(*code); + } + } + } + json!({"succeededIds": succeeded, "failedIds": failed, "error": error}) +} + +/// What `mail.act` may plan: whether there is an Archive and a Trash to move to (never a Junk), and +/// `refusals` with an `archive` entry when the mailbox has no Archive folder. A plan changes nothing, +/// so its token is refreshed without saving a rotated refresh token. +pub(crate) async fn planned_action_availability( + account: &str, + refusals: Value, +) -> Result { + let account = ids::account_id(account)?; + let roles = guarded(account, None, async { + Graph::read_only(account).roles(false).await + }) + .await?; + let has_archive = roles.contains_key(&Role::Archive); + Ok(crate::mail::action::ActionAvailability { + refusals: with_archive_refusal(refusals, has_archive), + mailboxes: json!({"archive": has_archive, "trash": roles.contains_key(&Role::Trash), "spam": false}), + mailbox_required: Value::Null, + rows_context: Value::Null, + }) +} + +/// `refusals` plus the `archive` sentence if the mailbox has no Archive folder; an existing one stays. +pub(super) fn with_archive_refusal(refusals: Value, has_archive: bool) -> Value { + if has_archive { + return refusals; + } + let mut map = match refusals { + Value::Object(map) => map, + Value::Null => Map::new(), + other => return other, + }; + map.entry("archive") + .or_insert_with(|| json!(ARCHIVE_REFUSAL)); + Value::Object(map) +} diff --git a/src/providers/m365/cancel.rs b/src/providers/m365/cancel.rs new file mode 100644 index 0000000..89d898b --- /dev/null +++ b/src/providers/m365/cancel.rs @@ -0,0 +1,124 @@ +//! Request tokens, `m365.cancel` and the deadline every call runs under. +//! +//! A listing, a message fetch or a verification may carry a `requestToken`; `m365.cancel` ends the +//! call registered under that account and token with `m365_cancelled`, dropping its requests where +//! they stand. Mutations and sends take no token and are never cancelled: a caller that gives up +//! cannot leave a half-applied batch it does not know about. + +use std::collections::HashMap; +use std::future::Future; +use std::sync::{Arc, LazyLock, Mutex, PoisonError}; +use std::time::Duration; + +use serde_json::{Value, json}; +use tokio::sync::Notify; + +/// Above the operation's own 20 seconds in `graph`, below the 30 the desktop waits. +pub(super) const DEADLINE: Duration = Duration::from_secs(25); +/// A full message: the body may take `MIME_BUDGET`, then the parse. Above that, below the desktop. +pub(super) const READ_DEADLINE: Duration = Duration::from_secs(70); +const MAX_REGISTERED: usize = 64; + +/// Test builds: a shorter deadline for the calls that follow. +#[cfg(test)] +pub(super) static SHORT_DEADLINE: Mutex> = Mutex::new(None); + +type Key = (String, String); +static CALLS: LazyLock>>> = LazyLock::new(Default::default); + +struct Registration(Key, Arc); + +impl Drop for Registration { + fn drop(&mut self) { + CALLS + .lock() + .unwrap_or_else(PoisonError::into_inner) + .remove(&self.0); + } +} + +pub(super) fn cancel(account: &str, token: &str) -> Value { + let calls = CALLS.lock().unwrap_or_else(PoisonError::into_inner); + let found = calls.get(&(account.to_owned(), token.to_owned())); + if let Some(notify) = found { + // A permit is kept, so a cancel that lands before the call first waits still ends it. + notify.notify_one(); + } + json!({"cancelled": found.is_some()}) +} + +/// Runs `work` for at most `DEADLINE`, or until `token` (when there is one) is cancelled; either +/// way `work` is dropped where it stands. A token already in use is `invalid_params`. +pub(super) async fn guarded( + account: &str, + token: Option<&str>, + work: impl Future>, +) -> Result { + bounded(account, token, DEADLINE, work).await +} + +/// Same as `guarded`, for a call whose caller waits `limit` (before a test build shortens it, as +/// `limit` says). +pub(super) async fn guarded_for( + account: &str, + limit: Duration, + work: impl Future>, +) -> Result { + bounded(account, None, limit, work).await +} + +/// The deadline a call given `limit` runs under: `limit`, or in a test build the one a test set. +pub(super) fn limit(limit: Duration) -> Duration { + #[cfg(test)] + if let Some(short) = *SHORT_DEADLINE + .lock() + .unwrap_or_else(PoisonError::into_inner) + { + return short; + } + limit +} + +/// Same as `guarded`, for a call that downloads a message body. +pub(super) async fn guarded_read( + account: &str, + token: Option<&str>, + work: impl Future>, +) -> Result { + bounded(account, token, READ_DEADLINE, work).await +} + +async fn bounded( + account: &str, + token: Option<&str>, + limit: Duration, + work: impl Future>, +) -> Result { + let registration = token + .map(|token| { + let mut calls = CALLS.lock().unwrap_or_else(PoisonError::into_inner); + let key = (account.to_owned(), token.to_owned()); + if calls.contains_key(&key) { + return Err("invalid_params"); + } + if calls.len() >= MAX_REGISTERED { + return Err("m365_request_limit"); + } + let notify = Arc::new(Notify::new()); + calls.insert(key.clone(), Arc::clone(¬ify)); + Ok(Registration(key, notify)) + }) + .transpose()?; + let bounded = async { + tokio::time::timeout(self::limit(limit), work) + .await + .unwrap_or(Err("m365_timeout")) + }; + match ®istration { + None => bounded.await, + Some(Registration(_, notify)) => tokio::select! { + result = bounded => result, + () = notify.notified() => Err("m365_cancelled"), + }, + } +} diff --git a/src/providers/m365/drafts.rs b/src/providers/m365/drafts.rs new file mode 100644 index 0000000..2cad7c8 --- /dev/null +++ b/src/providers/m365/drafts.rs @@ -0,0 +1,96 @@ +//! `m365.saveDraft` and `m365.deleteDraft`, in the shapes IMAP's take and answer. +//! +//! A draft is saved as Microsoft documents "create a message in MIME format": `POST /me/messages`, +//! `Content-Type: text/plain`, the MIME message as base64; Graph keeps it in Drafts. A save that +//! names the draft it replaces (`draftId`) deletes that one only after the new one exists, and a +//! failure to delete it is a warning on a saved draft, never a failed save. A save whose answer was +//! lost after it was written (the connection, a deadline, a 5xx) may have made a draft nobody knows +//! the id of: it is `m365_draft_unconfirmed`, and it is never repeated. +//! +//! A delete reads the item first and refuses (`m365_not_a_draft`) one that is not a draft, so an id +//! from anywhere else cannot remove a received or sent message; then `DELETE /me/messages/{id}`. + +use std::time::Duration; + +use serde_json::{Value, json}; +use tokio::time::{Instant, timeout}; + +use super::cancel::{self, guarded}; +use super::graph::{Fault, Graph, MESSAGE_TOO_LARGE}; +use super::model::Roles; +use super::{ids, params, send}; + +const NOT_A_DRAFT: &str = "m365_not_a_draft"; +const UNCONFIRMED: &str = "m365_draft_unconfirmed"; +const CLEANUP: Duration = Duration::from_secs(5); +/// The upload ends this long before the call does, which leaves the old copy's removal a moment. +const GRACE: Duration = Duration::from_secs(1); +const OLD_COPY_KEPT: &str = "The updated draft was saved, but the old copy could not be removed"; + +/// `m365.saveDraft {accountId, raw, draftId?}` answers `{saved, warning, id}`; `id` is the new draft's, +/// or empty when Graph's answer did not say it. Not cancelled: the save and the old copy's removal +/// answer together inside the call's deadline, below the desktop's wait. The upload's clock grows with +/// the draft's size, and the removal has five seconds or what is left, whichever is less. +pub(super) async fn save(request: &Value) -> Result { + let map = params::object(request, &["accountId", "raw", "draftId"])?; + let account = params::account(map)?; + let mime = send::decode(params::text(map, "raw")?)?; + let replaced = params::optional(map, "draftId")? + .filter(|id| !id.is_empty()) + .map(ids::decode) + .transpose()?; + let graph = Graph::new(account); + let limit = cancel::limit(cancel::DEADLINE); + let end = Instant::now() + limit; + let id = graph + .create_draft(&mime, limit.saturating_sub(GRACE)) + .await + .map_err(save_error)?; + let mut warning = ""; + if let Some(old) = replaced { + let removed = async { + let roles = graph.roles(false).await?; + remove(&graph, &roles, &old).await + }; + let left = end.saturating_duration_since(Instant::now()).min(CLEANUP); + if !matches!(timeout(left, removed).await, Ok(Ok(()))) { + warning = OLD_COPY_KEPT; + } + } + Ok(json!({"saved": true, "warning": warning, "id": id.unwrap_or_default()})) +} + +/// What a failed save proves. A refusal for its size is one whatever the status (as on a send); one +/// that may have reached Graph with no answer that says what came of it (a lost connection or a +/// deadline after it was written, any other 5xx) may have left a draft behind. +fn save_error(fault: Fault) -> &'static str { + if fault.code == MESSAGE_TOO_LARGE { + return MESSAGE_TOO_LARGE; + } + if fault.sent && (fault.status == 0 || fault.status >= 500) { + UNCONFIRMED + } else { + fault.code + } +} + +/// `m365.deleteDraft {accountId, id}` answers `{}`. +pub(super) async fn delete(request: &Value) -> Result { + let map = params::object(request, &["accountId", "id"])?; + let account = params::account(map)?; + let graph_id = ids::decode(params::message_id(map, "id")?)?; + guarded(account, None, async { + let graph = Graph::new(account); + let roles = graph.roles(false).await?; + remove(&graph, &roles, &graph_id).await?; + Ok(json!({})) + }) + .await +} + +async fn remove(graph: &Graph, roles: &Roles, graph_id: &str) -> Result<(), &'static str> { + if !graph.is_draft(graph_id, roles).await? { + return Err(NOT_A_DRAFT); + } + graph.delete(graph_id).await +} diff --git a/src/providers/m365/graph.rs b/src/providers/m365/graph.rs new file mode 100644 index 0000000..d56efab --- /dev/null +++ b/src/providers/m365/graph.rs @@ -0,0 +1,555 @@ +//! The wire to Microsoft Graph: one fixed host, a bearer token from `auth`, no redirects, no proxy, +//! bounded bodies and deadlines, and the throttling rules. +//! +//! - HTTPS only with certificate verification; a 3xx is an answer like any other, so the bearer token +//! is never sent again to a `Location`. No setting, environment variable or request field names a +//! host; only a test build can point the client at a local listener. +//! - Reads are repeated after 429, 503 and 504 (twice, after the `Retry-After` wait, never longer than +//! `MAX_RETRY_WAIT`: asking sooner is what a throttle punishes). Writes only after 429, which +//! Microsoft sends instead of acting; a 5xx or a lost connection after the bytes went out is an +//! unknown outcome and is not repeated. A 401 is repeated once with a fresh token. +//! - Every request, with its waits for a slot, a token and (after a 401) the refresh lane, has a clock +//! of its own: 20 seconds, and a message body (up to 24 MiB) 60, since 20 is a `m365_timeout` on a +//! slow link before those bytes arrive. A message uploaded whole (a send, a draft) gets a second +//! more for every 256 KiB, up to what its caller can wait (`upload_clock`). The call around it has +//! its own deadline (`cancel`), and a batch caps every request at its own end (`until`). Four +//! requests at most are in flight per mailbox (Microsoft's per-mailbox limit for an application). +//! - A change answered with a 2xx was applied: its answer is not read past what the caller needs, +//! and an answer too long to read does not make it a failure. +//! - Errors are static codes. What Graph says in an error body is read only for its `error.code`. + +use std::collections::HashMap; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, LazyLock, Mutex, OnceLock, PoisonError}; +use std::time::Duration; + +use reqwest::{Client, Method, Url, header}; +use serde_json::Value; +use tokio::sync::Semaphore; +use tokio::time::{Instant, sleep, timeout_at}; + +const ROOT: &str = "https://graph.microsoft.com/v1.0"; +const OPERATION: Duration = Duration::from_secs(20); +/// A full message, on its own clock. 24 MiB at about 3 Mbit/s, and still inside the longer wait +/// `reader.open` and the desktop give a Microsoft 365 message. +const MIME_BUDGET: Duration = Duration::from_secs(60); +const MAX_RETRY_WAIT: Duration = Duration::from_secs(5); +const MAX_RETRIES: usize = 2; +/// A repeat needs this much of the deadline left after its wait. +const MIN_RETRY_BUDGET: Duration = Duration::from_secs(2); +const MAX_ERROR_BODY: usize = 16 * 1024; +pub(super) const MAX_JSON: usize = 4 * 1024 * 1024; +/// An upload's clock grows by a second for every this many bytes of its body: about 2 Mbit/s. +const UPLOAD_RATE: usize = 256 * 1024; + +pub(super) const NOT_FOUND: &str = "m365_message_not_found"; +const DENIED: &str = "m365_access_denied"; +const AUTH: &str = "m365_auth_failed"; +const THROTTLED: &str = "m365_throttled"; +const SERVER: &str = "m365_server_error"; +const REQUEST: &str = "m365_invalid_request"; +pub(super) const TOO_LARGE: &str = "m365_response_too_large"; +/// Microsoft refused what was sent for its size. +pub(super) const MESSAGE_TOO_LARGE: &str = "m365_message_too_large"; + +/// The requests one mailbox may have in flight: four, each mailbox its own. +pub(super) fn slots(account: &str) -> Arc { + static SLOTS: LazyLock>>> = + LazyLock::new(Default::default); + let mut slots = SLOTS.lock().unwrap_or_else(PoisonError::into_inner); + Arc::clone( + slots + .entry(account.to_owned()) + .or_insert_with(|| Arc::new(Semaphore::new(4))), + ) +} + +/// A failed request: the code, the HTTP status (0 before an answer) and whether the request may +/// have reached the server. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) struct Fault { + pub code: &'static str, + pub status: u16, + pub sent: bool, +} + +fn before_send(code: &'static str) -> Fault { + Fault { + code, + status: 0, + sent: false, + } +} + +pub(super) struct Reply { + pub status: u16, + pub body: Vec, + retry_after: Option, +} + +/// Which failures a request is repeated after. +#[derive(Clone, Copy, PartialEq, Eq)] +pub(super) enum Mode { + Read, + Write, + Once, +} + +/// What every client of this provider is: no environment proxy and no redirect, so a bearer token is +/// never sent anywhere a server points. Tests add nothing and drop only the HTTPS requirement. +pub(super) fn builder() -> reqwest::ClientBuilder { + Client::builder() + .hickory_dns(true) + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .connect_timeout(Duration::from_secs(10)) +} + +#[cfg(test)] +pub(super) static LOCAL: std::sync::Mutex> = std::sync::Mutex::new(None); +/// Test builds: a shorter deadline for the operations that follow. +#[cfg(test)] +pub(super) static SHORT_OPERATION: std::sync::Mutex> = std::sync::Mutex::new(None); +/// Test builds: a shorter clock for a message body. Unset, the body keeps `MIME_BUDGET`. +#[cfg(test)] +pub(super) static SHORT_MIME: std::sync::Mutex> = std::sync::Mutex::new(None); + +/// How long a message body may take to arrive. Every other request has the operation's clock. +fn body_budget() -> Duration { + #[cfg(test)] + if let Some(short) = *SHORT_MIME.lock().unwrap_or_else(|e| e.into_inner()) { + return short; + } + MIME_BUDGET +} + +/// The client and the `/v1.0` root: fixed, or in a test build the local listener a test installed. +fn root() -> Result<(Client, Url), &'static str> { + #[cfg(test)] + if let Some((client, root)) = LOCAL.lock().unwrap_or_else(|e| e.into_inner()).clone() { + return Ok((client, root)); + } + static CLIENT: OnceLock> = OnceLock::new(); + let client = CLIENT.get_or_init(|| { + builder() + .https_only(true) + .build() + .map_err(|_| "m365_network_failed") + }); + let root = Url::parse(ROOT).map_err(|_| "m365_network_failed")?; + Ok((client.clone()?, root)) +} + +/// The code for an auth failure, in this provider's words. +pub(super) fn token_error(code: &'static str) -> &'static str { + match code { + "auth_signed_out" | "auth_client_missing" | "auth_client_invalid" => "m365_signed_out", + "auth_consent_required" => "m365_consent_required", + "auth_refresh_failed" => AUTH, + "auth_transport_failed" => "m365_network_failed", + "auth_timeout" => "m365_timeout", + "auth_response_too_large" | "auth_invalid_response" | "auth_redirect_refused" => { + "m365_invalid_response" + } + other => other, + } +} + +/// Percent-encodes a query value with `%20` for a space: not every service reads a `+` the same. +fn encode_query(pairs: &[(&str, &str)]) -> String { + let mut out = String::new(); + for (key, value) in pairs { + out.push(if out.is_empty() { '?' } else { '&' }); + out.push_str(key); + out.push('='); + for byte in value.bytes() { + if byte.is_ascii_alphanumeric() || b"-._~".contains(&byte) { + out.push(char::from(byte)); + } else { + out.push_str(&format!("%{byte:02X}")); + } + } + } + out +} + +/// `/?`. A segment is a word or an id, never a path step. +pub(super) fn url(segments: &[&str], query: &[(&str, &str)]) -> Result { + if segments + .iter() + .any(|s| s.is_empty() || *s == "." || *s == "..") + { + return Err(REQUEST); + } + let mut url = root()?.1; + url.path_segments_mut() + .map_err(|_| REQUEST)? + .extend(segments); + if !query.is_empty() { + url.set_query(Some(&encode_query(query)[1..])); + } + Ok(url) +} + +/// A `@odata.nextLink` (or the same in a page token) that may be followed: https on the Graph origin +/// exactly, no user name or fragment, a messages listing under `/v1.0/me/` with no dot segment or +/// escaped separator. Anything else is a hostile or forged link and the bearer token never goes there. +pub(super) fn next_link(raw: &str) -> Option { + if raw.len() > 8192 + || raw + .chars() + .any(|c| c.is_control() || c.is_whitespace() || c == '\\') + { + return None; + } + let (_, root) = root().ok()?; + let link = Url::parse(raw).ok()?; + let path = link.path(); + let safe = |segment: &str| { + let lower = segment.to_ascii_lowercase(); + !matches!(segment, "." | "..") + && !["%2f", "%5c", "%2e"].iter().any(|bad| lower.contains(bad)) + }; + (link.origin() == root.origin() + && link.username().is_empty() + && link.password().is_none() + && link.fragment().is_none() + && path.starts_with(&format!("{}/me/", root.path())) + && path.ends_with("/messages") + && path.split('/').skip(1).all(safe)) + .then_some(link) +} + +/// The JSON object of an answer. +pub(super) fn object(reply: &Reply) -> Result { + serde_json::from_slice::(&reply.body) + .ok() + .filter(Value::is_object) + .ok_or("m365_invalid_response") +} + +/// `error.code` of a Graph error body: ASCII word characters and dots, at most 128. +fn error_code(body: &[u8]) -> Option { + let value: Value = serde_json::from_slice(body).ok()?; + let code = value["error"]["code"].as_str()?; + (code.len() <= 128 + && code + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'.')) + .then(|| code.to_owned()) +} + +/// The code for an error answer. The body's `error.code` refines the status: Graph answers some +/// missing items with a 400. +fn fault(reply: &Reply) -> Fault { + let code = match error_code(&reply.body).as_deref().unwrap_or("") { + "ErrorItemNotFound" + | "ErrorInvalidIdMalformed" + | "ErrorInvalidMailboxItemId" + | "ResourceNotFound" + | "ErrorFolderNotFound" => NOT_FOUND, + "ErrorAccessDenied" + | "ErrorSendAsDenied" + | "MailboxNotEnabledForRESTAPI" + | "MailboxNotSupportedForRESTAPI" + | "Authorization_RequestDenied" => DENIED, + "InvalidAuthenticationToken" => AUTH, + // Exchange's response code for a message over the size the mailbox takes. + "ErrorMessageSizeExceeded" => MESSAGE_TOO_LARGE, + "ApplicationThrottled" | "ErrorServerBusy" | "MailboxConcurrency" | "TooManyRequests" => { + THROTTLED + } + _ => match reply.status { + 401 => AUTH, + 403 => DENIED, + 404 => NOT_FOUND, + 413 => MESSAGE_TOO_LARGE, + 429 => THROTTLED, + 400 | 405..=499 => REQUEST, + 503 | 504 if reply.retry_after.is_some() => THROTTLED, + 500..=599 => SERVER, + // A 3xx was not followed, and nothing else answers a request like ours. + _ => "m365_bad_response", + }, + }; + Fault { + code, + status: reply.status, + sent: true, + } +} + +/// A transport error as a fault. A failure to connect (refused, no route, a certificate the platform +/// does not trust) came before any byte of the request could be acted on. +fn classify(error: reqwest::Error) -> Fault { + if error.is_timeout() { + return Fault { + code: "m365_timeout", + status: 0, + sent: !error.is_connect(), + }; + } + if error.is_connect() || error.is_builder() { + let mut cause: Option<&(dyn std::error::Error + 'static)> = Some(&error); + while let Some(current) = cause { + // An `io::Error` hides what it wraps from `source()`: look through `get_ref` too. + let inner = current + .downcast_ref::() + .and_then(std::io::Error::get_ref); + if current.is::() || inner.is_some_and(|e| e.is::()) { + return before_send("m365_tls_failed"); + } + cause = current.source(); + } + return before_send("m365_network_failed"); + } + // The connection was up when it failed: the server may have received the request. + Fault { + code: "m365_network_failed", + status: 0, + sent: true, + } +} + +/// One operation's access to a mailbox: every request ends at its own deadline. +pub(super) struct Graph { + pub account: String, + operation: Duration, + /// A plan's look at the mailbox: a token refreshed for it does not save a rotated refresh token. + read_only: bool, + /// When set, no request runs past it: a batch's end. + until: Option, + /// Why there is no token, once there has been no token: the rest of the operation fails with it, + /// so a batch of fifty messages does not send a refused grant to the login host fifty times. + no_token: Mutex>, +} + +impl Graph { + pub(super) fn new(account: &str) -> Graph { + #[cfg(test)] + let operation = SHORT_OPERATION + .lock() + .unwrap_or_else(|e| e.into_inner()) + .unwrap_or(OPERATION); + #[cfg(not(test))] + let operation = OPERATION; + Graph { + account: account.to_owned(), + operation, + read_only: false, + until: None, + no_token: Default::default(), + } + } + + /// The same, with every request ending by `end` at the latest. + pub(super) fn until(self, end: Instant) -> Graph { + Graph { + until: Some(end), + ..self + } + } + + /// The clock of a request that uploads `len` bytes: the operation's, a second more for every + /// `UPLOAD_RATE` bytes, and never more than `budget`, the caller's own wait. + pub(super) fn upload_clock(&self, len: usize, budget: Duration) -> Duration { + let grown = self.operation + Duration::from_secs((len / UPLOAD_RATE) as u64); + grown.min(budget) + } + + /// For reads that change nothing, the keyring included (what `mail.act` plans from). + pub(super) fn read_only(account: &str) -> Graph { + Graph { + read_only: true, + ..Graph::new(account) + } + } + + /// One request, repeated as `mode` allows; only a 2xx answer comes back as `Ok`. + pub(super) async fn request( + &self, + mode: Mode, + method: Method, + url: &Url, + body: Option<(&'static str, &[u8])>, + ceiling: usize, + ) -> Result { + // A body larger than a JSON page is the MIME of a message, on a longer clock. + let clock = if ceiling > MAX_JSON { + body_budget() + } else { + self.operation + }; + self.request_within(mode, method, url, body, ceiling, clock) + .await + } + + /// The same on a clock of `clock`, or less if the operation ends sooner (`until`). + pub(super) async fn request_within( + &self, + mode: Mode, + method: Method, + url: &Url, + body: Option<(&'static str, &[u8])>, + ceiling: usize, + clock: Duration, + ) -> Result { + let (client, root) = root().map_err(before_send)?; + if url.origin() != root.origin() { + return Err(before_send(REQUEST)); + } + let (mut repeats, mut refreshed) = (0, false); + let mut deadline = Instant::now() + clock; + if let Some(until) = self.until { + deadline = deadline.min(until); + } + let slots = slots(&self.account); + // Set once this attempt's request is handed to the connection: a deadline before that sent + // nothing. Cleared for every attempt, since every status that is repeated (a 401, a write's + // 429) applied nothing: a deadline in the waits for the next attempt's slot or token is not a + // request that may have gone out. + let written = AtomicBool::new(false); + loop { + written.store(false, Ordering::SeqCst); + let exchange = async { + let _slot = slots.acquire().await.map_err(|_| before_send(REQUEST))?; + let held = *self.no_token.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(code) = held { + return Err(before_send(code)); + } + let token = if self.read_only { + crate::auth::access_token_readonly("m365", &self.account, "graph").await + } else { + crate::auth::access_token("m365", &self.account, "graph").await + }; + let token = token.map_err(|code| { + let code = token_error(code); + *self.no_token.lock().unwrap_or_else(|e| e.into_inner()) = Some(code); + before_send(code) + })?; + let mut bearer = header::HeaderValue::from_str(&format!("Bearer {token}")) + .map_err(|_| before_send(AUTH))?; + bearer.set_sensitive(true); + let mut request = client + .request(method.clone(), url.clone()) + .header(header::AUTHORIZATION, bearer) + .header("Prefer", "IdType=\"ImmutableId\""); + if let Some((kind, bytes)) = body { + request = request + .header(header::CONTENT_TYPE, kind) + .body(bytes.to_vec()); + } + written.store(true, Ordering::SeqCst); + let mut response = request.send().await.map_err(classify)?; + let status = response.status().as_u16(); + let retry_after = response + .headers() + .get(header::RETRY_AFTER) + .and_then(|value| value.to_str().ok()) + .and_then(|text| text.trim().parse::().ok()) + .map(Duration::from_secs); + let wanted = (200..300).contains(&status); + // These writes consume only the status. Once Graph confirmed + // them, a missing or broken optional body cannot undo success. + if wanted && ceiling == 0 { + return Ok(Reply { + status, + body: Vec::new(), + retry_after, + }); + } + let limit = if wanted { ceiling } else { MAX_ERROR_BODY }; + // Only a read needs its whole answer. An error is read for its code and a change that + // succeeded for what the caller asked of it: neither fails for being longer than that. + let whole = wanted && mode == Mode::Read; + let too_large = Fault { + code: TOO_LARGE, + status, + sent: true, + }; + if response.content_length().is_some_and(|n| n > limit as u64) { + if whole { + return Err(too_large); + } + if wanted { + return Ok(Reply { + status, + body: Vec::new(), + retry_after, + }); + } + } + let mut body = Vec::new(); + while let Some(chunk) = response.chunk().await.map_err(|error| Fault { + sent: true, + ..classify(error) + })? { + if chunk.len() > limit - body.len() { + if whole { + return Err(too_large); + } + body.clear(); + break; + } + body.extend_from_slice(&chunk); + } + Ok(Reply { + status, + body, + retry_after, + }) + }; + let reply = timeout_at(deadline, exchange).await.map_err(|_| Fault { + code: "m365_timeout", + status: 0, + sent: written.load(Ordering::SeqCst), + })??; + if (200..300).contains(&reply.status) { + return Ok(reply); + } + if reply.status == 401 && !refreshed { + // Refused before any work: a fresh token and once more. Forgetting the old token waits + // for the mailbox's refresh lane, which another request's refresh may hold for its whole + // exchange with the login host, so that wait ends at this request's deadline too. The + // 401 applied nothing, so a request that runs out of time there did nothing. + refreshed = true; + if timeout_at(deadline, crate::auth::invalidate(&self.account)) + .await + .is_err() + { + return Err(before_send("m365_timeout")); + } + continue; + } + let repeatable = match mode { + Mode::Read => matches!(reply.status, 429 | 503 | 504), + Mode::Write => reply.status == 429, + Mode::Once => false, + }; + if repeatable && repeats < MAX_RETRIES { + let wait = reply + .retry_after + .filter(|wait| !wait.is_zero()) + .unwrap_or(Duration::from_secs(1)); + let left = deadline.saturating_duration_since(Instant::now()); + if wait <= MAX_RETRY_WAIT && left >= wait + MIN_RETRY_BUDGET { + repeats += 1; + sleep(wait).await; + continue; + } + } + return Err(fault(&reply)); + } + } + + pub(super) async fn get(&self, url: &Url) -> Result { + let reply = self + .request(Mode::Read, Method::GET, url, None, MAX_JSON) + .await?; + object(&reply).map_err(|code| Fault { + code, + status: reply.status, + sent: true, + }) + } +} diff --git a/src/providers/m365/ids.rs b/src/providers/m365/ids.rs new file mode 100644 index 0000000..177d748 --- /dev/null +++ b/src/providers/m365/ids.rs @@ -0,0 +1,76 @@ +//! Identifiers and the small validators of what the desktop sends. +//! +//! A Graph message id (asked for as an immutable id, so a move keeps it) is an opaque, +//! case-sensitive string of up to about 150 URL-safe base64 characters. The body cache names a +//! file `.json` and spells every byte outside `[a-z0-9.-]` as `_xx`, refusing names over 255 +//! bytes, so the Omamail id is the lower-case base32 of the Graph id's ASCII bytes without +//! padding: one cache byte per character, 250 for the longest id, and exactly one spelling. + +const MIN_GRAPH_ID: usize = 8; +const MAX_GRAPH_ID: usize = 156; +const ALPHABET: &[u8; 32] = b"abcdefghijklmnopqrstuvwxyz234567"; +const INVALID: &str = "invalid_params"; + +pub(super) fn encode(graph_id: &str) -> Result { + let bytes = graph_id.as_bytes(); + // Anything outside URL-safe base64 could change meaning inside a URL path segment. + if !(MIN_GRAPH_ID..=MAX_GRAPH_ID).contains(&bytes.len()) + || !bytes + .iter() + .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'=')) + { + return Err("m365_bad_response"); + } + let (mut out, mut acc, mut bits) = (String::new(), 0u32, 0u32); + for &byte in bytes { + acc = acc << 8 | u32::from(byte); + bits += 8; + while bits >= 5 { + bits -= 5; + out.push(char::from(ALPHABET[(acc >> bits & 31) as usize])); + } + acc &= (1 << bits) - 1; + } + if bits > 0 { + out.push(char::from(ALPHABET[(acc << (5 - bits) & 31) as usize])); + } + Ok(out) +} + +/// The Graph id of an Omamail id. Re-encoding the result accepts one spelling only: no upper case, +/// padding, stray bits or length the codec would not have made. +pub(super) fn decode(id: &str) -> Result { + let (mut bytes, mut acc, mut bits) = (Vec::new(), 0u32, 0u32); + for c in id.bytes().take(250) { + let value = ALPHABET.iter().position(|&a| a == c).ok_or(INVALID)? as u32; + acc = acc << 5 | value; + bits += 5; + if bits >= 8 { + bits -= 8; + bytes.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + let graph = String::from_utf8(bytes).map_err(|_| INVALID)?; + (encode(&graph).as_deref() == Ok(id)) + .then_some(graph) + .ok_or(INVALID) +} + +pub(super) fn validate_message_id(id: &str) -> Result<(), &'static str> { + decode(id).map(|_| ()) +} + +/// `m365:` and a lower-case mailbox address, as the account registry spells one. +pub(super) fn account_id(id: &str) -> Result<&str, &'static str> { + let address = id.strip_prefix("m365:").ok_or(INVALID)?; + (id == id.to_lowercase() && id.len() <= 320 && crate::account::valid_email(address)) + .then_some(id) + .ok_or(INVALID) +} + +/// An application (client) id: a GUID, which is all the login endpoints take, in the one spelling +/// the keyring and the refresh lane know it by. +pub(super) fn client_id(text: &str) -> Result { + crate::auth::m365::client_id(text).ok_or(INVALID) +} diff --git a/src/providers/m365/mailbox.rs b/src/providers/m365/mailbox.rs new file mode 100644 index 0000000..0f32538 --- /dev/null +++ b/src/providers/m365/mailbox.rs @@ -0,0 +1,468 @@ +//! Microsoft Graph mail operations: the documented `/v1.0/me` API, typed and bounded. +//! +//! Folders are found by their well-known names. A listing is Graph's own paging: the token that +//! continues one carries the `@odata.nextLink` whole (Graph says not to take it apart), bound to the +//! query it continues and judged before the network is touched, so a forged token can send the +//! bearer token nowhere but to a Graph messages listing. + +use std::collections::HashMap; +use std::sync::{LazyLock, Mutex, PoisonError}; +use std::time::{Duration, Instant}; + +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use futures_util::StreamExt as _; +use reqwest::{Method, Url}; +use serde_json::{Value, json}; + +use super::graph::{Fault, Graph, MAX_JSON, Mode, NOT_FOUND, Reply, TOO_LARGE, next_link, url}; +use super::ids; +use super::model::{self, Folder, MAX_COUNT, Query, ROW_SELECT, Role, Roles, Scope}; + +pub(super) const MAX_PAGE: usize = 100; +/// The most a message may weigh: 24 MiB (mail servers are commonly set to accept more than the +/// 16 MiB other providers stop at). +pub(super) const MAX_MIME: usize = 24 * 1024 * 1024; +const FOLDER: &str = "m365_folder_unavailable"; +const BAD: &str = "m365_invalid_response"; +const FOLDER_SELECT: &str = "id,totalItemCount,unreadItemCount"; +/// A search whose whole page was Trash and Junk is asked again this many times, no more. +const MAX_FOLLOW: usize = 3; +const ROLES_TTL: Duration = Duration::from_secs(60); +const MAX_TOKEN: usize = 16 * 1024; + +static ROLES: LazyLock>> = LazyLock::new(Default::default); + +/// Drops what is remembered about a mailbox's folders. +pub(super) fn forget(account: &str) { + ROLES + .lock() + .unwrap_or_else(PoisonError::into_inner) + .remove(account); +} + +pub(super) struct Page { + pub ids: Vec, + pub rows: Vec, + pub next: String, + pub estimate: u64, +} + +impl Graph { + /// One folder by its well-known name; `None` when the mailbox has none (Graph answers 404). + pub(super) async fn folder(&self, role: Role) -> Result, &'static str> { + let url = url( + &["me", "mailFolders", role.well_known()], + &[("$select", FOLDER_SELECT)], + )?; + match self.get(&url).await { + Ok(value) => model::folder(&value).map(Some), + // Only "not found" is an absent folder; a 404 that names a refusal is the refusal. + Err(fault) if fault.status == 404 && fault.code == NOT_FOUND => Ok(None), + Err(fault) => Err(fault.code), + } + } + + /// The folder table, at most a minute old: one request per role, four at a time. A role the + /// mailbox lacks is absent (an Archive exists only once somebody has made one); a mailbox + /// without an Inbox is not a mailbox. + pub(super) async fn roles(&self, fresh: bool) -> Result { + if !fresh + && let Some((at, roles)) = ROLES + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get(&self.account) + && at.elapsed() < ROLES_TTL + { + return Ok(roles.clone()); + } + let mut found = futures_util::stream::iter(Role::ALL) + .map(|role| async move { (role, self.folder(role).await) }) + .buffered(4); + let mut roles = Roles::new(); + // The first failure ends the lookup: the requests still in the air are dropped. + while let Some((role, answer)) = found.next().await { + if let Some(folder) = answer? { + roles.insert(role, folder); + } + } + if !roles.contains_key(&Role::Inbox) { + return Err(FOLDER); + } + ROLES + .lock() + .unwrap_or_else(PoisonError::into_inner) + .insert(self.account.clone(), (Instant::now(), roles.clone())); + Ok(roles) + } + + /// One page of a listing. A folder query lists that folder newest first; a text query is Graph's + /// `$search` over the mailbox with Trash and Junk taken out. `cursor` is what `unseal` made of the + /// previous page's token. + /// + /// `estimate`: a folder's unread or total count (from `roles`), and otherwise the rows plus one if + /// more exist. Later pages repeat the first one's, and are the size the first one asked for. + pub(super) async fn page( + &self, + roles: &Roles, + query: &Query, + limit: usize, + cursor: Option, + ) -> Result { + if !(1..=MAX_PAGE).contains(&limit) { + return Err("invalid_params"); + } + // Graph's link carries the first page's `$top`: a later page is that size whatever is asked. + let limit = cursor.as_ref().map_or(limit, |cursor| cursor.size); + let mut fetched = match &query.scope { + Scope::Folder(role) => { + if !roles.contains_key(role) { + return Err(FOLDER); + } + let url = match &cursor { + Some(cursor) => cursor.link.clone(), + None => folder_url(*role, query, limit)?, + }; + self.fetch(url, limit, roles, &[]).await? + } + Scope::Text(text) => { + let url = match &cursor { + Some(cursor) => cursor.link.clone(), + None => { + let top = limit.to_string(); + let phrase = format!("\"{text}\""); + url( + &["me", "messages"], + &[ + ("$search", &phrase), + ("$top", &top), + ("$select", ROW_SELECT), + ], + )? + } + }; + let skip: Vec<&str> = [Role::Trash, Role::Junk] + .iter() + .filter_map(|role| roles.get(role).map(|folder| folder.id.as_str())) + .collect(); + self.fetch(url, limit, roles, &skip).await? + } + }; + let more = fetched.next.is_some(); + let fallback = fetched.rows.len() as u64 + u64::from(more); + let estimate = match (&cursor, &query.scope) { + (Some(cursor), _) => cursor.estimate, + (None, Scope::Folder(role)) if !query.flagged => roles + .get(role) + .map(|f| if query.unread { f.unread } else { f.total }) + .unwrap_or(fallback) + .max(fallback), + _ => fallback, + } + .min(MAX_COUNT); + let next = fetched + .next + .take() + .map(|link| seal(query, &link, estimate, limit)) + .transpose()? + .unwrap_or_default(); + Ok(Page { + ids: fetched.ids, + rows: fetched.rows, + next, + estimate, + }) + } + + /// The rows of one listing answer, and the link to the next. A message whose id cannot be carried is + /// left out, as is one in a `skip` folder; a search answer that was nothing else is asked for again + /// from its own link, `MAX_FOLLOW` times at most. + async fn fetch( + &self, + mut url: Url, + limit: usize, + roles: &Roles, + skip: &[&str], + ) -> Result { + for follow in 0.. { + let document = self.get(&url).await.map_err(|fault| { + if fault.status == 404 && fault.code == NOT_FOUND { + FOLDER + } else { + fault.code + } + })?; + let values = document["value"].as_array().ok_or(BAD)?; + if values.len() > limit { + return Err(BAD); + } + let next = match document.get("@odata.nextLink") { + None | Some(Value::Null) => None, + Some(Value::String(link)) => Some(next_link(link).ok_or(BAD)?), + Some(_) => return Err(BAD), + }; + let mut fetched = Fetched { + ids: Vec::new(), + rows: Vec::new(), + next: next.clone(), + }; + for value in values { + let parent = value["parentFolderId"].as_str().unwrap_or(""); + if skip.contains(&parent) { + continue; + } + if let Ok(row) = model::row(value, roles) { + fetched + .ids + .push(row["id"].as_str().unwrap_or("").to_owned()); + fetched.rows.push(row); + } + } + match next { + Some(next) + if fetched.rows.is_empty() && !skip.is_empty() && follow < MAX_FOLLOW => + { + url = next; + } + _ => return Ok(fetched), + } + } + unreachable!() + } + + /// The header properties of one message, by Graph id. + pub(super) async fn header(&self, graph_id: &str) -> Result { + let url = url(&["me", "messages", graph_id], &[("$select", ROW_SELECT)])?; + let mut message = self.get(&url).await.map_err(|fault| fault.code)?; + // The id asked for is the id the caller holds: the answer is not trusted to rename it. + message["id"] = json!(graph_id); + Ok(message) + } + + /// The whole message as MIME, at most `MAX_MIME` bytes. + pub(super) async fn mime(&self, graph_id: &str) -> Result, &'static str> { + let url = url(&["me", "messages", graph_id, "$value"], &[])?; + let reply = self + .request(Mode::Read, Method::GET, &url, None, MAX_MIME) + .await + .map_err(|fault| { + if fault.code == TOO_LARGE { + "m365_message_too_large" + } else { + fault.code + } + })?; + if reply.body.is_empty() { + return Err(BAD); + } + Ok(reply.body) + } + + /// Sets the read state and/or the flag of one message (`PATCH`, which is the same twice). + pub(super) async fn update( + &self, + graph_id: &str, + read: Option, + flagged: Option, + ) -> Result<(), &'static str> { + let mut body = serde_json::Map::new(); + if let Some(read) = read { + body.insert("isRead".into(), json!(read)); + } + if let Some(flagged) = flagged { + let status = if flagged { "flagged" } else { "notFlagged" }; + body.insert("flag".into(), json!({"flagStatus": status})); + } + let url = url(&["me", "messages", graph_id], &[])?; + let body = Value::Object(body).to_string(); + // Graph answers with the whole message, which is not read: the 2xx is what says it was done. + self.request( + Mode::Write, + Method::PATCH, + &url, + Some(("application/json", body.as_bytes())), + 0, + ) + .await + .map(|_| ()) + .map_err(|fault| fault.code) + } + + /// Moves one message to the folder a role names; its id stays (immutable ids). + pub(super) async fn move_to(&self, graph_id: &str, role: Role) -> Result<(), &'static str> { + let url = url(&["me", "messages", graph_id, "move"], &[])?; + let body = json!({"destinationId": role.well_known()}).to_string(); + self.request( + Mode::Write, + Method::POST, + &url, + Some(("application/json", body.as_bytes())), + 0, + ) + .await + .map(|_| ()) + .map_err(|fault| fault.code) + } + + /// `POST /me/` with the whole MIME message as base64 text, the one way Graph takes one, on + /// a clock that grows with its size up to `budget`. + async fn post_mime( + &self, + what: &str, + mode: Mode, + mime: &[u8], + ceiling: usize, + budget: Duration, + ) -> Result { + let text = base64::engine::general_purpose::STANDARD.encode(mime); + let url = url(&["me", what], &[]).map_err(|code| Fault { + code, + status: 0, + sent: false, + })?; + let clock = self.upload_clock(text.len(), budget); + let body = Some(("text/plain", text.as_bytes())); + self.request_within(mode, Method::POST, &url, body, ceiling, clock) + .await + } + + /// `POST /me/messages` with the MIME: Graph files it in Drafts as a draft and answers `201` with + /// the message, whose (immutable) id is the new draft's Omamail id when the answer could be read. + pub(super) async fn create_draft( + &self, + mime: &[u8], + budget: Duration, + ) -> Result, Fault> { + let reply = self + .post_mime("messages", Mode::Write, mime, MAX_JSON, budget) + .await?; + let id = serde_json::from_slice::(&reply.body) + .ok() + .and_then(|value| value["id"].as_str().and_then(|id| ids::encode(id).ok())); + Ok(id) + } + + /// Whether a message is a draft: Graph says so, or it is in the Drafts folder. + pub(super) async fn is_draft( + &self, + graph_id: &str, + roles: &Roles, + ) -> Result { + let url = url( + &["me", "messages", graph_id], + &[("$select", "isDraft,parentFolderId")], + )?; + let message = self.get(&url).await.map_err(|fault| fault.code)?; + let parent = message["parentFolderId"].as_str(); + Ok(message["isDraft"] == true + || roles + .get(&Role::Drafts) + .is_some_and(|drafts| parent == Some(drafts.id.as_str()))) + } + + /// `DELETE /me/messages/{id}`, answered `204`. + pub(super) async fn delete(&self, graph_id: &str) -> Result<(), &'static str> { + let url = url(&["me", "messages", graph_id], &[])?; + self.request(Mode::Write, Method::DELETE, &url, None, 0) + .await + .map(|_| ()) + .map_err(|fault| fault.code) + } + + /// `POST /me/sendMail` with the MIME, answered `202 Accepted`. Never repeated: what a failure + /// proves about the message is `send`'s decision. + pub(super) async fn send_mail(&self, mime: &[u8], budget: Duration) -> Result<(), Fault> { + let reply = self + .post_mime("sendMail", Mode::Once, mime, 0, budget) + .await?; + if reply.status == 202 { + Ok(()) + } else { + Err(Fault { + code: "m365_bad_response", + status: reply.status, + sent: true, + }) + } + } +} + +struct Fetched { + ids: Vec, + rows: Vec, + next: Option, +} + +fn folder_url(role: Role, query: &Query, limit: usize) -> Result { + let top = limit.to_string(); + // `$orderby` properties must lead `$filter` (Graph's `InefficientFilter`): an always-true bound + // on the sort property is how a flag filter keeps the newest-first order. + let mut filter = Vec::new(); + if query.unread || query.flagged { + filter.push("receivedDateTime ge 1900-01-01T00:00:00Z"); + } + if query.unread { + filter.push("isRead eq false"); + } + if query.flagged { + filter.push("flag/flagStatus eq 'flagged'"); + } + let filter = filter.join(" and "); + let mut params = vec![ + ("$top", top.as_str()), + ("$select", ROW_SELECT), + ("$orderby", "receivedDateTime desc"), + ]; + if !filter.is_empty() { + params.push(("$filter", filter.as_str())); + } + url( + &["me", "mailFolders", role.well_known(), "messages"], + ¶ms, + ) +} + +pub(super) fn seal( + query: &Query, + link: &Url, + estimate: u64, + size: usize, +) -> Result { + let token = URL_SAFE_NO_PAD + .encode(json!({"q":query.canonical(),"e":estimate,"l":size,"n":link.as_str()}).to_string()); + (token.len() <= MAX_TOKEN).then_some(token).ok_or(BAD) +} + +/// Where a listing continues: the link the client will follow, the first page's estimate and the size +/// its pages are. +#[derive(Debug, PartialEq, Eq)] +pub(super) struct Cursor { + pub link: Url, + pub estimate: u64, + pub size: usize, +} + +/// The cursor of a page token, which must be one of ours for this very query. +pub(super) fn unseal(token: &str, query: &Query) -> Result { + const INVALID: &str = "invalid_params"; + let bytes = URL_SAFE_NO_PAD + .decode(token.get(..MAX_TOKEN).unwrap_or(token)) + .map_err(|_| INVALID)?; + let value: Value = serde_json::from_slice(&bytes).map_err(|_| INVALID)?; + let estimate = value["e"] + .as_u64() + .filter(|e| *e <= MAX_COUNT) + .ok_or(INVALID)?; + let size = value["l"] + .as_u64() + .filter(|l| (1..=MAX_PAGE as u64).contains(l)) + .ok_or(INVALID)? as usize; + if token.len() > MAX_TOKEN || value["q"] != query.canonical() { + return Err(INVALID); + } + let link = next_link(value["n"].as_str().ok_or(INVALID)?).ok_or(INVALID)?; + Ok(Cursor { + link, + estimate, + size, + }) +} diff --git a/src/providers/m365/mod.rs b/src/providers/m365/mod.rs new file mode 100644 index 0000000..848ab17 --- /dev/null +++ b/src/providers/m365/mod.rs @@ -0,0 +1,76 @@ +//! Microsoft 365 mail over Microsoft Graph: the `m365.*` RPC surface. Read +//! [the provider notes](../../../docs/providers/m365.md) before changing it. + +mod actions; +mod cancel; +mod drafts; +mod graph; +mod ids; +mod mailbox; +mod model; +mod params; +mod read; +mod send; +mod signin; +mod verify; + +#[cfg(test)] +mod tests; + +use serde_json::Value; + +pub(crate) use actions::planned_action_availability; +pub(crate) use send::send_queued; +pub(crate) use verify::check; + +/// Dispatches every `m365.*` method. Each handler's future is boxed: they are large, and the backend's +/// workers have small stacks (see `backend::Session::dispatch`). +pub async fn call(method: &str, params: &Value) -> Result { + match method { + "m365.signInBegin" => Box::pin(signin::begin(params)).await, + "m365.signInPoll" => Box::pin(signin::poll(params)).await, + "m365.signOut" => Box::pin(signin::sign_out(params)).await, + "m365.verify" => Box::pin(verify::verify(params)).await, + "m365.list" => Box::pin(read::list(params)).await, + "m365.messages" => Box::pin(read::messages(params)).await, + "m365.read" => Box::pin(read::read(params)).await, + "m365.attachment" => Box::pin(read::attachment(params)).await, + "m365.modify" | "m365.trash" | "m365.untrash" => { + Box::pin(actions::run(method, params)).await + } + "m365.send" => Box::pin(send::send(params)).await, + "m365.saveDraft" => Box::pin(drafts::save(params)).await, + "m365.deleteDraft" => Box::pin(drafts::delete(params)).await, + "m365.sendAs" => send::send_as(params), + "m365.invalidate" => invalidate(params).await, + "m365.cancel" => { + let map = params::object(params, &["accountId", "requestToken"])?; + let token = params::request_token(map)?.ok_or("invalid_params")?; + Ok(cancel::cancel(params::account(map)?, token)) + } + _ => Err("method_not_found"), + } +} + +/// `m365.invalidate {accountId}`: forgets the tokens and what was remembered of the mailbox. The +/// keyring is not touched. The tokens are forgotten under the refresh lane, waited for at most +/// `signin::LANE_WAIT`. +async fn invalidate(params: &Value) -> Result { + let map = params::object(params, &["accountId"])?; + let account = params::account(map)?; + mailbox::forget(account); + read::forget(account, None); + signin::within_lane_wait(crate::auth::invalidate(account)).await?; + Ok(serde_json::json!({"invalidated": true})) +} + +/// Validates a message id before any lookup or mutation. +pub(crate) fn validate_message_id(id: &str) -> Result<(), &'static str> { + ids::validate_message_id(id) +} + +/// An Omamail message id for a Graph id, for tests of code that takes ids. +#[cfg(test)] +pub(crate) fn encode_id_for_test(graph_id: &str) -> String { + ids::encode(graph_id).unwrap() +} diff --git a/src/providers/m365/model.rs b/src/providers/m365/model.rs new file mode 100644 index 0000000..b878c32 --- /dev/null +++ b/src/providers/m365/model.rs @@ -0,0 +1,401 @@ +//! The mailbox as the desktop sees it: folder roles, the query language and Graph messages as the +//! Gmail-shaped rows the shared list and reader take. +//! +//! Every value in a Graph message is a stranger's. The shared summary reads header values as raw +//! header text, so a row's headers are built here so that nothing in a name or subject can start a +//! new header or recipient: names always travel as one RFC 2047 word, an address that is not a +//! plain addr-spec is not written as one, and control characters become spaces. + +use std::collections::HashMap; + +use base64::{Engine, engine::general_purpose::STANDARD}; +use chrono::{DateTime, Datelike, Utc}; +use serde_json::{Value, json}; +use unicode_normalization::UnicodeNormalization; + +use super::ids; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub(super) enum Role { + Inbox, + Sent, + Drafts, + Trash, + Junk, + Archive, +} + +impl Role { + pub(super) const ALL: [Role; 6] = [ + Role::Inbox, + Role::Sent, + Role::Drafts, + Role::Trash, + Role::Junk, + Role::Archive, + ]; + + /// Graph's well-known folder name, which is how a folder is found whatever it is called. + pub(super) fn well_known(self) -> &'static str { + match self { + Role::Inbox => "inbox", + Role::Sent => "sentitems", + Role::Drafts => "drafts", + Role::Trash => "deleteditems", + Role::Junk => "junkemail", + Role::Archive => "archive", + } + } + + pub(super) fn word(self) -> &'static str { + match self { + Role::Inbox => "inbox", + Role::Sent => "sent", + Role::Drafts => "drafts", + Role::Trash => "trash", + Role::Junk => "junk", + Role::Archive => "archive", + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) struct Folder { + pub id: String, + pub total: u64, + pub unread: u64, +} + +/// The folders a mailbox has, by role: a mailbox without an Archive has no `Archive` entry. +pub(super) type Roles = HashMap; + +/// The largest count a folder or a page reports; past this the figure is noise. +pub(super) const MAX_COUNT: u64 = 10_000_000; + +pub(super) fn folder(value: &Value) -> Result { + let id = value["id"] + .as_str() + .filter(|id| !id.is_empty() && id.len() <= 512 && !id.chars().any(char::is_control)) + .ok_or("m365_invalid_response")?; + let count = |name: &str| value[name].as_u64().unwrap_or(0).min(MAX_COUNT); + Ok(Folder { + id: id.to_owned(), + total: count("totalItemCount"), + unread: count("unreadItemCount"), + }) +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) enum Scope { + Folder(Role), + /// Words searched for across the mailbox (Trash and Junk are left out by the listing). + Text(String), +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) struct Query { + pub scope: Scope, + pub unread: bool, + pub flagged: bool, +} + +const MAX_TEXT: usize = 4096; + +/// `role: [unseen] [flagged]` or `text:`; keywords in any +/// case, anything else (an unprefixed query included) is `invalid_params`. +pub(super) fn parse(query: &str) -> Result { + const INVALID: &str = "invalid_params"; + if query.len() > MAX_TEXT + 64 || query.chars().any(char::is_control) { + return Err(INVALID); + } + let query = query.trim(); + let keyword = |prefix: &str| { + query + .get(..prefix.len()) + .filter(|head| head.eq_ignore_ascii_case(prefix)) + .map(|_| &query[prefix.len()..]) + }; + if let Some(text) = keyword("text:") { + if text.len() > MAX_TEXT { + return Err(INVALID); + } + // Graph reads what is inside `$search="..."` as KQL, which takes `from:x`, `OR`, wildcards and + // groups. The words are searched as words: whatever could be syntax (after NFKC, which turns + // fullwidth spellings into ASCII) is a word break, and a bare connector is lower-cased. + let normalized: String = text.nfkc().collect(); + let words: Vec = normalized + .split(|c: char| { + c.is_whitespace() + || matches!( + c, + '"' | '\\' | '(' | ')' | ':' | '<' | '>' | '=' | '~' | '*' + ) + || matches!(c, '\u{ab}' | '\u{bb}' | '\u{201c}'..='\u{201f}') + }) + .map(|word| word.trim_start_matches(['+', '-'])) + .filter(|word| !word.is_empty()) + .map(|word| match word { + "AND" | "OR" | "NOT" => word.to_ascii_lowercase(), + _ => word.to_owned(), + }) + .collect(); + if words.is_empty() { + return Err(INVALID); + } + return Ok(Query { + scope: Scope::Text(words.join(" ")), + unread: false, + flagged: false, + }); + } + let rest = keyword("role:").ok_or(INVALID)?; + if rest.starts_with(char::is_whitespace) { + return Err(INVALID); + } + let mut words = rest.split_whitespace(); + let name = words.next().ok_or(INVALID)?; + let role = Role::ALL + .into_iter() + .find(|role| name.eq_ignore_ascii_case(role.word())) + .ok_or(INVALID)?; + let (mut unread, mut flagged) = (false, false); + for word in words { + let flag = match word.to_ascii_lowercase().as_str() { + "unseen" => &mut unread, + "flagged" => &mut flagged, + _ => return Err(INVALID), + }; + if std::mem::replace(flag, true) { + return Err(INVALID); + } + } + Ok(Query { + scope: Scope::Folder(role), + unread, + flagged, + }) +} + +impl Query { + /// One spelling per meaning: the page token that continues a listing is bound to it. + pub(super) fn canonical(&self) -> String { + match &self.scope { + Scope::Text(text) => format!("text:{text}"), + Scope::Folder(role) => format!( + "role:{}{}{}", + role.word(), + if self.unread { " unseen" } else { "" }, + if self.flagged { " flagged" } else { "" } + ), + } + } +} + +/// What a listing and a header read ask Graph for: the properties `row` shows, and no body. +pub(super) const ROW_SELECT: &str = "id,parentFolderId,internetMessageId,subject,from,toRecipients,ccRecipients,receivedDateTime,sentDateTime,isRead,flag,isDraft,bodyPreview"; + +const HEADER_MAX: usize = 998; +const NAME_MAX: usize = 256; +const ADDRESS_MAX: usize = 320; + +fn cut(text: &str, max: usize) -> &str { + let mut end = max.min(text.len()); + while !text.is_char_boundary(end) { + end -= 1; + } + &text[..end] +} + +/// Header text from an untrusted string: control characters and line separators become spaces and +/// the ends are trimmed, at most `max` bytes of whole characters. +fn flat(value: &Value, max: usize) -> String { + let text: String = value + .as_str() + .unwrap_or("") + .chars() + .take(max) + .map(|c| { + if c.is_control() || matches!(c, '\u{2028}' | '\u{2029}') { + ' ' + } else { + c + } + }) + .collect(); + cut(text.trim(), max).to_owned() +} + +fn word(text: &str) -> String { + format!("=?UTF-8?B?{}?=", STANDARD.encode(text)) +} + +/// A conservative addr-spec: one `@`, none of the characters a header splitter or an encoded word +/// could take for syntax. +fn addr_spec(address: &str) -> bool { + address.len() <= ADDRESS_MAX + && address.matches('@').count() == 1 + && !address.starts_with('@') + && !address.ends_with('@') + && !address.contains("=?") + && !address.chars().any(|c| { + c.is_control() + || c.is_whitespace() + || matches!( + c, + '<' | '>' | '(' | ')' | '[' | ']' | '\\' | ',' | ';' | ':' | '"' + ) + }) +} + +fn mailbox(party: &Value) -> Option { + let address = flat(&party["emailAddress"]["address"], ADDRESS_MAX); + let name = flat(&party["emailAddress"]["name"], NAME_MAX); + if addr_spec(&address) { + if name.is_empty() || name.eq_ignore_ascii_case(&address) { + return Some(address); + } + return Some(format!("{} <{address}>", word(&name))); + } + // Without a trustworthy address only a name can be shown, with nothing in it that reads as one. + let shown = if name.is_empty() { address } else { name }; + let shown = shown.replace(['<', '>'], " "); + (!shown.trim().is_empty()).then(|| word(shown.trim())) +} + +fn mailboxes(list: &Value) -> String { + let mut out = String::new(); + for entry in list + .as_array() + .into_iter() + .flatten() + .take(200) + .filter_map(mailbox) + { + if out.len() + entry.len() + 2 > HEADER_MAX { + break; + } + if !out.is_empty() { + out.push_str(", "); + } + out.push_str(&entry); + } + out +} + +/// A usable instant: after the epoch and inside the years RFC 5322 can write. +fn instant(value: &Value) -> Option> { + let date = + DateTime::parse_from_rfc3339(value.as_str().filter(|text| text.len() <= 64)?).ok()?; + let date = date.with_timezone(&Utc); + (date.timestamp_millis() > 0 && date.year() <= 9999).then_some(date) +} + +/// The label ids the shared summary reads: folder role, read state and flag. +fn labels(message: &Value, roles: &Roles) -> Vec<&'static str> { + let parent = message["parentFolderId"].as_str().unwrap_or(""); + let role = Role::ALL + .into_iter() + .find(|role| !parent.is_empty() && roles.get(role).is_some_and(|f| f.id == parent)); + let mut labels = Vec::new(); + // A message Graph does not say is unread is not shown as new mail. + if message["isRead"].as_bool() == Some(false) { + labels.push("UNREAD"); + } + if message["flag"]["flagStatus"].as_str() == Some("flagged") { + labels.push("STARRED"); + } + if message["isDraft"].as_bool() == Some(true) || role == Some(Role::Drafts) { + labels.push("DRAFT"); + } + match role { + Some(Role::Inbox) => labels.push("INBOX"), + Some(Role::Sent) => labels.push("SENT"), + Some(Role::Trash) => labels.push("TRASH"), + Some(Role::Junk) => labels.push("SPAM"), + _ => {} + } + labels +} + +/// The preview as the shared summary expects it: HTML it strips and decodes, so `a < b` is escaped +/// (and `&#` split, which the decoder would turn into a typed control character). +fn snippet(value: &Value) -> String { + let text = flat(value, 8192) + .split_whitespace() + .collect::>() + .join(" "); + let text: String = text.replace("&#", "& #").chars().take(200).collect(); + text.trim_end() + .replace('&', "&") + .replace('<', "<") + .replace('>', ">") +} + +fn headers(message: &Value) -> Vec { + let subject = flat(&message["subject"], HEADER_MAX); + // The summary decodes encoded words again, so a typed one is sent as a word of its own. + let subject = if subject.contains("=?") { + word(cut(&subject, 600)) + } else { + subject + }; + let date = instant(&message["sentDateTime"]) + .or_else(|| instant(&message["receivedDateTime"])) + .map(|date| date.format("%a, %-d %b %Y %H:%M:%S +0000").to_string()); + let id = flat(&message["internetMessageId"], HEADER_MAX); + let id = id.trim_matches(['<', '>']); + let id = (!id.is_empty() + && !id + .chars() + .any(|c| c.is_whitespace() || c == '<' || c == '>')) + .then(|| format!("<{id}>")); + [ + ("From", mailbox(&message["from"])), + ("To", Some(mailboxes(&message["toRecipients"]))), + ("Cc", Some(mailboxes(&message["ccRecipients"]))), + ("Subject", Some(subject)), + ("Date", date), + ("Message-ID", id), + ] + .into_iter() + .filter_map(|(name, value)| Some(json!({"name":name,"value":value.filter(|v| !v.is_empty())?}))) + .collect() +} + +/// A header-only row (a listing's, or a `messages` answer's): Gmail's resource with headers and no +/// parts. A message whose id cannot be carried is an error and the caller leaves it out. +pub(super) fn row(message: &Value, roles: &Roles) -> Result { + let id = ids::encode(message["id"].as_str().ok_or("m365_invalid_response")?)?; + Ok(resource( + message, + roles, + id, + json!({ + "partId":"","mimeType":"text/plain","filename":"","headers":headers(message), + "body":{"size":0},"parts":[], + }), + )) +} + +/// The resource of a message with the payload the shared parser made of its MIME. The server's +/// words are only what MIME cannot know: the folder, the flags and when it arrived. +pub(super) fn full( + message: &Value, + roles: &Roles, + id: String, + payload: Value, + size: usize, +) -> Value { + let mut resource = resource(message, roles, id, payload); + resource["sizeEstimate"] = json!(size); + resource +} + +fn resource(message: &Value, roles: &Roles, id: String, payload: Value) -> Value { + let date = instant(&message["receivedDateTime"]).or_else(|| instant(&message["sentDateTime"])); + json!({ + "id":id,"threadId":id,"labelIds":labels(message, roles), + "internalDate":date.map(|date| date.timestamp_millis().to_string()).unwrap_or_default(), + "sizeEstimate":0,"snippet":snippet(&message["bodyPreview"]),"payload":payload, + }) +} diff --git a/src/providers/m365/params.rs b/src/providers/m365/params.rs new file mode 100644 index 0000000..b82df53 --- /dev/null +++ b/src/providers/m365/params.rs @@ -0,0 +1,118 @@ +//! Request parameters: every field named, typed and bounded before the registry is read, a credential +//! is touched or a socket is opened. A request is an object with exactly the keys its method defines +//! (an unknown key is a refusal, not an ignored typo); every refusal is `invalid_params` and echoes +//! no input byte. + +use serde_json::{Map, Value}; + +use super::ids; + +const INVALID: &str = "invalid_params"; + +pub(super) type Fields<'a> = &'a Map; + +/// The members of `params`, if it is an object with none outside `allowed`. +pub(super) fn object<'a>(params: &'a Value, allowed: &[&str]) -> Result, &'static str> { + let map = params.as_object().ok_or(INVALID)?; + if map.keys().any(|key| !allowed.contains(&key.as_str())) { + return Err(INVALID); + } + Ok(map) +} + +pub(super) fn text<'a>(map: Fields<'a>, key: &str) -> Result<&'a str, &'static str> { + map.get(key).and_then(Value::as_str).ok_or(INVALID) +} + +/// An optional string member; absent and null are `None`. +pub(super) fn optional<'a>(map: Fields<'a>, key: &str) -> Result, &'static str> { + match map.get(key) { + None | Some(Value::Null) => Ok(None), + Some(Value::String(text)) => Ok(Some(text)), + Some(_) => Err(INVALID), + } +} + +pub(super) fn account<'a>(map: Fields<'a>) -> Result<&'a str, &'static str> { + ids::account_id(text(map, "accountId")?) +} + +/// `requestToken`: absent, or 1 to 128 bytes without control characters. +pub(super) fn request_token<'a>(map: Fields<'a>) -> Result, &'static str> { + optional(map, "requestToken")? + .map(|token| { + (!token.is_empty() && token.len() <= 128 && !token.chars().any(char::is_control)) + .then_some(token) + .ok_or(INVALID) + }) + .transpose() +} + +pub(super) fn message_id<'a>(map: Fields<'a>, key: &str) -> Result<&'a str, &'static str> { + let id = text(map, key)?; + ids::validate_message_id(id)?; + Ok(id) +} + +/// 1 to `max` message ids; a repeated id is kept once, at its first place. +pub(super) fn message_ids(map: Fields, max: usize) -> Result, &'static str> { + let list = map.get("ids").and_then(Value::as_array).ok_or(INVALID)?; + if list.is_empty() || list.len() > max { + return Err(INVALID); + } + let mut out: Vec = Vec::with_capacity(list.len()); + for value in list { + let id = value.as_str().ok_or(INVALID)?; + ids::validate_message_id(id)?; + if !out.iter().any(|seen| seen == id) { + out.push(id.to_owned()); + } + } + Ok(out) +} + +/// An optional list of label names (at most 32, each 1 to 64 bytes without control characters); +/// what a name means is the action's business. +pub(super) fn labels<'a>(map: Fields<'a>, key: &str) -> Result, &'static str> { + let list = match map.get(key) { + None | Some(Value::Null) => return Ok(Vec::new()), + Some(Value::Array(list)) if list.len() <= 32 => list, + Some(_) => return Err(INVALID), + }; + list.iter() + .map(|value| { + value + .as_str() + .filter(|name| { + !name.is_empty() && name.len() <= 64 && !name.chars().any(char::is_control) + }) + .ok_or(INVALID) + }) + .collect() +} + +/// An integer member in `low..=high`; a float or a string is refused even if it looks whole. +pub(super) fn bounded(map: Fields, key: &str, low: u64, high: u64) -> Result { + map.get(key) + .and_then(Value::as_u64) + .filter(|n| (low..=high).contains(n)) + .map(|n| n as usize) + .ok_or(INVALID) +} + +pub(super) fn flag(map: Fields, key: &str) -> Result { + map.get(key).and_then(Value::as_bool).ok_or(INVALID) +} + +/// An attachment id as `message::parse` writes one: `part:` and dot-separated part numbers (none +/// for a message that is itself the attachment). +pub(super) fn attachment_id<'a>(map: Fields<'a>, key: &str) -> Result<&'a str, &'static str> { + let id = text(map, key)?; + let numbers = id.strip_prefix("part:").ok_or(INVALID)?; + let sound = id.len() <= 64 + && (numbers.is_empty() + || numbers + .split('.') + .all(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()))); + sound.then_some(id).ok_or(INVALID) +} diff --git a/src/providers/m365/read.rs b/src/providers/m365/read.rs new file mode 100644 index 0000000..d96258c --- /dev/null +++ b/src/providers/m365/read.rs @@ -0,0 +1,210 @@ +//! Reading mail: `m365.list`, `m365.messages`, `m365.read` and `m365.attachment`. +//! +//! A listing answers its page and the header-only rows together (`ids[i]` is `rows[i].id`) and keeps +//! the rows for a minute, so the `m365.messages` that follows for the same ids asks Graph nothing. +//! A full read is never cached (a message is up to 24 MiB). A message that is not there does not fail +//! a request that asked for others: it is left out and the answer says `"partial"`; the request fails +//! only when it got nothing and the reason was not "not found". + +use std::collections::VecDeque; +use std::sync::{LazyLock, Mutex, PoisonError}; +use std::time::{Duration, Instant}; + +use futures_util::StreamExt as _; +use serde_json::{Value, json}; +use tokio::sync::Semaphore; + +use super::cancel::{guarded, guarded_read}; +use super::graph::{Graph, NOT_FOUND}; +use super::mailbox::{MAX_MIME, MAX_PAGE, unseal}; +use super::model::{self, Roles}; +use super::{ids, params}; + +const ROW_TTL: Duration = Duration::from_secs(60); +const MAX_ROWS: usize = 256; +const MAX_MESSAGES: usize = 100; + +type CachedRow = (String, String, Instant, Value); +static ROWS: LazyLock>> = LazyLock::new(Default::default); +/// Parsing a hostile 24 MiB message costs hundreds of MiB while its tree exists: two at a time. +static PARSING: Semaphore = Semaphore::const_new(2); + +fn remember(account: &str, row: &Value) { + let Some(id) = row["id"].as_str() else { return }; + let mut rows = ROWS.lock().unwrap_or_else(PoisonError::into_inner); + rows.retain(|(a, i, at, _)| !(a == account && i == id) && at.elapsed() < ROW_TTL); + if rows.len() >= MAX_ROWS { + rows.pop_front(); + } + rows.push_back(( + account.to_owned(), + id.to_owned(), + Instant::now(), + row.clone(), + )); +} + +fn recall(account: &str, id: &str) -> Option { + ROWS.lock() + .unwrap_or_else(PoisonError::into_inner) + .iter() + .find(|(a, i, at, _)| a == account && i == id && at.elapsed() < ROW_TTL) + .map(|(.., row)| row.clone()) +} + +/// Drops the remembered rows of `account` (all of them, or only those of `ids`). +pub(super) fn forget(account: &str, ids: Option<&[String]>) { + ROWS.lock() + .unwrap_or_else(PoisonError::into_inner) + .retain(|(a, i, ..)| a != account || ids.is_some_and(|ids| !ids.contains(i))); +} + +pub(super) async fn list(request: &Value) -> Result { + let map = params::object( + request, + &["accountId", "query", "limit", "pageToken", "requestToken"], + )?; + let account = params::account(map)?; + let query = model::parse(params::text(map, "query")?)?; + let limit = params::bounded(map, "limit", 1, MAX_PAGE as u64)?; + // Judged before the network is touched: a forged token never reaches a request. + let cursor = params::optional(map, "pageToken")? + .filter(|token| !token.is_empty()) + .map(|token| unseal(token, &query)) + .transpose()?; + let token = params::request_token(map)?; + guarded(account, token, async { + let graph = Graph::new(account); + let roles = graph.roles(false).await?; + let page = graph.page(&roles, &query, limit, cursor).await?; + page.rows.iter().for_each(|row| remember(account, row)); + Ok(json!({ + "page": {"ids": page.ids, "nextPageToken": page.next, "estimate": page.estimate}, + "rows": page.rows, + })) + }) + .await +} + +pub(super) async fn messages(request: &Value) -> Result { + let map = params::object(request, &["accountId", "ids", "full", "requestToken"])?; + let account = params::account(map)?; + let wanted = params::message_ids(map, MAX_MESSAGES)?; + let full = params::flag(map, "full")?; + let token = params::request_token(map)?; + let run = async { + let graph = Graph::new(account); + let roles = graph.roles(false).await?; + let (graph, roles) = (&graph, &roles); + let results: Vec> = futures_util::stream::iter(wanted) + .map(move |id| async move { + if full { + full_message(graph, roles, &id).await + } else { + header_row(graph, roles, &id).await + } + }) + // Full messages are up to 24 MiB each: two at a time, as parses are. + .buffered(if full { 2 } else { 4 }) + .collect() + .await; + let (mut messages, mut cause, mut left_out) = (Vec::new(), None, false); + for result in results { + match result { + Ok(message) => messages.push(message), + Err(code) => { + left_out = true; + if code != NOT_FOUND { + cause.get_or_insert(code); + } + } + } + } + // A failure is the answer only when nothing came back and "not found" is not why. + if let (true, Some(cause)) = (messages.is_empty(), cause) { + return Err(cause); + } + let warning = if left_out { + json!("partial") + } else { + Value::Null + }; + Ok(json!({"messages": messages, "warning": warning, "continuation": null})) + }; + if full { + guarded_read(account, token, run).await + } else { + guarded(account, token, run).await + } +} + +async fn header_row(graph: &Graph, roles: &Roles, id: &str) -> Result { + if let Some(row) = recall(&graph.account, id) { + return Ok(row); + } + let message = graph.header(&ids::decode(id)?).await?; + let row = model::row(&message, roles)?; + remember(&graph.account, &row); + Ok(row) +} + +/// The resource of one message: its header properties and its MIME, asked for together, the MIME +/// parsed on a blocking thread behind the gate. +async fn full_message(graph: &Graph, roles: &Roles, id: &str) -> Result { + let graph_id = ids::decode(id)?; + let (message, mime) = tokio::try_join!(graph.header(&graph_id), graph.mime(&graph_id))?; + let size = mime.len(); + let payload = parse(mime).await?; + Ok(model::full(&message, roles, id.to_owned(), payload, size)) +} + +async fn parse(mime: Vec) -> Result { + let permit = PARSING.acquire().await.map_err(|_| "worker_failed")?; + tokio::task::spawn_blocking(move || { + let _permit = permit; + crate::message::parse_up_to(&mime, MAX_MIME) + }) + .await + .map_err(|_| "worker_failed")? + .map_err(|code| match code { + "message_too_large" | "too_many_mime_parts" => "m365_message_too_large", + _ => "m365_invalid_response", + }) +} + +/// `m365.read`: only complete messages are read here; header-only rows come from `m365.messages`. +pub(super) async fn read(request: &Value) -> Result { + let map = params::object(request, &["accountId", "id", "full"])?; + let account = params::account(map)?; + let id = params::message_id(map, "id")?; + if !params::flag(map, "full")? { + return Err("invalid_params"); + } + guarded_read(account, None, async { + let graph = Graph::new(account); + let roles = graph.roles(false).await?; + full_message(&graph, &roles, id).await + }) + .await +} + +/// `m365.attachment`: the bytes of one MIME part as `{data}` (unpadded base64url). The message is +/// read again and the part taken from its parse, so the id is the one a resource of that message named. +pub(super) async fn attachment(request: &Value) -> Result { + let map = params::object(request, &["accountId", "messageId", "attachmentId"])?; + let account = params::account(map)?; + let message = ids::decode(params::message_id(map, "messageId")?)?; + let part = params::attachment_id(map, "attachmentId")?; + guarded_read(account, None, async { + let payload = parse(Graph::new(account).mime(&message).await?).await?; + let mut stack = vec![&payload]; + while let Some(found) = stack.pop() { + if found["body"]["attachmentId"] == part { + return Ok(json!({"data": found["body"]["data"]})); + } + stack.extend(found["parts"].as_array().into_iter().flatten()); + } + Err("m365_attachment_not_found") + }) + .await +} diff --git a/src/providers/m365/send.rs b/src/providers/m365/send.rs new file mode 100644 index 0000000..9012b9b --- /dev/null +++ b/src/providers/m365/send.rs @@ -0,0 +1,109 @@ +//! `m365.send` and `m365.sendAs`. +//! +//! The message goes out as Microsoft documents "send a message in MIME format": `POST /me/sendMail`, +//! `Content-Type: text/plain`, the whole MIME message as base64, answered `202 Accepted`; Graph files +//! the copy in Sent Items itself. A Bcc header travels inside the MIME as composed. Microsoft's page +//! says nothing about Bcc in MIME; on one work tenant it was observed (not documented) that the blind +//! recipient got the message and the To recipient's copy had no Bcc header and did not name them. +//! +//! What a failure proves (the outbox treats the first three as "definitely refused", the rest as +//! "unknown: check Sent"): `m365_send_not_attempted` is nothing written (the account, the token, the +//! connection, a certificate) or a 401 or 429, which Microsoft sends instead of acting; +//! `m365_send_rejected` is Microsoft's own refusal (400, 403, 404, ...); `m365_message_too_large` is a +//! message over the ceiling, refused before any I/O, or one Microsoft refused for its size (a 413 or +//! `ErrorMessageSizeExceeded`); `m365_send_failed` is everything else (a lost connection or a deadline +//! after the request went out, a 5xx), and the message is never repeated. +//! +//! The upload's clock grows with the message (`Graph::upload_clock`) up to what the caller waits: a +//! call from the desktop answers inside its 30 seconds (`cancel::DEADLINE`), the outbox, which waits +//! a minute for a delivery and its draft's removal, gives it `QUEUED`. + +use std::time::Duration; + +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use serde_json::{Value, json}; + +use super::cancel::{self, guarded_for}; +use super::graph::{Fault, Graph, MESSAGE_TOO_LARGE}; +use super::params; + +const NOT_ATTEMPTED: &str = "m365_send_not_attempted"; +const REJECTED: &str = "m365_send_rejected"; +const FAILED: &str = "m365_send_failed"; +const TOO_LARGE: &str = MESSAGE_TOO_LARGE; +/// The outbox's wait for one delivery is a minute, five seconds of which remove the draft after it. +const QUEUED: Duration = Duration::from_secs(50); +/// The upload ends this long before its call does: a clock that ran out before the request was +/// written then says so, where the call's own deadline could not. +const GRACE: Duration = Duration::from_secs(1); + +/// The message bytes of a `raw` member, which must be exactly what `URL_SAFE_NO_PAD` would write: no +/// padding, whitespace, other alphabet or stray trailing bits. +pub(super) fn decode(raw: &str) -> Result, &'static str> { + let max = crate::message::MAX_MESSAGE; + if raw.len() > max.div_ceil(3) * 4 { + return Err(TOO_LARGE); + } + let bytes = URL_SAFE_NO_PAD.decode(raw).map_err(|_| "invalid_params")?; + if bytes.is_empty() || URL_SAFE_NO_PAD.encode(&bytes) != raw { + return Err("invalid_params"); + } + if bytes.len() > max { + return Err(TOO_LARGE); + } + Ok(bytes) +} + +pub(super) fn classify(fault: Fault) -> &'static str { + if fault.code == TOO_LARGE || fault.status == 413 { + return TOO_LARGE; + } + match (fault.status, fault.sent) { + (0, false) => NOT_ATTEMPTED, + (0, true) => FAILED, + (400 | 403 | 404 | 405 | 409 | 415 | 422, _) => REJECTED, + (401 | 429, _) => NOT_ATTEMPTED, + _ => FAILED, + } +} + +/// `m365.send`, asked by the desktop. +pub(super) async fn send(request: &Value) -> Result { + deliver(request, cancel::DEADLINE).await +} + +/// `m365.send` for the outbox, which waits longer for a delivery than the desktop does. +pub(crate) async fn send_queued(request: &Value) -> Result { + deliver(request, QUEUED).await +} + +async fn deliver(request: &Value, wait: Duration) -> Result { + let map = params::object(request, &["accountId", "raw"])?; + let account = params::account(map)?; + let mime = decode(params::text(map, "raw")?)?; + let graph = Graph::new(account); + let limit = cancel::limit(wait); + let sent = guarded_for(account, limit, async { + let budget = limit.saturating_sub(GRACE); + Ok(graph.send_mail(&mime, budget).await.map_err(classify)) + }) + .await; + match sent { + Ok(Ok(())) => Ok(json!({"sent": true, "warning": ""})), + Ok(Err(code)) => Err(code), + // The guard's own deadline fired: the request may have gone out. + Err(_) => Err(FAILED), + } +} + +/// `m365.sendAs`: the mailbox's own address, the only identity there is; no network, no credential. +pub(super) fn send_as(request: &Value) -> Result { + let map = params::object(request, &["accountId"])?; + let account = params::account(map)?; + Ok(json!([{ + "email": account.strip_prefix("m365:").unwrap_or(account), + "displayName": "", + "isPrimary": true, + "isDefault": true, + }])) +} diff --git a/src/providers/m365/signin.rs b/src/providers/m365/signin.rs new file mode 100644 index 0000000..48a5e29 --- /dev/null +++ b/src/providers/m365/signin.rs @@ -0,0 +1,532 @@ +//! Device-code sign-in: `m365.signInBegin`, `m365.signInPoll` and `m365.signOut`. +//! +//! One code asks Microsoft only for Graph scopes (`auth::m365::SCOPE`), so one entry of the code at +//! microsoft.com/devicelogin grants mail and calendar together. Every request to the login host goes +//! through `auth::m365`; nothing a token or a device code is leaves Rust. +//! +//! - `signInBegin {accountId, address, clientId}` asks the device-code endpoint of the address's +//! organization (see `auth::m365::login`) for a code and keeps the `device_code` here, in a table of +//! at most four requests, each until Microsoft's own expiry (at most 30 minutes). What goes back is +//! a unique `requestId` (a handle, not a secret), the `userCode` to type and the page to type it at. +//! - `signInPoll {requestId}` makes one token request, and only when the interval Microsoft gave has +//! passed since the last: a call sooner is answered `pending` without contacting Microsoft, so any +//! rhythm of the caller is safe. There is no background task; an abandoned request just lapses. The +//! exchange runs on a task of its own: a caller that goes away cannot stop it between Microsoft +//! handing over a refresh token and the keyring saving it. A poll waits for it at most `POLL_WAIT` +//! (a keyring may be waiting for somebody to unlock it) and then answers `pending` with the warning +//! `m365_exchange_running`: the exchange goes on, and a later poll of the same request has its +//! outcome. That warning is this case's alone: a token request that failed in transit (its own +//! `m365_timeout` included) is over, and saved nothing. +//! - A grant is judged before anything is stored: the Graph permissions must be in it, there must be a +//! refresh token, and the id token must name the address the code was started for by one of its +//! `email`, `preferred_username` or `upn` claims (a sign-in name can differ from the mail address), +//! else `m365_wrong_account`, or `m365_auth_failed` when it names nobody, and nothing is kept. Then +//! the refresh token goes to the keyring; `m365.verify` is the caller's next step. +//! - A finished request answers the same final state for five minutes; an unknown one is `expired` (the +//! usual cause is a backend restart between begin and poll). +//! - `signOut` deletes the keyring item under the refresh lane, which a refresh holds until its keyring +//! save is done. It waits for the lane at most `LANE_WAIT` and then answers `m365_timeout`: the +//! sign-out may not have been done, and the window says so rather than outwaiting its own 30 seconds. + +use std::collections::HashMap; +use std::sync::{LazyLock, Mutex, MutexGuard, PoisonError}; +use std::time::Duration; + +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use tokio::time::Instant; +use zeroize::Zeroizing; + +use super::{ids, mailbox, params, read}; +use crate::auth::m365::{ + SCOPE, change, consent_needed, login, login_at, permissions_granted, save_grant, tenant_unknown, +}; + +const MAX_PENDING: usize = 4; +const DONE_TTL: Duration = Duration::from_secs(5 * 60); +/// How long a poll waits for its exchange: above the login request's own 20 seconds, below the 30 the +/// desktop waits for an answer. +const POLL_WAIT: Duration = Duration::from_secs(24); +/// The warning of a poll that stopped waiting while its exchange goes on, which may yet save a grant. +const EXCHANGE_RUNNING: &str = "m365_exchange_running"; +/// How long a sign-out or an invalidation waits for the mailbox's refresh lane, which a refresh holds +/// for its whole exchange and keyring save (a keyring may be waiting for somebody to unlock it): below +/// the 30 seconds the desktop waits for an answer, after which it stops the whole backend. +const LANE_WAIT: Duration = Duration::from_secs(20); +const GRANT_TYPE: &str = "urn:ietf:params:oauth:grant-type:device_code"; +/// The pages Microsoft sends people to for a device code. +const PAGES: [&str; 4] = [ + "microsoft.com", + "www.microsoft.com", + "login.microsoft.com", + "login.microsoftonline.com", +]; +const FAILED: &str = "m365_auth_failed"; +const BAD: &str = "m365_invalid_response"; + +struct Pending { + account: String, + address: String, + client_id: String, + /// The login authority that answered the device-code request: the token requests go there too. + authority: String, + device_code: Zeroizing, + expires: Instant, + interval: Duration, + next_poll: Instant, + /// A token request of this sign-in is in the air. + polling: bool, +} + +#[derive(Clone)] +struct Done { + state: &'static str, + error: Option<&'static str>, + signed_in_as: Option, +} + +enum Entry { + Pending(Pending), + Done(Done, Instant), +} + +static FLOWS: LazyLock>> = LazyLock::new(Default::default); + +#[cfg(test)] +/// Test builds: Microsoft's interval is replaced, so a test need not wait it out. +pub(super) static INTERVAL: Mutex> = Mutex::new(None); + +#[cfg(test)] +/// Test builds: a shorter wait of a poll for its exchange. +pub(super) static SHORT_POLL_WAIT: Mutex> = Mutex::new(None); + +#[cfg(test)] +/// Test builds: a shorter wait of a sign-out or an invalidation for the refresh lane. +pub(super) static SHORT_LANE_WAIT: Mutex> = Mutex::new(None); + +/// `work`, which waits for the mailbox's refresh lane, or `m365_timeout` once `LANE_WAIT` has passed: +/// a wait still for the lane then did nothing, and one that got it finishes on a task of its own. +pub(super) async fn within_lane_wait( + work: impl Future>, +) -> Result { + #[cfg(test)] + let short = *SHORT_LANE_WAIT + .lock() + .unwrap_or_else(PoisonError::into_inner); + #[cfg(not(test))] + let short: Option = None; + tokio::time::timeout(short.unwrap_or(LANE_WAIT), work) + .await + .map_err(|_| "m365_timeout")? +} + +fn poll_wait() -> Duration { + #[cfg(test)] + if let Some(wait) = *SHORT_POLL_WAIT + .lock() + .unwrap_or_else(PoisonError::into_inner) + { + return wait; + } + POLL_WAIT +} + +/// The wait between two token requests of one sign-in: what Microsoft said. +fn between_polls(told: u64) -> Duration { + #[cfg(test)] + if let Some(wait) = *INTERVAL.lock().unwrap_or_else(PoisonError::into_inner) { + return wait; + } + Duration::from_secs(told) +} + +fn flows() -> MutexGuard<'static, HashMap> { + FLOWS.lock().unwrap_or_else(PoisonError::into_inner) +} + +fn done(state: &'static str, error: Option<&'static str>) -> Done { + Done { + state, + error, + signed_in_as: None, + } +} + +fn answer(done: &Done) -> Value { + let mut out = json!({"state": done.state}); + if let Some(error) = done.error { + out["error"] = json!(error); + } + if let Some(who) = &done.signed_in_as { + out["signedInAs"] = json!(who); + } + out +} + +/// Drops what has lapsed: a pending request past its expiry becomes `expired`, a finished one past +/// `DONE_TTL` goes. +fn tidy(map: &mut HashMap) { + let now = Instant::now(); + map.retain(|_, entry| !matches!(entry, Entry::Done(_, until) if *until <= now)); + for entry in map.values_mut() { + if matches!(entry, Entry::Pending(p) if p.expires <= now && !p.polling) { + *entry = Entry::Done(done("expired", None), now + DONE_TTL); + } + } +} + +/// A short unique handle for a request. It protects nothing: the device code is the secret. +fn handle() -> String { + static COUNT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let count = COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |since| since.as_nanos()); + let digest = Sha256::digest(format!("{count}:{now}:{}", std::process::id())); + digest + .iter() + .take(16) + .map(|byte| format!("{byte:02x}")) + .collect() +} + +/// `https` on one of Microsoft's sign-in pages, with nothing added: no user name, port or fragment. +pub(super) fn verification_uri(text: &str) -> Option { + if text.len() > 256 || text.chars().any(|c| c.is_control() || c.is_whitespace()) { + return None; + } + let url = reqwest::Url::parse(text).ok()?; + (url.scheme() == "https" + && PAGES.contains(&url.host_str()?) + && url.port().is_none() + && url.username().is_empty() + && url.password().is_none() + && url.fragment().is_none()) + .then(|| url.to_string()) +} + +/// A token, secret or code as Microsoft sends one: non-empty, bounded, printable. +fn secret(value: &Value) -> Option<&str> { + value + .as_str() + .filter(|text| !text.is_empty() && text.len() <= 16 * 1024) + .filter(|text| !text.chars().any(|c| c.is_control() || c.is_whitespace())) +} + +/// The code for a failed device-code or token request that is not one of the flow's own states. +pub(super) fn flow_error(status: u16, body: &Value) -> &'static str { + match (status, body["error"].as_str().unwrap_or("")) { + (429, _) => "m365_throttled", + (500..=599, _) => "m365_server_error", + _ if consent_needed(body) => "m365_consent_required", + _ if tenant_unknown(body) => "m365_tenant_invalid", + (_, "access_denied") => "m365_access_denied", + _ => FAILED, + } +} + +/// The code for a login request that failed in transit. +fn link_error(code: &'static str) -> &'static str { + match code { + "auth_transport_failed" => "m365_network_failed", + "auth_timeout" => "m365_timeout", + _ => BAD, + } +} + +pub(super) async fn begin(request: &Value) -> Result { + let map = params::object(request, &["accountId", "address", "clientId"])?; + let account = params::account(map)?; + // The id is the address, lower-cased: nothing else is the same mailbox. + let address = params::text(map, "address")?.to_ascii_lowercase(); + if format!("m365:{address}") != account { + return Err("invalid_params"); + } + let client_id = ids::client_id(params::text(map, "clientId")?)?; + let (authority, status, body) = login( + &address, + "device", + &[("client_id", &client_id), ("scope", SCOPE)], + ) + .await + .map_err(link_error)?; + if status != 200 { + return Err(flow_error(status, &body)); + } + let device_code = secret(&body["device_code"]).ok_or(BAD)?; + let user_code = body["user_code"] + .as_str() + .filter(|code| { + (4..=32).contains(&code.len()) + && code.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') + }) + .ok_or(BAD)?; + let uri = body["verification_uri"] + .as_str() + .and_then(verification_uri) + .ok_or(BAD)?; + let lifetime = body["expires_in"].as_u64().unwrap_or(0).clamp(60, 30 * 60); + let told = body["interval"].as_u64().unwrap_or(5).clamp(1, 60); + let interval = between_polls(told); + let request_id = handle(); + let now = Instant::now(); + let mut map = flows(); + tidy(&mut map); + let pending = |entry: &Entry| match entry { + Entry::Pending(p) => Some(p.expires), + Entry::Done(..) => None, + }; + if map.values().filter_map(pending).count() >= MAX_PENDING { + // The newest request wins: the oldest pending one gives way. + let oldest = map + .iter() + .filter_map(|(id, entry)| pending(entry).map(|expires| (expires, id.clone()))) + .min() + .map(|(_, id)| id); + oldest.iter().for_each(|id| drop(map.remove(id))); + } + map.insert( + request_id.clone(), + Entry::Pending(Pending { + account: account.to_owned(), + address, + client_id, + authority, + device_code: Zeroizing::new(device_code.to_owned()), + expires: now + Duration::from_secs(lifetime), + interval, + next_poll: now + interval, + polling: false, + }), + ); + Ok(json!({ + "requestId": request_id, + "userCode": user_code, + "verificationUri": uri, + "expiresIn": lifetime, + "interval": told, + })) +} + +/// What one token request of a sign-in came to. +enum Step { + /// Ask again after the interval; `warning` names a transient failure, `slower` a `slow_down`. + Pending { + warning: Option<&'static str>, + slower: bool, + }, + Final(Done), +} + +pub(super) async fn poll(request: &Value) -> Result { + let map = params::object(request, &["requestId"])?; + let id = params::text(map, "requestId")?; + if id.len() != 32 || !id.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) { + return Err("invalid_params"); + } + let id = id.to_owned(); + let (account, address, client_id, authority, device_code) = { + let mut map = flows(); + tidy(&mut map); + match map.get_mut(&id) { + None => return Ok(answer(&done("expired", None))), + Some(Entry::Done(done, _)) => return Ok(answer(done)), + Some(Entry::Pending(pending)) => { + let now = Instant::now(); + if pending.polling || now < pending.next_poll { + return Ok(json!({"state": "pending"})); + } + pending.polling = true; + pending.next_poll = now + pending.interval; + ( + pending.account.clone(), + pending.address.clone(), + pending.client_id.clone(), + pending.authority.clone(), + pending.device_code.clone(), + ) + } + } + }; + // A task of its own, which also records the outcome: once Microsoft has given a refresh token for + // the code it must be saved, and the request must say so, whether or not the caller is still there. + let exchanged = tokio::spawn(async move { + let step = exchange( + &id, + &account, + &address, + &client_id, + &authority, + &device_code, + ) + .await; + let mut map = flows(); + match step { + Step::Final(done) => { + let out = answer(&done); + map.insert(id, Entry::Done(done, Instant::now() + DONE_TTL)); + out + } + Step::Pending { warning, slower } => { + if let Some(Entry::Pending(pending)) = map.get_mut(&id) { + pending.polling = false; + if slower { + pending.interval = (pending.interval + Duration::from_secs(5)) + .min(Duration::from_secs(60)); + } + pending.next_poll = Instant::now() + pending.interval; + } + let mut out = json!({"state": "pending"}); + if let Some(warning) = warning { + out["warning"] = json!(warning); + } + out + } + } + }); + match tokio::time::timeout(poll_wait(), exchanged).await { + Ok(answer) => answer.map_err(|_| "worker_failed"), + // Still exchanging or saving: the task carries on and records what it came to. + Err(_) => Ok(json!({"state": "pending", "warning": EXCHANGE_RUNNING})), + } +} + +async fn exchange( + id: &str, + account: &str, + address: &str, + client_id: &str, + authority: &str, + device_code: &str, +) -> Step { + let fields = [ + ("client_id", client_id), + ("grant_type", GRANT_TYPE), + ("device_code", device_code), + ]; + let (status, body) = match login_at(authority, "token", &fields).await { + Ok(answer) => answer, + Err(code) => { + return Step::Pending { + warning: Some(link_error(code)), + slower: false, + }; + } + }; + if status != 200 { + return match body["error"].as_str().unwrap_or("") { + "authorization_pending" => Step::Pending { + warning: None, + slower: false, + }, + "slow_down" => Step::Pending { + warning: None, + slower: true, + }, + "authorization_declined" => Step::Final(done("declined", None)), + "expired_token" | "code_expired" => Step::Final(done("expired", None)), + _ => match flow_error(status, &body) { + // Throttling and a server's own trouble are the service's, not the flow's: keep asking. + code @ ("m365_throttled" | "m365_server_error") => Step::Pending { + warning: Some(code), + slower: false, + }, + code => Step::Final(done("failed", Some(code))), + }, + }; + } + Step::Final(grant(id, account, address, client_id, &body).await) +} + +/// A successful token response: judged, then kept. +async fn grant(id: &str, account: &str, address: &str, client_id: &str, body: &Value) -> Done { + let failed = |code| done("failed", Some(code)); + if secret(&body["access_token"]).is_none() { + return failed(BAD); + } + if let Some(scope) = body["scope"].as_str() + && !permissions_granted(scope) + { + return failed("m365_consent_required"); + } + // Without a refresh token (`offline_access`) a sign-in would be signed out within the hour. + let Some(refresh) = secret(&body["refresh_token"]) else { + return failed(BAD); + }; + // Whose code was it? An id token that names accounts, none of them the address asked for, is a + // code entered as somebody else, and nothing of it is kept; nor of one that names nobody (`openid` + // and `email` were asked for), since whose it is cannot be told. + let names = claim_names(body["id_token"].as_str().unwrap_or("")); + if names.is_empty() { + return failed(FAILED); + } + if !names.iter().any(|name| name == address) { + return Done { + signed_in_as: names.into_iter().next(), + ..failed("m365_wrong_account") + }; + } + // A sign-out can end this flow before or during its keyring write. The pending checks and + // rollback share the refresh lane, so that rollback cannot delete a newer flow's saved grant. + let request = id.to_owned(); + match save_grant(account, client_id, refresh, move || pending(&request)).await { + Ok(true) => {} + Ok(false) => return done("expired", None), + Err(code) => return failed(code), + } + mailbox::forget(account); + read::forget(account, None); + done("signedIn", None) +} + +fn pending(id: &str) -> bool { + matches!(flows().get(id), Some(Entry::Pending(_))) +} + +/// The names an id token gives the person (`preferred_username`, `email`, `upn`), trimmed and +/// lower-cased, in that order and without empty ones; the first is what the browser signed in as. The +/// token is Microsoft's own answer over TLS and is read for this alone: it authorizes nothing here. +pub(super) fn claim_names(id_token: &str) -> Vec { + let claims = id_token + .split('.') + .nth(1) + .filter(|_| id_token.len() <= 16 * 1024) + .and_then(|payload| URL_SAFE_NO_PAD.decode(payload.trim_end_matches('=')).ok()) + .and_then(|bytes| serde_json::from_slice::(&bytes).ok()) + .unwrap_or(Value::Null); + ["preferred_username", "email", "upn"] + .iter() + .filter_map(|name| claims[*name].as_str()) + .map(|text| { + text.chars() + .filter(|c| !c.is_control()) + .take(254) + .collect::() + }) + .map(|text| text.trim().to_lowercase()) + .filter(|text| !text.is_empty()) + .collect() +} + +/// A sign-in begun for this mailbox and application is not going to finish into the keyring item that +/// is being emptied. One of another application ID writes an item of its own and is left alone: the +/// window signs out a cancelled sign-in of the ID typed before while one with the ID typed now waits. +pub(super) fn end_flows(account: &str, client_id: &str) { + flows().retain(|_, entry| { + !matches!(entry, Entry::Pending(p) if p.account == account && p.client_id == client_id) + }); +} + +/// `m365.signOut {accountId, clientId}`: the keyring item goes, and with it everything in memory. +pub(super) async fn sign_out(request: &Value) -> Result { + let map = params::object(request, &["accountId", "clientId"])?; + let account = params::account(map)?; + let client_id = ids::client_id(params::text(map, "clientId")?)?; + end_flows(account, &client_id); + // Perhaps not signed out: the keyring item may still be there, and the window says so. + within_lane_wait(change(account, &client_id, None)).await?; + mailbox::forget(account); + read::forget(account, None); + Ok(json!({"signedOut": true})) +} diff --git a/src/providers/m365/tests.rs b/src/providers/m365/tests.rs new file mode 100644 index 0000000..207143b --- /dev/null +++ b/src/providers/m365/tests.rs @@ -0,0 +1,724 @@ +//! The provider against a scripted fake of Graph and the login host on one loopback listener, with +//! synthetic accounts, credentials and mail. The registry file, credential store and hosts are +//! process-wide, so a test that uses a `World` runs in an isolated child process +//! (`mail::tests::isolated`); pure functions are tested directly in `unit`. + +mod actions; +mod drafts; +mod mail; +mod signin; +mod unit; +mod wire; + +use std::collections::{HashMap, VecDeque}; +use std::sync::{Arc, Mutex}; + +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use serde_json::{Value, json}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +use super::model::Role; +use crate::credentials::tests::{StoreOverride, isolated_store}; +use crate::credentials::{CredentialKey, CredentialStore, Error, Secret}; +use crate::mail::tests::{AccountFixture, account_fixture}; + +pub(super) const ADDRESS: &str = "alice@example.org"; +pub(super) const ACCOUNT: &str = "m365:alice@example.org"; +pub(super) const CLIENT: &str = "00000000-1111-2222-3333-444444444444"; + +/// Runs the test in a child process of its own: the registry, keyring and hosts are process-wide. +macro_rules! isolated { + () => { + if crate::mail::tests::isolated() { + return; + } + }; +} +pub(super) use isolated; + +/// `world.call(method, json!(params))` unwrapped (`ok!`) or its error (`err!`); the braces of the +/// params keep rustfmt from wrapping a short call over five lines. +macro_rules! ok { + ($world:expr, $method:expr, $params:tt) => { + $world + .call($method, serde_json::json!($params)) + .await + .unwrap() + }; +} +macro_rules! err { + ($world:expr, $method:expr, $params:tt) => { + $world + .call($method, serde_json::json!($params)) + .await + .unwrap_err() + }; +} +pub(super) use {err, ok}; + +/// One request the fake received. +#[derive(Clone, Debug)] +pub(super) struct Req { + pub method: String, + pub path: String, + pub query: Vec<(String, String)>, + pub headers: Vec<(String, String)>, + pub body: Vec, +} + +impl Req { + pub(super) fn header(&self, name: &str) -> Option<&str> { + let found = self + .headers + .iter() + .find(|(k, _)| k.eq_ignore_ascii_case(name)); + found.map(|(_, v)| v.as_str()) + } + + pub(super) fn param(&self, name: &str) -> Option<&str> { + self.query + .iter() + .find(|(k, _)| k == name) + .map(|(_, v)| v.as_str()) + } + + pub(super) fn json(&self) -> Value { + serde_json::from_slice(&self.body).unwrap() + } + + /// A field of a urlencoded body. + pub(super) fn field(&self, name: &str) -> Option { + let url = reqwest::Url::parse(&format!( + "http://x/?{}", + String::from_utf8_lossy(&self.body) + )); + url.unwrap() + .query_pairs() + .find(|(k, _)| k == name) + .map(|(_, v)| v.into_owned()) + } +} + +/// What the fake does with a request. +pub(super) enum Resp { + Send(u16, Vec<(String, String)>, Vec), + /// Declares `Content-Length` of the second field and sends the body. + Lying(usize, Vec), + /// A body with no length, one chunk per item. + Chunked(Vec>), + /// Confirms success, then stalls or truncates a chunked body. + UnfinishedBody(u16, bool), + /// Reads the request and never answers. + Stall, + /// Waits, then answers. A test sits a body between the listing clock and the body clock. + Wait(std::time::Duration, Box), +} + +impl Resp { + pub(super) fn json(status: u16, value: Value) -> Resp { + Resp::Send(status, vec![], value.to_string().into_bytes()) + } + + pub(super) fn status(status: u16) -> Resp { + Resp::Send(status, vec![], vec![]) + } + + pub(super) fn with(self, name: &str, value: &str) -> Resp { + match self { + Resp::Send(status, mut headers, body) => { + headers.push((name.into(), value.into())); + Resp::Send(status, headers, body) + } + other => other, + } + } +} + +type Hook = Arc Option + Send + Sync>; + +pub(super) struct Msg { + pub id: String, + pub folder: Role, + pub subject: String, + pub read: bool, + pub flagged: bool, + pub mime: Vec, +} + +#[derive(Default)] +pub(super) struct State { + pub msgs: Vec, + /// The roles the mailbox has a folder for (an Archive exists only once somebody made one). + pub roles: Vec, + /// The refresh token the login host accepts, and how many it has issued. + pub refresh: String, + pub access: String, + pub issued: u32, + /// Answers of the token endpoint to a device-code grant, in order (default: pending). + pub device: VecDeque<(u16, Value)>, + pub sent: Vec>, + /// Consulted before anything else: a rule that replaces the answer to a chosen request. + pub hook: Option, + pub seen: Vec, +} + +pub(super) fn gid(n: u32) -> String { + format!("AAMkAGI2{n:04}AAA=") +} + +pub(super) fn oid(n: u32) -> String { + super::ids::encode(&gid(n)).unwrap() +} + +pub(super) fn plain_mime(subject: &str) -> Vec { + format!("From: Bob \r\nTo: alice@example.org\r\nSubject: {subject}\r\nDate: Fri, 9 Oct 2026 10:00:00 +0000\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nBody of {subject}\r\n").into_bytes() +} + +impl State { + pub(super) fn add(&mut self, n: u32, folder: Role, subject: &str) -> &mut Msg { + self.msgs.push(Msg { + id: gid(n), + folder, + subject: subject.into(), + read: false, + flagged: false, + mime: plain_mime(subject), + }); + self.msgs.last_mut().unwrap() + } + + pub(super) fn msg(&mut self, n: u32) -> &mut Msg { + let id = gid(n); + self.msgs.iter_mut().find(|m| m.id == id).unwrap() + } + + fn folder_id(role: Role) -> String { + format!("folder-{}", role.well_known()) + } + + fn message_json(&self, m: &Msg) -> Value { + let n: i64 = m.id[8..12].parse().unwrap(); + json!({ + "id": m.id, "parentFolderId": Self::folder_id(m.folder), + "internetMessageId": format!(""), "subject": m.subject, + "from": {"emailAddress": {"name": "Bob", "address": "bob@example.org"}}, + "toRecipients": [{"emailAddress": {"name": "Alice", "address": ADDRESS}}], + "ccRecipients": [], "isRead": m.read, "isDraft": m.folder == Role::Drafts, + "flag": {"flagStatus": if m.flagged {"flagged"} else {"notFlagged"}}, + "receivedDateTime": format!("2026-10-09T10:{:02}:00Z", n % 60), "sentDateTime": "2026-10-09T10:00:00Z", + "bodyPreview": format!("Body of {}", m.subject), + }) + } + + /// A listing page: newest first (the higher number), `$skiptoken` is an offset. + fn page(&self, req: &Req, base: &str, rows: Vec<&Msg>) -> Resp { + let mut rows = rows; + rows.sort_by_key(|m| std::cmp::Reverse(m.id.clone())); + let top: usize = req.param("$top").map_or(10, |t| t.parse().unwrap()); + let skip: usize = req.param("$skiptoken").map_or(0, |t| t.parse().unwrap()); + let mut body = json!({"value": rows.iter().skip(skip).take(top).map(|m| self.message_json(m)).collect::>()}); + if skip + top < rows.len() { + let mut query: Vec = req + .query + .iter() + .filter(|(k, _)| k != "$skiptoken") + .map(|(k, v)| format!("{k}={}", v.replace(' ', "%20"))) + .collect(); + query.push(format!("$skiptoken={}", skip + top)); + body["@odata.nextLink"] = json!(format!("{base}{}?{}", req.path, query.join("&"))); + } + Resp::json(200, body) + } + + fn graph(&mut self, req: &Req, base: &str) -> Resp { + if req.header("authorization") != Some(&format!("Bearer {}", self.access)) { + return Resp::json( + 401, + json!({"error": {"code": "InvalidAuthenticationToken"}}), + ); + } + let segments: Vec<&str> = req + .path + .trim_start_matches("/v1.0/me/") + .split('/') + .collect(); + let missing = || Resp::json(404, json!({"error": {"code": "ErrorItemNotFound"}})); + match (req.method.as_str(), segments.as_slice()) { + ("GET", ["mailFolders", wk]) => { + let Some(role) = Role::ALL + .into_iter() + .find(|r| r.well_known() == *wk) + .filter(|r| self.roles.contains(r)) + else { + return Resp::json(404, json!({"error": {"code": "ErrorFolderNotFound"}})); + }; + let here = self.msgs.iter().filter(|m| m.folder == role); + let unread = here.clone().filter(|m| !m.read).count(); + Resp::json( + 200, + json!({"id": Self::folder_id(role), "totalItemCount": here.count(), "unreadItemCount": unread}), + ) + } + ("GET", ["mailFolders", wk, "messages"]) => { + let filter = req.param("$filter").unwrap_or(""); + let rows = self.msgs.iter().filter(|m| m.folder.well_known() == *wk); + let rows = rows + .filter(|m| !filter.contains("isRead eq false") || !m.read) + .filter(|m| !filter.contains("flagged") || m.flagged) + .collect(); + self.page(req, base, rows) + } + ("GET", ["messages"]) => { + let phrase = req + .param("$search") + .unwrap_or("") + .trim_matches('"') + .to_lowercase(); + let rows = self + .msgs + .iter() + .filter(|m| m.subject.to_lowercase().contains(&phrase)) + .collect(); + self.page(req, base, rows) + } + ("GET", ["messages", id]) => match self.msgs.iter().find(|m| m.id == *id) { + Some(m) => Resp::json(200, self.message_json(m)), + None => missing(), + }, + ("GET", ["messages", id, "$value"]) => match self.msgs.iter().find(|m| m.id == *id) { + Some(m) => Resp::Send(200, vec![], m.mime.clone()), + None => missing(), + }, + ("PATCH", ["messages", id]) => { + let body = req.json(); + let Some(m) = self.msgs.iter_mut().find(|m| m.id == *id) else { + return missing(); + }; + if let Some(read) = body["isRead"].as_bool() { + m.read = read; + } + if let Some(status) = body["flag"]["flagStatus"].as_str() { + m.flagged = status == "flagged"; + } + Resp::json(200, json!({})) + } + ("POST", ["messages", id, "move"]) => { + let to = req.json()["destinationId"].as_str().unwrap().to_owned(); + let Some(role) = Role::ALL.into_iter().find(|r| r.well_known() == to) else { + return Resp::status(400); + }; + let Some(m) = self.msgs.iter_mut().find(|m| m.id == *id) else { + return missing(); + }; + m.folder = role; + Resp::json(201, json!({"id": m.id})) + } + ("POST", ["messages"]) => { + let n = 9000 + self.seen.len() as u32; + let mime = base64::engine::general_purpose::STANDARD + .decode(&req.body) + .unwrap(); + self.msgs.push(Msg { + id: gid(n), + folder: Role::Drafts, + subject: "Draft".into(), + read: true, + flagged: false, + mime, + }); + Resp::json(201, self.message_json(self.msgs.last().unwrap())) + } + ("DELETE", ["messages", id]) => match self.msgs.iter().position(|m| m.id == *id) { + Some(at) => { + self.msgs.remove(at); + Resp::status(204) + } + None => missing(), + }, + ("POST", ["sendMail"]) => { + self.sent.push(req.body.clone()); + Resp::status(202) + } + _ => Resp::status(404), + } + } + + /// A token response: a new access token and a new refresh token (the old one is no longer good), + /// with an id token whose `preferred_username` is `user` when there is one. + pub(super) fn issue(&mut self, user: Option<&str>) -> Value { + self.issue_claims(user.map(|user| json!({"preferred_username": user}))) + } + + /// The same with an id token of any claims. + pub(super) fn issue_claims(&mut self, claims: Option) -> Value { + self.issued += 1; + self.access = format!("access-{}", self.issued); + self.refresh = format!("refresh-{}", self.issued); + let mut body = json!({"access_token": self.access, "refresh_token": self.refresh, + "expires_in": 3600, "scope": "Mail.ReadWrite Mail.Send Calendars.ReadWrite openid profile"}); + if let Some(claims) = claims { + body["id_token"] = json!(format!( + "e30.{}.sig", + URL_SAFE_NO_PAD.encode(claims.to_string()) + )); + } + body + } + + fn login(&mut self, req: &Req) -> Resp { + match req.path.rsplit('/').next().unwrap() { + "devicecode" => Resp::json( + 200, + json!({ + "device_code": "synthetic-device-code", "user_code": "ABCD1234", "expires_in": 900, + "verification_uri": "https://microsoft.com/devicelogin", "interval": 5, + }), + ), + "token" if req.field("grant_type").as_deref() == Some("refresh_token") => { + if req.field("refresh_token").as_deref() != Some(&self.refresh) { + return Resp::json(400, json!({"error": "invalid_grant"})); + } + Resp::json(200, self.issue(None)) + } + "token" => { + let (status, body) = self + .device + .pop_front() + .unwrap_or((400, json!({"error": "authorization_pending"}))); + Resp::json(status, body) + } + _ => Resp::status(404), + } + } +} + +pub(super) struct Fake { + pub base: String, + pub state: Arc>, + task: tokio::task::JoinHandle<()>, +} + +impl Drop for Fake { + fn drop(&mut self) { + self.task.abort(); + } +} + +impl Fake { + async fn start() -> Fake { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://127.0.0.1:{}", listener.local_addr().unwrap().port()); + let state = Arc::new(Mutex::new(State { + roles: Role::ALL.to_vec(), + refresh: "refresh-0".into(), + ..State::default() + })); + let (served, origin) = (Arc::clone(&state), base.clone()); + let task = tokio::spawn(async move { + loop { + let Ok((socket, _)) = listener.accept().await else { + return; + }; + tokio::spawn(serve(socket, Arc::clone(&served), origin.clone())); + } + }); + Fake { base, state, task } + } + + pub(super) fn state(&self) -> std::sync::MutexGuard<'_, State> { + self.state.lock().unwrap() + } + + pub(super) fn hook(&self, hook: impl Fn(&Req) -> Option + Send + Sync + 'static) { + self.state().hook = Some(Arc::new(hook)); + } + + /// The requests that went to Graph, and those that went to the login host. + pub(super) fn graph(&self) -> Vec { + self.state() + .seen + .iter() + .filter(|r| r.path.starts_with("/v1.0/")) + .cloned() + .collect() + } + + pub(super) fn login(&self) -> Vec { + self.state() + .seen + .iter() + .filter(|r| !r.path.starts_with("/v1.0/")) + .cloned() + .collect() + } +} + +async fn serve(mut socket: tokio::net::TcpStream, state: Arc>, base: String) { + let mut buffer = Vec::new(); + let end = loop { + let mut chunk = [0u8; 4096]; + let n = socket.read(&mut chunk).await.unwrap_or(0); + if n == 0 { + return; + } + buffer.extend_from_slice(&chunk[..n]); + if let Some(at) = buffer.windows(4).position(|w| w == b"\r\n\r\n") { + break at + 4; + } + }; + let head = String::from_utf8_lossy(&buffer[..end]).into_owned(); + let mut lines = head.lines(); + let mut start = lines.next().unwrap().split(' '); + let (method, target) = ( + start.next().unwrap().to_owned(), + start.next().unwrap().to_owned(), + ); + let headers: Vec<(String, String)> = lines + .filter_map(|l| l.split_once(':')) + .map(|(k, v)| (k.trim().to_owned(), v.trim().to_owned())) + .collect(); + let length: usize = headers + .iter() + .find(|(k, _)| k.eq_ignore_ascii_case("content-length")) + .map_or(0, |(_, v)| v.parse().unwrap()); + while buffer.len() < end + length { + let mut chunk = [0u8; 65536]; + let n = socket.read(&mut chunk).await.unwrap_or(0); + if n == 0 { + return; + } + buffer.extend_from_slice(&chunk[..n]); + } + let url = reqwest::Url::parse(&format!("http://x{target}")).unwrap(); + let req = Req { + method, + path: url.path().to_owned(), + query: url + .query_pairs() + .map(|(k, v)| (k.into_owned(), v.into_owned())) + .collect(), + headers, + body: buffer[end..end + length].to_vec(), + }; + let resp = { + let mut state = state.lock().unwrap(); + state.seen.push(req.clone()); + let hooked = state.hook.clone().and_then(|hook| hook(&req)); + hooked.unwrap_or_else(|| { + if req.path.starts_with("/v1.0/") { + state.graph(&req, &base) + } else { + state.login(&req) + } + }) + }; + let resp = { + let mut resp = resp; + while let Resp::Wait(wait, inner) = resp { + tokio::time::sleep(wait).await; + resp = *inner; + } + resp + }; + let write = |status: u16, headers: String, body: &[u8]| { + let mut out = + format!("HTTP/1.1 {status} X\r\n{headers}Connection: close\r\n\r\n").into_bytes(); + out.extend_from_slice(body); + out + }; + let bytes = match resp { + Resp::Send(status, headers, body) => { + let extra: String = headers + .iter() + .map(|(k, v)| format!("{k}: {v}\r\n")) + .collect(); + write( + status, + format!("Content-Length: {}\r\n{extra}", body.len()), + &body, + ) + } + Resp::Lying(declared, body) => write(200, format!("Content-Length: {declared}\r\n"), &body), + Resp::Chunked(chunks) => { + let mut body = Vec::new(); + for chunk in chunks { + body.extend_from_slice(format!("{:x}\r\n", chunk.len()).as_bytes()); + body.extend_from_slice(&chunk); + body.extend_from_slice(b"\r\n"); + } + body.extend_from_slice(b"0\r\n\r\n"); + write(200, "Transfer-Encoding: chunked\r\n".into(), &body) + } + Resp::UnfinishedBody(status, stall) => { + let head = write(status, "Transfer-Encoding: chunked\r\n".into(), &[]); + let _ = socket.write_all(&head).await; + if stall { + tokio::time::sleep(std::time::Duration::from_secs(3600)).await; + } + return; + } + Resp::Stall => { + tokio::time::sleep(std::time::Duration::from_secs(3600)).await; + return; + } + Resp::Wait(_, _) => return, + }; + let _ = socket.write_all(&bytes).await; + let _ = socket.shutdown().await; +} + +/// The keyring item of the test account: kind `m365-refresh-token`, bound to account and client id. +pub(super) fn key() -> CredentialKey { + CredentialKey { + provider: "m365".into(), + account_id: ACCOUNT.into(), + kind: crate::credentials::CredentialKind::M365RefreshToken { + client_id: CLIENT.into(), + }, + } +} + +/// A credential store in memory that remembers every write. +#[derive(Clone, Default)] +pub(super) struct MemoryStore { + secrets: Arc>>>, + pub gets: Arc, + pub puts: Arc>>, + /// Runs while a token is being written, before the write lands. + pub during_put: Arc>>>, +} + +impl MemoryStore { + fn name(key: &CredentialKey) -> String { + format!("{key:?}") + } + + pub(super) fn set(&self, token: &str) { + let key = key(); + self.secrets + .lock() + .unwrap() + .insert(Self::name(&key), token.as_bytes().to_vec()); + } + + pub(super) fn token(&self) -> Option { + let key = key(); + let found = self.secrets.lock().unwrap().get(&Self::name(&key)).cloned(); + found.map(|bytes| String::from_utf8(bytes).unwrap()) + } +} + +impl CredentialStore for MemoryStore { + fn get(&self, key: &CredentialKey) -> Result { + self.gets.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + match self.secrets.lock().unwrap().get(&Self::name(key)) { + Some(bytes) => Secret::new(bytes.clone()), + None => Err(Error::Missing), + } + } + + fn put(&self, key: &CredentialKey, secret: &[u8]) -> Result<(), Error> { + let during = self.during_put.lock().unwrap().clone(); + if let Some(during) = during { + during(); + } + let (name, text) = (Self::name(key), String::from_utf8(secret.to_vec()).unwrap()); + self.puts.lock().unwrap().push((name.clone(), text)); + self.secrets.lock().unwrap().insert(name, secret.to_vec()); + Ok(()) + } + + fn delete(&self, key: &CredentialKey) -> Result<(), Error> { + match self.secrets.lock().unwrap().remove(&Self::name(key)) { + Some(_) => Ok(()), + None => Err(Error::Missing), + } + } +} + +/// A fake, a registry with one Microsoft 365 account, a keyring and the two hosts pointed at the fake. +pub(super) struct World { + pub fake: Fake, + pub store: MemoryStore, + _fixture: AccountFixture, + _override: StoreOverride, +} + +impl Drop for World { + fn drop(&mut self) { + *super::graph::LOCAL.lock().unwrap() = None; + *crate::auth::m365::TEST_LOGIN.lock().unwrap() = None; + *super::graph::SHORT_OPERATION.lock().unwrap() = None; + *super::graph::SHORT_MIME.lock().unwrap() = None; + *super::cancel::SHORT_DEADLINE.lock().unwrap() = None; + *super::signin::INTERVAL.lock().unwrap() = None; + *super::signin::SHORT_POLL_WAIT.lock().unwrap() = None; + *super::signin::SHORT_LANE_WAIT.lock().unwrap() = None; + } +} + +impl World { + /// Signed in: the keyring holds the refresh token the login host accepts. + pub(super) async fn new() -> World { + let world = World::signed_out().await; + world.store.set("refresh-0"); + world + } + + pub(super) async fn signed_out() -> World { + let fake = Fake::start().await; + let client = super::graph::builder().build().unwrap(); + let root = reqwest::Url::parse(&format!("{}/v1.0", fake.base)).unwrap(); + *super::graph::LOCAL.lock().unwrap() = Some((client, root)); + *crate::auth::m365::TEST_LOGIN.lock().unwrap() = Some(fake.base.clone()); + *super::signin::INTERVAL.lock().unwrap() = Some(std::time::Duration::ZERO); + let registry = json!({"version": 1, "activeId": ACCOUNT, "accounts": [ + {"provider": "m365", "email": ADDRESS, "clientId": CLIENT}, + {"provider": "outlook", "email": "bob@example.org", "clientId": CLIENT}, + ]}); + let store = MemoryStore::default(); + World { + fake, + _fixture: account_fixture(registry), + _override: isolated_store(store.clone()), + store, + } + } + + /// The registry's Microsoft 365 entry with this `clientId`, as the window may have saved it. + pub(super) fn save_client(&self, client: &str) { + let registry = json!({"version": 1, "activeId": ACCOUNT, "accounts": [ + {"provider": "m365", "email": ADDRESS, "clientId": client}, + ]}); + let path = self + ._fixture + .config + .join(crate::platform::dirs::APP_DIRECTORY); + std::fs::write(path.join("accounts.json"), registry.to_string()).unwrap(); + } + + /// Deadlines for the calls that follow: an operation's and a call's, in milliseconds. + pub(super) fn deadlines(&self, operation: u64, call: u64) { + *super::graph::SHORT_OPERATION.lock().unwrap() = + Some(std::time::Duration::from_millis(operation)); + *super::cancel::SHORT_DEADLINE.lock().unwrap() = + Some(std::time::Duration::from_millis(call)); + } + + pub(super) async fn call( + &self, + method: &str, + mut params: Value, + ) -> Result { + params["accountId"] = json!(ACCOUNT); + super::call(method, ¶ms).await + } +} + +/// An inbox of `count` messages numbered 1..=count. +pub(super) fn inbox(world: &World, count: u32) { + let mut state = world.fake.state(); + for n in 1..=count { + state.add(n, Role::Inbox, &format!("Message {n}")); + } +} diff --git a/src/providers/m365/tests/actions.rs b/src/providers/m365/tests/actions.rs new file mode 100644 index 0000000..66c70d7 --- /dev/null +++ b/src/providers/m365/tests/actions.rs @@ -0,0 +1,780 @@ +//! Changing and sending mail, and the shared mail pipeline (`mail.list`, `mail.act`, `mail.send`) +//! running over this provider. + +use base64::{ + Engine, + engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD}, +}; +use std::time::Duration; + +use serde_json::{Value, json}; + +use super::{ACCOUNT, Resp, Role, World, inbox, isolated, oid}; + +fn writes(world: &World) -> Vec<(String, String, Value)> { + let sent = world.fake.graph(); + let writes = sent + .iter() + .filter(|r| matches!(r.method.as_str(), "PATCH" | "POST") && !r.path.ends_with("sendMail")); + writes + .map(|r| { + ( + r.method.clone(), + r.path + .rsplit('/') + .nth(usize::from(r.method == "POST")) + .unwrap() + .to_owned(), + r.json(), + ) + }) + .collect() +} + +fn answer(succeeded: &[String], failed: &[String], error: Option<&str>) -> Value { + json!({"succeededIds": succeeded, "failedIds": failed, "error": error}) +} + +#[tokio::test] +async fn labels_become_one_patch_or_one_move_per_message_and_every_id_is_answered_once_in_order() { + isolated!(); + let world = World::new().await; + inbox(&world, 4); + let ids = [oid(2), oid(1), oid(2)]; + let ok = answer(&[oid(2), oid(1)], &[], None); + // Read state and flag together are one PATCH each; the ids are named once, as first asked. + let both = json!({"ids": ids, "addLabelIds": ["STARRED"], "removeLabelIds": ["UNREAD"]}); + assert_eq!(world.call("m365.modify", both).await.unwrap(), ok); + let sent = writes(&world); + assert_eq!(sent.len(), 2); + assert!(sent.iter().all(|(method, _, body)| method == "PATCH" + && *body == json!({"isRead": true, "flag": {"flagStatus": "flagged"}}))); + assert!( + world + .fake + .state() + .msgs + .iter() + .filter(|m| m.read && m.flagged) + .count() + == 2 + ); + world + .call( + "m365.modify", + json!({"ids": [oid(1)], "addLabelIds": ["UNREAD"], "removeLabelIds": ["STARRED"]}), + ) + .await + .unwrap(); + assert!(!world.fake.state().msg(1).flagged && !world.fake.state().msg(1).read); + // Archive is a move to the archive folder and the id survives it; so is the way back. + let before = writes(&world).len(); + assert_eq!( + world + .call( + "m365.modify", + json!({"ids": [oid(3)], "removeLabelIds": ["INBOX"]}) + ) + .await + .unwrap(), + answer(&[oid(3)], &[], None) + ); + assert_eq!(world.fake.state().msg(3).folder, Role::Archive); + assert_eq!( + writes(&world)[before], + ( + "POST".into(), + super::gid(3), + json!({"destinationId": "archive"}) + ) + ); + world + .call( + "m365.modify", + json!({"ids": [oid(3)], "addLabelIds": ["INBOX"]}), + ) + .await + .unwrap(); + assert_eq!(world.fake.state().msg(3).folder, Role::Inbox); + world + .call("m365.trash", json!({"ids": [oid(4), oid(3)]})) + .await + .unwrap(); + assert!( + world + .fake + .state() + .msgs + .iter() + .filter(|m| m.folder == Role::Trash) + .count() + == 2 + ); + assert_eq!( + writes(&world).last().unwrap().2, + json!({"destinationId": "deleteditems"}) + ); + world + .call("m365.untrash", json!({"ids": [oid(4)]})) + .await + .unwrap(); + assert_eq!(world.fake.state().msg(4).folder, Role::Inbox); + assert_eq!( + writes(&world).last().unwrap().2, + json!({"destinationId": "inbox"}) + ); +} + +#[tokio::test] +async fn what_cannot_be_done_is_said_per_id_and_nothing_else_is_sent() { + isolated!(); + let world = World::new().await; + inbox(&world, 3); + // A label with no meaning here is never ignored: every id fails and no request is made. + let unsupported = world + .call( + "m365.modify", + json!({"ids": [oid(1), oid(2)], "addLabelIds": ["IMPORTANT"]}), + ) + .await + .unwrap(); + assert_eq!( + unsupported, + answer(&[], &[oid(1), oid(2)], Some("invalid_params")) + ); + assert!(world.fake.graph().is_empty()); + // One message the server does not have: the others are changed and the answer names it. + let partial = world + .call( + "m365.modify", + json!({"ids": [oid(1), oid(40), oid(2)], "removeLabelIds": ["UNREAD"]}), + ) + .await + .unwrap(); + assert_eq!( + partial, + answer( + &[oid(1), oid(2)], + &[oid(40)], + Some("m365_message_not_found") + ) + ); + // A failed update is not followed by its move, and a server error is not asked twice. + world + .fake + .hook(|req| (req.method == "PATCH").then(|| Resp::status(500))); + let before = writes(&world).len(); + let both = json!({"ids": [oid(3)], "addLabelIds": ["STARRED"], "removeLabelIds": ["INBOX"]}); + assert_eq!( + world.call("m365.modify", both).await.unwrap(), + answer(&[], &[oid(3)], Some("m365_server_error")) + ); + assert_eq!( + writes(&world).len(), + before + 1, + "the move followed a failed update, or the update was repeated" + ); + // A mailbox without an Archive has nowhere to put them: every id says so, nothing is sent. + world.fake.hook(|_| None); + world.fake.state().roles.retain(|r| *r != Role::Archive); + world.call("m365.invalidate", json!({})).await.unwrap(); + let before = world.fake.graph().len(); + let archived = world + .call( + "m365.modify", + json!({"ids": [oid(1), oid(2)], "removeLabelIds": ["INBOX"]}), + ) + .await + .unwrap(); + assert_eq!( + archived, + answer(&[], &[oid(1), oid(2)], Some("m365_folder_unavailable")) + ); + assert!( + world.fake.graph()[before..] + .iter() + .all(|r| r.method == "GET" && r.path.contains("mailFolders")) + ); + // Signed out: the call fails whole, with nothing half done. + world + .call("m365.signOut", json!({"clientId": super::CLIENT})) + .await + .unwrap(); + assert_eq!( + world.call("m365.trash", json!({"ids": [oid(1)]})).await, + Err("m365_signed_out") + ); +} + +#[tokio::test] +async fn the_shared_pipeline_lists_plans_and_acts_through_this_provider() { + isolated!(); + let world = World::new().await; + inbox(&world, 3); + world.fake.state().msg(2).flagged = true; + let session = crate::backend::Session::default(); + let listed = session + .dispatch("mail.list", &json!({"mailbox": "inbox", "limit": 2})) + .await + .unwrap(); + assert_eq!(listed["accountId"], ACCOUNT); + let first = &listed["messages"][0]; + assert_eq!( + ( + first["id"].clone(), + first["subject"].clone(), + first["unread"].clone() + ), + (json!(oid(3)), json!("Message 3"), json!(true)) + ); + assert_eq!( + (first["from"]["email"].clone(), listed["estimate"].clone()), + (json!("bob@example.org"), json!(3)) + ); + assert_ne!(listed["nextPageToken"], ""); + let starred = session + .dispatch("mail.list", &json!({"mailbox": "starred"})) + .await + .unwrap(); + assert_eq!(starred["messages"].as_array().unwrap().len(), 1); + assert_eq!(starred["messages"][0]["starred"], true); + let found = session + .dispatch("mail.list", &json!({"query": "Message 1"})) + .await + .unwrap(); + assert_eq!(found["messages"].as_array().unwrap().len(), 1); + // A plan reads nothing it has no need of and changes nothing; executing it moves the message. + for (operation, role) in [("archive", Role::Archive), ("trash", Role::Trash)] { + let params = json!({"operation": operation, "ids": [oid(1)]}); + let preview = session.dispatch("mail.act", ¶ms).await.unwrap(); + assert_eq!(preview["targetIds"], json!([oid(1)])); + assert_eq!( + world.fake.state().msg(1).folder, + Role::Inbox, + "a preview moved a message" + ); + let result = session + .dispatch( + "mail.act", + &json!({"operation": operation, "ids": [oid(1)], "execute": true}), + ) + .await + .unwrap(); + assert_eq!( + (result["succeededIds"].clone(), result["failedIds"].clone()), + (json!([oid(1)]), json!([])) + ); + assert_eq!(world.fake.state().msg(1).folder, role); + world.fake.state().msg(1).folder = Role::Inbox; + } + let read = session + .dispatch( + "mail.act", + &json!({"operation": "read", "ids": [oid(2)], "execute": true}), + ) + .await + .unwrap(); + assert_eq!(read["succeededIds"], json!([oid(2)])); + assert!(world.fake.state().msg(2).read); + // A mailbox with no Archive refuses the plan before anything is sent. + world.fake.state().roles.retain(|r| *r != Role::Archive); + world.call("m365.invalidate", json!({})).await.unwrap(); + let refused = session + .dispatch( + "mail.act", + &json!({"operation": "archive", "ids": [oid(1)], "execute": true}), + ) + .await; + assert!(refused.is_err(), "{refused:?}"); + assert_eq!(world.fake.state().msg(1).folder, Role::Inbox); + // Spam is not an action this provider declares. + assert!( + session + .dispatch( + "mail.act", + &json!({"operation": "spam", "ids": [oid(1)], "execute": true}) + ) + .await + .is_err() + ); +} + +#[tokio::test] +async fn a_plan_asks_only_about_where_mail_goes_and_saves_no_rotated_token() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + let session = crate::backend::Session::default(); + // Marking read moves nothing: its plan asks nobody anything. + let params = json!({"operation": "read", "ids": [oid(1)]}); + let preview = session.dispatch("mail.act", ¶ms).await.unwrap(); + assert_eq!(preview["targetIds"], json!([oid(1)])); + assert!(world.fake.graph().is_empty() && world.fake.login().is_empty()); + // Archiving reads the folders, with a token whose rotated refresh token is not saved. + let params = json!({"operation": "archive", "ids": [oid(1)]}); + session.dispatch("mail.act", ¶ms).await.unwrap(); + assert!(!world.fake.graph().is_empty()); + assert_eq!(world.fake.state().issued, 1, "the refresh token rotated"); + assert!( + world.store.puts.lock().unwrap().is_empty(), + "a plan wrote the keyring" + ); + assert_eq!(world.store.token().as_deref(), Some("refresh-0")); +} + +#[tokio::test] +async fn a_send_given_up_on_before_it_was_written_was_not_sent() { + isolated!(); + let world = World::new().await; + world.deadlines(300, 5000); + // The token never comes, so the request's clock runs out before the message went anywhere. + world + .fake + .hook(|req| (!req.path.starts_with("/v1.0/")).then_some(Resp::Stall)); + let message = raw("From: alice@example.org\r\nTo: bob@example.org\r\n"); + assert_eq!( + world.call("m365.send", json!({"raw": message})).await, + Err("m365_send_not_attempted") + ); + assert!(world.fake.graph().is_empty() && world.fake.state().sent.is_empty()); +} + +#[tokio::test] +async fn a_send_refused_its_token_and_given_up_on_while_a_fresh_one_is_fetched_was_not_sent() { + isolated!(); + let world = World::new().await; + world.deadlines(300, 5000); + // Graph refuses the token, which applied nothing; the fresh token never comes, so the clock runs + // out before the second attempt was written. The first attempt's write is not this one's. + let refreshes = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let counted = std::sync::Arc::clone(&refreshes); + world.fake.hook(move |req| { + if req.path.ends_with("sendMail") { + let refused = json!({"error": {"code": "InvalidAuthenticationToken"}}); + return Some(Resp::json(401, refused)); + } + let refresh = req.field("grant_type").as_deref() == Some("refresh_token"); + let later = refresh && counted.fetch_add(1, std::sync::atomic::Ordering::SeqCst) > 0; + later.then_some(Resp::Stall) + }); + // Reach the retried refresh before spending the request's clock. Polling while yielding + // keeps paused time from auto-advancing while the loopback socket and keyring do real work. + tokio::time::pause(); + let message = raw("From: alice@example.org\r\nTo: bob@example.org\r\n"); + let send = world.call("m365.send", json!({"raw": message})); + tokio::pin!(send); + let started = std::time::Instant::now(); + while refreshes.load(std::sync::atomic::Ordering::SeqCst) < 2 { + assert!( + futures_util::poll!(send.as_mut()).is_pending(), + "send ended before the retried refresh reached the login host" + ); + assert!( + started.elapsed() < Duration::from_secs(10), + "retried refresh never arrived" + ); + tokio::task::yield_now().await; + } + tokio::time::advance(Duration::from_millis(300)).await; + assert_eq!(send.await, Err("m365_send_not_attempted")); + let sends = world.fake.graph(); + assert_eq!( + sends + .iter() + .filter(|r| r.path.ends_with("sendMail")) + .count(), + 1 + ); + assert_eq!(refreshes.load(std::sync::atomic::Ordering::SeqCst), 2); +} + +fn raw(headers: &str) -> String { + URL_SAFE_NO_PAD.encode(format!( + "{headers}Subject: Hi\r\nMIME-Version: 1.0\r\nContent-Type: text/plain\r\n\r\nHello\r\n" + )) +} + +#[tokio::test] +async fn a_message_goes_to_graph_once_as_composed_blind_copy_and_all() { + isolated!(); + let world = World::new().await; + let message = "From: alice@example.org\r\nTo: bob@example.org\r\nBcc: blind@example.org\r\n"; + let sent = world + .call("m365.send", json!({"raw": raw(message)})) + .await + .unwrap(); + assert_eq!(sent, json!({"sent": true, "warning": ""})); + let requests: Vec<_> = world + .fake + .graph() + .into_iter() + .filter(|r| r.path == "/v1.0/me/sendMail") + .collect(); + assert_eq!(requests.len(), 1); + let request = &requests[0]; + assert_eq!( + (request.method.as_str(), request.header("content-type")), + ("POST", Some("text/plain")) + ); + assert_eq!(request.header("authorization"), Some("Bearer access-1")); + // Graph is given the MIME exactly as composed, Bcc header included (what Graph then does with it + // was only observed on one work tenant, not documented). It is in no other request. + let mime = String::from_utf8(STANDARD.decode(&request.body).unwrap()).unwrap(); + assert_eq!( + mime, + String::from_utf8(URL_SAFE_NO_PAD.decode(raw(message)).unwrap()).unwrap() + ); + assert!(mime.contains("Bcc: blind@example.org")); + assert_eq!( + world + .fake + .graph() + .iter() + .filter(|r| String::from_utf8_lossy(&r.body).contains("blind")) + .count(), + 0 + ); + assert!(!sent.to_string().contains("blind")); + assert_eq!(world.fake.state().sent.len(), 1); + // The only identity there is is the mailbox's own, and asking costs no request. + let before = world.fake.graph().len(); + assert_eq!( + world.call("m365.sendAs", json!({})).await.unwrap(), + json!([{"email": "alice@example.org", "displayName": "", "isPrimary": true, "isDefault": true}]) + ); + assert_eq!(world.fake.graph().len(), before); +} + +#[tokio::test] +async fn a_failed_send_is_never_repeated_and_says_whether_it_can_have_gone_out() { + isolated!(); + let world = World::new().await; + let message = raw("From: alice@example.org\r\nTo: bob@example.org\r\n"); + let attempts = |world: &World| { + world + .fake + .graph() + .iter() + .filter(|r| r.path.ends_with("sendMail")) + .count() + }; + let mut total = 0; + for (status, retry, code) in [ + (400, None, "m365_send_rejected"), + (403, None, "m365_send_rejected"), + (413, None, "m365_message_too_large"), + (429, Some("1"), "m365_send_not_attempted"), + (500, None, "m365_send_failed"), + (503, Some("1"), "m365_send_failed"), + (504, None, "m365_send_failed"), + (302, None, "m365_send_failed"), + ] { + world.fake.hook(move |req| { + req.path.ends_with("sendMail").then(|| { + let resp = Resp::json( + status, + json!({"error": {"code": "SomeCode", "message": "secret words"}}), + ); + retry.map_or(resp, |after| { + Resp::with(Resp::json(status, json!({})), "Retry-After", after) + }) + }) + }); + assert_eq!( + world.call("m365.send", json!({"raw": message})).await, + Err(code), + "{status}" + ); + total += 1; + assert_eq!(attempts(&world), total, "{status} was repeated"); + } + // Exchange's own code for a message too large is a refusal for the size, whatever the status. + world.fake.hook(|req| { + req.path + .ends_with("sendMail") + .then(|| Resp::json(400, json!({"error": {"code": "ErrorMessageSizeExceeded"}}))) + }); + assert_eq!( + world.call("m365.send", json!({"raw": message})).await, + Err("m365_message_too_large") + ); + total += 1; + assert_eq!(attempts(&world), total); + // A reply that never comes may have been sent: unknown, and not repeated. + world.deadlines(300, 2000); + world + .fake + .hook(|req| req.path.ends_with("sendMail").then_some(Resp::Stall)); + assert_eq!( + world.call("m365.send", json!({"raw": message})).await, + Err("m365_send_failed") + ); + assert_eq!(attempts(&world), total + 1); + // Nothing was written when there is no token: that is a definite refusal. + world.fake.hook(|_| None); + world + .call("m365.signOut", json!({"clientId": super::CLIENT})) + .await + .unwrap(); + let before = attempts(&world); + assert_eq!( + world.call("m365.send", json!({"raw": message})).await, + Err("m365_send_not_attempted") + ); + assert_eq!(attempts(&world), before); +} + +#[tokio::test] +async fn the_outbox_hands_graph_the_queued_message_whole_and_keeps_the_draft_when_graph_refuses() { + isolated!(); + let world = World::new().await; + // The draft each message was written from: one per send. + for n in [21, 22] { + world.fake.state().add(n, Role::Drafts, "Draft"); + } + let session = crate::backend::Session::default(); + let message = + raw("From: alice@example.org\r\nTo: bob@example.org\r\nBcc: blind@example.org\r\n"); + let queue = |id: &'static str| { + let draft = match id { + "one" => oid(21), + "two" => oid(22), + _ => String::new(), + }; + let job = json!({"accountId": ACCOUNT, "provider": "m365", "sendId": id, "delaySeconds": 0, + "payload": {"raw": message, "threadId": "t1", "sendId": id, "draftId": draft}}); + let session = &session; + async move { + session.dispatch("outbox.enqueue", &job).await.unwrap(); + let mut done = json!({"accountId": ACCOUNT, "sendId": id}); + session.finish_cli_send(&mut done).await.unwrap(); + done["outbox"]["entries"][0]["clone"].clone() + } + }; + let _ = queue("one").await; + let entry = session + .dispatch( + "outbox.snapshot", + &json!({"accountId": ACCOUNT, "sendId": "one"}), + ) + .await + .unwrap(); + assert_eq!(entry["entries"][0]["state"], "sent"); + // One request, the MIME as queued (blind copy included, for Graph to deliver and to hide), and no + // field the provider does not take: it refuses unknown ones, so `threadId` never reached it. + let sent = world.fake.state().sent.clone(); + assert_eq!(sent.len(), 1); + assert_eq!( + STANDARD.decode(&sent[0]).unwrap(), + URL_SAFE_NO_PAD.decode(&message).unwrap() + ); + // Once it is sent, the draft it was written from is deleted. + let drafts = |world: &World| { + world + .fake + .state() + .msgs + .iter() + .map(|m| m.id.clone()) + .collect::>() + }; + assert_eq!(drafts(&world), [super::gid(22)]); + // A refusal by Graph is definitely not a send: the entry fails and nothing is sent twice. + world + .fake + .hook(|req| req.path.ends_with("sendMail").then(|| Resp::status(400))); + let _ = queue("two").await; + let entry = session + .dispatch( + "outbox.snapshot", + &json!({"accountId": ACCOUNT, "sendId": "two"}), + ) + .await + .unwrap(); + assert_eq!( + ( + entry["entries"][0]["state"].clone(), + entry["entries"][0]["error"].clone() + ), + (json!("failed"), json!("outbox_send_refused")) + ); + assert_eq!(world.fake.state().sent.len(), 1); + assert_eq!( + drafts(&world), + [super::gid(22)], + "an unsent message keeps its draft" + ); + // Microsoft refusing the message for its size is as definite. + world + .fake + .hook(|req| req.path.ends_with("sendMail").then(|| Resp::status(413))); + let _ = queue("large").await; + let entry = session + .dispatch( + "outbox.snapshot", + &json!({"accountId": ACCOUNT, "sendId": "large"}), + ) + .await + .unwrap(); + assert_eq!( + ( + entry["entries"][0]["state"].clone(), + entry["entries"][0]["error"].clone() + ), + (json!("failed"), json!("outbox_send_refused")) + ); + assert_eq!(world.fake.state().sent.len(), 1); + // A reply that never came may have been sent: it is shown as unknown, not as refused. + world.deadlines(300, 2000); + world + .fake + .hook(|req| req.path.ends_with("sendMail").then_some(Resp::Stall)); + let _ = queue("three").await; + let entry = session + .dispatch( + "outbox.snapshot", + &json!({"accountId": ACCOUNT, "sendId": "three"}), + ) + .await + .unwrap(); + assert_eq!(entry["entries"][0]["state"], "unknown"); +} + +/// A message of `size` bytes of body, as `m365.send` takes one. +fn sized(size: usize) -> String { + URL_SAFE_NO_PAD.encode(format!( + "From: alice@example.org\r\nTo: bob@example.org\r\nSubject: Big\r\nMIME-Version: 1.0\r\nContent-Type: text/plain\r\n\r\n{}\r\n", + "x".repeat(size) + )) +} + +#[tokio::test] +async fn a_large_message_is_given_longer_to_upload_and_a_small_one_is_not() { + isolated!(); + let world = World::new().await; + world.deadlines(300, 5000); + // The token is fetched first, so what follows times only the upload. + assert!( + world + .call("m365.send", json!({"raw": sized(10)})) + .await + .is_ok() + ); + world.fake.hook(|req| { + req.path + .ends_with("sendMail") + .then(|| Resp::Wait(Duration::from_millis(900), Box::new(Resp::status(202)))) + }); + // A short message has the request's own clock, which an answer this slow misses. + assert_eq!( + world.call("m365.send", json!({"raw": sized(10)})).await, + Err("m365_send_failed") + ); + // A megabyte has seconds more, still inside the call's deadline, and the same answer is in time. + assert_eq!( + world + .call("m365.send", json!({"raw": sized(1024 * 1024)})) + .await, + Ok(json!({"sent": true, "warning": ""})) + ); + assert_eq!(world.fake.state().sent.len(), 1); +} + +#[tokio::test] +async fn a_batch_that_runs_out_of_time_answers_for_what_was_done_and_begins_nothing_more() { + isolated!(); + let world = World::new().await; + inbox(&world, 48); + // Each change takes a fifth of a second and four go at a time: the call's two seconds end + // part-way through the batch. + world.deadlines(5000, 2000); + world.fake.hook(|req| { + (req.method == "PATCH").then(|| { + Resp::Wait( + Duration::from_millis(200), + Box::new(Resp::json(200, json!({}))), + ) + }) + }); + let ids: Vec = (1..=48).map(oid).collect(); + let started = std::time::Instant::now(); + let answer = world + .call( + "m365.modify", + json!({"ids": ids, "removeLabelIds": ["UNREAD"]}), + ) + .await + .expect("a batch answers for its ids, not with one error for all of them"); + assert!(started.elapsed() < Duration::from_millis(2500)); + let list = |key: &str| -> Vec { serde_json::from_value(answer[key].clone()).unwrap() }; + let (succeeded, failed) = (list("succeededIds"), list("failedIds")); + assert!(!succeeded.is_empty() && !failed.is_empty(), "{answer}"); + assert_eq!(answer["error"], "m365_timeout"); + // Every id once, in order: what was done first, what was never begun after it. + assert_eq!([succeeded.clone(), failed.clone()].concat(), ids); + // Graph was asked to change exactly what is reported done: a request already written was waited + // for, and nothing reported failed was sent. + let mut patched: Vec = world + .fake + .graph() + .iter() + .filter(|r| r.method == "PATCH") + .map(|r| super::super::ids::encode(r.path.rsplit('/').next().unwrap()).unwrap()) + .collect(); + patched.sort(); + let mut done = succeeded.clone(); + done.sort(); + assert_eq!(patched, done); +} + +#[tokio::test] +async fn a_refused_token_waits_for_a_slow_refresh_no_longer_than_the_batch() { + isolated!(); + let world = World::new().await; + inbox(&world, 2); + world.deadlines(5000, 1000); + // Graph refuses both changes' token, the second a moment after the first. The refresh that + // follows the first refusal takes three seconds, and holds the mailbox's refresh lane all that + // time: the second refusal waits for that lane to forget its token. + let refreshes = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let counted = std::sync::Arc::clone(&refreshes); + world.fake.hook(move |req| { + if req.method == "PATCH" { + let refused = Resp::json( + 401, + json!({"error": {"code": "InvalidAuthenticationToken"}}), + ); + return Some(if req.path.ends_with(&super::gid(2)) { + Resp::Wait(Duration::from_millis(200), Box::new(refused)) + } else { + refused + }); + } + let refresh = req.field("grant_type").as_deref() == Some("refresh_token"); + let later = refresh && counted.fetch_add(1, std::sync::atomic::Ordering::SeqCst) > 0; + later.then(|| { + Resp::Wait( + Duration::from_secs(3), + Box::new(Resp::json(400, json!({"error": "temporarily_unavailable"}))), + ) + }) + }); + let started = std::time::Instant::now(); + let answered = world + .call( + "m365.modify", + json!({"ids": [oid(1), oid(2)], "removeLabelIds": ["UNREAD"]}), + ) + .await + .unwrap(); + assert!( + started.elapsed() < Duration::from_millis(1500), + "the batch answered after {:?}, past its deadline", + started.elapsed() + ); + assert_eq!( + answered, + answer(&[], &[oid(1), oid(2)], Some("m365_timeout")) + ); + assert_eq!(refreshes.load(std::sync::atomic::Ordering::SeqCst), 2); +} diff --git a/src/providers/m365/tests/drafts.rs b/src/providers/m365/tests/drafts.rs new file mode 100644 index 0000000..f33cbef --- /dev/null +++ b/src/providers/m365/tests/drafts.rs @@ -0,0 +1,268 @@ +//! Drafts on the server: `m365.saveDraft` creates one from MIME and then removes the copy it replaces, +//! `m365.deleteDraft` removes only what is a draft. + +use base64::{ + Engine, + engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD}, +}; +use std::time::Duration; + +use serde_json::json; + +use super::{Resp, Role, World, err, inbox, isolated, oid, ok}; + +fn mime() -> Vec { + b"From: alice@example.org\r\nTo: bob@example.org\r\nBcc: blind@example.org\r\nSubject: Plan\r\nMIME-Version: 1.0\r\nContent-Type: text/plain\r\n\r\nNot yet\r\n".to_vec() +} + +/// The Graph requests that were not listings or folder lookups, as `METHOD path`. +fn writes(world: &World) -> Vec { + world + .fake + .graph() + .iter() + .filter(|r| !r.path.contains("/mailFolders/")) + .map(|r| format!("{} {}", r.method, r.path)) + .collect() +} + +#[tokio::test] +async fn a_draft_is_saved_as_mime_and_the_copy_it_replaces_goes_only_once_it_exists() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + world.fake.state().add(5, Role::Drafts, "Old draft"); + let raw = URL_SAFE_NO_PAD.encode(mime()); + let saved = ok!(world, "m365.saveDraft", {"raw": raw, "draftId": oid(5)}); + let new = world + .fake + .state() + .msgs + .iter() + .find(|m| m.subject == "Draft") + .map(|m| (m.id.clone(), m.folder, m.mime.clone())) + .unwrap(); + assert_eq!( + saved, + json!({"saved": true, "warning": "", "id": super::super::ids::encode(&new.0).unwrap()}) + ); + // Graph is given the MIME as composed, as base64 text, and the new draft is in Drafts. + let create = world + .fake + .graph() + .into_iter() + .find(|r| r.method == "POST" && r.path == "/v1.0/me/messages") + .unwrap(); + assert_eq!(create.header("content-type"), Some("text/plain")); + assert_eq!(create.header("prefer"), Some("IdType=\"ImmutableId\"")); + assert_eq!(STANDARD.decode(&create.body).unwrap(), mime()); + assert_eq!((new.1, new.2), (Role::Drafts, mime())); + // The old copy is asked whether it is a draft, then deleted, after the new one exists. + let old = format!("/v1.0/me/messages/{}", super::gid(5)); + assert_eq!( + writes(&world), + [ + "POST /v1.0/me/messages".to_owned(), + format!("GET {old}"), + format!("DELETE {old}") + ] + ); + let check = world + .fake + .graph() + .into_iter() + .find(|r| r.path == old) + .unwrap(); + assert_eq!(check.param("$select"), Some("isDraft,parentFolderId")); + assert!( + !world + .fake + .state() + .msgs + .iter() + .any(|m| m.subject == "Old draft") + ); + // With nothing to replace, nothing else is asked. + let before = writes(&world).len(); + let saved = ok!(world, "m365.saveDraft", {"raw": raw}); + assert_eq!(saved["warning"], ""); + assert_eq!( + writes(&world)[before..], + ["POST /v1.0/me/messages".to_owned()] + ); +} + +#[tokio::test] +async fn a_saved_draft_whose_old_copy_stays_is_a_warning_and_a_failed_save_removes_nothing() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + world.fake.state().add(5, Role::Drafts, "Old draft"); + let raw = URL_SAFE_NO_PAD.encode(mime()); + // The id names a received message: it is not a draft, so it stays, and the save is still a save. + let saved = ok!(world, "m365.saveDraft", {"raw": raw, "draftId": oid(1)}); + assert_eq!(saved["saved"], true); + assert_eq!( + saved["warning"], + "The updated draft was saved, but the old copy could not be removed" + ); + assert!(!writes(&world).iter().any(|w| w.starts_with("DELETE"))); + assert_eq!(world.fake.state().msg(1).folder, Role::Inbox); + // Graph refuses to delete the old copy: a warning again. + world + .fake + .hook(|req| (req.method == "DELETE").then(|| Resp::status(403))); + let saved = ok!(world, "m365.saveDraft", {"raw": raw, "draftId": oid(5)}); + assert_eq!( + (saved["saved"].clone(), saved["warning"] != ""), + (json!(true), true) + ); + // A save Graph refuses is a failure, and the draft it would have replaced is left alone. + world.fake.hook(|req| { + (req.method == "POST" && req.path == "/v1.0/me/messages").then(|| Resp::status(400)) + }); + let before = writes(&world).len(); + assert_eq!( + err!(world, "m365.saveDraft", {"raw": raw, "draftId": oid(5)}), + "m365_invalid_request" + ); + assert_eq!( + writes(&world)[before..], + ["POST /v1.0/me/messages".to_owned()] + ); + assert!( + world + .fake + .state() + .msgs + .iter() + .any(|m| m.subject == "Old draft") + ); +} + +#[tokio::test] +async fn only_a_draft_is_deleted_and_bad_requests_reach_nobody() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + world.fake.state().add(5, Role::Drafts, "Old draft"); + assert_eq!( + err!(world, "m365.deleteDraft", {"id": oid(1)}), + "m365_not_a_draft" + ); + assert_eq!(world.fake.state().msg(1).folder, Role::Inbox); + assert!(!writes(&world).iter().any(|w| w.starts_with("DELETE"))); + assert_eq!(ok!(world, "m365.deleteDraft", {"id": oid(5)}), json!({})); + assert_eq!( + writes(&world)[1..], + [ + format!("GET /v1.0/me/messages/{}", super::gid(5)), + format!("DELETE /v1.0/me/messages/{}", super::gid(5)) + ] + ); + assert!( + !world + .fake + .state() + .msgs + .iter() + .any(|m| m.subject == "Old draft") + ); + // What cannot be a draft id or a message is refused before any request. + let before = world.fake.graph().len(); + let raw = URL_SAFE_NO_PAD.encode(mime()); + for (method, params) in [ + ("m365.deleteDraft", json!({"id": "5:Drafts"})), + ("m365.deleteDraft", json!({})), + ("m365.deleteDraft", json!({"id": oid(5), "extra": 1})), + ("m365.saveDraft", json!({"raw": raw, "draftId": "5:Drafts"})), + ("m365.saveDraft", json!({"raw": raw, "draftId": 5})), + ("m365.saveDraft", json!({"raw": "not base64!"})), + ("m365.saveDraft", json!({"raw": raw, "threadId": "t"})), + ] { + assert_eq!( + world.call(method, params.clone()).await, + Err("invalid_params"), + "{method} {params}" + ); + } + assert_eq!(world.fake.graph().len(), before); +} + +#[tokio::test] +async fn a_save_with_no_answer_may_have_made_a_draft_and_is_never_repeated() { + isolated!(); + let world = World::new().await; + world.fake.state().add(5, Role::Drafts, "Old draft"); + world.deadlines(300, 2000); + let raw = URL_SAFE_NO_PAD.encode(mime()); + let posts = |world: &World| { + world + .fake + .graph() + .iter() + .filter(|r| r.method == "POST" && r.path == "/v1.0/me/messages") + .count() + }; + // A 5xx, a lost answer: Graph may have filed the draft. Said so, sent once, the old copy kept. + for resp in [|| Resp::status(500), || Resp::status(503), || Resp::Stall] { + world + .fake + .hook(move |req| (req.method == "POST" && req.path == "/v1.0/me/messages").then(resp)); + let before = posts(&world); + assert_eq!( + err!(world, "m365.saveDraft", {"raw": raw, "draftId": oid(5)}), + "m365_draft_unconfirmed" + ); + assert_eq!(posts(&world), before + 1, "a save is never repeated"); + assert!(!writes(&world).iter().any(|w| w.starts_with("DELETE"))); + } + // A refusal for its size is a refusal, not a doubt, whatever status carries it, as on a send. + world.fake.hook(|req| { + (req.method == "POST" && req.path == "/v1.0/me/messages").then(|| Resp::status(413)) + }); + assert_eq!( + err!(world, "m365.saveDraft", {"raw": raw}), + "m365_message_too_large" + ); + world.fake.hook(|req| { + (req.method == "POST" && req.path == "/v1.0/me/messages") + .then(|| Resp::json(500, json!({"error": {"code": "ErrorMessageSizeExceeded"}}))) + }); + assert_eq!( + err!(world, "m365.saveDraft", {"raw": raw}), + "m365_message_too_large" + ); +} + +#[tokio::test] +async fn a_large_draft_is_given_longer_to_upload_and_a_small_one_is_not() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + world.deadlines(300, 5000); + let sized = |size: usize| { + let mut message = mime(); + message.extend(std::iter::repeat_n(b'x', size)); + URL_SAFE_NO_PAD.encode(message) + }; + // The token is fetched first, so what follows times only the upload. + assert_eq!( + ok!(world, "m365.saveDraft", {"raw": sized(10)})["saved"], + true + ); + world.fake.hook(|req| { + (req.method == "POST" && req.path == "/v1.0/me/messages").then(|| { + Resp::Wait( + Duration::from_millis(900), + Box::new(Resp::json(201, json!({"id": super::gid(77)}))), + ) + }) + }); + assert_eq!( + err!(world, "m365.saveDraft", {"raw": sized(10)}), + "m365_draft_unconfirmed" + ); + let saved = ok!(world, "m365.saveDraft", {"raw": sized(1024 * 1024)}); + assert_eq!(saved["id"], oid(77)); +} diff --git a/src/providers/m365/tests/mail.rs b/src/providers/m365/tests/mail.rs new file mode 100644 index 0000000..cdf8691 --- /dev/null +++ b/src/providers/m365/tests/mail.rs @@ -0,0 +1,452 @@ +//! Reading: listings and their paging, header rows, whole messages, attachments, the folder check +//! and the background check. + +use serde_json::{Value, json}; + +use super::super::check; +use super::{ACCOUNT, ADDRESS, Resp, Role, World, err, gid, inbox, isolated, oid, ok, plain_mime}; + +fn ids(page: &Value) -> Vec { + let ids = page["page"]["ids"].as_array().unwrap(); + ids.iter() + .map(|id| id.as_str().unwrap().to_owned()) + .collect() +} + +async fn list(world: &World, query: &str, limit: u32, token: &str) -> Result { + world + .call( + "m365.list", + json!({"query": query, "limit": limit, "pageToken": token}), + ) + .await +} + +/// The newest request that carried `$filter` (or `$search`). +fn last_query(world: &World, param: &str) -> super::Req { + world + .fake + .graph() + .into_iter() + .rev() + .find(|r| r.param(param).is_some()) + .unwrap() +} + +#[tokio::test] +async fn a_listing_pages_a_folder_newest_first_with_the_counts_the_screen_shows() { + isolated!(); + let world = World::new().await; + inbox(&world, 7); + { + let mut state = world.fake.state(); + state.msg(3).flagged = true; + state.msg(2).read = true; + state.add(8, Role::Sent, "Sent one").read = true; + } + let first = list(&world, "role:inbox", 3, "").await.unwrap(); + assert_eq!( + (ids(&first), first["page"]["estimate"].clone()), + (vec![oid(7), oid(6), oid(5)], json!(7)) + ); + assert_eq!(first["rows"][0]["labelIds"], json!(["UNREAD", "INBOX"])); + assert_eq!(first["rows"][1]["id"], oid(6)); + // The request asks for newest first, ids that survive a move, and no body. + let sent = world.fake.graph(); + let listing = sent + .iter() + .find(|r| r.path.ends_with("/inbox/messages")) + .unwrap(); + assert_eq!( + (listing.param("$orderby"), listing.param("$top")), + (Some("receivedDateTime desc"), Some("3")) + ); + let select = listing.param("$select").unwrap(); + assert!(select.contains("bodyPreview") && !select.contains("body,")); + assert_eq!(listing.header("prefer"), Some("IdType=\"ImmutableId\"")); + assert_eq!(listing.header("authorization"), Some("Bearer access-1")); + let token = first["page"]["nextPageToken"].as_str().unwrap(); + // A later page is the first one's size, whatever limit continues it (Graph's link says `$top`). + let resized = list(&world, "role:inbox", 1, token).await.unwrap(); + assert_eq!(ids(&resized), [oid(4), oid(3), oid(2)]); + let second = list(&world, "role:inbox", 3, token).await.unwrap(); + assert_eq!(ids(&second), [oid(4), oid(3), oid(2)]); + assert_eq!( + second["rows"][1]["labelIds"], + json!(["UNREAD", "STARRED", "INBOX"]) + ); + // The estimate is the first page's, whatever has changed since; Graph's own paging shifts with the mail. + world.fake.state().add(9, Role::Inbox, "Late"); + let third = list( + &world, + "role:inbox", + 3, + second["page"]["nextPageToken"].as_str().unwrap(), + ) + .await + .unwrap(); + assert_eq!( + ( + ids(&third).len(), + third["page"]["estimate"].clone(), + third["page"]["nextPageToken"].clone() + ), + (2, json!(7), json!("")) + ); + // A token continues its own query and no other. + assert_eq!( + list(&world, "role:sent", 3, token).await, + Err("invalid_params") + ); + // An unread view counts unread mail; folder facts come from the folder table (at most a minute + // old: the late message is not in it), not from the page. + let unread = list(&world, "role:inbox unseen", 2, "").await.unwrap(); + assert_eq!( + (ids(&unread).len(), unread["page"]["estimate"].clone()), + (2, json!(6)) + ); + assert_eq!( + last_query(&world, "$filter").param("$filter"), + Some("receivedDateTime ge 1900-01-01T00:00:00Z and isRead eq false") + ); + // A folder the mailbox lacks is said so before anything is listed. + world.fake.state().roles.retain(|r| *r != Role::Archive); + ok!(world, "m365.invalidate", {}); + assert_eq!( + list(&world, "role:archive", 3, "").await, + Err("m365_folder_unavailable") + ); +} + +#[tokio::test] +async fn a_flagged_view_filters_the_inbox_and_estimates_from_what_it_read() { + isolated!(); + let world = World::new().await; + inbox(&world, 5); + for n in [1, 4, 5] { + world.fake.state().msg(n).flagged = true; + } + let flagged = list(&world, "role:inbox flagged", 2, "").await.unwrap(); + // The rows and one more: Graph is not asked to count. + assert_eq!( + (ids(&flagged), flagged["page"]["estimate"].clone()), + (vec![oid(5), oid(4)], json!(3)) + ); + let request = last_query(&world, "$filter"); + assert!(request.param("$count").is_none()); + assert!( + request + .param("$filter") + .unwrap() + .ends_with("flag/flagStatus eq 'flagged'") + ); +} + +#[tokio::test] +async fn a_search_is_one_phrase_over_the_mailbox_without_trash_and_junk() { + isolated!(); + let world = World::new().await; + { + let mut state = world.fake.state(); + for (n, role, subject) in [ + (1, Role::Inbox, "Invoice June"), + (2, Role::Trash, "Invoice May"), + (3, Role::Archive, "Invoice April"), + (4, Role::Junk, "Invoice March"), + (5, Role::Inbox, "Lunch"), + ] { + state.add(n, role, subject); + } + } + // The phrase is `"invoice x"`: no match for that exact text. + let none = list(&world, "text: invoice \"x\" ", 10, "").await.unwrap(); + assert!(ids(&none).is_empty()); + let request = last_query(&world, "$search"); + assert_eq!(request.param("$search"), Some("\"invoice x\"")); + assert!(request.param("$orderby").is_none() && request.param("$filter").is_none()); + assert_eq!( + ids(&list(&world, "text:invoice", 10, "").await.unwrap()), + [oid(3), oid(1)] + ); + // A page that held only Trash and Junk is not an empty result: the listing goes on, a few pages. + { + let mut state = world.fake.state(); + state.msgs.clear(); + state.add(1, Role::Inbox, "Invoice new"); + (5..=16).for_each(|n| { + state.add(n, Role::Trash, "Invoice old"); + }); + } + let reached = list(&world, "text:invoice", 2, "").await.unwrap(); + assert!( + ids(&reached).is_empty() && reached["page"]["nextPageToken"] != "", + "the search ends after a few pages and says there is more" + ); + world + .fake + .state() + .msgs + .retain(|m| m.folder != Role::Trash || m.id < gid(9)); + assert_eq!( + ids(&list(&world, "text:invoice", 2, "").await.unwrap()), + [oid(1)] + ); +} + +#[tokio::test] +async fn header_rows_come_from_the_listing_and_a_missing_message_does_not_fail_the_others() { + isolated!(); + let world = World::new().await; + inbox(&world, 3); + let listed = list(&world, "role:inbox", 3, "").await.unwrap(); + let asked = |world: &World| { + world + .fake + .graph() + .iter() + .filter(|r| r.path.starts_with("/v1.0/me/messages/")) + .count() + }; + let rows = ok!(world, "m365.messages", {"ids": [oid(3), oid(1)], "full": false}); + assert_eq!( + rows["messages"], + json!([listed["rows"][0], listed["rows"][2]]) + ); + assert_eq!((rows["warning"].clone(), asked(&world)), (Value::Null, 0)); + // A message nobody listed is read once; one Graph does not have is left out and said so. + world.fake.state().add(4, Role::Inbox, "Fresh"); + world.fake.state().add(5, Role::Inbox, "Unlisted"); + let mixed = ok!(world, "m365.messages", {"ids": [oid(4), oid(77), oid(3)], "full": false}); + assert_eq!( + ( + mixed["messages"].as_array().unwrap().len(), + mixed["warning"].clone() + ), + (2, json!("partial")) + ); + let headers = mixed["messages"][0]["payload"]["headers"] + .as_array() + .unwrap(); + assert_eq!( + ( + mixed["messages"][0]["id"].clone(), + headers.iter().find(|h| h["name"] == "Subject").unwrap()["value"].clone() + ), + (json!(oid(4)), json!("Fresh")) + ); + // Nothing found is an answer when "not found" is why, and an error when something else is. + let none = ok!(world, "m365.messages", {"ids": [oid(77)], "full": false}); + assert_eq!( + (none["messages"].clone(), none["warning"].clone()), + (json!([]), json!("partial")) + ); + world.fake.hook(|req| { + req.path + .starts_with("/v1.0/me/messages/") + .then(|| Resp::status(500)) + }); + assert_eq!( + err!(world, "m365.messages", {"ids": [oid(5)], "full": false}), + "m365_server_error" + ); + // A change drops what was remembered: the next read shows it. + world.fake.hook(|_| None); + ok!(world, "m365.modify", {"ids": [oid(3)], "removeLabelIds": ["UNREAD"]}); + let after = ok!(world, "m365.messages", {"ids": [oid(3)], "full": false}); + assert_eq!(after["messages"][0]["labelIds"], json!(["INBOX"])); +} + +fn multipart() -> Vec { + "From: Bob \r\nTo: alice@example.org\r\nSubject: With file\r\nDate: Fri, 9 Oct 2026 10:00:00 +0000\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"b\"\r\n\r\n--b\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nSee attached.\r\n--b\r\nContent-Type: application/octet-stream; name=\"a.bin\"\r\nContent-Disposition: attachment; filename=\"a.bin\"\r\nContent-Transfer-Encoding: base64\r\n\r\nAAECAwQF\r\n--b--\r\n".into() +} + +#[tokio::test] +async fn a_whole_message_is_the_shared_parse_of_its_mime_and_attachments_are_fetched_by_part() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + world.fake.state().add(2, Role::Inbox, "With file").mime = multipart(); + let read = ok!(world, "m365.read", {"id": oid(2), "full": true}); + assert_eq!( + ( + read["id"].clone(), + read["labelIds"].clone(), + read["sizeEstimate"].clone() + ), + ( + json!(oid(2)), + json!(["UNREAD", "INBOX"]), + json!(multipart().len()) + ) + ); + assert_eq!( + read["payload"]["parts"][0]["body"]["data"], + "U2VlIGF0dGFjaGVkLg" + ); + let attachment = read["payload"]["parts"][1]["body"]["attachmentId"] + .as_str() + .unwrap(); + assert_eq!(attachment, "part:2"); + assert_eq!( + ok!(world, "m365.attachment", {"messageId": oid(2), "attachmentId": attachment}), + json!({"data": "AAECAwQF"}) + ); + for (message, part, code) in [ + (2, "part:9", "m365_attachment_not_found"), + (1, "part:1", "m365_attachment_not_found"), + (55, "part:1", "m365_message_not_found"), + ] { + assert_eq!( + err!(world, "m365.attachment", {"messageId": oid(message), "attachmentId": part}), + code + ); + } + // `messages` with `full` is the same resource, many at a time. + let many = ok!(world, "m365.messages", {"ids": [oid(2), oid(1)], "full": true}); + assert_eq!( + ( + many["messages"][0].clone(), + many["messages"][1]["id"].clone() + ), + (read, json!(oid(1))) + ); + assert_eq!( + err!(world, "m365.read", {"id": oid(66), "full": true}), + "m365_message_not_found" + ); + // A message Graph has with a MIME nobody can read is not shown as an empty one. + world.fake.state().msg(1).mime = Vec::new(); + assert_eq!( + err!(world, "m365.read", {"id": oid(1), "full": true}), + "m365_invalid_response" + ); +} + +#[tokio::test] +async fn a_message_reads_up_to_24_mib_and_not_a_byte_more() { + isolated!(); + let world = World::new().await; + let big = |size: usize| { + let mut mime = plain_mime("Big"); + mime.resize(size, b'x'); + mime + }; + world.fake.state().add(1, Role::Inbox, "Big").mime = big(17 * 1024 * 1024); + world.fake.state().add(2, Role::Inbox, "Bigger").mime = big(24 * 1024 * 1024 + 1); + assert_eq!( + ok!(world, "m365.read", {"id": oid(1), "full": true})["sizeEstimate"], + 17 * 1024 * 1024 + ); + assert_eq!( + err!(world, "m365.read", {"id": oid(2), "full": true}), + "m365_message_too_large" + ); + // The ceiling is the same for an attachment and for a batch, where the rest still arrive. + assert_eq!( + err!(world, "m365.attachment", {"messageId": oid(2), "attachmentId": "part:"}), + "m365_message_too_large" + ); + let both = ok!(world, "m365.messages", {"ids": [oid(1), oid(2)], "full": true}); + assert_eq!( + ( + both["messages"].as_array().unwrap().len(), + both["warning"].clone() + ), + (1, json!("partial")) + ); +} + +#[tokio::test] +async fn verify_says_what_the_mailbox_has_and_withdraws_what_it_lacks() { + isolated!(); + let world = World::new().await; + inbox(&world, 3); + world.fake.state().msg(1).read = true; + world.fake.state().add(4, Role::Sent, "Out").read = true; + let verified = ok!(world, "m365.verify", {}); + assert_eq!( + ( + verified["ok"].clone(), + verified["email"].clone(), + verified["refusals"].clone() + ), + (json!(true), json!(ADDRESS), json!({})) + ); + assert_eq!( + ( + verified["roles"]["inbox"].clone(), + verified["roles"]["sent"].clone() + ), + ( + json!({"total": 3, "unread": 2}), + json!({"total": 1, "unread": 0}) + ) + ); + // No Archive and no Sent folder: those roles are null and the matching abilities are withdrawn. + world + .fake + .state() + .roles + .retain(|r| !matches!(r, Role::Archive | Role::Sent)); + let lacking = ok!(world, "m365.verify", {}); + assert_eq!( + ( + lacking["roles"]["archive"].clone(), + lacking["roles"]["sent"].clone() + ), + (Value::Null, Value::Null) + ); + assert_eq!( + lacking["refusals"]["archive"], + "No Archive folder was found in this mailbox" + ); + assert_eq!( + lacking["refusals"]["send"], + "No Sent folder was found in this mailbox" + ); + // Without an Inbox it is not a mailbox. + world.fake.state().roles.clear(); + assert_eq!(err!(world, "m365.verify", {}), "m365_folder_unavailable"); +} + +#[tokio::test] +async fn the_background_check_counts_unread_mail_and_its_fingerprint_follows_the_inbox() { + isolated!(); + let world = World::new().await; + inbox(&world, 5); + let first = check(ACCOUNT).await.unwrap(); + assert_eq!( + ( + first["estimate"].clone(), + first["messages"].as_array().unwrap().len(), + first["messages"][0]["id"].clone() + ), + (json!(5), 3, json!(oid(5))) + ); + assert_eq!( + check(ACCOUNT).await.unwrap()["fingerprint"], + first["fingerprint"] + ); + world.fake.state().msg(5).read = true; + let read = check(ACCOUNT).await.unwrap(); + assert_eq!( + ( + read["fingerprint"] != first["fingerprint"], + read["estimate"].clone() + ), + (true, json!(4)) + ); + world.fake.state().add(6, Role::Inbox, "New"); + assert_ne!( + check(ACCOUNT).await.unwrap()["fingerprint"], + read["fingerprint"] + ); + assert_eq!( + check("m365:stranger@example.org").await, + Err("auth_account_missing") + ); + assert_eq!( + check("outlook:alice@example.org").await, + Err("invalid_params") + ); +} diff --git a/src/providers/m365/tests/signin.rs b/src/providers/m365/tests/signin.rs new file mode 100644 index 0000000..e0640c4 --- /dev/null +++ b/src/providers/m365/tests/signin.rs @@ -0,0 +1,894 @@ +//! The sign-in and the tokens behind it: the device flow, where the refresh token goes, rotation, +//! refusals and what never leaves Rust. + +use std::time::Duration; + +use serde_json::{Value, json}; + +use super::super::{call, signin}; +use super::{ACCOUNT, ADDRESS, CLIENT, MemoryStore, Resp, World, inbox, isolated}; +use crate::auth::m365::SCOPE; +use signin::INTERVAL; + +async fn begin(world: &World) -> Value { + let params = json!({"address": ADDRESS, "clientId": CLIENT}); + world.call("m365.signInBegin", params).await.unwrap() +} + +async fn poll(request: &Value) -> Value { + call( + "m365.signInPoll", + &json!({"requestId": request["requestId"]}), + ) + .await + .unwrap() +} + +/// What the whole conversation leaves in an answer: no token, no device code. +fn assert_no_secret(answer: &Value) { + let text = answer.to_string(); + for secret in ["refresh-", "access-", "synthetic-device-code", "id_token"] { + assert!(!text.contains(secret), "{secret} in {text}"); + } +} + +#[tokio::test] +async fn a_sign_in_keeps_the_refresh_token_in_the_keyring_and_nothing_in_the_answers() { + isolated!(); + let world = World::signed_out().await; + let started = begin(&world).await; + assert_eq!(started["userCode"], "ABCD1234"); + assert_eq!( + started["verificationUri"], + "https://microsoft.com/devicelogin" + ); + assert_eq!( + (started["expiresIn"].clone(), started["interval"].clone()), + (json!(900), json!(5)) + ); + assert_eq!(started["requestId"].as_str().unwrap().len(), 32); + assert_no_secret(&started); + // One code asks for Graph scopes only, for the application id the person gave, of the tenant the + // address's domain names. + let asked = &world.fake.login()[0]; + assert_eq!(asked.path, "/example.org/oauth2/v2.0/devicecode"); + assert_eq!(asked.field("client_id").as_deref(), Some(CLIENT)); + assert_eq!(asked.field("scope").as_deref(), Some(SCOPE)); + assert!(asked.field("client_secret").is_none()); + + // Pending, then signed in; the refresh token is in the keyring under its kind. + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world + .fake + .state() + .device + .extend([(400, json!({"error": "authorization_pending"})), grant]); + assert_eq!(poll(&started).await, json!({"state": "pending"})); + let done = poll(&started).await; + assert_eq!(done, json!({"state": "signedIn"})); + assert_no_secret(&done); + assert_eq!(world.store.token().as_deref(), Some("refresh-1")); + assert_eq!(world.store.puts.lock().unwrap().len(), 1); + let token_request = world.fake.login().pop().unwrap(); + assert_eq!(token_request.path, "/example.org/oauth2/v2.0/token"); + assert_eq!( + token_request.field("device_code").as_deref(), + Some("synthetic-device-code") + ); + assert_eq!(token_request.field("client_id").as_deref(), Some(CLIENT)); + // The finished sign-in answers the same again, without asking Microsoft. + let requests = world.fake.login().len(); + assert_eq!(poll(&started).await, done); + assert_eq!(world.fake.login().len(), requests); + // And the mailbox works with the token it made. + inbox(&world, 1); + assert_eq!( + world.call("m365.verify", json!({})).await.unwrap()["ok"], + true + ); + // `slow_down` lengthens the interval: the next poll at once is answered without a request. + let started = begin(&world).await; + world + .fake + .state() + .device + .push_back((400, json!({"error": "slow_down"}))); + assert_eq!(poll(&started).await, json!({"state": "pending"})); + let requests = world.fake.login().len(); + assert_eq!(poll(&started).await, json!({"state": "pending"})); + assert_eq!(world.fake.login().len(), requests); +} + +#[tokio::test] +async fn an_application_id_typed_in_capitals_is_the_same_application_after_the_sign_in() { + isolated!(); + let world = World::signed_out().await; + let upper = "ABCDEF00-1111-2222-3333-44444444444F"; + let lower = upper.to_ascii_lowercase(); + world.save_client(upper); + let params = json!({"address": ADDRESS, "clientId": upper}); + let started = world.call("m365.signInBegin", params).await.unwrap(); + assert_eq!( + world.fake.login()[0].field("client_id"), + Some(lower.clone()) + ); + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world.fake.state().device.push_back(grant); + assert_eq!(poll(&started).await, json!({"state": "signedIn"})); + // The first request after it refreshes from the saved entry, and finds the token it saved. + inbox(&world, 1); + assert_eq!( + world.call("m365.verify", json!({})).await.unwrap()["ok"], + true + ); + let refresh = world.fake.login().pop().unwrap(); + assert_eq!( + refresh.field("grant_type").as_deref(), + Some("refresh_token") + ); + assert_eq!(refresh.field("client_id"), Some(lower)); + // And a sign-out in capitals removes that same item. + world + .call("m365.signOut", json!({"clientId": upper})) + .await + .unwrap(); + assert_eq!( + world.call("m365.verify", json!({})).await, + Err("m365_signed_out") + ); +} + +#[tokio::test] +async fn a_sign_in_ends_as_the_person_and_microsoft_decide_and_a_wrong_account_keeps_nothing() { + isolated!(); + let world = World::signed_out().await; + for (answer, state, error) in [ + ( + (400, json!({"error": "authorization_declined"})), + "declined", + None, + ), + ((400, json!({"error": "expired_token"})), "expired", None), + ( + ( + 400, + json!({"error": "invalid_grant", "error_codes": [65001]}), + ), + "failed", + Some("m365_consent_required"), + ), + ( + ( + 400, + json!({"error": "invalid_grant", "error_description": "AADSTS90094: admin"}), + ), + "failed", + Some("m365_consent_required"), + ), + ( + ( + 400, + json!({"error": "invalid_request", "error_codes": [900023]}), + ), + "failed", + Some("m365_tenant_invalid"), + ), + ( + (400, json!({"error": "access_denied"})), + "failed", + Some("m365_access_denied"), + ), + ( + (400, json!({"error": "unauthorized_client"})), + "failed", + Some("m365_auth_failed"), + ), + // A grant that is not what was asked for is refused whole. + ( + ( + 200, + json!({"access_token": "a", "scope": "Mail.Send openid"}), + ), + "failed", + Some("m365_consent_required"), + ), + ( + ( + 200, + json!({"access_token": "a", "scope": "Mail.ReadWrite Mail.Send Calendars.ReadWrite", "refresh_token": ""}), + ), + "failed", + Some("m365_invalid_response"), + ), + ( + ( + 200, + json!({"scope": "Mail.ReadWrite Mail.Send Calendars.ReadWrite", "refresh_token": "r"}), + ), + "failed", + Some("m365_invalid_response"), + ), + ] { + let started = begin(&world).await; + world.fake.state().device.push_back(answer.clone()); + let done = poll(&started).await; + assert_eq!(done["state"], state, "{answer:?}"); + assert_eq!(done["error"].as_str(), error, "{answer:?}"); + assert!(world.store.token().is_none(), "{answer:?} kept a token"); + } + // Somebody else's code: the id token names another account, which is said and not kept. + let started = begin(&world).await; + let other = (200, world.fake.state().issue(Some("Mallory@Example.org"))); + world.fake.state().device.push_back(other); + let done = poll(&started).await; + assert_eq!( + done, + json!({"state": "failed", "error": "m365_wrong_account", "signedInAs": "mallory@example.org"}) + ); + assert!(world.store.token().is_none() && world.store.puts.lock().unwrap().is_empty()); + // Throttling and a server's own trouble keep the sign-in going; the warning says so. + let started = begin(&world).await; + world + .fake + .state() + .device + .extend([(429, json!({})), (503, json!({}))]); + for code in ["m365_throttled", "m365_server_error"] { + assert_eq!( + poll(&started).await, + json!({"state": "pending", "warning": code}) + ); + } + // Nor is a grant whose id token names nobody: whose code it was cannot be told. + let anonymous = (200, world.fake.state().issue(None)); + world.fake.state().device.push_back(anonymous); + assert_eq!( + poll(&started).await, + json!({"state": "failed", "error": "m365_auth_failed"}) + ); + assert!(world.store.token().is_none()); +} + +#[tokio::test] +async fn any_name_the_id_token_gives_may_be_the_address_and_otherwise_the_browser_is_named() { + isolated!(); + let world = World::signed_out().await; + for (claims, signed_in_as) in [ + // The sign-in name differs from the mail address (an alias or a hybrid account): the + // address is in another claim, in any case. + ( + json!({"preferred_username": "asmith@corp.example", "email": "Alice@Example.ORG"}), + None, + ), + (json!({"preferred_username": "ALICE@EXAMPLE.ORG"}), None), + ( + json!({"preferred_username": "x@y.example", "upn": " alice@example.org "}), + None, + ), + // Not one of the names is the address: the browser was signed in as another account, and + // the sign-in name is the one said. + ( + json!({"preferred_username": "Asmith@corp.example", "email": "bob@example.org", "upn": "c@d.example"}), + Some("asmith@corp.example"), + ), + (json!({"email": "bob@example.org"}), Some("bob@example.org")), + ] { + let started = begin(&world).await; + let grant = (200, world.fake.state().issue_claims(Some(claims.clone()))); + world.fake.state().device.push_back(grant); + let done = poll(&started).await; + match signed_in_as { + None => { + assert_eq!(done, json!({"state": "signedIn"}), "{claims}"); + assert!(world.store.token().is_some()); + world + .call("m365.signOut", json!({"clientId": CLIENT})) + .await + .unwrap(); + } + Some(name) => { + assert_eq!( + done, + json!({"state": "failed", "error": "m365_wrong_account", "signedInAs": name}), + "{claims}" + ); + assert!(world.store.token().is_none(), "{claims}"); + } + } + } +} + +#[tokio::test] +async fn a_sign_in_refuses_what_microsoft_should_not_say() { + isolated!(); + let world = World::signed_out().await; + for bad in [ + json!({"device_code": "d", "user_code": "ABCD", "verification_uri": "https://evil.test/login", "expires_in": 900}), + json!({"device_code": "d", "user_code": "AB CD", "verification_uri": "https://microsoft.com/devicelogin"}), + json!({"device_code": "", "user_code": "ABCD", "verification_uri": "https://microsoft.com/devicelogin"}), + json!({"user_code": "ABCD", "verification_uri": "https://microsoft.com/devicelogin"}), + json!({"device_code": "d d", "user_code": "ABCD", "verification_uri": "https://microsoft.com/devicelogin"}), + ] { + world.fake.hook(move |req| { + req.path + .ends_with("devicecode") + .then(|| Resp::json(200, bad.clone())) + }); + let params = json!({"address": ADDRESS, "clientId": CLIENT}); + assert_eq!( + world.call("m365.signInBegin", params).await, + Err("m365_invalid_response") + ); + } + // The login host failing in transit or answering something else is a code, never its words. + world + .fake + .hook(|_| Some(Resp::Send(200, vec![], b"not json".to_vec()))); + let params = json!({"address": ADDRESS, "clientId": CLIENT}); + assert_eq!( + world.call("m365.signInBegin", params).await, + Err("m365_invalid_response") + ); + world + .fake + .hook(|_| Some(Resp::Lying(300_000, vec![b'x'; 300_000]))); + let params = json!({"address": ADDRESS, "clientId": CLIENT}); + assert_eq!( + world.call("m365.signInBegin", params).await, + Err("m365_invalid_response") + ); +} + +#[tokio::test] +async fn a_sign_in_goes_to_the_domains_tenant_and_once_more_to_organizations_when_there_is_none() { + isolated!(); + let world = World::signed_out().await; + let paths = |world: &World| -> Vec { + let all = world.fake.login(); + all.iter().map(|request| request.path.clone()).collect() + }; + // Nobody has the domain: Microsoft says so, the request is made again to `organizations`, and + // the rest of that sign-in (its polls) stays there. + world + .fake + .hook(move |req| req.path.starts_with("/example.org/").then(unknown_tenant)); + let started = begin(&world).await; + assert_eq!( + paths(&world), + [ + "/example.org/oauth2/v2.0/devicecode", + "/organizations/oauth2/v2.0/devicecode" + ] + ); + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world.fake.state().device.push_back(grant); + assert_eq!(poll(&started).await, json!({"state": "signedIn"})); + assert_eq!( + paths(&world).last().unwrap(), + "/organizations/oauth2/v2.0/token" + ); + // A refresh asks as the sign-in did, and every one of them: nothing is remembered between them. + inbox(&world, 1); + assert_eq!( + world.call("m365.verify", json!({})).await.unwrap()["ok"], + true + ); + assert_eq!( + paths(&world)[paths(&world).len() - 2..], + [ + "/example.org/oauth2/v2.0/token", + "/organizations/oauth2/v2.0/token" + ] + ); + // Not a tenant anywhere: two requests and no more, and the code says so. + world + .fake + .hook(|req| req.path.ends_with("devicecode").then(unknown_tenant)); + let before = paths(&world).len(); + let params = json!({"address": ADDRESS, "clientId": CLIENT}); + assert_eq!( + world.call("m365.signInBegin", params.clone()).await, + Err("m365_tenant_invalid") + ); + assert_eq!(paths(&world).len() - before, 2); + // Any other refusal from the domain's tenant is its answer: it is not asked again elsewhere. + world.fake.hook(|req| { + req.path + .starts_with("/example.org/") + .then(|| Resp::json(400, json!({"error_codes": [50194]}))) + }); + let before = paths(&world).len(); + assert_eq!( + world.call("m365.signInBegin", params).await, + Err("m365_auth_failed") + ); + assert_eq!(paths(&world).len() - before, 1); +} + +fn unknown_tenant() -> Resp { + let words = "AADSTS90002: Tenant not found"; + Resp::json( + 400, + json!({"error": "invalid_request", "error_codes": [90002], "error_description": words}), + ) +} + +#[tokio::test] +async fn at_most_four_sign_ins_wait_and_a_request_asked_too_soon_costs_microsoft_nothing() { + isolated!(); + let world = World::signed_out().await; + *INTERVAL.lock().unwrap() = Some(Duration::from_secs(600)); + let mut started = Vec::new(); + for _ in 0..5 { + started.push(begin(&world).await); + } + let requests = world.fake.login().len(); + for request in &started[1..] { + assert_eq!(poll(request).await, json!({"state": "pending"})); + } + assert_eq!( + world.fake.login().len(), + requests, + "an early poll reached Microsoft" + ); + // The newest request wins: the oldest gave way and is unknown. + assert_eq!(poll(&started[0]).await, json!({"state": "expired"})); + assert_eq!( + call("m365.signInPoll", &json!({"requestId": "0".repeat(32)})) + .await + .unwrap(), + json!({"state": "expired"}) + ); +} + +#[tokio::test] +async fn sign_out_deletes_the_token_stops_a_sign_in_under_way_and_works_when_nothing_is_stored() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + assert_eq!( + world.call("m365.verify", json!({})).await.unwrap()["ok"], + true + ); + let started = begin(&world).await; + let out = || world.call("m365.signOut", json!({"clientId": CLIENT})); + assert_eq!(out().await.unwrap(), json!({"signedOut": true})); + assert_eq!(out().await.unwrap(), json!({"signedOut": true})); + assert!(world.store.token().is_none()); + // What a sign-in under way would have kept is not kept after a sign-out. + assert_eq!(poll(&started).await, json!({"state": "expired"})); + // The in-memory access token went too: the mailbox is signed out at once. + assert_eq!( + world.call("m365.verify", json!({})).await, + Err("m365_signed_out") + ); + // A client id other than the stored one touches nothing. + world.store.set("refresh-0"); + let other = world + .call( + "m365.signOut", + json!({"clientId": "99999999-1111-2222-3333-444444444444"}), + ) + .await; + assert!(other.is_ok() && world.store.token().is_some()); +} + +#[tokio::test] +async fn a_sign_out_of_one_application_leaves_a_sign_in_of_another_under_way() { + isolated!(); + let world = World::signed_out().await; + let started = begin(&world).await; + // The window signs out what a cancelled sign-in of the ID typed before may have saved, while the + // sign-in with the ID typed now waits for its code: that one writes another keyring item. + let before = "99999999-1111-2222-3333-444444444444"; + assert_eq!( + world + .call("m365.signOut", json!({"clientId": before})) + .await + .unwrap(), + json!({"signedOut": true}) + ); + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world.fake.state().device.push_back(grant); + assert_eq!(poll(&started).await, json!({"state": "signedIn"})); + assert_eq!(world.store.token().as_deref(), Some("refresh-1")); +} + +#[tokio::test] +async fn a_sign_out_behind_a_refresh_waiting_on_the_keyring_says_it_was_not_done_in_time() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + *signin::SHORT_LANE_WAIT.lock().unwrap() = Some(Duration::from_millis(300)); + // A refresh rotates the token, and its keyring save waits for somebody to unlock the keyring: + // the mailbox's refresh lane is held all that time. + let saving = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let entered = std::sync::Arc::clone(&saving); + let held = std::sync::Arc::new(move || { + entered.store(true, std::sync::atomic::Ordering::SeqCst); + std::thread::sleep(Duration::from_millis(1500)); + }); + *world.store.during_put.lock().unwrap() = Some(held); + let refresh = world.call("m365.verify", json!({})); + let behind = async { + while !saving.load(std::sync::atomic::Ordering::SeqCst) { + tokio::time::sleep(Duration::from_millis(10)).await; + } + let started = std::time::Instant::now(); + let out = world + .call("m365.signOut", json!({"clientId": CLIENT})) + .await; + let forgot = world.call("m365.invalidate", json!({})).await; + (out, forgot, started.elapsed()) + }; + let (verified, (out, forgot, waited)) = tokio::join!(refresh, behind); + assert_eq!(out, Err("m365_timeout")); + assert_eq!(forgot, Err("m365_timeout")); + assert!( + waited < Duration::from_millis(1000), + "answered after {waited:?}, with the lane" + ); + // Not signed out: the rotated token was saved after all and the mailbox goes on working. + assert_eq!(verified.unwrap()["ok"], true); + assert_eq!(world.store.token().as_deref(), Some("refresh-1")); + // With the lane free, the same sign-out is done. + *world.store.during_put.lock().unwrap() = None; + let out = world + .call("m365.signOut", json!({"clientId": CLIENT})) + .await; + assert_eq!(out, Ok(json!({"signedOut": true}))); + assert!(world.store.token().is_none()); +} + +#[tokio::test] +async fn a_sign_out_that_lands_while_the_token_is_being_saved_leaves_nothing_saved() { + isolated!(); + let world = World::signed_out().await; + let started = begin(&world).await; + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world.fake.state().device.push_back(grant); + // The sign-out comes after the grant was judged and before the keyring write has landed. + let during = std::sync::Arc::new(|| signin::end_flows(ACCOUNT, CLIENT)); + *world.store.during_put.lock().unwrap() = Some(during); + assert_eq!(poll(&started).await, json!({"state": "expired"})); + assert!(world.store.token().is_none()); +} + +#[tokio::test] +async fn a_refreshed_token_that_rotates_is_saved_before_it_is_used() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + assert_eq!( + world.call("m365.verify", json!({})).await.unwrap()["ok"], + true + ); + let refresh = world.fake.login(); + assert_eq!(refresh.len(), 1); + assert_eq!(refresh[0].path, "/example.org/oauth2/v2.0/token"); + assert_eq!( + refresh[0].field("grant_type").as_deref(), + Some("refresh_token") + ); + assert_eq!( + refresh[0].field("refresh_token").as_deref(), + Some("refresh-0") + ); + assert_eq!(refresh[0].field("scope").as_deref(), Some(SCOPE)); + assert_eq!(refresh[0].field("client_id").as_deref(), Some(CLIENT)); + assert_eq!(world.store.token().as_deref(), Some("refresh-1")); + // The access token is reused until it nears its expiry: no second refresh, no second token. + world + .call("m365.list", json!({"query": "role:inbox", "limit": 5})) + .await + .unwrap(); + assert_eq!(world.fake.login().len(), 1); + // The next refresh presents the rotated token, and its own rotation is saved in turn. + world.call("m365.invalidate", json!({})).await.unwrap(); + world + .call("m365.list", json!({"query": "role:inbox", "limit": 5})) + .await + .unwrap(); + assert_eq!( + world.fake.login()[1].field("refresh_token").as_deref(), + Some("refresh-1") + ); + let saved: Vec = world + .store + .puts + .lock() + .unwrap() + .iter() + .map(|(_, token)| token.clone()) + .collect(); + assert_eq!(saved, ["refresh-1", "refresh-2"]); + assert!( + world + .store + .puts + .lock() + .unwrap() + .iter() + .all(|(name, _)| name.contains("M365RefreshToken")) + ); +} + +#[tokio::test] +async fn a_burst_of_requests_makes_one_refresh_and_a_revoked_token_is_refreshed_once() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + let list = |_| world.call("m365.list", json!({"query": "role:inbox", "limit": 5})); + let answers = futures_util::future::join_all((0..6).map(list)).await; + assert!(answers.iter().all(Result::is_ok)); + assert_eq!(world.fake.login().len(), 1, "one refresh serves the burst"); + // Graph refuses the token (revoked): one fresh token and the request again, no more. + world.fake.state().access = "revoked".into(); + let listed = world + .call("m365.list", json!({"query": "role:inbox", "limit": 5})) + .await; + assert!(listed.is_ok(), "{listed:?}"); + assert_eq!(world.fake.login().len(), 2); +} + +#[tokio::test] +async fn a_refused_grant_a_withdrawn_consent_and_an_empty_keyring_each_say_so_and_ask_no_further() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + let verify = || async { + let result = world.call("m365.verify", json!({})).await; + // A failed folder lookup drops its siblings, but an admitted refresh finishes under its + // lane guard. Drain it before changing the next scenario's hook or counting its requests. + tokio::time::timeout(Duration::from_secs(30), crate::auth::invalidate(ACCOUNT)) + .await + .expect("previous refresh did not finish") + .unwrap(); + result + }; + world.store.set("stale"); + assert_eq!(verify().await, Err("m365_signed_out")); + world.store.set("refresh-0"); + world.fake.hook(|req| { + req.path.ends_with("/token").then(|| { + Resp::json( + 400, + json!({"error": "invalid_grant", "error_codes": [65001]}), + ) + }) + }); + assert_eq!(verify().await, Err("m365_consent_required")); + // Consent withdrawn for one permission: the token Microsoft gives lacks it. + world.fake.hook(|req| req.path.ends_with("/token").then(|| Resp::json(200, json!({"access_token": "a", "scope": "Mail.ReadWrite Mail.Send", "expires_in": 3600})))); + assert_eq!(verify().await, Err("m365_consent_required")); + world.fake.hook(|req| { + req.path + .ends_with("/token") + .then(|| Resp::json(503, json!({}))) + }); + assert_eq!(verify().await, Err("m365_auth_failed")); + // A batch whose grant is refused does not send it fifty times: the rest fail without asking. + world.fake.hook(|_| None); + world.store.set("stale"); + inbox(&world, 12); + let before = world.fake.login().len(); + let ids: Vec = (1..=12).map(super::oid).collect(); + let changed = world + .call( + "m365.modify", + json!({"ids": ids, "removeLabelIds": ["UNREAD"]}), + ) + .await + .unwrap(); + assert_eq!( + ( + changed["failedIds"].as_array().unwrap().len(), + &changed["error"] + ), + (12, &json!("m365_signed_out")) + ); + assert!( + world.fake.login().len() - before <= 4, + "{} refresh requests", + world.fake.login().len() - before + ); + world.store.set("refresh-0"); + // Nothing in the keyring: signed out, and the login host is not asked. + let requests = world.fake.login().len(); + world.fake.hook(|_| None); + world + .call("m365.signOut", json!({"clientId": CLIENT})) + .await + .unwrap(); + assert_eq!(verify().await, Err("m365_signed_out")); + assert_eq!(world.fake.login().len(), requests); +} + +#[tokio::test] +async fn a_mailbox_that_is_not_in_the_registry_asks_nothing_of_the_keyring_or_the_network() { + isolated!(); + let world = World::new().await; + let gets = |store: &MemoryStore| store.gets.load(std::sync::atomic::Ordering::SeqCst); + let before = gets(&world.store); + let stranger = "m365:stranger@example.org"; + for (method, params) in [ + ("m365.verify", json!({"accountId": stranger})), + ( + "m365.list", + json!({"accountId": stranger, "query": "role:inbox", "limit": 5}), + ), + ( + "m365.read", + json!({"accountId": stranger, "id": super::oid(1), "full": true}), + ), + ] { + assert_eq!( + call(method, ¶ms).await, + Err("auth_account_missing"), + "{method}" + ); + } + assert_eq!(gets(&world.store), before); + assert!(world.fake.state().seen.is_empty()); + // An Outlook mailbox of the same address is another account: its token is not this one's. + assert_eq!( + crate::auth::graph_token("outlook:bob@example.org").await, + Err("auth_signed_out") + ); + assert!(world.fake.login().is_empty()); + // The calendar takes the Graph token of an m365 account from this sign-in, with the same scope. + assert_eq!(crate::auth::graph_token(ACCOUNT).await.unwrap(), "access-1"); + assert_eq!(world.fake.login()[0].field("scope").as_deref(), Some(SCOPE)); + assert_eq!(world.store.token().as_deref(), Some("refresh-1")); +} + +#[tokio::test] +async fn a_poll_that_outwaits_its_exchange_says_pending_and_a_later_one_has_the_outcome() { + isolated!(); + let world = World::signed_out().await; + *signin::SHORT_POLL_WAIT.lock().unwrap() = Some(Duration::from_millis(200)); + let started = begin(&world).await; + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world.fake.state().device.push_back(grant); + // The keyring asks somebody to unlock it, and that takes longer than a poll waits. + let slow = std::sync::Arc::new(|| std::thread::sleep(Duration::from_millis(800))); + *world.store.during_put.lock().unwrap() = Some(slow); + // A warning of its own: a token request that timed out (`m365_timeout`) is over and saved nothing, + // while this exchange may still save a grant. + assert_eq!( + poll(&started).await, + json!({"state": "pending", "warning": "m365_exchange_running"}) + ); + assert_eq!( + poll(&started).await, + json!({"state": "pending"}), + "asked again while it saves" + ); + tokio::time::sleep(Duration::from_millis(1000)).await; + assert_eq!(poll(&started).await, json!({"state": "signedIn"})); + assert_eq!(world.store.token().as_deref(), Some("refresh-1")); + let grants = world + .fake + .login() + .iter() + .filter(|r| r.field("grant_type").as_deref() == Some(signin_grant())) + .count(); + assert_eq!(grants, 1, "Microsoft was asked once for the code"); +} + +fn signin_grant() -> &'static str { + "urn:ietf:params:oauth:grant-type:device_code" +} + +#[tokio::test] +async fn cancelled_older_grant_must_not_delete_newer_same_app_grant() { + isolated!(); + let world = World::signed_out().await; + let first = begin(&world).await; + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world.fake.state().device.push_back(grant); + let saving = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let release = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let entered = saving.clone(); + let released = release.clone(); + *world.store.during_put.lock().unwrap() = Some(std::sync::Arc::new(move || { + if !entered.swap(true, std::sync::atomic::Ordering::SeqCst) { + let deadline = std::time::Instant::now() + Duration::from_secs(5); + while !released.load(std::sync::atomic::Ordering::SeqCst) + && std::time::Instant::now() < deadline + { + std::thread::sleep(Duration::from_millis(5)); + } + } + })); + let next = async { + while !saving.load(std::sync::atomic::Ordering::SeqCst) { + tokio::time::sleep(Duration::from_millis(5)).await; + } + let sign_out = tokio::spawn(async { + call( + "m365.signOut", + &json!({"accountId": ACCOUNT, "clientId": CLIENT}), + ) + .await + }); + // The sign-out ends the old flow and queues its deletion behind the blocked save. + tokio::time::sleep(Duration::from_millis(50)).await; + let second = begin(&world).await; + let grant = (200, world.fake.state().issue(Some(ADDRESS))); + world.fake.state().device.push_back(grant); + let unblocking = async { + // Queue the second grant's save before allowing the older one to finish. + tokio::time::sleep(Duration::from_millis(100)).await; + release.store(true, std::sync::atomic::Ordering::SeqCst); + }; + let (newer, _) = tokio::join!(poll(&second), unblocking); + let out = sign_out.await.unwrap(); + (newer, out) + }; + let (older, (newer, out)) = tokio::join!(poll(&first), next); + assert_eq!(older, json!({"state": "expired"})); + assert_eq!(out, Ok(json!({"signedOut": true}))); + assert_eq!(newer, json!({"state": "signedIn"})); + assert_eq!( + world.store.token().as_deref(), + Some("refresh-2"), + "newer signed-in flow must keep its own token" + ); +} + +#[tokio::test] +async fn a_cancelled_grant_with_reused_token_bytes_cannot_delete_its_replacement() { + isolated!(); + let world = World::signed_out().await; + let saving = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let release = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let current = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)); + let entered = saving.clone(); + let released = release.clone(); + *world.store.during_put.lock().unwrap() = Some(std::sync::Arc::new(move || { + if !entered.swap(true, std::sync::atomic::Ordering::SeqCst) { + let deadline = std::time::Instant::now() + Duration::from_secs(5); + while !released.load(std::sync::atomic::Ordering::SeqCst) + && std::time::Instant::now() < deadline + { + std::thread::sleep(Duration::from_millis(5)); + } + } + })); + let live = current.clone(); + let older = crate::auth::m365::save_grant(ACCOUNT, CLIENT, "same-refresh", move || { + live.load(std::sync::atomic::Ordering::SeqCst) + }); + let replacement = async { + while !saving.load(std::sync::atomic::Ordering::SeqCst) { + tokio::time::sleep(Duration::from_millis(5)).await; + } + current.store(false, std::sync::atomic::Ordering::SeqCst); + let newer = crate::auth::m365::save_grant(ACCOUNT, CLIENT, "same-refresh", || true); + tokio::pin!(newer); + // This future reaches the held lane before the old keyring write may finish. + assert!(futures_util::poll!(newer.as_mut()).is_pending()); + release.store(true, std::sync::atomic::Ordering::SeqCst); + newer.await + }; + let (old, new) = tokio::join!(older, replacement); + assert_eq!(old, Ok(false)); + assert_eq!(new, Ok(true)); + assert_eq!(world.store.token().as_deref(), Some("same-refresh")); +} + +#[tokio::test] +async fn a_grant_cancelled_before_its_lane_is_admitted_keeps_no_token() { + isolated!(); + let world = World::signed_out().await; + assert_eq!( + crate::auth::m365::save_grant(ACCOUNT, CLIENT, "cancelled-refresh", || false).await, + Ok(false) + ); + assert!(world.store.token().is_none()); + assert!( + world.store.puts.lock().unwrap().is_empty(), + "cancelled grant must never reach the keyring" + ); +} diff --git a/src/providers/m365/tests/unit.rs b/src/providers/m365/tests/unit.rs new file mode 100644 index 0000000..5b6ac49 --- /dev/null +++ b/src/providers/m365/tests/unit.rs @@ -0,0 +1,527 @@ +//! Pure functions: identifiers, the query language, header synthesis, link and token validation. +//! String tables are `|`-separated. + +use std::time::Duration; + +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use serde_json::{Value, json}; + +use super::super::graph::{Fault, next_link, url}; +use super::super::mailbox::{seal, unseal}; +use super::super::model::{self, Folder, Query, Role, Roles, Scope}; +use super::super::{call, ids, send, signin}; +use super::{ACCOUNT, CLIENT, gid, oid}; + +fn b64(value: Value) -> String { + URL_SAFE_NO_PAD.encode(value.to_string()) +} + +#[test] +fn message_ids_have_one_spelling_and_fit_the_cache_file_name() { + let longest = "A".repeat(156); + for graph in [gid(1), longest.clone(), "AAMk-_=AAAA".to_owned()] { + let id = ids::encode(&graph).unwrap(); + assert_eq!(ids::decode(&id).unwrap(), graph); + } + assert_eq!(ids::encode(&gid(1)).unwrap(), "ifau222bi5etembqgayucqkbhu"); + // The cache escapes every byte outside [a-z0-9.-] and refuses names over 255 bytes. + let id = ids::encode(&longest).unwrap(); + assert_eq!(id.len(), 250); + crate::cache::validate_params(&json!({"accountId": ACCOUNT, "id": id}), true).unwrap(); + let bad = format!( + "|ifau222|ifau222bi5etembqgayucqkbhv|IFAU222BI5ETEMBQGAYUCQKBHU|{0}=|{0}aa|{1}aa|{2}|1:INBOX|../x|mfrgg43f|{3}", + "ifau222bi5etembqgayucqkbhu", + id, + "ifau222 bi5etembqgayucqkbhu", + "a".repeat(251) + ); + for bad in bad.split('|') { + assert_eq!( + ids::validate_message_id(bad), + Err("invalid_params"), + "{bad:?}" + ); + } + // What a server sends that cannot be carried is its fault. + for bad in [ + "", + "short", + "has space 12345", + "a/b/c/d/e/f", + "ünïcode12345", + &"A".repeat(157), + ] { + assert_eq!(ids::encode(bad), Err("m365_bad_response"), "{bad:?}"); + } +} + +#[test] +fn account_and_client_ids_are_validated_before_use() { + assert_eq!( + ids::account_id("m365:a@example.org"), + Ok("m365:a@example.org") + ); + let bad = "a@example.org|outlook:a@example.org|m365:A@example.org|m365:|m365:a|m365:a@example|m365:a b@example.org|m365:a@example.org\n"; + for bad in bad + .split('|') + .chain([format!("m365:{}@example.org", "a".repeat(400)).as_str()]) + { + assert_eq!(ids::account_id(bad), Err("invalid_params"), "{bad:?}"); + } + assert_eq!( + ids::client_id("ABCDEF01-2345-6789-abcd-ef0123456789").unwrap(), + "abcdef01-2345-6789-abcd-ef0123456789" + ); + let bad = "|not-a-guid|00000000-1111-2222-3333-44444444444|00000000111122223333444444444444|0000000g-1111-2222-3333-444444444444|00000000-1111-2222-3333-444444444444\n"; + for bad in bad.split('|') { + assert_eq!(ids::client_id(bad), Err("invalid_params"), "{bad:?}"); + } +} + +#[test] +fn queries_are_the_role_and_text_forms_and_nothing_else() { + let role = |role, unread, flagged| { + Ok(Query { + scope: Scope::Folder(role), + unread, + flagged, + }) + }; + for (text, expected) in [ + ("role:inbox", role(Role::Inbox, false, false)), + ("ROLE:Junk unseen", role(Role::Junk, true, false)), + (" role:inbox flagged unseen ", role(Role::Inbox, true, true)), + ("role:archive", role(Role::Archive, false, false)), + ] { + assert_eq!(model::parse(text), expected, "{text}"); + } + let bad = "|inbox|hello|role:|role: inbox|role:spam|role:inbox unseen unseen|role:inbox other|folder:inbox|text:|text: |text:\"|text:\u{201c}\u{201d}|text:***|text:()|text:-+|role:inbox\n|text:a\nb"; + for bad in bad + .split('|') + .chain([format!("text:{}", "a".repeat(5000)).as_str()]) + { + assert_eq!(model::parse(bad), Err("invalid_params"), "{bad:?}"); + } + // Graph reads a search as KQL: the words are searched as words, with nothing that is syntax. + for (typed, words) in [ + (" invoice \"OR\" \\ a\u{201d}b ", "invoice or a b"), + ( + "from:ada (x OR y) z* a AND NOT b", + "from ada x or y z a and not b", + ), + ("-x +y ~z a=b \u{ff02}c\u{ff02}", "x y z q a b c"), + ] { + let query = model::parse(&format!("text:{typed}")).unwrap(); + assert_eq!(query.scope, Scope::Text(words.into()), "{typed}"); + } + assert_eq!(model::parse("text:a b").unwrap().canonical(), "text:a b"); + assert_eq!( + model::parse("role:inbox flagged unseen") + .unwrap() + .canonical(), + "role:inbox unseen flagged" + ); +} + +fn graph_message(change: Value) -> Value { + let mut message = json!({ + "id": gid(1), "parentFolderId": "folder-inbox", "subject": "Hello", "isRead": false, + "from": {"emailAddress": {"name": "Bob", "address": "bob@example.org"}}, + "toRecipients": [{"emailAddress": {"name": "Alice", "address": "alice@example.org"}}], + "receivedDateTime": "2026-10-09T10:00:00Z", "bodyPreview": "Hi there", + }); + for (key, value) in change.as_object().unwrap() { + message[key] = value.clone(); + } + message +} + +fn roles() -> Roles { + let folder = |role: Role| Folder { + id: format!("folder-{}", role.well_known()), + total: 0, + unread: 0, + }; + Role::ALL + .into_iter() + .map(|role| (role, folder(role))) + .collect() +} + +fn summary(change: Value) -> Value { + let row = model::row(&graph_message(change), &roles()).unwrap(); + crate::message::content::summarize(&row, 0).unwrap() +} + +#[test] +fn a_row_is_a_gmail_resource_whose_labels_come_from_the_folder_and_the_flags() { + let flagged = json!({"isRead": true, "flag": {"flagStatus": "flagged"}}); + let in_folder = + |name: &str| json!({"parentFolderId": format!("folder-{name}"), "isRead": true}); + for (change, labels) in [ + (json!({}), json!(["UNREAD", "INBOX"])), + (flagged, json!(["STARRED", "INBOX"])), + (in_folder("sentitems"), json!(["SENT"])), + (in_folder("deleteditems"), json!(["TRASH"])), + (in_folder("junkemail"), json!(["SPAM"])), + (in_folder("drafts"), json!(["DRAFT"])), + (in_folder("archive"), json!([])), + // A message Graph does not say is unread is not shown as new mail; an unknown folder has no role. + ( + json!({"isRead": null, "parentFolderId": "elsewhere"}), + json!([]), + ), + ] { + let row = model::row(&graph_message(change.clone()), &roles()).unwrap(); + assert_eq!(row["labelIds"], labels, "{change}"); + } + let row = model::row(&graph_message(json!({})), &roles()).unwrap(); + assert_eq!( + (row["id"].clone(), row["threadId"].clone()), + (json!(oid(1)), json!(oid(1))) + ); + assert_eq!(row["internalDate"], "1791540000000"); + let shown = summary(json!({"subject": "a < b & c"})); + assert_eq!( + (shown["subject"].as_str(), shown["snippet"].as_str()), + (Some("a < b & c"), Some("Hi there")) + ); + assert_eq!( + ( + shown["from"]["email"].clone(), + shown["to"][0]["name"].clone() + ), + (json!("bob@example.org"), json!("Alice")) + ); + assert_eq!(shown["date"], "2026-10-09T10:00:00.000Z"); + // A row Graph gave an id nothing can carry is an error, left out of the page by the caller. + assert!(model::row(&graph_message(json!({"id": "a b"})), &roles()).is_err()); +} + +#[test] +fn nothing_a_stranger_writes_can_start_a_header_or_a_recipient() { + let named = |name: &str, address: &str| { + let party = json!({"emailAddress": {"name": name, "address": address}}); + let second = json!({"emailAddress": {"name": "Second", "address": "second@example.org"}}); + summary(json!({"from": party, "toRecipients": [party, second]})) + }; + for name in [ + "x\\\" , \\\"y", + "Evil\r\nBcc: x@y.z", + "Name ", + "=?UTF-8?B?QQ==?=", + "Ünï \u{2028} cøde", + "a, b", + ] { + let shown = named(name, "bob@example.org"); + let to = shown["to"].as_array().unwrap(); + assert_eq!(to.len(), 2, "{name:?}"); + assert_eq!( + (to[0]["email"].clone(), to[1]["email"].clone()), + (json!("bob@example.org"), json!("second@example.org")) + ); + assert!( + !to[0]["name"] + .as_str() + .unwrap() + .contains(['\r', '\n', '\u{2028}']) + ); + assert_eq!(shown["from"]["email"], "bob@example.org"); + } + // An address that is not a plain addr-spec is never written as one. + for address in [ + "a@b@c", + "a b@c.d", + "x,y@example.org", + "\"q\"@example.org", + "(c)@example.org", + "/O=ORG/CN=BOB", + "a@example.org>, ', ',']), + "{address:?}" + ); + assert_ne!(to[0]["email"], "evil@example.org"); + } + // A subject holding an encoded word is shown as typed, not decoded into what it spells. + let shown = summary(json!({"subject": "=?UTF-8?B?SGVsbG8=?= \r\nBcc: x@y.z"})); + assert_eq!(shown["subject"], "=?UTF-8?B?SGVsbG8=?= Bcc: x@y.z"); + let escape = summary(json!({"bodyPreview": " "})); + assert!(!escape["snippet"].as_str().unwrap().contains('\u{1b}')); + let long = summary(json!({"subject": "s".repeat(5000), "bodyPreview": "p".repeat(5000)})); + assert!( + long["subject"].as_str().unwrap().len() <= 998 + && long["snippet"].as_str().unwrap().len() <= 200 + ); +} + +#[test] +fn only_a_graph_messages_listing_may_be_followed() { + let good = + "https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages?$top=25&$skiptoken=abc"; + assert!(next_link(good).is_some()); + assert!( + next_link("https://graph.microsoft.com/v1.0/me/messages?$search=%22x%22&$skiptoken=1") + .is_some() + ); + let g = "https://graph.microsoft.com"; + let bad = format!( + "http://graph.microsoft.com/v1.0/me/messages|{g}.evil.test/v1.0/me/messages|https://graph.microsoft.com@evil.test/v1.0/me/messages|https://user:pw@graph.microsoft.com/v1.0/me/messages|{g}:8443/v1.0/me/messages|https://evil.test/v1.0/me/messages|{g}/v1.0/users/x/messages|{g}/v1.0/me/sendMail|{g}/v1.0/me/../users/x/messages|{g}/v1.0/me/a%2fb/messages|{g}/v1.0/me/a%5cb/messages|{g}/v1.0/me/messages#fragment|{g}/v1.0/me/messages\\x|{g}/v1.0/me/messages x|/v1.0/me/messages|{good}&{}", + "a".repeat(9000) + ); + for bad in bad.split('|').chain([""]) { + assert!(next_link(bad).is_none(), "{bad:.60}"); + } + for segments in [vec![""], vec![".."], vec!["me", "."]] { + assert!(url(&segments, &[]).is_err()); + } + // A query value is encoded so that it cannot add a parameter. + let url = url(&["me", "messages"], &[("$search", "a&b=c d")]).unwrap(); + assert_eq!(url.query(), Some("$search=a%26b%3Dc%20d")); +} + +#[test] +fn a_page_token_continues_only_its_own_query_and_only_toward_graph() { + let query = model::parse("role:inbox unseen").unwrap(); + let link = reqwest::Url::parse( + "https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages?$skiptoken=1", + ) + .unwrap(); + let token = seal(&query, &link, 42, 25).unwrap(); + let cursor = unseal(&token, &query).unwrap(); + assert_eq!( + (cursor.link, cursor.estimate, cursor.size), + (link.clone(), 42, 25) + ); + let canonical = query.canonical(); + let forged = |link: &str| b64(json!({"q": canonical, "e": 1, "l": 25, "n": link})); + for bad in [ + token.clone(), + "not base64!".into(), + b64(json!("text")), + b64(json!({"q": canonical, "e": "x", "l": 25, "n": link.as_str()})), + b64(json!({"q": canonical, "e": 1, "l": 25})), + b64(json!({"q": canonical, "e": u64::MAX, "l": 25, "n": link.as_str()})), + b64(json!({"q": canonical, "e": 1, "n": link.as_str()})), + b64(json!({"q": canonical, "e": 1, "l": 101, "n": link.as_str()})), + forged("https://evil.test/v1.0/me/messages"), + forged("http://graph.microsoft.com/v1.0/me/messages"), + "a".repeat(20000), + ] { + let other = if bad == token { + model::parse("role:inbox flagged").unwrap() + } else { + query.clone() + }; + assert_eq!(unseal(&bad, &other).map(|_| ()), Err("invalid_params")); + } +} + +#[tokio::test] +async fn requests_name_exactly_their_fields_and_are_refused_before_any_account_is_read() { + let (id, a) = (oid(1), "m365:a@example.org"); + let list = |extra: &str| format!(r#"{{"query":"role:inbox","limit":5{extra}}}"#); + let ids = |extra: &str| format!(r#"{{"ids":["{id}"]{extra}}}"#); + for (method, params) in [ + ( + "m365.list", + r#"{"query":"role:nowhere","limit":5}"#.to_owned(), + ), + ("m365.list", r#"{"query":"role:inbox","limit":101}"#.into()), + ("m365.list", r#"{"query":"role:inbox","limit":1.5}"#.into()), + ("m365.list", list(r#","extra":true"#)), + ("m365.list", list(r#","pageToken":"forged""#)), + ("m365.list", list(r#","requestToken":"""#)), + ("m365.messages", r#"{"ids":["BAD"],"full":false}"#.into()), + ("m365.messages", r#"{"ids":[],"full":false}"#.into()), + ("m365.messages", ids(r#","full":"yes""#)), + ("m365.read", format!(r#"{{"id":"{id}","full":false}}"#)), + ("m365.modify", ids("")), + ( + "m365.modify", + ids(r#","addLabelIds":["UNREAD"],"removeLabelIds":["UNREAD"]"#), + ), + ("m365.trash", r#"{"ids":[]}"#.into()), + ("m365.send", r#"{"raw":"not base64url!"}"#.into()), + ( + "m365.signInBegin", + format!(r#"{{"address":"b@example.org","clientId":"{CLIENT}"}}"#), + ), + ("m365.signInBegin", r#"{"address":"a@example.org"}"#.into()), + // There is no tenant to name: the address decides. + ( + "m365.signInBegin", + format!( + r#"{{"address":"a@example.org","clientId":"{CLIENT}","tenant":"organizations"}}"# + ), + ), + ("m365.signOut", r#"{"clientId":"forged"}"#.into()), + ("m365.cancel", "{}".into()), + ] { + let mut params: Value = serde_json::from_str(¶ms).unwrap(); + params["accountId"] = json!(a); + assert_eq!( + call(method, ¶ms).await, + Err("invalid_params"), + "{method} {params}" + ); + } + let foreign = json!({"accountId": "imap:a@example.org", "query": "role:inbox", "limit": 5}); + assert_eq!(call("m365.list", &foreign).await, Err("invalid_params")); + assert_eq!( + call("m365.signInPoll", &json!({"requestId": "short"})).await, + Err("invalid_params") + ); + for method in ["m365.check", "m365.token", "m365.signInCancel", "imap.list"] { + assert_eq!(call(method, &json!({})).await, Err("method_not_found")); + } +} + +#[test] +fn a_send_body_is_exactly_canonical_base64url_within_the_ceiling() { + let raw = URL_SAFE_NO_PAD.encode(b"To: a@example.org\r\n\r\nx"); + assert_eq!(send::decode(&raw).unwrap(), b"To: a@example.org\r\n\r\nx"); + let ceiling = crate::message::MAX_MESSAGE; + for (bad, expected) in [ + (format!("{raw}="), "invalid_params"), + (format!(" {raw}"), "invalid_params"), + (String::new(), "invalid_params"), + ("AB".into(), "invalid_params"), + ( + URL_SAFE_NO_PAD.encode(vec![b'x'; ceiling + 1]), + "m365_message_too_large", + ), + ( + "A".repeat(ceiling.div_ceil(3) * 4 + 1), + "m365_message_too_large", + ), + ] { + assert_eq!(send::decode(&bad), Err(expected), "{:.20}", bad); + } + assert!(send::decode(&URL_SAFE_NO_PAD.encode(vec![b'x'; ceiling])).is_ok()); +} + +#[test] +fn a_failed_send_proves_only_what_the_status_proves() { + for (status, sent, code) in [ + (0, false, "m365_send_not_attempted"), + (0, true, "m365_send_failed"), + (401, true, "m365_send_not_attempted"), + (429, true, "m365_send_not_attempted"), + (400, true, "m365_send_rejected"), + (413, true, "m365_message_too_large"), + (500, true, "m365_send_failed"), + (302, true, "m365_send_failed"), + ] { + assert_eq!( + send::classify(Fault { + code: "x", + status, + sent + }), + code, + "{status}" + ); + } + // Refused for its size by Exchange's own code: as definite whatever status carried it. + let sized = Fault { + code: "m365_message_too_large", + status: 400, + sent: true, + }; + assert_eq!(send::classify(sized), "m365_message_too_large"); +} + +#[test] +fn an_upload_has_longer_the_larger_it_is_and_never_longer_than_its_caller_waits() { + let graph = super::super::graph::Graph::new(ACCOUNT); + let (second, twenty) = (Duration::from_secs(1), Duration::from_secs(20)); + let quarter = 256 * 1024; + assert_eq!(graph.upload_clock(0, 24 * second), twenty); + assert_eq!(graph.upload_clock(quarter - 1, 24 * second), twenty); + assert_eq!(graph.upload_clock(4 * quarter, 50 * second), 24 * second); + assert_eq!(graph.upload_clock(4 * quarter, 22 * second), 22 * second); + assert_eq!( + graph.upload_clock(16 * 1024 * 1024 * 4 / 3, 49 * second), + 49 * second + ); +} + +#[test] +fn sign_in_accepts_only_microsofts_pages_and_names_only_what_the_id_token_says() { + assert!(signin::verification_uri("https://microsoft.com/devicelogin").is_some()); + assert!( + signin::verification_uri("https://login.microsoftonline.com/common/oauth2/deviceauth") + .is_some() + ); + let bad = "http://microsoft.com/devicelogin|https://microsoft.com.evil.test/devicelogin|https://evil.test/devicelogin|https://user@microsoft.com/devicelogin|https://microsoft.com:444/devicelogin|https://microsoft.com/devicelogin#x|https://microsoft.com/device login|javascript:alert(1)"; + for bad in bad.split('|') { + assert!(signin::verification_uri(bad).is_none(), "{bad}"); + } + let token = |claims: Value| format!("e30.{}.sig", b64(claims)); + assert_eq!( + signin::claim_names(&token(json!({"preferred_username": " Alice@Example.org "}))), + ["alice@example.org"] + ); + // All three, in the order the browser's own sign-in name comes first; empty ones are left out. + assert_eq!( + signin::claim_names(&token( + json!({"upn": "c@example.org", "email": "b@example.org", "preferred_username": "a@example.org"}) + )), + ["a@example.org", "b@example.org", "c@example.org"] + ); + assert_eq!( + signin::claim_names(&token( + json!({"preferred_username": " ", "email": "b@example.org"}) + )), + ["b@example.org"] + ); + for none in ["", "garbage", "a.b.c", "e30.e30.sig"] { + assert!(signin::claim_names(none).is_empty(), "{none}"); + } + // Microsoft's own numbers say consent and tenant problems; its words are never copied. + for (status, body, code) in [ + ( + 400, + json!({"error_codes": [65001]}), + "m365_consent_required", + ), + ( + 400, + json!({"error_description": "AADSTS90094: needs admin"}), + "m365_consent_required", + ), + ( + 400, + json!({"error": "invalid_tenant"}), + "m365_tenant_invalid", + ), + (400, json!({"error_codes": [90002]}), "m365_tenant_invalid"), + (400, json!({"error": "access_denied"}), "m365_access_denied"), + ( + 400, + json!({"error": "invalid_client", "error_description": "secret words"}), + "m365_auth_failed", + ), + (429, json!({}), "m365_throttled"), + (503, json!({}), "m365_server_error"), + ] { + assert_eq!(signin::flow_error(status, &body), code, "{body}"); + } +} + +#[test] +fn four_requests_at_a_time_per_mailbox_and_not_for_the_whole_process() { + let slots = super::super::graph::slots; + let a = slots("m365:slots-a@example.org"); + let held: Vec<_> = (0..4) + .map(|_| std::sync::Arc::clone(&a).try_acquire_owned().unwrap()) + .collect(); + assert!(std::sync::Arc::clone(&a).try_acquire_owned().is_err()); + assert!(slots("m365:slots-a@example.org").try_acquire().is_err()); + assert!(slots("m365:slots-b@example.org").try_acquire().is_ok()); + drop(held); +} diff --git a/src/providers/m365/tests/wire.rs b/src/providers/m365/tests/wire.rs new file mode 100644 index 0000000..93c17d2 --- /dev/null +++ b/src/providers/m365/tests/wire.rs @@ -0,0 +1,498 @@ +//! The wire: where credentials may go, how much a server may say, how long a call may take, what a +//! throttle costs, and cancelling. + +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; + +use super::{ACCOUNT, Fake, Resp, World, inbox, isolated}; + +async fn list(world: &World) -> Result { + world + .call("m365.list", json!({"query": "role:inbox", "limit": 5})) + .await +} + +fn listing(req: &super::Req) -> bool { + req.path.ends_with("/inbox/messages") +} + +#[tokio::test] +async fn credentials_go_to_graph_and_the_login_host_alone_and_never_follow_a_link_away() { + isolated!(); + let world = World::new().await; + inbox(&world, 2); + let elsewhere = Fake::start().await; + let away = elsewhere.base.clone(); + let away_base = away.clone(); + // A server that points the next page, or a redirect, anywhere else is a broken or hostile one. + for link in [ + format!("{away}/v1.0/me/messages"), + format!("{away}/v1.0/me/mailFolders/inbox/messages"), + "http://127.0.0.1:9/v1.0/me/mailFolders/inbox/messages".to_owned(), + "https://graph.microsoft.com/v1.0/users/x/messages".to_owned(), + "javascript:alert(1)".to_owned(), + ] { + let served = link.clone(); + world.fake.hook(move |req| { + listing(req).then(|| Resp::json(200, json!({"value": [], "@odata.nextLink": served}))) + }); + assert_eq!(list(&world).await, Err("m365_invalid_response"), "{link}"); + } + world.fake.hook(move |req| { + listing(req).then(|| Resp::status(302).with("Location", &format!("{away}/v1.0/me/stolen"))) + }); + assert_eq!(list(&world).await, Err("m365_bad_response")); + // The login host's redirect is no more followed than Graph's: the refresh token is not sent on. + let login_away = away_base.clone(); + world.fake.hook(move |req| { + req.path + .ends_with("/token") + .then(|| Resp::status(302).with("Location", &format!("{login_away}/stolen"))) + }); + world.call("m365.invalidate", json!({})).await.unwrap(); + assert_eq!(list(&world).await, Err("m365_invalid_response")); + assert!( + elsewhere.state().seen.is_empty(), + "the login redirect was followed" + ); + // The one place that attaches the token refuses a URL of another origin before any byte is sent. + let foreign = reqwest::Url::parse(&format!("{away_base}/v1.0/me/messages")).unwrap(); + let refused = super::super::graph::Graph::new(ACCOUNT) + .request( + super::super::graph::Mode::Read, + reqwest::Method::GET, + &foreign, + None, + 1024, + ) + .await; + assert_eq!( + refused.err().map(|fault| (fault.code, fault.sent)), + Some(("m365_invalid_request", false)) + ); + // A forged page token never produces a request at all. + world.fake.hook(|_| None); + let forged = base64::Engine::encode( + &base64::engine::general_purpose::URL_SAFE_NO_PAD, + json!({"q": "role:inbox", "e": 1, "n": format!("{}/v1.0/me/messages", elsewhere.base)}) + .to_string(), + ); + let before = world.fake.graph().len(); + let params = json!({"query": "role:inbox", "limit": 5, "pageToken": forged}); + assert_eq!(world.call("m365.list", params).await, Err("invalid_params")); + assert_eq!(world.fake.graph().len(), before); + assert!( + elsewhere.state().seen.is_empty(), + "something was sent to another host: {:?}", + elsewhere.state().seen + ); + // The bearer token is on every Graph request and on no login request; the refresh token and the + // client's secrets are on none of Graph's. + world.call("m365.invalidate", json!({})).await.unwrap(); + list(&world).await.unwrap(); + assert!( + world + .fake + .login() + .iter() + .all(|r| r.header("authorization").is_none()) + ); + for request in world.fake.graph() { + let everything = format!("{request:?}"); + assert!( + !everything.contains("refresh-") && !everything.contains("device-code"), + "{everything}" + ); + assert!( + request + .header("authorization") + .is_some_and(|value| value.starts_with("Bearer access-")), + "{everything}" + ); + } +} + +#[tokio::test] +async fn an_error_is_a_code_and_never_what_the_server_said_or_a_token() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + for (status, body, code) in [ + ( + 400, + json!({"error": {"code": "BadRequest", "message": "Bearer access-1 refresh-1"}}), + "m365_invalid_request", + ), + ( + 403, + json!({"error": {"code": "ErrorAccessDenied", "message": "access-1"}}), + "m365_access_denied", + ), + ( + 404, + json!({"error": {"code": "ErrorFolderNotFound", "message": "access-1"}}), + "m365_folder_unavailable", + ), + ( + 500, + json!({"error": {"code": "x", "message": "access-1"}}), + "m365_server_error", + ), + ( + 418, + json!({"error": "access-1 refresh-1"}), + "m365_invalid_request", + ), + (200, json!(["access-1"]), "m365_invalid_response"), + ] { + world + .fake + .hook(move |req| listing(req).then(|| Resp::json(status, body.clone()))); + let failure = list(&world).await.unwrap_err(); + assert_eq!(failure, code, "{status}"); + assert!(!failure.contains("access-") && !failure.contains("refresh-")); + } +} + +#[tokio::test] +async fn no_server_can_make_the_client_read_or_hold_more_than_the_bounds() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + let huge = 4 * 1024 * 1024 + 1; + // Declared too long, or no length and too long: refused, not read to the end. + world + .fake + .hook(move |req| listing(req).then(|| Resp::Lying(huge, vec![b' '; 100]))); + assert_eq!(list(&world).await, Err("m365_response_too_large")); + world + .fake + .hook(move |req| listing(req).then(|| Resp::Chunked(vec![vec![b' '; 1024 * 1024]; 5]))); + assert_eq!(list(&world).await, Err("m365_response_too_large")); + // The excess of an error answer is no error of its own: it is read only for its code. + world + .fake + .hook(move |req| listing(req).then(|| Resp::Send(503, vec![], vec![b'x'; 200_000]))); + assert_eq!(list(&world).await, Err("m365_server_error")); + world + .fake + .hook(move |req| listing(req).then(|| Resp::Lying(huge, vec![]))); + // More rows than were asked for, or rows of the wrong kind, are a broken server. + let row = json!({"id": super::gid(1), "subject": "x"}); + world.fake.hook(move |req| { + listing(req).then(|| Resp::json(200, json!({"value": vec![row.clone(); 6]}))) + }); + assert_eq!(list(&world).await, Err("m365_invalid_response")); + world + .fake + .hook(|req| listing(req).then(|| Resp::json(200, json!({"value": "none"})))); + assert_eq!(list(&world).await, Err("m365_invalid_response")); + // A row whose id cannot be carried is left out; the rest of the page stands. + let bad = json!({"id": "has space", "subject": "x"}); + let good = json!({"id": super::gid(2), "subject": "y"}); + world.fake.hook(move |req| { + listing(req).then(|| Resp::json(200, json!({"value": [bad.clone(), good.clone()]}))) + }); + let page = list(&world).await.unwrap(); + assert_eq!(page["page"]["ids"], json!([super::oid(2)])); +} + +#[tokio::test] +async fn a_change_graph_confirmed_is_done_however_long_its_answer() { + isolated!(); + let world = World::new().await; + inbox(&world, 2); + // Graph answers a change with the whole message, which is never read: the 2xx is the answer. + world + .fake + .hook(|req| (req.method == "PATCH").then(|| Resp::Lying(64 * 1024 * 1024, b"{}".to_vec()))); + let done = world + .call( + "m365.modify", + json!({"ids": [super::oid(1)], "removeLabelIds": ["UNREAD"]}), + ) + .await + .unwrap(); + assert_eq!(done["succeededIds"], json!([super::oid(1)])); + world.fake.hook(|req| { + req.path + .ends_with("/move") + .then(|| Resp::Chunked(vec![vec![b' '; 1024 * 1024]; 5])) + }); + let done = world + .call("m365.trash", json!({"ids": [super::oid(2)]})) + .await + .unwrap(); + assert_eq!(done["succeededIds"], json!([super::oid(2)])); +} + +#[tokio::test] +async fn every_message_of_a_full_read_has_a_clock_of_its_own() { + isolated!(); + let world = World::new().await; + inbox(&world, 3); + // Two bodies at a time, each slower than a request's clock: the third message's requests start + // after that clock has run out for the first two, and still have their own. + world.deadlines(400, 8000); + *super::super::graph::SHORT_MIME.lock().unwrap() = Some(Duration::from_secs(3)); + world.fake.hook(|req| { + req.path.ends_with("/$value").then(|| { + let body = super::plain_mime("Slow"); + Resp::Wait( + Duration::from_millis(600), + Box::new(Resp::Send(200, vec![], body)), + ) + }) + }); + let ids: Vec = (1..=3).map(super::oid).collect(); + let read = world + .call("m365.messages", json!({"ids": ids, "full": true})) + .await + .unwrap(); + assert_eq!(read["warning"], Value::Null); + assert_eq!(read["messages"].as_array().unwrap().len(), 3); +} + +#[tokio::test] +async fn a_throttled_read_waits_as_told_within_bounds_and_a_write_only_after_a_429() { + isolated!(); + let world = World::new().await; + inbox(&world, 2); + let count = |world: &World, method: &str| { + world + .fake + .graph() + .iter() + .filter(|r| r.method == method && r.path.contains("/me/messages")) + .count() + }; + let counted = |world: &World| world.fake.graph().iter().filter(|r| listing(r)).count(); + // Two 429s with a one second wait, then the answer: the wait is the server's, the rest is ours. + let left = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(2)); + let remaining = std::sync::Arc::clone(&left); + world.fake.hook(move |req| { + (listing(req) + && remaining + .fetch_update( + std::sync::atomic::Ordering::SeqCst, + std::sync::atomic::Ordering::SeqCst, + |n| n.checked_sub(1), + ) + .is_ok()) + .then(|| Resp::json(429, json!({})).with("Retry-After", "1")) + }); + let started = Instant::now(); + assert!(list(&world).await.is_ok()); + assert!( + started.elapsed() >= Duration::from_secs(2), + "{:?}", + started.elapsed() + ); + assert_eq!(counted(&world), 3); + // A wait longer than the bound is not waited out, and a throttle that goes on is given up on. + let before = counted(&world); + world + .fake + .hook(|req| listing(req).then(|| Resp::status(429).with("Retry-After", "6"))); + let started = Instant::now(); + assert_eq!(list(&world).await, Err("m365_throttled")); + assert!(started.elapsed() < Duration::from_secs(1) && counted(&world) == before + 1); + let before = counted(&world); + world + .fake + .hook(|req| listing(req).then(|| Resp::json(503, json!({})).with("Retry-After", "1"))); + assert_eq!(list(&world).await, Err("m365_throttled")); + assert_eq!( + counted(&world), + before + 3, + "a read is asked three times at most" + ); + // A write is repeated after a 429, which Microsoft sends instead of acting, and after nothing else. + let before = count(&world, "PATCH"); + let first = std::sync::atomic::AtomicBool::new(true); + world.fake.hook(move |req| { + (req.method == "PATCH" && first.swap(false, std::sync::atomic::Ordering::SeqCst)) + .then(|| Resp::status(429).with("Retry-After", "1")) + }); + let done = world + .call( + "m365.modify", + json!({"ids": [super::oid(1)], "removeLabelIds": ["UNREAD"]}), + ) + .await + .unwrap(); + assert_eq!(done["succeededIds"], json!([super::oid(1)])); + assert_eq!(count(&world, "PATCH"), before + 2); + let before = count(&world, "PATCH"); + world + .fake + .hook(|req| (req.method == "PATCH").then(|| Resp::status(503).with("Retry-After", "1"))); + let done = world + .call( + "m365.modify", + json!({"ids": [super::oid(2)], "removeLabelIds": ["UNREAD"]}), + ) + .await + .unwrap(); + assert_eq!( + (done["failedIds"].clone(), done["error"].clone()), + (json!([super::oid(2)]), json!("m365_throttled")) + ); + assert_eq!( + count(&world, "PATCH"), + before + 1, + "a write that may have been done was repeated" + ); +} + +#[tokio::test] +async fn a_message_body_outlives_the_listing_deadline() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + // The listing clock is 1.5 seconds. The body waits longer than that and is still accepted, + // because a 24 MiB message has a clock of its own. + world.deadlines(1500, 8000); + *super::super::graph::SHORT_MIME.lock().unwrap() = Some(Duration::from_secs(5)); + let mime = b"From: a@example.org\r\nTo: b@example.org\r\nSubject: Hi\r\n\nHello\r\n".to_vec(); + let size = mime.len(); + world.fake.hook(move |req| { + let body = mime.clone(); + req.path.ends_with("/$value").then(move || { + Resp::Wait( + Duration::from_millis(2500), + Box::new(Resp::Send(200, vec![], body)), + ) + }) + }); + let started = Instant::now(); + let read = world + .call("m365.read", json!({"id": super::oid(1), "full": true})) + .await + .unwrap(); + assert!(started.elapsed() >= Duration::from_millis(2500)); + assert!(started.elapsed() < Duration::from_secs(7)); + assert_eq!(read["id"], json!(super::oid(1))); + assert_eq!(read["sizeEstimate"], json!(size)); +} + +#[tokio::test] +async fn a_call_ends_at_its_deadline_and_a_call_given_up_on_is_cancelled_where_it_stands() { + isolated!(); + let world = World::new().await; + inbox(&world, 1); + // A server that never answers: the operation's own deadline ends the request. + world.deadlines(300, 5000); + world.fake.hook(|req| listing(req).then_some(Resp::Stall)); + let started = Instant::now(); + assert_eq!(list(&world).await, Err("m365_timeout")); + assert!(started.elapsed() < Duration::from_secs(3)); + // And the call's, which also covers what the operation does not (a keyring or token wait). + world.deadlines(5000, 300); + let started = Instant::now(); + assert_eq!(list(&world).await, Err("m365_timeout")); + assert!(started.elapsed() < Duration::from_secs(3)); + // A caller that gives up cancels by the token it made the call with. + world.deadlines(5000, 5000); + let account = ACCOUNT.to_owned(); + let asked = world.fake.graph().iter().filter(|r| listing(r)).count(); + let running = tokio::spawn(async move { + let params = + json!({"accountId": account, "query": "role:inbox", "limit": 5, "requestToken": "r1"}); + crate::providers::m365::call("m365.list", ¶ms).await + }); + for _ in 0..200 { + if world.fake.graph().iter().filter(|r| listing(r)).count() > asked { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + let again = world + .call( + "m365.list", + json!({"query": "role:inbox", "limit": 5, "requestToken": "r1"}), + ) + .await; + assert_eq!( + again, + Err("invalid_params"), + "a token in use is not taken twice" + ); + assert_eq!( + world + .call("m365.cancel", json!({"requestToken": "never-used"})) + .await + .unwrap(), + json!({"cancelled": false}) + ); + assert_eq!( + world + .call("m365.cancel", json!({"requestToken": "r1"})) + .await + .unwrap(), + json!({"cancelled": true}) + ); + assert_eq!(running.await.unwrap(), Err("m365_cancelled")); + // The token is free again, and the account unharmed. + world.fake.hook(|_| None); + let params = json!({"query": "role:inbox", "limit": 5, "requestToken": "r1"}); + assert!(world.call("m365.list", params).await.is_ok()); +} + +#[tokio::test] +async fn confirmed_mutations_do_not_wait_for_or_fail_on_an_unused_response_body() { + isolated!(); + let world = World::new().await; + inbox(&world, 2); + world.deadlines(200, 3000); + for stall in [false, true] { + world + .fake + .hook(move |req| (req.method == "PATCH").then(|| Resp::UnfinishedBody(200, stall))); + let done = world + .call( + "m365.modify", + json!({ + "ids": [super::oid(1)], "removeLabelIds": ["UNREAD"] + }), + ) + .await + .unwrap(); + assert_eq!( + done["succeededIds"], + json!([super::oid(1)]), + "confirmed PATCH, stall={stall}" + ); + assert_eq!(done["failedIds"], json!([])); + } +} + +#[tokio::test] +async fn an_accepted_send_does_not_become_unknown_when_the_unused_body_stalls() { + use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; + isolated!(); + let world = World::new().await; + world.deadlines(200, 3000); + for stall in [false, true] { + world.fake.hook(move |req| { + req.path + .ends_with("/sendMail") + .then(|| Resp::UnfinishedBody(202, stall)) + }); + let raw = URL_SAFE_NO_PAD.encode(super::plain_mime("Confirmed send")); + let sent = world.call("m365.send", json!({"raw":raw})).await; + assert!(sent.is_ok(), "confirmed 202, stall={stall}: {sent:?}"); + assert_eq!(sent.unwrap()["sent"], true); + } + assert_eq!( + world + .fake + .graph() + .iter() + .filter(|req| req.path.ends_with("/sendMail")) + .count(), + 2, + "each user send is sent only once" + ); +} diff --git a/src/providers/m365/verify.rs b/src/providers/m365/verify.rs new file mode 100644 index 0000000..0cb3886 --- /dev/null +++ b/src/providers/m365/verify.rs @@ -0,0 +1,82 @@ +//! `m365.verify` (is this mailbox signed in, and what does it look like?) and the background check +//! behind `mail.watch`. +//! +//! Verify refreshes the token from the keyring's refresh token, so it is the real sign-in check: a +//! refused grant, a withdrawn consent and a mailbox without Graph mail all show here. It reads the +//! folder table afresh and answers `{ok, email, displayName, roles, refusals}`: `roles` holds +//! `{total, unread}` per folder role (`null` when the mailbox has none; Archive often has not), +//! `refusals` the withdrawn capabilities (`send` without a Sent folder, `archive` without an Archive). +//! `displayName` is empty: the permissions asked for include no profile read. + +use serde_json::{Map, Value, json}; +use sha2::{Digest, Sha256}; + +use super::actions::with_archive_refusal; +use super::cancel::guarded; +use super::graph::Graph; +use super::model::{self, Role}; +use super::{ids, params}; + +const SEND_REFUSAL: &str = "No Sent folder was found in this mailbox"; + +pub(super) async fn verify(request: &Value) -> Result { + let map = params::object(request, &["accountId", "requestToken"])?; + let account = params::account(map)?; + let token = params::request_token(map)?; + guarded(account, token, async { + let roles = Graph::new(account).roles(true).await?; + let counts = |role: Role| { + roles.get(&role).map_or( + Value::Null, + |f| json!({"total": f.total, "unread": f.unread}), + ) + }; + let mut refusals = Map::new(); + if !roles.contains_key(&Role::Sent) { + refusals.insert("send".into(), json!(SEND_REFUSAL)); + } + let refusals = + with_archive_refusal(Value::Object(refusals), roles.contains_key(&Role::Archive)); + Ok(json!({ + "ok": true, + "email": account.strip_prefix("m365:").unwrap_or(account), + "displayName": "", + "roles": { + "inbox": counts(Role::Inbox), "sent": counts(Role::Sent), + "drafts": counts(Role::Drafts), "trash": counts(Role::Trash), + "junk": counts(Role::Junk), "archive": counts(Role::Archive), + }, + "refusals": refusals, + })) + }) + .await +} + +/// The check behind `mail.watch`: the Inbox's unread count, a sample of the newest unread rows and a +/// fingerprint of the Inbox (`total|unread|ids`) that changes when mail arrives, is read or moves. +pub(crate) async fn check(account: &str) -> Result { + let account = ids::account_id(account)?; + // Boxed: the sync layer awaits this inside its own, already large, future. + Box::pin(guarded(account, None, async { + let graph = Graph::new(account); + let inbox = graph + .folder(Role::Inbox) + .await? + .ok_or("m365_folder_unavailable")?; + let roles = model::Roles::from([(Role::Inbox, inbox.clone())]); + let unread = model::parse("role:inbox unseen")?; + let page = graph.page(&roles, &unread, 3, None).await?; + let digest = Sha256::digest(format!( + "{}|{}|{}", + inbox.total, + inbox.unread, + page.ids.join(",") + )); + Ok(json!({ + "estimate": page.estimate, + "fingerprint": digest.iter().map(|byte| format!("{byte:02x}")).collect::(), + "messages": page.rows, + })) + })) + .await +} diff --git a/src/providers/mod.rs b/src/providers/mod.rs index 9389286..1618237 100644 --- a/src/providers/mod.rs +++ b/src/providers/mod.rs @@ -13,6 +13,7 @@ pub mod hey_access; pub mod hey_actions; pub mod imap; pub mod jmap; +pub mod m365; pub mod outlook; const CAPABILITIES: &[&str] = &[ @@ -77,6 +78,13 @@ const PROVIDERS: &[Provider] = &[ auth: "oauth", capabilities: IMAP_CAPABILITIES, }, + Provider { + id: "m365", + name: "Microsoft 365", + summary: "Work or school Microsoft 365 mail and calendar over Microsoft Graph, with one sign-in code.", + auth: "oauth", + capabilities: &["archive", "star", "batch", "search", "send"], + }, Provider { id: "hey", name: "HEY", @@ -176,6 +184,14 @@ mod tests { assert!(can(provider, "move", &Value::Null)); } assert!(!can("jmap", "move", &Value::Null)); + // Microsoft 365 has Graph's well-known archive folder, but no labels, folders to move into, + // spam report or conversations. + for operation in ["archive", "star", "batch", "search", "send"] { + assert!(can("m365", operation, &Value::Null), "{operation}"); + } + for operation in ["move", "labels", "manageLabels", "threads", "spam", "web"] { + assert!(!can("m365", operation, &Value::Null), "{operation}"); + } assert!(!can("unknown", "send", &Value::Null)); assert!(!can("gmail", "unknown", &Value::Null)); assert!(can(" GMAIL ", "send", &Value::Null)); @@ -204,7 +220,7 @@ mod tests { .iter() .map(|p| p["id"].as_str().unwrap()) .collect::>(), - ["gmail", "outlook", "hey", "jmap", "imap"] + ["gmail", "outlook", "m365", "hey", "jmap", "imap"] ); for provider in providers { assert_eq!( @@ -219,6 +235,7 @@ mod tests { } } assert_eq!(providers[0]["capabilities"]["conversations"], false); - assert_eq!(providers[2]["capabilities"]["conversations"], true); + assert_eq!(providers[3]["capabilities"]["conversations"], true); + assert_eq!(providers[2]["name"], "Microsoft 365"); } } diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 33d51b9..260aef6 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -143,7 +143,8 @@ where } } -/// IMAP and Outlook idle on INBOX; every other provider only polls here. +/// IMAP and Outlook idle on INBOX; every other provider only polls here (Graph change notifications +/// need a public endpoint, so Microsoft 365 is poll-only too). fn provider_push(account: String, trigger: Arc) -> Option> { (account.starts_with("imap:") || account.starts_with("outlook:")).then(|| { Box::pin(crate::providers::imap::idle::watch(account, trigger)) as BoxFuture<'static, ()> @@ -164,6 +165,10 @@ impl Sync { if account.starts_with("jmap:") { return jmap.check(&account).await; } + // Without this arm a Microsoft 365 id would fall through to Gmail. + if account.starts_with("m365:") { + return crate::providers::m365::check(&account).await; + } if account.starts_with("imap:") || account.starts_with("outlook:") { let oauth = account.starts_with("outlook:"); let provider = if oauth { "outlook" } else { "imap" }; @@ -1011,7 +1016,12 @@ mod tests { for account in ["imap:a@example.org", "outlook:a@example.org"] { assert!(provider_push(account.into(), Arc::new(Notify::new())).is_some()); } - for account in ["a@example.org", "jmap:a@example.org", "hey:a@example.org"] { + for account in [ + "a@example.org", + "jmap:a@example.org", + "hey:a@example.org", + "m365:a@example.org", + ] { assert!(provider_push(account.into(), Arc::new(Notify::new())).is_none()); } } diff --git a/src/sync/preload.rs b/src/sync/preload.rs index 551aa6a..93a54c7 100644 --- a/src/sync/preload.rs +++ b/src/sync/preload.rs @@ -84,14 +84,25 @@ where F: FnOnce() -> Fut, Fut: std::future::Future>, { + // A size of 0 means the listing did not report one (Graph rows carry none), so the read + // could be arbitrarily large; an explicit reader may still open it. if size > MAX_AUTOMATIC_BYTES - || (size == 0 && (account.starts_with("imap:") || account.starts_with("outlook:"))) + || (size == 0 + && (account.starts_with("imap:") + || account.starts_with("outlook:") + || account.starts_with("m365:"))) { return Ok(None); } let resource = match read().await { Ok(resource) => resource, - Err("message_too_large" | "imap_response_too_large" | "gmail_response_too_large") => { + Err( + "message_too_large" + | "imap_response_too_large" + | "gmail_response_too_large" + | "m365_message_too_large" + | "m365_response_too_large", + ) => { return Ok(None); } Err(error) => return Err(error), @@ -120,6 +131,8 @@ fn inbox(account: &str) -> &'static str { "imap" } else if account.starts_with("outlook:") { "outlook" + } else if account.starts_with("m365:") { + "m365" } else { "gmail" }; @@ -153,6 +166,8 @@ impl Provider { crate::providers::hey::call(method, &checked).await } else if account.starts_with("imap:") || account.starts_with("outlook:") { crate::providers::imap::call(method, ¶ms).await + } else if account.starts_with("m365:") { + Box::pin(crate::providers::m365::call(method, ¶ms)).await } else { self.gmail.call(method, ¶ms).await } @@ -184,6 +199,23 @@ impl Provider { return Err("preload_incomplete"); } Ok(result["page"].clone()) + } else if account.starts_with("m365:") { + let result = self + .request( + account, + "m365.list", + json!({"accountId":account,"query":query,"limit":limit,"pageToken":""}), + live, + ) + .await?; + // The rows are the header-only resources of the page's ids: they travel on as held + // previews (the `messages` key) and the warm job reads nothing per message for them. + let mut page = result["page"].clone(); + if !page.is_object() { + return Err("preload_invalid_page"); + } + page["messages"] = result["rows"].clone(); + Ok(page) } else { self.request( account, @@ -231,6 +263,31 @@ impl Provider { .and_then(|m| m.first()) .cloned() .ok_or("preload_message_missing") + } else if account.starts_with("m365:") { + if full { + return self + .request( + account, + "m365.read", + json!({"accountId":account,"id":id,"full":true}), + live, + ) + .await; + } + // `m365.read` answers complete messages only; header-only rows come from `m365.messages`. + let result = self + .request( + account, + "m365.messages", + json!({"accountId":account,"ids":[id],"full":false}), + live, + ) + .await?; + result["messages"] + .as_array() + .and_then(|m| m.iter().find(|m| m["id"] == id)) + .cloned() + .ok_or("preload_message_missing") } else { self.request( account, diff --git a/tests/cli.rs b/tests/cli.rs index 71a3dc5..a845692 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -138,6 +138,10 @@ impl Drop for MailListFixture { } } +/// A well-formed Microsoft 365 message id (lower-case base32 of a Graph id). No such message exists +/// anywhere: these tests stop before any network. +const M365_ID: &str = "ifau222bi5etembqgayucqkbhu"; + fn mail_list_fixture(imap_port: u16, keyring_succeeds: bool) -> MailListFixture { let root = std::env::temp_dir().canonicalize().unwrap().join(format!( "omamail-cli-mail-list-{}-{}", @@ -162,7 +166,8 @@ fn mail_list_fixture(imap_port: u16, keyring_succeeds: bool) -> MailListFixture {"provider":"gmail","email":"gmail@example.org"}, {"provider":"imap","email":"imap@example.org","imap":{"username":"imap@example.org","imapHost":"127.0.0.1","imapPort":imap_port,"insecure":true,"testPlaintext":true}}, {"provider":"jmap","email":"jmap@example.org","jmap":{"sessionUrl":"https://localhost:9/session","username":"jmap@example.org"}}, - {"provider":"outlook","email":"outlook@example.org","clientId":"synthetic-client","imap":{"tenant":"consumers"}} + {"provider":"outlook","email":"outlook@example.org","clientId":"synthetic-client","imap":{"tenant":"consumers"}}, + {"provider":"m365","email":"m365@example.org","clientId":"00000000-1111-2222-3333-444444444444"} ] }) .to_string(), @@ -403,6 +408,12 @@ fn malformed_provider_action_previews_refuse_without_credentials_or_writes() { ("imap:imap@example.org", "0:INBOX"), ("outlook:outlook@example.org", "4294967296:INBOX"), ("outlook:outlook@example.org", "1:"), + // Microsoft 365 ids are canonical lower-case base32 of a Graph id of 8..=156 bytes. + ("m365:m365@example.org", "1:INBOX"), + ("m365:m365@example.org", "IFAU222BI5ETEMBQGAYUCQKBHU"), + ("m365:m365@example.org", "ifau222bi5etembqgayucqkbhu="), + ("m365:m365@example.org", "ifau222bi5etembqgayucqkbhv"), + ("m365:m365@example.org", "AAMkAGI20001AAA="), ] { for operation in ["read", "trash"] { let output = root_mail( @@ -522,6 +533,7 @@ fn configured_list_failures_do_not_repair_registry_metadata() { ("imap:imap@example.org", Some("auth_signed_out")), ("jmap:jmap@example.org", Some("auth_signed_out")), ("outlook:outlook@example.org", Some("auth_signed_out")), + ("m365:m365@example.org", Some("m365_signed_out")), ] { let output = call_mail_list(&fixture.0, account); assert!(!output.status.success(), "{account}: {output:?}"); @@ -567,6 +579,7 @@ fn configured_read_failures_do_not_repair_registry_metadata() { "7:INBOX", Some("auth_signed_out"), ), + ("m365:m365@example.org", M365_ID, Some("m365_signed_out")), ] { let output = call_mail_read(&fixture.0, account, id); assert!(!output.status.success(), "{account}: {output:?}"); @@ -590,7 +603,7 @@ fn configured_read_failures_do_not_repair_registry_metadata() { } #[test] -fn imap_and_outlook_destination_previews_require_readonly_credentials() { +fn imap_outlook_and_m365_destination_previews_require_readonly_credentials() { let fixture = mail_list_fixture(9, false); let directory = config_root(&fixture.0).join("omamail"); let registry = directory.join("accounts.json"); @@ -599,19 +612,20 @@ fn imap_and_outlook_destination_previews_require_readonly_credentials() { metadata(®istry), fs::read(®istry).unwrap(), ); - for account in ["imap:imap@example.org", "outlook:outlook@example.org"] { + for (account, id, expected) in [ + ("imap:imap@example.org", "7:INBOX", "auth_signed_out"), + ("outlook:outlook@example.org", "7:INBOX", "auth_signed_out"), + ("m365:m365@example.org", M365_ID, "m365_signed_out"), + ] { for operation in ["archive", "trash"] { let output = root_mail( &fixture.0, - &[operation, "7:INBOX", "--account", account, "--json"], + &[operation, id, "--account", account, "--json"], b"", ); assert_eq!(output.status.code(), Some(1)); let value: Value = serde_json::from_slice(&output.stdout).unwrap(); - assert_eq!( - value["error"]["code"], "auth_signed_out", - "{account}: {operation}" - ); + assert_eq!(value["error"]["code"], expected, "{account}: {operation}"); assert_eq!( ( metadata(&directory), @@ -696,6 +710,32 @@ fn jmap_planning_helpers_are_not_public_methods() { } } +#[test] +fn an_address_registered_under_another_provider_is_not_an_m365_account() { + let fixture = mail_list_fixture(9, true); + let touched = fixture.0.join("credential-touched"); + for account in [ + "m365:imap@example.org", + "m365:outlook@example.org", + "imap:m365@example.org", + "outlook:m365@example.org", + "jmap:m365@example.org", + "m365:m365@example.org.invalid", + ] { + let output = call_mail_list(&fixture.0, account); + assert_eq!(output.status.code(), Some(1), "{account}: {output:?}"); + assert_eq!( + serde_json::from_slice::(&output.stdout).unwrap()["error"]["code"], + "mail_account_unknown", + "{account}" + ); + } + assert!( + !touched.exists(), + "an unknown account must not reach the keyring" + ); +} + #[test] fn generic_call_errors_are_json_without_echoing_input() { for (method, input, code) in [ @@ -800,6 +840,7 @@ fn default_output_is_a_table_and_json_is_global() { assert!(providers.status.success()); let text = String::from_utf8(providers.stdout).unwrap(); assert!(text.contains("Gmail") && text.contains("|")); + assert!(text.contains("Microsoft 365"), "{text}"); } #[test] diff --git a/tests/test_backend_api.py b/tests/test_backend_api.py index 911e2d4..6781d69 100644 --- a/tests/test_backend_api.py +++ b/tests/test_backend_api.py @@ -206,7 +206,8 @@ def terminate_process_group(process, platform=None): await call('message.parse', {raw:17}, -32602); const providers = await call('providers.snapshot'); - for (const id of ['gmail','outlook','hey','jmap','imap']) { + // The pinned release has no m365 provider; its rules are unreleased. + for (const id of ['gmail','outlook','hey','jmap','imap'].concat(released ? [] : ['m365'])) { assert.ok(providers[id] && providers[id].queries && providers[id].capabilities); assert.equal(typeof providers[id].nativeSync, 'boolean'); } @@ -327,7 +328,9 @@ def main(): {'provider': 'imap', 'email': 'named@example.org', 'senderName': 'Jane Example', 'imap': {'username': 'named@example.org'}}, {'provider': 'hey', 'email': 'sender@example.org'}, - {'provider': 'outlook', 'email': 'sender@example.org'}] + {'provider': 'outlook', 'email': 'sender@example.org'}, + {'provider': 'm365', 'email': 'sender@example.org', + 'clientId': '00000000-1111-2222-3333-444444444444'}] registry.write_text(json.dumps({'version': 1, 'activeId': 'contract@example.org', 'accounts': accounts})) registry.chmod(0o600) diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py index 0b4e52b..6e3acdd 100644 --- a/tests/test_diagnostics.py +++ b/tests/test_diagnostics.py @@ -79,7 +79,7 @@ def test_static_backend_error_vocabulary_does_not_drift(self): stored = re.compile(r'\["error"\]\s*=\s*json!\("([a-z]+_[a-z_]+)"\)') codes = set() for path in (ROOT / 'src').rglob('*.rs'): - if path.name == 'tests.rs' or path.stem.endswith(('_tests', '_test')): + if path.name == 'tests.rs' or path.stem.endswith(('_tests', '_test')) or 'tests' in path.relative_to(ROOT).parts: continue source = path.read_text() for pattern in (produced, returned, stored): diff --git a/tests/test_source.sh b/tests/test_source.sh index 4e5a4d9..1790c24 100755 --- a/tests/test_source.sh +++ b/tests/test_source.sh @@ -1347,6 +1347,22 @@ if "selectionIsPreview" not in mark.group(0): "(`root.selectionIsPreview`), or stepping a list reads it") PREVIEWREAD +# The standalone host (`backend/Runtime.qml`) accepts its bundled backend's API +# and the one step past it. Below apiVersion-1, `make app-run` refuses the +# checkout's own backend as incompatible; above releasedApiVersion, a release +# bundle claims an API it does not carry. The release pin folds only +# backend-api.json, so the window, not equality, is what holds on main. +python3 - <<'BUNDLEDAPI' || exit 1 +import json, pathlib, re +contract = json.loads(pathlib.Path("../backend-api.json").read_text()) +api, released = contract["apiVersion"], contract["releasedApiVersion"] +found = re.search(r"bundledApiVersion: root\.standalone \? (\d+) : 0", pathlib.Path("Service.qml").read_text()) +bundled = int(found.group(1)) if found else None +if bundled is None or not api - 1 <= bundled <= released: + raise SystemExit(f"BUNDLEDAPI: ui/Service.qml bundledApiVersion {bundled} must be between apiVersion-1 ({api - 1}) " + f"and releasedApiVersion ({released}): the standalone host accepts its bundled API and one step past it") +BUNDLEDAPI + # The backend a QML file calls is the contract's, by name: a method that is # not declared cannot be called at all, and one the checkout has not shipped # yet is declared unreleased so `Backend` can refuse it on the pinned binary. @@ -1368,6 +1384,31 @@ if unknown: + ", ".join(m + " (" + ", ".join(sorted(called[m])) + ")" for m in unknown)) CONTRACTCALLS + +# Microsoft 365 signs in on Microsoft's own page and everything that could +# authenticate anybody stays in Rust: the device code, the refresh token in the +# keyring, the access token in memory. QML that named a token, assembled a header, +# read the keyring or spoke to the network would be a second place the rule lived. +python3 - <<'M365LITERALS' +from pathlib import Path +import re + +names = ("providers/M365Auth.qml", "providers/M365Client.qml", "providers/M365Protocol.js", + "providers/M365.js", "components/M365SetupPage.qml") +for name in names: + code = "" + for token in re.finditer(r'"(?:[^"\\\n]|\\.)*"|//[^\n]*|[^"/]+|/', Path(name).read_text()): + text = token.group(0) + if text.startswith("//"): + continue + if text.startswith('"') and re.search( + r"Authorization|Bearer |Basic |refresh_token|access_token|device_code|id_token|m365-refresh-token", text): + raise SystemExit("test_source.sh: Microsoft 365 holds no token in QML; " + name + " names one: " + text) + code += '""' if text.startswith('"') else text + if re.search(r"XMLHttpRequest|\bfetch\(|WebSocket|\bProcess\b|credentialGet|credentialPut|credentialDelete", code): + raise SystemExit("test_source.sh: " + name + " must not carry network, process or keyring code") +M365LITERALS + # Credential metadata and secret values cross one typed backend RPC. Provider # and calendar QML must never regain a platform command or keyring helper. if grep -E 'secret-tool|scripts/keyring-(lookup|store|clear)\.sh' \ diff --git a/ui/App.qml b/ui/App.qml index 28f4cb9..9d80688 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -1175,95 +1175,18 @@ Item { // before the service has finished making the row. } - // The setup pages. Built by the Loader above, one at a time, so the ones not - // in use hold no half-typed fields and no state to go stale. - Component { - id: providerPickerPage - - ProviderPicker { - textColor: root.foreground - dimColor: root.dim - accentColor: root.accent - panelFontFamily: root.fontFamily - // Back is shown wherever there is something to go back to — which on - // first run, where the chooser is the root, there is not. - onChosen: function(providerId) { root.chooseProvider(providerId) } - } - } - - Component { - id: gmailSetupPage - - SetupPage { - service: root.service - textColor: root.foreground - dimColor: root.dim - dangerColor: root.danger - accentColor: root.accent - panelFontFamily: root.fontFamily - accountCount: root.service ? root.service.accountCount : 1 - onRemoveRequested: root.removeCurrentAccountFromEditor() - } - } - - Component { - id: heySetupPage - - HeySetupPage { - service: root.service - textColor: root.foreground - dimColor: root.dim - dangerColor: root.danger - accentColor: root.accent - panelFontFamily: root.fontFamily - accountCount: root.service ? root.service.accountCount : 1 - onRemoveRequested: root.removeCurrentAccountFromEditor() - } - } - - Component { - id: outlookSetupPage - - OutlookSetupPage { - service: root.service - textColor: root.foreground - dimColor: root.dim - dangerColor: root.danger - accentColor: root.accent - panelFontFamily: root.fontFamily - accountCount: root.service ? root.service.accountCount : 1 - onRemoveRequested: root.removeCurrentAccountFromEditor() - } - } - - Component { - id: jmapSetupPage - - JmapSetupPage { - service: root.service - textColor: root.foreground - dimColor: root.dim - dangerColor: root.danger - accentColor: root.accent - panelFontFamily: root.fontFamily - accountCount: root.service ? root.service.accountCount : 1 - onRemoveRequested: root.removeCurrentAccountFromEditor() - } - } - - Component { - id: imapSetupPage - - ImapSetupPage { - service: root.service - textColor: root.foreground - dimColor: root.dim - dangerColor: root.danger - accentColor: root.accent - panelFontFamily: root.fontFamily - accountCount: root.service ? root.service.accountCount : 1 - onRemoveRequested: root.removeCurrentAccountFromEditor() - } + // The setup pages live in SetupPages, one map. A new mailbox is added + // there, so this file does not grow. + SetupPages { + id: setupPages + service: root.service + textColor: root.foreground + dimColor: root.dim + dangerColor: root.danger + accentColor: root.accent + panelFontFamily: root.fontFamily + onChosen: function(providerId) { root.chooseProvider(providerId) } + onRemoveRequested: root.removeCurrentAccountFromEditor() } // Every mailbox at once. The rail it lands on has to be one they all have, @@ -1344,12 +1267,26 @@ Item { if (!service) return var index = Accounts.confirmRemoval({ accounts: service.accountSummaries || [] }, request) if (index < 0) return - service.removeAccountAt(index) - // The editor's page is gone with its account. Settings is where the rest - // of them are, over a fresh root: the list underneath may have changed - // mailbox with the removal. - navUntouched = false - nav = Nav.push(Nav.resetTo(nav, rootKind()), Nav.entry("settings")) + // Keep the account until its saved sign-in is confirmed gone. The backend + // may be starting or the keyring locked; a destroyed delegate cannot retry. + var host = service.accountAt(index) + var entry = service.accountList.accounts[index] + var removalNav = nav + if (!host) return + host.forgetStoredCredentials(function(forgotten) { + if (!root || !root.service || !forgotten) return + var current = Accounts.confirmRemoval({ accounts: root.service.accountSummaries || [] }, request) + // An edit or model rebuild while cleanup was pending is a newer account. + if (current < 0 || root.service.accountAt(current) !== host + || root.service.accountList.accounts[current] !== entry) return + // A newer page or composer belongs to the user, not this delayed reply. + var stillOnRemovalPage = root.nav === removalNav + root.service.removeAccountAt(current) + if (stillOnRemovalPage) { + root.navUntouched = false + root.nav = Nav.push(Nav.resetTo(root.nav, root.rootKind()), Nav.entry("settings")) + } + }) } // Confirm the named event or series before deleting it. @@ -2243,7 +2180,7 @@ Item { // Setups that share nothing but their place on screen: a chooser for // a mailbox whose kind is not settled yet, then whichever page that // kind needs — a Cloud walkthrough, a program and a button, or a - // server and a password. A Loader rather than four visibilities, so + // server and a password. A Loader rather than a visibility each, so // the pages not in use hold no fields and no state. Loader { id: setup @@ -2255,12 +2192,7 @@ Item { // The provider is on the entry, so a service that briefly reports // Gmail while it rebuilds an account host cannot swap the page. readonly property string kind: root.editingProvider !== "" ? root.editingProvider : "gmail" - sourceComponent: root.showPicker - ? providerPickerPage - : (setup.kind === "imap" ? imapSetupPage - : (setup.kind === "jmap" ? jmapSetupPage - : (setup.kind === "outlook" ? outlookSetupPage - : (setup.kind === "hey" ? heySetupPage : gmailSetupPage)))) + sourceComponent: setupPages.pageFor(kind, root.showPicker) } } } diff --git a/ui/Service.qml b/ui/Service.qml index 0e446f0..4fdbe76 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -59,7 +59,7 @@ Item { pluginDir: root.pluginDir bundledExecutable: root.standalone ? String(root.platform.backendPath || "") : "" bundledVersion: root.standalone ? root.version : "" - bundledApiVersion: root.standalone ? 6 : 0 + bundledApiVersion: root.standalone ? 7 : 0 bundledMode: root.standalone developmentExecutable: root.standalone ? "" : (Quickshell.env("OMAMAIL_BIN") || "") onValidated: Qt.callLater(rustBackend.reconcileProcess) diff --git a/ui/account/Accounts.js b/ui/account/Accounts.js index 48c87e7..bcaaad6 100644 --- a/ui/account/Accounts.js +++ b/ui/account/Accounts.js @@ -58,7 +58,7 @@ function calendarProvider(raw) { var account = raw || {} var provider = trimmed(account.provider).toLowerCase() if (provider === "gmail" || provider === "") return "google" - if (provider === "outlook") return "microsoft" + if (provider === "outlook" || provider === "m365") return "microsoft" if (provider !== "imap") return "" var host = trimmed(account.imap && account.imap.imapHost).toLowerCase() var email = trimmed(account.email).toLowerCase() @@ -91,7 +91,7 @@ function accountId(email, provider) { // anything written before providers existed — is Gmail: that is what every // account in an upgraded install actually is, and defaulting to it is what // stops an upgrade from presenting a working mailbox as unconfigured. -var PROVIDERS = ["gmail", "outlook", "hey", "imap", "jmap"] +var PROVIDERS = ["gmail", "outlook", "m365", "hey", "imap", "jmap"] var DEFAULT_PROVIDER = "gmail" function normalizeProvider(value) { diff --git a/ui/account/MailAccount.qml b/ui/account/MailAccount.qml index 6a36b0e..232645c 100644 --- a/ui/account/MailAccount.qml +++ b/ui/account/MailAccount.qml @@ -2601,6 +2601,13 @@ Item { return auth.signIn(secret) } + // A removed mailbox's password must not wait in the keyring for one added + // again under the same address to find. + function forgetStoredCredentials(done) { + if (auth && typeof auth.forgetCredential === "function") auth.forgetCredential(done) + else if (typeof done === "function") done(true) + } + function signOut() { intents.clear() cancelActionCompletions() @@ -2704,7 +2711,8 @@ Item { sourceComponent: root.providerId === "imap" ? imapAuthComponent : (root.providerId === "jmap" ? jmapAuthComponent : (root.providerId === "outlook" ? outlookAuthComponent - : (root.providerId === "hey" ? heyAuthComponent : gmailAuthComponent))) + : (root.providerId === "m365" ? m365AuthComponent + : (root.providerId === "hey" ? heyAuthComponent : gmailAuthComponent)))) } // The client takes the manager as a required property, so it cannot be built @@ -2719,7 +2727,8 @@ Item { : (root.providerId === "imap" || root.providerId === "outlook" ? imapClientComponent : (root.providerId === "jmap" ? jmapClientComponent - : (root.providerId === "hey" ? heyClientComponent : gmailClientComponent))) + : (root.providerId === "m365" ? m365ClientComponent + : (root.providerId === "hey" ? heyClientComponent : gmailClientComponent)))) } Component { @@ -2838,6 +2847,25 @@ Item { } } + Component { + id: m365AuthComponent + + M365Auth { + backend: root.backend + platform: root.platform + accountId: root.accountId + address: root.configuredEmail + clientId: root.oauthClientId + + onLoginSucceeded: { + root.lastError = lastError + root.afterSignIn() + } + onLoggedOut: root.clearNotice() + onSessionUnavailable: function(reason) { root.fail(reason) } + } + } + Component { id: gmailClientComponent GmailApiClient { auth: authLoader.item; backend: root.backend } @@ -2857,6 +2885,11 @@ Item { } } + Component { + id: m365ClientComponent + M365Client { auth: authLoader.item; email: root.configuredEmail; backend: root.backend } + } + Component { id: jmapClientComponent JmapClient { diff --git a/ui/account/Model.js b/ui/account/Model.js index 11854ac..516a05e 100644 --- a/ui/account/Model.js +++ b/ui/account/Model.js @@ -1,6 +1,7 @@ .pragma library .import "Conversation.js" as Conversation +.import "../providers/Registry.js" as Registry // View models. Anything the panel decides — what the setup card should say, // whether a message still belongs in the list after an action, what the badge @@ -48,6 +49,7 @@ function providerName(provider) { function setupHeadline(state, provider, authKind) { var name = providerName(provider) + var microsoft = Registry.signsInWithMicrosoft(Registry.named(name)) if (state === "unavailable") return name + " integration is coming later" // A provider whose sign-in is a program of its own says which program: the // generic sentence sends somebody looking through Omarchy for a package this @@ -59,13 +61,13 @@ function setupHeadline(state, provider, authKind) { if (state === "no_credentials") { if (authKind === "password") return "Add this mailbox" if (authKind === "cli") return "Sign in to " + name - if (name === "Outlook") return "Add this Outlook mailbox" + if (microsoft) return "Add this " + name + " mailbox" return "Connect a Google Cloud project" } if (state === "signing_in") { if (authKind === "password") return "Checking the mailbox…" if (authKind === "cli") return "Waiting for " + name + "…" - if (name === "Outlook") return "Waiting for Microsoft…" + if (microsoft) return "Waiting for Microsoft…" return "Waiting for Google…" } if (state === "reconnecting") return "Reconnecting to " + name + "…" @@ -95,6 +97,8 @@ function setupDetail(state, missingTools, reason, provider, authKind) { return "The HEY CLI is installed. Signing in opens HEY in your browser; the token it comes back with is the CLI's own, and Omamail never sees it." if (name === "Outlook") return "Add the mailbox and a Microsoft public-client ID. Sign-in happens on Microsoft's page; Omamail never sees the account password." + if (Registry.signsInWithMicrosoft(Registry.named(name))) + return "Add the mailbox and the Application (client) ID of your Microsoft app. Sign-in happens on Microsoft's page; Omamail never sees the account password." return "Gmail has no shared app to sign in through, so this plugin uses an OAuth client you own. It takes about two minutes to create." } if (state === "signing_in") { diff --git a/ui/backend/Backend.qml b/ui/backend/Backend.qml index b902a27..12722b2 100644 --- a/ui/backend/Backend.qml +++ b/ui/backend/Backend.qml @@ -97,7 +97,8 @@ Item { // Snapshot the payload now, just as an immediately written request did. // The caller may reuse or mutate its model while this frame is waiting. var entry = { id: id, frame: Wire.request(id, method, params), - callback: done, operation: operation, cancelled: false } + callback: done, operation: operation, cancelled: false, + budget: requestBudget(operation, params) } queued.push(entry) drainRequests() var handle = { withdraw: function() { @@ -113,6 +114,24 @@ Item { return handle } + // How long a sent frame may wait for its answer. + function requestBudget(operation, params) { + if (operation === "agent.context") return 65000 + // An IMAP frame is a whole conversation — connect, login, SELECT, then + // one command per 4 096-UID window — on a server that charges about + // half a second per command, so it outlives the backend's own call + // budget (CALL_BUDGET in src/providers/imap/mod.rs) instead of killing + // the process while the server is still answering. + if (operation.indexOf("imap.") === 0) return 180000 + // A Microsoft 365 message body is up to 24 MiB and is given a minute on the wire. + // Everything else, including a header listing, keeps the 30 second wait. + var account = params ? String(params.accountId || "") : "" + if (operation === "m365.read" || operation === "m365.attachment") return 90000 + if (operation === "m365.messages" && params && params.full === true) return 90000 + if (operation === "reader.open" && account.indexOf("m365:") === 0) return 90000 + return 30000 + } + // Only sent frames occupy a slot or start their request deadline. New work // joins the tail even from a response callback, so a busy account cannot // repeatedly jump ahead of the other accounts already waiting. @@ -123,13 +142,7 @@ Item { while (queued.length > 0 && Object.keys(pending).length < 64 && connected) { var entry = queued.shift() var next = Object.assign({}, pending) - var timeout = entry.operation === "agent.context" ? 65000 : 30000 - // An IMAP frame is a whole conversation — connect, login, SELECT, then - // one command per 4 096-UID window — on a server that charges about - // half a second per command, so it outlives the backend's own call - // budget (CALL_BUDGET in src/providers/imap/mod.rs) instead of killing - // the process while the server is still answering. - if (entry.operation.indexOf("imap.") === 0) timeout = 180000 + var timeout = entry.budget || 30000 entry.deadline = Date.now() + timeout next[entry.id] = entry pending = next diff --git a/ui/calendar/Calendar.js b/ui/calendar/Calendar.js index 20a3f9c..b751178 100644 --- a/ui/calendar/Calendar.js +++ b/ui/calendar/Calendar.js @@ -550,10 +550,29 @@ function graphResponseError(status, responseText) { return detail !== "" ? "Microsoft Graph answered: " + detail : "Microsoft Graph answered " + status } +var M365_SIGN_IN_AGAIN = ["auth_signed_out", "calendar_auth_refused", "auth_client_missing", + "auth_client_invalid", "auth_refresh_failed", "auth_secret_invalid"] + +var M365_CONSENT = "Microsoft calendar request failed. An administrator has to approve this app for your " + + "organization. Then sign in to this mailbox again" + // Keep native backend diagnostics private while giving each provider an // actionable recovery path. This deliberately does not depend on backend // error-message semantics, so it remains compatible with the pinned API. -function nativeRequestError(kind) { +function nativeRequestError(kind, accountId, error) { + // A Microsoft 365 mailbox has one sign-in for mail and calendar: nothing to + // check in Settings, and no second permission to grant. A sign-in that is + // missing, refused, unreadable or made for an app ID that is gone is one to + // sign in again for, as on the mail side; this API knows its codes. Consent is + // an administrator's to give, in words of the calendar's own: the mail side's + // advice about the page a code is entered on is no use to a refresh. + if (kind === "microsoft" && String(accountId || "").indexOf("m365:") === 0) { + var code = error && typeof error === "object" ? String(error.message || "") : "" + if (code === "auth_consent_required") return M365_CONSENT + return M365_SIGN_IN_AGAIN.indexOf(code) >= 0 + ? "Microsoft calendar request failed. Sign in to this mailbox again" + : "Microsoft calendar request failed. Try again in a moment" + } if (kind === "microsoft") return "Microsoft calendar request failed. Check Graph permissions in Settings, then sign in again" if (kind === "google") diff --git a/ui/calendar/CalendarController.qml b/ui/calendar/CalendarController.qml index a0b38c1..297cd80 100644 --- a/ui/calendar/CalendarController.qml +++ b/ui/calendar/CalendarController.qml @@ -602,7 +602,7 @@ Item { if (error) { reason = notifyConsent(error) ? Microsoft.graphConsentMessage() : error.message === "calendar_conflict" ? "This event changed elsewhere. Refresh and try again." - : Calendar.nativeRequestError(String(source && source.kind || "")) + : Calendar.nativeRequestError(String(source && source.kind || ""), source && source.accountId, error) } callback(result, reason) }) diff --git a/ui/calendar/Sources.js b/ui/calendar/Sources.js index 7c5f2ef..ff2848f 100644 --- a/ui/calendar/Sources.js +++ b/ui/calendar/Sources.js @@ -1,6 +1,7 @@ .pragma library .import "Palette.js" as Palette +.import "../account/Accounts.js" as Accounts var VERSION = 1 var KINDS = ["caldav", "google", "microsoft", "icloud", "hey"] @@ -210,7 +211,7 @@ function withMicrosoftAccounts(list, accountSummaries) { var accounts = Array.isArray(accountSummaries) ? accountSummaries : [] for (var i = 0; i < accounts.length; i++) { var account = accounts[i] || {} - if (account.provider !== "outlook" || account.signedIn !== true) continue + if (Accounts.calendarProvider(account) !== "microsoft" || account.signedIn !== true) continue var accountId = trimmed(account.id || account.email) if (accountId === "") continue var saved = null diff --git a/ui/components/M365SetupPage.qml b/ui/components/M365SetupPage.qml new file mode 100644 index 0000000..31add6c --- /dev/null +++ b/ui/components/M365SetupPage.qml @@ -0,0 +1,402 @@ +import QtQuick +import qs.Commons +import qs.Ui +import "../providers/M365Protocol.js" as M365 +import "../account/Accounts.js" as Accounts + +// A work or school mailbox on Microsoft 365: an address, the Application (client) +// ID of an app registered once in Microsoft Entra, and one code. +// +// The sign-in happens on Microsoft's own page, so there is no password here. +// Pressing the button asks Microsoft for a code, opens its page and shows the +// code to enter there; the window then waits and says which step it is on. The +// browser may already be signed in to another Microsoft account, which Microsoft +// then offers: the code card says what to do about that. +Column { + id: root + + required property var service + required property color textColor + required property color dimColor + required property color dangerColor + required property color accentColor + required property string panelFontFamily + + signal removeRequested() + + readonly property var auth: service ? service.auth : null + readonly property bool signedIn: !!auth && auth.loggedIn === true + readonly property bool removing: !!auth && auth.removalBusy === true + readonly property bool busy: !!auth && (auth.loginBusy === true || removing) + readonly property string stage: auth && auth.signInStage !== undefined ? String(auth.signInStage) : "" + readonly property bool reconnecting: !signedIn && !busy && !!auth && auth.recoveringSession === true + // The same rule the auth object applies before it sends anything: an older + // backend has no Microsoft 365 methods, and the page says so up front, as it + // does while the backend is still starting. + readonly property string backendProblem: service ? M365.backendProblem(service.backend) : "" + readonly property bool backendTooOld: backendProblem !== "" && !!service && !!service.backend + && service.backend.ready === true + + spacing: Style.space(16) + + function validatedAddress() { + var address = addressField.text.trim() + if (Accounts.isValidEmail(address)) return address + errorText.text = address === "" ? "Add your work email address" : "That is not a full email address" + return "" + } + + // Saved as the backend keys the sign-in: a GUID in lower case. + function validatedClientId() { + var value = clientIdField.text.trim() + if (M365.clientId(value) !== "") return M365.clientId(value) + errorText.text = value === "" + ? "Add the Application (client) ID from Microsoft Entra" + : "That is not a Microsoft Application (client) ID" + return "" + } + + // The sign-in starts on the account the form describes: the address and the ID + // are written down first, so a mailbox whose sign-in fails keeps them. + function signIn() { + if (!service || busy || signedIn) return + var address = validatedAddress() + var clientId = address === "" ? "" : validatedClientId() + if (clientId === "") return + errorText.text = "" + service.configureCurrentAccountAndSignInOAuth({ provider: "m365", email: address, clientId: clientId }) + } + + function syncFromStore() { + errorText.text = auth ? String(auth.lastError || "") : "" + if (!service) return + addressField.text = String(service.accountAddress || "") + clientIdField.text = auth && auth.clientId ? String(auth.clientId) : "" + } + + function open(url) { + if (service && typeof service.openExternal === "function") service.openExternal(url) + } + + // The auth object is rebuilt when the entry is saved, so what the page shows + // is read again from whichever one is current. + onAuthChanged: syncFromStore() + Component.onCompleted: syncFromStore() + + Connections { + target: root.auth + ignoreUnknownSignals: true + function onLastErrorChanged() { + errorText.text = root.auth ? String(root.auth.lastError || "") : "" + } + // A session that is there is no failure to report any more. + function onLoggedInChanged() { + if (root.auth && root.auth.loggedIn === true) errorText.text = "" + } + } + + ProviderHero { + width: parent.width + providerId: "m365" + title: "Add a Microsoft 365 mailbox" + detail: "Your work or school mailbox, signed in with Microsoft, with your calendar. Folders of your own are not shown." + textColor: root.textColor + dimColor: root.dimColor + panelFontFamily: root.panelFontFamily + } + + Rectangle { + width: parent.width + visible: root.backendProblem !== "" && !root.signedIn + implicitHeight: missingText.implicitHeight + Style.space(20) + radius: Style.cornerRadius + color: Style.normalFillFor(root.textColor, root.accentColor) + border.width: 1 + border.color: Style.hoverBorderFor(root.textColor, root.accentColor) + + Text { + id: missingText + objectName: "m365-backend-note" + anchors.left: parent.left + anchors.right: parent.right + anchors.margins: Style.space(12) + anchors.verticalCenter: parent.verticalCenter + textFormat: Text.PlainText + text: root.backendTooOld ? "Update the mail backend first: this version cannot talk to Microsoft 365." + : "Waiting for the mail backend to start. Sign in is available once it has." + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + } + + Column { + width: parent.width + spacing: Style.space(10) + + TextField { + id: addressField + objectName: "m365-address-field" + width: parent.width + visible: !root.signedIn + readOnly: root.busy + foreground: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + placeholderText: "Work email address — you@example.org" + onAccepted: clientIdField.forceActiveFocus() + } + + TextField { + id: clientIdField + objectName: "m365-client-id-field" + width: parent.width + visible: !root.signedIn + readOnly: root.busy + foreground: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + placeholderText: "Microsoft Application (client) ID" + onAccepted: root.signIn() + } + + Text { + objectName: "m365-asks" + width: parent.width + visible: !root.signedIn && !root.busy + textFormat: Text.PlainText + text: "Omamail asks Microsoft to sign you in, and for this: read and write mail, send mail, read and write your calendar. That is one sign-in on Microsoft's own page: there is no second code, and Omamail never sees your password." + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + + Text { + id: errorText + objectName: "m365-error" + width: parent.width + visible: text !== "" + textFormat: Text.PlainText + text: "" + color: root.dangerColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + } + + Column { + width: parent.width + // Not while a code is out: the code is what the person needs to see. + visible: !root.signedIn && !root.busy + spacing: Style.space(6) + + Text { + width: parent.width + text: "One-time Microsoft app setup" + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + font.bold: true + } + + Repeater { + model: [ + "In Microsoft Entra, register an app for accounts in your organization's directory. It needs no redirect URI: the sign-in uses a code.", + "Under Authentication, set Allow public client flows to Yes.", + "Under API permissions, add the delegated Microsoft Graph permissions Mail.ReadWrite, Mail.Send, Calendars.ReadWrite, openid, profile, email and offline_access.", + "Copy the Application (client) ID from the app's Overview page into the field above." + ] + + Text { + required property string modelData + required property int index + + objectName: "m365-step" + width: parent.width + textFormat: Text.PlainText + text: (index + 1) + ". " + modelData + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + } + + Text { + objectName: "m365-admin-note" + width: parent.width + textFormat: Text.PlainText + text: "If your organization does not let people approve apps themselves, an administrator has to approve this one before the sign-in works." + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + + LinkLabel { + text: "Open Microsoft Entra app registrations..." + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + tooltipText: "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade" + onActivated: root.open(tooltipText) + } + } + + Text { + objectName: "m365-progress" + width: parent.width + visible: root.busy || root.reconnecting + textFormat: Text.PlainText + text: root.removing ? "Removing the saved Microsoft sign-in" + : root.reconnecting ? "Checking the saved Microsoft sign-in" + : root.stage === "checking" ? "Signed in on Microsoft's side. Checking the mailbox" + : root.stage === "waiting" ? "Waiting for you to finish on the Microsoft page" + : "Asking Microsoft for a code" + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + + // The code and where to enter it, for as long as it is wanted: once it has + // been entered the page says the mailbox is being checked instead. The page + // address is selectable, to be pasted into a private window. + Rectangle { + objectName: "m365-code-card" + width: parent.width + visible: root.busy && !!root.auth && root.auth.userCode !== "" + implicitHeight: deviceColumn.implicitHeight + Style.space(20) + radius: Style.cornerRadius + color: Style.normalFillFor(root.textColor, root.accentColor) + border.width: 1 + border.color: Style.hoverBorderFor(root.textColor, root.accentColor) + + Column { + id: deviceColumn + anchors.left: parent.left + anchors.right: parent.right + anchors.margins: Style.space(12) + anchors.verticalCenter: parent.verticalCenter + spacing: Style.space(8) + + Text { + width: parent.width + textFormat: Text.PlainText + text: "Enter this code on the Microsoft page opened in your browser, then sign in with your work account. If it offers another account, paste the page address below into a private window:" + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + + TextField { + objectName: "m365-code" + width: parent.width + readOnly: true + text: root.auth ? String(root.auth.userCode) : "" + foreground: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.body + } + + TextField { + objectName: "m365-link" + width: parent.width + readOnly: true + text: root.auth ? String(root.auth.verificationUri) : "" + foreground: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + } + + LinkLabel { + objectName: "m365-open-again" + text: "Open the Microsoft sign-in page again..." + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + onActivated: if (root.auth) root.open(String(root.auth.verificationUri)) + } + } + } + + Column { + width: parent.width + visible: root.signedIn + spacing: Style.space(4) + + Text { + objectName: "m365-signed-in" + width: parent.width + textFormat: Text.PlainText + text: "Signed in · " + (root.service ? String(root.service.accountAddress || "") : "") + color: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + font.bold: true + elide: Text.ElideRight + } + + Text { + width: parent.width + textFormat: Text.PlainText + text: "Mail and calendar use this one sign-in. Signing out removes it from this computer." + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + wrapMode: Text.WordWrap + } + } + + Row { + spacing: Style.space(8) + + Button { + objectName: "m365-sign-in" + visible: !root.signedIn + text: root.removing ? "Removing account" : root.busy ? "Waiting for Microsoft" : "Sign in with Microsoft..." + enabled: !root.busy && root.backendProblem === "" && addressField.text.trim() !== "" + && clientIdField.text.trim() !== "" + foreground: root.textColor + bordered: true + fontSize: Style.font.bodySmall + onClicked: root.signIn() + } + + Button { + objectName: "m365-cancel-sign-in" + visible: root.busy && !root.removing + text: "Cancel" + foreground: root.dimColor + bordered: false + fontSize: Style.font.bodySmall + onClicked: if (root.service) root.service.cancelSignIn() + } + + Button { + objectName: "m365-sign-out" + visible: root.signedIn + text: "Sign out" + foreground: root.textColor + bordered: true + fontSize: Style.font.bodySmall + onClicked: if (root.service) root.service.signOut() + } + + // The window asks first; the saved sign-in goes once the removal is confirmed. + Button { + objectName: "m365-remove" + visible: !!root.service && root.service.accountCount > 1 + text: "Remove account" + enabled: !root.removing + foreground: root.dangerColor + bordered: false + fontSize: Style.font.bodySmall + onClicked: root.removeRequested() + } + } +} diff --git a/ui/components/SetupPages.qml b/ui/components/SetupPages.qml new file mode 100644 index 0000000..ed9d741 --- /dev/null +++ b/ui/components/SetupPages.qml @@ -0,0 +1,139 @@ +import QtQuick + +// Which setup page a mailbox kind opens: an entry in the map and its page +// below. The window composes this one object rather than a page per provider, +// so a new kind is added here and the window file does not grow. An Item so +// the pages can be declared as plain children; it draws nothing. +Item { + id: root + width: 0 + height: 0 + visible: false + + required property var service + required property color textColor + required property color dimColor + required property color dangerColor + required property color accentColor + required property string panelFontFamily + + signal chosen(string providerId) + signal removeRequested() + + // Gmail is the default for a kind this map does not name. + readonly property var pages: ({ + imap: imapSetupPage, + jmap: jmapSetupPage, + outlook: outlookSetupPage, + m365: m365SetupPage, + hey: heySetupPage + }) + + function pageFor(kind, picker) { + if (picker) return providerPickerPage + return pages.hasOwnProperty(kind) ? pages[kind] : gmailSetupPage + } + + Component { + id: providerPickerPage + + ProviderPicker { + textColor: root.textColor + dimColor: root.dimColor + accentColor: root.accentColor + panelFontFamily: root.panelFontFamily + // Back is shown wherever there is something to go back to — which on + // first run, where the chooser is the root, there is not. + onChosen: function(providerId) { root.chosen(providerId) } + } + } + + Component { + id: gmailSetupPage + + SetupPage { + service: root.service + textColor: root.textColor + dimColor: root.dimColor + dangerColor: root.dangerColor + accentColor: root.accentColor + panelFontFamily: root.panelFontFamily + accountCount: root.service ? root.service.accountCount : 1 + onRemoveRequested: root.removeRequested() + } + } + + Component { + id: heySetupPage + + HeySetupPage { + service: root.service + textColor: root.textColor + dimColor: root.dimColor + dangerColor: root.dangerColor + accentColor: root.accentColor + panelFontFamily: root.panelFontFamily + accountCount: root.service ? root.service.accountCount : 1 + onRemoveRequested: root.removeRequested() + } + } + + Component { + id: outlookSetupPage + + OutlookSetupPage { + service: root.service + textColor: root.textColor + dimColor: root.dimColor + dangerColor: root.dangerColor + accentColor: root.accentColor + panelFontFamily: root.panelFontFamily + accountCount: root.service ? root.service.accountCount : 1 + onRemoveRequested: root.removeRequested() + } + } + + Component { + id: m365SetupPage + + M365SetupPage { + service: root.service + textColor: root.textColor + dimColor: root.dimColor + dangerColor: root.dangerColor + accentColor: root.accentColor + panelFontFamily: root.panelFontFamily + onRemoveRequested: root.removeRequested() + } + } + + Component { + id: jmapSetupPage + + JmapSetupPage { + service: root.service + textColor: root.textColor + dimColor: root.dimColor + dangerColor: root.dangerColor + accentColor: root.accentColor + panelFontFamily: root.panelFontFamily + accountCount: root.service ? root.service.accountCount : 1 + onRemoveRequested: root.removeRequested() + } + } + + Component { + id: imapSetupPage + + ImapSetupPage { + service: root.service + textColor: root.textColor + dimColor: root.dimColor + dangerColor: root.dangerColor + accentColor: root.accentColor + panelFontFamily: root.panelFontFamily + accountCount: root.service ? root.service.accountCount : 1 + onRemoveRequested: root.removeRequested() + } + } +} diff --git a/ui/providers/M365.js b/ui/providers/M365.js new file mode 100644 index 0000000..bb9fe59 --- /dev/null +++ b/ui/providers/M365.js @@ -0,0 +1,62 @@ +.pragma library + +// Provider presentation only. NativeDomain.js supplies all mail capabilities +// and mailbox queries; providers.resolve builds dynamic queries in Rust. + +var ID = "m365" + +var NAME = "Microsoft 365" + +// Beside Outlook's, which reads a work mailbox over IMAP: this one needs no IMAP. +var SUMMARY = "Work or school mail and calendar, one sign-in, no IMAP needed." + +var AUTH = "oauth" + +// Outlook is the mail app of a Microsoft 365 mailbox; the name says which service. +var MARK = "outlook.svg" + +var MAILBOXES = [ + { + "key": "inbox", + "label": "Inbox", + "icon": "inbox" + }, + { + "key": "unread", + "label": "Unread", + "icon": "unread" + }, + { + "key": "starred", + "label": "Flagged", + "icon": "star" + }, + { + "key": "drafts", + "label": "Drafts", + "icon": "compose" + }, + { + "key": "sent", + "label": "Sent", + "icon": "sent" + }, + { + "key": "archive", + "label": "Archive", + "icon": "archive", + "optional": true + }, + { + "key": "spam", + "label": "Junk", + "icon": "spam", + "optional": true + }, + { + "key": "trash", + "label": "Trash", + "icon": "trash", + "optional": true + } +] diff --git a/ui/providers/M365Auth.qml b/ui/providers/M365Auth.qml new file mode 100644 index 0000000..ee6f9cb --- /dev/null +++ b/ui/providers/M365Auth.qml @@ -0,0 +1,420 @@ +import QtQuick + +import "M365Protocol.js" as M365 + +// A Microsoft 365 mailbox's sign-in: one code, entered on Microsoft's own page. +// +// The same shape from outside as `OutlookAuth`: `MailAccount` asks whether it is +// `loggedIn`, starts a sign-in with `beginLogin` and hears of the result by +// signal. Everything behind that is Rust's: it asks Microsoft for the code, keeps +// the refresh token in the keyring and the access token in memory. Nothing that +// could authenticate anybody reaches this object. +// +// 1. `m365.signInBegin` answers with a code and Microsoft's page, which is +// opened and the code shown to be entered there. +// 2. `m365.signInPoll` is asked every `interval` seconds until the person has +// finished (`signedIn`), refused (`declined`), run out of time (`expired`) +// or Microsoft refused (`failed`). +// 3. `m365.verify` proves the mailbox before the window shows it as signed in, +// and the folders it found teach the rail what it draws. A saved session is +// restored by the same check, with nothing typed. +Item { + id: root + + visible: false + width: 0 + height: 0 + + property var backend: null + property var platform: null + property string accountId: "" + property string address: "" + property string clientId: "" + + readonly property string effectiveClientId: M365.clientId(clientId) + readonly property bool configured: accountId !== "" && effectiveClientId !== "" + readonly property bool credentialsPresent: configured + + property bool loggedIn: false + property bool loginBusy: false + property bool removalBusy: false + // "", "starting", "waiting" or "checking": what the page says it is doing. + property string signInStage: "" + // A restore is owed from the start until it ends: the mailbox then does not + // look signed out for the second the backend takes to come up. + property bool restoreOwed: true + property bool restoring: false + readonly property bool recoveringSession: !loggedIn && !loginBusy && configured && restoreOwed + readonly property bool sessionBusy: restoring + property string lastError: "" + property string userCode: "" + property string verificationUri: "" + // The last passing check's answer, which the client draws the rail from. + property var checked: null + + // Wall-clock steps are scaled by this, which is only ever not 1 in a test. + property real timeScale: 1 + // Bumped whenever what is in the air stops being wanted: a cancel, a sign-out, + // another mailbox. An answer that comes back for an older one is dropped. + property int generation: 0 + property string requestId: "" + property int retryAttempt: 0 + // Sign-outs asked for before the backend was up: `{accountId, clientId}`. + property var owedSignOuts: [] + // Every sign-in this object asked the backend for, `{accountId, clientId}`. One of + // an ID typed before may still be exchanging its code there, and a sign-out ends + // only the sign-ins of the ID it names, so signing out names each of them. + property var signInClients: [] + // The backend may hold a sign-in of this mailbox the window has not seen: a + // poll's answer was lost, or came late while a newer sign-in was under way. A + // sign-in that ends without a mailbox removes it. + property bool grantUnknown: false + + // Read by `MailAccount` for every provider: nothing here needs a tool. + readonly property bool toolsChecked: true + readonly property bool toolsPresent: true + readonly property var missingTools: [] + + signal loginSucceeded() + signal loggedOut() + signal sessionUnavailable(string reason) + + function clearCode() { + pollTimer.stop() + requestId = "" + userCode = "" + verificationUri = "" + signInStage = "" + } + + // The mailbox check, which proves a session and says what the mailbox has. + // `done(result, code)`, with a code only when it did not. + function check(done) { + var gen = generation + backend.call("m365.verify", { accountId: accountId }, function(result, failure) { + if (!root || gen !== root.generation) return + done(result, failure ? (M365.errorCode(failure) || "m365_invalid_response") + : (M365.verified(result) ? "" : "m365_invalid_response")) + }) + } + + function accept(result) { + clearCode() + checked = result + loggedIn = true + loginBusy = false + restoring = false + restoreOwed = false + retryAttempt = 0 + retryTimer.stop() + lastError = "" + grantUnknown = false + loginSucceeded() + } + + function beginLogin() { + if (loginBusy || loggedIn || removalBusy) return + var problem = M365.backendProblem(backend) + if (problem === "" && !configured) problem = "Add this mailbox's Application (client) ID first" + if (problem !== "") { + lastError = problem + return + } + // A restore still waiting is not the answer to a sign-in that was asked for. + var gen = ++generation + retryTimer.stop() + restoring = false + restoreOwed = false + lastError = "" + loginBusy = true + signInStage = "starting" + var asked = { accountId: accountId, clientId: effectiveClientId } + if (!signInClients.some(function(known) { + return known.accountId === asked.accountId && known.clientId === asked.clientId + })) signInClients = signInClients.concat([asked]) + backend.call("m365.signInBegin", { accountId: accountId, address: address, clientId: effectiveClientId }, + function(result, failure) { + if (!root || gen !== root.generation) return + var begin = failure ? null : M365.beginResult(result) + if (!begin) { + root.failSignIn(failure ? M365.errorCode(failure) : "m365_invalid_response") + return + } + root.requestId = begin.requestId + root.userCode = begin.userCode + root.verificationUri = begin.verificationUri + root.signInStage = "waiting" + pollTimer.interval = Math.max(1, Math.round(begin.interval * 1000 * root.timeScale)) + // The app has no private-window helper: the page tells the person what + // to do if the browser offers the wrong account. + if (root.platform && typeof root.platform.openExternal === "function") + root.platform.openExternal(begin.verificationUri) + pollTimer.restart() + }) + } + + function poll() { + if (signInStage !== "waiting") return + var gen = generation + var account = accountId + var client = effectiveClientId + backend.call("m365.signInPoll", { requestId: requestId }, function(result, failure) { + if (!root) return + if (gen !== root.generation) { + // Cancelled while Microsoft was answering: the backend may keep a sign-in nobody + // wants. A newer one of the same app writes the same keyring item: a sign-out now + // would end it while it waits, so it is left to that one's outcome, and would delete + // what it saved while it is signed in or its check is to be asked again. One of + // another app has an item of its own, which signing this one out leaves alone. + if (!M365.pollMayHaveSaved(result, failure)) return + var same = account === root.accountId && client === root.effectiveClientId + if (same && root.loginBusy) root.grantUnknown = true + else if (!same || !(root.loggedIn || root.restoreOwed)) root.forget(account, client) + return + } + // The answer never came: the same request is asked again, and holds its outcome. + if (failure && M365.isLostAnswer(failure)) { + root.grantUnknown = true + pollTimer.restart() + return + } + if (failure) { + root.failSignIn(M365.errorCode(failure)) + return + } + var answer = M365.pollResult(result) + if (answer.state === "pending") { + // Still at Microsoft's or the keyring's: what comes of it is asked for next time. + if (M365.pollMayHaveSaved(result, null)) root.grantUnknown = true + pollTimer.restart() + } else if (answer.state === "signedIn") { + // The one saved is this one, wanted; the check decides what becomes of it. + root.grantUnknown = false + root.prove() + } else root.failSignIn(answer.code, answer.who) + }) + } + + // The code was entered: what the page shows from here is the mailbox being + // checked, not a code that has done its work. + function prove() { + clearCode() + signInStage = "checking" + check(function(result, code) { + if (code === "") { + accept(result) + return + } + // A check that failed for a reason that stays leaves nothing signed in + // behind it. The network failing does not undo the sign-in: it is checked + // again the way a saved one is, with no new code. + if (M365.isTransient(code)) { + clearCode() + loginBusy = false + restoreOwed = true + lastError = M365.sentence(code) + scheduleRetry() + sessionUnavailable(lastError) + return + } + forget() + failSignIn(code) + }) + } + + function failSignIn(code, who) { + clearCode() + loginBusy = false + if (grantUnknown) forget() + lastError = M365.failureSentence(code, who || "", address) + sessionUnavailable(lastError) + } + + // Cuts a sign-in short, which is not a failure and says nothing. The backend's + // own wait for the code ends at its expiry. + function cancelLogin() { + if (!loginBusy) return + var entered = signInStage === "checking" + generation++ + clearCode() + loginBusy = false + lastError = "" + // The code was entered, so the backend holds a sign-in nobody wants. + if (entered || grantUnknown) forget() + } + + // Asked when the account is built and again when the backend comes up: the + // saved sign-in is proven by the same check. Quiet when there is none, since a + // mailbox being added has none by design. + function restoreSession() { + if (loginBusy || restoring || loggedIn || removalBusy || !configured || !restoreOwed) return + var problem = M365.backendProblem(backend) + if (problem !== "") { + // One that is not up yet is waited for; one that is up and too old is not. + if (backend && backend.ready === true) { + restoreOwed = false + lastError = problem + sessionUnavailable(problem) + } + return + } + restoring = true + check(function(result, code) { + restoring = false + if (code === "") { + accept(result) + } else if (M365.isNothingSaved(code)) { + restoreOwed = false + checked = null + } else { + lastError = M365.sentence(code) + restoreOwed = M365.isTransient(code) + if (restoreOwed) scheduleRetry() + sessionUnavailable(lastError) + } + }) + } + + function scheduleRetry() { + var step = M365.RETRY_DELAYS[Math.min(retryAttempt++, M365.RETRY_DELAYS.length - 1)] + retryTimer.interval = Math.max(1, Math.round(step * timeScale)) + retryTimer.restart() + } + + // Called by the client when a request found the sign-in gone while the window + // thought it was there: the refresh token was revoked or removed elsewhere. + function noteSignedOut() { + if (!loggedIn) return + loggedIn = false + checked = null + restoreOwed = false + lastError = M365.sentence("m365_signed_out") + sessionUnavailable(lastError) + } + + // Deletes a saved sign-in, this mailbox's unless another is named. One asked + // for before the backend is up is sent when it is. A backend that cannot says + // so, because one left behind would be restored at the next start as if + // nothing had happened. + function forget(account, client) { + var request = { accountId: account || accountId, clientId: client || effectiveClientId } + if (request.accountId === "" || request.clientId === "") return + if (request.accountId === accountId && request.clientId === effectiveClientId) grantUnknown = false + if (!backend || backend.ready !== true) { + owedSignOuts = owedSignOuts.concat([request]) + return + } + backend.call("m365.signOut", request, function(result, failure) { + if (root && failure && request.accountId === root.accountId) + root.lastError = M365.signOutFailure(failure) + }) + } + + // This mailbox's sign-in, and any of another ID this object asked for: its + // exchange may still save a grant that would be restored at the next start. + function forgetEvery() { + forget() + for (var i = 0; i < signInClients.length; i++) { + var asked = signInClients[i] + if (asked.accountId === accountId && asked.clientId !== effectiveClientId) + forget(asked.accountId, asked.clientId) + } + } + + function sendOwedSignOuts() { + var owed = owedSignOuts + owedSignOuts = [] + for (var i = 0; i < owed.length; i++) forget(owed[i].accountId, owed[i].clientId) + } + + function reset() { + generation++ + removalBusy = false + clearCode() + retryTimer.stop() + loginBusy = false + restoring = false + loggedIn = false + checked = null + lastError = "" + retryAttempt = 0 + grantUnknown = false + } + + // Also for a mailbox that was never signed in. + function logout() { + reset() + restoreOwed = false + forgetEvery() + loggedOut() + } + + // Removal must keep this account alive until the backend confirms its saved + // sign-in is gone. A per-account queue dies with its delegate, and a failed + // keyring deletion must leave a visible account the person can retry. + function forgetCredential(done) { + if (removalBusy) { if (typeof done === "function") done(false); return } + var requests = [] + function add(account, client) { + if (account !== root.accountId || client === "") return + if (!requests.some(function(known) { return known.clientId === client })) + requests.push({ accountId: account, clientId: client }) + } + add(accountId, effectiveClientId) + for (var i = 0; i < signInClients.length; i++) add(signInClients[i].accountId, signInClients[i].clientId) + for (var j = 0; j < owedSignOuts.length; j++) add(owedSignOuts[j].accountId, owedSignOuts[j].clientId) + if (requests.length === 0) { if (typeof done === "function") done(true); return } + var problem = M365.backendProblem(backend) + if (problem !== "") { + lastError = "The mailbox was not removed. " + problem + ". Try Remove account again when it is ready" + if (typeof done === "function") done(false) + return + } + reset() + restoreOwed = false + removalBusy = true + loggedOut() + var gen = generation + var remaining = requests.length + var failureText = "" + for (var k = 0; k < requests.length; k++) { + backend.call("m365.signOut", requests[k], function(result, failure) { + if (!root || gen !== root.generation) return + if (failure || !result || result.signedOut !== true) + failureText = M365.signOutFailure(failure || "m365_invalid_response") + if (--remaining !== 0) return + root.removalBusy = false + if (failureText !== "") root.lastError = "The mailbox was not removed. " + failureText + else root.owedSignOuts = root.owedSignOuts.filter(function(owed) { return owed.accountId !== root.accountId }) + if (typeof done === "function") done(failureText === "") + }) + } + } + + // A different mailbox has a different sign-in, and whatever was in the air + // belongs to the old one. + onAccountIdChanged: { + reset() + restoreOwed = true + Qt.callLater(function() { if (root) root.restoreSession() }) + } + + Connections { + target: root.backend + ignoreUnknownSignals: true + function onReadyChanged() { + if (root.backend.ready === true) root.sendOwedSignOuts() + root.restoreSession() + } + } + + Timer { + id: pollTimer + onTriggered: root.poll() + } + + Timer { + id: retryTimer + onTriggered: root.restoreSession() + } +} diff --git a/ui/providers/M365Client.qml b/ui/providers/M365Client.qml new file mode 100644 index 0000000..d16e897 --- /dev/null +++ b/ui/providers/M365Client.qml @@ -0,0 +1,231 @@ +import QtQuick + +import "../account/Aliases.js" as Aliases +import "M365Protocol.js" as M365 + +// Presentation adapter: the sign-in, Microsoft Graph, MIME conversion and every +// mutation belong to Rust. This turns a call from `MailAccount` into one `m365.*` +// request and the answer back into the shape that call expects. +// +// A request names the account and carries nothing that could authenticate it. One +// the backend answers as signed out is the sign-in having gone: the auth object +// is told once. One made while the auth object is checking the sign-in waits for +// that check: it is sent if the check passes and answered as signed out if not. +Item { + id: root + + visible: false + width: 0 + height: 0 + + required property var auth + property var backend: null + property string email: "" + + property int inFlight: 0 + readonly property bool busy: inFlight > 0 + // Bumped when a session ends, so a reply from the one before is dropped. + property int epoch: 0 + property int sequence: 0 + readonly property string tokenPrefix: Date.now().toString(36) + "-" + Math.random().toString(36).slice(2) + // `[method, params, callback, handle, accountId]` of the requests waiting for the check. + property var waiting: [] + + // What the last check taught: the capabilities this account gives back to the + // provider's ceiling and the rail rows whose folder it lacks. Null until a + // check has answered, which leaves the ceiling and every row as they are. + readonly property var refusals: M365.refusals(auth ? auth.checked : null) + readonly property var absentMailboxes: M365.absentMailboxes(auth ? auth.checked : null) + + function newHandle() { return { aborted: false, children: [], tokens: [], accountId: "" } } + + // A handle can have several requests in the air (a search's listing and the + // rows read for it), each cancelled by its own token. + function abortRequest(handle) { + if (!handle || handle.aborted) return + handle.aborted = true + var tokens = handle.tokens || [] + for (var i = 0; i < tokens.length; i++) { + if (backend && backend.ready) + backend.call("m365.cancel", { accountId: handle.accountId, requestToken: tokens[i] }, function() {}) + } + handle.tokens = [] + var children = handle.children || [] + for (var j = 0; j < children.length; j++) abortRequest(children[j]) + } + + function nativeRequest(method, params, callback, existingHandle) { + var handle = existingHandle || newHandle() + if (handle.aborted) return handle + if (checking()) { + waiting = waiting.concat([[method, params, callback, handle, String(auth.accountId || "")]]) + return handle + } + var owner = auth + var account = owner ? String(owner.accountId || "") : "" + var problem = owner && owner.loggedIn === true ? M365.backendProblem(backend) : M365.sentence("m365_signed_out") + if (problem !== "") { + if (typeof callback === "function") callback(null, problem) + return handle + } + var request = Object.assign({}, params, { accountId: account }) + // Only the requests that can be long take a token, and the backend refuses a + // field it does not know. + var token = "" + if (method === "m365.list" || method === "m365.messages") { + token = tokenPrefix + ":" + String(++sequence) + request.requestToken = token + handle.tokens = (handle.tokens || []).concat([token]) + handle.accountId = account + } + var generation = epoch + inFlight++ + backend.call(method, request, function(result, failure) { + if (!root) return + root.inFlight = Math.max(0, root.inFlight - 1) + if (token !== "") handle.tokens = handle.tokens.filter(function(held) { return held !== token }) + var live = owner === root.auth && String(owner.accountId || "") === account && generation === root.epoch + if (live && failure && M365.isSignedOut(failure)) root.signedOut() + if (handle.aborted || !live) return + if (typeof callback === "function") + callback(failure ? null : result, failure ? M365.requestSentence(method, request, failure) : "") + }) + return handle + } + + function checking() { + return !!auth && auth.loggedIn !== true && (auth.restoring === true || auth.signInStage === "checking") + } + + // Called a tick after the auth object changes, when its state is whole again. + function release() { + if (checking() || waiting.length === 0) return + var held = waiting + waiting = [] + for (var i = 0; i < held.length; i++) { + // Asked of the mailbox that was being checked, never of the one after it. + if (held[i][4] === String(auth ? auth.accountId || "" : "")) nativeRequest(held[i][0], held[i][1], held[i][2], held[i][3]) + else if (!held[i][3].aborted && typeof held[i][2] === "function") held[i][2](null, M365.sentence("m365_signed_out")) + } + } + + function signedOut() { + if (auth && typeof auth.noteSignedOut === "function") auth.noteSignedOut() + } + + // An answer that needs no request, given on the next tick like the others. + function answerLater(callback, result, error) { + var handle = newHandle() + Qt.callLater(function() { + if (root && !handle.aborted && typeof callback === "function") callback(result, error) + }) + return handle + } + + function listMessages(query, maxResults, pageToken, callback) { + var limit = Math.floor(Number(maxResults)) + return nativeRequest("m365.list", { query: String(query || ""), + limit: limit >= 1 ? Math.min(limit, 100) : 25, pageToken: String(pageToken || "") }, + function(result, error) { + var page = result ? result.page : null + if (!error && (!page || !Array.isArray(page.ids))) error = M365.sentence("") + if (typeof callback !== "function") return + if (error) { callback(null, error); return } + callback({ ids: page.ids, threadIds: [], nextPageToken: String(page.nextPageToken || ""), + estimate: Math.max(0, Math.floor(Number(page.estimate)) || 0) }, + result.warning ? "The mailbox search could not be completed" : "") + }) + } + + // A listing is never collapsed into conversations, so there are no members. + function getSummaries(ids, callback) { return answerLater(callback, [], "") } + + function getMessages(ids, full, callback, existingHandle) { + if (!Array.isArray(ids) || ids.length === 0) return answerLater(callback, [], "") + return nativeRequest("m365.messages", { ids: ids, full: full === true }, function(result, error) { + if (typeof callback !== "function") return + var messages = result && Array.isArray(result.messages) ? result.messages : [] + var partial = !!result && (!!result.warning || !!result.continuation) + callback(messages, error || (partial ? "Some messages could not be loaded" : "")) + }, existingHandle) + } + + function getAttachment(messageId, attachmentId, callback) { + return nativeRequest("m365.attachment", { messageId: String(messageId || ""), + attachmentId: String(attachmentId || "") }, function(result, error) { + if (typeof callback === "function") callback(error || !result ? "" : String(result.data || ""), error) + }) + } + + // No folders of the user's own: the rail is the roles the registry draws. + function getLabels(callback) { + if (typeof callback === "function") callback([], "") + return newHandle() + } + + function getProfile(callback) { + return answerLater(callback, { email: String(email || ""), messagesTotal: 0, threadsTotal: 0, historyId: "" }, "") + } + + // The mailbox's own address and nothing else: there is no list of identities. + function getSendAs(callback) { + return answerLater(callback, Aliases.sendAsList(String(email || "").trim(), []), "") + } + + function modifyMessage(id, addLabelIds, removeLabelIds, callback) { + return batchModify([id], addLabelIds, removeLabelIds, callback) + } + + function batchModify(ids, addLabelIds, removeLabelIds, callback) { + return changeMessages("m365.modify", { ids: ids, addLabelIds: addLabelIds, removeLabelIds: removeLabelIds }, callback) + } + + function trashMessage(ids, callback) { + return changeMessages("m365.trash", { ids: Array.isArray(ids) ? ids : [ids] }, callback) + } + + function untrashMessage(ids, callback) { + return changeMessages("m365.untrash", { ids: Array.isArray(ids) ? ids : [ids] }, callback) + } + + // A change names every id as done or not. Anything not done is a failure, and + // the failed ids go back as a third argument so the list restores exactly + // those rows; a failure naming none is the whole request's. + function changeMessages(method, params, callback) { + return nativeRequest(method, params, function(result, error) { + if (typeof callback !== "function") return + var failed = result && Array.isArray(result.failedIds) ? result.failedIds : [] + var code = result && result.error ? String(result.error) : "" + if (error || (failed.length === 0 && code === "")) { callback(error ? null : result, error); return } + if (M365.isSignedOut(code)) root.signedOut() + callback(result, code !== "" ? M365.sentence(code) : "Microsoft 365 did not apply this change to every message", + failed.length > 0 ? failed : undefined) + }) + } + + function saveDraft(payload, callback) { + return nativeRequest("m365.saveDraft", { raw: String(payload && payload.raw || ""), + draftId: String(payload && payload.draftId || "") }, callback) + } + + function deleteDraft(messageId, callback) { + return nativeRequest("m365.deleteDraft", { id: String(messageId || "") }, callback) + } + + function sendMessage(payload, callback) { + return nativeRequest("m365.send", { raw: String(payload && payload.raw || "") }, callback) + } + + Connections { + target: root.auth + ignoreUnknownSignals: true + function onLoggedOut() { + root.epoch++ + if (root.backend && root.backend.ready && root.auth && root.auth.accountId) + root.backend.call("m365.invalidate", { accountId: String(root.auth.accountId) }, function() {}) + } + function onLoggedInChanged() { Qt.callLater(root.release) } + function onRestoringChanged() { Qt.callLater(root.release) } + function onSignInStageChanged() { Qt.callLater(root.release) } + } +} diff --git a/ui/providers/M365Protocol.js b/ui/providers/M365Protocol.js new file mode 100644 index 0000000..59ceff7 --- /dev/null +++ b/ui/providers/M365Protocol.js @@ -0,0 +1,248 @@ +.pragma library + +.import "MicrosoftOAuth.js" as Microsoft + +// What the Microsoft 365 provider knows without asking anybody: how the answers +// of the device sign-in are read, the sentence for every failure, and what a +// mailbox check teaches the rail. No transport and no token: Rust makes every +// request, keeps the refresh token in the keyring and the access token in memory. + +// The first backend API with the `m365.*` methods. +var MIN_API_VERSION = 8 + +// An Application (client) ID in the one spelling the backend keys a saved +// sign-in by (a GUID, lower case), else "". There is no built-in one: each +// person enters the ID of their own app (Outlook's is another registration). +function clientId(value) { + return Microsoft.isValidClientId(value) ? Microsoft.trimmed(value).toLowerCase() : "" +} + +function trimmed(value) { + return String(value === undefined || value === null ? "" : value).trim() +} + +function isObject(value) { + return value !== null && typeof value === "object" && !Array.isArray(value) +} + +// Text that came from an answer and reaches a label: one bounded plain line. +function lineText(value, limit) { + var text = trimmed(String(value).replace(/[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2066-\u2069\ufeff]+/g, " ")) + return text.length > limit ? text.substring(0, limit - 3) + "..." : text +} + +var SIGN_IN_AGAIN = "Sign in to Microsoft 365 again" + +// Every code the backend answers with, and the sentence a person gets for it. +// The codes are Rust's static ones, so no server text is passed on. +var SENTENCES = { + auth_account_missing: "This mailbox is not set up on this computer", + auth_keyring_failed: "The system keyring is locked or unavailable. Unlock it: the mailbox's sign-in is kept there", + backend_needs_update: "Update the mail backend to use this mailbox", + credential_store_unavailable: "The credential store is unavailable", + m365_access_denied: "Microsoft refused access to this mailbox or message. Ask your administrator if it keeps happening", + m365_attachment_not_found: "That attachment is no longer in the mailbox", + m365_auth_failed: "Microsoft refused this sign-in. Check the Application (client) ID and that the app allows public client flows and accounts of your organization, then try again", + m365_bad_response: "Microsoft's answer was not what Omamail expected. Try again in a moment", + m365_cancelled: "Cancelled", + m365_consent_required: "Your organization's administrator has to approve this app before it can open the mailbox. Ask them to grant consent for it, then sign in again. If the page named another company, your browser was signed in to a different Microsoft account: try a private window", + m365_declined: "The sign-in was declined on the Microsoft page. Press Sign in to try again", + m365_draft_unconfirmed: "Microsoft did not confirm saving the draft. It may already be in Drafts", + m365_expired: "The code ran out before it was used. Press Sign in for a new one", + m365_folder_unavailable: "A folder this needs was not found in the mailbox", + m365_invalid_request: "Omamail could not make that request. Try again", + m365_invalid_response: "Microsoft's answer was not what Omamail expected. Try again in a moment", + m365_message_not_found: "That message is no longer in the mailbox", + m365_message_too_large: "This message is too large to send through Microsoft 365. Remove an attachment or make it smaller, then try again", + m365_network_failed: "Microsoft 365 could not be reached. Check your connection", + m365_not_a_draft: "That message is not a draft, so it was left in place", + m365_request_limit: "Too many requests are waiting. Try again in a moment", + m365_response_too_large: "Microsoft's answer was larger than Omamail accepts", + m365_send_failed: "Microsoft did not confirm sending. Check Sent before trying again", + m365_send_not_attempted: "The message was not sent. It is safe to try again", + m365_send_rejected: "Microsoft 365 rejected this message", + m365_server_error: "Microsoft 365 reported an error while handling the request. Try again in a moment", + m365_signed_out: SIGN_IN_AGAIN, + m365_tenant_invalid: "Microsoft could not find an organization for that address. Check the address", + m365_throttled: "Microsoft asked Omamail to slow down. Try again in a moment", + m365_timeout: "Microsoft 365 took too long to answer", + m365_tls_failed: "The secure connection to Microsoft could not be verified", + m365_wrong_account: "The code was entered with a different Microsoft account than this mailbox's. Press Sign in again; if the browser offers the wrong account, open the page in a private window", + request_timed_out: "Microsoft 365 took too long to answer" +} + +var GENERIC = "Microsoft 365 could not complete this request" + +// The same code from a read is a message too large to open, not one refused. +var TOO_LARGE_TO_OPEN = "This message is larger than Omamail opens from Microsoft 365" +var READS = ["m365.read", "m365.messages", "m365.attachment"] + +// A sign-in that could not be saved has to be done again; a saved one that a +// locked keyring hides is checked again by itself (`TRANSIENT`). +var NOT_SAVED = "The sign-in could not be saved in the system keyring. Unlock it, then sign in again" + +// The Rust code of whatever a failed call handed back: the `message` of a +// backend error, or a bare string. +function errorCode(error) { + if (error === undefined || error === null) return "" + return trimmed(typeof error === "object" ? error.message : error) +} + +function sentence(error) { + var code = errorCode(error) + return Object.prototype.hasOwnProperty.call(SENTENCES, code) ? SENTENCES[code] : GENERIC +} + +// A code that means there is no sign-in to use: nothing else ends a session, +// since a refused message or a network that is down leaves the account as it was. +function isSignedOut(error) { + return errorCode(error) === "m365_signed_out" +} + +// No saved sign-in, or no such mailbox in the list yet, which is the moment +// between typing an address and the file holding it. Neither is a failure. +function isNothingSaved(error) { + return isSignedOut(error) || errorCode(error) === "auth_account_missing" +} + +// Failures that pass by themselves, which a restored session waits out. A +// locked keyring is one: unlocking it is all a saved sign-in needs. +var TRANSIENT = ["m365_network_failed", "m365_timeout", "request_timed_out", "m365_throttled", + "m365_server_error", "m365_request_limit", "m365_bad_response", "auth_keyring_failed"] + +function isTransient(error) { + return TRANSIENT.indexOf(errorCode(error)) >= 0 +} + +// A request whose answer never came: the backend's frame ran out before it +// started, the window stopped waiting, the answer stopped arriving part-way, +// the backend stopped, or what came back could not be read and the window +// stopped it. What it did is not known. +var LOST = ["request_timed_out", "Backend request timed out", "Backend response timed out", + "Backend stopped", "Invalid backend response"] + +function isLostAnswer(error) { + return LOST.indexOf(errorCode(error)) >= 0 +} + +// What a sign-out that failed leaves behind. One that could not wait for the +// sign-in to be free (`m365_timeout`: a refresh was waiting on the keyring) may +// not have been done, and the mailbox may then be signed in again at the next start. +var SIGN_OUT_UNFINISHED = "Signing out could not be completed while the sign-in was in use, so it may still be " + + "saved on this computer. Unlock the system keyring if it asks, and sign out again if the mailbox comes back signed in" + +function signOutFailure(error) { + if (errorCode(error) === "m365_timeout") return SIGN_OUT_UNFINISHED + return "The saved sign-in could not be removed. " + sentence(error) +} + +// Steps up to five minutes: a laptop that wakes without a network is not asking +// Microsoft every few seconds. +var RETRY_DELAYS = [5000, 15000, 60000, 300000] + +// A typed search that is only quotes or operators comes back as JSON-RPC's +// "Invalid params", which for a search means there was nothing to look for. +function requestSentence(method, params, error) { + var code = errorCode(error) + var typed = /^\s*text:/i.test(String(params && params.query || "")) + if (method === "m365.list" && typed && (code === "invalid_params" || (error && error.code === -32602))) + return "That search has nothing Microsoft 365 can look for" + if (code === "m365_message_too_large" && READS.indexOf(method) >= 0) return TOO_LARGE_TO_OPEN + return sentence(error) +} + +// "" when the backend can be asked for mail, else the sentence for why not. A +// backend stating no API version is taken at its word, so a test can stand in. +function backendProblem(backend) { + if (!backend || backend.ready !== true) return "Mail backend is unavailable" + if (typeof backend.apiVersion === "number" && backend.apiVersion < MIN_API_VERSION) + return SENTENCES.backend_needs_update + return "" +} + +// ------------------------------------------------------------- the sign-in + +// What `m365.signInBegin` answered, or null when it cannot be used. The code is +// entered by a person on Microsoft's page, so what is shown of it is bounded and +// plain, and the page to open is Microsoft's own, over HTTPS, and nothing else. +// The interval between two polls is the backend's, held to a ceiling. +function beginResult(result) { + var value = isObject(result) ? result : {} + var requestId = trimmed(value.requestId) + var code = trimmed(value.userCode) + var link = Microsoft.verificationUri(value.verificationUri) + var interval = Math.floor(Number(value.interval)) + if (requestId === "" || requestId.length > 256 || /[\u0000-\u001f\u007f-\u009f]/.test(requestId) + || !/^[A-Za-z0-9-]{4,32}$/.test(code) || link === "") return null + return { requestId: requestId, userCode: code, verificationUri: link, + interval: interval > 0 ? Math.min(30, interval) : 5 } +} + +// What `m365.signInPoll` answered: `{ state, code, who }`. A state that is none +// of the five is a failed sign-in, never a pending one: a poll that went on for +// ever on a reply nobody understood would be a sign-in that never ends. `code` +// is the sentence's code for a sign-in that ended without a mailbox, `who` the +// account Microsoft reported for a wrong one. +function pollResult(result) { + var value = isObject(result) ? result : {} + var state = value.state + if (state === "pending" || state === "signedIn") return { state: state, code: "", who: "" } + if (state === "declined" || state === "expired") return { state: state, code: "m365_" + state, who: "" } + if (state !== "failed") return { state: "failed", code: "m365_invalid_response", who: "" } + var code = errorCode(value.error) + return { state: "failed", who: typeof value.signedInAs === "string" ? lineText(value.signedInAs, 80) : "", + code: Object.prototype.hasOwnProperty.call(SENTENCES, code) ? code : "m365_auth_failed" } +} + +// Whether the backend may have saved a sign-in for this poll: it says so, or it +// stopped waiting while Microsoft or the keyring still had the exchange +// (`pending` with `m365_exchange_running`), or the answer was lost. A token +// request that failed, its own `m365_timeout` included, saved nothing. +function pollMayHaveSaved(result, failure) { + if (failure) return isLostAnswer(failure) + var value = isObject(result) ? result : {} + return value.state === "signedIn" || (value.state === "pending" && value.warning === "m365_exchange_running") +} + +// The wrong-account sentence names the account Microsoft reported when it did. +function failureSentence(code, who, address) { + if (code === "auth_keyring_failed") return NOT_SAVED + if (code !== "m365_wrong_account" || who === "") return sentence(code) + return "The code was entered as " + who + ", not " + trimmed(address) + + ". Press Sign in again; if the browser keeps offering that account, open the page in a private window" +} + +// ------------------------------------------------- what a check taught us + +// Whether a check's answer proves a signed-in mailbox: it said so. +function verified(result) { + return isObject(result) && result.ok === true +} + +// The capabilities this account withdraws from the provider's ceiling, as +// `Registry.can` reads them: `{ capability: sentence }`. `null` says nothing +// has been read. +function refusals(result) { + if (!verified(result)) return null + var out = {} + var raw = isObject(result.refusals) ? result.refusals : {} + for (var key in raw) { + if (!Object.prototype.hasOwnProperty.call(raw, key) || raw[key] === null || raw[key] === false) continue + out[key] = lineText(typeof raw[key] === "string" ? raw[key] : "", 300) || "This mailbox cannot do that" + } + return out +} + +// The rail rows whose folder the mailbox lacks, as `Registry.mailboxes` takes +// them (the rail's Junk is `spam`). Null until a check has answered, which +// draws every row. +var OPTIONAL_ROLES = [["archive", "archive"], ["junk", "spam"], ["trash", "trash"]] + +function absentMailboxes(result) { + if (!verified(result) || !isObject(result.roles)) return null + var out = [] + for (var i = 0; i < OPTIONAL_ROLES.length; i++) { + if (!isObject(result.roles[OPTIONAL_ROLES[i][0]])) out.push(OPTIONAL_ROLES[i][1]) + } + return out +} diff --git a/ui/providers/NativeDomain.js b/ui/providers/NativeDomain.js index 5f60cac..eebb9ac 100644 --- a/ui/providers/NativeDomain.js +++ b/ui/providers/NativeDomain.js @@ -127,6 +127,37 @@ var FACTS = { }, "webHomeUrl": "" }, + "m365": { + "addressSearch": false, + "capabilities": { + "archive": true, + "batch": true, + "conversations": false, + "labels": false, + "manageLabels": false, + "move": false, + "search": true, + "send": true, + "spam": false, + "star": true, + "threads": false, + "web": false, + "webBox": false + }, + "inheritedDefault": "in:inbox", + "nativeSync": true, + "queries": { + "archive": "role:archive", + "drafts": "role:drafts", + "inbox": "role:inbox", + "sent": "role:sent", + "spam": "role:junk", + "starred": "role:inbox flagged", + "trash": "role:trash", + "unread": "role:inbox unseen" + }, + "webHomeUrl": "" + }, "outlook": { "addressSearch": false, "capabilities": { diff --git a/ui/providers/Registry.js b/ui/providers/Registry.js index 23fe4e1..2c42603 100644 --- a/ui/providers/Registry.js +++ b/ui/providers/Registry.js @@ -6,6 +6,7 @@ .import "NativeDomain.js" as NativeDomain .import "Gmail.js" as Gmail .import "Outlook.js" as Outlook +.import "M365.js" as M365 .import "Imap.js" as Imap .import "Hey.js" as Hey .import "Jmap.js" as Jmap @@ -82,7 +83,7 @@ function define(source) { } } -var ALL = [define(Gmail), define(Hey), define(Outlook), define(Imap), define(Jmap)] +var ALL = [define(Gmail), define(Hey), define(Outlook), define(M365), define(Imap), define(Jmap)] var DEFAULT_ID = "gmail" @@ -179,6 +180,19 @@ function badge(id) { return get(id).name } +// The provider shown under a name, for what is handed a name rather than an id. +function named(name) { + for (var i = 0; i < ALL.length; i++) { + if (ALL[i].name === String(name)) return ALL[i].id + } + return "" +} + +// The providers that sign in on Microsoft's own page, with an Application (client) ID. +function signsInWithMicrosoft(id) { + return ["outlook", "m365"].indexOf(String(id)) >= 0 +} + function summary(id) { return String(get(id).summary || "") } diff --git a/ui/tests/oracles/providers/M365.js b/ui/tests/oracles/providers/M365.js new file mode 100644 index 0000000..89383d5 --- /dev/null +++ b/ui/tests/oracles/providers/M365.js @@ -0,0 +1,19 @@ +.pragma library + +.import "../../../providers/M365.js" as Provider + +// What the legacy provider functions say for a Microsoft 365 mailbox. The +// description is the real one; only the query function is restated here, because +// production has none: the backend resolves them. + +var ID = Provider.ID +var NAME = Provider.NAME +var SUMMARY = Provider.SUMMARY +var AUTH = Provider.AUTH +var MAILBOXES = Provider.MAILBOXES + +// A typed search runs against the whole mailbox: the words are the text filter. +function searchQuery(text) { + var value = String(text === undefined || text === null ? "" : text).trim() + return value === "" ? "" : "text:" + value +} diff --git a/ui/tests/oracles/providers/Registry.js b/ui/tests/oracles/providers/Registry.js index 31dd5c7..e09ba4b 100644 --- a/ui/tests/oracles/providers/Registry.js +++ b/ui/tests/oracles/providers/Registry.js @@ -3,6 +3,7 @@ .import "../../../providers/NativeDomain.js" as NativeDomain .import "Gmail.js" as Gmail .import "Outlook.js" as Outlook +.import "M365.js" as M365 .import "Imap.js" as Imap .import "Hey.js" as Hey .import "Jmap.js" as Jmap @@ -157,7 +158,7 @@ function define(source) { // speaks both is better read over JMAP — threads, a junk verb, and one round // trip for a batch — so somebody who has one should meet it before they settle // for the catch-all. -var ALL = [define(Gmail), define(Outlook), define(Hey), define(Jmap), define(Imap)] +var ALL = [define(Gmail), define(Outlook), define(M365), define(Hey), define(Jmap), define(Imap)] var DEFAULT_ID = "gmail" diff --git a/ui/tests/qml/tst_backend.qml b/ui/tests/qml/tst_backend.qml index 0b31b78..d3d5406 100644 --- a/ui/tests/qml/tst_backend.qml +++ b/ui/tests/qml/tst_backend.qml @@ -48,6 +48,22 @@ Item { verify(pending[2].deadline >= before + 30000 && pending[2].deadline < before + 31000) } + function test_a_microsoft_365_message_waits_longer_than_another_mailbox() { + var backend = makeBackend() + makeReady(backend) + var before = Date.now() + backend.call("reader.open", {accountId: "m365:ada@example.org", id: "abc", requestId: "r", cacheOnly: false}, function() {}) + backend.call("m365.read", {accountId: "m365:ada@example.org", id: "abc", full: true}, function() {}) + backend.call("m365.messages", {accountId: "m365:ada@example.org", ids: ["abc"], full: false}, function() {}) + backend.call("reader.open", {accountId: "ada@example.org", id: "abc", requestId: "r", cacheOnly: false}, function() {}) + var pending = Object.keys(backend.pending).map(function(id) { return backend.pending[id] }) + compare(pending.length, 4) + verify(pending[0].deadline >= before + 90000) + verify(pending[1].deadline >= before + 90000) + verify(pending[2].deadline >= before + 30000 && pending[2].deadline < before + 31000) + verify(pending[3].deadline >= before + 30000 && pending[3].deadline < before + 31000) + } + function test_cancelled_upload_discards_staged_data_without_domain_dispatch() { var backend = makeBackend() var process = makeReady(backend) diff --git a/ui/tests/qml/tst_calendar_m365.qml b/ui/tests/qml/tst_calendar_m365.qml new file mode 100644 index 0000000..3f08ea2 --- /dev/null +++ b/ui/tests/qml/tst_calendar_m365.qml @@ -0,0 +1,134 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../../calendar" as Omamail + +// A Microsoft 365 mailbox's calendar comes with its one sign-in: the Graph source +// appears for a signed-in `m365:` account the way it does for an Outlook one, is +// asked for through the same native requests, and its failures are told without +// a second permission to look for. +Item { + width: 400 + height: 300 + + QtObject { + id: mailService + + property var requests: [] + property var answers: ({}) + property bool backendCanDiscoverCalendars: true + property bool backendCanGoogleCalendars: false + property var backend: ({ ready: true, call: function(method, params, callback) { + mailService.requests.push({ method: method, params: JSON.parse(JSON.stringify(params)) }) + var answer = mailService.answers[method] || { result: { body: "{\"value\":[]}", status: 200 }, error: null } + callback(answer.result, answer.error) + } }) + property bool unifiedCalendarView: false + property var accountSummaries: [] + } + + Omamail.CalendarController { + id: controller + service: mailService + pluginDir: "/tmp/omamail-test" + accountId: "m365:jane@corp.example.test" + sourceList: ({ version: 1, sources: [] }) + } + + TestCase { + name: "CalendarM365" + when: windowShown + + readonly property string accountId: "m365:jane@corp.example.test" + readonly property string sourceId: "microsoft:m365:jane@corp.example.test" + + function cache() { + for (var i = 0; i < controller.children.length; i++) { + if (controller.children[i].cacheName !== undefined) return controller.children[i] + } + return null + } + + function init() { + mailService.requests = [] + mailService.answers = ({}) + mailService.accountSummaries = [ + { id: accountId, email: "jane@corp.example.test", provider: "m365", calendarProvider: "microsoft", signedIn: true }, + { id: "outlook:me@example.org", email: "me@example.org", provider: "outlook", calendarProvider: "microsoft", + signedIn: true }] + controller.accountId = accountId + controller.sourceList = ({ version: 1, sources: [] }) + controller.lastError = "" + controller.loading = false + controller.queue = [] + controller.events = [] + cache().loaded = true + } + + function cleanup() { cache().loaded = false } + + function mine() { + return controller.availableSources.sources.filter(function(source) { + return source.kind === "microsoft" && source.accountId === accountId }) + } + + function test_a_signed_in_mailbox_brings_its_graph_calendar_and_a_signed_out_one_none() { + compare(mine().length, 1, "one calendar for the mailbox") + compare(mine()[0].id, sourceId) + compare(mine()[0].readOnly, false) + compare(mine()[0].enabled, true) + compare(controller.discoverableAccount(accountId), true, "its other calendars can be found") + var summaries = JSON.parse(JSON.stringify(mailService.accountSummaries)) + summaries[0].signedIn = false + mailService.accountSummaries = summaries + compare(mine().length, 0) + compare(controller.discoverableAccount(accountId), false) + } + + function test_the_range_is_asked_of_the_shared_native_request_for_this_account_and_nothing_else() { + controller.refresh(1790000000000, 1792592000000) + var asked = mailService.requests.filter(function(request) { return request.params.source + && request.params.source.accountId === accountId }) + compare(asked.length, 1) + compare(asked[0].method, "calendar.request", "no method of its own: the Graph calendar code is shared") + compare(asked[0].params.operation, "list") + compare(asked[0].params.source.id, sourceId) + verify(!/token|secret|password|bearer|authorization|clientId/i.test(JSON.stringify(asked[0].params)), + "nothing that could authenticate") + compare(controller.lastError, "") + } + + function test_a_refused_calendar_says_to_sign_in_again_and_names_no_second_permission() { + var told = null + // Refused, signed out, an app ID gone, a refresh refused or a saved sign-in that cannot be + // read: the mail side asks for the sign-in again for each, and so does the calendar. + for (var refused of ["calendar_auth_refused", "auth_signed_out", "auth_client_missing", + "auth_client_invalid", "auth_refresh_failed", "auth_secret_invalid"]) { + told = null + mailService.answers["calendar.request"] = { result: null, error: { code: -32000, message: refused } } + controller.nativeRequest({ id: sourceId, kind: "microsoft", accountId: accountId }, "list", {}, + function(result, error) { told = error }) + compare(told, "Microsoft calendar request failed. Sign in to this mailbox again", refused) + } + // Consent is an administrator's to give for the app, and the one sign-in follows it: there is + // still no second permission to grant, and no page with a code that a private window would help. + mailService.answers["calendar.request"] = { result: null, + error: { code: -32000, message: "auth_consent_required" } } + controller.nativeRequest({ id: sourceId, kind: "microsoft", accountId: accountId }, "list", {}, + function(result, error) { told = error }) + compare(told, "Microsoft calendar request failed. An administrator has to approve this app for your " + + "organization. Then sign in to this mailbox again") + // A network or a throttle that fails is not a sign-in to redo. + for (var code of ["calendar_network_failed", "calendar_timeout", "calendar_request_failed", "auth_transport_failed"]) { + mailService.answers["calendar.request"] = { result: null, error: { code: -32000, message: code } } + controller.nativeRequest({ id: sourceId, kind: "microsoft", accountId: accountId }, "list", {}, + function(result, error) { told = error }) + compare(told, "Microsoft calendar request failed. Try again in a moment", code) + } + // Outlook's own words are as they were: its settings have a Graph step to look at. + var other = null + controller.nativeRequest({ id: "microsoft:outlook:me@example.org", kind: "microsoft", + accountId: "outlook:me@example.org" }, "list", {}, function(result, error) { other = error }) + compare(other, "Microsoft calendar request failed. Check Graph permissions in Settings, then sign in again") + } + } +} diff --git a/ui/tests/qml/tst_m365_auth.qml b/ui/tests/qml/tst_m365_auth.qml new file mode 100644 index 0000000..6e1486c --- /dev/null +++ b/ui/tests/qml/tst_m365_auth.qml @@ -0,0 +1,669 @@ +import QtQuick +import QtTest +import "../../providers" as Providers + +// The Microsoft 365 sign-in on its own: the code, the poll until the person has +// finished, the check that proves the mailbox, the restore of a saved session and +// the sign-out. The backend is a recorder whose answers the test gives by hand; +// nothing here reaches Microsoft, a keyring or a disk. +Item { + id: root + width: 400 + height: 300 + + readonly property string account: "m365:ada@corp.example.test" + readonly property string client: "11111111-2222-3333-4444-555555555555" + readonly property string page: "https://microsoft.com/devicelogin" + + QtObject { + id: platformStub + property var opened: [] + function openExternal(url) { opened = opened.concat([url]); return true } + } + + QtObject { + id: backendStub + property bool ready: true + property int apiVersion: 8 + property var calls: [] + function call(method, params, callback) { + calls = calls.concat([{ method: method, params: JSON.parse(JSON.stringify(params)), callback: callback, + answered: false }]) + } + } + + // Time passes a hundred times as fast: a poll every five seconds is every fifty + // milliseconds and the first retry of a restore comes after fifty as well. + Component { + id: authFactory + Providers.M365Auth { + backend: backendStub + platform: platformStub + accountId: root.account + address: "ada@corp.example.test" + clientId: root.client + timeScale: 0.01 + } + } + + SignalSpy { id: succeeded; signalName: "loginSucceeded" } + SignalSpy { id: loggedOut; signalName: "loggedOut" } + SignalSpy { id: unavailable; signalName: "sessionUnavailable" } + + TestCase { + name: "M365Auth" + when: windowShown + + readonly property var mailbox: ({ ok: true, email: "ada@corp.example.test", + roles: { inbox: { total: 4, unread: 2 }, junk: null, archive: { total: 7, unread: 0 } } }) + + function init() { + backendStub.calls = [] + backendStub.ready = true + backendStub.apiVersion = 8 + platformStub.opened = [] + } + + function calls(method) { + return backendStub.calls.filter(function(call) { return call.method === method }) + } + + function answer(call, result, code) { + verify(!call.answered, "answered twice: " + call.method) + call.answered = true + call.callback(result === undefined ? null : result, code === undefined ? null : { code: -32000, message: code }) + } + + function begin(overrides) { + return Object.assign({ requestId: "req-1", userCode: "ABCD1234E", verificationUri: root.page, + expiresIn: 900, interval: 5 }, overrides || {}) + } + + // A built auth object, its spies on it and nothing asked of it yet. + function built() { + var auth = createTemporaryObject(authFactory, root) + verify(auth) + for (var spy of [succeeded, loggedOut, unavailable]) { spy.target = auth; spy.clear() } + return auth + } + + // A mailbox that was just added: its owed restore is answered "nothing saved". + function made() { + var auth = built() + auth.restoreSession() + answer(calls("m365.verify")[0], null, "m365_signed_out") + backendStub.calls = [] + return auth + } + + function polled(count) { + tryVerify(function() { return calls("m365.signInPoll").length >= count }, 2000, "poll number " + count) + } + + // The press and the answer with the code, then the first poll answered as given. + function codeShown(auth, first) { + auth.beginLogin() + compare(calls("m365.signInBegin").length, 1) + answer(calls("m365.signInBegin")[0], begin()) + if (first === undefined) return + polled(1) + answer(calls("m365.signInPoll")[0], first) + } + + // ------------------------------------------------------------- the sign-in + + function test_one_code_is_asked_shown_polled_and_the_mailbox_checked() { + var auth = made() + auth.beginLogin() + compare(calls("m365.signInBegin")[0].params, { accountId: root.account, address: "ada@corp.example.test", + clientId: root.client }, "the ID is the one typed, and no tenant is chosen") + compare(auth.signInStage, "starting") + compare(auth.recoveringSession, false, "a sign-in is not a restore") + answer(calls("m365.signInBegin")[0], begin()) + compare(auth.userCode, "ABCD1234E") + compare(auth.verificationUri, root.page) + compare(auth.signInStage, "waiting") + compare(platformStub.opened, [root.page], "Microsoft's page is opened once") + compare(calls("m365.signInPoll").length, 0, "the first poll waits for the interval") + + polled(1) + compare(calls("m365.signInPoll")[0].params, { requestId: "req-1" }) + wait(150) + compare(calls("m365.signInPoll").length, 1, "one answer at a time") + auth.beginLogin() + compare(calls("m365.signInBegin").length, 1, "one code at a time") + answer(calls("m365.signInPoll")[0], { state: "pending", warning: "m365_network_failed" }) + polled(2) + compare(auth.loginBusy, true, "a pending answer, warning or not, is not the end of the wait") + compare(calls("m365.verify").length, 0, "a code not entered proves nothing") + answer(calls("m365.signInPoll")[1], { state: "signedIn" }) + compare(auth.signInStage, "checking") + compare(auth.userCode, "", "the used code is gone") + compare(calls("m365.verify")[0].params, { accountId: root.account }) + compare(auth.loggedIn, false, "not signed in until the mailbox has answered") + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, true) + compare(auth.loginBusy, false) + compare(auth.lastError, "") + compare(auth.checked.roles.junk, null, "what the check found is kept for the rail") + compare(succeeded.count, 1) + compare(calls("m365.signOut").length, 0) + } + + function test_each_way_a_sign_in_ends_without_a_mailbox() { + var cases = [[{ state: "declined" }, "declined"], [{ state: "expired" }, "ran out"], + [{ state: "failed", error: "m365_consent_required" }, "administrator"], + [{ state: "failed", error: "m365_tenant_invalid" }, "organization"], + [{ state: "failed", error: "m365_auth_failed" }, "Application (client) ID"], + [{ state: "failed" }, "Application (client) ID"], [{ state: "failed", error: "never_heard_of_it" }, "Application (client) ID"], + [{ state: "unheard" }, "not what Omamail expected"], [null, "not what Omamail expected"], + [{ state: "failed", error: "m365_wrong_account", signedInAs: "bob@other.example.org" }, "bob@other.example.org"], + [{ state: "failed", error: "auth_keyring_failed" }, "could not be saved"]] + for (var i = 0; i < cases.length; i++) { + init() + var auth = made() + codeShown(auth, cases[i][0]) + verify(auth.lastError.indexOf(cases[i][1]) >= 0 && auth.lastError.indexOf("m365_") < 0, JSON.stringify(cases[i][0]) + ": " + auth.lastError) + compare(auth.loginBusy, false) + compare(auth.userCode, "", "the code is gone") + compare(calls("m365.verify").length, 0, "nothing is checked") + compare(calls("m365.signOut").length, 0, "nothing was stored, so nothing is removed") + } + } + + function test_a_begin_or_a_poll_that_fails_or_cannot_be_used_ends_the_sign_in() { + var auth = made() + auth.beginLogin() + answer(calls("m365.signInBegin")[0], null, "m365_network_failed") + compare(auth.lastError, "Microsoft 365 could not be reached. Check your connection") + compare(auth.loginBusy, false) + var unusable = [{ verificationUri: "https://evil.example/devicelogin" }, { userCode: "x" }] + for (var i = 0; i < unusable.length; i++) { + backendStub.calls = [] + auth.beginLogin() + answer(calls("m365.signInBegin")[0], begin(unusable[i])) + compare(auth.lastError, "Microsoft's answer was not what Omamail expected. Try again in a moment", JSON.stringify(unusable[i])) + compare(auth.userCode, "") + compare(auth.loginBusy, false) + } + compare(platformStub.opened, [], "a page that cannot be trusted is not opened") + backendStub.calls = [] + auth.beginLogin() + answer(calls("m365.signInBegin")[0], begin()) + polled(1) + answer(calls("m365.signInPoll")[0], null, "m365_invalid_request") + compare(auth.lastError, "Omamail could not make that request. Try again") + compare(auth.loginBusy, false) + } + + // ------------------------------------------------------------------ cancel + + function test_cancel_while_waiting_stops_the_poll_and_says_nothing() { + var auth = made() + codeShown(auth) + polled(1) + auth.cancelLogin() + compare(auth.loginBusy, false) + compare(auth.userCode, "") + compare(auth.lastError, "", "a cancel is not a failure") + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + wait(150) + compare(calls("m365.verify").length, 0, "the code that was cancelled is not proved") + compare(calls("m365.signOut").length, 1, "and the sign-in the backend kept for it is removed") + compare(calls("m365.signOut")[0].params, { accountId: root.account, clientId: root.client }) + compare(auth.loggedIn, false) + compare(calls("m365.signInPoll").length, 1, "and nothing asks again") + auth.cancelLogin() + compare(unavailable.count, 0, "cancel with nothing under way does nothing") + } + + function test_a_poll_answered_after_a_cancel_with_no_sign_in_removes_nothing() { + var auth = made() + codeShown(auth) + polled(1) + auth.cancelLogin() + answer(calls("m365.signInPoll")[0], { state: "pending" }) + wait(100) + compare(calls("m365.signOut").length, 0, "nothing was kept, so nothing is removed") + } + + function test_cancel_before_the_code_arrives_drops_the_code_when_it_does() { + var auth = made() + auth.beginLogin() + auth.cancelLogin() + answer(calls("m365.signInBegin")[0], begin()) + compare(auth.userCode, "") + compare(platformStub.opened, []) + wait(150) + compare(calls("m365.signInPoll").length, 0) + } + + function test_cancel_after_the_code_was_entered_removes_the_sign_in_the_backend_holds() { + var auth = made() + codeShown(auth, { state: "signedIn" }) + compare(auth.signInStage, "checking") + auth.cancelLogin() + compare(calls("m365.signOut")[0].params, { accountId: root.account, clientId: root.client }) + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, false, "the answer of a check nobody wants any more is dropped") + compare(succeeded.count, 0) + } + + // A cancelled sign-in's poll answers after a new one was asked for: the newer one + // writes the same keyring item, and a sign-out for the old one would end it. + function cancelledThenAgain() { + var auth = made() + codeShown(auth) + polled(1) + auth.cancelLogin() + auth.beginLogin() + answer(calls("m365.signInBegin")[1], begin({ requestId: "req-2" })) + return auth + } + + function test_a_late_sign_in_of_a_cancelled_code_leaves_the_newer_sign_in_alone() { + var auth = cancelledThenAgain() + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + compare(calls("m365.signOut").length, 0, "a sign-out now would end the newer sign-in") + compare(auth.loginBusy, true) + polled(2) + compare(calls("m365.signInPoll")[1].params, { requestId: "req-2" }) + answer(calls("m365.signInPoll")[1], { state: "signedIn" }) + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, true) + compare(calls("m365.signOut").length, 0, "the newer sign-in is the one saved") + + // The other order: the newer one is done before the old answer comes. + init() + auth = cancelledThenAgain() + polled(2) + answer(calls("m365.signInPoll")[1], { state: "signedIn" }) + answer(calls("m365.verify")[0], mailbox) + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + compare(auth.loggedIn, true) + compare(calls("m365.signOut").length, 0, "a sign-out now would remove the session in use") + } + + function test_a_late_sign_in_left_for_a_newer_one_goes_when_that_one_ends_without_a_mailbox() { + var auth = cancelledThenAgain() + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + compare(calls("m365.signOut").length, 0) + auth.cancelLogin() + compare(calls("m365.signOut").length, 1, "the newer one was cancelled too: nobody wants either") + compare(calls("m365.signOut")[0].params, { accountId: root.account, clientId: root.client }) + + init() + auth = cancelledThenAgain() + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + polled(2) + answer(calls("m365.signInPoll")[1], { state: "declined" }) + compare(calls("m365.signOut").length, 1, "the newer one was declined") + } + + // The newer sign-in saved its grant and its check failed on the way: the window asks the + // check again, and the item the late answer would sign out is the one it is about to read. + function test_a_late_sign_in_leaves_a_newer_one_whose_check_is_to_be_asked_again() { + var auth = cancelledThenAgain() + polled(2) + answer(calls("m365.signInPoll")[1], { state: "signedIn" }) + answer(calls("m365.verify")[0], null, "m365_network_failed") + compare(auth.loginBusy, false) + compare(auth.recoveringSession, true, "saved, and checked again with no new code") + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + compare(calls("m365.signOut").length, 0, "a sign-out now would delete what the newer sign-in saved") + tryVerify(function() { return calls("m365.verify").length === 2 }, 2000, "the check is asked again") + answer(calls("m365.verify")[1], mailbox) + compare(auth.loggedIn, true) + compare(calls("m365.signOut").length, 0) + } + + // Only the application ID changed between the two: the late one is signed out by its own + // ID at once, which the backend keeps to that ID's sign-ins, and the newer one goes on. + function test_a_late_sign_in_of_the_id_typed_before_is_signed_out_by_that_id() { + var auth = made() + codeShown(auth) + polled(1) + auth.cancelLogin() + var typedNow = "99999999-2222-3333-4444-555555555555" + auth.clientId = typedNow + auth.beginLogin() + compare(calls("m365.signInBegin")[1].params.clientId, typedNow) + answer(calls("m365.signInBegin")[1], begin({ requestId: "req-2" })) + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + compare(calls("m365.signOut").length, 1) + compare(calls("m365.signOut")[0].params, { accountId: root.account, clientId: root.client }) + compare(auth.loginBusy, true, "the newer sign-in goes on") + polled(2) + answer(calls("m365.signInPoll")[1], { state: "signedIn" }) + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, true) + compare(calls("m365.signOut").length, 1, "and what it saved stays") + } + + // The sign-in of the ID typed before may still be exchanging its code at the backend, which + // ends only the sign-ins of the ID a sign-out names: removing the mailbox names both IDs, or + // that grant could be saved afterwards and restored at the next start. + function test_a_removal_signs_out_every_application_id_a_sign_in_was_asked_with() { + var auth = made() + codeShown(auth) + polled(1) + auth.cancelLogin() + var typedNow = "99999999-2222-3333-4444-555555555555" + auth.clientId = typedNow + auth.beginLogin() + answer(calls("m365.signInBegin")[1], begin({ requestId: "req-2" })) + compare(calls("m365.signOut").length, 0, "nothing is known to be saved yet") + auth.forgetCredential() + compare(calls("m365.signOut").map(function(call) { return call.params }), + [{ accountId: root.account, clientId: typedNow }, { accountId: root.account, clientId: root.client }]) + // Asked again with an ID already asked with, a sign-out still names each ID once. + init() + auth = made() + codeShown(auth) + auth.cancelLogin() + auth.beginLogin() + auth.logout() + compare(calls("m365.signOut").map(function(call) { return call.params }), + [{ accountId: root.account, clientId: root.client }]) + } + + // ---------------------------------------------- an answer that never came + + function test_a_poll_whose_answer_was_lost_asks_the_same_request_again() { + var lost = ["request_timed_out", "Backend request timed out", "Backend response timed out"] + for (var i = 0; i < lost.length; i++) { + init() + var auth = made() + codeShown(auth) + polled(1) + answer(calls("m365.signInPoll")[0], null, lost[i]) + compare(auth.loginBusy, true, lost[i] + " is not a failed sign-in") + compare(auth.lastError, "") + polled(2) + compare(calls("m365.signInPoll")[1].params, { requestId: "req-1" }, "the same request, which kept its outcome") + answer(calls("m365.signInPoll")[1], { state: "signedIn" }) + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, true) + compare(calls("m365.signOut").length, 0) + } + } + + function test_a_cancel_after_a_poll_with_no_outcome_yet_removes_what_it_may_have_saved() { + // Lost on the way back, or still at Microsoft's or the keyring's when the poll stopped waiting. + var unsettled = [[null, "request_timed_out"], [null, "Backend response timed out"], + [{ state: "pending", warning: "m365_exchange_running" }, undefined]] + for (var i = 0; i < unsettled.length; i++) { + init() + var auth = made() + codeShown(auth) + polled(1) + answer(calls("m365.signInPoll")[0], unsettled[i][0], unsettled[i][1]) + auth.cancelLogin() + compare(calls("m365.signOut").length, 1, JSON.stringify(unsettled[i])) + compare(calls("m365.signOut")[0].params, { accountId: root.account, clientId: root.client }) + } + // A poll still in the air at the cancel that then turns out lost is the same. + init() + auth = made() + codeShown(auth) + polled(1) + auth.cancelLogin() + answer(calls("m365.signInPoll")[0], null, "request_timed_out") + compare(calls("m365.signOut").length, 1) + // A token request that itself timed out is over and saved nothing. + init() + auth = made() + codeShown(auth) + polled(1) + answer(calls("m365.signInPoll")[0], { state: "pending", warning: "m365_timeout" }) + auth.cancelLogin() + compare(calls("m365.signOut").length, 0, "nothing was saved, so nothing is removed") + } + + // ------------------------------------------------------------- the check + + function test_a_check_that_fails_leaves_nothing_signed_in_behind() { + var cases = [["m365_access_denied", true], ["m365_consent_required", true], ["m365_network_failed", false], + ["m365_timeout", false], [undefined, true]] + for (var i = 0; i < cases.length; i++) { + init() + var auth = made() + codeShown(auth, { state: "signedIn" }) + if (cases[i][0] === undefined) answer(calls("m365.verify")[0], { ok: false }) + else answer(calls("m365.verify")[0], null, cases[i][0]) + compare(auth.loggedIn, false, String(cases[i][0])) + compare(auth.loginBusy, false) + verify(auth.lastError !== "") + compare(calls("m365.signOut").length, cases[i][1] ? 1 : 0, + "a failure that stays removes the sign-in; the network failing leaves it") + auth.reset() + } + } + + function test_a_check_the_network_failed_right_after_the_sign_in_is_asked_again_with_no_new_code() { + var auth = made() + codeShown(auth, { state: "signedIn" }) + answer(calls("m365.verify")[0], null, "m365_network_failed") + compare(auth.loginBusy, false) + compare(auth.recoveringSession, true, "reconnecting, not asking for a code") + compare(auth.lastError, "Microsoft 365 could not be reached. Check your connection") + tryVerify(function() { return calls("m365.verify").length === 2 }, 2000, "the check is asked again") + answer(calls("m365.verify")[1], mailbox) + compare(auth.loggedIn, true) + compare(auth.lastError, "") + compare(calls("m365.signInBegin").length, 1, "and the code was asked for once") + compare(calls("m365.signOut").length, 0) + } + + function test_a_backend_that_cannot_be_asked_is_told_so_and_sent_nothing() { + var auth = made() + backendStub.apiVersion = 7 + auth.beginLogin() + compare(auth.lastError, "Update the mail backend to use this mailbox") + backendStub.apiVersion = 8 + backendStub.ready = false + auth.beginLogin() + compare(auth.lastError, "Mail backend is unavailable") + auth.clientId = "not a guid" + backendStub.ready = true + auth.beginLogin() + compare(auth.lastError, "Add this mailbox's Application (client) ID first") + compare(auth.credentialsPresent, false) + compare(backendStub.calls.length, 0) + compare(auth.loginBusy, false) + } + + // ----------------------------------------------------------- saved session + + function test_a_saved_session_is_restored_by_the_check_alone() { + var auth = built() + auth.restoreSession() + compare(calls("m365.verify")[0].params, { accountId: root.account }) + compare(auth.recoveringSession, true, "reconnecting, not signed out") + compare(auth.sessionBusy, true) + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, true) + compare(auth.recoveringSession, false) + compare(calls("m365.signInBegin").length + platformStub.opened.length, 0, "no code and no browser for a restore") + } + + function test_a_restore_that_finds_nothing_is_quiet_and_one_that_fails_says_why() { + // What the check says, whether the mailbox still waits for a session, the sentence. + var cases = [["m365_signed_out", false, ""], ["auth_account_missing", false, ""], + ["m365_consent_required", false, "administrator"], [{ ok: false }, false, "not what Omamail expected"], + ["m365_network_failed", true, "could not be reached"]] + for (var i = 0; i < cases.length; i++) { + init() + var auth = built() + auth.restoreSession() + if (typeof cases[i][0] === "string") answer(calls("m365.verify")[0], null, cases[i][0]) + else answer(calls("m365.verify")[0], cases[i][0]) + compare(auth.recoveringSession, cases[i][1], JSON.stringify(cases[i][0])) + verify(cases[i][2] === "" ? auth.lastError === "" : auth.lastError.indexOf(cases[i][2]) >= 0, auth.lastError) + compare(unavailable.count, cases[i][2] === "" ? 0 : 1) + compare(auth.loggedIn, false) + wait(120) + compare(calls("m365.verify").length, cases[i][1] ? 2 : 1, "only a network that is down is asked again") + compare(calls("m365.signOut").length, 0, "and no session is thrown away for it") + } + } + + function test_a_locked_keyring_at_restore_is_waited_out_with_no_new_code() { + var auth = built() + auth.restoreSession() + answer(calls("m365.verify")[0], null, "auth_keyring_failed") + compare(auth.recoveringSession, true, "the saved sign-in is still there, behind the lock") + verify(auth.lastError.indexOf("Unlock") >= 0 && auth.lastError.indexOf("could not be saved") < 0, auth.lastError) + tryVerify(function() { return calls("m365.verify").length === 2 }, 2000, "asked again on the retry schedule") + answer(calls("m365.verify")[1], mailbox) + compare(auth.loggedIn, true) + compare(calls("m365.signInBegin").length + calls("m365.signOut").length, 0, "no new code, nothing removed") + } + + function test_the_restore_waits_for_the_backend_and_an_old_one_is_not_waited_for() { + backendStub.ready = false + var auth = built() + auth.restoreSession() + wait(50) + compare(backendStub.calls.length, 0) + compare(auth.recoveringSession, true, "owed, so not shown as signed out") + compare(unavailable.count, 0) + backendStub.ready = true + tryVerify(function() { return calls("m365.verify").length === 1 }, 2000, "asked when the backend comes up") + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, true) + + var old = built() + backendStub.calls = [] + backendStub.apiVersion = 7 + old.restoreSession() + compare(calls("m365.verify").length, 0) + compare(old.recoveringSession, false) + compare(old.lastError, "Update the mail backend to use this mailbox") + compare(unavailable.count, 1) + } + + function test_a_press_while_the_restore_is_out_wins_and_the_restore_is_let_go_of() { + var auth = built() + auth.restoreSession() + auth.beginLogin() + compare(auth.sessionBusy, false) + answer(calls("m365.verify")[0], mailbox) + compare(auth.loggedIn, false, "the answer of the restore is dropped") + answer(calls("m365.signInBegin")[0], begin()) + compare(auth.signInStage, "waiting") + } + + // ---------------------------------------------------------------- signing out + + function test_signing_out_removes_the_saved_sign_in_and_tells_the_account() { + var auth = built() + auth.restoreSession() + answer(calls("m365.verify")[0], mailbox) + auth.logout() + compare(calls("m365.signOut")[0].params, { accountId: root.account, clientId: root.client }) + compare(auth.loggedIn, false) + compare(auth.checked, null) + compare(loggedOut.count, 1) + answer(calls("m365.signOut")[0], null, "auth_keyring_failed") + verify(auth.lastError.indexOf("could not be removed") >= 0 && auth.lastError.indexOf("keyring") >= 0, auth.lastError) + auth.restoreSession() + compare(calls("m365.verify").length, 1, "a signed-out mailbox is not restored") + } + + // A refresh that holds the sign-in (a keyring waiting to be unlocked) outlasts the backend's + // wait: the sign-out may not have happened, and the window does not take it as done. + function test_a_sign_out_that_could_not_wait_for_the_sign_in_says_it_was_not_completed() { + var auth = built() + auth.restoreSession() + answer(calls("m365.verify")[0], mailbox) + auth.logout() + answer(calls("m365.signOut")[0], null, "m365_timeout") + verify(auth.lastError.indexOf("could not be completed") >= 0 + && auth.lastError.indexOf("may still be saved") >= 0, auth.lastError) + compare(auth.loggedIn, false) + } + + function test_signing_out_deletes_a_saved_sign_in_even_when_none_was_proved() { + var auth = made() + auth.logout() + compare(calls("m365.signOut").length, 1, "a mailbox added again under the same address starts clean") + } + + function test_removal_waits_for_confirmed_cleanup_and_retains_the_account_on_failure() { + var auth = made() + var results = [] + backendStub.ready = false + auth.forgetCredential(function(ok) { results.push(ok) }) + compare(results, [false]) + compare(auth.removalBusy, false) + compare(calls("m365.signOut").length, 0) + verify(auth.lastError.indexOf("not removed") >= 0) + backendStub.ready = true + auth.forgetCredential(function(ok) { results.push(ok) }) + compare(auth.removalBusy, true) + compare(results, [false], "no completion before the keyring result") + auth.beginLogin() + compare(calls("m365.signInBegin").length, 0, "no newer grant during removal") + answer(calls("m365.signOut")[0], null, "auth_keyring_failed") + compare(results, [false, false]) + compare(auth.removalBusy, false) + verify(auth.lastError.indexOf("not removed") >= 0 && auth.lastError.indexOf("keyring") >= 0) + auth.forgetCredential(function(ok) { results.push(ok) }) + answer(calls("m365.signOut")[1], { signedOut: true }) + compare(results, [false, false, true]) + compare(auth.removalBusy, false) + compare(auth.loggedIn, false) + } + + function test_removal_checks_every_client_and_refuses_an_unconfirmed_reply() { + var auth = made() + var oldClient = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" + auth.signInClients = [{ accountId: root.account, clientId: oldClient }, + { accountId: root.account, clientId: root.client }] + var result = null + auth.forgetCredential(function(ok) { result = ok }) + compare(calls("m365.signOut").length, 2, "one deletion per app ID") + answer(calls("m365.signOut")[0], { signedOut: true }) + compare(result, null, "the older app's grant is also awaited") + answer(calls("m365.signOut")[1], {}) + compare(result, false, "a missing success flag cannot remove the account") + compare(auth.removalBusy, false) + } + + function test_an_application_id_in_capitals_is_sent_as_the_backend_keys_it() { + var auth = built() + auth.clientId = "ABCDEF11-2222-3333-4444-55555555555F" + compare(auth.effectiveClientId, "abcdef11-2222-3333-4444-55555555555f") + auth.logout() + compare(calls("m365.signOut")[0].params.clientId, "abcdef11-2222-3333-4444-55555555555f") + } + + function test_a_request_that_found_the_sign_in_gone_signs_the_window_out_once() { + var auth = built() + auth.restoreSession() + answer(calls("m365.verify")[0], mailbox) + auth.noteSignedOut() + compare(auth.loggedIn, false) + compare(auth.checked, null) + compare(auth.lastError, "Sign in to Microsoft 365 again") + compare(unavailable.count, 1) + auth.noteSignedOut() + compare(unavailable.count, 1, "told once") + } + + function test_another_mailbox_drops_what_was_in_the_air_and_has_its_own_session_to_restore() { + var auth = made() + codeShown(auth) + polled(1) + auth.accountId = "m365:bob@corp.example.test" + compare(auth.loginBusy, false) + compare(auth.userCode, "") + answer(calls("m365.signInPoll")[0], { state: "signedIn" }) + wait(100) + compare(auth.loggedIn, false) + tryVerify(function() { return calls("m365.verify").length >= 1 }, 2000, "the new mailbox's session is looked for") + compare(calls("m365.verify")[0].params, { accountId: "m365:bob@corp.example.test" }) + } + } +} diff --git a/ui/tests/qml/tst_m365_client.qml b/ui/tests/qml/tst_m365_client.qml new file mode 100644 index 0000000..dc9ff06 --- /dev/null +++ b/ui/tests/qml/tst_m365_client.qml @@ -0,0 +1,306 @@ +import QtQuick +import QtTest +import "../../providers" as Providers + +// What `MailAccount` asks of a Microsoft 365 mailbox and what comes back: each call +// is one `m365.*` request that names the account and nothing else, the answer is in +// the shape the account reads, and a request is cancelled, dropped or refused as the +// session around it changes. The backend is a recorder answered by hand. +Item { + id: root + width: 200 + height: 200 + + readonly property string account: "m365:ada@corp.example.test" + + QtObject { + id: authStub + property string accountId: root.account + property bool loggedIn: true + property bool restoring: false + property string signInStage: "" + property var checked: null + property int signedOutNotes: 0 + signal loggedOut() + function noteSignedOut() { signedOutNotes++ } + } + + QtObject { + id: backendStub + property bool ready: true + property int apiVersion: 8 + property var calls: [] + function call(method, params, callback) { + calls = calls.concat([{ method: method, params: JSON.parse(JSON.stringify(params)), callback: callback, answered: false }]) + } + } + + Component { + id: clientFactory + Providers.M365Client { auth: authStub; backend: backendStub; email: "ada@corp.example.test" } + } + + TestCase { + name: "M365Client" + when: windowShown + + property var client: null + property var got: null + + function init() { + backendStub.calls = [] + backendStub.ready = true + backendStub.apiVersion = 8 + authStub.accountId = root.account + authStub.loggedIn = true + authStub.restoring = false + authStub.signInStage = "" + authStub.checked = null + authStub.signedOutNotes = 0 + client = createTemporaryObject(clientFactory, root) + got = null + } + + function calls(method) { return backendStub.calls.filter(function(call) { return call.method === method }) } + function last() { return backendStub.calls[backendStub.calls.length - 1] } + + function answer(call, result, code) { + verify(!call.answered, "answered twice") + call.answered = true + call.callback(result === undefined ? null : result, code === undefined ? null : { code: -32000, message: code }) + } + + function take(result, error, failed) { got = { result: result, error: error, failed: failed } } + + function test_a_listing_asks_for_a_page_and_answers_in_the_shape_the_account_reads() { + client.listMessages("role:inbox", 25, "next-1", take) + compare(last().method, "m365.list") + compare(Object.keys(last().params).sort(), ["accountId", "limit", "pageToken", "query", "requestToken"]) + compare(last().params.accountId, root.account) + compare(last().params.query, "role:inbox") + compare(last().params.pageToken, "next-1", "a token goes back as it came") + answer(last(), { page: { ids: ["a", "b"], nextPageToken: "more", estimate: 40 } }) + compare(got.result, { ids: ["a", "b"], threadIds: [], nextPageToken: "more", estimate: 40 }) + compare(got.error, "") + compare(client.busy, false) + for (var limit of [[500, 100], [0, 25], ["x", 25], [3, 3]]) { + client.listMessages("role:inbox", limit[0], "", take) + compare(last().params.limit, limit[1], "limit " + limit[0]) + } + } + + function test_a_listing_with_nothing_usable_is_an_error_and_a_warning_travels_beside_the_rows() { + client.listMessages("role:inbox", 25, "", take) + answer(last(), { page: null }) + compare(got.result, null) + compare(got.error, "Microsoft 365 could not complete this request", "not an empty mailbox") + client.listMessages("role:inbox", 25, "", take) + answer(last(), { page: { ids: ["a"], nextPageToken: "", estimate: 1 }, warning: "mail_list_incomplete" }) + compare(got.result.ids, ["a"]) + compare(got.error, "The mailbox search could not be completed") + client.listMessages("text:\"", 25, "", take) + answer(last(), null, "invalid_params") + compare(got.error, "That search has nothing Microsoft 365 can look for") + } + + function test_rows_are_read_by_id_and_a_partial_answer_is_said_to_be() { + client.getMessages(["a", "b"], false, take) + compare(Object.keys(last().params).sort(), ["accountId", "full", "ids", "requestToken"]) + compare(last().params.ids, ["a", "b"]) + compare(last().params.full, false) + answer(last(), { messages: [{ id: "a" }, { id: "b" }] }) + compare(got.result.length, 2) + compare(got.error, "") + client.getMessages(["a"], true, take) + compare(last().params.full, true) + answer(last(), { messages: [{ id: "a" }], continuation: "more" }) + compare(got.error, "Some messages could not be loaded") + var before = backendStub.calls.length + client.getMessages([], false, take) + tryCompare(client, "busy", false) + wait(20) + compare(backendStub.calls.length, before, "nothing to read, nothing asked") + compare(got.result, []) + } + + function test_an_attachment_is_asked_for_by_message_and_part() { + client.getAttachment("m1", "p1", take) + compare(last().method, "m365.attachment") + compare(last().params, { accountId: root.account, messageId: "m1", attachmentId: "p1" }) + answer(last(), { data: "QUJD" }) + compare(got.result, "QUJD") + client.getAttachment("m1", "p1", take) + answer(last(), null, "m365_attachment_not_found") + compare(got.result, "") + compare(got.error, "That attachment is no longer in the mailbox") + } + + function test_changes_ask_for_the_labels_the_account_means_and_name_what_failed() { + client.modifyMessage("one", ["STARRED"], [], take) + compare(last().method, "m365.modify") + compare(last().params, { accountId: root.account, ids: ["one"], addLabelIds: ["STARRED"], removeLabelIds: [] }) + answer(last(), { succeededIds: ["one"], failedIds: [], error: null }) + compare(got.error, "") + client.batchModify(["a", "b"], [], ["INBOX"], take) + compare(last().params.ids, ["a", "b"]) + compare(last().params.removeLabelIds, ["INBOX"], "archive is taking the Inbox label off") + answer(last(), { succeededIds: ["a"], failedIds: ["b"], error: "m365_throttled" }) + compare(got.error, "Microsoft asked Omamail to slow down. Try again in a moment") + compare(got.failed, ["b"], "the list restores exactly the rows that failed") + client.trashMessage("c", take) + compare(last().method, "m365.trash") + compare(last().params, { accountId: root.account, ids: ["c"] }) + answer(last(), { succeededIds: [], failedIds: ["c"], error: null }) + compare(got.error, "Microsoft 365 did not apply this change to every message") + client.untrashMessage(["c", "d"], take) + compare(last().method, "m365.untrash") + compare(last().params, { accountId: root.account, ids: ["c", "d"] }) + answer(last(), { succeededIds: [], failedIds: [], error: "m365_message_not_found" }) + compare(got.failed, undefined, "a failure naming no ids is the whole request's") + compare(got.error, "That message is no longer in the mailbox") + } + + function test_a_message_is_sent_as_the_raw_mime_and_nothing_else() { + client.sendMessage({ raw: "UkFX", to: "x@example.test", extra: 1 }, take) + compare(last().method, "m365.send") + compare(last().params, { accountId: root.account, raw: "UkFX" }) + } + + function test_a_draft_is_saved_and_deleted_on_the_mailbox() { + client.saveDraft({ raw: "UkFX", draftId: "old", to: "x@example.test" }, take) + compare(last().method, "m365.saveDraft") + compare(last().params, { accountId: root.account, raw: "UkFX", draftId: "old" }) + answer(last(), { saved: true, warning: "", id: "new" }) + compare(got.result.saved, true) + compare(got.error, "") + client.saveDraft({ raw: "UkFX" }, take) + compare(last().params.draftId, "", "a draft that replaces nothing names none") + client.deleteDraft("old", take) + compare(last().method, "m365.deleteDraft") + compare(last().params, { accountId: root.account, id: "old" }) + answer(last(), null, "m365_not_a_draft") + compare(got.error, "That message is not a draft, so it was left in place") + } + + function test_a_request_made_while_the_sign_in_is_checked_waits_for_the_check() { + authStub.loggedIn = false + authStub.restoring = true + client.listMessages("role:inbox", 25, "", take) + compare(backendStub.calls.length, 0, "nothing is asked of a mailbox being proved") + compare(got, null, "and nothing answered yet") + authStub.restoring = false + authStub.loggedIn = true + tryVerify(function() { return calls("m365.list").length === 1 }, 1000, "sent once the check passed") + answer(last(), { page: { ids: ["a"], nextPageToken: "", estimate: 1 } }) + compare(got.result.ids, ["a"]) + // A check that ends without a mailbox answers what waited for it as signed out. + got = null + authStub.loggedIn = false + authStub.signInStage = "checking" + client.trashMessage("a", take) + authStub.signInStage = "" + tryVerify(function() { return got !== null }, 1000) + compare(got.error, "Sign in to Microsoft 365 again") + compare(calls("m365.trash").length, 0) + // One that waited for another mailbox's check is never sent for the next one. + got = null + authStub.restoring = true + client.trashMessage("b", take) + authStub.accountId = "m365:bob@corp.example.test" + authStub.restoring = false + authStub.loggedIn = true + tryVerify(function() { return got !== null }, 1000) + compare(got.error, "Sign in to Microsoft 365 again") + compare(calls("m365.trash").length, 0) + } + + function test_what_needs_no_request_still_answers() { + client.getProfile(take) + tryVerify(function() { return got !== null && got.result.email === "ada@corp.example.test" }) + compare(got.result.messagesTotal, 0) + client.getSendAs(take) + tryVerify(function() { return got.result.length === 1 && got.result[0].email === "ada@corp.example.test" }) + client.getLabels(take) + compare(got.result, []) + client.getSummaries(["a"], take) + tryVerify(function() { return got.result.length === 0 && got.error === "" }) + compare(backendStub.calls.length, 0, "none of them is a request") + } + + // ---------------------------------------------------- cancelling and dropping + + function test_each_request_of_a_search_is_cancelled_by_its_own_token_and_a_finished_one_is_not() { + var handle = client.listMessages("text:x", 25, "", take) + answer(last(), { page: { ids: ["a"], nextPageToken: "", estimate: 1 } }) + client.getMessages(["a"], false, take, handle) + var read = last() + client.getMessages(["b"], false, take, handle) + var second = last() + verify(read.params.requestToken !== second.params.requestToken) + answer(second, { messages: [] }) + client.abortRequest(handle) + compare(calls("m365.cancel").length, 1, "the finished requests are not cancelled") + compare(calls("m365.cancel")[0].params, { accountId: root.account, requestToken: read.params.requestToken }) + var before = got + answer(read, { messages: [{ id: "a" }] }) + compare(got, before, "the answer of a cancelled request is dropped") + client.abortRequest(handle) + compare(calls("m365.cancel").length, 1, "once") + } + + function test_a_reply_after_the_account_or_the_session_changed_is_dropped() { + client.listMessages("role:inbox", 25, "", take) + authStub.accountId = "m365:bob@corp.example.test" + answer(last(), { page: { ids: ["a"], nextPageToken: "", estimate: 1 } }) + compare(got, null) + authStub.accountId = root.account + client.listMessages("role:inbox", 25, "", take) + var request = last() + authStub.loggedOut() + answer(request, { page: { ids: ["a"], nextPageToken: "", estimate: 1 } }) + compare(got, null, "nothing of a signed-out session arrives") + compare(calls("m365.invalidate")[0].params, { accountId: root.account }, "and the backend forgets the mailbox") + } + + // ------------------------------------------------------------- the session + + function test_nothing_is_asked_without_a_session_or_of_a_backend_too_old() { + authStub.loggedIn = false + client.listMessages("role:inbox", 25, "", take) + compare(got.error, "Sign in to Microsoft 365 again") + authStub.loggedIn = true + backendStub.apiVersion = 7 + client.trashMessage("a", take) + compare(got.error, "Update the mail backend to use this mailbox") + backendStub.apiVersion = 8 + backendStub.ready = false + client.trashMessage("a", take) + compare(got.error, "Mail backend is unavailable") + compare(backendStub.calls.length, 0) + } + + function test_a_request_that_finds_the_sign_in_gone_tells_the_auth_object_and_the_caller() { + client.listMessages("role:inbox", 25, "", take) + answer(last(), null, "m365_signed_out") + compare(authStub.signedOutNotes, 1) + compare(got.error, "Sign in to Microsoft 365 again") + client.batchModify(["a"], [], [], take) + answer(last(), { succeededIds: [], failedIds: ["a"], error: "m365_signed_out" }) + compare(authStub.signedOutNotes, 2, "also when it is a change's own answer") + client.listMessages("role:inbox", 25, "", take) + answer(last(), null, "m365_network_failed") + compare(authStub.signedOutNotes, 2, "a network that is down does not end a session") + } + + function test_what_the_check_found_draws_the_rail_and_the_buttons() { + compare(client.refusals, null) + compare(client.absentMailboxes, null, "every row until a check has answered") + authStub.checked = { ok: true, roles: { archive: null, junk: { total: 0, unread: 0 }, trash: null }, refusals: { star: "" } } + compare(client.absentMailboxes, ["archive", "trash"]) + compare(Object.keys(client.refusals), ["star"]) + authStub.checked = null + compare(client.absentMailboxes, null, "forgotten with the session") + compare(client.refusals, null) + } + } +} diff --git a/ui/tests/qml/tst_m365_sign_in.qml b/ui/tests/qml/tst_m365_sign_in.qml new file mode 100644 index 0000000..43756ea --- /dev/null +++ b/ui/tests/qml/tst_m365_sign_in.qml @@ -0,0 +1,741 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../../" as Omamail +import "BackendFixture.js" as BackendFixture +import "NativeIntentFixture.js" as NativeIntentFixture +import "../../account/Accounts.js" as Accounts +import "../../message/Message.js" as Message + +// A Microsoft 365 mailbox from the first run to a window with mail in it, through +// the real objects on purpose: the setup page, the service's account list, the +// account, its auth object and its client. Each can be right while the value goes +// nowhere. The backend is the fixture of `BackendFixture.js`: the requests the +// mailbox makes are written to its pipe and answered here by hand, so the code, its +// waits, its refusals and the first reads are the test's to give and nothing +// touches Microsoft, the keyring or the disk. +Item { + width: 900 + height: 600 + + QtObject { + id: fakeShell + function hide(_id) {} + } + + Omamail.Service { + id: mailService + shell: fakeShell + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-test" }) + } + + Omamail.App { + id: app + service: mailService + shell: fakeShell + } + + TestCase { + name: "M365SignIn" + when: windowShown + + property var fixture: null + // How many requests the shell had written when the test began, so a test + // reads its own and not those of the tests before it. + property int mark: 0 + property var opened: [] + + readonly property var plainAnswers: ({ + "credentials.get": { found: false }, + "credentials.put": { stored: true }, + "credentials.delete": { deleted: true } + }) + + readonly property string accountId: "m365:jane@corp.example.test" + readonly property string address: "jane@corp.example.test" + readonly property string client: "11111111-2222-3333-4444-555555555555" + readonly property string page: "https://microsoft.com/devicelogin" + + // What the check says for a mailbox with every folder but a Junk one. + readonly property var checked: ({ + ok: true, email: "jane@corp.example.test", displayName: "", scheme: "oauth", serverVersion: "", + roles: { inbox: { total: 4, unread: 2 }, sent: { total: 1, unread: 0 }, drafts: { total: 0, unread: 0 }, + trash: { total: 0, unread: 0 }, junk: null, archive: { total: 9, unread: 0 } }, + refusals: {}, learned: {} + }) + + function initTestCase() { + fixture = BackendFixture.markReady(mailService, 8) + fixture.answers = plainAnswers + NativeIntentFixture.install(mailService) + mailService.platform = { openExternal: function(url) { opened = opened.concat([url]); return true } } + } + + function named(item, objectName) { + if (!item) return null + if (item.objectName === objectName) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = named(values[i], objectName) + if (found) return found + } + return null + } + + // Every item of the name, in the order they are drawn. + function namedAll(item, objectName, found) { + found = found || [] + if (!item) return found + if (item.objectName === objectName) found.push(item) + var values = item.children || [] + for (var i = 0; i < values.length; i++) namedAll(values[i], objectName, found) + return found + } + + function init() { + mark = fixture.requests.length + opened = [] + app.opened = true + mailService.accountList = ({ version: 1, accounts: [], activeId: "" }) + wait(0) + } + + function cleanup() { + mailService.accountList = ({ version: 1, accounts: [], activeId: "" }) + wait(0) + mailService.recount() + BackendFixture.markReady(mailService, 8) + fixture.answers = plainAnswers + mailService.backend.pending = ({}) + } + + // ---------------------------------------------------- the scripted backend + + function asked(method) { + return fixture.requests.slice(mark).filter(function(item) { return item.method === method }) + } + + function waiting(method, query) { + return asked(method).filter(function(item) { return !item.answeredHere + && (query === undefined || item.params.query === query) }) + } + + function reply(request, result) { BackendFixture.respond(mailService, request, result) } + + function refuse(request, code) { + BackendFixture.respond(mailService, request, null, { code: -32000, message: code }) + } + + // The restore every account is owed when it is built, answered "nothing saved". + function answerRestore() { + tryVerify(function() { return waiting("m365.verify").length >= 1 }, 2000, "the restore is asked") + waiting("m365.verify").forEach(function(item) { item.answeredHere = true; refuse(item, "m365_signed_out") }) + } + + function answerList(request, ids, estimate) { + request.answeredHere = true + reply(request, { page: { ids: ids, threadIds: [], nextPageToken: "", estimate: estimate }, rows: [], warning: null }) + } + + // Every listing the mailbox asks for is answered with an empty page, until it + // asks for no more: the first read leads to a second as the counts come in. + function settleLists() { + for (var round = 0; round < 12; round++) { + wait(60) + var pending = waiting("m365.list") + if (pending.length === 0) return + pending.forEach(function(item) { answerList(item, [], 0) }) + } + fail("the mailbox keeps asking for lists") + } + + // A keyring that keeps what it is given, and a mailbox the backend polls for. + function useBackend() { + fixture.answers = Object.assign({}, plainAnswers, { + "mail.unwatch": {}, + "mail.watch": function(params) { + return { accountId: params.accountId, sequence: 1, estimate: 0, messages: [], error: "synthetic" } + } + }) + } + + // --------------------------------------------------------------- the window + + // First run, the way a person meets it: the chooser, then the Microsoft 365 + // page. The sign-in is not yet pressed; `typed` says what was filled in. + function showChooser() { + mailService.accountsLoaded = true + mailService.accountList = ({ version: 1, accounts: [{ id: "", email: "", provider: "gmail", clientId: "", + clientSecret: "", pending: true }], activeId: "" }) + tryCompare(mailService, "accountCount", 1) + tryVerify(function() { return mailService.current !== null }, 1000) + wait(0) + tryCompare(app, "anyReady", false) + app.resetNavigation() + waitForRendering(app) + var loader = named(app, "setup-page") + verify(!!loader.item && typeof loader.item.chosen === "function", "first run opens on the chooser") + return loader + } + + function openForm(typed) { + var loader = showChooser() + loader.item.chosen("m365") + waitForRendering(app) + compare(loader.kind, "m365", "choosing Microsoft 365 opens its page") + var account = mailService.current + tryCompare(account, "providerId", "m365") + tryVerify(function() { return !!account.auth && !!account.api }, 1000) + if (typed !== false) { + named(app, "m365-address-field").text = address + named(app, "m365-client-id-field").text = client + } + return account + } + + function pressSignIn(account) { + named(app, "setup-page").item.signIn() + tryCompare(account, "accountId", accountId) + tryVerify(function() { return asked("m365.signInBegin").length === 1 }, 2000, "sign-in asks for a code") + return asked("m365.signInBegin")[0] + } + + function begin(overrides) { + return Object.assign({ requestId: "req-1", userCode: "ABCD1234E", verificationUri: page, + expiresIn: 900, interval: 5 }, overrides || {}) + } + + function polled(count) { + tryVerify(function() { return asked("m365.signInPoll").length >= count }, 2000, "poll number " + count) + } + + // One sign-in, answered, and the mailbox begun on: what the tests below start from. + function signedInMailbox(verdict) { + useBackend() + var account = openForm() + var begun = pressSignIn(account) + account.auth.timeScale = 0.01 + answerRestore() + reply(begun, begin()) + polled(1) + reply(asked("m365.signInPoll")[0], { state: "signedIn" }) + tryVerify(function() { return asked("m365.verify").length === 2 }, 2000, "the mailbox is checked") + reply(asked("m365.verify")[1], verdict || checked) + tryVerify(function() { return account.ready }, 2000, "the mailbox is ready") + tryVerify(function() { return asked("m365.list").length >= 2 }, 2000, "the mailbox is read") + return account + } + + // A mailbox saved before, as a restart finds it. + function savedMailbox() { + var list = Accounts.add(Accounts.emptyList(), { email: address, provider: "m365", clientId: client }) + mailService.activeIndex = -1 + mailService.accountList = Accounts.setActive(list, accountId) + mailService.accountsLoaded = true + mailService.refreshCurrent() + tryVerify(function() { return mailService.current !== null && !!mailService.current.auth && !!mailService.current.api }, 2000) + tryVerify(function() { return asked("m365.verify").length === 1 }, 2000, "the saved sign-in is checked") + compare(asked("m365.verify")[0].params, { accountId: accountId }) + return mailService.current + } + + // ------------------------------------------------------------- the sign-in + + function test_the_page_asks_for_an_address_and_an_application_id_and_says_what_it_does() { + openForm(false) + verify(named(app, "m365-address-field").visible) + verify(named(app, "m365-client-id-field").visible, "there is no built-in registration, so it is asked for") + verify(named(app, "m365-sign-in").visible) + compare(named(app, "m365-secret-field"), null, "and no password anywhere") + compare(named(app, "m365-sign-in").enabled, false, "nothing to press before both are filled") + var asks = named(app, "m365-asks").text + verify(asks.indexOf("read and write mail, send mail, read and write your calendar") >= 0, asks) + verify(asks.indexOf("one sign-in") >= 0 && asks.indexOf("no second code") >= 0, asks) + compare(named(app, "m365-code-card").visible, false, "no hint about the browser's account before a code is out") + var steps = namedAll(app, "m365-step").map(function(item) { return item.text }) + compare(steps.length, 4, "the registration is a list of four steps") + for (var n = 0; n < steps.length; n++) verify(steps[n].indexOf((n + 1) + ". ") === 0, steps[n]) + var help = steps.join(" ") + for (var word of ["your organization's directory", "no redirect URI", "Allow public client flows", "Mail.ReadWrite", + "Mail.Send", "Calendars.ReadWrite", "openid", "profile", "email", "offline_access", "Application (client) ID", + "Overview page into the field above"]) + verify(help.indexOf(word) >= 0, "the registration steps name " + word + ": " + help) + verify(asks.indexOf("nothing else") < 0, "the sign-in asks for more than mail and calendar: " + asks) + verify(named(app, "m365-admin-note").text.indexOf("administrator has to approve") >= 0) + named(app, "m365-address-field").text = address + named(app, "m365-client-id-field").text = client + compare(named(app, "m365-sign-in").enabled, true) + } + + function test_the_chooser_says_in_one_line_when_this_is_the_mailbox_to_choose() { + var found = [] + function walk(item) { + if (!item) return + if (typeof item.text === "string" && item.text.indexOf("no IMAP needed") >= 0) found.push(item) + var values = item.children || [] + for (var i = 0; i < values.length; i++) walk(values[i]) + } + walk(showChooser().item) + compare(found.length, 1, "the Microsoft 365 card says it needs no IMAP") + compare(found[0].lineCount, 1, "in one line: " + found[0].text) + } + + function test_an_application_id_typed_in_capitals_is_saved_and_sent_in_lower_case() { + useBackend() + var account = openForm(false) + named(app, "m365-address-field").text = address + named(app, "m365-client-id-field").text = "ABCDEF11-2222-3333-4444-55555555555F" + var begun = pressSignIn(account) + compare(begun.params.clientId, "abcdef11-2222-3333-4444-55555555555f") + compare(mailService.accountList.accounts[0].clientId, "abcdef11-2222-3333-4444-55555555555f") + } + + function test_what_cannot_be_an_address_or_an_id_is_refused_before_anything_is_saved() { + var account = openForm(false) + var cases = [["", client, "Add your work email address"], ["jane", client, "That is not a full email address"], + [address, "", "Add the Application (client) ID from Microsoft Entra"], + [address, "not-a-guid", "That is not a Microsoft Application (client) ID"]] + for (var i = 0; i < cases.length; i++) { + named(app, "m365-address-field").text = cases[i][0] + named(app, "m365-client-id-field").text = cases[i][1] + named(app, "setup-page").item.signIn() + compare(named(app, "m365-error").text, cases[i][2]) + compare(mailService.accountList.accounts[0].email, "", "nothing was written") + compare(asked("m365.signInBegin").length, 0) + } + } + + function test_the_whole_route_from_the_chooser_to_a_window_with_mail_in_it() { + useBackend() + var account = openForm() + var begun = pressSignIn(account) + compare(begun.params, { accountId: accountId, address: address, clientId: client }, "no tenant, no secret") + var entry = mailService.accountList.accounts[0] + compare(entry.provider, "m365") + compare(entry.id, accountId, "the row is the mailbox, not a new one") + compare(entry.email, address) + compare(entry.clientId, client, "the application's name, which is no secret") + compare(entry.clientSecret, "") + var text = JSON.stringify(mailService.accountList) + verify(text.indexOf("token") < 0 && text.indexOf("ABCD1234E") < 0, "nothing that could sign anyone in is saved: " + text) + compare(named(app, "m365-progress").text, "Asking Microsoft for a code") + account.auth.timeScale = 0.01 + answerRestore() + compare(account.auth.lastError, "", "the restore owed at the moment the address was saved is quiet") + + reply(begun, begin()) + tryVerify(function() { return named(app, "m365-code-card").visible }, 2000, "the code card appears") + compare(named(app, "m365-code").text, "ABCD1234E") + compare(named(app, "m365-link").text, page, "and the address, to be copied into a private window") + compare(opened, [page], "the page is opened once") + compare(named(app, "m365-progress").text, "Waiting for you to finish on the Microsoft page") + polled(1) + reply(asked("m365.signInPoll")[0], { state: "pending" }) + polled(2) + compare(asked("m365.verify").length, 1, "only the restore so far: a code not entered proves nothing") + reply(asked("m365.signInPoll")[1], { state: "signedIn" }) + tryVerify(function() { return asked("m365.verify").length === 2 }, 2000) + compare(named(app, "m365-code-card").visible, false, "the used code is gone") + compare(named(app, "m365-progress").text, "Signed in on Microsoft's side. Checking the mailbox") + compare(account.ready, false, "not signed in until the mailbox has answered") + compare(asked("m365.list").length, 0, "and nothing is read before") + reply(asked("m365.verify")[1], checked) + + tryVerify(function() { return account.ready }, 2000, "the mailbox is ready without a restart") + tryCompare(app, "anyReady", true) + verify(app.page !== "setup" && app.page !== "picker", "the window leaves setup for the mailbox") + // The first read: the inbox and the unread count, by role, after the check. + tryVerify(function() { return asked("m365.list").length >= 2 }, 2000, "reading the mailbox begins") + var queries = asked("m365.list").map(function(item) { return item.params.query }) + for (var wanted of ["role:inbox", "role:inbox unseen"]) + verify(queries.indexOf(wanted) >= 0, wanted + " is asked for: " + queries) + verify(fixture.requests.indexOf(asked("m365.list")[0]) > fixture.requests.indexOf(asked("m365.verify")[1]), + "the first read comes after the check that proves the mailbox") + for (var listed of asked("m365.list")) compare(listed.params.accountId, accountId) + var keys = account.mailboxes.map(function(box) { return box.key }) + verify(keys.indexOf("archive") >= 0, "Archive is a row: the mailbox has one") + compare(keys.indexOf("spam"), -1, "a missing Junk folder leaves no row with nothing behind it") + compare(account.canArchive && account.canStar && account.canSend, true) + } + + function test_the_signed_in_page_says_who_and_offers_sign_out() { + signedInMailbox() + mailService.activeIndex = 0 + app.openSettings() + app.editAccount(0) + waitForRendering(app) + compare(app.page, "setup") + compare(named(app, "m365-signed-in").text, "Signed in · " + address) + compare(named(app, "m365-sign-in").visible, false) + compare(named(app, "m365-address-field").visible, false) + compare(named(app, "m365-client-id-field").visible, false) + verify(named(app, "m365-sign-out").visible) + } + + // -------------------------------------------------------------------- failures + + function test_a_sign_in_that_ends_says_so_in_the_errors_place_and_can_be_tried_again() { + useBackend() + var cases = [[{ state: "declined" }, "The sign-in was declined on the Microsoft page. Press Sign in to try again"], + [{ state: "failed", error: "m365_consent_required" }, "administrator has to approve this app"], + [{ state: "failed", error: "m365_wrong_account", signedInAs: "ada@other.example.org" }, "ada@other.example.org"]] + for (var i = 0; i < cases.length; i++) { + init() + var account = openForm() + var begun = pressSignIn(account) + account.auth.timeScale = 0.01 + answerRestore() + reply(begun, begin()) + polled(1) + reply(asked("m365.signInPoll")[0], cases[i][0]) + tryVerify(function() { return !account.auth.loginBusy }, 2000) + verify(named(app, "m365-error").visible) + verify(named(app, "m365-error").text.indexOf(cases[i][1]) >= 0, named(app, "m365-error").text) + compare(named(app, "m365-code-card").visible, false) + compare(account.ready, false) + compare(named(app, "m365-sign-in").enabled, true, "the way to try again is there") + compare(asked("m365.verify").length + asked("m365.signOut").length, 1, "only the restore: nothing checked, nothing removed") + if (i === 0) { + named(app, "m365-sign-in").clicked() + tryVerify(function() { return asked("m365.signInBegin").length === 2 }, 2000) + compare(named(app, "m365-error").text, "", "and the old sentence is gone") + } + cleanup() + } + } + + function test_cancel_stops_the_poll_and_the_page_goes_back_to_the_form() { + useBackend() + var account = openForm() + var begun = pressSignIn(account) + account.auth.timeScale = 0.01 + answerRestore() + reply(begun, begin()) + polled(1) + named(app, "m365-cancel-sign-in").clicked() + tryVerify(function() { return !account.auth.loginBusy }, 2000) + compare(named(app, "m365-code-card").visible, false) + compare(named(app, "m365-error").text, "", "a cancel is not a failure") + compare(named(app, "m365-sign-in").enabled, true) + reply(asked("m365.signInPoll")[0], { state: "signedIn" }) + wait(150) + compare(asked("m365.verify").length, 1, "the code that was cancelled is not proved") + compare(asked("m365.signInPoll").length, 1, "and nothing asks again") + } + + function test_a_backend_not_up_yet_keeps_sign_in_off_and_says_why() { + openForm() + mailService.backend.protocolInfo = null + waitForRendering(app) + verify(named(app, "m365-backend-note").visible) + compare(named(app, "m365-backend-note").text, "Waiting for the mail backend to start. Sign in is available once it has.") + compare(named(app, "m365-sign-in").enabled, false) + compare(asked("m365.signInBegin").length, 0) + } + + function test_an_old_backend_is_told_so_and_sent_nothing() { + mailService.backend.protocolInfo = { apiVersion: 7, protocol: 1, version: "0.0.0" } + openForm() + verify(named(app, "m365-backend-note").visible) + compare(named(app, "m365-backend-note").text, "Update the mail backend first: this version cannot talk to Microsoft 365.") + compare(named(app, "m365-sign-in").enabled, false) + compare(asked("m365.signInBegin").length + asked("m365.verify").length, 0) + } + + // --------------------------------------------------------------------- restart + + function test_a_saved_mailbox_restores_with_one_check_and_no_code() { + useBackend() + var account = savedMailbox() + compare(account.setupState, "reconnecting", "and the window says so rather than 'signed out'") + compare(account.ready, false) + reply(asked("m365.verify")[0], checked) + tryVerify(function() { return account.ready }, 2000) + compare(opened.length + asked("m365.signInBegin").length, 0, "no browser and no code for a restore") + tryVerify(function() { return asked("m365.list").length >= 1 }, 2000, "and the mailbox is read") + } + + function test_a_saved_mailbox_with_no_sign_in_left_asks_for_one_and_says_nothing_wrong() { + var account = savedMailbox() + refuse(asked("m365.verify")[0], "m365_signed_out") + tryCompare(account, "setupState", "signed_out") + compare(account.lastError, "", "a mailbox that was signed out is not an error to report") + compare(asked("m365.signInBegin").length, 0, "no code until it is pressed") + compare(account.setupHeadline, "Sign in to Microsoft 365") + compare(account.setupActionLabel, "Sign in to Microsoft 365...") + } + + // ----------------------------------------------------------- sign out, remove + + function test_signing_out_removes_the_saved_sign_in_and_the_window_shows_it_signed_out() { + var account = signedInMailbox() + mailService.signOut() + tryVerify(function() { return asked("m365.signOut").length === 1 }, 2000) + compare(asked("m365.signOut")[0].params, { accountId: accountId, clientId: client }) + compare(account.auth.loggedIn, false) + compare(account.ready, false) + compare(account.messages.length, 0, "no mail of a signed-out mailbox stays on screen") + tryVerify(function() { return asked("m365.invalidate").length === 1 }, 2000, "the backend forgets what it held") + compare(asked("m365.invalidate")[0].params, { accountId: accountId }) + } + + function test_removing_the_mailbox_deletes_its_sign_in_only_once_the_removal_is_confirmed() { + signedInMailbox() + settleLists() + mailService.accountList = Accounts.add(mailService.accountList, { email: "bob@example.test", provider: "gmail" }) + tryCompare(mailService, "accountCount", 2) + // The list was rebuilt around the new row, and the mailbox restores its sign-in. + tryVerify(function() { return asked("m365.verify").length === 3 }, 2000) + reply(asked("m365.verify")[2], checked) + tryCompare(mailService.findAccount(accountId).auth, "loggedIn", true) + mailService.activeIndex = 0 + app.openSettings() + app.editAccount(0) + waitForRendering(app) + var remove = named(app, "m365-remove") + tryCompare(remove, "visible", true, 1000, "offered beside another mailbox") + remove.clicked() + var dialog = removalDialog(app) + verify(dialog && dialog.request !== null, "the window's removal dialog is asked") + compare(dialog.request.id, accountId, "for this mailbox, not the one beside it") + // Cancelled: nothing is sent and the mailbox stays as it was. + dialog.close() + tryVerify(function() { return dialog.request === null }, 1000) + wait(50) + compare(asked("m365.signOut").length, 0, "a removal that was not confirmed removes no sign-in") + compare(mailService.findAccount(accountId).auth.loggedIn, true, "and the mailbox stays signed in") + compare(mailService.accountList.accounts.filter(function(entry) { return entry.id === accountId }).length, 1) + // Confirmed: the saved sign-in goes with the entry. + remove.clicked() + dialog = removalDialog(app) + app.confirmAccountRemoval(dialog.request) + tryVerify(function() { return asked("m365.signOut").length === 1 }, 2000, "its sign-in is deleted") + compare(asked("m365.signOut")[0].params, { accountId: accountId, clientId: client }) + compare(mailService.accountList.accounts.filter(function(entry) { return entry.id === accountId }).length, 1, + "the account stays until its saved sign-in was removed") + reply(asked("m365.signOut")[0], { signedOut: true }) + tryVerify(function() { return mailService.accountList.accounts.filter(function(entry) { return entry.id === accountId }).length === 0 }, 2000) + } + + function mailboxForRemoval(ready) { + mailService.backend.connected = ready + var list = Accounts.add(Accounts.emptyList(), { email: address, provider: "m365", clientId: client }) + list = Accounts.add(list, { email: "bob@example.test", provider: "gmail" }) + mailService.accountList = Accounts.setActive(list, accountId) + mailService.accountsLoaded = true + mailService.refreshCurrent() + tryVerify(function() { return mailService.findAccount(accountId) !== null && !!mailService.findAccount(accountId).auth }, 2000) + var request = Accounts.removalRequest({ accounts: mailService.accountSummaries }, 0) + compare(request.id, accountId) + return request + } + + function test_removal_before_backend_start_keeps_the_account_until_cleanup_can_be_retried() { + var request = mailboxForRemoval(false) + compare(mailService.backend.ready, false) + app.confirmAccountRemoval(request) + wait(50) + verify(mailService.findAccount(accountId) !== null, "no destroyed delegate with owed credential deletion") + verify(mailService.findAccount(accountId).auth.lastError.indexOf("not removed") >= 0) + compare(asked("m365.signOut").length, 0) + BackendFixture.markReady(mailService, 8) + app.confirmAccountRemoval(request) + tryVerify(function() { return asked("m365.signOut").length === 1 }, 2000) + verify(mailService.findAccount(accountId) !== null, "still present while keyring cleanup is pending") + reply(asked("m365.signOut")[0], { signedOut: true }) + tryVerify(function() { return mailService.findAccount(accountId) === null }, 2000) + } + + function test_failed_removal_keeps_the_account_and_reports_the_keyring_error() { + var request = mailboxForRemoval(true) + app.confirmAccountRemoval(request) + tryVerify(function() { return asked("m365.signOut").length === 1 }, 2000) + refuse(asked("m365.signOut")[0], "auth_keyring_failed") + tryVerify(function() { return mailService.findAccount(accountId).auth.lastError.indexOf("not removed") >= 0 }, 2000) + verify(mailService.findAccount(accountId).auth.lastError.indexOf("keyring") >= 0) + compare(mailService.accountCount, 2) + app.confirmAccountRemoval(request) + tryVerify(function() { return asked("m365.signOut").length === 2 }, 2000) + reply(asked("m365.signOut")[1], { signedOut: true }) + tryVerify(function() { return mailService.findAccount(accountId) === null }, 2000) + } + + function test_late_removal_success_cannot_delete_an_account_replaced_while_waiting() { + var request = mailboxForRemoval(true) + app.confirmAccountRemoval(request) + tryVerify(function() { return asked("m365.signOut").length === 1 }, 2000) + var replacement = JSON.parse(JSON.stringify(mailService.accountList)) + replacement.accounts[0].clientId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" + mailService.accountList = replacement + tryCompare(mailService.findAccount(accountId).auth, "clientId", replacement.accounts[0].clientId) + reply(asked("m365.signOut")[0], { signedOut: true }) + wait(50) + compare(mailService.accountCount, 2) + verify(mailService.findAccount(accountId) !== null, "a stale completion cannot remove the replacement") + } + + function test_late_removal_keeps_newer_navigation_and_its_overlay() { + var request = mailboxForRemoval(true) + app.confirmAccountRemoval(request) + tryVerify(function() { return asked("m365.signOut").length === 1 }, 2000) + app.openHelp() + var newerNav = app.nav + reply(asked("m365.signOut")[0], { signedOut: true }) + tryVerify(function() { return mailService.findAccount(accountId) === null }, 2000) + compare(app.nav, newerNav, "a delayed removal cannot replace a newer page or overlay") + compare(app.overlay, "help") + } + + function test_removal_error_is_visible_after_returning_and_clears_for_retry() { + var request = mailboxForRemoval(true) + app.openSettings() + app.editAccount(0) + waitForRendering(app) + app.confirmAccountRemoval(request) + tryVerify(function() { return asked("m365.signOut").length === 1 }, 2000) + compare(named(app, "m365-remove").enabled, false) + compare(named(app, "m365-sign-in").enabled, false) + compare(named(app, "m365-cancel-sign-in").visible, false) + verify(named(app, "m365-progress").text.indexOf("Removing") >= 0) + refuse(asked("m365.signOut")[0], "auth_store_unavailable") + tryVerify(function() { return named(app, "m365-error").visible }, 2000) + verify(named(app, "m365-error").text.indexOf("not removed") >= 0) + verify(named(app, "m365-remove").enabled) + app.back() + app.editAccount(0) + waitForRendering(app) + verify(named(app, "m365-error").visible, "a recreated form still explains the failed removal") + verify(named(app, "m365-error").text.indexOf("not removed") >= 0) + app.confirmAccountRemoval(request) + tryVerify(function() { return asked("m365.signOut").length === 2 }, 2000) + compare(named(app, "m365-error").text, "", "a retry clears the previous failure") + compare(named(app, "m365-remove").enabled, false) + reply(asked("m365.signOut")[1], { signedOut: true }) + tryVerify(function() { return mailService.findAccount(accountId) === null }, 2000) + } + + // The one removal dialog that was asked: the window has a few dialogs of this shape. + function removalDialog(item) { + var found = [] + function walk(node) { + if (!node) return + if (typeof node.openFor === "function" && node.request !== undefined && node.request !== null) found.push(node) + var values = node.children || [] + for (var i = 0; i < values.length; i++) walk(values[i]) + } + walk(item) + return found.length === 1 ? found[0] : null + } + + function test_a_request_that_finds_the_sign_in_gone_signs_the_window_out() { + var account = signedInMailbox() + settleLists() + tryCompare(account, "countLoading", false) + account.refreshCounts() + tryVerify(function() { return waiting("m365.list").length >= 1 }, 2000) + refuse(waiting("m365.list")[0], "m365_signed_out") + tryCompare(account.auth, "loggedIn", false) + compare(account.auth.lastError, "Sign in to Microsoft 365 again") + tryCompare(account, "setupState", "signed_out") + } + + // ------------------------------------------------------------- the mailbox's own work + + function inboxRow(id, starred, unread) { + return ({ id: id, threadId: "", from: { email: "x@corp.example.test", display: "X" }, subject: id, + snippet: "", time: "", date: "", unread: unread === true, starred: starred, inInbox: true, + labelIds: ["INBOX"].concat(starred ? ["STARRED"] : []).concat(unread === true ? ["UNREAD"] : []) }) + } + + function withRows(account, rows) { + settleLists() + account.messages = rows + account.listLoaded = true + } + + // The mailbox's answer to the change asked for, and the account done with it. + function done(account, method, count, result) { + reply(asked(method)[count - 1], result) + tryCompare(account, "pendingAction", "") + } + + function test_marking_flagging_archiving_and_trashing_ask_for_the_labels_the_mailbox_takes() { + var account = signedInMailbox() + withRows(account, [inboxRow("one", false, true), inboxRow("two", true), inboxRow("three", false), inboxRow("four", false)]) + verify(account.act("one", "markRead", false, false)) + tryVerify(function() { return asked("m365.modify").length === 1 }, 2000) + compare(asked("m365.modify")[0].params, { accountId: accountId, ids: ["one"], addLabelIds: [], removeLabelIds: ["UNREAD"] }) + done(account, "m365.modify", 1, { succeededIds: ["one"], failedIds: [], error: null }) + verify(account.act("three", "star", false, false)) + tryVerify(function() { return asked("m365.modify").length === 2 }, 2000) + compare(asked("m365.modify")[1].params.addLabelIds, ["STARRED"]) + done(account, "m365.modify", 2, { succeededIds: ["three"], failedIds: [], error: null }) + verify(account.act("four", "archive", false, false)) + tryVerify(function() { return asked("m365.modify").length === 3 }, 2000) + compare(asked("m365.modify")[2].params, { accountId: accountId, ids: ["four"], addLabelIds: [], removeLabelIds: ["INBOX"] }, + "archive is taking the Inbox label off, which the backend files in Archive") + verify(account.messages.every(function(row) { return row.id !== "four" }), "and the row leaves the Inbox at once") + done(account, "m365.modify", 3, { succeededIds: ["four"], failedIds: [], error: null }) + verify(account.act("two", "trash", false, false)) + tryVerify(function() { return asked("m365.trash").length === 1 }, 2000) + compare(asked("m365.trash")[0].params, { accountId: accountId, ids: ["two"] }) + done(account, "m365.trash", 1, { succeededIds: ["two"], failedIds: [], error: null }) + } + + function test_a_change_the_mailbox_refused_puts_the_row_back_and_says_why() { + var account = signedInMailbox() + withRows(account, [inboxRow("one", false), inboxRow("two", false)]) + verify(account.act("one", "archive", false, false)) + tryVerify(function() { return asked("m365.modify").length === 1 }, 2000) + reply(asked("m365.modify")[0], { succeededIds: [], failedIds: ["one"], error: "m365_throttled" }) + tryCompare(account, "pendingAction", "") + verify(account.messages.some(function(row) { return row.id === "one" }), "the row is back") + compare(account.lastError, "Microsoft asked Omamail to slow down. Try again in a moment") + } + + function test_the_archive_is_read_by_its_role_a_search_by_its_text_and_moving_back_adds_the_inbox() { + var account = signedInMailbox() + settleLists() + account.selectMailbox("archive") + tryVerify(function() { return waiting("m365.list", "role:archive").length >= 1 }, 2000, "the Archive row opens the folder by its role") + var row = inboxRow("filed", false) + row.inInbox = false + row.labelIds = [] + account.messages = [row] + account.listLoaded = true + verify(account.act("filed", "unarchive", false, false)) + tryVerify(function() { return asked("m365.modify").length === 1 }, 2000) + compare(asked("m365.modify")[0].params.addLabelIds, ["INBOX"], "moving back to the Inbox puts its label on") + compare(asked("m365.modify")[0].params.removeLabelIds, []) + account.search("invoice") + tryVerify(function() { return waiting("m365.list", "text:invoice").length >= 1 }, 2000, "a typed search is the text filter of the mailbox") + } + + // -------------------------------------------------------------------------- send + + function test_a_message_is_queued_for_the_backend_from_this_mailbox_and_a_draft_is_saved_on_it() { + var account = signedInMailbox() + var queued = fixture.requests.filter(function(item) { return item.method === "outbox.enqueue" }).length + var id = account.send({ to: "friend@example.test", body: "Hello", subject: "Hi" }) + verify(id !== false && id !== "", "a send id, not a refusal: " + id) + tryVerify(function() { return fixture.requests.filter(function(item) { return item.method === "outbox.enqueue" }).length > queued }, 2000) + var request = fixture.requests.filter(function(item) { return item.method === "outbox.enqueue" })[queued] + compare(request.params.provider, "m365", "the backend's outbox delivers it through m365.send") + compare(request.params.accountId, accountId) + verify(/(^|\r\n)To: friend@example\.test\r\n/.test(Message.decodeBase64Url(request.params.payload.raw))) + tryVerify(function() { return account.availableSendAsAliases.length > 0 }, 2000) + compare(account.availableSendAsAliases.map(function(alias) { return alias.email }), [address], "the sender is the mailbox address") + compare(asked("m365.sendAs").length, 0, "with no request for it") + var said = null + account.saveDraft({ to: "friend@example.test", body: "x" }, function(saved, error) { said = [saved, error] }) + tryVerify(function() { return asked("m365.saveDraft").length === 1 }, 2000, "Save asks the mailbox to keep it") + var save = asked("m365.saveDraft")[0] + compare(save.params.accountId, accountId) + compare(save.params.draftId, "") + verify(/(^|\r\n)To: friend@example\.test\r\n/.test(Message.decodeBase64Url(save.params.raw))) + reply(save, { saved: true, warning: "", id: "draft-1" }) + tryVerify(function() { return said !== null }, 2000) + compare(said[1], "") + compare(said[0].saved, true) + } + } +} diff --git a/ui/tests/qml/tst_m365_unified.qml b/ui/tests/qml/tst_m365_unified.qml new file mode 100644 index 0000000..4da70cf --- /dev/null +++ b/ui/tests/qml/tst_m365_unified.qml @@ -0,0 +1,104 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../.." as Omamail +import "../../account/Unified.js" as Unified +import "ModelFixture.js" as NativeModel + +// A Microsoft 365 mailbox and an Outlook one in the same window, against a real +// `Service` with real account hosts: the merged list and what the two may offer +// together. The routing of an id to its mailbox is the provider-independent +// `tst_unified_routing`'s. +// +// The hosts are real `MailAccount`s with no credentials, so nothing is fetched. +Item { + id: fixtureRoot + width: 1200 + height: 700 + + QtObject { + id: fakeShell + function updateEntryInline(id, entry) {} + function hide(_id) {} + } + + Omamail.Service { + id: service + shell: fakeShell + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-m365-unified-test" }) + } + + TestCase { + name: "M365Unified" + when: windowShown + + readonly property string janeId: "m365:jane@corp.example.test" + readonly property string bobId: "outlook:bob@example.net" + + function initTestCase() { NativeModel.install(service) } + + function seed(accounts) { + service.applySettings({ unifiedMailboxes: true }) + service.applyAccounts(JSON.stringify({ version: 1, activeId: janeId, accounts: accounts || [ + { id: janeId, email: "jane@corp.example.test", provider: "m365" }, + { id: bobId, email: "bob@example.net", provider: "outlook" }] })) + wait(50) + } + + function jane() { return service.findAccount(janeId) } + function bob() { return service.findAccount(bobId) } + + function row(id, date, subject) { + return { id: id, subject: subject, unread: true, starred: false, inInbox: true, inTrash: false, isDraft: false, + labelIds: ["INBOX"], from: { email: "sender@example.com", display: "Sender" }, snippet: "", time: "now", + fullTime: "today", date: date } + } + + function init() { + seed() + jane().messages = [row("ifau222bi5itcqkbifat2", 3000, "jane newest"), row("same", 1000, "jane oldest")] + bob().messages = [row("same", 2000, "bob middle")] + service.bumpListEpoch() + wait(20) + } + + function cleanup() { service.applySettings({ unifiedMailboxes: true }) } + + function test_the_two_mailboxes_have_their_own_kinds_and_become_one_list_by_date() { + compare(jane().providerId, "m365") + compare(bob().providerId, "outlook") + verify(jane().auth.effectiveClientId !== undefined, "the Microsoft 365 mailbox runs on its own sign-in") + compare(service.unified, true) + compare(service.messages.map(function(m) { return m.subject }).join(" | "), "jane newest | bob middle | jane oldest") + var ids = service.messages.map(function(m) { return m.id }) + compare(ids[2], Unified.unifiedId(janeId, "same"), "one id in two mailboxes is two rows") + compare(service.hostForId(ids[0]), jane()) + compare(service.hostForId(ids[1]), bob()) + } + + function test_a_button_is_offered_only_when_every_mailbox_can_honour_it() { + compare(service.canArchive, true, "both Microsoft kinds can archive") + compare(service.canReportSpam, false, "and neither reports spam") + seed([{ id: janeId, email: "jane@corp.example.test", provider: "m365" }, + { id: "bob@example.net", email: "bob@example.net", provider: "gmail" }]) + compare(service.hasLabels, false, "Gmail has labels and the Microsoft 365 mailbox has none: a merged list has none") + compare(service.canArchive, true) + } + + function test_a_rail_row_reaches_both_mailboxes_each_in_its_own_words() { + service.selectMailbox("sent") + compare(jane().mailboxKey, "sent") + compare(bob().mailboxKey, "sent") + compare(jane().effectiveQuery, "role:sent") + compare(bob().effectiveQuery, "folder:\\Sent") + } + + function test_the_summaries_name_each_mailboxs_provider_and_calendar() { + var summaries = service.accountSummaries + for (var id of [janeId, bobId]) { + var mine = summaries.filter(function(item) { return item.id === id })[0] + compare(mine.calendarProvider, "microsoft", id + " has a Graph calendar") + } + compare(summaries.filter(function(item) { return item.id === janeId })[0].provider, "m365") + } + } +} diff --git a/ui/tests/qml/tst_setup_pages.qml b/ui/tests/qml/tst_setup_pages.qml new file mode 100644 index 0000000..f17451c --- /dev/null +++ b/ui/tests/qml/tst_setup_pages.qml @@ -0,0 +1,94 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../.." as Omamail +import "BackendFixture.js" as BackendFixture +import "../../account/Accounts.js" as Accounts +import "../../providers/Registry.js" as Provider + +// Every kind the chooser offers opens its own page, through the window and +// back. The pages are built in SetupPages, so each signal crosses one more +// object than it did: a kind missing from its map opens Gmail's page instead, +// and a handler written without its call drops Remove on the floor, with every +// page still drawing. +Item { + width: 900 + height: 600 + + QtObject { + id: shellStore + function updateEntryInline(id, entry) {} + function hide(id) {} + } + + Omamail.Service { + id: mailService + shell: shellStore + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-test" }) + } + + Omamail.App { id: app; service: mailService } + + TestCase { + name: "SetupPages" + when: windowShown + + function initTestCase() { BackendFixture.markReady(mailService) } + + function named(item, objectName) { + if (!item) return null + if (item.objectName === objectName) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = named(values[i], objectName) + if (found) return found + } + return null + } + + function seed() { + var list = Accounts.add(Accounts.emptyList(), { email: "ada@example.com", provider: "imap", + clientId: "", clientSecret: "", label: "", signature: "", + imap: { imapHost: "imap.example.com", imapPort: 993, smtpHost: "smtp.example.com", + smtpPort: 465, username: "ada@example.com", aliases: [], insecure: false } }) + mailService.activeIndex = -1 + mailService.accountList = Accounts.setActive(list, "imap:ada@example.com") + mailService.accountsLoaded = true + mailService.refreshCurrent() + } + + // A page is named after its kind; Gmail's, the first, is plain SetupPage. + function pageType(id) { + return id === "gmail" ? "SetupPage" : id.charAt(0).toUpperCase() + id.slice(1) + "SetupPage" + } + + function typeOf(item) { + return String(item).split("_QMLTYPE_")[0].split("(")[0] + } + + function init() { + seed() + app.opened = true + app.resetNavigation() + } + + function test_each_kind_opens_its_own_page_data() { + return Provider.ids().map(function(id) { return { tag: id, id: id } }) + } + + function test_each_kind_opens_its_own_page(data) { + app.addMailbox() + var setup = named(app, "setup-page") + tryVerify(function() { return !!setup.item && typeOf(setup.item) === "ProviderPicker" }) + + setup.item.chosen(data.id) + tryVerify(function() { return !!setup.item && typeOf(setup.item) !== "ProviderPicker" }) + compare(typeOf(setup.item), pageType(data.id), "the chooser's choice reaches the window") + compare(mailService.accountCount, 2, "choosing made the mailbox's row") + if (setup.item.accountCount !== undefined) + compare(setup.item.accountCount, 2, "the page counts the mailboxes there are") + + setup.item.removeRequested() + tryCompare(mailService, "accountCount", 1) + } + } +} diff --git a/ui/tests/test_calendar_feed.js b/ui/tests/test_calendar_feed.js index 9b80c64..5595bee 100644 --- a/ui/tests/test_calendar_feed.js +++ b/ui/tests/test_calendar_feed.js @@ -770,6 +770,12 @@ console.log("test_calendar_feed.js ok") assert.strictEqual(feed.graphResponseError(500, "not json"), "Microsoft Graph answered 500") assert.strictEqual(feed.nativeRequestError("microsoft"), "Microsoft calendar request failed. Check Graph permissions in Settings, then sign in again") + // A Microsoft 365 calendar refused for consent says what the administrator does and what follows, + // and nothing about the sign-in page, which a calendar refresh never shows. + const consent = feed.nativeRequestError("microsoft", "m365:ada@example.org", { message: "auth_consent_required" }) + assert.strictEqual(consent, "Microsoft calendar request failed. An administrator has to approve this app " + + "for your organization. Then sign in to this mailbox again") + assert.ok(!/private window|page|browser/.test(consent), consent) assert.strictEqual(feed.nativeRequestError("caldav"), "CalDAV calendar request failed. Check its server address and password in Settings") assert.strictEqual(feed.nativeRequestError("icloud"), diff --git a/ui/tests/test_m365.js b/ui/tests/test_m365.js new file mode 100644 index 0000000..57c943e --- /dev/null +++ b/ui/tests/test_m365.js @@ -0,0 +1,230 @@ +const assert = require("assert") +const { load, deepEqual } = require("./load") + +const M365 = load("providers/M365Protocol.js") +const Registry = load("providers/Registry.js") +const Accounts = load("account/Accounts.js") +const Model = load("account/Model.js") + +// ------------------------------------------------- what the backend says + +// Every code the backend answers with has a sentence of its own, said plainly: +// no code, protocol, header or status reaches a person. +const CODES = ["auth_account_missing", "auth_keyring_failed", "backend_needs_update", "credential_store_unavailable", + "m365_access_denied", "m365_attachment_not_found", "m365_auth_failed", "m365_bad_response", "m365_cancelled", + "m365_consent_required", "m365_declined", "m365_draft_unconfirmed", "m365_expired", "m365_folder_unavailable", + "m365_invalid_request", + "m365_invalid_response", "m365_message_not_found", "m365_message_too_large", + "m365_network_failed", "m365_not_a_draft", "m365_request_limit", "m365_response_too_large", "m365_send_failed", + "m365_send_not_attempted", "m365_send_rejected", "m365_server_error", "m365_signed_out", "m365_tenant_invalid", + "m365_throttled", "m365_timeout", "m365_tls_failed", "m365_wrong_account", "request_timed_out"] +{ + const seen = new Set() + for (const code of CODES) { + const text = M365.sentence({ message: code }) + assert.ok(text !== M365.GENERIC && text.length > 0, code + " has its own sentence") + assert.strictEqual(M365.sentence(code), text, "a bare string is read like an error's message") + assert.ok(!/m365_|HTTP|\b[45]\d\d\b|IMAP|SMTP|MIME|Graph/.test(text), code + " is said plainly: " + text) + seen.add(code) + } + assert.strictEqual(seen.size, CODES.length) + deepEqual(Object.keys(M365.SENTENCES).sort(), CODES, "no sentence is for a code nobody sends") + for (const odd of ["", "m365_no_such_code", undefined, null, { message: "oops" }, { code: -32000 }, 42, "toString"]) + assert.strictEqual(M365.sentence(odd), M365.GENERIC, "an unknown code is the generic sentence: " + odd) +} +{ + const consent = M365.sentence("m365_consent_required") + assert.ok(/administrator/.test(consent) && /private window/.test(consent), consent) + assert.ok(/Sign in/.test(M365.sentence("m365_declined")) && /Sign in/.test(M365.sentence("m365_expired"))) + assert.ok(/Application \(client\) ID/.test(M365.sentence("m365_auth_failed"))) +} + +// A wrong account names the one Microsoft reported, as bounded plain text. +{ + const named = M365.failureSentence("m365_wrong_account", "ada@other.example.org", "jane@corp.example.test") + assert.ok(named.includes("ada@other.example.org") && named.includes("jane@corp.example.test"), named) + assert.strictEqual(M365.failureSentence("m365_wrong_account", "", "x@y.test"), M365.sentence("m365_wrong_account")) + assert.strictEqual(M365.failureSentence("m365_timeout", "ada", "x@y.test"), M365.sentence("m365_timeout")) + const poll = M365.pollResult({ state: "failed", error: "m365_wrong_account", signedInAs: "A\u202eB\n" + "x".repeat(200) }) + assert.ok(poll.who.length <= 80 && !/[\u202e\n]/.test(poll.who), poll.who) +} + +// Which failures end a session, which mean nothing is saved yet, and which pass. +for (const [code, signedOut, nothing, transient] of [ + ["m365_signed_out", true, true, false], ["auth_account_missing", false, true, false], + ["m365_network_failed", false, false, true], ["m365_timeout", false, false, true], + ["request_timed_out", false, false, true], ["m365_throttled", false, false, true], + ["m365_server_error", false, false, true], ["m365_request_limit", false, false, true], + ["m365_bad_response", false, false, true], ["auth_keyring_failed", false, false, true], + ["m365_access_denied", false, false, false], + ["m365_consent_required", false, false, false], ["m365_auth_failed", false, false, false]]) { + const error = { code: -32000, message: code } + assert.strictEqual(M365.isSignedOut(error), signedOut, code) + assert.strictEqual(M365.isNothingSaved(error), nothing, code) + assert.strictEqual(M365.isTransient(error), transient, code) +} + +// A keyring that is locked hides a saved sign-in, which unlocking it brings back; +// a sign-in that could not be saved there has to be done again. +assert.ok(/Unlock/.test(M365.sentence("auth_keyring_failed")) && !/sign in again/i.test(M365.sentence("auth_keyring_failed"))) +assert.ok(/could not be saved/.test(M365.failureSentence("auth_keyring_failed", "", "x@y.test"))) + +// A sign-out that could not wait for the sign-in may not have been done; any other +// failure says the saved sign-in could not be removed, and why. +assert.ok(/could not be completed/.test(M365.signOutFailure({ message: "m365_timeout" }))) +assert.strictEqual(M365.signOutFailure({ message: "auth_keyring_failed" }), + "The saved sign-in could not be removed. " + M365.sentence("auth_keyring_failed")) + +// A draft whose save was not confirmed may be in Drafts already. +assert.ok(/may already be in Drafts/.test(M365.sentence("m365_draft_unconfirmed"))) + +// Too large is a message too large to send, whether Omamail or Microsoft said so +// (the sentence names neither), or a message too large to open. +for (const method of ["m365.send", "m365.saveDraft"]) { + const tooLarge = M365.requestSentence(method, {}, { message: "m365_message_too_large" }) + assert.ok(/too large to send/.test(tooLarge) && !/refused/.test(tooLarge), method + ": " + tooLarge) +} +for (const method of ["m365.read", "m365.messages", "m365.attachment"]) + assert.ok(/larger than Omamail opens/.test(M365.requestSentence(method, {}, { message: "m365_message_too_large" })), method) + +// A poll whose answer was lost, or which is still at Microsoft's or the keyring's, +// may leave a sign-in saved; one answered otherwise has said what it did, and a +// token request that timed out (`m365_timeout`) saved nothing. +for (const lost of ["request_timed_out", "Backend request timed out", "Backend response timed out", + "Backend stopped", "Invalid backend response"]) + assert.strictEqual(M365.isLostAnswer({ code: -32010, message: lost }), true, lost) +for (const other of ["m365_timeout", "Backend unavailable", "m365_network_failed", ""]) + assert.strictEqual(M365.isLostAnswer({ message: other }), false, other) +for (const [result, failure, saved] of [ + [{ state: "signedIn" }, null, true], [{ state: "pending", warning: "m365_exchange_running" }, null, true], + [{ state: "pending", warning: "m365_timeout" }, null, false], + [{ state: "pending" }, null, false], [{ state: "pending", warning: "m365_network_failed" }, null, false], + [{ state: "declined" }, null, false], [null, { message: "request_timed_out" }, true], + [null, { message: "Backend response timed out" }, true], [null, { message: "Backend stopped" }, true], + [null, { message: "Invalid backend response" }, true], + [null, { message: "m365_invalid_request" }, false], [null, { message: "Backend unavailable" }, false]]) + assert.strictEqual(M365.pollMayHaveSaved(result, failure), saved, JSON.stringify([result, failure])) + +// A search that is only quotes or operators comes back as Invalid params. +assert.strictEqual(M365.requestSentence("m365.list", { query: "text:\"" }, { code: -32602, message: "Invalid params" }), + "That search has nothing Microsoft 365 can look for") +assert.strictEqual(M365.requestSentence("m365.list", { query: "role:inbox" }, { code: -32602, message: "Invalid params" }), + M365.GENERIC, "only a typed search is told as an empty one") +assert.strictEqual(M365.requestSentence("m365.modify", { query: "text:x" }, { message: "m365_throttled" }), + M365.sentence("m365_throttled")) + +// ------------------------------------------------------------ the gate + +assert.strictEqual(M365.MIN_API_VERSION, 8) +assert.strictEqual(M365.backendProblem({ ready: true, apiVersion: 8 }), "") +assert.strictEqual(M365.backendProblem({ ready: true }), "", "a stand-in that states no version is taken at its word") +assert.strictEqual(M365.backendProblem({ ready: true, apiVersion: 7 }), M365.sentence("backend_needs_update")) +assert.ok(/unavailable/.test(M365.backendProblem({ ready: false, apiVersion: 8 }))) +assert.ok(M365.backendProblem(null) !== "") + +// ------------------------------------------------------- the device sign-in + +const begun = { requestId: "0123456789abcdef", userCode: "ABCD1234E", verificationUri: "https://microsoft.com/devicelogin", + interval: 5 } +deepEqual(M365.beginResult(Object.assign({ expiresIn: 900 }, begun)), begun) +for (const [given, interval] of [[0, 5], ["soon", 5], [0.4, 5], [-3, 5], [1, 1], [7, 7], [500, 30]]) + assert.strictEqual(M365.beginResult(Object.assign({}, begun, { interval: given })).interval, interval, "interval " + given) +for (const bad of [{ userCode: "AB" }, { userCode: "A".repeat(33) }, { userCode: "ABCD 1234" }, { userCode: "x" }, + { userCode: "" }, { requestId: "" }, { requestId: "x\ny" }, { requestId: "x".repeat(257) }, + { verificationUri: "http://microsoft.com/devicelogin" }, { verificationUri: "https://evil.example/devicelogin" }, + { verificationUri: "https://microsoft.com.evil.example/devicelogin" }, { verificationUri: "" }, { verificationUri: undefined }]) + assert.strictEqual(M365.beginResult(Object.assign({}, begun, bad)), null, "unusable: " + JSON.stringify(bad)) +for (const bad of [null, undefined, "text", 3, []]) assert.strictEqual(M365.beginResult(bad), null) + +for (const [answer, expected] of [ + [{ state: "pending" }, { state: "pending", code: "", who: "" }], + [{ state: "pending", warning: "m365_network_failed" }, { state: "pending", code: "", who: "" }], + [{ state: "signedIn" }, { state: "signedIn", code: "", who: "" }], + [{ state: "declined" }, { state: "declined", code: "m365_declined", who: "" }], + [{ state: "expired" }, { state: "expired", code: "m365_expired", who: "" }], + [{ state: "failed", error: "m365_consent_required" }, { state: "failed", code: "m365_consent_required", who: "" }], + [{ state: "failed", error: "m365_tenant_invalid" }, { state: "failed", code: "m365_tenant_invalid", who: "" }], + [{ state: "failed" }, { state: "failed", code: "m365_auth_failed", who: "" }], + [{ state: "failed", error: "unheard_of" }, { state: "failed", code: "m365_auth_failed", who: "" }], + [{ state: "failed", error: "m365_wrong_account", signedInAs: "Ada" }, { state: "failed", code: "m365_wrong_account", who: "Ada" }], + [{ state: "done" }, { state: "failed", code: "m365_invalid_response", who: "" }], + [{ state: "" }, { state: "failed", code: "m365_invalid_response", who: "" }], + [{}, { state: "failed", code: "m365_invalid_response", who: "" }], + [null, { state: "failed", code: "m365_invalid_response", who: "" }], + ["pending", { state: "failed", code: "m365_invalid_response", who: "" }]]) + deepEqual(M365.pollResult(answer), expected, JSON.stringify(answer)) + +// -------------------------------------------------- what a check taught us + +const checked = { ok: true, email: "jane@corp.example.test", displayName: "", scheme: "oauth", serverVersion: "", + roles: { inbox: { total: 4, unread: 2 }, sent: { total: 1, unread: 0 }, drafts: { total: 0, unread: 0 }, + trash: { total: 0, unread: 0 }, junk: null, archive: { total: 9, unread: 0 } }, refusals: {}, learned: {} } +assert.strictEqual(M365.verified(checked), true) +for (const bad of [null, {}, "ok", { ok: "true" }, { ok: false, roles: {} }]) assert.strictEqual(M365.verified(bad), false) +deepEqual(M365.absentMailboxes(checked), ["spam"], "a mailbox without Junk draws no Junk row") +deepEqual(M365.absentMailboxes({ ok: true, roles: { archive: null, junk: {}, trash: null } }), ["archive", "trash"]) +deepEqual(M365.absentMailboxes({ ok: true, roles: {} }), ["archive", "spam", "trash"]) +assert.strictEqual(M365.absentMailboxes({ ok: true }), null, "no roles said, nothing hidden") +assert.strictEqual(M365.absentMailboxes(null), null) +assert.strictEqual(M365.absentMailboxes({ ok: false, roles: { archive: null } }), null, "a failed check teaches nothing") +deepEqual(M365.refusals(checked), {}) +deepEqual(M365.refusals(Object.assign({}, checked, { refusals: { send: "No Sent folder", archive: "", star: null, junk: false } })), + { send: "No Sent folder", archive: "This mailbox cannot do that" }) +assert.strictEqual(M365.refusals(null), null) +assert.strictEqual(M365.refusals({ ok: false, refusals: { send: "x" } }), null) +assert.ok(M365.refusals(Object.assign({}, checked, { refusals: { send: "a\nb\u202e" + "x".repeat(500) } })).send.length <= 300) +assert.ok(!/[\n\u202e]/.test(M365.refusals(Object.assign({}, checked, { refusals: { send: "a\nb\u202e" } })).send)) + +// ------------------------------------------------ the provider and its place + +const m365 = Registry.get("m365") +assert.strictEqual(m365.name, "Microsoft 365") +assert.strictEqual(Registry.authKind("m365"), "oauth") +assert.strictEqual(Registry.nativeSync("m365"), true) +assert.strictEqual(Registry.mark("m365"), "outlook.svg") +assert.strictEqual(Registry.webHomeUrl("m365"), "") +deepEqual(Object.keys(m365.capabilities).filter(name => m365.capabilities[name]).sort(), + ["archive", "batch", "search", "send", "star"], "the ceiling is archive, star, batch, search and send") +deepEqual(m365.mailboxes.map(box => [box.key, box.query, box.optional]), [ + ["inbox", "role:inbox", false], ["unread", "role:inbox unseen", false], ["starred", "role:inbox flagged", false], + ["drafts", "role:drafts", false], ["sent", "role:sent", false], ["archive", "role:archive", true], + ["spam", "role:junk", true], ["trash", "role:trash", true]]) +deepEqual(Registry.mailboxes("m365", M365.absentMailboxes(checked)).map(box => box.key), + ["inbox", "unread", "starred", "drafts", "sent", "archive", "trash"]) +assert.strictEqual(Registry.can("m365", "archive", {}), true) +assert.strictEqual(Registry.can("m365", "archive", { archive: "No Archive folder" }), false, "an account may withdraw Archive") +assert.strictEqual(Registry.can("m365", "labels", {}), false) +assert.strictEqual(Registry.can("m365", "spam", {}), false) + +// ------------------------------------------------ the account and its calendar + +assert.strictEqual(Accounts.normalizeProvider("M365"), "m365") +assert.strictEqual(Accounts.accountId(" Jane@Corp.Example.test ", "m365"), "m365:jane@corp.example.test") +assert.strictEqual(Accounts.calendarProvider({ provider: "m365", email: "jane@corp.example.test" }), "microsoft") +{ + const entry = Accounts.makeAccount({ provider: "m365", email: "jane@corp.example.test", + clientId: "11111111-2222-3333-4444-555555555555" }) + assert.strictEqual(entry.provider, "m365") + assert.strictEqual(entry.clientId, "11111111-2222-3333-4444-555555555555", "the ID is configuration, kept like Outlook's") + assert.strictEqual(entry.clientSecret, "") +} +// The setup card speaks of Microsoft, not of a Cloud project. +for (const [state, text] of [["no_credentials", /Add this Microsoft 365 mailbox/], ["signing_in", /Waiting for Microsoft/], + ["signed_out", /Sign in to Microsoft 365/]]) + assert.ok(text.test(Model.setupHeadline(state, "Microsoft 365", "oauth")), state) +assert.ok(/Application \(client\) ID/.test(Model.setupDetail("no_credentials", [], "", "Microsoft 365", "oauth"))) +assert.strictEqual(Model.setupHeadline("no_credentials", "Outlook", "oauth"), "Add this Outlook mailbox") +assert.ok(/Microsoft public-client ID/.test(Model.setupDetail("no_credentials", [], "", "Outlook", "oauth")), "Outlook's words stay") +// Which sign-in is Microsoft's is the registry's to say, not the name's. +assert.strictEqual(Registry.named("Microsoft 365"), "m365") +assert.strictEqual(Registry.signsInWithMicrosoft("m365") && Registry.signsInWithMicrosoft("outlook"), true) +assert.strictEqual(Registry.signsInWithMicrosoft("gmail") || Registry.signsInWithMicrosoft(Registry.named("Nobody")), false) +assert.strictEqual(Model.setupHeadline("no_credentials", "Gmail", "oauth"), "Connect a Google Cloud project") + +// The Application (client) ID is saved and sent in the spelling the backend keys a +// sign-in by, and this provider has no built-in one of its own (nor Outlook's). +assert.strictEqual(M365.BUILTIN_CLIENT_ID, undefined) +assert.strictEqual(M365.clientId(" 11111111-AAAA-3333-4444-55555555555F "), "11111111-aaaa-3333-4444-55555555555f") +for (const bad of ["", "not-a-guid", null, undefined, "11111111-2222-3333-4444-5555555555555"]) + assert.strictEqual(M365.clientId(bad), "", String(bad)) +console.log("m365 ok") diff --git a/ui/tests/test_provider.js b/ui/tests/test_provider.js index 1f21d07..1bc065c 100644 --- a/ui/tests/test_provider.js +++ b/ui/tests/test_provider.js @@ -17,13 +17,13 @@ for (const method of ["query", "cachedSummaryInSearch", "labelQuery", "addressQu // ------------------------------------------------------------- the registry // -// Four providers, and the ids are what an accounts.json holds — renaming one +// Six providers, and the ids are what an accounts.json holds — renaming one // silently orphans every account already written with the old name. // -// The order is the order the chooser lists them in: the three hosted mailboxes +// The order is the order the chooser lists them in: the four hosted mailboxes // with a service of their own, then the two that are every other mailbox, IMAP // first because it is the one nearly every server speaks. -deepEqual(provider.ids(), ["gmail", "hey", "outlook", "imap", "jmap"]) +deepEqual(provider.ids(), ["gmail", "hey", "outlook", "m365", "imap", "jmap"]) assert.strictEqual(provider.get("gmail").name, "Gmail") assert.strictEqual(provider.get("outlook").name, "Outlook") assert.strictEqual(provider.get("imap").name, "IMAP") From dda462ebbdef427679091933457a615294c367ba Mon Sep 17 00:00:00 2001 From: Oliver Lukschander Date: Mon, 19 Oct 2026 12:01:58 +0200 Subject: [PATCH 38/88] Fix restored mailboxes showing as signed out after restart (#356) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a restart, a mailbox restored from its saved session works, but the window says it is signed out. Settings → Mailboxes and the account switcher show "Signed out", and Settings' calendar section shows "Sign in to connect calendars". The Google or Microsoft calendar that comes with a Gmail or Outlook mailbox is left out of the calendar's sources, so creating an event says "Choose a calendar" with nothing to choose, unless that account's discovered calendars are already saved in `calendars.json`. It stays that way until something rewrites the account list, such as signing in, which is why it only shows after a restart. A mailbox whose saved session is refused shows "Signed out" instead of the reason. `accountSummaries` in `ui/Service.qml` reads each mailbox's host with `accountHosts.objectAt(i)`, which is not a property, so the binding is never told that a host has been built. At start-up the account list is applied before the Instantiator builds the hosts, so the summary is computed without one and keeps `signedIn: false`. The Instantiator now bumps `hostsEpoch` when it adds a host, and `accountSummaries` reads it, the way `senderSources` already does. Once the summary has read a host, it follows that host's sign-in, unread count and error directly. Only the epoch moves, not `recount()`. Qt rebuilds every host whenever the number of mailboxes changes, and recounting before the rebuilt hosts sign in again would briefly flip `anyAccountReady`, which resets the window's navigation (a first version did that, and the review caught it). `ui/Service.qml` goes from 130,991 to 131,005 bytes. `barMessages` and the unified view's lists also walk the hosts. They are unchanged: `barMessages` re-evaluated after the hosts existed in every flow tried (start-up, adding, removing), and the unified lists follow their own `listEpoch`. Not fixed here, unchanged from `main`: when a mailbox is removed and the one left never signs in again, nothing calls `recount()`. The bar keeps the removed mailbox's unread count and `anyAccountReady` stays true until a mailbox signs in. Recounting on a rebuild would fix that, but it brings back the navigation reset described above. ## Verification `make -k validate` on this commit (133abbb, Qt 6.12.0): 705 Rust tests (615 in the library), the node and shell suites, the standalone composition test and `qmllint` pass. The QML suite has 1302 passed and 3 failed: the three `tst_label_ownership.qml` tests that fail on `main` under Qt 6.12 too (#354 fixes them). I ran by hand the steps make skips after that failure, and all pass: `tests/test_outlook_http.py` (6), `tests/test_sidebar_text.py` (64), the ignored `native_output_cannot_trigger_qt_resource_requests` Rust test, `make qml-check`, `omarchy plugin validate .` and `git diff --check`. `ui/tests/qml/tst_account_hosts.qml` is new and listed for `qmllint`. It starts the real Service and App from a saved account registry the way the window does. It checks the mailbox's row in Settings and its calendar section, the event composer's calendar, and a restore the backend refuses. It also checks that adding or removing a mailbox keeps the page open while the rebuilt hosts wait for their held token exchanges, and that the summary then follows them. On `main`, the 3 restore tests fail on Qt 6.12.0 and on 6.11.2. With only the `hostsEpoch` read, the refused restore still fails. With a `recount()` in place of the bump, the add and remove tests fail. On Qt 6.11.2 (the Arch packages unpacked, not installed), the whole QML suite passes on this commit: 1305 passed, 0 failed. By hand, on Qt 6.12.0 in the standalone host, with an iCloud (IMAP) mailbox restored at start-up: on `main`, Settings → Mailboxes says "Signed out" and its calendar "Sign in to connect calendars" while the mailbox has just fetched mail; on this branch the same mailbox, at the same moment, says "No unread mail" and "iCloud · Uses this mailbox's sign-in" with Find calendars. Screenshots of both are attached, with the address covered. ![Settings → Mailboxes, before and after, a restored mailbox](https://github.com/user-attachments/assets/ab09c246-5bce-4618-8af0-a038c6588c4d) A fresh agent reviewed the diff with AGENTS.md. On the first version, which called `recount()` when a host was built, it found that adding or removing a mailbox sent the window back to the list. It reproduced this with a token exchange answering 40 ms late. That is fixed above, and the two navigation tests hold it. On this version it found nothing blocking (Security: PASS). It checked that the synchronous bump during a rebuild reads only finished hosts and merges into one `model.unified` and one `account.identities` call. It also noted the known gap above. ## Release Notes - After a restart, a mailbox restored from its saved session no longer shows as signed out in Settings and the account switcher, and its Google or Microsoft calendar is offered again when you create an event. ## Contact Thanks for reading! Happy to answer questions or change anything: here, by email (oliver@lukschander.com), on X (@olukschander) or Discord (oliverlukschander). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- Makefile | 2 +- ui/Service.qml | 7 +- ui/tests/qml/tst_account_hosts.qml | 238 +++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+), 4 deletions(-) create mode 100644 ui/tests/qml/tst_account_hosts.qml diff --git a/Makefile b/Makefile index 3beb913..7ef8024 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ QMLLINT := /usr/lib/qt6/bin/qmllint QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml ui/compose/PendingCompose.qml \ ui/tests/compatibility/tst_published_agent.qml \ ui/tests/qml/tst_unified_batch.qml ui/tests/qml/tst_clipboard_arguments.qml \ - ui/tests/qml/tst_reader_subject_menu.qml \ + ui/tests/qml/tst_reader_subject_menu.qml ui/tests/qml/tst_account_hosts.qml \ ui/backend/Backend.qml ui/backend/Runtime.qml ui/diagnostics/Diagnostics.qml \ ui/components/BackendSetup.qml ui/components/OmamailLogo.qml \ ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/UnifiedBatchAction.qml ui/account/UnifiedBatchSelection.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ diff --git a/ui/Service.qml b/ui/Service.qml index 4fdbe76..a658abe 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -1382,9 +1382,8 @@ Item { // to send the whole window back to the beginning. property bool anyAccountReady: false - // Instantiator.objectAt is not a property. sendIdentities has to watch - // something recount() updates, or From is computed once with no hosts - // and never lists the other signed-in mailboxes. + // Instantiator.objectAt is not a property, so accountSummaries and + // senderSources read this. It moves when a host is built and in recount(). property int hostsEpoch: 0 function recount() { @@ -1441,6 +1440,7 @@ Item { } readonly property var accountSummaries: { + var _epoch = hostsEpoch var out = [] var accounts = accountList ? accountList.accounts : [] for (var i = 0; i < accounts.length; i++) { @@ -2642,6 +2642,7 @@ Item { Instantiator { id: accountHosts model: root.accountCount + onObjectAdded: root.hostsEpoch++ delegate: MailAccount { platform: root diff --git a/ui/tests/qml/tst_account_hosts.qml b/ui/tests/qml/tst_account_hosts.qml new file mode 100644 index 0000000..39dcaf4 --- /dev/null +++ b/ui/tests/qml/tst_account_hosts.qml @@ -0,0 +1,238 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../../" as Omamail +import "BackendFixture.js" as BackendFixture +import "NativeIntentFixture.js" as NativeIntentFixture +import "../../account/Accounts.js" as Accounts + +// The account hosts are built by an Instantiator after the account list that +// names them: at start-up, and again, every one of them, whenever the number of +// mailboxes changes. Settings, the switcher and the calendar a mailbox brings +// read a summary of its host, so the summary has to follow the hosts that exist +// rather than the ones that existed when the list was applied, and following +// them must not move the window while the rebuilt hosts sign in again. Through +// the real Service and App, starting the way the window does. +Item { + width: 900 + height: 600 + + QtObject { id: fakeShell; function hide(_id) {} } + + Omamail.Service { + id: mailService + shell: fakeShell + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-account-hosts-test" }) + } + + Omamail.App { id: app; service: mailService; shell: fakeShell } + + TestCase { + id: testCase + name: "AccountHosts" + when: windowShown + + property var fixture: null + readonly property string bob: "outlook:bob@example.net" + readonly property string carol: "outlook:carol@example.net" + // The mailboxes whose saved session the backend no longer holds. + property var signedOut: [] + // Token exchanges held back, the way a real backend answers a little later. + property bool holdTokens: false + property var heldTokens: [] + readonly property var token: ({ access_token: "synthetic-access", token_type: "Bearer", + expires_in: 3600, scope: "https://outlook.office.com/IMAP.AccessAsUser.All" }) + + function initTestCase() { + fixture = BackendFixture.install(mailService) + // The read the window makes as soon as the backend is up finds nothing. + fixture.answers = { "accounts.read": { revision: "empty", registry: Accounts.emptyList() } } + BackendFixture.markReady(mailService, 7) + NativeIntentFixture.install(mailService) + tryVerify(function() { return mailService.accountsLoaded }, 3000) + } + + function init() { + signedOut = [] + holdTokens = false + heldTokens = [] + fixture.answers = { + "credentials.get": { found: false }, + "mail.watch": function(params) { return { accountId: params.accountId, sequence: 1, estimate: 0, messages: [], error: "synthetic" } }, + "mail.unwatch": {}, + "accounts.save": { revision: "saved" }, + // A saved Outlook session is restored by the backend's token exchange. + "auth.token": function(params, request) { + if (testCase.signedOut.indexOf(params.accountId) >= 0) + BackendFixture.respond(mailService, request, null, { code: -32000, message: "auth_signed_out" }) + else if (testCase.holdTokens) testCase.heldTokens = testCase.heldTokens.concat([request]) + else return testCase.token + return undefined + } + } + // A fresh start: nothing loaded and no account hosts yet. What earlier + // tests left unanswered would otherwise hold the backend's 64 slots. + mailService.backend.pending = ({}) + mailService.accountsLoaded = false + mailService.accountList = ({ version: 1, accounts: [], activeId: "" }) + app.resetNavigation() + wait(0) + compare(mailService.accountCount, 0) + } + + function releaseTokens() { + var held = heldTokens + heldTokens = [] + holdTokens = false + for (var i = 0; i < held.length; i++) BackendFixture.respond(mailService, held[i], token) + } + + // What the window does when it starts: read the saved list and apply it. + function start(addresses) { + var list = Accounts.emptyList() + for (var i = 0; i < addresses.length; i++) { + list = Accounts.add(list, { email: addresses[i], provider: "outlook", + clientId: "11111111-2222-3333-4444-555555555555" }) + } + fixture.answers["accounts.read"] = { revision: "start-up", + registry: Accounts.setActive(list, "outlook:" + addresses[0]) } + mailService.restoreAccountRegistry() + tryCompare(mailService, "accountCount", addresses.length, 3000) + } + + function summary(id) { + return mailService.accountSummaries.filter(function(entry) { return entry.id === id })[0] + } + + function named(item, objectName) { + if (!item) return null + if (item.objectName === objectName) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = named(values[i], objectName) + if (found) return found + } + return null + } + + function typed(item, prefix) { + if (String(item).indexOf(prefix) === 0) return item + var children = item.children || [] + for (var i = 0; i < children.length; i++) { + var found = typed(children[i], prefix) + if (found) return found + } + return null + } + + // What Settings' list of mailboxes says on a mailbox's row. + function settingsRow(id) { + app.openSettings() + verify(app.showSettings, "Settings opens") + function texts(item, out) { + if (typeof item.text === "string") out.push(item.text) + var values = item.children || [] + for (var i = 0; i < values.length; i++) texts(values[i], out) + return out + } + function row(item) { + if (String(item).indexOf("QQuickRectangle") === 0 && item.modelData && item.modelData.id === id) + return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = row(values[i]) + if (found) return found + } + return null + } + var found = row(named(app, "settings-page")) + verify(!!found, "Settings lists the mailbox") + return texts(found, []) + } + + function test_a_restored_mailbox_reads_as_signed_in() { + start(["bob@example.net"]) + tryVerify(function() { return mailService.current && mailService.current.ready }, 3000, + "the saved session is restored") + compare(summary(bob).signedIn, true, "the summary follows the host that became ready") + var row = settingsRow(bob) + verify(row.indexOf("No unread mail") >= 0, "Settings counts its mail: " + row) + verify(row.indexOf("Signed out") < 0, "and does not say Signed out") + verify(named(named(app, "settings-page"), "calendar-discover-microsoft").text !== "Sign in...", + "and its calendar does not ask to be signed in") + } + + function test_a_restored_mailbox_offers_its_calendar_to_the_event_composer() { + start(["bob@example.net"]) + tryVerify(function() { return mailService.current && mailService.current.ready }, 3000) + app.showCalendar() + waitForRendering(app) + tryCompare(named(app, "key-router"), "context", "calendar") + keyClick(Qt.Key_C) + var composer = typed(app, "CalendarEventComposer") + tryVerify(function() { return composer.opened }, 2000, "c opens the event composer") + var selector = named(composer, "event-calendar-selector") + compare(selector.contentItem.text, "bob@example.net", + "the composer names the mailbox's calendar, not 'Choose a calendar'") + compare(selector.count, 1, "and offers it") + keyClick(Qt.Key_Escape) + tryVerify(function() { return !composer.opened }, 2000) + } + + // A host that never signs in changes nothing recount() hears, so this is + // the summary following the host being built and nothing else. + function test_a_restored_mailbox_that_cannot_sign_in_says_why() { + signedOut = [bob] + start(["bob@example.net"]) + var host = mailService.findAccount(bob) + tryVerify(function() { return host && String(host.lastError) !== "" }, 3000, + "the saved session is refused") + compare(host.ready, false) + compare(summary(bob).error, host.lastError, "the summary carries what the mailbox said") + var row = settingsRow(bob) + verify(row.indexOf(host.lastError) >= 0, "and Settings says it: " + row) + verify(row.indexOf("Signed out") < 0, "rather than just Signed out") + } + + // Adding a mailbox rebuilds every host, and none of them is signed in + // until its token exchange answers. The form being filled in stays. + function test_adding_a_mailbox_keeps_its_form_while_the_others_sign_in_again() { + start(["bob@example.net"]) + var before = mailService.findAccount(bob) + tryVerify(function() { return before && before.ready }, 3000) + app.openSettings() + app.addMailbox() + holdTokens = true + app.chooseProvider("outlook") + compare(app.page, "setup") + var rebuilt = mailService.findAccount(bob) + if (rebuilt === before) skip("this Qt keeps the hosts when a mailbox is added") + tryVerify(function() { return heldTokens.length > 0 }, 3000, "the hosts sign in again") + wait(50) + compare(app.page, "setup", "the new mailbox's form is still open") + releaseTokens() + tryVerify(function() { return rebuilt.ready }, 3000) + compare(summary(bob).signedIn, true, "the summary follows the rebuilt host") + compare(app.page, "setup") + } + + function test_removing_a_mailbox_keeps_settings_while_the_other_signs_in_again() { + start(["bob@example.net", "carol@example.net"]) + tryVerify(function() { return mailService.findAccount(bob) && mailService.findAccount(bob).ready + && mailService.findAccount(carol) && mailService.findAccount(carol).ready }, 3000) + var before = mailService.findAccount(carol) + holdTokens = true + app.confirmAccountRemoval(Accounts.removalRequest({ accounts: mailService.accountSummaries }, 0)) + tryCompare(mailService, "accountCount", 1) + compare(app.page, "settings") + var rebuilt = mailService.findAccount(carol) + if (rebuilt === before) skip("this Qt keeps the hosts when a mailbox is removed") + tryVerify(function() { return heldTokens.length > 0 }, 3000, "the mailbox left signs in again") + wait(50) + compare(app.page, "settings", "Settings is still open") + releaseTokens() + tryVerify(function() { return rebuilt.ready }, 3000) + compare(summary(carol).signedIn, true, "the summary follows the rebuilt host") + compare(app.page, "settings") + } + } +} From 8321c69c34bac4b5c9715b02e24df21991361b74 Mon Sep 17 00:00:00 2001 From: Oliver Lukschander Date: Mon, 19 Oct 2026 12:11:19 +0200 Subject: [PATCH 39/88] Fix standalone Color and Style withAlpha turning shell.toml colours black (#366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the standalone host (`app/`), dialogs, menus and tooltips are drawn black. On a light Omarchy theme such as Flexoki Light their text is the theme's dark foreground, so the "Save this draft?" question, the right-click menus and the tooltips cannot be read. On a dark theme they are pure black instead of the theme's background, which is easy to miss. Hover and selected fills have the same cause: they start from black, so on a dark theme a selected row sits darker than the window instead of lighter, and control borders and pane dividers barely show. The shell build is not affected. To reproduce: with Flexoki Light as the Omarchy theme, run `make app-run`, then right-click a message, hover a row's trash button, or start a message, type a subject and press Escape. Omarchy renders the current theme's `shell.toml` from `default/themed/shell.toml.tpl`, which writes the `[popups]` and `[controls]` colours as quoted strings such as `background = "#FFFCF0"`. The standalone `Color.shellColor` and `Style.controlColor` hand such a value back as that string, and both `withAlpha` helpers read it with `Qt.rgba(value.r, value.g, value.b, …)`. A string has no `.r`, so the result is black. Text colours do not go through `withAlpha`, so they stayed the theme's foreground: dark text on black. `Color.withAlpha` and `Style.withAlpha` now use `Qt.alpha`, which takes a colour or its name, as the shell's own `Util.alpha` does by converting a string first. `Qt.alpha` is already used across `ui/`, so the host needs nothing new from Qt. The fix sits in the standalone host's `qs.Commons` because that is the only place this host reads `shell.toml`; the shell's `qs.Commons` already handles it. The dialog's and menus' own bindings were right and are unchanged: they follow a theme switch while open. A theme without a `shell.toml` was never affected, because every fallback is already a colour. ## Verification `make validate` is green on this commit, except three tests in `tst_label_ownership.qml` that fail on `main` under Qt 6.12 (fixed by #354). The steps make skips after them were run by hand and pass. `tst_standalone_composition.qml` installs Flexoki Light and Tokyo Night through the host's file store the way Omarchy leaves them (`colors.toml` and a `shell.toml` with quoted hex colours). It gains three tests, and all of them fail on `main`: - `test_menus_and_tooltips_take_the_theme_popup_colours`, for both themes: the window menu and a styled tooltip in the real standalone composition have the theme's background and foreground, with text at a 4.5:1 contrast or more. On `main` the background is `#000000`. - `test_control_fills_take_the_theme_control_colours`, for both themes: the switch's selected fill, the spin box's normal fill and the style's hover fill are the `[controls]` colour at its alpha, and a selected fill moves the window toward the text colour. On `main` Tokyo Night's selected fill is `#2e000000`. - `test_compose_exit_dialog_follows_the_theme`: the draft question, opened by leaving a changed draft, has the theme's background and button fill in Tokyo Night, and still does after a switch to Flexoki Light while it is open. On `main` its background is `#000000`. By hand, in the standalone host built from this branch, in Tokyo Night against an iCloud test mailbox: the right-click menu and a row's tooltip have the theme's own background, the menu's separators show, and a hovered row is lighter than the window. Flexoki Light was checked through the offscreen renders below rather than by switching the machine's theme. Screenshots: from `main` and from this branch, rendered offscreen through the standalone host's own style and `qs.Commons` with each theme's generated `shell.toml`, at 980×720 with a synthetic mailbox: ![Right-click menu in Flexoki Light, before and after](https://github.com/user-attachments/assets/c493ac1a-207d-4b55-bc4c-ab0269196ba5) ![The "Save this draft?" dialog in Flexoki Light, before and after](https://github.com/user-attachments/assets/076c9e74-8d8d-42f2-8dfb-bccadedd94c0) ![A row's tooltip in Flexoki Light, before and after](https://github.com/user-attachments/assets/19953dfe-fd0b-4e64-b288-296e481e495a) ![Right-click menu in Tokyo Night, before and after](https://github.com/user-attachments/assets/2f7c8083-50fe-4409-b43d-083739952064) ![A row's tooltip in Tokyo Night, before and after](https://github.com/user-attachments/assets/c087d98e-e391-4b13-9682-8762d6ceb1fc) ![Hover and selected fills in Tokyo Night, before and after](https://github.com/user-attachments/assets/b052626a-4b5b-46db-ae71-810b9318aa0b) - The right-click menu on a message and the tooltip of a row's trash button in Flexoki Light: black with unreadable text before, the theme's paper background after. - The same in Tokyo Night: pure black before, the theme's own background after, where the menu's separators now show. - The "Save this draft?" dialog in Flexoki Light: black with unreadable Cancel and Save draft before, readable with filled buttons after. - Hover and selected fills in Tokyo Night (the open mailbox, the keyboard's row and a hovered row): darker than the window before, lighter after, with the pane dividers and button borders visible. A fresh agent reviewed the change with AGENTS.md, with an empty context: no blocking findings, and no security boundary touched (colour arithmetic on theme tokens `ShellTheme.colorToken` has already validated). It ran the new tests against `main` (5 rows fail) and the branch (all pass), probed `Qt.alpha` with every kind of value the helpers can receive, and checked the screenshots against this text. It also noted a pre-existing difference this PR leaves alone: an eight-digit colour in a hand-written `shell.toml` is read as `#AARRGGBB` here and as `#RRGGBBAA` by the shell. No theme ships one and the template writes six digits, so it is a follow-up rather than part of this fix. ## Release Notes - In the standalone app, dialogs, menus and tooltips follow the Omarchy theme's colours. On a light theme they were black with dark text, which made the "Save this draft?" question and the right-click menus unreadable. - Hover and selected fills in the standalone app follow the theme too: on a dark theme a selected row is now lighter than the window, not darker. ## Contact Thanks for reading! Happy to answer questions or change anything: here, by email (oliver@lukschander.com), on X (@olukschander) or Discord (oliverlukschander). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- app/qml/imports/qs/Commons/Color.qml | 6 +- app/qml/imports/qs/Commons/Style.qml | 4 +- app/tests/qml/tst_standalone_composition.qml | 179 +++++++++++++++++++ 3 files changed, 187 insertions(+), 2 deletions(-) diff --git a/app/qml/imports/qs/Commons/Color.qml b/app/qml/imports/qs/Commons/Color.qml index 67fb9aa..d337a4b 100644 --- a/app/qml/imports/qs/Commons/Color.qml +++ b/app/qml/imports/qs/Commons/Color.qml @@ -57,10 +57,14 @@ QtObject { if (role === "transparent") return Qt.rgba(0, 0, 0, 0) return token.charAt(0) === "#" ? token : fallback } + // shellColor answers a shell.toml colour with the "#rrggbb" string the file + // wrote, and a string has no .r: Qt.rgba over one is black, which drew every + // dialog, menu and tooltip black under a light theme's dark text. + // Qt.alpha reads a colour or its name, as the shell's own Util.alpha does. function withAlpha(value, alpha) { var amount = Number(alpha) if (!isFinite(amount)) amount = 1 - return Qt.rgba(value.r, value.g, value.b, Math.max(0, Math.min(1, amount))) + return Qt.alpha(value, Math.max(0, Math.min(1, amount))) } // A menu is a piece of the window, not a raised card: without a shell.toml // saying otherwise it sits on the window's own background, and only its diff --git a/app/qml/imports/qs/Commons/Style.qml b/app/qml/imports/qs/Commons/Style.qml index fc1b417..929caca 100644 --- a/app/qml/imports/qs/Commons/Style.qml +++ b/app/qml/imports/qs/Commons/Style.qml @@ -100,7 +100,9 @@ QtObject { var n = Number(value) * spacingScale return n <= 0 ? 0 : Math.max(1, Math.round(n)) } - function withAlpha(color, alpha) { return Qt.rgba(color.r, color.g, color.b, alpha) } + // controlColor answers a [controls] colour with the "#rrggbb" string + // shell.toml wrote; Qt.rgba over its .r was black, Qt.alpha reads it. + function withAlpha(color, alpha) { return Qt.alpha(color, alpha) } function mix(first, second, amount) { var n = Math.max(0, Math.min(1, Number(amount))) return Qt.rgba(first.r * (1 - n) + second.r * n, diff --git a/app/tests/qml/tst_standalone_composition.qml b/app/tests/qml/tst_standalone_composition.qml index e2cfac8..172fb59 100644 --- a/app/tests/qml/tst_standalone_composition.qml +++ b/app/tests/qml/tst_standalone_composition.qml @@ -120,12 +120,191 @@ TestCase { manifest: ({id:"omamail"}) } + property bool omarchyThemeWritten: false + function init() { host.reset() calendarFixture.calendarPalettePath = "" calendarFixture.colorKey = "accent" } + // Color is a singleton: a test that installs an Omarchy theme takes it out + // again, so the next test starts from the fallback palette it expects. + function cleanup() { + if (!omarchyThemeWritten) return + omarchyThemeWritten = false + host.reset() + Commons.Color.reload() + } + + // An installed theme as Omarchy leaves it: colors.toml, and the shell.toml + // omarchy-theme-set-templates renders from it, which writes every colour as + // a quoted "#rrggbb" and the popup border as a reference to [hyprland]. + function installOmarchyTheme(theme) { + var dir = "/fixture/home/.local/state/omarchy/current/theme/" + host.write(dir + "colors.toml", [ + 'mode = "' + theme.mode + '"', 'background = "' + theme.background + '"', + 'foreground = "' + theme.foreground + '"', 'accent = "' + theme.accent + '"', + 'red = "' + theme.red + '"', 'yellow = "' + theme.yellow + '"', + 'green = "' + theme.green + '"'].join("\n"), false) + host.write(dir + "shell.toml", [ + "[controls]", 'normal-color = "' + theme.foreground + '"', + "normal-fill-alpha = 0.04", 'hover-cursor-color = "' + theme.foreground + '"', + "hover-cursor-fill-alpha = 0.08", 'selected-color = "' + theme.foreground + '"', + "selected-fill-alpha = 0.18", "", + "[popups]", 'background = "' + theme.background + '"', + "background-alpha = 1.0", 'text = "' + theme.foreground + '"', + 'border = "hyprland.active-border"', "border-alpha = 1.0"].join("\n"), false) + omarchyThemeWritten = true + Quickshell.nativeHost = host + Quickshell.fileStore = host + verify(Commons.Color.reload(), "the fixture must read as an installed Omarchy theme") + compare(Commons.Color.dark, theme.mode === "dark") + } + + function contrastRatio(first, second) { + function luminance(value) { + var channels = [value.r, value.g, value.b] + var weights = [0.2126, 0.7152, 0.0722] + var result = 0 + for (var i = 0; i < 3; i++) { + var c = channels[i] + result += weights[i] * (c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4)) + } + return result + } + var a = luminance(first) + var b = luminance(second) + return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05) + } + + function popupOf(owner) { + var values = owner.data || [] + for (var i = 0; i < values.length; i++) + if (values[i] instanceof Popup) return values[i] + return null + } + + function omarchyThemes() { + return [ + {tag: "Flexoki Light", mode: "light", background: "#fffcf0", foreground: "#100f0f", + accent: "#205ea6", red: "#d14d41", yellow: "#d0a215", green: "#879a39"}, + {tag: "Tokyo Night", mode: "dark", background: "#1a1b26", foreground: "#a9b1d6", + accent: "#7aa2f7", red: "#f7768e", yellow: "#e0af68", green: "#9ece6a"} + ] + } + + function test_menus_and_tooltips_take_the_theme_popup_colours_data() { return omarchyThemes() } + + function test_menus_and_tooltips_take_the_theme_popup_colours(data) { + installOmarchyTheme(data) + var composition = createTemporaryObject(compositionComponent, testCase) + verify(composition) + + var appMenu = findChild(composition, "app-menu") + verify(appMenu) + appMenu.openAt(40, 40) + tryCompare(appMenu, "opened", true) + var menu = popupOf(appMenu) + verify(menu, "the window menu is a Popup") + var quitRow = findChild(composition, "app-menu-quit") + verify(quitRow) + var rowLabel = quitRow.children[0] + verify(rowLabel && rowLabel.text !== undefined, "the row's label") + compare(String(menu.background.color), data.background) + compare(String(rowLabel.color), data.foreground) + verify(contrastRatio(rowLabel.color, menu.background.color) >= 4.5, + "menu text on its background: " + rowLabel.color + " on " + menu.background.color) + appMenu.close() + tryCompare(appMenu, "opened", false) + + var tip = findChild(composition, "omamail-tooltip") + verify(tip, "the composed app must show a styled tooltip somewhere") + tip.visible = true + tryCompare(tip, "opened", true, Style.tooltipDelay + 1000) + var tipBackground = findChild(tip, "omamail-tooltip-background") + var tipLabel = findChild(tip, "omamail-tooltip-label") + verify(tipBackground) + verify(tipLabel) + compare(String(tipBackground.color), data.background) + compare(String(tipLabel.color), data.foreground) + verify(contrastRatio(tipLabel.color, tipBackground.color) >= 4.5, + "tooltip text on its background: " + tipLabel.color + " on " + tipBackground.color) + tip.visible = false + tryCompare(tip, "opened", false) + } + + function test_control_fills_take_the_theme_control_colours_data() { return omarchyThemes() } + + // A fill is the [controls] colour at its alpha. On a dark theme that colour + // is the light foreground, so a selected control sits a little lighter than + // the window; a fill started from black sat darker. + function test_control_fills_take_the_theme_control_colours(data) { + installOmarchyTheme(data) + var track = findChild(styledSwitch, "omamail-switch-knob").parent + verify(track && styledSwitch.checked, "the checked switch's track") + // The track animates its colour, so it is waited for. + var selected = String(Qt.alpha(data.foreground, 0.18)) + tryVerify(function() { return String(track.color) === selected }, 1000, + "selected fill: " + track.color + ", expected " + selected) + var field = findChild(styledSpinBox, "omamail-spinbox-background") + verify(field) + compare(String(field.color), String(Qt.alpha(data.foreground, 0.04)), "normal fill") + compare(String(Style.hoverFillFor(Commons.Color.foreground, Commons.Color.accent)), + String(Qt.alpha(data.foreground, 0.08)), "hover fill") + function over(fill, base) { + return Qt.rgba(base.r * (1 - fill.a) + fill.r * fill.a, base.g * (1 - fill.a) + fill.g * fill.a, + base.b * (1 - fill.a) + fill.b * fill.a, 1) + } + var window = Commons.Color.background + var shown = over(track.color, window) + verify(contrastRatio(shown, Commons.Color.foreground) < contrastRatio(window, Commons.Color.foreground), + "a selected fill moves the window toward the theme's text colour: " + shown + " on " + window) + } + + // The draft question is a Popup like a menu, and its buttons are filled + // like any control: both were drawn from black. Its colours are bindings, + // so a theme changed while it is open reaches it too. + function test_compose_exit_dialog_follows_the_theme() { + var themes = omarchyThemes() + var dark = themes[1] + var light = themes[0] + installOmarchyTheme(dark) + var composition = createTemporaryObject(compositionComponent, testCase) + verify(composition) + composition.app.backToList() + composition.app.startCompose("new") + var field = findChild(composition, "compose-subject-field") + verify(field) + var compose = field + while (compose && typeof compose.hasUserChanges !== "function") compose = compose.parent + verify(compose, "the composer") + compose.userModified = true + composition.app.goBack() + var exitDialog = findChild(composition, "compose-exit-dialog") + verify(exitDialog) + tryCompare(exitDialog, "opened", true) + var dialog = popupOf(exitDialog) + verify(dialog, "the draft question is a Popup") + var cancel = dialog.contentItem.children[2].children[0] + verify(cancel && cancel.text === "Cancel", "the Cancel button") + var title = dialog.contentItem.children[0] + verify(title && title.text === "Save this draft?", "the dialog's title") + + compare(String(dialog.background.color), dark.background) + compare(String(cancel.color), String(Qt.alpha(dark.foreground, 0.04)), "button fill") + + installOmarchyTheme(light) + verify(exitDialog.opened) + compare(String(dialog.background.color), light.background) + compare(String(cancel.color), String(Qt.alpha(light.foreground, 0.04)), "button fill") + compare(String(title.color), light.foreground) + verify(contrastRatio(title.color, dialog.background.color) >= 4.5, + "dialog text on its background: " + title.color + " on " + dialog.background.color) + exitDialog.close() + tryCompare(exitDialog, "opened", false) + } + function test_calendar_settings_palette_data() { return [{tag: "theme palette", path: "/fixture/theme/colors.toml"}, {tag: "no theme palette", path: ""}] From 611570da854168e3701ee3dae08f61c9e97c775a Mon Sep 17 00:00:00 2001 From: Jan Renz Date: Mon, 19 Oct 2026 12:35:27 +0200 Subject: [PATCH 40/88] Fix calendar week-view collisions and expand events into free lanes (#295) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What this changes Calendar week-view events expand into adjacent free lanes, while events that overlap on the rendered local-time grid occupy separate lanes. This covers repeated local hours during DST fallback and the grid's two-pixel minimum height. Dense event blocks and their hitboxes stay within their assigned lanes, and narrow titles remain readable through a plain-text hover tooltip. The branch integrates current main's calendar interactions: drag to move or resize, drag to create a time range, all-day and multi-day placement, pending-save states, locale-aware time labels, and duration-scaled short event heights. Lane collision calculations use the view's actual hour scale. The standalone tooltip's default text sink is also explicitly plain text. ## Prior work #123 by @inumineq proposed the original side-by-side layout and identified collision checks needing to match drawn height. Current main now supplies basic interval columns; this PR adds free-lane expansion, rendered-coordinate collision handling, dense-block containment, and narrow-title presentation on top of that implementation. ## Verification Tested integration commit `8cf9e4a2c95326dcabb680e8ef24ed0ab8992bfb`, merging main at `8321c69c34bac4b5c9715b02e24df21991361b74`. - `make validate` passed its Rust, JavaScript, shell and QML stages: 1,470 QML checks passed with no failures. The four failures recorded before integration are no longer present. - The native tooltip/resource suite passed all six checks using a real Qt image loader and a controlled loopback observer, with synthetic services/styling. It covers title controls, repeated local hours, 30 simultaneous blocks, disjoint contained hitboxes, hover text and click activation. **Security: PASS** for this affected text/resource boundary. - All 13 dedicated calendar gesture checks passed, including resizing, pending-save restoration, separate overlap targets and all-day/multi-day placement. New JavaScript assertions cover main's short-event heights, grid-scale-dependent minimum-height collisions and exclusion of multi-day events from timed lanes. - Native sanitized-HTML resource, Outlook HTTP and sidebar text/resource checks passed. `make qml-check`, `omarchy plugin validate .` and `git diff --check` passed. - `make validate` stopped when the standalone application target tried to invoke the missing `cmake` executable. The standalone build/composition target remains unverified. Current-head CI must be checked separately. Earlier repair `dcfeac1` demonstrated that the original tooltip produced an unintended image request and that the patched tooltip prevented it. This integration retains explicit plain-text sinks and passed the current-head resource regression on Qt 6.12.0. No real mailbox or manual desktop interaction was used. Earlier synthetic Node performance observations are not current Qt GUI measurements. ## Screenshots The original comparable synthetic dark-theme pair below predates integration with current main. It is hosted on a mutable contributor media branch rather than GitHub attachments. Earlier repair captures are stored locally; they do not establish current-head visual evidence after the main merge. Updated matching screenshots as GitHub attachments and live desktop interaction evidence remain outstanding. UI evidence: **BLOCK**. | Original before | Original after | |---|---| | ![before](https://raw.githubusercontent.com/janrenz/omamail/pr-media/media/week-before.png) | ![after](https://raw.githubusercontent.com/janrenz/omamail/pr-media/media/week-after.png) | The original pair shows overlapping meetings becoming visible side by side. Current main already has basic side-by-side columns; new evidence must show free-lane expansion, dense columns and the current hover tooltip. ## Release Notes - Calendar week-view events use adjacent free lanes and avoid collisions in repeated local hours during DST fallback. - Dense calendar events stay within their assigned lanes; narrow titles are available as plain text on hover. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Jason Lee --- Makefile | 1 + app/qml/styles/Omamail/ToolTip.qml | 1 + tests/test_tooltip_text.py | 174 +++++++++++++++++++++++++++++ ui/calendar/Calendar.js | 75 +++++++++++++ ui/components/WeekCalendarView.qml | 18 ++- ui/tests/test_calendar_feed.js | 53 +++++++++ 6 files changed, 316 insertions(+), 6 deletions(-) create mode 100644 tests/test_tooltip_text.py diff --git a/Makefile b/Makefile index 7ef8024..3104e0b 100644 --- a/Makefile +++ b/Makefile @@ -261,6 +261,7 @@ test-qml: python3 tests/run_qml_native.py "$(QMLTESTRUNNER)" -input ui/tests/qml python3 tests/test_outlook_http.py "$(QMLTESTRUNNER)" python3 tests/test_sidebar_text.py "$(QMLTESTRUNNER)" + python3 tests/test_tooltip_text.py "$(QMLTESTRUNNER)" QMLTESTRUNNER="$(QMLTESTRUNNER)" cargo test --locked --lib message::html::tests::native_output_cannot_trigger_qt_resource_requests -- --ignored test-app-qml: diff --git a/app/qml/styles/Omamail/ToolTip.qml b/app/qml/styles/Omamail/ToolTip.qml index 5fffc53..42bbd00 100644 --- a/app/qml/styles/Omamail/ToolTip.qml +++ b/app/qml/styles/Omamail/ToolTip.qml @@ -46,6 +46,7 @@ T.ToolTip { contentItem: Text { objectName: "omamail-tooltip-label" text: control.text + textFormat: Text.PlainText color: Commons.Color.popups.text font.family: Style.font.family font.pixelSize: Style.font.caption diff --git a/tests/test_tooltip_text.py b/tests/test_tooltip_text.py new file mode 100644 index 0000000..b0719f1 --- /dev/null +++ b/tests/test_tooltip_text.py @@ -0,0 +1,174 @@ +"""Actual standalone tooltip renders untrusted titles without resource requests.""" +import json +import os +import shutil +from pathlib import Path +import subprocess +import sys +import tempfile +import threading +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +ROOT = Path(__file__).resolve().parents[1] +requests = [] + + +class Handler(BaseHTTPRequestHandler): + def log_message(self, *_): + pass + + def do_GET(self): + requests.append(self.path) + self.send_response(404) + self.end_headers() + + +QML = r''' +import QtQuick +import QtQuick.Controls +import QtTest +import @STYLE@ as AppStyle +import @COMPONENTS@ as Mail +import @CALENDAR@ as Calendar +Item { + id: host + width: 500; height: 400 + property string endpoint: @ENDPOINT@ + Component { id: activationSpyFactory; SignalSpy { signalName: "eventActivated" } } + Component { id: controlFactory; Text { textFormat: Text.AutoText } } + Component { + id: controllerFactory + QtObject { + property var events: [] + function colorKeyFor(source) { return "accent" } + } + } + Component { id: tipFactory; AppStyle.ToolTip {} } + Component { + id: weekFactory + Mail.WeekCalendarView { + width: 500; height: 400 + days: Calendar.weekDays(new Date(2026, 9, 25, 12).getTime(), 1) + nowMs: 0; textColor: "#eeeeee"; backgroundColor: "#222222" + accentColor: "#5599ff"; urgentColor: "#ff5555"; dimColor: "#999999" + calendarBorderColor: "#555555"; calendarTodayBackgroundColor: "#333333" + calendarBorderWidth: 1; panelFontFamily: "monospace"; selectedEventId: "" + } + } + TestCase { + name: "TooltipPlainText" + when: windowShown + function test_1_control() { + var item = createTemporaryObject(controlFactory, host, {text: ''}) + verify(item !== null) + wait(300) + } + function blocks(item, out) { + if (item.eventData !== undefined && item.laneWidth !== undefined) out.push(item) + var children = item.children || [] + for (var i = 0; i < children.length; i++) blocks(children[i], out) + return out + } + function test_3_calendar_dense_and_hover() { + var events = [] + var start = new Date(2026, 9, 25, 9).getTime() + for (var i = 0; i < 30; i++) events.push({uid: "event-" + i, + summary: '', + start: {ms: start}, end: {ms: start + 3600000}}) + var controller = createTemporaryObject(controllerFactory, host) + controller.events = events + var view = createTemporaryObject(weekFactory, host, {controller: controller}) + verify(view !== null) + wait(100) + var drawn = blocks(view, []) + compare(drawn.length, 30) + drawn.sort(function(a, b) { return a.x - b.x }) + for (var j = 0; j < drawn.length; j++) { + var block = drawn[j] + verify(block.x >= 0) + verify(block.x + block.width <= block.parent.width + 0.001) + if (j > 0) verify(drawn[j - 1].x + drawn[j - 1].width <= block.x + 0.001) + } + // The center of a narrow block exercises the actual hover binding. + mouseMove(drawn[0], drawn[0].width / 2, 10) + wait(600) + var tip = findChild(drawn[0], "calendar-event-tooltip") + verify(tip !== null) + verify(tip.visible, "hover opens the production calendar tooltip") + verify(tip.contentItem.text.indexOf(events[0].summary) === 0) + compare(tip.contentItem.textFormat, Text.PlainText) + var spy = createTemporaryObject(activationSpyFactory, host, {target: view}) + mouseClick(drawn[0], drawn[0].width / 2, 10) + compare(spy.count, 1) + } + function test_4_repeated_hour() { + var day = {startMs: new Date('2026-10-25T00:00:00+02:00').getTime(), + endMs: new Date('2026-10-26T00:00:00+01:00').getTime()} + var events = ['+02:00', '+01:00'].map(function(offset) { + return {start: {ms: new Date('2026-10-25T02:10:00' + offset).getTime()}, + end: {ms: new Date('2026-10-25T02:40:00' + offset).getTime()}} + }) + compare(Calendar.eventTop(events[0], day, 0, 60), Calendar.eventTop(events[1], day, 0, 60)) + var lanes = Calendar.dayLanes(events, day) + compare(lanes[0].lanes, 2) + verify(lanes[0].lane !== lanes[1].lane) + } + function test_2_titles() { + var suffixes = ["", "\n", "\r", "\r\n", "\u0000"] + for (var i = 0; i < suffixes.length; i++) { + var title = '' + suffixes[i] + var tip = createTemporaryObject(tipFactory, host, {text: title}) + verify(tip !== null) + tip.open() + wait(100) + compare(tip.contentItem.text, title) + compare(tip.contentItem.textFormat, Text.PlainText) + tip.close() + } + var tip = createTemporaryObject(tipFactory, host, {text: '中文 "quotes" \\ slash\nline & literal'}) + tip.open() + compare(tip.contentItem.text, tip.text) + compare(tip.contentItem.textFormat, Text.PlainText) + wait(300) + } + } +} +''' + + + +def main(): + runner = sys.argv[1] if len(sys.argv) > 1 else "/usr/lib/qt6/bin/qmltestrunner" + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + try: + with tempfile.TemporaryDirectory(prefix="omamail-tooltip-text-") as directory: + imports = Path(directory) / "imports" + shutil.copytree(ROOT / "ui/tests/qml/imports/Quickshell", imports / "Quickshell") + shutil.copytree(ROOT / "ui/tests/qml/imports/qs", imports / "qs", dirs_exist_ok=True) + color = imports / "qs/Commons/Color.qml" + color.write_text(color.read_text().replace("QtObject {", 'QtObject {\n readonly property var popups: ({text: "#eeeeee", background: "#222222", border: "#555555"})', 1)) + style = imports / "qs/Commons/Style.qml" + style.write_text(style.read_text().replace("QtObject {", "QtObject {\n readonly property int tooltipDelay: 400", 1)) + source = QML.replace("@STYLE@", json.dumps((ROOT / "app/qml/styles/Omamail").as_uri())) + for key, path in { + "CALENDAR": "calendar/Calendar.js", "COMPONENTS": "components", + }.items(): + source = source.replace("@" + key + "@", json.dumps((ROOT / "ui" / path).as_uri())) + source = source.replace("@ENDPOINT@", json.dumps(f"http://127.0.0.1:{server.server_port}")) + fixture = Path(directory) / "tst_tooltip_text.qml" + fixture.write_text(source) + env = dict(os.environ, TZ="Europe/Berlin", QT_QPA_PLATFORM="offscreen", QT_QUICK_BACKEND="software", + QT_QPA_PLATFORMTHEME="", NO_PROXY="127.0.0.1,localhost", no_proxy="127.0.0.1,localhost") + result = subprocess.run([runner, "-import", str(imports), + "-input", str(fixture)], env=env, timeout=30) + assert requests == ["/control"], f"Unexpected title network requests: {requests}" + result.check_returncode() + print("Tooltip text: native image control observed; no title initiated a request") + finally: + server.shutdown() + server.server_close() + + +if __name__ == "__main__": + main() diff --git a/ui/calendar/Calendar.js b/ui/calendar/Calendar.js index b751178..5bca6ed 100644 --- a/ui/calendar/Calendar.js +++ b/ui/calendar/Calendar.js @@ -1291,6 +1291,81 @@ function weekNowOffset(days, firstHour, lastHour, hourHeight, nowMs) { return -1 } +// Side-by-side lanes for one day's timed events. Events that overlap, even +// through a third one, form a cluster; each event takes the leftmost lane +// free at its start, and every event in a cluster is drawn at the width of +// the cluster's lane count - so three meetings at nine sit in three thirds +// instead of on top of each other, and a lone meeting keeps the whole +// column. An event then stretches over the lanes to its right that stay free +// for its whole length, which is what keeps a short clash from narrowing a +// long meeting beside it for the rest of the day. +// +// Answers [{event, lane, lanes, span}] in the order the events came; a caller +// places an event at lane / lanes of the column, span / lanes wide. +function dayLanes(events, day, hourHeight) { + var height = Math.max(1, Number(hourHeight) || 60) + var values = Array.isArray(events) ? events : [] + var items = [] + for (var i = 0; i < values.length; i++) { + var event = values[i] + if (!event || !event.start || displayInAllDayLane(event)) continue + // Use the actual grid scale: even a zero-length event occupies the + // two-pixel minimum, and repeated local hours share drawn coordinates. + var start = eventTop(event, day, 0, height) + var end = start + eventHeight(event, day, height) + items.push({ event: event, index: i, start: start, end: end, lane: 0, lanes: 1, span: 1 }) + } + items.sort(function(a, b) { return a.start - b.start || b.end - a.end || a.index - b.index }) + var out = [] + var cluster = [] + var laneEnds = [] + var clusterEnd = -Infinity + function close() { + // Each lane is sorted and non-overlapping. Find the first interval ending + // after this block starts instead of rescanning the entire cluster. + var laneItems = [] + for (var l = 0; l < laneEnds.length; l++) laneItems.push([]) + for (var n = 0; n < cluster.length; n++) laneItems[cluster[n].lane].push(cluster[n]) + for (var c = 0; c < cluster.length; c++) { + var item = cluster[c] + item.lanes = laneEnds.length + var span = 1 + for (var next = item.lane + 1; next < laneEnds.length; next++) { + var candidates = laneItems[next] + var low = 0 + var high = candidates.length + while (low < high) { + var middle = Math.floor((low + high) / 2) + if (candidates[middle].end <= item.start) low = middle + 1 + else high = middle + } + if (low < candidates.length && candidates[low].start < item.end) break + span++ + } + item.span = span + out.push(item) + } + cluster = [] + laneEnds = [] + clusterEnd = -Infinity + } + for (var k = 0; k < items.length; k++) { + var current = items[k] + if (current.start >= clusterEnd) close() + var lane = 0 + while (lane < laneEnds.length && laneEnds[lane] > current.start) lane++ + current.lane = lane + laneEnds[lane] = current.end + cluster.push(current) + clusterEnd = Math.max(clusterEnd, current.end) + } + close() + out.sort(function(a, b) { return a.index - b.index }) + return out.map(function(item) { + return { event: item.event, lane: item.lane, lanes: item.lanes, span: item.span } + }) +} + function eventsOnDay(events, day) { var values = Array.isArray(events) ? events : [] var out = [] diff --git a/ui/components/WeekCalendarView.qml b/ui/components/WeekCalendarView.qml index 2a51d42..c6a545e 100644 --- a/ui/components/WeekCalendarView.qml +++ b/ui/components/WeekCalendarView.qml @@ -376,7 +376,7 @@ Item { moved.end = { ms: root.pendingGesture.end } return moved }), modelData) - readonly property var positionedEvents: Calendar.timedLayout(dayEvents, modelData) + readonly property var positionedEvents: Calendar.dayLanes(dayEvents, modelData, timeline.hourHeight) width: (timeline.width - root.timeRailWidth) / Math.max(1, root.days.length) height: parent.height @@ -424,14 +424,18 @@ Item { delegate: Rectangle { id: eventBlock required property var modelData + readonly property real laneWidth: Math.max(0, dayColumn.width - Style.space(6)) / Math.max(1, modelData.lanes) + readonly property bool narrow: width < Style.space(44) readonly property color eventColor: calendarPalette.colorFor( root.controller ? root.controller.colorKeyFor(eventData.sourceId) : "") readonly property var eventData: modelData.event readonly property bool canReschedule: !!root.controller && typeof root.controller.rescheduleRefusal === "function" && root.controller.rescheduleRefusal(eventData) === "" - x: Style.space(3) + modelData.column * (dayColumn.width - Style.space(6)) / modelData.columns - width: (dayColumn.width - Style.space(6)) / modelData.columns - Style.space(2) + x: Style.space(3) + modelData.lane * laneWidth + width: Math.max(0, modelData.span * laneWidth + - (modelData.lane + modelData.span < modelData.lanes + ? Math.min(Style.space(2), laneWidth / 4) : 0)) y: Calendar.eventTop(eventData, dayColumn.modelData, root.firstHour, timeline.hourHeight) height: Calendar.eventHeight(eventData, dayColumn.modelData, timeline.hourHeight) @@ -461,14 +465,15 @@ Item { font.family: root.panelFontFamily font.pixelSize: Style.font.caption font.bold: true - wrapMode: Text.WordWrap - maximumLineCount: eventBlock.height > Style.space(70) ? 2 : 1 + wrapMode: eventBlock.narrow ? Text.NoWrap : Text.WordWrap + maximumLineCount: eventBlock.narrow ? 1 : Math.max(1, Math.floor( + (eventBlock.height - Style.space(10)) / (Style.font.caption * 1.35)) - 1) elide: Text.ElideRight textFormat: Text.PlainText } Text { width: parent.width - visible: eventBlock.height >= Style.space(38) + visible: !eventBlock.narrow && eventBlock.height >= Style.font.caption * 2.9 + Style.space(10) text: width >= timeMetrics.advanceWidth ? timeMetrics.text : Calendar.timeLabel(eventBlock.eventData.start.ms, root.timeFormat) elide: Text.ElideRight @@ -555,6 +560,7 @@ Item { visible: eventBlock.canReschedule && (eventMouse.containsMouse || eventMouse.pressed) } ToolTip { + objectName: "calendar-event-tooltip" visible: eventMouse.containsMouse && !eventMouse.pressed delay: 500 contentItem: Text { diff --git a/ui/tests/test_calendar_feed.js b/ui/tests/test_calendar_feed.js index 5595bee..ac4bbca 100644 --- a/ui/tests/test_calendar_feed.js +++ b/ui/tests/test_calendar_feed.js @@ -413,6 +413,8 @@ assert.strictEqual(feed.displayInAllDayLane(holiday), true) const travelDay = feed.weekDays(new Date(2026,8,2).getTime(),1)[2] assert.strictEqual(feed.allDayEventsOnDay([holiday,dailyTwo],travelDay)[0],holiday) assert.strictEqual(feed.timedLayout([holiday,dailyTwo],travelDay).length,1) +assert.strictEqual(feed.dayLanes([holiday,dailyTwo],travelDay).length,1, + "multi-day events stay in the all-day lane after integrating timed lanes") assert.strictEqual(holiday.start.allDay,false,"display promotion must not mutate timed events") const originalTZ = process.env.TZ process.env.TZ = "Europe/Berlin" @@ -787,3 +789,54 @@ console.log("test_calendar_feed.js ok") assert.strictEqual(made.graph.subject, "Plan") assert.strictEqual(made.recurring, false) } + +// Overlapping timed events share a day column in lanes. +{ + const day = { startMs: new Date(2026, 8, 28).getTime(), endMs: new Date(2026, 8, 29).getTime() } + const at = (h, m) => new Date(2026, 8, 28, h, m || 0).getTime() + const ev = (id, from, to) => ({ uid: id, start: { ms: from, allDay: false }, end: { ms: to, allDay: false } }) + const lanes = (events) => JSON.parse(JSON.stringify(feed.dayLanes(events, day))) + .map(l => [l.event.uid, l.lane, l.lanes, l.span]) + assert.deepStrictEqual(lanes([ev("a", at(9), at(10))]), [["a", 0, 1, 1]], "a lone event keeps the column") + assert.deepStrictEqual(lanes([ev("a", at(9), at(10)), ev("b", at(9), at(10)), ev("c", at(9, 30), at(11))]), + [["a", 0, 3, 1], ["b", 1, 3, 1], ["c", 2, 3, 1]], "three at once are three lanes") + assert.deepStrictEqual(lanes([ev("a", at(9), at(12)), ev("b", at(9), at(10)), ev("c", at(10), at(11))]), + [["a", 0, 2, 1], ["b", 1, 2, 1], ["c", 1, 2, 1]], "a lane is reused once its event has ended") + assert.deepStrictEqual(lanes([ev("a", at(9), at(10)), ev("b", at(13), at(14))]), + [["a", 0, 1, 1], ["b", 0, 1, 1]], "events apart are separate clusters, each whole width") + assert.deepStrictEqual(lanes([ev("long", at(9), at(17)), ev("x", at(9), at(10)), ev("y", at(9), at(10)), ev("z", at(12), at(13))]), + [["long", 0, 3, 1], ["x", 1, 3, 1], ["y", 2, 3, 1], ["z", 1, 3, 2]], + "a later event stretches over the lanes left free beside it") + const shortEvents = [ev("p", at(9), at(9, 5)), ev("q", at(9, 15), at(9, 20))] + assert.deepStrictEqual(lanes(shortEvents), [["p", 0, 1, 1], ["q", 0, 1, 1]], + "preserve main's duration-scaled short blocks") + assert.strictEqual(feed.eventHeight(shortEvents[0], day, 60), 5) + const tiny = [ev("p", at(9), at(9)), ev("q", at(9, 1), at(9, 1))] + assert.strictEqual(feed.dayLanes(tiny, day, 60)[0].lanes, 2) + assert.strictEqual(feed.dayLanes(tiny, day, 240)[0].lanes, 1, + "minimum-height collisions use the view's actual pixels per hour") + assert.deepStrictEqual(lanes([ev("zero", at(9), at(9))]), [["zero", 0, 1, 1]], "a zero-length event still takes a slot") + assert.deepStrictEqual(lanes([ev("p", at(9), at(9)), ev("q", at(9, 1), at(9, 1))]), + [["p", 0, 2, 1], ["q", 1, 2, 1]], "events that overlap as drawn share lanes, not only by the clock") +} + +// Repeated local hours are distinct instants but occupy the same grid pixels. +{ + const previousTZ = process.env.TZ + process.env.TZ = 'Europe/Berlin' + try { + const day = { startMs: new Date('2026-10-25T00:00:00+02:00').getTime(), + endMs: new Date('2026-10-26T00:00:00+01:00').getTime() } + const events = ['+02:00', '+01:00'].map(offset => ({ + start: { ms: new Date('2026-10-25T02:10:00' + offset).getTime() }, + end: { ms: new Date('2026-10-25T02:40:00' + offset).getTime() } + })) + assert.strictEqual(feed.eventTop(events[0], day, 0, 60), feed.eventTop(events[1], day, 0, 60)) + const layout = feed.dayLanes(events, day) + assert.strictEqual(layout[0].lanes, 2) + assert.notStrictEqual(layout[0].lane, layout[1].lane) + } finally { + if (previousTZ === undefined) delete process.env.TZ + else process.env.TZ = previousTZ + } +} From 6f8d80dccedecce26f91140c92761a1d96fed483 Mon Sep 17 00:00:00 2001 From: Oliver Lukschander Date: Mon, 19 Oct 2026 12:50:47 +0200 Subject: [PATCH 41/88] Fix stale draft readers and hide drafts when their send is queued (#367) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Saving a draft replaces it. Gmail, IMAP, Outlook (over IMAP) and JMAP write a new copy and remove the old one. HEY rewrites it in place. The save then reads Drafts again, and on a healthy server that reload puts the list right. The reader does not: it keeps the copy it had open, with the old content, and on the first four with an id that no longer exists. Choosing "Continue editing" on it and saving again writes a second draft. On `main`, the test below ends with both `100:Drafts` and `101:Drafts` on the server. Sending a draft has the opposite problem. The draft stays in Drafts through the undo window (10 s by default) and until the outbox reports it delivered. The current base includes #354, which makes that report reach the window correctly on Qt 6.12. What changes: - **The reader lets go of a saved draft's replaced copy.** When a save is answered, a reader open on the draft it replaced closes and the window goes back to the list. In the wide layout, that leaves the reader pane beside the list empty. The list itself still comes from the save's reload, as before. - **A sent draft leaves Drafts when its send is queued.** Its row leaves the Drafts list, and that list's cached page, as soon as the outbox accepts the send. Drafts is read again, which brings the row back, after an Undo, after a send the outbox refuses or cannot confirm, and after a delivered send whose draft the server kept. HEY always keeps it; the others keep it when removing it fails or takes longer than the outbox's 5 seconds. How, and why there: - `ui/account/DraftList.qml` sits beside the account, like `LabelActions`, because `MailAccount.qml` is at its size ceiling. It has `saved(draftId)`, `drop(draftId, key)` and `reload()`. - `MailAccount` calls them like this: - `saved` from the save's answer; - `drop` once `SendQueue.park` has accepted a send, with the cache key taken when the send was asked for, and from `sentDraftRemoved` on delivery; - `reload` from `reportSendFailure` and from the new `draftKept`. - `SendQueue` calls `draftKept` for a delivered send that named a draft without `draftRemoved`, and when an undo is confirmed. - At queue time, `drop` checks the mailbox role and the key captured when sending began. This protects a different list opened while compose preparation was in flight. The intended scope is Drafts only; reviewer investigation found that custom folders still need an explicit view guard, both when sending from a folder and when switching to one before delivery. See the outstanding finding below. - `App.saveAndLeaveCompose` no longer calls `service.refresh()` after a save. That only started a cache-first load, which the account's own reload overtook straight away. In its place, the window goes back to the list when the reader has lost its message. - Why there: the account owns the list, its cached page and the selection, and the composer and the outbox already hand it `draftId`. No provider is named, the backend is unchanged, and no API revision is needed. - After integration with current main, `MailAccount.qml` is 129,289 bytes and `App.qml` is 129,094 bytes, both under the repository limit. This PR does not add changes to `Service.qml`. Left as before: a reload that lands during the undo window (the two-minute poll, a refresh by hand) lists the queued draft again, because the server still holds it. Delivery removes it again. One case needs #354 on Qt 6.12. A send refused after the undo window is reported by an `outbox.changed` notification, and Qt 6.12 hands its entries over in a form `SendQueue` reads as empty. Without #354 the refusal is not seen, and the draft stays out of the list until the next reload. With #354, it comes back. ## Reviewer status on current head Conflict resolution was pushed as `ec1c4c7eb8b23004eccfe11fda44c930b571c25f`, merging main at `611570da854168e3701ee3dae08f61c9e97c775a`. The only textual conflict was the Makefile QML inventory; both the draft-refresh test and main's account-host test are retained. This is a normal merge, without rebasing or force-pushing. GitHub reports the current head as mergeable without code conflicts. **Product decision: pending.** The recommendation is to keep the focused stale-reader fix and the immediate queued-draft feedback. Jason still needs to accept closing the old reader after save and hiding a queued draft during the undo window. A refresh during that window can still list the server-held draft again; that existing documented limitation is unchanged. **Outstanding P2 — custom-folder views can still lose rows.** `DraftList.qml:30` tests `mailboxKey === "drafts"`, but `selectLabel` leaves that mailbox role unchanged while setting `rawQuery`/`rawLabelId`. Sending from that folder captures the folder's own matching key and removes its row at queue time. After switching from Drafts to a folder during delivery, `MailAccount.sentDraftRemoved` passes the current `cacheKey`, so that key check also passes and removes the folder row. Using the real Service/App/MailAccount with the existing scripted provider, both isolated scenarios failed: the folder's two rows became one. No implementation repair for this finding has been authorized or pushed. The delivery case retains part of main's pre-existing behavior; the new queue-time drop also reaches this incomplete view guard. Add an explicit actual-Drafts view check and regressions asserting both displayed rows and the folder's cached page remain intact, while ordinary Drafts still updates. Reviewer validation on Qt 6.12.0: - `make validate` passed Rust, JavaScript, shell and all 1,487 QML checks with zero failures or skips. All 14 DraftsRefresh cases passed, including the #354-dependent failed-send notification case, which now executes rather than skips. - Separate foreign-account SendQueue investigation passed all 16 checks and verified no draft removal/reload, success notification or outgoing request for another account's receipt. The new helper only changes account-owned selection/list/cache state; normal backend outbox dispatch and credential boundaries are unchanged. **Security: PASS** for these affected ownership and dispatch boundaries, not a complete audit. - The two custom-folder scenarios above fail despite the existing suite passing. These are local isolated investigation fixtures, not committed repairs. - The native sanitized-HTML resource, Outlook HTTP, sidebar text/resource and tooltip text/resource checks passed. `make qml-check`, `omarchy plugin validate .` and `git diff --check` passed. - `make validate` stopped at the standalone application target because this machine has no `cmake`; that build/composition target remains unverified. Current-head CI is still pending. - The four GitHub attachment sets below were downloaded and inspected against the retained workflow change: they show the save clearing the stale reader, queued send removing the row, and light/dark after states at matching dimensions. **UI evidence: PASS** for these demonstrated states; manual interaction evidence remains author-reported. There was no new visual implementation in the conflict resolution. No approval, Request Changes review or merge has been published during this review. The additional P2 repair versus Request Changes decision is pending Jason's choice. ## Original author verification `make -k validate` on 40447bc, Qt 6.12.0 (2b96074 only rewords three comments): the Rust suite (615 library tests, 705 in all), the node and shell suites, the standalone composition test and `qmllint` pass. The QML suite has 1311 passed, 3 failed and 1 skipped. The 3 failures are the `tst_label_ownership.qml` tests that fail on `main` under Qt 6.12, fixed by #354. The skip is the #354-dependent test below. I ran by hand the steps make skips after that failure, and all pass: `tests/test_outlook_http.py` (6), `tests/test_sidebar_text.py` (64), the ignored Qt resource-request test, `make qml-check`, `omarchy plugin validate .` and `git diff --check`. `ui/tests/qml/tst_drafts_refresh.qml` is new and listed for `qmllint`. It drives the real Service, App and MailAccount with a scripted server behind each mailbox, which answers a listing only when the test lets it. - **A save:** - The reader lets go and the window is back on the list. This runs for a draft opened in the reader whose save writes a new copy, one opened in the reader that keeps its id (HEY), and one opened from the list (the wide layout's reader pane). The save's reload then lists the server's copy. - Continuing after a save writes no second draft. - A merged Drafts list of two mailboxes does the same. - **A send:** - It leaves the list and the cached page when queued. - Undo puts it back, and so does a send the outbox does not queue. - Delivered with the draft removed, it stays gone; delivered with the draft kept, it comes back. - Only the Drafts list the send started on loses the row: not a folder opened from Drafts in the meantime, and not Starred. At delivery, a list that is not Drafts keeps its row. - **A send refused after the window**, reported as an `outbox.changed` line through the real `Backend`, puts the draft back. On Qt 6.12 without #354 it skips with that reason. With #354's two files applied, it passes. `tst_send_queue.qml` gains one case: a delivered send whose draft the server kept, and a confirmed undo, report `draftKept`; a send from no draft does not. On `main`, run one at a time, 11 of the 14 new cases fail. Most of the send cases fail at their first step, because `main` keeps the draft listed while it is queued. The 3 that pass are guards: the two cases for other lists, and the refused enqueue. The new `tst_send_queue.qml` case fails on `main` too. Eleven mutations of the change each fail at least one test: - the reader letting go, and App's return to the list; - `drop`'s Drafts-only guard, its key guard, and the key taken when the send was asked for; - the drop at queue time, and the cached page it writes; - the reload for a kept draft, after an undo, and after a failure; - reversing the `draftRemoved` branch. By hand, in the standalone host built from this branch (on the revision before the review, whose save and send paths this one keeps), against an iCloud (IMAP) test mailbox on Qt 6.12.0: a new draft was saved and listed. Edited and saved again, the list showed the new subject within seconds and the reader let go of the replaced copy. Sent, it left Drafts within a second ("Message queued"), stayed gone after a manual refresh, and arrived in Sent. The test mails were removed afterwards. Screenshots: from `main` and from this branch, rendered offscreen with a synthetic Gmail mailbox served by a scripted client: ![After saving an edited draft from the reader, before and after: the reader lets go of the replaced copy](https://github.com/user-attachments/assets/d85dac17-84db-4deb-a2b3-9999c5a49f51) ![Right after sending a draft, before and after: it leaves Drafts at once](https://github.com/user-attachments/assets/149492ef-3e54-4ece-adb7-8bd7284c4888) ![After a save, both themes and sizes](https://github.com/user-attachments/assets/dc02463e-e95d-4daa-aa24-ff426106042f) ![After a send, both themes and sizes](https://github.com/user-attachments/assets/9a2309f2-fe52-4868-a152-74c0668ace09) 1. Drafts after saving "Notes from the design review" from the reader as "Design review: three decisions", once the save's reload has listed it. Tokyo Night, 980×720, before | after. Before, the list is right but the reader still shows the replaced copy with "Continue editing". After, the reader pane is empty. 2. Drafts right after sending "Lunch on Friday?", with the undo toast up, before | after. Before, it is still listed. After, it is gone. 3. The after state of 1 in Tokyo Night and Flexoki Light, at 980×720 and 760×520. 4. The after state of 2 in Tokyo Night and Flexoki Light, at 980×720 and 760×520. A fresh agent reviewed the change with AGENTS.md, with an empty context; security PASS. It found that a sent draft the server keeps (HEY always, Gmail and IMAP when removing it fails) stayed hidden until the next poll, that lists other than Drafts were changed too, and that an earlier version's row for a saved draft, shown before the reload landed, kept the replaced copy's id. The first two are fixed with tests, and that row was removed, which made the change smaller. ## Release Notes - After you save a draft you were reading, the reader closes instead of showing the copy the save replaced, so continuing to edit it no longer creates a second draft. - A draft you send leaves Drafts as soon as it is queued. It comes back if you undo the send, if the send fails, or if the server keeps the sent draft. ## Contact Thanks for reading! Happy to answer questions or change anything: here, by email (oliver@lukschander.com), on X (@olukschander) or Discord (oliverlukschander). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 Co-authored-by: Jason Lee --- Makefile | 4 +- ui/App.qml | 2 +- ui/account/DraftList.qml | 44 +++ ui/account/MailAccount.qml | 25 +- ui/account/SendQueue.qml | 8 +- ui/tests/qml/tst_drafts_refresh.qml | 593 ++++++++++++++++++++++++++++ ui/tests/qml/tst_send_queue.qml | 17 +- 7 files changed, 675 insertions(+), 18 deletions(-) create mode 100644 ui/account/DraftList.qml create mode 100644 ui/tests/qml/tst_drafts_refresh.qml diff --git a/Makefile b/Makefile index 3104e0b..c1a6260 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ QMLLINT := /usr/lib/qt6/bin/qmllint QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml ui/compose/PendingCompose.qml \ ui/tests/compatibility/tst_published_agent.qml \ ui/tests/qml/tst_unified_batch.qml ui/tests/qml/tst_clipboard_arguments.qml \ - ui/tests/qml/tst_reader_subject_menu.qml ui/tests/qml/tst_account_hosts.qml \ + ui/tests/qml/tst_reader_subject_menu.qml ui/tests/qml/tst_drafts_refresh.qml ui/tests/qml/tst_account_hosts.qml \ ui/backend/Backend.qml ui/backend/Runtime.qml ui/diagnostics/Diagnostics.qml \ ui/components/BackendSetup.qml ui/components/OmamailLogo.qml \ - ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/UnifiedBatchAction.qml ui/account/UnifiedBatchSelection.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ + ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/DraftList.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/UnifiedBatchAction.qml ui/account/UnifiedBatchSelection.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ ui/cache/CacheStore.qml ui/cache/BodyCache.qml \ ui/providers/AuthManager.qml ui/providers/GmailApiClient.qml \ ui/providers/OutlookAuth.qml \ diff --git a/ui/App.qml b/ui/App.qml index 9d80688..e6313d3 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -771,7 +771,7 @@ Item { if (warning !== "" && service && typeof service.note === "function") service.note(warning) draftSavedTimer.restart() - if (service.mailboxKey === "drafts") service.refresh() + if (currentView === "reader" && service.selectedId === "") backToList() }) } diff --git a/ui/account/DraftList.qml b/ui/account/DraftList.qml new file mode 100644 index 0000000..f9959f3 --- /dev/null +++ b/ui/account/DraftList.qml @@ -0,0 +1,44 @@ +import QtQuick +import "Model.js" as Model + +// What a save or a send of a draft already says about the Drafts list and the +// reader, applied when it is known rather than when the server next lists the +// drafts. Beside the account rather than in it, which is at its size ceiling. +QtObject { + id: draftList + + required property var account + + // A save writes a new copy and removes the old one on Gmail, IMAP, Outlook + // and JMAP, and rewrites it in place on HEY. Either way the copy the reader + // holds is out of date, and where the save wrote a new copy, editing the old + // one again wrote a second draft. + function saved(draftId) { + var id = String(draftId || "") + if (id !== "" && account.selectedId === id) account.clearSelection() + } + + // A draft being sent is not a draft any more. Its row, and its copy in the + // cache, go when the send is queued and again once the draft is removed, + // from the Drafts list the send started on (`key`) and no other. A send + // that is undone or does not go, and a sent draft the server kept, read the + // list again, which shows the draft where it is. + function drop(draftId, key) { + var id = String(draftId || "") + if (id === "") return + if (account.selectedId === id) account.clearSelection() + if (account.mailboxKey !== "drafts" || account.cacheKey !== key + || Model.indexById(account.messages, id) < 0) return + account.messages = Model.removeById(account.messages, id) + account.rememberList() + } + + // The Drafts list read from the server again, when it is the list on screen. + function reload() { + if (account.mailboxKey !== "drafts") return false + account.listSerial++ + account.nextPageToken = "" + account.loadMessages(false, true, "") + return true + } +} diff --git a/ui/account/MailAccount.qml b/ui/account/MailAccount.qml index 232645c..b4a158f 100644 --- a/ui/account/MailAccount.qml +++ b/ui/account/MailAccount.qml @@ -2195,6 +2195,7 @@ Item { // guards are reachable and not only the transport's own error. function reportSendFailure(error, sendId) { fail(error) + draftList.reload() // A zero-delay send can be rejected synchronously by a provider before // ComposeView has returned from service.send() and parked its accepted // draft. Cross the event-loop boundary so every terminal signal observes @@ -2218,14 +2219,12 @@ Item { } function sentDraftRemoved(draftId) { - if (Model.indexById(messages, draftId) >= 0) { - messages = Model.removeById(messages, draftId) - rememberList() - } - if (selectedId === draftId) clearSelection() + draftList.drop(draftId, cacheKey) refreshCounts() } + function draftKept() { draftList.reload() } + function deliverPending() { return sendQueue.deliverAll() } function undoSend(callback) { return sendQueue.undoLatest(callback) } @@ -2235,6 +2234,11 @@ Item { account: root } + DraftList { + id: draftList + account: root + } + // A mailbox whose token was just refused is not ready until the next // lookup answers — and the next lookup is asked for by the next request. // A save or a send that arrived in that window failed as "not ready" for @@ -2304,18 +2308,13 @@ Item { } var child = root.api.saveDraft(payload, function(saved, error) { if (handle.aborted || account !== root.accountId) return + if (!error) draftList.saved(composeFields.draftId) if (typeof callback === "function") callback(saved, error) // The Drafts list on screen is what the server had before the save: the // copy replaced is gone there and the new one is not yet listed, so // a list left as it was showed both — the old row until the next poll, // and a second row for every save. Read it again from the server now. - if (!error && root && root.mailboxKey === "drafts") { - root.listSerial++ - root.nextPageToken = "" - root.loadMessages(false, true, "") - } else if (!error && root) { - root.refreshCounts() - } + if (!error && root && !draftList.reload()) root.refreshCounts() }) if (child) handle.children.push(child) }) @@ -2391,6 +2390,7 @@ Item { draftId: String(values.draftId || "") }) var account = accountId + var draftsKey = cacheKey if (!backend) { reportSendFailure("Mail backend is unavailable", id); return "" } backend.call("message.compose", {fields: composeFields}, function(payload, error) { if (account !== root.accountId) return @@ -2398,6 +2398,7 @@ Item { payload.draftId = String(values.draftId || "") payload.sendId = id if (!sendQueue.park(payload, id, order)) root.reportSendFailure("Could not queue this message", id) + else draftList.drop(payload.draftId, draftsKey) }) return id } diff --git a/ui/account/SendQueue.qml b/ui/account/SendQueue.qml index 20b414d..8be34ce 100644 --- a/ui/account/SendQueue.qml +++ b/ui/account/SendQueue.qml @@ -76,8 +76,11 @@ QtObject { done[id] = entry.state receipts.push(id) account.reportSendSuccess(entry.result || {}, id) - if (entry.result && entry.result.draftRemoved && String(entry.draftId || "") !== "" - && typeof account.sentDraftRemoved === "function") account.sentDraftRemoved(String(entry.draftId)) + var draft = String(entry.draftId || "") + if (draft !== "" && entry.result && entry.result.draftRemoved + && typeof account.sentDraftRemoved === "function") account.sentDraftRemoved(draft) + // HEY keeps a sent draft, and the others do when removing it failed. + else if (draft !== "" && typeof account.draftKept === "function") account.draftKept() } else if (entry.state === "failed" || entry.state === "unknown") { done[id] = entry.state account.reportSendFailure(entry.state === "unknown" @@ -175,6 +178,7 @@ QtObject { var pending = undoPending undoPending = null undoBusy = false + if (id && typeof account.draftKept === "function") account.draftKept() if (pending && typeof pending.callback === "function") pending.callback(id) } function reconcileUndo() { diff --git a/ui/tests/qml/tst_drafts_refresh.qml b/ui/tests/qml/tst_drafts_refresh.qml new file mode 100644 index 0000000..4b57133 --- /dev/null +++ b/ui/tests/qml/tst_drafts_refresh.qml @@ -0,0 +1,593 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "../.." as Omamail +import "BackendFixture.js" as BackendFixture +import "ModelFixture.js" as NativeModel +import "NativeIntentFixture.js" as NativeIntentFixture +import "NativeDomainFixture.js" as NativeDomain +import "../../account/Accounts.js" as Accounts + +// The Drafts list and the reader after a save and after a send, through the +// real Service, App and MailAccount, with a scripted mail server behind every +// mailbox. +// +// A save replaces the draft: Gmail, IMAP, Outlook and JMAP write a new copy and +// remove the old one, HEY rewrites it in place. The reader kept showing the old +// copy, and editing it again wrote a second draft. A send waited out the undo +// window, and a sent draft the server kept was taken out of the list for good. +// The server here answers a listing only when a test lets it, so the list can +// be seen before and after the reload. +Item { + width: 980 + height: 720 + + QtObject { + id: shellStore + function updateEntryInline(_id, _entry) {} + function hide(_id) {} + } + + // Each mailbox's Drafts folder as its server holds it. `replaces` is a save + // that writes a new copy and removes the old one, as Gmail, IMAP, Outlook and + // JMAP do; otherwise the copy keeps its id, as on HEY. + QtObject { + id: server + property var boxes: [[], []] + property var owners: ["", ""] + property bool replaces: true + property int nextUid: 100 + property var saved: [] + property bool holding: false + property var held: [] + function rows(box) { return boxes[box] } + function find(box, id) { + var list = boxes[box] + for (var i = 0; i < list.length; i++) if (list[i].id === id) return list[i] + return null + } + function put(box, list) { + var next = boxes.slice() + next[box] = list + boxes = next + } + // The listings asked for while holding, answered with what the server + // holds now. + function release() { + var waiting = held + held = [] + holding = false + for (var i = 0; i < waiting.length; i++) waiting[i]() + } + } + + // A Gmail-shaped resource for a row the server holds. + function resource(box, row) { + return ({ id: row.id, threadId: row.id, labelIds: ["DRAFT"], snippet: row.snippet, + internalDate: String(row.dateMs), payload: { mimeType: "text/plain", headers: [ + { name: "From", value: "Me <" + server.owners[box] + ">" }, + { name: "To", value: "ada@example.org" }, + { name: "Subject", value: row.subject }], + body: { data: Qt.btoa(row.subject + " body").replace(/\+/g, "-").replace(/\//g, "_") } } }) + } + + // What a save wrote, read back out of the message the compose step built: + // its subject, and the first text part, which that step encodes as base64. + function written(raw) { + var text = Qt.atob(String(raw).replace(/-/g, "+").replace(/_/g, "/")) + var subject = (/^Subject: ([^\r\n]*)/m.exec(text) || ["", ""])[1] + var part = /Content-Transfer-Encoding: base64\r?\n(?:[^\r\n]+\r?\n)*\r?\n([A-Za-z0-9+\/=\r\n]+)/.exec(text) + return { subject: subject, body: part ? Qt.atob(part[1].replace(/\s+/g, "")).replace(/\s+/g, " ").trim() : "" } + } + + component ScriptedClient: QtObject { + id: client + property int box: 0 + property var auth: null + property string email: "" + function handle() { return ({ aborted: false }) } + function later(fn) { Qt.callLater(fn); return handle() } + function listMessages(query, limit, token, callback) { + var box = client.box + function answer() { + var ids = server.rows(box).slice().sort(function(a, b) { return b.dateMs - a.dateMs }) + .map(function(row) { return row.id }) + callback({ ids: ids, estimate: ids.length, nextPageToken: "" }, "") + } + if (server.holding) server.held = server.held.concat([answer]) + else Qt.callLater(answer) + return handle() + } + function getMessages(ids, full, callback) { + var out = [] + for (var i = 0; i < ids.length; i++) { + var row = server.find(box, ids[i]) + if (row) out.push(resource(box, row)) + } + return later(function() { callback(out, "") }) + } + function saveDraft(payload, callback) { + var fields = written(payload.raw) + var previous = String(payload.draftId || "") + var before = server.find(box, previous) + server.saved = server.saved.concat([previous]) + var copy = { id: previous, subject: fields.subject, dateMs: Date.now(), + snippet: before && before.snippet !== "" ? fields.body : "" } + if (server.replaces || previous === "") copy.id = String(server.nextUid++) + ":Drafts" + server.put(box, server.rows(box).filter(function(row) { return row.id !== previous }).concat([copy])) + return later(function() { callback({ saved: true, warning: "" }, "") }) + } + function deleteDraft() { return handle() } + function getMessage() { return handle() } + function getSummaries(ids, callback) { return later(function() { callback([], "") }) } + function getLabels(callback) { return later(function() { callback([], "") }) } + function getLabelCounts(id, callback) { return later(function() { callback({ id: id, unread: 0, total: 0, threadsUnread: 0 }, "") }) } + function getProfile(callback) { return later(function() { callback({ email: server.owners[box] }, "") }) } + function getSendAs(callback) { + return later(function() { callback([{ email: server.owners[box], displayName: "", isPrimary: true, isDefault: true }], "") }) + } + function getAttachment() { return handle() } + function trashMessage() { return handle() } + function untrashMessage() { return handle() } + function batchModify(ids, add, remove, callback) { return later(function() { callback(null, "") }) } + function modifyMessage(id, add, remove, callback) { return later(function() { callback(null, "") }) } + function sendMessage() { return handle() } + function createLabel() { return handle() } + function renameLabel() { return handle() } + function deleteLabel() { return handle() } + function abortRequest(h) { if (h) h.aborted = true } + } + + Component { id: firstClient; ScriptedClient { box: 0 } } + Component { id: secondClient; ScriptedClient { box: 1 } } + + Omamail.Service { + id: mailService + shell: shellStore + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-test" }) + } + + Omamail.App { id: app; service: mailService } + + TestCase { + name: "DraftsRefresh" + when: windowShown + + property var fixture: null + // The query cache, by account and key, as the backend keeps it. + property var cached: ({}) + property var enqueued: [] + // The outbox as the backend holds it, by send id. + property var outbox: ({}) + property int outboxRevision: 1 + property bool refuseEnqueue: false + // Compose requests held back, so the list can change before a send is queued. + property bool holdCompose: false + property var heldCompose: [] + + function cacheKey(params) { + return params.key !== undefined ? String(params.key) : String(params.query || "").trim() + "|" + (params.limit || 25) + } + + function initTestCase() { + fixture = BackendFixture.install(mailService) + fixture.answers = { + "cache.queryRestore": function() { return { generation: 1, store: { version: 2, account: "", profile: null, labels: [], session: null } } }, + "cache.queryGet": function(params) { + var entry = cached[params.accountId + "#" + cacheKey(params)] || null + return { key: cacheKey(params), entry: entry, summaries: entry ? entry.summaries : [], local: [], stale: false } + }, + "cache.queryPut": function(params) { + var entry = JSON.parse(JSON.stringify(params.page)) + var next = Object.assign({}, cached) + next[params.accountId + "#" + cacheKey(params)] = entry + cached = next + return { key: cacheKey(params), entry: entry } + }, + "cache.queryInvalidate": function() { return { invalidated: true } }, + "cache.queryLabels": function(params) { return { labels: params.labels } }, + "cache.queryProfile": function(params) { return { profile: params.profile } }, + "cache.queryBind": function(params) { return { store: { version: 2, account: params.email, profile: null, labels: [], session: null } } }, + "reader.open": function(params) { + if (params.cacheOnly) return null + var box = String(params.accountId) === mailService.accountAt(0).accountId ? 0 : 1 + var row = server.find(box, String(params.id)) + return row ? NativeDomain.readerProjection(resource(box, row), params) : null + }, + "message.compose": function(params, request) { + if (!holdCompose) return NativeDomain.answer("message.compose", params) + heldCompose = heldCompose.concat([request]) + return undefined + }, + "outbox.enqueue": function(params, request) { + enqueued = enqueued.concat([params]) + if (refuseEnqueue) { + BackendFixture.respond(mailService, request, null, { code: -32000, message: "outbox_storage_unavailable" }) + return undefined + } + return { snapshot: outboxSnapshot(params, "queued") } + }, + "outbox.undo": function(params) { + var sent = enqueued.filter(function(entry) { return entry.sendId === params.sendId })[0] + return { id: params.sendId, snapshot: outboxSnapshot(sent, "cancelled") } + }, + "outbox.snapshot": function(params) { + var entries = [] + for (var id in outbox) if (outbox[id].accountId === params.accountId) entries.push(outbox[id]) + return { accountId: params.accountId, revision: ++outboxRevision, entries: entries } + }, + "outbox.forget": function(params) { + return { snapshot: { accountId: params.accountId, revision: ++outboxRevision, entries: [] } } + } + } + NativeModel.install(mailService) + NativeIntentFixture.install(mailService) + } + + function outboxEntry(params, state, result) { + return { id: params.sendId, accountId: params.accountId, state: state, queuedAt: Date.now(), + dueAt: Date.now() + 10000, order: params.order, draftId: String(params.payload.draftId || ""), + result: result || {} } + } + + function outboxSnapshot(params, state) { + var entry = outboxEntry(params, state) + var next = Object.assign({}, outbox) + next[params.sendId] = entry + outbox = next + return { accountId: params.accountId, revision: ++outboxRevision, entries: [entry] } + } + + function releaseCompose() { + var waiting = heldCompose + heldCompose = [] + holdCompose = false + for (var i = 0; i < waiting.length; i++) + BackendFixture.respond(mailService, waiting[i], NativeDomain.answer("message.compose", waiting[i].params)) + } + + function sendQueueOf(account) { + var items = account.data + for (var i = 0; i < items.length; i++) + if (items[i] && typeof items[i].park === "function" && typeof items[i].apply === "function") return items[i] + return null + } + + // The outbox reports the send as delivered when the window next reads it, + // as it does after the undo window: a reply, not a signal, so this path + // does not depend on how Qt hands a signal's lists over. + function deliver(account, result) { + var params = enqueued[0] + if (params.sendId in outbox) server.put(0, server.rows(0).filter(function(row) { + return !(result && result.draftRemoved) || row.id !== String(params.payload.draftId) + })) + var next = Object.assign({}, outbox) + next[params.sendId] = outboxEntry(params, "sent", result) + outbox = next + sendQueueOf(account).sync() + tryVerify(function() { return sendQueueOf(account).deliveryStates[params.sendId] === "sent" }) + } + + property int seeded: 0 + // Mailboxes on Drafts holding these rows, read from their servers the way + // the window reads them. + function seed(boxes, unified) { + server.boxes = [boxes[0], boxes.length > 1 ? boxes[1] : []] + server.saved = [] + server.held = [] + server.holding = false + var list = Accounts.emptyList() + var owners = [] + for (var i = 0; i < boxes.length; i++) { + seeded++ + owners.push("me" + seeded + "@example.com") + list = Accounts.add(list, { email: owners[i], provider: "imap", clientSecret: "", label: "", signature: "", + clientId: "", imap: { imapHost: "imap.example.com", imapPort: 993, smtpHost: "smtp.example.com", + smtpPort: 465, username: owners[i], aliases: [], insecure: false } }) + } + server.owners = owners.concat([""]) + mailService.setUnifiedMailboxes(false) + mailService.activeIndex = -1 + mailService.accountList = list + mailService.accountsLoaded = true + wait(0) + mailService.refreshCurrent() + var clients = [firstClient, secondClient] + var accounts = [] + for (var h = 0; h < boxes.length; h++) { + var account = mailService.accountAt(h) + verify(account !== null) + account.clientOverride = clients[h] + account.auth.toolsChecked = true + account.auth.missingTools = [] + account.auth.passwordChecked = true + account.auth.password = "test-password" + tryCompare(account, "ready", true) + // A host can outlive the account it held, and its profile with it. + account.profile = ({ email: owners[h] }) + account.sendAsAliases = [{ email: owners[h], displayName: "", isPrimary: true, isDefault: true }] + accounts.push(account) + } + if (unified) mailService.setUnifiedMailboxes(true) + mailService.selectMailbox("drafts") + for (var a = 0; a < accounts.length; a++) { + accounts[a].messages = [] + accounts[a].loadMessages(false, true, "") + } + var expected = boxes[0].length + (boxes.length > 1 ? boxes[1].length : 0) + tryVerify(function() { return mailService.messages.length === expected }, 3000) + return accounts + } + + function draft(id, subject, snippet, ageMinutes) { + return { id: id, subject: subject, snippet: snippet, dateMs: Date.now() - ageMinutes * 60000 } + } + + function named(item, objectName) { + if (!item) return null + if (item.objectName === objectName) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = named(values[i], objectName) + if (found) return found + } + return null + } + + function composeView() { + function walk(item) { + if (!item) return null + if (item.beginDraft !== undefined && item.fieldsForDraft !== undefined) return item + var values = item.children || [] + for (var i = 0; i < values.length; i++) { + var found = walk(values[i]) + if (found) return found + } + return null + } + return walk(app) + } + + // The list as it reads on screen: subject and snippet, row by row. + function listed() { + return mailService.messages.map(function(row) { return row.subject + " / " + row.snippet }) + } + + function cachedSubjects(account) { + var entry = cached[account.accountId + "#" + account.cacheKey] + return entry ? entry.summaries.map(function(row) { return row.subject }) : [] + } + + // Edit the draft open in the composer and save it with the exit dialog's + // Save, holding the reload's listing so the list is seen before it lands. + function editAndSave(subject, body) { + var compose = composeView() + tryCompare(compose, "opened", true) + named(compose, "compose-subject-field").text = subject + named(compose, "compose-body-editor").text = body + compose.noteUserModified() + server.holding = true + app.goBack() + named(app, "compose-exit-dialog").save() + tryCompare(app, "draftSavedToast", "Draft saved") + } + + // Open a draft the way a reader opens one: a click, then Continue editing. + function editFromReader(id) { + app.openMessage(id) + tryVerify(function() { return mailService.detailPainted }) + compare(app.currentView, "reader") + app.editDraft(id) + } + + function sendOpenDraft() { + var compose = composeView() + tryCompare(compose, "opened", true) + compose.submit() + tryCompare(compose, "opened", false) + } + + function init() { + enqueued = [] + outbox = ({}) + refuseEnqueue = false + holdCompose = false + heldCompose = [] + app.draftSavedToast = "" + mailService.clearSelection() + app.resetNavigation() + app.open() + } + + function cleanup() { + server.release() + releaseCompose() + var compose = composeView() + if (compose.opened) compose.finish() + app.close() + } + + // ------------------------------------------------------------ a save + + function test_a_save_lets_go_of_the_copy_it_replaced_data() { + return [ + { tag: "a new copy, from the reader", replaces: true, reader: true, id: "100:Drafts" }, + // HEY rewrites the draft in place: the reader's copy is out of date all the same. + { tag: "the same id, from the reader", replaces: false, reader: true, id: "1:Drafts" }, + // Opened from the list, the reader beside it holds the draft too. + { tag: "a new copy, from the list", replaces: true, reader: false, id: "100:Drafts" } + ] + } + + function test_a_save_lets_go_of_the_copy_it_replaced(data) { + server.replaces = data.replaces + server.nextUid = 100 + var account = seed([[draft("1:Drafts", "Old subject", "old body", 60), + draft("2:Drafts", "Other draft", "other body", 30)]])[0] + if (data.reader) editFromReader("1:Drafts") + else app.openOrEdit("1:Drafts") + editAndSave("New subject", "New body for Ada") + compare(server.saved, ["1:Drafts"]) + compare(mailService.selectedId, "", "nothing holds the copy the save replaced") + compare(app.currentView, "list") + verify(server.held.length > 0, "the save reads Drafts again") + server.release() + tryVerify(function() { return listed()[0] === "New subject / New body for Ada" }, 3000, "list: " + listed().join(" | ")) + compare(account.messages.map(function(row) { return row.id }).join(","), data.id + ",2:Drafts") + } + + // The copy a reader kept could be edited again, and saving it wrote a + // second draft beside the first save's. + function test_continuing_after_a_save_writes_no_second_draft() { + server.replaces = true + server.nextUid = 100 + seed([[draft("1:Drafts", "Old subject", "old body", 60), draft("2:Drafts", "Other draft", "other body", 30)]]) + editFromReader("1:Drafts") + editAndSave("New subject", "New body") + server.release() + tryVerify(function() { return listed()[0] === "New subject / New body" }, 3000) + if (mailService.selectedId !== "" && app.editDraft(mailService.selectedId)) { + tryCompare(composeView(), "opened", true) + editAndSave("Newer subject", "Newer body") + server.release() + } + wait(100) + compare(server.rows(0).map(function(row) { return row.id }).sort().join(","), "100:Drafts,2:Drafts", + "one copy of the draft") + } + + function test_a_merged_drafts_list_lets_go_of_the_saved_draft() { + server.replaces = true + server.nextUid = 100 + var accounts = seed([[draft("1:Drafts", "Old subject", "old body", 60)], + [draft("7:Drafts", "Second mailbox", "second body", 30)]], true) + compare(mailService.unified, true) + var target = mailService.messages.filter(function(row) { return row.subject === "Old subject" })[0].id + editFromReader(target) + editAndSave("New subject", "New body") + compare(mailService.selectedId, "") + compare(app.currentView, "list") + server.release() + tryVerify(function() { return listed().join(" | ") === "New subject / New body | Second mailbox / second body" }, + 3000, "merged: " + listed().join(" | ")) + compare(accounts[1].messages.length, 1, "the other mailbox is left as it was") + } + + // ------------------------------------------------------------ a send + + function test_a_draft_leaves_drafts_when_its_send_is_queued() { + var account = seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]])[0] + app.openOrEdit("1:Drafts") + sendOpenDraft() + tryVerify(function() { return enqueued.length === 1 }) + compare(enqueued[0].payload.draftId, "1:Drafts") + tryVerify(function() { return listed().join(" | ") === "Other draft / other" }, 2000, "list: " + listed().join(" | ")) + tryVerify(function() { return cachedSubjects(account).join(",") === "Other draft" }, 2000, + "the list kept for the next visit: " + cachedSubjects(account)) + compare(mailService.selectedId, "") + } + + function test_an_undone_send_puts_its_draft_back() { + seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]]) + app.openOrEdit("1:Drafts") + sendOpenDraft() + tryVerify(function() { return listed().join(" | ") === "Other draft / other" }) + verify(app.undoPendingSend()) + tryVerify(function() { return listed().join(" | ") === "Other draft / other | To Ada / hello" }, 3000) + } + + function test_a_send_that_is_not_queued_puts_its_draft_back() { + refuseEnqueue = true + seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]]) + app.openOrEdit("1:Drafts") + sendOpenDraft() + tryVerify(function() { return enqueued.length === 1 }) + tryVerify(function() { return listed().join(" | ") === "Other draft / other | To Ada / hello" }, 3000) + } + + function test_a_delivered_draft_stays_gone() { + var account = seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]])[0] + app.openOrEdit("1:Drafts") + sendOpenDraft() + tryVerify(function() { return listed().join(" | ") === "Other draft / other" }) + deliver(account, { draftRemoved: true }) + wait(200) + compare(listed().join(" | "), "Other draft / other") + compare(cachedSubjects(account).join(","), "Other draft") + } + + // HEY keeps a sent draft, and the others do when removing it failed or + // took longer than the outbox waits: the server still lists it. + function test_a_sent_draft_the_server_kept_comes_back() { + var account = seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]])[0] + app.openOrEdit("1:Drafts") + sendOpenDraft() + tryVerify(function() { return listed().join(" | ") === "Other draft / other" }) + deliver(account, { warning: "Sent, but a follow-up operation failed" }) + verify(server.find(0, "1:Drafts") !== null) + tryVerify(function() { return listed().join(" | ") === "Other draft / other | To Ada / hello" }, 3000, + "after delivery: " + listed().join(" | ")) + } + + // A send refused after the undo window is reported by an `outbox.changed` + // notification. On Qt 6.12 its entries cross the signal as a sequence that + // SendQueue reads as no entries, so the refusal is not seen at all until + // #354 (Plain.plain); the draft then stays out of the list until the next + // reload. Where the refusal is seen, the draft is back. + function test_a_send_refused_after_the_window_puts_its_draft_back() { + var account = seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]])[0] + app.openOrEdit("1:Drafts") + sendOpenDraft() + tryVerify(function() { return listed().join(" | ") === "Other draft / other" }) + var params = enqueued[0] + mailService.backend.receive(JSON.stringify({ jsonrpc: "2.0", method: "outbox.changed", params: { + accountId: account.accountId, revision: ++outboxRevision, entries: [outboxEntry(params, "failed")] } })) + wait(200) + if (sendQueueOf(account).deliveryStates[params.sendId] !== "failed") + skip("this Qt hands the notification's entries over as a sequence; needs #354") + tryVerify(function() { return listed().join(" | ") === "Other draft / other | To Ada / hello" }, 3000) + } + + // Only the Drafts list a send started on loses the draft: a list opened + // since is left to its own reload, a folder opened from Drafts included. + function test_a_draft_leaves_only_the_drafts_list_the_send_started_on_data() { + return [ + { tag: "a folder since", folder: "Projects" }, + { tag: "Starred since", mailbox: "starred" } + ] + } + + function test_a_draft_leaves_only_the_drafts_list_the_send_started_on(data) { + var account = seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]])[0] + app.openOrEdit("1:Drafts") + holdCompose = true + sendOpenDraft() + tryVerify(function() { return heldCompose.length === 1 }) + if (data.folder) account.selectLabel(data.folder, data.folder) + else account.selectMailbox(data.mailbox) + tryVerify(function() { return account.rawQuery !== "" || account.mailboxKey !== "drafts" }) + tryVerify(function() { return !account.listLoading && account.messages.length === 2 }, 3000) + compare(account.mailboxKey === "drafts", !!data.folder) + releaseCompose() + tryVerify(function() { return enqueued.length === 1 }) + wait(100) + compare(listed().join(" | "), "Other draft / other | To Ada / hello") + } + + // A list that is not Drafts keeps its row when the draft is removed: there + // the row may be the message the draft became, so its own reload decides. + function test_a_delivered_draft_is_taken_only_from_drafts() { + var account = seed([[draft("1:Drafts", "To Ada", "hello", 60), draft("2:Drafts", "Other draft", "other", 30)]])[0] + app.openOrEdit("1:Drafts") + sendOpenDraft() + tryVerify(function() { return listed().join(" | ") === "Other draft / other" }) + server.holding = true + account.selectMailbox("starred") + server.release() + tryVerify(function() { return !account.listLoading && account.messages.length === 2 }, 3000) + server.holding = true + deliver(account, { draftRemoved: true }) + wait(100) + compare(listed().join(" | "), "Other draft / other | To Ada / hello") + } + } +} diff --git a/ui/tests/qml/tst_send_queue.qml b/ui/tests/qml/tst_send_queue.qml index 033650f..ffccf71 100644 --- a/ui/tests/qml/tst_send_queue.qml +++ b/ui/tests/qml/tst_send_queue.qml @@ -26,9 +26,11 @@ Item { property var successes: [] property var failures: [] property var removed: [] + property int kept: 0 function reportSendSuccess(result, id) { successes = successes.concat([id]) } function reportSendFailure(message, id) { failures = failures.concat([id]) } function sentDraftRemoved(id) { removed = removed.concat([id]) } + function draftKept() { kept++ } function note(message) {} function deliver(payload) { throw new Error("UI must never deliver mail") } } @@ -41,7 +43,7 @@ Item { queue.parked = []; queue.submitted = ({}); queue.handled = ({}); queue.revision = -1 queue.deliveryStates = ({}) queue.undoBusy = false; queue.undoPending = null; queue.uncertain = ({}); queue.serverEntries = []; queue.arm(); backend.calls = [] - account.successes = []; account.failures = []; account.removed = []; account.sending = false + account.successes = []; account.failures = []; account.removed = []; account.kept = 0; account.sending = false } function snapshot(revision, entries) { return {accountId: account.accountId, revision: revision, entries: entries} } function entry(id, state) { return {id: id, state: state, order: 1, queuedAt: Date.now(), dueAt: Date.now() + 10000} } @@ -105,6 +107,19 @@ Item { compare(queue.parked.length,0);compare(account.successes.length,1);compare(account.removed[0],"draft1") queue.apply(snapshot(4,[sent]));compare(account.successes.length,1) } + // A draft the server kept after the send (HEY always, the others when + // removing it failed) and a draft whose send was undone are still drafts. + function test_a_draft_still_on_the_server_is_reported_kept() { + var sent=entry("send1","sent");sent.draftId="draft1";sent.result={warning:"Sent, but a follow-up operation failed"} + queue.apply(snapshot(1,[sent])) + compare(account.removed,[]);compare(account.kept,1) + var plain=entry("send2","sent");plain.result={} + queue.apply(snapshot(2,[plain]));compare(account.kept,1,"a send from no draft names none") + queue.apply(snapshot(3,[entry("send3","queued")])) + verify(queue.undoLatest(function(){})) + backend.calls[0].callback({id:"send3",snapshot:snapshot(4,[entry("send3","cancelled")])},null) + compare(account.kept,2) + } function test_lost_enqueue_acknowledgement_does_not_claim_failure_or_retry() { verify(queue.park({raw:"private"},"lost",1)) backend.calls[0].callback(null,{message:"Backend disconnected"}) From 396f375fc832791af50190a47a60c8351254cfe8 Mon Sep 17 00:00:00 2001 From: Marc Date: Mon, 19 Oct 2026 12:53:07 +0200 Subject: [PATCH 42/88] Fix the GMX preset to suggest imap.gmx.net for gmx.de and gmx.net addresses (#383) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #370. GMX runs two services under one name. International GMX (gmx.com) documents `imap.gmx.com` and `mail.gmx.com` ([support.gmx.com](https://support.gmx.com/pop-imap/imap/server.html)). GMX Germany, which serves gmx.de and gmx.net addresses, documents `imap.gmx.net` and `mail.gmx.net` ([hilfe.gmx.net](https://hilfe.gmx.net/pop-imap/imap/imap-serverdaten.html)). The IMAP preset listed all three domains in one row with the gmx.com servers, so the setup form filled in the international servers for every German address. The row is now split. gmx.de and gmx.net get `imap.gmx.net:993` and `mail.gmx.net:465`. gmx.com keeps `imap.gmx.com:993` and `mail.gmx.com:465`. Both GMX help pages document 993 and 465 with TLS on connect, so the ports, TLS behavior and `insecure: false` stay as they were. Other GMX country domains (gmx.at, gmx.ch, …) are deliberately not added until someone checks which service they belong to. Only the suggestion changes. The setup page still stops suggesting once the server fields are edited (`serversTouched`), and a saved account loads its stored servers through `syncFromStore`, so accounts already saved with `imap.gmx.com` keep those servers. ## Screenshots Standalone app (`make app-build`, run with empty temporary XDG config/cache/data/state directories), Omarchy theme *Wallhaven new*, window 1896×1030. The address was typed and the server settings expanded; nothing was connected. | `jane@gmx.de` (after) | `jane@gmx.com` (after) | |---|---| | jane_de | jane_com| Before/after for `jane@gmx.de`, rendered offscreen with `qmltestrunner` and the repository's test theme imports (software backend, 640×560, identical setup for both): | Before (`origin/main`) | After | |---|---| |before-gmx-de |after-gmx-de | The same offscreen render for `jane@gmx.com` is byte-identical before and after. ## Verification `make validate` green on this commit (Rust 1.99.1, Qt 6.11.2). The qmllint warnings it prints are the expected unresolved-import ones; the exit code is 0. `ui/tests/test_imap.js` now covers the GMX rows: gmx.de, gmx.net and an upper-case GMX.DE address get `imap.gmx.net:993` / `mail.gmx.net:465`, gmx.com keeps `imap.gmx.com:993` / `mail.gmx.com:465`, `insecure` stays false, and both build `imaps://` and `smtps://` URLs. I watched it fail against `origin/main` (first on `presetFor("jane@gmx.de").id`: `actual: 'gmx', expected: 'gmx-de'`; with that line removed, on `actual: 'imap.gmx.com', expected: 'imap.gmx.net'`) and pass with the change. By hand: the standalone app from this checkout, isolated from the real configuration as described above, filled in the expected servers for `jane@gmx.de` and `jane@gmx.com` (screenshots above). One theme and one window size only, because the change alters the text of two prefilled fields and no layout. The plugin inside `omarchy-shell` was not driven by hand. Not tested with a live login. A fresh agent with no context from this work reviewed the diff against `AGENTS.md`, `CONTRIBUTING.md` and `docs/providers/imap.md` and found nothing blocking. It searched for other users of the preset (none; `ImapSetupPage.qml` reads only the host, port, note and guide fields) and checked that saved accounts and edited server fields keep their values. Security: PASS (constant hosts that pass `isValidHost`, implicit TLS ports, `insecure` false, no credential or transport code touched). Its two suggestions are applied: the test now pins the TLS URLs and the preset ids, and its comment states the rule. ## Release Notes - Adding a gmx.de or gmx.net mailbox now fills in GMX Germany's servers (imap.gmx.net, mail.gmx.net) instead of the international gmx.com servers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- ui/providers/ImapProtocol.js | 13 ++++++++++++- ui/tests/test_imap.js | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/ui/providers/ImapProtocol.js b/ui/providers/ImapProtocol.js index 7b184d3..dd832b9 100644 --- a/ui/providers/ImapProtocol.js +++ b/ui/providers/ImapProtocol.js @@ -102,13 +102,24 @@ var PRESETS = [ imapHost: "imap.zoho.com", imapPort: 993, smtpHost: "smtp.zoho.com", smtpPort: 465 }, + // GMX is two services under one name. gmx.com is the international one; + // gmx.de and gmx.net are GMX Germany, which documents its own servers at + // hilfe.gmx.net. The other country domains are not listed until someone + // checks which of the two they belong to. { id: "gmx", name: "GMX", - domains: ["gmx.com", "gmx.net", "gmx.de"], + domains: ["gmx.com"], imapHost: "imap.gmx.com", imapPort: 993, smtpHost: "mail.gmx.com", smtpPort: 465 }, + { + id: "gmx-de", + name: "GMX", + domains: ["gmx.de", "gmx.net"], + imapHost: "imap.gmx.net", imapPort: 993, + smtpHost: "mail.gmx.net", smtpPort: 465 + }, // Here for the note rather than the servers: imap.163.com and smtp.163.com // on the standard ports are exactly what `imap.` would have guessed. // What no address can imply is that the mailbox refuses the account diff --git a/ui/tests/test_imap.js b/ui/tests/test_imap.js index 44b4ec7..9b84832 100644 --- a/ui/tests/test_imap.js +++ b/ui/tests/test_imap.js @@ -37,6 +37,31 @@ assert.strictEqual(imap.suggestedSettings("jane@126.com").imapHost, "imap.126.co assert.strictEqual(imap.suggestedSettings("jane@yeah.net").smtpHost, "smtp.yeah.net") assert.strictEqual(imap.suggestedSettings("jane@126.com").note, "") +// GMX Germany (gmx.de, gmx.net) and international GMX (gmx.com) document +// different servers, so a German address must not be handed the .com ones. +// Both stay TLS on connect. +assert.strictEqual(imap.presetFor("jane@gmx.de").id, "gmx-de") +assert.strictEqual(imap.presetFor("jane@gmx.com").id, "gmx") +for (const address of ["jane@gmx.de", "jane@gmx.net", "Jane@GMX.DE"]) { + const gmx = imap.suggestedSettings(address) + assert.strictEqual(gmx.imapHost, "imap.gmx.net", address) + assert.strictEqual(gmx.smtpHost, "mail.gmx.net", address) + assert.strictEqual(gmx.imapPort, 993, address) + assert.strictEqual(gmx.smtpPort, 465, address) + assert.strictEqual(gmx.insecure, false, address) +} +const gmxDe = imap.setupSettings(Object.assign({ address: "jane@gmx.de" }, imap.suggestedSettings("jane@gmx.de"))) +assert.ok(imap.imapUrl(gmxDe, "INBOX").startsWith("imaps://imap.gmx.net:993/")) +assert.ok(imap.smtpUrl(gmxDe).startsWith("smtps://mail.gmx.net:465")) +const gmxCom = imap.suggestedSettings("jane@gmx.com") +assert.strictEqual(gmxCom.imapHost, "imap.gmx.com") +assert.strictEqual(gmxCom.smtpHost, "mail.gmx.com") +assert.strictEqual(gmxCom.imapPort, 993) +assert.strictEqual(gmxCom.smtpPort, 465) +const gmxComSetup = imap.setupSettings(Object.assign({ address: "jane@gmx.com" }, gmxCom)) +assert.ok(imap.imapUrl(gmxComSetup, "INBOX").startsWith("imaps://imap.gmx.com:993/")) +assert.ok(imap.smtpUrl(gmxComSetup).startsWith("smtps://mail.gmx.com:465")) + // An unknown domain still gets a guess: imap. is right far more often // than an empty field is useful, and a wrong guess is visible and editable. const guess = imap.suggestedSettings("jane@example.org") From dbbb0f123da56ccab3349d5b2c1a20f3e5e15f49 Mon Sep 17 00:00:00 2001 From: Marc Date: Mon, 19 Oct 2026 13:19:42 +0200 Subject: [PATCH 43/88] Fix Gmail access tokens being reused after they expired during suspend (#384) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes the expiry part of #371. After a laptop resumed from suspend, Google calendar requests failed with `calendar_auth_refused` for a minute or two. The Gmail access-token cache in `AccountSession` (`src/providers/gmail.rs`) decided whether a grant was still fresh by `Instant` alone. Linux's monotonic clock stops while the machine sleeps, so a grant Google had expired during a long suspend still looked fresh afterwards and was sent with every Google calendar request until a Gmail mail request happened to get a 401 and evict it. Anyone with a Gmail account and its Google calendar on a machine that suspends is affected; the visible symptom is a transient calendar error banner and refreshes that keep cached events after wake. The cached `Token` now carries a wall-clock deadline (`SystemTime`) beside the monotonic one, both measured from the moment the grant arrived, and is reused only while both are ahead. Wall time catches the sleep; the monotonic deadline still bounds reuse if the wall clock is moved backwards. This is the rule Outlook's token cache has followed since #317 (issue #238). The same check replaces the monotonic-only one in the session eviction rule in `Session::account`. The 60-second margin is unchanged: it is still subtracted from the lifetime when the grant is stored. `token_with` now delegates to `token_with_clock`, which takes the clock as a parameter so the tests can simulate a suspend without sleeping. Production calls pass `Instant::now()` and `SystemTime::now()`. Out of scope on purpose, as asked in #371: no change to the calendar retry policy. A Google calendar request still does not reject a token on 401 and retry, and no write is retried. Sharing one expiry helper between the Gmail and Outlook caches would be a refactor and is left for a follow-up. ## Independent reviewer validation On exact head `3b41f0bb479a65a21e6a5c640e740f1725fe5697`, Rust 1.99.0: `cargo test --locked --lib providers::gmail` passed all 49 tests using isolated XDG state and an external TMPDIR. Temporarily restoring the old monotonic-only freshness predicate made two of the four new clock tests fail; restoring the exact-head source made all four pass. The working tree was restored clean. No live account or real suspend was used, and full `make validate` was not independently rerun for this Rust-only change. Source review found no introduced blocker. **Security: PASS** for the affected token expiry/reuse boundary, supported by the controlled-clock tests and existing Gmail concurrency, invalidation and native HTTP boundary regressions. **UI evidence: NOT APPLICABLE.** Product decision: not applicable; this restores existing expiry semantics. The combined suspend-plus-backward-wall-clock limitation and calendar 401 retry policy remain as documented below. Exact-head CI remains pending (the macOS arm64 standalone-app job was still running at the final review read); Native credential contracts passed. No approval or merge has been published. This independent runtime evidence supersedes the earlier comment's missing-local-test limitation; pending CI remains an approval gate. ## Author verification `make validate` green on this commit (Rust 1.99.1, Qt 6.11.2). The qmllint warnings it prints are the expected unresolved-import ones; the exit code is 0. Four new tests in `src/providers/gmail_tests.rs` drive `token_with_clock` with an injected clock and a fake grant (3600 s, cached for 3540 s): - `cached_token_expires_on_wall_time_slept_through_suspend`: 10 s awake, 2 h of wall time, so a new grant is fetched. - `cached_token_survives_a_short_suspend_until_its_wall_deadline`: a 10-minute nap and the last second of the wall lifetime reuse the grant; the wall deadline itself fetches a new one. - `cached_token_stays_bounded_by_monotonic_time_when_wall_clock_moves_back`: monotonic deadline reached while the wall clock went back 1 h, so a new grant is fetched. - `cached_token_is_reused_while_both_clocks_are_inside_its_lifetime`: no needless refresh. Because `token_with_clock` is new, the tests cannot compile against `origin/main` as is. To watch them fail I reverted `Token::fresh_at` to the old monotonic-only check (`self.expires > monotonic`): the two suspend tests fail (`left: "grant-1"`, `right: "grant-2"`), the other two pass. Changing the wall comparison from `>` to `>=` makes the deadline test fail. With the change, all 4 pass, and `cargo test --lib providers::gmail` passes (49 tests). Not driven by hand: there is no UI change, and reproducing it live needs more than an hour of suspend with a development backend and a Google account. The original evidence is in #371: every isolated `calendar_auth_refused` came 30–110 s after a resume in `journalctl`. A fresh agent with no context from this work reviewed the diff against `AGENTS.md`, `CONTRIBUTING.md` and `docs/SECURITY-BOUNDARIES.md`. It found no correctness, concurrency or lock problems: the clock is read under the token lock after `check()`, and `reject`, invalidation and eviction behave as before. It checked that every Gmail and Google calendar token path goes through `token_with`, and that overflow is unreachable because the lifetime is capped at 86 340 s. Security: PASS (token values, logging, argv and settings are untouched; reuse only becomes stricter). Its findings were applied: the commit referenced #238 instead of #317, and it asked for a wall-deadline boundary and a short-suspend case, which are now the second test. It also noted a remaining gap that Outlook shares: a suspend combined with a backward wall-clock step can still reuse a stale grant until the monotonic deadline; closing that would need `CLOCK_BOOTTIME`. ## Release Notes - Google calendars no longer fail with a sign-in error for a minute or two after the computer wakes from sleep. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- src/providers/gmail.rs | 39 +++++++++++++--- src/providers/gmail_tests.rs | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 6 deletions(-) diff --git a/src/providers/gmail.rs b/src/providers/gmail.rs index 9572616..6369510 100644 --- a/src/providers/gmail.rs +++ b/src/providers/gmail.rs @@ -8,7 +8,7 @@ use std::{ Arc, Mutex, atomic::{AtomicBool, Ordering}, }, - time::{Duration, Instant}, + time::{Duration, Instant, SystemTime}, }; #[path = "gmail_queue.rs"] @@ -23,6 +23,21 @@ mod writes; struct Token { value: String, expires: Instant, + wall_expires: SystemTime, +} + +impl Token { + // Linux's monotonic clock stops during suspend, so a token that expired + // while the machine slept still looks fresh to `Instant` alone. Wall time + // catches sleep; the monotonic deadline still bounds reuse if the wall + // clock moves backwards. + fn fresh_at(&self, monotonic: Instant, wall: SystemTime) -> bool { + self.expires > monotonic && self.wall_expires > wall + } + + fn fresh(&self) -> bool { + self.fresh_at(Instant::now(), SystemTime::now()) + } } // Gmail meters each user at 250 quota units per second, a moving average @@ -101,11 +116,22 @@ impl AccountSession { async fn token_with>>( &self, refresh: impl FnOnce() -> F, + ) -> Result, &'static str> { + self.token_with_clock(refresh, || (Instant::now(), SystemTime::now())) + .await + } + + /// `token_with` on a supplied clock, so tests can sleep the machine. + async fn token_with_clock>>( + &self, + refresh: impl FnOnce() -> F, + clock: impl Fn() -> (Instant, SystemTime), ) -> Result, &'static str> { // Refresh coalesces per account. Invalidation never waits on this lock. let mut cached = self.token.lock().await; self.check()?; - if let Some(token) = cached.as_ref().filter(|t| t.expires > Instant::now()) { + let (monotonic, wall) = clock(); + if let Some(token) = cached.as_ref().filter(|t| t.fresh_at(monotonic, wall)) { return Ok(Arc::clone(token)); } let answer = refresh().await; @@ -121,9 +147,12 @@ impl AccountSession { .unwrap_or(3600) .min(86400) .saturating_sub(60); + let lifetime = Duration::from_secs(lifetime); + let (monotonic, wall) = clock(); let token = Arc::new(Token { value, - expires: Instant::now() + Duration::from_secs(lifetime), + expires: monotonic + lifetime, + wall_expires: wall + lifetime, }); *cached = Some(Arc::clone(&token)); Ok(token) @@ -296,9 +325,7 @@ impl Session { accounts.retain(|_, session| { Arc::strong_count(session) > 1 || session.token.try_lock().map_or(true, |token| { - token - .as_ref() - .is_some_and(|token| token.expires > Instant::now()) + token.as_ref().is_some_and(|token| token.fresh()) }) }); } diff --git a/src/providers/gmail_tests.rs b/src/providers/gmail_tests.rs index 70a864c..97beaaf 100644 --- a/src/providers/gmail_tests.rs +++ b/src/providers/gmail_tests.rs @@ -282,6 +282,96 @@ async fn concurrent_requests_coalesce_refresh_for_one_account() { assert_eq!(refreshes.get(), 1); } +// A grant from `grant` lives 3600 s, cached for 3540 s after the margin. +async fn token_at( + session: &AccountSession, + refreshes: &Cell, + clock: (Instant, SystemTime), +) -> String { + let token = session + .token_with_clock( + || async { + refreshes.set(refreshes.get() + 1); + grant(&format!("grant-{}", refreshes.get())) + }, + || clock, + ) + .await + .unwrap(); + token.value.clone() +} + +#[tokio::test] +async fn cached_token_expires_on_wall_time_slept_through_suspend() { + let session = &AccountSession::new(); + let refreshes = &Cell::new(0); + let (monotonic, wall) = (Instant::now(), SystemTime::now()); + assert_eq!( + token_at(session, refreshes, (monotonic, wall)).await, + "grant-1" + ); + // Ten awake seconds, two hours of suspend: the monotonic clock alone + // still calls the grant fresh, but Google expired it an hour ago. + let resumed = ( + monotonic + Duration::from_secs(10), + wall + Duration::from_secs(2 * 3600), + ); + assert_eq!(token_at(session, refreshes, resumed).await, "grant-2"); + assert_eq!(refreshes.get(), 2); +} + +#[tokio::test] +async fn cached_token_stays_bounded_by_monotonic_time_when_wall_clock_moves_back() { + let session = &AccountSession::new(); + let refreshes = &Cell::new(0); + let (monotonic, wall) = (Instant::now(), SystemTime::now()); + assert_eq!( + token_at(session, refreshes, (monotonic, wall)).await, + "grant-1" + ); + let corrected = ( + monotonic + Duration::from_secs(3540), + wall - Duration::from_secs(3600), + ); + assert_eq!(token_at(session, refreshes, corrected).await, "grant-2"); +} + +#[tokio::test] +async fn cached_token_is_reused_while_both_clocks_are_inside_its_lifetime() { + let session = &AccountSession::new(); + let refreshes = &Cell::new(0); + let (monotonic, wall) = (Instant::now(), SystemTime::now()); + assert_eq!( + token_at(session, refreshes, (monotonic, wall)).await, + "grant-1" + ); + let later = Duration::from_secs(3539); + assert_eq!( + token_at(session, refreshes, (monotonic + later, wall + later)).await, + "grant-1" + ); + assert_eq!(refreshes.get(), 1); +} + +#[tokio::test] +async fn cached_token_survives_a_short_suspend_until_its_wall_deadline() { + let session = &AccountSession::new(); + let refreshes = &Cell::new(0); + let (monotonic, wall) = (Instant::now(), SystemTime::now()); + assert_eq!( + token_at(session, refreshes, (monotonic, wall)).await, + "grant-1" + ); + let awake = Duration::from_secs(10); + // A ten-minute nap leaves most of the lifetime, so no needless refresh. + let napped = (monotonic + awake, wall + Duration::from_secs(600)); + assert_eq!(token_at(session, refreshes, napped).await, "grant-1"); + let last_second = (monotonic + awake, wall + Duration::from_secs(3539)); + assert_eq!(token_at(session, refreshes, last_second).await, "grant-1"); + let deadline = (monotonic + awake, wall + Duration::from_secs(3540)); + assert_eq!(token_at(session, refreshes, deadline).await, "grant-2"); +} + #[tokio::test] async fn invalidate_is_idempotent_without_config_or_keyring() { let session = &Session::default(); From e1aa6ab3b6c2e02daf4ae09f938bc9423e7a8a8c Mon Sep 17 00:00:00 2001 From: Marc Date: Mon, 19 Oct 2026 13:35:58 +0200 Subject: [PATCH 44/88] Fix Save changes dropping a replacement password on a signed-in IMAP mailbox (#386) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes the Save changes half of #369, and very likely the "password is not saved" part of #280. When an IMAP server starts refusing a mailbox's saved password (a changed password, a revoked app password, IMAP access switched off and on again), the mailbox still counts as signed in: `ImapAuth.loggedIn` only means a password was loaded. Its setup page therefore shows **Save changes** and **Sign out**, not **Connect the mailbox**. Save changes saved the servers and silently ignored the password field, so a corrected password or app password typed there was never verified or stored, and every background request kept sending the refused one. The only way out was Sign out and Connect again. This affects anyone whose IMAP password changes after setup; the user who hit it changed GMX servers and app passwords several times without any of it reaching the keyring. What changes: - **Save changes with a typed password goes through the sign-in path** (`ImapSetupPage.save()` → `signIn()` → `ImapAuth.signIn` → `ImapClient.verifyCredentials`). The replacement is verified before it is stored; a refused one leaves the saved password in place and stays in the field to be corrected; the field clears once the new one is stored (`credentialsSaved`). Save changes without a password still saves only the settings. While the password is checked, Save changes reads "Checking" and is disabled, like Connect the mailbox. - **A replacement keeps the mailbox in service while it is checked.** It is tracked by a new `ImapAuth.replacementBusy` instead of `loginBusy`, the way `OutlookAuth` keeps `graphRoundBusy` apart. With `loginBusy` the mailbox became `signing_in`; with one mailbox, `App.rootState.anyReady` flipped, the window rebuilt its root while the password was checked and again when the answer arrived, and the setup page closed with the refusal in it. The hands-on test below found this. A first sign-in (no password yet) still uses `loginBusy` and `signing_in`. - **One sign-in attempt at a time, each with a generation.** `ImapAuth.signIn` refuses while an attempt is in flight, and `ImapClient` now passes `sessionGeneration` back for IMAP too (it already did for OAuth). Before, pressing Enter in the password field while a password was being checked started a second attempt and the first answer was applied to the password typed last, which could store an unverified password. Sign out, cancel and switching mailboxes abandon the attempt in flight, so a late answer is dropped instead of completing it, and sign-in is not left refused. Out of scope, on purpose (one change per PR): - Taking a mailbox out of the signed-in state when the server refuses its saved password (`mail_auth_failed`). `ImapAuth.invalidateAccessToken` exists for that but nothing calls it; that is the other half of #369. - The status-bar text "The server rejected the sign-in. Sign in again." names a control the signed-in page does not have. - JMAP's Save changes also re-verifies through `loginBusy`, so with a single JMAP mailbox it probably closes its page the same way. Not verified. - After a successful replacement, a backend IDLE task that hit `mail_auth_failed` stays in its back-off until it retries; the list and counts refresh at once through `afterSignIn`. ## Screenshots Hands-on recording with dummy credentials, final code of this branch. The standalone app was built from this checkout and run against a synthetic loopback IMAP server (STARTTLS, self-signed certificate, empty mailbox) that accepts exactly one password, read from a file on every LOGIN. App configuration, cache and state lived in temporary XDG directories, and the app ran in its own `dbus-run-session` with its own `gnome-keyring-daemon`, so nothing touched a real mailbox or keyring. The mailbox was connected with one password, the server was then switched to accept only another, and the recording starts from that refused state: Save changes with `wrong-password` (refused, the page stays, the error is shown under the field, the password stays), then with `new-password` (accepted, stored, the field clears, "Mailbox saved"). Omarchy theme *Wallhaven new*, window 1896×1030. https://github.com/user-attachments/assets/d6ce8b43-8ce7-405a-aaea-6bcb9f9e4fb4 Before (`origin/main`, left) and after (this branch, right), rendered offscreen with `qmltestrunner`, the repository's test theme imports and a real `ImapAuth` holding a saved password (software backend, 640×520 each): a replacement typed; right after Save changes; after the server refused it. Before, Save changes did nothing visible and the password was never sent to the server; the second and third states are identical. pr369-before-after ## Independent reviewer status Reviewed exact head `276c17a7b3aa48e4956ca357570a4a1a0326ac5b`. The existing Save changes workflow should verify and store a typed replacement, so this fix is useful and appropriately scoped; keeping the mailbox ready while verification runs avoids destroying the settings page. Product decision: not applicable, this repairs existing behavior rather than adding a new workflow. **Outstanding P2 — serialize replacements through the credential write.** `ImapAuth.completeSignIn` clears both busy flags before calling `storePassword`, while `signIn` only checks those flags and the setup page also ignores `credentialWriteBusy`. If the first `credentials.put` callback is delayed, a second replacement is accepted and can pass verification; `storePassword` then silently returns because the first write is still busy. A controlled Qt probe over the real `ImapAuth` accepted both verified replacements but observed only one credential write (the first password), while the in-memory password was the second. The real Service uses asynchronous RPC for credential writes, so this is an actual unprotected window. Keep the replacement operation guarded until its storage outcome, or queue/correlate subsequent verified writes, and cover the delayed callback through both auth and the setup page. The existing new credential fixture calls its write callback synchronously and therefore misses this case. No implementation repair has been made by the reviewer. **P3 — register the new QML tests for lint.** `tst_imap_password_replacement.qml` and `tst_app_imap_password_replacement.qml` are missing from Makefile `QML_FILES`, despite the repository requiring new QML files there. Test discovery executes them, but `make qml-check` omits them. An explicit separate qmllint invocation for these files passes. Independent validation on Qt 6.12.0: - `make validate` passed Rust, JavaScript, shell and all 1,481 QML checks, with zero failures or skips. The new password test file passed 15 Qt checks, and the App-level refused-replacement case passed. - The additional delayed-keyring-write probe fails: second replacement accepted, only one `credentialPut` request, and that request contains the first password while memory holds the second. The probe is isolated and synthetic, not a tracked test or production repair. - Existing native HTML, Outlook HTTP, sidebar and tooltip resource checks passed; `make qml-check`, explicit lint of both new QML tests, `omarchy plugin validate .` and `git diff --check` passed. `make validate` stopped at the standalone app target because this machine has no `cmake`. Exact-head CI now has a failed macOS arm64 standalone-app job: the Rust test `providers::m365::tests::wire::confirmed_mutations_do_not_wait_for_or_fail_on_an_unused_response_body` failed at `src/providers/m365/tests/wire.rs:462`, with `succeededIds = []` for a confirmed PATCH instead of the expected one ID (`stall=false`). The job is https://github.com/huacnlee/omamail/actions/runs/39219123710/job/118020486897 . This PR changes only QML and QML tests; the M365 test/transport source is unchanged. The same test passed in the local Linux full suite, but that does not establish the macOS failure as flaky or pre-existing. The failure remains an independent approval gate; no CI rerun or speculative M365 repair was performed. Linux and Windows standalone-app CI jobs passed, which is separate upstream evidence rather than a local build pass. - **Security: PASS** scoped to verified-before-store and account/generation ownership in the tested paths: refusal/control characters and abandoned/foreign-account attempts do not store a password, credentials use existing IPC storage/verification paths, and no new logging/argv/HTML destination boundary is introduced. The P2 is a demonstrated persistence/completion bug; the probe does not demonstrate unverified-secret storage, credential disclosure or a cross-account write. This is not a complete credential-lifecycle audit. - **UI evidence: PASS** for the supplied matching before/after typed/checking/refused states. The recording was downloaded and sampled to confirm the retained settings page and successful field clearing/Mailbox saved state. Its server/keyring setup is author-reported; the reviewer did not operate a real mailbox or keyring. No unrelated visual matrix is required for this focused existing form correction. No approval, Request Changes publication, fixes or pushes have been performed. The recommendation is to repair the bounded P2 and lint inventory omission, then revalidate; Jason has been asked to choose help fixing or Request Changes. ## Author verification `make validate` green on this commit (Rust 1.99.1, Qt 6.11.2); the qmllint warnings it prints are the expected unresolved-import ones, and the exit code is 0. New tests: - `ui/tests/qml/tst_imap_password_replacement.qml` (10 tests, 13 cases): the setup page over a real `ImapAuth` with a saved password, through Save changes and Enter: the replacement is verified before anything is stored; a refusal keeps the field, shows the error and keeps the saved password; a stored replacement clears the field, a keyring write failure does not; Save changes without a password only saves settings; "Checking" and disabled while checking; Enter during a check starts no second attempt and the verified password, not the last typed, is stored. At the `ImapAuth` level: a refused replacement stores nothing; a verified one overwrites; a replacement uses `replacementBusy` and a first sign-in `loginBusy`; a second attempt is refused in both answer orders; sign out, cancel and another mailbox each free sign-in and drop the late answer, which also cannot finish the next attempt. - `ui/tests/qml/tst_app_imap_password_replacement.qml`: the real `Service` and `App` over the stubbed backend transport, one IMAP mailbox, Settings → Edit → a replacement → Save changes → `mail_auth_failed`. The mailbox stays `ready` and the same setup page stays open during the check, the refusal is shown on that page, the field keeps the password, and nothing is stored. Watched failing against the unfixed code. With the `origin/main` setup page (plus the two new `objectName`s) over this branch's `ImapAuth`, the App test fails (the typed password is never verified) and so do the four page tests for verification, the refusal shown in the field, "Checking"/Enter and the keyring-failure case. With `origin/main` page, `ImapAuth` and `ImapClient` together, the App test fails the same way and every test in `tst_imap_password_replacement.qml` already fails in `init()`, because `replacementBusy` does not exist there. Reverting single parts of `ImapAuth`: without the `loginBusy || replacementBusy` guard both overlap orders fail; with replacements using `loginBusy` again the App test fails (`signing_in` instead of `ready`). By hand: the recording above, in the standalone app only; the plugin inside `omarchy-shell` was not driven by hand. The synthetic server's log matches the recording: the saved password refused, `wrong-password` refused, `new-password` accepted, then the mailbox loading with it. An earlier hand-test of this branch, before `replacementBusy`, is what showed the setup page closing on a single-mailbox install. Two fresh agents with no context from this work reviewed the diff against `AGENTS.md`, `CONTRIBUTING.md`, `docs/KEYS.md` and `docs/SECURITY-BOUNDARIES.md`. The first found the overlapping-attempt store of an unverified password and asked for tests over a real `ImapAuth`. The second, on a version that kept the mailbox ready through a `Model.setupState` special case keyed on `authMode === "password"`, found that signing out mid-check left sign-in refused until the old answer arrived (or forever if `ImapClient` dropped it), suggested the `replacementBusy` design used here instead, and contributed the App-level test. All of that is applied. Both reported Security: PASS: the typed password only travels the existing verification path, is stored only after verification and under the account that asked, and never on refusal or after an abandoned attempt. ## Release Notes - A new password or app password typed into a signed-in IMAP mailbox's settings is now checked and saved by Save changes, instead of being ignored. If the server refuses it, the error stays on the settings page and the saved password is kept. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- ui/components/ImapSetupPage.qml | 21 +- ui/providers/ImapAuth.qml | 35 ++- ui/providers/ImapClient.qml | 2 +- .../qml/tst_app_imap_password_replacement.qml | 95 +++++++ .../qml/tst_imap_password_replacement.qml | 269 ++++++++++++++++++ 5 files changed, 414 insertions(+), 8 deletions(-) create mode 100644 ui/tests/qml/tst_app_imap_password_replacement.qml create mode 100644 ui/tests/qml/tst_imap_password_replacement.qml diff --git a/ui/components/ImapSetupPage.qml b/ui/components/ImapSetupPage.qml index 9f9c979..c1043b4 100644 --- a/ui/components/ImapSetupPage.qml +++ b/ui/components/ImapSetupPage.qml @@ -30,7 +30,7 @@ Column { readonly property var auth: service ? service.auth : null readonly property bool signedIn: !!auth && auth.loggedIn - readonly property bool busy: !!auth && auth.loginBusy + readonly property bool busy: !!auth && (auth.loginBusy || auth.replacementBusy === true) readonly property bool toolsMissing: !!auth && auth.toolsChecked && auth.missingTools.length > 0 // Recomputed as the address is typed, and only ever used to *fill* the @@ -114,8 +114,17 @@ Column { // Saved before the password is tried, so a mailbox that the server rejects // still has its settings to correct rather than an empty form to fill again. + // + // A password typed into a signed-in mailbox is a replacement, usually for + // one the server has started refusing. It goes through `signIn`, which + // verifies it before it is stored; saving only the servers would drop it + // without a word and keep sending the refused one. function save() { if (!service) return + if (passwordField.text !== "") { + signIn() + return + } var address = validatedAddress() if (address === "") return var check = Imap.validateSettings(currentSettings()) @@ -134,7 +143,7 @@ Column { } function signIn() { - if (!service) return + if (!service || busy) return var address = validatedAddress() if (address === "") return var check = Imap.validateSettings(currentSettings()) @@ -160,6 +169,9 @@ Column { function onLastErrorChanged() { if (root.auth && root.auth.lastError !== "") errorText.text = root.auth.lastError } + // Stored means done with: left in the field, the next Save changes would + // verify it all over again. A refused one stays, to be corrected. + function onCredentialsSaved() { passwordField.text = "" } } // ------------------------------------------------------------------ hero @@ -305,6 +317,7 @@ Column { TextField { id: passwordField + objectName: "imap-password-field" anchors.left: parent.left anchors.right: parent.right // Masked by default: this window is shoulder-surfable. Readable on @@ -469,8 +482,10 @@ Column { } Button { + objectName: "imap-save-button" visible: root.signedIn - text: "Save changes" + text: root.busy ? "Checking" : "Save changes" + enabled: !root.busy foreground: root.textColor bordered: true fontSize: Style.font.bodySmall diff --git a/ui/providers/ImapAuth.qml b/ui/providers/ImapAuth.qml index d539ce3..003416c 100644 --- a/ui/providers/ImapAuth.qml +++ b/ui/providers/ImapAuth.qml @@ -51,6 +51,14 @@ Item { // them without knowing which provider it has. readonly property bool credentialsPresent: configured property bool loginBusy: false + // A signed-in mailbox checking a replacement password. Kept apart from + // `loginBusy`, which would take the mailbox out of service while it is + // checked: with one mailbox the window would rebuild its root and close the + // setup page, and a refusal with it. + property bool replacementBusy: false + // Which attempt an answer belongs to. Sign-out, a cancel or another mailbox + // moves it on, so a late answer cannot finish an attempt nobody is waiting on. + property int sessionGeneration: 0 property bool credentialLookupBusy: false property bool credentialWriteBusy: false property int credentialLookupSerial: 0 @@ -167,6 +175,10 @@ Item { // will answer everything else — rather than by being written down first and // failing silently later. function signIn(secret) { + // One attempt at a time: `pendingPassword` holds a single value, so a + // second attempt started before the first answers would have that answer + // applied to the password typed last, and an unverified one stored. + if (loginBusy || replacementBusy) return false if (platform && platform.canAccessCredentials === false) { lastError = "Install or update the mail backend before signing in" return false @@ -193,7 +205,9 @@ Item { return false } lastError = "" - loginBusy = true + sessionGeneration++ + if (password !== "") replacementBusy = true + else loginBusy = true pendingPassword = value verifyRequested(settings, settings.username + ":" + value) return true @@ -202,8 +216,11 @@ Item { // The client owns the transport, so it performs the check and reports back. signal verifyRequested(var settings, string credentials) - function completeSignIn(ok, error) { + function completeSignIn(ok, error, generation) { + if (generation !== undefined && generation !== sessionGeneration) return + if (!loginBusy && !replacementBusy) return loginBusy = false + replacementBusy = false if (!ok) { pendingPassword = "" lastError = safeError(error) || "The server rejected that username or password" @@ -248,8 +265,8 @@ Item { function logout() { var boundAccount = accountId + abandonSignIn() password = "" - pendingPassword = "" passwordChecked = true if (credentialWriteBusy && credentialWriteAccount === boundAccount) pendingCredentialDelete = boundAccount @@ -270,12 +287,22 @@ Item { // `MailAccount` should not have to ask which provider it holds before // calling one. function beginLogin() { /* the setup form drives sign-in, not a browser */ } - function cancelLogin() { loginBusy = false } + function cancelLogin() { abandonSignIn() } + + // Nothing is waiting for the attempt in flight any more; its answer, if it + // ever comes, finds a newer generation and is dropped. + function abandonSignIn() { + sessionGeneration++ + loginBusy = false + replacementBusy = false + pendingPassword = "" + } onAccountIdChanged: { // A different mailbox has a different password. Dropping the one in memory // is what stops an account rename from leaving the previous account's // credential in front of the new one's server. + abandonSignIn() password = "" passwordChecked = false lookupHandled = false diff --git a/ui/providers/ImapClient.qml b/ui/providers/ImapClient.qml index 83f48b1..9c439a0 100644 --- a/ui/providers/ImapClient.qml +++ b/ui/providers/ImapClient.qml @@ -321,7 +321,7 @@ Item { target: root.auth function onVerifyRequested(settings, credentials) { var owner = root.auth - var generation = root.oauthTransport ? owner.sessionGeneration : undefined + var generation = owner.sessionGeneration root.verifyCredentials(settings, credentials, function(ok, error) { if (root.auth === owner) owner.completeSignIn(ok, error, generation) }) diff --git a/ui/tests/qml/tst_app_imap_password_replacement.qml b/ui/tests/qml/tst_app_imap_password_replacement.qml new file mode 100644 index 0000000..ca9a663 --- /dev/null +++ b/ui/tests/qml/tst_app_imap_password_replacement.qml @@ -0,0 +1,95 @@ +import QtQuick 2.15 +import QtTest 1.3 +import "transports.js" as Transports +import "../.." as Omamail +import "BackendFixture.js" as BackendFixture + +// Save changes with a replacement password keeps the setup page open. +// +// One IMAP mailbox, signed in with a password the server now refuses. Settings, +// Edit, a new password, Save changes, and the server refuses that one too. The +// refusal has to be read where it was typed. What the user saw instead was the +// window rebuilding its root while the password was checked and again when the +// answer came, landing on the list with the error only in the status bar — so +// this runs the real window, service and host over the stubbed transport. +Item { + width: 900 + height: 600 + + QtObject { + id: fakeShell + function hide(_id) {} + } + + Omamail.Service { + id: mailService + shell: fakeShell + manifest: ({ id: "omamail", __sourceDir: "/tmp/omamail-test" }) + } + + Omamail.App { + id: app + service: mailService + shell: fakeShell + } + + TestCase { + name: "AppImapPasswordReplacement" + when: windowShown + + property var fixture: null + // The verification of a typed password: it carries the credential, which + // a request on the saved one does not. + property var held: null + readonly property string imapId: "imap:shawn@example.test" + + function initTestCase() { + fixture = BackendFixture.markReady(mailService, 4) + fixture.answers = { + "credentials.get": {found: true, secret: "old-password"}, + "credentials.put": {stored: true}, + "credentials.delete": {deleted: true}, + "imap.folders": function(params, request) { + if (params.credential) { held = request; return undefined } + return {folders: [], special: {}, capabilities: [], labels: []} + } + } + } + + function test_a_refused_replacement_is_shown_on_the_page_that_typed_it() { + app.opened = true + mailService.accountsLoaded = true + mailService.accountList = ({ version: 1, accounts: [ + { id: imapId, email: "shawn@example.test", provider: "imap", + imap: { imapHost: "imap.example.test", imapPort: 993, smtpHost: "smtp.example.test", + smtpPort: 465, username: "shawn@example.test", aliases: [], insecure: false } } ], + activeId: imapId }) + var imap = null + tryVerify(function() { imap = mailService.findAccount(imapId); return !!imap && imap.ready }, 2000) + tryCompare(app, "anyReady", true) + app.resetNavigation() + app.openSettings() + app.editAccount(0) + compare(app.page, "setup") + waitForRendering(app) + + var page = findChild(app, "setup-page").item + var password = findChild(app, "imap-password-field") + password.text = "wrong-password" + findChild(app, "imap-save-button").clicked() + tryVerify(function() { return held !== null }, 1000, "the replacement is verified") + compare(held.params.credential, "shawn@example.test:wrong-password") + + compare(imap.setupState, "ready", "the mailbox stays in service while it is checked") + compare(app.page, "setup") + verify(findChild(app, "setup-page").item === page, "and the page is the same one") + + BackendFixture.respond(mailService, held, undefined, {code: -32000, message: "mail_auth_failed"}) + tryCompare(findChild(app, "imap-error"), "text", "The server rejected the sign-in. Sign in again.") + compare(app.page, "setup", "the refusal is read where it was typed") + compare(findChild(app, "imap-password-field").text, "wrong-password", "and the field keeps it") + verify(!fixture.requests.some(function(request) { return request.method === "credentials.put" }), + "nothing refused is stored") + } + } +} diff --git a/ui/tests/qml/tst_imap_password_replacement.qml b/ui/tests/qml/tst_imap_password_replacement.qml new file mode 100644 index 0000000..2a86abe --- /dev/null +++ b/ui/tests/qml/tst_imap_password_replacement.qml @@ -0,0 +1,269 @@ +import QtQuick +import QtTest +import qs.Commons as Commons +import "../../components" as Mail +import "../../providers" as Providers + +// A mailbox whose saved password the server has started refusing still counts +// as signed in, so its setup page offers Save changes rather than Connect the +// mailbox. A replacement password typed there has to be verified and stored, +// not dropped while only the servers are saved; and until it is verified, the +// saved one stays. +Item { + id: root + width: 700 + height: 900 + + QtObject { + id: credentials + property bool backendCanStoreCredentials: true + property bool failWrites: false + property var writes: [] + property int deletes: 0 + function credentialGet(kind, accountId, clientId, callback) { return true } + function credentialPut(kind, accountId, clientId, secret, callback) { + writes = writes.concat([{kind: kind, accountId: accountId, secret: secret}]) + callback(!failWrites, failWrites ? "credential_store_failed" : "") + return true + } + function credentialDelete(kind, accountId, clientId, callback) { + deletes++ + callback(true, "") + return true + } + } + + // The mailbox on screen: signed in with a password the server now refuses. + Providers.ImapAuth { + id: savedAuth + pluginDir: "/synthetic" + platform: credentials + accountId: "imap:one@example.org" + settings: ({imapHost: "imap.example.org", imapPort: 993, smtpHost: "smtp.example.org", + smtpPort: 465, username: "one@example.org", aliases: [], insecure: false}) + } + + // What `Service.configureCurrentAccountAndSignIn` and + // `MailAccount.signInWithPassword` do, without the account list. + QtObject { + id: service + property var auth: savedAuth + property string accountAddress: "one@example.org" + property string accountName: "" + property string accountSenderName: "" + property bool backendCanNameSender: false + property var saved: null + function configureCurrentAccount(values) { saved = values } + function configureCurrentAccountAndSignIn(values, typed) { savedAuth.signIn(typed) } + } + + Mail.ImapSetupPage { + id: page + width: 600 + service: service + textColor: Commons.Color.foreground + dimColor: Commons.Color.foreground + dangerColor: Commons.Color.accent + accentColor: Commons.Color.accent + panelFontFamily: "monospace" + } + + Component { + id: imapFactory + Providers.ImapAuth { + pluginDir: "/synthetic" + platform: credentials + accountId: "imap:one@example.org" + settings: ({imapHost: "imap.example.org", imapPort: 993, smtpHost: "smtp.example.org", + smtpPort: 465, username: "one@example.org", aliases: [], insecure: false}) + } + } + + TestCase { + name: "ImapPasswordReplacement" + when: windowShown + + property var verified: [] + + function initTestCase() { + savedAuth.verifyRequested.connect(function(settings, value) { + verified = verified.concat([value]) + }) + } + + function init() { + savedAuth.loginBusy = false + savedAuth.replacementBusy = false + savedAuth.pendingPassword = "" + savedAuth.lastError = "" + savedAuth.password = "old-password" + savedAuth.passwordChecked = true + credentials.failWrites = false + credentials.writes = [] + credentials.deletes = 0 + service.saved = null + verified = [] + findChild(page, "imap-password-field").text = "" + } + + function signedIn(auth) { + auth.password = "old-password" + auth.passwordChecked = true + return auth + } + + function test_save_changes_verifies_a_typed_replacement_before_storing_it() { + var save = findChild(page, "imap-save-button") + var password = findChild(page, "imap-password-field") + verify(save.visible, "a signed-in mailbox offers Save changes") + password.text = "new-app-password" + save.clicked() + compare(verified, ["one@example.org:new-app-password"], "the replacement is what gets verified") + compare(credentials.writes.length, 0, "and nothing is stored before the server answers") + compare(service.saved, null, "not a settings-only save that drops it") + savedAuth.completeSignIn(true, "") + compare(credentials.writes.length, 1) + compare(credentials.writes[0].kind, "imap-password") + compare(credentials.writes[0].accountId, "imap:one@example.org") + compare(credentials.writes[0].secret, "new-app-password") + compare(savedAuth.password, "new-app-password") + compare(password.text, "", "a stored password is not verified again by the next save") + } + + function test_a_refused_replacement_stays_in_the_field_and_the_saved_password_stays() { + var password = findChild(page, "imap-password-field") + password.text = "wrong-password" + findChild(page, "imap-save-button").clicked() + compare(verified, ["one@example.org:wrong-password"]) + savedAuth.completeSignIn(false, "The server rejected the sign-in. Sign in again.") + compare(password.text, "wrong-password", "kept, to be corrected") + compare(findChild(page, "imap-error").text, "The server rejected the sign-in. Sign in again.") + compare(savedAuth.password, "old-password") + compare(credentials.writes.length, 0) + } + + function test_save_changes_without_a_password_only_saves_the_settings() { + findChild(page, "imap-save-button").clicked() + verify(service.saved !== null) + compare(verified.length, 0, "nothing to verify") + } + + function test_save_changes_and_enter_wait_while_the_password_is_checked() { + var save = findChild(page, "imap-save-button") + var password = findChild(page, "imap-password-field") + password.text = "new-app-password" + save.clicked() + compare(save.text, "Checking") + verify(!save.enabled) + password.text = "typed-while-checking" + password.accepted() + page.save() + compare(verified, ["one@example.org:new-app-password"], "no second attempt overlaps the first") + savedAuth.completeSignIn(true, "") + compare(save.text, "Save changes") + verify(save.enabled) + compare(credentials.writes.length, 1) + compare(credentials.writes[0].secret, "new-app-password", "the verified one, not the last typed") + } + + function test_a_password_the_keyring_could_not_store_stays_in_the_field() { + var password = findChild(page, "imap-password-field") + credentials.failWrites = true + password.text = "new-app-password" + findChild(page, "imap-save-button").clicked() + savedAuth.completeSignIn(true, "") + compare(password.text, "new-app-password", "so the next Save changes can store it") + verify(savedAuth.lastError.indexOf("could not be saved") >= 0) + } + + // The auth half: a mailbox already holding a password keeps it until the + // replacement has been verified, and one attempt cannot answer for another. + function test_a_refused_replacement_keeps_the_saved_password_and_stores_nothing() { + var auth = signedIn(createTemporaryObject(imapFactory, root)) + verify(auth.signIn("wrong-password")) + auth.completeSignIn(false, "mail_auth_failed") + compare(auth.password, "old-password") + compare(auth.pendingPassword, "") + compare(credentials.writes.length, 0) + verify(auth.lastError !== "") + } + + function test_a_verified_replacement_overwrites_the_saved_password() { + var auth = signedIn(createTemporaryObject(imapFactory, root)) + var saved = 0 + auth.credentialsSaved.connect(function() { saved++ }) + verify(auth.signIn("new-app-password")) + auth.completeSignIn(true, "") + compare(auth.password, "new-app-password") + compare(credentials.writes.length, 1) + compare(credentials.writes[0].secret, "new-app-password") + compare(saved, 1) + } + + function test_a_second_attempt_is_refused_while_the_first_is_checked_data() { + return [ + {tag: "first accepted", first: "right-password", second: "wrong-password", ok: true}, + {tag: "first refused", first: "wrong-password", second: "right-password", ok: false} + ] + } + + function test_a_second_attempt_is_refused_while_the_first_is_checked(data) { + var auth = signedIn(createTemporaryObject(imapFactory, root)) + var sent = [] + auth.verifyRequested.connect(function(settings, value) { sent = sent.concat([value]) }) + verify(auth.signIn(data.first)) + verify(!auth.signIn(data.second)) + compare(sent, ["one@example.org:" + data.first]) + auth.completeSignIn(data.ok, data.ok ? "" : "mail_auth_failed") + if (data.ok) { + compare(credentials.writes.length, 1) + compare(credentials.writes[0].secret, data.first) + compare(auth.password, data.first) + } else { + compare(credentials.writes.length, 0) + compare(auth.password, "old-password", "the saved password is not lost to the refusal") + } + } + + // A replacement keeps the mailbox in service while it is checked; a first + // sign-in has nothing to keep and is what `loginBusy` means. + function test_a_replacement_is_checked_without_taking_the_mailbox_out_of_service() { + var auth = signedIn(createTemporaryObject(imapFactory, root)) + verify(auth.signIn("new-app-password")) + verify(auth.replacementBusy) + verify(!auth.loginBusy, "loginBusy would make the mailbox signing_in") + var fresh = createTemporaryObject(imapFactory, root) + verify(fresh.signIn("first-password")) + verify(fresh.loginBusy) + verify(!fresh.replacementBusy) + } + + function test_an_abandoned_check_frees_sign_in_and_its_late_answer_is_dropped_data() { + return [ + {tag: "sign out", abandon: function(auth) { auth.logout() }}, + {tag: "cancel", abandon: function(auth) { auth.cancelLogin() }}, + {tag: "another mailbox", abandon: function(auth) { auth.accountId = "imap:two@example.org" }} + ] + } + + function test_an_abandoned_check_frees_sign_in_and_its_late_answer_is_dropped(data) { + var auth = signedIn(createTemporaryObject(imapFactory, root)) + var generations = [] + auth.verifyRequested.connect(function() { generations = generations.concat([auth.sessionGeneration]) }) + var succeeded = 0 + auth.loginSucceeded.connect(function() { succeeded++ }) + verify(auth.signIn("new-app-password")) + data.abandon(auth) + verify(!auth.loginBusy && !auth.replacementBusy, "nothing is left checking") + auth.completeSignIn(true, "", generations[0]) + compare(credentials.writes.length, 0, "a late answer does not bring a password back") + compare(succeeded, 0, "or report a sign-in nobody is waiting for") + verify(auth.signIn("next-password"), "and the next attempt is not refused") + auth.completeSignIn(true, "", generations[0]) + compare(credentials.writes.length, 0, "the old answer cannot finish the new attempt either") + auth.completeSignIn(true, "", generations[1]) + compare(credentials.writes.length, 1) + compare(credentials.writes[0].secret, "next-password") + } + } +} From d3edf6544a9ebe8a92292ea8e89280b5327a7a42 Mon Sep 17 00:00:00 2001 From: Dandiccf Date: Mon, 19 Oct 2026 13:36:19 +0200 Subject: [PATCH 45/88] Fix single-tenant Microsoft sign-in authority (#180) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Outlook setup saves `organizations` for every work or school mailbox. That authority works for an Entra registration that accepts accounts from any organizational directory. A public client registered as single-tenant instead receives `AADSTS50059` before device sign-in, because the `/organizations/devicecode` request carries no signed-in account from which Microsoft could find the tenant. Work or school setup now accepts the Entra Directory (tenant) ID or a verified tenant domain and addresses both the device-code and token requests to that tenant. Leaving the field blank keeps `organizations`, so multi-tenant registrations behave exactly as before. The value is validated as a UUID or fully qualified DNS name before saving, lower-cased, and restored when an existing account is edited. The mailbox domain is deliberately not inferred: an Exchange accepted domain need not be an Entra verified domain. This concerns the IMAP/SMTP **Outlook** provider. The Graph-only **Microsoft 365** provider added in #381 picks its tenant from the address domain in Rust and is unchanged. ## Personal accounts and the backend's normalization Personal accounts are unchanged: the setup page still saves an empty tenant, which `stored_tenant()` from #287 reads as `consumers` for token rotation and its lock, and which `MicrosoftOAuth.normalizeTenant` reads the same way for the device flow. A hidden tenant field cannot turn a personal account into a work one. Every value `workTenant` can save is accepted by `auth::destination`'s tenant check and selects `smtp.office365.com` in `outlook_settings`, matching `Outlook.settings` in QML. This PR does not take over #179's explicit `consumers` write; either order of merging is compatible. ## Tests - JavaScript: blank → `organizations`, directory IDs, verified domains (case and whitespace normalized), and refusals for `consumers`, `common`, path traversal, single labels, empty labels, trailing hyphens and malformed UUIDs; agreement of saved values with `normalizeTenant`, `isWorkTenant` and the exact device/token URLs. - QML, real `OutlookSetupPage`: tenant field visibility, validation error, help text per account type; the tenant error clears once the value is edited or the account is switched to personal, and leaves another field's error alone; personal account saves `""` even with text in the hidden field; editing restores a directory ID, a domain, multi-tenant (blank field) and personal, and saving unchanged keeps the same authority; Enter moves from the client ID to the tenant field for a work account and then signs in, and signs in directly for a personal account; existing cancel/retry coverage still passes. - QML, real `OutlookAuth` adapter: for a directory ID, a domain, `organizations` and personal, the device-code `auth.form` request and the token redemption both name the saved authority (`consumers` for personal), and no destination URL crosses to the backend. - Rust: `stored_tenant` plus `destination` build `https://login.microsoftonline.com//oauth2/v2.0/token` for a saved directory ID and domain; `outlook_settings` sends single-tenant mailboxes through `smtp.office365.com` and personal ones through `smtp-mail.outlook.com`. ## Verification - Rebased onto main `d4d80f1`. `make validate` passes locally with Rust 1.99.0 and Qt 6.11.2 on Omarchy 4.0.4: 700 Rust library tests plus integration suites, JavaScript and shell checks, 1478 main QML cases, 6 Outlook HTTP cases, 64 sidebar cases, the native HTML resource-request test, qmllint, `omarchy plugin validate` and `git diff --check`. - `cargo fmt --check` reports differences in files this PR does not touch on main itself; the new Rust tests add none. - The earlier red [CI run](https://github.com/huacnlee/omamail/actions/runs/34768380601) for `6c64da0` had no jobs, so no step failed there; this push starts a fresh run on the rebased head. - Live, before this rebase: a single-tenant registration reproduced `AADSTS50059` through `organizations`, and a device-code request addressed to the verified tenant domain succeeded. - **Not included:** a live recording of the patched UI completing a single-tenant sign-in through Microsoft, including cancel and retry. The end-to-end path is covered by the adapter-level tests above and the earlier live authority-request check, not by a live run of this head. ## Security Scope: the tenant value from the setup page to the Microsoft login URL. The tenant is one path segment of a fixed `https://login.microsoftonline.com/…/oauth2/v2.0/` URL, validated three times: `workTenant` on save (UUID or DNS name only), `normalizeTenant` when the auth object reads it (falls back to `consumers`), and `auth::destination` in Rust before any request (rejects empty, `..`, and anything other than ASCII alphanumerics, `.` and `-`). QML passes a tenant name to `auth.form`, never a URL, and refresh tokens stay in the backend keyring. The client ID, refresh token and tokens are unaffected. Author's scoped verdict: no new destination or credential path; the live end-to-end evidence above is still outstanding. ## Screenshots Before and after, the setup page with empty fields (same window size and theme as the original submission): **Before** before **After** after The tenant field's states on the current head, in the standalone app with synthetic data and no network requests. The red "saved session" line in the first two comes from the synthetic account having no stored token: **1. Editing an existing account restores the saved tenant** Editing an existing account restores
the saved tenant **2. Enter moves from the address through the client ID to the tenant field** Enter moves from the address through
the client ID to the tenant field **3. An invalid tenant is refused before any request** An invalid tenant is refused before
any request **4. Correcting the value clears the refusal** Correcting the value clears the
refusal **5. A blank field means a multi-tenant registration** A blank field means a multi-tenant
registration **6. A personal account hides the field and shows the personal help text** A personal account hides the field
and shows the personal help text **7. Switching back to work or school** Switching back to work or school @huacnlee — the added tenant field, its help text and the Enter/Tab focus order (client ID → tenant → sign in) are ready for your confirmation. ## Release Notes - Outlook work or school accounts can now sign in with a single-tenant Microsoft Entra app registration by entering its Directory (tenant) ID or verified tenant domain. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Christian Dandachi Co-authored-by: Claude Opus 5.5 (1M context) --- docs/MAILBOXES.md | 2 +- src/auth/credentials.rs | 20 +++ src/providers/imap/tests.rs | 15 ++ ui/components/OutlookSetupPage.qml | 44 +++++- ui/providers/MicrosoftOAuth.js | 40 ++++- ui/tests/qml/tst_outlook_backend_consent.qml | 30 ++++ ui/tests/qml/tst_outlook_boundaries.qml | 151 +++++++++++++++++++ ui/tests/test_microsoft_oauth.js | 25 +++ 8 files changed, 317 insertions(+), 10 deletions(-) diff --git a/docs/MAILBOXES.md b/docs/MAILBOXES.md index 5d2e04d..5c1a3bb 100644 --- a/docs/MAILBOXES.md +++ b/docs/MAILBOXES.md @@ -32,7 +32,7 @@ and only then is the IMAP row worth removing. **Outlook** signs in on Microsoft's own page and uses [Microsoft's supported OAuth route for IMAP and SMTP][microsoft-mail-oauth]. It works with Outlook.com, Hotmail, Live and MSN accounts; Omamail never asks for the Microsoft account password. Until Omamail ships a maintainer-owned public client, the setup page asks for an Application (client) ID from a one-time Microsoft Entra app registration. Make it a public client for personal Microsoft accounts; the sign-in asks for `IMAP.AccessAsUser.All`, `SMTP.Send`, `offline_access` and `openid` and shows the device code to enter in the Microsoft page it opens. Microsoft Graph — sending where the tenant has SMTP off, the calendar — is a second code for `Mail.Send` and `Calendars.ReadWrite`, asked for once, when Microsoft refuses the Graph exchange for want of consent: straight after the sign-in where the mailbox sends through Graph, else from the mailbox's settings (*Allow Microsoft Graph...*). -A **work or school** mailbox (Microsoft 365) is the same sign-in addressed to its own tenant: turn on *Work or school account* on the setup page, and register the client in that tenant, or as multi-tenant. Where the tenant has switched authenticated SMTP off — the common Microsoft 365 default, which fails a send with "SmtpClientAuthentication is disabled" — turn on *Send through Microsoft Graph*: the same message goes to Graph's `sendMail` with a token of Graph's own audience, obtained with the same refresh token, and Graph files the sent copy itself. That needs the `Mail.Send` permission on the registration. Where the organization has turned IMAP off, or for one sign-in for mail and calendar instead of an IMAP one and a second code for Graph, choose **Microsoft 365** below instead. +A **work or school** mailbox (Microsoft 365) is the same sign-in addressed to its own tenant: turn on *Work or school account* on the setup page, and register the client in that tenant, or as multi-tenant. For a client registered in one tenant, copy its Directory (tenant) ID or a verified tenant domain into the field below the account-type switch; leave that field blank for a multi-tenant registration. Where the tenant has switched authenticated SMTP off — the common Microsoft 365 default, which fails a send with "SmtpClientAuthentication is disabled" — turn on *Send through Microsoft Graph*: the same message goes to Graph's `sendMail` with a token of Graph's own audience, obtained with the same refresh token, and Graph files the sent copy itself. That needs the `Mail.Send` permission on the registration. Where the organization has turned IMAP off, or for one sign-in for mail and calendar instead of an IMAP one and a second code for Graph, choose **Microsoft 365** below instead. Before signing in, enable IMAP in Outlook.com: **Settings > Mail > Forwarding and IMAP > Let devices and apps use IMAP**, then save. Microsoft disables IMAP by default; OAuth consent alone does not enable mailbox access. See [Microsoft's IMAP setup instructions](https://support.microsoft.com/en-us/outlook/pop-imap-and-smtp-settings-for-outlook-com). diff --git a/src/auth/credentials.rs b/src/auth/credentials.rs index 0c0bddc..1bed342 100644 --- a/src/auth/credentials.rs +++ b/src/auth/credentials.rs @@ -1122,6 +1122,26 @@ mod tests { ); } + /// A single-tenant registration's directory id or verified domain, as + /// the setup page saves it, is the authority every refresh goes to. + #[test] + fn a_single_tenant_accounts_stored_tenant_builds_its_own_token_url() { + for tenant in [ + "12345678-1234-4abc-9def-1234567890ab", + "contoso.onmicrosoft.com", + ] { + let entry = json!({"imap":{"tenant":tenant}}); + assert_eq!(stored_tenant(&entry), tenant); + assert_eq!( + destination( + &json!({"provider":"outlook","endpoint":"token","tenant":stored_tenant(&entry)}), + ) + .unwrap(), + format!("https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token") + ); + } + } + fn expiry_at( monotonic: std::time::Instant, wall: std::time::SystemTime, diff --git a/src/providers/imap/tests.rs b/src/providers/imap/tests.rs index aeca20a..f731d9d 100644 --- a/src/providers/imap/tests.rs +++ b/src/providers/imap/tests.rs @@ -519,3 +519,18 @@ fn outlook_settings_ignore_persisted_credential_destination_and_identity() { assert_eq!(settings["username"], "owner@example.org"); assert_eq!(settings["insecure"], false); } +#[test] +fn outlook_settings_send_a_single_tenant_mailbox_through_microsoft_365() { + for tenant in [ + "12345678-1234-4abc-9def-1234567890ab", + "contoso.onmicrosoft.com", + ] { + let entry = json!({"imap":{"tenant":tenant}}); + let settings = outlook_settings(&entry, "alice@contoso.com"); + assert_eq!(settings["smtpHost"], "smtp.office365.com"); + } + for personal in [json!({"imap":{"tenant":""}}), json!({"imap":{}})] { + let settings = outlook_settings(&personal, "alice@hotmail.com"); + assert_eq!(settings["smtpHost"], "smtp-mail.outlook.com"); + } +} diff --git a/ui/components/OutlookSetupPage.qml b/ui/components/OutlookSetupPage.qml index db96ece..7145b2d 100644 --- a/ui/components/OutlookSetupPage.qml +++ b/ui/components/OutlookSetupPage.qml @@ -54,12 +54,29 @@ Column { return "" } + readonly property string tenantError: "That is not a Microsoft Entra Directory (tenant) ID or domain" + + function validatedTenant() { + if (!workSwitch.checked) return "" + var tenant = Microsoft.workTenant(tenantField.text) + if (tenant !== "") return tenant + errorText.text = root.tenantError + return "" + } + + // The refusal is about the field as it was; editing it or switching the + // account type away from it answers that, and leaves other errors alone. + function clearTenantError() { + if (errorText.text === root.tenantError) errorText.text = "" + } + function accountValues() { var address = validatedAddress() if (address === "") return null var clientId = validatedClientId() if (clientId === "") return null - var tenant = workSwitch.checked ? "organizations" : "" + var tenant = validatedTenant() + if (workSwitch.checked && tenant === "") return null var send = workSwitch.checked && graphSwitch.checked ? "graph" : "" var imap = Outlook.settings(address, tenant, send) imap.tenant = tenant @@ -118,7 +135,9 @@ Column { addressField.text = String(service.accountAddress || "") if (auth && auth.configuredClientId) clientIdField.text = String(auth.configuredClientId) - workSwitch.checked = !!auth && Microsoft.isWorkTenant(auth.tenant) + var tenant = auth ? Microsoft.normalizeTenant(auth.tenant) : "consumers" + workSwitch.checked = tenant !== "consumers" + tenantField.text = tenant !== "consumers" && tenant !== "organizations" ? tenant : "" graphSwitch.checked = !!auth && String(auth.configuredSend || "") === "graph" } @@ -217,7 +236,7 @@ Column { font.family: root.panelFontFamily font.pixelSize: Style.font.bodySmall placeholderText: "Microsoft Application (client) ID" - onAccepted: root.signIn() + onAccepted: if (workSwitch.checked) tenantField.forceActiveFocus(); else root.signIn() } // A work or school mailbox lives in its own tenant rather than the @@ -234,6 +253,7 @@ Column { foreground: root.textColor accent: root.accentColor onToggled: checked = !checked + onCheckedChanged: root.clearTenantError() } Text { @@ -254,6 +274,19 @@ Column { } } + TextField { + id: tenantField + objectName: "outlook-tenant-field" + width: parent.width + visible: workSwitch.checked + foreground: root.textColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.bodySmall + placeholderText: "Directory (tenant) ID or domain — blank for multitenant" + onTextChanged: root.clearTenantError() + onAccepted: root.signIn() + } + Row { width: parent.width spacing: Style.space(10) @@ -356,8 +389,11 @@ Column { } Text { + objectName: "outlook-app-setup-help" width: parent.width - text: "1. Register an app for personal Microsoft accounts. 2. Under Authentication, enable public client flows. 3. Copy its Application (client) ID above. Omamail requests only IMAP, SMTP and offline access when you sign in." + text: workSwitch.checked + ? "1. Register the app in this Microsoft Entra tenant, or as multitenant. 2. Under Authentication, enable public client flows. 3. For a single-tenant app, copy its Directory (tenant) ID above; leave it blank for a multitenant app. 4. Copy its Application (client) ID above. Omamail requests only IMAP, SMTP and offline access when you sign in." + : "1. Register an app for personal Microsoft accounts. 2. Under Authentication, enable public client flows. 3. Copy its Application (client) ID above. Omamail requests only IMAP, SMTP and offline access when you sign in." color: root.dimColor font.family: root.panelFontFamily font.pixelSize: Style.font.caption diff --git a/ui/providers/MicrosoftOAuth.js b/ui/providers/MicrosoftOAuth.js index a4d3693..2e5bb2a 100644 --- a/ui/providers/MicrosoftOAuth.js +++ b/ui/providers/MicrosoftOAuth.js @@ -31,11 +31,12 @@ var GRAPH_SCOPES = [ ] // The tenant the sign-in is addressed to. Personal accounts live under -// `consumers`; a Microsoft 365 mailbox lives under its own tenant, which -// `organizations` finds from the address, or a tenant id or domain names -// outright. Anything that is not one of those spellings is the consumer -// tenant, so a stored value cannot steer the sign-in to another host: the -// tenant is one path segment of a fixed URL, never a URL of its own. +// `consumers`; a Microsoft 365 mailbox lives under its own tenant, named by +// its id or one of its verified domain names. `organizations` is the generic +// authority for multi-tenant registrations. Anything that is not one of those +// spellings is the consumer tenant, so a stored value cannot steer the sign-in +// to another host: the tenant is one path segment of a fixed URL, never a URL +// of its own. // Every request names one resource, the device-code request that starts a // sign-in included: Microsoft refuses two in one (AADSTS28000). So consent // for Graph is a sign-in of its own — a second code — asked for only when @@ -62,6 +63,35 @@ function isWorkTenant(tenant) { return normalizeTenant(tenant) !== "consumers" } +function isDirectoryId(value) { + return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i + .test(trimmed(value)) +} + +function isTenantDomain(value) { + var text = trimmed(value).toLowerCase() + if (text.length > 253 || text.indexOf(".") < 1) return false + var labels = text.split(".") + for (var i = 0; i < labels.length; ++i) { + if (!/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(labels[i])) return false + } + return true +} + +// Empty means a registration that accepts accounts from any organization. +// A single-tenant public client needs the directory id or a verified tenant +// domain instead: unlike an authorization request, the device-code request +// carries no signed-in account from which Microsoft could discover one. +// An empty result for non-empty input tells the form that the value is not a +// work authority rather than quietly sending that sign-in to `consumers`. +function workTenant(value) { + var text = trimmed(value).toLowerCase() + if (text === "") return "organizations" + if (text === "organizations") return text + if (isDirectoryId(text) || isTenantDomain(text)) return text + return "" +} + function authorityFor(tenant) { return "https://login.microsoftonline.com/" + normalizeTenant(tenant) + "/oauth2/v2.0" } diff --git a/ui/tests/qml/tst_outlook_backend_consent.qml b/ui/tests/qml/tst_outlook_backend_consent.qml index 6115bed..76be131 100644 --- a/ui/tests/qml/tst_outlook_backend_consent.qml +++ b/ui/tests/qml/tst_outlook_backend_consent.qml @@ -161,6 +161,36 @@ Item { compare(fixture.writes[0].clientId, auth.clientId) compare(fixture.deletes.length, 0) } + function test_device_sign_in_goes_to_the_saved_tenant_data() { + return [ + {tag: "directory id", saved: "12345678-1234-4abc-9def-1234567890ab", + authority: "12345678-1234-4abc-9def-1234567890ab"}, + {tag: "tenant domain", saved: "contoso.onmicrosoft.com", authority: "contoso.onmicrosoft.com"}, + {tag: "multitenant", saved: "organizations", authority: "organizations"}, + {tag: "personal", saved: "", authority: "consumers"} + ] + } + function test_device_sign_in_goes_to_the_saved_tenant(data) { + var auth = fresh() + auth.entrySettings = ({ tenant: data.saved }) + auth.loggedIn = false + fixture.calls = [] + auth.beginLogin() + compare(auth.devicePurpose, "mail") + var forms = fixture.calls.filter(function(call) { return call.method === "auth.form" }) + compare(forms.length, 1) + compare(forms[0].params.endpoint, "device") + compare(forms[0].params.tenant, data.authority, + "the device code is asked for at the authority the account was saved with") + verify(forms[0].params.url === undefined, "the UI names a tenant, never a destination") + auth.pollDeviceCode() + forms = fixture.calls.filter(function(call) { return call.method === "auth.form" }) + compare(forms.length, 2) + compare(forms[1].params.endpoint, "token") + compare(forms[1].params.tenant, data.authority, + "the code is redeemed at the same authority that issued it") + auth.cancelLogin() + } function test_graph_device_grant_uses_the_serialized_backend_store() { var auth = fresh() auth.acceptGraphSignIn({accessToken: "graph-token", refreshToken: "device-graph-refresh", diff --git a/ui/tests/qml/tst_outlook_boundaries.qml b/ui/tests/qml/tst_outlook_boundaries.qml index de40cec..90ee517 100644 --- a/ui/tests/qml/tst_outlook_boundaries.qml +++ b/ui/tests/qml/tst_outlook_boundaries.qml @@ -44,6 +44,25 @@ Item { function checkMicrosoftConnection(callback) { connectionCallback = callback } } } + Component { + id: savedAccountFactory + QtObject { + property string accountAddress: "alice@contoso.com" + property bool backendCanCheckMicrosoftConnection: true + property int starts: 0 + property var saved: null + property var auth: QtObject { + property bool loggedIn: false + property bool loginBusy: false + property string lastError: "" + property string tenant: "" + property string configuredClientId: "12345678-1234-4abc-9def-1234567890ab" + property string configuredSend: "" + } + function configureCurrentAccountAndSignInOAuth(values) { saved = values; starts++ } + function configureCurrentAccount(values) { saved = values } + } + } TestCase { name: "OutlookBoundaryReview" function readyHost() { @@ -125,6 +144,138 @@ Item { compare(label.textFormat, Text.PlainText, "Server error messages must not be interpreted as resource-bearing HTML") } + function outlookValues(page, tenant) { + var address = findChild(page, "outlook-address-field") + var client = findChild(page, "outlook-client-id-field") + var work = findChild(page, "outlook-work-switch") + var tenantField = findChild(page, "outlook-tenant-field") + verify(address !== null) + verify(client !== null) + verify(work !== null) + verify(tenantField !== null) + address.text = "alice@marketing.contoso.com" + client.text = "12345678-1234-4abc-9def-1234567890ab" + work.checked = true + tenantField.text = tenant + return page.accountValues() + } + function test_work_account_targets_the_configured_tenant() { + var page = createTemporaryObject(pageFactory, parent) + verify(page !== null) + var values = outlookValues(page, "contoso.onmicrosoft.com") + verify(values !== null) + compare(values.imap.tenant, "contoso.onmicrosoft.com", + "the mailbox suffix need not be the single-tenant registration's authority") + values = outlookValues(page, "") + compare(values.imap.tenant, "organizations", + "blank keeps the generic authority for a multi-tenant registration") + } + function test_work_account_refuses_an_invalid_tenant() { + var page = createTemporaryObject(pageFactory, parent) + verify(page !== null) + compare(outlookValues(page, "evil.example/../consumers"), null) + compare(outlookValues(page, "contoso-.com"), null) + var error = findChild(page, "outlook-error") + verify(error.text.indexOf("tenant") >= 0) + } + function test_tenant_refusal_clears_once_it_no_longer_applies() { + var page = createTemporaryObject(pageFactory, parent) + var error = findChild(page, "outlook-error") + var work = findChild(page, "outlook-work-switch") + var tenantField = findChild(page, "outlook-tenant-field") + compare(outlookValues(page, "contoso/../consumers"), null) + verify(error.text !== "") + tenantField.text = "contoso.onmicrosoft.com" + compare(error.text, "", "a corrected tenant is not still refused") + compare(outlookValues(page, "contoso/../consumers"), null) + work.checked = false + compare(error.text, "", "a personal account has no tenant to refuse") + work.checked = true + findChild(page, "outlook-address-field").text = "not an address" + compare(page.accountValues(), null) + var addressError = error.text + verify(addressError !== "") + tenantField.text = "other.onmicrosoft.com" + compare(error.text, addressError, "editing the tenant leaves another field's error alone") + } + function test_work_account_setup_names_the_right_registration() { + var page = createTemporaryObject(pageFactory, parent) + verify(page !== null) + var work = findChild(page, "outlook-work-switch") + var help = findChild(page, "outlook-app-setup-help") + verify(work !== null) + verify(help !== null) + work.checked = false + verify(help.text.indexOf("personal Microsoft accounts") >= 0) + work.checked = true + verify(help.text.indexOf("this Microsoft Entra tenant, or as multitenant") >= 0) + verify(help.text.indexOf("Directory (tenant) ID") >= 0) + verify(help.text.indexOf("personal Microsoft accounts") < 0) + } + function test_personal_account_keeps_the_tenant_the_backend_reads_as_consumers() { + var page = createTemporaryObject(pageFactory, parent) + var address = findChild(page, "outlook-address-field") + var client = findChild(page, "outlook-client-id-field") + var work = findChild(page, "outlook-work-switch") + var tenantField = findChild(page, "outlook-tenant-field") + address.text = "alice@hotmail.com" + client.text = "12345678-1234-4abc-9def-1234567890ab" + work.checked = false + compare(tenantField.visible, false) + tenantField.text = "contoso.onmicrosoft.com" + var values = page.accountValues() + verify(values !== null) + compare(values.imap.tenant, "", + "a hidden tenant field must not turn a personal account into a work one") + compare(values.imap.smtpHost, "smtp-mail.outlook.com") + } + function test_editing_restores_the_saved_tenant_data() { + return [ + {tag: "directory id", saved: "12345678-1234-4abc-9def-1234567890ab", + work: true, field: "12345678-1234-4abc-9def-1234567890ab"}, + {tag: "tenant domain", saved: "contoso.onmicrosoft.com", work: true, field: "contoso.onmicrosoft.com"}, + {tag: "multitenant", saved: "organizations", work: true, field: ""}, + {tag: "personal", saved: "", work: false, field: ""} + ] + } + function test_editing_restores_the_saved_tenant(data) { + var service = createTemporaryObject(savedAccountFactory, parent) + service.auth.tenant = data.saved + var page = createTemporaryObject(pageFactory, parent, { service: service }) + var work = findChild(page, "outlook-work-switch") + var tenantField = findChild(page, "outlook-tenant-field") + compare(work.checked, data.work) + compare(tenantField.text, data.field) + compare(tenantField.visible, data.work) + page.save() + verify(service.saved !== null) + compare(service.saved.imap.tenant, data.saved, + "saving an account unchanged must keep the authority it signs in at") + } + function test_enter_walks_from_the_client_id_to_the_tenant_then_signs_in() { + var service = createTemporaryObject(savedAccountFactory, parent) + service.auth.tenant = "organizations" + var page = createTemporaryObject(pageFactory, parent, { service: service }) + var client = findChild(page, "outlook-client-id-field") + var tenantField = findChild(page, "outlook-tenant-field") + client.forceActiveFocus() + keyClick(Qt.Key_Return) + verify(tenantField.activeFocus, "a work account's Enter on the client ID reaches the tenant field") + compare(service.starts, 0) + keyClick(Qt.Key_Return) + compare(service.starts, 1) + compare(service.saved.imap.tenant, "organizations") + } + function test_enter_on_a_personal_client_id_signs_in() { + var service = createTemporaryObject(savedAccountFactory, parent) + service.accountAddress = "alice@hotmail.com" + var page = createTemporaryObject(pageFactory, parent, { service: service }) + var client = findChild(page, "outlook-client-id-field") + client.forceActiveFocus() + keyClick(Qt.Key_Return) + compare(service.starts, 1) + compare(service.saved.imap.tenant, "") + } function test_cancel_matches_google_signin_and_allows_retry() { var host = readyHost() var service = createTemporaryObject(serviceFactory, parent, { auth: host.auth }) diff --git a/ui/tests/test_microsoft_oauth.js b/ui/tests/test_microsoft_oauth.js index 931909d..4ca2505 100644 --- a/ui/tests/test_microsoft_oauth.js +++ b/ui/tests/test_microsoft_oauth.js @@ -39,6 +39,31 @@ assert.strictEqual(microsoft.deviceUrlFor("bad tenant"), microsoft.DEVICE_URL, "an unusable tenant falls back to the consumer authority") assert.strictEqual(microsoft.isWorkTenant("consumers"), false) assert.strictEqual(microsoft.isWorkTenant("organizations"), true) +assert.strictEqual(microsoft.workTenant(""), "organizations", + "blank selects the generic authority for a multi-tenant registration") +assert.strictEqual(microsoft.workTenant(" Contoso.onmicrosoft.COM "), "contoso.onmicrosoft.com") +assert.strictEqual(microsoft.workTenant("12345678-1234-4abc-9def-1234567890ab"), + "12345678-1234-4abc-9def-1234567890ab") +assert.strictEqual(microsoft.workTenant("consumers"), "", "a work account cannot use the consumer authority") +assert.strictEqual(microsoft.workTenant("common"), "", "personal accounts cannot enter through the work form") +assert.strictEqual(microsoft.workTenant("evil.example/../consumers"), "", + "input cannot steer the sign-in outside one authority segment") +assert.strictEqual(microsoft.workTenant("x"), "", "a tenant domain is fully qualified") +assert.strictEqual(microsoft.workTenant("123"), "", "a number is not a Directory ID") +assert.strictEqual(microsoft.workTenant("contoso."), "", "a tenant domain has no empty label") +assert.strictEqual(microsoft.workTenant("contoso-.com"), "", "a domain label cannot end in a hyphen") +assert.strictEqual(microsoft.workTenant("12345678-1234-4abc-9def-1234567890a"), "", + "a malformed Directory ID is refused") +// The backend reads a saved tenant the same way: an empty one is a personal +// account's, and anything workTenant answers is a work authority it accepts. +assert.strictEqual(microsoft.normalizeTenant(""), "consumers", + "a personal account still saves no tenant and still signs in at consumers") +assert.strictEqual(microsoft.isWorkTenant(microsoft.workTenant("contoso.onmicrosoft.com")), true) +assert.strictEqual(microsoft.isWorkTenant(microsoft.workTenant("12345678-1234-4abc-9def-1234567890ab")), true) +assert.strictEqual(microsoft.deviceUrlFor(microsoft.workTenant("12345678-1234-4abc-9def-1234567890ab")), + "https://login.microsoftonline.com/12345678-1234-4abc-9def-1234567890ab/oauth2/v2.0/devicecode") +assert.strictEqual(microsoft.tokenUrlFor(microsoft.workTenant("Contoso.onmicrosoft.com")), + "https://login.microsoftonline.com/contoso.onmicrosoft.com/oauth2/v2.0/token") // Graph's token is asked for with the same refresh token and Graph's scope. const graphBody = microsoft.graphRefreshBody(clientId, "refresh-secret") From 72dbef469d38504e9af3b2fa059a2dc5f56feddf Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 19 Oct 2026 20:37:39 +0800 Subject: [PATCH 46/88] Release v0.11.1 (#387) Publish Omamail v0.11.1 from this branch. Release CI builds and verifies the plugin backends and all three standalone desktop packages, creates the tag and one release, then updates backend-version and the released API contract in this PR after verifying every public download. Merge this PR once the pin commit and required checks pass. Main receives the version and runtime pin together in one merge. ## Release Notes - Update the Omarchy backend and standalone desktop applications to v0.11.1. --------- Co-authored-by: Omamail Release <41898282+github-actions[bot]@users.noreply.github.com> --- Cargo.lock | 2 +- Cargo.toml | 2 +- app/CMakeLists.txt | 2 +- backend-api.json | 38 +++----------------------------------- backend-version | 2 +- manifest.json | 2 +- 6 files changed, 8 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c4573b..153be21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1320,7 +1320,7 @@ dependencies = [ [[package]] name = "omamail" -version = "0.11.0" +version = "0.11.1" dependencies = [ "base64", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 9aa7b1b..8b03c81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "omamail" -version = "0.11.0" +version = "0.11.1" edition = "2024" publish = false diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index a4695bc..87e7144 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.21) -project(omamail-app VERSION 0.11.0 LANGUAGES CXX) +project(omamail-app VERSION 0.11.1 LANGUAGES CXX) if(WIN32) enable_language(RC) diff --git a/backend-api.json b/backend-api.json index 4d8f99d..d5be6de 100644 --- a/backend-api.json +++ b/backend-api.json @@ -1,6 +1,6 @@ { "apiVersion": 8, - "releasedApiVersion": 7, + "releasedApiVersion": 8, "protocolVersion": 1, "methods": [ "system.info", @@ -1473,39 +1473,7 @@ } ], "unreleased": { - "methods": [ - "m365.signInBegin", - "m365.signInPoll", - "m365.signOut", - "m365.verify", - "m365.list", - "m365.messages", - "m365.read", - "m365.attachment", - "m365.modify", - "m365.trash", - "m365.untrash", - "m365.send", - "m365.saveDraft", - "m365.deleteDraft", - "m365.sendAs", - "m365.invalidate", - "m365.cancel" - ], - "cases": [ - "Microsoft 365 provider rules", - "Microsoft 365 text search is a text query", - "Microsoft 365 answers one sender identity without a request", - "Microsoft 365 verify names a mailbox that is not saved", - "Microsoft 365 list refuses a forged page token", - "Microsoft 365 messages refuse an id that is not its own", - "Microsoft 365 send refuses a body that is not canonical base64url", - "Microsoft 365 sign-in needs an application id", - "mail action rejects malformed Microsoft 365 IDs before preview", - "Microsoft 365 saves a draft only from canonical base64url", - "Microsoft 365 deletes a draft only by an id of its own", - "Microsoft 365 calendar discovery asks the mailbox's own sign-in", - "outbox accepts the Microsoft 365 provider" - ] + "methods": [], + "cases": [] } } diff --git a/backend-version b/backend-version index d9df1bb..af88ba8 100644 --- a/backend-version +++ b/backend-version @@ -1 +1 @@ -0.11.0 +0.11.1 diff --git a/manifest.json b/manifest.json index 3746eff..dd32ef8 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "id": "omamail", "name": "Omamail", - "version": "0.11.0", + "version": "0.11.1", "author": "huacnlee", "license": "MIT", "homepage": "https://huacnlee.github.io/omamail/", From 8749c14692293eb79f748559ff1bdd3bbbc4b99c Mon Sep 17 00:00:00 2001 From: Hannes Spitz <44113112+brokkoli71@users.noreply.github.com> Date: Tue, 20 Oct 2026 16:23:42 +0200 Subject: [PATCH 47/88] Take the mouse's back button as Escape's step, stopping short of the window (#202) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The back button on a mouse is the quickest way there is to leave a page, and in this window it did nothing. Reading a message and returning to the list is the step this application asks for most often, and both ways to take it — `Escape` and the on-screen Back — mean lifting the hand off the mouse to reach them. It matters most in a narrow window: below `compact` the list is not beside the reader but replaced by it, so every message read is a step in and a step back out. It calls `goBack()`, the step `Escape` takes, and not `back()`. I tried `back()` first, on the reasoning that a thumb reaching for the previous page has not asked for its search to be cleared — and that was wrong, because `agentPrompt` and `composeAgent` are `FocusScope`s rather than nav entries, so `Nav.depth` cannot see them. Routed to `back()`, a press with the assistant dock open pulled the page out from under a dock still bound to a message no longer on screen, where `Escape` closed the dock and left the message standing. That is the drift `docs/ARCHITECTURE.md` forbids: an action describes intent, not an input device. The one place it departs from `Escape` is the end. `back()` on an empty history closes the window, which is right for a key somebody pressed on purpose and wrong for a button sitting under the hand with no affordance saying the next press shuts the mailbox. `canGoBack` asks whether anything nearer is open before allowing the step, so the button is inert exactly where the on-screen Back is absent. The subtle part is a popup. A non-modal `QQC.Popup` with `CloseOnPressOutside` — the app menu, a message menu, the account switcher, and five more — is dismissed by this press, and Qt's overlay then delivers the same press to what lies beneath it, so acting on it again would close the menu and take the page under it back together: one gesture spent twice. Reading `Overlay.overlay.children` inside the handler does not work, and that was measured rather than assumed — Qt empties the overlay before running the handler, so the guard sees zero open popups at exactly the moment one is closing. The close is therefore caught as it happens, and the flag lives exactly one turn of the event loop: long enough for the press that caused it, gone before any press that did not. `ui/components/MouseBack.qml` reports the press and `ui/App.qml` decides what it means, which is the shape `KeyRouter` already has for keys, and for the same reason. ## Verification `make validate` is green on this commit, rebased onto v0.10.8 (2d5f2bd): the Rust suite is 687 tests with none failing, and the QML suite 1163, 6 and 64 assertions with none failing. The rebase conflicted in one line of the Makefile's `QML_FILES` list, where upstream had added `AiSettings.qml` beside `MouseBack.qml`, and nowhere else. `ui/tests/qml/tst_app_navigation.qml` gains five cases, in the file whose header already frames itself as "every Back — the bar on a page, `Escape`, a draft closing — pops one". I mutation-tested them rather than trusting them, because an earlier revision of this change passed its own tests while three of its claims were unpinned: `Qt.AllButtons` in place of `Qt.BackButton`, `back()` in place of `goBack()`, a `canGoBack` reduced to the depth test alone, and the popup guard removed each produce failures now. Running `make validate` against `upstream/main` at 2d5f2bd and against this branch, both are green, and the QML suite goes from 1158 passes to 1163: these five cases. Driven by hand on Omarchy 4, on the v0.10.6 base this was first written against, in a light theme and in a dark one, and at both layouts — three columns, and the single column below `compact`, where the list is replaced by the reader rather than sitting beside it and the button therefore does the most work. The mouse reports `BTN_SIDE`, which Qt's Wayland client maps to `Qt.BackButton`; one reporting `BTN_BACK` would land on `ExtraButton3` and silently do nothing, and I have no such mouse to test with. Nothing visible changes, so before-and-after screenshots would be the same image. A fresh agent reviewed an earlier revision of this change with no context of the work that produced it. It found the assistant-dock defect, the popup double-action, and the three unpinned tests. All of them are fixed here. Written with Claude Code. I have read the diff and can explain any line in it. ## Release Notes - The back button on a mouse now goes back, taking the same step `Escape` takes: out of an open message, a page, or a draft. At the mailbox itself it stops rather than closing the window, and with a menu open it closes only the menu. --------- Co-authored-by: Claude Opus 5.5 --- Makefile | 1 + docs/KEYS.md | 2 + ui/App.qml | 7 +++ ui/components/MouseBack.qml | 62 ++++++++++++++++++++++ ui/tests/qml/tst_app_navigation.qml | 79 +++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+) create mode 100644 ui/components/MouseBack.qml diff --git a/Makefile b/Makefile index c1a6260..a7962fe 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,7 @@ QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryContr ui/components/AccountRemovalDialog.qml \ ui/components/ComposeExitDialog.qml \ ui/components/BackBar.qml \ + ui/components/MouseBack.qml \ ui/components/SettingsPage.qml ui/components/AiSettings.qml \ ui/components/SettingsSidebar.qml \ ui/components/CalendarSettings.qml \ diff --git a/docs/KEYS.md b/docs/KEYS.md index 16da26d..5b27b51 100644 --- a/docs/KEYS.md +++ b/docs/KEYS.md @@ -313,6 +313,8 @@ different row under the still pointer, and the cursor snapped back to it — so `j` and `k` stuck on whichever rows the mouse was resting near. `ui/tests/qml/tst_hover_under_scroll.qml` pins the Qt behaviour that makes this so. +A mouse with a back button takes the same step `Escape` takes — `goBack()`, so the two do not become separate implementations of going back — with one exception. It never closes the window. `Escape` on a root with nothing left to clear ends the window, which is right for a key somebody pressed on purpose and wrong for a button sitting under the hand with no affordance saying the next press shuts the mailbox, so the mouse stops there instead. `ui/components/MouseBack.qml` reports the press and `ui/App.qml` decides what it means, the same way `KeyRouter` does for keys. + ## Adding a key 1. Add a row to `BINDINGS` in `ui/keys/Keymap.js`. Name the contexts it means diff --git a/ui/App.qml b/ui/App.qml index e6313d3..8f35707 100644 --- a/ui/App.qml +++ b/ui/App.qml @@ -1418,6 +1418,13 @@ Item { anchors.fill: parent focus: true + MouseBack { + canGoBack: Nav.depth(root.nav) > 1 || root.assistantOpen + || root.selectionActive || searchBar.fieldFocused + || (!!root.service && root.service.searchQuery !== "") + onActivated: root.goBack() + } + // Where the window is, and the only thing that says what a key means. // A page is a form before it is anything else, a draft beats reading, a // query being typed beats the list underneath it. diff --git a/ui/components/MouseBack.qml b/ui/components/MouseBack.qml new file mode 100644 index 0000000..4ed4a76 --- /dev/null +++ b/ui/components/MouseBack.qml @@ -0,0 +1,62 @@ +import QtQuick +import QtQuick.Controls + +// The mouse's back button, carrying the same intent Escape carries. +// +// It reports; the window decides. `canGoBack` is the application's answer to +// whether there is a step to take, and the step itself is the caller's — this +// draws nothing and knows nothing about where the window has been. That is +// the same shape `KeyRouter` has, and for the same reason: an action +// describes intent, not the device that raised it, so the mouse and the +// keyboard must not grow two implementations of going back. +// +// A press that dismisses a menu does not also go back. A non-modal +// `QQC.Popup` with `CloseOnPressOutside` — the app menu, a message menu, the +// account switcher, and five more — is closed by this press, and Qt's overlay +// then delivers the same press to what lies beneath, so without a guard the +// menu would close and the page under it would go back together: one gesture +// spent twice. `dismissedAPopup` below is that guard, and +// `tst_app_navigation.qml` covers it. +// +// Only `Qt.BackButton` is accepted, so every other button reaches whatever is +// under the pointer exactly as before: left clicks on rows and chrome, a +// splitter drag, text selection in the reader, and the wheel handler in +// `WheelScroller` are all untouched by this lying beneath them. +MouseArea { + id: root + + objectName: "mouse-back" + + // Whatever it is given, in full. The button is pressed wherever the pointer + // happens to be resting, so there is no smaller area that would be right. + anchors.fill: parent + + // Required rather than defaulted: a caller that forgets it would otherwise + // get a button that is silently dead, which is the hardest kind to notice. + required property bool canGoBack + + signal activated() + + acceptedButtons: Qt.BackButton + + // Qt's overlay closes a non-modal popup on a press outside it and then + // delivers that same press to what lies beneath, so acting on it here would + // spend one gesture twice: the menu would close and the page under it would + // go back together. Reading the overlay inside the handler is too late — by + // then it is already empty — so the close is caught as it happens instead, + // and the flag lives exactly one turn of the event loop: long enough for the + // press that caused it, gone before any press that did not. + property bool dismissedAPopup: false + property int popupCount: Overlay.overlay ? Overlay.overlay.children.length : 0 + + onPopupCountChanged: { + if (popupCount !== 0) return + root.dismissedAPopup = true + Qt.callLater(function() { root.dismissedAPopup = false }) + } + + onPressed: function(mouse) { + mouse.accepted = true + if (root.canGoBack && !root.dismissedAPopup) root.activated() + } +} diff --git a/ui/tests/qml/tst_app_navigation.qml b/ui/tests/qml/tst_app_navigation.qml index 2d81038..b48a4dc 100644 --- a/ui/tests/qml/tst_app_navigation.qml +++ b/ui/tests/qml/tst_app_navigation.qml @@ -724,6 +724,85 @@ Item { compare(fakeShell.hidden.length, 1) } + // The mouse's back button carries the same intent Escape carries, and the + // reader is where it is reached for most: below `compact` the list is not + // beside the message but replaced by it. + function test_the_mouse_back_button_leaves_the_message() { + var area = named(app, "mouse-back") + verify(area, "the window carries a back-button area") + + app.openMessage("message-1") + compare(kinds(), "list,reader") + + mouseClick(area, 40, 40, Qt.BackButton) + compare(kinds(), "list", "the mouse's back button leaves the message") + compare(fakeShell.hidden.length, 0, "and does not close the window doing it") + } + + // A button under the hand has no affordance saying the next press shuts + // the mailbox, so the one branch of `back()` that ends the window is the + // one it must not reach. Escape, which somebody pressed on purpose, still + // does — `test_the_calendar_is_the_other_root_and_back_on_a_root_closes` + // is that half. + function test_the_mouse_back_button_does_not_close_the_window() { + var area = named(app, "mouse-back") + verify(area, "the window carries a back-button area") + compare(kinds(), "list", "the root is where this starts") + + mouseClick(area, 40, 40, Qt.BackButton) + compare(kinds(), "list", "the root is not popped") + compare(fakeShell.hidden.length, 0, "and the window is not closed") + } + + // It is `goBack()` and not `back()`: with rows ticked, the nearer thing to + // leave is the selection. Routed to `back()` this press would pop the + // history out from under a selection nobody had dropped — and on the root, + // close the window instead. + function test_the_mouse_back_button_drops_a_selection_before_the_page() { + var area = named(app, "mouse-back") + app.checkedIds = ["message-1"] + verify(app.selectionActive, "the selection is live") + + mouseClick(area, 40, 40, Qt.BackButton) + compare(app.checkedIds.length, 0, "the selection is what the press left") + compare(kinds(), "list", "and the page stayed") + compare(fakeShell.hidden.length, 0, "and the window stayed") + } + + // Qt's overlay dismisses a non-modal popup on a press outside it and then + // delivers that same press to what lies beneath. Acted on twice, one press + // would close the menu and take the page under it back together, which is + // a gesture spent twice and the only place in the app where that happens. + function test_the_mouse_back_button_only_closes_an_open_menu() { + var area = named(app, "mouse-back") + var menu = named(app, "app-menu") + verify(menu, "the window carries the app menu") + + app.openMessage("message-1") + compare(kinds(), "list,reader") + + menu.openAt(20, 20) + tryCompare(menu, "opened", true) + + mouseClick(area, 300, 300, Qt.BackButton) + tryCompare(menu, "opened", false) + compare(kinds(), "list,reader", "the press closed the menu and nothing else") + } + + // Only the back button. The area lies under the whole window, so a left + // click that reached it would be a click taken from whatever the pointer + // was actually on. + function test_the_mouse_back_area_takes_no_other_button() { + var area = named(app, "mouse-back") + app.openMessage("message-1") + compare(kinds(), "list,reader") + + mouseClick(area, 40, 40, Qt.LeftButton) + compare(kinds(), "list,reader", "a left click is not a step back") + mouseClick(area, 40, 40, Qt.MiddleButton) + compare(kinds(), "list,reader", "and neither is a middle click") + } + function test_first_run_stacks_follow_what_the_service_knows() { mailService.anyAccountReady = false mailService.hasSavedAccounts = false From 8e47f75dabbbb0c8759b5dc0ef4b56651a8f6b69 Mon Sep 17 00:00:00 2001 From: Daniel Biasiotto Date: Sun, 25 Oct 2026 04:17:51 +0100 Subject: [PATCH 48/88] Fix mailboxes restored before credential store is available (#401) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves #391. ## Summary Adds exponential backoff retry to IMAP and JMAP authentication when the system credential store answers `credential_store_unavailable` during session restore, avoids repeated D-Bus unlock prompts on locked Secret Service collections, and clears recovery errors upon successful sign-in. ## Problem & Motivation At desktop login, Omamail restores mailboxes before background Secret Service daemons (such as KeePassXC or keyring managers) finish unlocking. Previously, the initial unavailability response was treated as terminal or missing, stranding mailboxes at "Not connected" until a manual shell restart. Additionally, querying locked Secret Service collections during startup triggered interactive D-Bus unlock prompts with a 5-second deadline. When entering passphrases or selecting keyfiles on external media, this deadline repeatedly timed out and cancelled in the daemon's event loop. Users with locked-at-boot keyrings or external hardware/keyfile credentials benefit. ## Visual Evidence ### Flow Demonstration (Locked Keyring → Auto Reconnecting → Keyring Unlocked → Auto Synchronized) ![Credential Store Recovery Demonstration](https://github.com/denialbb/omamail/releases/download/pr401-assets/omamail-recovery-censored.gif) ### Before / After | Reconnecting State (`"The credential store is unavailable"`) | Recovered State (Inbox Synced & Error Cleared) | | :---: | :---: | | ![Reconnecting State](https://github.com/denialbb/omamail/releases/download/pr401-assets/omamail-reconnecting-censored.png) | ![Recovered State](https://github.com/denialbb/omamail/releases/download/pr401-assets/omamail-connected-censored.png) | *(Direct video download: [omamail-recovery-censored.mp4](https://github.com/denialbb/omamail/releases/download/pr401-assets/omamail-recovery-censored.mp4))* ## Architectural Changes - **Secret Service matching item query (`src/credentials/secret_service.rs`)**: Searches items across collections first. If an unlocked matching item exists, reads it immediately without gating on unrelated locked collections. If no match exists and the default collection is locked, or if the matching item is locked, returns `Error::Unavailable` without triggering interactive prompts. - **Session recovery backoff (`ui/providers/ImapAuth.qml`, `ui/providers/JmapAuth.qml`)**: Schedules exponential backoff (5s doubling to 5m via `Secrets.retryDelay`) when `credentialGet` returns `credential_store_unavailable`. - **State integration & cleanup**: Exposes `recoveringSession` for `MailAccount.setupState` (`"reconnecting"`), clears `lastError` on recovery, and cancels retries and in-flight lookups immediately on `logout()` and `accountId` changes. - **In-flight lookup lifecycle**: Clears `credentialLookupBusy` and settles pending waiters when manual sign-in (`signIn()`) or session invalidation supersedes an in-flight background lookup, ensuring a subsequent failed manual sign-in never leaves restore or retry blocked. - **Startup readiness & concurrency guards**: Defers retry execution while manual sign-in is in flight and gates `restoreSession()` on `backend.ready`. - **Regression test suite (`ui/tests/qml/tst_credential_store_retry.qml`, `src/credentials/secret_service_tests.rs`)**: Adds fast, deterministic tests exercising locked collection recovery, permanent error rejection, `logout()` cancellation, account change cancellation, in-flight sign-in deferral, unlocked reads alongside unrelated locked collections, and delayed lookup callback supersession. ## Verification - `make validate` passed on Linux (x86_64, Qt 6.12.0, cargo 1.99.1): - 704 Rust unit & integration tests (`cargo test --locked --lib`). - 12 Native Secret Service contract tests (`cargo test --features integration-test-credentials credentials_native_linux -- --ignored`), including locked collection handling and unlocked target reads with unrelated locked collections. - 1,537 QML tests passed (`make test-qml`, including 12 test cases in `tst_credential_store_retry.qml`). - Node.js unit tests passed (`make test-js`). - `qmllint` passed with no structural errors (`make qml-check`). - `omarchy plugin validate .` passed. - `git diff --check` clean. - Manual verification: Built and ran standalone app (`make app-run`) against locked KeePassXC; observed `"reconnecting"` state without unexpected unlock prompts, followed by automatic authentication and mail synchronization once unlocked. ## Security **PASS** — No credentials, arguments, or destinations are exposed. Lookup cancellation on `logout()` and account change ensures credentials cannot repopulate signed-out or swapped accounts. Secret Service inspection uses non-prompting D-Bus queries without modifying keyring state. ## Release Notes - Automatically retry IMAP and JMAP connection when the desktop credential store or keyring unlocks after login. - Avoid repeated D-Bus prompt timeouts in Secret Service daemons (such as KeePassXC) while unlocking password databases. - Display "Reconnecting..." status during credential store recovery and clear errors automatically once authenticated. --- Makefile | 2 + src/credentials/secret_service.rs | 20 +- src/credentials/secret_service_tests.rs | 75 +++- tests/test_clipboard.py | 4 +- ui/providers/ImapAuth.qml | 81 ++++- ui/providers/JmapAuth.qml | 84 ++++- ui/providers/Secrets.js | 15 + ui/tests/qml/tst_credential_store_retry.qml | 357 ++++++++++++++++++++ ui/tests/test_secrets.js | 11 + 9 files changed, 642 insertions(+), 7 deletions(-) create mode 100644 ui/tests/qml/tst_credential_store_retry.qml diff --git a/Makefile b/Makefile index a7962fe..b3e7e37 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,11 @@ +export PATH := $(HOME)/.cargo/bin:$(PATH) QMLLINT := /usr/lib/qt6/bin/qmllint .DEFAULT_GOAL := test QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryController.qml ui/compose/PendingCompose.qml \ ui/tests/compatibility/tst_published_agent.qml \ ui/tests/qml/tst_unified_batch.qml ui/tests/qml/tst_clipboard_arguments.qml \ ui/tests/qml/tst_reader_subject_menu.qml ui/tests/qml/tst_drafts_refresh.qml ui/tests/qml/tst_account_hosts.qml \ + ui/tests/qml/tst_credential_store_retry.qml \ ui/backend/Backend.qml ui/backend/Runtime.qml ui/diagnostics/Diagnostics.qml \ ui/components/BackendSetup.qml ui/components/OmamailLogo.qml \ ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/DraftList.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/UnifiedBatchAction.qml ui/account/UnifiedBatchSelection.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ diff --git a/src/credentials/secret_service.rs b/src/credentials/secret_service.rs index 8fa991f..303d989 100644 --- a/src/credentials/secret_service.rs +++ b/src/credentials/secret_service.rs @@ -258,9 +258,19 @@ async fn close(connection: zbus::Connection) { let _ = tokio::time::timeout(CLOSE_DEADLINE, connection.close()).await; } +async fn default_collection_locked(service: &SecretService<'_>) -> bool { + if let Ok(default) = service.get_default_collection().await { + if default.is_locked().await.unwrap_or(false) { + return true; + } + } + false +} + async fn find<'a>( service: &'a SecretService<'a>, attrs: &HashMap, + operation: Operation<'_>, ) -> Result>, Fault> { let result = service .search_items( @@ -281,7 +291,13 @@ async fn find<'a>( .map_err(|_| Fault::Answer(Error::Unavailable))?; return Ok(Some(item)); } - Ok(result.unlocked.into_iter().next()) + if let Some(item) = result.unlocked.into_iter().next() { + return Ok(Some(item)); + } + if matches!(operation, Operation::Get) && default_collection_locked(service).await { + return Err(Fault::Answer(Error::Unavailable)); + } + Ok(None) } async fn execute( @@ -294,7 +310,7 @@ async fn execute( .map_err(Fault::Answer)? .into_iter() .collect(); - let item = find(service, &attrs).await?; + let item = find(service, &attrs, operation).await?; match operation { Operation::Get => { let item = item.ok_or(Fault::Answer(Error::Missing))?; diff --git a/src/credentials/secret_service_tests.rs b/src/credentials/secret_service_tests.rs index 3642cc4..2b9060e 100644 --- a/src/credentials/secret_service_tests.rs +++ b/src/credentials/secret_service_tests.rs @@ -59,6 +59,13 @@ impl Service { fn read_alias(&self, _name: &str) -> OwnedObjectPath { path("/org/freedesktop/secrets/collection/default") } + #[zbus(property)] + fn collections(&self) -> Vec { + vec![ + path("/org/freedesktop/secrets/collection/default"), + path("/org/freedesktop/secrets/collection/unrelated"), + ] + } fn search_items( &self, _attributes: HashMap, @@ -66,7 +73,7 @@ impl Service { self.searches.fetch_add(1, Ordering::SeqCst); if self .failures - .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |left| { + .try_update(Ordering::SeqCst, Ordering::SeqCst, |left| { left.checked_sub(1) }) .is_ok() @@ -154,6 +161,7 @@ struct Fixture { unlocks: Arc, creates: Arc, collection_locked: Arc, + unrelated_collection_locked: Arc, _daemon: Daemon, } impl Fixture { @@ -176,6 +184,13 @@ impl Fixture { fixture.collection_locked.store(true, Ordering::SeqCst); fixture } + fn with_unrelated_locked_collection() -> Self { + let fixture = Self::build(false, false, false, false, 0); + fixture + .unrelated_collection_locked + .store(true, Ordering::SeqCst); + fixture + } fn build( denial: bool, missing: bool, @@ -207,6 +222,7 @@ impl Fixture { let unlocks = Arc::new(AtomicUsize::new(0)); let locked = Arc::new(AtomicBool::new(locked)); let collection_locked = Arc::new(AtomicBool::new(false)); + let unrelated_collection_locked = Arc::new(AtomicBool::new(false)); let failures = Arc::new(AtomicUsize::new(failures)); let creates = Arc::new(AtomicUsize::new(0)); let runtime = tokio::runtime::Builder::new_multi_thread() @@ -244,6 +260,14 @@ impl Fixture { }, ) .unwrap() + .serve_at( + "/org/freedesktop/secrets/collection/unrelated", + Collection { + creates: creates.clone(), + locked: unrelated_collection_locked.clone(), + }, + ) + .unwrap() .serve_at( "/org/freedesktop/secrets/prompt/stuck", Prompt(prompts.clone()), @@ -263,6 +287,7 @@ impl Fixture { unlocks, creates, collection_locked, + unrelated_collection_locked, _daemon: daemon, } } @@ -526,7 +551,7 @@ fn credentials_native_linux_silently_unlocks_a_locked_item() { let service = SecretService::connect_with_existing(EncryptionType::Dh, connection) .await .unwrap(); - find(&service, &attrs).await.map(|item| item.is_some()) + find(&service, &attrs, Operation::Get).await.map(|item| item.is_some()) }); assert!(matches!(found, Ok(true))); @@ -568,6 +593,52 @@ fn credentials_native_linux_a_locked_collection_is_unlocked_before_a_write() { ); } +/// A read on a locked collection answers unavailable immediately rather than +/// mistaking the locked entries for a missing secret or prompting on an +/// unattended startup. +#[test] +#[ignore = "requires native dbus-daemon; mandatory in the Linux credential gate"] +fn credentials_native_linux_a_locked_collection_returns_unavailable_for_get() { + let fixture = Fixture::with_locked_collection(); + let shared = Shared::new().unwrap(); + + assert!(matches!( + run_on( + &shared, + &key(), + Operation::Get, + || fixture.client(), + DEADLINE + ), + Err(Error::Unavailable) + )); + assert_eq!( + fixture.searches.load(Ordering::SeqCst), + 1, + "search did not run before checking collection lock" + ); +} + +/// An unlocked item in the target collection must be read even if an unrelated +/// collection on the Secret Service daemon is locked. +#[test] +#[ignore = "requires native dbus-daemon; mandatory in the Linux credential gate"] +fn credentials_native_linux_unlocked_target_reads_with_unrelated_locked_collection() { + let fixture = Fixture::with_unrelated_locked_collection(); + let attrs: HashMap<_, _> = key().attributes().unwrap().into_iter().collect(); + + let found = fixture.runtime.block_on(async { + let connection = fixture.client().await.unwrap(); + let service = SecretService::connect_with_existing(EncryptionType::Dh, connection) + .await + .unwrap(); + find(&service, &attrs, Operation::Get).await.map(|item| item.is_some()) + }); + + assert!(matches!(found, Ok(true))); + assert_eq!(fixture.searches.load(Ordering::SeqCst), 1); +} + /// One operation costs the caller one deadline. Opening the session and running /// the operation share a single budget, so a connect that never answers cannot /// be waited for once and then waited for again. diff --git a/tests/test_clipboard.py b/tests/test_clipboard.py index e50bc3c..bf91a3b 100644 --- a/tests/test_clipboard.py +++ b/tests/test_clipboard.py @@ -8,9 +8,11 @@ import tempfile ROOT = Path(__file__).resolve().parents[1] -runner = shutil.which("qmltestrunner6") or shutil.which("qmltestrunner") +runner = os.environ.get("QMLTESTRUNNER") or shutil.which("qmltestrunner6") if not runner and Path("/usr/lib/qt6/bin/qmltestrunner").exists(): runner = "/usr/lib/qt6/bin/qmltestrunner" +if not runner: + runner = shutil.which("qmltestrunner") clipboard = shutil.which("wl-copy") if not runner or not clipboard: print("Clipboard transport regression SKIP: requires QML test tooling and wl-copy") diff --git a/ui/providers/ImapAuth.qml b/ui/providers/ImapAuth.qml index 003416c..a536f41 100644 --- a/ui/providers/ImapAuth.qml +++ b/ui/providers/ImapAuth.qml @@ -3,6 +3,7 @@ import Quickshell import Quickshell.Io import "ImapProtocol.js" as Imap +import "Secrets.js" as Secrets // An IMAP account's sign-in, which is a server address and a password. // @@ -50,6 +51,9 @@ Item { // The same three names `AuthManager` exposes, because `MailAccount` reads // them without knowing which provider it has. readonly property bool credentialsPresent: configured + property bool restoreOwed: true + readonly property bool recoveringSession: !loggedIn && !loginBusy && !replacementBusy && configured && restoreOwed + property real timeScale: 1 property bool loginBusy: false // A signed-in mailbox checking a replacement password. Kept apart from // `loginBusy`, which would take the mailbox out of service while it is @@ -76,6 +80,10 @@ Item { property var credentialWaiters: [] property bool lookupHandled: false property string pendingPassword: "" + // Which retry comes next after the store said it was unavailable. The wait + // between attempts doubles; the attempts themselves do not stop, because + // the store can be unlocked at any point in the session. + property int secretRetryAttempt: 0 signal loginSucceeded() signal loggedOut() @@ -122,9 +130,11 @@ Item { function restoreSession() { if (!configured) { + restoreOwed = false passwordChecked = true return } + if (backend && !backend.ready) return if (credentialLookupBusy) return startSecretLookup() } @@ -142,18 +152,58 @@ Item { if (serial !== root.credentialLookupSerial) return root.credentialLookupBusy = false if (boundAccount !== root.accountId) return + if (root.loginBusy || root.replacementBusy) return root.handleSecretLookup(error ? "" : value, error) }) } + function scheduleSecretRetry() { + if (!configured || credentialLookupBusy || secretRetry.running) return + secretRetry.interval = Math.max(1, Math.round(Secrets.retryDelay(secretRetryAttempt) * timeScale)) + secretRetryAttempt++ + secretRetry.start() + } + + function stopSecretRetry() { + secretRetry.stop() + secretRetryAttempt = 0 + } + + Timer { + id: secretRetry + onTriggered: { + if (root.loginBusy || root.replacementBusy) { + secretRetry.interval = Math.max(1, Math.round(1000 * root.timeScale)) + secretRetry.restart() + return + } + if (!root.credentialLookupBusy) root.startSecretLookup() + } + } + function handleSecretLookup(line, error) { if (lookupHandled) return lookupHandled = true - if (error && error !== "credential_missing") { + if (loginBusy || replacementBusy) return + if (error === "credential_store_unavailable") { passwordChecked = false + restoreOwed = true lastError = "The credential store is unavailable" finishWaiters("", lastError) if (configured) sessionUnavailable(lastError) + // A store that is starting, or whose database is still locked, answers + // exactly like this at login and works a moment later. Asking again is + // what turns that into a mailbox that comes up on its own. + scheduleSecretRetry() + return + } + stopSecretRetry() + restoreOwed = false + if (error && error !== "credential_missing") { + passwordChecked = false + lastError = safeError(error) + finishWaiters("", lastError) + if (configured) sessionUnavailable(lastError) return } passwordChecked = true @@ -165,6 +215,7 @@ Item { if (configured) sessionUnavailable("Sign in to this mailbox") return } + lastError = "" password = value finishWaiters(settings.username + ":" + password, "") loginSucceeded() @@ -206,6 +257,9 @@ Item { } lastError = "" sessionGeneration++ + credentialLookupSerial++ + credentialLookupBusy = false + finishWaiters("", "Manual sign-in in progress") if (password !== "") replacementBusy = true else loginBusy = true pendingPassword = value @@ -226,10 +280,15 @@ Item { lastError = safeError(error) || "The server rejected that username or password" return } + restoreOwed = false password = pendingPassword pendingPassword = "" passwordChecked = true lastError = "" + // A password the server just accepted needs no keyring lookup: stop any + // retry scheduled while the store was unreachable, so a late one cannot + // report the mailbox as unavailable over a working sign-in. + stopSecretRetry() storePassword() loginSucceeded() } @@ -266,6 +325,11 @@ Item { function logout() { var boundAccount = accountId abandonSignIn() + stopSecretRetry() + restoreOwed = false + credentialLookupSerial++ + credentialLookupBusy = false + finishWaiters("", "Signed out") password = "" passwordChecked = true if (credentialWriteBusy && credentialWriteAccount === boundAccount) @@ -279,6 +343,11 @@ Item { // that has started rejecting it should not be asked a hundred more times // with the same value. function invalidateAccessToken() { + stopSecretRetry() + restoreOwed = false + credentialLookupSerial++ + credentialLookupBusy = false + finishWaiters("", "Session invalidated") password = "" passwordChecked = false } @@ -303,6 +372,8 @@ Item { // is what stops an account rename from leaving the previous account's // credential in front of the new one's server. abandonSignIn() + stopSecretRetry() + restoreOwed = true password = "" passwordChecked = false lookupHandled = false @@ -313,4 +384,12 @@ Item { pendingCredentialDelete = credentialWriteAccount } + Connections { + target: root.backend + ignoreUnknownSignals: true + function onReadyChanged() { + if (root.backend && root.backend.ready === true) root.restoreSession() + } + } + } diff --git a/ui/providers/JmapAuth.qml b/ui/providers/JmapAuth.qml index 9b472a9..4fab904 100644 --- a/ui/providers/JmapAuth.qml +++ b/ui/providers/JmapAuth.qml @@ -3,6 +3,7 @@ import Quickshell import Quickshell.Io import "JmapProtocol.js" as Jmap +import "Secrets.js" as Secrets // A JMAP account's sign-in, which is an address and one secret. // @@ -63,6 +64,9 @@ Item { // The three names `MailAccount` reads without knowing which provider it has. readonly property bool credentialsPresent: configured + property bool restoreOwed: true + readonly property bool recoveringSession: !loggedIn && !loginBusy && configured && restoreOwed + property real timeScale: 1 property bool loginBusy: false property bool credentialLookupBusy: false property bool credentialWriteBusy: false @@ -108,6 +112,10 @@ Item { property var credentialWaiters: [] property bool lookupHandled: false property string pendingSecret: "" + // Which retry comes next after the store said it was unavailable. The wait + // between attempts doubles; the attempts themselves do not stop, because + // the store can be unlocked at any point in the session. + property int secretRetryAttempt: 0 signal loginSucceeded() signal loggedOut() @@ -170,9 +178,11 @@ Item { function restoreSession() { if (!configured) { + restoreOwed = false secretChecked = true return } + if (backend && !backend.ready) return if (credentialLookupBusy) return startSecretLookup() } @@ -190,18 +200,58 @@ Item { if (serial !== root.credentialLookupSerial) return root.credentialLookupBusy = false if (boundAccount !== root.accountId) return + if (root.loginBusy) return root.handleSecretLookup(error ? "" : value, error) }) } + function scheduleSecretRetry() { + if (!configured || credentialLookupBusy || secretRetry.running) return + secretRetry.interval = Math.max(1, Math.round(Secrets.retryDelay(secretRetryAttempt) * timeScale)) + secretRetryAttempt++ + secretRetry.start() + } + + function stopSecretRetry() { + secretRetry.stop() + secretRetryAttempt = 0 + } + + Timer { + id: secretRetry + onTriggered: { + if (root.loginBusy) { + secretRetry.interval = Math.max(1, Math.round(1000 * root.timeScale)) + secretRetry.restart() + return + } + if (!root.credentialLookupBusy) root.startSecretLookup() + } + } + function handleSecretLookup(line, error) { if (lookupHandled) return lookupHandled = true - if (error && error !== "credential_missing") { + if (loginBusy) return + if (error === "credential_store_unavailable") { secretChecked = false + restoreOwed = true lastError = "The credential store is unavailable" finishWaiters(null, lastError) if (configured) sessionUnavailable(lastError) + // A store that is starting, or whose database is still locked, answers + // exactly like this at login and works a moment later. Asking again is + // what turns that into a mailbox that comes up on its own. + scheduleSecretRetry() + return + } + stopSecretRetry() + restoreOwed = false + if (error && error !== "credential_missing") { + secretChecked = false + lastError = safeError(error) + finishWaiters(null, lastError) + if (configured) sessionUnavailable(lastError) return } secretChecked = true @@ -213,6 +263,7 @@ Item { if (configured) sessionUnavailable("Sign in to this mailbox") return } + lastError = "" secret = value finishWaiters(credential(), "") loginSucceeded() @@ -246,6 +297,9 @@ Item { } lastError = "" serverNeeded = false + credentialLookupSerial++ + credentialLookupBusy = false + finishWaiters(null, "Manual sign-in in progress") loginBusy = true pendingSecret = typed verifyRequested(settings, address, typed) @@ -269,11 +323,16 @@ Item { return } serverNeeded = false + restoreOwed = false sendingOffered = !result || result.canSend !== false secret = pendingSecret pendingSecret = "" secretChecked = true lastError = "" + // A secret the server just accepted needs no keyring lookup: stop any + // retry scheduled while the store was unreachable, so a late one cannot + // report the mailbox as unavailable over a working sign-in. + stopSecretRetry() sessionVerified(result || {}) storeSecret() // A tick later than the rest of this, and deliberately: `sessionVerified` @@ -315,6 +374,12 @@ Item { function logout() { var boundAccount = accountId + cancelLogin() + stopSecretRetry() + restoreOwed = false + credentialLookupSerial++ + credentialLookupBusy = false + finishWaiters(null, "Signed out") secret = "" pendingSecret = "" secretChecked = true @@ -329,6 +394,11 @@ Item { // that has started refusing it should not be asked a hundred more times with // the same value. function invalidateAccessToken() { + stopSecretRetry() + restoreOwed = false + credentialLookupSerial++ + credentialLookupBusy = false + finishWaiters(null, "Session invalidated") secret = "" secretChecked = false } @@ -340,12 +410,16 @@ Item { function cancelLogin() { loginBusy = false progressStep = 0 + pendingSecret = "" } onAccountIdChanged: { // A different mailbox has a different secret. Dropping the one in memory // is what stops an account rename from leaving the previous account's // credential in front of the new one's server. + cancelLogin() + stopSecretRetry() + restoreOwed = true secret = "" secretChecked = false lookupHandled = false @@ -356,4 +430,12 @@ Item { pendingCredentialDelete = credentialWriteAccount } + Connections { + target: root.backend + ignoreUnknownSignals: true + function onReadyChanged() { + if (root.backend && root.backend.ready === true) root.restoreSession() + } + } + } diff --git a/ui/providers/Secrets.js b/ui/providers/Secrets.js index b1978a3..55e4c9c 100644 --- a/ui/providers/Secrets.js +++ b/ui/providers/Secrets.js @@ -1,5 +1,7 @@ .pragma library +.import "OAuth.js" as OAuth + // What comes back from `secret-tool`, and what it is safe to change about it. // // A secret is bytes the user chose and the keyring stored, so the only thing @@ -18,3 +20,16 @@ function fromKeyring(value) { var text = String(value === undefined || value === null ? "" : value) return text.charAt(text.length - 1) === "\n" ? text.substring(0, text.length - 1) : text } + +// How long to wait before asking the store again after it answered that it was +// unavailable. A mailbox restored at login can beat the password manager that +// owns the Secret Service: the service is starting, or the database it serves +// has not been unlocked yet. Neither is a mailbox without a password, and both +// resolve on their own, so the lookup is repeated rather than reported as a +// sign-out. Doubling up to five minutes: frequent enough that an unlock a few +// seconds after login is noticed while the window is still open, sparse enough +// that a store which is simply not there costs one refused request per mailbox +// every five minutes instead of a spin. +function retryDelay(attempt) { + return OAuth.refreshRetryDelay(attempt) +} diff --git a/ui/tests/qml/tst_credential_store_retry.qml b/ui/tests/qml/tst_credential_store_retry.qml new file mode 100644 index 0000000..add4dde --- /dev/null +++ b/ui/tests/qml/tst_credential_store_retry.qml @@ -0,0 +1,357 @@ +import QtQuick +import QtTest +import "../../providers" as Providers + +// A mailbox restored at login can ask the credential store before the password +// manager that owns it has finished starting or the database it serves has been +// unlocked. The store says it is unavailable, which is not the same answer as +// "this mailbox has no password": the second one stays true, the first changes +// a moment later. These pin the difference — the lookup is repeated, and it is +// repeated only while the store is the thing that failed. +Item { + id: root + + QtObject { + id: store + property bool available: false + property string errorCode: "credential_store_unavailable" + property string value: "app-password" + property int lookups: 0 + property var pendingCallback: null + property bool delayed: false + function reset() { + available = false + errorCode = "credential_store_unavailable" + value = "app-password" + lookups = 0 + pendingCallback = null + delayed = false + } + function triggerPending() { + if (typeof pendingCallback === "function") { + var cb = pendingCallback + pendingCallback = null + if (!available) cb("", errorCode) + else cb(value, "") + } + } + function credentialGet(kind, accountId, clientId, callback) { + lookups++ + if (delayed) { + pendingCallback = callback + return true + } + if (!available) { + callback("", errorCode) + return true + } + callback(value, "") + return true + } + function credentialDelete(kind, accountId, clientId, callback) { + if (typeof callback === "function") callback(true, "") + return true + } + } + + QtObject { + id: backendMock + property bool ready: false + } + + Component { + id: imapFactory + + Providers.ImapAuth { + pluginDir: "/synthetic" + platform: store + accountId: "imap:one@example.org" + timeScale: 0.01 + settings: ({imapHost:"imap.example.org",imapPort:993,smtpHost:"smtp.example.org", + smtpPort:465,username:"one@example.org",aliases:[],insecure:false}) + } + } + + Component { + id: jmapFactory + + Providers.JmapAuth { + pluginDir: "/synthetic" + platform: store + accountId: "jmap:one@example.org" + address: "one@example.org" + timeScale: 0.01 + settings: ({sessionUrl:"https://jmap.example.org/session",username:"one@example.org", + authScheme:"basic",accountId:"one"}) + } + } + + Component { + id: imapWithBackendFactory + + Providers.ImapAuth { + pluginDir: "/synthetic" + platform: store + backend: backendMock + accountId: "imap:delayed@example.org" + timeScale: 0.01 + settings: ({imapHost:"imap.example.org",imapPort:993,smtpHost:"smtp.example.org", + smtpPort:465,username:"delayed@example.org",aliases:[],insecure:false}) + } + } + + SignalSpy { id: loginSpy; signalName: "loginSucceeded" } + SignalSpy { id: logoutSpy; signalName: "loggedOut" } + + TestCase { + name: "CredentialStoreRetry" + + function init() { + store.reset() + backendMock.ready = false + loginSpy.target = null + loginSpy.clear() + logoutSpy.target = null + logoutSpy.clear() + } + + function test_an_unavailable_store_is_asked_again_by_an_imap_mailbox() { + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + compare(auth.recoveringSession, true) + auth.restoreSession() + compare(store.lookups, 1) + compare(auth.loggedIn, false) + compare(auth.recoveringSession, true) + compare(auth.lastError, "The credential store is unavailable") + + // The store finishes starting, or the user unlocks it, before the + // mailbox is looked at again. + store.available = true + tryVerify(function() { return auth.loggedIn }, 1000) + verify(store.lookups >= 2) + compare(auth.password, "app-password") + compare(auth.lastError, "") + compare(auth.recoveringSession, false) + compare(auth.secretRetryAttempt, 0) + } + + function test_an_unavailable_store_is_asked_again_by_a_jmap_mailbox() { + var auth = createTemporaryObject(jmapFactory, root) + verify(auth) + compare(auth.recoveringSession, true) + auth.restoreSession() + compare(store.lookups, 1) + compare(auth.loggedIn, false) + compare(auth.recoveringSession, true) + compare(auth.lastError, "The credential store is unavailable") + + store.available = true + tryVerify(function() { return auth.loggedIn }, 1000) + verify(store.lookups >= 2) + compare(auth.secret, "app-password") + compare(auth.lastError, "") + compare(auth.recoveringSession, false) + compare(auth.secretRetryAttempt, 0) + } + + function test_a_missing_credential_is_not_asked_again() { + store.available = true + store.value = "" + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + auth.restoreSession() + compare(store.lookups, 1) + compare(auth.loggedIn, false) + compare(auth.password, "") + // No retry is scheduled: the answer will not change by itself. + compare(auth.secretRetryAttempt, 0) + compare(auth.recoveringSession, false) + } + + function test_permanent_error_is_not_retried() { + store.available = false + store.errorCode = "backend_needs_update" + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + auth.restoreSession() + compare(store.lookups, 1) + compare(auth.loggedIn, false) + compare(auth.lastError, "backend_needs_update") + compare(auth.secretRetryAttempt, 0) + compare(auth.recoveringSession, false) + } + + function test_logout_cancels_retry_timer_and_discards_subsequent_keyring_unlock() { + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + loginSpy.target = auth + logoutSpy.target = auth + + auth.restoreSession() + compare(store.lookups, 1) + compare(auth.loggedIn, false) + compare(auth.recoveringSession, true) + compare(auth.secretRetryAttempt, 1) + + // User logs out while retry is active + auth.logout() + compare(logoutSpy.count, 1) + compare(auth.secretRetryAttempt, 0) + compare(auth.recoveringSession, false) + compare(auth.password, "") + + // Keyring unlocks later + store.available = true + store.value = "stolen-password" + wait(150) + + // The signed-out account must not be repopulated or emit loginSucceeded + compare(auth.loggedIn, false) + compare(auth.password, "") + compare(auth.recoveringSession, false) + compare(loginSpy.count, 0) + } + + function test_in_flight_signin_prevents_retry_clobbering_error() { + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + auth.restoreSession() + compare(auth.secretRetryAttempt, 1) + compare(auth.lastError, "The credential store is unavailable") + + // Manual sign-in begins while retry timer is running + var started = auth.signIn("typed-password") + verify(started) + compare(auth.loginBusy, true) + compare(auth.lastError, "") + + // Wait for timer to trigger while sign-in is busy; lastError must not be overwritten + wait(100) + compare(auth.loginBusy, true) + compare(auth.lastError, "") + + // Server rejects the manual sign-in + auth.completeSignIn(false, "Server rejected password") + compare(auth.loginBusy, false) + compare(auth.lastError, "Server rejected password") + } + + function test_account_change_cancels_retry_and_discards_stale_callback() { + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + loginSpy.target = auth + + auth.restoreSession() + compare(store.lookups, 1) + compare(auth.loggedIn, false) + compare(auth.recoveringSession, true) + compare(auth.secretRetryAttempt, 1) + + // Account changes while retry is scheduled + auth.accountId = "imap:two@example.org" + compare(auth.secretRetryAttempt, 0) + compare(auth.password, "") + compare(auth.loggedIn, false) + + // Keyring unlocks later; previous retry timer was cancelled, so no lookup fires + store.available = true + store.value = "stale-password" + wait(150) + + compare(auth.password, "") + compare(auth.loggedIn, false) + compare(loginSpy.count, 0) + } + + function test_backend_not_ready_delays_lookup_until_ready() { + backendMock.ready = false + var auth = createTemporaryObject(imapWithBackendFactory, root) + verify(auth) + auth.restoreSession() + // Backend was not ready: no lookup attempted yet, but restore is owed + compare(store.lookups, 0) + compare(auth.recoveringSession, true) + + // Backend comes up + backendMock.ready = true + compare(store.lookups, 1) + } + + function test_manual_signin_supersedes_in_flight_lookup_and_clears_busy() { + store.delayed = true + var auth = createTemporaryObject(imapFactory, root) + verify(auth) + + var waiterCalled = false + var waiterError = "" + auth.withCredentials(function(creds, err) { + waiterCalled = true + waiterError = err + }) + + auth.restoreSession() + compare(store.lookups, 1) + verify(store.pendingCallback !== null) + + var started = auth.signIn("typed-password") + verify(started) + compare(auth.loginBusy, true) + verify(waiterCalled) + compare(waiterError, "Manual sign-in in progress") + + store.triggerPending() + compare(auth.password, "") + + auth.completeSignIn(false, "Server rejected password") + compare(auth.loginBusy, false) + compare(auth.lastError, "Server rejected password") + + store.delayed = false + store.available = true + store.value = "saved-password" + auth.restoreSession() + compare(store.lookups, 2) + compare(auth.password, "saved-password") + compare(auth.loggedIn, true) + } + + function test_jmap_manual_signin_supersedes_in_flight_lookup_and_clears_busy() { + store.delayed = true + var auth = createTemporaryObject(jmapFactory, root) + verify(auth) + + var waiterCalled = false + var waiterError = "" + auth.withCredentials(function(creds, err) { + waiterCalled = true + waiterError = err + }) + + auth.restoreSession() + compare(store.lookups, 1) + verify(store.pendingCallback !== null) + + var started = auth.signIn("typed-token", "one@example.org") + verify(started) + compare(auth.loginBusy, true) + verify(waiterCalled) + compare(waiterError, "Manual sign-in in progress") + + store.triggerPending() + compare(auth.secret, "") + + auth.completeSignIn(false, null, "Unauthorized", false) + compare(auth.loginBusy, false) + compare(auth.lastError, "Unauthorized") + + store.delayed = false + store.available = true + store.value = "saved-token" + auth.restoreSession() + compare(store.lookups, 2) + compare(auth.secret, "saved-token") + compare(auth.loggedIn, true) + } + } +} diff --git a/ui/tests/test_secrets.js b/ui/tests/test_secrets.js index 83d871a..92482cb 100644 --- a/ui/tests/test_secrets.js +++ b/ui/tests/test_secrets.js @@ -19,4 +19,15 @@ assert.strictEqual(secrets.fromKeyring(""), "") assert.strictEqual(secrets.fromKeyring(null), "") assert.strictEqual(secrets.fromKeyring(undefined), "") +// A store that is unavailable is worth asking again, but not in a tight loop: +// the wait doubles and then stays at five minutes. +assert.strictEqual(secrets.retryDelay(0), 5000) +assert.strictEqual(secrets.retryDelay(1), 10000) +assert.strictEqual(secrets.retryDelay(2), 20000) +assert.strictEqual(secrets.retryDelay(6), 300000) +assert.strictEqual(secrets.retryDelay(100), 300000) +assert.strictEqual(secrets.retryDelay(-1), 5000) +assert.strictEqual(secrets.retryDelay("2"), 20000) +assert.strictEqual(secrets.retryDelay(undefined), 5000) + console.log("Secrets.js ok") From 38a15ed4489678d5367a24951a28d34ff4784667 Mon Sep 17 00:00:00 2001 From: aishag80g-stack Date: Sun, 25 Oct 2026 01:09:40 -0300 Subject: [PATCH 49/88] Fix generic Gmail sign-in messages when mailbox access is refused (#394) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Reproduction 1. A Gmail account managed by a Workspace admin (or an account setting) that has refused this OAuth app's mailbox access. 2. Sign in: the poll ends terminally with the static backend error `gmail_forbidden` — the identifier is already reviewed and in `scripts/diagnostics.py`'s `MESSAGES`, so it survives redaction into `errors.json`. 3. The login screen shows: *"Google sign-in failed. Please try again"*. 4. Retrying forever never clears it: the refusal is an admin/account setting, not a transient sign-in hiccup. The generic copy points the user at the one action that cannot help. ## Fix Name the error in the terminal-auth chain of `ui/providers/AuthManager.qml`, next to the existing `auth_missing_scope` and `auth_keyring_failed` branches: > Google refused this app's access to your mailbox. Allow the app in your account or Workspace admin settings, then sign in again ## Verification Author-reported checks: `tests/test_source.sh` grows a source tripwire in the file's existing style: the terminal auth error chain must name `gmail_forbidden`. - On the base commit: `test_source.sh: a refused gmail_forbidden sign-in must not read as a transient sign-in failure` (exit 1). - With the change: `test_source.sh ok` (exit 0), `tests/test_diagnostics.py` OK (13 tests), `qmllint` clean on the touched file. ## Review scope and limitations The added branch applies to the existing broad `gmail_forbidden` identifier. The backend also uses this identifier for non-rate-limited 403 responses such as `dailyLimitExceeded`; the current wording does not distinguish those cases from an account or Workspace policy refusal. Independent review of `2e7e247708fc7e4194ca7c33f798afaafad069af`: source checks and 13 diagnostic tests passed, as did the native controlled-server 403 classification regression. A synthetic runtime check exercised the actual login-error function for forbidden, missing-scope, keyring and unknown errors. QML lint, plugin validation and diff checks passed. Rust and JavaScript suites passed. `make validate` stopped at an unchanged AI-session file-permission test; a targeted rerun then passed all 14 AI bridge tests. The setup-page QML suite passed all 8 results. The aggregate validation command has not completed successfully, and local standalone composition remains unverified. No live Gmail account or browser/keyring data was used. Before/after UI screenshots have not been supplied. ## Release Notes - Gmail sign-in errors reporting refused mailbox access now show account and Workspace admin guidance instead of generic retry advice. --- tests/test_source.sh | 11 +++++++++++ ui/providers/AuthManager.qml | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/test_source.sh b/tests/test_source.sh index 1790c24..c8c79b4 100755 --- a/tests/test_source.sh +++ b/tests/test_source.sh @@ -1415,3 +1415,14 @@ if grep -E 'secret-tool|scripts/keyring-(lookup|store|clear)\.sh' \ providers/*.qml calendar/*.qml >/dev/null; then fail "QML credential paths must use the typed backend credential RPC" fi + +# A Workspace admin refusing the app's mailbox access surfaces as the static +# backend error gmail_forbidden. Naming it is the only way the login screen +# says "allow the app in your account settings"; the generic fallback sends +# people to sign in again forever against a refusal that never clears. +python3 - <<'PY' +from pathlib import Path +text = Path("providers/AuthManager.qml").read_text() +if '"gmail_forbidden"' not in text: + raise SystemExit("test_source.sh: a refused gmail_forbidden sign-in must not read as a transient sign-in failure") +PY diff --git a/ui/providers/AuthManager.qml b/ui/providers/AuthManager.qml index 98e8230..709744e 100644 --- a/ui/providers/AuthManager.qml +++ b/ui/providers/AuthManager.qml @@ -425,7 +425,9 @@ Item { root.failLogin(error.message === "auth_missing_scope" ? "Google sign-in is missing permissions. Sign in again and leave every checkbox ticked" : (error.message === "auth_keyring_failed" ? "Could not save the Google session to the keyring. Please try again" - : "Google sign-in failed. Please try again")) + : (error.message === "gmail_forbidden" + ? "Google refused this app's access to your mailbox. Allow the app in your account or Workspace admin settings, then sign in again" + : "Google sign-in failed. Please try again"))) return } if (result.pending) { nativePoll.start(); return } From 9e5e416e517b2229a9e40a3c898d3c99d030862a Mon Sep 17 00:00:00 2001 From: jansimner Date: Sun, 25 Oct 2026 05:27:31 +0100 Subject: [PATCH 50/88] Fix oversized UID arguments in IMAP listings and searches (#393) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An IMAP inbox on a self-hosted Stalwart 0.15.5 server never lists: every `imap.list` fails with `imap_command_failed`, while login, folders and counts succeed. Stalwart refuses any single command argument longer than 4096 bytes before running it (`BAD [PARSE] Argument exceeds maximum length of 4096 bytes.`; measured against the live server, an argument of exactly 4096 bytes is accepted and 4097 is not), and the date scan in `list()` names every UID of a batch of up to 4096 snapshot UIDs in one comma list. For an INBOX of 1,795 messages with four-digit UIDs that list is about 9 KB. This is #349, where a Coremail-derived server closes the connection near 10 KB instead, and the IMAP half of #294, where Exchange Online answers `BAD Command Error. 10`. The date scan's batch is a contiguous slice of the sorted snapshot, so it is now named by its first and last UID, as the second commit of #294 proposed. The range adds no message: new mail only ever gets a higher UID, so any other UID it spans was expunged, and the existing filter still holds the reply to the batch. Server search has the same defect, and #294 did not reach it. `search.rs` fetched the arrival dates of a window's matches as one comma list, so on the same server a search for `https` (2,218 matches) failed with `imap_command_failed` while one for `unsubscribe` (148 matches) worked. Matches are a subset of the window, so always naming the window's range would make every small search fetch up to 4096 rows. Instead the matches are written as one set with consecutive runs folded into `a:b`, and only a set longer than 4096 bytes becomes the range of the inventory window the SEARCH just covered; `fetched` already drops the non-matches. That keeps one FETCH per window, so a slow server spends no extra round trips inside the 12-second worker deadline. Content verification never reaches the fallback: its batches of at most 64 UIDs fit with room to spare, and a range there would read bodies of messages that are not candidates. `imap.messages` and the mutation commands still send comma lists. They are capped at 500 UIDs per call, which stays within 4096 bytes while UIDs have at most seven digits and within the ~10 KB limits above at ten digits, so they are left for a separate change. `docs/providers/imap.md` now records the argument limit. ## Verification Author-reported verification: `make validate` green on this commit: Rust 799 passed in 16 targets, QML 1601 passed, JavaScript and shell suites, `omarchy plugin validate` and `git diff --check`. On this machine that needed `/usr/lib/qt6/bin` ahead of `/usr/bin`, because qt5-declarative's `qmltestrunner` is also installed and `tests/test_clipboard.py` picks it first, where it fails silently on Qt 6 QML; and `commit.gpgsign=false` through `GIT_CONFIG_*`, because `tests/test_backend_release.py` commits in a temporary repository under the global signing configuration. Neither touches this change. New tests; the first two were run against unfixed `main` and failed with `imap_command_failed`: - `date_scan_fits_a_server_capping_each_argument_at_4096_bytes`: a fake server that refuses any argument over 4096 bytes with Stalwart's reply, and an INBOX of UIDs 4–2300 with every seventh missing. - `sparse_match_fetches_fit_a_server_capping_each_argument_at_4096_bytes`: 5,000 UIDs of which only odd ones match, so nothing folds; asserts the estimate, the first page and exactly one FETCH per window. - `a_fetch_names_the_matches_until_they_are_longer_than_one_argument`: run folding and the boundary. A 4096-byte set is named as it is, a 4097-byte one becomes the inventory range; changing `>` to `>=` makes it fail. Fixtures that parsed comma lists now also read `a:b`, and the paging fixture answers a set with the messages it names rather than expanding every number in a range, as a server does. Their assertions are unchanged, except that `sparse_search…` now expects the date scan's range. Live against Stalwart 0.15.5 through `omamail list`, the 0.11.1 release backend against this change: INBOX failed / 25 messages, estimate 1,795; search `https` failed / works; search `unsubscribe` works / works. With this backend installed in the plugin and the shell restarted, `errors.json` gained no entries and the plugin filled its INBOX cache. A fresh agent reviewed the diff against AGENTS.md. It found no correctness fault and confirmed both behavioural tests fail without the change. It raised that the byte boundary was neither tested nor verified, now covered by the unit test and measured against Stalwart; that an earlier version split oversized sets into several FETCHes, adding round trips inside the worker deadline, now replaced by the single inventory-range fallback; stale wording in `docs/providers/imap.md` and a `read.rs` comment, now updated; the remaining 500-UID comma lists, noted above as out of scope; and a no-op `drop(p)` and a weak `> 2` assertion, removed and tightened. A second pass over the revised diff found nothing blocking: it confirmed that every identity reaching the fallback searches a contiguous slice of existing UIDs and that the unit test catches a fallback range off by one at either end, and it narrowed the new documentation bullet, which had claimed the limit for the 500-UID paths as well. ## Release Notes - IMAP mailboxes on servers that refuse long commands, such as Stalwart, now list their mail, and searches with many results no longer fail there. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Independent review Reviewed `2c446be5b6de2f7806f105f651046a60a2010ddb`: no actionable implementation findings identified. This restores existing IMAP listing/search behavior; it adds no UI or configuration. Security: PASS for numeric UID-set construction, filtered result scope, exact body-candidate reads, and preserved transport/resource boundaries. UI evidence: NOT APPLICABLE. Local Rust, JavaScript, shell/source and QML suites passed (main QML suite: 1,525 results; additional suites: 6, 64 and 6). All three new regressions passed. Three additional isolated tests of the exact set formatter passed for maximum UID values, exact 1–64-message body batches, and sparse metadata fallback bounds. Standalone composition/inventory tests, QML lint, plugin validation and diff checks passed separately. The aggregate `make validate` command stopped because CMake was absent from its PATH; the standalone stage was independently completed with isolated CMake 3.31.6. No live IMAP account was used in the independent review, and the author's unfixed-base negative controls were not independently rerun. The initial CI runs required maintainer approval; both were inspected and approved for execution. The first CI attempt failed only in the macOS standalone-app job, in the unchanged Microsoft 365 test `an_accepted_send_does_not_become_unknown_when_the_unused_body_stalls`. All other jobs passed. A single targeted rerun of the macOS job has been started; its result is pending. Required portable/backend checks have passed, but no approval is being published while the failed job remains unresolved. The branch also needs synchronization with main under the repository's strict up-to-date policy. No implementation changes or merge were made during review. Co-authored-by: Claude Opus 5.5 (1M context) --- docs/providers/imap.md | 3 +- src/providers/imap/read.rs | 24 ++-- src/providers/imap/read/search.rs | 40 +++++- src/providers/imap/read/search/tests.rs | 133 +++++++++++++++++- .../imap/read/search/tests/paging.rs | 14 +- src/providers/imap/read/tests.rs | 70 ++++++++- tests/cli/search.rs | 8 +- 7 files changed, 266 insertions(+), 26 deletions(-) diff --git a/docs/providers/imap.md b/docs/providers/imap.md index 99ad38e..aa59e2e 100644 --- a/docs/providers/imap.md +++ b/docs/providers/imap.md @@ -5,7 +5,8 @@ Required when touching native IMAP/SMTP transport, IMAP parsing, listing, paging - Loopback connections, including custom Proton Bridge ports, upgrade with STARTTLS before authentication; 993/465 use implicit TLS. `insecure` permits a self-signed certificate only on loopback, never plaintext or downgrade after failed STARTTLS. Remote connections use STARTTLS on standard STARTTLS ports and implicit TLS otherwise, with certificate verification. Plaintext synthetic peers require `testPlaintext` compiled only for tests or `integration-test-credentials`. - `Imap.js` owns protocol strings/parsing; RFC 822 formatting belongs to the message module. The legacy `scripts/mail-transport.sh` carries base64 fields over stdin and passes curl config over stdin. Follow [security boundaries](../SECURITY-BOUNDARIES.md); base64 does not validate config values. - Keep responses byte-preserving: IMAP literal lengths count octets, not UTF-8 characters. The legacy base64 transport preserves that distinction. -- UID order is not arrival order: Bridge can assign imported mail UIDs newest-first. Snapshot UIDs with `UID FETCH 1:* (UID)`, fetch INTERNALDATE in explicit batches of at most 4096, search stable UID windows, and sort by date before paging. Descending UID is only a date tie-breaker. Do not settle a newest prefix from a high-UID window or substitute shifting sequence-number windows. Unsolicited UID/FLAGS updates must preserve known dates. +- UID order is not arrival order: Bridge can assign imported mail UIDs newest-first. Snapshot UIDs with `UID FETCH 1:* (UID)`, fetch INTERNALDATE in batches of at most 4096 snapshot UIDs named by their first and last UID, search stable UID windows, and sort by date before paging. Descending UID is only a date tie-breaker. Do not settle a newest prefix from a high-UID window or substitute shifting sequence-number windows. Unsolicited UID/FLAGS updates must preserve known dates. +- A UID set argument longer than 4096 bytes fails: Stalwart refuses it before running the command (`BAD [PARSE] Argument exceeds maximum length of 4096 bytes`; exactly 4096 is accepted), a Coremail-derived server closes the connection near 10 KB, and Exchange Online answers `BAD Command Error. 10`. A comma list of a few hundred UIDs already crosses that line. The date scan names each contiguous batch by its first and last UID; search names its matches exactly while they fit and otherwise the range of the inventory window it searched, which keeps one FETCH per window. `imap.messages` and mutations still send comma lists of at most 500 UIDs, which stay within the limit only while UIDs have at most seven digits. - Use `BODY.PEEK`, not `BODY`, for reads that must not mark mail seen. - Use `UID EXPUNGE`, never bare `EXPUNGE`, which also deletes messages marked by other clients. - IDs are `:`; UIDs alone collide across folders. diff --git a/src/providers/imap/read.rs b/src/providers/imap/read.rs index 9a11225..1fd7141 100644 --- a/src/providers/imap/read.rs +++ b/src/providers/imap/read.rs @@ -527,19 +527,27 @@ async fn list(w: &mut Wire, p: &Value, boxes: &Mailboxes) -> Result { // UID order is not arrival order (notably during Proton Bridge imports). // No UID window is a settled newest prefix: collect dates before paging. let scan = async { - // Keep the compact, response-limited UID snapshot. Explicit UID batches - // bound the larger date responses without walking gaps in sparse UIDs - // or relying on sequence numbers that move when mail is expunged. + // Keep the compact, response-limited UID snapshot. Batches of snapshot + // UIDs bound the larger date responses without walking gaps in sparse + // UIDs or relying on sequence numbers that move when mail is expunged. let mut snapshot: Vec<_> = fetched_dates(&command(w, "UID FETCH 1:* (UID)").await?)? .into_keys() .collect(); let mut dates = BTreeMap::new(); for window in snapshot.chunks(4096) { - let set = window - .iter() - .map(u32::to_string) - .collect::>() - .join(","); + // Name the batch by its first and last UID, never as a list: 1795 + // four-digit UIDs make a 9 KB argument, which Stalwart refuses with + // `BAD [PARSE] Argument exceeds maximum length of 4096 bytes` and + // other servers answer by closing the connection. The range adds + // no message: the batch is a contiguous slice of the sorted + // snapshot and new mail only ever gets a higher UID, so any other + // UID it spans was expunged or never assigned. + let (first, last) = (window[0], window[window.len() - 1]); + let set = if first == last { + first.to_string() + } else { + format!("{first}:{last}") + }; let fetched = fetched_dates(&command(w, &format!("UID FETCH {set} (UID INTERNALDATE)")).await?)?; // Ignore unsolicited updates outside this batch (including arrivals diff --git a/src/providers/imap/read/search.rs b/src/providers/imap/read/search.rs index ab16eb8..89d9a40 100644 --- a/src/providers/imap/read/search.rs +++ b/src/providers/imap/read/search.rs @@ -18,6 +18,8 @@ const TTL: Duration = Duration::from_secs(300); // visible rather than risk the 32 MiB response ceiling failing the whole search. const CONTENT_LIMIT: u64 = 8 * 1024 * 1024; const CONTENT_BATCH: usize = 64; +// Stalwart refuses any single argument longer than this, before running it. +const SET_BYTES: usize = 4096; #[derive(Clone, Copy, PartialEq)] enum Identity { @@ -318,11 +320,6 @@ async fn step( release(wire, key).await; return Ok(()); } - let set = window - .iter() - .map(u32::to_string) - .collect::>() - .join(","); let field = match identity { Identity::FolderUid => "", Identity::Content => " BODY.PEEK[]", @@ -330,6 +327,11 @@ async fn step( Identity::GmailId => " X-GM-MSGID", Identity::Candidate => " RFC822.SIZE BODY.PEEK[HEADER.FIELDS (MESSAGE-ID)]", }; + // Name exactly the matches, since a range of the inventory would also + // fetch every non-match in it, until they outgrow one argument. Content + // never does: CONTENT_BATCH UIDs fit with room to spare, and its range + // would read the bodies of messages that are not candidates. + let set = fetch_set(window, inventory); let data = command( &mut wire, &format!("UID FETCH {set} (UID INTERNALDATE{field})"), @@ -643,6 +645,34 @@ pub(super) async fn call(p: &Value) -> Result { Ok(result) } +/// Sorted matches as one IMAP set, consecutive runs folded into `first:last`. A set +/// longer than `SET_BYTES` becomes the range of the inventory the matches were +/// searched in: still one command and at most one row per inventory UID, which +/// `fetched` narrows back to the matches. +fn fetch_set(matches: &[u32], inventory: &[u32]) -> String { + let mut set = String::new(); + let mut at = 0; + while at < matches.len() { + let first = matches[at]; + while at + 1 < matches.len() && matches[at + 1] == matches[at] + 1 { + at += 1; + } + if !set.is_empty() { + set.push(','); + } + if matches[at] == first { + set.push_str(&first.to_string()); + } else { + set.push_str(&format!("{first}:{}", matches[at])); + } + at += 1; + } + if set.len() > SET_BYTES { + set = format!("{}:{}", inventory[0], inventory[inventory.len() - 1]); + } + set +} + fn fetched(data: &[u8], window: &[u32], identity: Identity) -> Result> { let mut found = BTreeMap::new(); for row in nodes(data)? { diff --git a/src/providers/imap/read/search/tests.rs b/src/providers/imap/read/search/tests.rs index 6160835..ed366c2 100644 --- a/src/providers/imap/read/search/tests.rs +++ b/src/providers/imap/read/search/tests.rs @@ -2,6 +2,21 @@ use super::*; use std::sync::atomic::{AtomicUsize, Ordering}; mod paging; +/// The UID ranges an IMAP set names, read as a server reads it. +fn ranges(set: &str) -> Vec> { + set.split(',') + .map(|part| { + let (first, last) = part.split_once(':').unwrap_or((part, part)); + first.parse().unwrap()..=last.parse().unwrap() + }) + .collect() +} + +/// The UIDs an IMAP set names. +fn uids(set: &str) -> Vec { + ranges(set).into_iter().flatten().collect() +} + #[derive(Default)] struct Counts { searches: AtomicUsize, @@ -75,8 +90,8 @@ async fn peer(socket: TcpStream, counts: Arc) { } counts.fetches.fetch_add(1, Ordering::SeqCst); assert!(rest.ends_with(" (UID INTERNALDATE)\r\n")); - for uid in rest.split_once(' ').unwrap().0.split(',') { - let day = if uid == "1" { 28 } else { 20 }; + for uid in uids(rest.split_once(' ').unwrap().0) { + let day = if uid == 1 { 28 } else { 20 }; data.push_str(&format!( "* 1 FETCH (UID {uid} INTERNALDATE \"{day}-Sep-2026 12:00:00 +0000\")\r\n" )); @@ -172,6 +187,117 @@ async fn large_account_scan_is_parallel_bounded_and_pages_reuse_snapshot() { server.abort(); } +/// One folder of 5000 messages where only odd UIDs match, served like Stalwart: +/// any argument over 4096 bytes is refused before the command runs. +async fn sparse_peer(socket: TcpStream, fetches: Arc) { + let mut wire: Wire = BufReader::new(Box::new(socket)); + write(&mut wire, b"* OK synthetic server\r\n") + .await + .unwrap(); + while let Ok(bytes) = line(&mut wire).await { + let cmd = String::from_utf8(bytes).unwrap(); + let mut data = String::new(); + if cmd.trim_end().split(' ').any(|arg| arg.len() > 4096) { + data.push_str("O1 BAD [PARSE] Argument exceeds maximum length of 4096 bytes.\r\n"); + if write(&mut wire, data.as_bytes()).await.is_err() { + break; + } + continue; + } + if cmd.starts_with("O1 LOGIN ") { + } else if cmd == "O1 CAPABILITY\r\n" { + data.push_str("* CAPABILITY IMAP4rev1\r\n"); + } else if cmd == "O1 LIST \"\" \"*\"\r\n" { + data.push_str("* LIST () \"/\" f000\r\n"); + } else if cmd == "O1 SELECT \"f000\"\r\n" { + data.push_str("* OK [UIDVALIDITY 1] stable\r\n* OK [UIDNEXT 5001] next\r\n"); + } else if cmd == "O1 UID FETCH 1:5000 (UID)\r\n" { + for uid in 1..=5000 { + data.push_str(&format!("* {uid} FETCH (UID {uid})\r\n")); + } + } else if let Some(rest) = cmd.strip_prefix("O1 UID SEARCH UID ") { + let (first, last) = rest.split_once(' ').unwrap().0.split_once(':').unwrap(); + let (first, last) = (first.parse::().unwrap(), last.parse::().unwrap()); + data.push_str("* SEARCH"); + for uid in (first..=last).filter(|uid| uid % 2 == 1) { + data.push_str(&format!(" {uid}")); + } + data.push_str("\r\n"); + } else if let Some(rest) = cmd.strip_prefix("O1 UID FETCH ") { + let (set, fields) = rest.split_once(' ').unwrap(); + if fields.starts_with("(UID INTERNALDATE") { + fetches.fetch_add(1, Ordering::SeqCst); + } + for uid in uids(set) { + data.push_str(&format!( + "* {uid} FETCH (UID {uid} INTERNALDATE \"20-Sep-2026 12:00:00 +0000\")\r\n" + )); + } + } else { + panic!("unexpected command: {cmd}"); + } + data.push_str("O1 OK done\r\n"); + if write(&mut wire, data.as_bytes()).await.is_err() { + break; + } + } +} + +#[tokio::test] +async fn sparse_match_fetches_fit_a_server_capping_each_argument_at_4096_bytes() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + let fetches = Arc::new(AtomicUsize::new(0)); + let counter = fetches.clone(); + let server = tokio::spawn(async move { + loop { + let (socket, _) = listener.accept().await.unwrap(); + tokio::spawn(sparse_peer(socket, counter.clone())); + } + }); + let mut p = json!({"settings":{"imapHost":"127.0.0.1","imapPort":port, + "username":"synthetic","insecure":true,"testPlaintext":true}, + "credential":"synthetic:sparse","query":"search:ALL","limit":2,"requestToken":"sparse"}); + let mut method = "imap.list"; + let result = loop { + let result = super::super::super::call(method, &p).await.unwrap(); + if result["continuation"].is_null() { + break result; + } + p["continuation"] = result["continuation"].clone(); + method = "imap.listContinue"; + }; + assert_eq!(result["page"]["estimate"], 2500); + assert_eq!(result["page"]["ids"], json!(["4999:f000", "4997:f000"])); + // Still one FETCH per 4096-UID window: the first window's 2048 single UIDs + // make 9.7 KB, so they go as the window's range and the even UIDs are dropped. + assert_eq!(fetches.load(Ordering::SeqCst), 2); + server.abort(); +} + +#[test] +fn a_fetch_names_the_matches_until_they_are_longer_than_one_argument() { + let inventory: Vec = (1..=20_000).collect(); + assert_eq!(fetch_set(&[7], &inventory), "7"); + assert_eq!(fetch_set(&[3, 4, 5, 7, 9, 10], &inventory), "3:5,7,9:10"); + // Even UIDs never fold into ranges. 817 four-digit and two five-digit ones + // make exactly 4096 bytes, which Stalwart accepts; 816 and three make 4097. + let fits: Vec = (1000..) + .step_by(2) + .take(817) + .chain([10000, 10002]) + .collect(); + let over: Vec = (1000..) + .step_by(2) + .take(816) + .chain([10000, 10002, 10004]) + .collect(); + let named = fetch_set(&fits, &inventory); + assert_eq!(named.len(), 4096); + assert_eq!(uids(&named), fits); + assert_eq!(fetch_set(&over, &inventory), "1:20000"); +} + fn metadata(uid: u32, day: u8, body: &str) -> Vec { format!("* 1 FETCH (UID {uid} INTERNALDATE \"{day:02}-Sep-2026 12:00:00 +0000\" BODY[] {{{}}}\r\n{body})\r\n", body.len()).into_bytes() } @@ -276,8 +402,7 @@ async fn generic_peer(socket: TcpStream, selected: Arc().unwrap(); + for uid in uids(set) { let (_, _, day, raw) = GENERIC .iter() .find(|m| m.0 == folder && m.1 == uid) diff --git a/src/providers/imap/read/search/tests/paging.rs b/src/providers/imap/read/search/tests/paging.rs index f286d6c..f32971d 100644 --- a/src/providers/imap/read/search/tests/paging.rs +++ b/src/providers/imap/read/search/tests/paging.rs @@ -124,10 +124,16 @@ async fn peer(socket: TcpStream, state: Arc>) { } } else { assert_eq!(fields, "(UID INTERNALDATE)\r\n"); - let wanted: Vec = set.split(',').map(|s| s.parse().unwrap()).collect(); - assert!(wanted.len() <= BATCH); - for (uid, day) in &state.folders[&folder] { - if wanted.contains(uid) && !state.missing.contains(uid) { + // A range between sparse UIDs spans far more numbers than + // messages; like a server, answer only the messages it names. + let ranges = super::ranges(set); + let named: Vec<_> = state.folders[&folder] + .iter() + .filter(|(uid, _)| ranges.iter().any(|range| range.contains(uid))) + .collect(); + assert!(named.len() <= BATCH); + for (uid, day) in named { + if !state.missing.contains(uid) { data.push_str(&format!("* 1 FETCH (UID {uid} INTERNALDATE \"{day:02}-Sep-2026 12:00:00 +0000\")\r\n")); } } diff --git a/src/providers/imap/read/tests.rs b/src/providers/imap/read/tests.rs index 824124c..c0da3fb 100644 --- a/src/providers/imap/read/tests.rs +++ b/src/providers/imap/read/tests.rs @@ -90,7 +90,7 @@ async fn sparse_search_orders_by_date_before_paging_even_when_progressive() { write(&mut w, b"* 1 FETCH (UID 7)\r\n* 2 FETCH (UID 1000)\r\n* 3 FETCH (UID 50000)\r\nO1 OK snapshot\r\n").await.unwrap(); assert_eq!( line(&mut w).await.unwrap(), - b"O1 UID FETCH 7,1000,50000 (UID INTERNALDATE)\r\n" + b"O1 UID FETCH 7:50000 (UID INTERNALDATE)\r\n" ); write(&mut w,b"* 1 FETCH (UID 7 INTERNALDATE \"23-Sep-2026 12:00:00 +0000\")\r\n* 2 FETCH (UID 1000 INTERNALDATE \"22-Sep-2026 12:00:00 +0000\")\r\n* 3 FETCH (UID 50000 INTERNALDATE \"21-Sep-2026 12:00:00 +0000\")\r\nO1 OK snapshot\r\n").await.unwrap(); assert_eq!( @@ -125,6 +125,68 @@ fn unsolicited_flags_preserve_snapshot_dates() { ); } +#[tokio::test] +async fn date_scan_fits_a_server_capping_each_argument_at_4096_bytes() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + let peer = tokio::spawn(async move { + let (stream, _) = listener.accept().await.unwrap(); + let mut w: Wire = BufReader::new(Box::new(stream)); + greeting(&mut w).await; + // A sparse four-digit inbox: as a comma list its UIDs need ~9 KB. + let inbox: Vec = (4..=2300).filter(|uid| uid % 7 != 0).collect(); + while let Ok(raw) = line(&mut w).await { + let request = String::from_utf8(raw).unwrap(); + let request = request.trim_end(); + // Stalwart refuses any single argument longer than 4096 bytes. + if request.split(' ').any(|arg| arg.len() > 4096) { + write( + &mut w, + b"O1 BAD [PARSE] Argument exceeds maximum length of 4096 bytes.\r\n", + ) + .await + .unwrap(); + continue; + } + let mut response = String::new(); + if request == "O1 SELECT \"INBOX\"" { + response.push_str("O1 OK selected\r\n"); + } else if let Some(fetch) = request.strip_prefix("O1 UID FETCH ") { + let (set, fields) = fetch.split_once(' ').unwrap(); + let wanted = |uid: u32| { + set.split(',').any(|part| match part.split_once(':') { + Some((first, "*")) => uid >= first.parse().unwrap(), + Some((first, last)) => { + (first.parse().unwrap()..=last.parse().unwrap()).contains(&uid) + } + None => uid == part.parse::().unwrap(), + }) + }; + let dated = fields.contains("INTERNALDATE"); + for uid in inbox.iter().copied().filter(|uid| wanted(*uid)) { + response.push_str(&if dated { + format!("* {uid} FETCH (UID {uid} INTERNALDATE \"21-Sep-2026 12:00:00 +0000\")\r\n") + } else { + format!("* {uid} FETCH (UID {uid})\r\n") + }); + } + response.push_str("O1 OK fetched\r\n"); + } else { + panic!("unexpected command: {request}"); + } + write(&mut w, response.as_bytes()).await.unwrap(); + } + }); + let mut p = params(port); + p["query"] = json!("folder:INBOX"); + let page = super::super::call("imap.list", &p).await.unwrap(); + assert_eq!( + page["page"]["ids"], + json!(["2300:INBOX", "2299:INBOX", "2298:INBOX"]) + ); + peer.abort(); +} + #[tokio::test] async fn multi_window_dates_settle_before_paging_and_ignore_unsolicited_flags() { let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); @@ -145,7 +207,8 @@ async fn multi_window_dates_settle_before_paging_and_ignore_unsolicited_flags() let ids: Vec = if set == "1:*" { (1..=4100).collect() } else { - set.split(',').map(|s| s.parse().unwrap()).collect() + let (first, last) = set.split_once(':').unwrap_or((set, set)); + (first.parse().unwrap()..=last.parse().unwrap()).collect() }; let dated = fields.contains("INTERNALDATE"); if dated { @@ -301,7 +364,8 @@ async fn a_slow_server_that_answers_every_command_still_pages() { let ids: Vec = if set == "1:*" { (1..=9000).collect() } else { - set.split(',').map(|id| id.parse().unwrap()).collect() + let (first, last) = set.split_once(':').unwrap_or((set, set)); + (first.parse().unwrap()..=last.parse().unwrap()).collect() }; for uid in &ids { response.push_str(&if dated { diff --git a/tests/cli/search.rs b/tests/cli/search.rs index 76a0e86..d013459 100644 --- a/tests/cli/search.rs +++ b/tests/cli/search.rs @@ -51,7 +51,13 @@ async fn search_pages_survive_process_exit_and_deletion_of_the_cursor_message() data.push_str("\r\n"); } else if let Some(rest) = cmd.strip_prefix("O1 UID FETCH ") { let (set, fields) = rest.split_once(' ').unwrap(); - let wanted: Vec = set.split(',').map(|s| s.parse().unwrap()).collect(); + let wanted: Vec = set + .split(',') + .flat_map(|part| { + let (first, last) = part.split_once(':').unwrap_or((part, part)); + first.parse::().unwrap()..=last.parse().unwrap() + }) + .collect(); if fields == "(UID)\r\n" { for (uid, _) in &rows { if wanted.contains(uid) { From 01de6160dd63bfaf9da72c6b369f17485eb7d815 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 26 Oct 2026 13:36:06 +0800 Subject: [PATCH 51/88] chore: Publish Omamail v0.11.2 (#404) Publish Omamail v0.11.2 from this branch. Release CI builds and verifies the plugin backends and all three standalone desktop packages, creates the tag and one release, then updates backend-version and the released API contract in this PR after verifying every public download. Merge this PR once the pin commit and required checks pass. Main receives the version and runtime pin together in one merge. ## Release Notes - Update the Omarchy backend and standalone desktop applications to v0.11.2. --------- Co-authored-by: Omamail Release <41898282+github-actions[bot]@users.noreply.github.com> --- Cargo.lock | 2 +- Cargo.toml | 2 +- app/CMakeLists.txt | 2 +- backend-version | 2 +- manifest.json | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 153be21..1c1e51d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1320,7 +1320,7 @@ dependencies = [ [[package]] name = "omamail" -version = "0.11.1" +version = "0.11.2" dependencies = [ "base64", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 8b03c81..14aa5e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "omamail" -version = "0.11.1" +version = "0.11.2" edition = "2024" publish = false diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 87e7144..d311782 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.21) -project(omamail-app VERSION 0.11.1 LANGUAGES CXX) +project(omamail-app VERSION 0.11.2 LANGUAGES CXX) if(WIN32) enable_language(RC) diff --git a/backend-version b/backend-version index af88ba8..bc859cb 100644 --- a/backend-version +++ b/backend-version @@ -1 +1 @@ -0.11.1 +0.11.2 diff --git a/manifest.json b/manifest.json index dd32ef8..9d636d5 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "id": "omamail", "name": "Omamail", - "version": "0.11.1", + "version": "0.11.2", "author": "huacnlee", "license": "MIT", "homepage": "https://huacnlee.github.io/omamail/", From 8838ef0369e65f1870c5ba136b29b6d8dac1f458 Mon Sep 17 00:00:00 2001 From: Nico Duldhardt <36307788+klaudworks@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:37:54 +0100 Subject: [PATCH 52/88] Add Google calendar destinations for accepted invitations (#412) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Add an opt-in Google calendar destination for invitations answered through a mailbox without native calendar attendance. This lets someone using IMAP/Proton Bridge for mail keep accepted meetings in their existing Google calendar, rather than replying successfully and then recreating the event by hand. - Settings → Calendars → **Accepted invitations** chooses a writable Google destination per mailbox; the default is off. The destination also appears in the receiving mailbox's calendar view. - Yes/Maybe sends the existing RFC 5546 email reply as the invited mailbox/alias, then imports a private schedule entry. Google sends no second invitation. No removes only this mailbox's managed copy. - Calendar-save status is separate from RSVP. A failed save can be retried without another reply email, including after navigating away and reopening the invitation. - Updated invitations are applied when answered or explicitly saved. Cancellation messages offer **Remove saved event**. Reading a message alone never writes to a calendar. ## Design and boundaries Recommend introducing this opt-in flow: it connects the existing RSVP and calendar capabilities without requiring a second calendar-provider integration. Gmail retains native attendance. The setting and save-status row add a small UI surface; this is invitation handling, not background synchronization of a separate Proton calendar. Rust owns lookup/import/update and validates the destination and event fields. Matching uses UID plus organizer, and updates additionally require the saved mailbox ownership marker. Existing native/manual copies are kept, older sequences cannot overwrite newer ones, patches use ETags, and writes never include guest lists. Import uses Google's private-copy endpoint; subsequent patches set `sendUpdates=none`. A backend-local mutex serializes lookup/import pairs, and retries look up the UID again before creating a copy. Whole recurring series preserve their rule and exclusions. Occurrence updates identify the original start in an already imported series; an occurrence-only invitation reports that the original series must be saved first. Incomplete dates and unresolved zones are refused. A hand-recreated event with a different UID cannot be deduplicated. External clients and Google's consistency can still produce conflicts; writes are not blindly retried. API 9 is an unreleased step with explicit QML feature gates and contract fixtures; the published 0.11.2/API-8 pin stays intact. The standalone bundled minimum advances to API 8. The branch also carries the same authentication-warmup fixture correction as #411 so the existing macOS Graph tests measure their intended operations rather than native trust-store startup. ## Verification - 713 Rust unit tests and integration suites passed locally, including six new invitation boundary/operation tests. - Focused QML coverage exercises RSVP-before-save, save-only retry, navigation during SMTP completion, old-backend and opt-out no-dispatch, cancellation, stale callbacks, and persisted calendar selection. JS tests cover dates, time zones, recurrence exclusions and mailbox calendar visibility. - Full `make validate` passed across the staged runs: Rust/integrations, JS/shell, 1,547 main QML cases plus auxiliary suites, standalone composition, lint and plugin validation. The standalone QML assertion checks the released API-8 handshake floor. Latest focused invitation tests also cover SMTP completion after navigation. - **All 14 hosted checks pass** on `268a174` ([CI run](https://github.com/huacnlee/omamail/actions/runs/40475786378)), including Linux, macOS and Windows app packaging and smoke tests, both released-backend gates, the unreleased API contract and native credential checks. The initial archive smoke tests exposed a C++ handshake hard-coded to API 8; the correction derives the exact required revision from `backend-api.json` at CMake configure time, with reconfiguration on contract changes. Native resource tests verify that both older and future APIs are refused without a readiness file. An actual API-9 backend reproduces the refusal with the previous host and passes with the rebuilt host. - **Security: PASS** for the exercised fixed Google origin, typed import payload, guest-list exclusion, organizer/mailbox ownership, stale-sequence refusal, explicit cancellation and no-second-email retry boundaries. Synthetic data and controlled fixtures only; native Linux/macOS/Windows CI passed. Live Google import behavior remains **NOT VERIFIED**. ## Workflow recording 36-second synthetic Qt workflow capture at 1000×900, using the production `InviteCard.qml` and `Rsvp.qml` with mocked mail/calendar responses. Click highlights show the actual QtTest mouse interactions; the audit panel counts calls made by the production controller. Frames are encoded at 10 fps with capture overhead removed. This is UI-flow evidence, not a live Google integration recording. https://github.com/user-attachments/assets/a2d0be34-a750-4aaa-8568-ab4099ab83f5 - **0:00–0:10 — Successful save:** click Yes → RSVP email → calendar save → saved confirmation. - **0:10–0:26 — Save-only retry:** a second invitation's RSVP succeeds and calendar save fails; click **Save / update calendar**. The calendar request counter increases while RSVP emails stay at 2. - **0:26–0:36 — Cancellation/removal:** opening the cancellation leaves the saved event intact; click **Remove saved event** to remove only that event. RSVP emails remain at 2, and the first saved event remains. The capture harness passes assertions for request counts, saved-event state and the production status messages after each sequence (3 passed, 0 failed, including setup/cleanup). ## Screenshots Matching before/after synthetic Qt component captures use production components and standalone controls with synthetic mailbox/calendar state, Solarized Light, scale 1, base font 14, Qt 6.12.0, at 900×900; they are not live-mail screenshots.
Settings: a per-mailbox destination, off by default | Before | After | | --- | --- | | ![Before settings](https://github.com/user-attachments/assets/b3faed19-a88c-4dde-95b3-2f5cb0ef20c7) | ![After settings](https://github.com/user-attachments/assets/2c2649d4-1cc6-405c-9bf0-ff0af17c8028) |
Accepted: the card confirms the calendar save separately | Before | After | | --- | --- | | ![Before accepted](https://github.com/user-attachments/assets/0a681c61-8856-467d-a22b-4691e052ce3c) | ![After accepted](https://github.com/user-attachments/assets/5b5c9a85-6c21-4e4d-b761-7c1902084401) |
Failure: retry just the calendar save without another email | Before | After | | --- | --- | | ![Before failure](https://github.com/user-attachments/assets/d562f70f-d78c-4941-a6eb-cf6e466ef123) | ![After failure](https://github.com/user-attachments/assets/5be3d22e-e93b-458d-a23f-46eaa6554150) |
Cancellation: explicitly remove the managed copy | Before | After | | --- | --- | | ![Before cancellation](https://github.com/user-attachments/assets/58b06bc6-cb76-4647-82c3-06b03526efab) | ![After cancellation](https://github.com/user-attachments/assets/5706d8e1-cd9d-4ebf-bc9f-b0841e284d71) |
## Release Notes - Choose a Google calendar for accepted invitations from a mail-only mailbox. - See calendar-save status and retry without sending another RSVP email. - Update or remove saved invitations while keeping their organizer and identity linked. --- Makefile | 2 + app/CMakeLists.txt | 7 + app/src/resource_check.cpp | 2 +- app/tests/native/tst_resources.cpp | 20 +- app/tests/qml/tst_standalone_composition.qml | 4 +- backend-api.json | 22 +- docs/INVITATION-CALENDARS.md | 13 + scripts/diagnostics.py | 2 +- src/backend/methods.rs | 1 + src/backend/mod.rs | 14 +- src/calendar/invitations.rs | 412 +++++++++++++++++++ src/calendar/mod.rs | 8 +- src/providers/m365/tests.rs | 13 + src/providers/m365/tests/drafts.rs | 2 + src/providers/m365/tests/wire.rs | 2 + ui/Service.qml | 15 +- ui/account/MailAccount.qml | 2 + ui/account/Rsvp.qml | 79 +++- ui/calendar/CalendarController.qml | 9 +- ui/calendar/Invitations.js | 66 +++ ui/calendar/Sources.js | 5 +- ui/components/CalendarSettings.qml | 63 +++ ui/components/InviteCard.qml | 29 +- ui/components/MessageReader.qml | 4 +- ui/tests/qml/tst_calendar_attendance.qml | 1 + ui/tests/qml/tst_calendar_settings.qml | 23 ++ ui/tests/qml/tst_invitation_calendar.qml | 135 ++++++ ui/tests/test_calendar_invitations.js | 27 ++ 28 files changed, 944 insertions(+), 38 deletions(-) create mode 100644 docs/INVITATION-CALENDARS.md create mode 100644 src/calendar/invitations.rs create mode 100644 ui/calendar/Invitations.js create mode 100644 ui/tests/qml/tst_invitation_calendar.qml create mode 100644 ui/tests/test_calendar_invitations.js diff --git a/Makefile b/Makefile index b3e7e37..bfd34e2 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ QML_FILES := ui/Service.qml ui/BarWidget.qml ui/App.qml ui/compose/RecoveryContr ui/tests/qml/tst_unified_batch.qml ui/tests/qml/tst_clipboard_arguments.qml \ ui/tests/qml/tst_reader_subject_menu.qml ui/tests/qml/tst_drafts_refresh.qml ui/tests/qml/tst_account_hosts.qml \ ui/tests/qml/tst_credential_store_retry.qml \ + ui/tests/qml/tst_invitation_calendar.qml \ ui/backend/Backend.qml ui/backend/Runtime.qml ui/diagnostics/Diagnostics.qml \ ui/components/BackendSetup.qml ui/components/OmamailLogo.qml \ ui/account/MailAccount.qml ui/account/BackendSync.qml ui/account/SendQueue.qml ui/account/DraftList.qml ui/account/Intents.qml ui/account/BatchAction.qml ui/account/UnifiedBatchAction.qml ui/account/UnifiedBatchSelection.qml ui/account/Rsvp.qml ui/account/LabelActions.qml ui/account/Unsubscribe.qml ui/account/NewMailNotification.qml \ @@ -154,6 +155,7 @@ test-js: node ui/tests/test_calendar_cache.js node ui/tests/test_calendar_feed.js node ui/tests/test_calendar_sources.js + node ui/tests/test_calendar_invitations.js node ui/tests/test_calendar_reminders.js node ui/tests/test_calendar_palette.js node ui/tests/test_bar_preview.js diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index d311782..3630fe2 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -2,6 +2,12 @@ cmake_minimum_required(VERSION 3.21) project(omamail-app VERSION 0.11.2 LANGUAGES CXX) +# The smoke-test handshake follows the same contract as the bundled backend. +set(OMAMAIL_API_CONTRACT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../backend-api.json") +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${OMAMAIL_API_CONTRACT_PATH}") +file(READ "${OMAMAIL_API_CONTRACT_PATH}" OMAMAIL_API_CONTRACT) +string(JSON OMAMAIL_BACKEND_API_VERSION GET "${OMAMAIL_API_CONTRACT}" apiVersion) + if(WIN32) enable_language(RC) endif() @@ -67,6 +73,7 @@ endif() target_include_directories(omamail_native PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src") target_compile_definitions(omamail_native PUBLIC OMAMAIL_APP_VERSION="${PROJECT_VERSION}" + OMAMAIL_BACKEND_API_VERSION=${OMAMAIL_BACKEND_API_VERSION} OMAMAIL_SOURCE_ROOT="${CMAKE_CURRENT_SOURCE_DIR}/.." ) if(DEFINED OMAMAIL_BACKEND AND NOT OMAMAIL_BACKEND STREQUAL "") diff --git a/app/src/resource_check.cpp b/app/src/resource_check.cpp index 035e9bd..6697825 100644 --- a/app/src/resource_check.cpp +++ b/app/src/resource_check.cpp @@ -365,7 +365,7 @@ bool runSmokeTest(const ResourcePaths &paths, const QString &readyFile, QString || !protocol.isDouble() || protocol.toInt() != 1 || !apiVersion.isDouble() || apiVersion.toDouble() != static_cast(apiVersion.toInt()) - || apiVersion.toInt() != 8) { + || apiVersion.toInt() != OMAMAIL_BACKEND_API_VERSION) { if (error) *error = QStringLiteral("Bundled backend system.info does not match this app"); stopProcess(backend); return false; diff --git a/app/tests/native/tst_resources.cpp b/app/tests/native/tst_resources.cpp index 546242a..51f1121 100644 --- a/app/tests/native/tst_resources.cpp +++ b/app/tests/native/tst_resources.cpp @@ -164,7 +164,7 @@ void ResourcesTest::smokeTestLoadsQmlHandshakesAndWritesReadyFile() QVERIFY(ready.open(QIODevice::ReadOnly)); const QJsonObject value = QJsonDocument::fromJson(ready.readAll()).object(); QCOMPARE(value.value(QStringLiteral("version")).toString(), QStringLiteral(OMAMAIL_APP_VERSION)); - QCOMPARE(value.value(QStringLiteral("apiVersion")).toInt(), 8); + QCOMPARE(value.value(QStringLiteral("apiVersion")).toInt(), OMAMAIL_BACKEND_API_VERSION); } void ResourcesTest::smokeRejectsMismatchedBackendApi() @@ -172,12 +172,14 @@ void ResourcesTest::smokeRejectsMismatchedBackendApi() QTemporaryDir directory; ResourcePaths paths = completeLayout(directory); paths.backend = QCoreApplication::applicationFilePath(); - qputenv("OMAMAIL_SMOKE_FIXTURE", "stale-api"); - QString error; - QVERIFY(!runSmokeTest(paths, directory.filePath(QStringLiteral("ready.json")), &error)); - qunsetenv("OMAMAIL_SMOKE_FIXTURE"); - QVERIFY(error.contains(QStringLiteral("does not match"))); - QVERIFY(!QFile::exists(directory.filePath(QStringLiteral("ready.json")))); + for (const QByteArray fixture : {QByteArrayLiteral("stale-api"), QByteArrayLiteral("future-api")}) { + qputenv("OMAMAIL_SMOKE_FIXTURE", fixture); + QString error; + QVERIFY(!runSmokeTest(paths, directory.filePath(QStringLiteral("ready.json")), &error)); + qunsetenv("OMAMAIL_SMOKE_FIXTURE"); + QVERIFY(error.contains(QStringLiteral("does not match"))); + QVERIFY(!QFile::exists(directory.filePath(QStringLiteral("ready.json")))); + } } void ResourcesTest::smokeRejectsOversizedStdoutFrame() @@ -344,7 +346,9 @@ int main(int argc, char *argv[]) result = {{QStringLiteral("name"), QStringLiteral("omamail")}, {QStringLiteral("version"), QStringLiteral(OMAMAIL_APP_VERSION)}, {QStringLiteral("protocol"), 1}, - {QStringLiteral("apiVersion"), fixture == QByteArrayLiteral("stale-api") ? 7 : 8}}; + {QStringLiteral("apiVersion"), OMAMAIL_BACKEND_API_VERSION + + (fixture == QByteArrayLiteral("stale-api") ? -1 + : fixture == QByteArrayLiteral("future-api") ? 1 : 0)}}; } else { result = {{QStringLiteral("quitReady"), true}}; } diff --git a/app/tests/qml/tst_standalone_composition.qml b/app/tests/qml/tst_standalone_composition.qml index 172fb59..6b2bf63 100644 --- a/app/tests/qml/tst_standalone_composition.qml +++ b/app/tests/qml/tst_standalone_composition.qml @@ -479,8 +479,8 @@ TestCase { compare(composition.service.settings.maxMessages, 17) compare(composition.service.notifyNewMail, false) compare(composition.service.backendRuntime.bundled, true) - compare(composition.service.backendRuntime.requiredApiVersion, 7, - "the bundled handshake must require this checkout's API") + compare(composition.service.backendRuntime.requiredApiVersion, 8, + "the bundled handshake must accept the released API and its unreleased step") compare(composition.service.backendRuntime.canInstall, false) compare(composition.service.backendRuntime.executable, host.backendPath) tryCompare(composition.app, "backendUnavailable", true) diff --git a/backend-api.json b/backend-api.json index d5be6de..e32ce50 100644 --- a/backend-api.json +++ b/backend-api.json @@ -1,5 +1,5 @@ { - "apiVersion": 8, + "apiVersion": 9, "releasedApiVersion": 8, "protocolVersion": 1, "methods": [ @@ -97,6 +97,7 @@ "calendar.request", "calendar.discover", "calendar.attendance", + "calendar.saveInvitation", "calendar.reminders", "auth.form", "auth.begin", @@ -354,6 +355,21 @@ "notifications.1.key": "string" } }, + { + "name": "Invitation import rejects non-Google destinations before authentication", + "method": "calendar.saveInvitation", + "params": { + "source": {"kind":"caldav", "url":"https://attacker.example/calendar"}, + "mailbox":"imap:owner@example.org", "uid":"meeting", "organizer":"host@example.org", "response":"cancelled", "sequence":0 + }, + "errorCode": -32602 + }, + { + "name": "Google import validates destination before authentication", + "method": "calendar.request", + "params": {"source":{"kind":"google","accountId":"missing@example.org","calendarId":".."},"operation":"import","body":"{}"}, + "errorCode": -32000 + }, { "name": "Google attendance rejects unsupported answers before authentication", "method": "calendar.attendance", @@ -1473,7 +1489,7 @@ } ], "unreleased": { - "methods": [], - "cases": [] + "methods": ["calendar.saveInvitation"], + "cases": ["Invitation import rejects non-Google destinations before authentication", "Google import validates destination before authentication"] } } diff --git a/docs/INVITATION-CALENDARS.md b/docs/INVITATION-CALENDARS.md new file mode 100644 index 0000000..70097af --- /dev/null +++ b/docs/INVITATION-CALENDARS.md @@ -0,0 +1,13 @@ +# Accepted invitations + +Settings → Calendars → Accepted invitations lets each mailbox without native calendar attendance choose a writable Google calendar. The default is “Don't save to a calendar”. The destination also appears in that mailbox's calendar view; its normal visibility setting still applies. + +Yes and Maybe first send the normal RFC 5546 reply from the invited mailbox/alias. After mail success, Omamail imports a private schedule entry into the chosen calendar. It retains the invitation UID and organizer and sends no additional invitations from Google. Tentative replies create tentative entries. No removes only the copy this mailbox previously saved. Gmail continues using its native attendance operation. + +The invitation card reports calendar-save success or failure separately from the reply. “Save / update calendar” retries the calendar operation without sending another email, including after reopening the message. An updated invitation is applied when answered or explicitly saved; a cancellation offers “Remove saved event”. Reading or fetching a message alone never writes to a calendar. This is invitation handling, not background Proton Calendar synchronization. + +The backend matches by destination, UID and organizer and checks its mailbox ownership marker before modifying a copy. A native or manually imported event with the same UID is left alone. Older sequence numbers cannot overwrite a newer saved invitation; patches use the current ETag and `sendUpdates=none`. Concurrent saves within the backend are serialized. A retry looks up the UID again before importing, including after a lost response. A manually recreated event with a different UID cannot be recognized as the same invitation. Google consistency and external calendar clients can still cause conflicts; the operation reports failure rather than automatically repeating writes. + +Whole recurring series preserve their recurrence rule and excluded dates. An occurrence update targets its original start in an already imported series; an occurrence-only invitation cannot create a second series. Unresolved zones or incomplete dates are refused. Google must recognize recurring time-zone identifiers; the original invitation remains available if it does not. + +`calendar.saveInvitation` and Google `calendar.request` import support require API 9. The published API-8 backend still sends RSVP email, while the new destination controls are disabled. The exact published pin remains unchanged until a normal backend release. diff --git a/scripts/diagnostics.py b/scripts/diagnostics.py index ac14ea0..974ab94 100644 --- a/scripts/diagnostics.py +++ b/scripts/diagnostics.py @@ -52,7 +52,7 @@ calendar_ambiguous_invitation calendar_attendance_unconfirmed calendar_attendee_not_found calendar_auth_refused calendar_auth_required calendar_conflict calendar_input_too_large calendar_invalid_input calendar_invalid_operation calendar_invalid_response calendar_invalid_url -calendar_invitation_cancelled calendar_invitation_not_found calendar_keyring_failed +calendar_invitation_cancelled calendar_invitation_not_found calendar_invitation_series_required calendar_keyring_failed calendar_network_failed calendar_not_found calendar_organizer_mismatch calendar_origin_refused calendar_password_invalid calendar_password_missing calendar_provider_unsupported calendar_reminder_not_found calendar_reminders_invalid calendar_reminders_unavailable diff --git a/src/backend/methods.rs b/src/backend/methods.rs index 28ca76a..6f5bf33 100644 --- a/src/backend/methods.rs +++ b/src/backend/methods.rs @@ -94,6 +94,7 @@ pub const ALL: &[&str] = &[ "calendar.request", "calendar.discover", "calendar.attendance", + "calendar.saveInvitation", "calendar.reminders", "auth.form", "auth.begin", diff --git a/src/backend/mod.rs b/src/backend/mod.rs index e405127..7b2316d 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -337,6 +337,18 @@ impl Session { }; return Box::pin(crate::calendar::call(params, token.as_deref())).await; } + if method == "calendar.saveInvitation" { + crate::calendar::invitations::validate(params)?; + let token = self + .gmail + .access_token( + params["source"]["accountId"] + .as_str() + .ok_or("invalid_params")?, + ) + .await?; + return crate::calendar::invitations::google(params, &token).await; + } if method == "calendar.attendance" { crate::calendar::attendance::validate(params)?; let token = self @@ -480,7 +492,7 @@ pub fn dispatch(method: &str, params: &Value) -> Result { match method { "system.info" => Ok(json!({ "name": "omamail", "version": env!("CARGO_PKG_VERSION"), - "protocol": 1, "apiVersion": 8, "methods": methods::available(), + "protocol": 1, "apiVersion": 9, "methods": methods::available(), "capabilities": {"agent": cfg!(all(feature = "agent", unix))} })), "system.quit" => Ok(json!({"quitReady": true})), diff --git a/src/calendar/invitations.rs b/src/calendar/invitations.rs new file mode 100644 index 0000000..cd4fd1f --- /dev/null +++ b/src/calendar/invitations.rs @@ -0,0 +1,412 @@ +//! A private Google copy of an invitation answered through another mailbox. +//! RSVP remains mail; import and silent patches never notify the guests again. +use serde_json::{Value, json}; +use std::future::Future; + +static WRITES: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); + +pub fn validate(p: &Value) -> Result<(), &'static str> { + let fields = p.as_object().ok_or("invalid_params")?; + if fields.keys().any(|k| { + ![ + "source", + "mailbox", + "uid", + "organizer", + "response", + "sequence", + "event", + "originalStart", + ] + .contains(&k.as_str()) + }) { + return Err("invalid_params"); + } + let source = &p["source"]; + if source["kind"] != "google" || source["readOnly"] == true { + return Err("invalid_params"); + } + let account = super::text(source, "accountId")?; + if !account.contains('@') || account.contains(':') || account.chars().any(char::is_whitespace) { + return Err("invalid_params"); + } + super::text(p, "mailbox")?; + super::text(p, "uid")?; + let organizer = super::text(p, "organizer")?; + if !organizer.contains('@') || organizer.chars().any(char::is_whitespace) { + return Err("invalid_params"); + } + if !matches!( + p["response"].as_str(), + Some("accepted" | "tentative" | "declined" | "cancelled") + ) || p["sequence"].as_u64().is_none_or(|n| n > i32::MAX as u64) + { + return Err("invalid_params"); + } + if let Some(start) = p.get("originalStart") { + let start = start.as_str().ok_or("invalid_params")?; + if chrono::DateTime::parse_from_rfc3339(start).is_err() + && chrono::NaiveDate::parse_from_str(start, "%Y-%m-%d").is_err() + { + return Err("invalid_params"); + } + } + if !matches!(p["response"].as_str(), Some("cancelled" | "declined")) { + let event = p["event"].as_object().ok_or("invalid_params")?; + if event.keys().any(|k| { + ![ + "summary", + "description", + "location", + "start", + "end", + "recurrence", + ] + .contains(&k.as_str()) + }) || event.len() > 6 + || p["event"].to_string().len() > 512 * 1024 + { + return Err("invalid_params"); + } + for key in ["summary", "description", "location"] { + if event + .get(key) + .is_some_and(|v| !v.is_string() || v.as_str().unwrap().contains('\0')) + { + return Err("invalid_params"); + } + } + for key in ["start", "end"] { + let time = event + .get(key) + .and_then(Value::as_object) + .ok_or("invalid_params")?; + if time + .keys() + .any(|k| !["date", "dateTime", "timeZone"].contains(&k.as_str())) + { + return Err("invalid_params"); + } + let valid = match (time.get("date"), time.get("dateTime")) { + (Some(d), None) => d + .as_str() + .is_some_and(|s| chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").is_ok()), + (None, Some(d)) => d + .as_str() + .is_some_and(|s| chrono::DateTime::parse_from_rfc3339(s).is_ok()), + _ => false, + }; + if !valid + || time.get("timeZone").is_some_and(|z| { + z.as_str().is_none_or(|s| { + s.is_empty() || s.len() > 256 || s.chars().any(char::is_control) + }) + }) + { + return Err("invalid_params"); + } + } + let instant = |time: &Value| -> Option { + if let Some(date) = time["date"].as_str() { + return chrono::NaiveDate::parse_from_str(date, "%Y-%m-%d") + .ok()? + .and_hms_opt(0, 0, 0) + .map(|v| v.and_utc().timestamp()); + } + chrono::DateTime::parse_from_rfc3339(time["dateTime"].as_str()?) + .ok() + .map(|v| v.timestamp()) + }; + if event["start"].get("date").is_some() != event["end"].get("date").is_some() + || instant(&event["end"]) <= instant(&event["start"]) + { + return Err("invalid_params"); + } + if let Some(rules) = event.get("recurrence") { + let rules = rules.as_array().ok_or("invalid_params")?; + if rules.len() > 128 + || rules.iter().any(|v| { + v.as_str().is_none_or(|s| { + s.len() > 8192 + || s.chars().any(char::is_control) + || !(s.starts_with("RRULE:") + || s.starts_with("EXDATE:") + || s.starts_with("EXDATE;VALUE=DATE:")) + }) + }) + { + return Err("invalid_params"); + } + } + } + super::validate(&json!({"source":source,"operation":"lookup","uid":p["uid"]})) +} + +pub async fn google(p: &Value, token: &str) -> Result { + validate(p)?; + tokio::time::timeout(std::time::Duration::from_secs(50), async { + // Serialize the lookup/import pair, including retries from another reader. + let _guard = WRITES.lock().await; + save(p, |params| async move { + let result = super::call(¶ms, Some(token)).await?; + let body = result["body"].as_str().ok_or("calendar_invalid_response")?; + if body.is_empty() { + return Ok(json!({})); + } + serde_json::from_str(body).map_err(|_| "calendar_invalid_response") + }) + .await + }) + .await + .map_err(|_| "calendar_timeout")? +} + +async fn save(p: &Value, mut request: F) -> Result +where + F: FnMut(Value) -> Fut, + Fut: Future>, +{ + validate(p)?; + let source = &p["source"]; + let found = request(json!({"source":source,"operation":"lookup","uid":p["uid"]})).await?; + if found.get("nextPageToken").is_some() { + return Err("calendar_ambiguous_invitation"); + } + let items = found["items"] + .as_array() + .ok_or("calendar_invalid_response")?; + let matching: Vec<&Value> = items + .iter() + .filter(|e| e["iCalUID"] == p["uid"] && e.get("recurringEventId").is_none()) + .collect(); + if matching.len() > 1 { + return Err("calendar_ambiguous_invitation"); + } + let parent = matching.first().copied(); + if let Some(event) = parent { + check_owner(event, p)?; + // An existing native/manual copy belongs to its calendar, not this importer. + if event["extendedProperties"]["private"]["omamailMailbox"] != p["mailbox"] { + return Ok(json!({"status":"existing"})); + } + if event["extendedProperties"]["private"]["omamailSequence"] + .as_str() + .and_then(|s| s.parse::().ok()) + .unwrap_or(0) + > p["sequence"].as_u64().unwrap() + { + return Ok(json!({"status":"newer"})); + } + } + let mut occurrence = None; + if let Some(original) = p.get("originalStart") { + let parent = parent.ok_or("calendar_invitation_series_required")?; + let instances = request(json!({"source":source,"operation":"instances","eventId":super::text(parent,"id")?,"originalStart":original})).await?; + if instances.get("nextPageToken").is_some() { + return Err("calendar_ambiguous_invitation"); + } + let matches: Vec = instances["items"] + .as_array() + .ok_or("calendar_invalid_response")? + .iter() + .filter(|e| { + let start = &e["originalStartTime"]; + start["date"] == *original + || start["dateTime"] == *original + || start["dateTime"] + .as_str() + .zip(original.as_str()) + .is_some_and(|(a, b)| { + chrono::DateTime::parse_from_rfc3339(a) + .ok() + .zip(chrono::DateTime::parse_from_rfc3339(b).ok()) + .is_some_and(|(a, b)| a == b) + }) + }) + .cloned() + .collect(); + if matches.len() != 1 { + return Err("calendar_invitation_not_found"); + } + check_owner(&matches[0], p)?; + if matches[0]["extendedProperties"]["private"]["omamailSequence"] + .as_str() + .and_then(|s| s.parse::().ok()) + .unwrap_or(0) + > p["sequence"].as_u64().unwrap() + { + return Ok(json!({"status":"newer"})); + } + occurrence = matches.into_iter().next(); + } + let existing = occurrence.as_ref().or(parent); + let remove = matches!(p["response"].as_str(), Some("cancelled" | "declined")); + if remove && existing.is_none() { + return Ok(json!({"status":"absent"})); + } + let mut body = if remove { + json!({"status":"cancelled"}) + } else { + p["event"].clone() + }; + // Never copy attendees: this is a private schedule entry, not a new invitation. + if !remove { + body["status"] = json!(if p["response"] == "tentative" { + "tentative" + } else { + "confirmed" + }); + body["transparency"] = json!("opaque"); + } + body["extendedProperties"] = json!({"private":{"omamailMailbox":p["mailbox"],"omamailSequence":p["sequence"].to_string()}}); + if let Some(event) = existing { + request(json!({"source":source,"operation":"update","eventId":super::text(event,"id")?,"ifMatch":super::text(event,"etag")?,"sendUpdates":"none","body":body.to_string()})).await?; + } else { + body["iCalUID"] = p["uid"].clone(); + body["organizer"] = json!({"email":p["organizer"]}); + request(json!({"source":source,"operation":"import","body":body.to_string()})).await?; + } + Ok(json!({"status":if remove { "removed" } else { "saved" }})) +} + +fn check_owner(event: &Value, p: &Value) -> Result<(), &'static str> { + if event["organizer"]["email"] + .as_str() + .is_none_or(|s| !s.eq_ignore_ascii_case(p["organizer"].as_str().unwrap())) + { + return Err("calendar_organizer_mismatch"); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + fn params() -> Value { + json!({"source":{"kind":"google","accountId":"calendar@example.org","calendarId":"work@example.org"}, + "mailbox":"imap:me@example.org","uid":"meeting","organizer":"host@example.org","response":"accepted","sequence":2, + "event":{"summary":"Quoted \"title\" \\ 日本語","description":"Line one\nLine two","location":"Office", + "start":{"dateTime":"2026-10-27T10:00:00Z"},"end":{"dateTime":"2026-10-27T11:00:00Z"}}}) + } + fn existing() -> Value { + json!({"id":"copy","etag":"\"version\"","iCalUID":"meeting","organizer":{"email":"host@example.org"}, + "extendedProperties":{"private":{"omamailMailbox":"imap:me@example.org","omamailSequence":"1"}}}) + } + async fn run(p: &Value, replies: Vec) -> (Result, Vec) { + let mut replies = replies.into_iter(); + let mut calls = Vec::new(); + let result = save(p, |request| { + calls.push(request); + std::future::ready(Ok(replies.next().expect("unexpected request"))) + }) + .await; + (result, calls) + } + #[tokio::test] + async fn import_is_private_and_retry_updates_the_same_uid_without_inviting_guests() { + let p = params(); + let (result, calls) = run(&p, vec![json!({"items":[]}), existing()]).await; + assert_eq!(result.unwrap()["status"], "saved"); + assert_eq!(calls.len(), 2); + assert_eq!(calls[1]["operation"], "import"); + let body: Value = serde_json::from_str(calls[1]["body"].as_str().unwrap()).unwrap(); + assert_eq!(body["iCalUID"], "meeting"); + assert_eq!(body["organizer"]["email"], "host@example.org"); + assert_eq!(body["description"], "Line one\nLine two"); + assert!(body.get("attendees").is_none()); + let (result, calls) = run(&p, vec![json!({"items":[existing()]}), existing()]).await; + assert_eq!(result.unwrap()["status"], "saved"); + assert_eq!(calls[1]["operation"], "update"); + assert_eq!(calls[1]["eventId"], "copy"); + assert_eq!(calls[1]["ifMatch"], "\"version\""); + assert_eq!(calls[1]["sendUpdates"], "none"); + } + #[tokio::test] + async fn mismatched_organizer_duplicates_manual_copies_and_old_updates_never_write() { + let p = params(); + let mut wrong = existing(); + wrong["organizer"]["email"] = json!("other@example.org"); + let mut manual = existing(); + manual.as_object_mut().unwrap().remove("extendedProperties"); + let mut newer = existing(); + newer["extendedProperties"]["private"]["omamailSequence"] = json!("3"); + for event in [wrong, manual, newer] { + let (_, calls) = run(&p, vec![json!({"items":[event]})]).await; + assert_eq!(calls.len(), 1, "no mutation or import"); + } + let (_, calls) = run(&p, vec![json!({"items":[existing(),existing()]})]).await; + assert_eq!(calls.len(), 1); + let (_, calls) = run(&p, vec![json!({"items":[],"nextPageToken":"more"})]).await; + assert_eq!(calls.len(), 1); + } + #[tokio::test] + async fn invalid_destinations_and_attendee_injection_do_not_make_even_a_lookup() { + for key in ["attendees", "organizer", "id", "conferenceData"] { + let mut p = params(); + p["event"][key] = json!({"email":"attacker@example.org"}); + let (result, calls) = run(&p, vec![]).await; + assert!(result.is_err()); + assert!(calls.is_empty()); + } + for id in ["..", ".", "calendar\r\nHost: attacker", "calendar\0"] { + let mut p = params(); + p["source"]["calendarId"] = json!(id); + let (result, calls) = run(&p, vec![]).await; + assert!(result.is_err()); + assert!(calls.is_empty()); + } + let mut p = params(); + p["source"]["kind"] = json!("caldav"); + let (_, calls) = run(&p, vec![]).await; + assert!(calls.is_empty()); + } + #[tokio::test] + async fn declines_and_cancellations_only_remove_managed_copies() { + for response in ["declined", "cancelled"] { + let mut p = params(); + p["response"] = json!(response); + let (result, calls) = run(&p, vec![json!({"items":[]})]).await; + assert_eq!(result.unwrap()["status"], "absent"); + assert_eq!(calls.len(), 1); + let (result, calls) = run(&p, vec![json!({"items":[existing()]}), json!({})]).await; + assert_eq!(result.unwrap()["status"], "removed"); + let body: Value = serde_json::from_str(calls[1]["body"].as_str().unwrap()).unwrap(); + assert_eq!(body["status"], "cancelled"); + assert_eq!(calls[1]["sendUpdates"], "none"); + } + } + #[tokio::test] + async fn an_occurrence_targets_its_original_start_and_never_imports_a_second_series() { + let mut p = params(); + p["originalStart"] = json!("2026-10-27T10:00:00Z"); + let (result, calls) = run(&p, vec![json!({"items":[]})]).await; + assert_eq!(result, Err("calendar_invitation_series_required")); + assert_eq!(calls.len(), 1); + let mut instance = existing(); + instance["id"] = json!("instance"); + instance["originalStartTime"] = json!({"dateTime":"2026-10-27T11:00:00+01:00"}); + let (result, calls) = run( + &p, + vec![ + json!({"items":[existing()]}), + json!({"items":[instance]}), + json!({}), + ], + ) + .await; + assert!(result.is_ok()); + assert_eq!(calls[1]["operation"], "instances"); + assert_eq!(calls[2]["eventId"], "instance"); + } + #[test] + fn import_transport_is_fixed_origin_post_and_contains_no_notification_parameter() { + let p = json!({"source":params()["source"],"operation":"import","body":"{}"}); + let request = super::super::prepare(&p).unwrap(); + assert_eq!(request.method, reqwest::Method::POST); + assert_eq!( + request.url.as_str(), + "https://www.googleapis.com/calendar/v3/calendars/work@example.org/events/import" + ); + assert!(request.url.query().is_none()); + } +} diff --git a/src/calendar/mod.rs b/src/calendar/mod.rs index 4bf97d0..0870bd1 100644 --- a/src/calendar/mod.rs +++ b/src/calendar/mod.rs @@ -5,6 +5,7 @@ use std::{sync::OnceLock, time::Duration}; pub mod attendance; mod discovery; +pub mod invitations; pub mod reminders; pub use discovery::discover; pub use discovery::google as discover_google; @@ -97,7 +98,7 @@ fn prepare(params: &Value) -> Result { let op = text(params, "operation")?; let method = match op { "list" | "get" | "lookup" | "instances" => Method::GET, - "create" | "move" => Method::POST, + "create" | "move" | "import" => Method::POST, "update" => Method::PATCH, "delete" => Method::DELETE, _ => return Err("calendar_invalid_operation"), @@ -180,6 +181,9 @@ fn prepare(params: &Value) -> Result { .unwrap(); } } + if op == "import" { + request.url.path_segments_mut().unwrap().push("import"); + } if matches!(op, "get" | "update" | "delete" | "instances" | "move") { let id = text(params, "eventId")?; if id == "." || id == ".." { @@ -253,7 +257,7 @@ fn prepare(params: &Value) -> Result { ]); } } - if op == "create" || op == "update" { + if op == "create" || op == "update" || op == "import" { let value: Value = serde_json::from_str(&request.body).map_err(|_| "calendar_invalid_input")?; if !value.is_object() { diff --git a/src/providers/m365/tests.rs b/src/providers/m365/tests.rs index 207143b..8fd0847 100644 --- a/src/providers/m365/tests.rs +++ b/src/providers/m365/tests.rs @@ -697,6 +697,19 @@ impl World { std::fs::write(path.join("accounts.json"), registry.to_string()).unwrap(); } + /// Establish authentication before a fixture times only Graph's response or upload. + /// Loading the native trust store for the login client can itself exceed the + /// deliberately short 200–300 ms operation clock on macOS. + pub(super) async fn warm_token(&self) { + crate::auth::access_token("m365", ACCOUNT, "graph") + .await + .unwrap(); + assert!( + self.fake.graph().is_empty(), + "warming auth must not send mail requests" + ); + } + /// Deadlines for the calls that follow: an operation's and a call's, in milliseconds. pub(super) fn deadlines(&self, operation: u64, call: u64) { *super::graph::SHORT_OPERATION.lock().unwrap() = diff --git a/src/providers/m365/tests/drafts.rs b/src/providers/m365/tests/drafts.rs index f33cbef..7e9103a 100644 --- a/src/providers/m365/tests/drafts.rs +++ b/src/providers/m365/tests/drafts.rs @@ -194,6 +194,7 @@ async fn a_save_with_no_answer_may_have_made_a_draft_and_is_never_repeated() { isolated!(); let world = World::new().await; world.fake.state().add(5, Role::Drafts, "Old draft"); + world.warm_token().await; world.deadlines(300, 2000); let raw = URL_SAFE_NO_PAD.encode(mime()); let posts = |world: &World| { @@ -240,6 +241,7 @@ async fn a_large_draft_is_given_longer_to_upload_and_a_small_one_is_not() { isolated!(); let world = World::new().await; inbox(&world, 1); + world.warm_token().await; world.deadlines(300, 5000); let sized = |size: usize| { let mut message = mime(); diff --git a/src/providers/m365/tests/wire.rs b/src/providers/m365/tests/wire.rs index 93c17d2..330fc60 100644 --- a/src/providers/m365/tests/wire.rs +++ b/src/providers/m365/tests/wire.rs @@ -445,6 +445,7 @@ async fn confirmed_mutations_do_not_wait_for_or_fail_on_an_unused_response_body( isolated!(); let world = World::new().await; inbox(&world, 2); + world.warm_token().await; world.deadlines(200, 3000); for stall in [false, true] { world @@ -473,6 +474,7 @@ async fn an_accepted_send_does_not_become_unknown_when_the_unused_body_stalls() use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; isolated!(); let world = World::new().await; + world.warm_token().await; world.deadlines(200, 3000); for stall in [false, true] { world.fake.hook(move |req| { diff --git a/ui/Service.qml b/ui/Service.qml index a658abe..5a13aa1 100644 --- a/ui/Service.qml +++ b/ui/Service.qml @@ -59,7 +59,7 @@ Item { pluginDir: root.pluginDir bundledExecutable: root.standalone ? String(root.platform.backendPath || "") : "" bundledVersion: root.standalone ? root.version : "" - bundledApiVersion: root.standalone ? 7 : 0 + bundledApiVersion: root.standalone ? 8 : 0 bundledMode: root.standalone developmentExecutable: root.standalone ? "" : (Quickshell.env("OMAMAIL_BIN") || "") onValidated: Qt.callLater(rustBackend.reconcileProcess) @@ -96,14 +96,14 @@ Item { } } - // Overall update status is diagnostic, not a feature requirement: its - // target moves whenever the checkout grows another API revision. + // Features gate on fixed API revisions; overall update status is diagnostic. readonly property bool backendNeedsUpdate: backend.needsUpdate // Event suggestions require API 2 regardless of when that API is released. readonly property bool backendCanSuggestEvents: backend.ready && backend.apiVersion >= 2 readonly property bool backendCanCheckMicrosoftConnection: backend.ready && backend.apiVersion >= 5 readonly property bool backendCanDiscoverCalendars: backend.ready && backend.apiVersion >= 5 readonly property bool backendCanGoogleCalendars: backend.ready && backend.apiVersion >= 6 + readonly property bool backendCanSaveInvitations: backend.ready && backend.apiVersion >= 9 // Older backends send the bare address. readonly property bool backendCanNameSender: backend.ready && backend.apiVersion >= 7 readonly property bool calendarRemindersEnabled: !settings || settings.calendarRemindersEnabled !== false @@ -1989,6 +1989,9 @@ Item { readonly property bool canRespondToInvite: !!reading && reading.canRespondToInvite readonly property bool rsvpSending: !!reading && reading.rsvpSending readonly property bool rsvpFallbackAvailable: !!reading && reading.rsvpFallbackAvailable === true + readonly property var invitationCalendarState: reading ? reading.invitationCalendarState : null + signal invitationCalendarSaved() + function saveInvitationCalendar() { if (reading) reading.saveInvitationCalendar() } readonly property string rsvpCalendarUrl: reading ? Provider.calendarAttendanceUrl(reading.providerId, reading.accountId) : "" // Empty when this message offers no way off a list, which is the answer for // everything that is not a newsletter. @@ -2634,11 +2637,7 @@ Item { onTriggered: root.refreshCalendarPreview() } - // The model is a COUNT, not the array. An Instantiator rebuilds every - // delegate when its model changes identity, and this list is reassigned - // whole on every save — so modelling the array tore down all the accounts - // whenever one of them learned its own address, dropping their loaded state - // and landing in-flight callbacks on half-destroyed objects. + // Count-based delegates survive account metadata saves and in-flight callbacks. Instantiator { id: accountHosts model: root.accountCount diff --git a/ui/account/MailAccount.qml b/ui/account/MailAccount.qml index b4a158f..490ad19 100644 --- a/ui/account/MailAccount.qml +++ b/ui/account/MailAccount.qml @@ -2414,6 +2414,8 @@ Item { // See `Rsvp.qml`: the account file is at its size ceiling. function rsvp(response) { rsvpAction.run(response) } readonly property bool rsvpFallbackAvailable: rsvpAction.fallbackAvailable + readonly property var invitationCalendarState: rsvpAction.calendarState + function saveInvitationCalendar() { rsvpAction.retryCalendar() } function rsvpMailOnly(response) { rsvpAction.run(response, true) } readonly property alias bodies: bodyCache diff --git a/ui/account/Rsvp.qml b/ui/account/Rsvp.qml index a4e7238..94a7316 100644 --- a/ui/account/Rsvp.qml +++ b/ui/account/Rsvp.qml @@ -3,6 +3,8 @@ import "../message/Message.js" as Mail import "../message/Calendar.js" as Calendar import "../providers/Registry.js" as Provider import "../calendar/Calendar.js" as Events +import "../calendar/Invitations.js" as Invitations +import "../backend/Plain.js" as Plain // Native attendance is authoritative where the provider supports it. Other // providers send an RFC 5546 reply through their mail transport. @@ -16,6 +18,22 @@ QtObject { property bool reconciling: false property int attendanceRevision: 0 property string fallbackMessageId: "" + property bool calendarSaving: false + property bool calendarDone: false + property string calendarMessage: "" + readonly property var destination: Provider.calendarAttendanceMethod(account.providerId) === "" + ? Invitations.destination(account.platform ? account.platform.settings : null, account.accountId) : null + readonly property bool calendarSupported: !!account.backend && account.backend.ready && account.backend.apiVersion >= 9 + onDestinationChanged: { calendarDone = false; calendarMessage = "" } + readonly property var calendarState: ({ + visible: !!destination && !!account.selectedInvite, + busy: calendarSaving, + text: calendarMessage || (calendarSupported ? "Accepted invitations → " + String(destination && destination.name || "Google Calendar") + : "Update the backend to save accepted invitations"), + canSave: !!destination && calendarSupported && !calendarSaving && !calendarDone && !account.rsvpSending && !!account.selectedInvite + && (Calendar.normalizedResponse(Calendar.responseOf(account.selectedInvite, account.receivedAsAddress)) !== "" + || account.selectedInvite.method === "CANCEL" || account.selectedInvite.status === "CANCELLED") + }) readonly property bool fallbackAvailable: (fallbackMessageId !== "" && fallbackMessageId === account.selectedId) || (!!account.selectedInvite && account.selectedId !== "" && Provider.calendarAttendanceMethod(account.providerId) !== "" @@ -23,8 +41,8 @@ QtObject { && account.backend.apiVersion > 0 && account.backend.apiVersion < 6) property Connections selection: Connections { target: rsvpAction.account - function onSelectedIdChanged() { rsvpAction.reconciledKey = ""; rsvpAction.attendanceRevision++; rsvpAction.fallbackMessageId = "" } - function onSelectedInviteChanged() { Qt.callLater(rsvpAction.reconcile) } + function onSelectedIdChanged() { rsvpAction.reconciledKey = ""; rsvpAction.attendanceRevision++; rsvpAction.fallbackMessageId = ""; rsvpAction.calendarMessage = ""; rsvpAction.calendarDone = false } + function onSelectedInviteChanged() { rsvpAction.calendarDone = false; Qt.callLater(rsvpAction.reconcile) } } property Timer reconcileTimer: Timer { interval: 60000 @@ -99,7 +117,7 @@ QtObject { // emits `replySent`, which closes it. This finishes with a card that has // changed its mind. function run(response, mailOnly) { - if (!account.ready || account.rsvpSending || !account.canRespondToInvite) return + if (!account.ready || account.rsvpSending || calendarSaving || !account.canRespondToInvite) return var answer = String(response || "") var method = Provider.calendarAttendanceMethod(account.providerId) if (mailOnly === true && !fallbackAvailable) return @@ -122,6 +140,9 @@ QtObject { // reader has moved on does not mark a different message answered. var messageId = account.selectedId var invited = account.selectedInvite + var identity = account.accountId + var target = destination ? Plain.plain(destination) : null + var answeredBody = inviteBody(Calendar.withResponse(invited, answeringAs, answer)) var summary = account.selectedMessage var payload = Mail.buildSendPayload({ // The ATTENDEE line claims this address; the envelope has to agree, or a @@ -148,6 +169,7 @@ QtObject { return } account.rsvpSending = true + calendarDone = false account.clearNotice() account.api.sendMessage(payload, function(sent, error) { @@ -158,8 +180,47 @@ QtObject { } account.note(mailOnly === true ? "Reply email sent; Calendar attendance is not confirmed" : "Answer sent to " + fields.to) if (mailOnly === true) return - if (account.selectedId !== messageId) return - rememberResponse(messageId, invited, answeringAs, answer) + if (account.accountId !== identity) return + account.bodies.put(messageId, answeredBody) + if (account.selectedId === messageId) account.selectedInvite = answeredBody.invite + if (target) saveToCalendar(target, identity, messageId, invited, Calendar.normalizedResponse(answer)) + }) + } + + function retryCalendar() { + if (!calendarState.canSave) return + var invited = account.selectedInvite + var response = invited.method === "CANCEL" || invited.status === "CANCELLED" ? "cancelled" + : Calendar.normalizedResponse(Calendar.responseOf(invited, account.receivedAsAddress)) + saveToCalendar(Plain.plain(destination), account.accountId, account.selectedId, invited, response) + } + + function saveToCalendar(target, identity, messageId, invited, response) { + if (!calendarSupported || calendarSaving) { + if (account.selectedId === messageId) calendarMessage = "Reply sent; update the backend to save to calendar" + return + } + var params = Invitations.params(target, identity, invited, response) + if (!params) { + if (account.selectedId === messageId) calendarMessage = "Not saved: the invitation has incomplete dates or an unresolved time zone" + return + } + calendarSaving = true + if (account.selectedId === messageId) calendarMessage = "Saving to calendar…" + account.backend.call("calendar.saveInvitation", params, function(result, error) { + rsvpAction.calendarSaving = false + if (!error && result && account.platform && typeof account.platform.invitationCalendarSaved === "function") + account.platform.invitationCalendarSaved() + if (identity !== account.accountId || messageId !== account.selectedId + || !rsvpAction.destination || rsvpAction.destination.id !== target.id + || !account.selectedInvite || account.selectedInvite.uid !== invited.uid + || Number(account.selectedInvite.sequence || 0) !== Number(invited.sequence || 0)) return + rsvpAction.calendarDone = !error && !!result + rsvpAction.calendarMessage = error || !result + ? "Not saved to calendar. Check calendar access, then retry; your reply will not be resent." + : Invitations.outcome(result.status) + if (error && error.message === "calendar_invitation_series_required") + rsvpAction.calendarMessage = "Save the original series invitation before updating this occurrence" }) } @@ -214,7 +275,11 @@ QtObject { function cacheInvite(messageId, updated) { account.selectedInvite = updated - account.bodies.put(messageId, ({ + account.bodies.put(messageId, inviteBody(updated)) + } + + function inviteBody(updated) { + return { text: account.selectedBody.text, source: account.selectedBody.source, html: "", @@ -222,6 +287,6 @@ QtObject { images: account.selectedImages, invite: updated, unsubscribe: account.selectedUnsubscribe - })) + } } } diff --git a/ui/calendar/CalendarController.qml b/ui/calendar/CalendarController.qml index 297cd80..4352920 100644 --- a/ui/calendar/CalendarController.qml +++ b/ui/calendar/CalendarController.qml @@ -4,6 +4,7 @@ import Quickshell.Io import qs.Commons import "Calendar.js" as Calendar import "Sources.js" as Sources +import "Invitations.js" as Invitations import "../providers/MicrosoftOAuth.js" as Microsoft import "../backend/Plain.js" as Plain @@ -81,7 +82,8 @@ Item { readonly property bool unifiedCalendarView: !!service && service.unifiedCalendarView === true readonly property var contextSources: unifiedCalendarView - ? availableSources : Sources.forAccount(availableSources, accountId) + ? availableSources : Sources.forAccount(availableSources, accountId, + (Invitations.destination(service ? service.settings : null, accountId) || {}).id) // One name for every mailbox, because the unified view is one view. // // A controller with no account is already showing every source — the bar @@ -120,6 +122,11 @@ Item { return root.sourceIncluded(source) }).map(function(source) { return String(source.id || "") }).join("\n") onEnabledSourceKeyChanged: reloadVisibleRange() + Connections { + target: root.service + ignoreUnknownSignals: true + function onInvitationCalendarSaved() { root.reloadVisibleRange() } + } // No `eventCache.loaded` guard, and it is not missing. `refresh` refuses on // an unloaded cache and `eventCache.onRestored` runs one as soon as it is diff --git a/ui/calendar/Invitations.js b/ui/calendar/Invitations.js new file mode 100644 index 0000000..510a09d --- /dev/null +++ b/ui/calendar/Invitations.js @@ -0,0 +1,66 @@ +.pragma library + +.import "../message/Calendar.js" as Calendar + +function destination(settings, mailbox) { + var targets = settings && settings.invitationCalendars || {} + var source = targets[mailbox] + return source && source.kind === "google" && source.accountId && source.readOnly !== true ? source : null +} + +function params(source, mailbox, invite, response) { + if (!source || !invite || !invite.uid) return null + var organizer = Calendar.replyRecipient(invite) + if (!organizer) return null + var result = { source: source, mailbox: mailbox, uid: invite.uid, organizer: organizer, + sequence: Number(invite.sequence) || 0, response: response } + if (invite.recurrenceIdMs) { + result.originalStart = new Date(invite.recurrenceIdMs).toISOString() + var day = /VALUE=DATE:(\d{4})(\d{2})(\d{2})$/.exec(String(invite.source && invite.source.recurrenceId || "")) + if (day) result.originalStart = day[1] + "-" + day[2] + "-" + day[3] + } + if (response === "cancelled" || response === "declined") return result + if (!invite.start || !invite.end || invite.start.resolved === false || invite.end.resolved === false + || !(invite.end.ms > invite.start.ms)) return null + function time(value, raw) { + if (value.allDay) { + var date = /:(\d{4})(\d{2})(\d{2})$/.exec(String(raw || "")) + // DATE values are local calendar dates, never UTC conversions. + var d = new Date(value.ms) + return {date: date ? date[1] + "-" + date[2] + "-" + date[3] + : d.getFullYear() + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2)} + } + var result = {dateTime: new Date(value.ms).toISOString()} + if (value.tzid) result.timeZone = value.tzid + return result + } + var original = invite.source || {} + result.event = {summary: String(invite.summary || ""), description: String(invite.description || ""), + location: String(invite.location || ""), start: time(invite.start, original.dtstart), end: time(invite.end, original.dtend)} + if (invite.meetLink && result.event.description.indexOf(invite.meetLink) < 0) + result.event.description += "\n" + invite.meetLink + if (invite.recurrenceRule && !invite.recurrenceIdMs) { + // Google requires an explicit recurrence zone. UTC events keep UTC; local + // floating recurrences need the organiser's zone before they can be saved. + if (!invite.start.allDay && !result.event.start.timeZone) { + if (!/Z$/.test(String(original.dtstart || ""))) return null + result.event.start.timeZone = "UTC" + result.event.end.timeZone = "UTC" + } + result.event.recurrence = ["RRULE:" + invite.recurrenceRule] + var excluded = invite.excludedMs || [] + for (var i = 0; i < excluded.length; i++) { + var excludedTime = time({ms:excluded[i],allDay:invite.start.allDay}, "") + result.event.recurrence.push(invite.start.allDay ? "EXDATE;VALUE=DATE:" + excludedTime.date.replace(/-/g, "") + : "EXDATE:" + excludedTime.dateTime.replace(/[-:]/g, "").replace(/\.\d{3}Z$/, "Z")) + } + } + return result +} + +function outcome(status) { + if (status === "existing") return "Already in this calendar; existing event kept" + if (status === "newer") return "A newer invitation is already saved" + if (status === "removed" || status === "absent") return "Removed from the saved calendar" + return "Saved to calendar" +} diff --git a/ui/calendar/Sources.js b/ui/calendar/Sources.js index ff2848f..09604f3 100644 --- a/ui/calendar/Sources.js +++ b/ui/calendar/Sources.js @@ -351,7 +351,7 @@ function applyDiscovery(list, result) { return next } -function forAccount(list, accountId) { +function forAccount(list, accountId, invitationCalendarId) { var source = list || emptyList() var wanted = trimmed(accountId) if (wanted === "") return copyList(source) @@ -359,7 +359,8 @@ function forAccount(list, accountId) { var values = Array.isArray(source.sources) ? source.sources : [] for (var i = 0; i < values.length; i++) { if (!values[i]) continue - if (!comesWithAccount(values[i]) || trimmed(values[i].accountId) === wanted) + if (!comesWithAccount(values[i]) || trimmed(values[i].accountId) === wanted + || (invitationCalendarId && values[i].id === invitationCalendarId)) next = add(next, values[i]) } return next diff --git a/ui/components/CalendarSettings.qml b/ui/components/CalendarSettings.qml index 54c16ac..ba1193e 100644 --- a/ui/components/CalendarSettings.qml +++ b/ui/components/CalendarSettings.qml @@ -2,6 +2,9 @@ import QtQuick import qs.Commons import qs.Ui import "../calendar/Sources.js" as Sources +import "../calendar/Invitations.js" as Invitations +import "../providers/Registry.js" as Provider +import "../backend/Plain.js" as Plain Column { id: root @@ -122,6 +125,9 @@ Column { readonly property var reminderSources: settingsSources.filter(function(source) { return Sources.nativeCalendarFeatures(source) && !root.orphaned(source) }) + readonly property var invitationSources: settingsSources.filter(function(source) { + return source.kind === "google" && !source.readOnly && !root.orphaned(source) + }) function reminderMode(source) { return source.remindersEnabled !== true ? "off" @@ -809,6 +815,63 @@ Column { } } + Column { + objectName: "invitation-calendar-settings" + width: parent.width + visible: invitationMailboxes.count > 0 + spacing: Style.space(6) + SectionHeading { text: "ACCEPTED INVITATIONS" } + Text { + width: parent.width + text: "Save Yes and Maybe replies from a mail-only mailbox to Google Calendar. Replies still use the invited address." + textFormat: Text.PlainText + wrapMode: Text.WordWrap + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + } + Repeater { + id: invitationMailboxes + model: root.service ? root.service.accountSummaries.filter(function(account) { + return Provider.calendarAttendanceMethod(account.provider) === "" + }) : [] + SettingRow { + id: invitationMailbox + required property var modelData + readonly property var target: Invitations.destination(root.service.settings, String(modelData.id)) + title: String(modelData.email || modelData.label || modelData.id) + detail: root.service.backendCanSaveInvitations !== true ? "Update the backend to choose a calendar" + : root.invitationSources.length ? "Save accepted invitations to" : "Connect a writable Google calendar above" + Dropdown { + objectName: "invitation-calendar-picker" + width: Style.space(240) + showLabel: false + foreground: root.textColor + accent: root.accentColor + fontFamily: root.panelFontFamily + enabled: root.service.backendCanSaveInvitations === true + value: invitationMailbox.target ? invitationMailbox.target.id : "" + options: { + var sources = root.invitationSources + var choices = [{value:"",label:"Don't save to a calendar"}] + for (var i = 0; i < sources.length; i++) choices.push({value:sources[i].id, + label:root.displayName(sources[i]) + " · " + sources[i].accountId}) + if (invitationMailbox.target && !sources.some(function(source) { return source.id === invitationMailbox.target.id })) + choices.push({value:invitationMailbox.target.id,label:"Unavailable calendar — choose another"}) + return choices + } + onChanged: function(next) { + var targets = Plain.plain(root.service.settings && root.service.settings.invitationCalendars || {}) + var source = root.invitationSources.filter(function(source) { return source.id === next })[0] + if (source) targets[String(invitationMailbox.modelData.id)] = Plain.plain(source) + else delete targets[String(invitationMailbox.modelData.id)] + root.service.persistSetting("invitationCalendars", targets) + } + } + } + } + } + // --------------------------------------------------------------- reminders Column { diff --git a/ui/components/InviteCard.qml b/ui/components/InviteCard.qml index 3ad67fa..2d7aec3 100644 --- a/ui/components/InviteCard.qml +++ b/ui/components/InviteCard.qml @@ -28,6 +28,8 @@ Rectangle { property bool sending: false property bool fallbackAvailable: false property string calendarUrl: "" + property var calendarState: null + signal saveCalendarRequested() required property color textColor required property color accentColor @@ -323,12 +325,37 @@ Rectangle { } } + Column { + width: parent.width + visible: !!root.calendarState && root.calendarState.visible === true + spacing: Style.space(6) + Text { + objectName: "invitation-calendar-status" + width: parent.width + text: root.calendarState ? String(root.calendarState.text || "") : "" + textFormat: Text.PlainText + wrapMode: Text.WordWrap + color: root.dimColor + font.family: root.panelFontFamily + font.pixelSize: Style.font.caption + } + IconTextButton { + objectName: "invitation-calendar-save" + visible: !!root.calendarState && root.calendarState.canSave === true + text: root.cancelled || root.response === "declined" ? "Remove saved event" : "Save / update calendar" + foreground: root.textColor + accent: root.accentColor + fontFamily: root.panelFontFamily + onClicked: root.saveCalendarRequested() + } + } + // What was answered, where there is no answering to be done — somebody // else's reply, a cancelled meeting, an invitation this account is the // organiser of. Text { width: parent.width - visible: !root.canRespond && root.response !== "" + visible: !root.cancelled && !root.canRespond && root.response !== "" textFormat: Text.PlainText text: root.response === "accepted" ? "You are going" : (root.response === "declined" ? "You are not going" : "You might be going") diff --git a/ui/components/MessageReader.qml b/ui/components/MessageReader.qml index 244ea71..47cbd79 100644 --- a/ui/components/MessageReader.qml +++ b/ui/components/MessageReader.qml @@ -652,8 +652,10 @@ Item { invite: root.service ? root.service.selectedInvite : null response: root.service ? root.service.selectedResponse : "" canRespond: !!root.service && root.service.canRespondToInvite - sending: !!root.service && root.service.rsvpSending + sending: !!root.service && (root.service.rsvpSending || !!root.service.invitationCalendarState && root.service.invitationCalendarState.busy) fallbackAvailable: !!root.service && root.service.rsvpFallbackAvailable === true + calendarState: root.service ? root.service.invitationCalendarState || null : null + onSaveCalendarRequested: if (root.service) root.service.saveInvitationCalendar() calendarUrl: String(root.service && root.service.rsvpCalendarUrl || "") textColor: root.textColor accentColor: root.accentColor diff --git a/ui/tests/qml/tst_calendar_attendance.qml b/ui/tests/qml/tst_calendar_attendance.qml index eb14f9f..8db6eb9 100644 --- a/ui/tests/qml/tst_calendar_attendance.qml +++ b/ui/tests/qml/tst_calendar_attendance.qml @@ -18,6 +18,7 @@ TestCase { QtObject { id: owner property var backend: null + property var platform: null property bool ready: true property bool rsvpSending: false property bool canRespondToInvite: true diff --git a/ui/tests/qml/tst_calendar_settings.qml b/ui/tests/qml/tst_calendar_settings.qml index 950e31d..2ccebc6 100644 --- a/ui/tests/qml/tst_calendar_settings.qml +++ b/ui/tests/qml/tst_calendar_settings.qml @@ -19,6 +19,9 @@ Item { property var accountSummaries: [] property bool backendCanDiscoverCalendars: true property bool backendCanGoogleCalendars: true + property bool backendCanSaveInvitations: true + property var settings: ({}) + function persistSetting(name, value) { var next = {}; next[name] = value; mailService.settings = next } property bool calendarRemindersEnabled: true property int calendarSnoozeMinutes: 5 property string calendarReminderError: "" @@ -123,6 +126,26 @@ Item { compare(toggle.checked, false) } + function test_invitation_destination_is_opt_in_persisted_and_backend_gated() { + mailService.settings = {} + mailService.accountSummaries = [{id:"imap:me@example.org",email:"me@example.org",provider:"imap"}, + {id:"calendar@example.org",email:"calendar@example.org",provider:"gmail",signedIn:true}] + calendarController.sourceList = {version:1,sources:[{id:"google:work",kind:"google",name:"Work",accountId:"calendar@example.org",readOnly:false}]} + wait(1) + var picker = findChild(settings,"invitation-calendar-picker") + verify(picker !== null) + compare(picker.value,"") + verify(picker.enabled) + picker.changed("google:work") + compare(mailService.settings.invitationCalendars["imap:me@example.org"].id,"google:work") + compare(picker.value,"google:work") + mailService.backendCanSaveInvitations = false + verify(!picker.enabled) + mailService.backendCanSaveInvitations = true + picker.changed("") + compare(picker.value,"") + } + function test_existing_icloud_account_is_discovered_and_selected_in_calendars() { mailService.accountSummaries = [{ id: "imap:person@icloud.com", email: "person@icloud.com", provider: "imap", calendarProvider: "icloud", diff --git a/ui/tests/qml/tst_invitation_calendar.qml b/ui/tests/qml/tst_invitation_calendar.qml new file mode 100644 index 0000000..f040249 --- /dev/null +++ b/ui/tests/qml/tst_invitation_calendar.qml @@ -0,0 +1,135 @@ +import QtQuick +import QtTest +import "../../account" +import "../../message/Calendar.js" as Calendar + +TestCase { + name: "InvitationCalendar" + QtObject { + id: platform + property var settings: ({}) + } + QtObject { + id: backend + property bool ready: true + property int apiVersion: 9 + property var calls: [] + property var callback: null + function call(method, params, done) { calls = calls.concat([{method:method,params:params}]); callback = done } + } + QtObject { + id: owner + property var backend: null + property var platform: null + property bool ready: true + property bool rsvpSending: false + property bool canRespondToInvite: true + property string providerId: "imap" + property string accountId: "imap:me@example.org" + property string selectedId: "1:INBOX" + property string receivedAsAddress: "alias@example.org" + property string receivedAsName: "Synthetic guest" + property string ownAddress: "me@example.org" + property var selectedMessage: ({messageId:"message",threadId:"thread"}) + property var selectedInvite: null + property var selectedBody: ({text:"Invite",source:"plain"}) + property var selectedAttachments: [] + property var selectedImages: [] + property var selectedUnsubscribe: null + property var cached: null + property var bodies: ({put:function(id, value) { owner.cached = {id:id,body:value} }}) + property var sent: [] + property var sendError: null + property bool deferSend: false + property var sendCallback: null + property var api: ({sendMessage:function(payload, done) { owner.sent = owner.sent.concat([payload]); if (owner.deferSend) owner.sendCallback = done; else done({},owner.sendError) }}) + function clearNotice() {} + function note(text) {} + function fail(text) {} + } + Rsvp { id: action; account: owner } + function init() { + owner.backend = backend + owner.platform = platform + platform.settings = {invitationCalendars:{"imap:me@example.org":{id:"google:work",name:"Work",kind:"google",accountId:"calendar@example.org",calendarId:"work"}}} + backend.calls = []; backend.callback = null; backend.apiVersion = 9 + action.calendarSaving = false; action.calendarMessage = ""; action.calendarDone = false + owner.sent = []; owner.sendError = null; owner.selectedId = "1:INBOX"; owner.rsvpSending = false + owner.cached = null; owner.deferSend = false; owner.sendCallback = null + owner.selectedInvite = {uid:"meeting",organizer:{email:"host@example.org"},summary:"Synthetic meeting",sequence:1, + attendees:[{email:"alias@example.org",partstat:"NEEDS-ACTION"}], + start:{ms:Date.UTC(2026,9,27,10),resolved:true},end:{ms:Date.UTC(2026,9,27,11),resolved:true}} + } + function test_yes_replies_as_invited_alias_then_imports_to_selected_calendar() { + action.run("accepted") + compare(owner.sent.length,1) + compare(backend.calls.length,1) + compare(backend.calls[0].method,"calendar.saveInvitation") + compare(backend.calls[0].params.source.accountId,"calendar@example.org") + compare(backend.calls[0].params.mailbox,"imap:me@example.org") + compare(backend.calls[0].params.response,"accepted") + compare(Calendar.responseOf(owner.selectedInvite,"alias@example.org"),"accepted") + backend.callback({status:"saved"},null) + compare(action.calendarMessage,"Saved to calendar") + verify(!action.calendarState.canSave) + } + function test_failed_save_can_retry_without_another_email() { + action.run("tentative") + backend.callback(null,{message:"calendar_network_failed"}) + verify(action.calendarState.canSave) + verify(action.calendarMessage.indexOf("Not saved") === 0) + action.retryCalendar() + compare(owner.sent.length,1) + compare(backend.calls.length,2) + compare(backend.calls[1].params.response,"tentative") + } + function test_failed_reply_does_not_save_or_claim_attendance() { + owner.sendError = "smtp_failed" + action.run("accepted") + compare(backend.calls.length,0) + compare(Calendar.responseOf(owner.selectedInvite,"alias@example.org"),"") + } + function test_opt_out_and_old_backend_never_import() { + platform.settings = {} + action.run("accepted") + compare(backend.calls.length,0) + init() + backend.apiVersion = 8 + action.run("accepted") + compare(owner.sent.length,1) + compare(backend.calls.length,0) + verify(!action.calendarState.canSave) + } + function test_cancellation_removes_only_on_explicit_action_and_sends_no_reply() { + owner.selectedInvite = {uid:"meeting",method:"CANCEL",sequence:2,organizer:{email:"host@example.org"}} + wait(1) + compare(backend.calls.length,0) + action.retryCalendar() + compare(backend.calls.length,1) + compare(backend.calls[0].params.response,"cancelled") + compare(owner.sent.length,0) + } + function test_late_save_result_does_not_label_a_different_message() { + action.run("accepted") + owner.selectedId = "2:INBOX" + backend.callback({status:"saved"},null) + compare(action.calendarMessage,"") + } + function test_reply_finishing_after_navigation_keeps_answer_for_calendar_only_retry() { + owner.deferSend = true + action.run("accepted") + owner.selectedId = "2:INBOX" + owner.selectedInvite = {uid:"other",organizer:{email:"other@example.org"}} + owner.sendCallback({},null) + compare(owner.selectedInvite.uid,"other") + compare(owner.cached.id,"1:INBOX") + compare(Calendar.responseOf(owner.cached.body.invite,"alias@example.org"),"accepted") + compare(backend.calls[0].params.uid,"meeting") + backend.callback(null,{message:"calendar_network_failed"}) + owner.selectedId = "1:INBOX" + owner.selectedInvite = owner.cached.body.invite + action.retryCalendar() + compare(owner.sent.length,1) + compare(backend.calls.length,2) + } +} diff --git a/ui/tests/test_calendar_invitations.js b/ui/tests/test_calendar_invitations.js new file mode 100644 index 0000000..07d51c9 --- /dev/null +++ b/ui/tests/test_calendar_invitations.js @@ -0,0 +1,27 @@ +const assert = require("assert") +const { load } = require("./load") +const invitations = load("calendar/Invitations.js") +const source = {kind:"google",accountId:"calendar@example.org",id:"google:work",name:"Work"} +const invite = {uid:"test",organizer:{email:"host@example.org"},sequence:2, + start:{ms:Date.UTC(2026,9,27,10),resolved:true},end:{ms:Date.UTC(2026,9,27,11),resolved:true}, + source:{dtstart:"DTSTART:20261027T100000Z"},recurrenceRule:"FREQ=WEEKLY;COUNT=4", + excludedMs:[Date.UTC(2026,10,3,10)],summary:"Weekly ",meetLink:"https://meet.example.org/join"} +const p = invitations.params(source,"imap:me@example.org",invite,"accepted") +assert.equal(p.event.start.timeZone,"UTC") +assert.equal(p.event.recurrence[1],"EXDATE:20261103T100000Z") +assert.ok(p.event.description.includes(invite.meetLink)) +assert.equal(p.event.attendees,undefined) +assert.equal(invitations.params(source,"mailbox",{...invite,start:{...invite.start,resolved:false}},"accepted"),null) +const day = {...invite,recurrenceRule:"",start:{ms:Date.UTC(2026,9,26,22),allDay:true},end:{ms:Date.UTC(2026,9,27,22),allDay:true}, + source:{dtstart:"DTSTART;VALUE=DATE:20261027",dtend:"DTEND;VALUE=DATE:20261028"}} +assert.equal(invitations.params(source,"mailbox",day,"tentative").event.start.date,"2026-10-27") +assert.equal(invitations.params(source,"mailbox",day,"tentative").event.end.date,"2026-10-28") +const cancel = invitations.params(source,"mailbox",{uid:"test",organizer:invite.organizer},"cancelled") +assert.equal(cancel.event,undefined) +assert.equal(invitations.destination({},"mailbox"),null) +assert.equal(invitations.destination({invitationCalendars:{mailbox:{...source,readOnly:true}}},"mailbox"),null) +console.log("calendar invitations ok") +const sources = load("calendar/Sources.js") +const calendars = {version:1,sources:[source,{kind:"google",id:"google:other",accountId:"other@example.org"}]} +assert.equal(sources.forAccount(calendars,"imap:me@example.org",source.id).sources.length,1) +assert.equal(sources.forAccount(calendars,"imap:me@example.org").sources.length,0) From 3b558a6e44a4045433ce889d26c7826359ab1cf7 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 28 Oct 2026 17:13:21 +0800 Subject: [PATCH 53/88] website: Add privacy policy and terms of service pages (#416) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What changes - Adds `docs/privacy.html` and `docs/terms.html`, styled by a shared `docs/legal.css` that uses the home page's palette and follows the system light/dark theme. - Links both pages from the home page footer, in both English and Chinese. - Adds the Google Search Console `google-site-verification` meta tag to the home page. - Publishes the new pages and stylesheet through the Pages workflow, which also redeploys when any of them changes. ## Why Google's OAuth app verification rejected the current consent screen settings for three reasons: - The home page `https://huacnlee.github.io/omamail` is not registered as the owner's site. The meta tag lets Search Console verify ownership. - The privacy policy link pointed at `LICENSE`, which does not describe how data is handled. - The policy was not hosted on the verified domain. After this deploys, the consent screen can use: - Privacy policy: `https://huacnlee.github.io/omamail/privacy.html` - Terms of service: `https://huacnlee.github.io/omamail/terms.html` Verifying the app is the prerequisite for shipping a built-in Google OAuth client. Without verification, Google treats the restricted Gmail scopes as being in testing, and refresh tokens expire after seven days. ## Policy content Every statement was checked against the code: - The four scopes in `OAuth.SCOPES` and the purpose of each. - All storage is local: refresh tokens in the OS credential store, access tokens in memory only, and cache, drafts and outbox in the app directories. - There is no telemetry, no analytics and no server operated by the developer. - The optional AI assistant passes content to a CLI tool the user installed only when the user asks, and every proposal needs the user's approval. - Remote images are blocked by default. Unsubscribe requests are sent only when the user asks. - The Google API Services User Data Policy Limited Use statement. ## Verification - `python3 tests/test_website_install.py` passes. It now also checks that both pages are published and linked from the home page, that the verification tag is present, that the Limited Use statement is present, and that every scope in `OAuth.SCOPES` is explained in the policy. - No screenshots: the browser tooling was unavailable in this session, so the rendered pages have not been checked visually yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- .github/workflows/pages.yml | 4 + docs/index.html | 3 +- docs/legal.css | 150 ++++++++++++++++++++++++++++++++++ docs/privacy.html | 103 +++++++++++++++++++++++ docs/terms.html | 81 ++++++++++++++++++ tests/test_website_install.py | 19 +++++ 6 files changed, 359 insertions(+), 1 deletion(-) create mode 100644 docs/legal.css create mode 100644 docs/privacy.html create mode 100644 docs/terms.html diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 2aee225..8ea9e79 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -5,6 +5,9 @@ on: branches: [main] paths: - docs/index.html + - docs/privacy.html + - docs/terms.html + - docs/legal.css - docs/images/** - install.sh - install.ps1 @@ -41,6 +44,7 @@ jobs: run: | mkdir -p _site cp docs/index.html _site/index.html + cp docs/privacy.html docs/terms.html docs/legal.css _site/ cp -R docs/images _site/images cp install.sh _site/install.sh cp install.ps1 _site/install.ps1 diff --git a/docs/index.html b/docs/index.html index a92c08b..61f0f23 100644 --- a/docs/index.html +++ b/docs/index.html @@ -3,6 +3,7 @@ + Omamail — mail as a native Omarchy window @@ -1044,7 +1045,7 @@

Run from source