3 * msgchk.c -- check for mail
9 #include <zotnet/mts/mts.h>
14 # include <h/popsbr.h>
22 # define POPminc(a) (a)
28 # define RPOPminc(a) (a)
30 # define RPOPminc(a) 0
34 # define APOPminc(a) (a)
36 # define APOPminc(a) 0
40 # define KPOPminc(a) (a)
42 # define KPOPminc(a) 0
46 # define SASLminc(a) (a)
48 # define SASLminc(a) 0
51 static struct swit switches[] = {
59 { "nonotify type", 0 },
61 { "host hostname", POPminc (-4) },
63 { "user username", POPminc (-4) },
65 { "apop", APOPminc (-4) },
67 { "noapop", APOPminc (-6) },
69 { "rpop", RPOPminc (-4) },
71 { "norpop", RPOPminc (-6) },
79 { "kpop", KPOPminc (-4) },
81 { "sasl", SASLminc(-4) },
83 { "saslmech", SASLminc(-5) },
88 * Maximum numbers of users we can check (plus
89 * one for the NULL vector at the end).
96 #define NT_ALL (NT_MAIL | NT_NMAI)
101 #define UUCPOK (UUCPOLD | UUCPNEW)
104 #define MMDFOK (MMDFOLD | MMDFNEW)
110 static int donote (char *, int);
111 static int checkmail (char *, char *, int, int, int);
114 static int remotemail (char *, char *, int, int, int, int, int, int, char *);
119 main (int argc, char **argv)
121 int datesw = 1, notifysw = NT_ALL;
122 int rpop, status = 0;
123 int kpop = 0, sasl = 0;
124 int snoop = 0, vecp = 0;
126 char *cp, *host = NULL, *user, buf[BUFSIZ], *saslmech = NULL;
127 char **argp, **arguments, *vec[MAXVEC];
131 struct hes_postoffice *po;
136 setlocale(LC_ALL, "");
138 invo_name = r1bindex (argv[0], '/');
140 /* read user profile/context */
143 mts_init (invo_name);
145 user = getusername();
147 arguments = getarguments (invo_name, argc, argv, 1);
151 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
157 while ((cp = *argp++)) {
159 switch (smatch (++cp, switches)) {
161 ambigsw (cp, switches);
164 adios (NULL, "-%s unknown", cp);
167 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
169 print_help (buf, switches, 1);
172 print_version(invo_name);
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
185 notifysw |= donote (cp, 1);
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 notifysw &= ~donote (cp, 0);
194 if (!(host = *argp++) || *host == '-')
195 adios (NULL, "missing argument to %s", argp[-2]);
198 if (!(cp = *argp++) || *cp == '-')
199 adios (NULL, "missing argument to %s", argp[-2]);
200 if (vecp >= MAXVEC-1)
201 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
233 if (!(saslmech = *argp++) || *saslmech == '-')
234 adios (NULL, "missing argument to %s", argp[-2]);
238 if (vecp >= MAXVEC-1)
239 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
246 * If -host is not specified by user
248 if (!host || !*host) {
252 * use MAILHOST environment variable if present,
254 * If that fails, use the default (if any)
255 * provided by mts.conf in mts_init()
257 if ((tmphost = getenv("MAILHOST")) != NULL)
259 else if ((po = hes_getmailhost(vecp ? vec[0] : user)) != NULL &&
260 strcmp(po->po_type, "POP") == 0)
261 pophost = po->po_host;
264 * If "pophost" is specified in mts.conf,
265 * use it as default value.
267 if (pophost && *pophost)
272 if (!host || rpop <= 0)
281 if ( strcmp( POPSERVICE, "kpop" ) == 0 ) {
285 status = remotemail (host, user, rpop, kpop, notifysw, 1, snoop,
288 for (vecp = 0; vec[vecp]; vecp++)
289 status += remotemail (host, vec[vecp], rpop, kpop, notifysw, 0,
290 snoop, sasl, saslmech);
298 home = (uid = geteuid()) ? home = getenv ("HOME") : NULL;
300 pw = getpwnam (user);
302 adios (NULL, "unable to get information about user");
306 status = checkmail (user, home, datesw, notifysw, 1);
308 for (vecp = 0; vec[vecp]; vecp++) {
309 if ((pw = getpwnam (vec[vecp])))
310 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
312 advise (NULL, "no such user as %s", vec[vecp]);
319 return done (status);
323 static struct swit ntswitches[] = {
335 donote (char *cp, int ntflag)
337 switch (smatch (cp, ntswitches)) {
339 ambigsw (cp, ntswitches);
342 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
352 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
357 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
363 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
366 st.st_atime = st.st_mtime = 0;
368 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
369 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
371 if ((mf & UUCPOK) || (mf & MMDFOK)) {
372 if (notifysw & NT_MAIL) {
373 printf (personal ? "You have " : "%s has ", user);
375 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
376 if ((mf & UUCPOK) && (mf & MMDFOK))
379 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
380 mf & UUCPOK ? " Internet" : "");
381 printf (" mail waiting");
388 if (notifysw & NT_NMAI)
389 printf (personal ? "You don't %s%s" : "%s doesn't %s",
390 personal ? "" : user, "have any mail waiting");
398 if (datesw && st.st_atime)
399 printf ("; last read on %s", dtime (&st.st_atime, 1));
408 extern char response[];
411 remotemail (char *host, char *user, int rpop, int kpop, int notifysw, int personal, int snoop, int sasl, char *saslmech)
413 int nmsgs, nbytes, status;
417 user = getusername ();
418 if (kpop || sasl || (rpop > 0))
419 pass = getusername ();
421 ruserpass (host, &user, &pass);
423 /* open the POP connection */
424 if (pop_init (host, user, pass, snoop, kpop ? 1 : rpop, kpop,
425 sasl, saslmech) == NOTOK
426 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
427 || pop_quit () == NOTOK) { /* quit POP connection */
428 advise (NULL, "%s", response);
433 if (notifysw & NT_MAIL) {
434 printf (personal ? "You have " : "%s has ", user);
435 printf ("%d message%s (%d bytes)",
436 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
443 if (notifysw & NT_NMAI)
444 printf (personal ? "You don't %s%s" : "%s doesn't %s",
445 personal ? "" : user, "have any mail waiting");
451 printf (" on %s\n", host);