home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / finger / part03 / mcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  4.8 KB  |  190 lines

  1. /*
  2.  * mcheck.c -- check for mail
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: mcheck.c,v 3.0 90/07/06 13:11:19 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include <sys/types.h>
  20. # include <sys/stat.h>
  21. # include <strings.h>
  22. # include <errno.h>
  23. # include <stdio.h>
  24. # include <ctype.h>
  25. # include "person.h"
  26. # include "args.h"            /* before luser.h */
  27. # include "luser.h"
  28. # include "finger.h"
  29.  
  30. extern void add4n();            /* from args.c */
  31.  
  32. /*
  33.  *    it is possible to detect aliasing by invoking sendmail -bv
  34.  *    <user>. This checks .forward files too!!  but is of little use
  35.  *    when the current host is a workstation or other mail moron
  36.  *    that hands off all work to a server.
  37.  *
  38.  *    "ypmatch <user> aliases" DOES work, but no functions 
  39.  *    are provided to access the mail.alias yp map.  TODO
  40.  */
  41.  
  42. # define LEN 1024
  43. GLOBAL void mcheck( u )
  44. LUSER *u;
  45. {
  46. # ifdef MAIL_SPOOL
  47.     BOOL nomailbox;
  48.     char fname[LEN];
  49.     struct stat stb;
  50.     extern int errno;
  51.     extern char *sys_errlist[];
  52.  
  53. # ifdef AIX_PS2
  54.     if( u->u_person == NULL )
  55.     return;
  56.     strcpy(fname, u->u_person->p_home );
  57.     strcat(fname, "/.newmail");
  58. # else  /* AIX_PS2 not defined */
  59.     strcpy(fname, MAIL_SPOOL );
  60.     strcat(fname, "/");
  61.     strcat(fname, u->u_user );
  62. # endif /* AIX_PS2 not defined */
  63.  
  64.     nomailbox = FALSE;
  65.     if( stat(fname, &stb) != 0 ) {
  66.     switch( errno ) {
  67.         case ENOENT:
  68.         nomailbox = TRUE;
  69.         break;
  70.  
  71.     case EPERM:
  72.         outline("  [Mail file protected]");
  73.         break;
  74.  
  75.     default:
  76.         sprintf(fname, "  [Funny mail file error (%s)]",
  77.             sys_errlist[errno] );
  78.         outline( fname );
  79.     } /* switch */
  80.     } /* stat failed */
  81.     else {                /* stat ok */
  82.     if( stb.st_size > 0  &&  stb.st_mtime >= stb.st_atime ) {
  83.         sprintf(fname, "  %s has new mail as of %s",
  84.             u->u_user, nicetime( stb.st_mtime ) );
  85.  
  86.         if( stb.st_atime != stb.st_ctime ) { /* read since created */
  87.         outline( fname );    /* output previous line */
  88.         sprintf(fname, "  last read %s", nicetime( stb.st_atime ) );
  89.         }
  90.     }
  91.     else                /* empty or access after modify */
  92.         sprintf(fname, "  %s has no new mail, last read %s",
  93.             u->u_user, nicetime( stb.st_atime ) );
  94.     outline( fname );
  95.     } /* stat ok */
  96.  
  97.     if( u->u_person != NULL && u->u_person->p_maddr != NULL ) {
  98.     sprintf(fname, "  [%s is forwarded to %s]",
  99.         (nomailbox ? "Mail" : "New mail"),
  100.         u->u_person->p_maddr );
  101.     outline( fname );
  102.     }
  103.     else if( nomailbox )
  104.     outline("  [No mail file]");
  105. # endif /* MAIL_SPOOL defined */
  106. } /* mcheck */
  107.  
  108. /**** THIS IS GROSS ****/
  109.  
  110. GLOBAL void maddr( p, uname )        /* get mailing address */
  111.     PERSON *p;
  112.     char *uname;            /* for possible alias hackery */
  113. {
  114.     register char *dp, *xp, *ap;
  115.     char fname[ LEN ], obuf[ LEN*3 ], *sp;
  116.     int room;
  117.     FILE *f;
  118.  
  119.     /* try yp_match( domain, "mail.alias", name, namelen, valp, vallenp ) */
  120.  
  121.     strcpy( fname, p->p_home );
  122.     strcat( fname, "/.forward" );
  123.     f = fopen( fname, "r" );
  124.     if( f == NULL )
  125.     return;
  126.  
  127.     dp = obuf;                /* set dest ptr */
  128.     room = sizeof( obuf );
  129.     while( fgets( fname, sizeof( fname ), f ) != NULL ) {
  130.     sp = fname;            /* set src ptr */
  131.  
  132.     for( ; ; ) {
  133.         if( !skipwhite( &sp ) )    /* eat whitespace */
  134.         break;            /* nothing left? */
  135.  
  136.         /* check for " / | \ */
  137.         if( room > 0 &&dp != obuf ) { /* not the first? */
  138.         *dp++ = ',';
  139.         room--;
  140.         }
  141.  
  142.         xp = sp;            /* save start of addr */
  143.         while( *sp != EOS && !isspace( *sp ) && *sp != ',' )
  144.         if( --room > 0 )
  145.             *dp++ = *sp++;    /* copy while not white */
  146.  
  147.         if( *sp != EOS )        /* if not end of source */
  148.         *sp++ = EOS;        /* tie off (blast/skip punct) */
  149.  
  150.         if( sw_follow ) {
  151.         if( (ap = rindex( xp, '@' )) != NULL ) /* have a host? */
  152.             *ap++ = EOS;    /* tie off host */
  153.  
  154.         if( *xp == '\\' )    /* backslash quoted username? */
  155.             xp++;        /* skip bs */
  156.  
  157.         if( *xp != '|' && *xp != '/' ) { /* not pipe or file */
  158.             char *pp;        /* percent ptr */
  159.  
  160.             /* perhaps blast only the rightmost %? */
  161.             pp = xp;        /* blast all %'s in user into @'s */
  162.             while( (pp = index(pp, '%')) != NULL )
  163.             *pp++ = '@';
  164.  
  165.             if( ap != NULL && !islocalhost(ap) )
  166.             add4n(savestr(xp), savestr(ap) ); /* user, host */
  167.             else if( strcmp( xp, uname ) != 0 ) { /* not me? */
  168.             struct switches SavedSw;
  169.             SavedSw = Sw;
  170.             Sw.sw_match = TRUE;
  171.             addlocal( savestr(xp) );
  172.             Sw = SavedSw;
  173.             }
  174.         } /* not pipe or file */
  175.         } /* sw_follow */
  176.     } /* for ever */
  177.     } /* read ok */
  178.     *dp = EOS;                /* tie off */
  179.  
  180.     p->p_maddr = savestr( obuf );    /* fill in person's mailing address */
  181.  
  182.     close( f );
  183. } /* maddr */
  184.  
  185. /*
  186.  * Local variables:
  187.  * comment-column: 40
  188.  * End:
  189.  */
  190.