refactor(preferences): conditionally activate the Sieve script

All the user defaults are now editable through the Preferences module,
even if an external Sieve script is enabled. However, the user can
disable the external Sieve script and force the activation of the
"sogo" Sieve script.
This commit is contained in:
Francis Lachapelle
2019-11-15 14:37:35 -05:00
parent 350677bbce
commit 5b3d84ee24
8 changed files with 137 additions and 94 deletions
+3 -1
View File
@@ -80,8 +80,10 @@ typedef enum {
- (id) getInboxQuota;
- (BOOL) updateFilters;
- (BOOL) updateFiltersAndForceActivation: (BOOL) forceActivation;
- (BOOL) updateFiltersWithUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword;
andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation;
- (NSArray *) identities;
- (NSString *) signature;
+15 -2
View File
@@ -300,19 +300,32 @@ static NSString *inboxFolderName = @"INBOX";
return inboxQuota;
}
- (BOOL) updateFiltersAndForceActivation: (BOOL) forceActivation
{
return [self updateFiltersWithUsername: nil
andPassword: nil
forceActivation: forceActivation];
}
- (BOOL) updateFilters
{
return [self updateFiltersWithUsername: nil andPassword: nil];
return [self updateFiltersWithUsername: nil
andPassword: nil
forceActivation: NO];
}
- (BOOL) updateFiltersWithUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation
{
SOGoSieveManager *manager;
manager = [SOGoSieveManager sieveManagerForUser: [context activeUser]];
return [manager updateFiltersForAccount: self withUsername: theUsername andPassword: thePassword];
return [manager updateFiltersForAccount: self
withUsername: theUsername
andPassword: thePassword
forceActivation: forceActivation];
}
+4 -1
View File
@@ -50,10 +50,13 @@
withUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword;
- (BOOL) hasActiveExternalSieveScripts: (NGSieveClient *) client;
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount;
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount
withUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword;
andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation;
@end
+44 -15
View File
@@ -804,6 +804,24 @@ static NSString *sieveScriptName = @"sogo";
return [client autorelease];
}
- (BOOL) hasActiveExternalSieveScripts: (NGSieveClient *) client
{
NSDictionary *scripts;
NSEnumerator *keys;
NSString *key;
scripts = [client listScripts];
keys = [scripts keyEnumerator];
while ((key = [keys nextObject]))
{
if ([key caseInsensitiveCompare: @"sogo"] != NSOrderedSame &&
[[[scripts objectForKey: key] stringValue] length] > 0)
return YES;
}
return NO;
}
//
//
@@ -812,7 +830,8 @@ static NSString *sieveScriptName = @"sogo";
{
return [self updateFiltersForAccount: theAccount
withUsername: nil
andPassword: nil];
andPassword: nil
forceActivation: NO];
}
//
@@ -821,6 +840,7 @@ static NSString *sieveScriptName = @"sogo";
- (BOOL) updateFiltersForAccount: (SOGoMailAccount *) theAccount
withUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword
forceActivation: (BOOL) forceActivation
{
NSString *filterScript, *v, *content;
NSMutableArray *req;
@@ -830,7 +850,7 @@ static NSString *sieveScriptName = @"sogo";
SOGoDomainDefaults *dd;
NGSieveClient *client;
NGImap4Client *imapClient;
BOOL b, dateCapability;
BOOL b, activate, dateCapability;
unsigned int now;
dd = [user domainDefaults];
@@ -844,6 +864,9 @@ static NSString *sieveScriptName = @"sogo";
if (!client)
return NO;
// Activate script Sieve when forced or when no external script is enabled
activate = forceActivation || ![self hasActiveExternalSieveScripts: client];
// We adjust the "methodRequirements" based on the server's
// capabilities. Cyrus exposes "imapflags" while Dovecot (and
// potentially others) expose "imap4flags" as specified in RFC5332
@@ -1094,6 +1117,7 @@ static NSString *sieveScriptName = @"sogo";
intoArray: req];
[script appendString: @"\n"];
[script appendString: v];
b = YES;
}
}
@@ -1102,21 +1126,23 @@ static NSString *sieveScriptName = @"sogo";
header = [NSString stringWithFormat: @"require [\"%@\"];\r\n",
[[req uniqueObjects] componentsJoinedByString: @"\",\""]];
[script insertString: header atIndex: 0];
b = YES;
}
/* We ensure to deactive the current active script since it could prevent
its deletion from the server. */
if (activate)
result = [client setActiveScript: @""];
// We delete the existing Sieve script
result = [client deleteScript: sieveScriptName];
if (![[result valueForKey:@"result"] boolValue])
[self logWithFormat: @"WARNING: Could not delete Sieve script - continuing...: %@", result];
/* We put and activate the script only if we actually have a script
that does something... */
if (b && [script length])
{
result = [client setActiveScript: @""];
// We delete the existing Sieve script
result = [client deleteScript: sieveScriptName];
if (![[result valueForKey:@"result"] boolValue])
[self logWithFormat: @"WARNING: Could not delete Sieve script - continuing...: %@", result];
result = [client putScript: sieveScriptName script: script];
if (![[result valueForKey:@"result"] boolValue])
@@ -1126,12 +1152,15 @@ static NSString *sieveScriptName = @"sogo";
return NO;
}
result = [client setActiveScript: sieveScriptName];
if (![[result valueForKey:@"result"] boolValue])
if (activate)
{
[self logWithFormat: @"Could not enable Sieve script: %@", result];
[client closeConnection];
return NO;
result = [client setActiveScript: sieveScriptName];
if (![[result valueForKey:@"result"] boolValue])
{
[self logWithFormat: @"Could not enable Sieve script: %@", result];
[client closeConnection];
return NO;
}
}
}