Ejemplo n.º 1
0
  /**
   * Creates new account.
   *
   * @param user full or bare jid.
   * @param password
   * @param accountType xmpp account type can be replaced depend on server part.
   * @param syncable
   * @param storePassword
   * @param useOrbot
   * @return assigned account name.
   * @throws NetworkException if user or server part are invalid.
   */
  public String addAccount(
      String user,
      String password,
      AccountType accountType,
      boolean syncable,
      boolean storePassword,
      boolean useOrbot)
      throws NetworkException {
    if (accountType.getProtocol().isOAuth()) {
      int index = 1;
      while (true) {
        user = String.valueOf(index);
        boolean found = false;
        for (AccountItem accountItem : accountItems.values())
          if (accountItem
                  .getConnectionSettings()
                  .getServerName()
                  .equals(accountType.getFirstServer())
              && accountItem.getConnectionSettings().getUserName().equals(user)) {
            found = true;
            break;
          }
        if (!found) break;
        index++;
      }
    }

    if (user == null) throw new NetworkException(R.string.EMPTY_USER_NAME);

    if (user.indexOf("@") == -1) {
      if ("".equals(accountType.getFirstServer()))
        throw new NetworkException(R.string.EMPTY_SERVER_NAME);
      else user += "@" + accountType.getFirstServer();
    }

    String serverName = StringUtils.parseServer(user);
    String userName = StringUtils.parseName(user);
    String resource = StringUtils.parseResource(user);
    String host = accountType.getHost();
    int port = accountType.getPort();
    boolean tlsRequired = accountType.isTLSRequired();
    if (useOrbot) tlsRequired = true;

    if ("".equals(serverName)) {
      throw new NetworkException(R.string.EMPTY_SERVER_NAME);
    } else if (!accountType.isAllowServer() && !serverName.equals(accountType.getFirstServer()))
      throw new NetworkException(R.string.INCORRECT_USER_NAME);

    if ("".equals(userName)) throw new NetworkException(R.string.EMPTY_USER_NAME);
    if ("".equals(resource)) resource = "android" + StringUtils.randomString(8);

    if (accountType.getId() == R.array.account_type_xmpp) {
      host = serverName;
      for (AccountType check : accountTypes)
        if (check.getServers().contains(serverName)) {
          accountType = check;
          host = check.getHost();
          port = check.getPort();
          tlsRequired = check.isTLSRequired();
          break;
        }
    }

    AccountItem accountItem;
    for (; ; ) {
      if (getAccount(userName + '@' + serverName + '/' + resource) == null) break;
      resource = "android" + StringUtils.randomString(8);
    }

    accountItem =
        addAccount(
            accountType.getProtocol(),
            true,
            host,
            port,
            serverName,
            userName,
            storePassword,
            password,
            resource,
            getNextColorIndex(),
            0,
            StatusMode.available,
            SettingsManager.statusText(),
            true,
            true,
            tlsRequired ? TLSMode.required : TLSMode.enabled,
            false,
            useOrbot ? ProxyType.orbot : ProxyType.none,
            "localhost",
            8080,
            "",
            "",
            syncable,
            null,
            null,
            ArchiveMode.available);
    onAccountChanged(accountItem.getAccount());
    if (accountItems.size() > 1 && SettingsManager.contactsEnableShowAccounts())
      SettingsManager.enableContactsShowAccount();
    return accountItem.getAccount();
  }