-
Notifications
You must be signed in to change notification settings - Fork 1
/
BLMautrixTask.m
556 lines (497 loc) · 28.3 KB
/
BLMautrixTask.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//
// BLMautrixTask.m
// xchighlight
//
// Created by EthanRDoesMC on 3/20/21.
//
#import "BLMautrixTask.h"
#import "IMCore.h"
#import "ChatKit.h"
#import "UIImage+Base64.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <CoreFoundation/CoreFoundation.h>
#import "CPDistributedMessagingCenter.h"
@interface UIApplication (BLSMSHelper)
-(BOOL)launchApplicationWithIdentifier:(NSString *)identifier suspended:(BOOL)launchSuspended;
@end
// you're a god among men, kirb, but we need consolidation
// all credits go to https://github.com/hbang/libcephei/blob/master/HBOutputForShellCommand.m
NSString *HBOutputForShellCommandWithReturnCode(NSString *command, int *returnCode) {
FILE *file = popen(command.UTF8String, "r");
if (!file) {
return nil;
}
char data[1024];
NSMutableString *output = [NSMutableString string];
while (fgets(data, 1024, file) != NULL) {
[output appendString:[NSString stringWithUTF8String:data]];
}
int result = pclose(file);
*returnCode = result;
return output;
}
NSString *HBOutputForShellCommand(NSString *command) {
int returnCode = 0;
NSString *output = HBOutputForShellCommandWithReturnCode(command, &returnCode);
return returnCode == 0 ? output : nil;
}
@implementation BLMautrixTask
NSInteger requestID = 0;
+ (instancetype)sharedTask {
static BLMautrixTask *_sharedTask = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedTask = [[BLMautrixTask alloc] initAndLaunch];
});
return _sharedTask;
}
-(id)initAndLaunch {
if (self.task) {
[self.task suspend];
[self.task interrupt];
}
// Register notification observers
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingMessage:) name:@"__kIMChatMessageReceivedNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(analyzeNotification:) name:@"__kIMChatItemsInserted" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleExternal:) name:@"__kIMChatMessageDidChangeNotification" object:nil];
self.sessionSentGUIDs = [NSMutableArray new];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTaskQuit:) name:@"NSTaskDidTerminateNotification" object:nil];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingMessage:) name:@"__kIMChatMessageDidChangeNotification" object:nil];
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/Beeper/%@.txt", [NSDate date]]];
//self.messageCenter = [CPDistributedMessagingCenter centerNamed:@"com.beeper.brooklyn"];
//[[UIApplication sharedApplication] launchApplicationWithIdentifier:@"com.apple.MobileSMS" suspended:YES];
NSString * fhp = [NSString stringWithFormat:@"/var/mobile/Documents/Beeper/%@.txt", [NSDate date]];
NSFileHandle* fh = [NSFileHandle fileHandleForWritingAtPath:fhp];
if ( !fh ) {
[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"/var/mobile/Documents/Beeper/%@.txt", [NSDate date]] contents:nil attributes:nil];
fh = [NSFileHandle fileHandleForWritingAtPath:[NSString stringWithFormat:@"/var/mobile/Documents/Beeper/%@.txt", [NSDate date]]];
}
freopen([fhp cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
NSLog(@"Brooklyn %@", HBOutputForShellCommand(@"dpkg-query --showformat='${Version}\n' --show com.beeper.brooklyn"));
NSLog(@"Brooklyn launching task");
self = [super init];
self.task = [NSTask new];
NSMutableArray *arguments = [[NSMutableArray alloc] init];
self.task.launchPath = @"/var/mobile/Documents/mautrix-imessage-armv7/mautrix-imessage";
if ([NSUserDefaults.standardUserDefaults stringForKey:@"configurl"]) {
[arguments addObject:[NSString stringWithFormat:@"-u%@",[NSUserDefaults.standardUserDefaults stringForKey:@"configurl"]]];
if ([NSUserDefaults.standardUserDefaults boolForKey:@"redirect"]) {
[arguments addObject:@"--output-redirect"];
}
NSLog(@"%@",arguments);
}
self.task.arguments = arguments;
self.task.currentDirectoryPath = @"/var/mobile/Documents/mautrix-imessage-armv7/"; //temporary location
NSMutableDictionary *defaultEnv = [[NSMutableDictionary alloc] initWithDictionary:[[NSProcessInfo processInfo] environment]];
[defaultEnv setObject:@"YES" forKey:@"NSUnbufferedIO"];
self.task.environment = defaultEnv;
//NSPipe *writePipe = [NSPipe pipe];
//NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
self.writePipe = [NSPipe pipe];
[self.task setStandardInput: self.writePipe];
self.task.standardOutput = [NSPipe pipe];
[[self.task.standardOutput fileHandleForReading] setReadabilityHandler:^(NSFileHandle *file) {
NSData *data = [file availableData];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"mautrix-imessage sent output:%@", string); //writes to log file as well. neat!
dispatch_async(dispatch_get_main_queue(), ^{
if (!self->_outputString) {
self->_outputString = [NSString stringWithFormat:@"%@ \n", HBOutputForShellCommand(@"dpkg-query --showformat='${Version}\n' --show com.beeper.brooklyn")]; //otherwise we're appending to nil
}
self.outputString = [[self outputString] stringByAppendingString:string];
// i'd like to replace this way of outputting the log by reading directly from the file, but i only just learned about file handles, so let's be real here: if I did it now (4/16) it would only make things slower
[[NSNotificationCenter defaultCenter] postNotificationName:@"BLMautrixLogUpdated" object:nil];
if (string) {
NSArray * commands = [string componentsSeparatedByString:@"\n"];
for (NSString * command in commands) {
if (![command isEqualToString:@""]) {
[self handleCommand:[NSJSONSerialization JSONObjectWithData:[command dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]];
}
}
}
else {
[self handleCommand:[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]];
}
});
}];
self.task.standardError = [NSPipe pipe];
[[self.task.standardError fileHandleForReading] setReadabilityHandler:^(NSFileHandle *errfile) {
NSData *errdata = [errfile availableData];
NSString *errstring = [[NSString alloc] initWithData:errdata encoding:NSUTF8StringEncoding];
NSLog(@"mautrix-imessage sent error:%@", errstring);
dispatch_async(dispatch_get_main_queue(), ^{
if (!self->_outputString) {
self->_outputString = [NSString stringWithFormat:@"%@ \n", HBOutputForShellCommand(@"dpkg-query --showformat='${Version}\n' --show com.beeper.brooklyn")];
}
// [fh seekToEndOfFile];
// [fh writeData:errdata];
self.outputString = [[self outputString] stringByAppendingString:errstring];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BLMautrixLogUpdated" object:nil];
});
}];
[self.task launch];
return self;
}
-(void)handleTaskQuit:(NSNotification *)notification {
[NSNotificationCenter.defaultCenter postNotificationName:@"BLLaunchOnboarding" object:nil];
// [self.task.standardOutput fileHandleForReading].readabilityHandler = nil;
// [self.task.standardError fileHandleForReading].readabilityHandler = nil;
}
#pragma mark - Command Switch
-(void)handleCommand:(NSDictionary *)command {
NSLog(@"Got command %@", command[@"command"]);
if ([command[@"command"] isEqual:@"get_chat"]) {
[self respondWithChatInfoForCommand:command];
} else if ([command[@"command"] isEqual:@"get_contact"]) {
[self getContactInfoForCommand:command];
} else if ([command[@"command"] isEqual:@"send_message"]) {
[self sendMessageCommand:command];
} else if ([command[@"command"] isEqual:@"send_media"]) {
[self sendAttachmentCommand:command];
} else if ([command[@"command"] isEqual:@"set_typing"]) {
[self setTypingCommand:command];
} else if ([command[@"command"] isEqual:@"get_chats"]) {
[self getChatListWithCommand:command];
} else if ([command[@"command"] isEqual:@"get_recent_messages"]) {
[self getMessagesForCommand:command];
} else if ([command[@"command"] isEqual:@"send_read_receipt"]) {
[self sendReadReceiptWithCommand:command];
} else if ([command[@"command"] isEqual:@"config_url"]) {
[self configURLCommand:command];
} else if ([command[@"command"] isEqual:@"log"]) {
NSLog(@"Log acknowledged");
} else if ([command[@"command"] isEqual:@"response"]) {
NSLog(@"Response acknowledged");
} else if (command[@"command"]) {
[self sendErrorCode:@"unknown_command" withDescription:[NSString stringWithFormat:@"Unknown command %@", command[@"command"]] forCommand:command];
}
}
#pragma mark - Config URL
// lmao
-(void)configURLCommand:(NSDictionary *)command {
[NSUserDefaults.standardUserDefaults setValue:command[@"data"][@"url"] forKey:@"configurl"];
[NSUserDefaults.standardUserDefaults setBool:NO forKey:@"redirect"];
}
#pragma mark - Notification Handlers
-(void)incomingMessage:(NSNotification *)notification {
[self forwardMessage:notification.userInfo[@"__kIMChatValueKey"] fromChat:notification.object];
NSLog(@"Forwarding incoming message to bridge");
}
-(void)analyzeNotification:(NSNotification *)notification {
NSLog(@"%@ / %@ / %@", notification.name, notification.object, notification.userInfo);
}
-(void)handleExternal:(NSNotification *)notification {
[self analyzeNotification:notification];
IMMessage * thisMessage = notification.userInfo[@"__kIMChatValueKey"];
NSLog(@"%@", thisMessage);
BOOL isDupe = false;
if (thisMessage.isFromMe) {
if (self.mostRecentMessage) {
if (thisMessage == self.mostRecentMessage) {
isDupe = true;
NSLog(@"%hhd", isDupe);
}
}
for (NSString * sentGUID in self.sessionSentGUIDs) {
if ([thisMessage.guid isEqualToString:sentGUID]) {
isDupe = true;
NSLog(@"%hhd", isDupe);
}
}
}
if (![thisMessage isFromMe]) {
isDupe = true;
NSLog(@"typing message: %hhd", thisMessage.isTypingMessage);
NSLog(@"retract typing indc: %hhd", thisMessage.isFinished);
if (thisMessage.isTypingMessage) {
[self forwardTypingIndicator:thisMessage fromChat:notification.object];
}
}
if (thisMessage.isRead) {
[self forwardReadReceipt:thisMessage fromChat:notification.object];
}
NSLog(@"%hhd", isDupe);
if (isDupe == false) {
[self forwardMessage:notification.userInfo[@"__kIMChatValueKey"] fromChat:notification.object];
}
}
#pragma mark - Dictionary Handlers
-(void)sendDictionary:(NSDictionary *)dictionary {
requestID += 1;
[self sendDictionary:dictionary withID:[NSNumber numberWithInteger:requestID]];
}
-(void)sendDictionary:(NSDictionary *)dictionary withID:(NSNumber *)msgID {
[dictionary setValue:msgID forKey: @"id"];
// if (msgID > [NSNumber numberWithInteger:requestID]) {
// requestID = (NSInteger)msgID;
// }
NSData *dataForSending = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
//NSString *jsonString = [[NSString alloc] initWithData:dataForSending encoding:NSUTF8StringEncoding];
//NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:dataForSending options:0 error:nil]);
//NSLog(@"%@", jsonString);
NSLog(@"Sending command (id: %@, contents: %@)", msgID, dictionary);
[[self.task.standardInput fileHandleForWriting] writeData:dataForSending];
}
#pragma mark - Error Handler
-(void)sendErrorCode:(NSString *)code withDescription:(NSString *)description forCommand:(NSDictionary *)command {
NSLog(@"Error: %@ (%@)",code,description);
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"error" forKey:@"command"];
NSMutableDictionary * data = [NSMutableDictionary new];
[data setValue:code forKey:@"code"];
[data setValue:description forKey:@"message"];
[request setValue:[NSDictionary dictionaryWithDictionary:data] forKey:@"data"];
[self sendDictionary:request withID:command[@"id"]];
}
#pragma mark - Ping
-(void)sendPing {
NSMutableDictionary *pingDictionary = [NSMutableDictionary new];
[pingDictionary setValue:@"ping" forKey:@"command"];
NSLog(@"Sending ping");
[self sendDictionary:pingDictionary];
}
#pragma mark - Message Forward
-(void)forwardMessage:(IMMessage *)message fromChat:(IMChat *)chat {
NSMutableDictionary * datadict = [NSMutableDictionary new];
[datadict setValue:[message guid] forKey:@"guid"];
[datadict setValue:[message text].string forKey:@"text"];
[datadict setValue:[chat guid] forKey:@"chat_guid"];
[datadict setValue:[message messageSubject].string forKey:@"subject"];
[datadict setValue:[NSNumber numberWithBool:[message isFromMe]] forKey:@"is_from_me"];
[datadict setValue:[NSString stringWithFormat:@"%@;-;%@", [[message sender] accountTypeName], [[message sender] ID]] forKey:@"sender_guid"];
if (message.fileTransferGUIDs) {
NSMutableDictionary * attachmentDict = [NSMutableDictionary new];
IMFileTransfer * ft = [[IMFileTransferCenter sharedInstance] transferForGUID:message.fileTransferGUIDs[0]];
[attachmentDict setValue:ft.filename forKey:@"file_name"];
[attachmentDict setValue:ft.mimeType forKey:@"mime_type"];
[attachmentDict setValue:ft.localPath forKey:@"path_on_disk"];
[datadict setObject:[NSDictionary dictionaryWithDictionary:attachmentDict] forKey:@"attachment"];
}
[datadict setValue:[NSNumber numberWithDouble:[[message time] timeIntervalSince1970]] forKey:@"timestamp"];
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"message" forKey:@"command"];
[request setObject:[NSDictionary dictionaryWithDictionary:datadict] forKey:@"data"];
NSLog(@"Forwarding a message:%@ from chat:%@", message, chat);
[self sendDictionary:request];
}
-(void)forwardTypingIndicator:(IMMessage *)typingIndicator fromChat:(IMChat *)chat {
NSMutableDictionary * dataDictionary = [NSMutableDictionary new];
[dataDictionary setValue:chat.guid forKey:@"chat_guid"];
[dataDictionary setValue:[NSNumber numberWithBool:!(BOOL)typingIndicator.isFinished] forKey:@"typing"];
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"typing" forKey:@"command"];
[request setObject:[NSDictionary dictionaryWithDictionary:dataDictionary] forKey:@"data"];
NSLog(@"Forwarding typing indicator:%@ from chat:%@", typingIndicator, chat);
[self sendDictionary:request];
}
-(void)forwardReadReceipt:(IMMessage *)readReceipt fromChat:(IMChat *)chat {
NSMutableDictionary * dataDictionary = [NSMutableDictionary new];
[dataDictionary setValue:chat.guid forKey:@"chat_guid"];
[dataDictionary setValue:chat.guid forKey:@"sender_guid"];
// read receipts are "applied" to messages. i think.
[dataDictionary setValue:[NSNumber numberWithBool:!(BOOL)readReceipt.isFromMe] forKey:@"is_from_me"];
//[dataDictionary setValue:[NSString stringWithFormat:@"%@;-;%@", [[readReceipt sender] accountTypeName], [[readReceipt sender] ID]] forKey:@"sender_guid"];
[dataDictionary setValue:readReceipt.guid forKey:@"read_up_to"];
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"read_receipt" forKey:@"command"];
[request setObject:[NSDictionary dictionaryWithDictionary:dataDictionary] forKey:@"data"];
NSLog(@"Forwarding read receipt:%@ from chat:%@", readReceipt, chat);
[self sendDictionary:request];
}
#pragma mark - Get Message GUIDs
-(void)getMessagesForCommand:(NSDictionary *)command {
IMChat * chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:command[@"data"][@"chat_guid"]];
[chat loadMessagesBeforeDate:[NSDate date] limit:100 loadImmediately:YES];
NSMutableArray * messageArray = [NSMutableArray new];
for (IMChatItem * chatItem in [chat chatItems]) {
//if ([[chat lastMessage] time] > command[@"data"][@"min_timestamp"]) {
if ([chatItem respondsToSelector:@selector(message)]) {
NSMutableDictionary * messageDict = [NSMutableDictionary new];
IMMessage * message = [(IMMessageChatItem *)chatItem message];
[messageDict setValue:[message guid] forKey:@"guid"];
[messageDict setValue:[message text].string forKey:@"text"];
[messageDict setValue:[chat guid] forKey:@"chat_guid"];
[messageDict setValue:[message messageSubject].string forKey:@"subject"];
[messageDict setValue:[NSNumber numberWithBool:[message isFromMe]] forKey:@"is_from_me"];
[messageDict setValue:[NSString stringWithFormat:@"%@;-;%@", [[message sender] accountTypeName], [[message sender] ID]] forKey:@"sender_guid"];
[messageDict setValue:[NSNumber numberWithDouble:[[message time] timeIntervalSince1970]] forKey:@"timestamp"];
// Attachments
if (message.fileTransferGUIDs) {
NSMutableDictionary * attachmentDict = [NSMutableDictionary new];
IMFileTransfer * ft = [[IMFileTransferCenter sharedInstance] transferForGUID:message.fileTransferGUIDs[0]];
[attachmentDict setValue:ft.filename forKey:@"file_name"];
[attachmentDict setValue:ft.mimeType forKey:@"mime_type"];
[attachmentDict setValue:ft.localPath forKey:@"path_on_disk"];
[messageDict setObject:[NSDictionary dictionaryWithDictionary:attachmentDict] forKey:@"attachment"];
}
[messageArray addObject:[NSDictionary dictionaryWithDictionary:messageDict]];
}
//}
}
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"response" forKey:@"command"];
[request setObject:[NSArray arrayWithArray:messageArray] forKey:@"data"];
NSLog(@"Got messages:%@", messageArray);
[self sendDictionary:request withID:command[@"id"]];
}
#pragma mark - Send Message
-(void)sendMessageCommand:(NSDictionary *)command {
NSMutableDictionary * datadict = [NSMutableDictionary new];
IMChat * thisChat = [[IMChatRegistry sharedInstance] existingChatWithGUID:command[@"data"][@"chat_guid"]];
CKConversation * conversation = [[CKConversation alloc] initWithChat:thisChat];
CKComposition * composition = [[CKComposition alloc] initWithText:[[NSAttributedString alloc] initWithString:command[@"data"][@"text"]] subject:nil];
IMMessage * message = [conversation messageWithComposition:composition];
self.mostRecentMessage = message;
[thisChat sendMessage:message];
NSMutableDictionary * request = [NSMutableDictionary new];
[datadict setValue:[message guid] forKey:@"guid"];
[self.sessionSentGUIDs addObject:[message guid]];
[datadict setValue:[NSNumber numberWithDouble:[[message time] timeIntervalSince1970]] forKey:@"timestamp"];
[request setValue:@"response" forKey:@"command"];
[request setObject:[NSDictionary dictionaryWithDictionary:datadict] forKey:@"data"];
NSLog(@"Sent message:%@, telling bridge...",message);
[self sendDictionary:request withID:command[@"id"]];
}
-(void)sendReadReceiptWithCommand:(NSDictionary *)command {
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"response" forKey:@"command"];
IMChat * thisChat = [[IMChatRegistry sharedInstance] existingChatWithGUID:command[@"data"][@"chat_guid"]];
CKConversation * conversation = [[CKConversation alloc] initWithChat:thisChat];
[conversation markAllMessagesAsRead];
[thisChat markAllMessagesAsRead];
[self sendDictionary:request withID:command[@"id"]];
NSLog(@"Read receipt for:%@",thisChat);
}
-(void)sendAttachmentCommand:(NSDictionary *)command {
if ([[UIApplication sharedApplication] launchApplicationWithIdentifier:@"com.apple.MobileSMS" suspended:YES]) {
NSLog(@"Launched successfully");
NSDictionary * message = @{ @"path_on_disk" : [command[@"data"][@"path_on_disk"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]], @"file_name" : command[@"data"][@"file_name"], @"chat_guid" : command[@"data"][@"chat_guid"] };
NSLog(@"Outgoing user info: %@", message);
NSDictionary *reply;
while (![self checkIfRunning:@"MobileSMS"]) {
NSLog(@"sms isn't running!");
[self checkIfRunning:@"MobileSMS"];
}
NSLog(@"sms is running!");
CPDistributedMessagingCenter * messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.beeper.brooklyn"];
NSLog(@"Ping!");
//[messagingCenter sendMessageName:@"sendAttachment" userInfo:nil];
while (![messagingCenter sendMessageAndReceiveReplyName:@"ping" userInfo:nil]) {
NSLog(@"Pain");
[messagingCenter sendMessageAndReceiveReplyName:@"ping" userInfo:nil];
NSLog(@"Ping!");
}
NSLog(@"POG");
//rocketbootstrap_distributedmessagingcenter_apply(messagingCenter);
//[messagingCenter sendMessageName:@"sendAttachment" userInfo:[NSDictionary dictionaryWithDictionary:message]];
if ((reply = [messagingCenter sendMessageAndReceiveReplyName:@"sendAttachment" userInfo:message])) {
NSLog(@"Reply: %@", reply);
[self.sessionSentGUIDs addObject:reply[@"sentGUID"]];
NSLog(@"Sent message; telling bridge...");
NSLog(@"Reply: %@", reply);
NSLog(@"Command's ID: %@", command[@"id"]);
[self sendDictionary:[NSMutableDictionary dictionaryWithDictionary:@{ @"command": @"response", @"data": @{ @"guid": reply[@"sentGUID"], @"timestamp": reply[@"request"][@"data"][@"timestamp"] } }] withID:command[@"id"]];
}
}
}
// source: https://github.com/iandwelker/libsmserver/blob/master/Tweak.xm
- (BOOL)checkIfRunning:(NSString *)bundle_id { /// Would return a _Bool but you can only send `id`s through MRYIPC funcs
/// Just checks if a certain app with the bundle id `bundle_id` is running at all, background or foreground.
NSTask* task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:@[@"-c", [NSString stringWithFormat:@"ps aux | grep %@ | grep -v grep", bundle_id]]];
NSPipe* pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle* file = [pipe fileHandleForReading];
[task launch];
NSData* data = [file readDataToEndOfFile];
NSString* out = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSNumber * temp = out.length > 0 ? @1 : @0;
return [temp boolValue];
}
-(void)respondWithChatInfoForCommand:(NSDictionary *)command {
IMChat * thisChat = [[IMChatRegistry sharedInstance] existingChatWithGUID:command[@"data"][@"chat_guid"]];
NSMutableArray * memberArray = [NSMutableArray new];
for (IMHandle *h in [thisChat participants]) {
[memberArray addObject:[h ID]];
}
NSMutableDictionary * datadict = [NSMutableDictionary new];
CKConversation * conversation = [[CKConversation alloc] initWithChat:thisChat];
if (conversation.hasDisplayName) {
[datadict setValue:[conversation displayName] forKey:@"title"];
} else {
[datadict setValue:[conversation name] forKey:@"title"];
}
[datadict setObject:[NSArray arrayWithArray:memberArray] forKey:@"members"];
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"response" forKey:@"command"];
[request setObject:[NSDictionary dictionaryWithDictionary:datadict] forKey:@"data"];
//NSLog(@"member array: %@, dictionary: %@", memberArray, request);
NSLog(@"Got chat info:%@, sending to bridge...", memberArray);
[self sendDictionary:request withID:command[@"id"]];
}
-(void)getContactInfoForCommand:(NSDictionary *)command {
NSMutableDictionary * datadict = [NSMutableDictionary new];
IMHandle * handle = [[[IMAccountController sharedInstance] bestAccountForService:[IMService iMessageService]] imHandleWithID:command[@"data"][@"user_guid"]];
if ([handle person]) {
[datadict setValue:[[handle person] firstName] forKey:@"first_name"];
[datadict setValue:[[handle person] lastName] forKey:@"last_name"];
[datadict setValue:[[handle person] nickname] forKey:@"nickname"];
//[datadict setValue:[[[[CKEntity alloc] initWithIMHandle:handle] transcriptContactImage] encodeToBase64String] forKey:@"avatar"];
//[datadict setValue:[[UIImage imageWithData:[[handle person] imageData]]encodeToBase64String] forKey:@"avatar"];
//NSLog(@"%@", [[[[CKEntity alloc] initWithIMHandle:handle] transcriptContactImage] encodeToBase64String]);
[datadict setValue:[[CKAddressBook contactImageOfDiameter:512 forRecordID:[handle person].recordID monogramStyle:1 tintMonogramText:NO] encodeToBase64String] forKey:@"avatar"];
[datadict setValue:[[handle person] phoneNumbers] forKey:@"phones"];
[datadict setValue:[[handle person] emails] forKey:@"emails"];
} else {
[datadict setValue:[handle firstName] forKey:@"first_name"];
[datadict setValue:[handle lastName] forKey:@"last_name"];
[datadict setValue:[handle nickname] forKey:@"nickname"];
if (![handle emails]) {
NSMutableArray * array = [NSMutableArray new];
[array addObject:command[@"data"][@"user_guid"]];
[datadict setValue:[NSArray arrayWithArray:array] forKey:@"phones"];
} else {
[datadict setValue:[handle emails] forKey:@"emails"];
}
}
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"response" forKey:@"command"];
[request setObject:[NSDictionary dictionaryWithDictionary:datadict] forKey:@"data"];
NSLog(@"Got contact info:%@ sending to bridge...", datadict);
[self sendDictionary:request withID:command[@"id"]];
}
-(void)getChatListWithCommand:(NSDictionary *)command {
NSArray * chatArray = [[IMChatRegistry sharedInstance] _allCreatedChats];
NSMutableArray * guidArray = [NSMutableArray new];
for (IMChat * chat in chatArray) {
if ([[[chat lastMessage] time] timeIntervalSince1970] >= [command[@"data"][@"min_timestamp"] doubleValue]) {
NSLog(@"%d", [[[chat lastMessage] time] timeIntervalSince1970] >= [command[@"data"][@"min_timestamp"] doubleValue]);
[guidArray addObject:chat.guid];
}
}
//NSMutableDictionary * datadict = [NSMutableDictionary new];
//[datadict setObject:[NSArray arrayWithArray:guidArray] forKey:@"chat_guids"];
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"response" forKey:@"command"];
[request setObject:[NSArray arrayWithArray:guidArray] forKey:@"data"];
NSLog(@"Got chats list:%@, returning...", chatArray);
[self sendDictionary:request withID:command[@"id"]];
}
#pragma mark - Typing
-(void)setTypingCommand:(NSDictionary *)command {
NSMutableDictionary * request = [NSMutableDictionary new];
[request setValue:@"response" forKey:@"command"];
IMChat * thisChat = [[IMChatRegistry sharedInstance] existingChatWithGUID:command[@"data"][@"chat_guid"]];
// CKConversation * conversation = [[CKConversation alloc] initWithChat:thisChat];
// [conversation setLocalUserIsTyping:[command[@"data"][@"typing"] boolValue]];
[thisChat setLocalUserIsTyping:[command[@"data"][@"typing"] boolValue]];
[self sendDictionary:request withID:command[@"id"]];
NSLog(@"Typing indicator for:%@",thisChat);
}
-(void)clearLogs {
HBOutputForShellCommand(@"rm /var/mobile/Documents/Beeper/*");
}
@end