diff --git a/ChangeLog b/ChangeLog index 301a15ef8..c76f406d4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2009-08-13 Wolfgang Sourdeau + + * Tools/SOGoToolRemoveDoubles.m: new "sogo-tool" utility method + implementing the old "sogo-contacts-removedoubles" utility. + + * Tools/SOGoToolCheckDoubles.m: new "sogo-tool" utility method + implementing the old "sogo-contacts-checkdoubles" utility. + + * Tools/SOGoTool.m: new class module implementing the base class + for all management modules for "sogo-tool". + + * Tools/sogo-tool.m: new command-line tool that offers a generic + utility for SOGo administration. + 2009-08-12 Francis Lachapelle * UI/MainUI/Version (SUBMINOR_VERSION): 1.0.4 release. diff --git a/Tools/GNUmakefile b/Tools/GNUmakefile index 2b16724cd..e65588288 100644 --- a/Tools/GNUmakefile +++ b/Tools/GNUmakefile @@ -4,19 +4,16 @@ include ../config.make include $(GNUSTEP_MAKEFILES)/common.make include ../Version -SOGO_CHECKDOUBLES = sogo-contacts-checkdoubles -$(SOGO_CHECKDOUBLES)_INSTALL_DIR = $(SOGO_ADMIN_TOOLS) -$(SOGO_CHECKDOUBLES)_OBJC_FILES += \ - sogo-contacts-checkdoubles.m +SOGO_TOOL = sogo-tool +$(SOGO_TOOL)_INSTALL_DIR = $(SOGO_ADMIN_TOOLS) +$(SOGO_TOOL)_OBJC_FILES += \ + sogo-tool.m \ + \ + SOGoTool.m \ + SOGoToolCheckDoubles.m \ + SOGoToolRemoveDoubles.m -SOGO_REMOVEDOUBLES = sogo-contacts-removedoubles -$(SOGO_REMOVEDOUBLES)_INSTALL_DIR = $(SOGO_ADMIN_TOOLS) -$(SOGO_REMOVEDOUBLES)_OBJC_FILES += \ - sogo-contacts-removedoubles.m -$(SOGO_REMOVEDOUBLES)_LIB_DIRS = \ - -L../SOPE/NGCards/$(GNUSTEP_OBJ_DIR)/ -lNGCards - -TOOL_NAME = $(SOGO_CHECKDOUBLES) $(SOGO_REMOVEDOUBLES) +TOOL_NAME = $(SOGO_TOOL) -include GNUmakefile.preamble include $(GNUSTEP_MAKEFILES)/tool.make diff --git a/Tools/GNUmakefile.preamble b/Tools/GNUmakefile.preamble index 85e2e5d24..680469320 100644 --- a/Tools/GNUmakefile.preamble +++ b/Tools/GNUmakefile.preamble @@ -7,10 +7,12 @@ ADDITIONAL_CPPFLAGS += \ -DSOGO_LIBDIR="\"$(SOGO_LIBDIR)\"" ADDITIONAL_INCLUDE_DIRS += \ - -I../SOPE/ -D_GNU_SOURCE + -D_GNU_SOURCE -I../SOPE/ -I../SoObjects/ ADDITIONAL_LIB_DIRS += \ - -L../SOPE/GDLContentStore/$(GNUSTEP_OBJ_DIR)/ -lGDLContentStore + -L../SoObjects/SOGo/$(GNUSTEP_OBJ_DIR)/ -lSOGo \ + -L../SOPE/GDLContentStore/$(GNUSTEP_OBJ_DIR)/ -lGDLContentStore \ + -L../SOPE/NGCards/$(GNUSTEP_OBJ_DIR)/ -lNGCards SYSTEM_LIB_DIR += -L/usr/local/lib -L/usr/lib diff --git a/Tools/SOGoTool.h b/Tools/SOGoTool.h new file mode 100644 index 000000000..2bb43eb1f --- /dev/null +++ b/Tools/SOGoTool.h @@ -0,0 +1,48 @@ +/* SOGoTool.h - this file is part of SOGo + * + * Copyright (C) 2009 Inverse inc. + * + * Author: Wolfgang Sourdeau + * + * 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. + */ + +#ifndef SOGOTOOL_H +#define SOGOTOOL_H + +#import + +// void printStringOnChannel(int channel, NSString * format, ...); + +@interface SOGoTool : NSObject +{ + BOOL verbose; + NSArray *arguments; +} + ++ (NSString *) command; ++ (NSString *) description; + ++ (BOOL) runToolWithArguments: (NSArray *) arguments + verbose: (BOOL) verbose; + +- (void) setArguments: (NSArray *) newArguments; +- (void) setVerbose: (BOOL) newVerbose; +- (BOOL) run; + +@end + +#endif /* SOGOTOOL_H */ diff --git a/Tools/SOGoTool.m b/Tools/SOGoTool.m new file mode 100644 index 000000000..2049977dd --- /dev/null +++ b/Tools/SOGoTool.m @@ -0,0 +1,85 @@ +/* SOGoTool.m - this file is part of SOGo + * + * Copyright (C) 2009 Inverse inc. + * + * Author: Wolfgang Sourdeau + * + * 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 "SOGoTool.h" + +@implementation SOGoTool + ++ (NSString *) command +{ + return nil; +} + ++ (NSString *) description +{ + return nil; +} + ++ (BOOL) runToolWithArguments: (NSArray *) arguments + verbose: (BOOL) verbose +{ + SOGoTool *instance; + + instance = [self new]; + [instance autorelease]; + + [instance setArguments: arguments]; + [instance setVerbose: verbose]; + + return [instance run]; +} + +- (id) init +{ + if ((self = [super init])) + { + arguments = nil; + verbose = NO; + } + + return self; +} + +- (void) setArguments: (NSArray *) newArguments +{ + ASSIGN (arguments, newArguments); +} + +- (void) setVerbose: (BOOL) newVerbose +{ + verbose = newVerbose; +} + +- (void) dealloc +{ + [arguments release]; + [super dealloc]; +} + +- (BOOL) run +{ + return NO; +} + +@end diff --git a/Tools/SOGoToolCheckDoubles.h b/Tools/SOGoToolCheckDoubles.h new file mode 100644 index 000000000..a8e44140f --- /dev/null +++ b/Tools/SOGoToolCheckDoubles.h @@ -0,0 +1,35 @@ +/* SOGoToolCheckDoubles.h - this file is part of SOGo + * + * Copyright (C) 2009 Inverse inc. + * + * Author: Wolfgang Sourdeau + * + * 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. + */ + +#ifndef SOGOTOOLCHECKDOUBLES_H +#define SOGOTOOLCHECKDOUBLES_H + +#import "SOGoTool.h" + +@interface SOGoToolCheckDoubles : SOGoTool +{ + unsigned int warningLimit; +} + +@end + +#endif /* SOGOTOOLCHECKDOUBLES_H */ diff --git a/Tools/sogo-contacts-checkdoubles.m b/Tools/SOGoToolCheckDoubles.m similarity index 75% rename from Tools/sogo-contacts-checkdoubles.m rename to Tools/SOGoToolCheckDoubles.m index 39ddd6217..88c2bf52c 100644 --- a/Tools/sogo-contacts-checkdoubles.m +++ b/Tools/SOGoToolCheckDoubles.m @@ -1,4 +1,4 @@ -/* sogo-ab-checkdoubles.m - this file is part of SOGo +/* SOGoToolCheckDoubles.m - this file is part of SOGo * * Copyright (C) 2009 Inverse inc. * @@ -25,7 +25,6 @@ - make sure we don't end up using 3000 different channels because of the amount of tables we need to wander */ -#import #import #import #import @@ -38,38 +37,41 @@ #import #import -typedef void (*NSUserDefaultsInitFunction) (); +#import "SOGoToolCheckDoubles.h" -static unsigned int ContactsCountWarningLimit = 1000; +@implementation SOGoToolCheckDoubles -// static void -// NSLogInhibitor (NSString *message) -// { -// } ++ (NSString *) command +{ + return @"check-doubles"; +} -@interface SOGoDoublesChecker : NSObject ++ (NSString *) description +{ + return @"check user addressbooks with duplicate contacts"; +} -- (BOOL) run; - -@end - -@implementation SOGoDoublesChecker - -+ (void) initialize +- (id) init { NSUserDefaults *ud; - NSNumber *warningLimit; + NSNumber *SOGoContactsCountWarningLimit; -// _NSLog_printf_handler = NSLogInhibitor; + if ((self = [super init])) + { + ud = [NSUserDefaults standardUserDefaults]; + SOGoContactsCountWarningLimit + = [ud objectForKey: @"SOGoContactsCountWarningLimit"]; + if (SOGoContactsCountWarningLimit) + warningLimit + = [SOGoContactsCountWarningLimit unsignedIntValue]; + else + warningLimit = 1000; - ud = [NSUserDefaults standardUserDefaults]; - [ud addSuiteNamed: @"sogod"]; - warningLimit = [ud objectForKey: @"SOGoContactsCountWarningLimit"]; - if (warningLimit) - ContactsCountWarningLimit = [warningLimit unsignedIntValue]; + fprintf (stderr, "The warning limit for folder records is set at %u\n", + warningLimit); + } - fprintf (stderr, "The warning limit for folder records is set at %u\n", - ContactsCountWarningLimit); + return self; } - (void) processIndexResults: (EOAdaptorChannel *) channel @@ -89,7 +91,7 @@ static unsigned int ContactsCountWarningLimit = 1000; if (currentFolder) { recordsCount = [currentFolder recordsCountByExcludingDeleted: YES]; - if (recordsCount > ContactsCountWarningLimit) + if (recordsCount > warningLimit) { fprintf (stderr, "'%s' (id: '%s'), of '%s': %u entries\n", [[folderRow objectForKey: @"c_foldername"] @@ -127,7 +129,8 @@ static unsigned int ContactsCountWarningLimit = 1000; ex = [channel evaluateExpressionX: sqlString]; if (ex) { - fprintf (stderr, "an exception occured during the fetching of folder names"); + fprintf (stderr, "an exception occured during the fetching of" + " folder names"); [ex raise]; rc = NO; } @@ -163,26 +166,3 @@ static unsigned int ContactsCountWarningLimit = 1000; } @end - -int -main (int argc, char **argv, char **env) -{ - NSAutoreleasePool *pool; - SOGoDoublesChecker *checker; - int rc; - - rc = 0; - - pool = [NSAutoreleasePool new]; - - checker = [SOGoDoublesChecker new]; - if ([checker run]) - rc = 0; - else - rc = -1; - [checker release]; - - [pool release]; - - return rc; -} diff --git a/Tools/SOGoToolRemoveDoubles.h b/Tools/SOGoToolRemoveDoubles.h new file mode 100644 index 000000000..ad83f666a --- /dev/null +++ b/Tools/SOGoToolRemoveDoubles.h @@ -0,0 +1,32 @@ +/* SOGoToolRemoveDoubles.h - this file is part of SOGo + * + * Copyright (C) 2009 Inverse inc. + * + * Author: Wolfgang Sourdeau + * + * 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. + */ + +#ifndef SOGOTOOLREMOVEDOUBLES_H +#define SOGOTOOLREMOVEDOUBLES_H + +#import "SOGoTool.h" + +@interface SOGoToolRemoveDoubles : SOGoTool + +@end + +#endif /* SOGOTOOLREMOVEDOUBLES_H */ diff --git a/Tools/sogo-contacts-removedoubles.m b/Tools/SOGoToolRemoveDoubles.m similarity index 92% rename from Tools/sogo-contacts-removedoubles.m rename to Tools/SOGoToolRemoveDoubles.m index c4726926b..a7ae5f31a 100644 --- a/Tools/sogo-contacts-removedoubles.m +++ b/Tools/SOGoToolRemoveDoubles.m @@ -1,4 +1,4 @@ -/* sogo-ab-removedoubles.m - this file is part of SOGo +/* SOGoToolRemoveDoubles.m - this file is part of SOGo * * Copyright (C) 2009 Inverse inc. * @@ -20,7 +20,8 @@ * Boston, MA 02111-1307, USA. */ -/* TODO: NSUserDefaults bootstrapping for using different backends */ +/* TODO: + - show usage */ #include @@ -45,7 +46,7 @@ #import #import -typedef void (*NSUserDefaultsInitFunction) (); +#import "SOGoToolRemoveDoubles.h" @interface NGVList (RemoveDoubles) @@ -73,17 +74,16 @@ typedef void (*NSUserDefaultsInitFunction) (); @end -@interface SOGoDoublesRemover : NSObject -@end +@implementation SOGoToolRemoveDoubles -@implementation SOGoDoublesRemover - -+ (void) initialize ++ (NSString *) command { - NSUserDefaults *ud; + return @"remove-doubles"; +} - ud = [NSUserDefaults standardUserDefaults]; - [ud addSuiteNamed: @"sogod"]; ++ (NSString *) description +{ + return @"remove duplicate contacts from the specified user addressbook"; } - (void) feedDoubleEmails: (NSMutableDictionary *) doubleEmails @@ -522,47 +522,27 @@ typedef void (*NSUserDefaultsInitFunction) (); return rc; } -@end - -static void -Usage (const char *name) +- (void) usage { - const char *slash, *start; - - slash = strrchr (name, '/'); - if (slash) - start = slash + 1; - else - start = name; - fprintf (stderr, "Usage: %s USER FOLDER\n\n" + fprintf (stderr, "Usage: remove-doubles USER FOLDER\n\n" " USER the owner of the contact folder\n" - " FOLDER the id of the folder to clean up\n", - start); + " FOLDER the id of the folder to clean up\n"); } -int -main (int argc, char **argv, char **env) +- (BOOL) run { - NSAutoreleasePool *pool; - SOGoDoublesRemover *remover; - int rc; + BOOL rc; - rc = -1; - - pool = [NSAutoreleasePool new]; - - if (argc > 2) - { - remover = [SOGoDoublesRemover new]; - if ([remover runWithFolder: [NSString stringWithFormat: @"%s", argv[2]] - andUser: [NSString stringWithFormat: @"%s", argv[1]]]) - rc = 0; - [remover release]; - } + if ([arguments count] == 2) + rc = [self runWithFolder: [arguments objectAtIndex: 1] + andUser: [arguments objectAtIndex: 0]]; else - Usage (argv[0]); - - [pool release]; + { + [self usage]; + rc = NO; + } return rc; } + +@end diff --git a/Tools/sogo-tool.m b/Tools/sogo-tool.m new file mode 100644 index 000000000..871c84392 --- /dev/null +++ b/Tools/sogo-tool.m @@ -0,0 +1,238 @@ +/* sogo-tool.m - this file is part of SOGo + * + * Copyright (C) 2009 Inverse inc. + * + * Author: Wolfgang Sourdeau + * + * 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 "SOGoTool.h" + +@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"]; + toolsEnum = [[tools allKeys] objectEnumerator]; + while ((command = [toolsEnum nextObject])) + { + currentTool = [tools objectForKey: command]; + [helpString appendFormat: @"\t%@\t-- %@\n", + 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; + } + + return NO; +} + +@end + +int +main (int argc, char **argv, char **env) +{ + NSAutoreleasePool *pool; + SOGoToolDispatcher *dispatcher; + NSUserDefaults *ud; + int rc; + + rc = 0; + + pool = [NSAutoreleasePool new]; + + ud = [NSUserDefaults standardUserDefaults]; + [ud addSuiteNamed: @"sogod"]; + + dispatcher = [SOGoToolDispatcher new]; + if ([dispatcher run]) + rc = 0; + else + rc = -1; + [dispatcher release]; + + [pool release]; + + return rc; +}