const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE IN-REPLY-TO REPLY-TO";
const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE IN-REPLY-TO REPLY-TO LINES";
int imap_read_headers (CONTEXT *ctx, int msgbegin, int msgend){  char buf[LONG_STRING],fetchbuf[LONG_STRING];  char hdrreq[STRING];  FILE *fp;  char tempfile[_POSIX_PATH_MAX];  char seq[8];  char *pc,*fpc,*hdr;  long ploc;  long bytes = 0;  int msgno,fetchlast;  IMAP_HEADER *h, *h0;  const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE IN-REPLY-TO REPLY-TO";  int using_body_peek = 0;  fetchlast = 0;  /* define search string */  if (mutt_bit_isset(CTX_DATA->capabilities,IMAP4REV1))  {    snprintf(hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s)]",       want_headers);     using_body_peek = 1;  }   else if (mutt_bit_isset(CTX_DATA->capabilities,IMAP4))  {    snprintf(hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s)",       want_headers);  }  else  {	/* Unable to fetch headers for lower versions */    mutt_error _("Unable to fetch headers from this IMAP server version.");    sleep (1);	/* pause a moment to let the user see the error */    return -1;  }  /*   * We now download all of the headers into one file. This should be   * faster on most systems.   */  mutt_mktemp (tempfile);  if (!(fp = safe_fopen (tempfile, "w+")))    return -1;  h = msg_new_header ();  h0 = h;  for (msgno = msgbegin; msgno <= msgend ; msgno++)  {    snprintf (buf, sizeof (buf), _("Fetching message headers... [%d/%d]"),       msgno + 1, msgend + 1);    mutt_message (buf);    if (msgno + 1 > fetchlast)    {      imap_make_sequence (seq, sizeof (seq));      /*       * Make one request for everything. This makes fetching headers an       * order of magnitude faster if you have a large mailbox.       *       * If we get more messages while doing this, we make another       * request for all the new messages.       */      snprintf (buf, sizeof (buf), 	"%s FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)\r\n", 	seq, msgno + 1, msgend + 1, hdrreq);      mutt_socket_write (CTX_DATA->conn, buf);      fetchlast = msgend + 1;    }    do    {      if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0)        return -1;      if (buf[0] == '*')      {        pc = buf;        pc = imap_next_word (pc);	h->number = atoi (pc);	dprint (1, (debugfile, "fetching message %d\n", h->number));        pc = imap_next_word (pc);        if (mutt_strncasecmp ("FETCH", pc, 5) == 0)        {          if (!(pc = strchr (pc, '(')))          {            imap_error ("imap_read_headers()", buf);            return (-1);          }          pc++;          fpc=fetchbuf;          while (*pc != '\0' && *pc != ')')          {	    if (using_body_peek) 	      hdr=strstr(pc,"BODY");	    else	      hdr=strstr(pc,"RFC822.HEADER");	    if (!hdr)            {              imap_error ("imap_read_headers()", buf);              return -1;            }            strncpy(fpc,pc,hdr-pc);            fpc += hdr-pc;            *fpc = '\0';            pc=hdr;            /* get some number of bytes */	    if (imap_get_literal_count(buf, &bytes) < 0)            {              imap_error ("imap_read_headers()", buf);              return -1;            }            imap_read_bytes (fp, CTX_DATA->conn, bytes);            if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0)              return -1;            pc = buf;          }        }        else if (imap_handle_untagged (CTX_DATA, buf) != 0)          return -1;      }    }    while ((msgno + 1) >= fetchlast && mutt_strncmp (seq, buf, SEQLEN) != 0);    h->content_length = -bytes;    if (msg_parse_fetch (h, fetchbuf) == -1)      return -1;    /* subtract the header length; the total message size will be       added to this */    /* in case we get new mail while fetching the headers */    if (((IMAP_DATA *) ctx->data)->status == IMAP_NEW_MAIL)    {      msgend = ((IMAP_DATA *) ctx->data)->newMailCount - 1;      while ((msgend + 1) > ctx->hdrmax)        mx_alloc_memory (ctx);      ((IMAP_DATA *) ctx->data)->status = 0;    }    h->next = msg_new_header ();    h = h->next;  }  rewind(fp);  h = h0;  /*   * Now that we have all the header information, we can tell mutt about   * it.   */  ploc=0;  for (msgno = msgbegin; msgno <= msgend;msgno++)  {    ctx->hdrs[ctx->msgcount] = mutt_new_header ();    ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;    ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno], 0, 0);    ploc=ftell(fp);    ctx->hdrs[msgno]->read = h->read;    ctx->hdrs[msgno]->old = h->old;    ctx->hdrs[msgno]->deleted = h->deleted;    ctx->hdrs[msgno]->flagged = h->flagged;    ctx->hdrs[msgno]->replied = h->replied;    ctx->hdrs[msgno]->changed = h->changed;    ctx->hdrs[msgno]->received = h->received;    ctx->hdrs[msgno]->content->length = h->content_length;    ctx->hdrs[msgno]->data = (void *) (h->data);    mx_update_context (ctx); /* increments ->msgcount */    h0 = h;    h = h->next;    /* hdata is freed later */    safe_free ((void **) &h0);  }  fclose(fp);  unlink(tempfile);  return (msgend);}