2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "ImapListTool.h"
25 #include <NGImap4/NGImap4.h>
26 #include <EOControl/EOControl.h>
27 #include <NGImap4/NGImap4FileManager.h>
28 #include <NGImap4/NGImap4Message.h>
30 @implementation ImapListTool
34 - (BOOL)outputResultsAsList:(NSArray *)dirContents
35 fileManager:(NGImap4FileManager *)fm part:(NSString *)_part
38 NSString *path = [fm currentDirectoryPath];
40 for (i = 0, count = [dirContents count]; i < count; i++) {
41 NSString *cpath, *apath;
45 if (!self->useDataSource) {
46 cpath = [dirContents objectAtIndex:i];
47 apath = [path stringByAppendingPathComponent:cpath];
49 info = [fm fileAttributesAtPath:apath
53 info = [dirContents objectAtIndex:i];
54 cpath = [NSString stringWithFormat:@"%u", [(id)info uid]];
55 apath = [path stringByAppendingPathComponent:cpath];
56 //cpath = [info valueForKey:@"NSFileName"];
57 //apath = [info valueForKey:@"NSFilePath"];
60 mid = [[info valueForKey:@"NSFileIdentifier"] description];
61 if ([mid length] > 39) {
62 mid = [mid substringToIndex:37];
63 mid = [mid stringByAppendingString:@"..."];
66 /* id uid date name */
69 [[fm contentsAtPath:[info valueForKey:@"NSFilePath"]
72 printf("%-40s %8s %8i %-32s %s",
74 [[info valueForKey:NSFileOwnerAccountName] cString],
75 [[info valueForKey:NSFileSize] intValue],
76 [[[info valueForKey:NSFileModificationDate]
77 description] cString],
80 if ([[info valueForKey:NSFileType]
81 isEqualToString:NSFileTypeDirectory])
91 - (BOOL)outputResultsAsXML:(NSArray *)_dirContents
92 fileManager:(NGFileManager *)_fm
94 NSLog(@"XML output not implemented ...");
98 - (BOOL)outputResults:(NSArray *)dirContents
99 fileManager:(NGImap4FileManager *)fm part:(NSString *)_part
101 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
102 NSAutoreleasePool *pool;
106 pool = [[NSAutoreleasePool alloc] init];
108 out = [ud stringForKey:@"out"];
109 if ([out length] == 0)
111 else if ([out isEqualToString:@"xml"])
112 result = [self outputResultsAsXML:dirContents fileManager:fm];
113 else if ([out isEqualToString:@"ls"])
114 result = [self outputResultsAsList:dirContents fileManager:fm part:_part];
116 NSLog(@"unknown output module: %@", out);
125 - (void)processFile:(NSString *)path fileManager:(NGImap4FileManager *)fm
126 part:(NSString *)_part
133 if ((contents = [fm contentsAtPath:path part:_part]) == nil) {
134 NSLog(@"could not get content of message: '%@'", path);
137 s = [[NSString alloc] initWithData:contents
138 encoding:[NSString defaultCStringEncoding]];
139 printf("%s\n", [s cString]);
144 NGImap4Message *contents;
146 if ((contents = [fm messageAtPath:path]) == nil) {
147 NSLog(@"could not get message at path: '%@'", path);
151 s = [[NSString alloc] initWithData:contents
152 encoding:[NSString defaultCStringEncoding]];
153 printf("%s\n", [s cString]);
156 printf("%s\n", [[contents description] cString]);
157 printf("%s\n", [[[contents bodyStructure] description] cString]);
164 - (void)processFolder:(NSString *)path fileManager:(NGImap4FileManager *)fm
165 part:(NSString *)_part
167 NSAutoreleasePool *pool;
168 NSTimeInterval startTime, endTime;
169 unsigned int startSize, endSize;
170 NSProcessInfo *pi = [NSProcessInfo processInfo];
171 NSArray *dirContents;
175 if (![fm changeCurrentDirectoryPath:path]) {
176 NSLog(@"%s: could not change to directory: '%@'", path);
179 ds = self->useDataSource
180 ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
185 for (i = 0; i < self->preloops; i++ ) {
186 NSAutoreleasePool *pool;
188 startTime = [[NSDate date] timeIntervalSince1970];
189 startSize = [pi virtualMemorySize];
193 pool = [[NSAutoreleasePool alloc] init];
195 ds = self->useDataSource
196 ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
199 dirContents = (!self->useDataSource)
200 ? [fm directoryContentsAtPath:path]
207 endSize = [pi virtualMemorySize];
208 endTime = [[NSDate date] timeIntervalSince1970];
212 "parsing time [%2i]: %.3fs, "
213 "vmem-diff: %8i (%4iK,%4iM), vmem: %8i (%4iK,%4iM))\n",
214 i, (endTime-startTime),
215 (endSize - startSize),
216 (endSize - startSize) / 1024,
217 (endSize - startSize) / 1024 / 1024,
218 endSize, endSize/1024, endSize/1024/1024);
224 startTime = [[NSDate date] timeIntervalSince1970];
225 startSize = [pi virtualMemorySize];
227 pool = [[NSAutoreleasePool alloc] init];
229 ds = self->useDataSource
230 ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
233 dirContents = (!self->useDataSource)
234 ? [fm directoryContentsAtPath:path]
237 dirContents = [dirContents retain];
239 dirContents = [dirContents autorelease];
243 endSize = [pi virtualMemorySize];
244 endTime = [[NSDate date] timeIntervalSince1970];
248 "parsing time: %.3fs, "
249 "vmem-diff: %8i (%4iK,%4iM), vmem: %8i (%4iK,%4iM))\n",
251 (endSize - startSize),
252 (endSize - startSize) / 1024,
253 (endSize - startSize) / 1024 / 1024,
254 endSize, endSize/1024, endSize/1024/1024);
258 [self outputResults:dirContents fileManager:fm part:_part];
262 path /INBOX/1233?part=1.2
266 - (void)processPath:(NSString *)path fileManager:(NGImap4FileManager *)fm {
271 array = [path componentsSeparatedByString:@"?"];
273 if ([array count] > 1) {
274 path = [array objectAtIndex:0];
275 part = [[[array objectAtIndex:1] componentsSeparatedByString:@"="]
281 if (![fm fileExistsAtPath:path isDirectory:&isDir]) {
282 NSLog(@"file/directory does not exist: %@", path);
287 [self processFolder:path fileManager:fm part:part];
290 [self processFile:path fileManager:fm part:part];
297 fprintf(stderr, "usage: imapls <pathes>?part=<part>\n");
298 fprintf(stderr, "usage: imapls <pathes>\n");
299 fprintf(stderr, " -url <url>\n");
300 fprintf(stderr, " -user <login>\n");
301 fprintf(stderr, " -password <pwd>\n");
302 fprintf(stderr, " -host <host>\n");
303 fprintf(stderr, " -datasource YES|NO\n");
304 fprintf(stderr, " -out ls|xml\n");
305 fprintf(stderr, " -statistics YES|NO\n");
306 fprintf(stderr, " -preloops <n>\n");
310 - (int)runWithArguments:(NSArray *)_args {
311 NGImap4FileManager *fm;
315 _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
316 if ([_args count] == 0)
319 ud = [NSUserDefaults standardUserDefaults];
321 self->useDataSource = [ud boolForKey:@"datasource"];
322 self->stats = [ud boolForKey:@"statistics"];
323 self->preloops = [ud integerForKey:@"preloops"];
325 if ((fm = [self fileManager]) == nil) {
326 NSLog(@"could not open IMAP connection (got no filemanager)");
331 NSLog(@"IMAP: %@", fm);
334 for (i = 0; i < [_args count]; i++) {
335 [self processPath:[_args objectAtIndex:i] fileManager:fm];
341 @end /* ImapListTool */