2009-08-13 20:31:15 +02:00
|
|
|
/* sogo-tool.m - this file is part of SOGo
|
|
|
|
*
|
2015-08-03 09:48:06 +02:00
|
|
|
* Copyright (C) 2009-2015 Inverse inc.
|
2009-08-13 20:31:15 +02: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/NSAutoreleasePool.h>
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
|
|
|
#import <Foundation/NSObject.h>
|
|
|
|
#import <Foundation/NSProcessInfo.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2010-04-28 19:58:19 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
2009-08-13 20:31:15 +02:00
|
|
|
|
2011-07-13 20:55:16 +02:00
|
|
|
#import <SOGo/SOGoSystemDefaults.h>
|
|
|
|
|
2009-08-13 20:31:15 +02:00
|
|
|
#import "SOGoTool.h"
|
|
|
|
|
2009-08-13 21:48:03 +02:00
|
|
|
/* TODO:
|
|
|
|
- help for modules
|
|
|
|
- have a help syntax/mechanism similar to the one in monotone */
|
|
|
|
|
2009-08-13 20:31:15 +02:00
|
|
|
@interface SOGoToolDispatcher : NSObject
|
|
|
|
{
|
|
|
|
NSMutableDictionary *tools;
|
|
|
|
BOOL verboseMode;
|
|
|
|
BOOL helpMode;
|
|
|
|
NSString *tool;
|
|
|
|
NSArray *toolArguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) run;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SOGoToolDispatcher
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
tools = [NSMutableDictionary new];
|
|
|
|
helpMode = NO;
|
|
|
|
verboseMode = NO;
|
|
|
|
tool = nil;
|
|
|
|
toolArguments = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[tools release];
|
|
|
|
[tool release];
|
|
|
|
[toolArguments release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerTool: (NSString *) command
|
|
|
|
withClass: (NSString *) className
|
|
|
|
andDescription: (NSString *) description
|
|
|
|
{
|
|
|
|
NSArray *toolData;
|
|
|
|
|
|
|
|
toolData = [NSArray arrayWithObjects: className, description, nil];
|
|
|
|
[tools setObject: toolData forKey: command];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) parseArguments: (NSArray *) arguments
|
|
|
|
{
|
|
|
|
BOOL error;
|
|
|
|
int count, max;
|
|
|
|
NSString *argument;
|
|
|
|
|
|
|
|
error = NO;
|
|
|
|
|
|
|
|
max = [arguments count];
|
|
|
|
count = 1;
|
|
|
|
while (!error && !tool && count < max)
|
|
|
|
{
|
|
|
|
argument = [arguments objectAtIndex: count];
|
|
|
|
if ([argument isEqualToString: @"-v"]
|
|
|
|
|| [argument isEqualToString: @"--verbose"])
|
|
|
|
verboseMode = YES;
|
|
|
|
else if ([argument isEqualToString: @"-h"]
|
|
|
|
|| [argument isEqualToString: @"--help"])
|
|
|
|
helpMode = YES;
|
|
|
|
else if ([argument hasPrefix: @"-"])
|
|
|
|
{
|
|
|
|
error = YES;
|
|
|
|
helpMode = YES;
|
|
|
|
NSLog (@"Invalid command line parameter: '%@'", argument);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSIGN (tool,
|
|
|
|
[[tools objectForKey: argument] objectAtIndex: 0]);
|
|
|
|
count++;
|
|
|
|
if (count < max)
|
|
|
|
{
|
|
|
|
max -= count;
|
|
|
|
toolArguments = [arguments
|
|
|
|
subarrayWithRange: NSMakeRange (count, max)];
|
|
|
|
[toolArguments retain];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) help
|
|
|
|
{
|
|
|
|
NSMutableString *helpString;
|
|
|
|
NSEnumerator *toolsEnum;
|
|
|
|
NSString *command;
|
|
|
|
NSArray *currentTool;
|
|
|
|
|
|
|
|
helpString = [NSMutableString stringWithString: @"sogo-tool [-v|--verbose]"
|
|
|
|
@" [-h|--help]"
|
|
|
|
@" command [argument1] ...\n"];
|
|
|
|
[helpString appendString: @" -v, --verbose\tenable verbose mode\n"];
|
|
|
|
[helpString appendString: @" -h, --help\tdisplay this help information\n\n"];
|
|
|
|
[helpString appendString: @" argument1, ...\targuments passed to the"
|
|
|
|
@" specified command\n\n"];
|
|
|
|
[helpString appendString: @" Available commands:\n"];
|
2012-04-04 22:14:52 +02:00
|
|
|
toolsEnum = [[[tools allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectEnumerator];
|
2009-08-13 20:31:15 +02:00
|
|
|
while ((command = [toolsEnum nextObject]))
|
|
|
|
{
|
|
|
|
currentTool = [tools objectForKey: command];
|
2012-04-04 22:14:52 +02:00
|
|
|
[helpString appendFormat: @"\t%-20@-- %@\n",
|
2009-08-13 20:31:15 +02:00
|
|
|
command, [currentTool objectAtIndex: 1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLog (helpString);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerTools
|
|
|
|
{
|
|
|
|
NSEnumerator *toolsEnum;
|
|
|
|
Class currentTool;
|
|
|
|
NSString *command, *description;
|
|
|
|
|
|
|
|
toolsEnum = [GSObjCAllSubclassesOfClass ([SOGoTool class]) objectEnumerator];
|
|
|
|
while ((currentTool = [toolsEnum nextObject]))
|
|
|
|
{
|
|
|
|
command = [currentTool command];
|
|
|
|
if (command)
|
|
|
|
{
|
|
|
|
description = [currentTool description];
|
|
|
|
[self registerTool: command
|
|
|
|
withClass: NSStringFromClass (currentTool)
|
|
|
|
andDescription: description];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) run
|
|
|
|
{
|
|
|
|
NSProcessInfo *processInfo;
|
|
|
|
NSArray *arguments;
|
|
|
|
Class toolClass;
|
|
|
|
NSString *toolsList;
|
|
|
|
BOOL rc;
|
|
|
|
|
|
|
|
[self registerTools];
|
|
|
|
|
|
|
|
processInfo = [NSProcessInfo processInfo];
|
|
|
|
arguments = [processInfo arguments];
|
|
|
|
[self parseArguments: arguments];
|
|
|
|
if (helpMode || !tool)
|
|
|
|
{
|
|
|
|
rc = YES;
|
|
|
|
[self help];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tool)
|
|
|
|
{
|
|
|
|
toolClass = NSClassFromString (tool);
|
|
|
|
if (toolClass)
|
|
|
|
rc = [toolClass runToolWithArguments: toolArguments
|
|
|
|
verbose: verboseMode];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = NO;
|
|
|
|
toolsList = [[tools allKeys] componentsJoinedByString: @", "];
|
|
|
|
NSLog (@"No tool named '%@'. Available tools are:\n\t%@",
|
|
|
|
tool, toolsList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = NO;
|
|
|
|
}
|
|
|
|
|
2010-03-31 22:11:30 +02:00
|
|
|
return rc;
|
2009-08-13 20:31:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2010-04-28 19:58:19 +02:00
|
|
|
static void
|
2012-10-23 19:42:02 +02:00
|
|
|
setupUserDefaults ()
|
2010-04-28 19:58:19 +02:00
|
|
|
{
|
|
|
|
NSMutableDictionary *defaultsOverrides;
|
2012-10-23 19:42:02 +02:00
|
|
|
NSUserDefaults *ud;
|
|
|
|
|
|
|
|
ud = [NSUserDefaults standardUserDefaults];
|
2010-04-28 19:58:19 +02:00
|
|
|
|
|
|
|
defaultsOverrides = [NSMutableDictionary new];
|
|
|
|
[defaultsOverrides setObject: [NSNumber numberWithInt: 0]
|
|
|
|
forKey: @"SOGoLDAPQueryLimit"];
|
|
|
|
[defaultsOverrides setObject: [NSNumber numberWithInt: 0]
|
|
|
|
forKey: @"SOGoLDAPQueryTimeout"];
|
2012-10-23 19:42:02 +02:00
|
|
|
[ud setVolatileDomain: defaultsOverrides
|
|
|
|
forName: @"sogo-tool-overrides"];
|
|
|
|
[ud addSuiteNamed: @"sogo-tool-overrides"];
|
2010-04-28 19:58:19 +02:00
|
|
|
[defaultsOverrides release];
|
|
|
|
}
|
|
|
|
|
2009-08-13 20:31:15 +02:00
|
|
|
int
|
|
|
|
main (int argc, char **argv, char **env)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool;
|
|
|
|
SOGoToolDispatcher *dispatcher;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
pool = [NSAutoreleasePool new];
|
|
|
|
|
2011-07-13 20:55:16 +02:00
|
|
|
[SOGoSystemDefaults sharedSystemDefaults];
|
2012-10-23 19:42:02 +02:00
|
|
|
setupUserDefaults ();
|
2009-08-13 20:31:15 +02:00
|
|
|
|
2011-02-17 21:06:12 +01:00
|
|
|
dispatcher = [SOGoToolDispatcher new];
|
|
|
|
if ([dispatcher run])
|
|
|
|
rc = 0;
|
2009-08-13 20:31:15 +02:00
|
|
|
else
|
2011-02-17 21:06:12 +01:00
|
|
|
rc = -1;
|
|
|
|
[dispatcher release];
|
2009-08-13 20:31:15 +02:00
|
|
|
[pool release];
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|