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

  1.  
  2. static char rcsid[] = "@(#)$Id: file.c,v 4.1.1.1 90/10/07 19:48:05 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:    file.c,v $
  17.  * Revision 4.1.1.1  90/10/07  19:48:05  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  90/04/28  22:43:02  syd
  22.  * checkin of Elm 2.3 as of Release PL0
  23.  * 
  24.  *
  25.  ******************************************************************************/
  26.  
  27. /** File I/O routines, mostly the save to file command...
  28.  
  29. **/
  30.  
  31. #ifdef PWDINSYS
  32. #  include <sys/pwd.h>
  33. #else
  34. #  include <pwd.h>
  35. #endif
  36.  
  37. #include "headers.h"
  38. #include <ctype.h>
  39. #include <errno.h>
  40.  
  41. #ifdef BSD
  42. #undef tolower
  43. #endif
  44.  
  45. extern int errno;
  46.  
  47. char *error_name(), *error_description(),/* *strcpy(), *getenv(), */  *nameof();
  48. unsigned long sleep();
  49.  
  50. int
  51. save(redraw, silently, delete)
  52. int *redraw, silently, delete;
  53. {
  54.     /** Save all tagged messages + current in a folder.  If no messages
  55.         are tagged, save the current message instead!  This routine
  56.         will return ZERO if the operation failed.
  57.         'redraw' is set to TRUE iff we use the '?' and mess up
  58.         the screen.  Pretty reasonable, eh?  If "silently" is set,
  59.         then don't output the "D" character upon marking for
  60.         deletion...
  61.         If delete is set, then delete the saved messages, else
  62.         we are just copying the messages without deletion.
  63.     **/
  64.  
  65.     register int tagged = 0, i, oldstat, appending = 0;
  66.     int mesgnum;    /* message whose address is used for save-by-name fn */
  67.     char filename[SLEN], address[LONG_STRING], buffer[LONG_STRING];
  68.     static char helpmsg[LONG_STRING];
  69.     FILE *save_file;
  70.  
  71.     oldstat = headers[current-1]->status;    /* remember */
  72.     *redraw = FALSE;
  73.  
  74.     for (i=0; i < message_count; i++) {
  75.       if (ison(headers[i]->status, TAGGED)) {
  76.         if(!tagged)
  77.           mesgnum = i;    /* first tagged msg -  use this one for
  78.                  * save-by-name folder name */
  79.         tagged++;
  80.       }
  81.     }
  82.  
  83.     if (tagged == 0) {
  84.       mesgnum = current-1;    /* use this one for save-by-name folder name */
  85.       tagged = 1;
  86.       setit(headers[current-1]->status, TAGGED);
  87.     }
  88.  
  89.     dprint(4, (debugfile, "%d message%s tagged for saving (save)\n", tagged,
  90.         plural(tagged)));
  91.  
  92.     while (1) {
  93.  
  94.       PutLine2(LINES-2, 0, "%s message%s to: ",
  95.           (delete ? "Save" : "Copy"), plural(tagged));
  96.  
  97.       if (save_by_name) {
  98.         /** build default filename to save to **/
  99.         get_return(address, mesgnum);
  100.         get_return_name(address, buffer, TRUE);
  101.         sprintf(filename, "=%s", buffer);
  102.       }
  103.       else
  104.         filename[0] = '\0';
  105.  
  106.       if (tagged > 1)
  107.         optionally_enter(filename, LINES-2, 19, FALSE, FALSE);
  108.       else    
  109.         optionally_enter(filename, LINES-2, 18, FALSE, FALSE);
  110.   
  111.  
  112.       if (strlen(filename) == 0) {  /** <return> means 'cancel', right? **/
  113.         headers[current-1]->status = oldstat;    /* BACK! */
  114.         return(0);
  115.       }
  116.      
  117.       if (strcmp(filename,"?") == 0) {    /* user asked for listing */
  118.         *redraw = TRUE;    /* set the flag so we know what to do later */
  119.         if(!*helpmsg) {    /* format helpmsg if not yet done */
  120.  
  121.           strcpy(helpmsg, "\n\r\n\rEnter: <nothing> to not ");
  122.           strcat(helpmsg, (delete ? "save" : "copy"));
  123.           strcat(helpmsg, " your message");
  124.           strcat(helpmsg, (plural(tagged) ? "s" : ""));
  125.           strcat(helpmsg, "\n\r       '>' to ");
  126.           strcat(helpmsg, (delete ? "save" : "copy"));
  127.           strcat(helpmsg, " your message");
  128.           strcat(helpmsg, (plural(tagged) ? "s" : ""));
  129.           strcat(helpmsg, " to your \"received\" folder (");
  130.           strcat(helpmsg, nameof(recvd_mail));
  131.           strcat(helpmsg, ")\n\r       '<' to ");
  132.           strcat(helpmsg, (delete ? "save" : "copy"));
  133.           strcat(helpmsg, " your message");
  134.           strcat(helpmsg, (plural(tagged) ? "s" : ""));
  135.           strcat(helpmsg, " to your \"sent\" folder (");
  136.           strcat(helpmsg, nameof(sent_mail));
  137.           strcat(helpmsg, ") \n\r       a filename");
  138.           strcat(helpmsg, " (leading '=' denotes your folder directory ");
  139.           strcat(helpmsg, folders);
  140.           strcat(helpmsg, ").\n\r");
  141.         }
  142.  
  143.         list_folders(4, helpmsg);
  144.         continue;
  145.       }
  146.  
  147.       /* else - got a folder name - check it out */
  148.       if (! expand_filename(filename, TRUE)) {
  149.         dprint(2, (debugfile,
  150.           "Error: Failed on expansion of filename %s (save)\n", 
  151.           filename));
  152.         continue;
  153.       }
  154.       if ((errno = can_open(filename, "a"))) {
  155.         error2("Cannot %s message to folder %s!",
  156.           delete ? "save":"copy", filename);
  157.         continue;
  158.       }
  159.       break;    /* got a valid filename */
  160.     }
  161.  
  162.     save_file_stats(filename);
  163.  
  164.     if (access(filename,ACCESS_EXISTS)== 0)     /* already there!! */
  165.       appending = 1;
  166.       
  167.     dprint(4,(debugfile, "Saving mail to folder '%s'...\n", filename));
  168.  
  169.     if ((save_file = fopen(filename,"a")) == NULL) {
  170.       dprint(2, (debugfile,
  171.         "Error: couldn't append to specified folder %s (save)\n", 
  172.         filename));
  173.       error1("Couldn't append to folder %s!", filename);
  174.       headers[current-1]->status = oldstat;    /* BACK! */
  175.       return(0); 
  176.     }
  177.  
  178.     /* if we need a redraw that means index screen no longer present
  179.      * so whatever silently was, now it's true - we can't show those
  180.      * delete markings.
  181.      */
  182.     if(*redraw) silently = TRUE;
  183.  
  184.     for (i=0; i < message_count; i++)     /* save each tagged msg */
  185.       if (headers[i]->status & TAGGED)
  186.         save_message(i, filename, save_file, (tagged > 1), appending++, 
  187.              silently, delete);
  188.  
  189.     fclose(save_file);
  190.  
  191.     restore_file_stats(filename);
  192.  
  193.     if (tagged > 1)
  194.       error2("Message%s %s.", plural(tagged), delete ? "saved": "copied");
  195.     return(1);
  196. }
  197.  
  198. int
  199. save_message(number, filename, fd, pause, appending, silently, delete)
  200. int number, pause, appending, silently, delete;
  201. char *filename;
  202. FILE *fd;
  203. {
  204.     /** Save an actual message to a folder.  This is called by 
  205.         "save()" only!  The parameters are the message number,
  206.         and the name and file descriptor of the folder to save to.
  207.         If 'pause' is true, a sleep(2) will be done after the
  208.         saved message appears on the screen...
  209.         'appending' is only true if the folder already exists 
  210.         If 'delete' is true, mark the message for deletion.
  211.     **/
  212.  
  213.     register int save_current, is_new;
  214.     
  215.     dprint(4, (debugfile, "\tSaving message %d to folder...\n", number));
  216.  
  217.     save_current = current;
  218.     current = number+1;
  219.  
  220.     /* change status from NEW before copy and reset to what it was
  221.      * so that copy doesn't look new, but we can preserve new status
  222.      * of message in this mailfile. This is important because if
  223.      * user does a resync, we don't want NEW status to be lost.
  224.      * I.e. NEW becomes UNREAD when we "really" leave a mailfile.
  225.      */
  226.     if(is_new = ison(headers[number]->status, NEW))
  227.       clearit(headers[number]->status, NEW);
  228.     copy_message("", fd, FALSE, FALSE, TRUE, FALSE, FALSE);
  229.     if(is_new)
  230.       setit(headers[number]->status, NEW);
  231.     current = save_current;
  232.  
  233.     if (delete)
  234.       setit(headers[number]->status, DELETED); /* deleted, but ...   */
  235.     clearit(headers[number]->status, TAGGED);  /* not tagged anymore */
  236.  
  237.     if (appending)
  238.       error2("Message %d appended to folder %s.", number+1, filename);
  239.     else
  240.       error3("Message %d %s to folder %s.", number+1,
  241.          delete ? "saved" : "copied", filename);
  242.  
  243.     if (! silently)
  244.       show_new_status(number);    /* update screen, if needed */
  245.  
  246.     if (pause && (!silently) && (!appending))
  247.       sleep(2);
  248. }
  249.  
  250. int
  251. expand_filename(filename, use_cursor_control)
  252. char *filename;
  253. int use_cursor_control;
  254. {
  255.     /** Expands    ~/    to the current user's home directory
  256.             ~user/    to the home directory of "user"
  257.             =,+,%    to the user's folder's directory
  258.             !    to the user's incoming mailbox
  259.             >    to the user's received folder
  260.             <    to the user's sent folder
  261.             shell variables (begun with $)
  262.  
  263.         Returns     1    upon proper expansions
  264.             0    upon failed expansions
  265.      **/
  266.  
  267.     char temp_filename[SLEN], varname[SLEN],
  268.         env_value[SLEN], logname[SLEN], *ptr;
  269.     register int iindex;
  270.     struct passwd *pass, *getpwnam();
  271.     char *getenv();
  272.  
  273.     ptr = filename;
  274.     while (*ptr == ' ') ptr++;    /* leading spaces GONE! */
  275.     strcpy(temp_filename, ptr);
  276.  
  277.     /** New stuff - make sure no illegal char as last **/
  278.     if (lastch(temp_filename) == '\n' || lastch(temp_filename) == '\r')
  279.       lastch(temp_filename) = '\0';
  280.       
  281.     /** Strip off any trailing backslashes **/
  282.     while (lastch(temp_filename) == '\\')
  283.         lastch(temp_filename) = '\0';
  284.  
  285.     if (temp_filename[0] == '~') {
  286.       if(temp_filename[1] == '/')
  287.         sprintf(filename, "%s%s%s",
  288.           home, (lastch(home) != '/' ? "/" : ""), &temp_filename[2]);
  289.       else {
  290.         for(ptr = &temp_filename[1], iindex = 0; *ptr && *ptr != '/'; ptr++, iindex++)
  291.           logname[iindex] = *ptr;
  292.         logname[iindex] = '\0';
  293.         if((pass = getpwnam(logname)) == NULL) {
  294.           dprint(3,(debugfile, 
  295.               "Error: Can't get home directory for %s (%s)\n",
  296.               logname, "expand_filename"));
  297.           if(use_cursor_control)
  298.         error1("Don't know what the home directory of \"%s\" is!",
  299.             logname);
  300.           else
  301.         printf(
  302.             "\n\rDon't know what the home directory of \"%s\" is!\n\r",
  303.             logname);
  304.           return(0);
  305.         }
  306.         sprintf(filename, "%s%s", pass->pw_dir, ptr);
  307.       }
  308.  
  309.     }
  310.     else if (temp_filename[0] == '=' || temp_filename[0] == '+' || 
  311.           temp_filename[0] == '%') {
  312.       sprintf(filename, "%s%s%s", folders, 
  313.         (temp_filename[1] != '/' && lastch(folders) != '/')? "/" : "",
  314.           &temp_filename[1]);
  315.     }
  316.     else if (temp_filename[0] == '$') {    /* env variable! */
  317.       for(ptr = &temp_filename[1], iindex = 0; isalnum(*ptr); ptr++, iindex++)
  318.         varname[iindex] = *ptr;
  319.       varname[iindex] = '\0';
  320.  
  321.       env_value[0] = '\0';            /* null string for strlen! */
  322.       if (getenv(varname) != NULL)
  323.         strcpy(env_value, getenv(varname));
  324.  
  325.       if (strlen(env_value) == 0) {
  326.         dprint(3,(debugfile, 
  327.             "Error: Can't expand environment variable $%s (%s)\n",
  328.             varname, "expand_filename"));
  329.         if(use_cursor_control)
  330.           error1("Don't know what the value of $%s is!", varname);
  331.         else
  332.           printf("\n\rDon't know what the value of $%s is!\n\r", varname);
  333.         return(0);
  334.       }
  335.  
  336.       sprintf(filename, "%s%s%s", env_value, 
  337.         (*ptr != '/' && lastch(env_value) != '/')? "/" : "", ptr);
  338.  
  339.     } else if (strcmp(temp_filename, "!") == 0) {
  340.       strcpy(filename, defaultfile);
  341.     } else if (strcmp(temp_filename, ">") == 0) {
  342.       strcpy(filename, recvd_mail);
  343.     } else if (strcmp(temp_filename, "<") == 0) {
  344.       strcpy(filename, sent_mail);
  345.     } else
  346.       strcpy(filename, temp_filename);
  347.       
  348.     return(1);
  349. }
  350.