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

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