@@ -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 */
94180export 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