@@ -58,18 +58,52 @@ int main(int argc, char* argv[]) {
5858 SCSettings* settings = [SCSettings settingsForUser: controllingUID];
5959
6060 if ([modeString isEqual: @" --install" ]) {
61+ if (blockIsRunningInSettingsOrDefaults (controllingUID)) {
62+ NSLog (@" ERROR: Block is already running" );
63+ printStatus (-222 );
64+ exit (EX_CONFIG);
65+ }
66+
6167 NSFileManager * fileManager = [NSFileManager defaultManager ];
6268
6369 // Initialize writeErr to nil so calling messages on it later don't cause
6470 // crashes (it doesn't make sense we need to do this, but whatever).
6571 NSError * writeErr = nil ;
6672 NSString * plistFormatPath = [[NSBundle mainBundle ] pathForResource: @" org.eyebeam.SelfControl"
6773 ofType: @" plist" ];
68-
6974 NSString * plistFormatString = [NSString stringWithContentsOfFile: plistFormatPath encoding: NSUTF8StringEncoding error: NULL ];
75+
76+ // while the main app will just expect us to fetch the blocklist / end date from settings
77+ // this helper tool can also be used via command line with no other settings
78+ // (ex: by auto-selfcontrol) and in that case they'll pass a blocklist file and the end date via args
79+ // we should read those values into settings for use later
80+ NSString * pathToBlocklistFile;
81+ NSDate * blockEndDateArg;
82+ if (argv[3 ] != NULL && argv[4 ] != NULL ) {
83+ pathToBlocklistFile = @(argv[3 ]);
84+ blockEndDateArg = [[NSISO8601DateFormatter new ] dateFromString: @(argv[4 ])];
85+
86+ // if we didn't get a valid block end date in the future, ignore the other args
87+ if (blockEndDateArg == nil || [blockEndDateArg timeIntervalSinceNow ] < 1 ) {
88+ pathToBlocklistFile = nil ;
89+ NSLog (@" Error: Block end date argument %@ is invalid" , @(argv[4 ]));
90+ printStatus (-220 );
91+ exit (EX_IOERR);
92+ } else {
93+ [settings setValue: blockEndDateArg forKey: @" BlockEndDate" ];
94+ BOOL readSuccess = [SCUtilities readBlocklistFromFile: [NSURL fileURLWithPath: pathToBlocklistFile] toSettings: settings];
95+
96+ if (!readSuccess) {
97+ NSLog (@" ERROR: Block could not be read from file %@ " , pathToBlocklistFile);
98+ printStatus (-221 );
99+ exit (EX_IOERR);
100+ }
101+ }
102+ }
70103
71104 // get the expiration minute, to make sure we run the helper then (if it hasn't run already)
72- NSDate * blockEndDate = [settings valueForKey: @" BlockEndDate" ];
105+ // use the block end date from the argument if it's available; otherwise fall back to the one in settings
106+ NSDate * blockEndDate = (blockEndDateArg != nil ) ? blockEndDateArg : [settings valueForKey: @" BlockEndDate" ];
73107 NSCalendar * calendar = [[NSCalendar alloc ] initWithCalendarIdentifier: NSGregorianCalendar ];
74108 NSDateComponents * components = [calendar components: NSMinuteCalendarUnit fromDate: blockEndDate];
75109 long expirationMinute = [components minute ];
0 commit comments