]> err.no Git - sope/blob - sope-mime/samples/imap_tool.m
fixed samples makefile for compilation, some cleanups
[sope] / sope-mime / samples / imap_tool.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
22 #include "common.h"
23 #include "NGImap4.h"
24
25 static void usage(void) {
26   fprintf(stderr, "usage: imap_tool"
27           " -login <login>"
28           " -pwd <pwd>"
29           " [-host <host>]\n");
30   exit(1);
31 }
32
33 static void runAction(NGImap4Client *client, NSString *action, NSString *arg) {
34   if ([action length] == 0)
35         return;
36         
37   if ([action isEqualToString:@"select"]) {
38     result = [client select:arg];
39   }
40   else if ([action isEqualToString:@"thread"]) {
41     result = [client threadBySubject:[arg boolValue] charset:nil];
42   }
43   else if ([action isEqualToString:@"list"]) {
44     result = [client list:arg pattern:@"*"];
45   }
46   else if ([action isEqualToString:@"fetch"]) {
47     NSArray *args;
48
49     args = [arg componentsSeparatedByString:@":"];
50     result = [client fetchFrom:[[args objectAtIndex:0] intValue]
51                      to:[[args objectAtIndex:1] intValue]
52                      parts:[args subarrayWithRange:
53                                    NSMakeRange(2,[args count] - 2)]];
54   }
55   
56   NSLog(@"action: %@:%@ : %@", action, arg, result);
57 }
58
59 static void run(void) {
60   NSString       *login, *pwd, *host;
61   NSUserDefaults *ud;
62   NGImap4Client  *client;
63   int            cnt;
64   
65   ud = [NSUserDefaults standardUserDefaults];
66
67   if ((login = [ud stringForKey:@"login"]) == nil)
68     usage();
69   if ((pwd = [ud stringForKey:@"pwd"]) == nil)
70     usage();
71   if ((host = [ud stringForKey:@"host"]) == nil)
72     host = @"localhost";
73   
74   client = [NGImap4Client clientWithHost:host];
75   
76   NSLog(@"attempt login '%@' ...", login);
77   [client login:login password:pwd];
78   NSLog(@"  got client: %@", client);
79   
80   for (cnt = 0; YES; cnt++) {
81       NSString *action;
82       NSString *arg;
83       id       result;
84       
85       action = [ud stringForKey:
86                      [NSString stringWithFormat:@"action_%d", cnt]]; 
87       arg = [ud stringForKey:
88                   [NSString stringWithFormat:@"arg_%d", cnt]];
89       if (!runAction(client, action, arg))
90         break;
91   }
92 }
93
94 int main(int argc, char **argv, char **env) {
95   NSAutoreleasePool *pool;
96   
97 #if LIB_FOUNDATION_LIBRARY
98   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
99 #endif
100
101   pool = [[NSAutoreleasePool alloc] init];
102   run();
103   [pool release];
104   return 0;
105 }
106