char host[SHORT_STRING];
int imap_open_mailbox (CONTEXT *ctx){  CONNECTION *conn;  IMAP_DATA *idata;  char buf[LONG_STRING];  char bufout[LONG_STRING];  char host[SHORT_STRING];  char seq[16];  char *pc = NULL;  int count = 0;  int n;  int port;  int socktype;  if (imap_parse_path (ctx->path, host, sizeof (host), &port, &socktype, &pc))  {    mutt_error ("%s is an invalid IMAP path", ctx->path);    return -1;  }  conn = mutt_socket_select_connection (host, port, 0);  idata = CONN_DATA;  if (!idata || (idata->state != IMAP_AUTHENTICATED))  {    if (!idata || (idata->state == IMAP_SELECTED) ||        (idata->state == IMAP_CONNECTED))    {      /* We need to create a new connection, the current one isn't useful */      idata = safe_calloc (1, sizeof (IMAP_DATA));      conn = mutt_socket_select_connection (host, port, socktype);      conn->data = idata;      idata->conn = conn;    }    if (imap_open_connection (idata, conn))      return (-1);  }  ctx->data = (void *) idata;  /* Clean up path and replace the one in the ctx */  imap_fix_path (idata, pc, buf, sizeof (buf));  FREE(&(idata->selected_mailbox));  idata->selected_mailbox = safe_strdup (buf);  imap_qualify_path (buf, sizeof (buf), host, port, idata->selected_mailbox,    NULL);  FREE (&(ctx->path));  ctx->path = safe_strdup (buf);  idata->selected_ctx = ctx;  /* clear status, ACL */  idata->status = 0;  memset (idata->rights, 0, (RIGHTSMAX+7)/8);  mutt_message (_("Selecting %s..."), idata->selected_mailbox);  imap_quote_string (buf, sizeof(buf), idata->selected_mailbox);  imap_make_sequence (seq, sizeof (seq));  snprintf (bufout, sizeof (bufout), "%s SELECT %s\r\n", seq, buf);  mutt_socket_write (conn, bufout);  idata->state = IMAP_SELECTED;  do  {    if (mutt_socket_read_line_d (buf, sizeof (buf), conn) < 0)      break;    if (buf[0] == '*')    {      pc = buf + 2;      if (isdigit (*pc))      {	char *pn = pc;	while (*pc && isdigit (*pc))	  pc++;	*pc++ = '\0';	n = atoi (pn);	SKIPWS (pc);	if (mutt_strncasecmp ("EXISTS", pc, 6) == 0)	  count = n;      }      /* Obtain list of available flags here, may be overridden by a       * PERMANENTFLAGS tag in the OK response */      else if (mutt_strncasecmp ("FLAGS", pc, 5) == 0)      {        /* don't override PERMANENTFLAGS */        if (!idata->flags)        {          dprint (2, (debugfile, "Getting mailbox FLAGS\n"));          if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)            return -1;        }      }      /* PERMANENTFLAGS are massaged to look like FLAGS, then override FLAGS */      else if (mutt_strncasecmp ("OK [PERMANENTFLAGS", pc, 18) == 0)      {        dprint (2, (debugfile,          "Getting mailbox PERMANENTFLAGS\n"));        /* safe to call on NULL */        mutt_free_list (&(idata->flags));	/* skip "OK [PERMANENT" so syntax is the same as FLAGS */        pc += 13;        if ((pc = imap_get_flags (&(idata->flags), pc)) == NULL)          return -1;      }      else if (imap_handle_untagged (idata, buf) != 0)	return (-1);    }  }  while (mutt_strncmp (seq, buf, mutt_strlen (seq)) != 0);  /* dump the mailbox flags we've found */  if (debuglevel > 2)  {    if (!idata->flags)      dprint (3, (debugfile, "No folder flags found\n"));    else    {      LIST* t = idata->flags;      dprint (3, (debugfile, "Mailbox flags: "));      t = t->next;      while (t)      {        dprint (3, (debugfile, "[%s] ", t->data));        t = t->next;      }      dprint (3, (debugfile, "\n"));    }  }  if (!imap_code (buf))  {    char *s;    s = imap_next_word (buf); /* skip seq */    s = imap_next_word (s); /* Skip response */    mutt_error (s);    idata->state = IMAP_AUTHENTICATED;    sleep (1);    return -1;  }  if (mutt_bit_isset (idata->capabilities, ACL))  {    if (imap_check_acl (idata))      return -1;  }  /* assume we have all rights if ACL is unavailable */  else  {    mutt_bit_set (idata->rights, IMAP_ACL_LOOKUP);    mutt_bit_set (idata->rights, IMAP_ACL_READ);    mutt_bit_set (idata->rights, IMAP_ACL_SEEN);    mutt_bit_set (idata->rights, IMAP_ACL_WRITE);    mutt_bit_set (idata->rights, IMAP_ACL_INSERT);    mutt_bit_set (idata->rights, IMAP_ACL_POST);    mutt_bit_set (idata->rights, IMAP_ACL_CREATE);    mutt_bit_set (idata->rights, IMAP_ACL_DELETE);  }  ctx->hdrmax = count;  ctx->hdrs = safe_malloc (count * sizeof (HEADER *));  ctx->v2r = safe_malloc (count * sizeof (int));  ctx->msgcount = 0;  count = imap_read_headers (ctx, 0, count - 1) + 1;  dprint (1, (debugfile, "imap_open_mailbox(): msgcount is %d\n", ctx->msgcount));  return 0;}