3 * msgchk.c -- check for mail
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
15 static struct swit switches[] = {
23 { "nonotify type", 0 },
31 * Maximum numbers of users we can check (plus
32 * one for the NULL vector at the end).
39 #define NT_ALL (NT_MAIL | NT_NMAI)
44 #define UUCPOK (UUCPOLD | UUCPNEW)
47 #define MMDFOK (MMDFOLD | MMDFNEW)
53 static int donote (char *, int);
54 static int checkmail (char *, char *, int, int, int);
58 main (int argc, char **argv)
60 int datesw = 1, notifysw = NT_ALL;
66 char **argp, **arguments, *vec[MAXVEC];
70 setlocale(LC_ALL, "");
72 invo_name = r1bindex (argv[0], '/');
74 /* read user profile/context */
81 arguments = getarguments (invo_name, argc, argv, 1);
84 while ((cp = *argp++)) {
86 switch (smatch (++cp, switches)) {
88 ambigsw (cp, switches);
91 adios (NULL, "-%s unknown", cp);
94 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
96 print_help (buf, switches, 1);
99 print_version(invo_name);
110 if (!(cp = *argp++) || *cp == '-')
111 adios (NULL, "missing argument to %s", argp[-2]);
112 notifysw |= donote (cp, 1);
115 if (!(cp = *argp++) || *cp == '-')
116 adios (NULL, "missing argument to %s", argp[-2]);
117 notifysw &= ~donote (cp, 0);
121 if (vecp >= MAXVEC-1)
122 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
133 /* Not sure this check makes sense... */
134 if (!geteuid() || NULL == (home = getenv("HOME"))) {
135 pw = getpwnam (user);
137 adios (NULL, "unable to get information about user");
140 status = checkmail (user, home, datesw, notifysw, 1);
142 for (vecp = 0; vec[vecp]; vecp++) {
143 if ((pw = getpwnam (vec[vecp])))
144 status += checkmail (pw->pw_name, pw->pw_dir, 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", ntflag ? "" : "no", cp);
184 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
189 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
195 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
198 st.st_atime = st.st_mtime = 0;
200 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
201 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
203 if ((mf & UUCPOK) || (mf & MMDFOK)) {
204 if (notifysw & NT_MAIL) {
205 printf (personal ? "You have " : "%s has ", user);
207 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
208 if ((mf & UUCPOK) && (mf & MMDFOK))
211 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
212 mf & UUCPOK ? " Internet" : "");
213 printf (" mail waiting");
220 if (notifysw & NT_NMAI)
221 printf (personal ? "You don't %s%s" : "%s doesn't %s",
222 personal ? "" : user, "have any mail waiting");
230 if (datesw && st.st_atime)
231 printf ("; last read on %s", dtime (&st.st_atime, 1));