From bdc5ce7cd9cd150226a5a996ddc0a06863214902 Mon Sep 17 00:00:00 2001 From: Jean Raby Date: Mon, 10 Dec 2012 14:10:56 -0500 Subject: [PATCH] Add sogo-tool dump-defaults Add sogo-tool dump-defaults, a tool to convert an existing sogo configuration in ~sogo/GNUstep/Defaults/.GNUstepDefaults to /etc/sogo/sogo.conf format. The output will be in OpenStep property list format, which is much easier to read and edit, at least for humans. While there, sort the Tools list --- Tools/GNUmakefile | 13 +-- Tools/SOGoToolDumpDefaults.m | 163 +++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 Tools/SOGoToolDumpDefaults.m diff --git a/Tools/GNUmakefile b/Tools/GNUmakefile index 99235cb8d..c5b56a1fc 100644 --- a/Tools/GNUmakefile +++ b/Tools/GNUmakefile @@ -12,14 +12,15 @@ $(SOGO_TOOL)_OBJC_FILES += \ \ SOGoTool.m \ SOGoToolBackup.m \ - SOGoToolRestore.m \ SOGoToolCheckDoubles.m \ - SOGoToolRemoveDoubles.m \ - SOGoToolRemove.m \ - SOGoToolRenameUser.m \ - SOGoToolUserPreferences.m \ + SOGoToolDumpDefaults.m \ SOGoToolExpireAutoReply.m \ - SOGoToolExpireUserSessions.m + SOGoToolExpireUserSessions.m \ + SOGoToolRemove.m \ + SOGoToolRemoveDoubles.m \ + SOGoToolRenameUser.m \ + SOGoToolRestore.m \ + SOGoToolUserPreferences.m TOOL_NAME += $(SOGO_TOOL) ### diff --git a/Tools/SOGoToolDumpDefaults.m b/Tools/SOGoToolDumpDefaults.m new file mode 100644 index 000000000..8f5770804 --- /dev/null +++ b/Tools/SOGoToolDumpDefaults.m @@ -0,0 +1,163 @@ +/* SOGoToolDumpDefaults.m - this file is part of SOGo + * + * Copyright (C) 2012 Inverse inc. + * + * Author: Jean Raby + * + * 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 +#import +#import +#import +#import +#import +#import +#import +#import + +#import "SOGoTool.h" + +@interface SOGoToolDumpDefaults : SOGoTool +@end + +@implementation SOGoToolDumpDefaults + ++ (void) initialize +{ +} + ++ (NSString *) command +{ + return @"dump-defaults"; +} ++ (NSString *) description +{ + return @"Prints the sogod GNUstep domain configuration as a property list"; +} + +- (void) usage +{ + fprintf (stderr, "dump-defaults [-f |[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 +{ + NSUserDefaults *ud; + NSDictionary *defaultsDict; + 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"]; + } + + plistData = [NSPropertyListSerialization dataWithPropertyList: (id) defaultsDict + format: NSPropertyListOpenStepFormat + options: 0 + error: 0 ]; + return [[[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding] autorelease]; +} + + +- (NSString *) defaultsFromFilename: (NSString *)filename +{ + NSData *rawData, *plistRawData, *plistDataOpenStep; + NSError *err; + + rawData = [NSData dataWithContentsOfFile: filename]; + if (rawData == nil) + { + NSLog(@"Cannot read configuration from file '%s'", + [filename UTF8String]); + return @""; + } + + plistRawData = [NSPropertyListSerialization propertyListWithData: rawData + options: 0 + format: 0 + error: &err]; + if (plistRawData == nil) + { + NSLog(@"Error converting '%s' to plist: %@", [filename UTF8String], err); + return @""; + } + + plistDataOpenStep = [NSPropertyListSerialization dataWithPropertyList: (id) plistRawData + format: NSPropertyListOpenStepFormat + options: 0 + error: &err ]; + if (!plistDataOpenStep) + { + NSLog(@"Error converting plist to OpenStep format: %@", err); + 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 +