From bfaa399215e63d237f169544f42a166c31e1c550 Mon Sep 17 00:00:00 2001 From: Marcus Vorwaller Date: Tue, 10 Nov 2026 14:49:24 -0800 Subject: [PATCH 1/2] Add guarded opt-in native replies with uncertain delivery state Gate native replies behind replies=on and confirm one Return through chat.db. Keep uncertain sends visible and never restore text unless the exit code proves a refusal; preserve newer drafts and bar-owned reply jobs. Rebase onto current upstream reactions and audio, keeping shared AX helpers and permission fixes intact. Regenerate the QML module and execute draft, recipient and target guards before any write; mutations of all four review gaps fail the tests. Preserve upstream UI, manifest, stdin transport and synthetic-only validation. --- .github/workflows/ci.yml | 11 +- BarWidget.qml | 51 +++- BlipView.qml | 165 ++++++++++-- MessageActions.mjs | 24 ++ MessageMenu.qml | 9 + SendState.mjs | 10 +- TapbackActions.mjs | 4 + bin-dir.test.ts | 2 +- bridge/linux/blip-shim | 18 +- bridge/mac/blip-dispatch | 5 +- bridge/mac/imsg | 28 +- bridge/mac/imsg-react | 5 +- bridge/mac/imsg-reply | 462 ++++++++++++++++++++++++++++++++ bridge/mac/install.sh | 2 +- bridge/mac/test_reply.py | 336 +++++++++++++++++++++++ bridge/mac/test_reply_enrich.py | 81 ++++++ bridge/mac/test_reply_jxa.py | 42 +++ collector.ts | 2 +- docs/PRIVACY.md | 2 + docs/SECURITY.md | 8 + docs/replies.md | 150 +++++++++++ message-actions.test.ts | 34 ++- message-actions.ts | 50 ++++ reply-ax.test.ts | 27 ++ reply-session.test.ts | 50 ++++ scripts/blip-setup | 8 +- scripts/test-reply-ui.py | 49 ++++ send-state.test.ts | 9 + send-state.ts | 10 + shim-path.ts | 2 +- source-id.ts | 2 +- tapback-actions.ts | 5 + tapbacks.test.ts | 12 + thread.test.ts | 19 ++ thread.ts | 42 ++- ui.test.ts | 10 +- 36 files changed, 1690 insertions(+), 56 deletions(-) create mode 100755 bridge/mac/imsg-reply create mode 100644 bridge/mac/test_reply.py create mode 100644 bridge/mac/test_reply_enrich.py create mode 100644 bridge/mac/test_reply_jxa.py create mode 100644 docs/replies.md create mode 100644 reply-ax.test.ts create mode 100644 reply-session.test.ts create mode 100644 scripts/test-reply-ui.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b7f0ea3..f4a6fe27 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,12 +34,21 @@ jobs: run: | sudo apt-get install -y -q qml6-module-qtquick-controls qml6-module-qtquick-templates python3 scripts/test-reaction-ui.py + - name: Reply target banner (synthetic QtTest) + run: | + sudo apt-get install -y -q qml6-module-qtquick-controls qml6-module-qtquick-templates qml6-module-qtquick-layouts + python3 scripts/test-reply-ui.py - name: OTP desktop adapter (no desktop needed) run: | python3 -m py_compile otp-desktop.py python3 test_otp_desktop.py - name: Mac tools compile (python3 syntax, no macOS needed) - run: python3 -m py_compile bridge/linux/blip-bridged bridge/mac/imsg bridge/mac/imsg-send bridge/mac/contacts bridge/mac/tcc-check bridge/mac/blip-check bridge/mac/blip-dispatch bridge/mac/imsg-read bridge/mac/imsg-react bridge/mac/calling_codes.py bridge/mac/contact-save bridge/mac/read_state.py bridge/mac/findmy_city.py + run: python3 -m py_compile bridge/linux/blip-bridged bridge/mac/imsg bridge/mac/imsg-send bridge/mac/contacts bridge/mac/tcc-check bridge/mac/blip-check bridge/mac/blip-dispatch bridge/mac/imsg-read bridge/mac/imsg-react bridge/mac/imsg-reply bridge/mac/calling_codes.py bridge/mac/contact-save bridge/mac/read_state.py bridge/mac/findmy_city.py + - name: Native replies (synthetic database and Messages) + run: | + python3 bridge/mac/test_reply.py + python3 bridge/mac/test_reply_enrich.py + python3 bridge/mac/test_reply_jxa.py - name: Sticker metadata and bounded animated rendering run: | sudo apt-get install -y -q ffmpeg imagemagick qt6-image-formats-plugins diff --git a/BarWidget.qml b/BarWidget.qml index 16208f3e..95248b18 100644 --- a/BarWidget.qml +++ b/BarWidget.qml @@ -1,5 +1,7 @@ import QtQuick import "ReadSync.mjs" as ReadSync +import "SendState.mjs" as SendState +import "SourceId.mjs" as SourceId import Quickshell import Quickshell.Io import qs.Commons @@ -61,6 +63,9 @@ BarWidget { * drives Messages through Accessibility, by action names Apple can rename. * The shim refuses the tool without the same key. */ property bool tapbacks: false + /** `replies=on` in bridge.conf: Reply in the message menu sends a native reply + * through the Mac's imsg-reply. OFF by default, like tapbacks. */ + property bool replies: false // The imsg-react run lives HERE, not in the view that asked for it: the // popout's surface and the app window are destroyed on close, and a run @@ -118,6 +123,48 @@ BarWidget { } } + property var replyQueue: [] + property var replyPending: [] + property var replyJob: null + signal replyExited(var job, int code, string reason) + function queueReply(job, pending) { + root.replyPending = root.replyPending.concat([Object.assign({}, pending, {inFlight: true})]) + root.replyQueue = root.replyQueue.concat([job]) + root.pumpReply() + } + function reconcileReplies(chat, pending) { + var ids = pending.map(function(p) { return p.localId }) + root.replyPending = root.replyPending.filter(function(p) { return p.inFlight === true || p.chat !== chat || ids.indexOf(p.localId) >= 0 }) + } + function pumpReply() { + if (replyProc.running || root.replyQueue.length === 0) return + root.replyJob = root.replyQueue[0] + root.replyQueue = root.replyQueue.slice(1) + replyProc.lastErr = "" + replyProc.command = SourceId.bridgeArgv(root.replyJob.chat, "imsg-reply", root.binDir).concat(["--guid", root.replyJob.reply.guid, "--yes", "--text-stdin"]) + replyProc.stdinEnabled = true + replyProc.running = true + replyProc.write(root.replyJob.text) + replyProc.stdinEnabled = false + } + function finishReply(code) { + var job = root.replyJob + if (!job) return + root.replyJob = null + root.replyPending = root.replyPending.map(function(p) { return p.localId === job.localId ? Object.assign({}, p, {inFlight: false}) : p }) + var reason = code === 0 ? "" : SendState.replyFailure(code, replyProc.lastErr) + if (code !== 0) root.replyPending = SendState.markSendFailed(root.replyPending, job.localId, reason) + root.replyExited(job, code, reason) + Qt.callLater(root.pumpReply) + } + Process { + id: replyProc + property string lastErr: "" + onRunningChanged: if (!running) Qt.callLater(function() { if (!replyProc.running && root.replyJob) root.finishReply(70) }) + stderr: StdioCollector { onStreamFinished: replyProc.lastErr = text } + onExited: function(code, status) { root.finishReply(code) } + } + // Local desktop-toast preferences outlive either view, just like tapbacks. // FileView only watches: the TypeScript helper owns parsing and private writes. property var toastAllowlist: [] @@ -1226,9 +1273,10 @@ BarWidget { root.touchpadScrollGain = root.parseGain(t, /^\s*touchpad_scroll_gain\s*=\s*['"]?(\d*\.?\d+)/mi) root.smoothScroll = /^\s*smooth_scroll\s*=\s*['"]?(on|true|1|yes)\b/mi.test(t) root.tapbacks = TapbackActions.tapbacksOn(t) + root.replies = TapbackActions.repliesOn(t) root.bridgeConfLoaded = true } - onLoadFailed: { root.otpAutofill = false; root.automationOn = false; root.uiFontTheme = false; root.uiFontSize = 0; root.scrollGain = 1.0; root.touchpadScrollGain = 1.0; root.smoothScroll = false; root.tapbacks = false; root.binDir = root.home + "/bin"; root.bridgeConfLoaded = true } + onLoadFailed: { root.replies = false; root.otpAutofill = false; root.automationOn = false; root.uiFontTheme = false; root.uiFontSize = 0; root.scrollGain = 1.0; root.touchpadScrollGain = 1.0; root.smoothScroll = false; root.tapbacks = false; root.binDir = root.home + "/bin"; root.bridgeConfLoaded = true } } IpcHandler { target: root.moduleName @@ -1245,6 +1293,7 @@ BarWidget { + " scroll_gain=" + root.scrollGain + (root.touchpadScrollGain !== 1 ? "/" + root.touchpadScrollGain : "") + (root.smoothScroll ? " smooth_scroll=on" : "") + (root.tapbacks ? " tapbacks=on" : "") + + (root.replies ? " replies=on" : "") + (root.lastError !== "" ? " error=" + root.lastError : "") } function citystatus(): string { diff --git a/BlipView.qml b/BlipView.qml index 35c5008c..ea6c1465 100644 --- a/BlipView.qml +++ b/BlipView.qml @@ -678,7 +678,7 @@ FocusScope { pendingThreadChat = "" root.threadPendingRevision = root.pendingRevision root.threadTapbackDone = !!(root.pendingTapback && root.pendingTapback.done) - var pending = root.pendingSends.filter(function(p) { return p.chat === threadRunningChat }) + var pending = root.pendingSends.concat(hostWidget ? hostWidget.replyPending : []).filter(function(p) { return p.chat === threadRunningChat }) threadProc.command = ["bun", root.threadScript, threadRunningChat, "80", "--time-format", root.timeFormat, "--date-format", root.dateFormat, @@ -858,9 +858,27 @@ FocusScope { if (isImageMime(atts[i].mime)) { copyAttachment(atts[i]); return } } } - /** Ctrl+R: quote the selected bubble into the compose field. iMessage's - * inline reply is not reachable through the bridge (no message GUID leaves - * the Mac and AppleScript has no reply-to), so this is a plain "> quote". */ + // ---- native replies: replies=on in bridge.conf, the Mac's imsg-reply. The + // composer holds one target at a time; send() hands it to the send queue. + readonly property bool repliesOn: hostWidget ? hostWidget.replies === true : false + property var replyTarget: null + onActiveChanged: if (root.replyTarget && (!root.active || String(root.active.chat) !== root.replyTarget.chat)) root.replyTarget = null + function canReplyTo(b) { + return root.repliesOn && root.online && !!root.active && root.isSendable(root.active) && MessageActions.canReply(b, root.activeIsGroup) + } + function startReply(b) { + var target = MessageActions.replyTarget(b) + target.chat = String(root.active.chat) + root.replyTarget = target + if (target.attachment && root.isImageMime(target.attachment.mime)) root.enqueueFetch(target.attachment, "", true) + leaveBubbles() + Qt.callLater(function() { composeField.forceActiveFocus() }) + } + /** Ctrl+R: a native reply where the Mac can send one, otherwise a "> quote". */ + function replyToBubble(b) { + if (root.canReplyTo(b)) root.startReply(b) + else root.quoteBubble(b) + } function quoteBubble(b) { composeField.text = MessageActions.quotedDraft(b, composeField.text) composeField.cursorPosition = composeField.length @@ -1234,6 +1252,8 @@ FocusScope { if (!inThread) return for (var i = 0; i < bubbles.length; i++) { var atts = (bubbles[i].attachments || []).concat((bubbles[i].stickers || []).map(function(sticker) { return sticker.attachment })) + var replied = bubbles[i].replyAttachment + if (replied && isImageMime(replied.mime) && typeof replied.bytes === "number" && replied.bytes > 0 && replied.bytes <= root.autoFetchMaxSource) enqueueFetch(replied, "", true) for (var j = 0; j < atts.length; j++) { // Bytes must be KNOWN — null/0 must not slip through and auto-pull // something the click-path 100 MB ceiling would allow. The SOURCE cap @@ -1692,6 +1712,11 @@ FocusScope { return } + if (root.replyTarget && root.attachCount > 0) { + note = "a reply can't carry attachments yet — remove them or cancel the reply" + return + } + if (root.attachCount > 0) { if (sendProc.running || fileSendProc.running) { note = "a message is already sending" @@ -1721,14 +1746,26 @@ FocusScope { // green-bubble (SMS/RCS) threads send on their own service (war room #2) var svc = String(root.active.service || "") if (!root.activeIsGroup && /^(SMS|RCS)$/i.test(svc)) target = target.concat(["--service", svc.toUpperCase()]) - var localId = String(++root.nextSendId) + var localId = String(Date.now()) + "-" + String(++root.nextSendId) root.pendingRevision++ - root.pendingSends = root.pendingSends.concat([{ chat: chat, text: text, ts: stamp, localId: localId }]) - root.bubbles = root.appendPendingBubble(root.bubbles, text, stamp, localId) + var reply = root.replyTarget + root.replyTarget = null + var pendingSend = { chat: chat, text: text, ts: stamp, localId: localId } + if (reply) { + pendingSend.replyText = reply.text + pendingSend.replyMine = reply.mine + pendingSend.replyAttachment = reply.attachment + } + if (!reply) root.pendingSends = root.pendingSends.concat([pendingSend]) + root.bubbles = root.appendPendingBubble(root.bubbles, text, stamp, localId, reply) root.pinToBottom = true composeField.text = "" note = "" - root.sendQueue = root.sendQueue.concat([{ chat: chat, text: text, stamp: stamp, localId: localId, target: target }]) + if (reply) { + hostWidget.queueReply({ chat: chat, text: text, stamp: stamp, localId: localId, reply: reply }, pendingSend) + return + } + root.sendQueue = root.sendQueue.concat([{ chat: chat, text: text, stamp: stamp, localId: localId, target: target, reply: null }]) pumpSend() } @@ -1758,7 +1795,7 @@ FocusScope { /** The instant echo: thread.ts's pendingBubble() in miniature — enough to * draw the bubble in the right run with the right clock. Every reload * replaces it with the TypeScript version until the real row lands. */ - function appendPendingBubble(list, text, stamp, localId) { + function appendPendingBubble(list, text, stamp, localId, reply) { var out = (list || []).slice() var prev = out.length ? out[out.length - 1] : null var newDay = !prev || root.localDay(prev.ts) !== root.localDay(stamp) @@ -1767,18 +1804,18 @@ FocusScope { if (!start) { var p = Object.assign({}, prev); p.groupEnd = false; p.time = ""; out[out.length - 1] = p } out.push({ localId: localId, ts: stamp, from_me: true, name: "", text: String(text).trim(), day: newDay ? "Today" : "", groupStart: start, groupEnd: true, time: Qt.formatTime(new Date(), root.timeFormat), - receipt: "", tapbacks: [], attachments: [], replyText: "", replyMine: false, edited: false, + receipt: "", tapbacks: [], attachments: [], replyText: reply ? reply.text : "", replyMine: reply ? reply.mine : false, replyAttachment: reply ? reply.attachment : null, edited: false, link: null, retracted: false, effect: "", audio: false, html: "", failed: false, pending: true }) return out } - function failPending(chat, localId, reason, text, stamp) { + function failPending(chat, localId, reason, text, stamp, reply) { root.pendingRevision++ root.pendingSends = SendState.markSendFailed(root.pendingSends, localId, reason, - {chat: chat, localId: localId, text: text, ts: stamp}) + {chat: chat, localId: localId, text: text, ts: stamp, replyText: reply ? reply.text : "", replyMine: reply ? reply.mine : false, replyAttachment: reply ? reply.attachment : null}) if (root.inThread && String(root.active.chat) === chat) { if (!root.bubbles.some(function(b) { return b.localId === localId })) - root.bubbles = root.appendPendingBubble(root.bubbles, text, stamp, localId) + root.bubbles = root.appendPendingBubble(root.bubbles, text, stamp, localId, reply) root.bubbles = SendState.markSendFailed(root.bubbles, localId, reason) } } @@ -1798,7 +1835,8 @@ FocusScope { // process on this machine and travels through ssh into the Mac's ps. // Which program sends is source-id.ts's answer for this conversation — in // stock Blip always the Mac's imsg-send in bin_dir, exactly as before. - sendProc.command = SourceId.bridgeArgv(job.chat, "imsg-send", hostWidget ? hostWidget.binDir : root.home + "/bin").concat(job.target).concat(["--yes", "--text-stdin", "--keep-dashes"]) + var binDir = hostWidget ? hostWidget.binDir : root.home + "/bin" + sendProc.command = SourceId.bridgeArgv(job.chat, "imsg-send", binDir).concat(job.target).concat(["--yes", "--text-stdin", "--keep-dashes"]) sendProc.stdinEnabled = true sendProc.running = true sendProc.write(job.text) @@ -1920,7 +1958,8 @@ FocusScope { // ordinary reload (the bubble stays up either way). if (Array.isArray(d.pending)) { var chat = root.threadRunningChat - root.pendingSends = root.pendingSends.filter(function(p) { return p.chat !== chat }).concat(d.pending) + root.pendingSends = root.pendingSends.filter(function(p) { return p.chat !== chat }).concat(d.pending.filter(function(p) { return !p.replyText })) + if (hostWidget) hostWidget.reconcileReplies(chat, d.pending) if (d.pending.some(function(p) { return p.failed !== true }) && root.reloadTries < 8) { root.reloadTries++ root.reloadChat = chat @@ -2003,7 +2042,9 @@ FocusScope { : plainErr !== "" ? plainErr : "Send failed (exit " + code + ")" root.failPending(completedChat, completedId, reason, completedText, completedStamp) if (belongsHere) { - if (composeField.text === "") composeField.text = completedText + if (composeField.text === "" && belongsHere) { + composeField.text = completedText + } if (code === 69 || code === 255) root.note = "not sent — Mac unreachable" else root.note = (plainErr !== "" ? "send failed: " + plainErr : "send failed (exit " + code + ")") } @@ -2016,6 +2057,17 @@ FocusScope { // imsg-react runs in hostWidget (BarWidget), which outlives this view. Connections { target: root.hostWidget + function onReplyPendingChanged() { root.pendingRevision++ } + function onReplyExited(job, code, reason) { + if (root.inThread && String(root.active.chat) === job.chat) { + root.note = reason + if (SendState.replyCanRestore(code) && composeField.text === "") { + composeField.text = job.text + root.replyTarget = job.reply + } + root.requestThreadLoad(job.chat) + } + } function onTapbackExited(chat, ok) { if (ok) { // the thread was left while it ran: nothing on screen is waiting for it @@ -3570,17 +3622,31 @@ FocusScope { spacing: 0 Item { Layout.fillWidth: true; visible: bubbleRow.mine } Rectangle { - Layout.preferredWidth: Math.min(Math.ceil(replySnippet.implicitWidth) + Style.space(18), Math.round(content.width * 0.7)) - Layout.preferredHeight: Math.ceil(replySnippet.implicitHeight) + Style.space(10) + id: replyChip + readonly property var att: modelData.replyAttachment || null + readonly property string thumb: att && root.isImageMime(att.mime) ? String(root.attFiles[String(att.id || "")] || "") : "" + readonly property int thumbSpace: thumb !== "" ? Style.space(28) + Style.space(6) : 0 + Layout.preferredWidth: Math.min(Math.ceil(replySnippet.implicitWidth) + Style.space(18) + thumbSpace, Math.round(content.width * 0.7)) + Layout.preferredHeight: Math.max(Math.ceil(replySnippet.implicitHeight), thumb !== "" ? Style.space(28) : 0) + Style.space(10) radius: Style.space(12) color: "transparent" border.color: root.dim border.width: 1 opacity: 0.75 + Image { + visible: replyChip.thumb !== "" + x: Style.space(9); y: Style.space(5) + width: Style.space(28); height: width + source: replyChip.thumb + fillMode: Image.PreserveAspectCrop + asynchronous: true + sourceSize.width: width * 2; sourceSize.height: height * 2 + } Text { id: replySnippet - x: Style.space(9); y: Style.space(5) - width: Math.round(content.width * 0.7) - Style.space(18) + x: Style.space(9) + replyChip.thumbSpace + anchors.verticalCenter: parent.verticalCenter + width: parent.width - x - Style.space(9) text: "↩ " + (modelData.replyMine ? "You: " : "") + String(modelData.replyText || "") textFormat: Text.PlainText elide: Text.ElideRight @@ -4086,7 +4152,7 @@ FocusScope { Layout.leftMargin: bubbleRow.mine ? 0 : Style.space(6) Layout.maximumWidth: Math.max(1, content.width * 0.78) wrapMode: Text.WrapAnywhere - text: [modelData.failed === true ? "⚠ Not Delivered" : "", + text: [modelData.failed === true ? (modelData.failureReason === "Check Messages before sending again" ? "⚠ Delivery unconfirmed" : "⚠ Not Delivered") : "", modelData.failed === true ? String(modelData.failureReason || "") : modelData.pending === true ? "Sending…" : modelData.scheduled === true ? "Scheduled for " + String(modelData.scheduledFor || "") @@ -4179,6 +4245,57 @@ FocusScope { foreground: root.foreground } + // The message a native reply is answering. ✕ or Escape cancels it. + Rectangle { + id: replyBanner + readonly property var target: root.replyTarget + readonly property var att: target ? target.attachment : null + readonly property string thumb: att && root.isImageMime(att.mime) ? String(root.attFiles[String(att.id || "")] || "") : "" + Layout.fillWidth: true + Layout.preferredHeight: Math.max(replyBannerText.implicitHeight, thumb !== "" ? Style.space(28) : 0) + Style.space(12) + visible: root.inThread && target !== null + radius: Style.space(12) + color: Qt.alpha(root.foreground, 0.08) + RowLayout { + anchors.fill: parent + anchors.leftMargin: Style.space(10) + anchors.rightMargin: Style.space(6) + spacing: Style.space(6) + Image { + visible: replyBanner.thumb !== "" + Layout.preferredWidth: Style.space(28) + Layout.preferredHeight: Style.space(28) + source: replyBanner.thumb + fillMode: Image.PreserveAspectCrop + asynchronous: true + sourceSize.width: Style.space(56); sourceSize.height: Style.space(56) + } + Text { + id: replyBannerText + Layout.fillWidth: true + text: replyBanner.target + ? "Replying to " + (replyBanner.target.mine ? "yourself" : (replyBanner.target.name || "message")) + ": " + replyBanner.target.text + : "" + textFormat: Text.PlainText + elide: Text.ElideRight + maximumLineCount: 1 + color: root.dim + font.family: root.fontFamily + font.pixelSize: root.fontCaption + } + Text { + text: "✕" + textFormat: Text.PlainText + color: root.dim + font.family: root.fontFamily + font.pixelSize: root.fontCaption + leftPadding: Style.space(6); rightPadding: Style.space(6) + Accessible.name: "Cancel reply" + HoverHandler { cursorShape: Qt.PointingHandCursor } + TapHandler { onTapped: { root.replyTarget = null; composeField.forceActiveFocus() } } + } + } + } // queued attachments — ✕ removes one. ONE CHIP PER ROW, never a // RowLayout of N: summed implicit widths stretch the whole column // past the panel and take every right-aligned element off-screen @@ -4341,7 +4458,7 @@ FocusScope { bottomPadding: Style.spacing.inputPaddingY + Border.bottom(_composeBorder) // Esc drops a bubble selection first (back to the bottom), then // leaves the thread — the two-step Esc a text selection gets. - Keys.onEscapePressed: if (root.shareUrl !== "") root.closeShare(); else if (root.bubbleCursor >= 0) root.leaveBubbles(); else root.back() + Keys.onEscapePressed: if (root.shareUrl !== "") root.closeShare(); else if (root.bubbleCursor >= 0) root.leaveBubbles(); else if (root.replyTarget) root.replyTarget = null; else root.back() // Left from the START of the text (or an empty field) hands focus // back to the sidebar (split view); anywhere else it moves the // caret as usual — the arrows' edge rule. Not while the share @@ -4386,7 +4503,7 @@ FocusScope { if (b) { if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) { event.accepted = true; root.openBubble(b); return } if (event.matches(StandardKey.Copy)) { event.accepted = true; root.copyBubble(b); return } - if (event.key === Qt.Key_R && (event.modifiers & Qt.ControlModifier)) { event.accepted = true; root.quoteBubble(b); return } + if (event.key === Qt.Key_R && (event.modifiers & Qt.ControlModifier)) { event.accepted = true; root.replyToBubble(b); return } if (event.key === Qt.Key_E && (event.modifiers & Qt.ControlModifier)) { event.accepted = true; root.openMessageMenuAt(b, root.bubbleCursorItem); return } } if ((event.key === Qt.Key_Return || event.key === Qt.Key_Enter) @@ -4566,6 +4683,8 @@ FocusScope { palette.buttonText: root.foreground palette.highlight: root.accent palette.highlightedText: "#ffffff" + canReply: root.canReplyTo(root.messageContext) + onReplyRequested: if (root.messageContext) root.startReply(root.messageContext) canQuote: root.messageContext !== null && MessageActions.quoteText(root.messageContext) !== "" && root.online && root.isSendable(root.active) canCopy: root.messageContext !== null && String(root.messageContext.text || "") !== "" diff --git a/MessageActions.mjs b/MessageActions.mjs index 8799f042..e6371f84 100644 --- a/MessageActions.mjs +++ b/MessageActions.mjs @@ -14,6 +14,28 @@ function quotedDraft(message, draft) { ` + draft; } +function canReply(message, group) { + if (!message || group || !String(message.guid || "")) + return false; + if (message.retracted || message.audio || message.pending || message.failed || message.scheduled || message.link) + return false; + const text = String(message.text || "").trim(); + const files = message.attachments ?? []; + if (files.length === 0) + return text !== "" && !/:\/\/|\bwww\./i.test(text); + const only = files[0]; + return files.length === 1 && text === "" && !only.sticker && String(only.mime || "").toLowerCase().startsWith("image/"); +} +function replyTarget(message) { + const attachment = (message.attachments ?? [])[0] ?? null; + return { + guid: String(message.guid || ""), + text: quoteText(message) || (attachment ? "Photo" : ""), + mine: message.from_me === true, + name: String(message.name || ""), + attachment + }; +} function bubbleIndexByGuid(bubbles, guid) { if (!guid) return -1; @@ -33,7 +55,9 @@ function attachmentActions(att) { export { attachmentActions, bubbleIndexByGuid, + canReply, quoteText, quotedDraft, + replyTarget, saveableMime }; diff --git a/MessageMenu.qml b/MessageMenu.qml index 368641d9..01c69739 100644 --- a/MessageMenu.qml +++ b/MessageMenu.qml @@ -23,8 +23,10 @@ Menu { property var attachmentRows: null readonly property bool attachmentShown: !!menu.attachmentRows && menu.attachmentRows.open === true readonly property bool textRowsShown: menu.linkUrl === "" && !menu.attachmentShown + property bool canReply: false property bool canQuote: false property bool canCopy: false + signal replyRequested() signal quoteRequested() signal copyRequested() signal openRequested(string url) @@ -96,6 +98,13 @@ Menu { height: visible ? implicitHeight : 0 onTriggered: menu.saveAttachmentRequested() } + MenuItem { + text: "Reply" + visible: menu.canReply && menu.linkUrl === "" + height: visible ? implicitHeight : 0 + enabled: visible + onTriggered: menu.replyRequested() + } MenuItem { text: "Quote and reply" visible: menu.textRowsShown diff --git a/SendState.mjs b/SendState.mjs index 62e3779c..5b54354b 100644 --- a/SendState.mjs +++ b/SendState.mjs @@ -7,7 +7,15 @@ function markSendFailed(items, id, reason, fallback) { function friendlySendError(raw) { return /(^|[^\d])-10810(\D|$)/.test(raw) ? "Messages can't start on the Mac: nobody is logged in there. Log in on the Mac, then send again." : raw; } +function replyFailure(code, detail) { + return ![1, 64, 65, 75, 77].includes(code) ? "Check Messages before sending again" : detail.trim().slice(0, 240) || "Reply was not sent"; +} +function replyCanRestore(code) { + return [1, 64, 65, 75, 77].includes(code); +} export { friendlySendError, - markSendFailed + markSendFailed, + replyCanRestore, + replyFailure }; diff --git a/TapbackActions.mjs b/TapbackActions.mjs index aab64f94..f5c19a8b 100644 --- a/TapbackActions.mjs +++ b/TapbackActions.mjs @@ -4003,6 +4003,9 @@ function moveTapbackCursor(cursor, dir, n = TAPBACKS.length) { function tapbacksOn(conf) { return confOn(conf, "tapbacks"); } +function repliesOn(conf) { + return confOn(conf, "replies"); +} function confOn(conf, key) { let v = ""; for (const raw of String(conf || "").split(` @@ -4213,6 +4216,7 @@ export { pendingAfterLoad, pendingTapback, reactionEmoji, + repliesOn, searchReactionEmoji, shownTapbacks, tapbackArgs, diff --git a/bin-dir.test.ts b/bin-dir.test.ts index 834ab414..7e5f936f 100644 --- a/bin-dir.test.ts +++ b/bin-dir.test.ts @@ -47,7 +47,7 @@ test("no spawner hard-codes ~/bin", () => { const dir = import.meta.dir; for (const f of readdirSync(dir).filter(n => /\.(ts|qml)$/.test(n) && !n.endsWith(".test.ts"))) { const src = readFileSync(join(dir, f), "utf8"); - expect({ f, hit: /["`'/]bin\/(imsg|imsg-send|imsg-read|imsg-react|contacts|contact-save)\b|["`']bin["`']\s*,\s*["`'](imsg|imsg-send|imsg-read|imsg-react|contacts|contact-save)["`']/.test(src.replace(/^\s*(\/\/|\*).*$/gm, "")) }) + expect({ f, hit: /["`'/]bin\/(imsg|imsg-send|imsg-read|imsg-react|imsg-reply|contacts|contact-save)\b|["`']bin["`']\s*,\s*["`'](imsg|imsg-send|imsg-read|imsg-react|imsg-reply|contacts|contact-save)["`']/.test(src.replace(/^\s*(\/\/|\*).*$/gm, "")) }) .toEqual({ f, hit: false }); } }); diff --git a/bridge/linux/blip-shim b/bridge/linux/blip-shim index ce8cd537..63397940 100755 --- a/bridge/linux/blip-shim +++ b/bridge/linux/blip-shim @@ -1,6 +1,6 @@ #!/usr/bin/env bash # blip-shim — the Linux side of Blip's Mac bridge. blip-setup installs it as -# imsg, imsg-send, imsg-read, imsg-react, contacts, and contact-save. It +# imsg, imsg-send, imsg-read, imsg-react, imsg-reply, contacts, and contact-save. It # dispatches on its own basename and execs the matching Mac tool over ssh. # # iMessage is macOS-only: chat.db is TCC-protected and AppleScript send needs @@ -14,7 +14,8 @@ # holding the tools; default ~/.blip/bin), python (default python3), # automation (on|off — read by the plugin, not the shim), hide_spam / # hide_unknown (on|off — injected as imsg flags; default off), tapbacks -# (on|off — imsg-react is refused unless on; default off). +# (on|off — imsg-react is refused unless on; default off), replies +# (on|off — imsg-reply is refused unless on; default off). # # There is deliberately NO connectivity probe — see the note above the ssh # call at the bottom. The old one cost 41 ms on every call and had to be @@ -25,7 +26,7 @@ CONF="${BLIP_BRIDGE_CONF:-$HOME/.config/blip/bridge.conf}" # remote_bin is expanded by the MAC's shell, so keep it literal here — a bare # ~ or $HOME in bridge.conf would expand to the Linux home (it did). host=""; remote_bin='$HOME/.blip/bin'; python="python3"; key="$HOME/.ssh/blip_ed25519" -hide_spam=""; hide_unknown=""; tapbacks="" +hide_spam=""; hide_unknown=""; tapbacks=""; replies="" # Parsed, not sourced: the file is data. Known keys only, quotes stripped. if [[ -r "$CONF" ]]; then while IFS= read -r line || [[ -n "$line" ]]; do @@ -41,6 +42,7 @@ if [[ -r "$CONF" ]]; then hide_spam) hide_spam="$v" ;; hide_unknown) hide_unknown="$v" ;; tapbacks) tapbacks="$v" ;; + replies) replies="$v" ;; esac done < "$CONF" fi @@ -58,8 +60,8 @@ fi tool="$(basename "$0")" case "$tool" in - imsg|imsg-send|imsg-read|imsg-react|contacts|contact-save|tcc-check|blip-check) ;; - *) echo "blip-shim: unknown tool name '$tool' (install as imsg, imsg-send, imsg-read, imsg-react, contacts, or contact-save)" >&2; exit 64 ;; + imsg|imsg-send|imsg-read|imsg-react|imsg-reply|contacts|contact-save|tcc-check|blip-check) ;; + *) echo "blip-shim: unknown tool name '$tool' (install as imsg, imsg-send, imsg-read, imsg-react, imsg-reply, contacts, or contact-save)" >&2; exit 64 ;; esac # Tapbacks drive Messages' own buttons through Accessibility, by names Apple # localizes and may rename: opt-in, refused before anything reaches the Mac. @@ -69,6 +71,12 @@ if [[ "$tool" == imsg-react ]]; then *) echo "imsg-react: tapbacks are off — set tapbacks=on in $CONF to enable" >&2; exit 78 ;; esac fi +if [[ "$tool" == imsg-reply ]]; then + case "${replies,,}" in + on|yes|true|1) ;; + *) echo "imsg-reply: replies are off — set replies=on in $CONF to enable" >&2; exit 78 ;; + esac +fi # With the dedicated key (blip-setup enrolls it as restrict,command=blip-dispatch) # the Mac side can ONLY run the bridge tools, so the remote command is bare. diff --git a/bridge/mac/blip-dispatch b/bridge/mac/blip-dispatch index ef6a51ed..57de06b8 100755 --- a/bridge/mac/blip-dispatch +++ b/bridge/mac/blip-dispatch @@ -19,8 +19,9 @@ import sys # Mail/Calendar/Notes/Reminders and pops four Automation prompts on the Mac. # blip-check covers what Blip needs. # imsg-react changes the Mac's Messages (a tapback); the shim only calls it with -# tapbacks=on in bridge.conf, and it acts only with --yes. -TOOLS = {"imsg", "imsg-send", "imsg-read", "imsg-react", "contacts", "contact-save", "blip-check"} +# tapbacks=on in bridge.conf, and it acts only with --yes. imsg-reply sends a native +# reply the same way, under replies=on. +TOOLS = {"imsg", "imsg-send", "imsg-read", "imsg-react", "imsg-reply", "contacts", "contact-save", "blip-check"} def refuse(msg: str) -> None: diff --git a/bridge/mac/imsg b/bridge/mac/imsg index 4bc4938d..6234eef4 100755 --- a/bridge/mac/imsg +++ b/bridge/mac/imsg @@ -1327,13 +1327,30 @@ def enrich(con, rows, with_names: bool, skew_ns=TAPBACK_SKEW_NS): if origins: for o in _chunked_in( con, - f"SELECT guid, text, attributedBody, is_from_me FROM {MESSAGE_SRC} m WHERE guid IN ({{marks}})", + f"SELECT ROWID, guid, text, attributedBody, is_from_me FROM {MESSAGE_SRC} m WHERE guid IN ({{marks}})", origins, ): osnip[o["guid"]] = { "text": (o["text"] or decode_attributed_body(o["attributedBody"]) or "")[:120], "from_me": bool(o["is_from_me"]), + "rowid": o["ROWID"], } + # A reply to a picture has no text to quote, so the context names the file. + bare = {v["rowid"]: k for k, v in osnip.items() if not v["text"].replace("\ufffc", "").strip()} + origin_files: dict[str, list] = {} + if bare: + for a in _chunked_in( + con, + """SELECT maj.message_id AS mid, a.ROWID AS aid, a.transfer_name AS name, a.mime_type AS mime + FROM message_attachment_join maj JOIN attachment a ON a.ROWID = maj.attachment_id + WHERE a.transfer_name NOT LIKE '%.pluginPayloadAttachment' AND maj.message_id IN ({marks}) + ORDER BY a.ROWID""", + list(bare), + ): + origin_files.setdefault(bare[a["mid"]], []).append( + {"id": str(a["aid"]), "name": a["name"] or "file", "mime": a["mime"]}) + for snip in osnip.values(): + del snip["rowid"] for r in kept: e = extras[r["ROWID"]] @@ -1344,7 +1361,13 @@ def enrich(con, rows, with_names: bool, skew_ns=TAPBACK_SKEW_NS): if has_edit: e.update(_edit_flags(r["date_edited"], r["date_retracted"], r["message_summary_info"])) if r["orig_guid"] and r["orig_guid"] in osnip: - e["reply_to"] = osnip[r["orig_guid"]] + e["reply_to"] = dict(osnip[r["orig_guid"]]) + files = origin_files.get(r["orig_guid"]) + if files: + # thread_originator_part opens with the part replied to ("2:0:1"). + part = str(r["orig_part"] or "0").split(":")[0] + index = int(part) if part.isdecimal() else 0 + e["reply_to"]["attachment"] = files[index] if index < len(files) else files[0] fx = _effect_name(r["style_id"]) if fx: e["effect"] = fx @@ -1515,6 +1538,7 @@ def base_select(con) -> str: m.associated_message_type AS assoc_type, m.associated_message_guid AS assoc_guid, m.thread_originator_guid AS orig_guid, + m.thread_originator_part AS orig_part, m.expressive_send_style_id AS style_id, m.is_audio_message, m.balloon_bundle_id, m.payload_data, diff --git a/bridge/mac/imsg-react b/bridge/mac/imsg-react index 01e797c7..86b09534 100755 --- a/bridge/mac/imsg-react +++ b/bridge/mac/imsg-react @@ -462,7 +462,8 @@ function bodyIs(body, want) { // and the menu (see LIVE). function collect(q) { const all = win().entireContents(); - const kinds = /\.(groups|uiElements)\.at\(\d+\)$/; + // A picture's live bubble is a button below its row (imsg-reply, 27.2). + const kinds = q && q.picture ? /\.(groups|uiElements|buttons)\.at\(\d+\)$/ : /\.(groups|uiElements)\.at\(\d+\)$/; const paths = all.map(el => Automation.getDisplayString(el)); const at = {}, kids = {}, fields = []; paths.forEach((s, i) => { @@ -1035,7 +1036,7 @@ def perform(target: dict, action: str, run=jxa, deadline=None) -> dict: things repeated, and they stop at `deadline` (time.monotonic()).""" request = {"text": target["text"], "from_me": target["from_me"], "hhmm": target["hhmm"], "ordinal": target["ordinal"], "newest": target["newest"], "actions": ACTIONS + [action], - "action": action, "frame": "", "mode": "perform"} + "action": action, "frame": "", "mode": "perform", "picture": bool(target.get("picture"))} down = up = settles = 0 looked_again = bottom_seen = False while True: diff --git a/bridge/mac/imsg-reply b/bridge/mac/imsg-reply new file mode 100755 index 00000000..08b615ef --- /dev/null +++ b/bridge/mac/imsg-reply @@ -0,0 +1,462 @@ +#!/usr/bin/env python3 +"""imsg-reply: send one native inline reply through Messages. Dry run unless --yes. + + imsg-reply --guid GUID --text-stdin [--yes] + +AppleScript cannot reply, so this drives the bubble's "Reply…" action through +Accessibility, the way imsg-react drives "Tapback Details…". Measured on macOS +27.2; see docs/replies.md for what was tried, what was refused and why. + +How one run goes: + 1. chat.db alone decides the target and refuses what is not proven: groups, + links and cards, captions, several attachments, anything but one picture, + and two pictures on one side in the same minute (their labels are identical). + 2. A draft in the Mac's composer stops the run. "Reply…" moves the draft into the + reply field, so sending would take it along or overwrite it. + 3. Press "Reply…" once on the target. The reply view must name that target. + 4. Fill the field through AX, never the clipboard: the clipboard would carry the + text to other machines. Read it back, recheck focus, press Return. + 5. chat.db decides. A row of yours with this text must name the target's thread. + +The text arrives on stdin and is never in argv or a log. + +Exit codes: 0 sent and confirmed; 75 nothing sent, try again; 76 Return was pressed +but chat.db did not confirm it, so the caller must NOT resend; others as imsg-react. +""" +from __future__ import annotations + +import argparse +import datetime as dt +import json +import os +import re +import sqlite3 +import subprocess +import sys +import time +from importlib.machinery import SourceFileLoader +from importlib.util import module_from_spec, spec_from_loader + +HERE = os.path.dirname(os.path.realpath(__file__)) + + +def _load(name: str): + loader = SourceFileLoader(name.replace("-", "_"), os.path.join(HERE, name)) + spec = spec_from_loader(loader.name, loader) + module = module_from_spec(spec) + loader.exec_module(module) + return module + + +rx = _load("imsg-react") +Stop = rx.Stop +EX_USAGE, EX_UNSUPPORTED, EX_UNAVAILABLE, EX_SOFTWARE, EX_TEMPFAIL, EX_NOPERM = ( + rx.EX_USAGE, rx.EX_UNSUPPORTED, rx.EX_UNAVAILABLE, rx.EX_SOFTWARE, rx.EX_TEMPFAIL, rx.EX_NOPERM) +EX_UNCONFIRMED = 76 + +REPLY_ACTION = "Reply…" +PICTURE_LABEL = "Includes picture" # a picture bubble's whole body, measured on 27.2 +MAX_CHARS = 4000 +MAX_BYTES = 16000 +CONFIRM_SECONDS = 15 +OPEN_POLLS = 10 +LOG = os.path.expanduser("~/.blip/reply.log") + +# Answers with no message text in them. The request (text included) is on stdin. +JXA = r""" +ObjC.import("Foundation"); +const se = Application("System Events"); +const proc = se.processes.byName("Messages"); +const REACTED = new RegExp(REACTED_SOURCE); +function safe(f, d) { try { const v = f(); return v === undefined ? d : v; } catch (e) { return d; } } +function norm(s) { return String(s || "").replace(//g, "").replace(/\s+/g, " ").trim(); } +function focused() { return safe(() => proc.attributes.byName("AXFocusedUIElement").value(), null); } +function attr(el, name) { return safe(() => el.attributes.byName(name).value(), null); } +function info(el) { + if (!el) return { id: "", placeholder: "", length: 0 }; + return { id: String(attr(el, "AXIdentifier") || ""), placeholder: String(attr(el, "AXPlaceholderValue") || ""), + length: String(safe(() => el.value(), "") || "").length }; +} +function front() { return safe(() => proc.frontmost(), false) === true; } +function parseLabel(d) { + const m = String(d).match(/^([\s\S]*), (\d{1,2})[:.](\d{2})(?:\s*([AP]M))?$/); + if (!m) return null; + let h = parseInt(m[2], 10); + if (m[4] === "PM" && h < 12) h += 12; + if (m[4] === "AM" && h === 12) h = 0; + return { time: (h < 10 ? "0" : "") + h + ":" + m[3], body: norm(m[1].replace(/^[^,]*, /, "").replace(/^Reply…, /, "")) }; +} +function bodyIs(body, want) { + if (body === want) return true; + return body.startsWith(want + ", ") && REACTED.test(body.slice(want.length + 2)); +} +// The reply view opens over the conversation, with its own transcript of the thread. +function viewNames(q) { + const all = safe(() => proc.windows[0].entireContents(), []); + const paths = all.map(e => Automation.getDisplayString(e)); + const at = all.findIndex(e => String(safe(() => e.description(), "")) === "Reply transcript"); + if (at < 0) return false; + const under = paths[at] + "."; + return all.some((e, i) => { + if (!paths[i].startsWith(under)) return false; + const label = parseLabel(String(safe(() => e.description(), "") || "")); + return !!label && label.time === q.hhmm && bodyIs(label.body, q.text); + }); +} +function composer() { + let el = focused(), i = info(el); + if (i.id === "messageBodyField") return i; + for (const e of safe(() => proc.windows[0].entireContents(), [])) { + if (!/\.textFields\.at\(\d+\)$/.test(Automation.getDisplayString(e))) continue; + i = info(e); + if (i.id === "messageBodyField") return i; + } + return null; +} +function identity(q) { + const w = safe(() => proc.windows[0], null); + if (!w || safe(() => w.sheets.length, 1) !== 0) return false; + const title = norm(safe(() => w.name(), "")); + return (q.chatNames || []).some(n => norm(n) === title); +} +function send(q) { + let el = null, i = null; + for (let n = 0; n < q.polls; n++) { + el = focused(); i = info(el); + if (i.id === "messageBodyField" && i.placeholder === "Reply") break; + i = null; + delay(0.3); + } + if (!i) return { ok: false, error: "no-reply-view" }; + if (!front()) return { ok: false, error: "not-front" }; + if (i.length > 0) return { ok: false, error: "draft" }; + if (!identity(q)) return { ok: false, error: "wrong-conversation" }; + if (!viewNames(q)) return { ok: false, error: "reply-elsewhere" }; + try { el.value = q.body; } catch (e) { return { ok: false, error: "fill-failed" }; } + delay(0.3); + if (String(safe(() => el.value(), "")) !== q.body) { safe(() => { el.value = ""; }); return { ok: false, error: "fill-failed" }; } + const now = info(focused()); + if (!identity(q) || !viewNames(q) || !front() || now.id !== "messageBodyField" || now.placeholder !== "Reply") { + safe(() => { el.value = ""; }); + return { ok: false, error: "focus-moved" }; + } + se.keyCode(36); + delay(0.8); + const left = info(focused()); + if (left.length > 0) return { ok: false, error: "unconfirmed" }; + return { ok: true }; +} +function run() { + const input = $.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile; + const q = JSON.parse(String(ObjC.unwrap($.NSString.alloc.initWithDataEncoding(input, $.NSUTF8StringEncoding)))); + if (q.mode === "composer") { + const c = composer(); + return JSON.stringify(c ? { ok: true, length: c.length, inReply: c.placeholder === "Reply" } : { ok: false, error: "no-conversation" }); + } + if (q.mode === "send") return JSON.stringify(send(q)); + if (q.mode === "close") { + const i = info(focused()); + if (front() && i.id === "messageBodyField" && i.placeholder === "Reply") { se.keyCode(53); return JSON.stringify({ ok: true, closed: true }); } + return JSON.stringify({ ok: true, closed: false }); + } + return JSON.stringify({ ok: false, error: "bad-mode" }); +} +""".replace("REACTED_SOURCE", json.dumps("^" + rx._REACTION + "(, " + rx._REACTION + ")*$")) + + +def jxa(request: dict) -> dict: + try: + r = subprocess.run(["/usr/bin/osascript", "-l", "JavaScript", "-e", JXA], + input=json.dumps(request, ensure_ascii=False).encode(), + capture_output=True, timeout=rx.JXA_TIMEOUT) + except subprocess.TimeoutExpired: + return {"ok": False, "error": "timeout"} + if r.returncode != 0: + return {"ok": False, "error": "osascript"} + try: + answer = json.loads(r.stdout) + except ValueError: + return {"ok": False, "error": "osascript"} + return answer if isinstance(answer, dict) else {"ok": False, "error": "osascript"} + + +def read_text(stream) -> str: + raw = stream.buffer.read(MAX_BYTES + 1) + if len(raw) > MAX_BYTES: + raise Stop("too-long", EX_USAGE, f"replies are limited to {MAX_CHARS} characters; nothing was sent") + try: + text = raw.decode("utf-8").strip() + except UnicodeDecodeError: + raise Stop("usage", EX_USAGE, "the reply is not valid UTF-8; nothing was sent") + if not text: + raise Stop("usage", EX_USAGE, "the reply is empty; nothing was sent") + if len(text) > MAX_CHARS: + raise Stop("too-long", EX_USAGE, f"replies are limited to {MAX_CHARS} characters; nothing was sent") + return text + + +def _attachments(con, rowid: int) -> list: + cols = {r[1] for r in con.execute("PRAGMA table_info(attachment)")} + sticker = "a.is_sticker" if "is_sticker" in cols else "0" + rows = con.execute( + f"""SELECT a.mime_type, a.transfer_name, {sticker} AS sticker + FROM message_attachment_join j JOIN attachment a ON a.ROWID = j.attachment_id + WHERE j.message_id = ?""", (rowid,)).fetchall() + return [r for r in rows if not str(r["transfer_name"] or "").endswith(".pluginPayloadAttachment")] + + +def resolve_picture(con, imsg, row, self_handles: list) -> dict: + """A message of exactly one picture and no text. Its label is only "Includes + picture" and a time, and the reply view can name nothing more, so a run + refuses whenever that does not pick out one bubble.""" + if rx.norm(imsg.message_text(row)): + raise Stop("unsupported", EX_UNSUPPORTED, "replying to a picture with a caption is not supported yet") + if row["style"] == 43: + raise Stop("unsupported", EX_UNSUPPORTED, "groups are not supported") + files = _attachments(con, row["ROWID"]) + if len(files) != 1: + raise Stop("unsupported", EX_UNSUPPORTED, "replying to a message with several attachments is not supported yet") + if files[0]["sticker"] or not str(files[0]["mime_type"] or "").lower().startswith("image/"): + raise Stop("unsupported", EX_UNSUPPORTED, "only pictures can be replied to, not other attachments") + ids = imsg.chat_cluster_ids(con, row["chat_identifier"]) + chats = [r[0] for r in con.execute( + f"SELECT ROWID FROM chat WHERE chat_identifier IN ({','.join('?' * len(ids))})", ids)] + guids = [row["guid"]] + from_me = bool(row["is_from_me"]) + date, rowid = row["date"], row["ROWID"] + self_thread = row["chat_identifier"] in self_handles + def picture(r): + files = _attachments(con, r["ROWID"]) + return not rx.norm(imsg.message_text(r)) and len(files) == 1 and not files[0]["sticker"] and str(files[0]["mime_type"] or "").lower().startswith("image/") + if self_thread: + twins = [t for t in rx.bubbles(con, chats, "m.cache_has_attachments AND ABS(m.date - ?) < ?", + "m.is_from_me DESC, m.date, m.ROWID", (date, rx.SELF_TWIN_NS)) if picture(t)] + guids = list(dict.fromkeys([row["guid"]] + [t["guid"] for t in twins])) + sent = next((t for t in twins if t["is_from_me"]), None) + if sent: + from_me, date, rowid = True, sent["date"], sent["ROWID"] + minute = rx.hhmm(date) + day = 86400 * 10**9 + ordinal = scanned = 0 + for r in rx.bubbles(con, chats, "m.is_from_me = ? AND m.cache_has_attachments", f"m.date, m.ROWID LIMIT {rx.LATER_SCAN}", (int(from_me),)): + scanned += 1 + if r["guid"] in guids or rx.hhmm(r["date"]) != minute or not picture(r): + continue + if abs(r["date"] - date) < day: + raise Stop("ambiguous", EX_TEMPFAIL, "two pictures were sent in that minute and Messages labels them alike; nothing was sent") + if (r["date"], r["ROWID"]) > (date, rowid): + ordinal += 1 + if scanned >= rx.LATER_SCAN: + raise Stop("ambiguous", EX_TEMPFAIL, "too many pictures on this side to rule out a look-alike; nothing was sent") + newest = None + for r in rx.bubbles(con, chats, "NOT m.cache_has_attachments AND m.balloon_bundle_id IS NULL", + "m.date DESC, m.ROWID DESC LIMIT 50"): + body = rx.norm(imsg.message_text(r)) + if body and not rx.LINK.search(body): + newest = {"text": body, "from_me": bool(r["is_from_me"]), "hhmm": rx.hhmm(r["date"])} + break + return {"guids": guids, "chats": chats, "text": PICTURE_LABEL, "from_me": from_me, "hhmm": minute, + "ordinal": ordinal, "chat": row["chat_identifier"], "self_thread": self_thread, "picture": True, + "newest": newest or {"text": PICTURE_LABEL, "from_me": from_me, "hhmm": minute}} + + +def resolve(con, imsg, guid: str, self_handles: list) -> dict: + row = con.execute( + f"""SELECT {rx.MSG_COLS}, c.chat_identifier, c.style FROM message m + JOIN chat_message_join j ON j.message_id = m.ROWID + JOIN chat c ON c.ROWID = j.chat_id WHERE m.guid = ?""", (guid,)).fetchone() + if not row: + raise Stop("no-such-message", EX_UNSUPPORTED, "no message with that GUID") + if row["style"] == 43: + raise Stop("unsupported", EX_UNSUPPORTED, "replying in a group is not supported yet") + if row["item_type"] != 0 or (row["associated_message_type"] or 0) in range(2000, 4000): + raise Stop("unsupported", EX_UNSUPPORTED, "that row is not a message bubble") + if row["balloon_bundle_id"]: + raise Stop("unsupported", EX_UNSUPPORTED, "replying to a link or app card is not supported yet") + if row["cache_has_attachments"]: + return resolve_picture(con, imsg, row, self_handles) + return rx.resolve(con, imsg, guid, self_handles) + + +def threads(con, target: dict) -> set: + """What a reply to this target may name. A reply to a reply names the thread's + first message, so the target's own originator counts too (measured on 27.2).""" + guids = list(target["guids"]) + marks = ",".join("?" * len(guids)) + roots = [r[0] for r in con.execute(f"SELECT thread_originator_guid FROM message WHERE guid IN ({marks})", guids) if r[0]] + return set(guids) | set(roots) + + +def find_reply(con, imsg, expected: set, text: str, since: int): + """Your row with this text in the target's thread. Not filtered by chat: a reply + can land under another address of the same person (seen in a self-thread).""" + marks = ",".join("?" * len(expected)) + want = rx.norm(text) + for row in con.execute( + f"""SELECT m.ROWID, m.guid, m.text, m.attributedBody, m.error FROM message m + WHERE m.is_from_me = 1 AND m.date >= ? AND m.thread_originator_guid IN ({marks}) + ORDER BY m.ROWID DESC""", (since, *expected)): + if rx.norm(imsg.message_text(row)) == want: + return row + return None + + +def confirm(con, imsg, expected: set, text: str, since: int, pause=time.sleep) -> dict: + for _ in range(int(CONFIRM_SECONDS * 2)): + row = find_reply(con, imsg, expected, text, since) + if row is not None: + if row["error"]: + raise Stop("failed", EX_UNCONFIRMED, "Messages could not deliver the reply; check it in Messages", error=row["error"]) + return {"ok": True, "result": "sent"} + pause(0.5) + raise Stop("unconfirmed", EX_UNCONFIRMED, + "the reply was submitted but Messages did not record it yet; check before sending it again") + + +def confirm_safely(*args): + try: + return confirm(*args) + except Stop: + raise + except Exception: + raise Stop("unconfirmed", EX_UNCONFIRMED, "Return may have been pressed; check Messages before sending again") + + +NOT_SENT = { + "draft": "this chat has a draft in Messages on the Mac; send or clear it first. Nothing was sent", + "no-reply-view": "the reply view did not open; nothing was sent", + "not-front": "Messages was not in front; nothing was sent", + "reply-elsewhere": "the reply view opened on a different message; nothing was sent", + "fill-failed": "the reply could not be filled in; nothing was sent", + "focus-moved": "focus moved away from the reply; nothing was sent", + "wrong-conversation": "Messages did not verify the recipient; nothing was sent", +} + + +def reply(con, imsg, target: dict, text: str, ui, run=jxa, perform=None, pause=time.sleep) -> dict: + perform = perform or rx.perform + why = rx.system_events_consent() + if why: + raise Stop("consent", EX_NOPERM, why) + why = ui.ensure_messages() + if why: + raise Stop("unavailable", EX_UNAVAILABLE, why) + why = ui.accessibility() + if why: + raise Stop("accessibility", EX_NOPERM, why) + prev = ui.frontmost() + started = time.monotonic() + opened = False + try: + why = ui.select_chat(target["chat"]) + if why: + raise Stop("unavailable", EX_TEMPFAIL, why) + if run is jxa: + rx.clear_stale_picker() + state = run({"mode": "composer"}) + if state.get("inReply"): # left open by a run that died + run({"mode": "close"}) + pause(0.8) + state = run({"mode": "composer"}) + if not state.get("ok"): + raise Stop("unavailable", EX_TEMPFAIL, "the conversation did not open in Messages; nothing was sent") + if state.get("length", 0) > 0 or state.get("inReply"): + raise Stop("draft", EX_TEMPFAIL, NOT_SENT["draft"]) + expected = threads(con, target) + since = rx.apple_now() + pressed = perform(target, REPLY_ACTION, deadline=started + rx.RUN_LIMIT) + if not pressed.get("ok") and pressed.get("error") not in rx.MAYBE_PERFORMED: + if pressed.get("error") in ("not-rendered", "unsettled"): + raise Stop(pressed["error"], EX_TEMPFAIL, "Messages could not find that message on screen; nothing was sent") + raise rx.failure(pressed, REPLY_ACTION) + opened = True + chat_names = [target["chat"]] + name = imsg.name_for(target["chat"]) if hasattr(imsg, "name_for") else None + if name: chat_names.append(name) + try: + sent = run({"chatNames": chat_names, "mode": "send", "body": text, "text": target["text"], "hhmm": target["hhmm"], "polls": OPEN_POLLS}) + except Exception: + return confirm_safely(con, imsg, expected, text, since, pause) + if not sent.get("ok"): + code = sent.get("error", "error") + if code not in NOT_SENT: + # osascript died or timed out, possibly after Return. Never resend. + return confirm_safely(con, imsg, expected, text, since, pause) + raise Stop(code, EX_TEMPFAIL, NOT_SENT.get(code, "the reply could not be sent; nothing was sent")) + result = confirm_safely(con, imsg, expected, text, since, pause) + result["seconds"] = round(time.monotonic() - started, 1) + return result + finally: + try: + if opened: + run({"mode": "close"}) + pause(0.4) + ui.restore_front(prev) + except Exception: + pass # Cleanup cannot change a confirmed or uncertain send outcome. + + +def log_outcome(code: str, exit_code: int, seconds=None) -> None: + """When, the outcome code and the exit status. Never a GUID or any text.""" + try: + if os.path.exists(LOG) and os.path.getsize(LOG) > 65536: + os.replace(LOG, LOG + ".1") + line = f"{dt.datetime.now(dt.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')} {code} exit={exit_code}" + if seconds is not None: + line += f" {seconds}s" + with open(LOG, "a") as f: + f.write(line + "\n") + except OSError: + pass + + +def emit(result) -> int: + if isinstance(result, Stop): + log_outcome(str(result.result.get("code", "?")), result.exit_code) + if result.why: + sys.stderr.write(f"imsg-reply: {result.why}\n") + print(json.dumps(result.result, ensure_ascii=False)) + return result.exit_code + log_outcome(str(result.get("result", "ok")), 0, result.get("seconds")) + print(json.dumps(result, ensure_ascii=False)) + return 0 + + +def main(argv=None) -> int: + p = argparse.ArgumentParser(prog="imsg-reply", description="Send one native reply. Dry run unless --yes.") + p.add_argument("--guid", required=True, help="the message's GUID (message.guid)") + p.add_argument("--text-stdin", action="store_true", required=True, help="read the reply from stdin") + p.add_argument("--yes", action="store_true", help="act; without it nothing on the Mac changes") + try: + a = p.parse_args(argv) + except SystemExit as e: + return EX_USAGE if e.code else 0 + if not re.fullmatch(r"[A-Za-z0-9:/_-]{1,128}", a.guid): + return emit(Stop("usage", EX_USAGE, "that is not a message GUID")) + try: + text = read_text(sys.stdin) + imsg = rx.sibling("imsg") + try: + self_handles = rx.sibling("imsg-send").detect_self_handles() + except Exception: + self_handles = [] + con = sqlite3.connect(f"file:{imsg.DB_PATH}?mode=ro", uri=True, timeout=5) + con.row_factory = sqlite3.Row + target = resolve(con, imsg, a.guid, self_handles) + if not a.yes: + return emit({"ok": True, "result": "dry-run", "target": "picture" if target.get("picture") else "text"}) + ui = rx.sibling("imsg-read") + with ui.messages_ui_lock(): + return emit(reply(con, imsg, target, text, ui)) + except Stop as stop: + return emit(stop) + except TimeoutError as exc: + return emit(Stop("busy", EX_TEMPFAIL, str(exc))) + except (sqlite3.Error, OSError) as exc: + return emit(Stop("unavailable", EX_UNAVAILABLE, str(exc))) + except Exception as exc: + return emit(Stop("error", EX_SOFTWARE, f"{type(exc).__name__}: {exc}")) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/bridge/mac/install.sh b/bridge/mac/install.sh index e2780a0b..a909f5a4 100755 --- a/bridge/mac/install.sh +++ b/bridge/mac/install.sh @@ -21,7 +21,7 @@ dest="$HOME/.blip/bin" # denial (#36, Astra #11). check=1; for a in "$@"; do [[ $a == --no-check ]] && check=0; done mkdir -p "$dest" -for t in imsg imsg-send imsg-read imsg-react contacts contact-repair.js contact-save contact-save.js tcc-check blip-check blip-dispatch calling_codes.py read_state.py findmy_city.py; do +for t in imsg imsg-send imsg-read imsg-react imsg-reply contacts contact-repair.js contact-save contact-save.js tcc-check blip-check blip-dispatch calling_codes.py read_state.py findmy_city.py; do if [[ -f "$here/$t" ]]; then install -m 0755 "$here/$t" "$dest/$t" else diff --git a/bridge/mac/test_reply.py b/bridge/mac/test_reply.py new file mode 100644 index 00000000..e8c63c2c --- /dev/null +++ b/bridge/mac/test_reply.py @@ -0,0 +1,336 @@ +#!/usr/bin/env python3 +"""imsg-reply on a synthetic chat.db with a scripted Messages; never opens Messages.""" +import contextlib +import io +import os +import sqlite3 +import tempfile +import types +import unittest +from importlib.machinery import SourceFileLoader +from importlib.util import module_from_spec, spec_from_loader +from pathlib import Path +from unittest.mock import patch + +_loader = SourceFileLoader("test_reply_tool", str(Path(__file__).with_name("imsg-reply"))) +_spec = spec_from_loader(_loader.name, _loader) +tool = module_from_spec(_spec) +_loader.exec_module(tool) +rx = tool.rx + +SCHEMA = """ +CREATE TABLE chat (ROWID INTEGER PRIMARY KEY, chat_identifier TEXT, style INT); +CREATE TABLE message (ROWID INTEGER PRIMARY KEY, guid TEXT, text TEXT, attributedBody BLOB, is_from_me INT, date INT, + item_type INT DEFAULT 0, associated_message_type INT DEFAULT 0, balloon_bundle_id TEXT, payload_data BLOB, + cache_has_attachments INT DEFAULT 0, thread_originator_guid TEXT, error INT DEFAULT 0); +CREATE TABLE chat_message_join (chat_id INT, message_id INT); +CREATE TABLE chat_recoverable_message_join (chat_id INT, message_id INT); +CREATE TABLE attachment (ROWID INTEGER PRIMARY KEY, mime_type TEXT, transfer_name TEXT, is_sticker INT DEFAULT 0); +CREATE TABLE message_attachment_join (message_id INT, attachment_id INT); +""" +BASE = 800_000_000 * 10**9 +MINUTE = 60 * 10**9 +imsg = types.SimpleNamespace(message_text=lambda r: r["text"], chat_cluster_ids=lambda con, chat: [chat]) + + +class Db: + def __init__(self): + self.con = sqlite3.connect(":memory:") + self.con.row_factory = sqlite3.Row + self.con.executescript(SCHEMA) + self.con.execute("INSERT INTO chat VALUES (1, '+15551234567', 45)") + self.con.execute("INSERT INTO chat VALUES (2, 'group-1', 43)") + self.con.execute("INSERT INTO chat VALUES (3, 'alias@example.com', 45)") + self.n = 0 + + def msg(self, guid, text="", me=1, date=BASE, chat=1, files=(), **extra): + self.n += 1 + cols = {"ROWID": self.n, "guid": guid, "text": text, "is_from_me": me, "date": date, + "cache_has_attachments": int(bool(files)), **extra} + self.con.execute(f"INSERT INTO message ({','.join(cols)}) VALUES ({','.join('?' * len(cols))})", tuple(cols.values())) + self.con.execute("INSERT INTO chat_message_join VALUES (?, ?)", (chat, self.n)) + for mime, name, sticker in files: + cur = self.con.execute("INSERT INTO attachment (mime_type, transfer_name, is_sticker) VALUES (?, ?, ?)", (mime, name, sticker)) + self.con.execute("INSERT INTO message_attachment_join VALUES (?, ?)", (self.n, cur.lastrowid)) + return guid + + +PNG = ("image/png", "a.png", 0) + + +class Targets(unittest.TestCase): + def refused(self, db, guid, code): + with self.assertRaises(rx.Stop) as ctx: + tool.resolve(db.con, imsg, guid, []) + self.assertEqual(ctx.exception.result["code"], code) + + def test_text_and_a_single_picture_are_targets(self): + db = Db() + db.msg("T", "hello") + db.msg("P", "", files=[PNG], date=BASE + MINUTE) + self.assertEqual(tool.resolve(db.con, imsg, "T", [])["text"], "hello") + picture = tool.resolve(db.con, imsg, "P", []) + self.assertEqual((picture["text"], picture["picture"], picture["ordinal"]), (tool.PICTURE_LABEL, True, 0)) + + def test_what_is_unproven_is_refused(self): + db = Db() + db.msg("G", "hi", chat=2) + db.msg("C", "caption", files=[PNG]) + db.msg("M", "", files=[PNG, ("image/jpeg", "b.jpg", 0)], date=BASE + MINUTE) + db.msg("V", "", files=[("video/mp4", "a.mp4", 0)], date=BASE + 2 * MINUTE) + db.msg("S", "", files=[("image/png", "s.png", 1)], date=BASE + 3 * MINUTE) + db.msg("L", "see https://example.com", date=BASE + 4 * MINUTE) + db.msg("A", "card", balloon_bundle_id="x.URLBalloonProvider", date=BASE + 5 * MINUTE) + for guid in "GCMVSLA": + self.refused(db, guid, "unsupported") + self.refused(db, "nope", "no-such-message") + + def test_other_attachment_types_never_shift_picture_ordinal(self): + for files in ([('video/mp4','v.mp4',0)], [('image/png','s.png',1)], [PNG,PNG]): + db=Db(); db.msg("P",files=[PNG]); db.msg("OTHER",files=files,date=BASE+1) + self.assertEqual(tool.resolve(db.con,imsg,"P",[])["ordinal"],0) + + def test_group_picture_is_refused(self): + db=Db(); db.msg("P",files=[PNG],chat=2) + self.refused(db,"P","unsupported") + + def test_two_pictures_in_a_minute_are_refused_not_guessed(self): + db = Db() + db.msg("P1", "", files=[PNG], date=BASE) + db.msg("P2", "", files=[PNG], date=BASE + 10**9) + self.refused(db, "P1", "ambiguous") + self.refused(db, "P2", "ambiguous") + + def test_the_other_side_does_not_count_as_a_look_alike(self): + db = Db() + db.msg("P1", "", files=[PNG], date=BASE) + db.msg("P2", "", files=[PNG], date=BASE + 10**9, me=0) + self.assertTrue(tool.resolve(db.con, imsg, "P1", [])["picture"]) + + def test_the_same_minute_on_a_later_day_is_counted_from_the_bottom(self): + db = Db() + db.msg("P1", "", files=[PNG], date=BASE) + db.msg("P2", "", files=[PNG], date=BASE + 86400 * 2 * 10**9) + self.assertEqual(tool.resolve(db.con, imsg, "P1", [])["ordinal"], 1) + + def test_a_self_thread_picture_aims_at_the_sent_row_and_knows_its_echo(self): + db = Db() + db.msg("ECHO", "", files=[PNG], me=0, date=BASE + 10**9) + db.msg("SENT", "", files=[PNG], me=1, date=BASE) + target = tool.resolve(db.con, imsg, "ECHO", ["+15551234567"]) + self.assertTrue(target["from_me"]) + self.assertEqual(set(target["guids"]), {"ECHO", "SENT"}) + + +class Ui: + def __init__(self, select=""): + self.select = select + self.restored = [] + + def ensure_messages(self): return "" + def accessibility(self): return "" + def frontmost(self): return "Terminal" + def select_chat(self, chat): return self.select + def restore_front(self, prev): self.restored.append(prev) + + +class Script: + """Messages as far as the tool can see it. `land` writes the reply row.""" + def __init__(self, db=None, composer=None, send=None, land=None): + self.db, self.composer, self.answer, self.land = db, composer, send, land + self.calls = [] + self.presses = 0 + + def run(self, q): + self.calls.append(q["mode"]) + if q["mode"] == "composer": + return self.composer.pop(0) if self.composer else {"ok": True, "length": 0, "inReply": False} + if q["mode"] == "send": + self.sent = q + if self.land: + self.land() + return self.answer or {"ok": True} + return {"ok": True} + + def perform(self, target, action, deadline=None): + self.presses += 1 + self.action = action + return {"ok": True} + + +def target_of(db, guid): + return tool.resolve(db.con, imsg, guid, []) + + +class Run(unittest.TestCase): + def setUp(self): + p = patch.object(rx, "system_events_consent", return_value="") + p.start() + self.addCleanup(p.stop) + now = patch.object(rx, "apple_now", return_value=BASE) + now.start() + self.addCleanup(now.stop) + + def go(self, db, script, ui=None, guid="T", text="the reply"): + return tool.reply(db.con, imsg, target_of(db, guid), text, ui or Ui(), run=script.run, + perform=script.perform, pause=lambda _: None) + + def land(self, db, orig, text="the reply", chat=1, error=0): + return lambda: db.msg("R", text, date=BASE + 5 * 10**9, chat=chat, thread_originator_guid=orig, error=error) + + def test_prefix_neighbour_does_not_crash_shared_reaction_resolution(self): + db = Db() + db.msg("T", "ok") + db.msg("N", "ok, see you then", date=BASE+1) + self.assertEqual(target_of(db, "T")["text"], "ok") + + def test_sent_when_chat_db_names_the_target(self): + db = Db(); db.msg("T", "hello") + ui = Ui() + s = Script(land=self.land(db, "T")) + self.assertEqual(self.go(db, s, ui)["result"], "sent") + self.assertEqual((s.presses, s.action), (1, "Reply…")) + self.assertEqual(s.sent["body"], "the reply") + self.assertEqual(s.calls[-1], "close") + self.assertEqual(ui.restored, ["Terminal"]) + + def test_a_reply_under_another_address_of_the_same_person_still_counts(self): + db = Db(); db.msg("T", "hello") + self.assertEqual(self.go(db, Script(land=self.land(db, "T", chat=3)))["result"], "sent") + + def test_a_reply_to_a_reply_names_the_start_of_the_thread(self): + db = Db(); db.msg("ROOT", "first"); db.msg("T", "hello", date=BASE + MINUTE, thread_originator_guid="ROOT") + self.assertEqual(self.go(db, Script(land=self.land(db, "ROOT")))["result"], "sent") + + def test_a_draft_stops_the_run_before_anything_is_pressed(self): + db = Db(); db.msg("T", "hello") + s = Script(composer=[{"ok": True, "length": 5, "inReply": False}]) + with self.assertRaises(rx.Stop) as ctx: + self.go(db, s) + self.assertEqual(ctx.exception.result["code"], "draft") + self.assertEqual(ctx.exception.exit_code, tool.EX_TEMPFAIL) + self.assertEqual(s.presses, 0) + self.assertNotIn("send", s.calls) + + def test_a_reply_view_left_open_is_closed_first(self): + db = Db(); db.msg("T", "hello") + s = Script(composer=[{"ok": True, "length": 0, "inReply": True}, {"ok": True, "length": 0, "inReply": False}], + land=self.land(db, "T")) + self.assertEqual(self.go(db, s)["result"], "sent") + self.assertEqual(s.calls[:3], ["composer", "close", "composer"]) + + def test_a_locked_mac_is_said_plainly_and_nothing_is_pressed(self): + db = Db(); db.msg("T", "hello") + s = Script() + with self.assertRaises(rx.Stop) as ctx: + self.go(db, s, Ui(select="the Mac's screen is locked")) + self.assertIn("locked", ctx.exception.why) + self.assertEqual(ctx.exception.exit_code, tool.EX_TEMPFAIL) + self.assertEqual(s.presses, 0) + + def test_nothing_sent_is_a_retryable_failure_and_the_view_closes(self): + for code in tool.NOT_SENT: + db = Db(); db.msg("T", "hello") + s = Script(send={"ok": False, "error": code}) + with self.assertRaises(rx.Stop) as ctx: + self.go(db, s) + self.assertEqual((ctx.exception.result["code"], ctx.exception.exit_code), (code, tool.EX_TEMPFAIL)) + self.assertEqual(s.calls[-1], "close") + + def test_slow_field_clear_confirms_instead_of_inviting_resend(self): + db = Db(); db.msg("T", "hello") + for error in ("not-sent", "unconfirmed"): + s = Script(send={"ok":False,"error":error}, land=self.land(db,"T")) + self.assertEqual(self.go(db,s)["result"], "sent") + + def test_confirmation_errors_are_always_uncertain(self): + for error in (sqlite3.OperationalError("unavailable"), OSError("unavailable")): + db = Db(); db.msg("T", "hello") + with patch.object(tool, "find_reply", side_effect=error): + with self.assertRaises(rx.Stop) as ctx: self.go(db, Script()) + self.assertEqual(ctx.exception.exit_code, 76) + + def test_old_or_incoming_rows_cannot_confirm(self): + for me, date in ((0, BASE+1), (1, BASE-1)): + db = Db(); db.msg("T", "hello") + db.msg("R", "the reply", me=me,date=date,thread_originator_guid="T") + self.assertIsNone(tool.find_reply(db.con,imsg,{"T"},"the reply",BASE)) + + def test_no_row_after_return_is_unconfirmed_and_never_says_resend(self): + db = Db(); db.msg("T", "hello") + s = Script() + with patch.object(tool, "CONFIRM_SECONDS", 0.5), self.assertRaises(rx.Stop) as ctx: + tool.reply(db.con, imsg, target_of(db, "T"), "the reply", Ui(), run=s.run, perform=s.perform, + pause=lambda _: None) + self.assertEqual((ctx.exception.result["code"], ctx.exception.exit_code), ("unconfirmed", tool.EX_UNCONFIRMED)) + + def test_a_script_that_dies_during_send_is_checked_never_retried(self): + db = Db(); db.msg("T", "hello") + s = Script(send={"ok": False, "error": "timeout"}, land=self.land(db, "T")) + self.assertEqual(self.go(db, s)["result"], "sent") + self.assertEqual(s.calls.count("send"), 1) + + def test_a_row_with_the_wrong_thread_or_text_does_not_confirm(self): + db = Db(); db.msg("T", "hello"); db.msg("OTHER", "other", date=BASE + MINUTE) + s = Script(land=self.land(db, "OTHER")) + with patch.object(tool, "CONFIRM_SECONDS", 0.5), self.assertRaises(rx.Stop) as ctx: + tool.reply(db.con, imsg, target_of(db, "T"), "the reply", Ui(), run=s.run, perform=s.perform, pause=lambda _: None) + self.assertEqual(ctx.exception.result["code"], "unconfirmed") + + def test_a_delivery_error_is_reported_without_inviting_a_resend(self): + db = Db(); db.msg("T", "hello") + with self.assertRaises(rx.Stop) as ctx: + self.go(db, Script(land=self.land(db, "T", error=22))) + self.assertEqual((ctx.exception.result["code"], ctx.exception.exit_code), ("failed", tool.EX_UNCONFIRMED)) + + def test_a_bubble_not_found_says_nothing_was_sent(self): + db = Db(); db.msg("T", "hello") + s = Script() + s.perform = lambda target, action, deadline=None: {"ok": False, "error": "not-rendered"} + with self.assertRaises(rx.Stop) as ctx: + self.go(db, s) + self.assertIn("nothing was sent", ctx.exception.why) + self.assertNotIn("send", s.calls) + + +class Text(unittest.TestCase): + def read(self, raw): + return tool.read_text(types.SimpleNamespace(buffer=io.BytesIO(raw))) + + def test_limits(self): + self.assertEqual(self.read(" two\nlines 👍\n".encode()), "two\nlines 👍") + for bad in (b"", b" \n", b"\xff\xfe", b"x" * (tool.MAX_BYTES + 1), b"x" * (tool.MAX_CHARS + 1)): + with self.assertRaises(rx.Stop): + self.read(bad) + + def test_the_log_never_holds_a_guid_or_text(self): + with tempfile.TemporaryDirectory() as d, patch.object(tool, "LOG", os.path.join(d, "reply.log")): + with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): + tool.emit({"ok": True, "result": "sent", "seconds": 1.2}) + tool.emit(rx.Stop("draft", 75, "a draft")) + log = Path(tool.LOG).read_text() + self.assertIn("sent exit=0 1.2s", log) + self.assertIn("draft exit=75", log) + self.assertNotIn("a draft", log) + + def test_dry_run_never_touches_the_ui(self): + db=Db(); db.msg("T","hello") + fake_imsg=types.SimpleNamespace(DB_PATH="synthetic",message_text=imsg.message_text,chat_cluster_ids=imsg.chat_cluster_ids) + def sibling(name): + if name == "imsg": return fake_imsg + if name == "imsg-send": return types.SimpleNamespace(detect_self_handles=lambda: []) + raise AssertionError("dry run touched UI") + with patch.object(rx,"sibling",side_effect=sibling), patch.object(tool.sqlite3,"connect",return_value=db.con), patch.object(tool.sys,"stdin",types.SimpleNamespace(buffer=io.BytesIO(b"synthetic reply"))), patch.object(tool,"emit",side_effect=lambda r: 0 if not isinstance(r,rx.Stop) else r.exit_code): + self.assertEqual(tool.main(["--guid","T","--text-stdin"]),0) + + def test_invalid_guid_is_refused_before_any_lookup(self): + with patch.object(rx,"sibling",side_effect=AssertionError("must not load bridge")), patch.object(tool,"emit",side_effect=lambda r:r.exit_code): + self.assertEqual(tool.main(["--guid","bad;guid","--text-stdin"]),tool.EX_USAGE) + + def test_no_guid_no_tool(self): + with contextlib.redirect_stderr(io.StringIO()), contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(tool.main(["--text-stdin"]), tool.EX_USAGE) + + +if __name__ == "__main__": + unittest.main() diff --git a/bridge/mac/test_reply_enrich.py b/bridge/mac/test_reply_enrich.py new file mode 100644 index 00000000..a63d2eca --- /dev/null +++ b/bridge/mac/test_reply_enrich.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +"""Synthetic sticker rows only. Never opens the user's Messages database.""" +import math +import io +import plistlib +import sqlite3 +import tempfile +import unittest +from importlib.machinery import SourceFileLoader +from importlib.util import module_from_spec, spec_from_loader +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + + +def load_imsg(): + loader = SourceFileLoader("blip_reply_enrich_test", str(Path(__file__).with_name("imsg"))) + module = module_from_spec(spec_from_loader(loader.name, loader)) + loader.exec_module(module) + return module + + +T0 = 800_000_000_000_000_000 + + +class ReplyEnrichment(unittest.TestCase): + def setUp(self): + self.imsg = load_imsg() + self.con = sqlite3.connect(":memory:") + self.con.row_factory = sqlite3.Row + self.con.executescript(""" + CREATE TABLE message ( + ROWID INTEGER PRIMARY KEY, guid TEXT, date INTEGER, is_from_me INTEGER DEFAULT 0, + handle_id INTEGER DEFAULT 1, text TEXT, attributedBody BLOB, item_type INTEGER DEFAULT 0, + associated_message_type INTEGER DEFAULT 0, associated_message_guid TEXT, + associated_message_emoji TEXT, date_read INTEGER DEFAULT 0, + balloon_bundle_id TEXT, payload_data BLOB, thread_originator_guid TEXT, thread_originator_part TEXT, + expressive_send_style_id TEXT, is_audio_message INTEGER DEFAULT 0); + CREATE TABLE handle (ROWID INTEGER PRIMARY KEY, id TEXT); + CREATE TABLE attachment (ROWID INTEGER PRIMARY KEY, transfer_name TEXT, mime_type TEXT, + uti TEXT, total_bytes INTEGER, is_sticker INTEGER DEFAULT 0, sticker_user_info BLOB, + attribution_info BLOB, emoji_image_short_description TEXT); + CREATE TABLE message_attachment_join (message_id INTEGER, attachment_id INTEGER); + CREATE TABLE chat_recoverable_message_join (message_id INTEGER); + INSERT INTO handle VALUES (1, '+15551234567'); + """) + + def tearDown(self): + self.con.close() + + def message(self, guid, kind=0, target=None, date=T0, emoji=None, mine=False): + cur = self.con.execute("""INSERT INTO message + (guid, date, associated_message_type, associated_message_guid, associated_message_emoji, is_from_me) + VALUES (?, ?, ?, ?, ?, ?)""", (guid, date, kind, target, emoji, mine)) + return cur.lastrowid + + def attachment(self, mid, aid, sticker=True, name="sticker.png", mime="image/png", info=None): + self.con.execute("INSERT INTO attachment VALUES (?, ?, ?, NULL, 128, ?, ?, NULL, NULL)", + (aid, name, mime, sticker, plistlib.dumps(info) if info is not None else None)) + self.con.execute("INSERT INTO message_attachment_join VALUES (?, ?)", (mid, aid)) + + def enrich(self, ids): + marks = ",".join("?" for _ in ids) + rows = list(self.con.execute(f"""SELECT m.*, associated_message_type AS assoc_type, + associated_message_guid AS assoc_guid, thread_originator_guid AS orig_guid, + expressive_send_style_id AS style_id, thread_originator_part AS orig_part FROM {self.imsg.MESSAGE_SRC} m + WHERE ROWID IN ({marks}) ORDER BY date, ROWID""", ids)) + return self.imsg.enrich(self.con, rows, False) + + def test_reply_attachment_selects_originator_part(self): + p = self.message("P") + self.attachment(p, 1, sticker=False, name="one.png") + self.attachment(p, 2, sticker=False, name="two.png") + r = self.message("R") + self.con.execute("UPDATE message SET thread_originator_guid='P', thread_originator_part='1:0' WHERE ROWID=?", (r,)) + extra = self.enrich([r])[1] + self.assertEqual(extra[r]["reply_to"]["attachment"]["id"], "2") + + +if __name__ == "__main__": + unittest.main() diff --git a/bridge/mac/test_reply_jxa.py b/bridge/mac/test_reply_jxa.py new file mode 100644 index 00000000..21146d4c --- /dev/null +++ b/bridge/mac/test_reply_jxa.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""Execute shipped JXA guards in Node with a synthetic accessibility tree.""" +import json +import subprocess +import unittest +from pathlib import Path +from test_reply import tool + +class GuardTests(unittest.TestCase): + def test_recipient_sheet_and_reply_subtree_are_rechecked_before_return(self): + script = tool.JXA[:tool.JXA.index('function run(')] + harness = r""" +let scenario="", filled=false, pressed=0; +const ObjC={import(){}}, REACTED_SOURCE="^$"; +const field={value(v){if(arguments.length){filled=true; this.text=v;}return this.text||"";}, + attributes:{byName:n=>({value:()=>n==="AXIdentifier"?"messageBodyField":"Reply"})}}; +Object.defineProperty(field,"value",{get:()=>()=>field.text||"",set:v=>{filled=true;field.text=v;}}); +const transcript={description:()=>"Reply transcript",path:"window.reply"}; +const target={description:()=>"Me, target, 09:15",path:"window.reply.message"}; +const win={sheets:{get length(){return scenario==="sheet"?1:0}}, + name:()=>scenario==="recipient"&&filled?"Other":"Synthetic", + entireContents:()=>[transcript,target]}; +const proc={windows:[win],frontmost:()=>true,attributes:{byName:()=>({value:()=>field})}}; +const se={processes:{byName:()=>proc},keyCode:()=>{pressed++;field.text="";}}; +const Application=()=>se, Automation={getDisplayString:e=>e.path}; +function delay(){} +""" + checks = r""" +for (scenario of ["valid","sheet","recipient","outside"]) { + filled=false; pressed=0; field.text="";target.path=scenario==="outside"?"window.main.message":"window.reply.message"; + const r=send({polls:1,body:"synthetic reply",chatNames:["Synthetic"],text:"target",hhmm:"09:15"}); + if(pressed !== (scenario==="valid"?1:0)) throw Error(scenario+" pressed "+pressed); +} +console.log("ok"); +""" + # proc and se come from Application in the shipping script. + harness = harness.replace('const proc=', 'const fakeProc=').replace('const se=', 'const fakeSe=').replace('()=>proc}', '()=>fakeProc}').replace('()=>se, Automation','()=>fakeSe, Automation') + result = subprocess.run(["node", "-e", harness + script + checks], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, result.stderr) + +if __name__ == "__main__": + unittest.main() diff --git a/collector.ts b/collector.ts index e90cd53b..0dfe9469 100644 --- a/collector.ts +++ b/collector.ts @@ -93,7 +93,7 @@ export interface ImsgMessage { tapbacks?: Tapback[] | null; attachments?: AttachmentMeta[] | null; stickers?: import("./stickers").StickerOverlay[] | null; - reply_to?: { text: string; from_me: boolean } | null; + reply_to?: { text: string; from_me: boolean; attachment?: AttachmentMeta } | null; edited?: boolean | null; retracted?: boolean | null; effect?: string | null; diff --git a/docs/PRIVACY.md b/docs/PRIVACY.md index 5658566b..afd605b9 100644 --- a/docs/PRIVACY.md +++ b/docs/PRIVACY.md @@ -138,6 +138,8 @@ timestamp when `chat.db` changes; the client then fetches privately. Edit or unsend, see typing indicators. Those need Apple private APIs that Blip deliberately does not use. +Native replies are opt-in too (`replies=on`): the Mac's `~/.blip/reply.log` holds outcome codes and durations only. + Tapbacks are the opt-in exception (`tapbacks=on`): they are Messages' own actions, performed through Accessibility, not a private API. diff --git a/docs/SECURITY.md b/docs/SECURITY.md index 53672a56..af376455 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -63,6 +63,14 @@ back before success. The helper never writes Contacts databases directly and does not edit, merge, or delete existing cards. Contacts write permission is separate from Full Disk Access and may prompt on first save. +## Optional native replies + +`imsg-reply` sends a message through the same Accessibility route as tapbacks, so it +can reach the same people `imsg-send` can. The shim refuses it unless `replies=on` is +in `bridge.conf`, which is an opt-in, not a boundary against someone who already holds +the key. It acts only with `--yes`. The text stays on stdin, is set directly on the +reply field and never touches the clipboard. Its log holds outcome codes only. + ## Optional tapbacks The confined key can invoke `imsg-react`, the first bridge tool that *acts* diff --git a/docs/replies.md b/docs/replies.md new file mode 100644 index 00000000..c78768ab --- /dev/null +++ b/docs/replies.md @@ -0,0 +1,150 @@ +# Native replies + +Reply in the message menu, or `Ctrl+R` on a selected bubble, sends a real inline +reply: the same thing Messages sends, recorded in `chat.db` with +`thread_originator_guid`. It needs `replies=on` in `bridge.conf` and the Mac's +existing Accessibility and Automation grants. `Quote and reply` is still there, and +`Ctrl+R` falls back to it on anything a native reply refuses. + +Everything below was measured on macOS 27.2 in a self-thread (2026-11-07). If you +are about to investigate replies, read this first: these paths have been walked. + +## What works + +| Target | Result | +|---|---| +| Plain text in a 1:1 | Sent and confirmed | +| One picture, no caption, in a 1:1 | Sent and confirmed | +| A message that is already a reply | Sent; names the start of the thread | +| Multi-line text with emoji | Sent intact | + +A run takes 11 to 13 seconds and brings Messages to the front on the Mac, then +restores whichever app was there. + +## How the Mac tool works + +`bridge/mac/imsg-reply --guid GUID --text-stdin --yes`, in this order: + +1. `chat.db` decides the target and refuses anything unproven. +2. A draft in the composer stops the run, before anything is pressed. +3. The bubble search in `imsg-react` presses `Reply…` once. +4. The reply view must name the target. The field is filled through AX and read back. +5. Focus is rechecked, then Return is pressed. +6. `chat.db` must show your row with this text naming the target's thread. +7. The reply view is closed and the previous app comes back. + +| Exit | Meaning | What Blip does | +|---|---|---| +| 0 | Sent and confirmed | Reloads to the real row | +| 75 | Nothing was sent, including a locked Mac or a draft | Failed bubble, words and target back in the composer | +| 76 | Return was pressed but not confirmed, or Messages reported an error | Says so; **never hands the words back**, because a resend could duplicate | +| 65, 77 | Refused or no permission | Same as 75 | +| 69, 70, 255 | Lost transport or unexpected error | Check Messages; no automatic handback | + +Never retry after Return. The log, `~/.blip/reply.log`, holds the outcome code, exit +status and duration, never a GUID or text. + +## What we found in Messages + +**The reply view.** `Reply…` opens a view over the conversation: + +- The focused field is an `AXTextField` with `AXIdentifier` `messageBodyField` and + placeholder `Reply`. The main composer has the same identifier and a placeholder of + `iMessage`, so the placeholder is how to tell them apart. +- A group described `Reply transcript` holds the thread, with bubble labels that name + the target. That is the check before anything is typed. +- **It stays open after sending.** Escape closes it. +- **There is no send button and no confirm action on the field.** The only `send` + match is `Send Later`. Return via System Events is the only way, and it goes to + whichever app is in front, which is why focus is rechecked immediately before. + +**Drafts.** A draft in the composer moves into the reply field when the view opens, +and Escape hands it back intact. Filling the field replaces the draft, and sending +destroys it. So the tool refuses when one exists. A draft could be saved and restored +around the send, but that wasn't worth the risk of losing someone's words. + +**Never use the clipboard for the text.** Setting `AXValue` works, including line +breaks and emoji. Pasting would put the message on the Mac's clipboard, where any +clipboard sync can carry it to another machine, breaking the rule that message text +never reaches Linux disk or argv. + +**Bubble labels.** Times can be 12-hour, and the sender comes first: + +- Text: `Your iMessage, hello, 11:30 AM` +- A reply: `Your iMessage, Reply…, hello, 11:43 AM`. `parseLabel` strips the + `Reply…, ` so replies can be targeted. +- A picture: `Your iMessage, Includes picture, 11:52 AM`. That is all it says, so two + pictures from one side in one minute can't be told apart, and the tool refuses. +- A picture's live bubble is an `AXButton` under its row, not a group, which is why + the search takes a `picture` flag. +- In a self-thread each message draws two bubbles, `Maybe: , …` and + `Your iMessage, …`. + +**`chat.db`.** + +- `thread_originator_guid` is the message replied to, or **the start of the thread** + when replying to a reply, so confirmation has to accept the target's own originator. +- `thread_originator_part` looks like `0:0:23` for text and `0:0:1` for a picture. The + first number is the part. Blip assumes that in a message with several attachments it + is the attachment's index, falling back to the first. **That is unverified.** +- **A reply can land under a different chat row for the same person.** In the + self-thread they went to the phone-number chat while the message replied to was + under the email chat. Confirm by thread and text, never by chat. Filtering by chat + made working replies look lost during the investigation. + +## Dead ends + +- **AppleScript has no reply.** `send` takes no reply target. +- **Private APIs.** Replying through `IMCore`, as BlueBubbles' Private API does, + requires SIP off as far as we know. Not considered for that reason. +- **`Edit ▸ Reply to Message…`.** Acts on the selected message, and selecting a bubble + from outside is an unsolved problem. The bubble's action needs no selection. +- **Testing with the Mac locked.** Accessibility sees no windows, and screen capture + fails too. Nothing can be probed until someone unlocks it. +- **Testing on another Mac over ssh.** Needs Full Disk Access and Accessibility for + `/usr/libexec/sshd-keygen-wrapper`, which only a person can grant. + +## Traps when probing + +- JXA calls a top-level `function run` by itself. Calling `run()` as well makes it read + stdin twice, and the second read fails with `JSON Parse error: Unexpected EOF`. +- Path filters on `ConversationList` no longer exclude the sidebar on macOS 27, so a + loose label filter prints other people's conversation previews. Filter on bubbles + offering `Reply…`, and keep to the self-thread. +- Never run `entireContents` while the emoji catalogue is open. See `reactions.md`. +- `imsg-send` takes attachments with `--file-stdin --file-bytes N --name X`. + +## Not built, and what it would take + +Each of these is refused today, in `canReply` (`message-actions.ts`) and in +`imsg-reply`. Change both together. Test in a self-thread first, except groups, which +need a real group you own and everyone's consent. + +| Feature | What to measure first | +|---|---| +| Groups | Bubble labels with several senders, and how to pick the side. The route should be the same. | +| Several pictures in a message | The label, whether each picture has its own `Reply…`, and how the part number maps | +| Picture with a caption | The label, and which part `Reply…` answers | +| Videos, files, audio | Each label | +| Link and app cards | The label is the page's title, not the text, so `chat.db` can't predict it | +| Two pictures in one minute | Whether bubble order can be trusted, and how to check it in the reply view | +| Replying with an attachment | How to attach in the reply view without the clipboard | +| Thread view like Apple's | Blip only; `thread_originator_guid` has what it needs | +| Replying while a draft exists | Save and restore around the send | +| Replies with the Mac locked | Not possible through Accessibility | + +The reply context above a bubble works whether or not the Mac is locked, because it +only reads `chat.db`. + +## Testing + +`bridge/mac/test_reply.py` runs on a synthetic `chat.db` with a scripted Messages. +`message-actions.test.ts`, `thread.test.ts` and `tapbacks.test.ts` cover the Linux side. `reply-ax.test.ts` executes the shipped JXA against a synthetic AX tree and checks that draft, recipient and target refusals happen before any field write. `reply-session.test.ts` executes the QML completion handler, including preserving a newer draft and ignoring another conversation. Removing any of those four guards fails the tests. +For a live run, target only your own self-thread: + + printf 'test' | ~/.blip/bin/imsg-reply --guid GUID --text-stdin # dry run + printf 'test' | ~/.blip/bin/imsg-reply --guid GUID --text-stdin --yes + +This split uses the native AX helpers and bounded stale-picker preflight already on main from the reactions feature. Its only changes to `imsg-react` add picture-bubble discovery and carry that flag into the native action request. + +Recipient verification fails closed: immediately before Return the Messages window must have no sheet and its title must exactly match the target address or contact name, in addition to the target label and time in the reply transcript. This title shape needs gateway validation; unsupported titles refuse the send. Return followed by slow field clearing, a confirm database error, or lost ssh is unconfirmed: check Messages before sending again. Reply jobs and their result bubbles are owned by BarWidget and survive view closure. diff --git a/message-actions.test.ts b/message-actions.test.ts index 51279f47..8f14cce9 100644 --- a/message-actions.test.ts +++ b/message-actions.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "bun:test"; -import { attachmentActions, bubbleIndexByGuid, quotedDraft, quoteText, saveableMime } from "./message-actions"; +import { attachmentActions, bubbleIndexByGuid, canReply, quotedDraft, quoteText, replyTarget, saveableMime } from "./message-actions"; test("quote an older message without replacing or normalizing the draft", () => { expect(quotedDraft({ text: "An older\nmessage" }, "My reply\n in progress")) @@ -49,3 +49,35 @@ test("a right-clicked picture offers open, copy and save; a document open and sa const runtime = await import("./MessageActions.mjs"); expect(runtime.attachmentActions({ id: "7", mime: "image/png" }).copy).toBe(true); }); + +test("replies are offered only where imsg-react's sibling tool has been proven", () => { + const text = { guid: "G", text: "hello" }; + const picture = { guid: "G", text: "", attachments: [{ id: "1", mime: "image/heic" }] }; + expect(canReply(text, false)).toBe(true); + expect(canReply(picture, false)).toBe(true); + expect(canReply(text, true)).toBe(false); + expect(canReply(null, false)).toBe(false); + expect(canReply({ text: "no guid" }, false)).toBe(false); + expect(canReply({ guid: "G", text: "see https://example.com" }, false)).toBe(false); + expect(canReply({ ...text, link: { url: "https://example.com" } }, false)).toBe(false); + for (const flag of ["retracted", "audio", "pending", "failed", "scheduled"]) + expect(canReply({ ...text, [flag]: true }, false)).toBe(false); + expect(canReply({ ...picture, text: "caption" }, false)).toBe(false); + expect(canReply({ guid: "G", attachments: [{ mime: "image/png" }, { mime: "image/png" }] }, false)).toBe(false); + expect(canReply({ guid: "G", attachments: [{ mime: "video/mp4" }] }, false)).toBe(false); + expect(canReply({ guid: "G", attachments: [{ mime: "image/png", sticker: {} }] }, false)).toBe(false); +}); + +test("a reply target carries what the banner and the pending bubble show", () => { + expect(replyTarget({ guid: "G", text: "hello there", from_me: true, name: "" })) + .toEqual({ guid: "G", text: "hello there", mine: true, name: "", attachment: null }); + const file = { id: "7", mime: "image/png" }; + expect(replyTarget({ guid: "G", text: "", name: "Ann", attachments: [file] })) + .toEqual({ guid: "G", text: "Photo", mine: false, name: "Ann", attachment: file }); +}); + +test("the QML runtime's reply rules are the same build", async () => { + const runtime = await import("./MessageActions.mjs"); + expect(runtime.canReply({ guid: "G", text: "hello" }, false)).toBe(true); + expect(runtime.replyTarget({ guid: "G", text: "x" }).guid).toBe("G"); +}); diff --git a/message-actions.ts b/message-actions.ts index 9a30659a..820fc23d 100644 --- a/message-actions.ts +++ b/message-actions.ts @@ -21,6 +21,56 @@ export function quotedDraft(message: QuotableMessage, draft: string): string { return "> " + excerpt + "\n\n" + draft; } +export interface ReplyAttachment { + id?: string | number; + name?: string; + mime?: string | null; + sticker?: unknown; +} +export interface ReplyableMessage extends QuotableMessage { + guid?: string; + from_me?: boolean; + name?: string; + attachments?: ReplyAttachment[] | null; + audio?: boolean; + pending?: boolean; + failed?: boolean; + scheduled?: boolean; +} + +/** True when the Mac's imsg-reply would act on the bubble: plain text, or exactly + * one picture with no caption, in a 1:1. What is refused has not been proven; + * docs/replies.md says what would have to be measured first. */ +export function canReply(message: ReplyableMessage | null | undefined, group: boolean): boolean { + if (!message || group || !String(message.guid || "")) return false; + if (message.retracted || message.audio || message.pending || message.failed || message.scheduled || message.link) return false; + const text = String(message.text || "").trim(); + const files = message.attachments ?? []; + if (files.length === 0) return text !== "" && !/:\/\/|\bwww\./i.test(text); + const only = files[0]!; + return files.length === 1 && text === "" && !only.sticker && String(only.mime || "").toLowerCase().startsWith("image/"); +} + +export interface ReplyTarget { + guid: string; + text: string; + mine: boolean; + name: string; + attachment: ReplyAttachment | null; +} + +/** What the composer's banner and the pending bubble need to know. */ +export function replyTarget(message: ReplyableMessage): ReplyTarget { + const attachment = (message.attachments ?? [])[0] ?? null; + return { + guid: String(message.guid || ""), + text: quoteText(message) || (attachment ? "Photo" : ""), + mine: message.from_me === true, + name: String(message.name || ""), + attachment, + }; +} + /** Where the selected bubble is after a reload: the row with the same guid, * or -1 when it is gone or has none. A reload renumbers the rows, and a * tapback landing on the selected bubble is itself a reload. */ diff --git a/reply-ax.test.ts b/reply-ax.test.ts new file mode 100644 index 00000000..668debc9 --- /dev/null +++ b/reply-ax.test.ts @@ -0,0 +1,27 @@ +import { expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +const source = readFileSync(new URL("./bridge/mac/imsg-reply", import.meta.url), "utf8"); +const start = source.indexOf('JXA = r"""') + 10; +const script = source.slice(start, source.indexOf('\n"""', start)).replace("REACTED_SOURCE", '"^\\\\d+ reactions?$"'); +function scene(options: {title?:string; draft?:string; label?:string; focus?:boolean; slow?:boolean} = {}) { + let value=options.draft || "", returns=0, writes=0; + const field:any={attributes:{byName:(name:string)=>({value:()=>name === "AXIdentifier" ? "messageBodyField" : name === "AXPlaceholderValue" ? "Reply" : null})}, value:()=>value}; + Object.defineProperty(field,"value",{get:()=>()=>value,set:(text:string)=>{writes++; value=text}}); + const transcript={description:()=>"Reply transcript", path:"window.group"}; + const bubble={description:()=>options.label || "You, hello, 14:05",path:"window.group.child"}; + const win={name:()=>options.title || "+15551234567",sheets:[],entireContents:()=>[transcript,bubble]}; + const proc={frontmost:()=>options.focus !== false,windows:[win],attributes:{byName:()=>({value:()=>field})}}; + const api=new Function("ObjC","Application","Automation","delay",script+'; return {send, viewNames};')( + {import(){}},()=>({processes:{byName:()=>proc},keyCode:(code:number)=>{if(code === 36) { returns++; if (!options.slow) value=""; }}}), + {getDisplayString:(el:any)=>el.path},()=>{}); + return {api, count:()=>returns, text:()=>value, writes:()=>writes}; +} +const request={body:"synthetic reply",text:"hello",hhmm:"14:05",polls:1,chatNames:["+15551234567"]}; +test("reply JXA refuses a draft, wrong recipient, wrong bubble or lost focus before filling",()=>{ + for (const option of [{draft:"existing draft"},{title:"other person"},{label:"You, other, 14:05"},{focus:false}]) { + const s=scene(option); expect(s.api.send(request).ok).toBe(false); expect(s.count()).toBe(0); expect(s.writes()).toBe(0); expect(s.text()).toBe(option.draft || ""); + } +}); +test("reply JXA slow clearing after Return is uncertain and preserves the result",()=>{ + const s=scene({slow:true}); expect(s.api.send(request).error).toBe("unconfirmed"); expect(s.count()).toBe(1); +}); diff --git a/reply-session.test.ts b/reply-session.test.ts new file mode 100644 index 00000000..c599ef01 --- /dev/null +++ b/reply-session.test.ts @@ -0,0 +1,50 @@ +import { expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import * as SendState from "./send-state"; +const source=readFileSync(new URL("./BarWidget.qml",import.meta.url),"utf8"); +function extract(name:string) { + const at=source.indexOf(" function "+name+"("); const begin=source.indexOf("{",at); let depth=0; + for(let i=begin;i{ + const later:Function[]=[], events:any[]=[], writes:string[]=[]; + const root:any={replyPending:[],replyQueue:[],replyJob:null,binDir:"/synthetic/bin",replyExited:(...args:any[])=>events.push(args)}; + const proc:any={running:false,lastErr:"",write:(text:string)=>writes.push(text)}; + const names=["queueReply","reconcileReplies","pumpReply","finishReply"]; + const api=new Function("root","replyProc","SourceId","SendState","Qt",names.map(extract).join("\n")+";return {"+names.join(",")+"};")( + root,proc,{bridgeArgv:()=>["/synthetic/imsg-reply"]},SendState,{callLater:(fn:Function)=>later.push(fn)}); + Object.assign(root,api); + const job={chat:"synthetic",text:"invented reply",localId:"1",stamp:"2026-11-10T00:00:00Z",reply:{guid:"GUID",text:"quoted target"}}; + root.queueReply(job,{...job,ts:job.stamp,replyText:job.reply.text}); + expect(proc.command).not.toContain(job.text); expect(writes).toEqual([job.text]); + root.reconcileReplies("synthetic", []); expect(root.replyPending).toHaveLength(1); + // No view owns these objects or receives the result. + root.finishReply(76); + expect(root.replyPending[0]).toMatchObject({failed:true,replyText:"quoted target",failureReason:"Check Messages before sending again"}); + expect(events[0][1]).toBe(76); expect(root.replyJob).toBe(null); + root.reconcileReplies("synthetic",root.replyPending); expect(root.replyPending).toHaveLength(1); + root.reconcileReplies("synthetic",[]); expect(root.replyPending).toEqual([]); +}); + +import { withPendingSends } from "./thread"; +test("queued and running replies survive a thread refresh after the age limit", () => { + const pending:any={chat:"synthetic",text:"queued words",ts:"2026-11-10T00:00:00Z",localId:"slow",inFlight:true}; + expect(withPendingSends([], [pending], "2026-11-10", undefined, Date.parse(pending.ts)+300000).pending).toEqual([pending]); +}); + +test("shipping reply completion hands words back only on proven refusals", () => { + const view=readFileSync(new URL("./BlipView.qml",import.meta.url),"utf8"); + const start=view.indexOf(" function onReplyExited("), begin=view.indexOf("{",start); + let end=begin, depth=0; + for(;end/dev/null; then cp -p "$target" "$target.pre-blip.$(date +%s)" @@ -200,7 +200,7 @@ for t in imsg imsg-send imsg-read imsg-react contacts contact-save; do install -m 0755 "$here/bridge/linux/blip-shim" "$target" done install -m 0755 "$here/bridge/linux/blip-bridged" "$bin_dir/blip-bridged" -echo "✓ shims installed in $bin_dir: imsg imsg-send imsg-read imsg-react contacts contact-save blip-bridged" +echo "✓ shims installed in $bin_dir: imsg imsg-send imsg-read imsg-react imsg-reply contacts contact-save blip-bridged" echo "• Save contact helpers are included. Mac permission guide: $here/docs/SAVE-CONTACT.md" # blip-bridged holds `imsg serve` channels open so a query costs the query # instead of ~90 ms of ssh + two Python starts + opening a 218 MB chat.db. diff --git a/scripts/test-reply-ui.py b/scripts/test-reply-ui.py new file mode 100644 index 00000000..95579eee --- /dev/null +++ b/scripts/test-reply-ui.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""Render the actual reply target banner with invented context; no bridge or send.""" +from pathlib import Path +import os, shutil, subprocess, tempfile +root = Path(__file__).resolve().parents[1] +s = (root / 'BlipView.qml').read_text() +a = s.index(' Rectangle {\n id: replyBanner') +b = s.index(' // queued attachments', a) +banner = s[a:b].replace('Style.', 'style.') +qml = '''import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtTest +Item { + id: root; width: 600; height: 100 + property bool inThread: true + property var replyTarget: null + property var attFiles: ({}) + property color foreground: "white" + property color dim: "#aaaaaa" + property string fontFamily: "sans-serif" + property int fontCaption: 12 + function isImageMime(mime) { return mime.indexOf("image/") === 0 } + QtObject { id: style; function space(n) { return n } } + TextArea { id: composeField; visible: false } + ColumnLayout { width: parent.width; @BANNER@ } + TestCase { + name: "ReplyTargetBanner"; when: windowShown + function test_target_visibility_and_plain_text() { + root.replyTarget = ({text:"Invented reply target", mine:false, name:"Demo contact"}) + tryCompare(replyBanner, "visible", true) + compare(replyBannerText.textFormat, Text.PlainText) + verify(replyBannerText.text.indexOf("") >= 0) + root.inThread = false + tryCompare(replyBanner, "visible", false) + root.inThread = true + tryCompare(replyBanner, "visible", true) + grabImage(root).save("/tmp/blip-reply-banner.png") + root.replyTarget = null + tryCompare(replyBanner, "visible", false) + } + } +} +'''.replace('@BANNER@', banner) +runner = shutil.which('qmltestrunner') or '/usr/lib/qt6/bin/qmltestrunner' +with tempfile.TemporaryDirectory(prefix='blip-reply-ui-') as folder: + Path(folder, 'tst_reply.qml').write_text(qml) + result = subprocess.run([runner, '-input', folder, '-platform', 'offscreen'], env={**os.environ,'QT_QPA_PLATFORM':'offscreen','QT_QPA_PLATFORMTHEME':'generic'}) + raise SystemExit(result.returncode) diff --git a/send-state.test.ts b/send-state.test.ts index 67601a3c..f25e7e69 100644 --- a/send-state.test.ts +++ b/send-state.test.ts @@ -111,3 +111,12 @@ test("a failed attempt of OTHER words is untouched by a delivered retry of diffe [failedSend("1", "hello", "2026-11-10 12:40:00"), keep], "2026-11-10", DEFAULT_FORMATS, Date.parse("2026-11-10T12:50:00")); expect(out.pending).toEqual([keep]); }); + +import { replyFailure } from "./send-state"; +test("uncertain reply transport outcomes always instruct checking Messages", () => { + for (const code of [-1,2,9,15,69,70,76,127,137,255]) expect(replyFailure(code,"nothing sent")).toBe("Check Messages before sending again"); + expect(replyFailure(75,"Mac draft exists")).toBe("Mac draft exists"); +}); + +import { replyCanRestore } from "./send-state"; +test("uncertain replies never hand back text", () => { for (const code of [-1,0,2,9,15,69,70,76,127,137,255]) expect(replyCanRestore(code)).toBe(false); for (const code of [1,64,65,75,77]) expect(replyCanRestore(code)).toBe(true); }); diff --git a/send-state.ts b/send-state.ts index 0b31e8ef..83a0a75e 100644 --- a/send-state.ts +++ b/send-state.ts @@ -24,3 +24,13 @@ export function friendlySendError(raw: string): string { ? "Messages can't start on the Mac: nobody is logged in there. Log in on the Mac, then send again." : raw; } + +/** Transport errors cannot prove whether Return reached the Mac. */ +export function replyFailure(code: number, detail: string): string { + return ![1, 64, 65, 75, 77].includes(code) + ? "Check Messages before sending again" + : (detail.trim().slice(0, 240) || "Reply was not sent"); +} + +/** Only explicit pre-send refusals prove the text is safe to hand back. */ +export function replyCanRestore(code: number): boolean { return [1, 64, 65, 75, 77].includes(code); } diff --git a/shim-path.ts b/shim-path.ts index 859d8bbb..f977b64d 100644 --- a/shim-path.ts +++ b/shim-path.ts @@ -5,7 +5,7 @@ import { homedir } from "node:os"; import { parseBinDir } from "./bin-dir"; import { bridgeArgv, type BridgeTool } from "./source-id"; -export type ShimTool = "imsg" | "imsg-send" | "imsg-read" | "imsg-react" | "contacts" | "contact-save"; +export type ShimTool = "imsg" | "imsg-send" | "imsg-read" | "imsg-react" | "imsg-reply" | "contacts" | "contact-save"; /** The shim directory: `bin_dir=` in bridge.conf, default ~/bin. */ export function shimDir(home: string = process.env.HOME ?? homedir()): string { diff --git a/source-id.ts b/source-id.ts index 8ade7fe8..bec91f40 100644 --- a/source-id.ts +++ b/source-id.ts @@ -11,7 +11,7 @@ /** The Mac-side tools a conversation is read, sent, marked read and * tapbacked through. */ -export type BridgeTool = "imsg" | "imsg-send" | "imsg-read" | "imsg-react"; +export type BridgeTool = "imsg" | "imsg-send" | "imsg-read" | "imsg-react" | "imsg-reply"; export interface Source { /** Stable lowercase name, e.g. "imessage". */ diff --git a/tapback-actions.ts b/tapback-actions.ts index da5c547e..95e45536 100644 --- a/tapback-actions.ts +++ b/tapback-actions.ts @@ -65,6 +65,11 @@ export function tapbacksOn(conf: string): boolean { return confOn(conf, "tapbacks"); } +/** `replies=` read the same way: the menu's half of the shim's gate on imsg-reply. */ +export function repliesOn(conf: string): boolean { + return confOn(conf, "replies"); +} + function confOn(conf: string, key: string): boolean { let v = ""; for (const raw of String(conf || "").split("\n")) { diff --git a/tapbacks.test.ts b/tapbacks.test.ts index 26da6474..716e110e 100644 --- a/tapbacks.test.ts +++ b/tapbacks.test.ts @@ -35,6 +35,18 @@ describe("blip-shim: tapbacks=on gates imsg-react", () => { } }); + test("replies have their own gate, and tapbacks=on does not open it", () => { + for (const conf of ["", "replies=off\n", "tapbacks=on\n"]) { + const r = run("imsg-reply", conf); + expect(r.code).toBe(78); + expect(r.err).toContain("replies=on"); + expect(r.log).toBe(""); + } + const r = run("imsg-reply", "replies=on\n"); + expect(r.code).toBe(0); + expect(r.log).toContain("imsg-reply '--guid' 'ABC'"); + }); + test("tapbacks=on passes it through like any bridge tool", () => { for (const conf of ["tapbacks=on\n", "tapbacks=ON\n", 'tapbacks="yes"\n', "tapbacks=1\n"]) { const r = run("imsg-react", conf); diff --git a/thread.test.ts b/thread.test.ts index 851b2314..4ca6d378 100644 --- a/thread.test.ts +++ b/thread.test.ts @@ -16,6 +16,7 @@ import { parsePendingSends, pendingBubble, receiptLabel, + replyAttachmentLabel, selectThread, withPendingSends, } from "./thread"; @@ -525,6 +526,24 @@ describe("linkify", () => { }); describe("rich decoration", () => { + test("a reply to a picture names the picture where there is no text to quote", () => { + const photo = { id: "9", name: "IMG_1.heic", mime: "image/heic", bytes: 5 }; + const b = decorate([msg({ reply_to: { text: "", from_me: true, attachment: photo } })], today)[0]!; + expect(b).toMatchObject({ replyText: "Photo", replyMine: true, replyAttachment: photo }); + const captioned = decorate([msg({ reply_to: { text: "look", from_me: false, attachment: photo } })], today)[0]!; + expect(captioned).toMatchObject({ replyText: "look", replyAttachment: null }); + expect(replyAttachmentLabel({ name: "a.mov", mime: "video/quicktime", bytes: 1 })).toBe("Video"); + expect(replyAttachmentLabel({ name: "a.m4a", mime: "audio/mp4", bytes: 1 })).toBe("Audio message"); + expect(replyAttachmentLabel({ name: "a.pdf", mime: "application/pdf", bytes: 1 })).toBe("a.pdf"); + }); + + test("a pending reply keeps its context through a reload", () => { + const send = { chat: "1", text: "b", ts: "2026-08-31T10:02:00Z", replyText: "Photo", replyMine: false, + replyAttachment: { id: "9", name: "x.png", mime: "image/png", bytes: 1 } }; + const { bubbles } = withPendingSends([], [send], today, undefined, Date.parse(send.ts)); + expect(bubbles[0]).toMatchObject({ replyText: "Photo", replyAttachment: { id: "9" } }); + }); + const today = "2026-08-31"; test("receipt belongs only to the newest sent message", () => { diff --git a/thread.ts b/thread.ts index 40fb5c37..d8080f76 100644 --- a/thread.ts +++ b/thread.ts @@ -88,6 +88,8 @@ export interface Bubble { /** Inline-reply context: the quoted snippet, "" when not a reply. */ replyText: string; replyMine: boolean; + /** The file a reply answers, when the original has no text to quote. */ + replyAttachment: AttachmentMeta | null; edited: boolean; /** Rich-link preview card, null when the message has none. */ link: LinkCard | null; @@ -116,7 +118,18 @@ export interface Bubble { /** A send in flight: what was typed, where, and when Enter was pressed * (the wire format, UTC — it is compared against real message stamps). * Lives in BarWidget memory only. */ -export interface PendingSend { chat: string; text: string; ts: string; localId?: string; failed?: boolean; failureReason?: string } +export interface PendingSend { + chat: string; + text: string; + ts: string; + localId?: string; + failed?: boolean; + failureReason?: string; + inFlight?: boolean; + replyText?: string; + replyMine?: boolean; + replyAttachment?: AttachmentMeta | null; +} export interface ThreadOutput { ok: boolean; @@ -335,8 +348,7 @@ export function decorate(msgs: ImsgMessage[], today: string, formats = DEFAULT_F ).map(normalizeStickerAttachment), stickers: m.retracted ? [] : stickerState.stickers, stickersOverflow: m.retracted ? 0 : stickerState.overflow, - replyText: m.reply_to?.text ?? "", - replyMine: m.reply_to?.from_me ?? false, + ...replyContext(m), edited: m.edited === true, link: normalizeLink(m.link), retracted: m.retracted === true, @@ -432,6 +444,9 @@ export function pendingBubble(prev: Bubble | undefined, send: PendingSend, today stickersOverflow: 0, replyText: "", replyMine: false, + replyText: send.replyText ?? "", + replyMine: send.replyMine ?? false, + replyAttachment: send.replyAttachment ?? null, edited: false, link: null, retracted: false, @@ -456,7 +471,7 @@ export function pendingBubble(prev: Bubble | undefined, send: PendingSend, today */ export function withPendingSends(bubbles: Bubble[], pending: PendingSend[], today: string, formats = DEFAULT_FORMATS, now = Date.now()): { bubbles: Bubble[]; pending: PendingSend[] } { const live = pending - .filter((p) => Number.isFinite(stampMs(p.ts)) && (p.failed === true || now - stampMs(p.ts) <= PENDING_MAX_AGE_MS)) + .filter((p) => Number.isFinite(stampMs(p.ts)) && (p.failed === true || p.inFlight === true || now - stampMs(p.ts) <= PENDING_MAX_AGE_MS)) .sort((a, b) => (a.ts < b.ts ? -1 : a.ts > b.ts ? 1 : 0)); if (live.length === 0) return { bubbles, pending: [] }; const taken = new Set(); @@ -654,3 +669,22 @@ if (import.meta.main) { console.log(JSON.stringify({ ok: false, online: false, error: String(e), bubbles: [] })); } } + +/** What a reply context says when the original was only a file. */ +export function replyAttachmentLabel(att: AttachmentMeta): string { + const mime = String(att.mime || "").toLowerCase(); + if (mime.startsWith("image/")) return "Photo"; + if (mime.startsWith("video/")) return "Video"; + if (mime.startsWith("audio/")) return "Audio message"; + return String(att.name || "") || "Attachment"; +} + +function replyContext(m: ImsgMessage): Pick { + const text = (m.reply_to?.text ?? "").replace(//g, "").trim(); + const attachment = text === "" ? m.reply_to?.attachment ?? null : null; + return { + replyText: text || (attachment ? replyAttachmentLabel(attachment) : ""), + replyMine: m.reply_to?.from_me ?? false, + replyAttachment: attachment, + }; +} diff --git a/ui.test.ts b/ui.test.ts index be7cbae8..912a7300 100644 --- a/ui.test.ts +++ b/ui.test.ts @@ -125,12 +125,12 @@ describe("QML safety invariants", () => { expect(panel).toContain("var completedStamp = root.sendStamp"); // the field clears as Enter is pressed; a failure puts the words back // only when nothing newer has been typed - expect(panel).toContain("if (composeField.text === \"\") composeField.text = completedText"); + expect(panel).toContain("if (composeField.text === \"\" && belongsHere) {"); }); test("a send shows its bubble at once and reloads carry the in-flight ledger on stdin", () => { - expect(panel).toContain("root.bubbles = root.appendPendingBubble(root.bubbles, text, stamp, localId)"); - expect(panel).toContain("root.pendingSends = root.pendingSends.concat([{ chat: chat, text: text, ts: stamp, localId: localId }])"); + expect(panel).toContain("root.bubbles = root.appendPendingBubble(root.bubbles, text, stamp, localId, reply)"); + expect(panel).toContain("root.pendingSends = root.pendingSends.concat([pendingSend])"); expect(panel).toContain('"--pending-stdin"'); expect(panel).toContain("threadProc.write(JSON.stringify(pending))"); expect(panel).toContain("root.failPending(completedChat, completedId, reason, completedText, completedStamp)"); @@ -564,7 +564,7 @@ describe("QML safety invariants", () => { expect(panel).toContain("readonly property color hoverFill: Style.hoverFillFor(foreground, accent)"); expect(panel).not.toContain("foreground.b, 0.08)"); expect(panel).toContain("onHasCursorChanged: if (hasCursor) root.bubbleCursorItem = bubbleRow"); - expect(panel).toContain("if (root.bubbleCursor >= 0) root.leaveBubbles(); else root.back()"); + expect(panel).toContain("if (root.bubbleCursor >= 0) root.leaveBubbles(); else if (root.replyTarget) root.replyTarget = null; else root.back()"); const move = qmlFunction("moveBubbleCursor"); expect(move).toContain("bubbleCursor = n - 1"); // Up from nothing = newest expect(move).toContain("leaveBubbles()"); // Down past newest = same exit as Esc @@ -966,7 +966,7 @@ test("idle remaps do not adopt the focused workspace", () => { test("share-sheet Escape, search generations, bounded sender labels", () => { // Astra A#7: the share sheet must close before back() (which would clear the // draft under it). #39 adds the bubble selection as a middle step. - expect(panel).toContain('Keys.onEscapePressed: if (root.shareUrl !== "") root.closeShare(); else if (root.bubbleCursor >= 0) root.leaveBubbles(); else root.back()'); + expect(panel).toContain('Keys.onEscapePressed: if (root.shareUrl !== "") root.closeShare(); else if (root.bubbleCursor >= 0) root.leaveBubbles(); else if (root.replyTarget) root.replyTarget = null; else root.back()'); expect(panel).toContain("if (q !== newQueryRan) { newResults = []; newCursor = 0 }"); expect(panel.split("Layout.maximumWidth: Math.max(1, bubbleRow.width - Style.space(40))").length - 1).toBe(2); expect(widget).toContain('return "code expired"'); From 6634bdf997369fb81274305f2a6f16c069ec5683 Mon Sep 17 00:00:00 2001 From: Marcus Vorwaller Date: Sat, 14 Nov 2026 10:02:55 -0800 Subject: [PATCH 2/2] Guard reply crashes, ambiguous targets and final field contents Treat signalled helpers as uncertain, reserve a distinct local not-started result, and refuse later twins before touching Messages. Require the focused reply field to retain the exact body before Return. Add synthetic regression and mutation coverage for the reviewed send guards; preserve the no-automatic-resend invariant. --- BarWidget.qml | 4 ++-- SendState.mjs | 10 ++++++++-- bridge/mac/imsg-reply | 11 ++++++++--- bridge/mac/test_reply.py | 34 ++++++++++++++++++++++++++++++++-- docs/replies.md | 6 +++++- reply-ax.test.ts | 26 +++++++++++++++++++++++--- reply-session.test.ts | 18 ++++++++++++++++-- send-state.ts | 10 ++++++++-- 8 files changed, 102 insertions(+), 17 deletions(-) diff --git a/BarWidget.qml b/BarWidget.qml index 95248b18..90f412d1 100644 --- a/BarWidget.qml +++ b/BarWidget.qml @@ -160,9 +160,9 @@ BarWidget { Process { id: replyProc property string lastErr: "" - onRunningChanged: if (!running) Qt.callLater(function() { if (!replyProc.running && root.replyJob) root.finishReply(70) }) + onRunningChanged: if (!running) Qt.callLater(function() { if (!replyProc.running && root.replyJob) root.finishReply(SendState.REPLY_NOT_STARTED) }) stderr: StdioCollector { onStreamFinished: replyProc.lastErr = text } - onExited: function(code, status) { root.finishReply(code) } + onExited: function(code, status) { root.finishReply(SendState.replyExitCode(code, status)) } } // Local desktop-toast preferences outlive either view, just like tapbacks. diff --git a/SendState.mjs b/SendState.mjs index 5b54354b..045efe2b 100644 --- a/SendState.mjs +++ b/SendState.mjs @@ -7,15 +7,21 @@ function markSendFailed(items, id, reason, fallback) { function friendlySendError(raw) { return /(^|[^\d])-10810(\D|$)/.test(raw) ? "Messages can't start on the Mac: nobody is logged in there. Log in on the Mac, then send again." : raw; } +var REPLY_NOT_STARTED = -2; +function replyExitCode(code, status) { + return status === 0 ? code : 76; +} function replyFailure(code, detail) { - return ![1, 64, 65, 75, 77].includes(code) ? "Check Messages before sending again" : detail.trim().slice(0, 240) || "Reply was not sent"; + return !replyCanRestore(code) ? "Check Messages before sending again" : detail.trim().slice(0, 240) || "Reply was not sent"; } function replyCanRestore(code) { - return [1, 64, 65, 75, 77].includes(code); + return [REPLY_NOT_STARTED, 1, 64, 65, 75, 77, 78].includes(code); } export { + REPLY_NOT_STARTED, friendlySendError, markSendFailed, replyCanRestore, + replyExitCode, replyFailure }; diff --git a/bridge/mac/imsg-reply b/bridge/mac/imsg-reply index 08b615ef..b8700c13 100755 --- a/bridge/mac/imsg-reply +++ b/bridge/mac/imsg-reply @@ -136,7 +136,7 @@ function send(q) { delay(0.3); if (String(safe(() => el.value(), "")) !== q.body) { safe(() => { el.value = ""; }); return { ok: false, error: "fill-failed" }; } const now = info(focused()); - if (!identity(q) || !viewNames(q) || !front() || now.id !== "messageBodyField" || now.placeholder !== "Reply") { + if (!identity(q) || !viewNames(q) || !front() || now.id !== "messageBodyField" || now.placeholder !== "Reply" || String(safe(() => focused().value(), "")) !== q.body) { safe(() => { el.value = ""; }); return { ok: false, error: "focus-moved" }; } @@ -274,8 +274,13 @@ def resolve(con, imsg, guid: str, self_handles: list) -> dict: if row["balloon_bundle_id"]: raise Stop("unsupported", EX_UNSUPPORTED, "replying to a link or app card is not supported yet") if row["cache_has_attachments"]: - return resolve_picture(con, imsg, row, self_handles) - return rx.resolve(con, imsg, guid, self_handles) + target = resolve_picture(con, imsg, row, self_handles) + else: + target = rx.resolve(con, imsg, guid, self_handles) + # The reply transcript cannot verify which identical label was selected. + if target["ordinal"] > 0: + raise Stop("ambiguous", EX_TEMPFAIL, "another message has the same text and time label; nothing was sent") + return target def threads(con, target: dict) -> set: diff --git a/bridge/mac/test_reply.py b/bridge/mac/test_reply.py index e8c63c2c..ddd99b83 100644 --- a/bridge/mac/test_reply.py +++ b/bridge/mac/test_reply.py @@ -107,11 +107,23 @@ def test_the_other_side_does_not_count_as_a_look_alike(self): db.msg("P2", "", files=[PNG], date=BASE + 10**9, me=0) self.assertTrue(tool.resolve(db.con, imsg, "P1", [])["picture"]) - def test_the_same_minute_on_a_later_day_is_counted_from_the_bottom(self): + def test_the_same_minute_on_a_later_day_is_refused(self): db = Db() db.msg("P1", "", files=[PNG], date=BASE) db.msg("P2", "", files=[PNG], date=BASE + 86400 * 2 * 10**9) - self.assertEqual(tool.resolve(db.con, imsg, "P1", [])["ordinal"], 1) + self.refused(db, "P1", "ambiguous") + + def test_identical_text_in_a_minute_or_on_another_day_is_refused(self): + for offset in (10**9, 86400 * 2 * 10**9): + db = Db() + db.msg("T", "ok") + db.msg("TWIN", "ok", date=BASE + offset) + self.refused(db, "T", "ambiguous") + + def test_picture_with_balloon_is_refused(self): + db = Db() + db.msg("P", files=[PNG], balloon_bundle_id="x.app") + self.refused(db, "P", "unsupported") def test_a_self_thread_picture_aims_at_the_sent_row_and_knows_its_echo(self): db = Db() @@ -270,6 +282,24 @@ def test_a_script_that_dies_during_send_is_checked_never_retried(self): self.assertEqual(self.go(db, s)["result"], "sent") self.assertEqual(s.calls.count("send"), 1) + def test_send_script_exception_never_claims_nothing_sent(self): + db = Db(); db.msg("T", "hello") + script = Script() + original = script.run + def run(q): + if q["mode"] == "send": + raise RuntimeError("lost script") + return original(q) + script.run = run + with patch.object(tool, "CONFIRM_SECONDS", 0.5), self.assertRaises(rx.Stop) as ctx: + self.go(db, script) + self.assertEqual(ctx.exception.exit_code, tool.EX_UNCONFIRMED) + + def test_wrong_text_in_correct_thread_cannot_confirm(self): + db = Db(); db.msg("T", "hello") + db.msg("R", "other words", date=BASE+1, thread_originator_guid="T") + self.assertIsNone(tool.find_reply(db.con, imsg, {"T"}, "the reply", BASE)) + def test_a_row_with_the_wrong_thread_or_text_does_not_confirm(self): db = Db(); db.msg("T", "hello"); db.msg("OTHER", "other", date=BASE + MINUTE) s = Script(land=self.land(db, "OTHER")) diff --git a/docs/replies.md b/docs/replies.md index c78768ab..89a88357 100644 --- a/docs/replies.md +++ b/docs/replies.md @@ -29,7 +29,7 @@ restores whichever app was there. 2. A draft in the composer stops the run, before anything is pressed. 3. The bubble search in `imsg-react` presses `Reply…` once. 4. The reply view must name the target. The field is filled through AX and read back. -5. Focus is rechecked, then Return is pressed. +5. Recipient, target, front app, reply-field identity and its exact text are rechecked, then Return is pressed. 6. `chat.db` must show your row with this text naming the target's thread. 7. The reply view is closed and the previous app comes back. @@ -39,6 +39,8 @@ restores whichever app was there. | 75 | Nothing was sent, including a locked Mac or a draft | Failed bubble, words and target back in the composer | | 76 | Return was pressed but not confirmed, or Messages reported an error | Says so; **never hands the words back**, because a resend could duplicate | | 65, 77 | Refused or no permission | Same as 75 | +| 78 or helper never started | Refused locally before ssh | Words and target back in the composer | +| Crash/signal exit | Return may have reached the Mac | Check Messages; no automatic handback | | 69, 70, 255 | Lost transport or unexpected error | Check Messages; no automatic handback | Never retry after Return. The log, `~/.blip/reply.log`, holds the outcome code, exit @@ -148,3 +150,5 @@ For a live run, target only your own self-thread: This split uses the native AX helpers and bounded stale-picker preflight already on main from the reactions feature. Its only changes to `imsg-react` add picture-bubble discovery and carry that flag into the native action request. Recipient verification fails closed: immediately before Return the Messages window must have no sheet and its title must exactly match the target address or contact name, in addition to the target label and time in the reply transcript. This title shape needs gateway validation; unsupported titles refuse the send. Return followed by slow field clearing, a confirm database error, or lost ssh is unconfirmed: check Messages before sending again. Reply jobs and their result bubbles are owned by BarWidget and survive view closure. + +Identical text or picture labels with a later twin at the same HH:MM are refused before opening Messages, even across different days. Reply targeting does not rely on an ordinal count to distinguish these twins. diff --git a/reply-ax.test.ts b/reply-ax.test.ts index 668debc9..a3bccf07 100644 --- a/reply-ax.test.ts +++ b/reply-ax.test.ts @@ -3,14 +3,16 @@ import { readFileSync } from "node:fs"; const source = readFileSync(new URL("./bridge/mac/imsg-reply", import.meta.url), "utf8"); const start = source.indexOf('JXA = r"""') + 10; const script = source.slice(start, source.indexOf('\n"""', start)).replace("REACTED_SOURCE", '"^\\\\d+ reactions?$"'); -function scene(options: {title?:string; draft?:string; label?:string; focus?:boolean; slow?:boolean} = {}) { +function scene(options: {title?:string; draft?:string; label?:string; focus?:boolean; slow?:boolean; afterFill?:string; noTranscript?:boolean} = {}) { let value=options.draft || "", returns=0, writes=0; const field:any={attributes:{byName:(name:string)=>({value:()=>name === "AXIdentifier" ? "messageBodyField" : name === "AXPlaceholderValue" ? "Reply" : null})}, value:()=>value}; Object.defineProperty(field,"value",{get:()=>()=>value,set:(text:string)=>{writes++; value=text}}); const transcript={description:()=>"Reply transcript", path:"window.group"}; const bubble={description:()=>options.label || "You, hello, 14:05",path:"window.group.child"}; - const win={name:()=>options.title || "+15551234567",sheets:[],entireContents:()=>[transcript,bubble]}; - const proc={frontmost:()=>options.focus !== false,windows:[win],attributes:{byName:()=>({value:()=>field})}}; + const other:any={attributes:{byName:(name:string)=>({value:()=>name === "AXIdentifier" ? (options.afterFill === "id" ? "other" : "messageBodyField") : (options.afterFill === "placeholder" ? "iMessage" : "Reply")})}, value:()=>["id","placeholder"].includes(options.afterFill || "") ? value : options.afterFill === "value" ? "different words" : ""}; + const after = () => writes > 0; + const win={name:()=>after() && options.afterFill === "recipient" ? "other" : options.title || "+15551234567",sheets:[],entireContents:()=>options.noTranscript ? [bubble] : after() && options.afterFill === "transcript" ? [transcript] : [transcript,bubble]}; + const proc={frontmost:()=>options.focus !== false && !(after() && options.afterFill === "front"),windows:[win],attributes:{byName:()=>({value:()=>after() && ["id","placeholder","empty","value"].includes(options.afterFill || "") ? other : field})}}; const api=new Function("ObjC","Application","Automation","delay",script+'; return {send, viewNames};')( {import(){}},()=>({processes:{byName:()=>proc},keyCode:(code:number)=>{if(code === 36) { returns++; if (!options.slow) value=""; }}}), {getDisplayString:(el:any)=>el.path},()=>{}); @@ -25,3 +27,21 @@ test("reply JXA refuses a draft, wrong recipient, wrong bubble or lost focus bef test("reply JXA slow clearing after Return is uncertain and preserves the result",()=>{ const s=scene({slow:true}); expect(s.api.send(request).error).toBe("unconfirmed"); expect(s.count()).toBe(1); }); + +test("reply refuses a wrong time and missing reply transcript before writing",()=>{ + for (const option of [{label:"You, hello, 14:06"},{noTranscript:true}]) { + const s=scene(option); + expect(s.api.send(request).ok).toBe(false); + expect(s.writes()).toBe(0); expect(s.count()).toBe(0); + } +}); +test("every last-moment guard refuses before Return and clears only the filled field",()=>{ + for (const afterFill of ["recipient","transcript","front","id","placeholder","empty","value"]) { + const s=scene({afterFill}); + expect(s.api.send(request).error).toBe("focus-moved"); + expect(s.count()).toBe(0); expect(s.text()).toBe(""); + } +}); +test("verified field content sends once",()=>{ + const s=scene(); expect(s.api.send(request).ok).toBe(true); expect(s.count()).toBe(1); +}); diff --git a/reply-session.test.ts b/reply-session.test.ts index c599ef01..c6296a04 100644 --- a/reply-session.test.ts +++ b/reply-session.test.ts @@ -39,12 +39,26 @@ test("shipping reply completion hands words back only on proven refusals", () => let end=begin, depth=0; for(;end { + const line = source.slice(source.indexOf("id: replyProc")); + const handler = line.match(/onExited: function\(code, status\) \{([^}]+)\}/)![1]; + for (const status of [0,1]) for (const code of [0,1,64,65,75,77,78]) { + let result = -99; + new Function("root","SendState","code","status",handler)({finishReply:(n:number)=>result=n},SendState,code,status); + expect(result).toBe(status === 0 ? code : 76); + if (status !== 0) expect(SendState.replyCanRestore(result)).toBe(false); + } + expect(line).toContain("root.finishReply(SendState.REPLY_NOT_STARTED)"); + expect(SendState.replyCanRestore(SendState.REPLY_NOT_STARTED)).toBe(true); + expect(SendState.replyCanRestore(70)).toBe(false); +}); diff --git a/send-state.ts b/send-state.ts index 83a0a75e..2c86fe05 100644 --- a/send-state.ts +++ b/send-state.ts @@ -25,12 +25,18 @@ export function friendlySendError(raw: string): string { : raw; } +/** Local sentinel for a helper that never started, distinct from Mac EX_SOFTWARE. */ +export const REPLY_NOT_STARTED = -2; + +/** QProcess CrashExit cannot establish whether Return reached Messages. */ +export function replyExitCode(code: number, status: number): number { return status === 0 ? code : 76; } + /** Transport errors cannot prove whether Return reached the Mac. */ export function replyFailure(code: number, detail: string): string { - return ![1, 64, 65, 75, 77].includes(code) + return !replyCanRestore(code) ? "Check Messages before sending again" : (detail.trim().slice(0, 240) || "Reply was not sent"); } /** Only explicit pre-send refusals prove the text is safe to hand back. */ -export function replyCanRestore(code: number): boolean { return [1, 64, 65, 75, 77].includes(code); } +export function replyCanRestore(code: number): boolean { return [REPLY_NOT_STARTED, 1, 64, 65, 75, 77, 78].includes(code); }