]> diplodocus.org Git - nmh/blob - uip/msgchk.c
With -messageid random, make the part after the @ more resemble a
[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 LOCALE
96 setlocale(LC_ALL, "");
97 #endif
98 invo_name = r1bindex (argv[0], '/');
99
100 /* read user profile/context */
101 context_read();
102
103 mts_init (invo_name);
104 user = getusername();
105
106 arguments = getarguments (invo_name, argc, argv, 1);
107 argp = arguments;
108
109 while ((cp = *argp++)) {
110 if (*cp == '-') {
111 switch (smatch (++cp, switches)) {
112 case AMBIGSW:
113 ambigsw (cp, switches);
114 done (1);
115 case UNKWNSW:
116 adios (NULL, "-%s unknown", cp);
117
118 case HELPSW:
119 snprintf (buf, sizeof(buf), "%s [switches] [users ...]",
120 invo_name);
121 print_help (buf, switches, 1);
122 done (0);
123 case VERSIONSW:
124 print_version(invo_name);
125 done (0);
126
127 case DATESW:
128 datesw++;
129 continue;
130 case NDATESW:
131 datesw = 0;
132 continue;
133
134 case NOTESW:
135 if (!(cp = *argp++) || *cp == '-')
136 adios (NULL, "missing argument to %s", argp[-2]);
137 notifysw |= donote (cp, 1);
138 continue;
139 case NNOTESW:
140 if (!(cp = *argp++) || *cp == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
142 notifysw &= ~donote (cp, 0);
143 continue;
144
145 case HOSTSW:
146 if (!(host = *argp++) || *host == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
148 continue;
149
150 case PORTSW:
151 if (!(port = *argp++) || *port == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
153 continue;
154
155 case USERSW:
156 if (!(cp = *argp++) || *cp == '-')
157 adios (NULL, "missing argument to %s", argp[-2]);
158 if (vecp >= MAXVEC-1)
159 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
160 else
161 vec[vecp++] = cp;
162 continue;
163
164 case SNOOPSW:
165 snoop++;
166 continue;
167
168 case SASLSW:
169 sasl++;
170 continue;
171
172 case SASLMECHSW:
173 if (!(saslmech = *argp++) || *saslmech == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 continue;
176
177 case PROXYSW:
178 if (!(proxy = *argp++) || *proxy == '-')
179 adios (NULL, "missing argument to %s", argp[-2]);
180 continue;
181 }
182 }
183 if (vecp >= MAXVEC-1)
184 adios (NULL, "you can only check %d users at a time", MAXVEC-1);
185 else
186 vec[vecp++] = cp;
187 }
188
189 /*
190 * If -host is not specified by user
191 */
192 if (!host || !*host) {
193 /*
194 * If "pophost" is specified in mts.conf,
195 * use it as default value.
196 */
197 if (pophost && *pophost)
198 host = pophost;
199 }
200 if (!host || !*host)
201 host = NULL;
202
203 if (vecp != 0)
204 vec[vecp] = NULL;
205
206 if (host) {
207 if (vecp == 0) {
208 status = remotemail (host, port, user, proxy, notifysw, 1,
209 snoop, sasl, saslmech);
210 } else {
211 for (vecp = 0; vec[vecp]; vecp++)
212 status += remotemail (host, port, vec[vecp], proxy, notifysw, 0,
213 snoop, sasl, saslmech);
214 }
215 } else {
216
217 if (vecp == 0) {
218 char *home;
219
220 /* Not sure this check makes sense... */
221 if (!geteuid() || NULL == (home = getenv("HOME"))) {
222 pw = getpwnam (user);
223 if (pw == NULL)
224 adios (NULL, "unable to get information about user");
225 home = pw->pw_dir;
226 }
227 status = checkmail (user, home, datesw, notifysw, 1);
228 } else {
229 for (vecp = 0; vec[vecp]; vecp++) {
230 if ((pw = getpwnam (vec[vecp])))
231 status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
232 else
233 advise (NULL, "no such user as %s", vec[vecp]);
234 }
235 }
236 } /* host == NULL */
237
238 done (status);
239 return 1;
240 }
241
242
243 static struct swit ntswitches[] = {
244 #define NALLSW 0
245 { "all", 0 },
246 #define NMAISW 1
247 { "mail", 0 },
248 #define NNMAISW 2
249 { "nomail", 0 },
250 { NULL, 0 }
251 };
252
253
254 static int
255 donote (char *cp, int ntflag)
256 {
257 switch (smatch (cp, ntswitches)) {
258 case AMBIGSW:
259 ambigsw (cp, ntswitches);
260 done (1);
261 case UNKWNSW:
262 adios (NULL, "-%snotify %s unknown", ntflag ? "" : "no", cp);
263
264 case NALLSW:
265 return NT_ALL;
266 case NMAISW:
267 return NT_MAIL;
268 case NNMAISW:
269 return NT_NMAI;
270 }
271
272 return 0; /* Before 1999-07-15, garbage was returned if control got here. */
273 }
274
275
276 static int
277 checkmail (char *user, char *home, int datesw, int notifysw, int personal)
278 {
279 int mf, status;
280 char buffer[BUFSIZ];
281 struct stat st;
282
283 snprintf (buffer, sizeof(buffer), "%s/%s", mmdfldir[0] ? mmdfldir : home, mmdflfil[0] ? mmdflfil : user);
284 if (datesw) {
285 st.st_size = 0;
286 st.st_atime = st.st_mtime = 0;
287 }
288 mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
289 : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
290
291 if ((mf & UUCPOK) || (mf & MMDFOK)) {
292 if (notifysw & NT_MAIL) {
293 printf (personal ? "You have " : "%s has ", user);
294 if (mf & UUCPOK)
295 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
296 if ((mf & UUCPOK) && (mf & MMDFOK))
297 printf (" and ");
298 if (mf & MMDFOK)
299 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
300 mf & UUCPOK ? " Internet" : "");
301 printf (" mail waiting");
302 } else {
303 notifysw = 0;
304 }
305 status = 0;
306 }
307 else {
308 if (notifysw & NT_NMAI)
309 printf (personal ? "You don't %s%s" : "%s doesn't %s",
310 personal ? "" : user, "have any mail waiting");
311 else
312 notifysw = 0;
313
314 status = 1;
315 }
316
317 if (notifysw)
318 if (datesw && st.st_atime)
319 printf ("; last read on %s", dtime (&st.st_atime, 1));
320 if (notifysw)
321 printf ("\n");
322
323 return status;
324 }
325
326
327 extern char response[];
328
329 static int
330 remotemail (char *host, char *port, char *user, char *proxy, int notifysw,
331 int personal, int snoop, int sasl, char *saslmech)
332 {
333 int nmsgs, nbytes, status;
334 char *pass = NULL;
335
336 if (user == NULL)
337 user = getusername ();
338 if (sasl)
339 pass = getusername ();
340 else
341 ruserpass (host, &user, &pass);
342
343 /* open the POP connection */
344 if (pop_init (host, port, user, pass, proxy, snoop, sasl, saslmech) == NOTOK
345 || pop_stat (&nmsgs, &nbytes) == NOTOK /* check for messages */
346 || pop_quit () == NOTOK) { /* quit POP connection */
347 advise (NULL, "%s", response);
348 return 1;
349 }
350
351 if (nmsgs) {
352 if (notifysw & NT_MAIL) {
353 printf (personal ? "You have " : "%s has ", user);
354 printf ("%d message%s (%d bytes)",
355 nmsgs, nmsgs != 1 ? "s" : "", nbytes);
356 }
357 else
358 notifysw = 0;
359
360 status = 0;
361 } else {
362 if (notifysw & NT_NMAI)
363 printf (personal ? "You don't %s%s" : "%s doesn't %s",
364 personal ? "" : user, "have any mail waiting");
365 else
366 notifysw = 0;
367 status = 1;
368 }
369 if (notifysw)
370 printf (" on %s\n", host);
371
372 return status;
373 }