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