File: SOGoToolDumpDefaults.m

package info (click to toggle)
sogo 5.12.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,828 kB
  • sloc: objc: 122,550; javascript: 75,288; sh: 2,352; ansic: 2,055; perl: 861; python: 777; sql: 307; makefile: 187; php: 43; xml: 27
file content (153 lines) | stat: -rw-r--r-- 4,710 bytes parent folder | download | duplicates (8)
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
/* SOGoToolDumpDefaults.m - this file is part of SOGo
 *
 * Copyright (C) 2012-2017 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/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
{
  return @"prints the sogod GNUstep domain configuration as a property list";
}

- (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;
  NSUserDefaults *ud;
  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"];
    }

  if (!defaultsDict)
    return @"No defaults found. Try to use -f.";

  plistData = [NSPropertyListSerialization dataFromPropertyList: (id) defaultsDict
                                         format: NSPropertyListOpenStepFormat
                               errorDescription: 0 ];
  return [[[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding] autorelease];
}


- (NSString *) defaultsFromFilename: (NSString *)filename
{
  NSData *rawData, *plistRawData, *plistDataOpenStep;
  NSString *errstr;

  rawData = [NSData dataWithContentsOfFile: filename];
  if (rawData == nil)
    {
      NSLog(@"Cannot read configuration from file '%s'",
                                [filename UTF8String]);
      return @"";
    }

  plistRawData = [NSPropertyListSerialization propertyListFromData: rawData
                                                  mutabilityOption: 0
                                                            format: 0
                                                  errorDescription: &errstr];
  if (plistRawData == nil)
    {
      NSLog(@"Error converting '%s' to plist: %@", [filename UTF8String], errstr);
      return @"";
    }
  
  plistDataOpenStep = [NSPropertyListSerialization dataFromPropertyList: (id) plistRawData
                                         format: NSPropertyListOpenStepFormat
                               errorDescription: &errstr ];
  if (!plistDataOpenStep)
    {
      NSLog(@"Error converting plist to OpenStep format: %@", errstr);
      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