]> diplodocus.org Git - nmh/blob - uip/msgchk.c
sendsbr.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/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
15 #include "h/mts.h"
16 #include "h/tws.h"
17 #include "h/done.h"
18 #include "h/utils.h"
19 #include <pwd.h>
20
21 #include "h/popsbr.h"
22
23 #ifndef CYRUS_SASL
24 # define SASLminc(a) (a)
25 #else
26 # define SASLminc(a) 0
27 #endif
28
29 #ifndef TLS_SUPPORT
30 # define TLSminc(a) (a)
31 #else
32 # define TLSminc(a) 0
33 #endif
34
35 #define MSGCHK_SWITCHES \
36 X("date", 0, DATESW) \
37 X("nodate", 0, NDATESW) \
38 X("notify type", 0, NOTESW) \
39 X("nonotify type", 0, NNOTESW) \
40 X("host hostname", 0, HOSTSW) \
41 X("user username", 0, USERSW) \
42 X("port name/number", 0, PORTSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45 X("snoop", 0, SNOOPSW) \
46 X("sasl", SASLminc(4), SASLSW) \
47 X("nosasl", SASLminc(6), NOSASLSW) \
48 X("saslmech", SASLminc(5), SASLMECHSW) \
49 X("authservice", SASLminc(0), AUTHSERVICESW) \
50 X("initialtls", TLSminc(-10), INITTLSSW) \
51 X("notls", TLSminc(-5), NOTLSSW) \
52 X("certverify", TLSminc(-10), CERTVERSW) \
53 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
54 X("proxy command", 0, PROXYSW) \
55
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(MSGCHK);
58 #undef X
59
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(MSGCHK, switches);
62 #undef X
63
64 /*
65 * Maximum numbers of users we can check (plus
66 * one for the NULL vector at the end).
67 */
68 #define MAXVEC 51
69
70 #define NT_MAIL 0x1
71 #define NT_NMAI 0x2
72 #define NT_ALL (NT_MAIL | NT_NMAI)
73
74
75 /*
76 * static prototypes
77 */
78 static int donote (char *, int) PURE;
79 static int checkmail (char *, char *, int, int, int);
80 static int remotemail (char *, char *, char *, char *, int, int, int, int,
81 char *, int, const char *);
82
83
84 int
85 main (int argc, char **argv)
86 {
87 bool datesw = true;
88 int notifysw = NT_ALL;
89 int status = 0;
90 bool sasl = false;
91 bool tls = false;
92 bool noverify = false;
93 bool snoop = false;
94 int vecp = 0;
95 char *cp, *host = NULL, *port = NULL, *user = NULL, *proxy = NULL;
96 char buf[BUFSIZ], *saslmech = NULL, *auth_svc = NULL;
97 char **argp, **arguments, *vec[MAXVEC];
98 struct passwd *pw;
99
100 if (nmh_init(argv[0], true, true)) { return 1; }
101
102 mts_init ();
103
104 arguments = getarguments (invo_name, argc, argv, 1);
105 argp = arguments;
106
107 while ((cp = *argp++)) {
108 if (*cp == '-') {
109 switch (smatch (++cp, switches)) {
110 case AMBIGSW:
111 ambigsw (cp, switches);
112 done (1);
113 case UNKWNSW:
114 die("-%s unknown", cp);
115
116 case HELPSW:
117 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
118 invo_name);
119 print_help (buf, switches, 1);
120 done (0);
121 case VERSIONSW:
122 print_version(invo_name);
123 done (0);
124
125 case DATESW:
126 datesw = true;
127 continue;
128 case NDATESW:
129 datesw = false;
130 continue;
131
132 case NOTESW:
133 if (!(cp = *argp++) || *cp == '-')
134 die("missing argument to %s", argp[-2]);
135 notifysw |= donote (cp, 1);
136 continue;
137 case NNOTESW:
138 if (!(cp = *argp++) || *cp == '-')
139 die("missing argument to %s", argp[-2]);
140 notifysw &= ~donote (cp, 0);
141 continue;
142
143 case HOSTSW:
144 if (!(host = *argp++) || *host == '-')
145 die("missing argument to %s", argp[-2]);
146 continue;
147
148 case PORTSW:
149 if (!(port = *argp++) || *port == '-')
150 die("missing argument to %s", argp[-2]);
151 continue;
152
153 case USERSW:
154 if (!(cp = *argp++) || *cp == '-')
155 die("missing argument to %s", argp[-2]);
156 if (vecp >= MAXVEC-1)
157 die("you can only check %d users at a time", MAXVEC-1);
158 user = vec[vecp++] = cp;
159 continue;
160
161 case SNOOPSW:
162 snoop = true;
163 continue;
164
165 case SASLSW:
166 sasl = true;
167 continue;
168
169 case NOSASLSW:
170 sasl = false;
171 continue;
172
173 case SASLMECHSW:
174 if (!(saslmech = *argp++) || *saslmech == '-')
175 die("missing argument to %s", argp[-2]);
176 continue;
177
178 case INITTLSSW:
179 tls = true;
180 continue;
181
182 case NOTLSSW:
183 tls = false;
184 continue;
185
186 case CERTVERSW:
187 noverify = false;
188 continue;
189
190 case NOCERTVERSW:
191 noverify = true;
192 continue;
193
194 case AUTHSERVICESW:
195 #ifdef OAUTH_SUPPORT
196 if (!(auth_svc = *argp++) || *auth_svc == '-')
197 die("missing argument to %s", argp[-2]);
198 #else
199 die("not built with OAuth support");
200 #endif
201 continue;
202
203 case PROXYSW:
204 if (!(proxy = *argp++) || *proxy == '-')
205 die("missing argument to %s", argp[-2]);
206 continue;
207 }
208 }
209 if (vecp >= MAXVEC-1)
210 die("you can only check %d users at a time", MAXVEC-1);
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 die("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 die("-%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 char buffer[BUFSIZ];
316 snprintf(buffer, sizeof buffer, "%s/%s", *mmdfldir ? mmdfldir : home,
317 *mmdflfil ? mmdflfil : user);
318
319 struct stat st;
320 bool statok = stat(buffer, &st) != -1;
321 bool empty = !statok || st.st_size == 0;
322
323 if ((empty && !(notifysw & NT_NMAI)) ||
324 (!empty && !(notifysw & NT_MAIL))) {
325 return empty;
326 }
327
328 if (empty) {
329 if (personal) {
330 fputs("You don't have any mail waiting", stdout);
331 } else {
332 printf("%s doesn't have any mail waiting", user);
333 }
334 } else {
335 char *kind = st.st_mtime < st.st_atime ? "old" : "new";
336 if (personal) {
337 printf("You have %s mail waiting", kind);
338 } else {
339 printf("%s has %s mail waiting", user, kind);
340 }
341 }
342
343 if (datesw && statok && st.st_atime)
344 printf("; last read on %s", dtime(&st.st_atime, 1));
345 putchar('\n');
346
347 return empty;
348 }
349
350
351 extern char response[];
352
353 static int
354 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
355 int personal, int snoop, int sasl, char *saslmech, int tls,
356 const char *auth_svc)
357 {
358 int nmsgs, nbytes;
359 bool status;
360
361 if (auth_svc == NULL) {
362 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
363 die("must specify -authservice with -saslmech xoauth2");
364 }
365 } else {
366 if (user == NULL) {
367 die("must specify -user with -saslmech xoauth2");
368 }
369 }
370
371 /* open the POP connection */
372 if (pop_init (host, port, user, proxy, snoop, sasl, saslmech, tls,
373 auth_svc) == NOTOK
374 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
375 || pop_quit () == NOTOK) { /* quit POP connection */
376 inform("%s", response);
377 return 1;
378 }
379
380 if (nmsgs) {
381 if (notifysw & NT_MAIL) {
382 if (personal)
383 fputs("You have ", stdout);
384 else
385 printf ("%s has ", user);
386
387 printf ("%d message%s (%d bytes)",
388 nmsgs, PLURALS(nmsgs), nbytes);
389 }
390 else
391 notifysw = 0;
392
393 status = false;
394 } else {
395 if (notifysw & NT_NMAI)
396 printf (personal ? "You don't %s%s" : "%s doesn't %s",
397 personal ? "" : user, "have any mail waiting");
398 else
399 notifysw = 0;
400 status = true;
401 }
402 if (notifysw)
403 printf (" on %s\n", host);
404
405 return status;
406 }