]>
diplodocus.org Git - nmh/blob - uip/msgchk.c
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)
262 if ( strcmp( POPSERVICE
, "kpop" ) == 0 ) {
266 status
= remotemail (host
, user
, rpop
, kpop
, notifysw
, 1, snoop
);
268 for (vecp
= 0; vec
[vecp
]; vecp
++)
269 status
+= remotemail (host
, vec
[vecp
], rpop
, kpop
, notifysw
, 0, snoop
);
277 home
= (uid
= geteuid()) ? home
= getenv ("HOME") : NULL
;
279 pw
= getpwnam (user
);
281 adios (NULL
, "unable to get information about user");
285 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
287 for (vecp
= 0; vec
[vecp
]; vecp
++) {
288 if ((pw
= getpwnam (vec
[vecp
])))
289 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
291 advise (NULL
, "no such user as %s", vec
[vecp
]);
298 return done (status
);
302 static struct swit ntswitches
[] = {
314 donote (char *cp
, int ntflag
)
316 switch (smatch (cp
, ntswitches
)) {
318 ambigsw (cp
, ntswitches
);
321 adios (NULL
, "-%snotify %s unknown", ntflag
? "" : "no", cp
);
331 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
336 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
342 snprintf (buffer
, sizeof(buffer
), "%s/%s", mmdfldir
[0] ? mmdfldir
: home
, mmdflfil
[0] ? mmdflfil
: user
);
345 st
.st_atime
= st
.st_mtime
= 0;
347 mf
= (stat (buffer
, &st
) == NOTOK
|| st
.st_size
== 0) ? NONEOK
348 : st
.st_atime
<= st
.st_mtime
? MMDFNEW
: MMDFOLD
;
350 if ((mf
& UUCPOK
) || (mf
& MMDFOK
)) {
351 if (notifysw
& NT_MAIL
) {
352 printf (personal
? "You have " : "%s has ", user
);
354 printf ("%s old-style bell", mf
& UUCPOLD
? "old" : "new");
355 if ((mf
& UUCPOK
) && (mf
& MMDFOK
))
358 printf ("%s%s", mf
& MMDFOLD
? "old" : "new",
359 mf
& UUCPOK
? " Internet" : "");
360 printf (" mail waiting");
367 if (notifysw
& NT_NMAI
)
368 printf (personal
? "You don't %s%s" : "%s doesn't %s",
369 personal
? "" : user
, "have any mail waiting");
377 if (datesw
&& st
.st_atime
)
378 printf ("; last read on %s", dtime (&st
.st_atime
, 1));
387 extern char response
[];
390 remotemail (char *host
, char *user
, int rpop
, int kpop
, int notifysw
, int personal
, int snoop
)
392 int nmsgs
, nbytes
, status
;
396 user
= getusername ();
397 if (kpop
|| (rpop
> 0))
398 pass
= getusername ();
400 ruserpass (host
, &user
, &pass
);
402 /* open the POP connection */
403 if (pop_init (host
, user
, pass
, snoop
, kpop
? 1 : rpop
, kpop
) == NOTOK
404 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
405 || pop_quit () == NOTOK
) { /* quit POP connection */
406 advise (NULL
, "%s", response
);
411 if (notifysw
& NT_MAIL
) {
412 printf (personal
? "You have " : "%s has ", user
);
413 printf ("%d message%s (%d bytes)",
414 nmsgs
, nmsgs
!= 1 ? "s" : "", nbytes
);
421 if (notifysw
& NT_NMAI
)
422 printf (personal
? "You don't %s%s" : "%s doesn't %s",
423 personal
? "" : user
, "have any mail waiting");
429 printf (" on %s\n", host
);