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