2014-12-08 16:45:34 +01:00
|
|
|
/* SOGoToolManageEAS.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 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/NSCharacterSet.h>
|
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
|
|
|
#import <Foundation/NSURL.h>
|
|
|
|
|
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
|
|
|
#import <NGObjWeb/WOApplication.h>
|
|
|
|
|
|
|
|
#import <SOGo/NSString+Utilities.h>
|
|
|
|
#import <SOGo/SOGoProductLoader.h>
|
|
|
|
#import "SOGo/SOGoCredentialsFile.h"
|
|
|
|
#import <SOGo/SOGoUser.h>
|
|
|
|
#import <SOGo/SOGoUserFolder.h>
|
|
|
|
#import <SOGo/SOGoUserDefaults.h>
|
|
|
|
#import <SOGo/SOGoSystemDefaults.h>
|
|
|
|
#import <SOGo/SOGoUserSettings.h>
|
|
|
|
#import <Mailer/SOGoMailAccounts.h>
|
|
|
|
#import <Mailer/SOGoMailAccount.h>
|
|
|
|
|
|
|
|
#import <SOGo/SOGoCacheGCSObject.h>
|
|
|
|
|
|
|
|
|
|
|
|
#import "SOGoTool.h"
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
ManageEASUnknown = -1,
|
|
|
|
ManageEASListDevices = 0,
|
|
|
|
ManageEASListFolders = 2,
|
|
|
|
ManageEASResetDevice = 3,
|
|
|
|
ManageEASRestFolder = 4,
|
|
|
|
} SOGoManageEASCommand;
|
|
|
|
|
|
|
|
@interface SOGoToolManageEAS : SOGoTool
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SOGoToolManageEAS
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *) command
|
|
|
|
{
|
|
|
|
return @"manage-eas";
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *) description
|
|
|
|
{
|
|
|
|
return @"manage EAS folders";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) usage
|
|
|
|
{
|
|
|
|
fprintf (stderr, "manage-eas listdevices|resetdevice|resetfolder user <devinceId | folderId> \n\n"
|
|
|
|
" user the user of whom to reset the whole device or a single folder\n"
|
|
|
|
" Examples:\n"
|
|
|
|
" sogo-tool manage-eas listdevices janedoe\n"
|
|
|
|
" sogo-tool manage-eas listfolders janedoe androidc316986417\n"
|
|
|
|
" sogo-tool manage-eas resetdevice janedoe androidc316986417\n"
|
|
|
|
" sogo-tool manage-eas resetfolder janedow androidc316986417+folderlala-dada-sasa_7a13_1a2386e0_e\n") ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (SOGoManageEASCommand) _cmdFromString: (NSString *) theString
|
|
|
|
{
|
|
|
|
if ([theString length] > 2)
|
|
|
|
{
|
|
|
|
if ([theString caseInsensitiveCompare: @"listdevices"] == NSOrderedSame)
|
|
|
|
return ManageEASListDevices;
|
|
|
|
else if ([theString caseInsensitiveCompare: @"listfolders"] == NSOrderedSame)
|
|
|
|
return ManageEASListFolders;
|
|
|
|
else if ([theString caseInsensitiveCompare: @"resetdevice"] == NSOrderedSame)
|
|
|
|
return ManageEASResetDevice;
|
|
|
|
else if ([theString caseInsensitiveCompare: @"resetfolder"] == NSOrderedSame)
|
|
|
|
return ManageEASRestFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ManageEASUnknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) run
|
|
|
|
{
|
2014-12-16 16:35:32 +01:00
|
|
|
NSString *urlString, *deviceId, *userId;
|
|
|
|
NSMutableString *ocFSTableName;
|
2014-12-08 16:45:34 +01:00
|
|
|
SOGoCacheGCSObject *oc, *foc;
|
|
|
|
NSURL *folderTableURL;
|
|
|
|
NSMutableArray *parts;
|
2014-12-09 13:21:34 +01:00
|
|
|
NSArray *entries;
|
|
|
|
id cacheEntry;
|
2014-12-16 16:35:32 +01:00
|
|
|
|
|
|
|
SOGoManageEASCommand cmd;
|
2014-12-09 13:21:34 +01:00
|
|
|
int i, max;
|
2014-12-16 16:35:32 +01:00
|
|
|
BOOL rc;
|
2014-12-08 16:45:34 +01:00
|
|
|
|
|
|
|
max = [sanitizedArguments count];
|
|
|
|
rc = NO;
|
|
|
|
|
|
|
|
if (max > 1)
|
|
|
|
{
|
|
|
|
SOGoUser *user;
|
|
|
|
|
|
|
|
cmd = [self _cmdFromString: [sanitizedArguments objectAtIndex: 0]];
|
|
|
|
|
|
|
|
userId = [sanitizedArguments objectAtIndex: 1];
|
|
|
|
|
|
|
|
user = [SOGoUser userWithLogin: userId];
|
|
|
|
|
|
|
|
if (![user loginInDomain])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
urlString = [[user domainDefaults] folderInfoURL];
|
|
|
|
parts = [[urlString componentsSeparatedByString: @"/"]
|
|
|
|
mutableCopy];
|
|
|
|
[parts autorelease];
|
|
|
|
if ([parts count] == 5)
|
|
|
|
{
|
|
|
|
/* If "OCSFolderInfoURL" is properly configured, we must have 5
|
|
|
|
parts in this url. We strip the '-' character in case we have
|
|
|
|
this in the domain part - like foo@bar-zot.com */
|
2014-12-16 16:35:32 +01:00
|
|
|
ocFSTableName = [NSMutableString stringWithFormat: @"sogo_cache_folder_%@",
|
2015-04-14 13:08:21 +02:00
|
|
|
[[user login] asCSSIdentifier]];
|
2014-12-16 16:35:32 +01:00
|
|
|
[ocFSTableName replaceOccurrencesOfString: @"-"
|
|
|
|
withString: @"_"
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(0, [ocFSTableName length])];
|
2014-12-08 16:45:34 +01:00
|
|
|
[parts replaceObjectAtIndex: 4 withObject: ocFSTableName];
|
|
|
|
folderTableURL
|
|
|
|
= [NSURL URLWithString: [parts componentsJoinedByString: @"/"]];
|
|
|
|
[folderTableURL retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case ManageEASListDevices:
|
|
|
|
oc = [SOGoCacheGCSObject objectWithName: @"0" inContainer: nil];
|
|
|
|
[oc setObjectType: ActiveSyncGlobalCacheObject];
|
|
|
|
|
|
|
|
[oc setTableUrl: folderTableURL];
|
2014-12-09 13:21:34 +01:00
|
|
|
entries = [oc cacheEntriesForDeviceId: nil newerThanVersion: -1];
|
2014-12-08 16:45:34 +01:00
|
|
|
|
2014-12-09 13:21:34 +01:00
|
|
|
for (i = 0; i < [entries count]; i++)
|
|
|
|
{
|
|
|
|
cacheEntry = [entries objectAtIndex: i];
|
|
|
|
fprintf(stdout,"%s\n", [[cacheEntry substringFromIndex: 1] UTF8String]);
|
|
|
|
}
|
2014-12-08 16:45:34 +01:00
|
|
|
|
|
|
|
rc = YES;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ManageEASListFolders:
|
|
|
|
if (max > 2)
|
|
|
|
{
|
|
|
|
/* value specified on command line */
|
|
|
|
deviceId = [sanitizedArguments objectAtIndex: 2];
|
|
|
|
|
|
|
|
oc = [SOGoCacheGCSObject objectWithName: @"0" inContainer: nil];
|
|
|
|
[oc setObjectType: ActiveSyncFolderCacheObject];
|
|
|
|
|
|
|
|
[oc setTableUrl: folderTableURL];
|
2014-12-09 13:21:34 +01:00
|
|
|
entries = [oc cacheEntriesForDeviceId: deviceId newerThanVersion: -1];
|
2014-12-08 16:45:34 +01:00
|
|
|
|
2014-12-09 13:21:34 +01:00
|
|
|
for (i = 0; i < [entries count]; i++)
|
|
|
|
{
|
|
|
|
cacheEntry = [entries objectAtIndex: i];
|
|
|
|
fprintf(stdout,"Folder Key: %s\n", [[cacheEntry substringFromIndex: 1] UTF8String]);
|
2014-12-08 16:45:34 +01:00
|
|
|
|
2014-12-09 13:21:34 +01:00
|
|
|
foc = [SOGoCacheGCSObject objectWithName: [cacheEntry substringFromIndex: 1] inContainer: nil];
|
|
|
|
[foc setObjectType: ActiveSyncFolderCacheObject];
|
|
|
|
[foc setTableUrl: folderTableURL];
|
2014-12-08 16:45:34 +01:00
|
|
|
|
2014-12-09 13:21:34 +01:00
|
|
|
[foc reloadIfNeeded];
|
2014-12-08 16:45:34 +01:00
|
|
|
|
2014-12-09 13:21:34 +01:00
|
|
|
fprintf(stdout, " Folder Name: %s\n\n", [[[foc properties] objectForKey: @"displayName"] UTF8String]);
|
2014-12-08 16:45:34 +01:00
|
|
|
|
2014-12-09 13:21:34 +01:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stdout, " metadata Name: %s\n\n", [[[foc properties] description] UTF8String]);
|
2014-12-08 16:45:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\nERROR: deviceId not specified\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ManageEASResetDevice:
|
|
|
|
if (max > 2)
|
|
|
|
{
|
|
|
|
/* value specified on command line */
|
|
|
|
deviceId = [sanitizedArguments objectAtIndex: 2];
|
|
|
|
oc = [SOGoCacheGCSObject objectWithName: deviceId inContainer: nil];
|
|
|
|
[oc setObjectType: ActiveSyncGlobalCacheObject];
|
|
|
|
[oc setTableUrl: folderTableURL];
|
|
|
|
|
|
|
|
[oc reloadIfNeeded];
|
|
|
|
if ([oc isNew]) {
|
|
|
|
fprintf(stderr, "ERROR: Device with ID '%s' not found\n", [deviceId UTF8String]);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableString *sql;
|
|
|
|
|
2015-11-05 18:29:26 +01:00
|
|
|
sql = [NSMutableString stringWithFormat: @"DELETE FROM %@ WHERE c_path like '/%@'", [oc tableName], deviceId];
|
2014-12-08 16:45:34 +01:00
|
|
|
|
|
|
|
[oc performBatchSQLQueries: [NSArray arrayWithObject: sql]];
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\nERROR: deviceId not specified\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ManageEASRestFolder:
|
|
|
|
if (max > 2)
|
|
|
|
{
|
|
|
|
/* value specified on command line */
|
|
|
|
deviceId = [sanitizedArguments objectAtIndex: 2];
|
|
|
|
|
|
|
|
//if ([deviceId rangeOfString: @"+"].location == NSNotFound) {
|
|
|
|
// fprintf(stderr, "ERROR: Deviceid invalid folder \"%@\" not found\n", deviceId);
|
|
|
|
// return rc;
|
|
|
|
//}
|
|
|
|
|
|
|
|
oc = [SOGoCacheGCSObject objectWithName: deviceId inContainer: nil];
|
|
|
|
[oc setObjectType: ActiveSyncFolderCacheObject];
|
|
|
|
[oc setTableUrl: folderTableURL];
|
|
|
|
|
|
|
|
[oc reloadIfNeeded];
|
|
|
|
|
|
|
|
if ([oc isNew]) {
|
|
|
|
fprintf(stderr, "ERROR: Folder with ID \"%s\" not found\n", [deviceId UTF8String]);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
[[oc properties] removeObjectForKey: @"SyncKey"];
|
|
|
|
[[oc properties] removeObjectForKey: @"SyncCache"];
|
|
|
|
[[oc properties] removeObjectForKey: @"DateCache"];
|
|
|
|
[[oc properties] removeObjectForKey: @"MoreAvailable"];
|
2016-03-28 14:43:56 +02:00
|
|
|
[[oc properties] removeObjectForKey: @"FirstIdInCache"];
|
|
|
|
[[oc properties] removeObjectForKey: @"LastIdInCache"];
|
2014-12-08 16:45:34 +01:00
|
|
|
|
|
|
|
[oc save];
|
|
|
|
rc = YES;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\nERROR: folderId not specified\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ManageEASUnknown:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
[self usage];
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|