summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tests/option-parser.c
blob: 57a15972c2426cbb483395817631ce487030671a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#define _GNU_SOURCE
#include <getopt.h>

#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <libetpan/libetpan.h>

#include "option-parser.h"

/*
  options
  
  --driver (pop3|imap|nntp|mbox|mh|maildir)  -d

  default driver is mbox

  --server {server-name} -s
  --port {port-number}   -p
  --tls                  -t
  --starttls             -x
  --user {login}         -u
  --password {password}  -v
  --path {mailbox}       -l
  --apop                 -a
  --cache {directory}    -c
  --flags {directory}    -f
*/

struct storage_name {
  int id;
  char * name;
};

static struct storage_name storage_tab[] = {
  {POP3_STORAGE, "pop3"},
  {IMAP_STORAGE, "imap"},
  {NNTP_STORAGE, "nntp"},
  {MBOX_STORAGE, "mbox"},
  {MH_STORAGE, "mh"},
  {MAILDIR_STORAGE, "maildir"},
};

static int get_driver(char * name)
{
  int driver_type;
  unsigned int i;

  driver_type = -1;
  for(i = 0 ; i < sizeof(storage_tab) / sizeof(struct storage_name) ; i++) {
    if (strcasecmp(name, storage_tab[i].name) == 0) {
      driver_type = i;
      break;
    }
  }

  return driver_type;
}

int parse_options(int argc, char ** argv,
    int * driver,
    char ** server, int * port, int * connection_type,
    char ** user, char ** password, int * auth_type,
    char ** path, char ** cache_directory,
    char ** flags_directory)
{
  int index;
  static struct option long_options[] = {
    {"driver",   1, 0, 'd'},
    {"server",   1, 0, 's'},
    {"port",     1, 0, 'p'},
    {"tls",      0, 0, 't'},
    {"starttls", 0, 0, 'x'},
    {"user",     1, 0, 'u'},
    {"password", 1, 0, 'v'},
    {"path",     1, 0, 'l'},
    {"apop",     0, 0, 'a'},
    {"cache",    1, 0, 'c'},
    {"flags",    1, 0, 'f'},
  };
  int r;
  char location[PATH_MAX];
  char * env_user;

  index = 0;

  * driver = MBOX_STORAGE;
  * server = NULL;
  * port = 0;
  * connection_type = CONNECTION_TYPE_PLAIN;
  * user = NULL;
  * password = NULL;
  * auth_type = POP3_AUTH_TYPE_PLAIN;
  env_user = getenv("USER");
  if (env_user != NULL) {
    snprintf(location, PATH_MAX, "/var/mail/%s", env_user);
    * path = strdup(location);
  }
  else
    * path = NULL;
  * cache_directory = NULL;
  * flags_directory = NULL;

  while (1) {
    r = getopt_long(argc, argv, "d:s:p:txu:v:l:ac:f:", long_options, &index);
    
    if (r == -1)
      break;

    switch (r) {
    case 'd':
      * driver = get_driver(optarg);
      break;
    case 's':
      if (* server != NULL)
        free(* server);
      * server = strdup(optarg);
      break;
    case 'p':
      * port = strtoul(optarg, NULL, 10);
      break;
    case 't':
      * connection_type = CONNECTION_TYPE_TLS;
      break;
    case 'x':
      * connection_type = CONNECTION_TYPE_STARTTLS;
        break;
    case 'u':
      if (* user != NULL)
        free(* user);
      * user = strdup(optarg);
      break;
    case 'v':
      if (* password != NULL)
        free(* password);
      * password = strdup(optarg);
      break;
    case 'l':
      if (* path != NULL)
        free(* path);
      * path = strdup(optarg);
      break;
    case 'a':
      * auth_type = POP3_AUTH_TYPE_APOP;
      break;
    case 'c':
      if (* cache_directory != NULL)
        free(* cache_directory);
      * cache_directory = strdup(optarg);
      break;
    case 'f':
      if (* flags_directory != NULL)
        free(* flags_directory);
      * flags_directory = strdup(optarg);
      break;
    }
  }

  return 0;
}

int init_storage(struct mailstorage * storage,
    int driver, char * server, int port,
    int connection_type, char * user, char * password, int auth_type,
    char * path, char * cache_directory, char * flags_directory)
{
  int r;
  int cached;

  cached = (cache_directory != NULL);
  
  switch (driver) {
  case POP3_STORAGE:
    r = pop3_mailstorage_init(storage, server, port, NULL, connection_type,
        auth_type, user, password, cached, cache_directory,
        flags_directory);
    if (r != MAIL_NO_ERROR) {
      printf("error initializing POP3 storage\n");
      goto err;
    }
    break;

  case IMAP_STORAGE:
    r = imap_mailstorage_init(storage, server, port, NULL, connection_type,
        IMAP_AUTH_TYPE_PLAIN, user, password, cached, cache_directory);
    if (r != MAIL_NO_ERROR) {
      printf("error initializing IMAP storage\n");
      goto err;
    }
    break;

  case NNTP_STORAGE:
    r = nntp_mailstorage_init(storage, server, port, NULL, connection_type,
        NNTP_AUTH_TYPE_PLAIN, user, password, cached, cache_directory,
        flags_directory);
    if (r != MAIL_NO_ERROR) {
      printf("error initializing NNTP storage\n");
      goto err;
    }
    break;

  case MBOX_STORAGE:
    r = mbox_mailstorage_init(storage, path, cached, cache_directory,
        flags_directory);
    if (r != MAIL_NO_ERROR) {
      printf("error initializing mbox storage\n");
      goto err;
    }
    break;

  case MH_STORAGE:
    r = mh_mailstorage_init(storage, path, cached, cache_directory,
        flags_directory);
    if (r != MAIL_NO_ERROR) {
      printf("error initializing MH storage\n");
      goto err;
    }
    break;
  case MAILDIR_STORAGE:
    r = maildir_mailstorage_init(storage, path, cached, cache_directory,
        flags_directory);
    if (r != MAIL_NO_ERROR) {
      printf("error initializing maildir storage\n");
      goto err;
    }
    break;
  }
  
  return MAIL_NO_ERROR;
  
 err:
  return r;
}