home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / mailmsg2.c < prev    next >
C/C++ Source or Header  |  1992-10-04  |  31KB  |  1,016 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: mailmsg2.c,v 4.1.1.8 90/10/07 19:48:10 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.8 $   $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:    mailmsg2.c,v $
  17.  * Revision 4.1.1.8  90/10/07  19:48:10  syd
  18.  * fix the bounce problem reported earlier when using MMDF submit as the MTA.
  19.  * From: Jim Clausing <jac%brahms.tinton.ccur.com@RELAY.CS.NET>
  20.  *
  21.  * Revision 4.1.1.7  90/08/15  22:02:36  syd
  22.  * deal with several of the problems that have come up trying to use the MMDF
  23.  * submit program directly rather than going through the sendmail stub
  24.  * included with MMDF.  This should take care of the problem of not being
  25.  * able to send mail to usernames beginning with "i" and with the
  26.  * 'No valid author specified' problem.
  27.  * From: jac%brahms.tinton.ccur.com@RELAY.CS.NET
  28.  *
  29.  * Revision 4.1.1.6  90/07/12  23:19:20  syd
  30.  * Make domain name checking case independent
  31.  * From: Syd, reported by Steven Baur
  32.  *
  33.  * Revision 4.1.1.5  90/07/12  22:52:42  syd
  34.  * When Elm is compiled with the NO_XHEADER symbol defined, it failed
  35.  * to put a blank line between the message header and message body.
  36.  * From: mca@medicus.medicus.com (Mark Adams)
  37.  *
  38.  * Revision 4.1.1.4  90/06/26  16:18:24  syd
  39.  * Make it encode lines that are [...] if not one of the reserved lines.
  40.  * It was messing up decoding
  41.  * From: Syd via report from Lenny Tropiano
  42.  *
  43.  * Revision 4.1.1.3  90/06/21  21:07:48  syd
  44.  * Fix XHEAD define
  45.  * From: Syd
  46.  *
  47.  * Revision 4.1.1.2  90/06/09  23:20:24  syd
  48.  * fix typo
  49.  *
  50.  * Revision 4.1.1.1  90/06/09  22:28:39  syd
  51.  * Allow use of submit with mmdf instead of sendmail stub
  52.  * From: martin <martin@hppcmart.grenoble.hp.com>
  53.  *
  54.  * Revision 4.1  90/04/28  22:43:28  syd
  55.  * checkin of Elm 2.3 as of Release PL0
  56.  *
  57.  *
  58.  ******************************************************************************/
  59.  
  60. /** Interface to allow mail to be sent to users.  Part of ELM  **/
  61.  
  62. #include "headers.h"
  63. #include <errno.h>
  64. #include <ctype.h>
  65.  
  66. #ifdef BSD
  67. #undef tolower
  68. #undef toupper
  69. #endif
  70.  
  71. #ifndef OS2
  72. extern int errno;
  73. #endif
  74. extern char version_buff[];
  75.  
  76. char *error_name(), *error_description(), *strip_parens();
  77. char *strcat(), *strcpy(), *index();
  78. char *format_long(), *strip_commas(), *tail_of_string();
  79.  
  80. unsigned long sleep();
  81.  
  82. #ifdef SITE_HIDING
  83.  char *get_ctime_date();
  84. #endif
  85. FILE *write_header_info();
  86.  
  87. /* these are all defined in the mailmsg1.c file! */
  88.  
  89. extern char subject[SLEN], in_reply_to[SLEN], expires[SLEN],
  90.             action[SLEN], priority[SLEN], reply_to[SLEN], to[VERY_LONG_STRING],
  91.         cc[VERY_LONG_STRING], expanded_to[VERY_LONG_STRING],
  92.         expanded_cc[VERY_LONG_STRING], user_defined_header[SLEN],
  93.         bcc[VERY_LONG_STRING], expanded_bcc[VERY_LONG_STRING];
  94.  
  95.  
  96. int  gotten_key;
  97. char *bounce_off_remote();
  98.  
  99. mail(copy_msg, edit_message, form)
  100. int  copy_msg, edit_message, form;
  101. {
  102.     /** Given the addresses and various other miscellany (specifically,
  103.         'copy-msg' indicates whether a copy of the current message should
  104.         be included, 'edit_message' indicates whether the message should
  105.         be edited) this routine will invoke an editor for the user and
  106.         then actually mail off the message. 'form' can be YES, NO, or
  107.         MAYBE.  YES=add "Content-Type: mailform" header, MAYBE=add the
  108.         M)ake form option to last question, and NO=don't worry about it!
  109.         Also, if 'copy_msg' = FORM, then grab the form temp file and use
  110.         that...
  111.         Return TRUE if the main part of the screen has been changed
  112.         (useful for knowing whether a redraw is needed.
  113.     **/
  114.  
  115.     FILE *reply, *real_reply; /* second is post-input buffer */
  116.     char *whole_msg_file = NULL, *tempnam();
  117.     char filename[SLEN], fname[SLEN], copy_file[SLEN],
  118.              very_long_buffer[VERY_LONG_STRING], mailerflags[NLEN];
  119.     int ch, sys_status;
  120.     register int retransmit = FALSE;
  121.     int      already_has_text = FALSE;        /* we need an ADDRESS */
  122.     int     signature_done = FALSE;
  123.     int     need_redraw = 0;
  124.  
  125.     static int cancelled_msg = 0;
  126.  
  127.     dprint(4, (debugfile, "\nMailing to \"%s\" (with%s editing)\n",
  128.           expanded_to, edit_message? "" : "out"));
  129.  
  130.     gotten_key = 0;        /* ignore previously gotten encryption key */
  131.  
  132.     /** first generate the temporary filename **/
  133.  
  134.     sprintf(filename,"%s%d%s", temp_dir, getpid(), temp_file);
  135.  
  136.     /** if possible, let's try to recall the last message? **/
  137.  
  138.     if (! batch_only && copy_msg != FORM && user_level != 0)
  139.       retransmit = recall_last_msg(filename, copy_msg, &cancelled_msg,
  140.                &already_has_text);
  141.  
  142.     /** if we're not retransmitting, create the file.. **/
  143.  
  144.     if (! retransmit)
  145.       if ((reply = fopen(filename,"w")) == NULL) {
  146.         dprint(1, (debugfile,
  147.                "Attempt to write to temp file %s failed with error %s (mail)\n",
  148.          filename, error_name(errno)));
  149.         if(batch_only)
  150.           printf("Could not create file %s (%s).\n",filename,
  151.          error_name(errno));
  152.         else
  153.           error2("Could not create file %s (%s).",filename,
  154.          error_name(errno));
  155.         return(need_redraw);
  156.       }
  157.  
  158.     chown (filename, userid, groupid);
  159.  
  160.     /* copy the message from standard input */
  161.     if (batch_only) {
  162.       while (fgets(very_long_buffer, VERY_LONG_STRING, stdin) != NULL)
  163.         fprintf(reply, "%s", very_long_buffer);
  164.     }
  165.  
  166.     if (copy_msg == FORM) {
  167.       sprintf(fname, "%s%d%s", temp_dir, getpid(), temp_form_file);
  168.       fclose(reply);    /* we can't retransmit a form! */
  169.       if (access(fname,ACCESS_EXISTS) != 0) {
  170.         if(batch_only)
  171.           printf("Couldn't find forms file!\n");
  172.         else
  173.           error("Couldn't find forms file!");
  174.         return(need_redraw);
  175.       }
  176.       dprint(4, (debugfile, "-- renaming existing file %s to file %s --\n",
  177.           fname, filename));
  178.       rename(fname, filename);
  179.     }
  180.     else if (copy_msg && ! retransmit) {  /* if retransmit we have it! */
  181.       if (forwarding) {
  182.         fputs("Forwarded message:\n", reply);
  183.       }
  184.       else if (attribution[0]) {
  185.         fprintf(reply, attribution, headers[current-1]->from);
  186.         fputc('\n', reply);
  187.       }
  188.       if (edit_message) {
  189.         copy_message(prefixchars, reply, noheader,FALSE,FALSE,TRUE,FALSE);
  190.         already_has_text = TRUE;    /* we just added it, right? */
  191.       }
  192.       else
  193.         copy_message("", reply, noheader, FALSE, FALSE, TRUE, FALSE);
  194.     }
  195.  
  196.         /* append signature now if we are going to use an external editor */
  197.     /* Don't worry about the remote/local determination too much */
  198.  
  199.         if (already_has_text ||
  200.            (strcmp(editor,"builtin") != 0 && strcmp(editor,"none") != 0)) {
  201.          signature_done = TRUE;
  202.              if (!(forwarding && !edit_message) &&
  203.                  !retransmit && copy_msg != FORM)
  204.            already_has_text |= append_sig(reply);
  205.     }
  206.  
  207.     if (! retransmit && copy_msg != FORM)
  208.       if (reply != NULL)
  209.         (void) fclose(reply);    /* on replies, it won't be open! */
  210.  
  211.     /** Edit the message **/
  212.  
  213.     if (edit_message)
  214.       create_readmsg_file();    /* for "readmsg" routine */
  215.  
  216.     ch = edit_message? 'e' : ' ';    /* drop through if needed... */
  217.  
  218.     /* calculate default save_file name */
  219.     if(auto_cc) {
  220.       if(save_by_name) {
  221.         if(force_name) {
  222.           strcpy(copy_file, "=");    /* signals save by 'to' logname */
  223.         } else {
  224.           strcpy(copy_file, "=?");    /* conditional save by 'to' logname */
  225.         }
  226.       } else {
  227.         strcpy(copy_file, "<");    /* signals save to sentmail */
  228.       }
  229.     } else *copy_file = '\0';    /* signals to not save a copy */
  230.  
  231.  
  232.     if (! batch_only) {
  233.       do {
  234.         switch (ch) {
  235.           case 'e': need_redraw = 1;
  236.             if (edit_the_message(filename, already_has_text)) {
  237.               cancelled_msg = TRUE;
  238.               return(need_redraw);
  239.             }
  240.             break;
  241.  
  242.           case 'c': if (name_copy_file(copy_file))
  243.               need_redraw = 1;
  244.             break;
  245.  
  246.           case 'h': edit_headers();
  247.             need_redraw = 1;
  248.             break;
  249.  
  250. #ifdef ALLOW_SUBSHELL
  251.           case '!': if (subshell()) {
  252.               ClearScreen();
  253.               need_redraw = 1;
  254.             }
  255.             break;
  256. #endif
  257.  
  258.           default : /* do nothing */ ;
  259.         }
  260.  
  261.         /** ask that silly question again... **/
  262.  
  263.         if ((ch = verify_transmission(filename, &form)) == 'f') {
  264.           cancelled_msg = TRUE;
  265.           return(need_redraw);
  266.         }
  267.  
  268.       } while (ch != 's');
  269.  
  270.       if (form == YES)
  271.         if (format_form(filename) < 1) {
  272.           cancelled_msg = TRUE;
  273.           return(need_redraw);
  274.         }
  275.  
  276.       if ((reply = fopen(filename,"r")) == NULL) {
  277.           dprint(1, (debugfile,
  278.         "Attempt to open file %s for reading failed with error %s (mail)\n",
  279.                 filename, error_name(errno)));
  280.           error1("Could not open reply file (%s).", error_name(errno));
  281.           return(need_redraw);
  282.       }
  283.     }
  284.     else if ((reply = fopen(filename,"r")) == NULL) {
  285.       dprint(1, (debugfile,
  286.         "Attempt to open file %s for reading failed with error %s (mail)\n",
  287.              filename, error_name(errno)));
  288.       printf("Could not open reply file (%s).\n", error_name(errno));
  289.       return(need_redraw);
  290.     }
  291.  
  292.     cancelled_msg = FALSE;    /* it ain't cancelled, is it? */
  293.  
  294.     /** ask about bounceback if the user wants us to.... **/
  295.  
  296.     if (uucp_hops(to) > bounceback && bounceback > 0 && copy_msg != FORM)
  297.       if (verify_bounceback() == TRUE) {
  298.         if (strlen(cc) > 0) strcat(expanded_cc, ", ");
  299.         strcat(expanded_cc, bounce_off_remote(to));
  300.       }
  301.  
  302.     /** grab a copy if the user so desires... **/
  303.  
  304.     if (*copy_file && !no_save) /* i.e. if copy_file contains a name */
  305.       save_copy(expanded_to, expanded_cc, expanded_bcc,
  306.            filename, copy_file, form);
  307.  
  308.     /** write all header information into whole_msg_file **/
  309.  
  310.     if((whole_msg_file=tempnam(temp_dir, "snd")) == NULL) {
  311.       dprint(1, (debugfile, "couldn't make temp file nam! (mail)\n"));
  312.       if(batch_only)
  313.         printf("Sorry - couldn't make temp file name!\n");
  314.       else if(mail_only)
  315.         error("Sorry - couldn't make temp file name.");
  316.       else
  317.         set_error("Sorry - couldn't make temp file name.");
  318.       return(need_redraw);
  319.     }
  320.  
  321.     /** try to write headers to new temp file **/
  322.  
  323.     dprint(6, (debugfile, "Composition file='%s' and mail buffer='%s'\n",
  324.             filename, whole_msg_file));
  325.  
  326.     dprint(2,(debugfile,"--\nTo: %s\nCc: %s\nBcc: %s\nSubject: %s\n---\n",
  327.           expanded_to, expanded_cc, expanded_bcc, subject));
  328.  
  329.     if ((real_reply=
  330.        write_header_info(whole_msg_file, expanded_to,
  331.          expanded_cc, expanded_bcc, form == YES, FALSE)) == NULL) {
  332.  
  333.       /** IT FAILED!!  MEIN GOTT!  Use a dumb mailer instead! **/
  334.  
  335.       dprint(3, (debugfile, "** write_header failed: %s\n",
  336.          error_name(errno)));
  337.  
  338.       if (cc[0] != '\0')          /* copies! */
  339.         sprintf(expanded_to,"%s %s", expanded_to, expanded_cc);
  340.  
  341.       quote_args(very_long_buffer, strip_parens(strip_commas(expanded_to)));
  342.       strcpy(expanded_to, very_long_buffer);
  343.  
  344.       sprintf(very_long_buffer, "%s -f %s -s \"%s\" %s",
  345.           mailx, filename, subject, expanded_to);
  346.  
  347.       if(batch_only)
  348.         printf("Message sent using dumb mailer %s.\n", mailx);
  349.       else
  350.         error1("Message sent using dumb mailer %s.", mailx);
  351.       sleep(2);    /* ensure time to see this prompt! */
  352.  
  353.     }
  354.     else {
  355.  
  356.       copy_message_across(reply, real_reply, FALSE);
  357.  
  358.           /* Append signature if not done earlier */
  359.  
  360.           if (!signature_done && !retransmit && copy_msg != FORM)
  361.                append_sig(real_reply);
  362.  
  363.       fclose(real_reply);
  364.  
  365.       if (cc[0] != '\0')                           /* copies! */
  366.         sprintf(expanded_to,"%s %s", expanded_to, expanded_cc);
  367.  
  368.       if (bcc[0] != '\0') {
  369.         strcat(expanded_to, " ");
  370.         strcat(expanded_to, expanded_bcc);
  371.       }
  372.  
  373.       remove_hostbang(expanded_to);
  374.  
  375.       if (strcmp(sendmail, mailer) == 0
  376. #ifdef SITE_HIDING
  377.           && ! is_a_hidden_user(username))
  378. #else
  379.                              )
  380. #endif
  381.  
  382.         strcpy(mailerflags, (sendmail_verbose ? smflagsv : smflags));
  383.       else if (strcmp(submitmail, mailer) == 0)
  384.         strcpy(mailerflags, submitflags);
  385.       else
  386.         mailerflags[0] ='\0';
  387.  
  388.       if (strcmp(submitmail, mailer) == 0)
  389.         strcpy(expanded_to, " ");
  390.       else {
  391.         quote_args(very_long_buffer, strip_parens(strip_commas(expanded_to)));
  392.         strcpy(expanded_to, very_long_buffer);
  393.       }
  394.  
  395.       sprintf(very_long_buffer,"%s -f %s %s %s 2>nul",
  396.           mailer, whole_msg_file, mailerflags, expanded_to);
  397.     }
  398.  
  399.     fclose(reply);
  400.  
  401.     if(batch_only)
  402.       printf("Sending mail...\r\n");
  403.     else {
  404.       PutLine0(LINES,0,"Sending mail...");
  405.       CleartoEOLN();
  406.     }
  407.  
  408.     /* Take note of mailer return value */
  409.  
  410.     if ( sys_status = system_call(very_long_buffer, SH, FALSE, FALSE) ) {
  411.         /* problem case: */
  412.         if (mail_only || batch_only)
  413.            printf("\r\nmailer returned error status %d\r\n", sys_status);
  414.         else {
  415.            sprintf(very_long_buffer, "mailer returned error status %d", sys_status);
  416.            set_error(very_long_buffer);
  417.         }
  418.     } else {
  419.         /* Success case: */
  420.         if(batch_only)
  421.           printf("Mail sent!\r\n");
  422.         else if(mail_only)
  423.           error("Mail sent!");
  424.         else
  425.           set_error("Mail sent!");
  426.     }
  427.  
  428.     /* Unlink temp file now.
  429.      * This is a precaution in case the message was encrypted.
  430.      * I.e. even though this file is readable by the owner only,
  431.      * encryption is supposed to hide things even from someone
  432.      * with root privelges. The best we can do is not let this
  433.      * file just hang after we're finished with it.
  434.      */
  435.     (void)unlink(filename);
  436.     (void)unlink(whole_msg_file);
  437.         free(whole_msg_file);
  438.     return(need_redraw);
  439. }
  440.  
  441. /*
  442.  * remove_hostbang - Given an expanded list of addresses, remove all
  443.  * occurrences of "thishost!" at the beginning of addresses.
  444.  * This hack is useful in itself, but it is required now because of the
  445.  * kludge disallowing alias expansion on return addresses.
  446.  */
  447.  
  448. remove_hostbang(addrs)
  449. char *addrs;
  450. {
  451.     int i, j, hlen, flen;
  452.  
  453.     if ((hlen = strlen(hostname)) < 1)
  454.       return;
  455.  
  456.     flen = strlen(hostfullname);
  457.     i = j = 0;
  458.  
  459.     while (addrs[i]) {
  460.       if (i == 0 || isspace(addrs[i - 1])) {
  461.         if (strncmp(&addrs[i], hostname, hlen) == 0 &&
  462.           addrs[i + hlen] == '!') {
  463.             i += hlen + 1;
  464.             continue;
  465.         }
  466.         if (strncmp(&addrs[i], hostfullname, flen) == 0 &&
  467.           addrs[i + flen] == '!') {
  468.             i += flen + 1;
  469.             continue;
  470.         }
  471.       }
  472.       addrs[j++] = addrs[i++];
  473.     }
  474.     addrs[j] = 0;
  475. }
  476.  
  477. mail_form(address, subj)
  478. char *address, *subj;
  479. {
  480.     /** copy the appropriate variables to the shared space... */
  481.  
  482.     strcpy(subject, subj);
  483.     strcpy(to, address);
  484.     strcpy(expanded_to, address);
  485.  
  486.     return(mail(FORM, NO, NO));
  487. }
  488.  
  489. int
  490. recall_last_msg(filename, copy_msg, cancelled_msg, already_has_text)
  491. char *filename;
  492. int  copy_msg, *cancelled_msg, *already_has_text;
  493. {
  494.     char ch;
  495.  
  496.     /** If filename exists and we've recently cancelled a message,
  497.         the ask if the user wants to use that message instead!  This
  498.         routine returns TRUE if the user wants to retransmit the last
  499.         message, FALSE otherwise...
  500.     **/
  501.  
  502.     register int retransmit = FALSE;
  503.  
  504.     if (access(filename, EDIT_ACCESS) == 0 && *cancelled_msg) {
  505.       Raw(ON);
  506.       CleartoEOLN();
  507.       if (copy_msg)
  508.         PutLine1(LINES-1,0,"Recall last kept message instead? (y/n) y%c",
  509.              BACKSPACE);
  510.       else
  511.         PutLine1(LINES-1,0,"Recall last kept message? (y/n) y%c",
  512.              BACKSPACE);
  513.       fflush(stdout);
  514.       ch = ReadCh();
  515.       if (tolower(ch) != 'n') {
  516.         Write_to_screen("Yes.",0);
  517.             retransmit++;
  518.         *already_has_text = TRUE;
  519.       }
  520.       else
  521.         Write_to_screen("No.",0);
  522.  
  523.       fflush(stdout);
  524.  
  525.       *cancelled_msg = 0;
  526.     }
  527.  
  528.     return(retransmit);
  529. }
  530.  
  531. int
  532. verify_transmission(filename, form_letter)
  533. char *filename;
  534. int  *form_letter;
  535. {
  536.     /** Ensure the user wants to send this.  This routine returns
  537.         the character entered.  Modified compliments of Steve Wolf
  538.         to add the'dead.letter' feature.
  539.         Also added form letter support...
  540.     **/
  541.  
  542.     FILE *deadfd, *messagefd;
  543.     char ch, buffer[SLEN], fname[SLEN];
  544.     int x_coord, y_coord;
  545.  
  546.     while(1) {
  547.       /* clear bottom part of screen */
  548.       MoveCursor(LINES-2,0);
  549.       CleartoEOS();
  550.  
  551.       /* display prompt and menu according to
  552.        * user level and what's available on the menu */
  553.       if (user_level == 0) {
  554.         PutLine0(LINES-2,0,
  555.       "Please choose one of the following options by parenthesized letter: s");
  556.         GetXYLocation(&x_coord, &y_coord);
  557.         y_coord--;    /* backspace over default answer */
  558.         Centerline(LINES-1,
  559.           "e)dit message, edit h)eaders, s)end it, or f)orget it.");
  560.       } else {
  561.         PutLine0(LINES-2, 0, "And now: s");
  562.         GetXYLocation(&x_coord, &y_coord);
  563.         y_coord--;    /* backspace over default answer */
  564.         if (*form_letter == PREFORMATTED)  {
  565.            strcpy(buffer, "Choose ");
  566.         } else if (*form_letter == YES) {
  567.            strcpy(buffer, "Choose e)dit form, ");
  568.         } else if (*form_letter == MAYBE)  {
  569.            strcpy(buffer, "Choose e)dit msg, m)ake form, ");
  570.         } else {
  571.            strcpy(buffer, "Choose e)dit message, ");
  572.         }
  573. #ifdef ALLOW_SUBSHELL
  574.         strcat(buffer, "!)shell, ");
  575. #endif
  576.         strcat(buffer, "h)eaders, c)opy file, s)end, or f)orget.");
  577.         Centerline(LINES-1, buffer);
  578.       }
  579.  
  580.       /* wait for answer */
  581.       fflush(stdin);
  582.       fflush(stdout);
  583.       Raw(ON);    /* double check... testing only... */
  584.       MoveCursor(x_coord, y_coord);
  585.       ch = ReadCh();
  586.       ch = tolower(ch);
  587.  
  588.       /* process answer */
  589.       switch (ch) {
  590.          case 'f': Write_to_screen("Forget",0);
  591.                if (mail_only) {
  592.              /** try to save it as a dead letter file **/
  593.              save_file_stats(fname);
  594.              sprintf(fname, "%s/%s", home, dead_letter);
  595.              if ((deadfd = fopen(fname,"a")) == NULL) {
  596.                dprint(1, (debugfile,
  597.            "\nAttempt to append to deadletter file '%s' failed: %s\n\r",
  598.                    fname, error_name(errno)));
  599.                error("Message not saved, Sorry.");
  600.              }
  601.              else if ((messagefd = fopen(filename, "r")) == NULL) {
  602.                dprint(1, (debugfile,
  603.                  "\nAttempt to read reply file '%s' failed: %s\n\r",
  604.                    filename, error_name(errno)));
  605.                error("Message not saved, Sorry.");
  606.              } else {
  607.                /* if we get here we're okay for everything */
  608.                while (fgets(buffer, SLEN, messagefd) != NULL)
  609.                  fputs(buffer, deadfd);
  610.  
  611.                fclose(messagefd);
  612.                fclose(deadfd);
  613.                restore_file_stats(fname);
  614.  
  615.                error1("Message saved in file \"$HOME/%s\".",
  616.                  dead_letter);
  617.  
  618.             }
  619.                } else if (user_level != 0)
  620.             set_error("Message kept. Can be restored at next f)orward, m)ail or r)eply.");
  621.                break;
  622.  
  623.          case '\n' :
  624.          case '\r' :
  625.          case 's'  : Write_to_screen("Send",0);
  626.              ch = 's';        /* make sure! */
  627.              break;
  628.  
  629.          case 'm'  : if (*form_letter == MAYBE) {
  630.                *form_letter = YES;
  631.                    switch (check_form_file(filename)) {
  632.                  case -1 : return('f');
  633.                  case 0  : *form_letter = MAYBE;  /* check later!*/
  634.                        error("No fields in form!");
  635.                        return('e');
  636.                  default : continue;
  637.                        }
  638.              }
  639.              else {
  640.                         Write_to_screen("%c??", 1, 07);    /* BEEP */
  641.                 sleep(1);
  642.                     continue;
  643.                      }
  644.          case 'e'  :  if (*form_letter != PREFORMATTED) {
  645.                 Write_to_screen("Edit",0);
  646.                      if (*form_letter == YES)
  647.                   *form_letter = MAYBE;
  648.                       }
  649.               else {
  650.                         Write_to_screen("%c??", 1, 07);    /* BEEP */
  651.                 sleep(1);
  652.                     continue;
  653.                      }
  654.              break;
  655.  
  656.          case 'h'  : Write_to_screen("Headers",0);
  657.              break;
  658.  
  659.          case 'c'  : Write_to_screen("Copy file",0);
  660.              break;
  661.  
  662.          case '!'  : break;
  663.  
  664.          default   : Write_to_screen("%c??", 1, 07);    /* BEEP */
  665.              sleep(1);
  666.                  continue;
  667.        }
  668.  
  669.        return(ch);
  670.     }
  671. }
  672.  
  673. FILE *
  674. write_header_info(filename, long_to, long_cc, long_bcc, form, copy)
  675. char *filename, *long_to, *long_cc, *long_bcc;
  676. int   form, copy;
  677. {
  678.     /** Try to open filedesc as the specified filename.  If we can,
  679.         then write all the headers into the file.  The routine returns
  680.         'filedesc' if it succeeded, NULL otherwise.  Added the ability
  681.         to have backquoted stuff in the users .elmheaders file!
  682.         If copy is TRUE, then treat this as the saved copy of outbound
  683.         mail.
  684.     **/
  685.  
  686.     char opentype[2];
  687.     long time(), thetime;
  688.     char *ctime();
  689.     static FILE *filedesc;        /* our friendly file descriptor  */
  690.  
  691. #ifdef SITE_HIDING
  692.     char  buffer[SLEN];
  693.     int   is_hidden_user;        /* someone we should know about?  */
  694. #endif
  695. #ifdef MMDF
  696.     int   is_submit_mailer;        /* using submit means change From: */
  697. #endif /* MMDF */
  698.  
  699.     char  *get_arpa_date();
  700.  
  701.     if(copy)
  702.         strcpy(opentype, "a");
  703.     else
  704.         strcpy(opentype, "w");
  705.  
  706.     save_file_stats(filename);
  707.     if ((filedesc = fopen(filename, opentype)) == NULL) {
  708.       dprint(1, (debugfile,
  709.         "Attempt to open file %s for writing failed! (write_header_info)\n",
  710.          filename));
  711.       dprint(1, (debugfile, "** %s - %s **\n\n", error_name(errno),
  712.          error_description(errno)));
  713.       error2("Error %s encountered trying to write to %s.",
  714.         error_name(errno), filename);
  715.       sleep(2);
  716.       return(NULL);        /* couldn't open it!! */
  717.     }
  718.  
  719.     restore_file_stats(filename);
  720.  
  721.     if(copy) {    /* Add top line that mailer would add */
  722. #ifdef MMDF
  723.       fprintf(filedesc, MSG_SEPERATOR);
  724. #endif /* MMDF */
  725.       thetime = time((long *) 0);
  726.       fprintf(filedesc,"From %s %s", username, ctime(&thetime));
  727.     }
  728.  
  729. #ifdef SITE_HIDING
  730.     if ( !copy && (is_hidden_user = is_a_hidden_user(username))) {
  731.       /** this is the interesting part of this trick... **/
  732.       sprintf(buffer, "From %s!%s %s\n",  HIDDEN_SITE_NAME,
  733.           username, get_ctime_date());
  734.       fprintf(filedesc, "%s", buffer);
  735.       dprint(1,(debugfile, "\nadded: %s", buffer));
  736.       /** so is this perverted or what? **/
  737.     }
  738. #endif
  739.  
  740.  
  741.     /** Subject moved to top of headers for mail because the
  742.         pure System V.3 mailer, in its infinite wisdom, now
  743.         assumes that anything the user sends is part of the
  744.         message body unless either:
  745.         1. the "-s" flag is used (although it doesn't seem
  746.            to be supported on all implementations??)
  747.         2. the first line is "Subject:".  If so, then it'll
  748.            read until a blank line and assume all are meant
  749.            to be headers.
  750.         So the gory solution here is to move the Subject: line
  751.         up to the top.  I assume it won't break anyone elses program
  752.         or anything anyway (besides, RFC-822 specifies that the *order*
  753.         of headers is irrelevant).  Gahhhhh....
  754.     **/
  755.  
  756.     fprintf(filedesc,"Date: %s\n", get_arpa_date());
  757.  
  758. #ifndef DONT_ADD_FROM
  759. #ifdef MMDF
  760.     is_submit_mailer = (strcmp(submitmail,mailer) == 0);
  761. #endif /* MMDF */
  762. # ifdef SITE_HIDING
  763. #    ifdef MMDF
  764.     if (is_submit_mailer)
  765.       fprintf(filedesc,"From: %s <%s>\n", full_username, username);
  766.     else
  767. #    endif /* MMDF */
  768.     if (is_hidden_user)
  769.       fprintf(filedesc,"From: %s <%s!%s!%s>\n", full_username,
  770.           hostname, HIDDEN_SITE_NAME, username);
  771.     else
  772.       fprintf(filedesc,"From: %s <%s!%s>\n", full_username,
  773.           hostname, username);
  774. # else
  775. #  ifdef  INTERNET
  776. #   ifdef  USE_DOMAIN
  777. #    ifdef _MMDF
  778.     if (is_submit_mailer)
  779.       fprintf(filedesc,"From: %s <%s>\n", full_username, username);
  780.     else
  781. #    endif /* MMDF */
  782.       fprintf(filedesc,"From: %s <%s@%s>\n", full_username,
  783.         username, hostfullname);
  784. #   else
  785. #    ifdef _MMDF
  786.     if (is_submit_mailer)
  787.       fprintf(filedesc,"From: %s <%s>\n", full_username, username);
  788.     else
  789. #    endif /* MMDF */
  790.     fprintf(filedesc,"From: %s <%s@%s>\n", full_username,
  791.         username, hostname);
  792. #   endif
  793. #  else
  794. #    ifdef MMDF
  795.     if (is_submit_mailer)
  796.       fprintf(filedesc,"From: %s <%s>\n", full_username, username);
  797.     else
  798. #    endif /* MMDF */
  799.     fprintf(filedesc,"From: %s <%s!%s>\n", full_username,
  800.         hostname, username);
  801. #  endif
  802. # endif
  803. #endif
  804.  
  805.     fprintf(filedesc, "Subject: %s\n", subject);
  806.  
  807.     fprintf(filedesc, "To: %s\n", format_long(long_to, strlen("To:")));
  808.  
  809.     if (cc[0] != '\0')
  810.         fprintf(filedesc, "Cc: %s\n", format_long(long_cc, strlen("Cc: ")));
  811.  
  812.     if (copy && (bcc[0] != '\0'))
  813.         fprintf(filedesc, "Bcc: %s\n", format_long(long_bcc, strlen("Bcc: ")));
  814.  
  815.     if (strlen(action) > 0)
  816.         fprintf(filedesc, "Action: %s\n", action);
  817.  
  818.     if (strlen(priority) > 0)
  819.         fprintf(filedesc, "Priority: %s\n", priority);
  820.  
  821.     if (strlen(expires) > 0)
  822.         fprintf(filedesc, "Expires: %s\n", expires);
  823.  
  824.     if (strlen(reply_to) > 0)
  825.         fprintf(filedesc, "Reply-To: %s\n", reply_to);
  826.  
  827.     if (strlen(in_reply_to) > 0)
  828.         fprintf(filedesc, "In-Reply-To: %s\n", in_reply_to);
  829.  
  830.     if (strlen(user_defined_header) > 0)
  831.         fprintf(filedesc, "%s\n", user_defined_header);
  832.  
  833.     add_mailheaders(filedesc);
  834.  
  835.     if (form)
  836.       fprintf(filedesc, "Content-Type: mailform\n");
  837.  
  838. #ifndef NO_XHEADER
  839.     fprintf(filedesc, "X-Mailer: ELM [version %s] for OS/2\n", version_buff);
  840. #endif /* !NO_XHEADER */
  841.  
  842.     putc('\n', filedesc);
  843.  
  844.     return((FILE *) filedesc);
  845. }
  846.  
  847. copy_message_across(source, dest, copy)
  848. FILE *source, *dest;
  849. int copy;
  850. {
  851.     /** Copy the message in the file pointed to by source to the
  852.         file pointed to by dest.
  853.         If copy is TRUE, treat as a saved copy of outbound mail. **/
  854.  
  855.     int  crypted = FALSE;            /* are we encrypting?  */
  856.     int  encoded_lines = 0;            /* # lines encoded     */
  857.     char buffer[SLEN];            /* file reading buffer */
  858.  
  859.     while (fgets(buffer, SLEN, source) != NULL) {
  860.       if (buffer[0] == '[') {
  861.         if (strncmp(buffer, START_ENCODE, strlen(START_ENCODE))==0)
  862.           crypted = TRUE;
  863.         else if (strncmp(buffer, END_ENCODE, strlen(END_ENCODE))==0)
  864.           crypted = FALSE;
  865.         else if ((strncmp(buffer, DONT_SAVE, strlen(DONT_SAVE)) == 0)
  866.               || (strncmp(buffer, DONT_SAVE2, strlen(DONT_SAVE2)) == 0)) {
  867.           if(copy) break;  /* saved copy doesn't want anything after this */
  868.           else continue;   /* next line? */
  869.         }
  870.         else if (crypted) {
  871.           if (! gotten_key++)
  872.             getkey(ON);
  873.           else if (! encoded_lines)
  874.             get_key_no_prompt();        /* reinitialize.. */
  875.           encode(buffer);
  876.           encoded_lines++;
  877.         }
  878.       }
  879.       else if (crypted) {
  880.         if (batch_only) {
  881.           printf(
  882.         "Sorry. Cannot send encrypted mail in \"batch mode\".\n\r");
  883.           leave();
  884.         } else if (! gotten_key++)
  885.           getkey(ON);
  886.         else if (! encoded_lines)
  887.           get_key_no_prompt();        /* reinitialize.. */
  888.         encode(buffer);
  889.         encoded_lines++;
  890.       }
  891.  
  892.       if (copy && (strncmp(buffer, "From ", 5) == 0))
  893.         /* Add in the > to a From on our copy */
  894.         fprintf(dest, ">%s", buffer);
  895.  
  896.       else if (!copy && strcmp(buffer, ".\n") == 0)
  897.         /* Because some mail transport agents take a lone period to
  898.          * mean EOF, we add a blank space on outbound message.
  899.          */
  900.         fputs(". \n", dest);
  901.         else
  902.           if (fputs(buffer, dest) == EOF) {
  903.         Write_to_screen("\n\rWrite failed in copy_message_across\n\r", 0);
  904.         emergency_exit();
  905.         }
  906.     }
  907. #ifdef _MMDF
  908.     if (copy) fputs(MSG_SEPERATOR, dest);
  909. #else
  910.     if (copy) fputs("\n", dest);    /* ensure a blank line at the end */
  911. #endif /* MMDF */
  912. }
  913.  
  914. int
  915. verify_bounceback()
  916. {
  917.     char    ch;
  918.  
  919.     /** Ensure the user wants to have a bounceback copy too.  (This is
  920.         only called on messages that are greater than the specified
  921.         threshold hops and NEVER for non-uucp addresses.... Returns
  922.         TRUE iff the user wants to bounce a copy back....
  923.      **/
  924.  
  925.     MoveCursor(LINES,0);
  926.     CleartoEOLN();
  927.     PutLine1(LINES,0,
  928.           "\"Bounce\" a copy off the remote machine? (y/n) y%c",
  929.           BACKSPACE);
  930.     fflush(stdin);    /* wait for answer! */
  931.     fflush(stdout);
  932.     ch = ReadCh();
  933.     if (tolower(ch) != 'y') {
  934.       Write_to_screen("No.", 0);
  935.       fflush(stdout);
  936.       return(FALSE);
  937.     }
  938.     Write_to_screen("Yes.", 0);
  939.     fflush(stdout);
  940.  
  941.     return(TRUE);
  942. }
  943.  
  944.  
  945. int
  946. append_sig(file)
  947. FILE *file;
  948. {
  949.     /* Append the correct signature file to file.  Return TRUE if
  950.            we append anything.  */
  951.  
  952.         /* Look at the to and cc list to determine which one to use */
  953.  
  954.     /* We could check the bcc list too, but we don't want people to
  955.            know about bcc, even indirectly */
  956.  
  957.     /* Some people claim that  user@anything.same_domain should be
  958.        considered local.  Since it's not the same machine, better be
  959.            safe and use the remote sig (presumably it has more complete
  960.            information).  You can't necessarily finger someone in the
  961.            same domain. */
  962.  
  963.       if (!batch_only && (local_signature[0] || remote_signature[0])) {
  964.  
  965.             char filename2[SLEN];
  966.         char *sig;
  967.  
  968.           if (index(expanded_to, '!') || index(expanded_cc,'!'))
  969.               sig = remote_signature;        /* ! always means remote */
  970.             else {
  971.           /* check each @ for @thissite.domain */
  972.           /* if any one is different than this, then use remote sig */
  973.           int len;
  974.           char *ptr;
  975.           char sitename[SLEN];
  976.           sprintf(sitename,"@%s",hostfullname);
  977.           len = strlen(sitename);
  978.               sig = local_signature;
  979.               for (ptr = index(expanded_to,'@'); ptr;  /* check To: list */
  980.               ptr = index(ptr+1,'@')) {
  981.         if (strincmp(ptr,sitename,len) != 0
  982.             || (*(ptr+len) != ',' && *(ptr+len) != 0
  983.             && *(ptr+len) != ' ')) {
  984.               sig = remote_signature;
  985.                   break;
  986.                 }
  987.               }
  988.               if (sig == local_signature)           /* still local? */
  989.                 for (ptr = index(expanded_cc,'@'); ptr;   /* check Cc: */
  990.             ptr = index(ptr+1,'@')) {
  991.           if (strincmp(ptr,sitename,len) != 0
  992.               || (*(ptr+len) != ',' && *(ptr+len) != 0
  993.               && *(ptr+len) != ' ')) {
  994.                 sig = remote_signature;
  995.                     break;
  996.                   }
  997.                 }
  998.             }
  999.  
  1000.             if (sig[0]) {  /* if there is a signature file */
  1001.           if (sig_dashes) /* dashes are optional */
  1002.             fprintf(file, "\n-- \n");  /* News 2.11 compatibility? */
  1003.           if (sig[0] != '/')
  1004.             sprintf(filename2, "%s/%s", home, sig);
  1005.           else
  1006.             strcpy(filename2, sig);
  1007.           (void) append(file, filename2);
  1008.  
  1009.               return TRUE;
  1010.             }
  1011.           }
  1012.  
  1013. return FALSE;
  1014.  
  1015. }
  1016.