Add 'server/' from commit '800090542d91beae40bc81fc41b67ba61c47da77'
git-subtree-dir: server git-subtree-mainline:6a4054c15agit-subtree-split:800090542d
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMAuthenticateOperation.h
|
||||
// MBIMAuthenticateOperation
|
||||
//
|
||||
// Created by James Magahern on 7/6/21.
|
||||
// Copyright © 2021 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMAuthenticateOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// MBIMAuthenticateOperation.m
|
||||
// MBIMAuthenticateOperation
|
||||
//
|
||||
// Created by James Magahern on 7/6/21.
|
||||
// Copyright © 2021 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMAuthenticateOperation.h"
|
||||
#import "MBIMBridge.h"
|
||||
#import "MBIMAuthToken.h"
|
||||
|
||||
@implementation MBIMAuthenticateOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"authenticate";
|
||||
}
|
||||
|
||||
+ (BOOL)requiresAuthentication
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
|
||||
if (self.requestBodyData.length == 0) {
|
||||
self.serverCompletionBlock([[HTTPErrorResponse alloc] initWithErrorCode:400]);
|
||||
return;
|
||||
}
|
||||
|
||||
NSError *error = nil;
|
||||
NSDictionary *args = [NSJSONSerialization JSONObjectWithData:self.requestBodyData options:0 error:&error];
|
||||
if (error || args.count == 0) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:400];
|
||||
} else {
|
||||
do {
|
||||
NSString *username = [args objectForKey:@"username"];
|
||||
NSString *password = [args objectForKey:@"password"];
|
||||
|
||||
if (!username || !password) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:400];
|
||||
break;
|
||||
}
|
||||
|
||||
if (![MBIMBridge.sharedInstance.authUsername isEqualToString:username]) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:401];
|
||||
break;
|
||||
}
|
||||
|
||||
if (![MBIMBridge.sharedInstance.authPassword isEqualToString:password]) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:401];
|
||||
break;
|
||||
}
|
||||
|
||||
MBIMAuthToken *token = [[MBIMAuthToken alloc] initWithUsername:username];
|
||||
|
||||
// All systems go
|
||||
MBIMJSONDataResponse *dataResponse = [MBIMJSONDataResponse responseWithJSONObject:@{
|
||||
@"jwt" : token.jwtToken
|
||||
}];
|
||||
|
||||
// Send a cookie down so we can use httpOnly cookies
|
||||
dataResponse.httpHeaders[@"Set-Cookie"] = [NSString stringWithFormat:@"auth_token=%@", token.jwtToken];
|
||||
|
||||
response = dataResponse;
|
||||
} while (NO);
|
||||
}
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
41
server/kordophone/Bridge/Operations/MBIMBridgeOperation.h
Normal file
41
server/kordophone/Bridge/Operations/MBIMBridgeOperation.h
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// MBIMBridgeOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "HTTPMessage.h"
|
||||
#import "HTTPResponse.h"
|
||||
#import "HTTPErrorResponse.h"
|
||||
|
||||
#import "MBIMJSONDataResponse.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef void (^MBIMBridgeOperationCompletionBlock)(NSObject<HTTPResponse> * _Nullable response);
|
||||
|
||||
@interface MBIMBridgeOperation : NSOperation
|
||||
@property (class, nonatomic, readonly) NSString *endpointName;
|
||||
@property (class, nonatomic, readonly) BOOL requiresAuthentication; // default YES
|
||||
|
||||
@property (nonatomic, strong) HTTPMessage *request;
|
||||
@property (nonatomic, strong) NSData *requestBodyData;
|
||||
@property (nonatomic, readonly) NSURL *requestURL;
|
||||
@property (nonatomic, readonly) MBIMBridgeOperationCompletionBlock serverCompletionBlock;
|
||||
|
||||
+ (dispatch_queue_t)sharedIMAccessQueue;
|
||||
|
||||
+ (nullable Class)operationClassForEndpointName:(NSString *)endpointName;
|
||||
- (instancetype)initWithRequestURL:(NSURL *)requestURL completion:(MBIMBridgeOperationCompletionBlock)completionBlock;
|
||||
|
||||
- (NSObject<HTTPResponse> *)cancelAndReturnTimeoutResponse;
|
||||
|
||||
// convenience
|
||||
- (nullable NSString *)valueForQueryItemWithName:(NSString *)queryItemName;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
86
server/kordophone/Bridge/Operations/MBIMBridgeOperation.m
Normal file
86
server/kordophone/Bridge/Operations/MBIMBridgeOperation.m
Normal file
@@ -0,0 +1,86 @@
|
||||
//
|
||||
// MBIMBridgeOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
#import "MBIMURLUtilities.h"
|
||||
|
||||
@interface MBIMBridgeOperation (/*INTERNAL*/)
|
||||
@property (nonatomic, strong) NSURL *requestURL;
|
||||
@property (nonatomic, copy) MBIMBridgeOperationCompletionBlock serverCompletionBlock;
|
||||
@end
|
||||
|
||||
@implementation MBIMBridgeOperation
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
// To be inplemented by subclasses
|
||||
return @"__unimplemented__";
|
||||
}
|
||||
|
||||
+ (NSMutableDictionary *)_operationClassMapping
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
static NSMutableDictionary *operationClassMapping = nil;
|
||||
dispatch_once(&onceToken, ^{
|
||||
operationClassMapping = [[NSMutableDictionary alloc] init];
|
||||
});
|
||||
|
||||
return operationClassMapping;
|
||||
}
|
||||
|
||||
+ (dispatch_queue_t)sharedIMAccessQueue
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
static dispatch_queue_t accessQueue = nil;
|
||||
dispatch_once(&onceToken, ^{
|
||||
accessQueue = dispatch_queue_create("IMAccessQueue", DISPATCH_QUEUE_SERIAL);
|
||||
});
|
||||
|
||||
return accessQueue;
|
||||
}
|
||||
|
||||
+ (void)load
|
||||
{
|
||||
if ([self class] != [MBIMBridgeOperation class]) {
|
||||
[[self _operationClassMapping] setObject:[self class] forKey:[self endpointName]];
|
||||
}
|
||||
}
|
||||
|
||||
+ (nullable Class)operationClassForEndpointName:(NSString *)endpointName
|
||||
{
|
||||
return [[self _operationClassMapping] objectForKey:endpointName];
|
||||
}
|
||||
|
||||
+ (BOOL)requiresAuthentication
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (instancetype)initWithRequestURL:(NSURL *)requestURL completion:(MBIMBridgeOperationCompletionBlock)completionBlock
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.requestURL = requestURL;
|
||||
self.serverCompletionBlock = completionBlock;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSObject<HTTPResponse> *)cancelAndReturnTimeoutResponse
|
||||
{
|
||||
[self cancel];
|
||||
return [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
}
|
||||
|
||||
- (NSString *)valueForQueryItemWithName:(NSString *)queryItemName
|
||||
{
|
||||
return [[self requestURL] valueForQueryItemWithName:queryItemName];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMConversationListOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMConversationListOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// MBIMConversationListOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMConversationListOperation.h"
|
||||
#import "MBIMHTTPUtilities.h"
|
||||
#import "IMChat+Encoded.h"
|
||||
|
||||
#import "IMCore_ClassDump.h"
|
||||
|
||||
@implementation MBIMConversationListOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"conversations";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
__block NSMutableArray *conversations = [NSMutableArray array];
|
||||
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
NSArray<IMChat *> *chats = [[IMChatRegistry sharedInstance] allExistingChats];
|
||||
for (IMChat *chat in chats) {
|
||||
NSDictionary *chatDict = [chat mbim_dictionaryRepresentation];
|
||||
[conversations addObject:chatDict];
|
||||
}
|
||||
});
|
||||
|
||||
MBIMJSONDataResponse *response = [MBIMJSONDataResponse responseWithJSONObject:conversations];
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMDeleteConversationOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 5/25/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMDeleteConversationOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// MBIMDeleteConversationOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 5/25/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMDeleteConversationOperation.h"
|
||||
#import "IMChat+Encoded.h"
|
||||
|
||||
#import "IMCore_ClassDump.h"
|
||||
|
||||
@implementation MBIMDeleteConversationOperation
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"delete";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
__block NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
NSString *guid = [self valueForQueryItemWithName:@"guid"];
|
||||
|
||||
if (!guid) {
|
||||
MBIMLogInfo(@"No conversation GUID provided.");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMChat *chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
MBIMLogInfo(@"Chat with guid: %@ not found", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
} else {
|
||||
[chat remove];
|
||||
}
|
||||
});
|
||||
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMFetchAttachmentOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/20/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMFetchAttachmentOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,135 @@
|
||||
//
|
||||
// MBIMFetchAttachmentOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/20/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMFetchAttachmentOperation.h"
|
||||
#import "MBIMDataResponse.h"
|
||||
#import "MBIMImageUtils.h"
|
||||
|
||||
#import "IMCore_ClassDump.h"
|
||||
#import "IMSharedUtilities_ClassDump.h"
|
||||
#import <CoreGraphics/CoreGraphics.h>
|
||||
|
||||
@implementation MBIMFetchAttachmentOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"attachment";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
BOOL preview = [[self valueForQueryItemWithName:@"preview"] boolValue];
|
||||
NSString *guid = [self valueForQueryItemWithName:@"guid"];
|
||||
if (!guid) {
|
||||
MBIMLogInfo(@"No query item provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid];
|
||||
if (!transfer) {
|
||||
MBIMLogInfo(@"No transfer found for guid: %@", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
|
||||
break;
|
||||
}
|
||||
|
||||
if (![transfer existsAtLocalPath]) {
|
||||
MBIMLogInfo(@"We don't have the file for this yet (still downloading to server?)");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
|
||||
break;
|
||||
}
|
||||
|
||||
NSData *responseData = nil;
|
||||
NSURL *localURL = [transfer localURL];
|
||||
NSString *extension = [[localURL pathExtension] lowercaseString];
|
||||
if (preview) {
|
||||
IMPreviewConstraints constraints = IMPreviewConstraintsZero();
|
||||
|
||||
// Fetch preview constraints from transfer
|
||||
NSDictionary *previewConstraintsDict = [[transfer attributionInfo] objectForKey:@"pgenszc"];
|
||||
if (previewConstraintsDict) {
|
||||
constraints = IMPreviewConstraintsFromDictionary(previewConstraintsDict);
|
||||
} else {
|
||||
// Or, make a guess.
|
||||
constraints.maxPxWidth = 500.0;
|
||||
constraints.scale = 1.0;
|
||||
}
|
||||
|
||||
NSURL *previewURL = IMAttachmentPreviewFileURL(localURL, extension, YES);
|
||||
if (!previewURL) {
|
||||
// I'm not sure why this sometimes returns nil...
|
||||
MBIMLogInfo(@"Unable to generate attachment preview cache URL for %@, making one up.", localURL);
|
||||
|
||||
NSURL *temporaryAttachmentCache = [[[NSFileManager defaultManager] temporaryDirectory] URLByAppendingPathComponent:@"kordophone_attachment_cache"];
|
||||
temporaryAttachmentCache = [temporaryAttachmentCache URLByAppendingPathComponent:guid];
|
||||
|
||||
[[NSFileManager defaultManager] createDirectoryAtURL:temporaryAttachmentCache withIntermediateDirectories:YES attributes:nil error:nil];
|
||||
|
||||
previewURL = [temporaryAttachmentCache URLByAppendingPathComponent:[localURL lastPathComponent]];
|
||||
}
|
||||
|
||||
if (![[NSFileManager defaultManager] fileExistsAtPath:[previewURL path]]) {
|
||||
MBIMLogInfo(@"Generating preview image for guid: %@ at %@", guid, [previewURL path]);
|
||||
|
||||
// Generate preview using preview generator manager
|
||||
NSError *error = nil;
|
||||
IMPreviewGeneratorManager *generator = [IMPreviewGeneratorManager sharedInstance];
|
||||
CGImageRef previewImage = [generator newPreviewFromSourceURL:localURL withPreviewConstraints:constraints error:&error];
|
||||
if (error) {
|
||||
MBIMLogInfo(@"Unable to generate preview for attachment guid: %@", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
// Convert to JPEG.
|
||||
responseData = MBIMCGImageJPEGRepresentation(previewImage);
|
||||
|
||||
// Persist JPEG preview to disk
|
||||
if (previewURL) {
|
||||
[responseData writeToURL:previewURL atomically:YES];
|
||||
}
|
||||
} else {
|
||||
// File exists
|
||||
MBIMLogInfo(@"Using cached preview image for guid: %@ at %@", guid, [previewURL path]);
|
||||
responseData = [NSData dataWithContentsOfURL:previewURL];
|
||||
}
|
||||
} else {
|
||||
responseData = [NSData dataWithContentsOfURL:localURL];
|
||||
}
|
||||
|
||||
if (!responseData) {
|
||||
MBIMLogInfo(@"Wasn't able to load data for guid: %@", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *mimeType = [transfer mimeType];
|
||||
if ([mimeType isEqualToString:@"image/heic"]) {
|
||||
// TODO: We should convert this to JPEG here. I don't want clients to have to deal with HEIC.
|
||||
MBIMLogInfo(@"WARNING: Returning HEIC data for attachment %@", guid);
|
||||
}
|
||||
|
||||
// It's unusual, but if this is nil, try to guess the MIME type based on the filename
|
||||
if (!mimeType) {
|
||||
// XXX: REALLY hacky
|
||||
mimeType = [NSString stringWithFormat:@"image/%@", extension];
|
||||
}
|
||||
|
||||
MBIMDataResponse *dataResponse = [[MBIMDataResponse alloc] initWithData:responseData contentType:mimeType];
|
||||
dataResponse.httpHeaders[@"Cache-Control"] = @"public, immutable, max-age=31536000";
|
||||
response = dataResponse;
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
17
server/kordophone/Bridge/Operations/MBIMMarkOperation.h
Normal file
17
server/kordophone/Bridge/Operations/MBIMMarkOperation.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMMarkOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/19/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMMarkOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
53
server/kordophone/Bridge/Operations/MBIMMarkOperation.m
Normal file
53
server/kordophone/Bridge/Operations/MBIMMarkOperation.m
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MBIMMarkOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/19/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMMarkOperation.h"
|
||||
#import "IMCore_ClassDump.h"
|
||||
|
||||
@implementation MBIMMarkOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"markConversation";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
__block NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
NSString *guid = [self valueForQueryItemWithName:@"guid"];
|
||||
|
||||
if (!guid) {
|
||||
MBIMLogInfo(@"No query item provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMChat *chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
MBIMLogInfo(@"Chat with guid: %@ not found", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
} else {
|
||||
// TODO: be smarter about this and mark individual messages as read? Could lead
|
||||
// to a race condition
|
||||
if ([chat unreadMessageCount] > 0) {
|
||||
[chat markAllMessagesAsRead];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMMessagesListOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMMessagesListOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
149
server/kordophone/Bridge/Operations/MBIMMessagesListOperation.m
Normal file
149
server/kordophone/Bridge/Operations/MBIMMessagesListOperation.m
Normal file
@@ -0,0 +1,149 @@
|
||||
//
|
||||
// MBIMMessagesListOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMMessagesListOperation.h"
|
||||
#import "MBIMHTTPUtilities.h"
|
||||
#import "IMMessageItem+Encoded.h"
|
||||
#import "MBIMErrorResponse.h"
|
||||
|
||||
#import "IMCore_ClassDump.h"
|
||||
|
||||
#define kDefaultMessagesLimit 75
|
||||
|
||||
@interface IMChat (MBIMSafeMessagesLoading)
|
||||
- (void)load:(NSUInteger)number messagesBeforeGUID:(NSString *)beforeMessageGUID;
|
||||
- (void)load:(NSUInteger)number messagesAfterGUID:(NSString *)afterMessageGUID;
|
||||
@end
|
||||
|
||||
@implementation MBIMMessagesListOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"messages";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
__block NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
// Required parameters
|
||||
NSString *guid = [self valueForQueryItemWithName:@"guid"];
|
||||
|
||||
// Optional
|
||||
NSString *limitValue = [self valueForQueryItemWithName:@"limit"];
|
||||
|
||||
NSDate *beforeDate = nil;
|
||||
NSString *beforeDateValue = [self valueForQueryItemWithName:@"beforeDate"];
|
||||
if (beforeDateValue) {
|
||||
beforeDate = [beforeDateValue ISO8601Date];
|
||||
if (!beforeDate) {
|
||||
response = [[MBIMErrorResponse alloc] initWithErrorCode:500 message:@"Unable to decode ISO8601 beforeDate value"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
NSString *beforeMessageGUID = [self valueForQueryItemWithName:@"beforeMessageGUID"];
|
||||
NSString *afterMessageGUID = [self valueForQueryItemWithName:@"afterMessageGUID"];
|
||||
|
||||
if (beforeMessageGUID && afterMessageGUID) {
|
||||
response = [[MBIMErrorResponse alloc] initWithErrorCode:500 message:@"Cannot provide both beforeMessageGUID and afterMessageGUID params."];
|
||||
break;
|
||||
}
|
||||
|
||||
if (!guid) {
|
||||
response = [[MBIMErrorResponse alloc] initWithErrorCode:500 message:@"No GUID provided."];
|
||||
break;
|
||||
}
|
||||
|
||||
__block NSMutableArray *messages = [NSMutableArray array];
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMChat *chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
MBIMLogInfo(@"Chat with guid: %@ not found", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
} else {
|
||||
// Load messages
|
||||
// (Must be done on main queue for some reason)
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
NSUInteger limit = kDefaultMessagesLimit;
|
||||
if (limitValue) {
|
||||
limit = [limitValue integerValue];
|
||||
}
|
||||
|
||||
if (beforeMessageGUID) {
|
||||
[chat load:limit messagesBeforeGUID:beforeMessageGUID];
|
||||
} else if (afterMessageGUID) {
|
||||
[chat load:limit messagesAfterGUID:afterMessageGUID];
|
||||
} else {
|
||||
[chat loadMessagesBeforeDate:beforeDate limit:limit loadImmediately:YES];
|
||||
}
|
||||
|
||||
IMMessage *beforeMessage = beforeMessageGUID ? [chat messageForGUID:beforeMessageGUID] : nil;
|
||||
IMMessage *afterMessage = afterMessageGUID ? [chat messageForGUID:afterMessageGUID] : nil;
|
||||
|
||||
[[chat chatItems] enumerateMessagesWithOptions:0 usingBlock:^(IMMessage *message, BOOL *stop) {
|
||||
BOOL includeMessage = YES;
|
||||
NSDate *messageDate = [message time];
|
||||
if (beforeMessage && [[beforeMessage time] compare:messageDate] != NSOrderedDescending) {
|
||||
includeMessage = NO;
|
||||
}
|
||||
|
||||
if (afterMessage && [[afterMessage time] compare:messageDate] != NSOrderedAscending) {
|
||||
includeMessage = NO;
|
||||
}
|
||||
|
||||
if (includeMessage) {
|
||||
NSDictionary *messageDict = [message mbim_dictionaryRepresentation];
|
||||
[messages addObject:messageDict];
|
||||
}
|
||||
}];
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
response = [MBIMJSONDataResponse responseWithJSONObject:messages];
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation IMChat (MBIMSafeMessagesLoading)
|
||||
|
||||
- (id)_safe_loadMessagesBeforeAndAfterGUID:(NSString *)messagesGUID numberOfMessagesToLoadBeforeGUID:(NSUInteger)limitBefore numberOfMessagesToLoadAfterGUID:(NSUInteger)limitAfter loadImmediately:(BOOL)loadImmediately
|
||||
{
|
||||
if ([self respondsToSelector:@selector(loadMessagesBeforeAndAfterGUID:
|
||||
numberOfMessagesToLoadBeforeGUID:
|
||||
numberOfMessagesToLoadAfterGUID:
|
||||
loadImmediately:threadIdentifier:)]) {
|
||||
return [self loadMessagesBeforeAndAfterGUID:messagesGUID
|
||||
numberOfMessagesToLoadBeforeGUID:limitBefore
|
||||
numberOfMessagesToLoadAfterGUID:limitAfter
|
||||
loadImmediately:YES threadIdentifier:nil];
|
||||
} else {
|
||||
return [self loadMessagesBeforeAndAfterGUID:messagesGUID
|
||||
numberOfMessagesToLoadBeforeGUID:limitBefore
|
||||
numberOfMessagesToLoadAfterGUID:limitAfter
|
||||
loadImmediately:YES];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)load:(NSUInteger)number messagesBeforeGUID:(NSString *)beforeMessageGUID
|
||||
{
|
||||
[self _safe_loadMessagesBeforeAndAfterGUID:beforeMessageGUID numberOfMessagesToLoadBeforeGUID:number numberOfMessagesToLoadAfterGUID:0 loadImmediately:YES];
|
||||
}
|
||||
|
||||
- (void)load:(NSUInteger)number messagesAfterGUID:(NSString *)afterMessageGUID
|
||||
{
|
||||
[self _safe_loadMessagesBeforeAndAfterGUID:afterMessageGUID numberOfMessagesToLoadBeforeGUID:0 numberOfMessagesToLoadAfterGUID:number loadImmediately:YES];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMResolveHandleOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 10/1/24.
|
||||
// Copyright © 2024 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMResolveHandleOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
134
server/kordophone/Bridge/Operations/MBIMResolveHandleOperation.m
Normal file
134
server/kordophone/Bridge/Operations/MBIMResolveHandleOperation.m
Normal file
@@ -0,0 +1,134 @@
|
||||
//
|
||||
// MBIMResolveHandleOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 10/1/24.
|
||||
// Copyright © 2024 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMResolveHandleOperation.h"
|
||||
|
||||
#import "IMCore_ClassDump.h"
|
||||
#import "IMMessageItem+Encoded.h"
|
||||
|
||||
/*
|
||||
# Response:
|
||||
Dictionary {
|
||||
"resolvedHandle": <Encoded IMHandle>
|
||||
"status": valid | invalid | unknown,
|
||||
"existingChat": chatGUID | null
|
||||
}
|
||||
|
||||
# IMHandle:
|
||||
Dictionary {
|
||||
"id" : resolvedID
|
||||
"name" : fullName | null
|
||||
}
|
||||
*/
|
||||
|
||||
@interface NSNumber (IDSStatusUtility)
|
||||
- (NSString *)_idsStatusDescription;
|
||||
@end
|
||||
|
||||
@implementation NSNumber (IDSStatusUtility)
|
||||
- (NSString *)_idsStatusDescription
|
||||
{
|
||||
switch ([self unsignedIntegerValue]) {
|
||||
case 1: return @"valid";
|
||||
case 2: return @"invalid";
|
||||
default:
|
||||
return @"unknown";
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
@interface IMHandle (Encoded)
|
||||
- (NSDictionary *)mbim_dictionaryRepresentation;
|
||||
@end
|
||||
|
||||
@implementation IMHandle (Encoded)
|
||||
- (NSDictionary *)mbim_dictionaryRepresentation
|
||||
{
|
||||
return @{
|
||||
@"id" : [self ID],
|
||||
@"name" : [self name] ?: [NSNull null],
|
||||
};
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MBIMResolveHandleOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"resolveHandle";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSString *specifiedIdentifier = [[self valueForQueryItemWithName:@"id"] _stripFZIDPrefix];
|
||||
if (!specifiedIdentifier) {
|
||||
MBIMLogError(@"No handle ID provided.");
|
||||
HTTPErrorResponse *response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
self.serverCompletionBlock(response);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (IMStringIsPhoneNumber(specifiedIdentifier)) {
|
||||
// Phone numbers will require a country code guess here.
|
||||
// Passing nil, I presume, will make some kind of guess for the country code (useNetworkCountryCode).
|
||||
specifiedIdentifier = IMCopyIDForPhoneNumber(specifiedIdentifier, nil, YES);
|
||||
}
|
||||
|
||||
NSString *canonicalAddress = [specifiedIdentifier _bestGuessURI];
|
||||
|
||||
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
|
||||
IMHandle *loginHandle = [iMessageAccount loginIMHandle];
|
||||
|
||||
NSString *lastAddressedHandle = [[[loginHandle ID] _stripFZIDPrefix] _bestGuessURI];
|
||||
IMChatCalculateServiceForSendingNewComposeMaybeForce(@[ canonicalAddress ], lastAddressedHandle, nil, NO, IMStringIsEmail(canonicalAddress), NO, NO, NO, [iMessageAccount service], ^(BOOL allAddressesiMessageCapable, NSDictionary * _Nullable perRecipientAvailability, BOOL checkedServer, IMChatServiceForSendingAvailabilityError error)
|
||||
{
|
||||
NSError *encodingError = nil;
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
|
||||
do {
|
||||
// Assume we have returned just one key here.
|
||||
NSString *resolvedHandleID = [[perRecipientAvailability allKeys] firstObject];
|
||||
if (!resolvedHandleID) {
|
||||
MBIMLogError(@"Unexpected missing handle id");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
IMHandle *resolvedHandle = [iMessageAccount imHandleWithID:resolvedHandleID];
|
||||
if (!resolvedHandle) {
|
||||
MBIMLogError(@"Couldn't resolve handle");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *handleStatus = [[perRecipientAvailability objectForKey:resolvedHandleID] _idsStatusDescription];
|
||||
|
||||
IMChat *existingChat = [[IMChatRegistry sharedInstance] existingChatForIMHandle:resolvedHandle];
|
||||
|
||||
NSDictionary *responseDict = @{
|
||||
@"resolvedHandle" : [resolvedHandle mbim_dictionaryRepresentation],
|
||||
@"status" : handleStatus,
|
||||
@"existingChat" : [existingChat guid] ?: [NSNull null],
|
||||
};
|
||||
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:responseDict options:0 error:&encodingError];
|
||||
if (encodingError) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
} else {
|
||||
response = [[MBIMDataResponse alloc] initWithData:data contentType:@"application/json"];
|
||||
}
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMSendMessageOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMSendMessageOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
122
server/kordophone/Bridge/Operations/MBIMSendMessageOperation.m
Normal file
122
server/kordophone/Bridge/Operations/MBIMSendMessageOperation.m
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// MBIMSendMessageOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMSendMessageOperation.h"
|
||||
|
||||
#import "IMCore_ClassDump.h"
|
||||
#import "IMMessageItem+Encoded.h"
|
||||
|
||||
@implementation MBIMSendMessageOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"sendMessage";
|
||||
}
|
||||
|
||||
- (IMMessage *)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID attachmentGUIDs:(NSArray<NSString *> *)guids
|
||||
{
|
||||
__block IMMessage *result = nil;
|
||||
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMChat *chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:chatGUID];
|
||||
|
||||
// TODO: chat might not be an iMessage chat!
|
||||
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
|
||||
IMHandle *senderHandle = [iMessageAccount loginIMHandle];
|
||||
|
||||
NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:messageBody];
|
||||
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) {
|
||||
MBIMLogInfo(@"Chat does not exist: %@", chatGUID);
|
||||
} else {
|
||||
result = reply;
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[chat sendMessage:reply];
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#if 0
|
||||
- (NSDictionary *)adjustMessageSummaryInfoForSending:(NSDictionary *)messageSummaryInfo
|
||||
{
|
||||
NSMutableDictionary *adjustedInfo = [messageSummaryInfo mutableCopy];
|
||||
if (!adjustedInfo) {
|
||||
adjustedInfo = [NSMutableDictionary dictionary];
|
||||
}
|
||||
|
||||
if ([fullText length] > 50) {
|
||||
summary = [[summary substringToIndex:[summary rangeOfComposedCharacterSequenceAtIndex:kMaxSummaryLength].location] stringByAppendingString:@"…"];
|
||||
adjustedInfo[IMMessageSummaryInfoSummary] = summary;
|
||||
}
|
||||
|
||||
|
||||
adjustedInfo[IMMessageSummaryInfoTapbackRepresentationKey] = @"Loved";
|
||||
|
||||
return adjustedInfo;
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSObject<HTTPResponse> *response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
|
||||
NSError *error = nil;
|
||||
NSDictionary *args = [NSJSONSerialization JSONObjectWithData:self.requestBodyData options:0 error:&error];
|
||||
if (error || args.count == 0) {
|
||||
self.serverCompletionBlock(response);
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *guid = [args objectForKey:@"guid"];
|
||||
NSString *messageBody = [args objectForKey:@"body"];
|
||||
if (!guid || !messageBody) {
|
||||
self.serverCompletionBlock(response);
|
||||
return;
|
||||
}
|
||||
|
||||
// tapbacks
|
||||
#if 0
|
||||
IMMessage *acknowledgment = [IMMessage instantMessageWithAssociatedMessageContent: /* [NSString stringWithFormat:@"%@ \"%%@\"", tapbackAction] */
|
||||
flags:0
|
||||
associatedMessageGUID:guid
|
||||
associatedMessageType:IMAssociatedMessageTypeAcknowledgmentHeart
|
||||
associatedMessageRange:[imMessage messagePartRange]
|
||||
messageSummaryInfo:[self adjustMessageSummaryInfoForSending:message]
|
||||
threadIdentifier:[imMessage threadIdentifier]];
|
||||
#endif
|
||||
|
||||
NSArray *transferGUIDs = [args objectForKey:@"fileTransferGUIDs"];
|
||||
if (!transferGUIDs) {
|
||||
transferGUIDs = @[];
|
||||
}
|
||||
|
||||
IMMessage *result = [self _sendMessage:messageBody toChatWithGUID:guid attachmentGUIDs:transferGUIDs];
|
||||
if (result) {
|
||||
response = [MBIMJSONDataResponse responseWithJSONObject:[result mbim_dictionaryRepresentation]];
|
||||
}
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
17
server/kordophone/Bridge/Operations/MBIMStatusOperation.h
Normal file
17
server/kordophone/Bridge/Operations/MBIMStatusOperation.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMStatusOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 8/3/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMStatusOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
25
server/kordophone/Bridge/Operations/MBIMStatusOperation.m
Normal file
25
server/kordophone/Bridge/Operations/MBIMStatusOperation.m
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// MBIMStatusOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 8/3/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMStatusOperation.h"
|
||||
|
||||
@implementation MBIMStatusOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"status";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
self.serverCompletionBlock([[MBIMDataResponse alloc] initWithData:[@"OK" dataUsingEncoding:NSUTF8StringEncoding]]);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMUpdatePollOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMUpdatePollOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// MBIMUpdatePollOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMUpdatePollOperation.h"
|
||||
#import "MBIMUpdateQueue.h"
|
||||
|
||||
@implementation MBIMUpdatePollOperation {
|
||||
__strong MBIMUpdateConsumer _updateConsumer;
|
||||
}
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"pollUpdates";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSInteger messageSeq = -1;
|
||||
NSString *messageSeqString = [self valueForQueryItemWithName:@"seq"];
|
||||
if (messageSeqString) {
|
||||
messageSeq = [messageSeqString integerValue];
|
||||
}
|
||||
|
||||
__weak __auto_type weakSelf = self;
|
||||
_updateConsumer = ^(NSArray<MBIMUpdateItem *> *updates) {
|
||||
NSMutableArray *encodedUpdates = [NSMutableArray array];
|
||||
for (MBIMUpdateItem *item in updates) {
|
||||
NSDictionary *updateDict = [item dictionaryRepresentation];
|
||||
[encodedUpdates addObject:updateDict];
|
||||
}
|
||||
|
||||
MBIMJSONDataResponse *response = [MBIMJSONDataResponse responseWithJSONObject:encodedUpdates];
|
||||
weakSelf.serverCompletionBlock(response);
|
||||
};
|
||||
|
||||
[[MBIMUpdateQueue sharedInstance] addPollingConsumer:_updateConsumer withLastSyncedMessageSeq:messageSeq];
|
||||
}
|
||||
|
||||
- (void)cancel
|
||||
{
|
||||
[super cancel];
|
||||
[[MBIMUpdateQueue sharedInstance] removePollingConsumer:_updateConsumer];
|
||||
}
|
||||
|
||||
- (NSObject<HTTPResponse> *)cancelAndReturnTimeoutResponse
|
||||
{
|
||||
[self cancel];
|
||||
return [[HTTPErrorResponse alloc] initWithErrorCode:205]; // 205 = nothing to report
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -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
|
||||
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// 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 "IMCore_ClassDump.h"
|
||||
|
||||
@implementation MBIMUploadAttachmentOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"uploadAttachment";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
NSString *filename = [self valueForQueryItemWithName:@"filename"];
|
||||
if ([filename length] == 0) {
|
||||
MBIMLogInfo(@"No filename provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSData *attachmentData = self.requestBodyData;
|
||||
if ([attachmentData length] == 0) {
|
||||
MBIMLogInfo(@"No attachment data in request");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
// Sanitize filename
|
||||
NSCharacterSet *dotCharacter = [NSCharacterSet characterSetWithCharactersInString:@"."];
|
||||
NSCharacterSet *illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@"/\\?%*|\"<>"];
|
||||
NSString *sanitizedFilename = [[[filename componentsSeparatedByCharactersInSet:illegalFileNameCharacters]
|
||||
componentsJoinedByString:@"-"]
|
||||
stringByTrimmingCharactersInSet:dotCharacter];
|
||||
|
||||
NSString *localPath = [NSTemporaryDirectory() stringByAppendingPathComponent:sanitizedFilename];
|
||||
NSURL *localURL = [NSURL fileURLWithPath:localPath];
|
||||
BOOL success = [attachmentData writeToURL:localURL atomically:NO];
|
||||
if (!success) {
|
||||
MBIMLogInfo(@"Error writing attachment to temporary directory");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *guid = [[IMFileTransferCenter sharedInstance] guidForNewOutgoingTransferWithLocalURL:localURL];
|
||||
if (!guid) {
|
||||
MBIMLogInfo(@"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
|
||||
17
server/kordophone/Bridge/Operations/MBIMVersionOperation.h
Normal file
17
server/kordophone/Bridge/Operations/MBIMVersionOperation.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMVersionOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 8/3/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMVersionOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
40
server/kordophone/Bridge/Operations/MBIMVersionOperation.m
Normal file
40
server/kordophone/Bridge/Operations/MBIMVersionOperation.m
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// MBIMVersionOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 8/3/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMVersionOperation.h"
|
||||
#import "MBIMErrorResponse.h"
|
||||
|
||||
#ifdef __clang_analyzer__
|
||||
const char* MBIMVersion() {
|
||||
return "UNKNOWN";
|
||||
}
|
||||
#else
|
||||
#import "MBIMVersion.c"
|
||||
#endif
|
||||
|
||||
@implementation MBIMVersionOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (BOOL)requiresAuthentication
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"version";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSString *versionString = [NSString stringWithUTF8String:MBIMVersion()];
|
||||
self.serverCompletionBlock([[MBIMDataResponse alloc] initWithData:[versionString dataUsingEncoding:NSUTF8StringEncoding]]);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// MBIMDataResponse.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/20/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "HTTPDataResponse.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMDataResponse : HTTPDataResponse
|
||||
@property (nonatomic, readonly) NSMutableDictionary *httpHeaders;
|
||||
- (instancetype)initWithData:(NSData *)data contentType:(NSString *)contentType;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// MBIMDataResponse.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/20/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMDataResponse.h"
|
||||
|
||||
@implementation MBIMDataResponse {
|
||||
NSString *_contentType;
|
||||
NSMutableDictionary *_httpHeaders;
|
||||
}
|
||||
|
||||
- (instancetype)initWithData:(NSData *)data contentType:(NSString *)contentType
|
||||
{
|
||||
self = [super initWithData:data];
|
||||
if (self) {
|
||||
_contentType = contentType;
|
||||
_httpHeaders = [@{
|
||||
@"Content-Type" : _contentType ?: @"application/octet-stream",
|
||||
@"Access-Control-Allow-Origin" : @"*", // CORS
|
||||
@"Access-Control-Allow-Credentials" : @"true"
|
||||
} mutableCopy];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSDictionary *)httpHeaders
|
||||
{
|
||||
return _httpHeaders;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// MBIMErrorResponse.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 8/3/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "HTTPDataResponse.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMErrorResponse : HTTPDataResponse
|
||||
- (instancetype)initWithErrorCode:(int)httpErrorCode;
|
||||
- (instancetype)initWithErrorCode:(int)httpErrorCode message:(NSString *)message;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// MBIMErrorResponse.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 8/3/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMErrorResponse.h"
|
||||
|
||||
@implementation MBIMErrorResponse {
|
||||
int _status;
|
||||
}
|
||||
|
||||
- (instancetype)initWithErrorCode:(int)httpErrorCode
|
||||
{
|
||||
if (self = [super initWithData:nil]) {
|
||||
_status = httpErrorCode;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithErrorCode:(int)httpErrorCode message:(NSString *)message
|
||||
{
|
||||
if (self = [super initWithData:[message dataUsingEncoding:NSUTF8StringEncoding]]) {
|
||||
_status = httpErrorCode;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSInteger)status
|
||||
{
|
||||
return _status;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// MBIMHTTPUtilities.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NSString* MBIMWebServerFormatRFC822(NSDate *date);
|
||||
NSString* MBIMWebServerFormatISO8601(NSDate *date);
|
||||
|
||||
@interface NSDate (MBIMWebServerFormat)
|
||||
- (NSString *)RFC822StringValue;
|
||||
- (NSString *)ISO8601StringValue;
|
||||
@end
|
||||
|
||||
@interface NSString (MBIMWebServerFormat)
|
||||
- (NSDate *)RFC822Date;
|
||||
- (NSDate *)ISO8601Date;
|
||||
@end
|
||||
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// MBIMHTTPUtilities.c
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#include "MBIMHTTPUtilities.h"
|
||||
|
||||
static NSDateFormatter* _dateFormatterRFC822 = nil;
|
||||
static NSDateFormatter* _dateFormatterISO8601 = nil;
|
||||
static dispatch_queue_t _dateFormatterQueue = NULL;
|
||||
|
||||
__attribute__((constructor))
|
||||
static void __InitializeDateFormatter()
|
||||
{
|
||||
_dateFormatterQueue = dispatch_queue_create("dateFormatter", DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
_dateFormatterRFC822 = [[NSDateFormatter alloc] init];
|
||||
_dateFormatterRFC822.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
|
||||
_dateFormatterRFC822.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
|
||||
_dateFormatterRFC822.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
|
||||
|
||||
_dateFormatterISO8601 = [[NSDateFormatter alloc] init];
|
||||
_dateFormatterISO8601.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
|
||||
_dateFormatterISO8601.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'+00:00'";
|
||||
_dateFormatterISO8601.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
|
||||
}
|
||||
|
||||
NSString* MBIMWebServerFormatRFC822(NSDate *date)
|
||||
{
|
||||
__block NSString *string = nil;
|
||||
dispatch_sync(_dateFormatterQueue, ^{
|
||||
string = [_dateFormatterRFC822 stringFromDate:date];
|
||||
});
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
NSString* MBIMWebServerFormatISO8601(NSDate *date)
|
||||
{
|
||||
__block NSString *string = nil;
|
||||
dispatch_sync(_dateFormatterQueue, ^{
|
||||
string = [_dateFormatterISO8601 stringFromDate:date];
|
||||
});
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
@implementation NSDate (MBIMWebServerFormat)
|
||||
|
||||
- (NSString *)RFC822StringValue
|
||||
{
|
||||
return MBIMWebServerFormatRFC822(self);
|
||||
}
|
||||
|
||||
- (NSString *)ISO8601StringValue
|
||||
{
|
||||
return MBIMWebServerFormatISO8601(self);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSString (MBIMWebServerFormat)
|
||||
|
||||
- (NSDate *)RFC822Date
|
||||
{
|
||||
return [_dateFormatterRFC822 dateFromString:self];
|
||||
}
|
||||
|
||||
- (NSDate *)ISO8601Date
|
||||
{
|
||||
return [_dateFormatterISO8601 dateFromString:self];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// MBIMImageUtils.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 12/20/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CoreGraphics/CoreGraphics.h>
|
||||
|
||||
extern NSData* MBIMCGImageJPEGRepresentation(CGImageRef imageRef);
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// MBIMImageUtils.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 12/20/22.
|
||||
// Copyright © 2022 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMImageUtils.h"
|
||||
#import <ImageIO/ImageIO.h>
|
||||
|
||||
NSData* MBIMCGImageJPEGRepresentation(CGImageRef imageRef)
|
||||
{
|
||||
if (imageRef == NULL) return nil;
|
||||
|
||||
NSNumber *const DPI = @72.0;
|
||||
NSNumber *const compressionQuality = @0.9;
|
||||
NSDictionary *properties = @{
|
||||
(__bridge NSString *)kCGImagePropertyDPIWidth : DPI,
|
||||
(__bridge NSString *)kCGImagePropertyDPIHeight : DPI,
|
||||
(__bridge NSString *)kCGImageDestinationLossyCompressionQuality : compressionQuality,
|
||||
};
|
||||
|
||||
bool success = false;
|
||||
NSMutableData *data = [NSMutableData data];
|
||||
if (data) {
|
||||
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((CFMutableDataRef)data, CFSTR("public.jpeg"), 1/*count*/, NULL/*options*/);
|
||||
if (imageDestination != NULL) {
|
||||
CGImageDestinationAddImage(imageDestination, imageRef, (__bridge CFDictionaryRef)properties);
|
||||
success = CGImageDestinationFinalize(imageDestination);
|
||||
CFRelease(imageDestination);
|
||||
}
|
||||
}
|
||||
|
||||
return success ? data : nil;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// MBIMJSONDataResponse.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MBIMDataResponse.h"
|
||||
|
||||
@interface MBIMJSONDataResponse : MBIMDataResponse
|
||||
|
||||
+ (instancetype)responseWithJSONObject:(id)object;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// MBIMJSONDataResponse.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMJSONDataResponse.h"
|
||||
|
||||
@implementation MBIMJSONDataResponse
|
||||
|
||||
+ (instancetype)responseWithJSONObject:(id)object
|
||||
{
|
||||
NSError *error = nil;
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
|
||||
if (data == nil) {
|
||||
NSLog(@"JSON encoding error: %@", error);
|
||||
return nil;
|
||||
}
|
||||
|
||||
MBIMJSONDataResponse *response = [[self alloc] initWithData:data contentType:@"application/json; charset=utf-8"];
|
||||
return response;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMURLUtilities.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 1/17/23.
|
||||
// Copyright © 2023 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSURL (MBIMURLUtilities)
|
||||
- (nullable NSString *)valueForQueryItemWithName:(NSString *)queryItemName;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// MBIMURLUtilities.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 1/17/23.
|
||||
// Copyright © 2023 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMURLUtilities.h"
|
||||
|
||||
@implementation NSURL (MBIMURLUtilities)
|
||||
|
||||
- (nullable NSString *)valueForQueryItemWithName:(NSString *)queryItemName
|
||||
{
|
||||
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:self resolvingAgainstBaseURL:NO];
|
||||
|
||||
NSString *value = nil;
|
||||
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
|
||||
if ([[queryItem name] isEqualToString:queryItemName]) {
|
||||
value = [queryItem value];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user