summaryrefslogtreecommitdiff
path: root/CyteKit/CyteObject.mm
blob: f55ddb95beba41248b65247a53004c20e62913b6 (plain)
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
/* Cydia - iPhone UIKit Front-End for Debian APT
 * Copyright (C) 2008-2015  Jay Freeman (saurik)
*/

/* GNU General Public License, Version 3 {{{ */
/*
 * Cydia is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * Cydia is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cydia.  If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */

#include "CyteKit/UCPlatform.h"

#include <sys/mount.h>
#include <sys/sysctl.h>

#include <IOKit/IOKitLib.h>
#include <objc/runtime.h>

#include "CyteKit/CyteObject.h"
#include "CyteKit/WebViewController.h"
#include "CyteKit/countByEnumeratingWithState.h"
#include "CyteKit/extern.h"

#include "iPhonePrivate.h"
#include <Menes/ObjectHandle.h>

@implementation NSDictionary (Cydia)
- (id) invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)arguments {
    if (false);
    else if ([name isEqualToString:@"get"])
        return [self objectForKey:[arguments objectAtIndex:0]];
    else if ([name isEqualToString:@"keys"])
        return [self allKeys];
    return nil;
} @end

static NSString *CYHex(NSData *data, bool reverse = false) {
    if (data == nil)
        return nil;

    size_t length([data length]);
    uint8_t bytes[length];
    [data getBytes:bytes];

    char string[length * 2 + 1];
    for (size_t i(0); i != length; ++i)
        sprintf(string + i * 2, "%.2x", bytes[reverse ? length - i - 1 : i]);

    return [NSString stringWithUTF8String:string];
}

static NSObject *CYIOGetValue(const char *path, NSString *property) {
    io_registry_entry_t entry(IORegistryEntryFromPath(kIOMasterPortDefault, path));
    if (entry == MACH_PORT_NULL)
        return nil;

    CFTypeRef value(IORegistryEntryCreateCFProperty(entry, (CFStringRef) property, kCFAllocatorDefault, 0));
    IOObjectRelease(entry);

    if (value == NULL)
        return nil;
    return [(id) value autorelease];
}

@implementation CyteObject {
    _H<CyteWebViewController> indirect_;
}

+ (BOOL) isKeyExcludedFromWebScript:(const char *)name {
    return false;
}

- (id) initWithDelegate:(CyteWebViewController *)indirect {
    if ((self = [super init]) != nil) {
        indirect_ = indirect;
    } return self;
}


- (NSArray *) attributeKeys {
    return [NSArray arrayWithObjects:
        @"bbsnum",
        @"bittage",
        @"build",
        @"coreFoundationVersionNumber",
        @"ecid",
        @"firmware",
        @"hostname",
        @"idiom",
        @"model",
        @"serial",
    nil];
}

- (unsigned) bittage {
#if 0
#elif defined(__arm64__)
    return 64;
#elif defined(__arm__)
    return 32;
#else
    return 0;
#endif
}

- (NSString *) bbsnum {
    return (id) CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false) ?: [NSNull null];
}

- (NSString *) build {
    return [NSString stringWithUTF8String:System_];
}

- (NSString *) coreFoundationVersionNumber {
    return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber];
}

- (NSString *) ecid {
    return (id) [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString] ?: [NSNull null];
}

- (NSString *) firmware {
    return [[UIDevice currentDevice] systemVersion];
}

- (NSString *) hostname {
    return [[UIDevice currentDevice] name];
}

- (NSString *) idiom {
    return IsWildcat_ ? @"ipad" : @"iphone";
}

- (NSString *) model {
    return [NSString stringWithUTF8String:Machine_];
}

- (NSString *) serial {
    return (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber");
}


+ (NSString *) webScriptNameForSelector:(SEL)selector {
    if (false);
    else if (selector == @selector(addInternalRedirect::))
        return @"addInternalRedirect";
    else if (selector == @selector(close))
        return @"close";
    else if (selector == @selector(stringWithFormat:arguments:))
        return @"format";
    else if (selector == @selector(getIORegistryEntry::))
        return @"getIORegistryEntry";
    else if (selector == @selector(getKernelNumber:))
        return @"getKernelNumber";
    else if (selector == @selector(getKernelString:))
        return @"getKernelString";
    else if (selector == @selector(getLocaleIdentifier))
        return @"getLocaleIdentifier";
    else if (selector == @selector(getPreferredLanguages))
        return @"getPreferredLanguages";
    else if (selector == @selector(isReachable:))
        return @"isReachable";
    else if (selector == @selector(localizedStringForKey:value:table:))
        return @"localize";
    else if (selector == @selector(popViewController:))
        return @"popViewController";
    else if (selector == @selector(registerFrame:))
        return @"registerFrame";
    else if (selector == @selector(removeButton))
        return @"removeButton";
    else if (selector == @selector(scrollToBottom:))
        return @"scrollToBottom";
    else if (selector == @selector(setAllowsNavigationAction:))
        return @"setAllowsNavigationAction";
    else if (selector == @selector(setBadgeValue:))
        return @"setBadgeValue";
    else if (selector == @selector(setButtonImage:withStyle:toFunction:))
        return @"setButtonImage";
    else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
        return @"setButtonTitle";
    else if (selector == @selector(setHidesBackButton:))
        return @"setHidesBackButton";
    else if (selector == @selector(setHidesNavigationBar:))
        return @"setHidesNavigationBar";
    else if (selector == @selector(setNavigationBarStyle:))
        return @"setNavigationBarStyle";
    else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:))
        return @"setNavigationBarTintColor";
    else if (selector == @selector(setPasteboardString:))
        return @"setPasteboardString";
    else if (selector == @selector(setPasteboardURL:))
        return @"setPasteboardURL";
    else if (selector == @selector(setScrollAlwaysBounceVertical:))
        return @"setScrollAlwaysBounceVertical";
    else if (selector == @selector(setScrollIndicatorStyle:))
        return @"setScrollIndicatorStyle";
    else if (selector == @selector(setViewportWidth:))
        return @"setViewportWidth";
    else if (selector == @selector(statfs:))
        return @"statfs";
    else if (selector == @selector(supports:))
        return @"supports";
    else if (selector == @selector(unload))
        return @"unload";
    else
        return nil;
}

+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
    return [self webScriptNameForSelector:selector] == nil;
}

- (void) addInternalRedirect:(NSString *)from :(NSString *)to {
    [CyteWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO];
}

- (void) close {
    [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO];
}

- (NSString *) getKernelString:(NSString *)name {
    const char *string([name UTF8String]);

    size_t size;
    if (sysctlbyname(string, NULL, &size, NULL, 0) == -1)
        return (id) [NSNull null];

    char value[size + 1];
    if (sysctlbyname(string, value, &size, NULL, 0) == -1)
        return (id) [NSNull null];

    // XXX: just in case you request something ludicrous
    value[size] = '\0';

    return [NSString stringWithCString:value];
}

- (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry {
    NSObject *value(CYIOGetValue([path UTF8String], entry));

    if (value != nil)
        if ([value isKindOfClass:[NSData class]])
            value = CYHex((NSData *) value);

    return value;
}

- (NSString *) getLocaleIdentifier {
    _H<const __CFLocale> locale(CFLocaleCopyCurrent(), true);
    return locale == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(locale);
}

- (NSArray *) getPreferredLanguages {
    return [NSLocale preferredLanguages];
}

- (NSNumber *) isReachable:(NSString *)name {
    return [NSNumber numberWithBool:CyteIsReachable([name UTF8String])];
}

- (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table {
    if (reinterpret_cast<id>(value) == [WebUndefined undefined])
        value = nil;
    if (reinterpret_cast<id>(table) == [WebUndefined undefined])
        table = nil;
    return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table];
}

- (void) popViewController:(NSNumber *)value {
    if (value == (id) [WebUndefined undefined])
        value = [NSNumber numberWithBool:YES];
    [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO];
}

- (void) registerFrame:(DOMHTMLIFrameElement *)iframe {
    WebFrame *frame([iframe contentFrame]);
    [indirect_ registerFrame:frame];
}

- (void) removeButton {
    [indirect_ removeButton];
}

- (void) scrollToBottom:(NSNumber *)animated {
    [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO];
}

- (void) setAllowsNavigationAction:(NSString *)value {
    [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO];
}

- (void) setBadgeValue:(id)value {
    [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO];
}

- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
    [indirect_ setButtonImage:button withStyle:style toFunction:function];
}

- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
    [indirect_ setButtonTitle:button withStyle:style toFunction:function];
}

- (void) setHidesBackButton:(NSString *)value {
    [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO];
}

- (void) setHidesNavigationBar:(NSString *)value {
    [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO];
}

- (void) setNavigationBarStyle:(NSString *)value {
    [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO];
}

- (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha {
    float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]);
    UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]);
    [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO];
}

- (void) setPasteboardString:(NSString *)value {
    [[objc_getClass("UIPasteboard") generalPasteboard] setString:value];
}

- (void) setPasteboardURL:(NSString *)value {
    [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]];
}

- (void) setScrollAlwaysBounceVertical:(NSNumber *)value {
    [indirect_ performSelectorOnMainThread:@selector(setScrollAlwaysBounceVerticalNumber:) withObject:value waitUntilDone:NO];
}

- (void) setScrollIndicatorStyle:(NSString *)style {
    [indirect_ performSelectorOnMainThread:@selector(setScrollIndicatorStyleWithName:) withObject:style waitUntilDone:NO];
}

- (void) setViewportWidth:(float)width {
    [indirect_ setViewportWidthOnMainThread:width];
}

- (NSArray *) statfs:(NSString *)path {
    struct statfs stat;

    if (path == nil || statfs([path UTF8String], &stat) == -1)
        return nil;

    return [NSArray arrayWithObjects:
        [NSNumber numberWithUnsignedLong:stat.f_bsize],
        [NSNumber numberWithUnsignedLong:stat.f_blocks],
        [NSNumber numberWithUnsignedLong:stat.f_bfree],
    nil];
}

- (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments {
    //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]);
    unsigned count([arguments count]);
    id values[count];
    for (unsigned i(0); i != count; ++i)
        values[i] = [arguments objectAtIndex:i];
    return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease];
}

- (BOOL) supports:(NSString *)feature {
    return [feature isEqualToString:@"window.open"];
}

- (void) unload {
    [[indirect_ rootViewController] performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO];
}

@end