This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.mm
302 lines (228 loc) · 8.65 KB
/
main.mm
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
// iLockMyKids Source Code
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import "SiriObjects_private.h"
#import "OS5Additions.h"
#import <locale.h>
#import <objc/runtime.h>
#include <substrate.h>
#import "main.h"
#import "shared.h"
#import "AESupport.h"
// concrete implementations
#include "systemcmds.h"
#include "AEExtension.h"
#import "AESpringBoardMsgCenter.h"
#import "AEAssistantdMsgCenter.h"
static bool s_regDone = false; // whether acronyms are registered
static ADSession* s_lastSession = nil;
// assistantd - server2client
HOOK(ADSession, _handleAceObject$, void, id aceObj)
{
//NSLog(@">> ADSession::_handleAceObject %@", aceObj);
s_lastSession = self;
NSDictionary* dict = [aceObj dictionary];
NSDictionary* resp = IPCCallResponse(@"me.k3a.AssistantExtensions", @"Server2Client", [NSDictionary dictionaryWithObject:dict forKey:@"object"]);
NSDictionary* respObj = [resp objectForKey:@"object"];
if (respObj) SessionSendToClient(respObj); // it's the same as calling ORIG
}
END
// assistantd - client2server
HOOK(ADSession, sendCommand$, void, id cmd)
{
s_lastSession = self;
NSDictionary* dict = [cmd dictionary];
NSDictionary* resp = IPCCallResponse(@"me.k3a.AssistantExtensions", @"Client2Server", [NSDictionary dictionaryWithObject:dict forKey:@"object"]);
NSDictionary* respObj = [resp objectForKey:@"object"];
if (respObj) SessionSendToServer(respObj); // it's the same as calling ORIG
}
END
// ... to inject SAK3AExtension acronym (both, but mainly for assistatd)
HOOK(BasicAceContext, init, id)
{
// TODO: maybe cache and use only one context?
//NSLog(@">> BasicAceContext:init");
s_regDone = true;
id orig = ORIG();
[self addAcronym:@"SAK3AExtension" forGroup:@"me.k3a.ace.extension"];
return orig;
}
END
// bundle search paths for SBAssistantUIPluginManager (in SpringBoard)
/*HOOK(SBAssistantUIPluginManager, _bundleSearchPaths, id)
{
NSMutableArray* arr = ORIG();
[arr addObject:@"/Library/AssistantExtensions/"];
//NSLog(@">> SBAssistantUIPluginManager:_bundleSearchPaths <%s> %@", object_getClassName(arr), arr);
return arr;
}
END*/
#pragma mark - HELPER FUNCTIONS ---------------------------------------------------------------
static NSString* s_ver = nil; // siri protocol version
id SessionSendToClient(NSDictionary* dict, id ctx)
{
static Class AceObject = objc_getClass("AceObject");
static Class BasicAceContext = objc_getClass("BasicAceContext");
if (!AceObject) NSLog(@"AE ERROR: No AceObject class");
if (!BasicAceContext) NSLog(@"AE ERROR: No BasicAceContext class");
if (!dict)
{
NSLog(@"AE ERROR: SessionSendToClient: nil dict as an argument!");
return nil;
}
// create context
if (ctx == nil) ctx = [[[BasicAceContext alloc] init] autorelease]; // ... is not needed normally, but just in case...
if (!ctx) NSLog(@"AE ERROR: No context");
if ([dict objectForKey:@"v"] && !s_ver)
s_ver = [[dict objectForKey:@"v"] copy];
else if (s_ver && ![dict objectForKey:@"v"])
{
dict = [NSMutableDictionary dictionaryWithDictionary:dict];
[(NSMutableDictionary*)dict setObject:s_ver forKey:@"v"];
}
NSLog(@"AE: ###### ===> Sending Ace Object to Client: %@", dict);
// create real AceObject
id obj = [AceObject aceObjectWithDictionary:dict context:ctx];
if (obj == nil)
{
NSLog(@"AE ERROR: SessionSendToClient: NIL ACE OBJECT RETURNED FOR DICT: %@", dict);
return nil;
}
else
{
//NSLog(@"SessionSendToClient <%s> from %@", object_getClassName(obj), dict);
}
// call the original method to handle our new object
if (s_lastSession == nil) { NSLog(@"AE: Last session is nil"); return nil; }
dispatch_async(dispatch_get_main_queue(), ^{
_ADSession$_handleAceObject$(s_lastSession, @selector(_handleAceObject:), obj);
});
return obj;
}
id SessionSendToServer(NSDictionary* dict, id ctx)
{
static Class AceObject = objc_getClass("AceObject");
static Class BasicAceContext = objc_getClass("BasicAceContext");
if (!AceObject) NSLog(@"AE ERROR: No AceObject class");
if (!BasicAceContext) NSLog(@"AE ERROR: No BasicAceContext class");
if (!dict)
{
NSLog(@"AE ERROR: SessionSendToServer: nil dict as an argument!");
return nil;
}
// create context
if (ctx == nil) ctx = [[[BasicAceContext alloc] init] autorelease]; // ... is not needed normally, but just in case...
if (!ctx) NSLog(@"AE ERROR: No context");
if (s_ver && ![dict objectForKey:@"v"])
{
dict = [NSMutableDictionary dictionaryWithDictionary:dict];
[(NSMutableDictionary*)dict setObject:s_ver forKey:@"v"];
}
NSLog(@"AE: ###### ===> Sending Ace Object to Server: %@", dict);
// create real AceObject
id obj = [AceObject aceObjectWithDictionary:dict context:ctx];
if (obj == nil)
{
NSLog(@"AE ERROR: SessionSendToServer: NIL ACE OBJECT RETURNED FOR DICT: %@", dict);
return nil;
}
else
{
//NSLog(@"SessionSendToClient <%s> from %@", object_getClassName(obj), dict);
}
dispatch_async(dispatch_get_main_queue(), ^{
_ADSession$sendCommand$(s_lastSession, @selector(sendCommand:), obj);
});
return obj;
}
#pragma mark - INITIALIZATION CODE ---------------------------------------------------------------
static void Shutdown()
{
NSLog(@"************* AssistantExtensions ShutDown *************");
AESupportShutdown();
//[[AESpringBoardMsgCenter sharedInstance] release];
//[[AEAssistantdMsgCenter sharedInstance] release];
}
static void uncaughtExceptionHandler(NSException *exception) {
NSLog(@"AE: CRASH DETECTED: %@", exception);
NSLog(@"AE: Stack Trace: %@", [exception callStackSymbols]);
// Internal error reporting
}
/*extern "C" NSArray* AFPreferencesSupportedLanguages();
static NSArray* (*original_AFPreferencesSupportedLanguages)();
static NSArray* replaced_AFPreferencesSupportedLanguages()
{
NSArray* orig = original_AFPreferencesSupportedLanguages();
NSMutableArray* repl = [NSMutableArray arrayWithArray:orig];
[repl addObject:@"ja-JP"];
return repl;
}*/
void* ServiceSpringBoard(void*)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[[AESpringBoardMsgCenter alloc] init];
CFRunLoopRun();
NSLog(@"AE: Exited from runloop");
[pool release];
return NULL;
}
void* ServiceAssistantd(void*)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[[AEAssistantdMsgCenter alloc] init];
CFRunLoopRun();
NSLog(@"AE: Exited from runloop");
[pool release];
return NULL;
}
bool s_inSB = false;
extern "C" void Initialize();
extern "C" void Initialize()
{
unsigned startStamp = GetTimestampMsec();
// Init
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
// bundle identifier
NSString* bundleIdent = getAppIdentifier();
NSLog(@"( AssistantExtensions init for %s )", [bundleIdent UTF8String]);
if ( !bundleIdent ||
(![bundleIdent isEqualToString:@"assistantd"] && ![bundleIdent isEqualToString:@"SpringBoard"])
)
{
[pool release];
return;
}
NSLog(@"************* AssistantExtensions %s init for %@ ************* ", AE_VERSION, bundleIdent);
// for custom acronyms
GET_CLASS(BasicAceContext)
LOAD_HOOK(BasicAceContext, init, init)
if ([bundleIdent isEqualToString:@"SpringBoard"])
{
s_inSB = true;
//sleep(2); // just in case (to avoid reboot crashes), probably can be removed later TODO
pthread_t thr;
pthread_create(&thr, NULL, &ServiceSpringBoard, NULL);
//GET_CLASS(SBAssistantUIPluginManager)
//LOAD_HOOK(SBAssistantUIPluginManager, _bundleSearchPaths, _bundleSearchPaths)
}
else if ([bundleIdent isEqualToString:@"assistantd"])
{
s_inSB = false;
GET_CLASS(ADSession)
LOAD_HOOK(ADSession, _handleAceObject:, _handleAceObject$)
LOAD_HOOK(ADSession, sendCommand:, sendCommand$)
pthread_t thr;
pthread_create(&thr, NULL, &ServiceAssistantd, NULL);
}
AESupportInit(s_inSB);
atexit(&Shutdown);
[pool release];
NSLog(@"AE: Init took %u ms", GetTimestampMsec() - startStamp);
//MSHookFunction(AFPreferencesSupportedLanguages, replaced_AFPreferencesSupportedLanguages, &original_AFPreferencesSupportedLanguages);
}
bool InSpringBoard()
{
return s_inSB;
}