]> Pileus Git - ~andy/fetchmail/blob - options.c
We can now set the envelope header from the command line.
[~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_SYSLOG       10
29 #define LA_RCFILE       11
30 #define LA_IDFILE       12
31 #define LA_PROTOCOL     13
32 #define LA_PORT         14
33 #define LA_AUTHENTICATE 15
34 #define LA_TIMEOUT      16
35 #define LA_ENVELOPE     17
36 #define LA_USERNAME     18
37 #define LA_ALL          19
38 #define LA_KILL         20
39 #define LA_KEEP         21
40 #define LA_FLUSH        22
41 #define LA_NOREWRITE    23
42 #define LA_LIMIT        24
43 #define LA_REMOTEFILE   25
44 #define LA_SMTPHOST     26
45 #define LA_BATCHLIMIT   27
46 #define LA_FETCHLIMIT   28
47 #define LA_MDA          29
48 #define LA_INTERFACE    30
49 #define LA_MONITOR      31
50 #define LA_YYDEBUG      32
51
52 static char *shortoptions = "?Vcsvd:NqL:f:i:p:P:A:t:E:u:akKFnl:r:S:b:B:m:I:M:y";
53 static struct option longoptions[] = {
54   {"help",      no_argument,       (int *) 0, LA_HELP        },
55   {"version",   no_argument,       (int *) 0, LA_VERSION     },
56   {"check",     no_argument,       (int *) 0, LA_CHECK       },
57   {"silent",    no_argument,       (int *) 0, LA_SILENT      },
58   {"verbose",   no_argument,       (int *) 0, LA_VERBOSE     },
59   {"daemon",    required_argument, (int *) 0, LA_DAEMON      },
60   {"nodetach",  no_argument,       (int *) 0, LA_NODETACH    },
61   {"quit",      no_argument,       (int *) 0, LA_QUIT        },
62   {"logfile",   required_argument, (int *) 0, LA_LOGFILE     },
63   {"syslog",    no_argument,       (int *) 0, LA_SYSLOG      },
64   {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE      },
65   {"idfile",    required_argument, (int *) 0, LA_IDFILE      },
66 #ifdef  linux
67   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
68   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
69 #endif
70
71   {"protocol",  required_argument, (int *) 0, LA_PROTOCOL    },
72   {"proto",     required_argument, (int *) 0, LA_PROTOCOL    },
73   {"port",      required_argument, (int *) 0, LA_PORT        },
74   {"auth",      required_argument, (int *) 0, LA_AUTHENTICATE},
75   {"timeout",   required_argument, (int *) 0, LA_TIMEOUT     },
76   {"envelope",  required_argument, (int *) 0, LA_ENVELOPE    },
77
78   {"user",      required_argument, (int *) 0, LA_USERNAME    },
79   {"username",  required_argument, (int *) 0, LA_USERNAME    },
80
81   {"all",       no_argument,       (int *) 0, LA_ALL         },
82   {"kill",      no_argument,       (int *) 0, LA_KILL        },
83   {"keep",      no_argument,       (int *) 0, LA_KEEP        },
84   {"flush",     no_argument,       (int *) 0, LA_FLUSH       },
85   {"norewrite", no_argument,       (int *) 0, LA_NOREWRITE   },
86   {"limit",     required_argument, (int *) 0, LA_LIMIT       },
87
88   {"remote",    required_argument, (int *) 0, LA_REMOTEFILE  },
89   {"smtphost",  required_argument, (int *) 0, LA_SMTPHOST    },
90   {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT  },
91   {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT  },
92   {"mda",       required_argument, (int *) 0, LA_MDA         },
93
94   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
95   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
96
97   {"yydebug",   no_argument,       (int *) 0, LA_YYDEBUG     },
98
99   {(char *) 0,  no_argument,       (int *) 0, 0              }
100 };
101
102 int parsecmdline (argc, argv, ctl)
103 /* parse and validate the command line options */
104 int argc;                       /* argument count */
105 char **argv;                    /* argument strings */
106 struct query *ctl;      /* option record to be initialized */
107 {
108     /*
109      * return value: if positive, argv index of last parsed option + 1
110      * (presumes one or more server names follows).  if zero, the
111      * command line switches are such that no server names are
112      * required (e.g. --version).  if negative, the command line is
113      * has one or more syntax errors.
114      */
115
116     int c;
117     int ocount = 0;     /* count of destinations specified */
118     int errflag = 0;   /* TRUE when a syntax error is detected */
119     int option_index;
120
121     memset(ctl, '\0', sizeof(struct query));    /* start clean */
122     cmd_batchlimit = -1;
123
124     while (!errflag && 
125            (c = getopt_long(argc,argv,shortoptions,
126                             longoptions,&option_index)) != -1) {
127
128         switch (c) {
129         case 'V':
130         case LA_VERSION:
131             versioninfo = TRUE;
132             break;
133         case 'c':
134         case LA_CHECK:
135             check_only = TRUE;
136             break;
137         case 's':
138         case LA_SILENT:
139             outlevel = O_SILENT;
140             break;
141         case 'v':
142         case LA_VERBOSE:
143             outlevel = O_VERBOSE;
144             break;
145         case 'd':
146         case LA_DAEMON:
147             poll_interval = atoi(optarg);
148             break;
149         case 'N':
150         case LA_NODETACH:
151             nodetach = TRUE;
152             break;
153         case 'q':
154         case LA_QUIT:
155             quitmode = TRUE;
156             break;
157         case 'L':
158         case LA_LOGFILE:
159             cmd_logfile = optarg;
160             break;
161         case 'f':
162         case LA_RCFILE:
163             rcfile = (char *) xmalloc(strlen(optarg)+1);
164             strcpy(rcfile,optarg);
165             break;
166         case 'i':
167         case LA_IDFILE:
168             idfile = (char *) xmalloc(strlen(optarg)+1);
169             strcpy(idfile,optarg);
170             break;
171         case 'p':
172         case LA_PROTOCOL:
173             /* XXX -- should probably use a table lookup here */
174             if (strcasecmp(optarg,"pop2") == 0)
175                 ctl->protocol = P_POP2;
176             else if (strcasecmp(optarg,"pop3") == 0)
177                 ctl->protocol = P_POP3;
178             else if (strcasecmp(optarg,"imap") == 0)
179                 ctl->protocol = P_IMAP;
180             else if (strcasecmp(optarg,"apop") == 0)
181                 ctl->protocol = P_APOP;
182             else if (strcasecmp(optarg,"kpop") == 0)
183             {
184                 ctl->protocol = P_POP3;
185                 ctl->port = KPOP_PORT;
186                 ctl->authenticate =  A_KERBEROS;
187             }
188             else {
189                 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
190                 errflag++;
191             }
192             break;
193         case 'P':
194         case LA_PORT:
195             ctl->port = atoi(optarg);
196             break;
197         case 'A':
198         case LA_AUTHENTICATE:
199             if (strcmp(optarg, "password") == 0)
200                 ctl->authenticate = A_PASSWORD;
201             else if (strcmp(optarg, "kerberos") == 0)
202                 ctl->authenticate = A_KERBEROS;
203             else {
204                 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
205                 errflag++;
206             }
207             break;
208         case 't':
209         case LA_TIMEOUT:
210             ctl->timeout = atoi(optarg);
211             break;
212         case 'E':
213         case LA_ENVELOPE:
214             ctl->envelope = xstrdup(optarg);
215             break;
216
217         case 'u':
218         case LA_USERNAME:
219             strncpy(ctl->remotename,optarg,sizeof(ctl->remotename)-1);
220             break;
221         case 'a':
222         case LA_ALL:
223             ctl->fetchall = TRUE;
224             break;
225         case 'K':
226         case LA_KILL:
227             ctl->keep = FALSE;
228             break;
229         case 'k':
230         case LA_KEEP:
231             ctl->keep = TRUE;
232             break;
233         case 'F':
234         case LA_FLUSH:
235             ctl->flush = TRUE;
236             break;
237         case 'n':
238         case LA_NOREWRITE:
239             ctl->norewrite = TRUE;
240             break;
241         case 'l':
242         case LA_LIMIT:
243             ctl->limit = atoi(optarg);
244             break;
245         case 'r':
246         case LA_REMOTEFILE:
247             strncpy(ctl->mailbox,optarg,sizeof(ctl->mailbox)-1);
248             break;
249         case 'S':
250         case LA_SMTPHOST:
251             strncpy(ctl->smtphost,optarg,sizeof(ctl->smtphost)-1);
252             ocount++;
253             break;
254         case 'b':
255         case LA_BATCHLIMIT:
256             cmd_batchlimit = atoi(optarg);
257             break;
258         case 'B':
259         case LA_FETCHLIMIT:
260             ctl->fetchlimit = atoi(optarg);
261             break;
262         case 'm':
263         case LA_MDA:
264             strncpy(ctl->mda,optarg,sizeof(ctl->mda));
265             ocount++;
266             break;
267
268 #ifdef  linux
269         case 'I':
270         case LA_INTERFACE:
271             cmd_interface = optarg;
272             break;
273         case 'M':
274         case LA_MONITOR:
275             cmd_monitor = optarg;
276             break;
277 #endif
278
279         case 'y':
280         case LA_YYDEBUG:
281             yydebug = TRUE;
282             break;
283
284         case LA_SYSLOG:
285             use_syslog = TRUE;
286             break;
287
288         case '?':
289         case LA_HELP:
290         default:
291             errflag++;
292         }
293     }
294
295     if (check_only && poll_interval)
296     {
297         fputs("The --check and --daemon options aren't compatible.\n", stderr);
298         return(-1);
299     }
300
301     if (poll_interval == 0 && use_syslog)
302     {
303         fputs("The --syslog option is only valid with the --daemon option.\n", stderr);
304         return(-1);
305     }
306
307     if (errflag || ocount > 1) {
308         /* squawk if syntax errors were detected */
309         fputs("usage:  fetchmail [options] [server ...]\n", stderr);
310         fputs("  Options are as follows:\n",stderr);
311         fputs("  -?, --help        display this option help\n", stderr);
312         fputs("  -V, --version     display version info\n", stderr);
313
314         fputs("  -c, --check       check for messages without fetching\n", stderr);
315         fputs("  -s, --silent      work silently\n", stderr);
316         fputs("  -v, --verbose     work noisily (diagnostic output)\n", stderr);
317         fputs("  -d, --daemon      run as a daemon once per n seconds\n", stderr);
318         fputs("  -N, --nodetach    don't detach daemon process\n", stderr);
319         fputs("  -q, --quit        kill daemon process\n", stderr);
320         fputs("  -L, --logfile     specify logfile name\n", stderr);
321         fputs("      --syslog      use syslog(3) for most messages when running as a daemon\n", stderr);
322         fputs("  -f, --fetchmailrc specify alternate run control file\n", stderr);
323         fputs("  -i, --idfile      specify alternate UIDs file\n", stderr);
324 #ifdef  linux
325         fputs("  -I, --interface   interface required specification\n",stderr);
326         fputs("  -M, --monitor     monitor interface for activity\n",stderr);
327 #endif
328
329         fputs("  -p, --protocol    specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
330         fputs("  -P, --port        TCP/IP service port to connect to\n",stderr);
331         fputs("  -A, --auth        authentication type (password or kerberos)\n",stderr);
332         fputs("  -t, --timeout     server nonresponse timeout\n",stderr);
333         fputs("  -E, --envelope    envelope address header\n",stderr);
334
335         fputs("  -u, --username    specify users's login on server\n", stderr);
336         fputs("  -a, --all         retrieve old and new messages\n", stderr);
337         fputs("  -K, --kill        delete new messages after retrieval\n", stderr);
338         fputs("  -k, --keep        save new messages after retrieval\n", stderr);
339         fputs("  -F, --flush       delete old messages from server\n", stderr);
340         fputs("  -n, --norewrite   don't rewrite header addresses\n", stderr);
341         fputs("  -l, --limit       don't fetch messages over given size\n", stderr);
342
343         fputs("  -S, --smtphost    set SMTP forwarding host\n", stderr);
344         fputs("  -b, --batchlimit  set batch limit for SMTP connections\n", stderr);
345         fputs("  -B, --fetchlimit  set fetch limit for server connections\n", stderr);
346         fputs("  -r, --remote      specify remote folder name\n", stderr);
347         return(-1);
348     }
349
350     return(optind);
351 }
352
353 /* options.c ends here */