Private
Public Access
1
0
Files
Kordophone/kordophone/Bridge/MBIMBridge.m
2018-11-13 12:29:15 -08:00

175 lines
5.6 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 <GCDWebServers/GCDWebServers.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) GCDWebServer *webServer;
@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];
}
return self;
}
#pragma mark -
#pragma mark Connection
- (void)connect
{
if (![sDaemonController hasListenerForID: MBIMBridgeToken]) {
if (![sDaemonController addListenerID:MBIMBridgeToken capabilities:(kFZListenerCapFileTransfers | kFZListenerCapManageStatus | kFZListenerCapChats | kFZListenerCapMessageHistory | kFZListenerCapIDQueries | kFZListenerCapSendMessages)]) {
NSLog(@"Failed to connect to imagent");
}
}
}
- (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
{
NSLog(@"Received message from chat with GUID: %@", [[notification object] guid]);
// TODO: Notify observers or something
}
- (void)_chatRegistryDidLoad:(NSNotification *)notification
{
NSLog(@"Loaded chat registry. %lu existing chats", (unsigned long)[sChatRegistry numberOfExistingChats]);
}
- (void)_chatItemsDidChange:(NSNotification *)notification
{
NSLog(@"chat items changed: %@", notification);
}
#pragma mark -
#pragma mark Web Server initialization
- (void)_handleAsyncServerRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completion
{
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]);
}
}
- (void)startWebServer
{
[GCDWebServer setLogLevel:3];
__auto_type __weak weakSelf = self;
self.webServer = [[GCDWebServer alloc] init];
[self.webServer addDefaultHandlerForMethod:@"GET"
requestClass:[GCDWebServerRequest class]
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
[self.webServer addDefaultHandlerForMethod:@"POST"
requestClass:[GCDWebServerURLEncodedFormRequest class]
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
[self.webServer startWithPort:8080 bonjourName:nil];
}
#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?)");
}
}
- (void)daemonControllerDidDisconnect
{
NSLog(@"Disconnected from imagent");
}
- (void)daemonConnectionLost
{
NSLog(@"Connection lost to imagent");
}
@end