home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / src / savecopy.c < prev    next >
C/C++ Source or Header  |  1993-09-29  |  7KB  |  246 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: savecopy.c,v 4.1 90/04/28 22:44:02 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:    savecopy.c,v $
  17.  * Revision 4.1  90/04/28  22:44:02  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  *
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** Save a copy of the specified message in a folder.
  24.  
  25. **/
  26.  
  27. #include "headers.h"
  28. #ifdef I_TIME
  29. #  include <time.h>
  30. #endif
  31. #ifdef I_SYSTIME
  32. #  include <sys/time.h>
  33. #endif
  34.  
  35. #include <errno.h>
  36.  
  37. char *format_long();
  38. char *error_name(), *error_description();
  39. char *ctime();
  40.  
  41. extern char in_reply_to[SLEN];    /* In-Reply-To: string */
  42. #ifndef OS2
  43. extern int errno;
  44. #endif
  45.  
  46. char *strcat(), *strcpy();
  47. unsigned long sleep();
  48. time_t time();
  49.  
  50. save_copy(to, cc, bcc, filename, copy_file, form)
  51. char *to, *cc, *bcc, *filename, *copy_file;
  52. int form;
  53. {
  54.     /** This routine appends a copy of the outgoing message to the
  55.         file specified.  **/
  56.  
  57.     FILE *save,        /* file id for file to save to */
  58.          *message,        /* file id for file with message body */
  59.          *write_header_info();
  60.     char  buffer[SLEN],    /* read buffer                */
  61.           savename[SLEN];    /* name of file saving into    */
  62.  
  63.  
  64.     /* presume copy_file is okay as is for now */
  65.     strcpy(savename, copy_file);
  66.  
  67.     /* if save-by-name wanted */
  68.     if((strcmp(copy_file, "=") == 0)  || (strcmp(copy_file, "=?") == 0)) {
  69.  
  70.       get_return_name(to, buffer, TRUE);    /* determine 'to' login */
  71.       if (strlen(buffer) == 0) {
  72.  
  73.         /* can't get file name from 'to' -- use sent_mail instead */
  74.         dprint(3, (debugfile,
  75.         "Warning: get_return_name couldn't break down %s\n", to));
  76.         error1(
  77. "Cannot determine `to' name to save by! Saving to \"sent\" folder %s instead.",
  78.           sent_mail);
  79.         strcpy(savename, "<");
  80.         sleep(3);
  81.       } else
  82.         sprintf(savename, "=%s", buffer);        /* good! */
  83.     }
  84.  
  85.     expand_filename(savename, TRUE);    /* expand special chars */
  86.  
  87.     /* If saving conditionally by logname but folder doesn't exist
  88.      * save to sent folder instead. */
  89.     if((strcmp(copy_file, "=?") == 0)
  90.           && (access(savename, ACCESS_EXISTS) != 0)) {
  91.       dprint(5, (debugfile,
  92.         "Conditional save by name: file %s doesn't exist - using \"<\".\n",
  93.         savename));
  94.       strcpy(savename, "<");
  95.       expand_filename(savename, TRUE);
  96.     }
  97.  
  98.     if ((errno = can_open(savename, "a"))) {
  99.       dprint(2, (debugfile,
  100.       "Error: attempt to autosave to a file that can't be appended to!\n"));
  101.       dprint(2, (debugfile, "\tfilename = \"%s\"\n", savename));
  102.       dprint(2, (debugfile, "** %s - %s **\n", error_name(errno),
  103.           error_description(errno)));
  104.  
  105.       /* Lets try sent_mail before giving up */
  106.       if(strcmp(sent_mail, savename) == 0) {
  107.         /* we are ALREADY using sent_mail! */
  108.         error1("Cannot save to %s!", savename);
  109.         sleep(3);
  110.         return(FALSE);
  111.       }
  112.  
  113.       if ((errno = can_open(sent_mail, "a"))) {
  114.         dprint(2, (debugfile,
  115.       "Error: attempt to autosave to a file that can't be appended to!\n"));
  116.         dprint(2, (debugfile, "\tfilename = \"%s\"\n", sent_mail));
  117.         dprint(2, (debugfile, "** %s - %s **\n", error_name(errno),
  118.             error_description(errno)));
  119.         error2("Cannot save to %s nor to \"sent\" folder %s!",
  120.             savename, sent_mail);
  121.         sleep(3);
  122.         return(FALSE);
  123.       }
  124.       error2("Cannot save to %s! Saving to \"sent\" folder %s instead.",
  125.           savename, sent_mail);
  126.       sleep(3);
  127.       strcpy(savename, sent_mail);
  128.     }
  129.  
  130.     save_file_stats(savename);
  131.  
  132.     /* Write header */
  133.     if ((save = write_header_info(savename, to, cc, bcc,
  134.           form == YES, TRUE, FALSE)) == NULL)
  135.       return(FALSE);
  136.  
  137.     /* Now add file with message as handed to mailer */
  138.     if ((message = fopen(filename, "r")) == NULL) {
  139.       fclose(save);
  140.       dprint(1, (debugfile,
  141.          "Error: Couldn't read folder %s (save_copy)\n", filename));
  142.       dprint(1, (debugfile, "** %s - %s **\n", error_name(errno),
  143.           error_description(errno)));
  144.       error1("Couldn't read folder %s!", filename);
  145.       sleep(3);
  146.       return(FALSE);
  147.     }
  148.  
  149.         copy_message_across(message, save, TRUE);
  150.  
  151.  
  152.     fclose(save);
  153.     fclose(message);
  154.  
  155.     restore_file_stats(savename);
  156.  
  157.     return(TRUE);
  158. }
  159. char *
  160. cf_english(fn)
  161. char *fn;
  162. {
  163.     /** Return "English" expansion for special copy file name abbreviations
  164.     or just the file name  **/
  165.  
  166.     if(!*fn)
  167.       return("<no save>");
  168.     else if(!fn[1]) {
  169.       if(*fn == '=')
  170.     return("<unconditionally save by name>");
  171.       else if(*fn == '<')
  172.     return("<\"sent\" folder>");
  173.     } else if ((fn[0] == '=') && (fn[1] == '?'))
  174.       return("<conditionally save by name>");
  175.  
  176.     return(fn);
  177. }
  178.  
  179. #define NCF_PROMPT    "Save copy in (use '?' for help/to list folders): "
  180. int
  181. name_copy_file(fn)
  182. char *fn;
  183. {
  184.     /** Prompt user for name of file for saving copy of outbound msg to.
  185.     Return true if we need a redraw. **/
  186.  
  187.     int redraw = 0;    /* set when we ask for help = need redraw */
  188.     char buffer[SLEN], origbuffer[SLEN];
  189.     static char helpmsg[LONG_STRING];
  190.  
  191.     /* expand passed copy file name into English */
  192.     strcpy(buffer, cf_english(fn));
  193.  
  194.     /* prepare screen with instructions */
  195.     MoveCursor(LINES-2, 0);
  196.     CleartoEOS();
  197.     PutLine0(LINES-2, 0, NCF_PROMPT);
  198.  
  199.     while(1) {
  200.  
  201.       /* get file name from user input */
  202.       strcpy(origbuffer, buffer);
  203.       optionally_enter(buffer, LINES-2, strlen(NCF_PROMPT), FALSE, FALSE);
  204.  
  205.       if(strcmp(buffer, "?") != 0) { /* got what we wanted - non-help choice */
  206.  
  207.     if(strcmp(origbuffer, buffer) != 0)
  208.       /* user changed from our English expansion
  209.        * so we'd better copy user input to fn
  210.        */
  211.       strcpy(fn, buffer);
  212.  
  213.     /* else user presumably left our English expansion - no change in fn */
  214.  
  215.     /* display English expansion of new user input a while */
  216.     PutLine1(LINES-2, strlen(NCF_PROMPT), cf_english(fn));
  217.     MoveCursor(LINES, 0);
  218.     sleep(1);
  219.     MoveCursor(LINES-2, 0);
  220.     CleartoEOS();
  221.  
  222.     return(redraw);
  223.       }
  224.  
  225.       /* give help and list folders */
  226.       redraw = TRUE;
  227.       if(!*helpmsg)     /* help message not yet formulated */
  228.         sprintf(helpmsg,
  229.     "\n\r%s\n\r%s%s%s\n\r%s\n\r%s\n\r%s\n\r%s\n\r%s\n\r\n\r",
  230.     "Enter: <nothing> to not save a copy of the message,",
  231.     "       '<'       to save in your \"sent\" folder (", sent_mail, "),",
  232.     "       '='       to save by name (the folder name depends on whom the",
  233.     "                     message is to, in the end),",
  234.     "       '=?'      to save by name if the folder already exists,",
  235.     "                     and if not, to your \"sent\" folder,",
  236.     "       or a filename (a leading '=' denotes your folder directory).");
  237.  
  238.       list_folders(4, helpmsg);
  239.       PutLine0(LINES-2, 0, NCF_PROMPT);
  240.  
  241.       /* restore as default to English version of the passed copy file name */
  242.       strcpy(buffer, cf_english(fn));
  243.  
  244.     }
  245. }
  246.