]>
diplodocus.org Git - nmh/blob - uip/msgchk.c
3 * msgchk.c -- check for mail
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * 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("saslmech", SASLminc(5), SASLMECHSW) \
42 X("authservice", SASLminc(0), AUTHSERVICESW) \
43 X("initialtls", TLSminc(-10), INITTLSSW) \
44 X("notls", TLSminc(-5), NOTLSSW) \
45 X("proxy command", 0, PROXYSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(MSGCHK
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(MSGCHK
, switches
);
56 * Maximum numbers of users we can check (plus
57 * one for the NULL vector at the end).
63 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
66 #define NT_ALL (NT_MAIL | NT_NMAI)
71 #define UUCPOK (UUCPOLD | UUCPNEW)
74 #define MMDFOK (MMDFOLD | MMDFNEW)
80 static int donote (char *, int);
81 static int checkmail (char *, char *, int, int, int);
82 static int remotemail (char *, char *, char *, char *, int, int, int, int,
83 char *, int, const char *);
87 main (int argc
, char **argv
)
89 int datesw
= 1, notifysw
= NT_ALL
;
90 int status
= 0, sasl
= 0, tls
= 0;
91 int snoop
= 0, vecp
= 0;
92 char *cp
, *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
93 char buf
[BUFSIZ
], *saslmech
= NULL
, *auth_svc
= NULL
;
94 char **argp
, **arguments
, *vec
[MAXVEC
];
97 if (nmh_init(argv
[0], 1)) { return 1; }
101 arguments
= getarguments (invo_name
, argc
, argv
, 1);
104 while ((cp
= *argp
++)) {
106 switch (smatch (++cp
, switches
)) {
108 ambigsw (cp
, switches
);
111 adios (NULL
, "-%s unknown", cp
);
114 snprintf (buf
, sizeof(buf
), "%s [switches] [users ...]",
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
130 if (!(cp
= *argp
++) || *cp
== '-')
131 adios (NULL
, "missing argument to %s", argp
[-2]);
132 notifysw
|= donote (cp
, 1);
135 if (!(cp
= *argp
++) || *cp
== '-')
136 adios (NULL
, "missing argument to %s", argp
[-2]);
137 notifysw
&= ~donote (cp
, 0);
141 if (!(host
= *argp
++) || *host
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
146 if (!(port
= *argp
++) || *port
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
151 if (!(cp
= *argp
++) || *cp
== '-')
152 adios (NULL
, "missing argument to %s", argp
[-2]);
153 if (vecp
>= MAXVEC
-1)
154 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
156 user
= vec
[vecp
++] = cp
;
168 if (!(saslmech
= *argp
++) || *saslmech
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
182 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
183 adios (NULL
, "missing argument to %s", argp
[-2]);
185 adios (NULL
, "not built with OAuth support");
190 if (!(proxy
= *argp
++) || *proxy
== '-')
191 adios (NULL
, "missing argument to %s", argp
[-2]);
195 if (vecp
>= MAXVEC
-1)
196 adios (NULL
, "you can only check %d users at a time", MAXVEC
-1);
202 * If -host is not specified by user
204 if (!host
|| !*host
) {
206 * If "pophost" is specified in mts.conf,
207 * use it as default value.
209 if (pophost
&& *pophost
)
220 status
= remotemail (host
, port
, user
, proxy
, notifysw
, 1,
221 snoop
, sasl
, saslmech
, tls
, auth_svc
);
223 for (vecp
= 0; vec
[vecp
]; vecp
++)
224 status
+= remotemail (host
, port
, vec
[vecp
], proxy
, notifysw
, 0,
225 snoop
, sasl
, saslmech
, tls
, auth_svc
);
228 if (user
== NULL
) user
= getusername ();
232 /* Not sure this check makes sense... */
233 if (!geteuid() || NULL
== (home
= getenv("HOME"))) {
234 pw
= getpwnam (user
);
236 adios (NULL
, "unable to get information about user");
239 status
= checkmail (user
, home
, datesw
, notifysw
, 1);
241 for (vecp
= 0; vec
[vecp
]; vecp
++) {
242 if ((pw
= getpwnam (vec
[vecp
])))
243 status
+= checkmail (pw
->pw_name
, pw
->pw_dir
, datesw
, notifysw
, 0);
245 advise (NULL
, "no such user as %s", vec
[vecp
]);
255 #define NOTE_SWITCHES \
256 X("all", 0, NALLSW) \
257 X("mail", 0, NMAISW) \
258 X("nomail", 0, NNMAISW) \
260 #define X(sw, minchars, id) id,
261 DEFINE_SWITCH_ENUM(NOTE
);
264 #define X(sw, minchars, id) { sw, minchars, id },
265 DEFINE_SWITCH_ARRAY(NOTE
, ntswitches
);
270 donote (char *cp
, int ntflag
)
272 switch (smatch (cp
, ntswitches
)) {
274 ambigsw (cp
, ntswitches
);
277 adios (NULL
, "-%snotify %s unknown", ntflag
? "" : "no", cp
);
287 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
292 checkmail (char *user
, char *home
, int datesw
, int notifysw
, int personal
)
298 snprintf (buffer
, sizeof(buffer
), "%s/%s", mmdfldir
[0] ? mmdfldir
: home
, mmdflfil
[0] ? mmdflfil
: user
);
301 st
.st_atime
= st
.st_mtime
= 0;
303 mf
= (stat (buffer
, &st
) == NOTOK
|| st
.st_size
== 0) ? NONEOK
304 : st
.st_atime
<= st
.st_mtime
? MMDFNEW
: MMDFOLD
;
306 if ((mf
& UUCPOK
) || (mf
& MMDFOK
)) {
307 if (notifysw
& NT_MAIL
) {
309 printf ("You have ");
311 printf ("%s has ", user
);
313 printf ("%s old-style bell", mf
& UUCPOLD
? "old" : "new");
314 if ((mf
& UUCPOK
) && (mf
& MMDFOK
))
317 printf ("%s%s", mf
& MMDFOLD
? "old" : "new",
318 mf
& UUCPOK
? " Internet" : "");
319 printf (" mail waiting");
326 if (notifysw
& NT_NMAI
)
327 printf (personal
? "You don't %s%s" : "%s doesn't %s",
328 personal
? "" : user
, "have any mail waiting");
336 if (datesw
&& st
.st_atime
)
337 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
)
352 int nmsgs
, nbytes
, status
;
354 if (auth_svc
== NULL
) {
355 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
356 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
360 adios (NULL
, "must specify -user with -saslmech xoauth2");
364 /* open the POP connection */
365 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
, tls
,
367 || pop_stat (&nmsgs
, &nbytes
) == NOTOK
/* check for messages */
368 || pop_quit () == NOTOK
) { /* quit POP connection */
369 advise (NULL
, "%s", response
);
374 if (notifysw
& NT_MAIL
) {
376 printf ("You have ");
378 printf ("%s has ", user
);
380 printf ("%d message%s (%d bytes)",
381 nmsgs
, nmsgs
!= 1 ? "s" : "", nbytes
);
388 if (notifysw
& NT_NMAI
)
389 printf (personal
? "You don't %s%s" : "%s doesn't %s",
390 personal
? "" : user
, "have any mail waiting");
396 printf (" on %s\n", host
);