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