3 * msgchk.c -- check for mail
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
18 # include <h/popsbr.h>
26 # define POPminc(a) (a)
32 # define RPOPminc(a) (a)
34 # define RPOPminc(a) 0
38 # define APOPminc(a) (a)
40 # define APOPminc(a) 0
44 # define KPOPminc(a) (a)
46 # define KPOPminc(a) 0
50 # define SASLminc(a) (a)
52 # define SASLminc(a) 0
55 static struct swit switches[] = {
63 { "nonotify type", 0 },
65 { "host hostname", POPminc (-4) },
67 { "user username", POPminc (-4) },
69 { "apop", APOPminc (-4) },
71 { "noapop", APOPminc (-6) },
73 { "rpop", RPOPminc (-4) },
75 { "norpop", RPOPminc (-6) },
83 { "kpop", KPOPminc (-4) },
85 { "sasl", SASLminc(-4) },
87 { "saslmech", SASLminc(-5) },
92 * Maximum numbers of users we can check (plus
93 * one for the NULL vector at the end).
100 #define NT_ALL (NT_MAIL | NT_NMAI)
105 #define UUCPOK (UUCPOLD | UUCPNEW)
108 #define MMDFOK (MMDFOLD | MMDFNEW)
114 static int donote (char *, int);
115 static int checkmail (char *, char *, int, int, int);
118 static int remotemail (char *, char *, int, int, int, int, int, int, char *);
123 main (int argc, char **argv)
125 int datesw = 1, notifysw = NT_ALL;
126 int rpop, status = 0;
127 int kpop = 0, sasl = 0;
128 int snoop = 0, vecp = 0;
130 char *cp, *host = NULL, *user, buf[BUFSIZ], *saslmech = NULL;
131 char **argp, **arguments, *vec[MAXVEC];
135 struct hes_postoffice *po;
140 setlocale(LC_ALL, "");
142 invo_name = r1bindex (argv[0], '/');
144 /* read user profile/context */
147 mts_init (invo_name);
149 user = getusername();
151 arguments = getarguments (invo_name, argc, argv, 1);
155 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
161 while ((cp = *argp++)) {
163 switch (smatch (++cp, switches)) {
165 ambigsw (cp, switches);
168 adios (NULL, "-%s unknown", cp);
171 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
173 print_help (buf, switches, 1);
176 print_version(invo_name);
187 if (!(cp = *argp++) || *cp == '-')
188 adios (NULL, "missing argument to %s", argp[-2]);
189 notifysw |= donote (cp, 1);
192 if (!(cp = *argp++) || *cp == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
194 notifysw &= ~donote (cp, 0);
198 if (!(host = *argp++) || *host == '-')
199 adios (NULL, "missing argument to %s", argp[-2]);
202 if (!(cp = *argp++) || *cp == '-')
203 adios (NULL, "missing argument to %s", argp[-2]);
204 if (vecp >= MAXVEC-1)
205 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
237 if (!(saslmech = *argp++) || *saslmech == '-')
238 adios (NULL, "missing argument to %s", argp[-2]);
242 if (vecp >= MAXVEC-1)
243 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
250 * If -host is not specified by user
252 if (!host || !*host) {
256 * use MAILHOST environment variable if present,
258 * If that fails, use the default (if any)
259 * provided by mts.conf in mts_init()
261 if ((tmphost = getenv("MAILHOST")) != NULL)
263 else if ((po = hes_getmailhost(vecp ? vec[0] : user)) != NULL &&
264 strcmp(po->po_type, "POP") == 0)
265 pophost = po->po_host;
268 * If "pophost" is specified in mts.conf,
269 * use it as default value.
271 if (pophost && *pophost)
276 if (!host || rpop <= 0)
285 if ( strcmp( POPSERVICE, "kpop" ) == 0 ) {
289 status = remotemail (host, user, rpop, kpop, notifysw, 1, snoop,
292 for (vecp = 0; vec[vecp]; vecp++)
293 status += remotemail (host, vec[vecp], rpop, kpop, notifysw, 0,
294 snoop, sasl, saslmech);
302 home = (uid = geteuid()) ? home = getenv ("HOME") : NULL;
304 pw = getpwnam (user);
306 adios (NULL, "unable to get information about user");
310 status = checkmail (user, home, datesw, notifysw, 1);
312 for (vecp = 0; vec[vecp]; vecp++) {
313 if ((pw = getpwnam (vec[vecp])))
314 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
316 advise (NULL, "no such user as %s", vec[vecp]);
323 return done (status);
327 static struct swit ntswitches[] = {
339 donote (char *cp, int ntflag)
341 switch (smatch (cp, ntswitches)) {
343 ambigsw (cp, ntswitches);
346 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
356 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
361 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
367 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
370 st.st_atime = st.st_mtime = 0;
372 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
373 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
375 if ((mf & UUCPOK) || (mf & MMDFOK)) {
376 if (notifysw & NT_MAIL) {
377 printf (personal ? "You have " : "%s has ", user);
379 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
380 if ((mf & UUCPOK) && (mf & MMDFOK))
383 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
384 mf & UUCPOK ? " Internet" : "");
385 printf (" mail waiting");
392 if (notifysw & NT_NMAI)
393 printf (personal ? "You don't %s%s" : "%s doesn't %s",
394 personal ? "" : user, "have any mail waiting");
402 if (datesw && st.st_atime)
403 printf ("; last read on %s", dtime (&st.st_atime, 1));
412 extern char response[];
415 remotemail (char *host, char *user, int rpop, int kpop, int notifysw, int personal, int snoop, int sasl, char *saslmech)
417 int nmsgs, nbytes, status;
421 user = getusername ();
422 if (kpop || sasl || (rpop > 0))
423 pass = getusername ();
425 ruserpass (host, &user, &pass);
427 /* open the POP connection */
428 if (pop_init (host, user, pass, snoop, kpop ? 1 : rpop, kpop,
429 sasl, saslmech) == NOTOK
430 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
431 || pop_quit () == NOTOK) { /* quit POP connection */
432 advise (NULL, "%s", response);
437 if (notifysw & NT_MAIL) {
438 printf (personal ? "You have " : "%s has ", user);
439 printf ("%d message%s (%d bytes)",
440 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
447 if (notifysw & NT_NMAI)
448 printf (personal ? "You don't %s%s" : "%s doesn't %s",
449 personal ? "" : user, "have any mail waiting");
455 printf (" on %s\n", host);