forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppController.m
executable file
·1040 lines (844 loc) · 38.8 KB
/
AppController.m
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
//
// AppController.m
// SelfControl
//
// Created by Charlie Stigler on 1/29/09.
// Copyright 2009 Eyebeam.
// This file is part of SelfControl.
//
// SelfControl 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.
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#import "AppController.h"
#import "MASPreferencesWindowController.h"
#import "PreferencesGeneralViewController.h"
#import "PreferencesAdvancedViewController.h"
#import "SCTimeIntervalFormatter.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import <LetsMove/PFMoveApplication.h>
NSString* const kSelfControlErrorDomain = @"SelfControlErrorDomain";
@implementation AppController {
NSWindowController* getStartedWindowController;
}
@synthesize addingBlock;
- (AppController*) init {
if(self = [super init]) {
defaults_ = [NSUserDefaults standardUserDefaults];
NSDictionary* appDefaults = @{@"BlockDuration": @15,
@"BlockStartedDate": [NSDate distantFuture],
@"HostBlacklist": @[],
@"EvaluateCommonSubdomains": @YES,
@"IncludeLinkedDomains": @YES,
@"HighlightInvalidHosts": @YES,
@"VerifyInternetConnection": @YES,
@"TimerWindowFloats": @NO,
@"BlockSoundShouldPlay": @NO,
@"BlockSound": @5,
@"ClearCaches": @YES,
@"BlockAsWhitelist": @NO,
@"BadgeApplicationIcon": @YES,
@"AllowLocalNetworks": @YES,
@"MaxBlockLength": @1440,
@"BlockLengthInterval": @15,
@"WhitelistAlertSuppress": @NO,
@"GetStartedShown": @NO};
[defaults_ registerDefaults:appDefaults];
self.addingBlock = false;
// refreshUILock_ is a lock that prevents a race condition by making the refreshUserInterface
// method alter the blockIsOn variable atomically (will no longer be necessary once we can
// use properties).
refreshUILock_ = [[NSLock alloc] init];
}
return self;
}
- (NSString*)selfControlHelperToolPath {
static NSString* path;
// Cache the path so it doesn't have to be searched for again.
if(!path) {
NSBundle* thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForAuxiliaryExecutable: @"org.eyebeam.SelfControl"];
}
return path;
}
- (char*)selfControlHelperToolPathUTF8String {
static char* path;
// Cache the converted path so it doesn't have to be converted again
if(!path) {
path = malloc(512);
[[self selfControlHelperToolPath] getCString: path
maxLength: 512
encoding: NSUTF8StringEncoding];
}
return path;
}
- (IBAction)updateTimeSliderDisplay:(id)sender {
NSInteger numMinutes = floor([blockDurationSlider_ integerValue]);
// Time-display code cleaned up thanks to the contributions of many users
NSString* timeString = [self timeSliderDisplayStringFromNumberOfMinutes:numMinutes];
[blockSliderTimeDisplayLabel_ setStringValue:timeString];
[submitButton_ setEnabled: (numMinutes > 0) && ([[defaults_ arrayForKey:@"HostBlacklist"] count] > 0)];
}
- (NSString *)timeSliderDisplayStringFromNumberOfMinutes:(NSInteger)numberOfMinutes {
static NSCalendar* gregorian = nil;
if (gregorian == nil) {
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
}
NSRange secondsRangePerMinute = [gregorian
rangeOfUnit:NSSecondCalendarUnit
inUnit:NSMinuteCalendarUnit
forDate:[NSDate date]];
NSUInteger numberOfSecondsPerMinute = NSMaxRange(secondsRangePerMinute);
NSTimeInterval numberOfSecondsSelected = (NSTimeInterval)(numberOfSecondsPerMinute * numberOfMinutes);
NSString* displayString = [self timeSliderDisplayStringFromTimeInterval:numberOfSecondsSelected];
return displayString;
}
- (NSString *)timeSliderDisplayStringFromTimeInterval:(NSTimeInterval)numberOfSeconds {
static SCTimeIntervalFormatter* formatter = nil;
if (formatter == nil) {
formatter = [[SCTimeIntervalFormatter alloc] init];
}
NSString* formatted = [formatter stringForObjectValue:@(numberOfSeconds)];
return formatted;
}
- (IBAction)addBlock:(id)sender {
[defaults_ synchronize];
if(([[defaults_ objectForKey:@"BlockStartedDate"] timeIntervalSinceNow] < 0)) {
// This method shouldn't be getting called, a block is on (block started date
// is in the past, not distantFuture) so the Start button should be disabled.
NSError* err = [NSError errorWithDomain:kSelfControlErrorDomain
code: -102
userInfo: @{NSLocalizedDescriptionKey: @"We can't start a block, because one is currently ongoing."}];
[NSApp presentError: err];
return;
}
if([[defaults_ arrayForKey:@"HostBlacklist"] count] == 0) {
// Since the Start button should be disabled when the blacklist has no entries,
// this should definitely not be happening. Exit.
NSError* err = [NSError errorWithDomain:kSelfControlErrorDomain
code: -102
userInfo: @{NSLocalizedDescriptionKey: @"Error -102: Attempting to add block, but no blocklist is set."}];
[NSApp presentError: err];
return;
}
if([defaults_ boolForKey: @"VerifyInternetConnection"] && ![self networkConnectionIsAvailable]) {
NSAlert* networkUnavailableAlert = [[NSAlert alloc] init];
[networkUnavailableAlert setMessageText: NSLocalizedString(@"No network connection detected", "No network connection detected message")];
[networkUnavailableAlert setInformativeText:NSLocalizedString(@"A block cannot be started without a working network connection. You can override this setting in Preferences.", @"Message when network connection is unavailable")];
[networkUnavailableAlert addButtonWithTitle: NSLocalizedString(@"Cancel", "Cancel button")];
[networkUnavailableAlert addButtonWithTitle: NSLocalizedString(@"Network Diagnostics...", @"Network Diagnostics button")];
if([networkUnavailableAlert runModal] == NSAlertFirstButtonReturn)
return;
// If the user selected Network Diagnostics launch an assisant to help them.
// apple.com is an arbitrary host chosen to pass to Network Diagnostics.
CFURLRef url = CFURLCreateWithString(NULL, CFSTR("http://apple.com"), NULL);
CFNetDiagnosticRef diagRef = CFNetDiagnosticCreateWithURL(kCFAllocatorDefault, url);
CFNetDiagnosticDiagnoseProblemInteractively(diagRef);
return;
}
[timerWindowController_ resetStrikes];
[NSThread detachNewThreadSelector: @selector(installBlock) toTarget: self withObject: nil];
}
- (void)refreshUserInterface {
// UI updates are for the main thread only!
if (![NSThread isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[self refreshUserInterface];
});
return;
}
if(![refreshUILock_ tryLock]) {
// already refreshing the UI, no need to wait and do it again
return;
}
BOOL blockWasOn = blockIsOn;
blockIsOn = [self selfControlLaunchDaemonIsLoaded];
if(blockIsOn) { // block is on
if(!blockWasOn) { // if we just switched states to on...
[self closeTimerWindow];
[self showTimerWindow];
[initialWindow_ close];
[self closeDomainList];
}
} else { // block is off
if(blockWasOn) { // if we just switched states to off...
[timerWindowController_ blockEnded];
// Makes sure the domain list will refresh when it comes back
[self closeDomainList];
NSWindow* mainWindow = [NSApp mainWindow];
// We don't necessarily want the initial window to be key and front,
// but no other message seems to show it properly.
[initialWindow_ makeKeyAndOrderFront: self];
// So we work around it and make key and front whatever was the main window
[mainWindow makeKeyAndOrderFront: self];
[self closeTimerWindow];
}
[defaults_ synchronize];
[self updateTimeSliderDisplay: blockDurationSlider_];
BOOL addBlockIsOngoing = self.addingBlock;
if([blockDurationSlider_ intValue] != 0 && [[defaults_ objectForKey: @"HostBlacklist"] count] != 0 && !addBlockIsOngoing) {
[submitButton_ setEnabled: YES];
} else {
[submitButton_ setEnabled: NO];
}
// If we're adding a block, we want buttons disabled.
if(!addBlockIsOngoing) {
[blockDurationSlider_ setEnabled: YES];
[editBlacklistButton_ setEnabled: YES];
[submitButton_ setTitle: NSLocalizedString(@"Start", @"Start button")];
} else {
[blockDurationSlider_ setEnabled: NO];
[editBlacklistButton_ setEnabled: NO];
[submitButton_ setTitle: NSLocalizedString(@"Loading", @"Loading button")];
}
// if block's off, and we haven't shown it yet, show the first-time modal
if (![defaults_ boolForKey: @"GetStartedShown"]) {
[defaults_ setBool: YES forKey: @"GetStartedShown"];
[defaults_ synchronize];
[self showGetStartedWindow: self];
}
}
[refreshUILock_ unlock];
}
- (void)showTimerWindow {
if(timerWindowController_ == nil) {
[NSBundle loadNibNamed: @"TimerWindow" owner: self];
} else {
[[timerWindowController_ window] makeKeyAndOrderFront: self];
[[timerWindowController_ window] center];
}
}
- (void)closeTimerWindow {
[timerWindowController_ close];
timerWindowController_ = nil;
}
- (IBAction)openPreferences:(id)sender {
if (preferencesWindowController_ == nil) {
NSViewController* generalViewController = [[PreferencesGeneralViewController alloc] init];
NSViewController* advancedViewController = [[PreferencesAdvancedViewController alloc] init];
NSString* title = NSLocalizedString(@"Preferences", @"Common title for Preferences window");
preferencesWindowController_ = [[MASPreferencesWindowController alloc] initWithViewControllers: @[generalViewController, advancedViewController] title: title];
}
[preferencesWindowController_ showWindow: nil];
}
- (IBAction)showGetStartedWindow:(id)sender {
if (!getStartedWindowController) {
getStartedWindowController = [[NSWindowController alloc] initWithWindowNibName: @"FirstTime"];
}
[getStartedWindowController.window center];
[getStartedWindowController.window makeKeyAndOrderFront: nil];
[getStartedWindowController showWindow: nil];
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
PFMoveToApplicationsFolderIfNecessary();
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSApplication sharedApplication].delegate = self;
// Register observers on both distributed and normal notification centers
// to receive notifications from the helper tool and the other parts of the
// main SelfControl app. Note that they are divided thusly because distributed
// notifications are very expensive and should be minimized.
[[NSDistributedNotificationCenter defaultCenter] addObserver: self
selector: @selector(refreshUserInterface)
name: @"SCConfigurationChangedNotification"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(refreshUserInterface)
name: @"SCConfigurationChangedNotification"
object: nil];
[initialWindow_ center];
// We'll set blockIsOn to whatever is NOT right, so that in refreshUserInterface
// it'll fix it and properly refresh the user interface.
blockIsOn = ![self selfControlLaunchDaemonIsLoaded];
// Change block duration slider for hidden user defaults settings
long numTickMarks = ([defaults_ integerForKey: @"MaxBlockLength"] / [defaults_ integerForKey: @"BlockLengthInterval"]) + 1;
[blockDurationSlider_ setMaxValue: [defaults_ integerForKey: @"MaxBlockLength"]];
[blockDurationSlider_ setNumberOfTickMarks: numTickMarks];
[blockDurationSlider_ bind: @"value"
toObject: [NSUserDefaultsController sharedUserDefaultsController]
withKeyPath: @"values.BlockDuration"
options: @{
NSContinuouslyUpdatesValueBindingOption: @YES
}];
[self refreshUserInterface];
NSOperatingSystemVersion minRequiredVersion = (NSOperatingSystemVersion){10,8,0}; // Mountain Lion
NSString* minRequiredVersionString = @"10.8 (Mountain Lion)";
if (![[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: minRequiredVersion]) {
NSLog(@"ERROR: Unsupported version for SelfControl");
NSAlert* unsupportedVersionAlert = [[NSAlert alloc] init];
[unsupportedVersionAlert setMessageText: NSLocalizedString(@"Unsupported version", nil)];
[unsupportedVersionAlert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"This version of SelfControl only supports Mac OS X version %@ or higher. To download a version for older operating systems, please go to www.selfcontrolapp.com", nil), minRequiredVersionString]];
[unsupportedVersionAlert addButtonWithTitle: NSLocalizedString(@"OK", nil)];
[unsupportedVersionAlert runModal];
}
}
- (BOOL)selfControlLaunchDaemonIsLoaded {
// First we check the host file, and see if a block is in there
NSString* hostFileContents = [NSString stringWithContentsOfFile: @"/etc/hosts" encoding: NSUTF8StringEncoding error: NULL];
if(hostFileContents != nil && [hostFileContents rangeOfString: @"# BEGIN SELFCONTROL BLOCK"].location != NSNotFound) {
return YES;
}
[defaults_ synchronize];
NSDate* blockStartedDate = [defaults_ objectForKey: @"BlockStartedDate"];
if(blockStartedDate != nil && ![blockStartedDate isEqualToDate: [NSDate distantFuture]]) {
return YES;
}
// If there's no block in the hosts file, no defaults BlockStartedDate, and no lock-file,
// we'll assume we're clear of blocks. Checking pf would be nice but usually requires
// root permissions, so it would be difficult to do here.
return [[NSFileManager defaultManager] fileExistsAtPath: SelfControlLockFilePath];
}
- (IBAction)showDomainList:(id)sender {
BOOL addBlockIsOngoing = self.addingBlock;
if([self selfControlLaunchDaemonIsLoaded] || addBlockIsOngoing) {
NSAlert* blockInProgressAlert = [[NSAlert alloc] init];
[blockInProgressAlert setMessageText: NSLocalizedString(@"Block in progress", @"Block in progress error title")];
[blockInProgressAlert setInformativeText:NSLocalizedString(@"The blacklist cannot be edited while a block is in progress.", @"Block in progress explanation")];
[blockInProgressAlert addButtonWithTitle: NSLocalizedString(@"OK", @"OK button")];
[blockInProgressAlert runModal];
return;
}
if(domainListWindowController_ == nil) {
[NSBundle loadNibNamed: @"DomainList" owner: self];
}
[domainListWindowController_ showWindow: self];
}
- (void)closeDomainList {
[domainListWindowController_ close];
domainListWindowController_ = nil;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) theApplication {
// Hack to make the application terminate after the last window is closed, but
// INCLUDE the HUD-style timer window.
if([[timerWindowController_ window] isVisible])
return NO;
if (PFMoveIsInProgress())
return NO;
return YES;
}
- (BOOL)networkConnectionIsAvailable {
SCNetworkReachabilityFlags flags;
// This method goes haywire if Google ever goes down...
SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithName (kCFAllocatorDefault, "google.com");
BOOL reachable = SCNetworkReachabilityGetFlags (target, &flags);
return reachable && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
}
- (void)addToBlockList:(NSString*)host lock:(NSLock*)lock {
if(host == nil)
return;
host = [[host stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
// Remove "http://" if a user tried to put that in
NSArray* splitString = [host componentsSeparatedByString: @"http://"];
for(int i = 0; i < [splitString count]; i++) {
if(![splitString[i] isEqual: @""]) {
host = splitString[i];
break;
}
}
// Delete anything after a "/" in case a user tried to copy-paste a web address.
host = [host componentsSeparatedByString: @"/"][0];
if([host isEqualToString: @""])
return;
NSMutableArray* list = [[defaults_ arrayForKey: @"HostBlacklist"] mutableCopy];
[list addObject: host];
[defaults_ setObject: list forKey: @"HostBlacklist"];
[defaults_ synchronize];
if(([[defaults_ objectForKey:@"BlockStartedDate"] isEqualToDate: [NSDate distantFuture]])) {
// This method shouldn't be getting called, a block is not on (block started
// is in the distantFuture) so the Start button should be disabled.
// Maybe the UI didn't get properly refreshed, so try refreshing it again
// before we return.
[self refreshUserInterface];
// Reverse the blacklist change made before we fail
NSMutableArray* list = [[defaults_ arrayForKey: @"HostBlacklist"] mutableCopy];
[list removeLastObject];
[defaults_ setObject: list forKey: @"HostBlacklist"];
NSError* err = [NSError errorWithDomain:kSelfControlErrorDomain
code: -103
userInfo: @{NSLocalizedDescriptionKey: @"Error -103: Attempting to add host to block, but no block appears to be in progress."}];
[NSApp presentError: err];
return;
}
if([defaults_ boolForKey: @"VerifyInternetConnection"] && ![self networkConnectionIsAvailable]) {
NSAlert* networkUnavailableAlert = [[NSAlert alloc] init];
[networkUnavailableAlert setMessageText: NSLocalizedString(@"No network connection detected", "No network connection detected message")];
[networkUnavailableAlert setInformativeText:NSLocalizedString(@"A block cannot be started without a working network connection. You can override this setting in Preferences.", @"Message when network connection is unavailable")];
[networkUnavailableAlert addButtonWithTitle: NSLocalizedString(@"Cancel", "Cancel button")];
[networkUnavailableAlert addButtonWithTitle: NSLocalizedString(@"Network Diagnostics...", @"Network Diagnostics button")];
if([networkUnavailableAlert runModal] == NSAlertFirstButtonReturn) {
// User clicked cancel
// Reverse the blacklist change made before we fail
NSMutableArray* list = [[defaults_ arrayForKey: @"HostBlacklist"] mutableCopy];
[list removeLastObject];
[defaults_ setObject: list forKey: @"HostBlacklist"];
return;
}
// If the user selected Network Diagnostics, launch an assisant to help them.
// apple.com is an arbitrary host chosen to pass to Network Diagnostics.
CFURLRef url = CFURLCreateWithString(NULL, CFSTR("http://apple.com"), NULL);
CFNetDiagnosticRef diagRef = CFNetDiagnosticCreateWithURL(kCFAllocatorDefault, url);
CFNetDiagnosticDiagnoseProblemInteractively(diagRef);
// Reverse the blacklist change made before we fail
NSMutableArray* list = [[defaults_ arrayForKey: @"HostBlacklist"] mutableCopy];
[list removeLastObject];
[defaults_ setObject: list forKey: @"HostBlacklist"];
return;
}
[NSThread detachNewThreadSelector: @selector(refreshBlock:) toTarget: self withObject: lock];
}
- (void)extendBlockTime:(NSInteger)minutesToAdd lock:(NSLock*)lock {
// sanity check: extending a block for 0 minutes is useless; 24 hour should be impossible
NSInteger MINUTES_IN_DAY = 24 * 60 * 60;
if(minutesToAdd < 1 || minutesToAdd > MINUTES_IN_DAY)
return;
// ensure block health before we try to change it
if(([[defaults_ objectForKey:@"BlockStartedDate"] isEqualToDate: [NSDate distantFuture]])) {
// This method shouldn't be getting called, a block is not on (block started
// is in the distantFuture) so the Start button should be disabled.
// Maybe the UI didn't get properly refreshed, so try refreshing it again
// before we return.
[self refreshUserInterface];
NSError* err = [NSError errorWithDomain:kSelfControlErrorDomain
code: -103
userInfo: @{NSLocalizedDescriptionKey: @"Error -103: Attempting to add host to block, but no block appears to be in progress."}];
[NSApp presentError: err];
return;
}
NSInteger currentBlockDuration = [defaults_ integerForKey: @"BlockDuration"];
NSInteger newBlockDuration = MIN(currentBlockDuration + minutesToAdd, 0); // make sure we don't do something freaky if BlockDuration is negative for some reason
[NSThread detachNewThreadSelector: @selector(setBlockDuration:)
toTarget: self
withObject: @{
@"lock": lock,
@"duration": @(newBlockDuration)
}];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver: self
name: @"SCConfigurationChangedNotification"
object: nil];
[[NSDistributedNotificationCenter defaultCenter] removeObserver: self
name: @"SCConfigurationChangedNotification"
object: nil];
}
- (id)initialWindow {
return initialWindow_;
}
- (id)domainListWindowController {
return domainListWindowController_;
}
- (void)setDomainListWindowController:(id)newController {
domainListWindowController_ = newController;
}
- (NSError*)errorFromHelperToolStatusCode:(int)status {
NSString* domain = kSelfControlErrorDomain;
NSMutableString* description = [NSMutableString stringWithFormat: @"Error %d: ", status];
switch(status) {
case -201:
[description appendString: @"Helper tool not launched as root."];
break;
case -202:
[description appendString: @"Helper tool launched with insufficient arguments."];
break;
case -203:
[description appendString: @"Host blocklist not set"];
break;
case -204:
[description appendString: @"Could not write launchd plist file to LaunchDaemons folder."];
break;
case -205:
[description appendString: @"Could not create PrivilegedHelperTools directory."];
break;
case -206:
[description appendString: @"Could not change permissions on PrivilegedHelperTools directory."];
break;
case -207:
[description appendString: @"Could not delete old helper binary."];
break;
case -208:
[description appendString: @"Could not copy SelfControl's helper binary to PrivilegedHelperTools directory."];
break;
case -209:
[description appendString: @"Could not change permissions on SelfControl's helper binary."];
break;
case -210:
[description appendString: @"Insufficient block information found."];
break;
case -211:
[description appendString: @"Launch daemon load returned a failure status code."];
break;
case -212:
[description appendString: @"Remove option called."];
break;
case -213:
[description appendString: @"Refreshing domain blacklist, but no block is currently ongoing."];
break;
case -214:
[description appendString: @"Insufficient block information found."];
break;
case -215:
[description appendString: @"Checkup ran but no block found."];
break;
case -216:
[description appendString: @"Could not write lock file."];
break;
case -217:
[description appendString: @"Could not write lock file."];
break;
case -218:
[description appendString: @"Could not remove SelfControl lock file."];
break;
case -219:
[description appendString: @"SelfControl lock file already exists. Please try your block again."];
break;
default:
[description appendString: [NSString stringWithFormat: @"Helper tool failed with unknown error code: %d", status]];
}
return [NSError errorWithDomain: domain code: status userInfo: @{NSLocalizedDescriptionKey: description}];
}
- (void)installBlock {
@autoreleasepool {
self.addingBlock = true;
[self refreshUserInterface];
AuthorizationRef authorizationRef;
char* helperToolPath = [self selfControlHelperToolPathUTF8String];
NSUInteger helperToolPathSize = strlen(helperToolPath);
AuthorizationItem right = {
kAuthorizationRightExecute,
helperToolPathSize,
helperToolPath,
0
};
AuthorizationRights authRights = {
1,
&right
};
AuthorizationFlags myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed;
OSStatus status;
status = AuthorizationCreate (&authRights,
kAuthorizationEmptyEnvironment,
myFlags,
&authorizationRef);
if(status) {
NSLog(@"ERROR: Failed to authorize block start.");
self.addingBlock = false;
[self refreshUserInterface];
return;
}
[defaults_ setObject: [NSDate date] forKey: @"BlockStartedDate"];
[defaults_ synchronize];
// We need to pass our UID to the helper tool. It needs to know whose defaults
// it should reading in order to properly load the blacklist.
char uidString[32];
snprintf(uidString, sizeof(uidString), "%d", getuid());
FILE* commPipe;
char* args[] = { uidString, "--install", NULL };
status = AuthorizationExecuteWithPrivileges(authorizationRef,
helperToolPath,
kAuthorizationFlagDefaults,
args,
&commPipe);
if(status) {
NSLog(@"WARNING: Authorized execution of helper tool returned failure status code %d", (int)status);
// reset BlockStartedDate on failure
[defaults_ removeObjectForKey: @"BlockStartedDate"];
NSError* err = [NSError errorWithDomain: kSelfControlErrorDomain
code: status
userInfo: @{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Error %d received from the Security Server.", (int)status]}];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
self.addingBlock = false;
[self refreshUserInterface];
return;
}
NSFileHandle* helperToolHandle = [[NSFileHandle alloc] initWithFileDescriptor: fileno(commPipe) closeOnDealloc: YES];
NSData* inData = [helperToolHandle readDataToEndOfFile];
NSString* inDataString = [[NSString alloc] initWithData: inData encoding: NSUTF8StringEncoding];
if([inDataString isEqualToString: @""]) {
// reset BlockStartedDate on failure
[defaults_ removeObjectForKey: @"BlockStartedDate"];
NSError* err = [NSError errorWithDomain: kSelfControlErrorDomain
code: -104
userInfo: @{NSLocalizedDescriptionKey: @"Error -104: The helper tool crashed. This may cause unexpected errors."}];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
}
int exitCode = [inDataString intValue];
if(exitCode) {
// reset BlockStartedDate on failure
[defaults_ removeObjectForKey: @"BlockStartedDate"];
NSError* err = [self errorFromHelperToolStatusCode: exitCode];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
}
self.addingBlock = false;
[self refreshUserInterface];
}
}
- (void)refreshBlock:(NSLock*)lockToUse {
if(![lockToUse tryLock]) {
return;
}
@autoreleasepool {
AuthorizationRef authorizationRef;
char* helperToolPath = [self selfControlHelperToolPathUTF8String];
long helperToolPathSize = strlen(helperToolPath);
AuthorizationItem right = {
kAuthorizationRightExecute,
helperToolPathSize,
helperToolPath,
0
};
AuthorizationRights authRights = {
1,
&right
};
AuthorizationFlags myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed;
OSStatus status;
status = AuthorizationCreate (&authRights,
kAuthorizationEmptyEnvironment,
myFlags,
&authorizationRef);
if(status) {
NSLog(@"ERROR: Failed to authorize block refresh.");
// Reverse the blacklist change made before we fail
NSMutableArray* list = [[defaults_ arrayForKey: @"HostBlacklist"] mutableCopy];
[list removeLastObject];
[defaults_ setObject: list forKey: @"HostBlacklist"];
[lockToUse unlock];
return;
}
// We need to pass our UID to the helper tool. It needs to know whose defaults
// it should read in order to properly load the blacklist.
char uidString[32];
snprintf(uidString, sizeof(uidString), "%d", getuid());
FILE* commPipe;
char* args[] = { uidString, "--refresh", NULL };
status = AuthorizationExecuteWithPrivileges(authorizationRef,
helperToolPath,
kAuthorizationFlagDefaults,
args,
&commPipe);
if(status) {
NSLog(@"WARNING: Authorized execution of helper tool returned failure status code %d", (int)status);
NSError* err = [self errorFromHelperToolStatusCode: status];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
[lockToUse unlock];
return;
}
NSFileHandle* helperToolHandle = [[NSFileHandle alloc] initWithFileDescriptor: fileno(commPipe) closeOnDealloc: YES];
NSData* inData = [helperToolHandle readDataToEndOfFile];
NSString* inDataString = [[NSString alloc] initWithData: inData encoding: NSUTF8StringEncoding];
if([inDataString isEqualToString: @""]) {
NSError* err = [NSError errorWithDomain: kSelfControlErrorDomain
code: -105
userInfo: @{NSLocalizedDescriptionKey: @"Error -105: The helper tool crashed. This may cause unexpected errors."}];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
}
int exitCode = [inDataString intValue];
if(exitCode) {
NSError* err = [self errorFromHelperToolStatusCode: exitCode];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
}
[timerWindowController_ closeAddSheet: self];
}
[lockToUse unlock];
}
- (void)setBlockDuration:(NSDictionary*)options {
NSLock* lock = options[@"lock"];
NSInteger newDuration = [options[@"duration"] integerValue];
if(![lock tryLock]) {
return;
}
NSInteger oldDuration = [defaults_ integerForKey: @"BlockDuration"];
[defaults_ setInteger: newDuration forKey: @"BlockDuration"];
[defaults_ synchronize];
@autoreleasepool {
AuthorizationRef authorizationRef;
char* helperToolPath = [self selfControlHelperToolPathUTF8String];
long helperToolPathSize = strlen(helperToolPath);
AuthorizationItem right = {
kAuthorizationRightExecute,
helperToolPathSize,
helperToolPath,
0
};
AuthorizationRights authRights = {
1,
&right
};
AuthorizationFlags myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed;
OSStatus status;
status = AuthorizationCreate (&authRights,
kAuthorizationEmptyEnvironment,
myFlags,
&authorizationRef);
if(status) {
NSLog(@"ERROR: Failed to authorize setting new block duration.");
// Reverse the block duration change made before we fail
[defaults_ setInteger: oldDuration forKey: @"BlockDuration"];
[lock unlock];
return;
}
// We need to pass our UID to the helper tool. It needs to know whose defaults
// it should read in order to properly load the blacklist.
char uidString[32];
snprintf(uidString, sizeof(uidString), "%d", getuid());
FILE* commPipe;
char* args[] = { uidString, "--rewrite-lock-file", NULL };
status = AuthorizationExecuteWithPrivileges(authorizationRef,
helperToolPath,
kAuthorizationFlagDefaults,
args,
&commPipe);
if(status) {
NSLog(@"WARNING: Authorized execution of helper tool returned failure status code %d", (int)status);
NSError* err = [self errorFromHelperToolStatusCode: status];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
[lock unlock];
return;
}
// Before we try to fix the block in the helper tool, make sure the block didn't finish in the meantime
// (note that the AuthorizationExecuteWithPrivileges blocks on user input, so we can't check the time left earlier in this function)
NSDate* oldBlockEndDate = [[defaults_ objectForKey:@"BlockStartedDate"] dateByAddingTimeInterval: (oldDuration * 60)];
// Block is finished if BlockStartedDate is set back to the distant future, OR if it's only a second left until we'll do that (allow some buffer for the helper tool)
if ([[defaults_ objectForKey:@"BlockStartedDate"] isEqualToDate: [NSDate distantFuture]] || [oldBlockEndDate timeIntervalSinceNow] < 1) {
// we're done, or will be by the time we get to it! so just let it expire. they can restart it.
return;
}
NSFileHandle* helperToolHandle = [[NSFileHandle alloc] initWithFileDescriptor: fileno(commPipe) closeOnDealloc: YES];
NSData* inData = [helperToolHandle readDataToEndOfFile];
NSString* inDataString = [[NSString alloc] initWithData: inData encoding: NSUTF8StringEncoding];
if([inDataString isEqualToString: @""]) {
NSError* err = [NSError errorWithDomain: kSelfControlErrorDomain
code: -105
userInfo: @{NSLocalizedDescriptionKey: @"Error -105: The helper tool crashed. This may cause unexpected errors."}];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
}
int exitCode = [inDataString intValue];
if(exitCode) {
NSError* err = [self errorFromHelperToolStatusCode: exitCode];
[NSApp performSelectorOnMainThread: @selector(presentError:)
withObject: err
waitUntilDone: YES];
}
[timerWindowController_ performSelectorOnMainThread:@selector(blockDurationUpdated)
withObject: nil
waitUntilDone: YES];
}
[lock unlock];
}
- (IBAction)save:(id)sender {
NSSavePanel *sp;
long runResult;
/* create or get the shared instance of NSSavePanel */
sp = [NSSavePanel savePanel];
sp.allowedFileTypes = @[@"selfcontrol"];
/* display the NSSavePanel */
runResult = [sp runModal];
/* if successful, save file under designated name */
if (runResult == NSOKButton) {
[defaults_ synchronize];
NSString* err;
NSDictionary* saveDict = @{@"HostBlacklist": [defaults_ objectForKey: @"HostBlacklist"],
@"BlockAsWhitelist": [defaults_ objectForKey: @"BlockAsWhitelist"]};
NSData* saveData = [NSPropertyListSerialization dataFromPropertyList: saveDict format: NSPropertyListBinaryFormat_v1_0 errorDescription: &err];
if(err) {
NSError* displayErr = [NSError errorWithDomain: kSelfControlErrorDomain code: -902 userInfo: @{NSLocalizedDescriptionKey: [@"Error 902: " stringByAppendingString: err]}];
[NSApp presentError: displayErr];
return;
}
if (![saveData writeToURL: sp.URL atomically: YES]) {
NSBeep();
} else {
NSDictionary* attribs = @{NSFileExtensionHidden: @YES};
[[NSFileManager defaultManager] setAttributes: attribs ofItemAtPath: [sp.URL path] error: NULL];
}
}
}
- (IBAction)open:(id)sender {
NSOpenPanel* oPanel = [NSOpenPanel openPanel];
oPanel.allowedFileTypes = @[@"selfcontrol"];
oPanel.allowsMultipleSelection = NO;
long result = [oPanel runModal];
if (result == NSOKButton) {
if([oPanel.URLs count] > 0) {
NSDictionary* openedDict = [NSDictionary dictionaryWithContentsOfURL: oPanel.URLs[0]];
[defaults_ setObject: openedDict[@"HostBlacklist"] forKey: @"HostBlacklist"];
[defaults_ setObject: openedDict[@"BlockAsWhitelist"] forKey: @"BlockAsWhitelist"];
BOOL domainListIsOpen = [[domainListWindowController_ window] isVisible];
NSRect frame = [[domainListWindowController_ window] frame];
[self closeDomainList];
if(domainListIsOpen) {
[self showDomainList: self];
[[domainListWindowController_ window] setFrame: frame display: YES];
}
}
}
}
- (BOOL)application:(NSApplication*)theApplication openFile:(NSString*)filename {
NSDictionary* openedDict = [NSDictionary dictionaryWithContentsOfFile: filename];
if(openedDict == nil) return NO;
NSArray* newBlocklist = openedDict[@"HostBlacklist"];
NSNumber* newWhitelistChoice = openedDict[@"BlockAsWhitelist"];
if(newBlocklist == nil || newWhitelistChoice == nil) return NO;
[defaults_ setObject: newBlocklist forKey: @"HostBlacklist"];