2011-06-29 19:43:10 +02:00
|
|
|
/* SOGoToolUserPreferences.m - this file is part of SOGo
|
|
|
|
*
|
2013-08-27 14:33:50 +02:00
|
|
|
* Copyright (C) 2011-2013 Inverse inc.
|
2011-06-29 19:43:10 +02:00
|
|
|
*
|
|
|
|
* Author: Ludovic Marcotte <lmarcotte@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>
|
2013-01-07 21:07:17 +01:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
2011-10-13 03:27:57 +02:00
|
|
|
#import <Foundation/NSData.h>
|
2011-06-29 19:43:10 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSString.h>
|
2013-01-07 21:07:17 +01:00
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2011-06-29 19:43:10 +02:00
|
|
|
|
2014-03-04 04:07:24 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
|
|
|
#import <NGObjWeb/WOApplication.h>
|
|
|
|
|
2011-06-29 19:43:10 +02:00
|
|
|
#import <SOGo/NSString+Utilities.h>
|
2014-03-04 04:07:24 +01:00
|
|
|
#import <SOGo/SOGoProductLoader.h>
|
2013-08-27 19:04:08 +02:00
|
|
|
#import "SOGo/SOGoCredentialsFile.h"
|
2011-06-29 19:43:10 +02:00
|
|
|
#import <SOGo/SOGoUser.h>
|
2014-03-04 04:07:24 +01:00
|
|
|
#import <SOGo/SOGoUserFolder.h>
|
2011-06-29 19:43:10 +02:00
|
|
|
#import <SOGo/SOGoUserDefaults.h>
|
|
|
|
#import <SOGo/SOGoUserSettings.h>
|
2014-03-04 04:07:24 +01:00
|
|
|
#import <Mailer/SOGoMailAccounts.h>
|
|
|
|
#import <Mailer/SOGoMailAccount.h>
|
2011-06-29 19:43:10 +02:00
|
|
|
|
|
|
|
#import "SOGoTool.h"
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
UserPreferencesUnknown = -1,
|
|
|
|
UserPreferencesGet = 0,
|
|
|
|
UserPreferencesSet = 1,
|
|
|
|
UserPreferencesUnset = 2,
|
|
|
|
} SOGoUserPreferencesCommand;
|
|
|
|
|
|
|
|
@interface SOGoToolUserPreferences : SOGoTool
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SOGoToolUserPreferences
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *) command
|
|
|
|
{
|
|
|
|
return @"user-preferences";
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *) description
|
|
|
|
{
|
|
|
|
return @"set user defaults / settings in the database";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) usage
|
|
|
|
{
|
2013-01-07 21:07:17 +01:00
|
|
|
fprintf (stderr, "user-preferences get|set|unset defaults|settings user [-p credentialFile] key [value|-f filename]\n\n"
|
|
|
|
" user the user of whom to set the defaults/settings key/value\n"
|
|
|
|
" value the JSON-formatted value of the key\n\n"
|
|
|
|
" -p credentialFile Specify the file containing the sieve admin credentials\n"
|
|
|
|
" The file should contain a single line:\n"
|
|
|
|
" username:password\n"
|
2013-01-04 03:58:00 +01:00
|
|
|
" Examples:\n"
|
|
|
|
" sogo-tool user-preferences get defaults janedoe SOGoLanguage\n"
|
|
|
|
" sogo-tool user-preferences unset settings janedoe Mail\n"
|
|
|
|
" sogo-tool user-preferences set defaults janedoe SOGoTimeFormat '{\"SOGoTimeFormat\":\"%%I:%%M %%p\"}'\n");
|
2011-06-29 19:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// possible values are: get | set | unset
|
|
|
|
//
|
|
|
|
- (SOGoUserPreferencesCommand) _cmdFromString: (NSString *) theString
|
|
|
|
{
|
|
|
|
if ([theString length] > 2)
|
|
|
|
{
|
|
|
|
if ([theString caseInsensitiveCompare: @"get"] == NSOrderedSame)
|
2013-01-04 03:58:00 +01:00
|
|
|
return UserPreferencesGet;
|
2011-06-29 19:43:10 +02:00
|
|
|
else if ([theString caseInsensitiveCompare: @"set"] == NSOrderedSame)
|
2013-01-04 03:58:00 +01:00
|
|
|
return UserPreferencesSet;
|
2011-06-29 19:43:10 +02:00
|
|
|
else if ([theString caseInsensitiveCompare: @"unset"] == NSOrderedSame)
|
2013-01-04 03:58:00 +01:00
|
|
|
return UserPreferencesUnset;
|
|
|
|
}
|
2011-06-29 19:43:10 +02:00
|
|
|
|
|
|
|
return UserPreferencesUnknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got any of those keys for "defaults", we regenerate the Sieve script
|
|
|
|
//
|
|
|
|
// Forward
|
|
|
|
// SOGoSieveFilters
|
|
|
|
// Vacation
|
|
|
|
//
|
|
|
|
- (BOOL) _updateSieveScripsForkey: (NSString *) theKey
|
2013-01-04 03:58:00 +01:00
|
|
|
login: (NSString *) theLogin
|
2011-06-29 19:43:10 +02:00
|
|
|
{
|
|
|
|
if ([theKey caseInsensitiveCompare: @"Forward"] == NSOrderedSame ||
|
|
|
|
[theKey caseInsensitiveCompare: @"SOGoSieveFilters"] == NSOrderedSame ||
|
|
|
|
[theKey caseInsensitiveCompare: @"Vacation"] == NSOrderedSame)
|
|
|
|
{
|
2013-01-07 21:07:17 +01:00
|
|
|
/* credentials file handling */
|
2013-08-27 19:04:08 +02:00
|
|
|
NSString *credsFilename, *authname, *authpwd;
|
|
|
|
SOGoCredentialsFile *cf;
|
2013-01-07 21:07:17 +01:00
|
|
|
|
2013-08-27 19:04:08 +02:00
|
|
|
credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"];
|
|
|
|
if (credsFilename)
|
2013-01-04 03:58:00 +01:00
|
|
|
{
|
2013-08-27 19:04:08 +02:00
|
|
|
cf = [SOGoCredentialsFile credentialsFromFile: credsFilename];
|
|
|
|
authname = [cf username];
|
|
|
|
authpwd = [cf password];
|
2013-01-07 21:07:17 +01:00
|
|
|
}
|
2013-08-27 19:04:08 +02:00
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
if (authname == nil || authpwd == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"To update Sieve scripts, you must provide the \"-p credentialFile\" parameter");
|
2013-01-04 03:58:00 +01:00
|
|
|
return NO;
|
|
|
|
}
|
2014-03-04 04:07:24 +01:00
|
|
|
|
|
|
|
/* update sieve script */
|
|
|
|
SOGoUser *user;
|
|
|
|
SOGoUserFolder *home;
|
|
|
|
SOGoMailAccounts *folder;
|
|
|
|
SOGoMailAccount *account;
|
|
|
|
WOContext *localContext;
|
|
|
|
Class SOGoMailAccounts_class;
|
|
|
|
|
|
|
|
[[SOGoProductLoader productLoader] loadProducts: [NSArray arrayWithObject: @"Mailer.SOGo"]];
|
|
|
|
SOGoMailAccounts_class = NSClassFromString(@"SOGoMailAccounts");
|
|
|
|
|
|
|
|
user = [SOGoUser userWithLogin: theLogin];
|
|
|
|
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];
|
|
|
|
|
|
|
|
return [account updateFiltersWithUsername: authname andPassword: authpwd];
|
2011-06-29 19:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
|
2011-06-29 19:43:10 +02:00
|
|
|
- (BOOL) run
|
|
|
|
{
|
2013-01-07 21:07:17 +01:00
|
|
|
NSString *userId, *type, *key, *value;
|
|
|
|
NSString *jsonValueFile;
|
2011-06-29 19:43:10 +02:00
|
|
|
SOGoUserPreferencesCommand cmd;
|
|
|
|
id o;
|
|
|
|
|
|
|
|
BOOL rc;
|
|
|
|
int max;
|
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
max = [sanitizedArguments count];
|
2011-06-29 19:43:10 +02:00
|
|
|
rc = NO;
|
|
|
|
|
|
|
|
if (max > 3)
|
|
|
|
{
|
|
|
|
SOGoDefaultsSource *source;
|
|
|
|
SOGoUser *user;
|
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
cmd = [self _cmdFromString: [sanitizedArguments objectAtIndex: 0]];
|
2011-06-29 19:43:10 +02:00
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
type = [sanitizedArguments objectAtIndex: 1];
|
|
|
|
userId = [sanitizedArguments objectAtIndex: 2];
|
|
|
|
key = [sanitizedArguments objectAtIndex: 3];
|
2013-01-04 03:58:00 +01:00
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
user = [SOGoUser userWithLogin: userId];
|
2013-01-04 03:58:00 +01:00
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
if ([type caseInsensitiveCompare: @"defaults"] == NSOrderedSame)
|
|
|
|
source = [user userDefaults];
|
|
|
|
else
|
|
|
|
source = [user userSettings];
|
2013-01-04 03:58:00 +01:00
|
|
|
|
2013-01-07 21:07:17 +01:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case UserPreferencesGet:
|
|
|
|
o = [source objectForKey: key];
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
{
|
|
|
|
printf("%s: %s\n", [key UTF8String], [[o jsonRepresentation] UTF8String]);
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Value for key \"%@\" not found in %@", key, type);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UserPreferencesSet:
|
|
|
|
if (max > 4)
|
|
|
|
{
|
|
|
|
/* value specified on command line */
|
|
|
|
value = [sanitizedArguments objectAtIndex: 4];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* value is to be found in file specified with -f filename */
|
|
|
|
jsonValueFile = [[NSUserDefaults standardUserDefaults]
|
|
|
|
stringForKey: @"f"];
|
|
|
|
|
|
|
|
if (jsonValueFile == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"No value specified, aborting");
|
|
|
|
[self usage];
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
NSData *data = [NSData dataWithContentsOfFile: jsonValueFile];
|
|
|
|
if (data == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Error reading file '%@'", jsonValueFile);
|
|
|
|
[self usage];
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
value = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
|
|
|
|
[value autorelease];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o = [value objectFromJSONString];
|
|
|
|
|
|
|
|
//
|
|
|
|
// We support setting only "values" - for example, setting :
|
|
|
|
//
|
|
|
|
// SOGoDayStartTime to 9:00
|
|
|
|
//
|
|
|
|
// Values in JSON must be a dictionary so we must support passing:
|
|
|
|
//
|
|
|
|
// key == SOGoDayStartTime
|
|
|
|
// value == '{"SOGoDayStartTime": "09:00"}'
|
|
|
|
//
|
|
|
|
// to achieve what we want.
|
|
|
|
//
|
|
|
|
if (o && [o count] == 1)
|
|
|
|
{
|
|
|
|
[source setObject: [[o allValues] lastObject] forKey: key];
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// We also support passing values that are already dictionaries so in this
|
|
|
|
// case, we simply set it to the passed key.
|
|
|
|
//
|
|
|
|
else if (o)
|
|
|
|
{
|
|
|
|
[source setObject: o forKey: key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Invalid JSON input - no changes performed in the database. The supplied value was: %@", value);
|
|
|
|
[self usage];
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = [self _updateSieveScripsForkey: key
|
|
|
|
login: userId];
|
|
|
|
if (rc)
|
|
|
|
[source synchronize];
|
|
|
|
else
|
|
|
|
NSLog(@"Error updating sieve script, not updating database");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UserPreferencesUnset:
|
|
|
|
[source removeObjectForKey: key];
|
|
|
|
rc = [self _updateSieveScripsForkey: key
|
|
|
|
login: userId];
|
|
|
|
if (rc)
|
|
|
|
[source synchronize];
|
|
|
|
else
|
|
|
|
NSLog(@"Error updating sieve script, not updating database");
|
|
|
|
|
|
|
|
break;
|
|
|
|
case UserPreferencesUnknown:
|
|
|
|
break;
|
2013-01-04 03:58:00 +01:00
|
|
|
}
|
2011-06-29 19:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
[self usage];
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|