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
39 static struct swit switches[] = {
47 { "nonotify type", 0 },
49 { "host hostname", POPminc (-4) },
51 { "user username", POPminc (-4) },
53 { "apop", APOPminc (-4) },
55 { "noapop", APOPminc (-6) },
57 { "rpop", RPOPminc (-4) },
59 { "norpop", RPOPminc (-6) },
70 * Maximum numbers of users we can check (plus
71 * one for the NULL vector at the end).
78 #define NT_ALL (NT_MAIL | NT_NMAI)
83 #define UUCPOK (UUCPOLD | UUCPNEW)
86 #define MMDFOK (MMDFOLD | MMDFNEW)
92 static int donote (char *, int);
93 static int checkmail (char *, char *, int, int, int);
96 static int remotemail (char *, char *, int, int, int, int);
101 main (int argc, char **argv)
103 int datesw = 1, notifysw = NT_ALL;
104 int rpop, status = 0;
105 int snoop = 0, vecp = 0;
107 char *cp, *host = NULL, *user, buf[BUFSIZ];
108 char **argp, **arguments, *vec[MAXVEC];
112 struct hes_postoffice *po;
117 setlocale(LC_ALL, "");
119 invo_name = r1bindex (argv[0], '/');
121 /* read user profile/context */
124 mts_init (invo_name);
126 user = getusername();
128 arguments = getarguments (invo_name, argc, argv, 1);
132 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
142 while ((cp = *argp++)) {
144 switch (smatch (++cp, switches)) {
146 ambigsw (cp, switches);
149 adios (NULL, "-%s unknown", cp);
152 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
154 print_help (buf, switches, 1);
157 print_version(invo_name);
168 if (!(cp = *argp++) || *cp == '-')
169 adios (NULL, "missing argument to %s", argp[-2]);
170 notifysw |= donote (cp, 1);
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 notifysw &= ~donote (cp, 0);
179 if (!(host = *argp++) || *host == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
185 if (vecp >= MAXVEC-1)
186 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
210 if (vecp >= MAXVEC-1)
211 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
218 * If -host is not specified by user
220 if (!host || !*host) {
224 * use MAILHOST environment variable if present,
226 * If that fails, use the default (if any)
227 * provided by mts.conf in mts_init()
229 if ((tmphost = getenv("MAILHOST")) != NULL)
231 else if ((po = hes_getmailhost(vecp ? vec[0] : user)) != NULL &&
232 strcmp(po->po_type, "POP") == 0)
233 pophost = po->po_host;
236 * If "pophost" is specified in mts.conf,
237 * use it as default value.
239 if (pophost && *pophost)
244 if (!host || rpop <= 0)
254 status = remotemail (host, user, rpop, notifysw, 1, snoop);
256 for (vecp = 0; vec[vecp]; vecp++)
257 status += remotemail (host, vec[vecp], rpop, notifysw, 0, snoop);
265 home = (uid = geteuid()) ? home = getenv ("HOME") : NULL;
267 pw = getpwnam (user);
269 adios (NULL, "unable to get information about user");
273 status = checkmail (user, home, datesw, notifysw, 1);
275 for (vecp = 0; vec[vecp]; vecp++) {
276 if ((pw = getpwnam (vec[vecp])))
277 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
279 advise (NULL, "no such user as %s", vec[vecp]);
286 return done (status);
290 static struct swit ntswitches[] = {
302 donote (char *cp, int ntflag)
304 switch (smatch (cp, ntswitches)) {
306 ambigsw (cp, ntswitches);
309 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
319 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
324 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
330 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
333 st.st_atime = st.st_mtime = 0;
335 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
336 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
338 if ((mf & UUCPOK) || (mf & MMDFOK)) {
339 if (notifysw & NT_MAIL) {
340 printf (personal ? "You have " : "%s has ", user);
342 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
343 if ((mf & UUCPOK) && (mf & MMDFOK))
346 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
347 mf & UUCPOK ? " Internet" : "");
348 printf (" mail waiting");
355 if (notifysw & NT_NMAI)
356 printf (personal ? "You don't %s%s" : "%s doesn't %s",
357 personal ? "" : user, "have any mail waiting");
365 if (datesw && st.st_atime)
366 printf ("; last read on %s", dtime (&st.st_atime, 1));
375 extern char response[];
378 remotemail (char *host, char *user, int rpop, int notifysw, int personal, int snoop)
380 int nmsgs, nbytes, status;
384 user = getusername ();
386 pass = getusername ();
388 ruserpass (host, &user, &pass);
390 /* open the POP connection */
391 if (pop_init (host, user, pass, snoop, rpop) == NOTOK
392 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
393 || pop_quit () == NOTOK) { /* quit POP connection */
394 advise (NULL, "%s", response);
399 if (notifysw & NT_MAIL) {
400 printf (personal ? "You have " : "%s has ", user);
401 printf ("%d message%s (%d bytes)",
402 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
409 if (notifysw & NT_NMAI)
410 printf (personal ? "You don't %s%s" : "%s doesn't %s",
411 personal ? "" : user, "have any mail waiting");
417 printf (" on %s\n", host);