]>
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) PURE
;
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);
157 user
= vec
[vecp
++] = cp
;
173 if (!(saslmech
= *argp
++) || *saslmech
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
195 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
198 adios (NULL
, "not built with OAuth support");
203 if (!(proxy
= *argp
++) || *proxy
== '-')
204 adios (NULL
, "missing argument to %s", argp
[-2]);
208 if (vecp
>= MAXVEC
-1)
209 adios (NULL
, "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 adios (NULL
, "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 adios (NULL
, "-%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
)
318 snprintf (buffer
, sizeof(buffer
), "%s/%s", mmdfldir
[0] ? mmdfldir
: home
, mmdflfil
[0] ? mmdflfil
: user
);
321 st
.st_atime
= st
.st_mtime
= 0;
323 mf
= (stat (buffer
, &st
) == NOTOK
|| st
.st_size
== 0) ? NONEOK
324 : st
.st_atime
<= st
.st_mtime
? MMDFNEW
: MMDFOLD
;
326 if ((mf
& UUCPOK
) || (mf
& MMDFOK
)) {
327 if (notifysw
& NT_MAIL
) {
329 printf ("You have ");
331 printf ("%s has ", user
);
333 printf ("%s old-style bell", mf
& UUCPOLD
? "old" : "new");
334 if ((mf
& UUCPOK
) && (mf
& MMDFOK
))
337 printf ("%s%s", mf
& MMDFOLD
? "old" : "new",
338 mf
& UUCPOK
? " Internet" : "");
339 printf (" mail waiting");
346 if (notifysw
& NT_NMAI
)
347 printf (personal
? "You don't %s%s" : "%s doesn't %s",
348 personal
? "" : user
, "have any mail waiting");
356 if (datesw
&& st
.st_atime
)
357 printf ("; last read on %s", dtime (&st
.st_atime
, 1));
365 extern char response
[];
368 remotemail (char *host
, char *port
, char *user
, char *proxy
, int notifysw
,
369 int personal
, int snoop
, int sasl
, char *saslmech
, int tls
,
370 const char *auth_svc
)
372 int nmsgs
, nbytes
, status
;
374 if (auth_svc
== NULL
) {
375 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
376 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
380 adios (NULL
, "must specify -user with -saslmech xoauth2");
384 /* open the POP connection */
385 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
387 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
388 || pop_quit () == NOTOK
) { /* quit POP connection */
389 inform("%s", response
);
394 if (notifysw
& NT_MAIL
) {
396 printf ("You have ");
398 printf ("%s has ", user
);
400 printf ("%d message%s (%d bytes)",
401 nmsgs
, PLURALS(nmsgs
), nbytes
);
408 if (notifysw
& NT_NMAI
)
409 printf (personal
? "You don't %s%s" : "%s doesn't %s",
410 personal
? "" : user
, "have any mail waiting");
416 printf (" on %s\n", host
);