-
Notifications
You must be signed in to change notification settings - Fork 4
/
Tweak.xm
1526 lines (1378 loc) · 58.6 KB
/
Tweak.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "NSPTestPush.h"
#import "global.h"
#import "helpers.h"
#import "iOSVersion.m"
#import <SystemConfiguration/CaptiveNetwork.h>
#import <notify.h>
// #import <MetalKit/MTKTextureLoader.h>
#define isBundle(z) [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:z]
@interface UIImage (UIApplicationIconPrivate)
/*
@param format
0 - 29x29
1 - 40x40
2 - 62x62
3 - 42x42
4 - 37x48
5 - 37x48
6 - 82x82
7 - 62x62
8 - 20x20
9 - 37x48
10 - 37x48
11 - 122x122
12 - 58x58
*/
+ (UIImage *)_applicationIconImageForBundleIdentifier:
(NSString *)bundleIdentifier
format:(int)format;
@end
static BOOL pusherEnabled = NO;
static int pusherWhenToPush = PUSHER_WHEN_TO_PUSH_EITHER;
static NSArray *pusherSNS = nil;
static BOOL pusherSNSIsAnd = YES;
static BOOL pusherSNSRequireANWithOR = YES;
static int pusherWhatNetwork = PUSHER_WHAT_NETWORK_ANY;
static BOOL globalAppListIsBlacklist = YES;
static NSArray *globalAppList = nil;
static NSMutableDictionary *pusherEnabledServices = nil;
static NSMutableDictionary *pusherServicePrefs = nil;
static NSMutableArray *pusherEnabledLogs = nil;
static BBServer *bbServerInstance = nil;
static NSMutableArray *recentNotificationTitles = [NSMutableArray new];
static NSMutableDictionary *pusherRetriesLeft = [NSMutableDictionary new];
static NSString *retriesLeftKeyForBulletinAndService(BBBulletin *bulletin,
NSString *service) {
return XStr(@"%@_%@_%@", bulletin.bulletinID ?: @"empty_bulletin_id",
bulletin.sectionID, service);
}
static NSString *stringForObject(id object, NSString *prefix,
BOOL dontTruncate) {
NSString *str = @"";
if (!object) {
str = XStr(@"%@nil", prefix);
} else if ([object isKindOfClass:NSArray.class]) {
NSArray *array = (NSArray *)object;
str = @"[";
for (id val in array) {
str = XStr(@"%@\n%@\t%@", str, prefix,
stringForObject(val, XStr(@"%@\t", prefix), dontTruncate));
}
str = XStr(@"%@\n%@]", str, prefix);
} else if ([object isKindOfClass:NSDictionary.class]) {
NSDictionary *dict = (NSDictionary *)object;
str = @"{";
for (id key in dict.allKeys) {
str =
XStr(@"%@\n%@\t%@: %@", str, prefix, key,
stringForObject(dict[key], XStr(@"%@\t", prefix), dontTruncate));
}
str = XStr(@"%@\n%@}", str, prefix);
} else {
if (!dontTruncate && [object isKindOfClass:NSString.class] &&
((NSString *)object).length > PUSHER_LOG_MAX_STRING_LENGTH) {
object =
XStr(@"%@...", [(NSString *)object
substringToIndex:PUSHER_LOG_MAX_STRING_LENGTH]);
}
str = XStr(@"%@%@", prefix, object);
}
return str;
}
static NSString *stringForObject(id object, BOOL dontTruncate) {
return stringForObject(object, @"", dontTruncate);
}
static void addToLogIfEnabled(NSString *service, BBBulletin *bulletin,
NSString *label, id object, BOOL dontTruncate) {
// allow global service which is @"" so empty
if (!XIsEmpty(service) && pusherEnabledLogs &&
![pusherEnabledLogs containsObject:service]) {
XLog(@"[S:%@] Log Disabled", service);
return;
}
CFPreferencesSynchronize(PUSHER_LOG_ID, kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
CFArrayRef keyList = CFPreferencesCopyKeyList(
PUSHER_LOG_ID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
NSDictionary *prefs = @{};
if (keyList) {
prefs = (NSDictionary *)CFPreferencesCopyMultiple(keyList, PUSHER_LOG_ID,
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
if (!prefs) {
prefs = @{};
}
CFRelease(keyList);
}
NSString *logKey = XStr(@"%@Log", service);
NSMutableArray *logSections = [(prefs[logKey] ?: @[]) mutableCopy];
NSString *currBulletinID = bulletin.bulletinID ?: @"empty_bulletin_id";
NSString *currSectionID = bulletin.sectionID ?: @"empty_app_id";
NSMutableDictionary *existingLogSection = nil;
int replaceIdx = -1;
for (int i = 0; i < logSections.count; i++) {
NSDictionary *logSection = logSections[i];
NSString *existingSectionID =
(NSString *)logSection[@"appID"] ?: @"empty_app_id";
NSDate *existingTimestamp = (NSDate *)logSection[@"timestamp"];
NSString *existingBulletinID =
(NSString *)logSection[@"bulletinID"] ?: @"empty_bulletin_id";
if (existingTimestamp && [existingTimestamp isKindOfClass:NSDate.class] &&
[existingTimestamp respondsToSelector:@selector(isEqualToDate:)] &&
[existingTimestamp isEqualToDate:bulletin.date] &&
XEq(existingBulletinID, currBulletinID) &&
XEq(existingSectionID, currSectionID)) {
existingLogSection = [logSection mutableCopy];
replaceIdx = i;
break;
}
}
if (!existingLogSection || replaceIdx == -1) {
existingLogSection = [@{
@"appID" : currSectionID,
@"bulletinID" : currBulletinID,
@"timestamp" : bulletin.date
} mutableCopy];
[logSections addObject:existingLogSection];
}
NSMutableArray *logs = [(existingLogSection[@"logs"] ?: @[]) mutableCopy];
existingLogSection[@"logs"] = logs;
if (logs.count == 0) {
[logs addObject:XStr(@"Processing %@", bulletin.sectionID)];
}
NSString *logItem = nil;
// if only one passed, only do one of them
if ((label && !object) || (!label && object)) {
logItem = label ?: stringForObject(object, dontTruncate);
} else {
logItem = XStr(@"%@: %@", label, stringForObject(object, dontTruncate));
}
[logs addObject:logItem];
if (replaceIdx > -1) {
[logSections replaceObjectAtIndex:replaceIdx withObject:existingLogSection];
}
// only keep last 100
if (logSections.count > 100) {
NSRange rangeToDelete = NSMakeRange(0, logSections.count - 100);
[logSections removeObjectsInRange:rangeToDelete];
}
// XLog(@"[S:%@] Saving log...", service);
CFPreferencesSetValue((__bridge CFStringRef)logKey,
(__bridge CFPropertyListRef)logSections, PUSHER_LOG_ID,
kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFPreferencesSynchronize(PUSHER_LOG_ID, kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
notify_post(PUSHER_LOG_PREFS_NOTIFICATION);
// XLog(@"[S:%@] Saved log!", service);
}
static void addToLogIfEnabled(NSString *service, BBBulletin *bulletin,
NSString *label, id object) {
addToLogIfEnabled(service, bulletin, label, object, NO);
}
static void addToLogIfEnabled(NSString *service, BBBulletin *bulletin,
NSString *label) {
addToLogIfEnabled(service, bulletin, label, nil, NO);
}
// returns array of all lowercase keys that begin with the given prefix that
// have a boolean value of true in the dictionary
static NSArray *getAppIDsWithPrefix(NSDictionary *prefs, NSString *prefix) {
NSMutableArray *keys = [NSMutableArray new];
for (id key in prefs.allKeys) {
if (![key isKindOfClass:NSString.class]) {
continue;
}
if ([key hasPrefix:prefix] && ((NSNumber *)prefs[key]).boolValue) {
NSString *subKey = [key substringFromIndex:prefix.length];
[keys addObject:subKey.lowercaseString];
}
}
return keys;
}
static NSArray *getSNSKeys(NSDictionary *prefs, NSString *prefix,
NSDictionary *backupPrefs, NSString *backupPrefix) {
NSMutableArray *keys = [NSMutableArray new];
NSDictionary *pusherDefaultSNSKeys = PUSHER_SNS_KEYS;
for (NSString *snsKey in pusherDefaultSNSKeys.allKeys) {
NSString *key = XStr(@"%@%@", prefix, snsKey);
id val = prefs[key];
if (val) {
if (((NSNumber *)val).boolValue) {
[keys addObject:snsKey];
}
continue;
} else if (!val && backupPrefs) {
NSString *backupKey = XStr(@"%@%@", backupPrefix, snsKey);
if (backupPrefs[backupKey]) {
if (((NSNumber *)backupPrefs[backupKey]).boolValue) {
[keys addObject:snsKey];
}
continue;
}
}
// check default if val is nil, not if it's set to false
if (!val && pusherDefaultSNSKeys[snsKey] &&
((NSNumber *)pusherDefaultSNSKeys[snsKey]).boolValue) {
[keys addObject:snsKey];
}
}
return keys;
}
static NSArray *getSNSKeys(NSDictionary *prefs, NSString *prefix) {
return getSNSKeys(prefs, prefix, nil, nil);
}
static NSString *getServiceURL(NSString *service, NSDictionary *options) {
if (XEq(service, PUSHER_SERVICE_PUSHOVER)) {
return PUSHER_SERVICE_PUSHOVER_URL;
} else if (XEq(service, PUSHER_SERVICE_PUSHBULLET)) {
return PUSHER_SERVICE_PUSHBULLET_URL;
} else if (XEq(service, PUSHER_SERVICE_IFTTT)) {
return [PUSHER_SERVICE_IFTTT_URL
stringByReplacingOccurrencesOfString:@"REPLACE_EVENT_NAME"
withString:options[@"eventName"]];
} else if (XEq(service, PUSHER_SERVICE_PUSHER_RECEIVER)) {
return [PUSHER_SERVICE_PUSHER_RECEIVER_URL
stringByReplacingOccurrencesOfString:@"REPLACE_DB_NAME"
withString:options[@"dbName"]];
}
return @"";
}
static NSString *getServiceAppID(NSString *service) {
if (XEq(service, PUSHER_SERVICE_PUSHOVER)) {
return PUSHER_SERVICE_PUSHOVER_APP_ID;
} else if (XEq(service, PUSHER_SERVICE_PUSHBULLET)) {
return PUSHER_SERVICE_PUSHBULLET_APP_ID;
}
return @"";
}
static PusherAuthorizationType getServiceAuthType(NSString *service,
NSDictionary *servicePrefs) {
if (servicePrefs[@"isCustomService"] &&
((NSNumber *)servicePrefs[@"isCustomService"]).boolValue) {
NSNumber *authMethod = servicePrefs[@"authenticationMethod"];
if (!authMethod) {
return PusherAuthorizationTypeNone;
}
switch (authMethod.intValue) {
case 1:
return PusherAuthorizationTypeHeader;
case 2:
return PusherAuthorizationTypeCredentials;
default:
return PusherAuthorizationTypeNone;
}
} else if (XEq(service, PUSHER_SERVICE_PUSHOVER)) {
return PusherAuthorizationTypeCredentials;
} else if (XEq(service, PUSHER_SERVICE_PUSHBULLET) ||
XEq(service, PUSHER_SERVICE_PUSHER_RECEIVER)) {
return PusherAuthorizationTypeHeader;
}
return PusherAuthorizationTypeReplaceKey; // ifttt key
}
static NSString *base64RepresentationForImage(UIImage *image) {
NSData *iconData = UIImagePNGRepresentation(image);
return [iconData
base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}
static UIImage *shrinkImage(UIImage *image, CGFloat factor) {
CGSize newSize =
CGSizeMake(image.size.width / factor, image.size.height / factor);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return smallImage;
// UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc]
// initWithSize:newSize]; return [renderer
// imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
// [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
// }];
}
static void pusherPrefsChanged() {
XLog(@"Reloading prefs");
CFArrayRef keyList = CFPreferencesCopyKeyList(
PUSHER_APP_ID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
NSDictionary *prefs = @{};
if (keyList) {
prefs = (NSDictionary *)CFPreferencesCopyMultiple(keyList, PUSHER_APP_ID,
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
if (!prefs) {
prefs = @{};
}
CFRelease(keyList);
}
id val = prefs[@"Enabled"];
pusherEnabled = val ? ((NSNumber *)val).boolValue : YES;
val = prefs[@"WhenToPush"];
pusherWhenToPush =
val ? ((NSNumber *)val).intValue : PUSHER_WHEN_TO_PUSH_EITHER;
val = prefs[@"WhatNetwork"];
pusherWhatNetwork =
val ? ((NSNumber *)val).intValue : PUSHER_WHAT_NETWORK_ANY;
val = prefs[@"GlobalAppListIsBlacklist"];
globalAppListIsBlacklist = val ? ((NSNumber *)val).boolValue : YES;
val = prefs[@"SufficientNotificationSettingsIsAnd"];
pusherSNSIsAnd = val ? ((NSNumber *)val).boolValue : YES;
val = prefs[@"SNSORRequireAllowNotifications"];
pusherSNSRequireANWithOR = val ? ((NSNumber *)val).boolValue : YES;
globalAppList = getAppIDsWithPrefix(prefs, NSPPreferenceGlobalBLPrefix);
pusherSNS = getSNSKeys(prefs, NSPPreferenceSNSPrefix);
if (pusherEnabledServices == nil) {
pusherEnabledServices = [NSMutableDictionary new];
}
if (pusherServicePrefs == nil) {
pusherServicePrefs = [NSMutableDictionary new];
}
pusherEnabledLogs = [NSMutableArray new];
NSDictionary *customServices = prefs[NSPPreferenceCustomServicesKey];
for (NSString *service in customServices.allKeys) {
NSDictionary *customService = customServices[service];
NSMutableDictionary *servicePrefs = [customService mutableCopy];
servicePrefs[@"isCustomService"] = @YES;
servicePrefs[@"appList"] =
getAppIDsWithPrefix(prefs, NSPPreferenceCustomServiceBLPrefix(service));
servicePrefs[@"whenToPush"] =
(!servicePrefs[@"whenToPush"] ||
((NSNumber *)servicePrefs[@"whenToPush"]).intValue ==
PUSHER_SEGMENT_CELL_DEFAULT)
? @(pusherWhenToPush)
: servicePrefs[@"whenToPush"];
servicePrefs[@"whatNetwork"] =
(!servicePrefs[@"whatNetwork"] ||
((NSNumber *)servicePrefs[@"whatNetwork"]).intValue ==
PUSHER_SEGMENT_CELL_DEFAULT)
? @(pusherWhatNetwork)
: servicePrefs[@"whatNetwork"];
servicePrefs[@"snsIsAnd"] =
servicePrefs[@"SufficientNotificationSettingsIsAnd"]
?: @(pusherSNSIsAnd);
servicePrefs[@"snsRequireANWithOR"] =
servicePrefs[@"SNSORRequireAllowNotifications"]
?: @(pusherSNSRequireANWithOR);
servicePrefs[@"sns"] = getSNSKeys(customService, NSPPreferenceSNSPrefix,
prefs, NSPPreferenceSNSPrefix);
NSString *logEnabledKey = XStr(@"%@LogEnabled", service);
id val = prefs[logEnabledKey];
BOOL logEnabled = val ? ((NSNumber *)val).boolValue : YES;
if (logEnabled) {
[pusherEnabledLogs addObject:service];
}
NSString *customAppsKey = NSPPreferenceCustomServiceCustomAppsKey(service);
// custom apps
NSDictionary *prefCustomApps = (NSDictionary *)prefs[customAppsKey] ?: @{};
NSMutableDictionary *customApps = [NSMutableDictionary new];
for (NSString *customAppID in prefCustomApps.allKeys) {
NSDictionary *customAppPrefs = prefCustomApps[customAppID];
// skip if custom app is disabled, default enabled so ignore bool check if
// key doesn't exist COMMENTED OUT BECAUSE REMOVED ENABLE/DISABLE STATUS
// FOR CUSTOM APPS if (customAppPrefs[@"enabled"] && !((NSNumber *)
// customAppPrefs[@"enabled"]).boolValue) { continue;
// }
if (!customAppPrefs) {
continue;
}
customApps[customAppID] = [customAppPrefs copy];
}
servicePrefs[@"customApps"] = [customApps copy];
pusherServicePrefs[service] = [servicePrefs copy];
// default is service disabled
if (customService[@"Enabled"] == nil ||
!((NSNumber *)customService[@"Enabled"]).boolValue) {
// skip if disabled
[pusherEnabledServices removeObjectForKey:service];
} else {
pusherEnabledServices[service] = pusherServicePrefs[service];
}
}
for (NSString *service in BUILTIN_PUSHER_SERVICES) {
NSMutableDictionary *servicePrefs = [NSMutableDictionary new];
NSString *appListPrefix = XStr(@"%@BL-", service);
NSString *tokenKey = XStr(@"%@Token", service);
NSString *userKey = XStr(@"%@User", service);
NSString *keyKey = XStr(@"%@Key", service);
NSString *devicesKey = XStr(@"%@Devices", service);
NSString *soundsKey = XStr(@"%@Sounds", service);
NSString *eventNameKey = XStr(@"%@EventName", service);
NSString *dateFormatKey = XStr(@"%@DateFormat", service);
NSString *customAppsKey = NSPPreferenceBuiltInServiceCustomAppsKey(service);
NSString *appListIsBlacklistKey = XStr(@"%@AppListIsBlacklist", service);
NSString *dbNameKey = XStr(@"%@DBName", service);
NSString *whenToPushKey = XStr(@"%@WhenToPush", service);
NSString *whatNetworkKey = XStr(@"%@WhatNetwork", service);
NSString *snsIsAndKey =
XStr(@"%@SufficientNotificationSettingsIsAnd", service);
NSString *snsRequireANWithORKey =
XStr(@"%@SNSORRequireAllowNotifications", service);
NSString *snsPrefix = XStr(@"%@%@", service, NSPPreferenceSNSPrefix);
NSString *logEnabledKey = XStr(@"%@LogEnabled", service);
servicePrefs[@"appList"] = getAppIDsWithPrefix(prefs, appListPrefix);
val = prefs[appListIsBlacklistKey];
servicePrefs[@"appListIsBlacklist"] = [(val ?: @YES) copy];
val = prefs[tokenKey];
servicePrefs[@"token"] = [(val ?: @"") copy];
val = prefs[userKey];
servicePrefs[@"user"] = [(val ?: @"") copy];
val = prefs[keyKey];
servicePrefs[@"key"] = [(val ?: @"") copy];
val = prefs[eventNameKey];
NSString *eventName = [(val ?: @"") copy];
val = prefs[dbNameKey];
NSString *dbName = [[(val ?: @"")
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]]
copy];
val = prefs[dateFormatKey];
servicePrefs[@"dateFormat"] = [(val ?: @"") copy];
servicePrefs[@"url"] =
getServiceURL(service, @{@"eventName" : eventName, @"dbName" : dbName});
val = prefs[whenToPushKey];
servicePrefs[@"whenToPush"] =
val ?: @(pusherWhenToPush); // if not set, go with default
servicePrefs[@"whenToPush"] =
[(((NSNumber *)servicePrefs[@"whenToPush"]).intValue ==
PUSHER_SEGMENT_CELL_DEFAULT
? @(pusherWhenToPush)
: servicePrefs[@"whenToPush"]) copy];
val = prefs[whatNetworkKey];
servicePrefs[@"whatNetwork"] =
val ?: @(pusherWhatNetwork); // if not set, go with default
servicePrefs[@"whatNetwork"] =
[(((NSNumber *)servicePrefs[@"whatNetwork"]).intValue ==
PUSHER_SEGMENT_CELL_DEFAULT
? @(pusherWhatNetwork)
: servicePrefs[@"whatNetwork"]) copy];
val = prefs[snsIsAndKey];
servicePrefs[@"snsIsAnd"] = [(val ?: @YES) copy];
val = prefs[snsRequireANWithORKey];
servicePrefs[@"snsRequireANWithOR"] = [(val ?: @YES) copy];
servicePrefs[@"sns"] =
getSNSKeys(prefs, snsPrefix, prefs, NSPPreferenceSNSPrefix);
val = prefs[logEnabledKey];
BOOL logEnabled = val ? ((NSNumber *)val).boolValue : YES;
if (logEnabled) {
[pusherEnabledLogs addObject:service];
}
if (XEq(service, PUSHER_SERVICE_IFTTT)) {
NSString *includeIconKey = XStr(@"%@IncludeIcon", service);
servicePrefs[@"includeIcon"] = prefs[includeIconKey] ?: @NO;
NSString *curateDataKey = XStr(@"%@CurateData", service);
servicePrefs[@"curateData"] = prefs[curateDataKey] ?: @YES;
}
if (XEq(service, PUSHER_SERVICE_PUSHER_RECEIVER)) {
NSString *includeIconKey = XStr(@"%@IncludeIcon", service);
servicePrefs[@"includeIcon"] = prefs[includeIconKey] ?: @YES;
NSString *includeImageKey = XStr(@"%@IncludeImage", service);
servicePrefs[@"includeImage"] = prefs[includeImageKey] ?: @YES;
NSString *imageMaxWidthKey = XStr(@"%@ImageMaxWidth", service);
servicePrefs[@"imageMaxWidth"] =
prefs[imageMaxWidthKey] ?: @(PUSHER_DEFAULT_MAX_WIDTH);
NSString *imageMaxHeightKey = XStr(@"%@ImageMaxHeight", service);
servicePrefs[@"imageMaxHeight"] =
prefs[imageMaxHeightKey] ?: @(PUSHER_DEFAULT_MAX_HEIGHT);
NSString *imageShrinkFactorKey = XStr(@"%@ImageShrinkFactor", service);
servicePrefs[@"imageShrinkFactor"] =
prefs[imageShrinkFactorKey] ?: @(PUSHER_DEFAULT_SHRINK_FACTOR);
}
// devices
NSArray *devices = prefs[devicesKey] ?: @[];
NSMutableArray *enabledDevices = [NSMutableArray new];
for (NSDictionary *device in devices) {
if (((NSNumber *)device[@"enabled"]).boolValue) {
[enabledDevices addObject:device];
}
}
servicePrefs[@"devices"] = [enabledDevices retain];
// [enabledDevices release];
// [devices release];
// sounds
NSArray *sounds = prefs[soundsKey] ?: @[];
NSMutableArray *enabledSounds = [NSMutableArray new];
for (NSDictionary *sound in sounds) {
if (((NSNumber *)sound[@"enabled"]).boolValue) {
[enabledSounds addObject:sound[@"id"]];
}
}
servicePrefs[@"sounds"] = [enabledSounds retain];
// custom apps
NSDictionary *prefCustomApps = (NSDictionary *)prefs[customAppsKey] ?: @{};
NSMutableDictionary *customApps = [NSMutableDictionary new];
for (NSString *customAppID in prefCustomApps.allKeys) {
NSDictionary *customAppPrefs = prefCustomApps[customAppID];
// skip if custom app is disabled, default enabled so ignore bool check if
// key doesn't exist
if (customAppPrefs[@"enabled"] &&
!((NSNumber *)customAppPrefs[@"enabled"]).boolValue) {
continue;
}
NSArray *customAppDevices = customAppPrefs[@"devices"] ?: @{};
NSMutableArray *customAppEnabledDevices = [NSMutableArray new];
for (NSDictionary *customAppDevice in customAppDevices) {
if (((NSNumber *)customAppDevice[@"enabled"]).boolValue) {
[customAppEnabledDevices addObject:customAppDevice];
}
}
NSArray *customAppSounds = customAppPrefs[@"sounds"] ?: @{};
NSMutableArray *customAppEnabledSounds = [NSMutableArray new];
for (NSDictionary *customAppSound in customAppSounds) {
if (((NSNumber *)customAppSound[@"enabled"]).boolValue) {
[customAppEnabledSounds addObject:customAppSound[@"id"]];
}
}
NSString *customAppEventName =
[(customAppPrefs[@"eventName"] ?: eventName) retain];
NSString *customAppUrl = getServiceURL(
service, @{@"eventName" : customAppEventName, @"dbName" : dbName});
NSMutableDictionary *customAppIDPref = [@{
@"devices" : [customAppEnabledDevices retain],
@"sounds" : [customAppEnabledSounds retain]
} mutableCopy];
if (!XEq(customAppUrl, servicePrefs[@"url"])) {
customAppIDPref[@"url"] = customAppUrl;
}
if (XEq(service, PUSHER_SERVICE_PUSHER_RECEIVER)) {
customAppIDPref[@"includeIcon"] =
customAppPrefs[@"includeIcon"] ?: @YES;
customAppIDPref[@"includeImage"] =
customAppPrefs[@"includeImage"] ?: @YES;
customAppIDPref[@"imageMaxWidth"] =
customAppPrefs[@"imageMaxWidth"] ?: @(PUSHER_DEFAULT_MAX_WIDTH);
customAppIDPref[@"imageMaxHeight"] =
customAppPrefs[@"imageMaxHeight"] ?: @(PUSHER_DEFAULT_MAX_HEIGHT);
customAppIDPref[@"imageShrinkFactor"] =
customAppPrefs[@"imageShrinkFactor"]
?: @(PUSHER_DEFAULT_SHRINK_FACTOR);
}
if (XEq(service, PUSHER_SERVICE_IFTTT)) {
customAppIDPref[@"includeIcon"] = customAppPrefs[@"includeIcon"] ?: @NO;
customAppIDPref[@"curateData"] = customAppPrefs[@"curateData"] ?: @YES;
}
customApps[customAppID] = customAppIDPref;
// [customAppEnabledDevices release];
// [customAppDevices release];
// [customAppPrefs release];
}
servicePrefs[@"customApps"] = [customApps copy];
// [customApps release];
pusherServicePrefs[service] = [servicePrefs copy];
NSString *enabledKey = XStr(@"%@Enabled", service);
// default is service disabled
if (prefs[enabledKey] == nil ||
!((NSNumber *)prefs[enabledKey]).boolValue) {
// skip if disabled
[pusherEnabledServices removeObjectForKey:service];
} else {
pusherEnabledServices[service] = pusherServicePrefs[service];
}
}
XLog(@"Reloaded");
}
static NSString *snsSaysNo(NSArray *sns, BBSectionInfo *sectionInfo, BOOL isAnd,
BOOL requireANWithOR) {
if (!isAnd && requireANWithOR && !sectionInfo.allowsNotifications) {
return @"'OR' and 'Require Allow Notifications with OR' both on, but Allow "
@"Notifications is disabled in this app's settings.";
}
for (NSString *key in sns) {
BOOL sufficient = YES;
if (XEq(key, PUSHER_SUFFICIENT_ALLOW_NOTIFICATIONS_KEY)) {
sufficient = sectionInfo.allowsNotifications;
} else if (XEq(key, PUSHER_SUFFICIENT_LOCK_SCREEN_KEY)) {
sufficient = sectionInfo.showsInLockScreen;
} else if (XEq(key, PUSHER_SUFFICIENT_NOTIFICATION_CENTER_KEY)) {
sufficient = sectionInfo.showsInNotificationCenter;
} else if (XEq(key, PUSHER_SUFFICIENT_BANNERS_KEY)) {
sufficient = sectionInfo.alertType == BBSectionInfoAlertTypeBanner;
} else if (XEq(key, PUSHER_SUFFICIENT_BADGES_KEY)) {
sufficient = (sectionInfo.pushSettings &
BBActualSectionInfoPushSettingsBadges) != 0;
// } else if (XEq(key, PUSHER_SUFFICIENT_SOUNDS_KEY)) {
// sufficient = (sectionInfo.pushSettings &
// BBActualSectionInfoPushSettingsSounds) != 0;
} else if (XEq(key, PUSHER_SUFFICIENT_SHOWS_PREVIEWS_KEY)) {
sufficient = sectionInfo.showsMessagePreview;
}
// AND, so if any one is insufficient, just return right away
if (isAnd && !sufficient) {
return XStr(@"'AND' but %@ incorrect", key);
// OR, so just one sufficient is enough
} else if (!isAnd && sufficient) {
XLog(@"OR and sufficient: %@", key);
break;
}
}
// none passed as sufficient before so insufficient if OR
if (!isAnd) {
XLog(@"OR and insufficient");
return @"'OR' and none were correct";
}
return nil;
}
static NSString *deviceConditionsSayNo(int whenToPush, int whatNetwork) {
BOOL deviceIsLocked =
((SBLockScreenManager *)[%c(SBLockScreenManager) sharedInstance])
.isUILocked;
NSString *wifiName = [[%c(SBWiFiManager) sharedInstance] currentNetworkName];
BOOL onWiFi = wifiName != nil;
if ((whatNetwork == PUSHER_WHAT_NETWORK_WIFI_ONLY && !onWiFi) ||
(whatNetwork == PUSHER_WHAT_NETWORK_OFF_WIFI_ONLY && onWiFi)) {
return XStr(@"What Network set to %@ but %@on WiFi",
(whatNetwork == PUSHER_WHAT_NETWORK_WIFI_ONLY
? @"WiFi Only"
: (whatNetwork == PUSHER_WHAT_NETWORK_OFF_WIFI_ONLY
? @"Cellular Only"
: @"Any Network")),
onWiFi ? @"" : @"not ");
}
if ((whenToPush == PUSHER_WHEN_TO_PUSH_LOCKED && !deviceIsLocked) ||
(whenToPush == PUSHER_WHEN_TO_PUSH_UNLOCKED && deviceIsLocked)) {
return XStr(
@"When to Push set to %@ but device %@locked",
(whenToPush == PUSHER_WHEN_TO_PUSH_LOCKED
? @"Locked"
: (whenToPush == PUSHER_WHEN_TO_PUSH_UNLOCKED ? @"Unlocked"
: @"Either")),
deviceIsLocked ? @"" : @"un");
}
return nil;
}
static NSString *prefsSayNo(BBServer *server, BBBulletin *bulletin) {
XLog(@"-------Bulletin------- %@", bulletin.sectionID);
if (!pusherEnabled) {
return XStr(@"Pusher %@abled", pusherEnabled ? @"En" : @"Dis");
}
if (bulletin.sectionID &&
XEq(bulletin.sectionID, PUSHER_TEST_NOTIFICATION_SECTION_ID) &&
bulletin.title && XEq(bulletin.title, @"Pusher") && bulletin.message &&
[bulletin.message hasPrefix:PUSHER_TEST_PUSH_RESULT_PREFIX]) {
return @"Not forwarding test notification result banner";
}
if (!globalAppList || ![globalAppList isKindOfClass:NSArray.class]) {
return XStr(@"Global app list is nil, it shouldn't be. %@", globalAppList);
}
if (!server) {
return @"Server is nil, it shouldn't be";
}
BBSectionInfo *sectionInfo =
[server _sectionInfoForSectionID:bulletin.sectionID effective:YES];
if (!sectionInfo) {
return @"Section info is nil, it shouldn't be";
}
if (!pusherEnabledServices ||
![pusherEnabledServices isKindOfClass:NSDictionary.class]) {
return XStr(@"Enabled services is nil, it shouldn't be. %@",
pusherEnabledServices);
}
for (NSString *service in pusherEnabledServices.allKeys) {
NSDictionary *servicePrefs = pusherEnabledServices[service];
if (!servicePrefs || ![servicePrefs isKindOfClass:NSDictionary.class]) {
return @"Service prefs are nil, they shouldn't be";
}
}
return nil;
}
%group SB
%hook BBServer
%new
+ (BBServer *)pusherSharedInstance {
return bbServerInstance;
}
- (void)_addObserver:(id)arg1 {
bbServerInstance = self;
%orig;
}
%new
- (void)sendBulletinToPusher:(BBBulletin *)bulletin {
if (!bulletin) {
XLog(@"Bulletin nil");
return;
}
if (!bulletin.date) {
bulletin.date = [NSDate date]; // for logging to visual log
}
// bulletin.lastInterruptDate is nil upon respring, so ignore if that's the
// case
if (!bulletin.lastInterruptDate) {
XLog(@"Not forwarding, Last Interrupt Date: %@",
bulletin.lastInterruptDate);
addToLogIfEnabled(
@"", bulletin,
@"Last interrupt date nil (this should only happen if SpringBoard just "
@"restarted; if some other time, there is a problem)");
return;
}
if (!NSClassFromString(@"SBApplicationController") ||
![%c(SBApplicationController) sharedInstance]) {
XLog(@"SpringBoard not ready");
addToLogIfEnabled(@"", bulletin, @"SpringBoard not ready");
return;
}
// THIS METHOD IS BAD BECAUSE THEN NOTIFICATIONS DATED IN THE PAST (e.g.
// Messages, Mail, etc.) WILL NOT GET FORWARDED THIS IS WHY USE NEW METHOD OF
// lastInterruptDate = nil ABOVE
// // Check if notification within last 5 seconds so we don't send uncleared
// notifications every respring NSDate *fiveSecondsAgo = [[NSDate date]
// dateByAddingTimeInterval:-5]; if (bulletin.date && [bulletin.date
// compare:fiveSecondsAgo] == NSOrderedAscending) { XLog(@"Bulletin 5
// seconds or older"); addToLogIfEnabled(@"", bulletin, @"Notification
// dated greater than 5 seconds ago, not sending to prevent resending all
// notifications on respring"); return;
// }
NSString *appID = bulletin.sectionID;
SBApplication *app = [[%c(SBApplicationController) sharedInstance]
applicationWithBundleIdentifier:appID];
NSString *appName = app && app.displayName && app.displayName.length > 0
? app.displayName
: XStr(@"Unknown App: %@", appID);
NSString *prefsResponse = prefsSayNo(self, bulletin);
if (prefsResponse) {
XLog(@"Prefs say no: %@", prefsResponse);
addToLogIfEnabled(@"", bulletin, XStr(@"Global prefs: %@", prefsResponse));
return;
}
// App list contains lowercase app IDs
BOOL appListContainsApp =
[globalAppList containsObject:appID.lowercaseString];
if (globalAppListIsBlacklist == appListContainsApp) {
XLog(@"[Global] Blocked by app list: %@", appID);
addToLogIfEnabled(
@"", bulletin,
XStr(@"Blocked by global app list (%@)",
globalAppListIsBlacklist ? @"blacklist" : @"whitelist"));
return;
}
NSString *title = XStr(@"%@%@", appName,
(bulletin.title && bulletin.title.length > 0
? XStr(@": %@", bulletin.title)
: @""));
NSString *message = @"";
if (bulletin.showsSubtitle && bulletin.subtitle &&
bulletin.subtitle.length > 0) {
message = bulletin.subtitle;
}
message = XStr(
@"%@%@%@", message,
(message.length > 0 && bulletin.message && bulletin.message.length > 0
? @"\n"
: @""),
bulletin.message ? bulletin.message : @"");
for (NSString *recentNotificationTitle in recentNotificationTitles) {
// prevent looping by checking if this title contains any recent titles in
// format of "App [Previous Title]"
if (XEq(title, XStr(@"%@: %@", appName, recentNotificationTitle))) {
XLog(@"Prevented loop");
addToLogIfEnabled(@"", bulletin, @"Prevented loop");
return;
}
}
// keep array small, looping shouldn't happen after 50 notifications have
// already passed
if (recentNotificationTitles.count >= 50) {
[recentNotificationTitles removeAllObjects];
}
[recentNotificationTitles addObject:title];
if (pusherEnabledServices.count == 0) {
XLog(@"No services enabled!");
return;
}
for (NSString *service in pusherEnabledServices.allKeys) {
[self sendToPusherService:service
bulletin:bulletin
appID:appID
appName:appName
title:title
message:message
isTest:NO];
}
}
%new
- (void)sendToPusherService:(NSString *)service bulletin:(BBBulletin *)bulletin appID:(NSString *)appID
appName:(NSString *)appName title:(NSString *)title message:(NSString *)message isTest:(BOOL)isTest {
if (!isTest && XEq(appID, getServiceAppID(service))) {
XLog(@"Prevented loop from same app");
addToLogIfEnabled(service, bulletin, @"Prevented loop from same app");
return;
}
NSDictionary *servicePrefs = pusherServicePrefs[service];
if (!isTest) {
NSArray *serviceAppList = servicePrefs[@"appList"];
BOOL appListContainsApp =
[serviceAppList containsObject:appID.lowercaseString];
id appListIsBlacklist = servicePrefs[@"appListIsBlacklist"];
if (appListIsBlacklist &&
((NSNumber *)appListIsBlacklist).boolValue == appListContainsApp) {
XLog(@"[S:%@] Blocked by app list: %@", service, appID);
addToLogIfEnabled(service, bulletin,
XStr(@"Blocked by app list (%@)",
((NSNumber *)appListIsBlacklist).boolValue
? @"blacklist"
: @"whitelist"));
return;
}
NSArray *sns = servicePrefs[@"sns"];
BOOL isAnd = servicePrefs[@"snsIsAnd"] &&
((NSNumber *)servicePrefs[@"snsIsAnd"]).boolValue;
BOOL requireANWithOR =
servicePrefs[@"snsRequireANWithOR"] &&
((NSNumber *)servicePrefs[@"snsRequireANWithOR"]).boolValue;
BBSectionInfo *sectionInfo =
[self _sectionInfoForSectionID:bulletin.sectionID effective:YES];
if (!sectionInfo) {
XLog(@"[S:%@,A:%@] sectionInfo nil", service, appID);
addToLogIfEnabled(service, bulletin,
@"sectionInfo is nil, it should not be");
return;
}
XLog(@"[S:%@,A:%@] Doing SNS", service, appID);
NSString *snsResponse = snsSaysNo(sns, sectionInfo, isAnd, requireANWithOR);
if (snsResponse) {
XLog(@"[S:%@,A:%@] SNS said no: %@", service, appID, snsResponse);
addToLogIfEnabled(service, bulletin, snsResponse);
return;
} else {
XLog(@"[S:%@,A:%@] SNS ok", service, appID);
}
int serviceWhenToPush = ((NSNumber *)servicePrefs[@"whenToPush"]).intValue;
int serviceWhatNetwork =
((NSNumber *)servicePrefs[@"whatNetwork"]).intValue;
XLog(@"[S:%@,A:%@] Doing Device Conditions", service, appID);
NSString *deviceConditionsResponse =
deviceConditionsSayNo(serviceWhenToPush, serviceWhatNetwork);
if (deviceConditionsResponse) {
XLog(@"[S:%@,A:%@] Device Conditions said no: %@", service, appID,
deviceConditionsResponse);
addToLogIfEnabled(service, bulletin, deviceConditionsResponse);
return;
} else {
XLog(@"[S:%@,A:%@] Device Conditions ok", service, appID);
}
}
// Custom app prefs?
NSDictionary *customApps = servicePrefs[@"customApps"];
NSArray *devices = servicePrefs[@"devices"];
NSArray *sounds = servicePrefs[@"sounds"];
NSString *url = servicePrefs[@"url"];
NSNumber *includeIcon =
servicePrefs[@"includeIcon"]
?: @NO; // default NO for custom services, default check for built in
// services done earlier so should never get to this NO
NSNumber *includeImage =
servicePrefs[@"includeImage"]
?: @NO; // default NO for custom services, default check for built in
// services done earlier so should never get to this NO
NSNumber *curateData = servicePrefs[@"curateData"] ?: @YES;
NSNumber *imageMaxWidth =
servicePrefs[@"imageMaxWidth"] ?: @(PUSHER_DEFAULT_MAX_WIDTH);
NSNumber *imageMaxHeight =
servicePrefs[@"imageMaxHeight"] ?: @(PUSHER_DEFAULT_MAX_HEIGHT);
NSNumber *imageShrinkFactor =
servicePrefs[@"imageShrinkFactor"] ?: @(PUSHER_DEFAULT_SHRINK_FACTOR);
if ([customApps.allKeys containsObject:appID]) {
NSDictionary *customAppPref = customApps[appID];
devices = customAppPref[@"devices"] ?: devices;
sounds = customAppPref[@"sounds"] ?: sounds;
url = customAppPref[@"url"] ?: url;
includeIcon = customAppPref[@"includeIcon"] ?: includeIcon;
includeImage = customAppPref[@"includeImage"] ?: includeImage;
curateData = customAppPref[@"curateData"] ?: curateData;
imageMaxWidth = customAppPref[@"imageMaxWidth"] ?: imageMaxWidth;
imageMaxHeight = customAppPref[@"imageMaxHeight"] ?: imageMaxHeight;
imageShrinkFactor =
customAppPref[@"imageShrinkFactor"] ?: imageShrinkFactor;
}
// Send
PusherAuthorizationType authType = getServiceAuthType(service, servicePrefs);
NSDictionary *infoDict = [self
getPusherInfoDictionaryForService:service
withDictionary:@{
@"title" : title ?: @"",
@"message" : message ?: @"",
@"devices" : devices ?: @[],
@"sounds" : sounds ?: @[],
@"appName" : appName ?: @"",
@"bulletin" : bulletin,
@"dateFormat" : XStrDefault(
servicePrefs[@"dateFormat"], @"MMM d, h:mm a"),
@"includeIcon" : includeIcon,
@"includeImage" : includeImage,
@"curateData" : curateData,
@"imageMaxWidth" : imageMaxWidth,
@"imageMaxHeight" : imageMaxHeight,
@"imageShrinkFactor" : imageShrinkFactor
}];