home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / mailmsg1.c < prev    next >
C/C++ Source or Header  |  1991-01-18  |  11KB  |  364 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: mailmsg1.c,v 4.1.1.1 90/06/05 20:52:21 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.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:    mailmsg1.c,v $
  17.  * Revision 4.1.1.1  90/06/05  20:52:21  syd
  18.  * Fixes the 'g' Group Reply command to send to the cc list also.
  19.  * A bad variable name caused it to be ignored.
  20.  * From: chip@chinacat.Unicom.COM (Chip Rosenthal)
  21.  *
  22.  * Revision 4.1  90/04/28  22:43:26  syd
  23.  * checkin of Elm 2.3 as of Release PL0
  24.  *
  25.  *
  26.  ******************************************************************************/
  27.  
  28. /** Interface to allow mail to be sent to users.  Part of ELM  **/
  29.  
  30.  
  31. #include "headers.h"
  32.  
  33. /** strings defined for the hdrconfg routines **/
  34.  
  35. char subject[SLEN], in_reply_to[SLEN], expires[SLEN],
  36.      action[SLEN], priority[SLEN], reply_to[SLEN], to[VERY_LONG_STRING],
  37.      cc[VERY_LONG_STRING], expanded_to[VERY_LONG_STRING],
  38.      expanded_cc[VERY_LONG_STRING], user_defined_header[SLEN],
  39.      bcc[VERY_LONG_STRING], expanded_bcc[VERY_LONG_STRING];
  40.  
  41. char *format_long(), *strip_commas(), *tail_of_string(), *strcpy();
  42. unsigned long sleep();
  43.  
  44. int
  45. sendmsg(given_to, given_cc, given_subject, edit_message, form_letter, replying)
  46. char *given_to, *given_cc, *given_subject;
  47. int   edit_message, form_letter, replying;
  48. {
  49.     /** Prompt for fields and then call mail() to send the specified
  50.         message.  If 'edit_message' is true then don't allow the
  51.             message to be edited. 'form_letter' can be "YES" "NO" or "MAYBE".
  52.         if YES, then add the header.  If MAYBE, then add the M)ake form
  53.         option to the last question (see mailsg2.c) etc. etc.
  54.         if (replying) then add an In-Reply-To: header...
  55.         Return TRUE if the main part of the screen has been changed
  56.         (useful for knowing whether a redraw is needed.
  57.     **/
  58.  
  59.     int  copy_msg = FALSE, is_a_response = FALSE;
  60.  
  61.     /* First: zero all current global message strings */
  62.  
  63.     cc[0] = bcc[0] = reply_to[0] = expires[0] = '\0';
  64.     action[0] = priority[0] = user_defined_header[0] = in_reply_to[0] ='\0';
  65.     expanded_to[0] = expanded_cc[0] = expanded_bcc[0] = '\0';
  66.  
  67.     strcpy(subject, given_subject);        /* copy given subject */
  68.     strcpy(to, given_to);            /* copy given to:     */
  69.     strcpy(cc, given_cc);            /*  and so on..       */
  70.  
  71.     /******* And now the real stuff! *******/
  72.  
  73.     copy_msg=copy_the_msg(&is_a_response); /* copy msg into edit buffer? */
  74.  
  75.     if (get_to(to, expanded_to) == 0)   /* get the To: address and expand */
  76.       return(0);
  77.     if ( cc[0] != '\0' )            /* expand out CC addresses */
  78.       build_address(strip_commas(cc), expanded_cc);
  79.  
  80.     /** if we're batchmailing, let's send it and GET OUTTA HERE! **/
  81.  
  82.     if (batch_only) {
  83.       return(mail(FALSE, FALSE, form_letter));
  84.     }
  85.  
  86.     display_to(expanded_to);    /* display the To: field on screen... */
  87.  
  88.     dprint(3, (debugfile, "\nMailing to \"%s\"\n", expanded_to));
  89.  
  90.     if (get_subject(subject) == 0)        /* get the Subject: field */
  91.       return(0);
  92.  
  93.     dprint(4, (debugfile, "Subject is %s\n", subject));
  94.  
  95.     if (prompt_for_cc) {
  96.       if (get_copies(cc, expanded_to, expanded_cc, copy_msg) == 0)
  97.         return(0);
  98.  
  99.       if (strlen(cc) > 0)
  100.         dprint(4, (debugfile, "Copies to %s\n", expanded_cc));
  101.     }
  102.  
  103.     MoveCursor(LINES,0);    /* so you know you've hit <return> ! */
  104.  
  105.     /** generate the In-Reply-To: header... **/
  106.  
  107.     if (is_a_response && replying)
  108.       generate_reply_to(current-1);
  109.  
  110.     /* and mail that puppy outta here! */
  111.  
  112.     return(mail(copy_msg, edit_message, form_letter));
  113. }
  114.  
  115. get_to(to_field, address)
  116. char *to_field, *address;
  117. {
  118.     /** prompt for the "To:" field, expanding into address if possible.
  119.         This routine returns ZERO if errored, or non-zero if okay **/
  120.  
  121.     if (strlen(to_field) == 0) {
  122.       if (user_level < 2) {
  123.         PutLine0(LINES-2, 0, "Send the message to: ");
  124.         (void) optionally_enter(to_field, LINES-2, 21, FALSE, FALSE);
  125.       }
  126.       else {
  127.         PutLine0(LINES-2, 0, "To: ");
  128.         (void) optionally_enter(to_field, LINES-2, 4, FALSE, FALSE);
  129.       }
  130.       if (strlen(to_field) == 0) {
  131.         ClearLine(LINES-2);
  132.         return(0);
  133.       }
  134.       (void) build_address(strip_commas(to_field), address);
  135.     }
  136.     else if (mail_only)
  137.       (void) build_address(strip_commas(to_field), address);
  138.     else
  139.       strcpy(address, to_field);
  140.  
  141.     if (strlen(address) == 0) {    /* bad address!  Removed!! */
  142.       ClearLine(LINES-2);
  143.       return(0);
  144.     }
  145.  
  146.     return(1);        /* everything is okay... */
  147. }
  148.  
  149. get_subject(subject_field)
  150. char *subject_field;
  151. {
  152.     char    ch;
  153.  
  154.     /** get the subject and return non-zero if all okay... **/
  155.     int len = 9, prompt_line;
  156.  
  157.     prompt_line = mail_only ? 4 : LINES-2;
  158.  
  159.     if (user_level == 0) {
  160.       PutLine0(prompt_line,0,"Subject of message: ");
  161.       len = 20;
  162.     }
  163.     else
  164.       PutLine0(prompt_line,0,"Subject: ");
  165.  
  166.     CleartoEOLN();
  167.  
  168.     if(optionally_enter(subject_field, prompt_line, len, TRUE, FALSE)==-1){
  169.       /** User hit the BREAK key! **/
  170.       MoveCursor(prompt_line,0);
  171.       CleartoEOLN();
  172.       error("Mail not sent.");
  173.       return(0);
  174.     }
  175.  
  176.     if (strlen(subject_field) == 0) {    /* zero length subject?? */
  177.       PutLine1(prompt_line,0,
  178.         "No subject - Continue with message? (y/n) n%c", BACKSPACE);
  179.  
  180.       ch = ReadCh();
  181.       if (tolower(ch) != 'y') {    /* user says no! */
  182.         Write_to_screen("No.", 0);
  183.         ClearLine(prompt_line);
  184.         error("Mail not sent.");
  185.         return(0);
  186.       }
  187.       else {
  188.         Write_to_screen("Yes.", 0);
  189.         PutLine0(prompt_line,0,"Subject: <none>");
  190.         CleartoEOLN();
  191.       }
  192.     }
  193.  
  194.     return(1);        /** everything is cruising along okay **/
  195. }
  196.  
  197. get_copies(cc_field, address, addressII, copy_message)
  198. char *cc_field, *address, *addressII;
  199. int   copy_message;
  200. {
  201.     /** Get the list of people that should be cc'd, returning ZERO if
  202.         any problems arise.  Address and AddressII are for expanding
  203.         the aliases out after entry!
  204.         If 'bounceback' is nonzero, add a cc to ourselves via the remote
  205.         site, but only if hops to machine are > bounceback threshold.
  206.         If copy-message, that means that we're going to have to invoke
  207.         a screen editor, so we'll need to delay after displaying the
  208.         possibly rewritten Cc: line...
  209.     **/
  210.     int prompt_line;
  211.  
  212.     prompt_line = mail_only ? 5 : LINES - 1;
  213.     PutLine0(prompt_line,0,"Copies to: ");
  214.  
  215.     fflush(stdout);
  216.  
  217.     if (optionally_enter(cc_field, prompt_line, 11, FALSE, FALSE) == -1) {
  218.       ClearLine(prompt_line-1);
  219.       ClearLine(prompt_line);
  220.  
  221.       error("Mail not sent.");
  222.       return(0);
  223.     }
  224.  
  225.     /** The following test is that if the build_address routine had
  226.         reason to rewrite the entry given, then, if we're mailing only
  227.         print the new Cc line below the old one.  If we're not, then
  228.         assume we're in screen mode and replace the incorrect entry on
  229.         the line above where we are (e.g. where we originally prompted
  230.         for the Cc: field).
  231.     **/
  232.  
  233.     if (build_address(strip_commas(cc_field), addressII)) {
  234.       PutLine1(prompt_line, 11, "%s", addressII);
  235.       if ((strcmp(editor, "builtin") != 0 && strcmp(editor, "none") != 0)
  236.           || copy_message)
  237.         sleep(2);
  238.     }
  239.  
  240.     if (strlen(address) + strlen(addressII) > VERY_LONG_STRING) {
  241.       dprint(2, (debugfile,
  242.         "String length of \"To:\" + \"Cc\" too long! (get_copies)\n"));
  243.       error("Too many people. Copies ignored.");
  244.       sleep(2);
  245.       cc_field[0] = '\0';
  246.     }
  247.  
  248.     return(1);        /* everything looks okay! */
  249. }
  250.  
  251. int
  252. copy_the_msg(is_a_response)
  253. int *is_a_response;
  254. {
  255.     /** Returns True iff the user wants to copy the message being
  256.         replied to into the edit buffer before invoking the editor!
  257.         Sets "is_a_response" to true if message is a response...
  258.     **/
  259.  
  260.     int answer = FALSE;
  261.  
  262.     if (forwarding)
  263.       answer = TRUE;
  264.     else if (strlen(to) > 0 && !mail_only) {  /* predefined 'to' line! */
  265.       if (auto_copy)
  266.         answer = TRUE;
  267.       else
  268.         answer = (want_to("Copy message? (y/n) ", 'n') == 'y');
  269.       *is_a_response = TRUE;
  270.     }
  271.  
  272.     return(answer);
  273. }
  274.  
  275. static int to_line, to_col;
  276.  
  277. display_to(address)
  278. char *address;
  279. {
  280.     /** Simple routine to display the "To:" line according to the
  281.         current configuration (etc)
  282.      **/
  283.     register int open_paren;
  284.  
  285.     to_line = mail_only ? 3 : LINES - 3;
  286.     to_col = mail_only ? 0 : COLUMNS - 50;
  287.     if (names_only)
  288.       if ((open_paren = chloc(address, '(')) > 0) {
  289.         if (open_paren < chloc(address, ')')) {
  290.           output_abbreviated_to(address);
  291.           return;
  292.         }
  293.       }
  294.     if(mail_only)
  295.       if(strlen(address) > 80)
  296.         PutLine1(to_line, to_col, "To: (%s)",
  297.             tail_of_string(address, 75));
  298.       else
  299.         PutLine1(to_line, to_col, "To: %s", address);
  300.     else if (strlen(address) > 45)
  301.       PutLine1(to_line, to_col, "To: (%s)",
  302.           tail_of_string(address, 40));
  303.     else {
  304.       if (strlen(address) > 30)
  305.         PutLine1(to_line, to_col, "To: %s", address);
  306.       else
  307.         PutLine1(to_line, to_col, "          To: %s", address);
  308.       CleartoEOLN();
  309.     }
  310. }
  311.  
  312. output_abbreviated_to(address)
  313. char *address;
  314. {
  315.     /** Output just the fields in parens, separated by commas if need
  316.         be, and up to COLUMNS-50 characters...This is only used if the
  317.         user is at level BEGINNER.
  318.     **/
  319.  
  320.     char newaddress[LONG_STRING];
  321.     register int iindex, newindex = 0, in_paren = 0, add_len;
  322.  
  323.     iindex = 0;
  324.  
  325.     add_len = strlen(address);
  326.     while (newindex < 55 && iindex < add_len) {
  327.       if (address[iindex] == '(') in_paren++;
  328.       else if (address[iindex] == ')') {
  329.         in_paren--;
  330.         if (iindex < add_len-4) {
  331.           newaddress[newindex++] = ',';
  332.           newaddress[newindex++] = ' ';
  333.         }
  334.       }
  335.  
  336.       /* copy if in_paren but not at the opening outer parens */
  337.       if (in_paren && !(address[iindex] == '(' && in_paren == 1))
  338.           newaddress[newindex++] = address[iindex];
  339.  
  340.       iindex++;
  341.     }
  342.  
  343.     newaddress[newindex] = '\0';
  344.  
  345.     if (mail_only)
  346.       if (strlen(newaddress) > 80)
  347.         PutLine1(to_line, to_col, "To: (%s)",
  348.            tail_of_string(newaddress, 60));
  349.       else
  350.         PutLine1(to_line, to_col, "To: %s", newaddress);
  351.     else if (strlen(newaddress) > 50)
  352.        PutLine1(to_line, to_col, "To: (%s)",
  353.            tail_of_string(newaddress, 40));
  354.      else {
  355.        if (strlen(newaddress) > 30)
  356.          PutLine1(to_line, to_col, "To: %s", newaddress);
  357.        else
  358.          PutLine1(to_line, to_col, "          To: %s", newaddress);
  359.        CleartoEOLN();
  360.      }
  361.  
  362.     return;
  363. }
  364.