snprintf (buf, sizeof (buf), "%s LOGIN %s %s\r\n", seq, user, pass); if (imap_exec (buf, sizeof (buf), ctx, seq, buf) != 0) {  FREE (&ImapUser); FREE (&ImapPass); imap_error ("imap_open_mailbox()", buf); return (-1); }  if (!ImapUser) ImapUser = safe_strdup (user); if (!ImapPass) ImapPass = safe_strdup (pass);  mutt_message ("Selecting %s...", mailbox); imap_make_sequence (seq, sizeof (seq), ctx); snprintf (bufout, sizeof (bufout), "%s SELECT %s\r\n", seq, mailbox); imap_write (ctx->fd, bufout);
snprintf (bufout, sizeof (bufout), "%s SELECT %s\r\n", seq, CTX_DATA->mailbox); imap_write (CTX_DATA->conn, bufout);
int imap_open_mailbox (CONTEXT *ctx){  struct sockaddr_in sin;  struct hostent *he;  char buf[LONG_STRING];  char bufout[LONG_STRING];  char host[SHORT_STRING];  char user[SHORT_STRING];  char pass[SHORT_STRING];  char mailbox[_POSIX_PATH_MAX];  char seq[16];  int count = 0;  int n;  char *pc;  if (ctx->path[0] != '{')    return (-1);  for(n = 0, pc = ctx->path + 1; *pc && *pc != '}' && n < sizeof(host) - 1;       n++, pc++)    host[n++] = *pc++;  if(*pc != '}')    return -1;  host[n] = 0;  pc++;  strfcpy (mailbox, pc, sizeof (mailbox));  if (!ImapUser)  {    strfcpy (user, Username, sizeof (user));    if (mutt_get_field ("IMAP Username: ", user, sizeof (user), 0) != 0 ||        !user[0])    {      user[0] = 0;      return (-1);    }  }  else    strfcpy (user, ImapUser, sizeof (user));  if (!ImapPass)  {    pass[0]=0;    snprintf (buf, sizeof (buf), "Password for %s@%s: ", user, host);    if (mutt_get_field (buf, pass, sizeof (pass), M_PASS) != 0 ||        !pass[0])    {      return (-1);    }  }  else    strfcpy (pass, ImapPass, sizeof (pass));  memset (&sin, 0, sizeof (sin));  sin.sin_port = htons (IMAP_PORT);  sin.sin_family = AF_INET;  if ((he = gethostbyname (host)) == NULL)  {    mutt_perror (host);    return (-1);  }  memcpy (&sin.sin_addr, he->h_addr_list[0], he->h_length);  if ((ctx->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)  {    mutt_perror ("socket");    return (-1);  }  mutt_message ("Connecting to %s...", host);  if (connect (ctx->fd, (struct sockaddr *) &sin, sizeof (sin)) < 0)  {    mutt_perror ("connect");    close (ctx->fd);  }  if (imap_read_line_d (buf, sizeof (buf), ctx->fd) < 0)  {    close (ctx->fd);    return (-1);  }  if (strncmp ("* OK", buf, 4) != 0)  {    imap_error ("imap_open_mailbox()", buf);    close (ctx->fd);    return (-1);  }  /* create IMAP-specific state struct */  ctx->data = safe_malloc (sizeof (IMAP_DATA));  memset (ctx->data, 0, sizeof (IMAP_DATA));  mutt_message ("Logging in...");  imap_make_sequence (seq, sizeof (seq), ctx);  snprintf (buf, sizeof (buf), "%s LOGIN %s %s\r\n", seq, user, pass);  if (imap_exec (buf, sizeof (buf), ctx, seq, buf) != 0)  {    /* Most likely an invalid login; clear username and password for re-entry. */    FREE (&ImapUser);    FREE (&ImapPass);    imap_error ("imap_open_mailbox()", buf);    return (-1);  }  /* If they have a successful login, we may as well cache the user/password. */  if (!ImapUser)    ImapUser = safe_strdup (user);  if (!ImapPass)    ImapPass = safe_strdup (pass);  mutt_message ("Selecting %s...", mailbox);  imap_make_sequence (seq, sizeof (seq), ctx);  snprintf (bufout, sizeof (bufout), "%s SELECT %s\r\n", seq, mailbox);  imap_write (ctx->fd, bufout);  do  {    if (imap_read_line_d (buf, sizeof (buf), ctx->fd) < 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 (strncasecmp ("EXISTS", pc, 6) == 0)	  count = n;      }      else if (imap_handle_untagged (ctx, buf) != 0)	return (-1);    }  }  while (strncmp (seq, buf, strlen (seq)) != 0);  ctx->hdrmax = count;  ctx->hdrs = safe_malloc (count * sizeof (HEADER *));  ctx->v2r = safe_malloc (count * sizeof (int));  for (ctx->msgcount = 0; ctx->msgcount < count; )  {    snprintf (buf, sizeof (buf), "Fetching message headers... [%d/%d]",       ctx->msgcount + 1, count);    mutt_message (buf);    ctx->hdrs[ctx->msgcount] = mutt_new_header ();    /* `count' can get modified if new mail arrives while fetching the     * header for this message     */    if (imap_read_header (ctx, ctx->msgcount) != 0)    {      mx_fastclose_mailbox (ctx);      return (-1);    }    mx_update_context (ctx); /* increments ->msgcount */    /* in case we get new mail while fetching the headers */    if (((IMAP_DATA *) ctx->data)->status == IMAP_NEW_MAIL)    {      count = ((IMAP_DATA *) ctx->data)->newMailCount;      while (count > ctx->hdrmax)	mx_alloc_memory (ctx);      ((IMAP_DATA *) ctx->data)->status = 0;    }  }  return 0;}