home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / elm.lzh / ELM / SRC / NEWMBOX.C < prev    next >
Text File  |  1991-01-11  |  23KB  |  722 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: newmbox.c,v 4.1.1.2 90/06/26 20:18:06 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.2 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1988, USENET Community Trust
  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:    newmbox.c,v $
  17.  * Revision 4.1.1.2  90/06/26  20:18:06  syd
  18.  * Fix double word
  19.  * From: Peter Kendell <pete@tcom.stc.co.uk>
  20.  * 
  21.  * 
  22.  * Revision 4.1.1.1  90/06/21  21:10:33  syd
  23.  * Add another fixed mailbox id
  24.  * From: Syd
  25.  * 
  26.  * Revision 4.1  90/04/28  22:43:34  syd
  27.  * checkin of Elm 2.3 as of Release PL0
  28.  * 
  29.  *
  30.  ******************************************************************************/
  31.  
  32. /**  read new folder **/
  33.  
  34. #include <ctype.h>
  35. #include "headers.h"
  36.  
  37. #ifdef BSD
  38. #undef tolower        /* we have our own "tolower" routine instead! */
  39. #endif
  40.  
  41. #include <sys/types.h>        
  42. #include <sys/stat.h>
  43. #include <errno.h>
  44.  
  45. #define ENOENT E_PNNF
  46.  
  47. #ifdef I_TIME
  48. #  include <time.h>
  49. #endif
  50. #ifdef I_SYSTIME
  51. #  include <sys/time.h>
  52. #endif
  53.  
  54. extern int errno;
  55.  
  56. char *error_name(), *error_description();
  57. char *malloc(), *realloc(), *strcpy(), *strncpy(), *rindex(), *index();
  58. unsigned long sleep();
  59. void rewind();
  60. void exit();
  61. long bytes();
  62.  
  63. int
  64. newmbox(new_file, adds_only)
  65. char *new_file;
  66. int adds_only;
  67. {
  68.     /** Read a folder.
  69.  
  70.         new_file    - name of folder  to read. It is up to the calling
  71.               function to make sure that the file can be
  72.               read by the user. This is not checked in this
  73.               function. The reason why it is not checked here
  74.               is due to the situation where the user wants to
  75.               change folders: the new folder must be checked
  76.               for access *before* leaving the old one, which
  77.               is before this function gets called.
  78.         adds_only    - set if we only want to read newly added messages to
  79.                 same old folder.
  80.  
  81.     **/
  82.  
  83.     int  same_file;
  84.     int  new_folder_type;
  85.     int err;
  86.     char new_tempfile[SLEN];
  87.  
  88.     /* determine type of new mailfile and calculate temp file name */
  89.     if((new_folder_type = get_folder_type(new_file)) == SPOOL)
  90.       mk_temp_mail_fn(new_tempfile, new_file);
  91.     else
  92.       *new_tempfile = '\0';
  93.  
  94.     /* determine whether we are changing files */
  95.     same_file = !(strcmp(new_file, cur_folder));
  96.  
  97.     /* If we are changing files and we are changing to a spool file,
  98.      * make sure there isn't a temp file for it, because if
  99.      * there is, someone else is using ELM to read the new file,
  100.      * and we don't want to be reading it at the same time.
  101.      */
  102.     if((new_folder_type == SPOOL) && (!same_file)) {
  103.       if (access(new_tempfile, ACCESS_EXISTS) != -1) {
  104.         if(folder_type != NO_NAME) ClearScreen();
  105.         Centerline(15,
  106.           "Hey! An instantiation of ELM is already reading this mail!");
  107.         Centerline(17,
  108.           "If this is in error, then you'll need to save a copy of");
  109.         Centerline(18, "the following file then remove it:");
  110.         Centerline(19, new_tempfile);
  111.         MoveCursor(LINES, 0);  /* so shell prompt upon exit is on newline */
  112.         silently_exit();
  113.       }
  114.     }
  115.  
  116.     /* If we were reading a spool file and we are not just reading
  117.      * in the additional new messages to the same file, we need to
  118.      * remove the corresponding tempfile.
  119.      */
  120.  
  121.     if((folder_type == SPOOL) && !adds_only) {
  122.       if (access(cur_tempfolder, ACCESS_EXISTS) != -1) {
  123.         fclose(mailfile);
  124.         if (unlink(cur_tempfolder) != 0) {
  125.           error2("Sorry, can't unlink the temp file %s [%s]!\l\r",
  126.             cur_tempfolder, error_name(errno));
  127.           silently_exit();
  128.         }
  129.       }
  130.     }
  131.  
  132.     /* Okay! Now establish this new file as THE file */
  133.     strcpy(cur_folder, new_file);
  134.     folder_type = new_folder_type;
  135.     strcpy(cur_tempfolder, new_tempfile);
  136.  
  137.     clear_error();
  138.     clear_central_message();
  139.  
  140.     if (mailfile != NULL)
  141.       (void) fclose(mailfile);  /* close it first, to avoid too many open */
  142.  
  143.     if ((mailfile = fopen(cur_folder,"r")) == NULL)  {
  144.       if (errno != ENOENT ) { /* error on anything but file not exist */
  145.         err = errno;
  146.         Write_to_screen("\l\rfail on open in newmbox, open %s failed!!\l\r", 1,
  147.             cur_folder);
  148.         Write_to_screen("** %s - %s. **\l\r", 2,
  149.             error_name(err), error_description(err));
  150.         dprint(1, (debugfile, "fail on open in newbox, file %s!!\n",
  151.             cur_folder));
  152.         rm_temps_exit();
  153.       }
  154.       else {
  155.         mailfile_size = 0;         /* must non-existant folder */
  156.         message_count = 0;
  157.         selected = 0;
  158.       }
  159.     } else {                          /* folder exists, read headers */
  160.       read_headers(adds_only);
  161.     }
  162.  
  163.     if(!same_file)        /* limit mode off if this is a new file */
  164.       selected = 0;
  165.     if (!adds_only)        /* limit mode off if recreating headers */
  166.       selected = 0;        /* because we loose the 'Visible' flag */
  167.  
  168.     dprint(1, (debugfile,
  169.       "New folder %s type %s temp file %s (%s)\n", cur_folder, 
  170.       (folder_type == SPOOL ? "spool" : "non-spool"),
  171.       (*cur_tempfolder ? cur_tempfolder : "none"), "newmbox"));
  172.  
  173.     return(0);
  174. }
  175.  
  176. int
  177. get_folder_type(filename)
  178. char *filename;
  179. {
  180.     /** returns the type of mailfile filename is
  181.         NO_NAME = no name
  182.         SPOOL = consisting only of mailhome plus base file name
  183.             (no intervening directory name)
  184.         NON_SPOOL = a name that is not SPOOL type above
  185.      **/
  186.  
  187.     char *last_slash;
  188.  
  189.     /* if filename is null or is of zero length */
  190.     if((filename == NULL) || (*filename == '\0'))
  191.       return(NO_NAME);
  192.  
  193.     /* if filename begins with mailhome,
  194.      * and there is a slash in filename,
  195.      * and there is a filename after it (i.e. last slash is not last char),
  196.      * and the last character of mailhome is last slash in filename,
  197.      * it's a spool file .
  198.      */
  199.     if((first_word(filename, mailhome)) &&
  200.       ((last_slash = rindex(filename, '/')) != NULL) &&
  201.       (*(last_slash+1) != '\0') &&
  202.       (filename + strlen(mailhome) - 1 == last_slash))
  203.         return(SPOOL);
  204.     /* if file name == default mailbox, its a spool file also
  205.      * even if its not in the spool directory. (SVR4)
  206.      */
  207.     if (strcmp(filename, defaultfile) == 0)
  208.         return(SPOOL);
  209.  
  210.     return(NON_SPOOL);
  211. }
  212.  
  213. mk_temp_mail_fn(tempfn, mbox)
  214. char *tempfn, *mbox;
  215. {
  216.     /** create in tempfn the name of the temp file corresponding to
  217.         mailfile mbox. Mbox is presumed to be a file in mailhome;
  218.         Strangeness may result if it is not!
  219.      **/
  220.  
  221.     char *cp;
  222.  
  223.     sprintf(tempfn, "%s%s", default_temp, temp_mbox);
  224.     if((cp = rindex(mbox, '/')) != NULL) {
  225.       cp++;
  226.       if (strcmp(cp, "mbox") == 0 || strcmp(cp, "mailbox") == 0 ||
  227.         strcmp(cp, "inbox") == 0 || *cp == '.')
  228.         strcat(tempfn, username);
  229.       else
  230.         strcat(tempfn, cp);
  231.     }
  232. }
  233.  
  234. int
  235. read_headers(add_new_only)
  236. int add_new_only;
  237. {
  238.     /** Reads the headers into the headers[] array and leaves the
  239.         file rewound for further I/O requests.   If the file being
  240.         read is a mail spool file (ie incoming) then it is copied to
  241.         a temp file and closed, to allow more mail to arrive during 
  242.         the elm session.  If 'add_new_only' is set, the program will copy
  243.         the status flags from the previous data structure to the new 
  244.         one if possible and only read in newly added messages.
  245.     **/
  246.  
  247.     FILE *temp;
  248.     struct header_rec *current_header = NULL;
  249.     char buffer[LONG_STRING], *c;
  250.     long fbytes = 0L, line_bytes = 0L;
  251.     register int line = 0, count = 0, another_count,
  252.       subj = 0, copyit = 0, in_header = 0;
  253.     int count_x, count_y = 17, err;
  254.     int in_to_list = FALSE, forwarding_mail = FALSE, first_line = TRUE;
  255.  
  256.     static int first_read = 0;
  257. #ifdef MMDF
  258.         int newheader = 0;
  259. #endif /* MMDF */
  260.  
  261.     if (folder_type == SPOOL) {
  262.       lock(INCOMING);    /* ensure no mail arrives while we do this! */
  263.       if (! add_new_only) {
  264.         if (access(cur_tempfolder, ACCESS_EXISTS) != -1) {
  265.           /* Hey!  What the hell is this?  The temp file already exists? */
  266.           /* Looks like a potential clash of processes on the same file! */
  267.           unlock();                     /* so remove lock file! */
  268.           error("What's this?  The temp folder already exists??");
  269.           sleep(2);
  270.           error("Ahhhh... I give up.");
  271.           silently_exit();    /* leave without tampering with it! */
  272.         }
  273.         if ((temp = fopen(cur_tempfolder,"w")) == NULL) {
  274.          err = errno;
  275.          unlock();    /* remove lock file! */
  276.          Raw(OFF);
  277.          Write_to_screen(
  278.              "\l\rCouldn't open file %s for use as temp file.\l\r",
  279.              1, cur_tempfolder);
  280.          Write_to_screen("** %s - %s. **\l\r", 2,
  281.              error_name(err), error_description(err));
  282.          dprint(1, (debugfile,
  283.                 "Error: Couldn't open file %s as temp mbox.  errno %s (%s)\n",
  284.              cur_tempfolder, error_name(err), "read_headers"));
  285.          fclose(temp);
  286.          fclose(mailfile);
  287.          rm_temps_exit();
  288.         }
  289.        copyit++;
  290.        ochown(cur_tempfolder, userid, groupid);
  291.        chmod(cur_tempfolder, (unsigned short) 0x03); /* shut off file for other people! */
  292.      }
  293.      else {
  294.        if ((temp = fopen(cur_tempfolder,"a")) == NULL) {
  295.          err = errno;
  296.          unlock();    /* remove lock file! */
  297.          Raw(OFF);
  298.          Write_to_screen(
  299.              "\l\rCouldn't reopen file %s for use as temp file.\l\r",
  300.              1, cur_tempfolder);
  301.          Write_to_screen("** %s - %s. **\l\r", 2,
  302.              error_name(err), error_description(err));
  303.          dprint(1, (debugfile, 
  304.                 "Error: Couldn't reopen file %s as temp mbox.  errno %s (%s)\n",
  305.              cur_tempfolder, error_name(err), "read_headers"));
  306.          fclose(temp);
  307.          fclose(mailfile);
  308.          rm_temps_exit();
  309.         }
  310.        copyit++;
  311.       }
  312.     }
  313.  
  314.     if (! first_read++) {
  315.       ClearLine(LINES-1);
  316.       ClearLine(LINES);
  317.       if (add_new_only)
  318.         PutLine2(LINES, 0, "Reading in %s, message: %d", cur_folder, 
  319.              message_count);
  320.       else
  321.         PutLine1(LINES, 0, "Reading in %s, message: 0", cur_folder);
  322.       count_x = LINES;
  323.           count_y = 22 + strlen(cur_folder);
  324.     }
  325.     else {
  326.       count_x = LINES-2;
  327.       PutLine0(LINES-2, 0, "Reading message: 0");
  328.     }
  329.  
  330.     if (add_new_only) {
  331.        if (fseek(mailfile, mailfile_size, 0) == -1) {
  332.          err = errno;
  333.          Write_to_screen(
  334.         "\l\rCouldn't seek to %ld (end of folder) in %s!\l\r", 2,
  335.              mailfile_size, cur_folder);    
  336.          Write_to_screen("** %s - %s. **\l\r", 2,
  337.              error_name(err), error_description(err));
  338.          dprint(1, (debugfile,
  339.      "Error: Couldn't seek to end of folder %s: (offset %ld) Errno %s (%s)\n",
  340.             cur_folder, mailfile_size, error_name(err), "read_headers"));
  341.          emergency_exit();
  342.        }
  343.        count = message_count;        /* next available  */
  344.        fbytes = mailfile_size;        /* start correctly */
  345.     }
  346.  
  347.     /** find the size of the folder then unlock the file **/
  348.  
  349.     mailfile_size = bytes(cur_folder);
  350.     unlock();
  351.  
  352.     /** now let's copy it all across accordingly... **/
  353.  
  354.     while (fbytes < mailfile_size) {
  355.  
  356.       if (fgets(buffer, LONG_STRING, mailfile) == NULL) break;
  357.  
  358.       if (copyit)
  359.         if (fputs(buffer, temp) == EOF) {
  360.         err = errno;
  361.         Write_to_screen("\l\rWrite to tempfile %s failed!!\l\r", 1,
  362.                 cur_tempfolder);
  363.         Write_to_screen("** %s - %s. **\l\r", 2,
  364.                 error_name(err), error_description(err));
  365.         dprint(1, (debugfile, "Can't write to tempfile %s!!\n",
  366.                cur_tempfolder));
  367.         fclose(temp);
  368.         fclose(mailfile);
  369.         rm_temps_exit();
  370.         }
  371.       line_bytes = (long) strlen(buffer); 
  372.  
  373.       /* Fix below to increment line count ONLY if we got a full line.
  374.        * Input lines longer than the fgets buffer size would
  375.        * get counted each time a subsequent part of them was
  376.        * read in. This meant that when the faulty line count was used
  377.        * to display the message, part of the next message
  378.        * was displayed at the end of the message.
  379.        */
  380.       if(buffer[strlen(buffer)-1] == '\n') line++;
  381.  
  382.       if (fbytes == 0L || first_line) {     /* first line of file... */    
  383.         if (folder_type == SPOOL) {
  384.           if (first_word(buffer, "Forward to ")) {
  385.             set_central_message("Mail being forwarded to %s", 
  386.                    (char *) (buffer + 11));
  387.             forwarding_mail = TRUE;
  388.           }
  389.         }
  390.  
  391.         /** flush leading blank lines before next test... **/
  392.         if (strlen(buffer) == 1) {
  393.           fbytes++;
  394.           continue;    
  395.         }
  396.         else
  397.           first_line = FALSE;
  398.               
  399.         if (! first_word(buffer, "From ") && !forwarding_mail) {
  400.  
  401.           PutLine0(LINES, 0,
  402.           "\l\rFolder is corrupt!!  I can't read it!!\l\r\l\r");
  403.           fflush(stderr);
  404.           dprint(1, (debugfile, 
  405.                "\n\n**** First mail header is corrupt!! ****\n\n"));
  406.           dprint(1, (debugfile, "Line is;\n\t%s\n\n", buffer));
  407.               mail_only++;    /* to avoid leave() cursor motion */
  408.           fclose(temp);
  409.           fclose(mailfile);
  410.               leave();
  411.         }
  412.       }
  413.  
  414.       if (first_word(buffer,"From ")) {
  415.  
  416.         /** allocate new header pointers, if needed... **/
  417.  
  418.         if (count >= max_headers) {
  419.           struct header_rec **new_headers;
  420.           int new_max;
  421.  
  422.           new_max = max_headers + KLICK;
  423.           if (max_headers == 0) {
  424.         new_headers = (struct header_rec **)
  425.           malloc(new_max * sizeof(struct header_rec *));
  426.           }
  427.           else {
  428.         new_headers = (struct header_rec **)
  429.           realloc(headers, new_max * sizeof(struct header_rec *));
  430.           }
  431.           if (new_headers == NULL) {
  432.             error1(
  433.       "\l\r\l\rCouldn't allocate enough memory! Message #%d.\l\r\l\r",
  434.             count);
  435.         fclose(temp);
  436.         fclose(mailfile);
  437.             leave();
  438.           }
  439.           headers = new_headers;
  440.           while (max_headers < new_max)
  441.         headers[max_headers++] = NULL;
  442.         }
  443.           
  444.         /** allocate new header structure, if needed... **/
  445.  
  446.         if (headers[count] == NULL) {
  447.           struct header_rec *h;
  448.  
  449.           if ((h = (struct header_rec *)
  450.             malloc(sizeof(struct header_rec))) == NULL) {
  451.             error1(
  452.       "\l\r\l\rCouldn't allocate enough memory! Message #%d.\l\r\l\r",
  453.             count);
  454.         fclose(temp);
  455.         fclose(mailfile);
  456.             leave();
  457.           }
  458.           headers[count] = h;
  459.         }
  460.  
  461.         if (real_from(buffer, headers[count])) {
  462.           current_header = headers[count];
  463.  
  464.           current_header->offset = (long) fbytes;
  465.           current_header->index_number = count+1;
  466.           /* set default status - always 'visible'  - and
  467.            * if a spool file, presume 'new', otherwise
  468.            * 'read', for the time being until overridden
  469.            * by a Status: header.
  470.            * We presume 'read' for nonspool mailfile messages
  471.            * to be compatible messages stored with older versions of elm,
  472.            * which didn't support a Status: header.
  473.            */
  474.           if(folder_type == SPOOL)
  475.         current_header->status = VISIBLE | NEW | UNREAD;
  476.           else
  477.         current_header->status = VISIBLE;
  478.  
  479.           strcpy(current_header->subject, "");    /* clear subj    */
  480.           strcpy(current_header->to, "");        /* clear to    */
  481.           strcpy(current_header->mailx_status, "");    /* clear status flags */
  482.           strcpy(current_header->messageid, "<no.id>"); /* set no id into message id */
  483.           current_header->encrypted = 0;        /* clear encrypted */
  484.           current_header->exit_disposition = UNSET;
  485.           current_header->status_chgd = FALSE;
  486.  
  487.           /* Set the number of lines for the _preceding_ message,
  488.            * but only if there was a preceding message and
  489.            * only if it wasn't calculated already. It would
  490.            * have been calculated already if we are only
  491.            * reading headers of new messages that have just arrived,
  492.            * and the preceding message was one of the old ones.
  493.            */
  494.           if ((count) && (!add_new_only || count > message_count))
  495.             headers[count-1]->lines = line;
  496.  
  497.           count++;
  498.           subj = 0;
  499.           line = 0;
  500.           in_header = 1;
  501.           PutLine1(count_x, count_y, "%d", count);
  502.  
  503.         } else if (count == 0) {
  504.           /* if this is the first "From" in file but the "From" line is
  505.            * not of the proper format, we've got a corrupt folder.
  506.            */
  507.           PutLine0(LINES, 0,
  508.           "\l\rFolder is corrupt!!  I can't read it!!\l\r\l\r");
  509.           fflush(stderr);
  510.           dprint(1, (debugfile, 
  511.                "\n\n**** First mail header is corrupt!! ****\n\n"));
  512.           dprint(1, (debugfile, "Line is;\n\t%s\n\n", buffer));
  513.               mail_only++;    /* to avoid leave() cursor motion */
  514.               fclose(temp);
  515.               fclose(mailfile);
  516.               leave();
  517.         }
  518.       }
  519.       else if (in_header) {
  520.  
  521.         if (first_word(buffer,">From:"))
  522.           parse_arpa_who(buffer, current_header->from, FALSE);
  523.         else if (first_word(buffer,">From")) 
  524.           forwarded(buffer, current_header); /* return address */
  525.         else if (first_word(buffer,"Subject:") ||
  526.              first_word(buffer,"Subj:") ||
  527.              first_word(buffer,"Re:")) {
  528.           if (! subj++) {
  529.             remove_first_word(buffer);
  530.             copy_sans_escape(current_header->subject, buffer, STRING);
  531.         remove_possible_trailing_spaces(current_header->subject);
  532.           }
  533.         }
  534.         else if (first_word(buffer,"From:")) {
  535.  
  536.           parse_arpa_who(buffer, current_header->from, FALSE);
  537.  
  538.         }
  539.         else if (first_word(buffer, "Message-Id:") ||
  540.              first_word(buffer, "Message-ID:")) {
  541.           buffer[strlen(buffer)-1] = '\0';
  542.           strcpy(current_header->messageid,
  543.              (char *) buffer + 12);
  544.         }
  545.  
  546.         else if (first_word(buffer, "Expires:"))
  547.           process_expiration_date((char *) buffer + 9, 
  548.                       &(current_header->status));
  549.         
  550.         /** when it was sent... **/
  551.  
  552.         else if (first_word(buffer, "Date:")) {
  553.           dprint(1, (debugfile, 
  554.                "\n\n**** Calling parse_arpa_date ****\n\n"));
  555.           remove_first_word(buffer);
  556.           parse_arpa_date(buffer, current_header);
  557.         }
  558.  
  559.         /** some status things about the message... **/
  560.  
  561.         else if (first_word(buffer, "Priority:") || 
  562.              first_word(buffer, "Importance: 2"))
  563.           current_header->status |= URGENT;
  564.         else if (first_word(buffer, "Sensitivity: 2"))
  565.           current_header->status |= PRIVATE;
  566.         else if (first_word(buffer, "Sensitivity: 3"))
  567.           current_header->status |= CONFIDENTIAL;
  568.         else if (first_word(buffer, "Content-Type: mailform"))
  569.           current_header->status |= FORM_LETTER;
  570.         else if (first_word(buffer, "Action:"))
  571.           current_header->status |= ACTION;
  572.  
  573.         /** next let's see if it's to us or not... **/
  574.  
  575.         else if (first_word(buffer, "To:")) {
  576.           in_to_list = TRUE;
  577.           current_header->to[0] = '\0';    /* nothing yet */
  578.           figure_out_addressee((char *) buffer +3, 
  579.                    current_header->to);
  580.         }
  581.         else if (first_word(buffer, "Status:")) {
  582.           remove_first_word(buffer);
  583.           strncpy(current_header->mailx_status, buffer, WLEN-1);
  584.           current_header->mailx_status[WLEN-1] ='\0';
  585.  
  586.           c = index(current_header->mailx_status, '\n');
  587.           if (c != NULL)
  588.         *c = '\0';
  589.           remove_possible_trailing_spaces(current_header->mailx_status);
  590.  
  591.           /* Okay readjust the status. If there's an 'R', message
  592.            * is read; if there is no 'R' but there is an 'O', message
  593.            * is unread. In any case it isn't new because a new message
  594.            * wouldn't have a Status: header.
  595.            */
  596.           if (index(current_header->mailx_status, 'R') != NULL)
  597.         current_header->status &= ~(NEW | UNREAD);
  598.           else if (index(current_header->mailx_status, 'O') != NULL) {
  599.         current_header->status &= ~NEW;
  600.         current_header->status |= UNREAD;
  601.           }
  602.         }
  603.  
  604.         else if (buffer[0] == LINE_FEED || buffer[0] == '\0') {
  605.           if (in_header) {
  606.             in_header = 0;    /* in body of message! */
  607.             fix_date(current_header);
  608.           }
  609.         }
  610.         else if (in_header) {
  611.            if ((!whitespace(buffer[0])) && index(buffer, ':') == NULL) {
  612.             in_header = 0;    /* in body of message! */
  613.             fix_date(current_header);
  614.           }
  615.         }
  616.         else if (in_to_list == TRUE) {
  617.           if (whitespace(buffer[0]))
  618.             figure_out_addressee(buffer, current_header->to);
  619.           else in_to_list = FALSE;
  620.         }
  621.       }
  622.       if (!in_header && first_word(buffer, START_ENCODE))
  623.         current_header->encrypted = 1;
  624.       fbytes += (long) line_bytes;
  625.     }
  626.  
  627.     if (count)
  628.       headers[count-1]->lines = line + 1;
  629.  
  630.     if (folder_type == SPOOL) {
  631.       unlock();    /* remove lock file! */
  632.       if ((ferror(mailfile)) || (fclose(mailfile) == EOF)) {
  633.           err = errno;
  634.           Write_to_screen("\l\rClose on folder %s failed!!\l\r", 1,
  635.                   cur_folder);
  636.           Write_to_screen("** %s - %s. **\l\r", 2,
  637.                   error_name(err), error_description(err));
  638.           dprint(1, (debugfile, "Can't close on folder %s!!\n",
  639.              cur_folder));
  640.           fclose(temp);
  641.           fclose(mailfile);
  642.           rm_temps_exit();
  643.       }
  644.       if ((ferror(temp)) || (fclose(temp) == EOF)) {
  645.           err = errno;
  646.           Write_to_screen("\l\rClose on tempfile %s failed!!\l\r", 1,
  647.                   cur_tempfolder);
  648.           Write_to_screen("** %s - %s. **\l\r", 2,
  649.                   error_name(err), error_description(err));
  650.           dprint(1, (debugfile, "Can't close on tempfile %s!!\n",
  651.              cur_tempfolder));
  652.           fclose(temp);
  653.           fclose(mailfile);
  654.           rm_temps_exit();
  655.       }
  656.       /* sanity check on append - is resulting temp file longer??? */
  657.       if ( bytes(cur_tempfolder) != mailfile_size) {
  658.          Write_to_screen(
  659.            "\l\rnewmbox - length of mbox. != spool mailbox length!!\l\r",
  660.         0);
  661.         dprint(0, (debugfile, "newmbox - mbox. != spool mail length"));
  662.         fclose(temp);
  663.         fclose(mailfile);
  664.         rm_temps_exit();
  665.       }
  666.       if ((mailfile = fopen(cur_tempfolder,"r")) == NULL) {
  667.         err = errno;
  668.         MoveCursor(LINES,0);
  669.         Raw(OFF);
  670.         Write_to_screen(
  671.            "\l\rAugh! Couldn't reopen %s as temp file.\l\r",
  672.                1, cur_tempfolder);
  673.         Write_to_screen("** %s - %s. **\l\r", 2, error_name(err),
  674.            error_description(err));
  675.         dprint(1, (debugfile,
  676.           "Error: Reopening %s as temp file failed!  errno %s (%s)\n",
  677.                cur_tempfolder, error_name(errno), "read_headers"));
  678.         fclose(temp);
  679.         fclose(mailfile);
  680.         leave();
  681.       }
  682.     }
  683.     else 
  684.           rewind(mailfile);
  685.  
  686.     /* Sort folder *before* we establish the current message, so that
  687.      * the current message is based on the post-sort order.
  688.      * Note that we have to set the global variable message_count
  689.      * before the sort for the sort to correctly keep the correct
  690.      * current message if we are only adding new messages here. */
  691.  
  692.     message_count = count;
  693.     sort_mailbox(count, 1);
  694.  
  695.     /* Now lets figure what the current message should be.
  696.      * If we are only reading in newly added messages from a mailfile
  697.      * that already had some messages, current should remain the same.
  698.      * If we have a folder of no messages, current should be zero.
  699.      * Otherwise, if we have point_to_new on then the current message
  700.      * is the first message of status NEW if there is one.
  701.      * If we don't have point_to_new on or if there are no messages of
  702.      * of status NEW, then the current message is the first message.
  703.      */
  704.     if(!(add_new_only && current != 0)) {
  705.       if(count == 0)
  706.         current = 0;
  707.       else {
  708.         current = 1;
  709.         if (point_to_new) {
  710.           for(another_count = 0; another_count < count; another_count++) {
  711.         if(ison(headers[another_count]->status, NEW)) {
  712.           current = another_count+1;
  713.           break;    /* first one found give up */
  714.         }
  715.           }
  716.         }
  717.       }
  718.     }
  719.         get_page(current);
  720.     return(count);
  721. }
  722.