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