home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume16 / deliver / part03 / uucp.c < prev   
C/C++ Source or Header  |  1988-11-14  |  3KB  |  164 lines

  1. /* $Header: uucp.c,v 1.1 88/06/06 09:39:42 chip Exp $
  2.  *
  3.  * Handle mail destined for other hosts via UUCP.
  4.  * Deliver is intended as a very low-level program, so we don't
  5.  * do anything fancy here.  We just hand the message to uux.
  6.  *
  7.  * $Log:    uucp.c,v $
  8.  * Revision 1.1  88/06/06  09:39:42  chip
  9.  * Initial revision
  10.  * 
  11.  */
  12.  
  13. #include "deliver.h"
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16.  
  17. /*
  18.  * Local functions.
  19.  */
  20.  
  21. static  int     uucp_copy();
  22.  
  23. /*----------------------------------------------------------------------
  24.  * Send mail to UUCP addresses (if any).
  25.  * This is a simple implementation: invoke uux once per address.
  26.  */
  27.  
  28. uucp_deliver()
  29. {
  30.     struct stat st;
  31.     DEST    *d;
  32.     char    *uux;
  33.     static char uux1[] = "/bin/uux";
  34.     static char uux2[] = "/usr/bin/uux";
  35.  
  36.     if (stat(uux1, &st) == 0)
  37.         uux = uux1;
  38.     else if (stat(uux2, &st) == 0)
  39.         uux = uux2;
  40.     else
  41.     {
  42.         error("can't find uux!?\n");
  43.         return;
  44.     }
  45.  
  46.     for (d = first_dest(); d; d = next_dest(d))
  47.     {
  48.         FILE    *uux_fp;
  49.         char    *bang;
  50.         char    *av[5];
  51.         char    rmail[40];
  52.         char    who[BUFSIZ];
  53.  
  54.         if (d->class != CL_UUCP || d->state != ST_WORKING)
  55.             continue;
  56.  
  57.         if (printaddrs)
  58.             (void) printf("%s\n", d->name);
  59.  
  60.         if (dryrun)
  61.         {
  62.             d->state = ST_DONE;
  63.             continue;
  64.         }
  65.  
  66.         bang = strchr(d->name, '!');
  67.         *bang = 0;
  68.         (void) sprintf(rmail, "%s!rmail", d->name);
  69.         *bang++ = '!';
  70.         (void) sprintf(who, "(%s)", bang);
  71.  
  72.         av[0] = "uux";
  73.         av[1] = "-";
  74.         av[2] = rmail;
  75.         av[3] = who;
  76.         av[4] = NULL;
  77.         if ((uux_fp = ct_popenv(eff_ct, uux, av, "w")) == NULL)
  78.             continue;
  79.  
  80.         if (uucp_copy(uux_fp) < 0)
  81.         {
  82.             d->state = ST_ERROR;
  83.             d->error = "Error piping to uux";
  84.         }
  85.  
  86.         if (ct_pclose(uux_fp))
  87.         {
  88.             /* Overrides any problems with uucp_copy() */
  89.  
  90.             d->state = ST_ERROR;
  91.             d->error = "UUCP not available to that host";
  92.         }
  93.         else
  94.             d->state = ST_DONE;
  95.     }
  96. }
  97.  
  98. /*----------------------------------------------------------------------
  99.  * Write the message for UUCP transmission to the given file.
  100.  */
  101.  
  102. static int
  103. uucp_copy(ofp)
  104. FILE    *ofp;
  105. {
  106.     FILE    *ifp;
  107.     char    *p;
  108.     register int c;
  109.     int     fd;
  110.     char    buf[BUFSIZ];
  111.  
  112.     if ((fd = dup(tfd[T_HEADER])) == -1)
  113.     {
  114.         syserr("can't dup header fd");
  115.         return -1;
  116.     }
  117.     (void) lseek(fd, 0L, 0);
  118.     if ((ifp = fdopen(fd, "r")) == NULL)
  119.     {
  120.         error("can't fdopen header fd");
  121.         return -1;
  122.     }
  123.  
  124.     /*
  125.      * Copy the header, but tack "remote from" onto the end of the
  126.      * From_ line.  (If it weren't for dealing with the From_ line,
  127.      * I'd skip stream I/O altogether and use read/write.  Maybe
  128.      * I should save the length of the From_ line when I copy it...)
  129.      */
  130.  
  131.     (void) fgets(buf, GETSIZE(buf), ifp);
  132.     if ((p = strchr(buf, '\n')) != NULL)
  133.         *p = 0;
  134.     (void) fprintf(ofp, "%s remote from %s\n", buf, hostname);
  135.  
  136.     while ((c = getc(ifp)) != EOF)
  137.         (void) putc(c, ofp);
  138.  
  139.     (void) fclose(ifp);
  140.  
  141.     /*
  142.      * Copy the body
  143.      */
  144.  
  145.     if ((fd = dup(tfd[T_BODY])) == -1)
  146.     {
  147.         syserr("can't dup body fd");
  148.         return -1;
  149.     }
  150.     (void) lseek(fd, 0L, 0);
  151.     if ((ifp = fdopen(fd, "r")) == NULL)
  152.     {
  153.         error("can't fdopen body fd");
  154.         (void) close(fd);
  155.         return -1;
  156.     }
  157.  
  158.     while ((c = getc(ifp)) != EOF)
  159.         (void) putc(c, ofp);
  160.  
  161.     (void) fclose(ifp);
  162.     return 0;
  163. }
  164.