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.
13 static struct swit switches[] = {
21 { "nonotify type", 0 },
29 ** Maximum numbers of users we can check (plus
30 ** one for the NULL vector at the end).
36 #define NT_ALL (NT_MAIL | NT_NMAI)
41 #define MAILOK (MAILOLD | MAILNEW)
47 static int donote(char *, int);
48 static int checkmail(char *, int, int, int);
52 main(int argc, char **argv)
54 int datesw = 1, notifysw = NT_ALL;
59 char **argp, **arguments, *vec[MAXVEC];
63 setlocale(LC_ALL, "");
65 invo_name = mhbasename(argv[0]);
67 /* read user profile/context */
72 arguments = getarguments(invo_name, argc, argv, 1);
75 while ((cp = *argp++)) {
77 switch (smatch(++cp, switches)) {
79 ambigsw(cp, switches);
82 adios(NULL, "-%s unknown", cp);
85 snprintf(buf, sizeof(buf), "%s [switches] [users ...]", invo_name);
86 print_help(buf, switches, 1);
89 print_version(invo_name);
100 if (!(cp = *argp++) || *cp == '-')
101 adios(NULL, "missing argument to %s",
103 notifysw |= donote(cp, 1);
106 if (!(cp = *argp++) || *cp == '-')
107 adios(NULL, "missing argument to %s",
109 notifysw &= ~donote(cp, 0);
113 if (vecp >= MAXVEC-1)
114 adios(NULL, "you can only check %d users at a time",
121 done(checkmail(user, datesw, notifysw, 1));
126 for (vecp = 0; vec[vecp]; vecp++) {
127 if ((pw = getpwnam(vec[vecp])))
128 status += checkmail(pw->pw_name, datesw, notifysw, 0);
130 advise(NULL, "no such user as %s", vec[vecp]);
137 static struct swit ntswitches[] = {
149 donote(char *cp, int ntflag)
151 switch (smatch(cp, ntswitches)) {
153 ambigsw(cp, ntswitches);
156 adios(NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
166 /* Before 1999-07-15, garbage was returned if control got here. */
172 checkmail(char *user, int datesw, int notifysw, int personal)
181 ** Evaluate the env var and profile only if the check is
182 ** for the calling user.
184 if ((maildrop = getenv("MAILDROP")) && *maildrop) {
185 snprintf(buffer, sizeof buffer, "%s",
187 } else if ((maildrop=context_find("maildrop")) && *maildrop) {
188 snprintf(buffer, sizeof buffer, "%s",
191 snprintf(buffer, sizeof buffer, "%s/%s",
195 snprintf(buffer, sizeof(buffer), "%s/%s", mailspool, user);
199 st.st_atime = st.st_mtime = 0;
201 mf = (stat(buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
202 : st.st_atime <= st.st_mtime ? MAILNEW : MAILOLD;
205 if (notifysw & NT_MAIL) {
209 printf("%s has ", user);
212 printf(mf & MAILOLD ? "old" : "new");
213 printf(" mail waiting");
219 if (notifysw & NT_NMAI) {
221 printf("You don't have any mail waiting");
223 printf("%s doesn't have any mail waiting",
233 if (datesw && st.st_atime) {
234 printf("; last read on %s", dtime(&st.st_atime));