]>
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.
17 # define SASLminc(a) (a)
19 # define SASLminc(a) 0
23 # define TLSminc(a) (a)
28 #define MSGCHK_SWITCHES \
29 X("date", 0, DATESW) \
30 X("nodate", 0, NDATESW) \
31 X("notify type", 0, NOTESW) \
32 X("nonotify type", 0, NNOTESW) \
33 X("host hostname", 0, HOSTSW) \
34 X("user username", 0, USERSW) \
35 X("port name/number", 0, PORTSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
38 X("snoop", 0, SNOOPSW) \
39 X("sasl", SASLminc(4), SASLSW) \
40 X("nosasl", SASLminc(6), NOSASLSW) \
41 X("saslmech", SASLminc(5), SASLMECHSW) \
42 X("authservice", SASLminc(0), AUTHSERVICESW) \
43 X("initialtls", TLSminc(-10), INITTLSSW) \
44 X("notls", TLSminc(-5), NOTLSSW) \
45 X("certverify", TLSminc(-10), CERTVERSW) \
46 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
47 X("proxy command", 0, PROXYSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(MSGCHK
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
58 * Maximum numbers of users we can check (plus
59 * one for the NULL vector at the end).
65 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
68 #define NT_ALL (NT_MAIL | NT_NMAI)
73 #define UUCPOK (UUCPOLD | UUCPNEW)
76 #define MMDFOK (MMDFOLD | MMDFNEW)
82 static int donote (char *, int);
83 static int checkmail (char *, char *, int, int, int);
84 static int remotemail (char *, char *, char *, char *, int, int, int, int,
85 char *, int, const char *);
89 main (int argc
, char **argv
)
91 int datesw
= 1, notifysw
= NT_ALL
;
92 int status
= 0, sasl
= 0, tls
= 0, noverify
= 0;
93 int snoop
= 0, vecp
= 0;
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], 1)) { return 1; }
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
106 while ((cp
= *argp
++)) {
108 switch (smatch (++cp
, switches
)) {
110 ambigsw (cp
, switches
);
113 adios (NULL
, "-%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 adios (NULL
, "missing argument to %s", argp
[-2]);
134 notifysw
|= donote (cp
, 1);
137 if (!(cp
= *argp
++) || *cp
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
139 notifysw
&= ~donote (cp
, 0);
143 if (!(host
= *argp
++) || *host
== '-')
144 adios (NULL
, "missing argument to %s", argp
[-2]);
148 if (!(port
= *argp
++) || *port
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
153 if (!(cp
= *argp
++) || *cp
== '-')
154 adios (NULL
, "missing argument to %s", argp
[-2]);
155 if (vecp
>= MAXVEC
-1)
156 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
158 user
= vec
[vecp
++] = cp
;
174 if (!(saslmech
= *argp
++) || *saslmech
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
196 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
197 adios (NULL
, "missing argument to %s", argp
[-2]);
199 adios (NULL
, "not built with OAuth support");
204 if (!(proxy
= *argp
++) || *proxy
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
209 if (vecp
>= MAXVEC
-1)
210 adios (NULL
, "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 adios (NULL
, "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 adios (NULL
, "-%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
)
320 snprintf (buffer
, sizeof(buffer
), "%s/%s", mmdfldir
[0] ? mmdfldir
: home
, mmdflfil
[0] ? mmdflfil
: user
);
323 st
.st_atime
= st
.st_mtime
= 0;
325 mf
= (stat (buffer
, &st
) == NOTOK
|| st
.st_size
== 0) ? NONEOK
326 : st
.st_atime
<= st
.st_mtime
? MMDFNEW
: MMDFOLD
;
328 if ((mf
& UUCPOK
) || (mf
& MMDFOK
)) {
329 if (notifysw
& NT_MAIL
) {
331 printf ("You have ");
333 printf ("%s has ", user
);
335 printf ("%s old-style bell", mf
& UUCPOLD
? "old" : "new");
336 if ((mf
& UUCPOK
) && (mf
& MMDFOK
))
339 printf ("%s%s", mf
& MMDFOLD
? "old" : "new",
340 mf
& UUCPOK
? " Internet" : "");
341 printf (" mail waiting");
348 if (notifysw
& NT_NMAI
)
349 printf (personal
? "You don't %s%s" : "%s doesn't %s",
350 personal
? "" : user
, "have any mail waiting");
358 if (datesw
&& st
.st_atime
)
359 printf ("; last read on %s", dtime (&st
.st_atime
, 1));
367 extern char response
[];
370 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
371 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
372 const char *auth_svc
)
374 int nmsgs
, nbytes
, status
;
376 if (auth_svc
== NULL
) {
377 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
378 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
382 adios (NULL
, "must specify -user with -saslmech xoauth2");
386 /* open the POP connection */
387 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
389 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
390 || pop_quit () == NOTOK
) { /* quit POP connection */
391 inform("%s", response
);
396 if (notifysw
& NT_MAIL
) {
398 printf ("You have ");
400 printf ("%s has ", user
);
402 printf ("%d message%s (%d bytes)",
403 nmsgs
, PLURALS(nmsgs
), nbytes
);
410 if (notifysw
& NT_NMAI
)
411 printf (personal
? "You don't %s%s" : "%s doesn't %s",
412 personal
? "" : user
, "have any mail waiting");
418 printf (" on %s\n", host
);