2011-11-21 15:09:26 +01:00
|
|
|
/* SOGoToolUserPreferences.m - this file is part of SOGo
|
|
|
|
*
|
2013-08-27 14:33:50 +02:00
|
|
|
* Copyright (C) 2011-2013 Inverse inc.
|
2011-11-21 15:09:26 +01:00
|
|
|
*
|
|
|
|
* Author: Francis Lachapelle <flachapelle@inverse.ca>
|
|
|
|
*
|
|
|
|
* 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/NSArray.h>
|
|
|
|
#import <Foundation/NSCalendarDate.h>
|
2013-01-07 21:01:06 +01:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
2011-11-21 15:09:26 +01:00
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSString.h>
|
2013-01-07 21:01:06 +01:00
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2011-11-21 15:09:26 +01:00
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
|
|
|
|
#import <GDLAccess/EOAdaptorChannel.h>
|
|
|
|
|
|
|
|
#import <GDLContentStore/GCSChannelManager.h>
|
|
|
|
#import <GDLContentStore/NSURL+GCS.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
|
|
|
|
2014-03-04 04:07:24 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
|
|
|
|
2011-11-21 15:09:26 +01:00
|
|
|
#import <SOGo/NSString+Utilities.h>
|
2013-08-27 19:04:08 +02:00
|
|
|
#import "SOGo/SOGoCredentialsFile.h"
|
2014-03-04 04:07:24 +01:00
|
|
|
#import <SOGo/SOGoProductLoader.h>
|
2013-08-27 19:04:08 +02:00
|
|
|
#import <SOGo/SOGoSieveManager.h>
|
2011-11-21 15:09:26 +01:00
|
|
|
#import <SOGo/SOGoSystemDefaults.h>
|
2013-08-27 19:04:08 +02:00
|
|
|
#import <SOGo/SOGoUser.h>
|
2011-11-21 15:09:26 +01:00
|
|
|
#import <SOGo/SOGoUserDefaults.h>
|
|
|
|
|
2014-03-04 04:07:24 +01:00
|
|
|
#import <Mailer/SOGoMailAccounts.h>
|
|
|
|
#import <Mailer/SOGoMailAccount.h>
|
|
|
|
|
2011-11-21 15:09:26 +01:00
|
|
|
#import "SOGoTool.h"
|
|
|
|
|
|
|
|
@interface SOGoToolExpireAutoReply : SOGoTool
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SOGoToolExpireAutoReply
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *) command
|
|
|
|
{
|
|
|
|
return @"expire-autoreply";
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *) description
|
|
|
|
{
|
|
|
|
return @"disable auto reply for reached end dates";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) usage
|
|
|
|
{
|
2013-01-07 21:01:06 +01:00
|
|
|
fprintf (stderr, "expire-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 expire-autoreply action should be configured as a daily cronjob.\n");
|
2011-11-21 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) removeAutoReplyForLogin: (NSString *) theLogin
|
|
|
|
withSieveUsername: (NSString *) theUsername
|
|
|
|
andPassword: (NSString *) thePassword
|
|
|
|
{
|
|
|
|
NSMutableDictionary *vacationOptions;
|
|
|
|
SOGoUserDefaults *userDefaults;
|
|
|
|
SOGoUser *user;
|
|
|
|
BOOL result;
|
|
|
|
|
|
|
|
user = [SOGoUser userWithLogin: theLogin];
|
|
|
|
userDefaults = [user userDefaults];
|
|
|
|
vacationOptions = [[userDefaults vacationOptions] mutableCopy];
|
|
|
|
[vacationOptions autorelease];
|
|
|
|
|
|
|
|
[vacationOptions setObject: [NSNumber numberWithBool: NO] forKey: @"enabled"];
|
|
|
|
[userDefaults setVacationOptions: vacationOptions];
|
|
|
|
result = [userDefaults synchronize];
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
2014-03-04 04:07:24 +01:00
|
|
|
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];
|
|
|
|
|
|
|
|
result = [account updateFiltersWithUsername: theUsername andPassword: thePassword];
|
2011-11-21 15:09:26 +01:00
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
// Can't update Sieve script -- Reactivate auto-reply
|
|
|
|
[vacationOptions setObject: [NSNumber numberWithBool: YES] forKey: @"enabled"];
|
|
|
|
[userDefaults setVacationOptions: vacationOptions];
|
|
|
|
[userDefaults synchronize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) expireAutoReplyWithUsername: (NSString *) theUsername
|
|
|
|
andPassword: (NSString *) thePassword
|
|
|
|
{
|
|
|
|
GCSChannelManager *cm;
|
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
NSArray *attrs;
|
|
|
|
NSDictionary *infos, *defaults, *vacationOptions;
|
|
|
|
NSString *sql, *profileURL, *user, *c_defaults;
|
|
|
|
NSURL *tableURL;
|
|
|
|
SOGoSystemDefaults *sd;
|
|
|
|
BOOL enabled;
|
|
|
|
unsigned int endTime, now;
|
|
|
|
|
|
|
|
now = [[NSCalendarDate calendarDate] timeIntervalSince1970];
|
|
|
|
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
|
|
|
profileURL = [sd profileURL];
|
2013-01-07 21:01:06 +01:00
|
|
|
if (!profileURL)
|
|
|
|
{
|
2013-01-14 22:57:17 +01:00
|
|
|
NSLog(@"Couldn't obtain the profileURL. (Hint: SOGoProfileURL)");
|
2013-01-07 21:01:06 +01:00
|
|
|
}
|
|
|
|
else
|
2011-11-21 15:09:26 +01:00
|
|
|
{
|
|
|
|
tableURL = [[NSURL alloc] initWithString: profileURL];
|
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
|
|
|
channel = [cm acquireOpenChannelForURL: tableURL];
|
2013-01-07 21:01:06 +01:00
|
|
|
if (!channel)
|
|
|
|
{
|
|
|
|
NSLog(@"Couldn't acquire channel for profileURL");
|
|
|
|
}
|
|
|
|
else
|
2011-11-21 15:09:26 +01:00
|
|
|
{
|
|
|
|
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"];
|
2013-10-15 16:49:21 +02:00
|
|
|
if (verbose)
|
|
|
|
NSLog(@"Checking user %@\n", user);
|
2011-11-21 15:09:26 +01:00
|
|
|
c_defaults = [infos objectForKey: @"c_defaults"];
|
|
|
|
if ([c_defaults isNotNull])
|
|
|
|
{
|
|
|
|
defaults = [c_defaults objectFromJSONString];
|
|
|
|
vacationOptions = (NSDictionary *) [defaults objectForKey: @"Vacation"];
|
|
|
|
enabled = [[vacationOptions objectForKey: @"enabled"] boolValue];
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
enabled = [[vacationOptions objectForKey: @"endDateEnabled"] boolValue];
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
endTime = [[vacationOptions objectForKey: @"endDate"] intValue];
|
|
|
|
if (endTime <= now)
|
|
|
|
{
|
|
|
|
if ([self removeAutoReplyForLogin: user
|
|
|
|
withSieveUsername: theUsername
|
|
|
|
andPassword: thePassword])
|
|
|
|
NSLog(@"Removed auto-reply of user %@", user);
|
|
|
|
else
|
|
|
|
NSLog(@"An error occured while removing auto-reply of user %@", user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) run
|
|
|
|
{
|
|
|
|
NSRange r;
|
2013-08-27 19:04:08 +02:00
|
|
|
NSString *creds, *credsFilename, *authname, *authpwd;
|
|
|
|
SOGoCredentialsFile *cf;
|
2011-11-21 15:09:26 +01:00
|
|
|
BOOL rc;
|
|
|
|
int max;
|
|
|
|
|
2013-01-07 21:01:06 +01:00
|
|
|
max = [sanitizedArguments count];
|
|
|
|
creds = nil;
|
|
|
|
authname = nil;
|
|
|
|
authpwd = nil;
|
2011-11-21 15:09:26 +01:00
|
|
|
rc = NO;
|
|
|
|
|
2013-08-27 19:04:08 +02:00
|
|
|
credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"];
|
|
|
|
if (credsFilename)
|
2011-11-21 15:09:26 +01:00
|
|
|
{
|
2013-08-27 19:04:08 +02:00
|
|
|
cf = [SOGoCredentialsFile credentialsFromFile: credsFilename];
|
|
|
|
authname = [cf username];
|
|
|
|
authpwd = [cf password];
|
2011-11-21 15:09:26 +01:00
|
|
|
}
|
|
|
|
|
2013-08-27 19:04:08 +02:00
|
|
|
/* DEPRECATED: this is only kept around to avoid breaking existing setups */
|
2013-01-07 21:01:06 +01:00
|
|
|
if (max > 0)
|
|
|
|
{
|
|
|
|
/* assume we got the creds directly on the cli */
|
|
|
|
creds = [sanitizedArguments objectAtIndex: 0];
|
2013-08-27 19:04:08 +02:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 21:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-14 22:57:17 +01:00
|
|
|
if (authname && authpwd)
|
2013-01-07 21:01:06 +01:00
|
|
|
{
|
|
|
|
[self expireAutoReplyWithUsername: authname andPassword: authpwd];
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
|
2011-11-21 15:09:26 +01:00
|
|
|
if (!rc)
|
|
|
|
[self usage];
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|