]>
diplodocus.org Git - nmh/blob - uip/msgchk.c
1 /* msgchk.c -- check for mail
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
16 # define SASLminc(a) (a)
18 # define SASLminc(a) 0
22 # define TLSminc(a) (a)
27 #define MSGCHK_SWITCHES \
28 X("date", 0, DATESW) \
29 X("nodate", 0, NDATESW) \
30 X("notify type", 0, NOTESW) \
31 X("nonotify type", 0, NNOTESW) \
32 X("host hostname", 0, HOSTSW) \
33 X("user username", 0, USERSW) \
34 X("port name/number", 0, PORTSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
37 X("snoop", 0, SNOOPSW) \
38 X("sasl", SASLminc(4), SASLSW) \
39 X("nosasl", SASLminc(6), NOSASLSW) \
40 X("saslmech", SASLminc(5), SASLMECHSW) \
41 X("authservice", SASLminc(0), AUTHSERVICESW) \
42 X("initialtls", TLSminc(-10), INITTLSSW) \
43 X("notls", TLSminc(-5), NOTLSSW) \
44 X("certverify", TLSminc(-10), CERTVERSW) \
45 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
46 X("proxy command", 0, PROXYSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(MSGCHK
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
57 * Maximum numbers of users we can check (plus
58 * one for the NULL vector at the end).
64 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
67 #define NT_ALL (NT_MAIL | NT_NMAI)
72 #define UUCPOK (UUCPOLD | UUCPNEW)
75 #define MMDFOK (MMDFOLD | MMDFNEW)
81 static int donote (char *, int);
82 static int checkmail (char *, char *, int, int, int);
83 static int remotemail (char *, char *, char *, char *, int, int, int, int,
84 char *, int, const char *);
88 main (int argc
, char **argv
)
90 int datesw
= 1, notifysw
= NT_ALL
;
91 int status
= 0, sasl
= 0, tls
= 0, noverify
= 0;
92 int snoop
= 0, vecp
= 0;
93 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
94 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
95 char **argp
, **arguments
, *vec
[MAXVEC
];
98 if (nmh_init(argv
[0], 1)) { return 1; }
102 arguments
= getarguments (invo_name
, argc
, argv
, 1);
105 while ((cp
= *argp
++)) {
107 switch (smatch (++cp
, switches
)) {
109 ambigsw (cp
, switches
);
112 adios (NULL
, "-%s unknown", cp
);
115 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
117 print_help (buf
, switches
, 1);
120 print_version(invo_name
);
131 if (!(cp
= *argp
++) || *cp
== '-')
132 adios (NULL
, "missing argument to %s", argp
[-2]);
133 notifysw
|= donote (cp
, 1);
136 if (!(cp
= *argp
++) || *cp
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
138 notifysw
&= ~donote (cp
, 0);
142 if (!(host
= *argp
++) || *host
== '-')
143 adios (NULL
, "missing argument to %s", argp
[-2]);
147 if (!(port
= *argp
++) || *port
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
152 if (!(cp
= *argp
++) || *cp
== '-')
153 adios (NULL
, "missing argument to %s", argp
[-2]);
154 if (vecp
>= MAXVEC
-1)
155 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
157 user
= vec
[vecp
++] = cp
;
173 if (!(saslmech
= *argp
++) || *saslmech
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
195 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
198 adios (NULL
, "not built with OAuth support");
203 if (!(proxy
= *argp
++) || *proxy
== '-')
204 adios (NULL
, "missing argument to %s", argp
[-2]);
208 if (vecp
>= MAXVEC
-1)
209 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
215 * If -host is not specified by user
217 if (!host
|| !*host
) {
219 * If "pophost" is specified in mts.conf,
220 * use it as default value.
222 if (pophost
&& *pophost
)
235 tlsflag
|= P_INITTLS
;
238 tlsflag
|= P_NOVERIFY
;
241 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
242 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
244 for (vecp
= 0; vec
[vecp
]; vecp
++)
245 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
246 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
249 if (user
== NULL
) user
= getusername ();
253 /* Not sure this check makes sense... */
254 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
255 pw
= getpwnam (user
);
257 adios (NULL
, "unable to get information about user");
260 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
262 for (vecp
= 0; vec
[vecp
]; vecp
++) {
263 if ((pw
= getpwnam (vec
[vecp
])))
264 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
266 inform("no such user as %s", vec
[vecp
]);
276 #define NOTE_SWITCHES \
277 X("all", 0, NALLSW) \
278 X("mail", 0, NMAISW) \
279 X("nomail", 0, NNMAISW) \
281 #define X(sw, minchars, id) id,
282 DEFINE_SWITCH_ENUM(NOTE
);
285 #define X(sw, minchars, id) { sw, minchars, id },
286 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
291 donote (char *cp
, int ntflag
)
293 switch (smatch (cp
, ntswitches
)) {
295 ambigsw (cp
, ntswitches
);
298 adios (NULL
, "-%snotify %s unknown", ntflag
? "" : "no", cp
);
308 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
313 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
319 snprintf (buffer
, sizeof(buffer
), "%s/%s", mmdfldir
[0] ? mmdfldir
: home
, mmdflfil
[0] ? mmdflfil
: user
);
322 st
.st_atime
= st
.st_mtime
= 0;
324 mf
= (stat (buffer
, &st
) == NOTOK
|| st
.st_size
== 0) ? NONEOK
325 : st
.st_atime
<= st
.st_mtime
? MMDFNEW
: MMDFOLD
;
327 if ((mf
& UUCPOK
) || (mf
& MMDFOK
)) {
328 if (notifysw
& NT_MAIL
) {
330 printf ("You have ");
332 printf ("%s has ", user
);
334 printf ("%s old-style bell", mf
& UUCPOLD
? "old" : "new");
335 if ((mf
& UUCPOK
) && (mf
& MMDFOK
))
338 printf ("%s%s", mf
& MMDFOLD
? "old" : "new",
339 mf
& UUCPOK
? " Internet" : "");
340 printf (" mail waiting");
347 if (notifysw
& NT_NMAI
)
348 printf (personal
? "You don't %s%s" : "%s doesn't %s",
349 personal
? "" : user
, "have any mail waiting");
357 if (datesw
&& st
.st_atime
)
358 printf ("; last read on %s", dtime (&st
.st_atime
, 1));
366 extern char response
[];
369 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
370 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
371 const char *auth_svc
)
373 int nmsgs
, nbytes
, status
;
375 if (auth_svc
== NULL
) {
376 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
377 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
381 adios (NULL
, "must specify -user with -saslmech xoauth2");
385 /* open the POP connection */
386 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
388 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
389 || pop_quit () == NOTOK
) { /* quit POP connection */
390 inform("%s", response
);
395 if (notifysw
& NT_MAIL
) {
397 printf ("You have ");
399 printf ("%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
);