]>
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/smatch.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
23 # define SASLminc(a) (a)
25 # define SASLminc(a) 0
29 # define TLSminc(a) (a)
34 #define MSGCHK_SWITCHES \
35 X("date", 0, DATESW) \
36 X("nodate", 0, NDATESW) \
37 X("notify type", 0, NOTESW) \
38 X("nonotify type", 0, NNOTESW) \
39 X("host hostname", 0, HOSTSW) \
40 X("user username", 0, USERSW) \
41 X("port name/number", 0, PORTSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("snoop", 0, SNOOPSW) \
45 X("sasl", SASLminc(4), SASLSW) \
46 X("nosasl", SASLminc(6), NOSASLSW) \
47 X("saslmech", SASLminc(5), SASLMECHSW) \
48 X("authservice", SASLminc(0), AUTHSERVICESW) \
49 X("initialtls", TLSminc(-10), INITTLSSW) \
50 X("notls", TLSminc(-5), NOTLSSW) \
51 X("certverify", TLSminc(-10), CERTVERSW) \
52 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
53 X("proxy command", 0, PROXYSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(MSGCHK
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
64 * Maximum numbers of users we can check (plus
65 * one for the NULL vector at the end).
71 #define NT_ALL (NT_MAIL | NT_NMAI)
77 static int donote (char *, int) PURE
;
78 static int checkmail (char *, char *, int, int, int);
79 static int remotemail (char *, char *, char *, char *, int, int, int, int,
80 char *, int, const char *);
84 main (int argc
, char **argv
)
87 int notifysw
= NT_ALL
;
91 bool noverify
= false;
94 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
95 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
96 char **argp
, **arguments
, *vec
[MAXVEC
];
99 if (nmh_init(argv
[0], true, true)) { return 1; }
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
106 while ((cp
= *argp
++)) {
108 switch (smatch (++cp
, switches
)) {
110 ambigsw (cp
, switches
);
113 die("-%s unknown", cp
);
116 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
118 print_help (buf
, switches
, 1);
121 print_version(invo_name
);
132 if (!(cp
= *argp
++) || *cp
== '-')
133 die("missing argument to %s", argp
[-2]);
134 notifysw
|= donote (cp
, 1);
137 if (!(cp
= *argp
++) || *cp
== '-')
138 die("missing argument to %s", argp
[-2]);
139 notifysw
&= ~donote (cp
, 0);
143 if (!(host
= *argp
++) || *host
== '-')
144 die("missing argument to %s", argp
[-2]);
148 if (!(port
= *argp
++) || *port
== '-')
149 die("missing argument to %s", argp
[-2]);
153 if (!(cp
= *argp
++) || *cp
== '-')
154 die("missing argument to %s", argp
[-2]);
155 if (vecp
>= MAXVEC
-1)
156 die("you can only check %d users at a time", MAXVEC
-1);
157 user
= vec
[vecp
++] = cp
;
173 if (!(saslmech
= *argp
++) || *saslmech
== '-')
174 die("missing argument to %s", argp
[-2]);
195 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
196 die("missing argument to %s", argp
[-2]);
198 die("not built with OAuth support");
203 if (!(proxy
= *argp
++) || *proxy
== '-')
204 die("missing argument to %s", argp
[-2]);
208 if (vecp
>= MAXVEC
-1)
209 die("you can only check %d users at a time", MAXVEC
-1);
214 * If -host is not specified by user
216 if (!host
|| !*host
) {
218 * If "pophost" is specified in mts.conf,
219 * use it as default value.
221 if (pophost
&& *pophost
)
234 tlsflag
|= P_INITTLS
;
237 tlsflag
|= P_NOVERIFY
;
240 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
241 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
243 for (vecp
= 0; vec
[vecp
]; vecp
++)
244 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
245 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
248 if (user
== NULL
) user
= getusername ();
252 /* Not sure this check makes sense... */
253 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
254 pw
= getpwnam (user
);
256 die("unable to get information about user");
259 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
261 for (vecp
= 0; vec
[vecp
]; vecp
++) {
262 if ((pw
= getpwnam (vec
[vecp
])))
263 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
265 inform("no such user as %s", vec
[vecp
]);
275 #define NOTE_SWITCHES \
276 X("all", 0, NALLSW) \
277 X("mail", 0, NMAISW) \
278 X("nomail", 0, NNMAISW) \
280 #define X(sw, minchars, id) id,
281 DEFINE_SWITCH_ENUM(NOTE
);
284 #define X(sw, minchars, id) { sw, minchars, id },
285 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
290 donote (char *cp
, int ntflag
)
292 switch (smatch (cp
, ntswitches
)) {
294 ambigsw (cp
, ntswitches
);
297 die("-%snotify %s unknown", ntflag
? "" : "no", cp
);
307 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
312 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
315 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
316 *mmdflfil
? mmdflfil
: user
);
319 bool statok
= stat(buffer
, &st
) != -1;
320 bool empty
= !statok
|| st
.st_size
== 0;
322 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
323 (!empty
&& !(notifysw
& NT_MAIL
))) {
329 fputs("You don't have any mail waiting", stdout
);
331 printf("%s doesn't have any mail waiting", user
);
334 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
336 printf("You have %s mail waiting", kind
);
338 printf("%s has %s mail waiting", user
, kind
);
342 if (datesw
&& statok
&& st
.st_atime
)
343 printf("; last read on %s", dtime(&st
.st_atime
, 1));
350 extern char response
[];
353 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
354 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
355 const char *auth_svc
)
360 if (auth_svc
== NULL
) {
361 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
362 die("must specify -authservice with -saslmech xoauth2");
366 die("must specify -user with -saslmech xoauth2");
370 /* open the POP connection */
371 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
373 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
374 || pop_quit () == NOTOK
) { /* quit POP connection */
375 inform("%s", response
);
380 if (notifysw
& NT_MAIL
) {
382 fputs("You have ", stdout
);
384 printf ("%s has ", user
);
386 printf ("%d message%s (%d bytes)",
387 nmsgs
, PLURALS(nmsgs
), nbytes
);
394 if (notifysw
& NT_NMAI
)
395 printf (personal
? "You don't %s%s" : "%s doesn't %s",
396 personal
? "" : user
, "have any mail waiting");
402 printf (" on %s\n", host
);