// // MBIMBridge.m // MessagesBridge // // Created by James Magahern on 11/12/18. // Copyright © 2018 James Magahern. All rights reserved. // #import "MBIMBridge.h" #import #import #import #import static NSString *const MBIMBridgeToken = @"net.buzzert.MBIMBridge"; @implementation MBIMBridge - (instancetype)init { self = [super init]; if (self) { [self registerForNotifications]; [sDaemonController setDelegate:self]; [sDaemonListener addHandler:self]; } 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] 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!"); // Automated reply IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]]; IMHandle *handle = [[iMessageAccount arrayOfAllIMHandles] firstObject]; /* this caused a crazy loop for some reason!! */ /* NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:@"This is a test automated reply. Please ignore."]; IMMessage *reply = [IMMessage fromMeIMHandle:handle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished]; IMChat *chat = [notification object]; [chat sendMessage:reply]; */ } - (void)_chatRegistryDidLoad:(NSNotification *)notification { NSLog(@"Loaded chat registry. %lu existing chats", (unsigned long)[sChatRegistry numberOfExistingChats]); } #pragma mark - #pragma mark Daemon lifecycle - (void)daemonControllerWillConnect { NSLog(@"About to connect to daemon"); } - (void)daemonControllerDidConnect { NSLog(@"Did connect to daemon"); IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]]; if (iMessageAccount) { NSLog(@"iMessage account connected: %@", iMessageAccount); } else { NSLog(@"Was not able to connect to iMessage account"); } } - (void)daemonControllerDidDisconnect { NSLog(@"Did disconnect from daemon"); } - (void)daemonConnectionLost { NSLog(@"Connection lost to daemon"); } @end