home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / uk-sendmail2.1 / Support / rmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-11  |  3.7 KB  |  187 lines

  1. #ifndef lint
  2. static char sccsid[] =    "@(#)rmail.c    4.4 (Berkeley) 8/11/83";
  3. #endif
  4.  
  5. /*
  6. **  RMAIL -- UUCP mail server.
  7. **
  8. **    This program reads the >From ... remote from ... lines that
  9. **    UUCP is so fond of and turns them into something reasonable.
  10. **    It calls sendmail giving it a -f option built from these
  11. **    lines.
  12. **
  13. **    Modified to set the sender's hostname (deduced from first
  14. **    "remote from host" line) and convert the final "user" part
  15. **    from an address with '@' and '%' (which mmdf is so fond of)
  16. **    in to a 'pure' uucp address.    -Jim Crammond, (hwcs!jim) 29/11/84
  17. */
  18.  
  19. # include <stdio.h>
  20. # include <sysexits.h>
  21.  
  22. typedef char    bool;
  23. #define TRUE    1
  24. #define FALSE    0
  25.  
  26. extern FILE    *popen();
  27. extern char    *index();
  28. extern char    *rindex();
  29. char    *perc_to_uucp();
  30.  
  31. bool    Debug;
  32.  
  33. # define MAILER    "/usr/lib/sendmail"
  34.  
  35. main(argc, argv)
  36.     char **argv;
  37. {
  38.     FILE *out;     /* output to sendmail */
  39.     char lbuf[512];     /* one line of the message */
  40.     char from[512];     /* accumulated path of sender */
  41.     char ufrom[128]; /* user on remote system */
  42.     char sys[64];     /* a system in path */
  43.     char sysname[64]; /* system received from */
  44.     char cmd[2000];
  45.     register char *cp;
  46.     register char *uf;    /* ptr into ufrom */
  47.     int linecount;
  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(sysname, "");
  67.     (void) strcpy(ufrom, "/dev/null");
  68.  
  69.     linecount = 0;
  70.     while (fgets(lbuf, sizeof lbuf, stdin) != NULL)
  71.     {
  72.         if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
  73.             break;
  74.         linecount++;
  75.         (void) sscanf(lbuf, "%*s %s", ufrom);
  76.         cp = lbuf;
  77.         uf = ufrom;
  78.  
  79.         while ((cp = index(cp, 'r')) != NULL)
  80.         {
  81. #ifdef DEBUG
  82.             if (Debug)
  83.                 printf("cp='%s'\n", cp);
  84. #endif
  85.             if (sscanf(cp, "remote from %s", sys) == 1)
  86.             {    (void) strcat(from, sys);
  87.                 (void) strcat(from, "!");
  88.                 if (linecount == 1)
  89.                     (void) strcpy(sysname, sys);
  90.                 break;
  91.             }
  92.             cp++;
  93.         }
  94. #ifdef DEBUG
  95.         if (Debug)
  96.             printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
  97. #endif
  98.     }
  99.  
  100.     /*
  101.      *  check for percent style addresses in user field
  102.      */
  103.     if (index(uf, '@') != NULL || index(uf, '%') != NULL)
  104.         uf = perc_to_uucp(uf);
  105.  
  106.     /*
  107.      *  if this is a new style "From domain!user .. remote from system"
  108.      *  header then don't prepend the system name to the from person
  109.      */
  110.     if (linecount == 1 && (cp = index(uf, '!')) != NULL)
  111.     {    char  *p = index(uf, '.');
  112.         if (p != NULL && p < cp)
  113.             (void) strcpy(from, uf);
  114.         else
  115.             (void) strcat(from, uf);
  116.     }
  117.     else
  118.         (void) strcat(from, uf);
  119.  
  120.     (void) sprintf(cmd, "%s -ee -oi -f%s", MAILER, from);
  121.     if (*sysname != '\0')
  122.     {    (void) strcat(cmd, " -oMs");
  123.         (void) strcat(cmd, sysname);
  124.     }
  125.  
  126.     while (*++argv != NULL)
  127.     {
  128.         (void) strcat(cmd, " '");
  129.         if (**argv == '(')
  130.             (void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
  131.         else
  132.             (void) strcat(cmd, *argv);
  133.         (void) strcat(cmd, "'");
  134.     }
  135. #ifdef DEBUG
  136.     if (Debug)
  137.         printf("cmd='%s'\n", cmd);
  138. #endif
  139.     out = popen(cmd, "w");
  140.     fputs(lbuf, out);
  141.     while (fgets(lbuf, sizeof lbuf, stdin))
  142.         fputs(lbuf, out);
  143.     i = pclose(out);
  144.     if ((i & 0377) != 0)
  145.     {
  146.         fprintf(stderr, "pclose: status 0%o\n", i);
  147.         exit(EX_OSERR);
  148.     }
  149.  
  150.     exit((i >> 8) & 0377);
  151. }
  152.  
  153.  
  154. /*
  155. **  PERC_TO_UUCP  --  converts an address in Percent style into uucp style
  156. **
  157. **    e.g.  user%c.bitnet%b.arpa@a.uucp -> a.uucp!b.arpa!c.bitnet!user
  158. */    
  159. char    *
  160. perc_to_uucp(addr)
  161. char    *addr;
  162. {
  163.     static    char    buf[512];
  164.     char    *bp = buf;
  165.     char    *p;
  166.  
  167. #ifdef DEBUG
  168.     if (Debug)
  169.         printf("perc_to_uucp(%s) ", addr);
  170. #endif
  171.  
  172.     while ((p = rindex(addr,'@')) != NULL || (p = rindex(addr,'%')) != NULL)
  173.     {
  174.         *p++ = '\0';
  175.         while (*p)
  176.             *bp++ = *p++;
  177.  
  178.         *bp++ = '!';
  179.     }
  180.  
  181.     strcpy(bp, addr);
  182. #ifdef DEBUG
  183.     printf("returns %s\n", buf);
  184. #endif
  185.     return(buf);
  186. }
  187.