summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tests/option-parser.c
Unidiff
Diffstat (limited to 'kmicromail/libetpan/tests/option-parser.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/tests/option-parser.c234
1 files changed, 234 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tests/option-parser.c b/kmicromail/libetpan/tests/option-parser.c
new file mode 100644
index 0000000..57a1597
--- a/dev/null
+++ b/kmicromail/libetpan/tests/option-parser.c
@@ -0,0 +1,234 @@
1#define _GNU_SOURCE
2#include <getopt.h>
3
4#include <stdlib.h>
5#include <string.h>
6#include <limits.h>
7
8#include <libetpan/libetpan.h>
9
10#include "option-parser.h"
11
12/*
13 options
14
15 --driver (pop3|imap|nntp|mbox|mh|maildir) -d
16
17 default driver is mbox
18
19 --server {server-name} -s
20 --port {port-number} -p
21 --tls -t
22 --starttls -x
23 --user {login} -u
24 --password {password} -v
25 --path {mailbox} -l
26 --apop -a
27 --cache {directory} -c
28 --flags {directory} -f
29*/
30
31struct storage_name {
32 int id;
33 char * name;
34};
35
36static struct storage_name storage_tab[] = {
37 {POP3_STORAGE, "pop3"},
38 {IMAP_STORAGE, "imap"},
39 {NNTP_STORAGE, "nntp"},
40 {MBOX_STORAGE, "mbox"},
41 {MH_STORAGE, "mh"},
42 {MAILDIR_STORAGE, "maildir"},
43};
44
45static int get_driver(char * name)
46{
47 int driver_type;
48 unsigned int i;
49
50 driver_type = -1;
51 for(i = 0 ; i < sizeof(storage_tab) / sizeof(struct storage_name) ; i++) {
52 if (strcasecmp(name, storage_tab[i].name) == 0) {
53 driver_type = i;
54 break;
55 }
56 }
57
58 return driver_type;
59}
60
61int parse_options(int argc, char ** argv,
62 int * driver,
63 char ** server, int * port, int * connection_type,
64 char ** user, char ** password, int * auth_type,
65 char ** path, char ** cache_directory,
66 char ** flags_directory)
67{
68 int index;
69 static struct option long_options[] = {
70 {"driver", 1, 0, 'd'},
71 {"server", 1, 0, 's'},
72 {"port", 1, 0, 'p'},
73 {"tls", 0, 0, 't'},
74 {"starttls", 0, 0, 'x'},
75 {"user", 1, 0, 'u'},
76 {"password", 1, 0, 'v'},
77 {"path", 1, 0, 'l'},
78 {"apop", 0, 0, 'a'},
79 {"cache", 1, 0, 'c'},
80 {"flags", 1, 0, 'f'},
81 };
82 int r;
83 char location[PATH_MAX];
84 char * env_user;
85
86 index = 0;
87
88 * driver = MBOX_STORAGE;
89 * server = NULL;
90 * port = 0;
91 * connection_type = CONNECTION_TYPE_PLAIN;
92 * user = NULL;
93 * password = NULL;
94 * auth_type = POP3_AUTH_TYPE_PLAIN;
95 env_user = getenv("USER");
96 if (env_user != NULL) {
97 snprintf(location, PATH_MAX, "/var/mail/%s", env_user);
98 * path = strdup(location);
99 }
100 else
101 * path = NULL;
102 * cache_directory = NULL;
103 * flags_directory = NULL;
104
105 while (1) {
106 r = getopt_long(argc, argv, "d:s:p:txu:v:l:ac:f:", long_options, &index);
107
108 if (r == -1)
109 break;
110
111 switch (r) {
112 case 'd':
113 * driver = get_driver(optarg);
114 break;
115 case 's':
116 if (* server != NULL)
117 free(* server);
118 * server = strdup(optarg);
119 break;
120 case 'p':
121 * port = strtoul(optarg, NULL, 10);
122 break;
123 case 't':
124 * connection_type = CONNECTION_TYPE_TLS;
125 break;
126 case 'x':
127 * connection_type = CONNECTION_TYPE_STARTTLS;
128 break;
129 case 'u':
130 if (* user != NULL)
131 free(* user);
132 * user = strdup(optarg);
133 break;
134 case 'v':
135 if (* password != NULL)
136 free(* password);
137 * password = strdup(optarg);
138 break;
139 case 'l':
140 if (* path != NULL)
141 free(* path);
142 * path = strdup(optarg);
143 break;
144 case 'a':
145 * auth_type = POP3_AUTH_TYPE_APOP;
146 break;
147 case 'c':
148 if (* cache_directory != NULL)
149 free(* cache_directory);
150 * cache_directory = strdup(optarg);
151 break;
152 case 'f':
153 if (* flags_directory != NULL)
154 free(* flags_directory);
155 * flags_directory = strdup(optarg);
156 break;
157 }
158 }
159
160 return 0;
161}
162
163int init_storage(struct mailstorage * storage,
164 int driver, char * server, int port,
165 int connection_type, char * user, char * password, int auth_type,
166 char * path, char * cache_directory, char * flags_directory)
167{
168 int r;
169 int cached;
170
171 cached = (cache_directory != NULL);
172
173 switch (driver) {
174 case POP3_STORAGE:
175 r = pop3_mailstorage_init(storage, server, port, NULL, connection_type,
176 auth_type, user, password, cached, cache_directory,
177 flags_directory);
178 if (r != MAIL_NO_ERROR) {
179 printf("error initializing POP3 storage\n");
180 goto err;
181 }
182 break;
183
184 case IMAP_STORAGE:
185 r = imap_mailstorage_init(storage, server, port, NULL, connection_type,
186 IMAP_AUTH_TYPE_PLAIN, user, password, cached, cache_directory);
187 if (r != MAIL_NO_ERROR) {
188 printf("error initializing IMAP storage\n");
189 goto err;
190 }
191 break;
192
193 case NNTP_STORAGE:
194 r = nntp_mailstorage_init(storage, server, port, NULL, connection_type,
195 NNTP_AUTH_TYPE_PLAIN, user, password, cached, cache_directory,
196 flags_directory);
197 if (r != MAIL_NO_ERROR) {
198 printf("error initializing NNTP storage\n");
199 goto err;
200 }
201 break;
202
203 case MBOX_STORAGE:
204 r = mbox_mailstorage_init(storage, path, cached, cache_directory,
205 flags_directory);
206 if (r != MAIL_NO_ERROR) {
207 printf("error initializing mbox storage\n");
208 goto err;
209 }
210 break;
211
212 case MH_STORAGE:
213 r = mh_mailstorage_init(storage, path, cached, cache_directory,
214 flags_directory);
215 if (r != MAIL_NO_ERROR) {
216 printf("error initializing MH storage\n");
217 goto err;
218 }
219 break;
220 case MAILDIR_STORAGE:
221 r = maildir_mailstorage_init(storage, path, cached, cache_directory,
222 flags_directory);
223 if (r != MAIL_NO_ERROR) {
224 printf("error initializing maildir storage\n");
225 goto err;
226 }
227 break;
228 }
229
230 return MAIL_NO_ERROR;
231
232 err:
233 return r;
234}