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.
18 # define SASLminc(a) (a)
20 # define SASLminc(a) 0
23 static struct swit switches[] = {
31 { "nonotify type", 0 },
33 { "host hostname", 0 },
35 { "user username", 0 },
37 { "port name/number", 0 },
45 { "sasl", SASLminc(-4) },
47 { "saslmech", SASLminc(-5) },
49 { "proxy command", 0 },
54 * Maximum numbers of users we can check (plus
55 * one for the NULL vector at the end).
61 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
64 #define NT_ALL (NT_MAIL | NT_NMAI)
69 #define UUCPOK (UUCPOLD | UUCPNEW)
72 #define MMDFOK (MMDFOLD | MMDFNEW)
78 static int donote (char *, int);
79 static int checkmail (char *, char *, int, int, int);
80 static int remotemail (char *, char *, char *, char *, int, int, int, int,
85 main (int argc, char **argv)
87 int datesw = 1, notifysw = NT_ALL;
88 int status = 0, sasl = 0;
89 int snoop = 0, vecp = 0;
90 char *cp, *host = NULL, *port = NULL, *user, *proxy = NULL;
91 char buf[BUFSIZ], *saslmech = NULL;
92 char **argp, **arguments, *vec[MAXVEC];
96 setlocale(LC_ALL, "");
98 invo_name = r1bindex (argv[0], '/');
100 /* read user profile/context */
103 mts_init (invo_name);
104 user = getusername();
106 arguments = getarguments (invo_name, argc, argv, 1);
109 while ((cp = *argp++)) {
111 switch (smatch (++cp, switches)) {
113 ambigsw (cp, switches);
116 adios (NULL, "-%s unknown", cp);
119 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
121 print_help (buf, switches, 1);
124 print_version(invo_name);
135 if (!(cp = *argp++) || *cp == '-')
136 adios (NULL, "missing argument to %s", argp[-2]);
137 notifysw |= donote (cp, 1);
140 if (!(cp = *argp++) || *cp == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
142 notifysw &= ~donote (cp, 0);
146 if (!(host = *argp++) || *host == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
151 if (!(port = *argp++) || *port == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
156 if (!(cp = *argp++) || *cp == '-')
157 adios (NULL, "missing argument to %s", argp[-2]);
158 if (vecp >= MAXVEC-1)
159 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
173 if (!(saslmech = *argp++) || *saslmech == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
178 if (!(proxy = *argp++) || *proxy == '-')
179 adios (NULL, "missing argument to %s", argp[-2]);
183 if (vecp >= MAXVEC-1)
184 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
190 * If -host is not specified by user
192 if (!host || !*host) {
194 * If "pophost" is specified in mts.conf,
195 * use it as default value.
197 if (pophost && *pophost)
208 status = remotemail (host, port, user, proxy, notifysw, 1,
209 snoop, sasl, saslmech);
211 for (vecp = 0; vec[vecp]; vecp++)
212 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
213 snoop, sasl, saslmech);
220 /* Not sure this check makes sense... */
221 if (!geteuid() || NULL == (home = getenv("HOME"))) {
222 pw = getpwnam (user);
224 adios (NULL, "unable to get information about user");
227 status = checkmail (user, home, datesw, notifysw, 1);
229 for (vecp = 0; vec[vecp]; vecp++) {
230 if ((pw = getpwnam (vec[vecp])))
231 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
233 advise (NULL, "no such user as %s", vec[vecp]);
243 static struct swit ntswitches[] = {
255 donote (char *cp, int ntflag)
257 switch (smatch (cp, ntswitches)) {
259 ambigsw (cp, ntswitches);
262 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
272 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
277 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
283 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
286 st.st_atime = st.st_mtime = 0;
288 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
289 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
291 if ((mf & UUCPOK) || (mf & MMDFOK)) {
292 if (notifysw & NT_MAIL) {
293 printf (personal ? "You have " : "%s has ", user);
295 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
296 if ((mf & UUCPOK) && (mf & MMDFOK))
299 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
300 mf & UUCPOK ? " Internet" : "");
301 printf (" mail waiting");
308 if (notifysw & NT_NMAI)
309 printf (personal ? "You don't %s%s" : "%s doesn't %s",
310 personal ? "" : user, "have any mail waiting");
318 if (datesw && st.st_atime)
319 printf ("; last read on %s", dtime (&st.st_atime, 1));
327 extern char response[];
330 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
331 int personal, int snoop, int sasl, char *saslmech)
333 int nmsgs, nbytes, status;
337 user = getusername ();
339 pass = getusername ();
341 ruserpass (host, &user, &pass);
343 /* open the POP connection */
344 if (pop_init (host, port, user, pass, proxy, snoop, sasl, saslmech) == NOTOK
345 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
346 || pop_quit () == NOTOK) { /* quit POP connection */
347 advise (NULL, "%s", response);
352 if (notifysw & NT_MAIL) {
353 printf (personal ? "You have " : "%s has ", user);
354 printf ("%d message%s (%d bytes)",
355 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
362 if (notifysw & NT_NMAI)
363 printf (personal ? "You don't %s%s" : "%s doesn't %s",
364 personal ? "" : user, "have any mail waiting");
370 printf (" on %s\n", host);