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 },
30 ** Maximum numbers of users we can check (plus
31 ** one for the NULL vector at the end).
37 #define NT_ALL (NT_MAIL | NT_NMAI)
42 #define MAILOK (MAILOLD | MAILNEW)
48 static int donote(char *, int);
49 static int checkmail(char *, int, int, int);
53 main(int argc, char **argv)
55 int datesw = 1, notifysw = NT_ALL;
60 char **argp, **arguments, *vec[MAXVEC];
64 setlocale(LC_ALL, "");
66 invo_name = mhbasename(argv[0]);
68 /* read user profile/context */
73 arguments = getarguments(invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch(++cp, switches)) {
80 ambigsw(cp, switches);
83 adios(NULL, "-%s unknown", cp);
86 snprintf(buf, sizeof(buf), "%s [switches] [users ...]", invo_name);
87 print_help(buf, switches, 1);
90 print_version(invo_name);
101 if (!(cp = *argp++) || *cp == '-')
102 adios(NULL, "missing argument to %s",
104 notifysw |= donote(cp, 1);
107 if (!(cp = *argp++) || *cp == '-')
108 adios(NULL, "missing argument to %s",
110 notifysw &= ~donote(cp, 0);
114 if (vecp >= MAXVEC-1)
115 adios(NULL, "you can only check %d users at a time",
122 done(checkmail(user, datesw, notifysw, 1));
127 for (vecp = 0; vec[vecp]; vecp++) {
128 if ((pw = getpwnam(vec[vecp])))
129 status += checkmail(pw->pw_name, datesw, notifysw, 0);
131 advise(NULL, "no such user as %s", vec[vecp]);
138 static struct swit ntswitches[] = {
150 donote(char *cp, int ntflag)
152 switch (smatch(cp, ntswitches)) {
154 ambigsw(cp, ntswitches);
157 adios(NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
167 /* Before 1999-07-15, garbage was returned if control got here. */
173 checkmail(char *user, int datesw, int notifysw, int personal)
182 ** Evaluate the env var and profile only if the check is
183 ** for the calling user.
185 if ((maildrop = getenv("MAILDROP")) && *maildrop) {
186 snprintf(buffer, sizeof buffer, "%s",
188 } else if ((maildrop=context_find("maildrop")) && *maildrop) {
189 snprintf(buffer, sizeof buffer, "%s",
192 snprintf(buffer, sizeof buffer, "%s/%s",
196 snprintf(buffer, sizeof(buffer), "%s/%s", mailspool, user);
200 st.st_atime = st.st_mtime = 0;
202 mf = (stat(buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
203 : st.st_atime <= st.st_mtime ? MAILNEW : MAILOLD;
206 if (notifysw & NT_MAIL) {
210 printf("%s has ", user);
213 printf(mf & MAILOLD ? "old" : "new");
214 printf(" mail waiting");
220 if (notifysw & NT_NMAI) {
222 printf("You don't have any mail waiting");
224 printf("%s doesn't have any mail waiting",
234 if (datesw && st.st_atime) {
235 printf("; last read on %s", dtime(&st.st_atime));