Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / RCS / msgchk.c,v
1 head    1.14;
2 access;
3 symbols;
4 locks
5         shettich:1.14; strict;
6 comment @ * @;
7
8
9 1.14
10 date    95.12.06.23.51.28;      author jromine; state Exp;
11 branches;
12 next    1.13;
13
14 1.13
15 date    93.09.01.21.44.19;      author jromine; state Exp;
16 branches;
17 next    1.12;
18
19 1.12
20 date    93.08.20.15.52.41;      author jromine; state Exp;
21 branches;
22 next    1.11;
23
24 1.11
25 date    92.12.15.00.20.22;      author jromine; state Exp;
26 branches;
27 next    1.10;
28
29 1.10
30 date    92.11.04.00.49.17;      author jromine; state Exp;
31 branches;
32 next    1.9;
33
34 1.9
35 date    92.10.16.16.51.48;      author jromine; state Exp;
36 branches;
37 next    1.8;
38
39 1.8
40 date    92.02.04.21.58.40;      author jromine; state Exp;
41 branches;
42 next    1.7;
43
44 1.7
45 date    92.02.04.21.54.20;      author jromine; state Exp;
46 branches;
47 next    1.6;
48
49 1.6
50 date    92.02.03.16.35.46;      author jromine; state Exp;
51 branches;
52 next    1.5;
53
54 1.5
55 date    92.01.31.22.49.20;      author jromine; state Exp;
56 branches;
57 next    1.4;
58
59 1.4
60 date    92.01.31.22.16.47;      author jromine; state Exp;
61 branches;
62 next    1.3;
63
64 1.3
65 date    90.04.05.15.01.32;      author sources; state Exp;
66 branches;
67 next    1.2;
68
69 1.2
70 date    90.02.06.13.21.51;      author sources; state Exp;
71 branches;
72 next    1.1;
73
74 1.1
75 date    90.02.06.13.21.36;      author sources; state Exp;
76 branches;
77 next    ;
78
79
80 desc
81 @@
82
83
84 1.14
85 log
86 @fix: null ptr
87 @
88 text
89 @/* msgchk.c - check for mail */
90 #ifndef lint
91 static char ident[] = "@@(#)$Id: msgchk.c,v 1.13 1993/09/01 21:44:19 jromine Exp jromine $";
92 #endif  /* lint */
93
94 #include "../h/mh.h"
95 #include <stdio.h>
96 #include "../zotnet/mts.h"
97 #include "../zotnet/tws.h"
98 #include <sys/types.h>
99 #include <sys/stat.h>
100 #include <pwd.h>
101 #ifdef LOCALE
102 #include <locale.h>
103 #endif
104 #ifdef HESIOD
105 #include <hesiod.h>
106 #endif
107
108 /* \f */
109
110 #ifndef POP
111 #define POPminc(a)      (a)
112 #else   /* POP */
113 #define POPminc(a)      0
114 #endif  /* POP */
115
116 #ifndef RPOP
117 #define RPOPminc(a)     (a)
118 #else   /* RPOP */
119 #define RPOPminc(a)     0
120 #endif  /* RPOP */
121
122 #ifndef APOP
123 #define APOPminc(a)     (a)
124 #else
125 #define APOPminc(a)     0
126 #endif
127
128 static struct swit  switches[] = {
129 #define DATESW  0
130     "date", 0,
131 #define NDATESW 1
132     "nodate", 0,
133
134 #define NOTESW  2
135     "notify type", 0,
136 #define NNOTESW 3
137     "nonotify type", 0,
138
139 #define HOSTSW  4
140     "host host", POPminc (-4),
141 #define USERSW  5
142     "user user", POPminc (-4),
143
144 #define APOPSW  6
145     "apop", APOPminc (-4),
146 #define NAPOPSW 7
147     "noapop", APOPminc (-6),
148
149 #define RPOPSW  8
150     "rpop", RPOPminc (-4),
151 #define NRPOPSW 9
152     "norpop", RPOPminc (-6),
153
154 #define HELPSW  10
155     "help", 4,
156
157     NULL, 0
158 };
159
160 /* \f */
161
162 #define NT_NONE 0x0
163 #define NT_MAIL 0x1
164 #define NT_NMAI 0x2
165 #define NT_ALL  (NT_MAIL | NT_NMAI)
166
167
168 #define NONEOK  0x0
169 #define UUCPOLD 0x1
170 #define UUCPNEW 0x2
171 #define UUCPOK  (UUCPOLD | UUCPNEW)
172 #define MMDFOLD 0x4
173 #define MMDFNEW 0x8
174 #define MMDFOK  (MMDFOLD | MMDFNEW)
175
176
177 #ifdef  SYS5
178 #ifndef __STDC__
179 struct passwd   *getpwuid(), *getpwnam();
180 #endif /* !__STDC__ */
181 #endif  /* SYS5 */
182
183 static int      donote(), checkmail(), remotemail();
184 /* \f */
185
186 /* ARGSUSED */
187
188 main (argc, argv)
189 int     argc;
190 char   *argv[];
191 {
192     int     datesw = 1,
193             notifysw = NT_ALL,
194             rpop = 1,
195             status = 0,
196             snoop = 0,
197             vecp = 0;
198     int uid = getuid ();
199     char   *cp,
200            *host = NULL,
201             buf[80],
202           **ap,
203           **argp,
204            *arguments[MAXARGS],
205            *vec[50];
206     char *user = getusr ();
207     struct passwd  *pw;
208 #ifdef HESIOD
209     struct hes_postoffice *po;
210     char *tmphost;
211 #endif
212
213 #ifdef LOCALE
214         setlocale(LC_ALL, "");
215 #endif
216     invo_name = r1bindex (argv[0], '/');
217     mts_init (invo_name);
218 #ifdef  POP
219     if ((cp = getenv ("MHPOPDEBUG")) && *cp)
220         snoop++;
221 #endif
222
223     if ((cp = m_find (invo_name)) != NULL) {
224         ap = brkstring (cp = getcpy (cp), " ", "\n");
225         ap = copyip (ap, arguments);
226     }
227     else
228         ap = arguments;
229     (void) copyip (argv + 1, ap);
230     argp = arguments;
231
232 /* \f */
233
234     while (cp = *argp++) {
235         if (*cp == '-')
236             switch (smatch (++cp, switches)) {
237                 case AMBIGSW: 
238                     ambigsw (cp, switches);
239                     done (1);
240                 case UNKWNSW: 
241                     adios (NULLCP, "-%s unknown", cp);
242                 case HELPSW: 
243                     (void) sprintf (buf, "%s [switches] [users ...]",
244                             invo_name);
245                     help (buf, switches);
246                     done (1);
247
248                 case DATESW:
249                     datesw++;
250                     continue;
251                 case NDATESW:
252                     datesw = 0;
253                     continue;
254
255                 case NOTESW:
256                     if (!(cp = *argp++) || *cp == '-')
257                         adios (NULLCP, "missing argument to %s", argp[-2]);
258                     notifysw |= donote (cp, 1);
259                     continue;
260                 case NNOTESW:
261                     if (!(cp = *argp++) || *cp == '-')
262                         adios (NULLCP, "missing argument to %s", argp[-2]);
263                     notifysw &= ~donote (cp, 0);
264                     continue;
265
266                 case HOSTSW: 
267                     if (!(host = *argp++) || *host == '-')
268                         adios (NULLCP, "missing argument to %s", argp[-2]);
269                     continue;
270                 case USERSW: 
271                     if (!(cp = *argp++) || *cp == '-')
272                         adios (NULLCP, "missing argument to %s", argp[-2]);
273                     vec[vecp++] = cp;
274                     continue;
275                 case APOPSW: 
276                     rpop = -1;
277                     continue;
278                 case RPOPSW: 
279                     rpop = 1;
280                     continue;
281                 case NAPOPSW:
282                 case NRPOPSW: 
283                     rpop = 0;
284                     continue;
285             }
286         vec[vecp++] = cp;
287     }
288
289 /* \f */
290 #ifdef  POP
291     if (!host || !*host) {      /* -host not specified by user */
292 #ifdef HESIOD
293         /*
294          * Scheme is:
295          *        use MAILHOST environment variable if present,
296          *  else try Hesiod.
297          *  If that fails, use the default (if any)
298          *  provided by mtstailor in mts_init()
299          */
300         if ((tmphost = getenv("MAILHOST")) != NULL)
301             pophost = tmphost;
302         else if ((po = hes_getmailhost(vecp ? vec[0] : user)) != NULL &&
303                 strcmp(po->po_type, "POP") == 0)
304             pophost = po->po_host;
305 #endif /* HESIOD */
306         if (pophost && *pophost)
307             host = pophost;
308     }
309     if (!host || !*host)
310         host = NULL;
311     if (!host || rpop <= 0)
312         (void) setuid (uid);
313 #endif /* POP */
314     if (vecp == 0) {
315 #ifdef  POP
316         if (host)
317             status = remotemail (host, user, rpop, notifysw, 1, snoop);
318         else
319 #endif
320           {
321             char *home = (uid = geteuid()) ? home = getenv ("HOME") : NULL;
322             if (home == NULL)
323               {
324                 pw = getpwnam (user);
325                 if (pw == NULL)
326                   adios (NULLCP, "unable to get information about user");
327                 if (home == NULL)
328                   home = pw->pw_dir;
329               }
330             status = checkmail (user, home, datesw, notifysw, 1);
331           }
332     }
333     else {
334         vec[vecp] = NULL;
335
336         for (vecp = 0; cp = vec[vecp]; vecp++)
337 #ifdef  POP
338             if (host)
339                 status += remotemail (host, cp, rpop, notifysw, 0, snoop);
340             else
341 #endif
342                 if (pw = getpwnam (cp))
343                     status += checkmail (pw->pw_name, pw->pw_dir, datesw, notifysw, 0);
344                 else
345                     advise (NULLCP, "no such user as %s", cp);
346     }
347
348     done (status);
349 }
350
351 /* \f */
352
353 static struct swit ntswitches[] = {
354 #define NALLSW  0
355     "all", 0,
356 #define NMAISW  1
357     "mail", 0,
358 #define NNMAISW 2
359     "nomail", 0,
360
361     NULL, 0
362 };
363
364
365 static int donote (cp, ntflag)
366 register char   *cp;
367 int     ntflag;
368 {
369     switch (smatch (cp, ntswitches)) {
370         case AMBIGSW: 
371             ambigsw (cp, ntswitches);
372             done (1);
373         case UNKWNSW: 
374             adios (NULLCP, "-%snotify %s unknown", ntflag ? "" : "no", cp);
375
376         case NALLSW: 
377             return NT_ALL;
378         case NMAISW: 
379             return NT_MAIL;
380         case NNMAISW: 
381             return NT_NMAI;
382     }
383 }
384
385 /* \f */
386
387 #ifdef  MF
388 /* ARGSUSED */
389 #endif  /* MF */
390
391 static int  checkmail (user, home, datesw, notifysw, personal)
392 register char *user, *home;
393 int     datesw,
394         notifysw,
395         personal;
396 {
397     int     mf,
398             status;
399     char    buffer[BUFSIZ];
400     struct stat st;
401
402     (void) sprintf (buffer, "%s/%s",
403             mmdfldir[0] ? mmdfldir : home,
404             mmdflfil[0] ? mmdflfil : user);
405 #ifndef MF
406     if (datesw) {
407         st.st_size = 0;
408         st.st_atime = st.st_mtime = 0;
409     }
410 #endif  /* MF */
411     mf = (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
412         : st.st_atime <= st.st_mtime ? MMDFNEW : MMDFOLD;
413
414 #ifdef  MF
415     if (umincproc != NULL && *umincproc != NULL) {
416         (void) sprintf (buffer, "%s/%s",
417                 uucpldir[0] ? uucpldir : home,
418                 uucplfil[0] ? uucplfil : user);
419         mf |= (stat (buffer, &st) == NOTOK || st.st_size == 0) ? NONEOK
420             : st.st_atime <= st.st_mtime ? UUCPNEW : UUCPOLD;
421     }
422 #endif  /* MF */
423
424     if ((mf & UUCPOK) || (mf & MMDFOK)) {
425         if (notifysw & NT_MAIL) {
426             printf (personal ? "You have " : "%s has ", user);
427             if (mf & UUCPOK)
428                 printf ("%s old-style bell", mf & UUCPOLD ? "old" : "new");
429             if ((mf & UUCPOK) && (mf & MMDFOK))
430                 printf (" and ");
431             if (mf & MMDFOK)
432                 printf ("%s%s", mf & MMDFOLD ? "old" : "new",
433                         mf & UUCPOK ? " Internet" : "");
434             printf (" mail waiting");
435         }
436         else
437             notifysw = 0;
438
439         status = 0;
440     }
441     else {
442         if (notifysw & NT_NMAI)
443             printf (personal ? "You don't %s%s" : "%s doesn't %s",
444                     personal ? "" : user, "have any mail waiting");
445         else
446             notifysw = 0;
447
448         status = 1;
449     }
450
451 #ifndef MF
452     if (notifysw)
453         if (datesw && st.st_atime)
454             printf ("; last read on %s",
455                     dasctime (dlocaltime ((long *) & st.st_atime), TW_NULL));
456 #endif  /* MF */
457     if (notifysw)
458         printf ("\n");
459
460     return status;
461 }
462
463 /* \f */
464
465 #ifdef  POP
466 extern  char response[];
467
468
469 static int  remotemail (host, user, rpop, notifysw, personal, snoop)
470 register char   *host;
471 char   *user;
472 int     rpop,
473         notifysw,
474         personal,
475         snoop;
476 {
477     int     nmsgs,
478             nbytes,
479             status;
480     char   *pass = NULL;
481
482     if (user == NULL)
483         user = getusr ();
484     if (rpop > 0)
485         pass = getusr ();
486     else
487         ruserpass (host, &user, &pass);
488
489     if (pop_init (host, user, pass, snoop, rpop) == NOTOK
490             || pop_stat (&nmsgs, &nbytes) == NOTOK
491             || pop_quit () == NOTOK) {
492         advise (NULLCP, "%s", response);
493         return 1;
494     }
495
496     if (nmsgs) {
497         if (notifysw & NT_MAIL) {
498             printf (personal ? "You have " : "%s has ", user);
499             printf ("%d message%s (%d bytes)",
500                     nmsgs, nmsgs != 1 ? "s" : "", nbytes);
501         }
502         else
503             notifysw = 0;
504
505         status = 0;
506     }
507     else {
508         if (notifysw & NT_NMAI)
509             printf (personal ? "You don't %s%s" : "%s doesn't %s",
510                     personal ? "" : user, "have any mail waiting");
511         else
512             notifysw = 0;
513         status = 1;
514     }
515     if (notifysw)
516         printf (" on %s\n", host);
517
518     return status;
519 }
520 #endif  /* POP */
521 @
522
523
524 1.13
525 log
526 @don't override -host value with pophost
527 @
528 text
529 @d3 1
530 a3 1
531 static char ident[] = "@@(#)$Id: msgchk.c,v 1.12 1993/08/20 15:52:41 jromine Exp jromine $";
532 d392 1
533 a392 1
534     char   *pass;
535 @
536
537
538 1.12
539 log
540 @better user defaulting on rpop/apop
541 should check to make sure this works OK
542 @
543 text
544 @d3 1
545 a3 1
546 static char ident[] = "@@(#)$Id: msgchk.c,v 1.11 1992/12/15 00:20:22 jromine Exp jromine $";
547 d203 1
548 d205 7
549 a211 9
550     /*
551       * Scheme is:
552       *        use MAILHOST environment variable if present,
553       *  else try Hesiod.
554       *  If that fails, use the default (if any)
555       *  provided by mtstailor in mts_init()
556       */
557     if (pophost == NULL || pophost[0] == '\0')
558       {
559 d213 1
560 a213 1
561           pophost = tmphost;
562 d215 2
563 a216 3
564                  strcmp(po->po_type, "POP") == 0)
565           pophost = po->po_host;
566       }
567 d218 3
568 a220 2
569     if (pophost && *pophost)
570         host = pophost;
571 @
572
573
574 1.11
575 log
576 @endif sugar
577 @
578 text
579 @d3 1
580 a3 1
581 static char ident[] = "@@(#)$Id: msgchk.c,v 1.10 1992/11/04 00:49:17 jromine Exp jromine $";
582 d395 3
583 a397 3
584     if (rpop) {
585         if (user == NULL)
586             user = getusr ();
587 a398 1
588     }
589 @
590
591
592 1.10
593 log
594 @LOCALE
595 @
596 text
597 @d3 2
598 a4 2
599 static char ident[] = "@@(#)$Id: msgchk.c,v 1.9 1992/10/16 16:51:48 jromine Exp jromine $";
600 #endif  lint
601 d24 1
602 a24 1
603 #else   POP
604 d26 1
605 a26 1
606 #endif  POP
607 d30 1
608 a30 1
609 #else   RPOP
610 d32 1
611 a32 1
612 #endif  RPOP
613 d93 1
614 a93 1
615 #endif  SYS5
616 d302 1
617 a302 1
618 #endif  MF
619 d323 1
620 a323 1
621 #endif  MF
622 d335 1
623 a335 1
624 #endif  MF
625 d369 1
626 a369 1
627 #endif  MF
628 d434 1
629 a434 1
630 #endif  POP
631 @
632
633
634 1.9
635 log
636 @security fix
637 @
638 text
639 @d3 1
640 a3 1
641 static char ident[] = "@@(#)$Id: msgchk.c,v 1.8 1992/02/04 21:58:40 jromine Exp jromine $";
642 d13 3
643 d125 3
644 @
645
646
647 1.8
648 log
649 @don't call getusr() unless needed
650 @
651 text
652 @d3 1
653 a3 1
654 static char ident[] = "@@(#)$Id: msgchk.c,v 1.7 1992/02/04 21:54:20 jromine Exp jromine $";
655 d389 5
656 a393 4
657     if (user == NULL)
658         user = getusr ();
659     if (rpop > 0)
660         pass = user;
661 @
662
663
664 1.7
665 log
666 @contributed patch
667 @
668 text
669 @d3 1
670 a3 1
671 static char ident[] = "@@(#)$Id: msgchk.c,v 1.6 1992/02/03 16:35:46 jromine Exp jromine $";
672 d392 1
673 a392 1
674         pass = getusr ();
675 @
676
677
678 1.6
679 log
680 @fix
681 @
682 text
683 @d3 1
684 a3 1
685 static char ident[] = "@@(#)$Id: msgchk.c,v 1.5 1992/01/31 22:49:20 jromine Exp jromine $";
686 d31 6
687 d53 6
688 a58 1
689 #define RPOPSW  6
690 d60 1
691 a60 1
692 #define NRPOPSW 7
693 d63 1
694 a63 1
695 #define HELPSW  8
696 d181 3
697 d185 1
698 a185 1
699                     rpop++;
700 d187 1
701 d218 1
702 a218 1
703     if (!host || !rpop)
704 d389 4
705 a392 5
706     if (rpop) {
707         if (user == NULL)
708             user = getusr ();
709         pass = user;
710     }
711 @
712
713
714 1.5
715 log
716 @kerberos fix
717 @
718 text
719 @d3 1
720 a3 1
721 static char ident[] = "@@(#)$Id: msgchk.c,v 1.4 1992/01/31 22:16:47 jromine Exp jromine $";
722 d76 1
723 d78 1
724 @
725
726
727 1.4
728 log
729 @kerberos
730 @
731 text
732 @d3 1
733 a3 1
734 static char ident[] = "@@(#)$Id: msgchk.c,v 1.3 1990/04/05 15:01:32 sources Exp jromine $";
735 d111 1
736 d114 1
737 @
738
739
740 1.3
741 log
742 @add ID
743 @
744 text
745 @d3 1
746 a3 1
747 static char ident[] = "@@(#)$Id:$";
748 d13 3
749 d55 1
750 a55 1
751     NULL, NULL
752 d94 1
753 d102 1
754 d104 4
755 a110 3
756 #ifdef  POP
757     if (pophost && *pophost)
758         host = pophost;
759 d113 1
760 a113 1
761 #endif  POP
762 a176 1
763
764 d178 19
765 d200 2
766 a201 2
767         (void) setuid (getuid ());
768 #endif  POP
769 d205 1
770 a205 1
771             status = remotemail (host, NULLCP, rpop, notifysw, 1, snoop);
772 d207 13
773 a219 5
774 #endif  POP
775             if ((pw = getpwuid (getuid ())) == NULL)
776                 adios (NULLCP, "you lose");
777             else
778                 status = checkmail (pw, datesw, notifysw, 1);
779 d229 1
780 a229 1
781 #endif  POP
782 d231 1
783 a231 1
784                     status += checkmail (pw, datesw, notifysw, 0);
785 d249 1
786 a249 1
787     NULL, NULL
788 d279 2
789 a280 2
790 static int  checkmail (pw, datesw, notifysw, personal)
791 register struct passwd  *pw;
792 d291 2
793 a292 2
794             mmdfldir[0] ? mmdfldir : pw -> pw_dir,
795             mmdflfil[0] ? mmdflfil : pw -> pw_name);
796 d305 2
797 a306 2
798                 uucpldir[0] ? uucpldir : pw -> pw_dir,
799                 uucplfil[0] ? uucplfil : pw -> pw_name);
800 d314 1
801 a314 1
802             printf (personal ? "You have " : "%s has ", pw -> pw_name);
803 d332 1
804 a332 1
805                     personal ? "" : pw -> pw_name, "have any mail waiting");
806 d373 1
807 a373 1
808         pass = getusr ();
809 @
810
811
812 1.2
813 log
814 @ANSI Compilance
815 @
816 text
817 @d2 3
818 @
819
820
821 1.1
822 log
823 @Initial revision
824 @
825 text
826 @d73 1
827 @