2 * options.c -- command-line option processing
4 * For license terms, see the file COPYING in this directory.
12 #if defined(STDC_HEADERS)
17 #include "fetchmail.h"
28 #define LA_INVISIBLE 10
32 #define LA_PROTOCOL 14
35 #define LA_AUTHENTICATE 17
37 #define LA_ENVELOPE 19
38 #define LA_QVIRTUAL 20
39 #define LA_USERNAME 21
44 #define LA_NOREWRITE 26
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
55 #define LA_INTERFACE 37
57 #define LA_CONFIGDUMP 39
60 /* options still left: CgGhHjJoORUwWxXYz */
61 static const char *shortoptions =
62 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:Z:b:B:e:m:T:I:M:y";
64 static const struct option longoptions[] = {
65 /* this can be const because all flag fields are 0 and will never get set */
66 {"help", no_argument, (int *) 0, LA_HELP },
67 {"version", no_argument, (int *) 0, LA_VERSION },
68 {"check", no_argument, (int *) 0, LA_CHECK },
69 {"silent", no_argument, (int *) 0, LA_SILENT },
70 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
71 {"daemon", required_argument, (int *) 0, LA_DAEMON },
72 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
73 {"quit", no_argument, (int *) 0, LA_QUIT },
74 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
75 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
76 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
77 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
78 {"idfile", required_argument, (int *) 0, LA_IDFILE },
80 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
81 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
82 {"uidl", no_argument, (int *) 0, LA_UIDL },
83 {"port", required_argument, (int *) 0, LA_PORT },
84 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
85 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
86 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
87 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
89 {"user", required_argument, (int *) 0, LA_USERNAME },
90 {"username", required_argument, (int *) 0, LA_USERNAME },
92 {"all", no_argument, (int *) 0, LA_ALL },
93 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
94 {"keep", no_argument, (int *) 0, LA_KEEP },
95 {"flush", no_argument, (int *) 0, LA_FLUSH },
96 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
97 {"limit", required_argument, (int *) 0, LA_LIMIT },
99 {"folder", required_argument, (int *) 0, LA_FOLDER },
100 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
101 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
102 {"antispam", required_argument, (int *) 0, LA_ANTISPAM },
104 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
105 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
106 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
107 {"mda", required_argument, (int *) 0, LA_MDA },
110 {"netsec", required_argument, (int *) 0, LA_NETSEC },
113 #if defined(linux) && !INET6
114 {"interface", required_argument, (int *) 0, LA_INTERFACE },
115 {"monitor", required_argument, (int *) 0, LA_MONITOR },
116 #endif /* defined(linux) && !INET6 */
118 {"configdump",no_argument, (int *) 0, LA_CONFIGDUMP },
120 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
122 {(char *) 0, no_argument, (int *) 0, 0 }
125 int parsecmdline (argc, argv, rctl, ctl)
126 /* parse and validate the command line options */
127 int argc; /* argument count */
128 char **argv; /* argument strings */
129 struct runctl *rctl; /* global run controls to modify */
130 struct query *ctl; /* option record to be initialized */
133 * return value: if positive, argv index of last parsed option + 1
134 * (presumes one or more server names follows). if zero, the
135 * command line switches are such that no server names are
136 * required (e.g. --version). if negative, the command line is
137 * has one or more syntax errors.
141 int ocount = 0; /* count of destinations specified */
142 int errflag = 0; /* TRUE when a syntax error is detected */
144 char buf[BUFSIZ], *cp;
146 rctl->poll_interval = -1;
148 memset(ctl, '\0', sizeof(struct query)); /* start clean */
149 ctl->smtp_socket = -1;
152 (c = getopt_long(argc,argv,shortoptions,
153 longoptions,&option_index)) != -1) {
170 outlevel = O_VERBOSE;
174 rctl->poll_interval = atoi(optarg);
186 rctl->logfile = optarg;
189 rctl->invisible = TRUE;
193 rcfile = (char *) xmalloc(strlen(optarg)+1);
194 strcpy(rcfile,optarg);
198 rctl->idfile = (char *) xmalloc(strlen(optarg)+1);
199 strcpy(rctl->idfile,optarg);
203 /* XXX -- should probably use a table lookup here */
204 if (strcasecmp(optarg,"pop2") == 0)
205 ctl->server.protocol = P_POP2;
206 else if (strcasecmp(optarg,"pop3") == 0)
207 ctl->server.protocol = P_POP3;
208 else if (strcasecmp(optarg,"apop") == 0)
209 ctl->server.protocol = P_APOP;
210 else if (strcasecmp(optarg,"rpop") == 0)
211 ctl->server.protocol = P_RPOP;
212 else if (strcasecmp(optarg,"kpop") == 0)
214 ctl->server.protocol = P_POP3;
216 ctl->server.service = KPOP_PORT;
218 ctl->server.port = KPOP_PORT;
221 ctl->server.preauthenticate = A_KERBEROS_V5;
223 ctl->server.preauthenticate = A_KERBEROS_V4;
224 #endif /* KERBEROS_V5 */
226 else if (strcasecmp(optarg,"imap") == 0)
227 ctl->server.protocol = P_IMAP;
229 else if (strcasecmp(optarg,"imap-k4") == 0)
230 ctl->server.protocol = P_IMAP_K4;
231 #endif /* KERBEROS_V4 */
232 else if (strcasecmp(optarg,"etrn") == 0)
233 ctl->server.protocol = P_ETRN;
235 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
241 ctl->server.uidl = FLAG_TRUE;
246 ctl->server.service = optarg;
248 ctl->server.port = atoi(optarg);
252 case LA_AUTHENTICATE:
253 if (strcmp(optarg, "password") == 0)
254 ctl->server.preauthenticate = A_PASSWORD;
255 else if (strcmp(optarg, "kerberos") == 0)
257 ctl->server.preauthenticate = A_KERBEROS_V5;
258 else if (strcmp(optarg, "kerberos_v5") == 0)
259 ctl->server.preauthenticate = A_KERBEROS_V5;
261 ctl->server.preauthenticate = A_KERBEROS_V4;
262 else if (strcmp(optarg, "kerberos_v4") == 0)
263 ctl->server.preauthenticate = A_KERBEROS_V4;
264 #endif /* KERBEROS_V5 */
266 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
272 ctl->server.timeout = atoi(optarg);
273 if (ctl->server.timeout == 0)
274 ctl->server.timeout = -1;
278 ctl->server.envelope = xstrdup(optarg);
282 ctl->server.qvirtual = xstrdup(optarg);
287 ctl->remotename = xstrdup(optarg);
291 ctl->fetchall = FLAG_TRUE;
295 ctl->keep = FLAG_FALSE;
299 ctl->keep = FLAG_TRUE;
303 ctl->flush = FLAG_TRUE;
307 ctl->rewrite = FLAG_FALSE;
312 ctl->limit = NUM_VALUE(c);
317 cp = strtok(buf, ",");
319 save_str(&ctl->mailboxes, cp, 0);
321 ((cp = strtok((char *)NULL, ",")));
326 cp = strtok(buf, ",");
328 save_str(&ctl->smtphunt, cp, TRUE);
330 ((cp = strtok((char *)NULL, ",")));
335 ctl->smtpaddress = xstrdup(optarg);
340 ctl->antispam = NUM_VALUE(c);
344 ctl->batchlimit = NUM_VALUE(c);
349 ctl->fetchlimit = NUM_VALUE(c);
354 ctl->expunge = NUM_VALUE(c);
358 ctl->mda = xstrdup(optarg);
365 ctl->server.netsec = (void *)optarg;
367 fprintf(stderr, "fetchmail: network security support is disabled\n");
369 #endif /* NET_SECURITY */
372 #if defined(linux) && !INET6
375 interface_parse(optarg, &ctl->server);
379 ctl->server.monitor = xstrdup(optarg);
381 #endif /* defined(linux) && !INET6 */
393 rctl->use_syslog = TRUE;
403 if (errflag || ocount > 1) {
404 /* squawk if syntax errors were detected */
405 fputs("usage: fetchmail [options] [server ...]\n", stderr);
406 fputs(" Options are as follows:\n",stderr);
407 fputs(" -?, --help display this option help\n", stderr);
408 fputs(" -V, --version display version info\n", stderr);
410 fputs(" -c, --check check for messages without fetching\n", stderr);
411 fputs(" -s, --silent work silently\n", stderr);
412 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
413 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
414 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
415 fputs(" -q, --quit kill daemon process\n", stderr);
416 fputs(" -L, --logfile specify logfile name\n", stderr);
417 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
418 fputs(" --invisible suppress Received line & enable host spoofing\n", stderr);
419 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
420 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
421 #if defined(linux) && !INET6
422 fputs(" -I, --interface interface required specification\n",stderr);
423 fputs(" -M, --monitor monitor interface for activity\n",stderr);
427 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
429 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
430 #endif /* KERBEROS_V4 */
431 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
432 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
433 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
434 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
435 fputs(" -E, --envelope envelope address header\n",stderr);
436 fputs(" -Q, --qvirtual prefix to remove from local user id\n",stderr);
438 fputs(" -u, --username specify users's login on server\n", stderr);
439 fputs(" -a, --all retrieve old and new messages\n", stderr);
440 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
441 fputs(" -k, --keep save new messages after retrieval\n", stderr);
442 fputs(" -F, --flush delete old messages from server\n", stderr);
443 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
444 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
447 fputs(" -T, --netsec set IP security request\n", stderr);
448 #endif /* NET_SECURITY */
449 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
450 fputs(" -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
451 fputs(" -Z, --antispam, set antispam response value\n", stderr);
452 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
453 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
454 fputs(" -e, --expunge set max deletions between expunges\n", stderr);
455 fputs(" -r, --folder specify remote folder name\n", stderr);
462 /* options.c ends here */