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 */
74 arguments = getarguments(invo_name, argc, argv, 1);
77 while ((cp = *argp++)) {
79 switch (smatch(++cp, switches)) {
81 ambigsw(cp, switches);
84 adios(NULL, "-%s unknown", cp);
87 snprintf(buf, sizeof(buf), "%s [switches] [users ...]", invo_name);
88 print_help(buf, switches, 1);
91 print_version(invo_name);
102 if (!(cp = *argp++) || *cp == '-')
103 adios(NULL, "missing argument to %s",
105 notifysw |= donote(cp, 1);
108 if (!(cp = *argp++) || *cp == '-')
109 adios(NULL, "missing argument to %s",
111 notifysw &= ~donote(cp, 0);
115 if (vecp >= MAXVEC-1)
116 adios(NULL, "you can only check %d users at a time",
123 done(checkmail(user, datesw, notifysw, 1));
128 for (vecp = 0; vec[vecp]; vecp++) {
129 if ((pw = getpwnam(vec[vecp])))
130 status += checkmail(pw->pw_name, datesw, notifysw, 0);
132 advise(NULL, "no such user as %s", vec[vecp]);
139 static struct swit ntswitches[] = {
151 donote(char *cp, int ntflag)
153 switch (smatch(cp, ntswitches)) {
155 ambigsw(cp, ntswitches);
158 adios(NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
168 /* Before 1999-07-15, garbage was returned if control got here. */
174 checkmail(char *user, int datesw, int notifysw, int personal)
183 ** Evaluate the env var and profile only if the check is
184 ** for the calling user.
186 if ((maildrop = getenv("MAILDROP")) && *maildrop) {
187 snprintf(buffer, sizeof buffer, "%s",
189 } else if ((maildrop=context_find("maildrop")) && *maildrop) {
190 snprintf(buffer, sizeof buffer, "%s",
193 snprintf(buffer, sizeof buffer, "%s/%s",
197 snprintf(buffer, sizeof(buffer), "%s/%s", mailspool, user);
201 st.st_atime = st.st_mtime = 0;
203 mf = (stat(buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
204 : st.st_atime <= st.st_mtime ? MAILNEW : MAILOLD;
207 if (notifysw & NT_MAIL) {
211 printf("%s has ", user);
214 printf(mf & MAILOLD ? "old" : "new");
215 printf(" mail waiting");
221 if (notifysw & NT_NMAI) {
223 printf("You don't have any mail waiting");
225 printf("%s doesn't have any mail waiting",
235 if (datesw && st.st_atime) {
236 printf("; last read on %s", dtime(&st.st_atime, 1));