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