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