2012-12-10 20:10:56 +01:00
|
|
|
/* SOGoToolDumpDefaults.m - this file is part of SOGo
|
|
|
|
*
|
2017-06-16 19:25:15 +02:00
|
|
|
* Copyright (C) 2012-2017 Inverse inc.
|
2012-12-10 20:10:56 +01:00
|
|
|
*
|
|
|
|
* 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/NSData.h>
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
|
|
|
#import <Foundation/NSPropertyList.h>
|
|
|
|
|
|
|
|
#import "SOGoTool.h"
|
|
|
|
|
|
|
|
@interface SOGoToolDumpDefaults : SOGoTool
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SOGoToolDumpDefaults
|
|
|
|
|
|
|
|
+ (NSString *) command
|
|
|
|
{
|
|
|
|
return @"dump-defaults";
|
|
|
|
}
|
|
|
|
+ (NSString *) description
|
|
|
|
{
|
2017-06-16 19:25:15 +02:00
|
|
|
return @"prints the sogod GNUstep domain configuration as a property list";
|
2012-12-10 20:10:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) usage
|
|
|
|
{
|
|
|
|
fprintf (stderr, "dump-defaults [-f <filename>|[all]]\n\n"
|
|
|
|
" Prints the sogod GNUstep domain configuration as a property list.\n"
|
|
|
|
" The output should be suitable for inclusion as /etc/sogo/sogo.conf\n"
|
|
|
|
"\n"
|
|
|
|
" If 'all' is specified, prints all defaults and their current values.\n"
|
|
|
|
" This might be useful to see the list of all known configuration parameters.\n"
|
|
|
|
"\n"
|
|
|
|
" If '-f filename' is specified, reads a property list in xml format\n"
|
|
|
|
" and outputs it in OpenStep format. To some, this is more readable than XML.\n"
|
|
|
|
" Note that the output might need further editing to be used as sogo.conf.\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) processDefaults: (BOOL)allDefaults
|
|
|
|
{
|
|
|
|
NSDictionary *defaultsDict;
|
2017-05-30 20:44:43 +02:00
|
|
|
NSUserDefaults *ud;
|
2012-12-10 20:10:56 +01:00
|
|
|
NSData *plistData;
|
|
|
|
|
|
|
|
ud = [NSUserDefaults standardUserDefaults];
|
|
|
|
if (allDefaults)
|
|
|
|
{
|
|
|
|
/* grab the running defaults - contains EVERYTHING */
|
|
|
|
defaultsDict = [ud dictionaryRepresentation];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* grab only the sogod domain */
|
|
|
|
defaultsDict = [ud persistentDomainForName: @"sogod"];
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:44:43 +02:00
|
|
|
if (!defaultsDict)
|
|
|
|
return @"No defaults found. Try to use -f.";
|
|
|
|
|
2013-01-29 16:31:16 +01:00
|
|
|
plistData = [NSPropertyListSerialization dataFromPropertyList: (id) defaultsDict
|
2012-12-10 20:10:56 +01:00
|
|
|
format: NSPropertyListOpenStepFormat
|
2013-01-29 16:31:16 +01:00
|
|
|
errorDescription: 0 ];
|
2012-12-10 20:10:56 +01:00
|
|
|
return [[[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (NSString *) defaultsFromFilename: (NSString *)filename
|
|
|
|
{
|
|
|
|
NSData *rawData, *plistRawData, *plistDataOpenStep;
|
2013-01-29 16:31:16 +01:00
|
|
|
NSString *errstr;
|
2012-12-10 20:10:56 +01:00
|
|
|
|
|
|
|
rawData = [NSData dataWithContentsOfFile: filename];
|
|
|
|
if (rawData == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Cannot read configuration from file '%s'",
|
|
|
|
[filename UTF8String]);
|
|
|
|
return @"";
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:31:16 +01:00
|
|
|
plistRawData = [NSPropertyListSerialization propertyListFromData: rawData
|
|
|
|
mutabilityOption: 0
|
2012-12-10 20:10:56 +01:00
|
|
|
format: 0
|
2013-01-29 16:31:16 +01:00
|
|
|
errorDescription: &errstr];
|
2012-12-10 20:10:56 +01:00
|
|
|
if (plistRawData == nil)
|
|
|
|
{
|
2013-01-29 16:31:16 +01:00
|
|
|
NSLog(@"Error converting '%s' to plist: %@", [filename UTF8String], errstr);
|
2012-12-10 20:10:56 +01:00
|
|
|
return @"";
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:31:16 +01:00
|
|
|
plistDataOpenStep = [NSPropertyListSerialization dataFromPropertyList: (id) plistRawData
|
2012-12-10 20:10:56 +01:00
|
|
|
format: NSPropertyListOpenStepFormat
|
2013-01-29 16:31:16 +01:00
|
|
|
errorDescription: &errstr ];
|
2012-12-10 20:10:56 +01:00
|
|
|
if (!plistDataOpenStep)
|
|
|
|
{
|
2013-01-29 16:31:16 +01:00
|
|
|
NSLog(@"Error converting plist to OpenStep format: %@", errstr);
|
2012-12-10 20:10:56 +01:00
|
|
|
return @"";
|
|
|
|
}
|
|
|
|
|
|
|
|
return [[[NSString alloc] initWithData:plistDataOpenStep encoding:NSUTF8StringEncoding] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) run
|
|
|
|
{
|
|
|
|
BOOL rc;
|
|
|
|
NSString *filename, *output;
|
|
|
|
|
|
|
|
rc = YES;
|
|
|
|
|
|
|
|
/* FIXME: this should really be replaced by getopt(3) or some equivalent */
|
|
|
|
if ([arguments count])
|
|
|
|
{
|
|
|
|
if ([arguments count] == 2 &&
|
|
|
|
[[arguments objectAtIndex: 0] isEqualToString: @"-f"])
|
|
|
|
{
|
|
|
|
filename = [arguments objectAtIndex: 1];
|
|
|
|
output = [self defaultsFromFilename: filename];
|
|
|
|
}
|
|
|
|
else if ([[arguments objectAtIndex: 0] isEqualToString: @"all"])
|
|
|
|
output = [self processDefaults: YES];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self usage];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
output = [self processDefaults: NO];
|
|
|
|
|
|
|
|
printf("%s\n", [output UTF8String]);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|