fix(preferences): improve error handling with Sieve server

pull/265/head
Francis Lachapelle 2019-12-16 14:54:03 -05:00
parent 9aeecead6c
commit 7180b5988d
5 changed files with 49 additions and 29 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2009-2017 Inverse inc. Copyright (C) 2009-2019 Inverse inc.
This file is part of SOGo. This file is part of SOGo.
@ -34,6 +34,7 @@
*/ */
@class NSArray; @class NSArray;
@class NSException;
@class NSMutableDictionary; @class NSMutableDictionary;
@class NSMutableArray; @class NSMutableArray;
@class NSString; @class NSString;
@ -79,11 +80,11 @@ typedef enum {
- (BOOL) supportsQResync; - (BOOL) supportsQResync;
- (id) getInboxQuota; - (id) getInboxQuota;
- (BOOL) updateFilters; - (NSException *) updateFilters;
- (BOOL) updateFiltersAndForceActivation: (BOOL) forceActivation; - (NSException *) updateFiltersAndForceActivation: (BOOL) forceActivation;
- (BOOL) updateFiltersWithUsername: (NSString *) theUsername - (NSException *) updateFiltersWithUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation; forceActivation: (BOOL) forceActivation;
- (NSArray *) identities; - (NSArray *) identities;
- (NSString *) signature; - (NSString *) signature;

View File

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2007-2016 Inverse inc. Copyright (C) 2007-2019 Inverse inc.
This file is part of SOGo. This file is part of SOGo.
@ -300,21 +300,21 @@ static NSString *inboxFolderName = @"INBOX";
return inboxQuota; return inboxQuota;
} }
- (BOOL) updateFiltersAndForceActivation: (BOOL) forceActivation - (NSException *) updateFiltersAndForceActivation: (BOOL) forceActivation
{ {
return [self updateFiltersWithUsername: nil return [self updateFiltersWithUsername: nil
andPassword: nil andPassword: nil
forceActivation: forceActivation]; forceActivation: forceActivation];
} }
- (BOOL) updateFilters - (NSException *) updateFilters
{ {
return [self updateFiltersWithUsername: nil return [self updateFiltersWithUsername: nil
andPassword: nil andPassword: nil
forceActivation: NO]; forceActivation: NO];
} }
- (BOOL) updateFiltersWithUsername: (NSString *) theUsername - (NSException *) updateFiltersWithUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation forceActivation: (BOOL) forceActivation
{ {

View File

@ -25,6 +25,7 @@
@class NSDictionary; @class NSDictionary;
@class NSException;
@class NSMutableArray; @class NSMutableArray;
@class NSString; @class NSString;
@class NGSieveClient; @class NGSieveClient;
@ -52,8 +53,8 @@
- (BOOL) hasActiveExternalSieveScripts: (NGSieveClient *) client; - (BOOL) hasActiveExternalSieveScripts: (NGSieveClient *) client;
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount; - (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount;
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount - (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount
withUsername: (NSString *) theUsername withUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation; forceActivation: (BOOL) forceActivation;

View File

@ -37,6 +37,7 @@
#import <NGImap4/NGImap4Connection.h> #import <NGImap4/NGImap4Connection.h>
#import <NGImap4/NGImap4Client.h> #import <NGImap4/NGImap4Client.h>
#import <NGImap4/NGSieveClient.h> #import <NGImap4/NGSieveClient.h>
#import <NGObjWeb/NSException+HTTP.h>
#import "../Mailer/SOGoMailAccount.h" #import "../Mailer/SOGoMailAccount.h"
@ -816,7 +817,7 @@ static NSString *sieveScriptName = @"sogo";
while ((key = [keys nextObject])) while ((key = [keys nextObject]))
{ {
if ([key caseInsensitiveCompare: @"sogo"] != NSOrderedSame && if ([key caseInsensitiveCompare: @"sogo"] != NSOrderedSame &&
[[[scripts objectForKey: key] stringValue] length] > 0) [[scripts objectForKey: key] intValue] > 0)
return YES; return YES;
} }
@ -826,7 +827,7 @@ static NSString *sieveScriptName = @"sogo";
// //
// //
// //
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount - (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount
{ {
return [self updateFiltersForAccount: theAccount return [self updateFiltersForAccount: theAccount
withUsername: nil withUsername: nil
@ -837,15 +838,16 @@ static NSString *sieveScriptName = @"sogo";
// //
// //
// //
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount - (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount
withUsername: (NSString *) theUsername withUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation forceActivation: (BOOL) forceActivation
{ {
NSString *filterScript, *v, *content; NSString *filterScript, *v, *content, *message;
NSMutableArray *req; NSMutableArray *req;
NSMutableString *script, *header; NSMutableString *script, *header;
NSDictionary *result, *values; NSDictionary *result, *values;
NSException *error;
SOGoUserDefaults *ud; SOGoUserDefaults *ud;
SOGoDomainDefaults *dd; SOGoDomainDefaults *dd;
NGSieveClient *client; NGSieveClient *client;
@ -853,16 +855,22 @@ static NSString *sieveScriptName = @"sogo";
BOOL b, activate, dateCapability; BOOL b, activate, dateCapability;
unsigned int now; unsigned int now;
error = nil;
dd = [user domainDefaults]; dd = [user domainDefaults];
if (!([dd sieveScriptsEnabled] || [dd vacationEnabled] || [dd forwardEnabled])) if (!([dd sieveScriptsEnabled] || [dd vacationEnabled] || [dd forwardEnabled]))
return YES; return error;
req = [NSMutableArray arrayWithCapacity: 15]; req = [NSMutableArray arrayWithCapacity: 15];
ud = [user userDefaults]; ud = [user userDefaults];
client = [self clientForAccount: theAccount withUsername: theUsername andPassword: thePassword]; client = [self clientForAccount: theAccount withUsername: theUsername andPassword: thePassword];
if (!client) 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 script Sieve when forced or when no external script is enabled
activate = forceActivation || ![self hasActiveExternalSieveScripts: client]; activate = forceActivation || ![self hasActiveExternalSieveScripts: client];
@ -909,9 +917,12 @@ static NSString *sieveScriptName = @"sogo";
} }
else else
{ {
[self errorWithFormat: @"Sieve generation failure: %@", [self lastScriptError]]; message = [NSString stringWithFormat: @"Sieve generation failure: %@", [self lastScriptError]];
[self errorWithFormat: message];
[client closeConnection]; [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]; result = [client deleteScript: sieveScriptName];
if (![[result valueForKey:@"result"] boolValue]) 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 /* We put and activate the script only if we actually have a script
that does something... */ that does something... */
@ -1147,9 +1158,12 @@ static NSString *sieveScriptName = @"sogo";
if (![[result valueForKey:@"result"] boolValue]) 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]; [client closeConnection];
return NO; error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
reason: message];
return error;
} }
if (activate) if (activate)
@ -1157,15 +1171,18 @@ static NSString *sieveScriptName = @"sogo";
result = [client setActiveScript: sieveScriptName]; result = [client setActiveScript: sieveScriptName];
if (![[result valueForKey:@"result"] boolValue]) 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]; [client closeConnection];
return NO; error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
reason: message];
return error;
} }
} }
} }
[client closeConnection]; [client closeConnection];
return YES; return error;
} }
@end @end

View File

@ -1613,6 +1613,7 @@ static NSArray *reminderValues = nil;
if ([[user userDefaults] synchronize]) if ([[user userDefaults] synchronize])
{ {
NSException *error;
SOGoMailAccount *account; SOGoMailAccount *account;
SOGoMailAccounts *folder; SOGoMailAccounts *folder;
SOGoDomainDefaults *dd; SOGoDomainDefaults *dd;
@ -1628,10 +1629,10 @@ static NSArray *reminderValues = nil;
inContext: context]; inContext: context];
account = [folder lookupName: @"0" inContext: context acquire: NO]; account = [folder lookupName: @"0" inContext: context acquire: NO];
if (![account updateFiltersAndForceActivation: forceActivation]) if ((error = [account updateFiltersAndForceActivation: forceActivation]))
{ {
results = (id <WOActionResults>) [self responseWithStatus: 502 results = (id <WOActionResults>) [self responseWithStatus: 500
andJSONRepresentation: [NSDictionary dictionaryWithObjectsAndKeys: @"Connection error", @"message", nil]]; andJSONRepresentation: [NSDictionary dictionaryWithObjectsAndKeys: [error reason], @"message", nil]];
} }
} }
else else