Private
Public Access
1
0

Moved stuff around and you can see convos, messages, and send messages!

This commit is contained in:
James Magahern
2018-11-12 22:11:36 -08:00
parent 58777807bc
commit 2d0fd5b290
22 changed files with 604 additions and 1082 deletions

View File

@@ -0,0 +1,23 @@
//
// MBIMBridge.h
// MessagesBridge
//
// Created by James Magahern on 11/12/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MBIMBridge : NSObject
+ (instancetype)sharedInstance;
- (instancetype)init NS_UNAVAILABLE;
- (void)connect;
- (void)disconnect;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,271 @@
//
// MBIMBridge.m
// MessagesBridge
//
// Created by James Magahern on 11/12/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import "MBIMBridge.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.MBIMBridge";
static NSString *const kAPIEndpointConversationList = @"conversations";
static NSString *const kAPIEndpointConversationContents = @"messages";
static NSString *const kAPIEndpointSendMessage = @"sendMessage";
@interface MBIMBridge (/* INTERNAL */)
@property (nonatomic, strong) GCDWebServer *webServer;
- (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];
}
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!");
// Sending a message
/*
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
IMHandle *handle = [[iMessageAccount arrayOfAllIMHandles] firstObject];
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]);
}
- (void)_chatItemsDidChange:(NSNotification *)notification
{
NSLog(@"chat items changed: %@", notification);
}
- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID
{
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
IMHandle *handle = [[iMessageAccount arrayOfAllIMHandles] firstObject];
NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:messageBody];
IMMessage *reply = [IMMessage fromMeIMHandle:handle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished];
IMChat *chat = [sChatRegistry existingChatWithGUID:chatGUID];
if (!chat) {
NSLog(@"Chat does not exist: %@", chatGUID);
return NO;
}
[chat sendMessage:reply];
return YES;
}
#pragma mark -
#pragma mark Web Server initialization
- (void)startWebServer
{
[GCDWebServer setLogLevel:3];
__auto_type __weak weakSelf = self;
self.webServer = [[GCDWebServer alloc] init];
[self.webServer addDefaultHandlerForMethod:@"GET"
requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse * _Nullable(__kindof GCDWebServerRequest * _Nonnull request) { return [weakSelf _handleWebServerRequest:request]; }];
[self.webServer addDefaultHandlerForMethod:@"POST"
requestClass:[GCDWebServerURLEncodedFormRequest class]
processBlock:^GCDWebServerResponse * _Nullable(__kindof GCDWebServerRequest * _Nonnull request) { return [weakSelf _handleWebServerPOST:(GCDWebServerURLEncodedFormRequest *)request]; }];
[self.webServer startWithPort:8080 bonjourName:nil];
}
- (GCDWebServerResponse *)_handleWebServerPOST:(__kindof GCDWebServerURLEncodedFormRequest * _Nonnull)request
{
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:[request URL] resolvingAgainstBaseURL:NO];
NSString *endpoint = [[urlComponents path] lastPathComponent];
if ([endpoint isEqualToString:kAPIEndpointSendMessage]) {
NSDictionary *args = [request arguments];
NSString *guid = [args objectForKey:@"guid"];
NSString *messageBody = [args objectForKey:@"body"];
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
if (result) {
return [GCDWebServerDataResponse responseWithStatusCode:200];
} else {
return [GCDWebServerDataResponse responseWithStatusCode:500];
}
}
return [GCDWebServerDataResponse responseWithStatusCode:404];
}
- (GCDWebServerResponse *)_handleWebServerRequest:(__kindof GCDWebServerRequest * _Nonnull)request
{
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:[request URL] resolvingAgainstBaseURL:NO];
NSString *endpoint = [[urlComponents path] lastPathComponent];
if ([endpoint isEqualToString:kAPIEndpointConversationList]) {
NSArray<IMChat *> *chats = [sChatRegistry allExistingChats];
NSMutableArray *conversations = [NSMutableArray array];
for (IMChat *chat in chats) {
NSMutableDictionary *chatDict = [NSMutableDictionary dictionary];
chatDict[@"guid"] = [chat guid];
chatDict[@"displayName"] = [chat displayName];
chatDict[@"date"] = GCDWebServerFormatRFC822([chat lastFinishedMessageDate]);
[conversations addObject:chatDict];
}
return [GCDWebServerDataResponse responseWithJSONObject:conversations];
}
if ([endpoint isEqualToString:kAPIEndpointConversationContents]) {
NSString *guid = nil;
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
if ([[queryItem name] isEqualToString:@"guid"]) {
guid = [queryItem value];
break;
}
}
if (!guid) {
NSLog(@"No query item provided");
return [GCDWebServerDataResponse responseWithStatusCode:500];
}
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
if (!chat) {
NSLog(@"Chat with guid: %@ not found", guid);
return [GCDWebServerDataResponse responseWithStatusCode:500];
}
// Load messages
[chat loadMessagesBeforeDate:[NSDate date] limit:50 loadImmediately:YES];
NSMutableArray *messages = [NSMutableArray array];
for (IMMessageItem *imMessage in [[chat chatItems] messages]) {
NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
messageDict[@"text"] = [[imMessage body] string];
messageDict[@"date"] = GCDWebServerFormatRFC822([imMessage time]);
messageDict[@"sender"] = [imMessage sender];
[messages addObject:messageDict];
}
return [GCDWebServerDataResponse responseWithJSONObject:messages];
}
return [GCDWebServerDataResponse responseWithStatusCode:404];
}
#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

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.private.corespotlight.search.internal</key>
<true/>
<key>com.apple.private.corespotlight.internal</key>
<true/>
<key>com.apple.CommCenter.fine-grained</key>
<array>
<string>spi</string>
</array>
<key>com.apple.accounts.inactive.fullaccess</key>
<true/>
<key>com.apple.imagent</key>
<true/>
<key>com.apple.private.accounts.allaccounts</key>
<true/>
<key>com.apple.private.aps-connection-initiate</key>
<array>
<string>com.apple.ess</string>
<string>com.apple.madrid</string>
</array>
<key>com.apple.private.communicationsfilter</key>
<true/>
<key>com.apple.private.ids.idquery-cache</key>
<true/>
<key>com.apple.private.ids.remoteurlconnection</key>
<true/>
<key>com.apple.private.imcore.imdpersistence.database-access</key>
<true/>
<key>com.apple.private.tcc.allow</key>
<array>
<string>kTCCServiceAddressBook</string>
</array>
<key>keychain-access-groups</key>
<array>
<string>appleaccount</string>
<string>InternetAccounts</string>
<string>IMCore</string>
<string>ichat</string>
<string>apple</string>
</array>
<key>com.apple.logd.admin</key>
<true/>
</dict>
</plist>

24
kordophone/main.m Normal file
View File

@@ -0,0 +1,24 @@
//
// main.m
// kordophone
//
// Created by James Magahern on 11/12/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MBIMBridge.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
MBIMBridge *bridge = [MBIMBridge sharedInstance];
[bridge connect];
BOOL running = YES;
while (running) {
[[NSRunLoop currentRunLoop] run];
}
}
return 0;
}