2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "ImapListTool.h"
24 #include <NGImap4/NGImap4.h>
25 #include <EOControl/EOControl.h>
26 #include <NGImap4/NGImap4FileManager.h>
27 #include <NGImap4/NGImap4Message.h>
29 @implementation ImapListTool
33 - (BOOL)outputResultsAsList:(NSArray *)dirContents
34 fileManager:(NGImap4FileManager *)fm part:(NSString *)_part
37 NSString *path = [fm currentDirectoryPath];
39 for (i = 0, count = [dirContents count]; i < count; i++) {
40 NSString *cpath, *apath;
44 if (!self->useDataSource) {
45 cpath = [dirContents objectAtIndex:i];
46 apath = [path stringByAppendingPathComponent:cpath];
48 info = [fm fileAttributesAtPath:apath
52 info = [dirContents objectAtIndex:i];
53 cpath = [NSString stringWithFormat:@"%u", [(id)info uid]];
54 apath = [path stringByAppendingPathComponent:cpath];
55 //cpath = [info valueForKey:@"NSFileName"];
56 //apath = [info valueForKey:@"NSFilePath"];
59 mid = [[info valueForKey:@"NSFileIdentifier"] description];
60 if ([mid length] > 39) {
61 mid = [mid substringToIndex:37];
62 mid = [mid stringByAppendingString:@"..."];
65 /* id uid date name */
68 [[fm contentsAtPath:[info valueForKey:@"NSFilePath"]
71 printf("%-40s %8s %8i %-32s %s",
73 [[info valueForKey:NSFileOwnerAccountName] cString],
74 [[info valueForKey:NSFileSize] intValue],
75 [[[info valueForKey:NSFileModificationDate]
76 description] cString],
79 if ([[info valueForKey:NSFileType]
80 isEqualToString:NSFileTypeDirectory])
90 - (BOOL)outputResultsAsXML:(NSArray *)_dirContents
91 fileManager:(NGFileManager *)_fm
93 NSLog(@"XML output not implemented ...");
97 - (BOOL)outputResults:(NSArray *)dirContents
98 fileManager:(NGImap4FileManager *)fm part:(NSString *)_part
100 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
101 NSAutoreleasePool *pool;
105 pool = [[NSAutoreleasePool alloc] init];
107 out = [ud stringForKey:@"out"];
108 if ([out length] == 0)
110 else if ([out isEqualToString:@"xml"])
111 result = [self outputResultsAsXML:dirContents fileManager:fm];
112 else if ([out isEqualToString:@"ls"])
113 result = [self outputResultsAsList:dirContents fileManager:fm part:_part];
115 NSLog(@"unknown output module: %@", out);
124 - (void)processFile:(NSString *)path fileManager:(NGImap4FileManager *)fm
125 part:(NSString *)_part
132 if ((contents = [fm contentsAtPath:path part:_part]) == nil) {
133 NSLog(@"could not get content of message: '%@'", path);
136 s = [[NSString alloc] initWithData:contents
137 encoding:[NSString defaultCStringEncoding]];
138 printf("%s\n", [s cString]);
143 NGImap4Message *contents;
145 if ((contents = [fm messageAtPath:path]) == nil) {
146 NSLog(@"could not get message at path: '%@'", path);
150 s = [[NSString alloc] initWithData:contents
151 encoding:[NSString defaultCStringEncoding]];
152 printf("%s\n", [s cString]);
155 printf("%s\n", [[contents description] cString]);
156 printf("%s\n", [[[contents bodyStructure] description] cString]);
163 - (void)processFolder:(NSString *)path fileManager:(NGImap4FileManager *)fm
164 part:(NSString *)_part
166 NSAutoreleasePool *pool;
167 NSTimeInterval startTime, endTime;
168 unsigned int startSize, endSize;
169 NSProcessInfo *pi = [NSProcessInfo processInfo];
170 NSArray *dirContents;
174 if (![fm changeCurrentDirectoryPath:path]) {
175 NSLog(@"%s: could not change to directory: '%@'", path);
178 ds = self->useDataSource
179 ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
184 for (i = 0; i < self->preloops; i++ ) {
185 NSAutoreleasePool *pool;
187 startTime = [[NSDate date] timeIntervalSince1970];
188 startSize = [pi virtualMemorySize];
192 pool = [[NSAutoreleasePool alloc] init];
194 ds = self->useDataSource
195 ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
198 dirContents = (!self->useDataSource)
199 ? [fm directoryContentsAtPath:path]
206 endSize = [pi virtualMemorySize];
207 endTime = [[NSDate date] timeIntervalSince1970];
211 "parsing time [%2i]: %.3fs, "
212 "vmem-diff: %8i (%4iK,%4iM), vmem: %8i (%4iK,%4iM))\n",
213 i, (endTime-startTime),
214 (endSize - startSize),
215 (endSize - startSize) / 1024,
216 (endSize - startSize) / 1024 / 1024,
217 endSize, endSize/1024, endSize/1024/1024);
223 startTime = [[NSDate date] timeIntervalSince1970];
224 startSize = [pi virtualMemorySize];
226 pool = [[NSAutoreleasePool alloc] init];
228 ds = self->useDataSource
229 ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
232 dirContents = (!self->useDataSource)
233 ? [fm directoryContentsAtPath:path]
236 dirContents = [dirContents retain];
238 dirContents = [dirContents autorelease];
242 endSize = [pi virtualMemorySize];
243 endTime = [[NSDate date] timeIntervalSince1970];
247 "parsing time: %.3fs, "
248 "vmem-diff: %8i (%4iK,%4iM), vmem: %8i (%4iK,%4iM))\n",
250 (endSize - startSize),
251 (endSize - startSize) / 1024,
252 (endSize - startSize) / 1024 / 1024,
253 endSize, endSize/1024, endSize/1024/1024);
257 [self outputResults:dirContents fileManager:fm part:_part];
261 path /INBOX/1233?part=1.2
265 - (void)processPath:(NSString *)path fileManager:(NGImap4FileManager *)fm {
270 array = [path componentsSeparatedByString:@"?"];
272 if ([array count] > 1) {
273 path = [array objectAtIndex:0];
274 part = [[[array objectAtIndex:1] componentsSeparatedByString:@"="]
280 if (![fm fileExistsAtPath:path isDirectory:&isDir]) {
281 NSLog(@"file/directory does not exist: %@", path);
286 [self processFolder:path fileManager:fm part:part];
289 [self processFile:path fileManager:fm part:part];
296 fprintf(stderr, "usage: imapls <pathes>?part=<part>\n");
297 fprintf(stderr, "usage: imapls <pathes>\n");
298 fprintf(stderr, " -url <url>\n");
299 fprintf(stderr, " -user <login>\n");
300 fprintf(stderr, " -password <pwd>\n");
301 fprintf(stderr, " -host <host>\n");
302 fprintf(stderr, " -datasource YES|NO\n");
303 fprintf(stderr, " -out ls|xml\n");
304 fprintf(stderr, " -statistics YES|NO\n");
305 fprintf(stderr, " -preloops <n>\n");
309 - (int)runWithArguments:(NSArray *)_args {
310 NGImap4FileManager *fm;
314 _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
315 if ([_args count] == 0)
318 ud = [NSUserDefaults standardUserDefaults];
320 self->useDataSource = [ud boolForKey:@"datasource"];
321 self->stats = [ud boolForKey:@"statistics"];
322 self->preloops = [ud integerForKey:@"preloops"];
324 if ((fm = [self fileManager]) == nil) {
325 NSLog(@"could not open IMAP connection (got no filemanager)");
330 NSLog(@"IMAP: %@", fm);
333 for (i = 0; i < [_args count]; i++) {
334 [self processPath:[_args objectAtIndex:i] fileManager:fm];
340 @end /* ImapListTool */