Example #1
0
  /**
   * The workgroup service may be configured to send email. This queries the Workgroup Service to
   * see if the email service has been configured and is available.
   *
   * @return true if the email service is available, otherwise return false.
   */
  public boolean isEmailAvailable() {
    ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection);

    try {
      String workgroupService = StringUtils.parseServer(workgroupJID);
      DiscoverInfo infoResult = discoManager.discoverInfo(workgroupService);
      return infoResult.containsFeature("jive:email:provider");
    } catch (XMPPException e) {
      return false;
    }
  }
  @Override
  public synchronized void loginAnonymously() throws XMPPException {
    if (!isConnected()) {
      throw new IllegalStateException("Not connected to server.");
    }
    if (authenticated) {
      throw new IllegalStateException("Already logged in to server.");
    }

    String response;
    if (config.isSASLAuthenticationEnabled() && saslAuthentication.hasAnonymousAuthentication()) {
      response = saslAuthentication.authenticateAnonymously();
    } else {
      // Authenticate using Non-SASL
      response = new NonSASLAuthentication(this).authenticateAnonymously();
    }

    // Set the user value.
    this.user = response;
    // Update the serviceName with the one returned by the server
    config.setServiceName(StringUtils.parseServer(response));

    // If compression is enabled then request the server to use stream compression
    if (config.isCompressionEnabled()) {
      useCompression();
    }

    // Set presence to online.
    packetWriter.sendPacket(new Presence(Presence.Type.available));

    // Indicate that we're now authenticated.
    authenticated = true;
    anonymous = true;

    // If debugging is enabled, change the the debug window title to include the
    // name we are now logged-in as.
    // If DEBUG_ENABLED was set to true AFTER the connection was created the debugger
    // will be null
    if (config.isDebuggerEnabled() && debugger != null) {
      debugger.userHasLogged(user);
    }
  }
 public void updateContactList() {
   for (RosterEntry entry : mConnectionManager.getEntries()) {
     String email = entry.getUser();
     if (!StringUtils.parseServer(email).equals(mConnectionManager.getDomain())) continue;
     if (email.equals(mConnectionManager.getUsernameEmail())) continue;
     Presence presence = mConnectionManager.getPresence(email);
     if (presence == null)
       return; // if disconnection happened we might get null, and in such case this method
     // execution can stop
     Contact contact;
     if (mContactsMap.containsKey(email)) {
       contact = mContactsList.get(getContactPosition(email));
       contact.update(presence);
     } else {
       contact = new Contact(presence);
       contact.setName(entry.getName());
       mContactsList.add(contact);
       mContactsMap.put(email, contact);
     }
   }
   Log.i(TAG, "updateContactList: " + mContactsList.toString());
   Collections.sort(mContactsList);
 }
 /**
  * Initialize the XMPP connection.
  *
  * @param jid the jid to use
  * @param server the server to use (not using dns srv) may be null
  * @param port the port
  * @return the XMPPConnection prepared to connect
  */
 private Connection prepareConnection(String jid, String server, int port) {
   boolean useProxy = settings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
   ProxyInfo proxyinfo = ProxyInfo.forNoProxy();
   if (useProxy) {
     String stype = settings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP");
     String phost = settings.getString(BeemApplication.PROXY_SERVER_KEY, "");
     String puser = settings.getString(BeemApplication.PROXY_USERNAME_KEY, "");
     String ppass = settings.getString(BeemApplication.PROXY_PASSWORD_KEY, "");
     int pport = Integer.parseInt(settings.getString(BeemApplication.PROXY_PORT_KEY, "1080"));
     ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
     proxyinfo = new ProxyInfo(type, phost, pport, puser, ppass);
   }
   String serviceName = StringUtils.parseServer(jid);
   if (port != -1 || !TextUtils.isEmpty(server)) {
     if (port == -1) port = 5222;
     if (TextUtils.isEmpty(server)) server = serviceName;
     config = new ConnectionConfiguration(server, port, serviceName, proxyinfo);
   } else {
     config = new ConnectionConfiguration(serviceName, proxyinfo);
   }
   if (settings.getBoolean(BeemApplication.SMACK_DEBUG_KEY, false))
     config.setDebuggerEnabled(true);
   return new XMPPConnection(config);
 }
Example #5
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();
  }
  @Override
  public synchronized void login(String username, String password, String resource)
      throws XMPPException {
    if (!isConnected()) {
      throw new IllegalStateException("Not connected to server.");
    }
    if (authenticated) {
      throw new IllegalStateException("Already logged in to server.");
    }
    // Do partial version of nameprep on the username.
    username = username.toLowerCase().trim();

    // Pause keep alive process while authentication and compression is in
    // progress
    packetWriter.stopKeepAliveProcess();

    String response;
    if (config.isSASLAuthenticationEnabled()
        && saslAuthentication.hasNonAnonymousAuthentication()) {
      // Authenticate using SASL
      if (password != null) {
        response = saslAuthentication.authenticate(username, password, resource);
      } else {
        response = saslAuthentication.authenticate(username, resource, config.getCallbackHandler());
      }
    } else {
      // Authenticate using Non-SASL
      response = new NonSASLAuthentication(this).authenticate(username, password, resource);
    }

    // Set the user.
    if (response != null) {
      this.user = response;
      // Update the serviceName with the one returned by the server
      config.setServiceName(StringUtils.parseServer(response));
    } else {
      this.user = username + "@" + getServiceName();
      if (resource != null) {
        this.user += "/" + resource;
      }
    }

    // If compression is enabled then request the server to use stream compression
    if (config.isCompressionEnabled()) {
      useCompression();
    }

    // Resume keep alive process (after authentication and compression was
    // complited)
    packetWriter.resumeKeepAliveProcess();

    // Indicate that we're now authenticated.
    authenticated = true;
    anonymous = false;

    if (config.isRosterLoadedAtLogin()) {
      // Create the roster if it is not a reconnection or roster already
      // created by getRoster()
      if (this.roster == null) {
        if (rosterStorage == null) {
          this.roster = new Roster(this);
        } else {
          this.roster = new Roster(this, rosterStorage);
        }
      }
      this.roster.reload();
    }

    // Set presence to online.
    if (config.isSendPresence()) {
      packetWriter.sendPacket(new Presence(Presence.Type.available));
    }

    // Stores the authentication for future reconnection
    config.setLoginInfo(username, password, resource);

    // If debugging is enabled, change the the debug window title to include the
    // name we are now logged-in as.
    // If DEBUG_ENABLED was set to true AFTER the connection was created the debugger
    // will be null
    if (config.isDebuggerEnabled() && debugger != null) {
      debugger.userHasLogged(user);
    }
  }