feat(mail): enable autoreply on specific days or at a specific time

Fixes #5328
This commit is contained in:
Francis Lachapelle
2021-12-02 14:20:47 -05:00
parent 6807d1df8b
commit 2ecd441f32
10 changed files with 498 additions and 141 deletions
+92 -26
View File
@@ -22,6 +22,7 @@
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSTimeZone.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSValue.h>
@@ -946,14 +947,14 @@ static NSString *sieveScriptName = @"sogo";
dateCapability || [[values objectForKey: @"endDate"] intValue] > now))
{
NSCalendarDate *startDate, *endDate;
NSMutableArray *allConditions;
NSMutableArray *allConditions, *timeConditions;
NSMutableString *vacation_script;
NSArray *addresses;
NSString *text, *templateFilePath, *customSubject;
NSArray *addresses, *weekdays;
NSString *text, *templateFilePath, *customSubject, *weekday, *startTime, *endTime, *timeCondition, *timeZone;
SOGoTextTemplateFile *templateFile;
BOOL ignore, alwaysSend, useCustomSubject, discardMails;
int days, i;
int days, i, seconds;
allConditions = [NSMutableArray array];
days = [[values objectForKey: @"daysBetweenResponse"] intValue];
@@ -1009,30 +1010,95 @@ static NSString *sieveScriptName = @"sogo";
[allConditions addObject: @"not header :comparator \"i;ascii-casemap\" :matches \"To\" \"Multiple recipients of*\""];
}
// Start date of auto-reply
if ([dd vacationPeriodEnabled] &&
[[values objectForKey: @"startDateEnabled"] boolValue] &&
dateCapability)
if ([dd vacationPeriodEnabled] && dateCapability)
{
[req addObjectUniquely: @"date"];
[req addObjectUniquely: @"relational"];
startDate = [NSCalendarDate dateWithTimeIntervalSince1970:
[[values objectForKey: @"startDate"] intValue]];
[allConditions addObject: [NSString stringWithFormat: @"currentdate :value \"ge\" \"date\" \"%@\"",
[startDate descriptionWithCalendarFormat: @"%Y-%m-%d"]]];
}
// Start date of auto-reply
if ([[values objectForKey: @"startDateEnabled"] boolValue])
{
[req addObjectUniquely: @"date"];
[req addObjectUniquely: @"relational"];
startDate = [NSCalendarDate dateWithTimeIntervalSince1970:
[[values objectForKey: @"startDate"] intValue]];
[allConditions addObject: [NSString stringWithFormat: @"currentdate :value \"ge\" \"date\" \"%@\"",
[startDate descriptionWithCalendarFormat: @"%Y-%m-%d"]]];
}
// End date of auto-reply
if ([dd vacationPeriodEnabled] &&
[[values objectForKey: @"endDateEnabled"] boolValue] &&
dateCapability)
{
[req addObjectUniquely: @"date"];
[req addObjectUniquely: @"relational"];
endDate = [NSCalendarDate dateWithTimeIntervalSince1970:
[[values objectForKey: @"endDate"] intValue]];
[allConditions addObject: [NSString stringWithFormat: @"currentdate :value \"le\" \"date\" \"%@\"",
[endDate descriptionWithCalendarFormat: @"%Y-%m-%d"]]];
// End date of auto-reply
if ([[values objectForKey: @"endDateEnabled"] boolValue])
{
[req addObjectUniquely: @"date"];
[req addObjectUniquely: @"relational"];
endDate = [NSCalendarDate dateWithTimeIntervalSince1970:
[[values objectForKey: @"endDate"] intValue]];
[allConditions addObject: [NSString stringWithFormat: @"currentdate :value \"le\" \"date\" \"%@\"",
[endDate descriptionWithCalendarFormat: @"%Y-%m-%d"]]];
}
seconds = [[ud timeZone] secondsFromGMT];
timeZone = [NSString stringWithFormat: @"%.2i%02i", seconds/60/60, seconds/60%60];
timeConditions = [NSMutableArray array];
startTime = endTime = nil;
// Start auto-reply from a specific time
if ([[values objectForKey: @"startTimeEnabled"] boolValue])
{
startTime = [values objectForKey: @"startTime"];
timeCondition = [NSString stringWithFormat: @"date :value \"ge\" :zone \"%@\" \"date\" \"time\" \"%@:00\"",
timeZone, startTime];
[timeConditions addObject: timeCondition];
}
// Stop auto-reply at a specific time
if ([[values objectForKey: @"endTimeEnabled"] boolValue])
{
endTime = [values objectForKey: @"endTime"];
timeCondition = [NSString stringWithFormat: @"date :value \"lt\" :zone \"%@\" \"date\" \"time\" \"%@:00\"",
timeZone, endTime];
[timeConditions addObject: timeCondition];
}
if (startTime && endTime)
{
[req addObjectUniquely: @"date"];
[req addObjectUniquely: @"relational"];
if ([startTime compare: endTime] == NSOrderedAscending)
{
// Start time is before end time:
// >= startTime && < endTime
[allConditions addObjectsFromArray: timeConditions];
}
else
{
// End time is before start time:
// >= startTime || < endTime
timeCondition = [NSString stringWithFormat: @"anyof ( %@ )", [timeConditions componentsJoinedByString: @", "]];
[allConditions addObject: timeCondition];
}
}
else if ([timeConditions count])
{
[req addObjectUniquely: @"date"];
[req addObjectUniquely: @"relational"];
[allConditions addObjectsFromArray: timeConditions];
}
// Auto-reply during specific days
if ([[values objectForKey: @"weekdaysEnabled"] boolValue])
{
[req addObjectUniquely: @"date"];
[req addObjectUniquely: @"relational"];
weekdays = [values objectForKey: @"days"];
NSMutableString *currentWeekday = [NSMutableString stringWithString: @"currentdate :is \"weekday\" ["];
for (i = 0; i < [weekdays count]; i++)
{
if (i > 0)
[currentWeekday appendString: @", "];
weekday = [weekdays objectAtIndex: i];
[currentWeekday appendString: [weekday asSieveQuotedString]];
}
[currentWeekday appendString: @"]"];
[allConditions addObject: currentWeekday];
}
}
// Apply conditions
+10 -2
View File
@@ -1,6 +1,5 @@
/*
Copyright (C) 2004-2005 SKYRIX Software AG
Copyright (C) 2007-2016 Inverse inc.
Copyright (C) 2007-2021 Inverse inc.
SOGo is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
@@ -18,6 +17,7 @@
02111-1307, USA.
*/
#import <Foundation/NSTimeZone.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/SoSecurityManager.h>
@@ -674,6 +674,14 @@
return [NSString stringWithFormat: @"urn:uuid:%@", nameInContainer];
}
- (NSString *) davCalendarTimeZone
{
NSTimeZone *tz = [[[context activeUser] userDefaults] timeZone];
int secs = [tz secondsFromGMT];
return [NSString stringWithFormat: @"%02i%02i", secs / 60 / 60, abs(secs % 60)];
}
// - (NSException *) setDavSignature: (NSString *) newSignature
// {
// SOGoUserDefaults *ud;