3 * msgchk.c -- check for mail
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
16 # include <h/popsbr.h>
20 # define POPminc(a) (a)
26 # define SASLminc(a) (a)
28 # define SASLminc(a) 0
31 static struct swit switches[] = {
39 { "nonotify type", 0 },
41 { "host hostname", POPminc (-4) },
43 { "user username", POPminc (-4) },
45 { "port name/number", POPminc(-4) },
53 { "sasl", SASLminc(-4) },
55 { "saslmech", SASLminc(-5) },
57 { "proxy command", POPminc(-5) },
62 * Maximum numbers of users we can check (plus
63 * one for the NULL vector at the end).
70 #define NT_ALL (NT_MAIL | NT_NMAI)
75 #define UUCPOK (UUCPOLD | UUCPNEW)
78 #define MMDFOK (MMDFOLD | MMDFNEW)
84 static int donote (char *, int);
85 static int checkmail (char *, char *, int, int, int);
88 static int remotemail (char *, char *, char *, char *, int, int, int, int,
94 main (int argc, char **argv)
96 int datesw = 1, notifysw = NT_ALL;
97 int status = 0, sasl = 0;
98 int snoop = 0, vecp = 0;
99 char *cp, *host = NULL, *port = NULL, *user, *proxy = NULL;
100 char buf[BUFSIZ], *saslmech = NULL;
101 char **argp, **arguments, *vec[MAXVEC];
105 struct hes_postoffice *po;
110 setlocale(LC_ALL, "");
112 invo_name = r1bindex (argv[0], '/');
114 /* read user profile/context */
117 mts_init (invo_name);
118 user = getusername();
120 arguments = getarguments (invo_name, argc, argv, 1);
124 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
128 while ((cp = *argp++)) {
130 switch (smatch (++cp, switches)) {
132 ambigsw (cp, switches);
135 adios (NULL, "-%s unknown", cp);
138 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
140 print_help (buf, switches, 1);
143 print_version(invo_name);
154 if (!(cp = *argp++) || *cp == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
156 notifysw |= donote (cp, 1);
159 if (!(cp = *argp++) || *cp == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
161 notifysw &= ~donote (cp, 0);
165 if (!(host = *argp++) || *host == '-')
166 adios (NULL, "missing argument to %s", argp[-2]);
170 if (!(port = *argp++) || *port == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
175 if (!(cp = *argp++) || *cp == '-')
176 adios (NULL, "missing argument to %s", argp[-2]);
177 if (vecp >= MAXVEC-1)
178 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
192 if (!(saslmech = *argp++) || *saslmech == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
197 if (!(proxy = *argp++) || *proxy == '-')
198 adios (NULL, "missing argument to %s", argp[-2]);
202 if (vecp >= MAXVEC-1)
203 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
210 * If -host is not specified by user
212 if (!host || !*host) {
214 * If "pophost" is specified in mts.conf,
215 * use it as default value.
217 if (pophost && *pophost)
230 status = remotemail (host, port, user, proxy, notifysw, 1,
231 snoop, sasl, saslmech);
233 for (vecp = 0; vec[vecp]; vecp++)
234 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
235 snoop, sasl, saslmech);
243 /* Not sure this check makes sense... */
244 if (!geteuid() || NULL == (home = getenv("HOME"))) {
245 pw = getpwnam (user);
247 adios (NULL, "unable to get information about user");
250 status = checkmail (user, home, datesw, notifysw, 1);
252 for (vecp = 0; vec[vecp]; vecp++) {
253 if ((pw = getpwnam (vec[vecp])))
254 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
256 advise (NULL, "no such user as %s", vec[vecp]);
268 static struct swit ntswitches[] = {
280 donote (char *cp, int ntflag)
282 switch (smatch (cp, ntswitches)) {
284 ambigsw (cp, ntswitches);
287 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
297 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
302 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
308 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
311 st.st_atime = st.st_mtime = 0;
313 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
314 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
316 if ((mf & UUCPOK) || (mf & MMDFOK)) {
317 if (notifysw & NT_MAIL) {
318 printf (personal ? "You have " : "%s has ", user);
320 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
321 if ((mf & UUCPOK) && (mf & MMDFOK))
324 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
325 mf & UUCPOK ? " Internet" : "");
326 printf (" mail waiting");
333 if (notifysw & NT_NMAI)
334 printf (personal ? "You don't %s%s" : "%s doesn't %s",
335 personal ? "" : user, "have any mail waiting");
343 if (datesw && st.st_atime)
344 printf ("; last read on %s", dtime (&st.st_atime, 1));
353 extern char response[];
356 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
357 int personal, int snoop, int sasl, char *saslmech)
359 int nmsgs, nbytes, status;
363 user = getusername ();
365 pass = getusername ();
367 ruserpass (host, &user, &pass);
369 /* open the POP connection */
370 if (pop_init (host, port, user, pass, proxy, snoop, sasl, saslmech) == NOTOK
371 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
372 || pop_quit () == NOTOK) { /* quit POP connection */
373 advise (NULL, "%s", response);
378 if (notifysw & NT_MAIL) {
379 printf (personal ? "You have " : "%s has ", user);
380 printf ("%d message%s (%d bytes)",
381 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
388 if (notifysw & NT_NMAI)
389 printf (personal ? "You don't %s%s" : "%s doesn't %s",
390 personal ? "" : user, "have any mail waiting");
396 printf (" on %s\n", host);