Skip to content

Instantly share code, notes, and snippets.

@stephancasas
Created February 3, 2023 01:09
Show Gist options
  • Save stephancasas/06322088ed0071f9e2dcddeee06974fe to your computer and use it in GitHub Desktop.
Save stephancasas/06322088ed0071f9e2dcddeee06974fe to your computer and use it in GitHub Desktop.
Query the attributedBody column in the updated macOS Ventura Messages database
//
// ventura_messages.m
// macOS Ventura Messages DB Query Example
//
// Created by Stephan Casas on 2/2/23.
//
#import <Foundation/Foundation.h>
@implementation NSData (NSDataExtended)
+ (NSData *)dataWithContentsOfHexEncodedString:(NSString *) string {
const char * chars = [string UTF8String];
int i = 0;
NSMutableData *data = [NSMutableData dataWithCapacity: string.length / 2];
char byteChars[3] = {'\0', '\0', '\0'};
unsigned long wholeByte;
while (i < string.length) {
byteChars[0] = chars[i++];
byteChars[1] = chars[i++];
wholeByte = strtoul(byteChars, NULL, 16);
[data appendBytes:&wholeByte length:1];
}
return data;
}
+ (NSData *)dataWithContentsOfHexEncodedFile:(NSString *) filePath {
return [self dataWithContentsOfHexEncodedString:[NSString
stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil]];
}
@end
int main(int argc, const char * argv[]) {
system([[[NSString alloc] initWithFormat:@"%s %s > %s",
"/usr/bin/sqlite3 ~/Library/Messages/chat.db",
"'SELECT HEX(attributedBody) FROM message ORDER BY ROWID DESC LIMIT 1'",
"/private/tmp/msgbody"] UTF8String]);
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSMutableAttributedString *msg = [[[NSUnarchiver alloc]
initForReadingWithData:[NSData dataWithContentsOfHexEncodedFile:@"/private/tmp/msgbody"]
] decodeTopLevelObjectAndReturnError:nil];
NSLog(@"%@", [msg string]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment