home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / uumail3 / part1 / rmail.c < prev    next >
C/C++ Source or Header  |  1986-11-30  |  4KB  |  146 lines

  1. #ifndef lint
  2. static char rcsid[]="$Header: rmail.c,v 3.0 86/03/14 12:04:54 sob RELEASE_3 $";
  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. This work in its current form is Copyright 1986 Stan Barber
  15. with the exception of opath, gethostname and the original getpath which
  16. as far as I know are in the Public Domain. This software may be distributed
  17. freely as long as no profit is made from such distribution and this notice
  18. is reproducted in whole.
  19. ***************************************************************************
  20. This software is provided on an "as is" basis with no guarantee of 
  21. usefulness or correctness of operation for any purpose, intended or
  22. otherwise. The author is in no way liable for this software's performance
  23. or any damage it may cause to any data of any kind anywhere.
  24. ***************************************************************************
  25. */
  26.  
  27. #define _DEFINE
  28.  
  29. #include "uuconf.h"
  30. extern FILE *popen();
  31. extern char *index();
  32. extern char *rindex();
  33.  
  34. # define MAILER    "/usr/lib/uucp/uumail"
  35.  
  36. main(argc, argv)
  37.     char **argv;
  38. {
  39.     FILE *out;    /* output to mail handler */
  40.     char lbuf[512];    /* one line of the message */
  41.     char from[512];    /* accumulated path of sender */
  42.     char ufrom[64];    /* user on remote system */
  43.     char sys[64];    /* a system in path */
  44.     char junk[512];    /* scratchpad */
  45.     char cmd[2000];
  46.     register char *cp;
  47.     register char *uf;    /* ptr into ufrom */
  48.     int i;
  49.  
  50. # ifdef DEBUG
  51.     if (argc > 1 && strcmp(argv[1], "-T") == 0)
  52.     {
  53.         Debug = TRUE;
  54.         argc--;
  55.         argv++;
  56.     }
  57. # endif DEBUG
  58.  
  59.     if (argc < 2)
  60.     {
  61.         fprintf(stderr, "Usage: rmail user ...\n");
  62.         exit(EX_USAGE);
  63.     }
  64.  
  65.     (void) strcpy(from, "");
  66.     (void) strcpy(ufrom, "/dev/null");
  67.     uf = NULL;
  68.  
  69.     for (;;)
  70.     {
  71.         (void) fgets(lbuf, sizeof lbuf, stdin);
  72.         if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
  73.             break;
  74.         (void) sscanf(lbuf, "%s %s", junk, ufrom);
  75.         cp = lbuf;
  76.         uf = ufrom;
  77.         for (;;)
  78.         {
  79.             cp = index(cp+1, 'r');
  80.             if (cp == NULL)
  81.             {
  82.                 register char *p = rindex(uf, '!');
  83.  
  84.                 if (p != NULL)
  85.                 {
  86.                     *p = '\0';
  87.                     if (uf != NULL) 
  88.                         (void) strcpy(sys, uf);
  89.                     else
  90.                         gethostname(sys,32);
  91.                     uf = p + 1;
  92.                     break;
  93.                 }
  94.                 cp = "remote from somewhere";
  95.             }
  96. #ifdef DEBUG
  97.             if (Debug)
  98.                 printf("cp='%s'\n", cp);
  99. #endif
  100.             if (strncmp(cp, "remote from ", 12)==0)
  101.                 break;
  102.         }
  103.         if (cp != NULL)
  104.             (void) sscanf(cp, "remote from %s", sys);
  105.         (void) strcat(from, sys);
  106.         (void) strcat(from, "!");
  107. #ifdef DEBUG
  108.         if (Debug)
  109.             printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
  110. #endif
  111.     }
  112.     if (uf != NULL)
  113.         (void) strcat(from, uf);
  114.  
  115. /*    (void) sprintf(cmd, "exec %s -em -f%s", MAILER, from);*/
  116.     if (from[0] == '\0')
  117.         (void) sprintf(cmd, "exec %s", MAILER);
  118.     else
  119.         (void) sprintf(cmd, "exec %s -f%s", MAILER, from);
  120.     while (*++argv != NULL)
  121.     {
  122.         (void) strcat(cmd, " '");
  123.         if (**argv == '(')
  124.             (void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
  125.         else
  126.             (void) strcat(cmd, *argv);
  127.         (void) strcat(cmd, "'");
  128.     }
  129. #ifdef DEBUG
  130.     if (Debug)
  131.         printf("cmd='%s'\n", cmd);
  132. #endif
  133.     out = popen(cmd, "w");
  134.     fputs(lbuf, out);
  135.     while (fgets(lbuf, sizeof lbuf, stdin))
  136.         fputs(lbuf, out);
  137.     i = pclose(out);
  138.     if ((i & 0377) != 0)
  139.     {
  140.         fprintf(stderr, "pclose: status 0%o\n", i);
  141.         exit(EX_OSERR);
  142.     }
  143.  
  144.     exit((i >> 8) & 0377);
  145. }
  146.