2 * msgchk.c -- check for mail
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
14 static struct swit switches[] = {
22 { "nonotify type", 0 },
30 * Maximum numbers of users we can check (plus
31 * one for the NULL vector at the end).
38 #define NT_ALL (NT_MAIL | NT_NMAI)
43 #define UUCPOK (UUCPOLD | UUCPNEW)
46 #define MMDFOK (MMDFOLD | MMDFNEW)
52 static int donote (char *, int);
53 static int checkmail (char *, char *, int, int, int);
57 main (int argc, char **argv)
59 int datesw = 1, notifysw = NT_ALL;
65 char **argp, **arguments, *vec[MAXVEC];
69 setlocale(LC_ALL, "");
71 invo_name = r1bindex (argv[0], '/');
73 /* read user profile/context */
80 arguments = getarguments (invo_name, argc, argv, 1);
83 while ((cp = *argp++)) {
85 switch (smatch (++cp, switches)) {
87 ambigsw (cp, switches);
90 adios (NULL, "-%s unknown", cp);
93 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
95 print_help (buf, switches, 1);
98 print_version(invo_name);
109 if (!(cp = *argp++) || *cp == '-')
110 adios (NULL, "missing argument to %s", argp[-2]);
111 notifysw |= donote (cp, 1);
114 if (!(cp = *argp++) || *cp == '-')
115 adios (NULL, "missing argument to %s", argp[-2]);
116 notifysw &= ~donote (cp, 0);
120 if (vecp >= MAXVEC-1)
121 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
132 /* Not sure this check makes sense... */
133 if (!geteuid() || NULL == (home = getenv("HOME"))) {
134 pw = getpwnam (user);
136 adios (NULL, "unable to get information about user");
139 status = checkmail (user, home, datesw, notifysw, 1);
141 for (vecp = 0; vec[vecp]; vecp++) {
142 if ((pw = getpwnam (vec[vecp])))
143 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
145 advise (NULL, "no such user as %s", vec[vecp]);
154 static struct swit ntswitches[] = {
166 donote (char *cp, int ntflag)
168 switch (smatch (cp, ntswitches)) {
170 ambigsw (cp, ntswitches);
173 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
183 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
188 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
194 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
197 st.st_atime = st.st_mtime = 0;
199 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
200 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
202 if ((mf & UUCPOK) || (mf & MMDFOK)) {
203 if (notifysw & NT_MAIL) {
204 printf (personal ? "You have " : "%s has ", user);
206 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
207 if ((mf & UUCPOK) && (mf & MMDFOK))
210 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
211 mf & UUCPOK ? " Internet" : "");
212 printf (" mail waiting");
219 if (notifysw & NT_NMAI)
220 printf (personal ? "You don't %s%s" : "%s doesn't %s",
221 personal ? "" : user, "have any mail waiting");
229 if (datesw && st.st_atime)
230 printf ("; last read on %s", dtime (&st.st_atime, 1));