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.
16 # include <h/popsbr.h>
24 # define POPminc(a) (a)
30 # define RPOPminc(a) (a)
32 # define RPOPminc(a) 0
36 # define APOPminc(a) (a)
38 # define APOPminc(a) 0
42 # define KPOPminc(a) (a)
44 # define KPOPminc(a) 0
48 # define SASLminc(a) (a)
50 # define SASLminc(a) 0
53 static struct swit switches[] = {
61 { "nonotify type", 0 },
63 { "host hostname", POPminc (-4) },
65 { "user username", POPminc (-4) },
67 { "apop", APOPminc (-4) },
69 { "noapop", APOPminc (-6) },
71 { "rpop", RPOPminc (-4) },
73 { "norpop", RPOPminc (-6) },
81 { "kpop", KPOPminc (-4) },
83 { "sasl", SASLminc(-4) },
85 { "saslmech", SASLminc(-5) },
87 { "proxy command", POPminc(-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 *, 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, *proxy = NULL;
131 char buf[BUFSIZ], *saslmech = NULL;
132 char **argp, **arguments, *vec[MAXVEC];
136 struct hes_postoffice *po;
141 setlocale(LC_ALL, "");
143 invo_name = r1bindex (argv[0], '/');
145 /* read user profile/context */
148 mts_init (invo_name);
150 user = getusername();
152 arguments = getarguments (invo_name, argc, argv, 1);
156 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
162 while ((cp = *argp++)) {
164 switch (smatch (++cp, switches)) {
166 ambigsw (cp, switches);
169 adios (NULL, "-%s unknown", cp);
172 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
174 print_help (buf, switches, 1);
177 print_version(invo_name);
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 notifysw |= donote (cp, 1);
193 if (!(cp = *argp++) || *cp == '-')
194 adios (NULL, "missing argument to %s", argp[-2]);
195 notifysw &= ~donote (cp, 0);
199 if (!(host = *argp++) || *host == '-')
200 adios (NULL, "missing argument to %s", argp[-2]);
203 if (!(cp = *argp++) || *cp == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
205 if (vecp >= MAXVEC-1)
206 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
238 if (!(saslmech = *argp++) || *saslmech == '-')
239 adios (NULL, "missing argument to %s", argp[-2]);
243 if (!(proxy = *argp++) || *proxy == '-')
244 adios (NULL, "missing argument to %s", argp[-2]);
248 if (vecp >= MAXVEC-1)
249 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
256 * If -host is not specified by user
258 if (!host || !*host) {
262 * use MAILHOST environment variable if present,
264 * If that fails, use the default (if any)
265 * provided by mts.conf in mts_init()
267 if ((tmphost = getenv("MAILHOST")) != NULL)
269 else if ((po = hes_getmailhost(vecp ? vec[0] : user)) != NULL &&
270 strcmp(po->po_type, "POP") == 0)
271 pophost = po->po_host;
274 * If "pophost" is specified in mts.conf,
275 * use it as default value.
277 if (pophost && *pophost)
282 if (!host || rpop <= 0)
291 if ( strcmp( POPSERVICE, "kpop" ) == 0 ) {
295 status = remotemail (host, user, proxy, rpop, kpop, notifysw, 1,
296 snoop, sasl, saslmech);
298 for (vecp = 0; vec[vecp]; vecp++)
299 status += remotemail (host, vec[vecp], proxy, rpop, kpop,
300 notifysw, 0, snoop, sasl, saslmech);
308 /* Not sure this check makes sense... */
309 if (!geteuid() || NULL == (home = getenv("HOME"))) {
310 pw = getpwnam (user);
312 adios (NULL, "unable to get information about user");
315 status = checkmail (user, home, datesw, notifysw, 1);
317 for (vecp = 0; vec[vecp]; vecp++) {
318 if ((pw = getpwnam (vec[vecp])))
319 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
321 advise (NULL, "no such user as %s", vec[vecp]);
333 static struct swit ntswitches[] = {
345 donote (char *cp, int ntflag)
347 switch (smatch (cp, ntswitches)) {
349 ambigsw (cp, ntswitches);
352 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
362 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
367 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
373 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
376 st.st_atime = st.st_mtime = 0;
378 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
379 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
381 if ((mf & UUCPOK) || (mf & MMDFOK)) {
382 if (notifysw & NT_MAIL) {
383 printf (personal ? "You have " : "%s has ", user);
385 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
386 if ((mf & UUCPOK) && (mf & MMDFOK))
389 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
390 mf & UUCPOK ? " Internet" : "");
391 printf (" mail waiting");
398 if (notifysw & NT_NMAI)
399 printf (personal ? "You don't %s%s" : "%s doesn't %s",
400 personal ? "" : user, "have any mail waiting");
408 if (datesw && st.st_atime)
409 printf ("; last read on %s", dtime (&st.st_atime, 1));
418 extern char response[];
421 remotemail (char *host, char *user, char *proxy, int rpop, int kpop, int notifysw, int personal, int snoop, int sasl, char *saslmech)
423 int nmsgs, nbytes, status;
427 user = getusername ();
428 if (kpop || sasl || (rpop > 0))
429 pass = getusername ();
431 ruserpass (host, &user, &pass);
433 /* open the POP connection */
434 if (pop_init (host, user, pass, proxy, snoop, kpop ? 1 : rpop, kpop,
435 sasl, saslmech) == NOTOK
436 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
437 || pop_quit () == NOTOK) { /* quit POP connection */
438 advise (NULL, "%s", response);
443 if (notifysw & NT_MAIL) {
444 printf (personal ? "You have " : "%s has ", user);
445 printf ("%d message%s (%d bytes)",
446 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
453 if (notifysw & NT_NMAI)
454 printf (personal ? "You don't %s%s" : "%s doesn't %s",
455 personal ? "" : user, "have any mail waiting");
461 printf (" on %s\n", host);