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