]> diplodocus.org Git - nmh/blob - uip/msgchk.c
Alter mh-chart(7)'s NAME to be lowercase.
[nmh] / uip / msgchk.c
1
2 /*
3 * msgchk.c -- check for mail
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/mts.h>
12 #include <h/tws.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_NONE 0x0
65 #ifdef NT_NONE
66 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
67 #define NT_MAIL 0x1
68 #define NT_NMAI 0x2
69 #define NT_ALL (NT_MAIL | NT_NMAI)
70
71 #define NONEOK 0x0
72 #define UUCPOLD 0x1
73 #define UUCPNEW 0x2
74 #define UUCPOK (UUCPOLD | UUCPNEW)
75 #define MMDFOLD 0x4
76 #define MMDFNEW 0x8
77 #define MMDFOK (MMDFOLD | MMDFNEW)
78
79
80 /*
81 * static prototypes
82 */
83 static int donote (char *, int);
84 static int checkmail (char *, char *, int, int, int);
85 static int remotemail (char *, char *, char *, char *, int, int, int, int,
86 char *, int, const char *);
87
88
89 int
90 main (int argc, char **argv)
91 {
92 int datesw = 1, notifysw = NT_ALL;
93 int status = 0, sasl = 0, tls = 0, noverify = 0;
94 int snoop = 0, vecp = 0;
95 char *cp, *host = NULL, *port = NULL, *user = NULL, *proxy = NULL;
96 char buf[BUFSIZ], *saslmech = NULL, *auth_svc = NULL;
97 char **argp, **arguments, *vec[MAXVEC];
98 struct passwd *pw;
99
100 if (nmh_init(argv[0], 1)) { return 1; }
101
102 mts_init ();
103
104 arguments = getarguments (invo_name, argc, argv, 1);
105 argp = arguments;
106
107 while ((cp = *argp++)) {
108 if (*cp == '-') {
109 switch (smatch (++cp, switches)) {
110 case AMBIGSW:
111 ambigsw (cp, switches);
112 done (1);
113 case UNKWNSW:
114 adios (NULL, "-%s unknown", cp);
115
116 case HELPSW:
117 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
118 invo_name);
119 print_help (buf, switches, 1);
120 done (0);
121 case VERSIONSW:
122 print_version(invo_name);
123 done (0);
124
125 case DATESW:
126 datesw++;
127 continue;
128 case NDATESW:
129 datesw = 0;
130 continue;
131
132 case NOTESW:
133 if (!(cp = *argp++) || *cp == '-')
134 adios (NULL, "missing argument to %s", argp[-2]);
135 notifysw |= donote (cp, 1);
136 continue;
137 case NNOTESW:
138 if (!(cp = *argp++) || *cp == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
140 notifysw &= ~donote (cp, 0);
141 continue;
142
143 case HOSTSW:
144 if (!(host = *argp++) || *host == '-')
145 adios (NULL, "missing argument to %s", argp[-2]);
146 continue;
147
148 case PORTSW:
149 if (!(port = *argp++) || *port == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
151 continue;
152
153 case USERSW:
154 if (!(cp = *argp++) || *cp == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
156 if (vecp >= MAXVEC-1)
157 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
158 else
159 user = vec[vecp++] = cp;
160 continue;
161
162 case SNOOPSW:
163 snoop++;
164 continue;
165
166 case SASLSW:
167 sasl++;
168 continue;
169
170 case NOSASLSW:
171 sasl = 0;
172 continue;
173
174 case SASLMECHSW:
175 if (!(saslmech = *argp++) || *saslmech == '-')
176 adios (NULL, "missing argument to %s", argp[-2]);
177 continue;
178
179 case INITTLSSW:
180 tls++;
181 continue;
182
183 case NOTLSSW:
184 tls = 0;
185 continue;
186
187 case CERTVERSW:
188 noverify = 0;
189 continue;
190
191 case NOCERTVERSW:
192 noverify++;
193 continue;
194
195 case AUTHSERVICESW:
196 #ifdef OAUTH_SUPPORT
197 if (!(auth_svc = *argp++) || *auth_svc == '-')
198 adios (NULL, "missing argument to %s", argp[-2]);
199 #else
200 adios (NULL, "not built with OAuth support");
201 #endif
202 continue;
203
204 case PROXYSW:
205 if (!(proxy = *argp++) || *proxy == '-')
206 adios (NULL, "missing argument to %s", argp[-2]);
207 continue;
208 }
209 }
210 if (vecp >= MAXVEC-1)
211 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
212 else
213 vec[vecp++] = cp;
214 }
215
216 /*
217 * If -host is not specified by user
218 */
219 if (!host || !*host) {
220 /*
221 * If "pophost" is specified in mts.conf,
222 * use it as default value.
223 */
224 if (pophost && *pophost)
225 host = pophost;
226 }
227 if (!host || !*host)
228 host = NULL;
229
230 if (vecp != 0)
231 vec[vecp] = NULL;
232
233 if (host) {
234 int tlsflag = 0;
235
236 if (tls)
237 tlsflag |= P_INITTLS;
238
239 if (noverify)
240 tlsflag |= P_NOVERIFY;
241
242 if (vecp == 0) {
243 status = remotemail (host, port, user, proxy, notifysw, 1,
244 snoop, sasl, saslmech, tlsflag, auth_svc);
245 } else {
246 for (vecp = 0; vec[vecp]; vecp++)
247 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
248 snoop, sasl, saslmech, tlsflag, auth_svc);
249 }
250 } else {
251 if (user == NULL) user = getusername ();
252 if (vecp == 0) {
253 char *home;
254
255 /* Not sure this check makes sense... */
256 if (!geteuid() || NULL == (home = getenv("HOME"))) {
257 pw = getpwnam (user);
258 if (pw == NULL)
259 adios (NULL, "unable to get information about user");
260 home = pw->pw_dir;
261 }
262 status = checkmail (user, home, datesw, notifysw, 1);
263 } else {
264 for (vecp = 0; vec[vecp]; vecp++) {
265 if ((pw = getpwnam (vec[vecp])))
266 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
267 else
268 advise (NULL, "no such user as %s", vec[vecp]);
269 }
270 }
271 } /* host == NULL */
272
273 done (status);
274 return 1;
275 }
276
277
278 #define NOTE_SWITCHES \
279 X("all", 0, NALLSW) \
280 X("mail", 0, NMAISW) \
281 X("nomail", 0, NNMAISW) \
282
283 #define X(sw, minchars, id) id,
284 DEFINE_SWITCH_ENUM(NOTE);
285 #undef X
286
287 #define X(sw, minchars, id) { sw, minchars, id },
288 DEFINE_SWITCH_ARRAY(NOTE, ntswitches);
289 #undef X
290
291
292 static int
293 donote (char *cp, int ntflag)
294 {
295 switch (smatch (cp, ntswitches)) {
296 case AMBIGSW:
297 ambigsw (cp, ntswitches);
298 done (1);
299 case UNKWNSW:
300 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
301
302 case NALLSW:
303 return NT_ALL;
304 case NMAISW:
305 return NT_MAIL;
306 case NNMAISW:
307 return NT_NMAI;
308 }
309
310 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
311 }
312
313
314 static int
315 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
316 {
317 int mf, status;
318 char buffer[BUFSIZ];
319 struct stat st;
320
321 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
322 if (datesw) {
323 st.st_size = 0;
324 st.st_atime = st.st_mtime = 0;
325 }
326 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
327 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
328
329 if ((mf & UUCPOK) || (mf & MMDFOK)) {
330 if (notifysw & NT_MAIL) {
331 if (personal)
332 printf ("You have ");
333 else
334 printf ("%s has ", user);
335 if (mf & UUCPOK)
336 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
337 if ((mf & UUCPOK) && (mf & MMDFOK))
338 printf (" and ");
339 if (mf & MMDFOK)
340 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
341 mf & UUCPOK ? " Internet" : "");
342 printf (" mail waiting");
343 } else {
344 notifysw = 0;
345 }
346 status = 0;
347 }
348 else {
349 if (notifysw & NT_NMAI)
350 printf (personal ? "You don't %s%s" : "%s doesn't %s",
351 personal ? "" : user, "have any mail waiting");
352 else
353 notifysw = 0;
354
355 status = 1;
356 }
357
358 if (notifysw)
359 if (datesw && st.st_atime)
360 printf ("; last read on %s", dtime (&st.st_atime, 1));
361 if (notifysw)
362 putchar('\n');
363
364 return status;
365 }
366
367
368 extern char response[];
369
370 static int
371 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
372 int personal, int snoop, int sasl, char *saslmech, int tls,
373 const char *auth_svc)
374 {
375 int nmsgs, nbytes, status;
376
377 if (auth_svc == NULL) {
378 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
379 adios (NULL, "must specify -authservice with -saslmech xoauth2");
380 }
381 } else {
382 if (user == NULL) {
383 adios (NULL, "must specify -user with -saslmech xoauth2");
384 }
385 }
386
387 /* open the POP connection */
388 if (pop_init (host, port, user, proxy, snoop, sasl, saslmech, tls,
389 auth_svc) == NOTOK
390 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
391 || pop_quit () == NOTOK) { /* quit POP connection */
392 advise (NULL, "%s", response);
393 return 1;
394 }
395
396 if (nmsgs) {
397 if (notifysw & NT_MAIL) {
398 if (personal)
399 printf ("You have ");
400 else
401 printf ("%s has ", user);
402
403 printf ("%d message%s (%d bytes)",
404 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
405 }
406 else
407 notifysw = 0;
408
409 status = 0;
410 } else {
411 if (notifysw & NT_NMAI)
412 printf (personal ? "You don't %s%s" : "%s doesn't %s",
413 personal ? "" : user, "have any mail waiting");
414 else
415 notifysw = 0;
416 status = 1;
417 }
418 if (notifysw)
419 printf (" on %s\n", host);
420
421 return status;
422 }