Skip to content

Commit

Permalink
ismultiparty is legit now
Browse files Browse the repository at this point in the history
  • Loading branch information
kfatehi committed Jan 8, 2017
1 parent d4d0407 commit a8cb63d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -5,3 +5,4 @@ Reads an .ichat binary plist file and writes newline-separated JSON to STDOUT
## Special Thanks

* https://github.com/jmah/iChat-Image-Indexer
* https://github.com/karstenBriksoft/PlistExplorer
4 changes: 2 additions & 2 deletions ichat2json/InstantMessage.h
Expand Up @@ -13,8 +13,8 @@

@property (readonly, copy) NSDate *date;
@property (readonly, copy) NSString *message;
@property (readonly, copy) NSString *attachmentGUID;
@property (readonly, copy) NSString *attachmentName;
@property (readonly, copy) NSMutableArray *files;
@property () BOOL isMultiParty;
@property (readonly, copy) Presentity *sender;
@property (readonly, copy) Presentity *subject;
- (NSString *) toJSONString;
Expand Down
33 changes: 23 additions & 10 deletions ichat2json/InstantMessage.m
Expand Up @@ -17,17 +17,32 @@ - (id)initWithCoder:(NSCoder *)decoder;
_subject = [decoder decodeObjectForKey:@"Subject"];
_date = [decoder decodeObjectForKey:@"Time"];
_message = [decoder decodeObjectForKey:@"OriginalMessage"];
_files = [[NSMutableArray alloc] init];
_isMultiParty = false;
NSAttributedString *attrMsg = [decoder decodeObjectForKey:@"MessageText"];

_attachmentName = [self getAttributeWithKey:@"__kIMFilenameAttributeName" fromAttributedString:attrMsg];
_attachmentGUID = [self getAttributeWithKey:@"__kIMFileTransferGUIDAttributeName" fromAttributedString:attrMsg];
NSArray *fileIds = [self getAttributesWithKey:@"__kIMFilenameAttributeName" fromAttributedString:attrMsg];
NSArray *fileNames = [self getAttributesWithKey:@"__kIMFileTransferGUIDAttributeName" fromAttributedString:attrMsg];
[fileIds enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *fileId = [fileIds objectAtIndex:idx];
NSString *fileName = [fileNames objectAtIndex:idx];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:fileId, @"id", fileName, @"name", nil];
[_files addObject:dict];
}];
return self;
}

- (NSString *) getAttributeWithKey: (NSString *)attrKey fromAttributedString:(NSAttributedString *)attrStr
- (NSArray *) getAttributesWithKey: (NSString *)attrKey fromAttributedString:(NSAttributedString *)attrStr
{
NSRange effectiveRange = NSMakeRange(0, 0);
return [attrStr attribute:attrKey atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
NSMutableArray *mArray = [NSMutableArray array];
id value;
while (NSMaxRange(effectiveRange) < [attrStr length]) {
value = [attrStr attribute:attrKey atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
if (value != nil) {
[mArray addObject:value];
}
}
return [mArray copy];
}

- (void)encodeWithCoder:(NSCoder *)encoder;
Expand All @@ -38,16 +53,14 @@ - (NSString *) toJSONString;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *dateStr = [dateFormat stringFromDate:_date];
bool isMultiParty = _subject == nil; // multi party chats lack a "subject"
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[_sender accountName], @"sender",
[_sender serviceName], @"service",
_message == nil ? [NSNull null] : _message, @"message",
dateStr, @"date",
[NSNumber numberWithBool:isMultiParty], @"isMultiParty",
isMultiParty ? [NSNull null] : [_subject accountName], @"subject",
_attachmentName, @"attachmentName",
_attachmentGUID, @"attachmentGUID",
[NSNumber numberWithBool:_isMultiParty], @"isMultiParty",
_subject ? [_subject accountName] : [NSNull null], @"subject",
_files, @"files",
nil];

NSError *error;
Expand Down
21 changes: 19 additions & 2 deletions ichat2json/main.m
Expand Up @@ -30,16 +30,33 @@ int main(int argc, const char * argv[]) {
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSArray* root = [unarchiver decodeObjectForKey:@"$root"];
NSMutableArray *ims = [NSMutableArray array];
NSMutableSet *people = [NSMutableSet set];
for (id object in root) {
if ([object isKindOfClass:[NSMutableArray class]]) {
for (id sub in object) {
if ([sub isKindOfClass:[InstantMessage class]]) {
InstantMessage *im = (InstantMessage *) sub;
fprintf(stdout, "%s\n", [[im toJSONString] UTF8String]);
InstantMessage *im = (InstantMessage *) sub;
if ([im subject] != nil)
[people addObject:im.subject.accountName];
if ([im sender] != nil)
[people addObject:im.sender.accountName];
[ims addObject:im];
}
if ([sub isKindOfClass:[Presentity class]]) {
Presentity *prs = (Presentity *) sub;
[people addObject:prs.accountName];
}
}
}
}
for (InstantMessage* im in ims){
if ([people count] > 2) {
[im setIsMultiParty:true];
}
fprintf(stdout, "%s\n", [[im toJSONString] UTF8String]);
}

}
return 0;
}

0 comments on commit a8cb63d

Please sign in to comment.