Example #1
0
  public ImapStore(
      StoreConfig storeConfig,
      TrustedSocketFactory trustedSocketFactory,
      ConnectivityManager connectivityManager,
      OAuth2TokenProvider oauthTokenProvider)
      throws MessagingException {
    super(storeConfig, trustedSocketFactory);

    ImapStoreSettings settings;
    try {
      settings = decodeUri(storeConfig.getStoreUri());
    } catch (IllegalArgumentException e) {
      throw new MessagingException("Error while decoding store URI", e);
    }

    host = settings.host;
    port = settings.port;

    connectionSecurity = settings.connectionSecurity;
    this.connectivityManager = connectivityManager;
    this.oauthTokenProvider = oauthTokenProvider;

    authType = settings.authenticationType;
    username = settings.username;
    password = settings.password;
    clientCertificateAlias = settings.clientCertificateAlias;

    // Make extra sure pathPrefix is null if "auto-detect namespace" is configured
    pathPrefix = (settings.autoDetectNamespace) ? null : settings.pathPrefix;

    folderNameCodec = FolderNameCodec.newInstance();
  }
Example #2
0
  void autoconfigureFolders(final ImapConnection connection)
      throws IOException, MessagingException {
    if (!connection.hasCapability(Capabilities.SPECIAL_USE)) {
      if (K9MailLib.isDebug()) {
        Log.d(LOG_TAG, "No detected folder auto-configuration methods.");
      }
      return;
    }

    if (K9MailLib.isDebug()) {
      Log.d(LOG_TAG, "Folder auto-configuration: Using RFC6154/SPECIAL-USE.");
    }

    String command =
        String.format(
            "LIST (SPECIAL-USE) \"\" %s", ImapUtility.encodeString(getCombinedPrefix() + "*"));
    List<ImapResponse> responses = connection.executeSimpleCommand(command);

    List<ListResponse> listResponses = ListResponse.parseList(responses);

    for (ListResponse listResponse : listResponses) {
      String decodedFolderName;
      try {
        decodedFolderName = folderNameCodec.decode(listResponse.getName());
      } catch (CharacterCodingException e) {
        Log.w(
            LOG_TAG,
            "Folder name not correctly encoded with the UTF-7 variant "
                + "as defined by RFC 3501: "
                + listResponse.getName(),
            e);
        // We currently just skip folders with malformed names.
        continue;
      }

      if (pathDelimiter == null) {
        pathDelimiter = listResponse.getHierarchyDelimiter();
        combinedPrefix = null;
      }

      if (listResponse.hasAttribute("\\Archive") || listResponse.hasAttribute("\\All")) {
        mStoreConfig.setArchiveFolderName(decodedFolderName);
        if (K9MailLib.isDebug()) {
          Log.d(LOG_TAG, "Folder auto-configuration detected Archive folder: " + decodedFolderName);
        }
      } else if (listResponse.hasAttribute("\\Drafts")) {
        mStoreConfig.setDraftsFolderName(decodedFolderName);
        if (K9MailLib.isDebug()) {
          Log.d(LOG_TAG, "Folder auto-configuration detected Drafts folder: " + decodedFolderName);
        }
      } else if (listResponse.hasAttribute("\\Sent")) {
        mStoreConfig.setSentFolderName(decodedFolderName);
        if (K9MailLib.isDebug()) {
          Log.d(LOG_TAG, "Folder auto-configuration detected Sent folder: " + decodedFolderName);
        }
      } else if (listResponse.hasAttribute("\\Junk")) {
        mStoreConfig.setSpamFolderName(decodedFolderName);
        if (K9MailLib.isDebug()) {
          Log.d(LOG_TAG, "Folder auto-configuration detected Spam folder: " + decodedFolderName);
        }
      } else if (listResponse.hasAttribute("\\Trash")) {
        mStoreConfig.setTrashFolderName(decodedFolderName);
        if (K9MailLib.isDebug()) {
          Log.d(LOG_TAG, "Folder auto-configuration detected Trash folder: " + decodedFolderName);
        }
      }
    }
  }
Example #3
0
  private Set<String> listFolders(ImapConnection connection, boolean subscribedOnly)
      throws IOException, MessagingException {
    String commandResponse = subscribedOnly ? "LSUB" : "LIST";

    List<ImapResponse> responses =
        connection.executeSimpleCommand(
            String.format(
                "%s \"\" %s",
                commandResponse, ImapUtility.encodeString(getCombinedPrefix() + "*")));

    List<ListResponse> listResponses =
        (subscribedOnly) ? ListResponse.parseLsub(responses) : ListResponse.parseList(responses);

    Set<String> folderNames = new HashSet<>(listResponses.size());

    for (ListResponse listResponse : listResponses) {
      boolean includeFolder = true;

      String decodedFolderName;
      try {
        decodedFolderName = folderNameCodec.decode(listResponse.getName());
      } catch (CharacterCodingException e) {
        Log.w(
            LOG_TAG,
            "Folder name not correctly encoded with the UTF-7 variant "
                + "as defined by RFC 3501: "
                + listResponse.getName(),
            e);

        // TODO: Use the raw name returned by the server for all commands that require
        //      a folder name. Use the decoded name only for showing it to the user.

        // We currently just skip folders with malformed names.
        continue;
      }

      String folder = decodedFolderName;

      if (pathDelimiter == null) {
        pathDelimiter = listResponse.getHierarchyDelimiter();
        combinedPrefix = null;
      }

      if (folder.equalsIgnoreCase(mStoreConfig.getInboxFolderName())) {
        continue;
      } else if (folder.equals(mStoreConfig.getOutboxFolderName())) {
        /*
         * There is a folder on the server with the same name as our local
         * outbox. Until we have a good plan to deal with this situation
         * we simply ignore the folder on the server.
         */
        continue;
      } else {
        int prefixLength = getCombinedPrefix().length();
        if (prefixLength > 0) {
          // Strip prefix from the folder name
          if (folder.length() >= prefixLength) {
            folder = folder.substring(prefixLength);
          }
          if (!decodedFolderName.equalsIgnoreCase(getCombinedPrefix() + folder)) {
            includeFolder = false;
          }
        }
      }

      if (listResponse.hasAttribute("\\NoSelect")) {
        includeFolder = false;
      }

      if (includeFolder) {
        folderNames.add(folder);
      }
    }

    folderNames.add(mStoreConfig.getInboxFolderName());

    return folderNames;
  }