-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOrangered.xm
732 lines (592 loc) · 32.8 KB
/
Orangered.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
#import "Orangered.h"
#import "ORProviders.h"
#import "External/FDKeychain/FDKeychain.h"
#import "External/RedditKit/RedditKit.h"
#import "External/RedditKit/AFNetworking/AFNetworking.h"
#import <PersistentConnection/PersistentConnection.h>
#import <UIKit/UIApplication+Private.h>
#import <BulletinBoard/BulletinBoard.h>
#import <SpringBoard/SBMediaController.h>
#import <ToneLibrary/ToneLibrary.h>
/*
/$$ /$$
| $$ | $$
| $$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$
| $$__ $$ |____ $$ /$$__ $$ /$$__ $$ /$$__ $$
| $$ \ $$ /$$$$$$$| $$ | $$| $$ \ $$| $$$$$$$$
| $$ | $$ /$$__ $$| $$ | $$| $$ | $$| $$_____/
| $$$$$$$/| $$$$$$$| $$$$$$$| $$$$$$$| $$$$$$$
|_______/ \_______/ \_______/ \____ $$ \_______/
/$$ \ $$
| $$$$$$/
\______/
*/
static void orangeredSetDisplayIdentifierBadge(NSString *displayIdentifier, NSInteger badgeValue) {
SBIconModel *iconModel = MSHookIvar<SBIconModel *>([%c(SBIconController) sharedInstance], "_iconModel");
NSString *stringBadgeValue;
if (badgeValue > 0) {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
stringBadgeValue = [formatter stringForObjectValue:@(badgeValue)];
}
SBApplicationIcon *clientAppIcon = [iconModel applicationIconForBundleIdentifier:displayIdentifier];
[clientAppIcon setBadge:stringBadgeValue];
ORLOG(@"set badge %@ to %@", stringBadgeValue, clientAppIcon);
}
/*
/$$ /$$ /$$ /$$ /$$
| $$ | $$| $$ | $$ |__/
| $$$$$$$ /$$ /$$| $$| $$ /$$$$$$ /$$$$$$ /$$ /$$$$$$$
| $$__ $$| $$ | $$| $$| $$ /$$__ $$|_ $$_/ | $$| $$__ $$
| $$ \ $$| $$ | $$| $$| $$| $$$$$$$$ | $$ | $$| $$ \ $$
| $$ | $$| $$ | $$| $$| $$| $$_____/ | $$ /$$| $$| $$ | $$
| $$$$$$$/| $$$$$$/| $$| $$| $$$$$$$ | $$$$/| $$| $$ | $$
|_______/ \______/ |__/|__/ \_______/ \___/ |__/|__/ |__/
*/
static void orangeredAddBulletin(BBServer *server, OrangeredProvider *provider, BBBulletinRequest *bulletin) {
BBDataProviderAddBulletin(provider, bulletin); // This works in iOS 8.1.2
}
static ORAlertViewDelegate *orangeredAlertDelegate;
static PCSimpleTimer *orangeredTimer;
static NSError *orangeredError;
static BOOL checkOnUnlock;
static NSTimeInterval lastRequestInterval;
static NSDate *lastMessageDate;
static NSMutableSet *previousUnreadMessages;
static NSDateFormatter *orangeredDateFormatter;
static NSString *previousClientIdentifier;
/*
/$$ /$$
| $$ | $$
/$$$$$$ /$$$$$$$ | $$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$
/$$__ $$| $$__ $$| $$__ $$ /$$__ $$ |____ $$ /$$__ $$ /$$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \ $$ /$$$$$$$| $$ \__/| $$ | $$
| $$ | $$| $$ | $$| $$ | $$| $$ | $$ /$$__ $$| $$ | $$ | $$
| $$$$$$/| $$ | $$| $$$$$$$/| $$$$$$/| $$$$$$$| $$ | $$$$$$$
\______/ |__/ |__/|_______/ \______/ \_______/|__/ \_______/
*/
@implementation ORAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
HBPreferences *preferences = PREFS;
[preferences setBool:YES forKey:@"Ran Before"];
if (buttonIndex == [alertView cancelButtonIndex]) {
return;
}
[[UIApplication sharedApplication] openURL:[ORAlertViewDelegate sharedLaunchPreferencesURL]];
}
+ (NSURL *)sharedLaunchPreferencesURL {
if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Library/MobileSubstrate/DynamicLibraries/PreferenceOrganizer.dylib"]) {
return [NSURL URLWithString:@"prefs:root=Cydia&path=Orangered"];
}
else {
return [NSURL URLWithString:@"prefs:root=Orangered"];
}
}
@end
/*
/$$ /$$ /$$
|__/ | $$ | $$
/$$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$$$$$$ /$$$$$$ | $$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$
/$$_____/ /$$__ $$ /$$__ $$| $$| $$__ $$ /$$__ $$| $$__ $$ /$$__ $$ |____ $$ /$$__ $$ /$$__ $$
| $$$$$$ | $$ \ $$| $$ \__/| $$| $$ \ $$| $$ \ $$| $$ \ $$| $$ \ $$ /$$$$$$$| $$ \__/| $$ | $$
\____ $$| $$ | $$| $$ | $$| $$ | $$| $$ | $$| $$ | $$| $$ | $$ /$$__ $$| $$ | $$ | $$
/$$$$$$$/| $$$$$$$/| $$ | $$| $$ | $$| $$$$$$$| $$$$$$$/| $$$$$$/| $$$$$$$| $$ | $$$$$$$
|_______/ | $$____/ |__/ |__/|__/ |__/ \____ $$|_______/ \______/ \_______/|__/ \_______/
| $$ /$$ \ $$
| $$ | $$$$$$/
|__/ \______/
*/
%hook SBLockScreenManager
- (void)_finishUIUnlockFromSource:(NSInteger)source withOptions:(NSDictionary *)options {
%orig;
HBPreferences *preferences = PREFS;
if (![preferences boolForKey:@"Ran Before" default:NO]) {
ORLOG(@"First run, prompting...");
orangeredAlertDelegate = [[ORAlertViewDelegate alloc] init];
UIAlertView *orangeredAlert = [[UIAlertView alloc] initWithTitle:@"Orangered" message:@"Welcome to Orangered. You'll never miss a message again. Tap Begin to get started, or head to the settings anytime." delegate:orangeredAlertDelegate cancelButtonTitle:@"Later" otherButtonTitles:@"Begin", nil];
[orangeredAlert show];
}
else if (checkOnUnlock) {
checkOnUnlock = NO;
ORLOG(@"Checking on unlock due to authentication issues...");
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:kOrangeredCheckNotificationName object:nil userInfo:@{ @"sender" : @"SpringBoard" }];
}
}
%end
%hook SpringBoard
- (void)applicationDidFinishLaunching:(UIApplication *)application {
%orig();
orangeredDateFormatter = [[NSDateFormatter alloc] init];
[orangeredDateFormatter setDateFormat:@"yyyyMMdd_HHmmss"];
ORLOG(@"Sending check message from SpringBoard...");
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:kOrangeredCheckNotificationName object:nil userInfo:@{ @"sender" : @"SpringBoard" }];
}
%end
/*
/$$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$
/$$_____/ /$$__ $$ /$$__ $$| $$ /$$//$$__ $$ /$$__ $$
| $$$$$$ | $$$$$$$$| $$ \__/ \ $$/$$/| $$$$$$$$| $$ \__/
\____ $$| $$_____/| $$ \ $$$/ | $$_____/| $$
/$$$$$$$/| $$$$$$$| $$ \ $/ | $$$$$$$| $$
|_______/ \_______/|__/ \_/ \_______/|__/
*/
static BBServer *orangeredServer;
static BBDataProviderConnection *dataProviderConnection;
static BBDataProviderManager *dataProviderManager;
%hook BBServer
- (id)init {
orangeredServer = %orig();
return orangeredServer;
}
%end
%hook BBDataProviderConnection
- (id)initWithServiceName:(id)arg1 onQueue:(id)arg2 {
dataProviderConnection = %orig;
return dataProviderConnection;
}
%end
%hook BBDataProviderManager
- (id)initWithDelegate:(id)arg1 queue:(id)arg2 {
dataProviderManager = %orig;
return dataProviderManager;
}
%end
/*
/$$
| $$
/$$$$$$$/$$$$$$ /$$$$$$ /$$$$$$
/$$_____/_ $$_/ /$$__ $$ /$$__ $$
| $$ | $$ | $$ \ $$| $$ \__/
| $$ | $$ /$$| $$ | $$| $$
| $$$$$$$ | $$$$/| $$$$$$/| $$
\_______/ \___/ \______/ |__/
*/
%ctor {
HBPreferences *orangeredPreferences = PREFS;
/*
/$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$$
/$$__ $$ /$$__ $$ /$$__ $$| $$__ $$ | $$__ $$ /$$_____/
| $$ \ $$| $$ \ $$| $$$$$$$$| $$ \ $$ | $$ \ $$| $$
| $$ | $$| $$ | $$| $$_____/| $$ | $$ | $$ | $$| $$
| $$$$$$/| $$$$$$$/| $$$$$$$| $$ | $$ | $$ | $$| $$$$$$$
\______/ | $$____/ \_______/|__/ |__/ |__/ |__/ \_______/
| $$
| $$
|__/
*/
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:kOrangeredOpenNCNotificationName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
NSString *clientIdentifier = [orangeredPreferences objectForKey:@"clientIdentifier" default:nil];
NSURL *notificationCenterURL;
if (clientIdentifier) {
notificationCenterURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@&path=%@", [%c(PSNotificationSettingsDetail) preferencesURL].absoluteString, clientIdentifier]];
}
else {
notificationCenterURL = [%c(PSNotificationSettingsDetail) preferencesURL];
}
[[UIApplication sharedApplication] openURL:notificationCenterURL];
}];
/*
/$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$
/$$__ $$ /$$__ $$ /$$__ $$ /$$__ $$ /$$__ $$
| $$$$$$$$| $$ \__/| $$ \__/| $$ \ $$| $$ \__/
| $$_____/| $$ | $$ | $$ | $$| $$
| $$$$$$$| $$ | $$ | $$$$$$/| $$
\_______/|__/ |__/ \______/ |__/
*/
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:kOrangeredErrorNotificationName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
NSLog(@"Responding to error: %@", orangeredError);
if (orangeredError) {
UIAlertView *orangeredErrorAlert = [[UIAlertView alloc] initWithTitle:@"Orangered" message:[NSString stringWithFormat:@"%@", [orangeredError localizedDescription]] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[orangeredErrorAlert show];
orangeredError = nil;
}
}];
/*
/$$$$$$
/$$__ $$
/$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ | $$ \__/
/$$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$ /$$__ $$ /$$__ $$| $$$$
| $$ \ $$| $$ \ $$| $$$$$$$$| $$ \ $$ | $$ \ $$| $$ \__/| $$$$$$$$| $$_/
| $$ | $$| $$ | $$| $$_____/| $$ | $$ | $$ | $$| $$ | $$_____/| $$
| $$$$$$/| $$$$$$$/| $$$$$$$| $$ | $$ | $$$$$$$/| $$ | $$$$$$$| $$
\______/ | $$____/ \_______/|__/ |__/ | $$____/ |__/ \_______/|__/
| $$ | $$
| $$ | $$
|__/ |__/
*/
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:kOrangeredOpenPrefsNotificationName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
[[UIApplication sharedApplication] openURL:[ORAlertViewDelegate sharedLaunchPreferencesURL]];
}];
/*
/$$ /$$
| $$ | $$
/$$$$$$$| $$$$$$$ /$$$$$$ /$$$$$$$| $$ /$$
/$$_____/| $$__ $$ /$$__ $$ /$$_____/| $$ /$$/
| $$ | $$ \ $$| $$$$$$$$| $$ | $$$$$$/
| $$ | $$ | $$| $$_____/| $$ | $$_ $$
| $$$$$$$| $$ | $$| $$$$$$$| $$$$$$$| $$ \ $$
\_______/|__/ |__/ \_______/ \_______/|__/ \__/
*/
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:kOrangeredCheckNotificationName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
OrangeredProvider *notificationProvider = [OrangeredProvider sharedInstance];
NSString *sectionIdentifier = [notificationProvider sectionIdentifier];
// Let's cancel our appointments...
[orangeredTimer invalidate];
orangeredSetDisplayIdentifierBadge(sectionIdentifier, 0);
// Load some preferences...
BOOL enabled = [orangeredPreferences boolForKey:@"enabled" default:YES];
if (!enabled) {
return;
}
NSString *clientIdentifier = [orangeredPreferences objectForKey:@"clientIdentifier" default:nil];
BOOL hasApplicationWithIdentifier = [(SBApplicationController *)[%c(SBApplicationController) sharedInstance] applicationWithBundleIdentifier:clientIdentifier] != nil;
// If there's a saved client identifier, which is different from the current identifier,
// and an app with that identifier is installed, then swap out the data provider so it
// uses the correct section identifier.
if (clientIdentifier && ![clientIdentifier isEqualToString:sectionIdentifier] && hasApplicationWithIdentifier) {
ORLOG(@"Detected change in app, swapping around data providers...");
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
notificationProvider.customSectionID = sectionIdentifier = clientIdentifier;
}
// If the current clientIdentifier doesn't have an app associated with it, revert back
// to a random check.
else if (!hasApplicationWithIdentifier) {
ORLOG(@"Detected bonkers app, reassigning data providers...");
notificationProvider.customSectionID = nil;
}
// If our data provider has yet to be registered with the manager, go ahead and do that now.
// All bulletins will be ignored without a proper dataProvider (LOCAL or REMOTE) registered for a bulletin request
if (![dataProviderManager dataProviderForSectionID:[OrangeredProvider sharedInstance].sectionIdentifier]) {
[dataProviderConnection addDataProvider:[OrangeredProvider sharedInstance]];
}
CGFloat intervalUnit = [orangeredPreferences floatForKey:@"intervalControl" default:60.0];
if (intervalUnit > 0.0) {
NSString *refreshIntervalString = [orangeredPreferences objectForKey:@"refreshInterval" default:@"60"];
CGFloat refreshInterval = [refreshIntervalString floatValue] * intervalUnit;
orangeredTimer = [[PCSimpleTimer alloc] initWithTimeInterval:refreshInterval serviceIdentifier:@"com.insanj.orangered" target:notificationProvider selector:@selector(fireAway) userInfo:nil];
[orangeredTimer scheduleInRunLoop:[NSRunLoop mainRunLoop]];
ORLOG(@"Spun up timer (%@) to ping Reddit every %f seconds.", orangeredTimer, refreshInterval);
}
else if ([notification.userInfo[@"sender"] isEqualToString:@"SpringBoard"]) {
ORLOG(@"Appears our interval is set for Never, and pinging from Reddit. Killing time!");
return;
}
else {
ORLOG(@"Appears our interval is set for Never. Sulking time... :/");
}
CGFloat rateGuard = [orangeredPreferences floatForKey:@"rateGuard" default:0];
if (rateGuard) {
NSTimeInterval currentRequestInterval = [[NSDate date] timeIntervalSince1970];
if (lastRequestInterval <= 0.0) {
[orangeredPreferences setFloat:currentRequestInterval forKey:@"lastRequestStamp"];
lastRequestInterval = [orangeredPreferences floatForKey:@"lastRequestStamp" default:currentRequestInterval];
}
if (currentRequestInterval - lastRequestInterval < 3.0) { // "Make no more than thirty requests per minute." with a little bit of leeway
ORLOG(@"Rate limit says YOU SHALL NOT PASS (last request interval: %f, current request interval: %f).", lastRequestInterval, currentRequestInterval);
return;
}
else {
ORLOG(@"Rate limit is letting this one slide (last request interval: %f, current request interval: %f).", lastRequestInterval, currentRequestInterval);
}
lastRequestInterval = currentRequestInterval;
}
NSString *username = [orangeredPreferences objectForKey:@"username" default:@""];
NSString *passwordKey = [orangeredPreferences objectForKey:@"password" default:@""];
username = [username stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
passwordKey = [passwordKey stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Apparently RedditKit crashes out if either are nil? Bizarre.
if ([username length] == 0 || [passwordKey length] == 0) {
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
BBBulletinRequest *bulletin = [[BBBulletinRequest alloc] init];
bulletin.recordID = @"com.insanj.orangered.bulletin";
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
bulletin.bulletinID = (__bridge_transfer NSString *)uuidStringRef;
bulletin.title = @"Orangered";
bulletin.message = @"Uh-oh! Please check your username and password in the settings.";
bulletin.sectionID = clientIdentifier;
bulletin.date = [NSDate date];
bulletin.defaultAction = [BBAction actionWithLaunchURL:[ORAlertViewDelegate sharedLaunchPreferencesURL] callblock:nil];
orangeredAddBulletin(orangeredServer, notificationProvider, bulletin);
return;
}
BOOL repeatNotify = [orangeredPreferences boolForKey:@"repeatNotify" default:YES];
BOOL alwaysNotify = [orangeredPreferences boolForKey:@"alwaysNotify" default:YES];
BOOL alwaysMarkRead = [orangeredPreferences boolForKey:@"alwaysMarkRead" default:NO];
BOOL securePassword = [orangeredPreferences boolForKey:@"secure" default:YES];
BOOL combineMessages = [orangeredPreferences boolForKey:@"combineMessages" default:YES];
BOOL useMessageTimeStamp = [orangeredPreferences boolForKey:@"useMessageTimeStamp" default:YES];
NSString *password;
if (securePassword) {
// Let's get the real password, now that we've covered all the bases...
NSError *getItemForKeyError;
password = [FDKeychain itemForKey:passwordKey forService:@"Orangered" error:&getItemForKeyError];
if (getItemForKeyError.code == -25308) {
NSLog(@"Error trying to retrieve secured password, postponing until we're not at the lockscreen...");
checkOnUnlock = YES;
return;
}
else if (getItemForKeyError.code == -25300) {
// NSLog(@"Error trying to retrieve secured password, have to secure it: %@", getItemForKeyError);
password = [NSString stringWithString:passwordKey];
NSMutableString *mutableKey = [[NSMutableString alloc] init];
for (int i = 0; i < password.length; i++) {
[mutableKey appendFormat:@"%c", arc4random_uniform(26) + 'a'];
}
NSError *saveItemForKeyError;
[FDKeychain saveItem:password forKey:mutableKey forService:@"Orangered" inAccessGroup:nil withAccessibility:FDKeychainAccessibleAfterFirstUnlock error:&saveItemForKeyError];
if (saveItemForKeyError) {
NSLog(@"Error trying to secure password: %@", saveItemForKeyError);
return;
}
else {
ORLOG(@"Secured password successfully! :)");
[orangeredPreferences setObject:mutableKey forKey:@"password"];
}
}
else if (getItemForKeyError) {
NSLog(@"Fatal error trying to retrieve secure password: %@", getItemForKeyError);
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
BBBulletinRequest *bulletin = [[BBBulletinRequest alloc] init];
bulletin.recordID = @"com.insanj.orangered.bulletin";
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
bulletin.bulletinID = (__bridge_transfer NSString *)uuidStringRef;
bulletin.title = @"Orangered";
bulletin.message = [NSString stringWithFormat:@"Had trouble securing your password. Fix to authenticate: %@", getItemForKeyError];
bulletin.sectionID = clientIdentifier;
bulletin.date = [NSDate date];
bulletin.defaultAction = [BBAction actionWithLaunchURL:[ORAlertViewDelegate sharedLaunchPreferencesURL] callblock:nil];
orangeredAddBulletin(orangeredServer, notificationProvider, bulletin);
return;
}
else {
ORLOG(@"Accessed secured password successfully!");
}
}
else {
password = passwordKey;
}
RKClient *client = [RKClient sharedClient];
RKListingCompletionBlock unreadCompletionBlock = ^(NSArray *messages, RKPagination *pagination, NSError *error) {
if (![orangeredPreferences boolForKey:@"disableNetworkIndicator" default:NO]) {
[[UIApplication sharedApplication] _endShowingNetworkActivityIndicator];
}
ORLOG(@"Received unreadMessages response from Reddit: %@", messages);
if (alwaysMarkRead && [messages count] > 0) {
ORLOG(@"Ensuring messages are all marked read...");
[client markMessageArrayAsRead:messages completion:^(NSError *error) {
ORLOG(@"%@ cleared out unread messages.", error ? [NSString stringWithFormat:@"Failed (%@). Wishing I", [error localizedDescription]] : @"Successfully");
}];
}
OrangeredProvider *provider = [OrangeredProvider sharedInstance];
NSString *sectionID = [provider sectionIdentifier];
orangeredSetDisplayIdentifierBadge(sectionID, messages.count);
if (messages && messages.count > 0) {
RKMessage *message = messages[0];
if (!repeatNotify && lastMessageDate && [lastMessageDate compare:message.created] != NSOrderedAscending) {
ORLOG(@"Not publishing duplicate bulletin request (current message date '%@' is equal to or earlier than previous message date '%@').", message.created, lastMessageDate);
if (previousUnreadMessages) {
// Previous Notification Center items exist. Loop through current list of unread messages so we can remove previous NC items that are now marked as read
NSMutableSet *currentUnreadMessages = [[NSMutableSet alloc] init];
for (int i = [messages count] - 1; i >= 0; i--) {
RKMessage *message = messages[i];
NSString *publisherBulletinID = [NSString stringWithFormat:@"com.insanj.orangered.bulletin/%@/%@", [orangeredDateFormatter stringFromDate:message.created], message.author];
[currentUnreadMessages addObject:publisherBulletinID];
}
if (previousUnreadMessages && [currentUnreadMessages count] > 0) {
[previousUnreadMessages minusSet:currentUnreadMessages];
for (NSString *publisherID in previousUnreadMessages) {
[orangeredServer withdrawBulletinRequestsWithPublisherBulletinID:publisherID forSectionID:sectionIdentifier];
}
} else if (previousUnreadMessages) {
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
}
previousUnreadMessages = currentUnreadMessages;
}
}
else {
BOOL isRingerMuted = [[%c(SBMediaController) sharedInstance] isRingerMuted];
BBSound *savedSound = nil;
NSString *ringtoneIdentifier = [orangeredPreferences objectForKey:@"alertTone" default:nil];
if (ringtoneIdentifier && ![ringtoneIdentifier isEqualToString:@"<none>"]) {
NSString *ringtonePath;
if (isRingerMuted) {
ringtonePath = @"/Library/PreferenceBundles/ORPrefs.bundle/silent.caf";
} else {
TLToneManager *manager = [%c(TLToneManager) sharedToneManager];
ringtonePath = [manager filePathForToneIdentifier:ringtoneIdentifier];
}
savedSound = [[BBSound alloc] initWithSystemSoundID:0 soundPath:ringtonePath behavior:1 vibrationPattern:nil];
ORLOG(@"Assigning saved sound %@ to ringtone %@ to play...", ringtoneIdentifier, savedSound);
}
NSMutableSet *currentUnreadMessages = [[NSMutableSet alloc] init];
// Reverse through the array so the oldest messages get added first
for (int i = [messages count] - 1; i >= 0; i--) {
RKMessage *message = messages[i];
NSString *publisherBulletinID = [NSString stringWithFormat:@"com.insanj.orangered.bulletin/%@/%@", [orangeredDateFormatter stringFromDate:message.created], message.author];
[currentUnreadMessages addObject:publisherBulletinID];
if (!repeatNotify && previousUnreadMessages && [previousUnreadMessages containsObject:publisherBulletinID]) {
ORLOG(@"Skipping already notified message %@", publisherBulletinID);
continue;
}
BBBulletinRequest *bulletin = [[BBBulletinRequest alloc] init];
bulletin.recordID = @"com.insanj.orangered.bulletin";
bulletin.publisherBulletinID = combineMessages ? @"com.insanj.orangered.bulletin" : publisherBulletinID;
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
bulletin.bulletinID = (__bridge_transfer NSString *)uuidStringRef;
bulletin.sectionID = sectionID;
if ([clientIdentifier isEqualToString:@"com.apple.mobilesafari"]) {
bulletin.defaultAction = [BBAction actionWithLaunchURL:[NSURL URLWithString:@"https://www.reddit.com/message/inbox/"] callblock:nil];
}
else {
bulletin.defaultAction = [BBAction actionWithLaunchBundleID:clientIdentifier callblock:nil];
}
if (useMessageTimeStamp) {
bulletin.date = message.created;
}
else {
bulletin.date = [NSDate date];
}
if (savedSound) {
bulletin.sound = savedSound;
}
bulletin.showsUnreadIndicator = message.unread;
if (!combineMessages || messages.count == 1) {
bulletin.title = message.author;
bulletin.subtitle = message.subject;
bulletin.message = message.messageBody;
}
else {
if (useMessageTimeStamp) {
// Use the date of the newest message
bulletin.date = lastMessageDate;
}
bulletin.title = @"Orangered";
bulletin.message = [NSString stringWithFormat:@"You have %i unread messages.", (int)messages.count];
}
orangeredAddBulletin(orangeredServer, provider, bulletin);
if (combineMessages) {
break;
}
}
if (!combineMessages && previousUnreadMessages && [currentUnreadMessages count] > 0) {
// Always try to remove the bulletin used for combined messages
[orangeredServer withdrawBulletinRequestsWithPublisherBulletinID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
[previousUnreadMessages minusSet:currentUnreadMessages];
for (NSString *publisherID in previousUnreadMessages) {
[orangeredServer withdrawBulletinRequestsWithPublisherBulletinID:publisherID forSectionID:sectionIdentifier];
}
} else if (previousUnreadMessages || combineMessages) {
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
}
previousUnreadMessages = currentUnreadMessages;
lastMessageDate = message.created;
}
}
else if (alwaysNotify) {
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
BBBulletinRequest *bulletin = [[BBBulletinRequest alloc] init];
bulletin.recordID = @"com.insanj.orangered.bulletin";
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
bulletin.bulletinID = (__bridge_transfer NSString *)uuidStringRef;
bulletin.title = @"Orangered";
bulletin.sectionID = sectionID;
bulletin.date = [NSDate date];
BOOL isRingerMuted = [[%c(SBMediaController) sharedInstance] isRingerMuted];
BBSound *savedSound = nil;
NSString *ringtoneIdentifier = [orangeredPreferences objectForKey:@"alertTone" default:nil];
if (ringtoneIdentifier && ![ringtoneIdentifier isEqualToString:@"<none>"]) {
NSString *ringtonePath;
if (isRingerMuted) {
ringtonePath = @"/Library/PreferenceBundles/ORPrefs.bundle/silent.caf";
} else {
TLToneManager *manager = [%c(TLToneManager) sharedToneManager];
ringtonePath = [manager filePathForToneIdentifier:ringtoneIdentifier];
}
savedSound = [[BBSound alloc] initWithSystemSoundID:0 soundPath:ringtonePath behavior:1 vibrationPattern:nil];
ORLOG(@"Assigning saved sound %@ to ringtone %@ to play...", ringtoneIdentifier, savedSound);
}
if (savedSound) {
bulletin.sound = savedSound;
}
if ([clientIdentifier isEqualToString:@"com.apple.mobilesafari"]) {
bulletin.defaultAction = [BBAction actionWithLaunchURL:[NSURL URLWithString:@"https://www.reddit.com/message/inbox/"] callblock:nil];
}
else {
bulletin.defaultAction = [BBAction actionWithLaunchBundleID:clientIdentifier callblock:nil];
}
NSArray *phrases = @[@"Take a coffee break.", @"Relax.", @"Time to pick up that old ten-speed.", @"Reserve your cat facts.", @"Channel your zen.", @"Why stress?", @"Orange you glad I didn't say Orangered?", @"Let's chill.", @"Head over to 4chan.", @"Buy yourself a tweak.", @"Hey, don't blame me.", @"Orangered powering down.", @"Have a nice day!", @"Don't even trip."];
bulletin.message = [phrases[arc4random_uniform(phrases.count)] stringByAppendingString:@" No new messages found."];
orangeredAddBulletin(orangeredServer, notificationProvider, bulletin);
}
else {
// When there are no new messages, remove all previous items from the Notification Center
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
}
}; // end unreadCompletionBlock
// Time to do some WERK
if ([client isSignedIn] && (![client.currentUser.username isEqualToString:username])) {
ORLOG(@"Detected user changed, signing out...");
[client signOut];
}
if (![orangeredPreferences boolForKey:@"disableNetworkIndicator" default:NO]) {
[[UIApplication sharedApplication] _beginShowingNetworkActivityIndicator];
}
if (![client isSignedIn]) {
ORLOG(@"No existing user session detected, signing in...");
// Sign in using RedditKit and supplied login information, and ping for unread messages.
[client signInWithUsername:username password:password completion:^(NSError *error) {
if (error) {
[orangeredServer withdrawBulletinRequestsWithRecordID:@"com.insanj.orangered.bulletin" forSectionID:sectionIdentifier];
ORLOG(@"Encountered error (%@, %@), pushing bulletin request...", error, error.userInfo);
BBBulletinRequest *bulletin = [[BBBulletinRequest alloc] init];
bulletin.recordID = @"com.insanj.orangered.bulletin";
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
bulletin.bulletinID = (__bridge_transfer NSString *)uuidStringRef;
bulletin.title = @"Orangered";
NSString *relevantMessage;
switch ((int)error.code) {
default:
relevantMessage = [NSString stringWithFormat:@"Uh-oh! %@", [error localizedDescription]];
break;
case 203:
relevantMessage = [NSString stringWithFormat:@"Invalid credentials. Reddit can't log you in with that username or password."];
break;
case 204:
relevantMessage = [NSString stringWithFormat:@"Reddit has rate limited your device. Wait before using Orangered again!"];
break;
case -1009:
relevantMessage = [NSString stringWithFormat:@"Rats! Please check your internet connection and try again."];
break;
}
bulletin.message = relevantMessage;
bulletin.sectionID = clientIdentifier;
bulletin.date = [NSDate date];
bulletin.defaultAction = [BBAction actionWithLaunchURL:[ORAlertViewDelegate sharedLaunchPreferencesURL] callblock:nil];
orangeredError = error;
orangeredAddBulletin(orangeredServer, notificationProvider, bulletin);
if (![orangeredPreferences boolForKey:@"disableNetworkIndicator" default:NO]) {
[[UIApplication sharedApplication] _endShowingNetworkActivityIndicator];
}
return;
}
// If properly signed in, check for unread messages...
[client unreadMessagesWithPagination:[RKPagination paginationWithLimit:100] markRead:alwaysMarkRead completion:unreadCompletionBlock];
}];
}
else {
ORLOG(@"Existing user session detected, pinging Reddit...");
// Check for unread messages...
[client unreadMessagesWithPagination:[RKPagination paginationWithLimit:100] markRead:alwaysMarkRead completion:unreadCompletionBlock];
}
}];
}