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 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
112 while ((cp = *argp++)) {
114 switch (smatch (++cp, switches)) {
116 ambigsw (cp, switches);
119 adios (NULL, "-%s unknown", cp);
122 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
124 print_help (buf, switches, 1);
127 print_version(invo_name);
138 if (!(cp = *argp++) || *cp == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
140 notifysw |= donote (cp, 1);
143 if (!(cp = *argp++) || *cp == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
145 notifysw &= ~donote (cp, 0);
149 if (!(host = *argp++) || *host == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
154 if (!(port = *argp++) || *port == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
159 if (!(cp = *argp++) || *cp == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
161 if (vecp >= MAXVEC-1)
162 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
176 if (!(saslmech = *argp++) || *saslmech == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
181 if (!(proxy = *argp++) || *proxy == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
186 if (vecp >= MAXVEC-1)
187 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
193 * If -host is not specified by user
195 if (!host || !*host) {
197 * If "pophost" is specified in mts.conf,
198 * use it as default value.
200 if (pophost && *pophost)
211 status = remotemail (host, port, user, proxy, notifysw, 1,
212 snoop, sasl, saslmech);
214 for (vecp = 0; vec[vecp]; vecp++)
215 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
216 snoop, sasl, saslmech);
223 /* Not sure this check makes sense... */
224 if (!geteuid() || NULL == (home = getenv("HOME"))) {
225 pw = getpwnam (user);
227 adios (NULL, "unable to get information about user");
230 status = checkmail (user, home, datesw, notifysw, 1);
232 for (vecp = 0; vec[vecp]; vecp++) {
233 if ((pw = getpwnam (vec[vecp])))
234 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
236 advise (NULL, "no such user as %s", vec[vecp]);
246 static struct swit ntswitches[] = {
258 donote (char *cp, int ntflag)
260 switch (smatch (cp, ntswitches)) {
262 ambigsw (cp, ntswitches);
265 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
275 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
280 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
286 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
289 st.st_atime = st.st_mtime = 0;
291 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
292 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
294 if ((mf & UUCPOK) || (mf & MMDFOK)) {
295 if (notifysw & NT_MAIL) {
296 printf (personal ? "You have " : "%s has ", user);
298 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
299 if ((mf & UUCPOK) && (mf & MMDFOK))
302 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
303 mf & UUCPOK ? " Internet" : "");
304 printf (" mail waiting");
311 if (notifysw & NT_NMAI)
312 printf (personal ? "You don't %s%s" : "%s doesn't %s",
313 personal ? "" : user, "have any mail waiting");
321 if (datesw && st.st_atime)
322 printf ("; last read on %s", dtime (&st.st_atime, 1));
330 extern char response[];
333 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
334 int personal, int snoop, int sasl, char *saslmech)
336 int nmsgs, nbytes, status;
340 user = getusername ();
342 pass = getusername ();
344 ruserpass (host, &user, &pass);
346 /* open the POP connection */
347 if (pop_init (host, port, user, pass, proxy, snoop, sasl, saslmech) == NOTOK
348 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
349 || pop_quit () == NOTOK) { /* quit POP connection */
350 advise (NULL, "%s", response);
355 if (notifysw & NT_MAIL) {
356 printf (personal ? "You have " : "%s has ", user);
357 printf ("%d message%s (%d bytes)",
358 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
365 if (notifysw & NT_NMAI)
366 printf (personal ? "You don't %s%s" : "%s doesn't %s",
367 personal ? "" : user, "have any mail waiting");
373 printf (" on %s\n", host);