Skip to content

Commit 2c919c5

Browse files
mayoalexanderclaude
andcommitted
fix: imessage read/search parses attributedBody for modern macOS
Root cause: modern macOS Messages.app stores content in attributedBody (binary NSAttributedString BLOB) instead of text column. All queries filtered WHERE m.text IS NOT NULL, returning zero results. Fix: - Added parseAttributedBody() to extract plain text from NSAttributedString hex - Added queryMessagesWithBody() — queries both text + hex(attributedBody) - Updated read + search commands to use new parser - Updated searchByHandle() with attributedBody fallback Tested: 20 messages from Richard Delgado now loading correctly Closes bug anomalyco#927 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 247a86b commit 2c919c5

2 files changed

Lines changed: 100 additions & 52 deletions

File tree

‎packages/opencode/src/cli/cmd/platform-imessage.ts‎

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as prompts from "./clack"
33
import { UI } from "../ui"
44
import { printDivider, dim, bold, success } from "./iris-api"
55
import { execSync } from "child_process"
6-
import { isAvailable, diagnoseAccess, query as queryMessages, normalizeHandle, getContactCards } from "../lib/imessage"
6+
import { isAvailable, diagnoseAccess, query as queryMessages, normalizeHandle, getContactCards, queryMessagesWithBody } from "../lib/imessage"
77
import { resolveContactName, resolveContactNames } from "../lib/contacts"
88

99
const ImessageSearchCommand = cmd({
@@ -73,36 +73,14 @@ const ImessageSearchCommand = cmd({
7373
const cutoffSeconds = args.since
7474
? Math.max(0, Math.floor((Date.now() - new Date(String(args.since)).getTime()) / 1000))
7575
: (args.days as number) * 86400
76-
const sql = `
77-
SELECT
78-
m.rowid,
79-
datetime(m.date/1000000000 + 978307200, 'unixepoch', 'localtime') as msg_date,
80-
m.is_from_me,
81-
REPLACE(REPLACE(m.text, char(10), ' '), char(13), ' ') as text
82-
FROM message m
83-
JOIN chat_message_join cmj ON m.rowid = cmj.message_id
84-
JOIN chat c ON cmj.chat_id = c.rowid
85-
WHERE ${whereClause}
86-
AND m.date/1000000000 + 978307200 > unixepoch('now') - ${cutoffSeconds}
87-
AND m.text IS NOT NULL
88-
AND m.text != ''
89-
ORDER BY m.date DESC
90-
LIMIT ${args.limit};
91-
`.replace(/\n/g, " ").trim()
92-
9376
try {
94-
const raw = queryMessages(sql)
95-
if (!raw) {
77+
const messages = queryMessagesWithBody(whereClause, cutoffSeconds, args.limit as number)
78+
if (!messages.length) {
9679
prompts.log.info(`No messages matching "${args.query}" in the last ${args.days} days`)
9780
prompts.outro("Done")
9881
return
9982
}
10083

101-
const messages = raw.split("\n").map((line) => {
102-
const [id, date, fromMe, ...textParts] = line.split("|")
103-
return { id, date, from_me: fromMe === "1", text: textParts.join("|") }
104-
})
105-
10684
if (args.json) {
10785
console.log(JSON.stringify(messages, null, 2))
10886
prompts.outro("Done")
@@ -163,36 +141,14 @@ const ImessageReadCommand = cmd({
163141
const cutoffSeconds = args.since
164142
? Math.max(0, Math.floor((Date.now() - new Date(String(args.since)).getTime()) / 1000))
165143
: (args.days as number) * 86400
166-
const sql = `
167-
SELECT
168-
m.rowid,
169-
datetime(m.date/1000000000 + 978307200, 'unixepoch', 'localtime') as msg_date,
170-
m.is_from_me,
171-
REPLACE(REPLACE(m.text, char(10), ' '), char(13), ' ') as text
172-
FROM message m
173-
JOIN chat_message_join cmj ON m.rowid = cmj.message_id
174-
JOIN chat c ON cmj.chat_id = c.rowid
175-
WHERE ${whereClause}
176-
AND m.date/1000000000 + 978307200 > unixepoch('now') - ${cutoffSeconds}
177-
AND m.text IS NOT NULL
178-
AND m.text != ''
179-
ORDER BY m.date DESC
180-
LIMIT ${args.last};
181-
`.replace(/\n/g, " ").trim()
182-
183144
try {
184-
const raw = queryMessages(sql)
185-
if (!raw) {
145+
const messages = queryMessagesWithBody(whereClause, cutoffSeconds, args.last as number)
146+
if (!messages.length) {
186147
prompts.log.info(`No messages matching "${args.query}"`)
187148
prompts.outro("Done")
188149
return
189150
}
190151

191-
const messages = raw.split("\n").map((line) => {
192-
const [id, date, fromMe, ...textParts] = line.split("|")
193-
return { id, date, from_me: fromMe === "1", text: textParts.join("|") }
194-
})
195-
196152
if (args.json) {
197153
console.log(JSON.stringify(messages, null, 2))
198154
prompts.outro("Done")

‎packages/opencode/src/cli/lib/imessage.ts‎

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,91 @@ export function query(sql: string): string {
7878
}).trim()
7979
}
8080

81+
/**
82+
* Parse NSAttributedString binary blob to extract plain text.
83+
* Modern macOS Messages.app stores content in attributedBody (BLOB) instead of text.
84+
* The plain text string is embedded after the NSString type marker in the binary.
85+
*/
86+
export function parseAttributedBody(hexStr: string): string {
87+
if (!hexStr) return ""
88+
try {
89+
const buf = Buffer.from(hexStr, "hex")
90+
// Find NSString marker followed by a length byte and the text content.
91+
// The pattern is: ...NSString\x01\x95\x84\x01\x2B<length><text>\x86...
92+
// We look for the \x2B byte ('+') which precedes the length+text.
93+
const text = buf.toString("utf-8", 0, buf.length)
94+
// Strategy: find the text between the NSString length marker and the next control sequence.
95+
// The attributedBody contains the raw string after a specific byte pattern.
96+
const nsStringMarker = "NSString"
97+
const markerIdx = text.indexOf(nsStringMarker)
98+
if (markerIdx === -1) return ""
99+
100+
// After NSString, skip to the '+' marker which precedes the length byte + text
101+
const afterMarker = buf.indexOf(0x2b, markerIdx + nsStringMarker.length)
102+
if (afterMarker === -1) return ""
103+
104+
// The byte after '+' is the string length
105+
const strLen = buf[afterMarker + 1]
106+
if (!strLen || strLen === 0) return ""
107+
108+
// Extract the text
109+
const start = afterMarker + 2
110+
const end = start + strLen
111+
if (end > buf.length) return ""
112+
113+
const extracted = buf.subarray(start, end).toString("utf-8")
114+
// Clean up: remove trailing binary garbage
115+
return extracted.replace(/[\x00-\x08\x0e-\x1f\x80-\xff]/g, "").trim()
116+
} catch {
117+
return ""
118+
}
119+
}
120+
121+
/**
122+
* Query messages with attributedBody fallback.
123+
* Returns messages even when text column is NULL (modern macOS Messages.app).
124+
*/
125+
export function queryMessagesWithBody(whereClause: string, cutoffSeconds: number, limit: number): Message[] {
126+
// Query both text and hex-encoded attributedBody
127+
const sql = `SELECT
128+
m.rowid,
129+
datetime(m.date/1000000000 + 978307200, 'unixepoch', 'localtime') as msg_date,
130+
m.is_from_me,
131+
REPLACE(REPLACE(m.text, char(10), ' '), char(13), ' ') as text,
132+
hex(m.attributedBody) as body_hex
133+
FROM message m
134+
JOIN chat_message_join cmj ON m.rowid = cmj.message_id
135+
JOIN chat c ON cmj.chat_id = c.rowid
136+
WHERE ${whereClause}
137+
AND m.date/1000000000 + 978307200 > unixepoch('now') - ${cutoffSeconds}
138+
AND (m.text IS NOT NULL OR m.attributedBody IS NOT NULL)
139+
ORDER BY m.date DESC
140+
LIMIT ${limit};`.replace(/\n/g, " ").trim()
141+
142+
try {
143+
const raw = query(sql)
144+
if (!raw) return []
145+
146+
return raw.split("\n").map((line) => {
147+
const [id, date, fromMe, textCol, bodyHex] = line.split("|")
148+
// Prefer text column, fall back to parsing attributedBody
149+
let msgText = textCol?.trim() || ""
150+
if (!msgText && bodyHex) {
151+
msgText = parseAttributedBody(bodyHex)
152+
}
153+
if (!msgText) return null
154+
return {
155+
id,
156+
date,
157+
from_me: fromMe === "1",
158+
text: msgText.replace(/\n/g, " ").trim(),
159+
} satisfies Message
160+
}).filter(Boolean) as Message[]
161+
} catch {
162+
return []
163+
}
164+
}
165+
81166
/**
82167
* Normalize a phone/email/handle to a search-friendly format.
83168
* Phones: strip non-digits, take last 10 digits.
@@ -90,6 +175,7 @@ export function normalizeHandle(handle: string): string {
90175

91176
/**
92177
* Search messages by phone number, email, or chat identifier.
178+
* Falls back to attributedBody when text column is NULL (modern macOS).
93179
*/
94180
export function searchByHandle(handle: string, days = 30, limit = 50): Message[] {
95181
const search = normalizeHandle(handle)
@@ -98,12 +184,13 @@ export function searchByHandle(handle: string, days = 30, limit = 50): Message[]
98184
const sql = `SELECT
99185
m.rowid, m.text, m.is_from_me, m.date,
100186
datetime(m.date/1000000000 + strftime('%s','2001-01-01'), 'unixepoch', 'localtime') as sent_dt,
101-
c.chat_identifier
187+
c.chat_identifier,
188+
hex(m.attributedBody) as body_hex
102189
FROM message m
103190
JOIN chat_message_join cmj ON m.rowid = cmj.message_id
104191
JOIN chat c ON cmj.chat_id = c.rowid
105192
WHERE c.chat_identifier LIKE '%${search}%'
106-
AND m.text IS NOT NULL AND m.text != ''
193+
AND (m.text IS NOT NULL OR m.attributedBody IS NOT NULL)
107194
AND m.date > (strftime('%s','now') - ${cutoffSeconds} - strftime('%s','2001-01-01')) * 1000000000
108195
ORDER BY m.date DESC LIMIT ${limit}`
109196

@@ -114,9 +201,14 @@ export function searchByHandle(handle: string, days = 30, limit = 50): Message[]
114201
return raw.split("\n").map((line) => {
115202
const parts = line.split("|")
116203
if (parts.length < 5) return null
204+
let text = parts[1]?.trim() || ""
205+
if (!text && parts[6]) {
206+
text = parseAttributedBody(parts[6])
207+
}
208+
if (!text) return null
117209
return {
118210
id: parts[0],
119-
text: parts[1],
211+
text,
120212
from_me: parts[2] === "1",
121213
date: parts[4],
122214
chat_identifier: parts[5] || search,

0 commit comments

Comments
 (0)