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