]> diplodocus.org Git - nmh/blob - uip/msgchk.c
Another pass at cleaning up (some of) the manpages.
[nmh] / uip / msgchk.c
1
2 /*
3 * msgchk.c -- check for mail
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/mts.h>
12 #include <h/tws.h>
13 #include <pwd.h>
14
15 #include <h/popsbr.h>
16
17 #ifndef CYRUS_SASL
18 # define SASLminc(a) (a)
19 #else
20 # define SASLminc(a) 0
21 #endif
22
23 #define MSGCHK_SWITCHES \
24 X("date", 0, DATESW) \
25 X("nodate", 0, NDATESW) \
26 X("notify type", 0, NOTESW) \
27 X("nonotify type", 0, NNOTESW) \
28 X("host hostname", 0, HOSTSW) \
29 X("user username", 0, USERSW) \
30 X("port name/number", 0, PORTSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
33 X("snoop", -5, SNOOPSW) \
34 X("sasl", SASLminc(-4), SASLSW) \
35 X("saslmech", SASLminc(-5), SASLMECHSW) \
36 X("proxy command", 0, PROXYSW) \
37
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MSGCHK);
40 #undef X
41
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MSGCHK, switches);
44 #undef X
45
46 /*
47 * Maximum numbers of users we can check (plus
48 * one for the NULL vector at the end).
49 */
50 #define MAXVEC 51
51
52 #define NT_NONE 0x0
53 #ifdef NT_NONE
54 #endif /* Use NT_NONE to prevent warning from gcc -Wunused-macros. */
55 #define NT_MAIL 0x1
56 #define NT_NMAI 0x2
57 #define NT_ALL (NT_MAIL | NT_NMAI)
58
59 #define NONEOK 0x0
60 #define UUCPOLD 0x1
61 #define UUCPNEW 0x2
62 #define UUCPOK (UUCPOLD | UUCPNEW)
63 #define MMDFOLD 0x4
64 #define MMDFNEW 0x8
65 #define MMDFOK (MMDFOLD | MMDFNEW)
66
67
68 /*
69 * static prototypes
70 */
71 static int donote (char *, int);
72 static int checkmail (char *, char *, int, int, int);
73 static int remotemail (char *, char *, char *, char *, int, int, int, int,
74 char *);
75
76
77 int
78 main (int argc, char **argv)
79 {
80 int datesw = 1, notifysw = NT_ALL;
81 int status = 0, sasl = 0;
82 int snoop = 0, vecp = 0;
83 char *cp, *host = NULL, *port = NULL, *user = NULL, *proxy = NULL;
84 char buf[BUFSIZ], *saslmech = NULL;
85 char **argp, **arguments, *vec[MAXVEC];
86 struct passwd *pw;
87
88 if (nmh_init(argv[0], 1)) { return 1; }
89
90 mts_init (invo_name);
91
92 arguments = getarguments (invo_name, argc, argv, 1);
93 argp = arguments;
94
95 while ((cp = *argp++)) {
96 if (*cp == '-') {
97 switch (smatch (++cp, switches)) {
98 case AMBIGSW:
99 ambigsw (cp, switches);
100 done (1);
101 case UNKWNSW:
102 adios (NULL, "-%s unknown", cp);
103
104 case HELPSW:
105 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
106 invo_name);
107 print_help (buf, switches, 1);
108 done (0);
109 case VERSIONSW:
110 print_version(invo_name);
111 done (0);
112
113 case DATESW:
114 datesw++;
115 continue;
116 case NDATESW:
117 datesw = 0;
118 continue;
119
120 case NOTESW:
121 if (!(cp = *argp++) || *cp == '-')
122 adios (NULL, "missing argument to %s", argp[-2]);
123 notifysw |= donote (cp, 1);
124 continue;
125 case NNOTESW:
126 if (!(cp = *argp++) || *cp == '-')
127 adios (NULL, "missing argument to %s", argp[-2]);
128 notifysw &= ~donote (cp, 0);
129 continue;
130
131 case HOSTSW:
132 if (!(host = *argp++) || *host == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
134 continue;
135
136 case PORTSW:
137 if (!(port = *argp++) || *port == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
139 continue;
140
141 case USERSW:
142 if (!(cp = *argp++) || *cp == '-')
143 adios (NULL, "missing argument to %s", argp[-2]);
144 if (vecp >= MAXVEC-1)
145 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
146 else
147 user = vec[vecp++] = cp;
148 continue;
149
150 case SNOOPSW:
151 snoop++;
152 continue;
153
154 case SASLSW:
155 sasl++;
156 continue;
157
158 case SASLMECHSW:
159 if (!(saslmech = *argp++) || *saslmech == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
161 continue;
162
163 case PROXYSW:
164 if (!(proxy = *argp++) || *proxy == '-')
165 adios (NULL, "missing argument to %s", argp[-2]);
166 continue;
167 }
168 }
169 if (vecp >= MAXVEC-1)
170 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
171 else
172 vec[vecp++] = cp;
173 }
174
175 /*
176 * If -host is not specified by user
177 */
178 if (!host || !*host) {
179 /*
180 * If "pophost" is specified in mts.conf,
181 * use it as default value.
182 */
183 if (pophost && *pophost)
184 host = pophost;
185 }
186 if (!host || !*host)
187 host = NULL;
188
189 if (vecp != 0)
190 vec[vecp] = NULL;
191
192 if (host) {
193 if (vecp == 0) {
194 status = remotemail (host, port, user, proxy, notifysw, 1,
195 snoop, sasl, saslmech);
196 } else {
197 for (vecp = 0; vec[vecp]; vecp++)
198 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
199 snoop, sasl, saslmech);
200 }
201 } else {
202 if (user == NULL) user = getusername ();
203 if (vecp == 0) {
204 char *home;
205
206 /* Not sure this check makes sense... */
207 if (!geteuid() || NULL == (home = getenv("HOME"))) {
208 pw = getpwnam (user);
209 if (pw == NULL)
210 adios (NULL, "unable to get information about user");
211 home = pw->pw_dir;
212 }
213 status = checkmail (user, home, datesw, notifysw, 1);
214 } else {
215 for (vecp = 0; vec[vecp]; vecp++) {
216 if ((pw = getpwnam (vec[vecp])))
217 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
218 else
219 advise (NULL, "no such user as %s", vec[vecp]);
220 }
221 }
222 } /* host == NULL */
223
224 done (status);
225 return 1;
226 }
227
228
229 #define NOTE_SWITCHES \
230 X("all", 0, NALLSW) \
231 X("mail", 0, NMAISW) \
232 X("nomail", 0, NNMAISW) \
233
234 #define X(sw, minchars, id) id,
235 DEFINE_SWITCH_ENUM(NOTE);
236 #undef X
237
238 #define X(sw, minchars, id) { sw, minchars, id },
239 DEFINE_SWITCH_ARRAY(NOTE, ntswitches);
240 #undef X
241
242
243 static int
244 donote (char *cp, int ntflag)
245 {
246 switch (smatch (cp, ntswitches)) {
247 case AMBIGSW:
248 ambigsw (cp, ntswitches);
249 done (1);
250 case UNKWNSW:
251 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
252
253 case NALLSW:
254 return NT_ALL;
255 case NMAISW:
256 return NT_MAIL;
257 case NNMAISW:
258 return NT_NMAI;
259 }
260
261 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
262 }
263
264
265 static int
266 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
267 {
268 int mf, status;
269 char buffer[BUFSIZ];
270 struct stat st;
271
272 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
273 if (datesw) {
274 st.st_size = 0;
275 st.st_atime = st.st_mtime = 0;
276 }
277 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
278 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
279
280 if ((mf & UUCPOK) || (mf & MMDFOK)) {
281 if (notifysw & NT_MAIL) {
282 if (personal)
283 printf ("You have ");
284 else
285 printf ("%s has ", user);
286 if (mf & UUCPOK)
287 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
288 if ((mf & UUCPOK) && (mf & MMDFOK))
289 printf (" and ");
290 if (mf & MMDFOK)
291 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
292 mf & UUCPOK ? " Internet" : "");
293 printf (" mail waiting");
294 } else {
295 notifysw = 0;
296 }
297 status = 0;
298 }
299 else {
300 if (notifysw & NT_NMAI)
301 printf (personal ? "You don't %s%s" : "%s doesn't %s",
302 personal ? "" : user, "have any mail waiting");
303 else
304 notifysw = 0;
305
306 status = 1;
307 }
308
309 if (notifysw)
310 if (datesw && st.st_atime)
311 printf ("; last read on %s", dtime (&st.st_atime, 1));
312 if (notifysw)
313 printf ("\n");
314
315 return status;
316 }
317
318
319 extern char response[];
320
321 static int
322 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
323 int personal, int snoop, int sasl, char *saslmech)
324 {
325 int nmsgs, nbytes, status;
326 struct nmh_creds creds = { 0, 0, 0 };
327
328 /* open the POP connection */
329 nmh_get_credentials (host, user, sasl, &creds);
330 if (pop_init (host, port, creds.user, creds.password, proxy, snoop, sasl,
331 saslmech) == NOTOK
332 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
333 || pop_quit () == NOTOK) { /* quit POP connection */
334 advise (NULL, "%s", response);
335 return 1;
336 }
337
338 if (nmsgs) {
339 if (notifysw & NT_MAIL) {
340 if (personal)
341 printf ("You have ");
342 else
343 printf ("%s has ", user);
344
345 printf ("%d message%s (%d bytes)",
346 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
347 }
348 else
349 notifysw = 0;
350
351 status = 0;
352 } else {
353 if (notifysw & NT_NMAI)
354 printf (personal ? "You don't %s%s" : "%s doesn't %s",
355 personal ? "" : user, "have any mail waiting");
356 else
357 notifysw = 0;
358 status = 1;
359 }
360 if (notifysw)
361 printf (" on %s\n", host);
362
363 return status;
364 }