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