home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / returnadd.c < prev    next >
C/C++ Source or Header  |  1991-11-26  |  13KB  |  449 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: returnadd.c,v 4.1.1.3 90/12/11 15:35:56 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.3 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    returnadd.c,v $
  17.  * Revision 4.1.1.3  90/12/11  15:35:56  syd
  18.  * Add back missing  strlen line, fixes segv problem
  19.  * From: Syd
  20.  * 
  21.  * Revision 4.1.1.2  90/12/05  22:33:58  syd
  22.  * Fix missing close brace due to indention error
  23.  * From: Syd
  24.  * 
  25.  * Revision 4.1.1.1  90/12/05  21:59:41  syd
  26.  * Fix where header could be going past end on return due to line
  27.  * combination on header continuation.
  28.  * From: Syd via report from Tom Davis
  29.  * 
  30.  * Revision 4.1  90/04/28  22:43:54  syd
  31.  * checkin of Elm 2.3 as of Release PL0
  32.  * 
  33.  * 
  34.  ******************************************************************************/
  35.  
  36. /** This set of routines is used to generate real return addresses
  37.     and also return addresses suitable for inclusion in a users
  38.     alias files (ie optimized based on the pathalias database).
  39.  
  40. **/
  41.  
  42. #include "headers.h"
  43.  
  44. #include <errno.h>
  45.  
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48.  
  49. char *shift_lower();
  50.  
  51. extern int errno;
  52.  
  53. char *error_name();  /* *strcat(), *strcpy(); */
  54.  
  55. #ifdef OPTIMIZE_RETURN
  56.  
  57. optimize_return(address)
  58. char *address;
  59. {
  60.     /** This routine tries to create an optimized address, that is,
  61.         an address that has the minimal information needed to 
  62.         route a message to this person given the current path
  63.         database...
  64.     **/
  65.  
  66. #ifndef INTERNET
  67.     char    bogus_internet[SLEN];
  68.  
  69.     sprintf(bogus_internet, "@%s", hostfullname);
  70.  
  71.     /** first off, let's see if we need to strip off the localhost
  72.         address crap... **/
  73.  
  74.     /** if we have a uucp part (e.g.a!b) AND the bogus address...**/
  75.  
  76.     if (chloc(address,'!') != -1 && in_string(address, bogus_internet))
  77.       address[strlen(address)-strlen(bogus_internet)] = '\0';
  78. #endif
  79.  
  80.     /** next step is to figure out what sort of address we have... **/
  81.  
  82.     if (chloc(address, '%') != -1)
  83.       optimize_cmplx_arpa(address);
  84.     else if (chloc(address, '@') != -1)
  85.       optimize_arpa(address);
  86.     else
  87.       optimize_usenet(address);
  88. }
  89.  
  90. optimize_cmplx_arpa(address)
  91. char *address;
  92. {
  93.     /** Try to optimize a complex ARPA address.  A Complex address is one 
  94.         that contains '%' (deferred '@').  For example:  
  95.         veeger!hpcnof!hplabs!joe%sytech@syte  
  96.         is a complex address (no kidding, right?).  The algorithm for 
  97.         trying to resolve it is to move all the way to the right, then 
  98.         back up left until the first '!' then from there to the SECOND 
  99.         metacharacter on the right is the name@host address...(in this 
  100.             example, it would be "joe%sytech").  Check this in the routing
  101.         table.  If not present, keep backing out to the right until we
  102.         find a host that is present, or we hit the '@' sign.  Once we
  103.         have a 'normal' ARPA address, hand it to optimize_arpa().
  104.     **/
  105.  
  106.     char name[NLEN], buffer[SLEN], junk[SLEN];
  107.     char host[NLEN], old_host[NLEN];
  108.     register int i, loc, nloc = 0, hloc = 0, passes = 1;
  109.  
  110.     /** first off, get the name%host... **/
  111.  
  112.     for (loc = strlen(address)-1; address[loc] != '!' && loc > -1; loc--)
  113.        ;
  114.  
  115.     while (address[loc] != '\0') {
  116.  
  117.       if (passes == 1) {
  118.         loc++;
  119.  
  120.         while (address[loc] != '%' && address[loc] != '@')
  121.           name[nloc++] = address[loc++];
  122.       }
  123.       else {
  124.         for (i=0; old_host[i] != '\0'; i++)
  125.           name[nloc++] = old_host[i];
  126.       }
  127.  
  128.       loc++;
  129.   
  130.       while (address[loc] != '%' && address[loc] != '@')
  131.         host[hloc++] = address[loc++];
  132.   
  133.       host[hloc] = name[nloc] = '\0';
  134.  
  135.       strcpy(old_host, host);
  136.  
  137.       sprintf(buffer, "%s@%s", name, shift_lower(host));
  138.  
  139.       if (expand_site(buffer, junk) == 0) {
  140.         strcpy(address, buffer);
  141.         return;
  142.       }
  143.       else if (address[loc] == '@') {
  144.         optimize_arpa(address);
  145.         return;
  146.       }
  147.       else
  148.         name[nloc++] = '%';    /* for next pass through */
  149.  
  150.     }
  151. }
  152.  
  153. optimize_arpa(address)
  154. char *address;
  155. {
  156.     /** Get an arpa address and simplify it to the minimal
  157.         route needed to get mail to this person... **/
  158.  
  159.     char name[NLEN], buffer[SLEN], junk[SLEN];
  160.     char host[NLEN];
  161.     register int loc, nloc = 0, hloc = 0, at_sign = 0;
  162.  
  163.     for (loc = strlen(address)-1; address[loc] != '!' && loc > -1; loc--) {
  164.       if (address[loc] == '@')
  165.          at_sign++;    /* remember this spot! */
  166.       else if (at_sign)
  167.         name[nloc++] = address[loc];
  168.       else
  169.         host[hloc++] = address[loc];
  170.     }
  171.  
  172.     name[nloc] = host[hloc] = '\0';
  173.  
  174.     reverse(name);
  175.     reverse(host);
  176.  
  177.     sprintf(buffer,"%s@%s", name, shift_lower(host));
  178.  
  179.     if (expand_site(buffer, junk) == 0) {
  180.       strcpy(address, buffer);
  181.       return;
  182.     }
  183.  
  184.     optimize_usenet(address);    /* that didn't work... */
  185. }
  186.     
  187. optimize_usenet(address)
  188. char *address;
  189. {
  190.     /** optimize the return address IFF it's a standard usenet
  191.         address...
  192.     **/
  193.  
  194.     char name[NLEN],  new_address[SLEN], buffer[SLEN], junk[SLEN];
  195.     register int loc, nloc = 0, aloc = 0, passes = 1;
  196.  
  197.     for (loc = strlen(address)-1; address[loc] != '!' && loc > -1; loc--) 
  198.       name[nloc++] = address[loc];
  199.     name[nloc] = '\0';
  200.  
  201.     reverse(name);
  202.  
  203.     new_address[0] = '\0';    
  204.  
  205.     /* got name, now get machine until we can get outta here */
  206.  
  207.     while (loc > -1) {
  208.  
  209.       new_address[aloc++] = address[loc--];    /* the '!' char */
  210.  
  211.       while (address[loc] != '!' && loc > -1)
  212.         new_address[aloc++] = address[loc--];
  213.  
  214.       new_address[aloc] = '\0';
  215.  
  216.       strcpy(buffer, new_address);
  217.       reverse(buffer);
  218.     
  219.       if (expand_site(buffer, junk) == 0) {
  220.         if (passes == 1 && chloc(name, '@') == -1) {
  221.           buffer[strlen(buffer) - 1] = '\0';    /* remove '!' */
  222.           sprintf(address, "%s@%s", name, buffer);
  223.         }
  224.         else 
  225.           sprintf(address, "%s%s", buffer, name);
  226.         return;        /* success! */
  227.       }
  228.       passes++;
  229.     }
  230.  
  231.     return;        /* nothing to do! */
  232. }
  233.  
  234. #endif    /* OPTIMIZE_RETURN */
  235.  
  236. int
  237. get_return(buffer, msgnum)
  238. char *buffer;
  239. int msgnum;
  240. {
  241.     /** reads msgnum message again, building up the full return 
  242.         address including all machines that might have forwarded 
  243.         the message.  Returns whether it is using the To line **/
  244.  
  245.     char buf[SLEN], name1[SLEN], name2[SLEN], lastname[SLEN];
  246.     char hold_return[SLEN], alt_name2[SLEN], buf2[SLEN];
  247.     int ok = 1, lines, len_buf, len_buf2;
  248.     int using_to = FALSE;
  249.  
  250.     /* now initialize all the char buffers [thanks Keith!] */
  251.  
  252.     buf[0] = name1[0] = name2[0] = lastname[0] = '\0';
  253.     hold_return[0] = alt_name2[0] = buf2[0] = '\0';
  254.  
  255.     /** get to the first line of the message desired **/
  256.  
  257.     if(msgnum < 0 || msgnum >= message_count || message_count < 1) {
  258.       dprint(1, (debugfile,
  259.         "Error: %d not a valid message number message_count = %d (%s)",
  260.         msgnum, message_count, "get_return"));
  261.       error1("%d not a valid message number!");
  262.       return(using_to);
  263.     }
  264.  
  265.     if (fseek(mailfile, headers[msgnum]->offset, 0) == -1) {
  266.       dprint(1, (debugfile,
  267.         "Error: seek %ld bytes into file hit errno %s (%s)", 
  268.         headers[msgnum]->offset, error_name(errno), 
  269.             "get_return"));
  270.       error2("Couldn't seek %d bytes into file (%s).",
  271.            headers[msgnum]->offset, error_name(errno));
  272.       return(using_to);
  273.     }
  274.  
  275.     /** okay!  Now we're there!  **/
  276.  
  277.     lines = headers[msgnum]->lines;
  278.  
  279.     buffer[0] = '\0';
  280.  
  281.     ok = (int) (fgets(buf2, SLEN, mailfile) != NULL);
  282.     if (ok) {
  283.       len_buf2 = strlen(buf2);
  284.       if(buf2[len_buf2-1] == '\n') lines--; /* got a full line */
  285.     }
  286.  
  287.     while (ok && lines) {
  288.       buf[0] = '\0';
  289.       strncat(buf, buf2, SLEN);
  290.       len_buf = strlen(buf);
  291.       ok = (int) (fgets(buf2, SLEN, mailfile) != NULL);
  292.       if (ok) {
  293.         len_buf2 = strlen(buf2);
  294.         if(buf2[len_buf2-1] == '\n') lines--; /* got a full line */
  295.       }
  296.       while (ok && lines && whitespace(buf2[0]) && len_buf >= 2) {
  297.         if (buf[len_buf-1] == '\n') {
  298.           len_buf--;
  299.           buf[len_buf] = '\0';
  300.         }
  301.         strncat(buf, buf2, (SLEN-len_buf-1));
  302.         len_buf = strlen(buf);
  303.         ok = (int) (fgets(buf2, SLEN, mailfile) != NULL);
  304.         if (ok) {
  305.           len_buf2 = strlen(buf2);
  306.           if(buf2[len_buf2-1] == '\n') lines--; /* got a full line */
  307.         }
  308.       }
  309.  
  310. /* At this point, "buf" contains the unfolded header line, while "buf2" contains
  311.    the next single line of text from the mail file */
  312.  
  313.       if (first_word(buf, "From ")) 
  314.         sscanf(buf, "%*s %s", hold_return);
  315.       else if (first_word(buf, ">From")) {
  316.         sscanf(buf,"%*s %s %*s %*s %*s %*s %*s %*s %*s %s %s", 
  317.                name1, name2, alt_name2);
  318.         if (strcmp(name2, "from") == 0)        /* remote from xyz  */
  319.           strcpy(name2, alt_name2);
  320.         else if (strcmp(name2, "by") == 0)    /* forwarded by xyz */
  321.           strcpy(name2, alt_name2);
  322.         add_site(buffer, name2, lastname);
  323.       }
  324.  
  325. #ifdef USE_EMBEDDED_ADDRESSES
  326.  
  327.       else if (first_word(buf, "From:")) {
  328.         get_address_from("From:", buf, hold_return);
  329.         buffer[0] = '\0';
  330.           }
  331.           else if (first_word(buf, "Reply-To:")) {
  332.         get_address_from("Reply-To:", buf, buffer);
  333.         return(using_to);
  334.           }
  335.  
  336. #endif
  337.  
  338.       else if (len_buf < 2)    /* done with header */
  339.             lines = 0; /* let's get outta here!  We're done!!! */
  340.     }
  341.  
  342.     if (buffer[0] == '\0')
  343.       strcpy(buffer, hold_return); /* default address! */
  344.     else
  345.       add_site(buffer, name1, lastname);    /* get the user name too! */
  346.  
  347.     if (first_word(buffer, "To:")) {    /* for backward compatibility */
  348.       get_existing_address(buffer,msgnum);
  349.       using_to = TRUE;
  350.     }
  351.     else {
  352.       /*
  353.        * KLUDGE ALERT - DANGER WILL ROBINSON
  354.        * We can't just leave a bare login name as the return address,
  355.        * or it will be alias-expanded.
  356.        * So we qualify it with the current host name (and, maybe, domain).
  357.        * Sigh.
  358.        */
  359.  
  360.       if (chloc(buffer, '@') < 0
  361.        && chloc(buffer, '%') < 0
  362.        && chloc(buffer, '!') < 0)
  363.       {
  364. #ifdef INTERNET
  365.         sprintf(buffer + strlen(buffer), "@%s", hostfullname);
  366. #else
  367.         strcpy(buf, buffer);
  368.         sprintf(buffer, "%s!%s", hostname, buf);
  369. #endif
  370.       }
  371.  
  372.       /*
  373.        * If we have a space character,
  374.        * or we DON'T have '!' or '@' chars,
  375.        * append the user-readable name.
  376.        */
  377.       if (chloc(headers[msgnum]->from, ' ') >= 0 ||
  378.           (chloc(headers[msgnum]->from, '!') < 0 &&
  379.            chloc(headers[msgnum]->from, '@') < 0)) {
  380.            sprintf(buffer + strlen(buffer),
  381.                " (%s)", headers[msgnum]->from);
  382.           }
  383.     }
  384.  
  385.     return(using_to);
  386. }
  387.  
  388. get_existing_address(buffer, msgnum)
  389. char *buffer;
  390. int msgnum;
  391. {
  392.     /** This routine is called when the message being responded to has
  393.         "To:xyz" as the return address, signifying that this message is
  394.         an automatically saved copy of a message previously sent.  The
  395.         correct to address can be obtained fairly simply by reading the
  396.         To: header from the message itself and (blindly) copying it to
  397.         the given buffer.  Note that this header can be either a normal
  398.         "To:" line (Elm) or "Originally-To:" (previous versions e.g.Msg)
  399.     **/
  400.  
  401.     char mybuf[LONG_STRING];
  402.     register char ok = 1, in_to = 0;
  403.  
  404.     buffer[0] = '\0';
  405.  
  406.     /** first off, let's get to the beginning of the message... **/
  407.  
  408.     if(msgnum < 0 || msgnum >= message_count || message_count < 1) {
  409.       dprint(1, (debugfile,
  410.         "Error: %d not a valid message number message_count = %d (%s)",
  411.         msgnum, message_count, "get_existing_address"));
  412.       error1("%d not a valid message number!");
  413.       return;
  414.     }
  415.         if (fseek(mailfile, headers[msgnum]->offset, 0) == -1) {
  416.         dprint(1, (debugfile, 
  417.             "Error: seek %ld bytes into file hit errno %s (%s)", 
  418.             headers[msgnum]->offset, error_name(errno), 
  419.             "get_existing_address"));
  420.         error2("Couldn't seek %d bytes into the file (%s).",
  421.                headers[msgnum]->offset, error_name(errno));
  422.         return;
  423.         }
  424.  
  425.         /** okay!  Now we're there!  **/
  426.  
  427.         while (ok) {
  428.           ok = (int) (fgets(mybuf, LONG_STRING, mailfile) != NULL);
  429.       no_ret(mybuf);    /* remove return character */
  430.  
  431.           if (first_word(mybuf, "To: ")) {
  432.         in_to = TRUE;
  433.         strcpy(buffer, (char *) mybuf + strlen("To: "));
  434.           }
  435.       else if (first_word(mybuf, "Original-To:")) {
  436.         in_to = TRUE;
  437.         strcpy(buffer, (char *) mybuf + strlen("Original-To:"));
  438.       }
  439.       else if (in_to && whitespace(mybuf[0])) {
  440.         strcat(buffer, " ");        /* tag a space in   */
  441.         strcat(buffer, (char *) mybuf + 1);    /* skip 1 whitespace */
  442.       }
  443.       else if (strlen(mybuf) < 2)
  444.         return;                /* we're done for!  */
  445.       else
  446.         in_to = 0;
  447.       }
  448. }
  449.