]>
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/ambigsw.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
22 # define SASLminc(a) (a)
24 # define SASLminc(a) 0
28 # define TLSminc(a) (a)
33 #define MSGCHK_SWITCHES \
34 X("date", 0, DATESW) \
35 X("nodate", 0, NDATESW) \
36 X("notify type", 0, NOTESW) \
37 X("nonotify type", 0, NNOTESW) \
38 X("host hostname", 0, HOSTSW) \
39 X("user username", 0, USERSW) \
40 X("port name/number", 0, PORTSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("snoop", 0, SNOOPSW) \
44 X("sasl", SASLminc(4), SASLSW) \
45 X("nosasl", SASLminc(6), NOSASLSW) \
46 X("saslmech", SASLminc(5), SASLMECHSW) \
47 X("authservice", SASLminc(0), AUTHSERVICESW) \
48 X("initialtls", TLSminc(-10), INITTLSSW) \
49 X("notls", TLSminc(-5), NOTLSSW) \
50 X("certverify", TLSminc(-10), CERTVERSW) \
51 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
52 X("proxy command", 0, PROXYSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(MSGCHK
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
63 * Maximum numbers of users we can check (plus
64 * one for the NULL vector at the end).
70 #define NT_ALL (NT_MAIL | NT_NMAI)
76 static int donote (char *, int) PURE
;
77 static int checkmail (char *, char *, int, int, int);
78 static int remotemail (char *, char *, char *, char *, int, int, int, int,
79 char *, int, const char *);
83 main (int argc
, char **argv
)
86 int notifysw
= NT_ALL
;
90 bool noverify
= false;
93 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
94 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
95 char **argp
, **arguments
, *vec
[MAXVEC
];
98 if (nmh_init(argv
[0], true, true)) { return 1; }
102 arguments
= getarguments (invo_name
, argc
, argv
, 1);
105 while ((cp
= *argp
++)) {
107 switch (smatch (++cp
, switches
)) {
109 ambigsw (cp
, switches
);
112 die("-%s unknown", cp
);
115 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
117 print_help (buf
, switches
, 1);
120 print_version(invo_name
);
131 if (!(cp
= *argp
++) || *cp
== '-')
132 die("missing argument to %s", argp
[-2]);
133 notifysw
|= donote (cp
, 1);
136 if (!(cp
= *argp
++) || *cp
== '-')
137 die("missing argument to %s", argp
[-2]);
138 notifysw
&= ~donote (cp
, 0);
142 if (!(host
= *argp
++) || *host
== '-')
143 die("missing argument to %s", argp
[-2]);
147 if (!(port
= *argp
++) || *port
== '-')
148 die("missing argument to %s", argp
[-2]);
152 if (!(cp
= *argp
++) || *cp
== '-')
153 die("missing argument to %s", argp
[-2]);
154 if (vecp
>= MAXVEC
-1)
155 die("you can only check %d users at a time", MAXVEC
-1);
156 user
= vec
[vecp
++] = cp
;
172 if (!(saslmech
= *argp
++) || *saslmech
== '-')
173 die("missing argument to %s", argp
[-2]);
194 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
195 die("missing argument to %s", argp
[-2]);
197 die("not built with OAuth support");
202 if (!(proxy
= *argp
++) || *proxy
== '-')
203 die("missing argument to %s", argp
[-2]);
207 if (vecp
>= MAXVEC
-1)
208 die("you can only check %d users at a time", MAXVEC
-1);
213 * If -host is not specified by user
215 if (!host
|| !*host
) {
217 * If "pophost" is specified in mts.conf,
218 * use it as default value.
220 if (pophost
&& *pophost
)
233 tlsflag
|= P_INITTLS
;
236 tlsflag
|= P_NOVERIFY
;
239 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
240 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
242 for (vecp
= 0; vec
[vecp
]; vecp
++)
243 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
244 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
247 if (user
== NULL
) user
= getusername ();
251 /* Not sure this check makes sense... */
252 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
253 pw
= getpwnam (user
);
255 die("unable to get information about user");
258 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
260 for (vecp
= 0; vec
[vecp
]; vecp
++) {
261 if ((pw
= getpwnam (vec
[vecp
])))
262 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
264 inform("no such user as %s", vec
[vecp
]);
274 #define NOTE_SWITCHES \
275 X("all", 0, NALLSW) \
276 X("mail", 0, NMAISW) \
277 X("nomail", 0, NNMAISW) \
279 #define X(sw, minchars, id) id,
280 DEFINE_SWITCH_ENUM(NOTE
);
283 #define X(sw, minchars, id) { sw, minchars, id },
284 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
289 donote (char *cp
, int ntflag
)
291 switch (smatch (cp
, ntswitches
)) {
293 ambigsw (cp
, ntswitches
);
296 die("-%snotify %s unknown", ntflag
? "" : "no", cp
);
306 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
311 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
314 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
315 *mmdflfil
? mmdflfil
: user
);
318 bool statok
= stat(buffer
, &st
) != -1;
319 bool empty
= !statok
|| st
.st_size
== 0;
321 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
322 (!empty
&& !(notifysw
& NT_MAIL
))) {
328 fputs("You don't have any mail waiting", stdout
);
330 printf("%s doesn't have any mail waiting", user
);
333 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
335 printf("You have %s mail waiting", kind
);
337 printf("%s has %s mail waiting", user
, kind
);
341 if (datesw
&& statok
&& st
.st_atime
)
342 printf("; last read on %s", dtime(&st
.st_atime
, 1));
349 extern char response
[];
352 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
353 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
354 const char *auth_svc
)
359 if (auth_svc
== NULL
) {
360 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
361 die("must specify -authservice with -saslmech xoauth2");
365 die("must specify -user with -saslmech xoauth2");
369 /* open the POP connection */
370 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
372 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
373 || pop_quit () == NOTOK
) { /* quit POP connection */
374 inform("%s", response
);
379 if (notifysw
& NT_MAIL
) {
381 fputs("You have ", stdout
);
383 printf ("%s has ", user
);
385 printf ("%d message%s (%d bytes)",
386 nmsgs
, PLURALS(nmsgs
), nbytes
);
393 if (notifysw
& NT_NMAI
)
394 printf (personal
? "You don't %s%s" : "%s doesn't %s",
395 personal
? "" : user
, "have any mail waiting");
401 printf (" on %s\n", host
);