]>
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.
18 # define SASLminc(a) (a)
20 # define SASLminc(a) 0
24 # define TLSminc(a) (a)
29 #define MSGCHK_SWITCHES \
30 X("date", 0, DATESW) \
31 X("nodate", 0, NDATESW) \
32 X("notify type", 0, NOTESW) \
33 X("nonotify type", 0, NNOTESW) \
34 X("host hostname", 0, HOSTSW) \
35 X("user username", 0, USERSW) \
36 X("port name/number", 0, PORTSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("snoop", 0, SNOOPSW) \
40 X("sasl", SASLminc(4), SASLSW) \
41 X("nosasl", SASLminc(6), NOSASLSW) \
42 X("saslmech", SASLminc(5), SASLMECHSW) \
43 X("authservice", SASLminc(0), AUTHSERVICESW) \
44 X("initialtls", TLSminc(-10), INITTLSSW) \
45 X("notls", TLSminc(-5), NOTLSSW) \
46 X("certverify", TLSminc(-10), CERTVERSW) \
47 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
48 X("proxy command", 0, PROXYSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MSGCHK
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
59 * Maximum numbers of users we can check (plus
60 * one for the NULL vector at the end).
66 #define NT_ALL (NT_MAIL | NT_NMAI)
72 static int donote (char *, int) PURE
;
73 static int checkmail (char *, char *, int, int, int);
74 static int remotemail (char *, char *, char *, char *, int, int, int, int,
75 char *, int, const char *);
79 main (int argc
, char **argv
)
82 int notifysw
= NT_ALL
;
86 bool noverify
= false;
89 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
90 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
91 char **argp
, **arguments
, *vec
[MAXVEC
];
94 if (nmh_init(argv
[0], true, true)) { return 1; }
98 arguments
= getarguments (invo_name
, argc
, argv
, 1);
101 while ((cp
= *argp
++)) {
103 switch (smatch (++cp
, switches
)) {
105 ambigsw (cp
, switches
);
108 die("-%s unknown", cp
);
111 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
113 print_help (buf
, switches
, 1);
116 print_version(invo_name
);
127 if (!(cp
= *argp
++) || *cp
== '-')
128 die("missing argument to %s", argp
[-2]);
129 notifysw
|= donote (cp
, 1);
132 if (!(cp
= *argp
++) || *cp
== '-')
133 die("missing argument to %s", argp
[-2]);
134 notifysw
&= ~donote (cp
, 0);
138 if (!(host
= *argp
++) || *host
== '-')
139 die("missing argument to %s", argp
[-2]);
143 if (!(port
= *argp
++) || *port
== '-')
144 die("missing argument to %s", argp
[-2]);
148 if (!(cp
= *argp
++) || *cp
== '-')
149 die("missing argument to %s", argp
[-2]);
150 if (vecp
>= MAXVEC
-1)
151 die("you can only check %d users at a time", MAXVEC
-1);
152 user
= vec
[vecp
++] = cp
;
168 if (!(saslmech
= *argp
++) || *saslmech
== '-')
169 die("missing argument to %s", argp
[-2]);
190 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
191 die("missing argument to %s", argp
[-2]);
193 die("not built with OAuth support");
198 if (!(proxy
= *argp
++) || *proxy
== '-')
199 die("missing argument to %s", argp
[-2]);
203 if (vecp
>= MAXVEC
-1)
204 die("you can only check %d users at a time", MAXVEC
-1);
209 * If -host is not specified by user
211 if (!host
|| !*host
) {
213 * If "pophost" is specified in mts.conf,
214 * use it as default value.
216 if (pophost
&& *pophost
)
229 tlsflag
|= P_INITTLS
;
232 tlsflag
|= P_NOVERIFY
;
235 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
236 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
238 for (vecp
= 0; vec
[vecp
]; vecp
++)
239 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
240 snoop
, sasl
, saslmech
, tlsflag
, auth_svc
);
243 if (user
== NULL
) user
= getusername ();
247 /* Not sure this check makes sense... */
248 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
249 pw
= getpwnam (user
);
251 die("unable to get information about user");
254 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
256 for (vecp
= 0; vec
[vecp
]; vecp
++) {
257 if ((pw
= getpwnam (vec
[vecp
])))
258 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
260 inform("no such user as %s", vec
[vecp
]);
270 #define NOTE_SWITCHES \
271 X("all", 0, NALLSW) \
272 X("mail", 0, NMAISW) \
273 X("nomail", 0, NNMAISW) \
275 #define X(sw, minchars, id) id,
276 DEFINE_SWITCH_ENUM(NOTE
);
279 #define X(sw, minchars, id) { sw, minchars, id },
280 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
285 donote (char *cp
, int ntflag
)
287 switch (smatch (cp
, ntswitches
)) {
289 ambigsw (cp
, ntswitches
);
292 die("-%snotify %s unknown", ntflag
? "" : "no", cp
);
302 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
307 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
310 snprintf(buffer
, sizeof buffer
, "%s/%s", *mmdfldir
? mmdfldir
: home
,
311 *mmdflfil
? mmdflfil
: user
);
314 bool statok
= stat(buffer
, &st
) != -1;
315 bool empty
= !statok
|| st
.st_size
== 0;
317 if ((empty
&& !(notifysw
& NT_NMAI
)) ||
318 (!empty
&& !(notifysw
& NT_MAIL
))) {
324 fputs("You don't have any mail waiting", stdout
);
326 printf("%s doesn't have any mail waiting", user
);
329 char *kind
= st
.st_mtime
< st
.st_atime
? "old" : "new";
331 printf("You have %s mail waiting", kind
);
333 printf("%s has %s mail waiting", user
, kind
);
337 if (datesw
&& statok
&& st
.st_atime
)
338 printf("; last read on %s", dtime(&st
.st_atime
, 1));
345 extern char response
[];
348 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
349 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
350 const char *auth_svc
)
355 if (auth_svc
== NULL
) {
356 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
357 die("must specify -authservice with -saslmech xoauth2");
361 die("must specify -user with -saslmech xoauth2");
365 /* open the POP connection */
366 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
368 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
369 || pop_quit () == NOTOK
) { /* quit POP connection */
370 inform("%s", response
);
375 if (notifysw
& NT_MAIL
) {
377 fputs("You have ", stdout
);
379 printf ("%s has ", user
);
381 printf ("%d message%s (%d bytes)",
382 nmsgs
, PLURALS(nmsgs
), nbytes
);
389 if (notifysw
& NT_NMAI
)
390 printf (personal
? "You don't %s%s" : "%s doesn't %s",
391 personal
? "" : user
, "have any mail waiting");
397 printf (" on %s\n", host
);