]>
diplodocus.org Git - nmh/blob - uip/msgchk.c
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.
18 # define SASLminc(a) (a)
20 # define SASLminc(a) 0
24 # define TLSminc(a) (a)
29 #define MSGCHK_SWITCHES \
30 X("date", 0, DATESW) \
31 X("nodate", 0, NDATESW) \
32 X("notify type", 0, NOTESW) \
33 X("nonotify type", 0, NNOTESW) \
34 X("host hostname", 0, HOSTSW) \
35 X("user username", 0, USERSW) \
36 X("port name/number", 0, PORTSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("snoop", 0, SNOOPSW) \
40 X("sasl", SASLminc(4), SASLSW) \
41 X("nosasl", SASLminc(6), NOSASLSW) \
42 X("saslmech", SASLminc(5), SASLMECHSW) \
43 X("authservice", SASLminc(0), AUTHSERVICESW) \
44 X("initialtls", TLSminc(-10), INITTLSSW) \
45 X("notls", TLSminc(-5), NOTLSSW) \
46 X("certverify", TLSminc(-10), CERTVERSW) \
47 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
48 X("proxy command", 0, PROXYSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MSGCHK
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
59 * Maximum numbers of users we can check (plus
60 * one for the NULL vector at the end).
66 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
69 #define NT_ALL (NT_MAIL | NT_NMAI)
74 #define UUCPOK (UUCPOLD | UUCPNEW)
77 #define MMDFOK (MMDFOLD | MMDFNEW)
83 static int donote (char *, int);
84 static int checkmail (char *, char *, int, int, int);
85 static int remotemail (char *, char *, char *, char *, int, int, int, int,
86 char *, int, const char *);
90 main (int argc
, char **argv
)
92 int datesw
= 1, notifysw
= NT_ALL
;
93 int status
= 0, sasl
= 0, tls
= 0, noverify
= 0;
94 int snoop
= 0, vecp
= 0;
95 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
96 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
97 char **argp
, **arguments
, *vec
[MAXVEC
];
100 if (nmh_init(argv
[0], 1)) { return 1; }
104 arguments
= getarguments (invo_name
, argc
, argv
, 1);
107 while ((cp
= *argp
++)) {
109 switch (smatch (++cp
, switches
)) {
111 ambigsw (cp
, switches
);
114 adios (NULL
, "-%s unknown", cp
);
117 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
119 print_help (buf
, switches
, 1);
122 print_version(invo_name
);
133 if (!(cp
= *argp
++) || *cp
== '-')
134 adios (NULL
, "missing argument to %s", argp
[-2]);
135 notifysw
|= donote (cp
, 1);
138 if (!(cp
= *argp
++) || *cp
== '-')
139 adios (NULL
, "missing argument to %s", argp
[-2]);
140 notifysw
&= ~donote (cp
, 0);
144 if (!(host
= *argp
++) || *host
== '-')
145 adios (NULL
, "missing argument to %s", argp
[-2]);
149 if (!(port
= *argp
++) || *port
== '-')
150 adios (NULL
, "missing argument to %s", argp
[-2]);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 adios (NULL
, "missing argument to %s", argp
[-2]);
156 if (vecp
>= MAXVEC
-1)
157 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
159 user
= vec
[vecp
++] = cp
;
175 if (!(saslmech
= *argp
++) || *saslmech
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
197 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
198 adios (NULL
, "missing argument to %s", argp
[-2]);
200 adios (NULL
, "not built with OAuth support");
205 if (!(proxy
= *argp
++) || *proxy
== '-')
206 adios (NULL
, "missing argument to %s", argp
[-2]);
210 if (vecp
>= MAXVEC
-1)
211 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
217 * If -host is not specified by user
219 if (!host
|| !*host
) {
221 * If "pophost" is specified in mts.conf,
222 * use it as default value.
224 if (pophost
&& *pophost
)
237 tlsflag
|= P_INITTLS
;
240 tlsflag
|= P_NOVERIFY
;
243 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
244 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
246 for (vecp
= 0; vec
[vecp
]; vecp
++)
247 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
248 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
251 if (user
== NULL
) user
= getusername ();
255 /* Not sure this check makes sense... */
256 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
257 pw
= getpwnam (user
);
259 adios (NULL
, "unable to get information about user");
262 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
264 for (vecp
= 0; vec
[vecp
]; vecp
++) {
265 if ((pw
= getpwnam (vec
[vecp
])))
266 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
268 advise (NULL
, "no such user as %s", vec
[vecp
]);
278 #define NOTE_SWITCHES \
279 X("all", 0, NALLSW) \
280 X("mail", 0, NMAISW) \
281 X("nomail", 0, NNMAISW) \
283 #define X(sw, minchars, id) id,
284 DEFINE_SWITCH_ENUM(NOTE
);
287 #define X(sw, minchars, id) { sw, minchars, id },
288 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
293 donote (char *cp
, int ntflag
)
295 switch (smatch (cp
, ntswitches
)) {
297 ambigsw (cp
, ntswitches
);
300 adios (NULL
, "-%snotify %s unknown", ntflag
? "" : "no", cp
);
310 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
315 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
321 snprintf (buffer
, sizeof(buffer
), "%s/%s", mmdfldir
[0] ? mmdfldir
: home
, mmdflfil
[0] ? mmdflfil
: user
);
324 st
.st_atime
= st
.st_mtime
= 0;
326 mf
= (stat (buffer
, &st
) == NOTOK
|| st
.st_size
== 0) ? NONEOK
327 : st
.st_atime
<= st
.st_mtime
? MMDFNEW
: MMDFOLD
;
329 if ((mf
& UUCPOK
) || (mf
& MMDFOK
)) {
330 if (notifysw
& NT_MAIL
) {
332 printf ("You have ");
334 printf ("%s has ", user
);
336 printf ("%s old-style bell", mf
& UUCPOLD
? "old" : "new");
337 if ((mf
& UUCPOK
) && (mf
& MMDFOK
))
340 printf ("%s%s", mf
& MMDFOLD
? "old" : "new",
341 mf
& UUCPOK
? " Internet" : "");
342 printf (" mail waiting");
349 if (notifysw
& NT_NMAI
)
350 printf (personal
? "You don't %s%s" : "%s doesn't %s",
351 personal
? "" : user
, "have any mail waiting");
359 if (datesw
&& st
.st_atime
)
360 printf ("; last read on %s", dtime (&st
.st_atime
, 1));
368 extern char response
[];
371 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
372 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
373 const char *auth_svc
)
375 int nmsgs
, nbytes
, status
;
377 if (auth_svc
== NULL
) {
378 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
379 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
383 adios (NULL
, "must specify -user with -saslmech xoauth2");
387 /* open the POP connection */
388 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
390 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
391 || pop_quit () == NOTOK
) { /* quit POP connection */
392 advise (NULL
, "%s", response
);
397 if (notifysw
& NT_MAIL
) {
399 printf ("You have ");
401 printf ("%s has ", user
);
403 printf ("%d message%s (%d bytes)",
404 nmsgs
, nmsgs
!= 1 ? "s" : "", nbytes
);
411 if (notifysw
& NT_NMAI
)
412 printf (personal
? "You don't %s%s" : "%s doesn't %s",
413 personal
? "" : user
, "have any mail waiting");
419 printf (" on %s\n", host
);