From be531100c6fc4c4099b382b128d9ff295ff03f2b Mon Sep 17 00:00:00 2001 From: Jean Raby Date: Tue, 27 Aug 2013 13:04:08 -0400 Subject: [PATCH] Use SOGoCredentialsFile to avoid duplicated code --- Tools/SOGoToolExpireAutoReply.m | 56 +++++++++++++++------------------ Tools/SOGoToolUserPreferences.m | 34 ++++++-------------- 2 files changed, 34 insertions(+), 56 deletions(-) diff --git a/Tools/SOGoToolExpireAutoReply.m b/Tools/SOGoToolExpireAutoReply.m index 2b48e4c65..4cab4f7c9 100644 --- a/Tools/SOGoToolExpireAutoReply.m +++ b/Tools/SOGoToolExpireAutoReply.m @@ -37,10 +37,11 @@ #import #import -#import -#import -#import +#import "SOGo/SOGoCredentialsFile.h" #import +#import +#import +#import #import "SOGoTool.h" @@ -180,9 +181,9 @@ - (BOOL) run { - NSData *credsData; NSRange r; - NSString *creds, *credsFile, *authname, *authpwd; + NSString *creds, *credsFilename, *authname, *authpwd; + SOGoCredentialsFile *cf; BOOL rc; int max; @@ -192,41 +193,34 @@ authpwd = nil; rc = NO; - credsFile = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; - if (credsFile) + credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; + if (credsFilename) { - credsData = [NSData dataWithContentsOfFile: credsFile]; - if (credsData == nil) - { - NSLog(@"Error reading credential file '%@'", credsFile); - return NO; - } - creds = [[NSString alloc] initWithData: credsData - encoding: NSUTF8StringEncoding]; - [creds autorelease]; - creds = [creds stringByTrimmingCharactersInSet: - [NSCharacterSet characterSetWithCharactersInString: @"\r\n"]]; + cf = [SOGoCredentialsFile credentialsFromFile: credsFilename]; + authname = [cf username]; + authpwd = [cf password]; } + /* DEPRECATED: this is only kept around to avoid breaking existing setups */ if (max > 0) { /* assume we got the creds directly on the cli */ creds = [sanitizedArguments objectAtIndex: 0]; + if (creds) + { + 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 (creds) - { - 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 && authpwd) { diff --git a/Tools/SOGoToolUserPreferences.m b/Tools/SOGoToolUserPreferences.m index b85e1b675..3d1f982a2 100644 --- a/Tools/SOGoToolUserPreferences.m +++ b/Tools/SOGoToolUserPreferences.m @@ -28,6 +28,7 @@ #import #import +#import "SOGo/SOGoCredentialsFile.h" #import #import #import @@ -109,34 +110,17 @@ typedef enum [theKey caseInsensitiveCompare: @"Vacation"] == NSOrderedSame) { /* credentials file handling */ - NSData *credsData; - NSRange r; - NSString *credsFile, *creds, *authname, *authpwd; - authname = nil; - authpwd = nil; + NSString *credsFilename, *authname, *authpwd; + SOGoCredentialsFile *cf; - - credsFile = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; - if (credsFile) + credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; + if (credsFilename) { - /* TODO: add back support for user:pwd here? */ - credsData = [NSData dataWithContentsOfFile: credsFile]; - if (credsData == nil) - { - NSLog(@"Error reading credential file '%@'", credsFile); - return NO; - } - - creds = [[NSString alloc] initWithData: credsData - encoding: NSUTF8StringEncoding]; - [creds autorelease]; - creds = [creds stringByTrimmingCharactersInSet: - [NSCharacterSet characterSetWithCharactersInString: @"\r\n"]]; - - r = [creds rangeOfString: @":"]; - authname = [creds substringToIndex: r.location]; - authpwd = [creds substringFromIndex: r.location+1]; + cf = [SOGoCredentialsFile credentialsFromFile: credsFilename]; + authname = [cf username]; + authpwd = [cf password]; } + if (authname == nil || authpwd == nil) { NSLog(@"To update Sieve scripts, you must provide the \"-p credentialFile\" parameter");