3 * msgchk.c -- check for mail
9 #include <zotnet/mts/mts.h>
10 #include <zotnet/tws/tws.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
45 static struct swit switches[] = {
53 { "nonotify type", 0 },
55 { "host hostname", POPminc (-4) },
57 { "user username", POPminc (-4) },
59 { "apop", APOPminc (-4) },
61 { "noapop", APOPminc (-6) },
63 { "rpop", RPOPminc (-4) },
65 { "norpop", RPOPminc (-6) },
73 { "kpop", KPOPminc (-4) },
78 * Maximum numbers of users we can check (plus
79 * one for the NULL vector at the end).
86 #define NT_ALL (NT_MAIL | NT_NMAI)
91 #define UUCPOK (UUCPOLD | UUCPNEW)
94 #define MMDFOK (MMDFOLD | MMDFNEW)
100 static int donote (char *, int);
101 static int checkmail (char *, char *, int, int, int);
104 static int remotemail (char *, char *, int, int, int, int, int);
109 main (int argc, char **argv)
111 int datesw = 1, notifysw = NT_ALL;
112 int rpop, status = 0;
114 int snoop = 0, vecp = 0;
116 char *cp, *host = NULL, *user, buf[BUFSIZ];
117 char **argp, **arguments, *vec[MAXVEC];
121 struct hes_postoffice *po;
126 setlocale(LC_ALL, "");
128 invo_name = r1bindex (argv[0], '/');
130 /* read user profile/context */
133 mts_init (invo_name);
135 user = getusername();
137 arguments = getarguments (invo_name, argc, argv, 1);
141 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
147 while ((cp = *argp++)) {
149 switch (smatch (++cp, switches)) {
151 ambigsw (cp, switches);
154 adios (NULL, "-%s unknown", cp);
157 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
159 print_help (buf, switches, 1);
162 print_version(invo_name);
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 notifysw |= donote (cp, 1);
178 if (!(cp = *argp++) || *cp == '-')
179 adios (NULL, "missing argument to %s", argp[-2]);
180 notifysw &= ~donote (cp, 0);
184 if (!(host = *argp++) || *host == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 if (vecp >= MAXVEC-1)
191 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
219 if (vecp >= MAXVEC-1)
220 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
227 * If -host is not specified by user
229 if (!host || !*host) {
233 * use MAILHOST environment variable if present,
235 * If that fails, use the default (if any)
236 * provided by mts.conf in mts_init()
238 if ((tmphost = getenv("MAILHOST")) != NULL)
240 else if ((po = hes_getmailhost(vecp ? vec[0] : user)) != NULL &&
241 strcmp(po->po_type, "POP") == 0)
242 pophost = po->po_host;
245 * If "pophost" is specified in mts.conf,
246 * use it as default value.
248 if (pophost && *pophost)
253 if (!host || rpop <= 0)
263 status = remotemail (host, user, rpop, kpop, notifysw, 1, snoop);
265 for (vecp = 0; vec[vecp]; vecp++)
266 status += remotemail (host, vec[vecp], rpop, kpop, notifysw, 0, snoop);
274 home = (uid = geteuid()) ? home = getenv ("HOME") : NULL;
276 pw = getpwnam (user);
278 adios (NULL, "unable to get information about user");
282 status = checkmail (user, home, datesw, notifysw, 1);
284 for (vecp = 0; vec[vecp]; vecp++) {
285 if ((pw = getpwnam (vec[vecp])))
286 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
288 advise (NULL, "no such user as %s", vec[vecp]);
295 return done (status);
299 static struct swit ntswitches[] = {
311 donote (char *cp, int ntflag)
313 switch (smatch (cp, ntswitches)) {
315 ambigsw (cp, ntswitches);
318 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
328 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
333 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
339 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
342 st.st_atime = st.st_mtime = 0;
344 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
345 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
347 if ((mf & UUCPOK) || (mf & MMDFOK)) {
348 if (notifysw & NT_MAIL) {
349 printf (personal ? "You have " : "%s has ", user);
351 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
352 if ((mf & UUCPOK) && (mf & MMDFOK))
355 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
356 mf & UUCPOK ? " Internet" : "");
357 printf (" mail waiting");
364 if (notifysw & NT_NMAI)
365 printf (personal ? "You don't %s%s" : "%s doesn't %s",
366 personal ? "" : user, "have any mail waiting");
374 if (datesw && st.st_atime)
375 printf ("; last read on %s", dtime (&st.st_atime, 1));
384 extern char response[];
387 remotemail (char *host, char *user, int rpop, int kpop, int notifysw, int personal, int snoop)
389 int nmsgs, nbytes, status;
393 user = getusername ();
394 if (kpop || (rpop > 0))
395 pass = getusername ();
397 ruserpass (host, &user, &pass);
399 /* open the POP connection */
400 if (pop_init (host, user, pass, snoop, kpop ? 1 : rpop, kpop) == NOTOK
401 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
402 || pop_quit () == NOTOK) { /* quit POP connection */
403 advise (NULL, "%s", response);
408 if (notifysw & NT_MAIL) {
409 printf (personal ? "You have " : "%s has ", user);
410 printf ("%d message%s (%d bytes)",
411 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
418 if (notifysw & NT_NMAI)
419 printf (personal ? "You don't %s%s" : "%s doesn't %s",
420 personal ? "" : user, "have any mail waiting");
426 printf (" on %s\n", host);