diff --git a/SoObjects/Mailer/SOGoMailAccount.h b/SoObjects/Mailer/SOGoMailAccount.h index 66da6f06d..daf9a859f 100644 --- a/SoObjects/Mailer/SOGoMailAccount.h +++ b/SoObjects/Mailer/SOGoMailAccount.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2009-2017 Inverse inc. + Copyright (C) 2009-2019 Inverse inc. This file is part of SOGo. @@ -34,6 +34,7 @@ */ @class NSArray; +@class NSException; @class NSMutableDictionary; @class NSMutableArray; @class NSString; @@ -79,11 +80,11 @@ typedef enum { - (BOOL) supportsQResync; - (id) getInboxQuota; -- (BOOL) updateFilters; -- (BOOL) updateFiltersAndForceActivation: (BOOL) forceActivation; -- (BOOL) updateFiltersWithUsername: (NSString *) theUsername - andPassword: (NSString *) thePassword - forceActivation: (BOOL) forceActivation; +- (NSException *) updateFilters; +- (NSException *) updateFiltersAndForceActivation: (BOOL) forceActivation; +- (NSException *) updateFiltersWithUsername: (NSString *) theUsername + andPassword: (NSString *) thePassword + forceActivation: (BOOL) forceActivation; - (NSArray *) identities; - (NSString *) signature; diff --git a/SoObjects/Mailer/SOGoMailAccount.m b/SoObjects/Mailer/SOGoMailAccount.m index e58539a13..38bb20458 100644 --- a/SoObjects/Mailer/SOGoMailAccount.m +++ b/SoObjects/Mailer/SOGoMailAccount.m @@ -1,5 +1,5 @@ /* - Copyright (C) 2007-2016 Inverse inc. + Copyright (C) 2007-2019 Inverse inc. This file is part of SOGo. @@ -300,21 +300,21 @@ static NSString *inboxFolderName = @"INBOX"; return inboxQuota; } -- (BOOL) updateFiltersAndForceActivation: (BOOL) forceActivation +- (NSException *) updateFiltersAndForceActivation: (BOOL) forceActivation { return [self updateFiltersWithUsername: nil andPassword: nil forceActivation: forceActivation]; } -- (BOOL) updateFilters +- (NSException *) updateFilters { return [self updateFiltersWithUsername: nil andPassword: nil forceActivation: NO]; } -- (BOOL) updateFiltersWithUsername: (NSString *) theUsername +- (NSException *) updateFiltersWithUsername: (NSString *) theUsername andPassword: (NSString *) thePassword forceActivation: (BOOL) forceActivation { diff --git a/SoObjects/SOGo/SOGoSieveManager.h b/SoObjects/SOGo/SOGoSieveManager.h index 0a179dc65..add00f801 100644 --- a/SoObjects/SOGo/SOGoSieveManager.h +++ b/SoObjects/SOGo/SOGoSieveManager.h @@ -25,6 +25,7 @@ @class NSDictionary; +@class NSException; @class NSMutableArray; @class NSString; @class NGSieveClient; @@ -52,8 +53,8 @@ - (BOOL) hasActiveExternalSieveScripts: (NGSieveClient *) client; -- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount; -- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount +- (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount; +- (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount withUsername: (NSString *) theUsername andPassword: (NSString *) thePassword forceActivation: (BOOL) forceActivation; diff --git a/SoObjects/SOGo/SOGoSieveManager.m b/SoObjects/SOGo/SOGoSieveManager.m index bbc6366af..f315426fa 100644 --- a/SoObjects/SOGo/SOGoSieveManager.m +++ b/SoObjects/SOGo/SOGoSieveManager.m @@ -37,6 +37,7 @@ #import #import #import +#import #import "../Mailer/SOGoMailAccount.h" @@ -816,7 +817,7 @@ static NSString *sieveScriptName = @"sogo"; while ((key = [keys nextObject])) { if ([key caseInsensitiveCompare: @"sogo"] != NSOrderedSame && - [[[scripts objectForKey: key] stringValue] length] > 0) + [[scripts objectForKey: key] intValue] > 0) return YES; } @@ -826,7 +827,7 @@ static NSString *sieveScriptName = @"sogo"; // // // -- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount +- (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount { return [self updateFiltersForAccount: theAccount withUsername: nil @@ -837,15 +838,16 @@ static NSString *sieveScriptName = @"sogo"; // // // -- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount +- (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount withUsername: (NSString *) theUsername andPassword: (NSString *) thePassword forceActivation: (BOOL) forceActivation { - NSString *filterScript, *v, *content; + NSString *filterScript, *v, *content, *message; NSMutableArray *req; NSMutableString *script, *header; NSDictionary *result, *values; + NSException *error; SOGoUserDefaults *ud; SOGoDomainDefaults *dd; NGSieveClient *client; @@ -853,16 +855,22 @@ static NSString *sieveScriptName = @"sogo"; BOOL b, activate, dateCapability; unsigned int now; + error = nil; dd = [user domainDefaults]; if (!([dd sieveScriptsEnabled] || [dd vacationEnabled] || [dd forwardEnabled])) - return YES; + return error; req = [NSMutableArray arrayWithCapacity: 15]; ud = [user userDefaults]; client = [self clientForAccount: theAccount withUsername: theUsername andPassword: thePassword]; if (!client) - return NO; + { + error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */ + reason: @"Error while connecting to Sieve server."]; + return error; + } + // Activate script Sieve when forced or when no external script is enabled activate = forceActivation || ![self hasActiveExternalSieveScripts: client]; @@ -909,9 +917,12 @@ static NSString *sieveScriptName = @"sogo"; } else { - [self errorWithFormat: @"Sieve generation failure: %@", [self lastScriptError]]; + message = [NSString stringWithFormat: @"Sieve generation failure: %@", [self lastScriptError]]; + [self errorWithFormat: message]; [client closeConnection]; - return NO; + error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */ + reason: message]; + return error; } // @@ -1137,7 +1148,7 @@ static NSString *sieveScriptName = @"sogo"; result = [client deleteScript: sieveScriptName]; if (![[result valueForKey:@"result"] boolValue]) - [self logWithFormat: @"WARNING: Could not delete Sieve script - continuing...: %@", result]; + [self warnWithFormat: @"Could not delete Sieve script: %@", [[result objectForKey: @"RawResponse"] objectForKey: @"reason"]]; /* We put and activate the script only if we actually have a script that does something... */ @@ -1147,9 +1158,12 @@ static NSString *sieveScriptName = @"sogo"; if (![[result valueForKey:@"result"] boolValue]) { - [self logWithFormat: @"Could not upload Sieve script: %@", result]; + message = [NSString stringWithFormat: @"Could not upload Sieve script: %@", [[result objectForKey: @"RawResponse"] objectForKey: @"reason"]]; + [self errorWithFormat: message]; [client closeConnection]; - return NO; + error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */ + reason: message]; + return error; } if (activate) @@ -1157,15 +1171,18 @@ static NSString *sieveScriptName = @"sogo"; result = [client setActiveScript: sieveScriptName]; if (![[result valueForKey:@"result"] boolValue]) { - [self logWithFormat: @"Could not enable Sieve script: %@", result]; + message = [NSString stringWithFormat: @"Could not enable Sieve script: %@", [[result objectForKey: @"RawResponse"] objectForKey: @"reason"]]; + [self errorWithFormat: message]; [client closeConnection]; - return NO; + error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */ + reason: message]; + return error; } } } [client closeConnection]; - return YES; + return error; } @end diff --git a/UI/PreferencesUI/UIxPreferences.m b/UI/PreferencesUI/UIxPreferences.m index 95f4003bd..674b5d4b7 100644 --- a/UI/PreferencesUI/UIxPreferences.m +++ b/UI/PreferencesUI/UIxPreferences.m @@ -1613,6 +1613,7 @@ static NSArray *reminderValues = nil; if ([[user userDefaults] synchronize]) { + NSException *error; SOGoMailAccount *account; SOGoMailAccounts *folder; SOGoDomainDefaults *dd; @@ -1628,10 +1629,10 @@ static NSArray *reminderValues = nil; inContext: context]; account = [folder lookupName: @"0" inContext: context acquire: NO]; - if (![account updateFiltersAndForceActivation: forceActivation]) + if ((error = [account updateFiltersAndForceActivation: forceActivation])) { - results = (id ) [self responseWithStatus: 502 - andJSONRepresentation: [NSDictionary dictionaryWithObjectsAndKeys: @"Connection error", @"message", nil]]; + results = (id ) [self responseWithStatus: 500 + andJSONRepresentation: [NSDictionary dictionaryWithObjectsAndKeys: [error reason], @"message", nil]]; } } else