2018-11-12 22:11:36 -08:00
|
|
|
//
|
|
|
|
|
// MBIMBridge.m
|
|
|
|
|
// MessagesBridge
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 11/12/18.
|
|
|
|
|
// Copyright © 2018 James Magahern. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "MBIMBridge.h"
|
2018-11-13 12:29:15 -08:00
|
|
|
#import "MBIMBridgeOperation.h"
|
2018-11-13 22:39:03 -08:00
|
|
|
#import "hooking.h"
|
2018-11-12 22:11:36 -08:00
|
|
|
|
|
|
|
|
#import <GCDWebServers/GCDWebServers.h>
|
|
|
|
|
|
|
|
|
|
#import <IMCore/IMCore.h>
|
|
|
|
|
#import <IMCore/IMCore_Private.h>
|
|
|
|
|
|
|
|
|
|
#import <IMFoundation/IMFoundation.h>
|
|
|
|
|
#import <IMFoundation/IMFoundation_Private.h>
|
|
|
|
|
|
2018-11-12 22:15:50 -08:00
|
|
|
static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
2018-11-12 22:11:36 -08:00
|
|
|
|
|
|
|
|
@interface MBIMBridge (/* INTERNAL */)
|
|
|
|
|
@property (nonatomic, strong) GCDWebServer *webServer;
|
2018-11-13 12:29:15 -08:00
|
|
|
@property (nonatomic, strong) NSOperationQueue *operationQueue;
|
2018-11-12 22:11:36 -08:00
|
|
|
|
|
|
|
|
- (instancetype)_init;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation MBIMBridge
|
|
|
|
|
|
|
|
|
|
+ (instancetype)sharedInstance
|
|
|
|
|
{
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
static __strong MBIMBridge *sharedBridge = nil;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
sharedBridge = [[MBIMBridge alloc] _init];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return sharedBridge;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)_init
|
|
|
|
|
{
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
[self registerForNotifications];
|
|
|
|
|
[self startWebServer];
|
|
|
|
|
|
|
|
|
|
[sDaemonController setDelegate:self];
|
|
|
|
|
[sDaemonListener addHandler:self];
|
2018-11-13 12:29:15 -08:00
|
|
|
|
|
|
|
|
_operationQueue = [[NSOperationQueue alloc] init];
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 22:39:03 -08:00
|
|
|
- (void)_terminate
|
|
|
|
|
{
|
|
|
|
|
// *shrug*
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-12 22:11:36 -08:00
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark Connection
|
|
|
|
|
|
|
|
|
|
- (void)connect
|
|
|
|
|
{
|
2018-11-13 22:39:03 -08:00
|
|
|
#if HOOK_IMAGENT
|
|
|
|
|
char *errorString = nil;
|
|
|
|
|
BOOL hooked = HookIMAgent(self.dylibPath, &errorString);
|
|
|
|
|
if (!hooked) {
|
|
|
|
|
NSString *errorNSString = [NSString stringWithUTF8String:errorString];
|
|
|
|
|
NSLog(@"Error hooking imagent: %@", errorNSString);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-11-12 22:11:36 -08:00
|
|
|
if (![sDaemonController hasListenerForID: MBIMBridgeToken]) {
|
|
|
|
|
if (![sDaemonController addListenerID:MBIMBridgeToken capabilities:(kFZListenerCapFileTransfers | kFZListenerCapManageStatus | kFZListenerCapChats | kFZListenerCapMessageHistory | kFZListenerCapIDQueries | kFZListenerCapSendMessages)]) {
|
|
|
|
|
NSLog(@"Failed to connect to imagent");
|
2018-11-13 22:39:03 -08:00
|
|
|
|
|
|
|
|
[self _terminate];
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)disconnect
|
|
|
|
|
{
|
|
|
|
|
[sDaemonController removeListenerID:MBIMBridgeToken];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark Notifications
|
|
|
|
|
|
|
|
|
|
- (void)registerForNotifications
|
|
|
|
|
{
|
|
|
|
|
(void)[IMChatRegistry sharedInstance];
|
|
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_messageReceived:) name:IMChatMessageReceivedNotification object:nil];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_chatRegistryDidLoad:) name:IMChatRegistryDidLoadNotification object:nil];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_chatItemsDidChange:) name:IMChatItemsDidChangeNotification object:nil];
|
|
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName: IMChatRegistryUnreadCountChangedNotification
|
|
|
|
|
object: nil
|
|
|
|
|
queue: [NSOperationQueue mainQueue]
|
|
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
|
NSLog(@"Unread count changed: %d", (int)[[IMChatRegistry sharedInstance] unreadCount]);
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)_messageReceived:(NSNotification *)notification
|
|
|
|
|
{
|
2018-11-12 22:15:50 -08:00
|
|
|
NSLog(@"Received message from chat with GUID: %@", [[notification object] guid]);
|
2018-11-12 22:11:36 -08:00
|
|
|
|
2018-11-12 22:15:50 -08:00
|
|
|
// TODO: Notify observers or something
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)_chatRegistryDidLoad:(NSNotification *)notification
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Loaded chat registry. %lu existing chats", (unsigned long)[sChatRegistry numberOfExistingChats]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)_chatItemsDidChange:(NSNotification *)notification
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"chat items changed: %@", notification);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 12:29:15 -08:00
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark Web Server initialization
|
|
|
|
|
|
|
|
|
|
- (void)_handleAsyncServerRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completion
|
2018-11-12 22:11:36 -08:00
|
|
|
{
|
2018-11-13 12:29:15 -08:00
|
|
|
NSString *endpointName = [[request URL] lastPathComponent];
|
|
|
|
|
Class operationClass = [MBIMBridgeOperation operationClassForEndpointName:endpointName];
|
|
|
|
|
if (operationClass != nil) {
|
|
|
|
|
MBIMBridgeOperation *operation = [[operationClass alloc] initWithRequest:request completion:completion];
|
|
|
|
|
[[self operationQueue] addOperation:operation];
|
|
|
|
|
} else {
|
|
|
|
|
completion([GCDWebServerDataResponse responseWithStatusCode:404]);
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)startWebServer
|
|
|
|
|
{
|
|
|
|
|
[GCDWebServer setLogLevel:3];
|
|
|
|
|
|
|
|
|
|
__auto_type __weak weakSelf = self;
|
|
|
|
|
self.webServer = [[GCDWebServer alloc] init];
|
|
|
|
|
[self.webServer addDefaultHandlerForMethod:@"GET"
|
|
|
|
|
requestClass:[GCDWebServerRequest class]
|
2018-11-13 12:29:15 -08:00
|
|
|
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
|
2018-11-12 22:11:36 -08:00
|
|
|
|
|
|
|
|
[self.webServer addDefaultHandlerForMethod:@"POST"
|
|
|
|
|
requestClass:[GCDWebServerURLEncodedFormRequest class]
|
2018-11-13 12:29:15 -08:00
|
|
|
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
|
2018-11-12 22:11:36 -08:00
|
|
|
|
|
|
|
|
[self.webServer startWithPort:8080 bonjourName:nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
#pragma mark Daemon lifecycle
|
|
|
|
|
|
|
|
|
|
- (void)daemonControllerWillConnect
|
|
|
|
|
{
|
2018-11-12 22:15:50 -08:00
|
|
|
NSLog(@"Connecting to imagent...");
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)daemonControllerDidConnect
|
|
|
|
|
{
|
2018-11-12 22:15:50 -08:00
|
|
|
NSLog(@"imagent responded.");
|
2018-11-12 22:11:36 -08:00
|
|
|
|
|
|
|
|
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
|
|
|
|
|
if (iMessageAccount) {
|
2018-11-12 22:15:50 -08:00
|
|
|
NSLog(@"Successfully got accounts from imagent");
|
2018-11-12 22:11:36 -08:00
|
|
|
NSLog(@"iMessage account connected: %@", iMessageAccount);
|
|
|
|
|
} else {
|
2018-11-12 22:15:50 -08:00
|
|
|
NSLog(@"imagent returned no accounts (not entitled?)");
|
2018-11-13 22:39:03 -08:00
|
|
|
[self _terminate];
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)daemonControllerDidDisconnect
|
|
|
|
|
{
|
2018-11-12 22:15:50 -08:00
|
|
|
NSLog(@"Disconnected from imagent");
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)daemonConnectionLost
|
|
|
|
|
{
|
2018-11-12 22:15:50 -08:00
|
|
|
NSLog(@"Connection lost to imagent");
|
2018-11-12 22:11:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|