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 struct hes_postoffice *po;
101 setlocale(LC_ALL, "");
103 invo_name = r1bindex (argv[0], '/');
105 /* read user profile/context */
108 mts_init (invo_name);
109 user = getusername();
111 arguments = getarguments (invo_name, argc, argv, 1);
114 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
117 while ((cp = *argp++)) {
119 switch (smatch (++cp, switches)) {
121 ambigsw (cp, switches);
124 adios (NULL, "-%s unknown", cp);
127 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
129 print_help (buf, switches, 1);
132 print_version(invo_name);
143 if (!(cp = *argp++) || *cp == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
145 notifysw |= donote (cp, 1);
148 if (!(cp = *argp++) || *cp == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
150 notifysw &= ~donote (cp, 0);
154 if (!(host = *argp++) || *host == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
159 if (!(port = *argp++) || *port == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
164 if (!(cp = *argp++) || *cp == '-')
165 adios (NULL, "missing argument to %s", argp[-2]);
166 if (vecp >= MAXVEC-1)
167 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
181 if (!(saslmech = *argp++) || *saslmech == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
186 if (!(proxy = *argp++) || *proxy == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
191 if (vecp >= MAXVEC-1)
192 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
198 * If -host is not specified by user
200 if (!host || !*host) {
202 * If "pophost" is specified in mts.conf,
203 * use it as default value.
205 if (pophost && *pophost)
216 status = remotemail (host, port, user, proxy, notifysw, 1,
217 snoop, sasl, saslmech);
219 for (vecp = 0; vec[vecp]; vecp++)
220 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
221 snoop, sasl, saslmech);
228 /* Not sure this check makes sense... */
229 if (!geteuid() || NULL == (home = getenv("HOME"))) {
230 pw = getpwnam (user);
232 adios (NULL, "unable to get information about user");
235 status = checkmail (user, home, datesw, notifysw, 1);
237 for (vecp = 0; vec[vecp]; vecp++) {
238 if ((pw = getpwnam (vec[vecp])))
239 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
241 advise (NULL, "no such user as %s", vec[vecp]);
251 static struct swit ntswitches[] = {
263 donote (char *cp, int ntflag)
265 switch (smatch (cp, ntswitches)) {
267 ambigsw (cp, ntswitches);
270 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
280 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
285 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
291 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
294 st.st_atime = st.st_mtime = 0;
296 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
297 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
299 if ((mf & UUCPOK) || (mf & MMDFOK)) {
300 if (notifysw & NT_MAIL) {
301 printf (personal ? "You have " : "%s has ", user);
303 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
304 if ((mf & UUCPOK) && (mf & MMDFOK))
307 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
308 mf & UUCPOK ? " Internet" : "");
309 printf (" mail waiting");
316 if (notifysw & NT_NMAI)
317 printf (personal ? "You don't %s%s" : "%s doesn't %s",
318 personal ? "" : user, "have any mail waiting");
326 if (datesw && st.st_atime)
327 printf ("; last read on %s", dtime (&st.st_atime, 1));
335 extern char response[];
338 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
339 int personal, int snoop, int sasl, char *saslmech)
341 int nmsgs, nbytes, status;
345 user = getusername ();
347 pass = getusername ();
349 ruserpass (host, &user, &pass);
351 /* open the POP connection */
352 if (pop_init (host, port, user, pass, proxy, snoop, sasl, saslmech) == NOTOK
353 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
354 || pop_quit () == NOTOK) { /* quit POP connection */
355 advise (NULL, "%s", response);
360 if (notifysw & NT_MAIL) {
361 printf (personal ? "You have " : "%s has ", user);
362 printf ("%d message%s (%d bytes)",
363 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
370 if (notifysw & NT_NMAI)
371 printf (personal ? "You don't %s%s" : "%s doesn't %s",
372 personal ? "" : user, "have any mail waiting");
378 printf (" on %s\n", host);