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 = mhbasename(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 ...]", invo_name);
94 print_help(buf, switches, 1);
97 print_version(invo_name);
108 if (!(cp = *argp++) || *cp == '-')
109 adios(NULL, "missing argument to %s",
111 notifysw |= donote(cp, 1);
114 if (!(cp = *argp++) || *cp == '-')
115 adios(NULL, "missing argument to %s",
117 notifysw &= ~donote(cp, 0);
121 if (vecp >= MAXVEC-1)
122 adios(NULL, "you can only check %d users at a time",
134 /* Not sure this check makes sense... */
135 if (!geteuid() || NULL == (home = getenv("HOME"))) {
138 adios(NULL, "unable to get information about user");
141 status = checkmail(user, home, datesw, notifysw, 1);
143 for (vecp = 0; vec[vecp]; vecp++) {
144 if ((pw = getpwnam(vec[vecp])))
145 status += checkmail(pw->pw_name, pw->pw_dir,
146 datesw, notifysw, 0);
148 advise(NULL, "no such user as %s", vec[vecp]);
157 static struct swit ntswitches[] = {
169 donote(char *cp, int ntflag)
171 switch (smatch(cp, ntswitches)) {
173 ambigsw(cp, ntswitches);
176 adios(NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
186 /* Before 1999-07-15, garbage was returned if control got here. */
192 checkmail(char *user, char *home, int datesw, int notifysw, int personal)
198 snprintf(buffer, sizeof(buffer), "%s/%s",
199 mmdfldir[0] ? mmdfldir : home,
200 mmdflfil[0] ? mmdflfil : user);
203 st.st_atime = st.st_mtime = 0;
205 mf = (stat(buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
206 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
208 if ((mf & UUCPOK) || (mf & MMDFOK)) {
209 if (notifysw & NT_MAIL) {
210 printf(personal ? "You have " : "%s has ", user);
212 printf("%s old-style bell",
213 mf & UUCPOLD ? "old" : "new");
214 if ((mf & UUCPOK) && (mf & MMDFOK))
217 printf("%s%s", mf & MMDFOLD ? "old" : "new",
218 mf & UUCPOK ? " Internet" : "");
219 printf(" mail waiting");
226 if (notifysw & NT_NMAI)
227 printf(personal ? "You don't %s%s" : "%s doesn't %s",
228 personal ? "" : user,
229 "have any mail waiting");
237 if (datesw && st.st_atime)
238 printf("; last read on %s", dtime(&st.st_atime, 1));