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