]>
diplodocus.org Git - nmh/blob - uip/msgchk.c
10feb3cba623fd7e661648a550633145ad8d2fa4
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.
19 # define SASLminc(a) (a)
21 # define SASLminc(a) 0
25 # define TLSminc(a) (a)
30 #define MSGCHK_SWITCHES \
31 X("date", 0, DATESW) \
32 X("nodate", 0, NDATESW) \
33 X("notify type", 0, NOTESW) \
34 X("nonotify type", 0, NNOTESW) \
35 X("host hostname", 0, HOSTSW) \
36 X("user username", 0, USERSW) \
37 X("port name/number", 0, PORTSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
40 X("snoop", 0, SNOOPSW) \
41 X("sasl", SASLminc(4), SASLSW) \
42 X("nosasl", SASLminc(6), NOSASLSW) \
43 X("saslmech", SASLminc(5), SASLMECHSW) \
44 X("authservice", SASLminc(0), AUTHSERVICESW) \
45 X("initialtls", TLSminc(-10), INITTLSSW) \
46 X("notls", TLSminc(-5), NOTLSSW) \
47 X("certverify", TLSminc(-10), CERTVERSW) \
48 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
49 X("proxy command", 0, PROXYSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MSGCHK
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
60 * Maximum numbers of users we can check (plus
61 * one for the NULL vector at the end).
67 #define NT_ALL (NT_MAIL | NT_NMAI)
73 static int donote (char *, int) PURE
;
74 static int checkmail (char *, char *, int, int, int);
75 static int remotemail (char *, char *, char *, char *, int, int, int, int,
76 char *, int, const char *);
80 main (int argc
, char **argv
)
83 int notifysw
= NT_ALL
;
87 bool noverify
= false;
90 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
91 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
92 char **argp
, **arguments
, *vec
[MAXVEC
];
95 if (nmh_init(argv
[0], true, true)) { return 1; }
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
104 switch (smatch (++cp
, switches
)) {
106 ambigsw (cp
, switches
);
109 die("-%s unknown", cp
);
112 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
114 print_help (buf
, switches
, 1);
117 print_version(invo_name
);
128 if (!(cp
= *argp
++) || *cp
== '-')
129 die("missing argument to %s", argp
[-2]);
130 notifysw
|= donote (cp
, 1);
133 if (!(cp
= *argp
++) || *cp
== '-')
134 die("missing argument to %s", argp
[-2]);
135 notifysw
&= ~donote (cp
, 0);
139 if (!(host
= *argp
++) || *host
== '-')
140 die("missing argument to %s", argp
[-2]);
144 if (!(port
= *argp
++) || *port
== '-')
145 die("missing argument to %s", argp
[-2]);
149 if (!(cp
= *argp
++) || *cp
== '-')
150 die("missing argument to %s", argp
[-2]);
151 if (vecp
>= MAXVEC
-1)
152 die("you can only check %d users at a time", MAXVEC
-1);
153 user
= vec
[vecp
++] = cp
;
169 if (!(saslmech
= *argp
++) || *saslmech
== '-')
170 die("missing argument to %s", argp
[-2]);
191 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
192 die("missing argument to %s", argp
[-2]);
194 die("not built with OAuth support");
199 if (!(proxy
= *argp
++) || *proxy
== '-')
200 die("missing argument to %s", argp
[-2]);
204 if (vecp
>= MAXVEC
-1)
205 die("you can only check %d users at a time", MAXVEC
-1);
210 * If -host is not specified by user
212 if (!host
|| !*host
) {
214 * If "pophost" is specified in mts.conf,
215 * use it as default value.
217 if (pophost
&& *pophost
)
230 tlsflag
|= P_INITTLS
;
233 tlsflag
|= P_NOVERIFY
;
236 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
237 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
239 for (vecp
= 0; vec
[vecp
]; vecp
++)
240 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
241 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
244 if (user
== NULL
) user
= getusername ();
248 /* Not sure this check makes sense... */
249 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
250 pw
= getpwnam (user
);
252 die("unable to get information about user");
255 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
257 for (vecp
= 0; vec
[vecp
]; vecp
++) {
258 if ((pw
= getpwnam (vec
[vecp
])))
259 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
261 inform("no such user as %s", vec
[vecp
]);
271 #define NOTE_SWITCHES \
272 X("all", 0, NALLSW) \
273 X("mail", 0, NMAISW) \
274 X("nomail", 0, NNMAISW) \
276 #define X(sw, minchars, id) id,
277 DEFINE_SWITCH_ENUM(NOTE
);
280 #define X(sw, minchars, id) { sw, minchars, id },
281 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
286 donote (char *cp
, int ntflag
)
288 switch (smatch (cp
, ntswitches
)) {
290 ambigsw (cp
, ntswitches
);
293 die("-%snotify %s unknown", ntflag
? "" : "no", cp
);
303 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
308 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
311 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
312 *mmdflfil
? mmdflfil
: user
);
315 bool statok
= stat(buffer
, &st
) != -1;
316 bool empty
= !statok
|| st
.st_size
== 0;
318 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
319 (!empty
&& !(notifysw
& NT_MAIL
))) {
325 fputs("You don't have any mail waiting", stdout
);
327 printf("%s doesn't have any mail waiting", user
);
330 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
332 printf("You have %s mail waiting", kind
);
334 printf("%s has %s mail waiting", user
, kind
);
338 if (datesw
&& statok
&& st
.st_atime
)
339 printf("; last read on %s", dtime(&st
.st_atime
, 1));
346 extern char response
[];
349 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
350 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
351 const char *auth_svc
)
356 if (auth_svc
== NULL
) {
357 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
358 die("must specify -authservice with -saslmech xoauth2");
362 die("must specify -user with -saslmech xoauth2");
366 /* open the POP connection */
367 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
369 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
370 || pop_quit () == NOTOK
) { /* quit POP connection */
371 inform("%s", response
);
376 if (notifysw
& NT_MAIL
) {
378 fputs("You have ", stdout
);
380 printf ("%s has ", user
);
382 printf ("%d message%s (%d bytes)",
383 nmsgs
, PLURALS(nmsgs
), nbytes
);
390 if (notifysw
& NT_NMAI
)
391 printf (personal
? "You don't %s%s" : "%s doesn't %s",
392 personal
? "" : user
, "have any mail waiting");
398 printf (" on %s\n", host
);