]>
diplodocus.org Git - nmh/blob - uip/msgchk.c
5551e504d3e5079b00a98e0b8bd0d60cbe7c3ac8
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.
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 #define NT_ALL (NT_MAIL | NT_NMAI)
72 static int donote (char *, int) PURE
;
73 static int checkmail (char *, char *, int, int, int);
74 static int remotemail (char *, char *, char *, char *, int, int, int, int,
75 char *, int, const char *);
79 main (int argc
, char **argv
)
81 int datesw
= 1, notifysw
= NT_ALL
;
82 int status
= 0, sasl
= 0, tls
= 0, noverify
= 0;
83 int snoop
= 0, vecp
= 0;
84 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
85 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
86 char **argp
, **arguments
, *vec
[MAXVEC
];
89 if (nmh_init(argv
[0], 1)) { return 1; }
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 adios (NULL
, "-%s unknown", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
122 if (!(cp
= *argp
++) || *cp
== '-')
123 adios (NULL
, "missing argument to %s", argp
[-2]);
124 notifysw
|= donote (cp
, 1);
127 if (!(cp
= *argp
++) || *cp
== '-')
128 adios (NULL
, "missing argument to %s", argp
[-2]);
129 notifysw
&= ~donote (cp
, 0);
133 if (!(host
= *argp
++) || *host
== '-')
134 adios (NULL
, "missing argument to %s", argp
[-2]);
138 if (!(port
= *argp
++) || *port
== '-')
139 adios (NULL
, "missing argument to %s", argp
[-2]);
143 if (!(cp
= *argp
++) || *cp
== '-')
144 adios (NULL
, "missing argument to %s", argp
[-2]);
145 if (vecp
>= MAXVEC
-1)
146 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
147 user
= vec
[vecp
++] = cp
;
163 if (!(saslmech
= *argp
++) || *saslmech
== '-')
164 adios (NULL
, "missing argument to %s", argp
[-2]);
185 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
188 adios (NULL
, "not built with OAuth support");
193 if (!(proxy
= *argp
++) || *proxy
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
198 if (vecp
>= MAXVEC
-1)
199 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
204 * If -host is not specified by user
206 if (!host
|| !*host
) {
208 * If "pophost" is specified in mts.conf,
209 * use it as default value.
211 if (pophost
&& *pophost
)
224 tlsflag
|= P_INITTLS
;
227 tlsflag
|= P_NOVERIFY
;
230 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
231 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
233 for (vecp
= 0; vec
[vecp
]; vecp
++)
234 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
235 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
238 if (user
== NULL
) user
= getusername ();
242 /* Not sure this check makes sense... */
243 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
244 pw
= getpwnam (user
);
246 adios (NULL
, "unable to get information about user");
249 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
251 for (vecp
= 0; vec
[vecp
]; vecp
++) {
252 if ((pw
= getpwnam (vec
[vecp
])))
253 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
255 inform("no such user as %s", vec
[vecp
]);
265 #define NOTE_SWITCHES \
266 X("all", 0, NALLSW) \
267 X("mail", 0, NMAISW) \
268 X("nomail", 0, NNMAISW) \
270 #define X(sw, minchars, id) id,
271 DEFINE_SWITCH_ENUM(NOTE
);
274 #define X(sw, minchars, id) { sw, minchars, id },
275 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
280 donote (char *cp
, int ntflag
)
282 switch (smatch (cp
, ntswitches
)) {
284 ambigsw (cp
, ntswitches
);
287 adios (NULL
, "-%snotify %s unknown", ntflag
? "" : "no", cp
);
297 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
302 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
305 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
306 *mmdflfil
? mmdflfil
: user
);
309 bool statok
= stat(buffer
, &st
) != -1;
310 bool empty
= !statok
|| st
.st_size
== 0;
312 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
313 (!empty
&& !(notifysw
& NT_MAIL
))) {
319 fputs("You don't have any mail waiting", stdout
);
321 printf("%s doesn't have any mail waiting", user
);
324 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
326 printf("You have %s mail waiting", kind
);
328 printf("%s has %s mail waiting", user
, kind
);
332 if (datesw
&& statok
&& st
.st_atime
)
333 printf("; last read on %s", dtime(&st
.st_atime
, 1));
340 extern char response
[];
343 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
344 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
345 const char *auth_svc
)
347 int nmsgs
, nbytes
, status
;
349 if (auth_svc
== NULL
) {
350 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
351 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
355 adios (NULL
, "must specify -user with -saslmech xoauth2");
359 /* open the POP connection */
360 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
362 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
363 || pop_quit () == NOTOK
) { /* quit POP connection */
364 inform("%s", response
);
369 if (notifysw
& NT_MAIL
) {
371 fputs("You have ", stdout
);
373 printf ("%s has ", user
);
375 printf ("%d message%s (%d bytes)",
376 nmsgs
, PLURALS(nmsgs
), nbytes
);
383 if (notifysw
& NT_NMAI
)
384 printf (personal
? "You don't %s%s" : "%s doesn't %s",
385 personal
? "" : user
, "have any mail waiting");
391 printf (" on %s\n", host
);