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 MAILOK (MAILOLD | MAILNEW)
49 static int donote(char *, int);
50 static int checkmail(char *, int, int, int);
54 main(int argc, char **argv)
56 int datesw = 1, notifysw = NT_ALL;
61 char **argp, **arguments, *vec[MAXVEC];
65 setlocale(LC_ALL, "");
67 invo_name = mhbasename(argv[0]);
69 /* read user profile/context */
75 arguments = getarguments(invo_name, argc, argv, 1);
78 while ((cp = *argp++)) {
80 switch (smatch(++cp, switches)) {
82 ambigsw(cp, switches);
85 adios(NULL, "-%s unknown", cp);
88 snprintf(buf, sizeof(buf), "%s [switches] [users ...]", invo_name);
89 print_help(buf, switches, 1);
92 print_version(invo_name);
103 if (!(cp = *argp++) || *cp == '-')
104 adios(NULL, "missing argument to %s",
106 notifysw |= donote(cp, 1);
109 if (!(cp = *argp++) || *cp == '-')
110 adios(NULL, "missing argument to %s",
112 notifysw &= ~donote(cp, 0);
116 if (vecp >= MAXVEC-1)
117 adios(NULL, "you can only check %d users at a time",
124 done(checkmail(user, datesw, notifysw, 1));
129 for (vecp = 0; vec[vecp]; vecp++) {
130 if ((pw = getpwnam(vec[vecp])))
131 status += checkmail(pw->pw_name, datesw, notifysw, 0);
133 advise(NULL, "no such user as %s", vec[vecp]);
140 static struct swit ntswitches[] = {
152 donote(char *cp, int ntflag)
154 switch (smatch(cp, ntswitches)) {
156 ambigsw(cp, ntswitches);
159 adios(NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
169 /* Before 1999-07-15, garbage was returned if control got here. */
175 checkmail(char *user, int datesw, int notifysw, int personal)
184 ** Evaluate the env var and profile only if the check is
185 ** for the calling user.
187 if ((maildrop = getenv("MAILDROP")) && *maildrop) {
188 snprintf(buffer, sizeof buffer, "%s",
190 } else if ((maildrop=context_find("maildrop")) && *maildrop) {
191 snprintf(buffer, sizeof buffer, "%s",
194 snprintf(buffer, sizeof buffer, "%s/%s",
198 snprintf(buffer, sizeof(buffer), "%s/%s", mailspool, user);
202 st.st_atime = st.st_mtime = 0;
204 mf = (stat(buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
205 : st.st_atime <= st.st_mtime ? MAILNEW : MAILOLD;
208 if (notifysw & NT_MAIL) {
212 printf("%s has ", user);
215 printf(mf & MAILOLD ? "old" : "new");
216 printf(" mail waiting");
222 if (notifysw & NT_NMAI) {
224 printf("You don't have any mail waiting");
226 printf("%s doesn't have any mail waiting",
236 if (datesw && st.st_atime) {
237 printf("; last read on %s", dtime(&st.st_atime, 1));