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 / reply.c < prev    next >
C/C++ Source or Header  |  1991-11-26  |  16KB  |  543 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: reply.c,v 4.1 90/04/28 22:43:51 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $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:    reply.c,v $
  17.  * Revision 4.1  90/04/28  22:43:51  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  * 
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /*** routine allows replying to the sender of the current message 
  24.  
  25. ***/
  26.  
  27. #include "headers.h"
  28. #include <errno.h>
  29.  
  30. #ifndef BSD
  31. #  include <sys/types.h>
  32. # ifndef VMS
  33. #  include <sys/utsname.h>
  34. # endif
  35. #endif
  36.  
  37. /** Note that this routine generates automatic header information
  38.     for the subject and (obviously) to lines, but that these can
  39.     be altered while in the editor composing the reply message! 
  40. **/
  41.  
  42. char *strip_parens(), *get_token();
  43.  
  44. extern int errno;
  45.  
  46. char *error_name();/* *strcat(), *strcpy(); */
  47.  
  48. int
  49. reply()
  50. {
  51.     /** Reply to the current message.  Returns non-zero iff
  52.         the screen has to be rewritten. **/
  53.  
  54.     char return_address[SLEN], subject[SLEN];
  55.     int  return_value, form_letter;
  56.  
  57.     form_letter = (headers[current-1]->status & FORM_LETTER);
  58.  
  59.     if (get_return(return_address, current-1)) {
  60.       strcpy(subject, headers[current-1]->subject);
  61.       if (form_letter)
  62.         return_value = mail_filled_in_form(return_address, subject);
  63.       else
  64.         return_value = sendmsg(return_address, "", subject, TRUE, NO, TRUE);
  65.     }
  66.     else if (headers[current-1]->subject[0] != '\0') {
  67.       if ((strncmp("Re:", headers[current-1]->subject, 3) == 0) ||
  68.           (strncmp("RE:", headers[current-1]->subject, 3) == 0) ||
  69.           (strncmp("re:", headers[current-1]->subject, 3) == 0)) 
  70.         strcpy(subject, headers[current-1]->subject);
  71.       else {
  72.         strcpy(subject,"Re: ");
  73.         strcat(subject,headers[current-1]->subject); 
  74.       }
  75.       if (form_letter)
  76.         return_value = mail_filled_in_form(return_address, subject);
  77.       else
  78.         return_value = sendmsg(return_address, "", subject, TRUE, NO, TRUE);
  79.     }
  80.     else
  81.       if (form_letter)
  82.         return_value = mail_filled_in_form(return_address, 
  83.                         "Filled in Form");
  84.       else
  85.         return_value = sendmsg(return_address, "", "Re: your mail", 
  86.                 TRUE, NO, TRUE);
  87.  
  88.     return(return_value);
  89. }
  90.  
  91. int
  92. reply_to_everyone()
  93. {
  94.     /** Reply to everyone who received the current message.  
  95.         This includes other people in the 'To:' line and people
  96.         in the 'Cc:' line too.  Returns non-zero iff the screen 
  97.             has to be rewritten. **/
  98.  
  99.     char return_address[SLEN], subject[SLEN];
  100.     char full_address[VERY_LONG_STRING];
  101.     int  return_value;
  102.  
  103.     get_return(return_address, current-1);
  104.  
  105.     full_address[0] = '\0';            /* no copies yet    */
  106.     get_and_expand_everyone(return_address, full_address);
  107.  
  108.     if (headers[current-1]->subject[0] != '\0') {
  109.       if ((strncmp("Re:", headers[current-1]->subject, 3) == 0) ||
  110.           (strncmp("RE:", headers[current-1]->subject, 3) == 0) ||
  111.           (strncmp("re:", headers[current-1]->subject, 3) == 0)) 
  112.         strcpy(subject, headers[current-1]->subject);
  113.       else {
  114.         strcpy(subject,"Re: ");
  115.         strcat(subject,headers[current-1]->subject); 
  116.       }
  117.       return_value = sendmsg(return_address, full_address, subject, 
  118.                  TRUE, NO, TRUE);
  119.     }
  120.     else
  121.       return_value = sendmsg(return_address, full_address, 
  122.                   "Re: your mail", TRUE, NO, TRUE);
  123.  
  124.     return(return_value);
  125.  
  126. }
  127.  
  128. int
  129. forward()
  130. {
  131.     /** Forward the current message.  What this actually does is
  132.         to temporarily set forwarding to true, then call 'send' to
  133.         get the address and route the mail.   Modified to also set
  134.         'noheader' to FALSE also, so that the original headers
  135.         of the message sent are included in the message body also.
  136.         Return TRUE if the main part of the screen has been changed
  137.         (useful for knowing whether a redraw is needed.
  138.     **/
  139.  
  140.     char subject[SLEN], address[VERY_LONG_STRING];
  141.     int  results, edit_msg = FALSE;
  142.  
  143.     forwarding = TRUE;
  144.  
  145.     address[0] = '\0';
  146.  
  147.     if (headers[current-1]->status & FORM_LETTER)
  148.       PutLine0(LINES-3,COLUMNS-40,"<No editing allowed.>");
  149.     else {
  150.       edit_msg = (want_to("Edit outgoing message? (y/n) ",'y') != 'n');
  151.     }
  152.  
  153.     if (strlen(headers[current-1]->subject) > 0) {
  154.  
  155.       strcpy(subject, headers[current-1]->subject); 
  156.  
  157.       /* this next strange compare is to see if the last few chars are
  158.          already '(fwd)' before we tack another on */
  159.  
  160.       if (strlen(subject) < 6 || (strcmp((char *) subject+strlen(subject)-5,
  161.                          "(fwd)") != 0))
  162.         strcat(subject, " (fwd)");
  163.  
  164.       results = sendmsg(address, "", subject, edit_msg,
  165.         headers[current-1]->status & FORM_LETTER? 
  166.         PREFORMATTED : allow_forms, FALSE);
  167.     }
  168.     else
  169.       results = sendmsg(address, "", "Forwarded mail...", edit_msg, 
  170.         headers[current-1]->status & FORM_LETTER? 
  171.         PREFORMATTED : allow_forms, FALSE);
  172.     
  173.     forwarding = FALSE;
  174.  
  175.     return(results);
  176. }
  177.  
  178. get_and_expand_everyone(return_address, full_address)
  179. char *return_address, *full_address;
  180. {
  181.     /** Read the current message, extracting addresses from the 'To:'
  182.         and 'Cc:' lines.   As each address is taken, ensure that it
  183.         isn't to the author of the message NOR to us.  If neither,
  184.         prepend with current return address and append to the 
  185.         'full_address' string.
  186.     **/
  187.  
  188.     char ret_address[SLEN], buf[SLEN], new_address[SLEN],
  189.      address[SLEN], comment[SLEN];
  190.     int  in_message = 1, first_pass = 0, iindex, line_pending = 0;
  191.  
  192.     /** First off, get to the first line of the message desired **/
  193.  
  194.     if (fseek(mailfile, headers[current-1]->offset, 0) == -1) {
  195.     dprint(1,(debugfile,"Error: seek %ld resulted in errno %s (%s)\n", 
  196.          headers[current-1]->offset, error_name(errno), 
  197.          "get_and_expand_everyone"));
  198.     error2("ELM [seek] couldn't read %d bytes into file (%s).",
  199.            headers[current-1]->offset, error_name(errno));
  200.     return;
  201.     }
  202.  
  203.     /** okay!  Now we're there!  **/
  204.  
  205.     /** let's fix the ret_address to reflect the return address of this
  206.     message with '%s' instead of the persons login name... **/
  207.  
  208.     translate_return(return_address, ret_address);
  209.  
  210.     /** now let's parse the actual message! **/
  211.  
  212.     while (in_message) {
  213.       if (! line_pending)
  214.         in_message = (int) (fgets(buf, SLEN, mailfile) != NULL);
  215.       line_pending = 0;
  216.       if (first_word(buf, "From ") && first_pass++ != 0) 
  217.     in_message = FALSE;
  218.       else if (first_word(buf, "To:") || first_word(buf, "Cc:") ||
  219.            first_word(buf, "CC:") || first_word(buf, "cc:")) {
  220.     do {
  221.       no_ret(buf);
  222.  
  223.       /** we have a buffer with a list of addresses, each of either the
  224.           form "address (name)" or "name <address>".  Our mission, should
  225.           we decide not to be too lazy, is to break it into the two parts.
  226.       **/
  227.           
  228.       if (!whitespace(buf[0]))
  229.         iindex = chloc(buf, ':')+1;        /* skip header field */
  230.       else
  231.         iindex = 0;                /* skip whitespace   */
  232.  
  233.       while (break_down_tolist(buf, &iindex, address, comment)) {
  234.  
  235.         if (okay_address(address, return_address)) {
  236.           sprintf(new_address, ret_address, address);
  237.           optimize_and_add(new_address, full_address);
  238.         }
  239.       }
  240.  
  241.           in_message = (int) (fgets(buf, SLEN, mailfile) != NULL);
  242.  
  243.       if (in_message) dprint(2, (debugfile, "> %s", buf));
  244.     
  245.     } while (in_message && whitespace(buf[0]));
  246.     line_pending++;
  247.       }
  248.       else if (strlen(buf) < 2)    /* done with header */
  249.      in_message = FALSE;
  250.     }
  251. }
  252.  
  253. int
  254. okay_address(address, return_address)
  255. char *address, *return_address;
  256. {
  257.     /** This routine checks to ensure that the address we just got
  258.         from the "To:" or "Cc:" line isn't us AND isn't the person    
  259.         who sent the message.  Returns true iff neither is the case **/
  260.  
  261.     char our_address[SLEN];
  262.     struct addr_rec  *alternatives;
  263.  
  264.     if (in_list(address, return_address))
  265.       return(FALSE);
  266.  
  267.     if(in_list(address, username))
  268.       return(FALSE);
  269.  
  270.     sprintf(our_address, "%s!%s", hostname, username);
  271.     if (in_list(address, our_address))
  272.       return(FALSE);
  273.  
  274.     sprintf(our_address, "%s!%s", hostfullname, username);
  275.     if (in_list(address, our_address))
  276.       return(FALSE);
  277.  
  278.     sprintf(our_address, "%s@%s", username, hostname);
  279.     if (in_list(address, our_address))
  280.       return(FALSE);
  281.  
  282.     sprintf(our_address, "%s@%s", username, hostfullname);
  283.     if (in_list(address, our_address))
  284.       return(FALSE);
  285.  
  286.     alternatives = alternative_addresses;
  287.  
  288.     while (alternatives != NULL) {
  289.       if (in_list(address, alternatives->address))
  290.         return(FALSE);
  291.       alternatives = alternatives->next;
  292.     }
  293.  
  294.     return(TRUE);
  295. }
  296.         
  297. optimize_and_add(new_address, full_address)
  298. char *new_address, *full_address;
  299. {
  300.     /** This routine will add the new address to the list of addresses
  301.         in the full address buffer IFF it doesn't already occur.  It
  302.         will also try to fix dumb hops if possible, specifically hops
  303.         of the form ...a!b...!a... and hops of the form a@b@b etc 
  304.     **/
  305.  
  306.     register int len, host_count = 0, i;
  307.     char     hosts[MAX_HOPS][SLEN];    /* array of machine names */
  308.     char     *host, *addrptr;
  309.  
  310.     if (in_list(full_address, new_address))
  311.       return(1);    /* duplicate address */
  312.  
  313.     /** optimize **/
  314.     /*  break down into a list of machine names, checking as we go along */
  315.     
  316.     addrptr = (char *) new_address;
  317.  
  318.     while ((host = get_token(addrptr, "!", 1)) != NULL) {
  319.       for (i = 0; i < host_count && ! equal(hosts[i], host); i++)
  320.           ;
  321.  
  322.       if (i == host_count) {
  323.         strcpy(hosts[host_count++], host);
  324.         if (host_count == MAX_HOPS) {
  325.            dprint(2, (debugfile,
  326.               "Error: hit max_hops limit trying to build return address (%s)\n",
  327.               "optimize_and_add"));
  328.            error("Can't build return address. Hit MAX_HOPS limit!");
  329.            return(1);
  330.         }
  331.       }
  332.       else 
  333.         host_count = i + 1;
  334.       addrptr = NULL;
  335.     }
  336.  
  337.     /** fix the ARPA addresses, if needed **/
  338.     
  339.     if (chloc(hosts[host_count-1], '@') > -1)
  340.       fix_arpa_address(hosts[host_count-1]);
  341.       
  342.     /** rebuild the address.. **/
  343.  
  344.     new_address[0] = '\0';
  345.  
  346.     for (i = 0; i < host_count; i++)
  347.       sprintf(new_address, "%s%s%s", new_address, 
  348.               new_address[0] == '\0'? "" : "!",
  349.               hosts[i]);
  350.  
  351.     if (full_address[0] == '\0')
  352.       strcpy(full_address, new_address);
  353.     else {
  354.       len = strlen(full_address);
  355.       full_address[len  ] = ',';
  356.       full_address[len+1] = ' ';
  357.       full_address[len+2] = '\0';
  358.       strcat(full_address, new_address);
  359.     }
  360.  
  361.     return(0);
  362. }
  363.  
  364. get_return_name(address, name, trans_to_lowercase)
  365. char *address, *name;
  366. int   trans_to_lowercase;
  367. {
  368.     /** Given the address (either a single address or a combined list 
  369.         of addresses) extract the login name of the first person on
  370.         the list and return it as 'name'.  Modified to stop at
  371.         any non-alphanumeric character. **/
  372.  
  373.     /** An important note to remember is that it isn't vital that this
  374.         always returns just the login name, but rather that it always
  375.         returns the SAME name.  If the persons' login happens to be,
  376.         for example, joe.richards, then it's arguable if the name 
  377.         should be joe, or the full login.  It's really immaterial, as
  378.         indicated before, so long as we ALWAYS return the same name! **/
  379.  
  380.     /** Another note: modified to return the argument as all lowercase
  381.         always, unless trans_to_lowercase is FALSE... **/
  382.  
  383.     char single_address[SLEN];
  384.     register int i, loc, iindex = 0;
  385.  
  386.     dprint(6, (debugfile,"get_return_name called with (%s, <>, shift=%s)\n",
  387.            address, onoff(trans_to_lowercase)));
  388.  
  389.     /* First step - copy address up to a comma, space, or EOLN */
  390.  
  391.     for (i=0; address[i] != ',' && ! whitespace(address[i]) && 
  392.          address[i] != '\0'; i++)
  393.       single_address[i] = address[i];
  394.     single_address[i] = '\0';
  395.  
  396.     /* Now is it an ARPA address?? */
  397.  
  398.     if ((loc = chloc(single_address, '@')) != -1) {      /* Yes */
  399.  
  400.       /* At this point the algorithm is to keep shifting our copy 
  401.          window left until we hit a '!'.  The login name is then
  402.              located between the '!' and the first metacharacter to 
  403.          it's right (ie '%', ':' or '@'). */
  404.  
  405.       for (i=loc; single_address[i] != '!' && i > -1; i--)
  406.           if (single_address[i] == '%' || 
  407.               single_address[i] == ':' ||
  408.           single_address[i] == '@') loc = i-1;
  409.     
  410.       if (i < 0 || single_address[i] == '!') i++;
  411.  
  412.       for (iindex = 0; iindex < loc - i + 1; iindex++)
  413.         if (trans_to_lowercase)
  414.           name[iindex] = tolower(single_address[iindex+i]);
  415.         else
  416.           name[iindex] = single_address[iindex+i];
  417.       name[iindex] = '\0';
  418.     }
  419.     else {    /* easier - standard USENET address */
  420.  
  421.       /* This really is easier - we just cruise left from the end of
  422.          the string until we hit either a '!' or the beginning of the
  423.              line.  No sweat. */
  424.  
  425.       loc = strlen(single_address)-1;     /* last char */
  426.  
  427.       for (i = loc; single_address[i] != '!' && single_address[i] != '.' 
  428.            && i > -1; i--)
  429.          if (trans_to_lowercase)
  430.            name[iindex++] = tolower(single_address[i]);
  431.          else
  432.            name[iindex++] = single_address[i];
  433.       name[iindex] = '\0';
  434.       reverse(name);
  435.     }
  436. }
  437.  
  438. int
  439. break_down_tolist(buf, iindex, address, comment)
  440. char *buf, *address, *comment;
  441. int  *iindex;
  442. {
  443.     /** This routine steps through "buf" and extracts a single address
  444.         entry.  This entry can be of any of the following forms;
  445.  
  446.         address (name)
  447.         name <address>
  448.         address
  449.     
  450.         Once it's extracted a single entry, it will then return it as
  451.         two tokens, with 'name' (e.g. comment) surrounded by parens.
  452.         Returns ZERO if done with the string...
  453.     **/
  454.  
  455.     char buffer[LONG_STRING];
  456.     register int i, loc = 0, hold_index, len;
  457.  
  458.     if (*iindex > strlen(buf)) return(FALSE);
  459.  
  460.     while (whitespace(buf[*iindex])) (*iindex)++;
  461.  
  462.     if (*iindex > strlen(buf)) return(FALSE);
  463.  
  464.     /** Now we're pointing at the first character of the token! **/
  465.  
  466.     hold_index = *iindex;
  467.  
  468.     while (buf[*iindex] != ',' && buf[*iindex] != '\0')
  469.       buffer[loc++] = buf[(*iindex)++];
  470.  
  471.     (*iindex)++;
  472.     buffer[loc] = '\0';
  473.  
  474.     while (whitespace(buffer[loc]))     /* remove trailing whitespace */
  475.       buffer[--loc] = '\0';
  476.  
  477.     if (strlen(buffer) == 0) return(FALSE);
  478.  
  479.     dprint(5, (debugfile, "\n* got \"%s\"\n", buffer));
  480.  
  481.     if (buffer[loc-1] == ')') {    /*   address (name)  format */
  482.       for (loc = 0, len = strlen(buffer);buffer[loc] != '(' && loc < len; loc++)
  483.         /* get to the opening comment character... */ ;
  484.  
  485.       loc--;    /* back up to just before the paren */
  486.       while (whitespace(buffer[loc])) loc--;    /* back up */
  487.  
  488.       /** get the address field... **/
  489.  
  490.       for (i=0; i <= loc; i++)
  491.         address[i] = buffer[i];
  492.       address[i] = '\0';
  493.  
  494.       /** now get the comment field, en toto! **/
  495.  
  496.       loc = 0;
  497.  
  498.       for (i = chloc(buffer, '('), len = strlen(buffer); i < len; i++)
  499.         comment[loc++] = buffer[i];
  500.       comment[loc] = '\0';
  501.     }
  502.     else if (buffer[loc-1] == '>') {    /*   name <address>  format */
  503.       dprint(7, (debugfile, "\tcomment <address>\n"));
  504.       for (loc = 0, len = strlen(buffer);buffer[loc] != '<' && loc < len; loc++)
  505.         /* get to the opening comment character... */ ;
  506.       while (whitespace(buffer[loc])) loc--;    /* back up */
  507.  
  508.       /** get the comment field... **/
  509.  
  510.       comment[0] = '(';
  511.       for (i=1; i < loc; i++)
  512.         comment[i] = buffer[i-1];
  513.       comment[i++] = ')';
  514.       comment[i] = '\0';
  515.  
  516.       /** now get the address field, en toto! **/
  517.  
  518.       loc = 0;
  519.  
  520.       for (i = chloc(buffer,'<') + 1, len = strlen(buffer); i < len - 1; i++)
  521.         address[loc++] = buffer[i];
  522.     
  523.       address[loc] = '\0';
  524.     }
  525.     else {
  526.       /** the next section is added so that all To: lines have commas
  527.           in them accordingly **/
  528.  
  529.       for (i=0; buffer[i] != '\0'; i++)
  530.         if (whitespace(buffer[i])) break;
  531.       if (i < strlen(buffer)) {    /* shouldn't be whitespace */
  532.         buffer[i] = '\0';
  533.         *iindex = hold_index + strlen(buffer) + 1;
  534.       }
  535.       strcpy(address, buffer);
  536.       comment[0] = '\0';
  537.     }
  538.  
  539.     dprint(5, (debugfile, "-- returning '%s' '%s'\n", address, comment));
  540.  
  541.     return(TRUE);
  542. }
  543.