1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/* SOGoToolUpdateAutoReply.m - this file is part of SOGo
*
* Copyright (C) 2011-2022 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSTimeZone.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSValue.h>
#import <GDLAccess/EOAdaptorChannel.h>
#import <GDLContentStore/GCSChannelManager.h>
#import <GDLContentStore/NSURL+GCS.h>
#import <NGExtensions/NSCalendarDate+misc.h>
#import <NGExtensions/NSNull+misc.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <SOGo/NSString+Utilities.h>
#import "SOGo/SOGoCredentialsFile.h"
#import <SOGo/SOGoProductLoader.h>
#import <SOGo/SOGoSystemDefaults.h>
#import <SOGo/SOGoUser.h>
#import <Mailer/SOGoMailAccounts.h>
#import <Mailer/SOGoMailAccount.h>
#import "SOGoTool.h"
#define statusquoAutoReply 0x00
#define enableAutoReply 0x01
#define disableAutoReply 0x02
#define temporarilyAutoReply 0x10
#define temporarilyEnableAutoReply 0x11
#define temporarilyDisableAutoReply 0x12
@interface SOGoToolUpdateAutoReply : SOGoTool
@end
@implementation SOGoToolUpdateAutoReply
+ (void) initialize
{
}
+ (NSString *) command
{
return @"update-autoreply";
}
+ (NSString *) description
{
return @"enable or disable auto reply for reached start/end dates";
}
- (void) usage
{
fprintf (stderr, "update-autoreply -p credentialFile\n\n"
" -p credentialFile Specify the file containing the sieve admin credentials\n"
" The file should contain a single line:\n"
" username:password\n"
"\n"
"The update-autoreply action should be configured as a daily cronjob.\n");
}
- (BOOL) checkConstraintsForRow: (NSDictionary *) infos
{
NSArray *weekdays, *startTime, *endTime;
NSCalendarDate *now, *startDate, *endDate;
NSDictionary *defaults, *vacationOptions;
NSString *c_defaults;
NSTimeZone *timeZone;
BOOL hasConstraints;
unsigned int beginOfDaysSecs, endDateTime, startDateTime, result;
result = statusquoAutoReply;
c_defaults = [infos objectForKey: @"c_defaults"];
startDate = endDate = nil;
hasConstraints = NO;
if ([c_defaults isNotNull])
{
defaults = [c_defaults objectFromJSONString];
vacationOptions = (NSDictionary *) [defaults objectForKey: @"Vacation"];
if ([[vacationOptions objectForKey: @"enabled"] boolValue])
{
timeZone = [NSTimeZone timeZoneWithName: (NSString *)[defaults objectForKey: @"SOGoTimeZone"]];
now = [NSCalendarDate calendarDate];
[now setTimeZone: timeZone];
beginOfDaysSecs = [[now beginOfDay] timeIntervalSince1970];
// We handle the start date
if ([[vacationOptions objectForKey: @"startDateEnabled"] boolValue])
{
hasConstraints = YES;
startDateTime = [[vacationOptions objectForKey: @"startDate"] intValue];
if (beginOfDaysSecs >= startDateTime)
result = enableAutoReply;
else
result = temporarilyDisableAutoReply;
}
// We handle the end date
if ([[vacationOptions objectForKey: @"endDateEnabled"] boolValue])
{
endDateTime = [[vacationOptions objectForKey: @"endDate"] intValue];
if (endDateTime < beginOfDaysSecs)
result = disableAutoReply;
}
if (!(result & disableAutoReply))
{
// We handle the start time
if ([[vacationOptions objectForKey: @"startTimeEnabled"] boolValue])
{
hasConstraints = YES;
startTime = [[vacationOptions objectForKey: @"startTime"] componentsSeparatedByString: @":"];
startDate = [NSCalendarDate dateWithYear: [now yearOfCommonEra]
month: [now monthOfYear]
day: [now dayOfMonth]
hour: [[startTime objectAtIndex: 0] intValue]
minute: [[startTime objectAtIndex: 1] intValue]
second: [now secondOfMinute]
timeZone: [now timeZone]];
if ([startDate compare: now] == NSOrderedSame ||
[startDate compare: now] == NSOrderedAscending)
result = temporarilyEnableAutoReply;
else if (![[vacationOptions objectForKey: @"endTimeEnabled"] boolValue])
result = temporarilyDisableAutoReply;
}
// We handle the end time
// NOTE: if end time is enabled, start time must be defined
if ([[vacationOptions objectForKey: @"endTimeEnabled"] boolValue])
{
endTime = [[vacationOptions objectForKey: @"endTime"] componentsSeparatedByString: @":"];
endDate = [NSCalendarDate dateWithYear: [now yearOfCommonEra]
month: [now monthOfYear]
day: [now dayOfMonth]
hour: [[endTime objectAtIndex: 0] intValue]
minute: [[endTime objectAtIndex: 1] intValue]
second: [now secondOfMinute]
timeZone: [now timeZone]];
if ([endDate compare: now] == NSOrderedSame ||
[endDate compare: now] == NSOrderedAscending)
{
if ([startDate compare: endDate] == NSOrderedAscending ||
(result != temporarilyEnableAutoReply))
result = temporarilyDisableAutoReply;
}
}
}
if (result != disableAutoReply)
{
// We handle the weekdays
if ([[vacationOptions objectForKey: @"weekdaysEnabled"] boolValue])
{
hasConstraints = YES;
weekdays = [vacationOptions objectForKey: @"days"];
if ([weekdays containsObject: [NSString stringWithFormat: @"%i", [now dayOfWeek]]])
result = temporarilyEnableAutoReply;
else
result = temporarilyDisableAutoReply;
}
}
if (result == statusquoAutoReply && !hasConstraints)
// No action and no constraint on start date; enable the auto-reply
result = enableAutoReply;
}
}
return result;
}
- (BOOL) updateAutoReplyForLogin: (NSString *) theLogin
withSieveUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword
disabling: (BOOL) disabling
temporarily: (BOOL) temporarily
{
NSMutableDictionary *vacationOptions;
SOGoUserDefaults *userDefaults;
SOGoUser *user;
BOOL result;
NSException *error;
result = YES;
user = [SOGoUser userWithLogin: theLogin];
userDefaults = [user userDefaults];
vacationOptions = [[userDefaults vacationOptions] mutableCopy];
[vacationOptions autorelease];
if (!temporarily)
{
if (disabling)
{
[vacationOptions setObject: [NSNumber numberWithBool: NO] forKey: @"enabled"];
}
else
{
// We do NOT enable the vacation message automatically if the domain
// preference is disabled.
if (![[user domainDefaults] vacationPeriodEnabled])
{
NSLog(@"SOGoVacationPeriodEnabled set to NO for the domain - ignoring.");
return NO;
}
[vacationOptions setObject: [NSNumber numberWithBool: NO] forKey: @"startDateEnabled"];
}
[userDefaults setVacationOptions: vacationOptions];
result = [userDefaults synchronize];
}
if (result)
{
SOGoUserFolder *home;
SOGoMailAccounts *folder;
SOGoMailAccount *account;
WOContext *localContext;
Class SOGoMailAccounts_class;
[[SOGoProductLoader productLoader] loadProducts: [NSArray arrayWithObject: @"Mailer.SOGo"]];
SOGoMailAccounts_class = NSClassFromString(@"SOGoMailAccounts");
localContext = [WOContext context];
[localContext setActiveUser: user];
home = [user homeFolderInContext: localContext];
folder = [SOGoMailAccounts_class objectWithName: @"Mail" inContainer: home];
account = [folder lookupName: @"0" inContext: localContext acquire: NO];
[account setContext: localContext];
error = [account updateFiltersWithUsername: theUsername
andPassword: thePassword
forceActivation: NO];
if (error && !temporarily)
{
// Can't update Sieve script -- Reactivate auto-reply
if (disabling)
[vacationOptions setObject: [NSNumber numberWithBool: YES] forKey: @"enabled"];
else
[vacationOptions setObject: [NSNumber numberWithBool: YES] forKey: @"startDateEnabled"];
[userDefaults setVacationOptions: vacationOptions];
[userDefaults synchronize];
result = NO;
}
}
return result;
}
- (void) updateAutoReplyWithUsername: (NSString *) theUsername
andPassword: (NSString *) thePassword
{
GCSChannelManager *cm;
EOAdaptorChannel *channel;
NSArray *attrs;
NSDictionary *infos;
NSString *sql, *profileURL, *user;
NSURL *tableURL;
SOGoSystemDefaults *sd;
unsigned int result;
sd = [SOGoSystemDefaults sharedSystemDefaults];
profileURL = [sd profileURL];
if (!profileURL)
{
NSLog(@"Couldn't obtain the profileURL. (Hint: SOGoProfileURL)");
}
else
{
tableURL = [[NSURL alloc] initWithString: profileURL];
cm = [GCSChannelManager defaultChannelManager];
channel = [cm acquireOpenChannelForURL: tableURL];
if (!channel)
{
NSLog(@"Couldn't acquire channel for profileURL");
}
else
{
sql = [NSString stringWithFormat: @"SELECT c_uid, c_defaults FROM %@",
[tableURL gcsTableName]];
[channel evaluateExpressionX: sql];
attrs = [channel describeResults: NO];
while ((infos = [channel fetchAttributes: attrs withZone: NULL]))
{
user = [infos objectForKey: @"c_uid"];
if (verbose)
NSLog(@"Checking user %@\n", user);
result = [self checkConstraintsForRow: infos];
if (result & enableAutoReply)
{
if ([self updateAutoReplyForLogin: user
withSieveUsername: theUsername
andPassword: thePassword
disabling: NO
temporarily: !!(result & temporarilyAutoReply)])
NSLog(@"Enabled auto-reply of user %@", user);
else
NSLog(@"An error occured while enabling auto-reply of user %@", user);
}
else if (result & disableAutoReply)
{
if ([self updateAutoReplyForLogin: user
withSieveUsername: theUsername
andPassword: thePassword
disabling: YES
temporarily: !!(result & temporarilyAutoReply)])
NSLog(@"Removed auto-reply of user %@", user);
else
NSLog(@"An error occured while removing auto-reply of user %@", user);
}
}
}
}
}
- (BOOL) run
{
NSRange r;
NSString *creds, *credsFilename, *authname, *authpwd;
SOGoCredentialsFile *cf;
BOOL rc;
int max;
max = [sanitizedArguments count];
creds = nil;
authname = nil;
authpwd = nil;
rc = NO;
credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"];
if (credsFilename)
{
cf = [SOGoCredentialsFile credentialsFromFile: credsFilename];
authname = [cf username];
authpwd = [cf password];
}
/* DEPRECATED: this is only kept around to avoid breaking existing setups */
if (max > 0)
{
/* assume we got the creds directly on the cli */
creds = [sanitizedArguments objectAtIndex: 0];
if (creds)
{
r = [creds rangeOfString: @":"];
if (r.location == NSNotFound)
{
NSLog(@"Invalid credential string format (user:pass)");
}
else
{
authname = [creds substringToIndex: r.location];
authpwd = [creds substringFromIndex: r.location+1];
}
}
}
if (authname && authpwd)
{
[self updateAutoReplyWithUsername: authname andPassword: authpwd];
rc = YES;
}
if (!rc)
[self usage];
return rc;
}
@end
|