]> diplodocus.org Git - nmh/blob - uip/msgchk.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / msgchk.c
1 /* msgchk.c -- check for mail
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/mts.h>
10 #include <h/tws.h>
11 #include "h/done.h"
12 #include <h/utils.h>
13 #include <pwd.h>
14
15 #include <h/popsbr.h>
16
17 #ifndef CYRUS_SASL
18 # define SASLminc(a) (a)
19 #else
20 # define SASLminc(a) 0
21 #endif
22
23 #ifndef TLS_SUPPORT
24 # define TLSminc(a) (a)
25 #else
26 # define TLSminc(a) 0
27 #endif
28
29 #define MSGCHK_SWITCHES \
30 X("date", 0, DATESW) \
31 X("nodate", 0, NDATESW) \
32 X("notify type", 0, NOTESW) \
33 X("nonotify type", 0, NNOTESW) \
34 X("host hostname", 0, HOSTSW) \
35 X("user username", 0, USERSW) \
36 X("port name/number", 0, PORTSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("snoop", 0, SNOOPSW) \
40 X("sasl", SASLminc(4), SASLSW) \
41 X("nosasl", SASLminc(6), NOSASLSW) \
42 X("saslmech", SASLminc(5), SASLMECHSW) \
43 X("authservice", SASLminc(0), AUTHSERVICESW) \
44 X("initialtls", TLSminc(-10), INITTLSSW) \
45 X("notls", TLSminc(-5), NOTLSSW) \
46 X("certverify", TLSminc(-10), CERTVERSW) \
47 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
48 X("proxy command", 0, PROXYSW) \
49
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MSGCHK);
52 #undef X
53
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MSGCHK, switches);
56 #undef X
57
58 /*
59 * Maximum numbers of users we can check (plus
60 * one for the NULL vector at the end).
61 */
62 #define MAXVEC 51
63
64 #define NT_MAIL 0x1
65 #define NT_NMAI 0x2
66 #define NT_ALL (NT_MAIL | NT_NMAI)
67
68
69 /*
70 * static prototypes
71 */
72 static int donote (char *, int) PURE;
73 static int checkmail (char *, char *, int, int, int);
74 static int remotemail (char *, char *, char *, char *, int, int, int, int,
75 char *, int, const char *);
76
77
78 int
79 main (int argc, char **argv)
80 {
81 bool datesw = true;
82 int notifysw = NT_ALL;
83 int status = 0;
84 bool sasl = false;
85 bool tls = false;
86 bool noverify = false;
87 bool snoop = false;
88 int vecp = 0;
89 char *cp, *host = NULL, *port = NULL, *user = NULL, *proxy = NULL;
90 char buf[BUFSIZ], *saslmech = NULL, *auth_svc = NULL;
91 char **argp, **arguments, *vec[MAXVEC];
92 struct passwd *pw;
93
94 if (nmh_init(argv[0], true, true)) { return 1; }
95
96 mts_init ();
97
98 arguments = getarguments (invo_name, argc, argv, 1);
99 argp = arguments;
100
101 while ((cp = *argp++)) {
102 if (*cp == '-') {
103 switch (smatch (++cp, switches)) {
104 case AMBIGSW:
105 ambigsw (cp, switches);
106 done (1);
107 case UNKWNSW:
108 die("-%s unknown", cp);
109
110 case HELPSW:
111 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
112 invo_name);
113 print_help (buf, switches, 1);
114 done (0);
115 case VERSIONSW:
116 print_version(invo_name);
117 done (0);
118
119 case DATESW:
120 datesw = true;
121 continue;
122 case NDATESW:
123 datesw = false;
124 continue;
125
126 case NOTESW:
127 if (!(cp = *argp++) || *cp == '-')
128 die("missing argument to %s", argp[-2]);
129 notifysw |= donote (cp, 1);
130 continue;
131 case NNOTESW:
132 if (!(cp = *argp++) || *cp == '-')
133 die("missing argument to %s", argp[-2]);
134 notifysw &= ~donote (cp, 0);
135 continue;
136
137 case HOSTSW:
138 if (!(host = *argp++) || *host == '-')
139 die("missing argument to %s", argp[-2]);
140 continue;
141
142 case PORTSW:
143 if (!(port = *argp++) || *port == '-')
144 die("missing argument to %s", argp[-2]);
145 continue;
146
147 case USERSW:
148 if (!(cp = *argp++) || *cp == '-')
149 die("missing argument to %s", argp[-2]);
150 if (vecp >= MAXVEC-1)
151 die("you can only check %d users at a time", MAXVEC-1);
152 user = vec[vecp++] = cp;
153 continue;
154
155 case SNOOPSW:
156 snoop = true;
157 continue;
158
159 case SASLSW:
160 sasl = true;
161 continue;
162
163 case NOSASLSW:
164 sasl = false;
165 continue;
166
167 case SASLMECHSW:
168 if (!(saslmech = *argp++) || *saslmech == '-')
169 die("missing argument to %s", argp[-2]);
170 continue;
171
172 case INITTLSSW:
173 tls = true;
174 continue;
175
176 case NOTLSSW:
177 tls = false;
178 continue;
179
180 case CERTVERSW:
181 noverify = false;
182 continue;
183
184 case NOCERTVERSW:
185 noverify = true;
186 continue;
187
188 case AUTHSERVICESW:
189 #ifdef OAUTH_SUPPORT
190 if (!(auth_svc = *argp++) || *auth_svc == '-')
191 die("missing argument to %s", argp[-2]);
192 #else
193 die("not built with OAuth support");
194 #endif
195 continue;
196
197 case PROXYSW:
198 if (!(proxy = *argp++) || *proxy == '-')
199 die("missing argument to %s", argp[-2]);
200 continue;
201 }
202 }
203 if (vecp >= MAXVEC-1)
204 die("you can only check %d users at a time", MAXVEC-1);
205 vec[vecp++] = cp;
206 }
207
208 /*
209 * If -host is not specified by user
210 */
211 if (!host || !*host) {
212 /*
213 * If "pophost" is specified in mts.conf,
214 * use it as default value.
215 */
216 if (pophost && *pophost)
217 host = pophost;
218 }
219 if (!host || !*host)
220 host = NULL;
221
222 if (vecp != 0)
223 vec[vecp] = NULL;
224
225 if (host) {
226 int tlsflag = 0;
227
228 if (tls)
229 tlsflag |= P_INITTLS;
230
231 if (noverify)
232 tlsflag |= P_NOVERIFY;
233
234 if (vecp == 0) {
235 status = remotemail (host, port, user, proxy, notifysw, 1,
236 snoop, sasl, saslmech, tlsflag, auth_svc);
237 } else {
238 for (vecp = 0; vec[vecp]; vecp++)
239 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
240 snoop, sasl, saslmech, tlsflag, auth_svc);
241 }
242 } else {
243 if (user == NULL) user = getusername ();
244 if (vecp == 0) {
245 char *home;
246
247 /* Not sure this check makes sense... */
248 if (!geteuid() || NULL == (home = getenv("HOME"))) {
249 pw = getpwnam (user);
250 if (pw == NULL)
251 die("unable to get information about user");
252 home = pw->pw_dir;
253 }
254 status = checkmail (user, home, datesw, notifysw, 1);
255 } else {
256 for (vecp = 0; vec[vecp]; vecp++) {
257 if ((pw = getpwnam (vec[vecp])))
258 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
259 else
260 inform("no such user as %s", vec[vecp]);
261 }
262 }
263 } /* host == NULL */
264
265 done (status);
266 return 1;
267 }
268
269
270 #define NOTE_SWITCHES \
271 X("all", 0, NALLSW) \
272 X("mail", 0, NMAISW) \
273 X("nomail", 0, NNMAISW) \
274
275 #define X(sw, minchars, id) id,
276 DEFINE_SWITCH_ENUM(NOTE);
277 #undef X
278
279 #define X(sw, minchars, id) { sw, minchars, id },
280 DEFINE_SWITCH_ARRAY(NOTE, ntswitches);
281 #undef X
282
283
284 static int
285 donote (char *cp, int ntflag)
286 {
287 switch (smatch (cp, ntswitches)) {
288 case AMBIGSW:
289 ambigsw (cp, ntswitches);
290 done (1);
291 case UNKWNSW:
292 die("-%snotify %s unknown", ntflag ? "" : "no", cp);
293
294 case NALLSW:
295 return NT_ALL;
296 case NMAISW:
297 return NT_MAIL;
298 case NNMAISW:
299 return NT_NMAI;
300 }
301
302 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
303 }
304
305
306 static int
307 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
308 {
309 char buffer[BUFSIZ];
310 snprintf(buffer, sizeof buffer, "%s/%s", *mmdfldir ? mmdfldir : home,
311 *mmdflfil ? mmdflfil : user);
312
313 struct stat st;
314 bool statok = stat(buffer, &st) != -1;
315 bool empty = !statok || st.st_size == 0;
316
317 if ((empty && !(notifysw & NT_NMAI)) ||
318 (!empty && !(notifysw & NT_MAIL))) {
319 return empty;
320 }
321
322 if (empty) {
323 if (personal) {
324 fputs("You don't have any mail waiting", stdout);
325 } else {
326 printf("%s doesn't have any mail waiting", user);
327 }
328 } else {
329 char *kind = st.st_mtime < st.st_atime ? "old" : "new";
330 if (personal) {
331 printf("You have %s mail waiting", kind);
332 } else {
333 printf("%s has %s mail waiting", user, kind);
334 }
335 }
336
337 if (datesw && statok && st.st_atime)
338 printf("; last read on %s", dtime(&st.st_atime, 1));
339 putchar('\n');
340
341 return empty;
342 }
343
344
345 extern char response[];
346
347 static int
348 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
349 int personal, int snoop, int sasl, char *saslmech, int tls,
350 const char *auth_svc)
351 {
352 int nmsgs, nbytes;
353 bool status;
354
355 if (auth_svc == NULL) {
356 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
357 die("must specify -authservice with -saslmech xoauth2");
358 }
359 } else {
360 if (user == NULL) {
361 die("must specify -user with -saslmech xoauth2");
362 }
363 }
364
365 /* open the POP connection */
366 if (pop_init (host, port, user, proxy, snoop, sasl, saslmech, tls,
367 auth_svc) == NOTOK
368 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
369 || pop_quit () == NOTOK) { /* quit POP connection */
370 inform("%s", response);
371 return 1;
372 }
373
374 if (nmsgs) {
375 if (notifysw & NT_MAIL) {
376 if (personal)
377 fputs("You have ", stdout);
378 else
379 printf ("%s has ", user);
380
381 printf ("%d message%s (%d bytes)",
382 nmsgs, PLURALS(nmsgs), nbytes);
383 }
384 else
385 notifysw = 0;
386
387 status = false;
388 } else {
389 if (notifysw & NT_NMAI)
390 printf (personal ? "You don't %s%s" : "%s doesn't %s",
391 personal ? "" : user, "have any mail waiting");
392 else
393 notifysw = 0;
394 status = true;
395 }
396 if (notifysw)
397 printf (" on %s\n", host);
398
399 return status;
400 }