/**
   * Selects the folder for use. Before performing any operations on this folder, it must be
   * selected.
   */
  private void doSelect() throws IOException, MessagingException {
    List<ImapResponse> responses =
        mConnection.executeSimpleCommand(
            String.format(
                ImapConstants.SELECT + " \"%s\"",
                ImapStore.encodeFolderName(mName, mStore.mPathPrefix)));

    // Assume the folder is opened read-write; unless we are notified otherwise
    mMode = OpenMode.READ_WRITE;
    int messageCount = -1;
    for (ImapResponse response : responses) {
      if (response.isDataResponse(1, ImapConstants.EXISTS)) {
        messageCount = response.getStringOrEmpty(0).getNumberOrZero();
      } else if (response.isOk()) {
        final ImapString responseCode = response.getResponseCodeOrEmpty();
        if (responseCode.is(ImapConstants.READ_ONLY)) {
          mMode = OpenMode.READ_ONLY;
        } else if (responseCode.is(ImapConstants.READ_WRITE)) {
          mMode = OpenMode.READ_WRITE;
        }
      } else if (response.isTagged()) { // Not OK
        throw new MessagingException(
            "Can't open mailbox: " + response.getStatusResponseTextOrEmpty());
      }
    }
    if (messageCount == -1) {
      throw new MessagingException("Did not find message count during select");
    }
    mMessageCount = messageCount;
    mExists = true;
  }
 @Override
 public final boolean equalsForTest(ImapElement that) {
   if (!super.equalsForTest(that)) {
     return false;
   }
   ImapString thatString = (ImapString) that;
   return getString().equals(thatString.getString());
 }
 String[] getSearchUids(List<ImapResponse> responses) {
   // S: * SEARCH 2 3 6
   final ArrayList<String> uids = new ArrayList<String>();
   for (ImapResponse response : responses) {
     if (!response.isDataResponse(0, ImapConstants.SEARCH)) {
       continue;
     }
     // Found SEARCH response data
     for (int i = 1; i < response.size(); i++) {
       ImapString s = response.getStringOrEmpty(i);
       if (s.isString()) {
         uids.add(s.getString());
       }
     }
   }
   return uids.toArray(Utility.EMPTY_STRINGS);
 }
  // We populate Message using the ImapList parsed by BODYSTRUCTURE returned from server.
  private static void parseBodyStructure(ImapList bs, Part part, String id)
      throws MessagingException {
    if (bs.getElementOrNone(0).isList()) {
      /*
       * This is a multipart/*
       */
      MimeMultipart mp = new MimeMultipart();
      for (int i = 0, count = bs.size(); i < count; i++) {
        ImapElement e = bs.getElementOrNone(i);
        if (e.isList()) {
          /*
           * For each part in the message we're going to add a new BodyPart and parse
           * into it.
           */
          MimeBodyPart bp = new MimeBodyPart();
          if (id.equals(ImapConstants.TEXT)) {
            parseBodyStructure(bs.getListOrEmpty(i), bp, Integer.toString(i + 1));

          } else {
            parseBodyStructure(bs.getListOrEmpty(i), bp, id + "." + (i + 1));
          }
          mp.addBodyPart(bp);

        } else {
          if (e.isString()) {
            mp.setSubType(bs.getStringOrEmpty(i).getString().toLowerCase());
          }
          break; // Ignore the rest of the list.
        }
      }
      part.setBody(mp);
    } else {
      /*
       * This is a body. We need to add as much information as we can find out about
       * it to the Part.
       */

      /*
      body type
      body subtype
      body parameter parenthesized list
      body id
      body description
      body encoding
      body size
      */

      final ImapString type = bs.getStringOrEmpty(0);
      final ImapString subType = bs.getStringOrEmpty(1);
      final String mimeType = (type.getString() + "/" + subType.getString()).toLowerCase();

      final ImapList bodyParams = bs.getListOrEmpty(2);
      final ImapString cid = bs.getStringOrEmpty(3);
      final ImapString encoding = bs.getStringOrEmpty(5);
      final int size = bs.getStringOrEmpty(6).getNumberOrZero();

      if (MimeUtility.mimeTypeMatches(mimeType, MimeUtility.MIME_TYPE_RFC822)) {
        // A body type of type MESSAGE and subtype RFC822
        // contains, immediately after the basic fields, the
        // envelope structure, body structure, and size in
        // text lines of the encapsulated message.
        // [MESSAGE, RFC822, [NAME, filename.eml], NIL, NIL, 7BIT, 5974, NIL,
        //     [INLINE, [FILENAME*0, Fwd: Xxx..., FILENAME*1, filename.eml]], NIL]
        /*
         * This will be caught by fetch and handled appropriately.
         */
        //                throw new MessagingException("BODYSTRUCTURE " +
        // MimeUtility.MIME_TYPE_RFC822
        //                        + " not yet supported.");
        Logging.w("BODYSTRUCTURE " + MimeUtility.MIME_TYPE_RFC822 + " not yet supported.");
        return;
      }

      /*
       * Set the content type with as much information as we know right now.
       */
      final StringBuilder contentType = new StringBuilder(mimeType);

      /*
       * If there are body params we might be able to get some more information out
       * of them.
       */
      for (int i = 1, count = bodyParams.size(); i < count; i += 2) {

        // TODO We need to convert " into %22, but
        // because MimeUtility.getHeaderParameter doesn't recognize it,
        // we can't fix it for now.
        contentType.append(
            String.format(
                ";\n %s=\"%s\"",
                bodyParams.getStringOrEmpty(i - 1).getString(),
                bodyParams.getStringOrEmpty(i).getString()));
      }

      part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType.toString());

      // Extension items
      final ImapList bodyDisposition;

      if (type.is(ImapConstants.TEXT) && bs.getElementOrNone(9).isList()) {
        // If media-type is TEXT, 9th element might be: [body-fld-lines] := number
        // So, if it's not a list, use 10th element.
        // (Couldn't find evidence in the RFC if it's ALWAYS 10th element.)
        bodyDisposition = bs.getListOrEmpty(9);
      } else {
        bodyDisposition = bs.getListOrEmpty(8);
      }

      final StringBuilder contentDisposition = new StringBuilder();

      if (bodyDisposition.size() > 0) {
        final String bodyDisposition0Str =
            bodyDisposition.getStringOrEmpty(0).getString().toLowerCase();
        if (!TextUtils.isEmpty(bodyDisposition0Str)) {
          contentDisposition.append(bodyDisposition0Str);
        }

        final ImapList bodyDispositionParams = bodyDisposition.getListOrEmpty(1);
        if (!bodyDispositionParams.isEmpty()) {
          /*
           * If there is body disposition information we can pull some more
           * information about the attachment out.
           */
          for (int i = 1, count = bodyDispositionParams.size(); i < count; i += 2) {

            // TODO We need to convert " into %22.  See above.
            contentDisposition.append(
                String.format(
                    ";\n %s=\"%s\"",
                    bodyDispositionParams.getStringOrEmpty(i - 1).getString().toLowerCase(),
                    bodyDispositionParams.getStringOrEmpty(i).getString()));
          }
        }
      }

      if ((size > 0)
          && (MimeUtility.getHeaderParameter(contentDisposition.toString(), "size") == null)) {
        // If the encoding is base64 and no Content-Disposition size fetched, then recounting the
        // actual size: the encoded size multiplied by a fixed ratio
        if (ENC_BASE64.equals(encoding.getString())) {
          contentDisposition.append(String.format(";\n size=%d", (int) (size * BASE64_RATIO)));
        } else {
          contentDisposition.append(String.format(";\n size=%d", size));
        }
      }

      if (contentDisposition.length() > 0) {
        /*
         * Set the content disposition containing at least the size. Attachment
         * handling code will use this down the road.
         */
        part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, contentDisposition.toString());
      }

      /*
       * Set the Content-Transfer-Encoding header. Attachment code will use this
       * to parse the body.
       */
      if (!encoding.isEmpty()) {
        part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding.getString());
      }

      /*
       * Set the Content-ID header.
       */
      if (!cid.isEmpty()) {
        part.setHeader(MimeHeader.HEADER_CONTENT_ID, cid.getString());
      }

      if (size > 0) {
        if (part instanceof ImapMessage) {
          ((ImapMessage) part).setSize(size);
        } else if (part instanceof MimeBodyPart) {
          ((MimeBodyPart) part).setSize(size);
        } else {
          throw new MessagingException("Unknown part type " + part.toString());
        }
      }
      part.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, id);
    }
  }
  public void fetchInternal(Message[] messages, FetchProfile fp, MessageRetrievalListener listener)
      throws MessagingException {
    if (messages.length == 0) {
      return;
    }
    checkOpen();
    HashMap<String, Message> messageMap = new HashMap<String, Message>();
    for (Message m : messages) {
      messageMap.put(m.getUid(), m);
    }

    /*
     * Figure out what command we are going to run:
     * FLAGS     - UID FETCH (FLAGS)
     * ENVELOPE  - UID FETCH (INTERNALDATE UID RFC822.SIZE FLAGS BODY.PEEK[
     *                            HEADER.FIELDS (date subject from content-type to cc)])
     * STRUCTURE - UID FETCH (BODYSTRUCTURE)
     * BODY_SANE - UID FETCH (BODY.PEEK[]<0.N>) where N = max bytes returned
     * BODY      - UID FETCH (BODY.PEEK[])
     * Part      - UID FETCH (BODY.PEEK[ID]) where ID = mime part ID
     */

    final LinkedHashSet<String> fetchFields = new LinkedHashSet<String>();

    fetchFields.add(ImapConstants.UID);
    if (fp.contains(FetchProfile.Item.FLAGS)) {
      fetchFields.add(ImapConstants.FLAGS);
    }
    if (fp.contains(FetchProfile.Item.ENVELOPE)) {
      fetchFields.add(ImapConstants.INTERNALDATE);
      fetchFields.add(ImapConstants.RFC822_SIZE);
      fetchFields.add(ImapConstants.FETCH_FIELD_HEADERS);
    }
    if (fp.contains(FetchProfile.Item.STRUCTURE)) {
      fetchFields.add(ImapConstants.BODYSTRUCTURE);
    }

    if (fp.contains(FetchProfile.Item.BODY_SANE)) {
      fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK_SANE);
    }
    if (fp.contains(FetchProfile.Item.BODY)) {
      fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK);
    }

    final Part fetchPart = fp.getFirstPart();
    if (fetchPart != null) {
      String[] partIds = fetchPart.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
      if (partIds != null) {
        fetchFields.add(
            ImapConstants.FETCH_FIELD_BODY_PEEK_BARE
                + "["
                + partIds[0]
                + "]"
                + (mFetchSize > 0 ? String.format("<0.%d>", mFetchSize) : ""));
      }
    }

    try {
      mConnection.sendCommand(
          String.format(
              ImapConstants.UID_FETCH + " %s (%s)",
              ImapStore.joinMessageUids(messages),
              Utility.combine(fetchFields.toArray(new String[fetchFields.size()]), ' ')),
          false);
      ImapResponse response;
      int messageNumber = 0;
      do {
        response = null;
        try {
          response = mConnection.readResponse(listener);

          if (!response.isDataResponse(1, ImapConstants.FETCH)) {
            continue; // Ignore
          }
          final ImapList fetchList = response.getListOrEmpty(2);
          final String uid = fetchList.getKeyedStringOrEmpty(ImapConstants.UID).getString();
          if (TextUtils.isEmpty(uid)) continue;

          ImapMessage message = (ImapMessage) messageMap.get(uid);
          if (message == null) continue;

          if (fp.contains(FetchProfile.Item.FLAGS)) {
            final ImapList flags = fetchList.getKeyedListOrEmpty(ImapConstants.FLAGS);
            for (int i = 0, count = flags.size(); i < count; i++) {
              final ImapString flag = flags.getStringOrEmpty(i);
              if (flag.is(ImapConstants.FLAG_DELETED)) {
                message.setFlagInternal(Flag.DELETED, true);
              } else if (flag.is(ImapConstants.FLAG_ANSWERED)) {
                message.setFlagInternal(Flag.ANSWERED, true);
              } else if (flag.is(ImapConstants.FLAG_SEEN)) {
                message.setFlagInternal(Flag.SEEN, true);
              } else if (flag.is(ImapConstants.FLAG_FLAGGED)) {
                message.setFlagInternal(Flag.FLAGGED, true);
              }
            }
          }
          if (fp.contains(FetchProfile.Item.ENVELOPE)) {
            final Date internalDate =
                fetchList.getKeyedStringOrEmpty(ImapConstants.INTERNALDATE).getDateOrNull();
            final int size =
                fetchList.getKeyedStringOrEmpty(ImapConstants.RFC822_SIZE).getNumberOrZero();
            final InputStream header =
                fetchList
                    .getKeyedStringOrEmpty(ImapConstants.BODY_BRACKET_HEADER, true)
                    .getAsStream();

            message.setInternalDate(internalDate);
            message.setSize(size);
            message.parse(header);
          }
          if (fp.contains(FetchProfile.Item.STRUCTURE)) {
            ImapList bs = fetchList.getKeyedListOrEmpty(ImapConstants.BODYSTRUCTURE);
            if (!bs.isEmpty()) {
              try {
                parseBodyStructure(bs, message, ImapConstants.TEXT);
              } catch (MessagingException e) {
                if (Logging.LOGD) {
                  Log.v(Logging.LOG_TAG, "Error handling message", e);
                }
                message.setBody(null);
              }
            }
          }
          if (fp.contains(FetchProfile.Item.BODY) || fp.contains(FetchProfile.Item.BODY_SANE)) {
            // Body is keyed by "BODY[]...".
            // Previously used "BODY[..." but this can be confused with "BODY[HEADER..."
            // TODO Should we accept "RFC822" as well??
            ImapString body = fetchList.getKeyedStringOrEmpty("BODY[]", true);
            String bodyText = body.getString();
            InputStream bodyStream = body.getAsStream();
            message.parse(bodyStream);
          }
          if (fetchPart != null && fetchPart.getSize() > 0) {
            InputStream bodyStream = fetchList.getKeyedStringOrEmpty("BODY[", true).getAsStream();
            String contentType = fetchPart.getContentType();
            String[] encodingHeader =
                fetchPart.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING);
            String contentTransferEncoding = null;

            if (encodingHeader != null) {
              contentTransferEncoding = encodingHeader[0];
            }

            // TODO Don't create 2 temp files.
            // decodeBody creates BinaryTempFileBody, but we could avoid this
            // if we implement ImapStringBody.
            // (We'll need to share a temp file.  Protect it with a ref-count.)
            fetchPart.setBody(
                decodeBody(bodyStream, contentTransferEncoding, fetchPart.getSize(), listener));
          }

          if (listener != null) {
            listener.messageRetrieved(message);
          }
        } finally {
          destroyResponses();
        }
      } while (!response.isTagged());
    } catch (IOException ioe) {
      throw ioExceptionHandler(mConnection, ioe);
    }
  }