]>
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
21 # define SASLminc(a) (a)
23 # define SASLminc(a) 0
27 # define TLSminc(a) (a)
32 #define MSGCHK_SWITCHES \
33 X("date", 0, DATESW) \
34 X("nodate", 0, NDATESW) \
35 X("notify type", 0, NOTESW) \
36 X("nonotify type", 0, NNOTESW) \
37 X("host hostname", 0, HOSTSW) \
38 X("user username", 0, USERSW) \
39 X("port name/number", 0, PORTSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
42 X("snoop", 0, SNOOPSW) \
43 X("sasl", SASLminc(4), SASLSW) \
44 X("nosasl", SASLminc(6), NOSASLSW) \
45 X("saslmech", SASLminc(5), SASLMECHSW) \
46 X("authservice", SASLminc(0), AUTHSERVICESW) \
47 X("initialtls", TLSminc(-10), INITTLSSW) \
48 X("notls", TLSminc(-5), NOTLSSW) \
49 X("certverify", TLSminc(-10), CERTVERSW) \
50 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
51 X("proxy command", 0, PROXYSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(MSGCHK
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
62 * Maximum numbers of users we can check (plus
63 * one for the NULL vector at the end).
69 #define NT_ALL (NT_MAIL | NT_NMAI)
75 static int donote (char *, int) PURE
;
76 static int checkmail (char *, char *, int, int, int);
77 static int remotemail (char *, char *, char *, char *, int, int, int, int,
78 char *, int, const char *);
82 main (int argc
, char **argv
)
85 int notifysw
= NT_ALL
;
89 bool noverify
= false;
92 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
93 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
94 char **argp
, **arguments
, *vec
[MAXVEC
];
97 if (nmh_init(argv
[0], true, true)) { return 1; }
101 arguments
= getarguments (invo_name
, argc
, argv
, 1);
104 while ((cp
= *argp
++)) {
106 switch (smatch (++cp
, switches
)) {
108 ambigsw (cp
, switches
);
111 die("-%s unknown", cp
);
114 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
130 if (!(cp
= *argp
++) || *cp
== '-')
131 die("missing argument to %s", argp
[-2]);
132 notifysw
|= donote (cp
, 1);
135 if (!(cp
= *argp
++) || *cp
== '-')
136 die("missing argument to %s", argp
[-2]);
137 notifysw
&= ~donote (cp
, 0);
141 if (!(host
= *argp
++) || *host
== '-')
142 die("missing argument to %s", argp
[-2]);
146 if (!(port
= *argp
++) || *port
== '-')
147 die("missing argument to %s", argp
[-2]);
151 if (!(cp
= *argp
++) || *cp
== '-')
152 die("missing argument to %s", argp
[-2]);
153 if (vecp
>= MAXVEC
-1)
154 die("you can only check %d users at a time", MAXVEC
-1);
155 user
= vec
[vecp
++] = cp
;
171 if (!(saslmech
= *argp
++) || *saslmech
== '-')
172 die("missing argument to %s", argp
[-2]);
193 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
194 die("missing argument to %s", argp
[-2]);
196 die("not built with OAuth support");
201 if (!(proxy
= *argp
++) || *proxy
== '-')
202 die("missing argument to %s", argp
[-2]);
206 if (vecp
>= MAXVEC
-1)
207 die("you can only check %d users at a time", MAXVEC
-1);
212 * If -host is not specified by user
214 if (!host
|| !*host
) {
216 * If "pophost" is specified in mts.conf,
217 * use it as default value.
219 if (pophost
&& *pophost
)
232 tlsflag
|= P_INITTLS
;
235 tlsflag
|= P_NOVERIFY
;
238 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
239 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
241 for (vecp
= 0; vec
[vecp
]; vecp
++)
242 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
243 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
246 if (user
== NULL
) user
= getusername ();
250 /* Not sure this check makes sense... */
251 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
252 pw
= getpwnam (user
);
254 die("unable to get information about user");
257 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
259 for (vecp
= 0; vec
[vecp
]; vecp
++) {
260 if ((pw
= getpwnam (vec
[vecp
])))
261 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
263 inform("no such user as %s", vec
[vecp
]);
273 #define NOTE_SWITCHES \
274 X("all", 0, NALLSW) \
275 X("mail", 0, NMAISW) \
276 X("nomail", 0, NNMAISW) \
278 #define X(sw, minchars, id) id,
279 DEFINE_SWITCH_ENUM(NOTE
);
282 #define X(sw, minchars, id) { sw, minchars, id },
283 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
288 donote (char *cp
, int ntflag
)
290 switch (smatch (cp
, ntswitches
)) {
292 ambigsw (cp
, ntswitches
);
295 die("-%snotify %s unknown", ntflag
? "" : "no", cp
);
305 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
310 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
313 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
314 *mmdflfil
? mmdflfil
: user
);
317 bool statok
= stat(buffer
, &st
) != -1;
318 bool empty
= !statok
|| st
.st_size
== 0;
320 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
321 (!empty
&& !(notifysw
& NT_MAIL
))) {
327 fputs("You don't have any mail waiting", stdout
);
329 printf("%s doesn't have any mail waiting", user
);
332 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
334 printf("You have %s mail waiting", kind
);
336 printf("%s has %s mail waiting", user
, kind
);
340 if (datesw
&& statok
&& st
.st_atime
)
341 printf("; last read on %s", dtime(&st
.st_atime
, 1));
348 extern char response
[];
351 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
352 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
353 const char *auth_svc
)
358 if (auth_svc
== NULL
) {
359 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
360 die("must specify -authservice with -saslmech xoauth2");
364 die("must specify -user with -saslmech xoauth2");
368 /* open the POP connection */
369 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
371 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
372 || pop_quit () == NOTOK
) { /* quit POP connection */
373 inform("%s", response
);
378 if (notifysw
& NT_MAIL
) {
380 fputs("You have ", stdout
);
382 printf ("%s has ", user
);
384 printf ("%d message%s (%d bytes)",
385 nmsgs
, PLURALS(nmsgs
), nbytes
);
392 if (notifysw
& NT_NMAI
)
393 printf (personal
? "You don't %s%s" : "%s doesn't %s",
394 personal
? "" : user
, "have any mail waiting");
400 printf (" on %s\n", host
);