]>
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.
10 #include "sbr/getarguments.h"
11 #include "sbr/smatch.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
25 # define SASLminc(a) (a)
27 # define SASLminc(a) 0
31 # define TLSminc(a) (a)
36 #define MSGCHK_SWITCHES \
37 X("date", 0, DATESW) \
38 X("nodate", 0, NDATESW) \
39 X("notify type", 0, NOTESW) \
40 X("nonotify type", 0, NNOTESW) \
41 X("host hostname", 0, HOSTSW) \
42 X("user username", 0, USERSW) \
43 X("port name/number", 0, PORTSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("snoop", 0, SNOOPSW) \
47 X("sasl", SASLminc(4), SASLSW) \
48 X("nosasl", SASLminc(6), NOSASLSW) \
49 X("saslmech", SASLminc(5), SASLMECHSW) \
50 X("authservice", SASLminc(0), AUTHSERVICESW) \
51 X("initialtls", TLSminc(-10), INITTLSSW) \
52 X("notls", TLSminc(-5), NOTLSSW) \
53 X("certverify", TLSminc(-10), CERTVERSW) \
54 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
55 X("proxy command", 0, PROXYSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(MSGCHK
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
66 * Maximum numbers of users we can check (plus
67 * one for the NULL vector at the end).
73 #define NT_ALL (NT_MAIL | NT_NMAI)
79 static int donote (char *, int) PURE
;
80 static int checkmail (char *, char *, int, int, int);
81 static int remotemail (char *, char *, char *, char *, int, int, int, int,
82 char *, int, const char *);
86 main (int argc
, char **argv
)
89 int notifysw
= NT_ALL
;
93 bool noverify
= false;
96 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
97 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
98 char **argp
, **arguments
, *vec
[MAXVEC
];
101 if (nmh_init(argv
[0], true, true)) { return 1; }
105 arguments
= getarguments (invo_name
, argc
, argv
, 1);
108 while ((cp
= *argp
++)) {
110 switch (smatch (++cp
, switches
)) {
112 ambigsw (cp
, switches
);
115 die("-%s unknown", cp
);
118 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
120 print_help (buf
, switches
, 1);
123 print_version(invo_name
);
134 if (!(cp
= *argp
++) || *cp
== '-')
135 die("missing argument to %s", argp
[-2]);
136 notifysw
|= donote (cp
, 1);
139 if (!(cp
= *argp
++) || *cp
== '-')
140 die("missing argument to %s", argp
[-2]);
141 notifysw
&= ~donote (cp
, 0);
145 if (!(host
= *argp
++) || *host
== '-')
146 die("missing argument to %s", argp
[-2]);
150 if (!(port
= *argp
++) || *port
== '-')
151 die("missing argument to %s", argp
[-2]);
155 if (!(cp
= *argp
++) || *cp
== '-')
156 die("missing argument to %s", argp
[-2]);
157 if (vecp
>= MAXVEC
-1)
158 die("you can only check %d users at a time", MAXVEC
-1);
159 user
= vec
[vecp
++] = cp
;
175 if (!(saslmech
= *argp
++) || *saslmech
== '-')
176 die("missing argument to %s", argp
[-2]);
197 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
198 die("missing argument to %s", argp
[-2]);
200 die("not built with OAuth support");
205 if (!(proxy
= *argp
++) || *proxy
== '-')
206 die("missing argument to %s", argp
[-2]);
210 if (vecp
>= MAXVEC
-1)
211 die("you can only check %d users at a time", MAXVEC
-1);
216 * If -host is not specified by user
218 if (!host
|| !*host
) {
220 * If "pophost" is specified in mts.conf,
221 * use it as default value.
223 if (pophost
&& *pophost
)
236 tlsflag
|= P_INITTLS
;
239 tlsflag
|= P_NOVERIFY
;
242 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
243 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
245 for (vecp
= 0; vec
[vecp
]; vecp
++)
246 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
247 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
250 if (user
== NULL
) user
= getusername ();
254 /* Not sure this check makes sense... */
255 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
256 pw
= getpwnam (user
);
258 die("unable to get information about user");
261 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
263 for (vecp
= 0; vec
[vecp
]; vecp
++) {
264 if ((pw
= getpwnam (vec
[vecp
])))
265 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
267 inform("no such user as %s", vec
[vecp
]);
277 #define NOTE_SWITCHES \
278 X("all", 0, NALLSW) \
279 X("mail", 0, NMAISW) \
280 X("nomail", 0, NNMAISW) \
282 #define X(sw, minchars, id) id,
283 DEFINE_SWITCH_ENUM(NOTE
);
286 #define X(sw, minchars, id) { sw, minchars, id },
287 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
292 donote (char *cp
, int ntflag
)
294 switch (smatch (cp
, ntswitches
)) {
296 ambigsw (cp
, ntswitches
);
299 die("-%snotify %s unknown", ntflag
? "" : "no", cp
);
309 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
314 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
317 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
318 *mmdflfil
? mmdflfil
: user
);
321 bool statok
= stat(buffer
, &st
) != -1;
322 bool empty
= !statok
|| st
.st_size
== 0;
324 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
325 (!empty
&& !(notifysw
& NT_MAIL
))) {
331 fputs("You don't have any mail waiting", stdout
);
333 printf("%s doesn't have any mail waiting", user
);
336 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
338 printf("You have %s mail waiting", kind
);
340 printf("%s has %s mail waiting", user
, kind
);
344 if (datesw
&& statok
&& st
.st_atime
)
345 printf("; last read on %s", dtime(&st
.st_atime
, 1));
352 extern char response
[];
355 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
356 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
357 const char *auth_svc
)
362 if (auth_svc
== NULL
) {
363 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
364 die("must specify -authservice with -saslmech xoauth2");
368 die("must specify -user with -saslmech xoauth2");
372 /* open the POP connection */
373 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
375 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
376 || pop_quit () == NOTOK
) { /* quit POP connection */
377 inform("%s", response
);
382 if (notifysw
& NT_MAIL
) {
384 fputs("You have ", stdout
);
386 printf ("%s has ", user
);
388 printf ("%d message%s (%d bytes)",
389 nmsgs
, PLURALS(nmsgs
), nbytes
);
396 if (notifysw
& NT_NMAI
)
397 printf (personal
? "You don't %s%s" : "%s doesn't %s",
398 personal
? "" : user
, "have any mail waiting");
404 printf (" on %s\n", host
);