]>
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.
9 #include "sbr/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
24 # define SASLminc(a) (a)
26 # define SASLminc(a) 0
30 # define TLSminc(a) (a)
35 #define MSGCHK_SWITCHES \
36 X("date", 0, DATESW) \
37 X("nodate", 0, NDATESW) \
38 X("notify type", 0, NOTESW) \
39 X("nonotify type", 0, NNOTESW) \
40 X("host hostname", 0, HOSTSW) \
41 X("user username", 0, USERSW) \
42 X("port name/number", 0, PORTSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45 X("snoop", 0, SNOOPSW) \
46 X("sasl", SASLminc(4), SASLSW) \
47 X("nosasl", SASLminc(6), NOSASLSW) \
48 X("saslmech", SASLminc(5), SASLMECHSW) \
49 X("authservice", SASLminc(0), AUTHSERVICESW) \
50 X("initialtls", TLSminc(-10), INITTLSSW) \
51 X("notls", TLSminc(-5), NOTLSSW) \
52 X("certverify", TLSminc(-10), CERTVERSW) \
53 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
54 X("proxy command", 0, PROXYSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(MSGCHK
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
65 * Maximum numbers of users we can check (plus
66 * one for the NULL vector at the end).
72 #define NT_ALL (NT_MAIL | NT_NMAI)
78 static int donote (char *, int) PURE
;
79 static int checkmail (char *, char *, int, int, int);
80 static int remotemail (char *, char *, char *, char *, int, int, int, int,
81 char *, int, const char *);
85 main (int argc
, char **argv
)
88 int notifysw
= NT_ALL
;
92 bool noverify
= false;
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], true, true)) { return 1; }
104 arguments
= getarguments (invo_name
, argc
, argv
, 1);
107 while ((cp
= *argp
++)) {
109 switch (smatch (++cp
, switches
)) {
111 ambigsw (cp
, switches
);
114 die("-%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 die("missing argument to %s", argp
[-2]);
135 notifysw
|= donote (cp
, 1);
138 if (!(cp
= *argp
++) || *cp
== '-')
139 die("missing argument to %s", argp
[-2]);
140 notifysw
&= ~donote (cp
, 0);
144 if (!(host
= *argp
++) || *host
== '-')
145 die("missing argument to %s", argp
[-2]);
149 if (!(port
= *argp
++) || *port
== '-')
150 die("missing argument to %s", argp
[-2]);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 die("missing argument to %s", argp
[-2]);
156 if (vecp
>= MAXVEC
-1)
157 die("you can only check %d users at a time", MAXVEC
-1);
158 user
= vec
[vecp
++] = cp
;
174 if (!(saslmech
= *argp
++) || *saslmech
== '-')
175 die("missing argument to %s", argp
[-2]);
196 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
197 die("missing argument to %s", argp
[-2]);
199 die("not built with OAuth support");
204 if (!(proxy
= *argp
++) || *proxy
== '-')
205 die("missing argument to %s", argp
[-2]);
209 if (vecp
>= MAXVEC
-1)
210 die("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 die("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 die("-%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
)
316 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
317 *mmdflfil
? mmdflfil
: user
);
320 bool statok
= stat(buffer
, &st
) != -1;
321 bool empty
= !statok
|| st
.st_size
== 0;
323 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
324 (!empty
&& !(notifysw
& NT_MAIL
))) {
330 fputs("You don't have any mail waiting", stdout
);
332 printf("%s doesn't have any mail waiting", user
);
335 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
337 printf("You have %s mail waiting", kind
);
339 printf("%s has %s mail waiting", user
, kind
);
343 if (datesw
&& statok
&& st
.st_atime
)
344 printf("; last read on %s", dtime(&st
.st_atime
, 1));
351 extern char response
[];
354 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
355 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
356 const char *auth_svc
)
361 if (auth_svc
== NULL
) {
362 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
363 die("must specify -authservice with -saslmech xoauth2");
367 die("must specify -user with -saslmech xoauth2");
371 /* open the POP connection */
372 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
374 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
375 || pop_quit () == NOTOK
) { /* quit POP connection */
376 inform("%s", response
);
381 if (notifysw
& NT_MAIL
) {
383 fputs("You have ", stdout
);
385 printf ("%s has ", user
);
387 printf ("%d message%s (%d bytes)",
388 nmsgs
, PLURALS(nmsgs
), nbytes
);
395 if (notifysw
& NT_NMAI
)
396 printf (personal
? "You don't %s%s" : "%s doesn't %s",
397 personal
? "" : user
, "have any mail waiting");
403 printf (" on %s\n", host
);