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