From 90775ebbba4900f7ec8c39f72c5210fcd02c5eaf Mon Sep 17 00:00:00 2001 From: James Magahern Date: Wed, 16 Jan 2019 14:17:31 -0800 Subject: [PATCH] Attachment uploading support --- MessagesBridge.xcodeproj/project.pbxproj | 6 ++ .../Operations/MBIMSendMessageOperation.m | 20 +++++- .../MBIMUploadAttachmentOperation.h | 17 +++++ .../MBIMUploadAttachmentOperation.m | 66 +++++++++++++++++++ 4 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.h create mode 100644 kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m diff --git a/MessagesBridge.xcodeproj/project.pbxproj b/MessagesBridge.xcodeproj/project.pbxproj index 1e13936..d556933 100644 --- a/MessagesBridge.xcodeproj/project.pbxproj +++ b/MessagesBridge.xcodeproj/project.pbxproj @@ -63,6 +63,7 @@ 1ACFCFDA219EB2AC00E2C237 /* GCDAsyncSocket.m in Sources */ = {isa = PBXBuildFile; fileRef = 1ACFCF1B219EB2AC00E2C237 /* GCDAsyncSocket.m */; }; 1ACFCFDC219EB2AC00E2C237 /* GCDAsyncSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ACFCF1D219EB2AC00E2C237 /* GCDAsyncSocket.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1ACFCFDF219EB31400E2C237 /* CocoaHTTPServer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1ACFCDE2219EB28A00E2C237 /* CocoaHTTPServer.framework */; }; + 1AD8936E21EFD986009B599A /* MBIMUploadAttachmentOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AD8936D21EFD986009B599A /* MBIMUploadAttachmentOperation.m */; }; CD14F18E219E2DB400E7DD22 /* CryptoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F18D219E2DB400E7DD22 /* CryptoTests.m */; }; CD14F1A1219FE7D600E7DD22 /* MBIMUpdatePollOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1A0219FE7D600E7DD22 /* MBIMUpdatePollOperation.m */; }; CD14F1A4219FF22700E7DD22 /* IMMessageItem+Encoded.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1A3219FF22700E7DD22 /* IMMessageItem+Encoded.m */; }; @@ -194,6 +195,8 @@ 1ACFCF1D219EB2AC00E2C237 /* GCDAsyncSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GCDAsyncSocket.h; sourceTree = ""; }; 1ACFCFE2219EB45300E2C237 /* MBIMHTTPConnection.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMHTTPConnection.h; sourceTree = ""; }; 1ACFCFE3219EB45300E2C237 /* MBIMHTTPConnection.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMHTTPConnection.m; sourceTree = ""; }; + 1AD8936C21EFD986009B599A /* MBIMUploadAttachmentOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMUploadAttachmentOperation.h; sourceTree = ""; }; + 1AD8936D21EFD986009B599A /* MBIMUploadAttachmentOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMUploadAttachmentOperation.m; sourceTree = ""; }; CD14F18B219E2DB400E7DD22 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; CD14F18D219E2DB400E7DD22 /* CryptoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CryptoTests.m; sourceTree = ""; }; CD14F18F219E2DB400E7DD22 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -525,6 +528,8 @@ CD602061219B68950024D9C5 /* MBIMSendMessageOperation.m */, CD14F19F219FE7D600E7DD22 /* MBIMUpdatePollOperation.h */, CD14F1A0219FE7D600E7DD22 /* MBIMUpdatePollOperation.m */, + 1AD8936C21EFD986009B599A /* MBIMUploadAttachmentOperation.h */, + 1AD8936D21EFD986009B599A /* MBIMUploadAttachmentOperation.m */, ); path = Operations; sourceTree = ""; @@ -797,6 +802,7 @@ 1AA43E95219EC38E00EDF1A7 /* MBIMHTTPUtilities.m in Sources */, CDE455A421A5308D0041F5DD /* MBIMFetchAttachmentOperation.m in Sources */, CD83E156219BE10A00F4CCEA /* hooking.m in Sources */, + 1AD8936E21EFD986009B599A /* MBIMUploadAttachmentOperation.m in Sources */, CDF6233A219A8A5600690038 /* MBIMBridge.m in Sources */, CDF62335219A895D00690038 /* main.m in Sources */, CD60205C219B623F0024D9C5 /* MBIMMessagesListOperation.m in Sources */, diff --git a/kordophone/Bridge/Operations/MBIMSendMessageOperation.m b/kordophone/Bridge/Operations/MBIMSendMessageOperation.m index cfb545c..0d1202b 100644 --- a/kordophone/Bridge/Operations/MBIMSendMessageOperation.m +++ b/kordophone/Bridge/Operations/MBIMSendMessageOperation.m @@ -20,7 +20,7 @@ return @"sendMessage"; } -- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID +- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID attachmentGUIDs:(NSArray *)guids { __block BOOL result = YES; @@ -32,7 +32,16 @@ IMHandle *senderHandle = [iMessageAccount loginIMHandle]; NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:messageBody]; - IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished]; + NSAttributedString *attrStringWithFileTransfers = IMCreateSuperFormatStringWithAppendedFileTransfers(replyAttrString, guids); + + IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle + withText:attrStringWithFileTransfers + fileTransferGUIDs:guids + flags:(kIMMessageFinished | kIMMessageIsFromMe)]; + + for (NSString *guid in [reply fileTransferGUIDs]) { + [[IMFileTransferCenter sharedInstance] assignTransfer:guid toHandle:chat.recipient]; + } if (!chat) { NSLog(@"Chat does not exist: %@", chatGUID); @@ -63,7 +72,12 @@ return; } - BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid]; + NSArray *transferGUIDs = [args objectForKey:@"fileTransferGUIDs"]; + if (!transferGUIDs) { + transferGUIDs = @[]; + } + + BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid attachmentGUIDs:transferGUIDs]; if (result) { response = [[HTTPErrorResponse alloc] initWithErrorCode:200]; } diff --git a/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.h b/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.h new file mode 100644 index 0000000..91e9fd5 --- /dev/null +++ b/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.h @@ -0,0 +1,17 @@ +// +// MBIMUploadAttachmentOperation.h +// kordophoned +// +// Created by James Magahern on 1/16/19. +// Copyright © 2019 James Magahern. All rights reserved. +// + +#import "MBIMBridgeOperation.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MBIMUploadAttachmentOperation : MBIMBridgeOperation + +@end + +NS_ASSUME_NONNULL_END diff --git a/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m b/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m new file mode 100644 index 0000000..09db295 --- /dev/null +++ b/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m @@ -0,0 +1,66 @@ +// +// MBIMUploadAttachmentOperation.m +// kordophoned +// +// Created by James Magahern on 1/16/19. +// Copyright © 2019 James Magahern. All rights reserved. +// + +#import "MBIMUploadAttachmentOperation.h" +#import "MBIMDataResponse.h" + +#import + +@implementation MBIMUploadAttachmentOperation + ++ (void)load { [super load]; } + ++ (NSString *)endpointName +{ + return @"uploadAttachment"; +} + +- (void)main +{ + NSObject *response = nil; + do { + NSString *filename = [self valueForQueryItemWithName:@"filename"]; + if ([filename length] == 0) { + NSLog(@"No filename provided"); + response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; + break; + } + + NSData *attachmentData = self.requestBodyData; + if ([attachmentData length] == 0) { + NSLog(@"No attachment data in request"); + response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; + break; + } + + NSString *localPath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename]; + NSURL *localURL = [NSURL fileURLWithPath:localPath]; + BOOL success = [attachmentData writeToURL:localURL atomically:NO]; + if (!success) { + NSLog(@"Error writing attachment to temporary directory"); + response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; + break; + } + + NSString *guid = [[IMFileTransferCenter sharedInstance] guidForNewOutgoingTransferWithLocalURL:localURL]; + if (!guid) { + NSLog(@"There was some problem shuttling the file to IMCore"); + response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; + break; + } + + NSDictionary *responseDict = @{ + @"fileTransferGUID" : guid + }; + response = [MBIMJSONDataResponse responseWithJSONObject:responseDict]; + } while (0); + + self.serverCompletionBlock(response); +} + +@end