home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / uumail2 / rmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.6 KB  |  129 lines

  1. #ifndef lint
  2. static char rcsid[]="$Header: rmail.c,v 1.3 85/11/08 03:05:31 sob RELEASE $";
  3.  
  4. #endif
  5.  
  6. /*
  7. **  RMAIL -- UUCP mail server.
  8. **
  9. **    This program reads the >From ... remote from ... lines that
  10. **    UUCP is so fond of and turns them into something reasonable.
  11. **    It calls uumail giving it a -f option built from these
  12. **    lines.
  13. */
  14.  
  15. #define _DEFINE
  16.  
  17. #include "uuconf.h"
  18. extern FILE *popen();
  19. extern char *index();
  20. extern char *rindex();
  21.  
  22. # define MAILER    "/usr/lib/uucp/uumail"
  23.  
  24. main(argc, argv)
  25.     char **argv;
  26. {
  27.     FILE *out;    /* output to mail handler */
  28.     char lbuf[512];    /* one line of the message */
  29.     char from[512];    /* accumulated path of sender */
  30.     char ufrom[64];    /* user on remote system */
  31.     char sys[64];    /* a system in path */
  32.     char junk[512];    /* scratchpad */
  33.     char cmd[2000];
  34.     register char *cp;
  35.     register char *uf;    /* ptr into ufrom */
  36.     int i;
  37.  
  38. # ifdef DEBUG
  39.     if (argc > 1 && strcmp(argv[1], "-T") == 0)
  40.     {
  41.         Debug = TRUE;
  42.         argc--;
  43.         argv++;
  44.     }
  45. # endif DEBUG
  46.  
  47.     if (argc < 2)
  48.     {
  49.         fprintf(stderr, "Usage: rmail user ...\n");
  50.         exit(EX_USAGE);
  51.     }
  52.  
  53.     (void) strcpy(from, "");
  54.     (void) strcpy(ufrom, "/dev/null");
  55.  
  56.     for (;;)
  57.     {
  58.         (void) fgets(lbuf, sizeof lbuf, stdin);
  59.         if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
  60.             break;
  61.         (void) sscanf(lbuf, "%s %s", junk, ufrom);
  62.         cp = lbuf;
  63.         uf = ufrom;
  64.         for (;;)
  65.         {
  66.             cp = index(cp+1, 'r');
  67.             if (cp == NULL)
  68.             {
  69.                 register char *p = rindex(uf, '!');
  70.  
  71.                 if (p != NULL)
  72.                 {
  73.                     *p = '\0';
  74.                     if (uf != NULL) 
  75.                         (void) strcpy(sys, uf);
  76.                     else
  77.                         gethostname(sys,32);
  78.                     uf = p + 1;
  79.                     break;
  80.                 }
  81.                 cp = "remote from somewhere";
  82.             }
  83. #ifdef DEBUG
  84.             if (Debug)
  85.                 printf("cp='%s'\n", cp);
  86. #endif
  87.             if (strncmp(cp, "remote from ", 12)==0)
  88.                 break;
  89.         }
  90.         if (cp != NULL)
  91.             (void) sscanf(cp, "remote from %s", sys);
  92.         (void) strcat(from, sys);
  93.         (void) strcat(from, "!");
  94. #ifdef DEBUG
  95.         if (Debug)
  96.             printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
  97. #endif
  98.     }
  99.     (void) strcat(from, uf);
  100.  
  101. /*    (void) sprintf(cmd, "exec %s -em -f%s", MAILER, from);*/
  102.     (void) sprintf(cmd, "exec %s -f%s", MAILER,from);
  103.     while (*++argv != NULL)
  104.     {
  105.         (void) strcat(cmd, " '");
  106.         if (**argv == '(')
  107.             (void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
  108.         else
  109.             (void) strcat(cmd, *argv);
  110.         (void) strcat(cmd, "'");
  111.     }
  112. #ifdef DEBUG
  113.     if (Debug)
  114.         printf("cmd='%s'\n", cmd);
  115. #endif
  116.     out = popen(cmd, "w");
  117.     fputs(lbuf, out);
  118.     while (fgets(lbuf, sizeof lbuf, stdin))
  119.         fputs(lbuf, out);
  120.     i = pclose(out);
  121.     if ((i & 0377) != 0)
  122.     {
  123.         fprintf(stderr, "pclose: status 0%o\n", i);
  124.         exit(EX_OSERR);
  125.     }
  126.  
  127.     exit((i >> 8) & 0377);
  128. }
  129.