Skip to content

Commit

Permalink
fix(preferences): improve error handling with Sieve server
Browse files Browse the repository at this point in the history
  • Loading branch information
cgx committed Dec 16, 2019
1 parent 9aeecea commit 7180b59
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
13 changes: 7 additions & 6 deletions SoObjects/Mailer/SOGoMailAccount.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2009-2017 Inverse inc.
Copyright (C) 2009-2019 Inverse inc.
This file is part of SOGo.
Expand Down Expand Up @@ -34,6 +34,7 @@
*/

@class NSArray;
@class NSException;
@class NSMutableDictionary;
@class NSMutableArray;
@class NSString;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions SoObjects/Mailer/SOGoMailAccount.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2007-2016 Inverse inc.
Copyright (C) 2007-2019 Inverse inc.
This file is part of SOGo.
Expand Down Expand Up @@ -300,21 +300,21 @@ - (id) getInboxQuota
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
{
Expand Down
5 changes: 3 additions & 2 deletions SoObjects/SOGo/SOGoSieveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


@class NSDictionary;
@class NSException;
@class NSMutableArray;
@class NSString;
@class NGSieveClient;
Expand Down Expand Up @@ -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;
Expand Down
45 changes: 31 additions & 14 deletions SoObjects/SOGo/SOGoSieveManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#import <NGImap4/NGImap4Connection.h>
#import <NGImap4/NGImap4Client.h>
#import <NGImap4/NGSieveClient.h>
#import <NGObjWeb/NSException+HTTP.h>

#import "../Mailer/SOGoMailAccount.h"

Expand Down Expand Up @@ -816,7 +817,7 @@ - (BOOL) hasActiveExternalSieveScripts: (NGSieveClient *) client
while ((key = [keys nextObject]))
{
if ([key caseInsensitiveCompare: @"sogo"] != NSOrderedSame &&
[[[scripts objectForKey: key] stringValue] length] > 0)
[[scripts objectForKey: key] intValue] > 0)
return YES;
}

Expand All @@ -826,7 +827,7 @@ - (BOOL) hasActiveExternalSieveScripts: (NGSieveClient *) client
//
//
//
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount
- (NSException *) updateFiltersForAccount: (SOGoMailAccount *) theAccount
{
return [self updateFiltersForAccount: theAccount
withUsername: nil
Expand All @@ -837,32 +838,39 @@ - (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount
//
//
//
- (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;
NGImap4Client *imapClient;
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];
Expand Down Expand Up @@ -909,9 +917,12 @@ - (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount
}
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;
}

//
Expand Down Expand Up @@ -1137,7 +1148,7 @@ - (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount
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... */
Expand All @@ -1147,25 +1158,31 @@ - (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount

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)
{
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
7 changes: 4 additions & 3 deletions UI/PreferencesUI/UIxPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,7 @@ - (NSString *) forwardEnabled

if ([[user userDefaults] synchronize])
{
NSException *error;
SOGoMailAccount *account;
SOGoMailAccounts *folder;
SOGoDomainDefaults *dd;
Expand All @@ -1628,10 +1629,10 @@ - (NSString *) forwardEnabled
inContext: context];
account = [folder lookupName: @"0" inContext: context acquire: NO];

if (![account updateFiltersAndForceActivation: forceActivation])
if ((error = [account updateFiltersAndForceActivation: forceActivation]))
{
results = (id <WOActionResults>) [self responseWithStatus: 502
andJSONRepresentation: [NSDictionary dictionaryWithObjectsAndKeys: @"Connection error", @"message", nil]];
results = (id <WOActionResults>) [self responseWithStatus: 500
andJSONRepresentation: [NSDictionary dictionaryWithObjectsAndKeys: [error reason], @"message", nil]];
}
}
else
Expand Down

0 comments on commit 7180b59

Please sign in to comment.