forked from ryanb93/CyDelete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCyDelete.xm
444 lines (381 loc) · 13.5 KB
/
CyDelete.xm
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
//
// CyDelete8.xm
// CyDelete8
//
// Created by Ryan Burke on 02.01.2014.
// Copyright (c) 2014 Ryan Burke. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <substrate.h>
#import <SpringBoard/SBApplication.h>
#import <SpringBoard/SBIcon.h>
#import <SpringBoard/SBIconView.h>
#import <SpringBoard/SBApplicationIcon.h>
#import <SpringBoard/SBIconController.h>
#import <SpringBoard/SBIconModel.h>
#import <SpringBoard/SBApplicationController.h>
#import <mach/mach_host.h>
#import <dirent.h>
#import <spawn.h>
__attribute__((unused)) static NSMutableString *outputForShellCommand(NSString *cmd);
static void removeBundleFromMIList(NSString *bundle);
static NSBundle *cyDelBundle = nil;
static NSMutableDictionary *iconPackagesDict;
static NSOperationQueue *uninstallQueue;
#define SBLocalizedString(key) [[NSBundle mainBundle] localizedStringForKey:key value:@"None" table:@"SpringBoard"]
#define CDLocalizedString(key) [cyDelBundle localizedStringForKey:key value:key table:nil]
#ifndef kCFCoreFoundationVersionNumber_iOS_8_0
#define kCFCoreFoundationVersionNumber_iOS_8_0 1129.15
#endif
@interface CDUninstallOperation : NSOperation {
BOOL _executing;
BOOL _finished;
}
@end
@interface CDUninstallDpkgOperation : CDUninstallOperation<UIAlertViewDelegate> {
NSString *_package;
}
@property (nonatomic, retain) NSString *package;
- (id)initWithPackage:(NSString *)package;
@end
//Loads the application translation bundle and stores it locally.
static void initTranslation() {
cyDelBundle = [NSBundle bundleWithPath:@"/Library/Application Support/CyDelete/CyDelete.bundle"];
}
static bool getCFBool(CFStringRef key, bool defaultValue) {
//Sync the latest version of the preferences.
bool synced = CFPreferencesAppSynchronize(CFSTR("com.ryanburke.cydelete"));
//If the sync failed, lets just default to protecting Cydia for safety.
if(!synced) return defaultValue;
//Create a boolean object to hold the success value from next function.
Boolean success;
//Get the value of the key from the preferences.
bool result = CFPreferencesGetAppBooleanValue(key, CFSTR("com.ryanburke.cydelete"), &success);
//If the enabled key existed and we got the value okay.
if(success) {
//Return the value of the key.
return result;
}
//If for some reason we couldn't get the value lets just default to protecting Cydia for safety.
return defaultValue;
}
static bool getProtectCydia() {
return getCFBool(CFSTR("CDProtectCydia"), true);
}
static bool getProtectPangu() {
return getCFBool(CFSTR("CDProtectPangu"), true);
}
static bool getEnabled() {
return getCFBool(CFSTR("enabled"), true);
}
// Thanks _BigBoss_!
__attribute__((unused)) static int getFreeMemory() {
vm_size_t pageSize;
host_page_size(mach_host_self(), &pageSize);
struct vm_statistics vmStats;
mach_msg_type_number_t infoCount = sizeof(vmStats);
host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
int availMem = vmStats.free_count + vmStats.inactive_count;
return (availMem * pageSize) / 1024 / 1024;
}
__attribute__((unused)) static NSMutableString *outputForShellCommand(NSString *cmd) {
FILE *fp;
char buf[1024];
NSMutableString* finalRet;
fp = popen([cmd UTF8String], "r");
if (fp == NULL) {
return nil;
}
fgets(buf, 1024, fp);
finalRet = [NSMutableString stringWithUTF8String:buf];
if(pclose(fp) != 0) {
return nil;
}
return finalRet;
}
#define fexists(n) access(n, F_OK)
static char *owner(const char *_bundle, const char *_title, const char *_path) {
char bundle[1024], title[1024];
static char pkgname[256];
int pathlen = strlen(_path);
snprintf(bundle, 1024, "/var/lib/dpkg/info/%s.list", _bundle);
snprintf(title, 1024, "/var/lib/dpkg/info/%s.list", _title);
if(fexists(bundle) == 0) {
strcpy(pkgname, _bundle);
return pkgname;
} else if(fexists(title) == 0) {
strcpy(pkgname, _title);
return pkgname;
}
DIR *d = opendir("/var/lib/dpkg/info");
if(!d) return NULL;
struct dirent *ent;
while((ent = readdir(d)) != NULL) {
int namelen = strlen(ent->d_name);
if(strcmp(ent->d_name + namelen - 5, ".list") != 0) continue;
char curpath[1024];
snprintf(curpath, 1024, "/var/lib/dpkg/info/%s", ent->d_name);
FILE *fp = fopen(curpath, "r");
char curfn[1024];
while(fgets(curfn, 1024, fp) != NULL) {
if(strncmp(_path, curfn, pathlen) == 0) {
strncpy(pkgname, ent->d_name, namelen - 5);
pkgname[namelen - 5] = '\0';
fclose(fp);
closedir(d);
return pkgname;
}
}
fclose(fp);
}
closedir(d);
return NULL;
}
static id ownerForSBApplication(SBApplication *application) {
NSString *bundle = [application bundleIdentifier];
NSString *title = [application displayName];
NSString *plistPath = [NSString stringWithFormat:@"%@/Info.plist", [application path]];
char *pkgNameC = owner([bundle UTF8String], [title UTF8String], [plistPath UTF8String]);
id package = pkgNameC ? [NSString stringWithUTF8String:pkgNameC] : [NSNull null];
return package;
}
@implementation CDUninstallOperation
- (id)init {
if((self = [super init]) != nil) {
_executing = NO;
_finished = NO;
}
return self;
}
- (BOOL)isConcurrent { return YES; }
- (BOOL)isExecuting { return _executing; }
- (BOOL)isFinished { return _finished; }
- (void)start {
if([self isCancelled]) {
[self willChangeValueForKey:@"isFinished"];
_finished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
}
[self willChangeValueForKey:@"isExecuting"];
[NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
_executing = YES;
[self didChangeValueForKey:@"isExecuting"];
}
- (void)completeOperation {
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
_executing = NO;
_finished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
- (void)main {
}
@end
@implementation CDUninstallDpkgOperation
//String to store the package name.
@synthesize package = _package;
- (id)initWithPackage:(NSString *)package {
if((self = [super init]) != nil) {
self.package = package;
}
return self;
}
- (void)displayError {
NSString *body = [NSString stringWithFormat:CDLocalizedString(@"PACKAGE_UNINSTALL_ERROR_BODY"), _package];
UIAlertView *delView = [[UIAlertView alloc] initWithTitle:CDLocalizedString(@"PACKAGE_UNINSTALL_ERROR_TITLE") message:body delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[delView show];
}
-(void)displayRespring {
NSString *body = [NSString stringWithFormat:CDLocalizedString(@"PACKAGE_FINISH_BODY"), _package, @"respring"];
UIAlertView *respring = [[UIAlertView alloc] initWithTitle:CDLocalizedString(@"PACKAGE_FINISH_RESTART") message:body delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
respring.tag = 100;
[respring show];
}
- (void)main {
NSString *command = [NSString stringWithFormat:@"/usr/libexec/cydelete/setuid /usr/libexec/cydelete/uninstall_dpkg.sh %@", _package];
NSString *output = outputForShellCommand(command);
if(!output) [self performSelectorOnMainThread:@selector(displayError) withObject:nil waitUntilDone:NO];
[self completeOperation];
//Horrible code - need a way to detect if an application needs a respring after uninstall. ("apt-cache depends pkgname" contains mobilesubstrate?)
if([_package isEqualToString:@"libactivator"]) {
[self performSelectorOnMainThread:@selector(displayRespring) withObject:nil waitUntilDone:NO];
}
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 100) {
[(SpringBoard *)[UIApplication sharedApplication] _relaunchSpringBoardNow];
}
}
@end
static void removeBundleFromMIList(NSString *bundle) {
NSString *path = [NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", NSHomeDirectory()];
NSMutableDictionary *cache = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[[cache objectForKey:@"System"] removeObjectForKey:bundle];
[cache writeToFile:path atomically:YES];
}
%hook SBApplicationController
-(void)uninstallApplication:(SBApplication *)application {
//If the application is not a system app or web app.
if(![application isSystemApplication] || [[application path] isEqualToString:@"/Applications/Web.app"]) {
%orig;
}
else {
//Get the package details for the bundle ID.
id package = nil;
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0) {
package = [iconPackagesDict objectForKey:[application bundleIdentifier]];
}
else {
package = [iconPackagesDict objectForKey:[application displayName]];
}
// We were called with an application that doesn't have an entry in the packages list.
if(!package) package = ownerForSBApplication(application);
// We still don't have an entry (or a NSNull). We should probably bail out.
if(!package) return;
//If the package equals null then we can assume it is not installed via Cydia.
if(package == [NSNull null]) {
//Show the user an error message warning them that we didn't remove the application.
NSString *nonCydiaText = [NSString stringWithFormat:CDLocalizedString(@"PACKAGE_NOT_CYDIA_BODY"), package];
UIAlertView *nonCydiaAlert = [[UIAlertView alloc] initWithTitle:CDLocalizedString(@"PACKAGE_NOT_CYDIA_TITLE")
message:nonCydiaText
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[nonCydiaAlert show];
}
else {
//Add the package to the Uninstall operation queue.
[uninstallQueue addOperation:[[CDUninstallDpkgOperation alloc] initWithPackage:package]];
removeBundleFromMIList([application bundleIdentifier]);
}
}
}
%end
@interface SBApplicationIcon (CyDelete)
-(BOOL)cydelete_allowsUninstall;
-(void)cydelete_uninstallClicked;
@end
static void uninstallClickedForIcon(SBIcon *self) {
//Get the application for this icon.
SBApplication *app = [self application];
//Get the bundle identifer for this application.
NSString *bundle = nil;
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0) {
bundle = [app bundleIdentifier];
}
else {
bundle = [app displayName];
}
//If iconPackagesDict does not contain this current application's bundle ID.
if(![[iconPackagesDict allKeys] containsObject:bundle]) {
//Get the owner of the application.
id pkgName = ownerForSBApplication(app);
//At this point, an app store app would be pkgname null.
[iconPackagesDict setObject:pkgName forKey:bundle];
}
}
%hook SBIconController
- (void)iconCloseBoxTapped:(id)_i {
%log;
SBIconView *iconView = _i;
SBIcon *icon = [iconView icon];
SBApplication *app = [icon application];
id pkgName = ownerForSBApplication(app);
if(pkgName != [NSNull null]) {
uninstallClickedForIcon(icon);
}
%orig;
}
%end
%hook SBApplicationIcon
%new(c@:)
-(BOOL)cydelete_allowsUninstall {
//Get the bundle ID for this application.
NSString *bundle = nil;
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0) {
bundle = [[self application] bundleIdentifier];
}
else {
bundle = [[self application] displayName];
}
//If the application is an Apple application.
bool isApple = ([bundle hasPrefix:@"com.apple."] && ![bundle hasPrefix:@"com.apple.samplecode."]);
//If the application is Cydia and user has protected it.
bool isCydia = ([bundle isEqualToString:@"com.saurik.Cydia"] && getProtectCydia());
//If the application is Cydia and user has protected it.
bool isPangu = ([bundle isEqualToString:@"io.pangu.loader"] && getProtectPangu());
//If any of these match then we don't want to allow uninstall.
if(isApple || isCydia || isPangu || !getEnabled() || getFreeMemory() < 20 ) {
return NO;
}
return YES;
}
-(BOOL)allowsCloseBox {
if([self class] != %c(SBApplicationIcon) && [self class] != %c(SBActivatorIcon)) {
return %orig;
}
return [self cydelete_allowsUninstall];
}
-(BOOL)allowsUninstall {
if([self class] != %c(SBApplicationIcon) && [self class] != %c(SBActivatorIcon)) {
return %orig;
}
return [self cydelete_allowsUninstall];
}
-(void)closeBoxClicked:(id)event {
if([self class] != %c(SBApplicationIcon) && [self class] != %c(SBActivatorIcon)) {
%orig;
return;
}
uninstallClickedForIcon(self);
%orig;
}
-(void)uninstallClicked:(id)event {
if([self class] != %c(SBApplicationIcon) && [self class] != %c(SBActivatorIcon)) {
%orig;
return;
}
uninstallClickedForIcon(self);
%orig;
}
-(NSString *)uninstallAlertTitle {
return [NSString stringWithFormat:SBLocalizedString(@"UNINSTALL_ICON_TITLE"),
[[self application] displayName]];
}
-(NSString *)uninstallAlertBody {
NSString *bundle = nil;
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0) {
bundle = [[self application] bundleIdentifier];
}
else {
bundle = [[self application] displayName];
}
id package = [iconPackagesDict objectForKey:bundle];
NSString *body;
if(package == [NSNull null]) {
body = [NSString stringWithFormat:SBLocalizedString(@"DELETE_WIDGET_BODY"),
[[self application] displayName]];
}
else {
NSString *localString = CDLocalizedString(@"PACKAGE_DELETE_BODY");
body = [NSString stringWithFormat:localString, [[self application] displayName], package];
}
return body;
}
-(NSString *)uninstallAlertConfirmTitle {
return SBLocalizedString(@"UNINSTALL_ICON_CONFIRM");
}
-(NSString *)uninstallAlertCancelTitle {
return SBLocalizedString(@"UNINSTALL_ICON_CANCEL");
}
%end
%ctor {
%init;
initTranslation();
iconPackagesDict = [[NSMutableDictionary alloc] init];
uninstallQueue = [[NSOperationQueue alloc] init];
[uninstallQueue setMaxConcurrentOperationCount:1];
}