201 lines
5.5 KiB
Objective-C
201 lines
5.5 KiB
Objective-C
//
|
|
// MBIMBridge.m
|
|
// MessagesBridge
|
|
//
|
|
// Created by James Magahern on 11/12/18.
|
|
// Copyright © 2018 James Magahern. All rights reserved.
|
|
//
|
|
|
|
#import "MBIMBridge.h"
|
|
#import "MBIMBridgeOperation.h"
|
|
#import "MBIMConcurrentHTTPServer.h"
|
|
#import "MBIMHTTPConnection.h"
|
|
#import "MBIMUpdateQueue.h"
|
|
#import "hooking.h"
|
|
|
|
#import <CocoaHTTPServer/HTTPServer.h>
|
|
|
|
#import <IMCore/IMCore.h>
|
|
#import <IMCore/IMCore_Private.h>
|
|
|
|
#import <IMFoundation/IMFoundation.h>
|
|
#import <IMFoundation/IMFoundation_Private.h>
|
|
|
|
static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
|
|
|
@interface MBIMBridge (/* INTERNAL */)
|
|
@property (nonatomic, strong) MBIMConcurrentHTTPServer *httpServer;
|
|
@property (nonatomic, strong) NSOperationQueue *operationQueue;
|
|
|
|
- (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];
|
|
|
|
_operationQueue = [[NSOperationQueue alloc] init];
|
|
_operationQueue.maxConcurrentOperationCount = 5;
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)_terminate
|
|
{
|
|
// *shrug*
|
|
exit(0);
|
|
}
|
|
|
|
#pragma mark -
|
|
#pragma mark Connection
|
|
|
|
- (void)connect
|
|
{
|
|
#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
|
|
|
|
if (![sDaemonController hasListenerForID: MBIMBridgeToken]) {
|
|
if (![sDaemonController addListenerID:MBIMBridgeToken capabilities:(kFZListenerCapFileTransfers | kFZListenerCapManageStatus | kFZListenerCapChats | kFZListenerCapMessageHistory | kFZListenerCapIDQueries | kFZListenerCapSendMessages)]) {
|
|
NSLog(@"Failed to connect to imagent");
|
|
|
|
[self _terminate];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (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] addObserver:self selector:@selector(_unreadCountChanged:) name:IMChatRegistryUnreadCountChangedNotification object:nil];
|
|
}
|
|
|
|
- (void)_unreadCountChanged:(NSNotification *)notification
|
|
{
|
|
// Not a lot of useful information plumbed here...
|
|
}
|
|
|
|
- (void)_messageReceived:(NSNotification *)notification
|
|
{
|
|
NSLog(@"Received message from chat with GUID: %@", [[notification object] guid]);
|
|
|
|
IMChat *chat = [notification object];
|
|
IMMessage *message = [[notification userInfo] objectForKey:IMChatValueKey];
|
|
if (chat && message) {
|
|
if (![message isFromMe]) {
|
|
MBIMUpdateItem *updateItem = [[MBIMUpdateItem alloc] init];
|
|
updateItem.changedChat = chat;
|
|
updateItem.addedMessage = message;
|
|
|
|
[[MBIMUpdateQueue sharedInstance] enqueueUpdateItem:updateItem];
|
|
} else {
|
|
// TODO: care about messages from me?
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)_chatRegistryDidLoad:(NSNotification *)notification
|
|
{
|
|
NSLog(@"Loaded chat registry. %lu existing chats", (unsigned long)[sChatRegistry numberOfExistingChats]);
|
|
}
|
|
|
|
- (void)_chatItemsDidChange:(NSNotification *)notification
|
|
{
|
|
IMChat *chat = [notification object];
|
|
if (chat) {
|
|
NSLog(@"Chat items change for GUID: %@", [chat guid]);
|
|
|
|
MBIMUpdateItem *updateItem = [[MBIMUpdateItem alloc] init];
|
|
updateItem.changedChat = chat;
|
|
[[MBIMUpdateQueue sharedInstance] enqueueUpdateItem:updateItem];
|
|
}
|
|
}
|
|
|
|
#pragma mark -
|
|
#pragma mark Web Server initialization
|
|
|
|
- (void)startWebServer
|
|
{
|
|
self.httpServer = [[MBIMConcurrentHTTPServer alloc] init];
|
|
[self.httpServer setConnectionClass:[MBIMHTTPConnection class]];
|
|
[self.httpServer setPort:8080];
|
|
|
|
NSError *error = nil;
|
|
if (![self.httpServer start:&error]) {
|
|
NSLog(@"Error starting HTTP server: %@", [error localizedDescription]);
|
|
}
|
|
}
|
|
|
|
#pragma mark -
|
|
#pragma mark Daemon lifecycle
|
|
|
|
- (void)daemonControllerWillConnect
|
|
{
|
|
NSLog(@"Connecting to imagent...");
|
|
}
|
|
|
|
- (void)daemonControllerDidConnect
|
|
{
|
|
NSLog(@"imagent responded.");
|
|
|
|
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
|
|
if (iMessageAccount) {
|
|
NSLog(@"Successfully got accounts from imagent");
|
|
NSLog(@"iMessage account connected: %@", iMessageAccount);
|
|
} else {
|
|
NSLog(@"imagent returned no accounts (not entitled?)");
|
|
[self _terminate];
|
|
}
|
|
}
|
|
|
|
- (void)daemonControllerDidDisconnect
|
|
{
|
|
NSLog(@"Disconnected from imagent");
|
|
}
|
|
|
|
- (void)daemonConnectionLost
|
|
{
|
|
NSLog(@"Connection lost to imagent");
|
|
}
|
|
|
|
@end
|