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