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