]> Pileus Git - ~andy/fetchmail/blob - options.c
Re-engineer the UIDL stuff to avoid having the status flag collide
[~andy/fetchmail] / options.c
1 /*
2  * options.c -- command-line option processing
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8
9 #include <stdio.h>
10 #include <pwd.h>
11 #include <string.h>
12 #if defined(STDC_HEADERS)
13 #include  <stdlib.h>
14 #endif
15
16 #include "getopt.h"
17 #include "fetchmail.h"
18
19 #define LA_HELP         1
20 #define LA_VERSION      2 
21 #define LA_CHECK        3
22 #define LA_SILENT       4 
23 #define LA_VERBOSE      5 
24 #define LA_DAEMON       6
25 #define LA_NODETACH     7
26 #define LA_QUIT         8
27 #define LA_LOGFILE      9
28 #define LA_INVISIBLE    10
29 #define LA_SYSLOG       11
30 #define LA_RCFILE       12
31 #define LA_IDFILE       13
32 #define LA_PROTOCOL     14
33 #define LA_UIDL         15
34 #define LA_PORT         16
35 #define LA_AUTHENTICATE 17
36 #define LA_TIMEOUT      18
37 #define LA_ENVELOPE     19
38 #define LA_QVIRTUAL     20
39 #define LA_USERNAME     21
40 #define LA_ALL          22
41 #define LA_NOKEEP       23
42 #define LA_KEEP         24
43 #define LA_FLUSH        25
44 #define LA_NOREWRITE    26
45 #define LA_LIMIT        27
46 #define LA_FOLDER       28
47 #define LA_SMTPHOST     29
48 #define LA_SMTPADDR     30
49 #define LA_ANTISPAM     31
50 #define LA_BATCHLIMIT   32
51 #define LA_FETCHLIMIT   33
52 #define LA_EXPUNGE      34
53 #define LA_MDA          35
54 #define LA_NETSEC       36
55 #define LA_INTERFACE    37
56 #define LA_MONITOR      38
57 #define LA_YYDEBUG      39
58
59 /* options still left: CgGhHjJoORUwWxXYzZ */
60 static const char *shortoptions = 
61         "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:b:B:e:m:I:M:y";
62
63 static const struct option longoptions[] = {
64 /* this can be const because all flag fields are 0 and will never get set */
65   {"help",      no_argument,       (int *) 0, LA_HELP        },
66   {"version",   no_argument,       (int *) 0, LA_VERSION     },
67   {"check",     no_argument,       (int *) 0, LA_CHECK       },
68   {"silent",    no_argument,       (int *) 0, LA_SILENT      },
69   {"verbose",   no_argument,       (int *) 0, LA_VERBOSE     },
70   {"daemon",    required_argument, (int *) 0, LA_DAEMON      },
71   {"nodetach",  no_argument,       (int *) 0, LA_NODETACH    },
72   {"quit",      no_argument,       (int *) 0, LA_QUIT        },
73   {"logfile",   required_argument, (int *) 0, LA_LOGFILE     },
74   {"invisible", no_argument,       (int *) 0, LA_INVISIBLE   },
75   {"syslog",    no_argument,       (int *) 0, LA_SYSLOG      },
76   {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE      },
77   {"idfile",    required_argument, (int *) 0, LA_IDFILE      },
78
79   {"protocol",  required_argument, (int *) 0, LA_PROTOCOL    },
80   {"proto",     required_argument, (int *) 0, LA_PROTOCOL    },
81   {"uidl",      no_argument,       (int *) 0, LA_UIDL        },
82   {"port",      required_argument, (int *) 0, LA_PORT        },
83   {"auth",      required_argument, (int *) 0, LA_AUTHENTICATE},
84   {"timeout",   required_argument, (int *) 0, LA_TIMEOUT     },
85   {"envelope",  required_argument, (int *) 0, LA_ENVELOPE    },
86   {"qvirtual",  required_argument, (int *) 0, LA_QVIRTUAL    },
87
88   {"user",      required_argument, (int *) 0, LA_USERNAME    },
89   {"username",  required_argument, (int *) 0, LA_USERNAME    },
90
91   {"all",       no_argument,       (int *) 0, LA_ALL         },
92   {"nokeep",    no_argument,       (int *) 0, LA_NOKEEP      },
93   {"keep",      no_argument,       (int *) 0, LA_KEEP        },
94   {"flush",     no_argument,       (int *) 0, LA_FLUSH       },
95   {"norewrite", no_argument,       (int *) 0, LA_NOREWRITE   },
96   {"limit",     required_argument, (int *) 0, LA_LIMIT       },
97
98   {"folder",    required_argument, (int *) 0, LA_FOLDER      },
99   {"smtphost",  required_argument, (int *) 0, LA_SMTPHOST    },
100   {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR  },
101   {"antispam",  required_argument, (int *) 0, LA_ANTISPAM    },
102   
103   {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT  },
104   {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT  },
105   {"expunge",   required_argument, (int *) 0, LA_EXPUNGE     },
106   {"mda",       required_argument, (int *) 0, LA_MDA         },
107
108 #ifdef INET6
109   {"netsec",    required_argument, (int *) 0, LA_NETSEC    },
110 #endif /* INET6 */
111
112 #if defined(linux) && !INET6
113   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
114   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
115 #endif /* defined(linux) && !INET6 */
116
117   {"yydebug",   no_argument,       (int *) 0, LA_YYDEBUG     },
118
119   {(char *) 0,  no_argument,       (int *) 0, 0              }
120 };
121
122 int parsecmdline (argc, argv, ctl)
123 /* parse and validate the command line options */
124 int argc;               /* argument count */
125 char **argv;            /* argument strings */
126 struct query *ctl;      /* option record to be initialized */
127 {
128     /*
129      * return value: if positive, argv index of last parsed option + 1
130      * (presumes one or more server names follows).  if zero, the
131      * command line switches are such that no server names are
132      * required (e.g. --version).  if negative, the command line is
133      * has one or more syntax errors.
134      */
135
136     int c;
137     int ocount = 0;     /* count of destinations specified */
138     int errflag = 0;   /* TRUE when a syntax error is detected */
139     int option_index;
140     char buf[BUFSIZ], *cp;
141
142     cmd_daemon = -1;
143
144     memset(ctl, '\0', sizeof(struct query));    /* start clean */
145     ctl->smtp_socket = -1;
146
147     while (!errflag && 
148            (c = getopt_long(argc,argv,shortoptions,
149                             longoptions,&option_index)) != -1) {
150
151         switch (c) {
152         case 'V':
153         case LA_VERSION:
154             versioninfo = TRUE;
155             break;
156         case 'c':
157         case LA_CHECK:
158             check_only = TRUE;
159             break;
160         case 's':
161         case LA_SILENT:
162             outlevel = O_SILENT;
163             break;
164         case 'v':
165         case LA_VERBOSE:
166             outlevel = O_VERBOSE;
167             break;
168         case 'd':
169         case LA_DAEMON:
170             cmd_daemon = atoi(optarg);
171             break;
172         case 'N':
173         case LA_NODETACH:
174             nodetach = TRUE;
175             break;
176         case 'q':
177         case LA_QUIT:
178             quitmode = TRUE;
179             break;
180         case 'L':
181         case LA_LOGFILE:
182             cmd_logfile = optarg;
183             break;
184         case LA_INVISIBLE:
185             use_invisible = TRUE;
186             break;
187         case 'f':
188         case LA_RCFILE:
189             rcfile = (char *) xmalloc(strlen(optarg)+1);
190             strcpy(rcfile,optarg);
191             break;
192         case 'i':
193         case LA_IDFILE:
194             idfile = (char *) xmalloc(strlen(optarg)+1);
195             strcpy(idfile,optarg);
196             break;
197         case 'p':
198         case LA_PROTOCOL:
199             /* XXX -- should probably use a table lookup here */
200             if (strcasecmp(optarg,"pop2") == 0)
201                 ctl->server.protocol = P_POP2;
202             else if (strcasecmp(optarg,"pop3") == 0)
203                 ctl->server.protocol = P_POP3;
204             else if (strcasecmp(optarg,"apop") == 0)
205                 ctl->server.protocol = P_APOP;
206             else if (strcasecmp(optarg,"rpop") == 0)
207                 ctl->server.protocol = P_RPOP;
208             else if (strcasecmp(optarg,"kpop") == 0)
209             {
210                 ctl->server.protocol = P_POP3;
211 #if INET6
212                 ctl->server.service = KPOP_PORT;
213 #else /* INET6 */
214                 ctl->server.port = KPOP_PORT;
215 #endif /* INET6 */
216 #ifdef KERBEROS_V5
217                 ctl->server.preauthenticate =  A_KERBEROS_V5;
218 #else
219                 ctl->server.preauthenticate =  A_KERBEROS_V4;
220 #endif /* KERBEROS_V5 */
221             }
222             else if (strcasecmp(optarg,"imap") == 0)
223                 ctl->server.protocol = P_IMAP;
224 #ifdef KERBEROS_V4
225             else if (strcasecmp(optarg,"imap-k4") == 0)
226                 ctl->server.protocol = P_IMAP_K4;
227 #endif /* KERBEROS_V4 */
228             else if (strcasecmp(optarg,"etrn") == 0)
229                 ctl->server.protocol = P_ETRN;
230             else {
231                 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
232                 errflag++;
233             }
234             break;
235         case 'U':
236         case LA_UIDL:
237             ctl->server.uidl = FLAG_TRUE;
238             break;
239         case 'P':
240         case LA_PORT:
241 #if INET6
242             ctl->server.service = optarg;
243 #else /* INET6 */
244             ctl->server.port = atoi(optarg);
245 #endif /* INET6 */
246             break;
247         case 'A':
248         case LA_AUTHENTICATE:
249             if (strcmp(optarg, "password") == 0)
250                 ctl->server.preauthenticate = A_PASSWORD;
251             else if (strcmp(optarg, "kerberos") == 0)
252 #ifdef KERBEROS_V5
253                 ctl->server.preauthenticate = A_KERBEROS_V5;
254             else if (strcmp(optarg, "kerberos_v5") == 0)
255                 ctl->server.preauthenticate = A_KERBEROS_V5;
256 #else
257                 ctl->server.preauthenticate = A_KERBEROS_V4;
258             else if (strcmp(optarg, "kerberos_v4") == 0)
259                 ctl->server.preauthenticate = A_KERBEROS_V4;
260 #endif /* KERBEROS_V5 */
261             else {
262                 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
263                 errflag++;
264             }
265             break;
266         case 't':
267         case LA_TIMEOUT:
268             ctl->server.timeout = atoi(optarg);
269             if (ctl->server.timeout == 0)
270                 ctl->server.timeout = -1;
271             break;
272         case 'E':
273         case LA_ENVELOPE:
274             ctl->server.envelope = xstrdup(optarg);
275             break;
276         case 'Q':    
277         case LA_QVIRTUAL:
278             ctl->server.qvirtual = xstrdup(optarg);
279             break;
280
281         case 'u':
282         case LA_USERNAME:
283             ctl->remotename = xstrdup(optarg);
284             break;
285         case 'a':
286         case LA_ALL:
287             ctl->fetchall = FLAG_TRUE;
288             break;
289         case 'K':
290         case LA_NOKEEP:
291             ctl->keep = FLAG_FALSE;
292             break;
293         case 'k':
294         case LA_KEEP:
295             ctl->keep = FLAG_TRUE;
296             break;
297         case 'F':
298         case LA_FLUSH:
299             ctl->flush = FLAG_TRUE;
300             break;
301         case 'n':
302         case LA_NOREWRITE:
303             ctl->rewrite = FLAG_FALSE;
304             break;
305         case 'l':
306         case LA_LIMIT:
307             c = atoi(optarg);
308             ctl->limit = NUM_VALUE(c);
309             break;
310         case 'r':
311         case LA_FOLDER:
312             strcpy(buf, optarg);
313             cp = strtok(buf, ",");
314             do {
315                 save_str(&ctl->mailboxes, cp, 0);
316             } while
317                 ((cp = strtok((char *)NULL, ",")));
318             break;
319         case 'S':
320         case LA_SMTPHOST:
321             strcpy(buf, optarg);
322             cp = strtok(buf, ",");
323             do {
324                 save_str(&ctl->smtphunt, cp, TRUE);
325             } while
326                 ((cp = strtok((char *)NULL, ",")));
327             ocount++;
328             break;
329         case 'D':
330         case LA_SMTPADDR:
331             ctl->smtpaddress = xstrdup(optarg);
332             break;
333         case 'Z':
334         case LA_ANTISPAM:
335             c = atoi(optarg);
336             ctl->antispam = NUM_VALUE(c);
337         case 'b':
338         case LA_BATCHLIMIT:
339             c = atoi(optarg);
340             ctl->batchlimit = NUM_VALUE(c);
341             break;
342         case 'B':
343         case LA_FETCHLIMIT:
344             c = atoi(optarg);
345             ctl->fetchlimit = NUM_VALUE(c);
346             break;
347         case 'e':
348         case LA_EXPUNGE:
349             c = atoi(optarg);
350             ctl->expunge = NUM_VALUE(c);
351             break;
352         case 'm':
353         case LA_MDA:
354             ctl->mda = xstrdup(optarg);
355             ocount++;
356             break;
357
358         case 'T':
359         case LA_NETSEC:
360 #if NET_SECURITY
361             ctl->server.netsec = (void *)optarg;
362 #else
363             fprintf(stderr, "fetchmail: network security support is disabled\n");
364             errflag++;
365 #endif /* NET_SECURITY */
366             break;
367
368 #if defined(linux) && !INET6
369         case 'I':
370         case LA_INTERFACE:
371             interface_parse(optarg, &ctl->server);
372             break;
373         case 'M':
374         case LA_MONITOR:
375             ctl->server.monitor = xstrdup(optarg);
376             break;
377 #endif /* defined(linux) && !INET6 */
378
379         case 'y':
380         case LA_YYDEBUG:
381             yydebug = TRUE;
382             break;
383
384         case LA_SYSLOG:
385             errors_to_syslog = TRUE;
386             break;
387
388         case '?':
389         case LA_HELP:
390         default:
391             errflag++;
392         }
393     }
394
395     if (errflag || ocount > 1) {
396         /* squawk if syntax errors were detected */
397         fputs("usage:  fetchmail [options] [server ...]\n", stderr);
398         fputs("  Options are as follows:\n",stderr);
399         fputs("  -?, --help        display this option help\n", stderr);
400         fputs("  -V, --version     display version info\n", stderr);
401
402         fputs("  -c, --check       check for messages without fetching\n", stderr);
403         fputs("  -s, --silent      work silently\n", stderr);
404         fputs("  -v, --verbose     work noisily (diagnostic output)\n", stderr);
405         fputs("  -d, --daemon      run as a daemon once per n seconds\n", stderr);
406         fputs("  -N, --nodetach    don't detach daemon process\n", stderr);
407         fputs("  -q, --quit        kill daemon process\n", stderr);
408         fputs("  -L, --logfile     specify logfile name\n", stderr);
409         fputs("      --syslog      use syslog(3) for most messages when running as a daemon\n", stderr);
410         fputs("      --invisible   suppress Received line & enable host spoofing\n", stderr);
411         fputs("  -f, --fetchmailrc specify alternate run control file\n", stderr);
412         fputs("  -i, --idfile      specify alternate UIDs file\n", stderr);
413 #if defined(linux) && !INET6
414         fputs("  -I, --interface   interface required specification\n",stderr);
415         fputs("  -M, --monitor     monitor interface for activity\n",stderr);
416 #endif
417
418 #ifdef KERBEROS_V4
419         fputs("  -p, --protocol    specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
420 #else
421         fputs("  -p, --protocol    specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
422 #endif /* KERBEROS_V4 */
423         fputs("  -U, --uidl        force the use of UIDLs (pop3 only)\n", stderr);
424         fputs("  -P, --port        TCP/IP service port to connect to\n",stderr);
425         fputs("  -A, --auth        authentication type (password or kerberos)\n",stderr);
426         fputs("  -t, --timeout     server nonresponse timeout\n",stderr);
427         fputs("  -E, --envelope    envelope address header\n",stderr);
428         fputs("  -Q, --qvirtual    prefix to remove from local user id\n",stderr);
429
430         fputs("  -u, --username    specify users's login on server\n", stderr);
431         fputs("  -a, --all         retrieve old and new messages\n", stderr);
432         fputs("  -K, --nokeep      delete new messages after retrieval\n", stderr);
433         fputs("  -k, --keep        save new messages after retrieval\n", stderr);
434         fputs("  -F, --flush       delete old messages from server\n", stderr);
435         fputs("  -n, --norewrite   don't rewrite header addresses\n", stderr);
436         fputs("  -l, --limit       don't fetch messages over given size\n", stderr);
437
438 #if NET_SECURITY
439         fputs("  -T, --netsec      set IP security request\n", stderr);
440 #endif /* NET_SECURITY */
441         fputs("  -S, --smtphost    set SMTP forwarding host\n", stderr);
442         fputs("  -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
443         fputs("  -Z, --antispam,   set antispam response value\n", stderr);
444         fputs("  -b, --batchlimit  set batch limit for SMTP connections\n", stderr);
445         fputs("  -B, --fetchlimit  set fetch limit for server connections\n", stderr);
446         fputs("  -e, --expunge     set max deletions between expunges\n", stderr);
447         fputs("  -r, --folder      specify remote folder name\n", stderr);
448         return(-1);
449     }
450
451     return(optind);
452 }
453
454 /* options.c ends here */