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", argp[-2]);
110 notifysw |= donote(cp, 1);
113 if (!(cp = *argp++) || *cp == '-')
114 adios(NULL, "missing argument to %s", argp[-2]);
115 notifysw &= ~donote(cp, 0);
119 if (vecp >= MAXVEC-1)
120 adios(NULL, "you can only check %d users at a time",
132 /* Not sure this check makes sense... */
133 if (!geteuid() || NULL == (home = getenv("HOME"))) {
136 adios(NULL, "unable to get information about user");
139 status = checkmail(user, home, datesw, notifysw, 1);
141 for (vecp = 0; vec[vecp]; vecp++) {
142 if ((pw = getpwnam(vec[vecp])))
143 status += checkmail(pw->pw_name, pw->pw_dir,
144 datesw, notifysw, 0);
146 advise(NULL, "no such user as %s", vec[vecp]);
155 static struct swit ntswitches[] = {
167 donote(char *cp, int ntflag)
169 switch (smatch(cp, ntswitches)) {
171 ambigsw(cp, ntswitches);
174 adios(NULL, "-%snotify %s unknown",
175 ntflag ? "" : "no", cp);
185 /* Before 1999-07-15, garbage was returned if control got here. */
191 checkmail(char *user, char *home, int datesw, int notifysw, int personal)
197 snprintf(buffer, sizeof(buffer), "%s/%s",
198 mmdfldir[0] ? mmdfldir : home,
199 mmdflfil[0] ? mmdflfil : 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 ? MMDFNEW : MMDFOLD;
207 if ((mf & UUCPOK) || (mf & MMDFOK)) {
208 if (notifysw & NT_MAIL) {
209 printf(personal ? "You have " : "%s has ", user);
211 printf("%s old-style bell",
212 mf & UUCPOLD ? "old" : "new");
213 if ((mf & UUCPOK) && (mf & MMDFOK))
216 printf("%s%s", mf & MMDFOLD ? "old" : "new",
217 mf & UUCPOK ? " Internet" : "");
218 printf(" mail waiting");
225 if (notifysw & NT_NMAI)
226 printf(personal ? "You don't %s%s" : "%s doesn't %s",
227 personal ? "" : user,
228 "have any mail waiting");
236 if (datesw && st.st_atime)
237 printf("; last read on %s", dtime(&st.st_atime, 1));