]> err.no Git - sope/blob - sope-core/samples/ImapListTool.m
renamed packages as discussed in the developer list
[sope] / sope-core / samples / ImapListTool.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "ImapListTool.h"
24 #include "common.h"
25 #include <NGImap4/NGImap4.h>
26 #include <EOControl/EOControl.h>
27 #include <NGImap4/NGImap4FileManager.h>
28 #include <NGImap4/NGImap4Message.h>
29
30 @implementation ImapListTool
31
32 /* output */
33
34 - (BOOL)outputResultsAsList:(NSArray *)dirContents
35   fileManager:(NGImap4FileManager *)fm part:(NSString *)_part
36 {
37   unsigned i, count;
38   NSString *path = [fm currentDirectoryPath];
39   
40   for (i = 0, count = [dirContents count]; i < count; i++) {
41     NSString     *cpath, *apath;
42     NSDictionary *info;
43     NSString     *mid;
44         
45     if (!self->useDataSource) {
46       cpath = [dirContents objectAtIndex:i];
47       apath = [path stringByAppendingPathComponent:cpath];
48         
49       info = [fm fileAttributesAtPath:apath
50                  traverseLink:NO];
51     }
52     else {
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"];
58     }
59     
60     mid = [[info valueForKey:@"NSFileIdentifier"] description];
61     if ([mid length] > 39) {
62       mid = [mid substringToIndex:37];
63       mid = [mid stringByAppendingString:@"..."];
64     }
65         
66     /* id uid date name */
67     if (_part) {
68       printf("%10d ",
69              [[fm contentsAtPath:[info valueForKey:@"NSFilePath"]
70                   part:_part] length]);
71     }
72     printf("%-40s  %8s  %8i  %-32s %s",
73            [mid cString],
74            [[info valueForKey:NSFileOwnerAccountName]      cString],
75            [[info valueForKey:NSFileSize] intValue],
76            [[[info valueForKey:NSFileModificationDate]
77               description] cString],
78            [apath cString]);
79
80     if ([[info valueForKey:NSFileType]
81           isEqualToString:NSFileTypeDirectory])
82       printf("/\n");
83     else
84       printf("\n");
85
86     
87   }
88   return YES;
89 }
90
91 - (BOOL)outputResultsAsXML:(NSArray *)_dirContents
92   fileManager:(NGFileManager *)_fm 
93 {
94   NSLog(@"XML output not implemented ...");
95   return NO;
96 }
97
98 - (BOOL)outputResults:(NSArray *)dirContents
99   fileManager:(NGImap4FileManager *)fm part:(NSString *)_part
100 {
101   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
102   NSAutoreleasePool *pool;
103   NSString *out;
104   BOOL result;
105   
106   pool = [[NSAutoreleasePool alloc] init];
107   
108   out = [ud stringForKey:@"out"];
109   if ([out length] == 0)
110     result = YES;
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];
115   else {
116     NSLog(@"unknown output module: %@", out);
117     result = NO;
118   }
119   [pool release];
120   return result;
121 }
122
123 /* ops */
124
125 - (void)processFile:(NSString *)path fileManager:(NGImap4FileManager *)fm
126   part:(NSString *)_part
127 {
128   /* a file */
129   NSData   *contents;
130   NSString *s;
131
132   if (_part) {
133     if ((contents = [fm contentsAtPath:path part:_part]) == nil) {
134       NSLog(@"could not get content of message: '%@'", path);
135     }
136     else {
137       s = [[NSString alloc] initWithData:contents
138                             encoding:[NSString defaultCStringEncoding]];
139       printf("%s\n", [s cString]);
140       [s release];
141     }
142   }
143   else {
144     NGImap4Message *contents;
145   
146     if ((contents = [fm messageAtPath:path]) == nil) {
147       NSLog(@"could not get message at path: '%@'", path);
148     }
149     else {
150 #if 0
151       s = [[NSString alloc] initWithData:contents
152                             encoding:[NSString defaultCStringEncoding]];
153       printf("%s\n", [s cString]);
154       [s release];
155 #else
156       printf("%s\n", [[contents description] cString]);
157       printf("%s\n", [[[contents bodyStructure] description] cString]);
158     
159 #endif
160     }
161   }
162 }
163
164 - (void)processFolder:(NSString *)path fileManager:(NGImap4FileManager *)fm
165   part:(NSString *)_part
166 {
167   NSAutoreleasePool *pool;
168   NSTimeInterval startTime, endTime;
169   unsigned int   startSize, endSize;
170   NSProcessInfo  *pi = [NSProcessInfo processInfo];
171   NSArray        *dirContents;
172   unsigned       i;
173   EODataSource   *ds;
174   
175   if (![fm changeCurrentDirectoryPath:path]) {
176     NSLog(@"%s: could not change to directory: '%@'", path);
177   }
178
179   ds = self->useDataSource
180     ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
181     : nil;
182   
183   /* pre fetches */
184   
185   for (i = 0; i < self->preloops; i++ ) {
186     NSAutoreleasePool *pool;
187     
188     startTime = [[NSDate date] timeIntervalSince1970];
189     startSize = [pi virtualMemorySize];
190     
191     /* fetch */
192     
193     pool = [[NSAutoreleasePool alloc] init];
194     {
195       ds = self->useDataSource
196         ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
197         : nil;
198   
199       dirContents = (!self->useDataSource)
200         ? [fm directoryContentsAtPath:path]
201         : [ds fetchObjects];
202     }
203     [pool release];
204     
205     /* statistics */
206     
207     endSize = [pi virtualMemorySize];
208     endTime = [[NSDate date] timeIntervalSince1970];
209     
210     if (self->stats) {
211       fprintf(stderr, 
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);
219     }
220   }
221   
222   /* actual fetch */
223
224   startTime = [[NSDate date] timeIntervalSince1970];
225   startSize = [pi virtualMemorySize];
226
227   pool = [[NSAutoreleasePool alloc] init];
228   
229   ds = self->useDataSource
230     ? [(id<NGFileManagerDataSources>)fm dataSourceAtPath:path]
231     : nil;
232   
233   dirContents = (!self->useDataSource)
234     ? [fm directoryContentsAtPath:path]
235     : [ds fetchObjects];
236   
237   dirContents = [dirContents retain];
238   [pool release];
239   dirContents = [dirContents autorelease];
240   
241   /* statistics */
242       
243   endSize = [pi virtualMemorySize];
244   endTime = [[NSDate date] timeIntervalSince1970];
245   
246   if (self->stats) {
247     fprintf(stderr, 
248             "parsing time: %.3fs, "
249             "vmem-diff: %8i (%4iK,%4iM), vmem: %8i (%4iK,%4iM))\n", 
250             (endTime-startTime), 
251             (endSize - startSize), 
252             (endSize - startSize) / 1024, 
253             (endSize - startSize) / 1024 / 1024, 
254             endSize, endSize/1024, endSize/1024/1024);
255   }
256   
257   /* output */
258   [self outputResults:dirContents fileManager:fm part:_part];
259 }
260
261 /*
262   path /INBOX/1233?part=1.2
263 */
264
265
266 - (void)processPath:(NSString *)path fileManager:(NGImap4FileManager *)fm {
267   BOOL    isDir;
268   NSArray *array;
269   NSString *part;
270
271   array = [path componentsSeparatedByString:@"?"];
272
273   if ([array count] > 1) {
274     path = [array objectAtIndex:0];
275     part = [[[array objectAtIndex:1] componentsSeparatedByString:@"="]
276                      lastObject]; 
277   }
278   else
279     part = nil;
280   
281   if (![fm fileExistsAtPath:path isDirectory:&isDir]) {
282     NSLog(@"file/directory does not exist: %@", path);
283     return;
284   }
285     
286   if (isDir) {
287     [self processFolder:path fileManager:fm part:part];
288   }
289   else {
290     [self processFile:path fileManager:fm part:part];
291   }
292 }
293
294 /* tool operation */
295  
296 - (int)usage {
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");
307   return 1;
308 }
309
310 - (int)runWithArguments:(NSArray *)_args {
311   NGImap4FileManager *fm;
312   NSUserDefaults *ud;
313   int            i;
314   
315   _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
316   if ([_args count] == 0)
317     return [self usage];
318   
319   ud = [NSUserDefaults standardUserDefaults];
320   
321   self->useDataSource = [ud boolForKey:@"datasource"];
322   self->stats         = [ud boolForKey:@"statistics"];
323   self->preloops      = [ud integerForKey:@"preloops"];
324   
325   if ((fm = [self fileManager]) == nil) {
326     NSLog(@"could not open IMAP connection (got no filemanager)");
327     return 2;
328   }
329   
330 #if 1
331   NSLog(@"IMAP: %@", fm);
332 #endif
333   
334   for (i = 0; i < [_args count]; i++) {
335     [self processPath:[_args objectAtIndex:i] fileManager:fm];
336   }
337   
338   return 0;
339 }
340
341 @end /* ImapListTool */