diff --git a/Tools/SOGoToolExpireAutoReply.m b/Tools/SOGoToolExpireAutoReply.m index b44b1863b..ac9e8035e 100644 --- a/Tools/SOGoToolExpireAutoReply.m +++ b/Tools/SOGoToolExpireAutoReply.m @@ -22,9 +22,11 @@ #import #import +#import #import #import #import +#import #import #import @@ -63,10 +65,12 @@ - (void) usage { - fprintf (stderr, "expire-autoreply authname:authpassword\n\n" - " authname administrator username of the Sieve server\n" - " authpassword administrator password of the Sieve server\n\n" - "The expire-autoreply action should be configured as a daily cronjob.\n"); + fprintf (stderr, "expire-autoreply -p credentialFile\n\n" + " -p credentialFile Specify the file containing the sieve admin credentials\n" + " The file should contain a single line:\n" + " username:password\n" + "\n" + "The expire-autoreply action should be configured as a daily cronjob.\n"); } - (BOOL) removeAutoReplyForLogin: (NSString *) theLogin @@ -123,12 +127,20 @@ now = [[NSCalendarDate calendarDate] timeIntervalSince1970]; sd = [SOGoSystemDefaults sharedSystemDefaults]; profileURL = [sd profileURL]; - if (profileURL) + if (!profileURL) + { + NSLog(@"No profileURL"); + } + else { tableURL = [[NSURL alloc] initWithString: profileURL]; cm = [GCSChannelManager defaultChannelManager]; channel = [cm acquireOpenChannelForURL: tableURL]; - if (channel) + if (!channel) + { + NSLog(@"Couldn't acquire channel for profileURL"); + } + else { sql = [NSString stringWithFormat: @"SELECT c_uid, c_defaults FROM %@", [tableURL gcsTableName]]; @@ -166,30 +178,60 @@ } } - - (BOOL) run { + NSError *err; NSRange r; - NSString *creds, *authname, *authpwd; + NSString *creds, *credsFile, *authname, *authpwd; BOOL rc; int max; - max = [arguments count]; + max = [sanitizedArguments count]; + creds = nil; + authname = nil; + authpwd = nil; rc = NO; + credsFile = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; + if (credsFile) + { + creds = [NSString stringWithContentsOfFile: credsFile + encoding: NSUTF8StringEncoding + error: &err]; + if (creds == nil) + { + NSLog(@"Error reading credential file '%@': %@", credsFile, err); + } + creds = [creds stringByTrimmingCharactersInSet: + [NSCharacterSet newlineCharacterSet]]; + } + if (max > 0) { - creds = [arguments objectAtIndex: 0]; - r = [creds rangeOfString: @":"]; - if (r.location != NSNotFound) - { - authname = [creds substringToIndex: r.location]; - authpwd = [creds substringFromIndex: r.location+1]; - [self expireAutoReplyWithUsername: authname andPassword: authpwd]; - rc = YES; - } + /* assume we got the creds directly on the cli */ + creds = [sanitizedArguments objectAtIndex: 0]; } + if (creds != nil) + { + r = [creds rangeOfString: @":"]; + if (r.location == NSNotFound) + { + NSLog(@"Invalid credential string format (user:pass)"); + } + else + { + authname = [creds substringToIndex: r.location]; + authpwd = [creds substringFromIndex: r.location+1]; + } + } + + if (authname != nil && authpwd != nil) + { + [self expireAutoReplyWithUsername: authname andPassword: authpwd]; + rc = YES; + } + if (!rc) [self usage];