XmppConnection.java

   1package eu.siacs.conversations.xmpp;
   2
   3import android.content.Context;
   4import android.graphics.Bitmap;
   5import android.graphics.BitmapFactory;
   6import android.os.SystemClock;
   7import android.security.KeyChain;
   8import android.util.Base64;
   9import android.util.Log;
  10import android.util.Pair;
  11import android.util.SparseArray;
  12
  13import androidx.annotation.NonNull;
  14
  15import com.google.common.base.Strings;
  16
  17import org.xmlpull.v1.XmlPullParserException;
  18
  19import java.io.ByteArrayInputStream;
  20import java.io.IOException;
  21import java.io.InputStream;
  22import java.net.ConnectException;
  23import java.net.IDN;
  24import java.net.InetAddress;
  25import java.net.InetSocketAddress;
  26import java.net.Socket;
  27import java.net.UnknownHostException;
  28import java.security.KeyManagementException;
  29import java.security.NoSuchAlgorithmException;
  30import java.security.Principal;
  31import java.security.PrivateKey;
  32import java.security.cert.X509Certificate;
  33import java.util.ArrayList;
  34import java.util.Arrays;
  35import java.util.Collections;
  36import java.util.HashMap;
  37import java.util.HashSet;
  38import java.util.Hashtable;
  39import java.util.Iterator;
  40import java.util.List;
  41import java.util.Map.Entry;
  42import java.util.Set;
  43import java.util.concurrent.CountDownLatch;
  44import java.util.concurrent.TimeUnit;
  45import java.util.concurrent.atomic.AtomicBoolean;
  46import java.util.concurrent.atomic.AtomicInteger;
  47import java.util.regex.Matcher;
  48
  49import javax.net.ssl.KeyManager;
  50import javax.net.ssl.SSLContext;
  51import javax.net.ssl.SSLPeerUnverifiedException;
  52import javax.net.ssl.SSLSocket;
  53import javax.net.ssl.SSLSocketFactory;
  54import javax.net.ssl.X509KeyManager;
  55import javax.net.ssl.X509TrustManager;
  56
  57import eu.siacs.conversations.Config;
  58import eu.siacs.conversations.R;
  59import eu.siacs.conversations.crypto.XmppDomainVerifier;
  60import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  61import eu.siacs.conversations.crypto.sasl.Anonymous;
  62import eu.siacs.conversations.crypto.sasl.DigestMd5;
  63import eu.siacs.conversations.crypto.sasl.External;
  64import eu.siacs.conversations.crypto.sasl.Plain;
  65import eu.siacs.conversations.crypto.sasl.SaslMechanism;
  66import eu.siacs.conversations.crypto.sasl.ScramSha1;
  67import eu.siacs.conversations.crypto.sasl.ScramSha256;
  68import eu.siacs.conversations.crypto.sasl.ScramSha512;
  69import eu.siacs.conversations.entities.Account;
  70import eu.siacs.conversations.entities.Message;
  71import eu.siacs.conversations.entities.ServiceDiscoveryResult;
  72import eu.siacs.conversations.generator.IqGenerator;
  73import eu.siacs.conversations.http.HttpConnectionManager;
  74import eu.siacs.conversations.persistance.FileBackend;
  75import eu.siacs.conversations.services.MemorizingTrustManager;
  76import eu.siacs.conversations.services.MessageArchiveService;
  77import eu.siacs.conversations.services.NotificationService;
  78import eu.siacs.conversations.services.XmppConnectionService;
  79import eu.siacs.conversations.utils.CryptoHelper;
  80import eu.siacs.conversations.utils.Patterns;
  81import eu.siacs.conversations.utils.Resolver;
  82import eu.siacs.conversations.utils.SSLSocketHelper;
  83import eu.siacs.conversations.utils.SocksSocketFactory;
  84import eu.siacs.conversations.utils.XmlHelper;
  85import eu.siacs.conversations.xml.Element;
  86import eu.siacs.conversations.xml.LocalizedContent;
  87import eu.siacs.conversations.xml.Namespace;
  88import eu.siacs.conversations.xml.Tag;
  89import eu.siacs.conversations.xml.TagWriter;
  90import eu.siacs.conversations.xml.XmlReader;
  91import eu.siacs.conversations.xmpp.forms.Data;
  92import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
  93import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  94import eu.siacs.conversations.xmpp.stanzas.AbstractAcknowledgeableStanza;
  95import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
  96import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  97import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
  98import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
  99import eu.siacs.conversations.xmpp.stanzas.csi.ActivePacket;
 100import eu.siacs.conversations.xmpp.stanzas.csi.InactivePacket;
 101import eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket;
 102import eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket;
 103import eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket;
 104import eu.siacs.conversations.xmpp.stanzas.streammgmt.ResumePacket;
 105import okhttp3.HttpUrl;
 106
 107public class XmppConnection implements Runnable {
 108
 109    private static final int PACKET_IQ = 0;
 110    private static final int PACKET_MESSAGE = 1;
 111    private static final int PACKET_PRESENCE = 2;
 112    public final OnIqPacketReceived registrationResponseListener = (account, packet) -> {
 113        if (packet.getType() == IqPacket.TYPE.RESULT) {
 114            account.setOption(Account.OPTION_REGISTER, false);
 115            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": successfully registered new account on server");
 116            throw new StateChangingError(Account.State.REGISTRATION_SUCCESSFUL);
 117        } else {
 118            final List<String> PASSWORD_TOO_WEAK_MSGS = Arrays.asList(
 119                    "The password is too weak",
 120                    "Please use a longer password.");
 121            Element error = packet.findChild("error");
 122            Account.State state = Account.State.REGISTRATION_FAILED;
 123            if (error != null) {
 124                if (error.hasChild("conflict")) {
 125                    state = Account.State.REGISTRATION_CONFLICT;
 126                } else if (error.hasChild("resource-constraint")
 127                        && "wait".equals(error.getAttribute("type"))) {
 128                    state = Account.State.REGISTRATION_PLEASE_WAIT;
 129                } else if (error.hasChild("not-acceptable")
 130                        && PASSWORD_TOO_WEAK_MSGS.contains(error.findChildContent("text"))) {
 131                    state = Account.State.REGISTRATION_PASSWORD_TOO_WEAK;
 132                }
 133            }
 134            throw new StateChangingError(state);
 135        }
 136    };
 137    protected final Account account;
 138    private final Features features = new Features(this);
 139    private final HashMap<Jid, ServiceDiscoveryResult> disco = new HashMap<>();
 140    private final HashMap<String, Jid> commands = new HashMap<>();
 141    private final SparseArray<AbstractAcknowledgeableStanza> mStanzaQueue = new SparseArray<>();
 142    private final Hashtable<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks = new Hashtable<>();
 143    private final Set<OnAdvancedStreamFeaturesLoaded> advancedStreamFeaturesLoadedListeners = new HashSet<>();
 144    private final XmppConnectionService mXmppConnectionService;
 145    private Socket socket;
 146    private XmlReader tagReader;
 147    private TagWriter tagWriter = new TagWriter();
 148    private boolean shouldAuthenticate = true;
 149    private boolean inSmacksSession = false;
 150    private boolean isBound = false;
 151    private Element streamFeatures;
 152    private String streamId = null;
 153    private int smVersion = 3;
 154    private int stanzasReceived = 0;
 155    private int stanzasSent = 0;
 156    private long lastPacketReceived = 0;
 157    private long lastPingSent = 0;
 158    private long lastConnect = 0;
 159    private long lastSessionStarted = 0;
 160    private long lastDiscoStarted = 0;
 161    private boolean isMamPreferenceAlways = false;
 162    private final AtomicInteger mPendingServiceDiscoveries = new AtomicInteger(0);
 163    private final AtomicBoolean mWaitForDisco = new AtomicBoolean(true);
 164    private final AtomicBoolean mWaitingForSmCatchup = new AtomicBoolean(false);
 165    private final AtomicInteger mSmCatchupMessageCounter = new AtomicInteger(0);
 166    private boolean mInteractive = false;
 167    private int attempt = 0;
 168    private OnPresencePacketReceived presenceListener = null;
 169    private OnJinglePacketReceived jingleListener = null;
 170    private OnIqPacketReceived unregisteredIqListener = null;
 171    private OnMessagePacketReceived messageListener = null;
 172    private OnStatusChanged statusListener = null;
 173    private OnBindListener bindListener = null;
 174    private OnMessageAcknowledged acknowledgedListener = null;
 175    private SaslMechanism saslMechanism;
 176    private HttpUrl redirectionUrl = null;
 177    private String verifiedHostname = null;
 178    private volatile Thread mThread;
 179    private CountDownLatch mStreamCountDownLatch;
 180
 181
 182    public XmppConnection(final Account account, final XmppConnectionService service) {
 183        this.account = account;
 184        this.mXmppConnectionService = service;
 185    }
 186
 187    private static void fixResource(Context context, Account account) {
 188        String resource = account.getResource();
 189        int fixedPartLength = context.getString(R.string.app_name).length() + 1; //include the trailing dot
 190        int randomPartLength = 4; // 3 bytes
 191        if (resource != null && resource.length() > fixedPartLength + randomPartLength) {
 192            if (validBase64(resource.substring(fixedPartLength, fixedPartLength + randomPartLength))) {
 193                account.setResource(resource.substring(0, fixedPartLength + randomPartLength));
 194            }
 195        }
 196    }
 197
 198    private static boolean validBase64(String input) {
 199        try {
 200            return Base64.decode(input, Base64.URL_SAFE).length == 3;
 201        } catch (Throwable throwable) {
 202            return false;
 203        }
 204    }
 205
 206    private void changeStatus(final Account.State nextStatus) {
 207        synchronized (this) {
 208            if (Thread.currentThread().isInterrupted()) {
 209                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": not changing status to " + nextStatus + " because thread was interrupted");
 210                return;
 211            }
 212            if (account.getStatus() != nextStatus) {
 213                if ((nextStatus == Account.State.OFFLINE)
 214                        && (account.getStatus() != Account.State.CONNECTING)
 215                        && (account.getStatus() != Account.State.ONLINE)
 216                        && (account.getStatus() != Account.State.DISABLED)) {
 217                    return;
 218                }
 219                if (nextStatus == Account.State.ONLINE) {
 220                    this.attempt = 0;
 221                }
 222                account.setStatus(nextStatus);
 223            } else {
 224                return;
 225            }
 226        }
 227        if (statusListener != null) {
 228            statusListener.onStatusChanged(account);
 229        }
 230    }
 231
 232    public Jid getJidForCommand(final String node) {
 233        synchronized (this.commands) {
 234            return this.commands.get(node);
 235        }
 236    }
 237
 238    public void prepareNewConnection() {
 239        this.lastConnect = SystemClock.elapsedRealtime();
 240        this.lastPingSent = SystemClock.elapsedRealtime();
 241        this.lastDiscoStarted = Long.MAX_VALUE;
 242        this.mWaitingForSmCatchup.set(false);
 243        this.changeStatus(Account.State.CONNECTING);
 244    }
 245
 246    public boolean isWaitingForSmCatchup() {
 247        return mWaitingForSmCatchup.get();
 248    }
 249
 250    public void incrementSmCatchupMessageCounter() {
 251        this.mSmCatchupMessageCounter.incrementAndGet();
 252    }
 253
 254    protected void connect() {
 255        if (mXmppConnectionService.areMessagesInitialized()) {
 256            mXmppConnectionService.resetSendingToWaiting(account);
 257        }
 258        Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": connecting");
 259        features.encryptionEnabled = false;
 260        inSmacksSession = false;
 261        isBound = false;
 262        this.attempt++;
 263        this.verifiedHostname = null; //will be set if user entered hostname is being used or hostname was verified with dnssec
 264        try {
 265            Socket localSocket;
 266            shouldAuthenticate = !account.isOptionSet(Account.OPTION_REGISTER);
 267            this.changeStatus(Account.State.CONNECTING);
 268            final boolean useTor = mXmppConnectionService.useTorToConnect() || account.isOnion();
 269            final boolean extended = mXmppConnectionService.showExtendedConnectionOptions();
 270            if (useTor) {
 271                String destination;
 272                if (account.getHostname().isEmpty() || account.isOnion()) {
 273                    destination = account.getServer();
 274                } else {
 275                    destination = account.getHostname();
 276                    this.verifiedHostname = destination;
 277                }
 278
 279                final int port = account.getPort();
 280                final boolean directTls = Resolver.useDirectTls(port);
 281
 282                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": connect to " + destination + " via Tor. directTls=" + directTls);
 283                localSocket = SocksSocketFactory.createSocketOverTor(destination, port);
 284
 285                if (directTls) {
 286                    localSocket = upgradeSocketToTls(localSocket);
 287                    features.encryptionEnabled = true;
 288                }
 289
 290                try {
 291                    startXmpp(localSocket);
 292                } catch (InterruptedException e) {
 293                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": thread was interrupted before beginning stream");
 294                    return;
 295                } catch (Exception e) {
 296                    throw new IOException(e.getMessage());
 297                }
 298            } else {
 299                final String domain = account.getServer();
 300                final List<Resolver.Result> results;
 301                final boolean hardcoded = extended && !account.getHostname().isEmpty();
 302                if (hardcoded) {
 303                    results = Resolver.fromHardCoded(account.getHostname(), account.getPort());
 304                } else {
 305                    results = Resolver.resolve(domain);
 306                }
 307                if (Thread.currentThread().isInterrupted()) {
 308                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Thread was interrupted");
 309                    return;
 310                }
 311                if (results.size() == 0) {
 312                    Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": Resolver results were empty");
 313                    return;
 314                }
 315                final Resolver.Result storedBackupResult;
 316                if (hardcoded) {
 317                    storedBackupResult = null;
 318                } else {
 319                    storedBackupResult = mXmppConnectionService.databaseBackend.findResolverResult(domain);
 320                    if (storedBackupResult != null && !results.contains(storedBackupResult)) {
 321                        results.add(storedBackupResult);
 322                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": loaded backup resolver result from db: " + storedBackupResult);
 323                    }
 324                }
 325                for (Iterator<Resolver.Result> iterator = results.iterator(); iterator.hasNext(); ) {
 326                    final Resolver.Result result = iterator.next();
 327                    if (Thread.currentThread().isInterrupted()) {
 328                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Thread was interrupted");
 329                        return;
 330                    }
 331                    try {
 332                        // if tls is true, encryption is implied and must not be started
 333                        features.encryptionEnabled = result.isDirectTls();
 334                        verifiedHostname = result.isAuthenticated() ? result.getHostname().toString() : null;
 335                        Log.d(Config.LOGTAG, "verified hostname " + verifiedHostname);
 336                        final InetSocketAddress addr;
 337                        if (result.getIp() != null) {
 338                            addr = new InetSocketAddress(result.getIp(), result.getPort());
 339                            Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
 340                                    + ": using values from resolver " + (result.getHostname() == null ? "" : result.getHostname().toString()
 341                                    + "/") + result.getIp().getHostAddress() + ":" + result.getPort() + " tls: " + features.encryptionEnabled);
 342                        } else {
 343                            addr = new InetSocketAddress(IDN.toASCII(result.getHostname().toString()), result.getPort());
 344                            Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
 345                                    + ": using values from resolver "
 346                                    + result.getHostname().toString() + ":" + result.getPort() + " tls: " + features.encryptionEnabled);
 347                        }
 348
 349                        localSocket = new Socket();
 350                        localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
 351
 352                        if (features.encryptionEnabled) {
 353                            localSocket = upgradeSocketToTls(localSocket);
 354                        }
 355
 356                        localSocket.setSoTimeout(Config.SOCKET_TIMEOUT * 1000);
 357                        if (startXmpp(localSocket)) {
 358                            localSocket.setSoTimeout(0); //reset to 0; once the connection is established we don’t want this
 359                            if (!hardcoded && !result.equals(storedBackupResult)) {
 360                                mXmppConnectionService.databaseBackend.saveResolverResult(domain, result);
 361                            }
 362                            break; // successfully connected to server that speaks xmpp
 363                        } else {
 364                            FileBackend.close(localSocket);
 365                            throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
 366                        }
 367                    } catch (final StateChangingException e) {
 368                        if (!iterator.hasNext()) {
 369                            throw e;
 370                        }
 371                    } catch (InterruptedException e) {
 372                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": thread was interrupted before beginning stream");
 373                        return;
 374                    } catch (final Throwable e) {
 375                        Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": " + e.getMessage() + "(" + e.getClass().getName() + ")");
 376                        if (!iterator.hasNext()) {
 377                            throw new UnknownHostException();
 378                        }
 379                    }
 380                }
 381            }
 382            processStream();
 383        } catch (final SecurityException e) {
 384            this.changeStatus(Account.State.MISSING_INTERNET_PERMISSION);
 385        } catch (final StateChangingException e) {
 386            this.changeStatus(e.state);
 387        } catch (final UnknownHostException | ConnectException | SocksSocketFactory.HostNotFoundException e) {
 388            this.changeStatus(Account.State.SERVER_NOT_FOUND);
 389        } catch (final SocksSocketFactory.SocksProxyNotFoundException e) {
 390            this.changeStatus(Account.State.TOR_NOT_AVAILABLE);
 391        } catch (final IOException | XmlPullParserException e) {
 392            Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": " + e.getMessage());
 393            this.changeStatus(Account.State.OFFLINE);
 394            this.attempt = Math.max(0, this.attempt - 1);
 395        } finally {
 396            if (!Thread.currentThread().isInterrupted()) {
 397                forceCloseSocket();
 398            } else {
 399                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": not force closing socket because thread was interrupted");
 400            }
 401        }
 402    }
 403
 404    /**
 405     * Starts xmpp protocol, call after connecting to socket
 406     *
 407     * @return true if server returns with valid xmpp, false otherwise
 408     */
 409    private boolean startXmpp(Socket socket) throws Exception {
 410        if (Thread.currentThread().isInterrupted()) {
 411            throw new InterruptedException();
 412        }
 413        this.socket = socket;
 414        tagReader = new XmlReader();
 415        if (tagWriter != null) {
 416            tagWriter.forceClose();
 417        }
 418        tagWriter = new TagWriter();
 419        tagWriter.setOutputStream(socket.getOutputStream());
 420        tagReader.setInputStream(socket.getInputStream());
 421        tagWriter.beginDocument();
 422        sendStartStream();
 423        final Tag tag = tagReader.readTag();
 424        if (Thread.currentThread().isInterrupted()) {
 425            throw new InterruptedException();
 426        }
 427        if (socket instanceof SSLSocket) {
 428            SSLSocketHelper.log(account, (SSLSocket) socket);
 429        }
 430        return tag != null && tag.isStart("stream");
 431    }
 432
 433    private SSLSocketFactory getSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
 434        final SSLContext sc = SSLSocketHelper.getSSLContext();
 435        final MemorizingTrustManager trustManager = this.mXmppConnectionService.getMemorizingTrustManager();
 436        final KeyManager[] keyManager;
 437        if (account.getPrivateKeyAlias() != null) {
 438            keyManager = new KeyManager[]{new MyKeyManager()};
 439        } else {
 440            keyManager = null;
 441        }
 442        final String domain = account.getServer();
 443        sc.init(keyManager, new X509TrustManager[]{mInteractive ? trustManager.getInteractive(domain) : trustManager.getNonInteractive(domain)}, mXmppConnectionService.getRNG());
 444        return sc.getSocketFactory();
 445    }
 446
 447    @Override
 448    public void run() {
 449        synchronized (this) {
 450            this.mThread = Thread.currentThread();
 451            if (this.mThread.isInterrupted()) {
 452                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": aborting connect because thread was interrupted");
 453                return;
 454            }
 455            forceCloseSocket();
 456        }
 457        connect();
 458    }
 459
 460    private void processStream() throws XmlPullParserException, IOException {
 461        final CountDownLatch streamCountDownLatch = new CountDownLatch(1);
 462        this.mStreamCountDownLatch = streamCountDownLatch;
 463        Tag nextTag = tagReader.readTag();
 464        while (nextTag != null && !nextTag.isEnd("stream")) {
 465            if (nextTag.isStart("error")) {
 466                processStreamError(nextTag);
 467            } else if (nextTag.isStart("features")) {
 468                processStreamFeatures(nextTag);
 469            } else if (nextTag.isStart("proceed")) {
 470                switchOverToTls();
 471            } else if (nextTag.isStart("success")) {
 472                final String challenge = tagReader.readElement(nextTag).getContent();
 473                try {
 474                    saslMechanism.getResponse(challenge);
 475                } catch (final SaslMechanism.AuthenticationException e) {
 476                    Log.e(Config.LOGTAG, String.valueOf(e));
 477                    throw new StateChangingException(Account.State.UNAUTHORIZED);
 478                }
 479                Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": logged in");
 480                account.setKey(Account.PINNED_MECHANISM_KEY,
 481                        String.valueOf(saslMechanism.getPriority()));
 482                tagReader.reset();
 483                sendStartStream();
 484                final Tag tag = tagReader.readTag();
 485                if (tag != null && tag.isStart("stream")) {
 486                    processStream();
 487                } else {
 488                    throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
 489                }
 490                break;
 491            } else if (nextTag.isStart("failure")) {
 492                final Element failure = tagReader.readElement(nextTag);
 493                if (Namespace.SASL.equals(failure.getNamespace())) {
 494                    if (failure.hasChild("temporary-auth-failure")) {
 495                        throw new StateChangingException(Account.State.TEMPORARY_AUTH_FAILURE);
 496                    } else if (failure.hasChild("account-disabled")) {
 497                        final String text = failure.findChildContent("text");
 498                        if ( Strings.isNullOrEmpty(text)) {
 499                            throw new StateChangingException(Account.State.UNAUTHORIZED);
 500                        }
 501                        final Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(text);
 502                        if (matcher.find()) {
 503                            final HttpUrl url;
 504                            try {
 505                                url = HttpUrl.get(text.substring(matcher.start(), matcher.end()));
 506                            } catch (final IllegalArgumentException e) {
 507                                throw new StateChangingException(Account.State.UNAUTHORIZED);
 508                            }
 509                            if (url.isHttps()) {
 510                                this.redirectionUrl = url;
 511                                throw new StateChangingException(Account.State.PAYMENT_REQUIRED);
 512                            }
 513                        }
 514                    }
 515                    throw new StateChangingException(Account.State.UNAUTHORIZED);
 516                } else if (Namespace.TLS.equals(failure.getNamespace())) {
 517                    throw new StateChangingException(Account.State.TLS_ERROR);
 518                } else {
 519                    throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 520                }
 521            } else if (nextTag.isStart("challenge")) {
 522                final String challenge = tagReader.readElement(nextTag).getContent();
 523                final Element response = new Element("response", Namespace.SASL);
 524                try {
 525                    response.setContent(saslMechanism.getResponse(challenge));
 526                } catch (final SaslMechanism.AuthenticationException e) {
 527                    // TODO: Send auth abort tag.
 528                    Log.e(Config.LOGTAG, e.toString());
 529                }
 530                tagWriter.writeElement(response);
 531            } else if (nextTag.isStart("enabled")) {
 532                final Element enabled = tagReader.readElement(nextTag);
 533                if ("true".equals(enabled.getAttribute("resume"))) {
 534                    this.streamId = enabled.getAttribute("id");
 535                    Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
 536                            + ": stream management(" + smVersion
 537                            + ") enabled (resumable)");
 538                } else {
 539                    Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
 540                            + ": stream management(" + smVersion + ") enabled");
 541                }
 542                this.stanzasReceived = 0;
 543                this.inSmacksSession = true;
 544                final RequestPacket r = new RequestPacket(smVersion);
 545                tagWriter.writeStanzaAsync(r);
 546            } else if (nextTag.isStart("resumed")) {
 547                this.inSmacksSession = true;
 548                this.isBound = true;
 549                this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
 550                lastPacketReceived = SystemClock.elapsedRealtime();
 551                final Element resumed = tagReader.readElement(nextTag);
 552                final String h = resumed.getAttribute("h");
 553                try {
 554                    ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
 555                    final boolean acknowledgedMessages;
 556                    synchronized (this.mStanzaQueue) {
 557                        final int serverCount = Integer.parseInt(h);
 558                        if (serverCount < stanzasSent) {
 559                            Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
 560                                    + ": session resumed with lost packages");
 561                            stanzasSent = serverCount;
 562                        } else {
 563                            Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": session resumed");
 564                        }
 565                        acknowledgedMessages = acknowledgeStanzaUpTo(serverCount);
 566                        for (int i = 0; i < this.mStanzaQueue.size(); ++i) {
 567                            failedStanzas.add(mStanzaQueue.valueAt(i));
 568                        }
 569                        mStanzaQueue.clear();
 570                    }
 571                    if (acknowledgedMessages) {
 572                        mXmppConnectionService.updateConversationUi();
 573                    }
 574                    Log.d(Config.LOGTAG, "resending " + failedStanzas.size() + " stanzas");
 575                    for (AbstractAcknowledgeableStanza packet : failedStanzas) {
 576                        if (packet instanceof MessagePacket) {
 577                            MessagePacket message = (MessagePacket) packet;
 578                            mXmppConnectionService.markMessage(account,
 579                                    message.getTo().asBareJid(),
 580                                    message.getId(),
 581                                    Message.STATUS_UNSEND);
 582                        }
 583                        sendPacket(packet);
 584                    }
 585                } catch (final NumberFormatException ignored) {
 586                }
 587                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": online with resource " + account.getResource());
 588                changeStatus(Account.State.ONLINE);
 589            } else if (nextTag.isStart("r")) {
 590                tagReader.readElement(nextTag);
 591                if (Config.EXTENDED_SM_LOGGING) {
 592                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": acknowledging stanza #" + this.stanzasReceived);
 593                }
 594                final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
 595                tagWriter.writeStanzaAsync(ack);
 596            } else if (nextTag.isStart("a")) {
 597                boolean accountUiNeedsRefresh = false;
 598                synchronized (NotificationService.CATCHUP_LOCK) {
 599                    if (mWaitingForSmCatchup.compareAndSet(true, false)) {
 600                        final int messageCount = mSmCatchupMessageCounter.get();
 601                        final int pendingIQs = packetCallbacks.size();
 602                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": SM catchup complete (messages=" + messageCount + ", pending IQs=" + pendingIQs + ")");
 603                        accountUiNeedsRefresh = true;
 604                        if (messageCount > 0) {
 605                            mXmppConnectionService.getNotificationService().finishBacklog(true, account);
 606                        }
 607                    }
 608                }
 609                if (accountUiNeedsRefresh) {
 610                    mXmppConnectionService.updateAccountUi();
 611                }
 612                final Element ack = tagReader.readElement(nextTag);
 613                lastPacketReceived = SystemClock.elapsedRealtime();
 614                try {
 615                    final boolean acknowledgedMessages;
 616                    synchronized (this.mStanzaQueue) {
 617                        final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
 618                        acknowledgedMessages = acknowledgeStanzaUpTo(serverSequence);
 619                    }
 620                    if (acknowledgedMessages) {
 621                        mXmppConnectionService.updateConversationUi();
 622                    }
 623                } catch (NumberFormatException | NullPointerException e) {
 624                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server send ack without sequence number");
 625                }
 626            } else if (nextTag.isStart("failed")) {
 627                Element failed = tagReader.readElement(nextTag);
 628                try {
 629                    final int serverCount = Integer.parseInt(failed.getAttribute("h"));
 630                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed but server acknowledged stanza #" + serverCount);
 631                    final boolean acknowledgedMessages;
 632                    synchronized (this.mStanzaQueue) {
 633                        acknowledgedMessages = acknowledgeStanzaUpTo(serverCount);
 634                    }
 635                    if (acknowledgedMessages) {
 636                        mXmppConnectionService.updateConversationUi();
 637                    }
 638                } catch (NumberFormatException | NullPointerException e) {
 639                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed");
 640                }
 641                resetStreamId();
 642                sendBindRequest();
 643            } else if (nextTag.isStart("iq")) {
 644                processIq(nextTag);
 645            } else if (nextTag.isStart("message")) {
 646                processMessage(nextTag);
 647            } else if (nextTag.isStart("presence")) {
 648                processPresence(nextTag);
 649            }
 650            nextTag = tagReader.readTag();
 651        }
 652        if (nextTag != null && nextTag.isEnd("stream")) {
 653            streamCountDownLatch.countDown();
 654        }
 655    }
 656
 657    private boolean acknowledgeStanzaUpTo(int serverCount) {
 658        if (serverCount > stanzasSent) {
 659            Log.e(Config.LOGTAG, "server acknowledged more stanzas than we sent. serverCount=" + serverCount + ", ourCount=" + stanzasSent);
 660        }
 661        boolean acknowledgedMessages = false;
 662        for (int i = 0; i < mStanzaQueue.size(); ++i) {
 663            if (serverCount >= mStanzaQueue.keyAt(i)) {
 664                if (Config.EXTENDED_SM_LOGGING) {
 665                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server acknowledged stanza #" + mStanzaQueue.keyAt(i));
 666                }
 667                final AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
 668                if (stanza instanceof MessagePacket && acknowledgedListener != null) {
 669                    final MessagePacket packet = (MessagePacket) stanza;
 670                    final String id = packet.getId();
 671                    final Jid to = packet.getTo();
 672                    if (id != null && to != null) {
 673                        acknowledgedMessages |= acknowledgedListener.onMessageAcknowledged(account, to, id);
 674                    }
 675                }
 676                mStanzaQueue.removeAt(i);
 677                i--;
 678            }
 679        }
 680        return acknowledgedMessages;
 681    }
 682
 683    private @NonNull
 684    Element processPacket(final Tag currentTag, final int packetType) throws IOException {
 685        final Element element;
 686        switch (packetType) {
 687            case PACKET_IQ:
 688                element = new IqPacket();
 689                break;
 690            case PACKET_MESSAGE:
 691                element = new MessagePacket();
 692                break;
 693            case PACKET_PRESENCE:
 694                element = new PresencePacket();
 695                break;
 696            default:
 697                throw new AssertionError("Should never encounter invalid type");
 698        }
 699        element.setAttributes(currentTag.getAttributes());
 700        Tag nextTag = tagReader.readTag();
 701        if (nextTag == null) {
 702            throw new IOException("interrupted mid tag");
 703        }
 704        while (!nextTag.isEnd(element.getName())) {
 705            if (!nextTag.isNo()) {
 706                element.addChild(tagReader.readElement(nextTag));
 707            }
 708            nextTag = tagReader.readTag();
 709            if (nextTag == null) {
 710                throw new IOException("interrupted mid tag");
 711            }
 712        }
 713        if (stanzasReceived == Integer.MAX_VALUE) {
 714            resetStreamId();
 715            throw new IOException("time to restart the session. cant handle >2 billion pcks");
 716        }
 717        if (inSmacksSession) {
 718            ++stanzasReceived;
 719        } else if (features.sm()) {
 720            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": not counting stanza(" + element.getClass().getSimpleName() + "). Not in smacks session.");
 721        }
 722        lastPacketReceived = SystemClock.elapsedRealtime();
 723        if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
 724            Log.d(Config.LOGTAG, "[background stanza] " + element);
 725        }
 726        if (element instanceof IqPacket
 727                && (((IqPacket) element).getType() == IqPacket.TYPE.SET)
 728                && element.hasChild("jingle", Namespace.JINGLE)) {
 729            return JinglePacket.upgrade((IqPacket) element);
 730        } else {
 731            return element;
 732        }
 733    }
 734
 735    private void processIq(final Tag currentTag) throws IOException {
 736        final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
 737        if (!packet.valid()) {
 738            Log.e(Config.LOGTAG, "encountered invalid iq from='" + packet.getFrom() + "' to='" + packet.getTo() + "'");
 739            return;
 740        }
 741        if (packet instanceof JinglePacket) {
 742            if (this.jingleListener != null) {
 743                this.jingleListener.onJinglePacketReceived(account, (JinglePacket) packet);
 744            }
 745        } else {
 746            OnIqPacketReceived callback = null;
 747            synchronized (this.packetCallbacks) {
 748                final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId());
 749                if (packetCallbackDuple != null) {
 750                    // Packets to the server should have responses from the server
 751                    if (packetCallbackDuple.first.toServer(account)) {
 752                        if (packet.fromServer(account)) {
 753                            callback = packetCallbackDuple.second;
 754                            packetCallbacks.remove(packet.getId());
 755                        } else {
 756                            Log.e(Config.LOGTAG, account.getJid().asBareJid().toString() + ": ignoring spoofed iq packet");
 757                        }
 758                    } else {
 759                        if (packet.getFrom() != null && packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
 760                            callback = packetCallbackDuple.second;
 761                            packetCallbacks.remove(packet.getId());
 762                        } else {
 763                            Log.e(Config.LOGTAG, account.getJid().asBareJid().toString() + ": ignoring spoofed iq packet");
 764                        }
 765                    }
 766                } else if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
 767                    callback = this.unregisteredIqListener;
 768                }
 769            }
 770            if (callback != null) {
 771                try {
 772                    callback.onIqPacketReceived(account, packet);
 773                } catch (StateChangingError error) {
 774                    throw new StateChangingException(error.state);
 775                }
 776            }
 777        }
 778    }
 779
 780    private void processMessage(final Tag currentTag) throws IOException {
 781        final MessagePacket packet = (MessagePacket) processPacket(currentTag, PACKET_MESSAGE);
 782        if (!packet.valid()) {
 783            Log.e(Config.LOGTAG, "encountered invalid message from='" + packet.getFrom() + "' to='" + packet.getTo() + "'");
 784            return;
 785        }
 786        this.messageListener.onMessagePacketReceived(account, packet);
 787    }
 788
 789    private void processPresence(final Tag currentTag) throws IOException {
 790        PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE);
 791        if (!packet.valid()) {
 792            Log.e(Config.LOGTAG, "encountered invalid presence from='" + packet.getFrom() + "' to='" + packet.getTo() + "'");
 793            return;
 794        }
 795        this.presenceListener.onPresencePacketReceived(account, packet);
 796    }
 797
 798    private void sendStartTLS() throws IOException {
 799        final Tag startTLS = Tag.empty("starttls");
 800        startTLS.setAttribute("xmlns", Namespace.TLS);
 801        tagWriter.writeTag(startTLS);
 802    }
 803
 804    private void switchOverToTls() throws XmlPullParserException, IOException {
 805        tagReader.readTag();
 806        final Socket socket = this.socket;
 807        final SSLSocket sslSocket = upgradeSocketToTls(socket);
 808        tagReader.setInputStream(sslSocket.getInputStream());
 809        tagWriter.setOutputStream(sslSocket.getOutputStream());
 810        sendStartStream();
 811        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS connection established");
 812        features.encryptionEnabled = true;
 813        final Tag tag = tagReader.readTag();
 814        if (tag != null && tag.isStart("stream")) {
 815            SSLSocketHelper.log(account, sslSocket);
 816            processStream();
 817        } else {
 818            throw new StateChangingException(Account.State.STREAM_OPENING_ERROR);
 819        }
 820        sslSocket.close();
 821    }
 822
 823    private SSLSocket upgradeSocketToTls(final Socket socket) throws IOException {
 824        final SSLSocketFactory sslSocketFactory;
 825        try {
 826            sslSocketFactory = getSSLSocketFactory();
 827        } catch (final NoSuchAlgorithmException | KeyManagementException e) {
 828            throw new StateChangingException(Account.State.TLS_ERROR);
 829        }
 830        final InetAddress address = socket.getInetAddress();
 831        final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, address.getHostAddress(), socket.getPort(), true);
 832        SSLSocketHelper.setSecurity(sslSocket);
 833        SSLSocketHelper.setHostname(sslSocket, IDN.toASCII(account.getServer()));
 834        SSLSocketHelper.setApplicationProtocol(sslSocket, "xmpp-client");
 835        final XmppDomainVerifier xmppDomainVerifier = new XmppDomainVerifier();
 836        try {
 837            if (!xmppDomainVerifier.verify(account.getServer(), this.verifiedHostname, sslSocket.getSession())) {
 838                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS certificate domain verification failed");
 839                FileBackend.close(sslSocket);
 840                throw new StateChangingException(Account.State.TLS_ERROR_DOMAIN);
 841            }
 842        } catch (final SSLPeerUnverifiedException e) {
 843            FileBackend.close(sslSocket);
 844            throw new StateChangingException(Account.State.TLS_ERROR);
 845        }
 846        return sslSocket;
 847    }
 848
 849    private void processStreamFeatures(final Tag currentTag) throws IOException {
 850        this.streamFeatures = tagReader.readElement(currentTag);
 851        final boolean isSecure = features.encryptionEnabled || Config.ALLOW_NON_TLS_CONNECTIONS || account.isOnion();
 852        final boolean needsBinding = !isBound && !account.isOptionSet(Account.OPTION_REGISTER);
 853        if (this.streamFeatures.hasChild("starttls") && !features.encryptionEnabled) {
 854            sendStartTLS();
 855        } else if (this.streamFeatures.hasChild("register") && account.isOptionSet(Account.OPTION_REGISTER)) {
 856            if (isSecure) {
 857                register();
 858            } else {
 859                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to find STARTTLS for registration process " + XmlHelper.printElementNames(this.streamFeatures));
 860                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 861            }
 862        } else if (!this.streamFeatures.hasChild("register") && account.isOptionSet(Account.OPTION_REGISTER)) {
 863            throw new StateChangingException(Account.State.REGISTRATION_NOT_SUPPORTED);
 864        } else if (this.streamFeatures.hasChild("mechanisms") && shouldAuthenticate && isSecure) {
 865            authenticate();
 866        } else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:" + smVersion) && streamId != null) {
 867            if (Config.EXTENDED_SM_LOGGING) {
 868                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resuming after stanza #" + stanzasReceived);
 869            }
 870            final ResumePacket resume = new ResumePacket(this.streamId, stanzasReceived, smVersion);
 871            this.mSmCatchupMessageCounter.set(0);
 872            this.mWaitingForSmCatchup.set(true);
 873            this.tagWriter.writeStanzaAsync(resume);
 874        } else if (needsBinding) {
 875            if (this.streamFeatures.hasChild("bind") && isSecure) {
 876                sendBindRequest();
 877            } else {
 878                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to find bind feature " + XmlHelper.printElementNames(this.streamFeatures));
 879                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 880            }
 881        }
 882    }
 883
 884    private void authenticate() throws IOException {
 885        final List<String> mechanisms = extractMechanisms(streamFeatures.findChild("mechanisms"));
 886        final Element auth = new Element("auth", Namespace.SASL);
 887        if (mechanisms.contains(External.MECHANISM) && account.getPrivateKeyAlias() != null) {
 888            saslMechanism = new External(tagWriter, account, mXmppConnectionService.getRNG());
 889        } else if (mechanisms.contains(ScramSha512.MECHANISM)) {
 890            saslMechanism = new ScramSha512(tagWriter, account, mXmppConnectionService.getRNG());
 891        } else if (mechanisms.contains(ScramSha256.MECHANISM)) {
 892            saslMechanism = new ScramSha256(tagWriter, account, mXmppConnectionService.getRNG());
 893        } else if (mechanisms.contains(ScramSha1.MECHANISM)) {
 894            saslMechanism = new ScramSha1(tagWriter, account, mXmppConnectionService.getRNG());
 895        } else if (mechanisms.contains(Plain.MECHANISM) && !account.getJid().getDomain().toEscapedString().equals("nimbuzz.com")) {
 896            saslMechanism = new Plain(tagWriter, account);
 897        } else if (mechanisms.contains(DigestMd5.MECHANISM)) {
 898            saslMechanism = new DigestMd5(tagWriter, account, mXmppConnectionService.getRNG());
 899        } else if (mechanisms.contains(Anonymous.MECHANISM)) {
 900            saslMechanism = new Anonymous(tagWriter, account, mXmppConnectionService.getRNG());
 901        }
 902        if (saslMechanism != null) {
 903            final int pinnedMechanism = account.getKeyAsInt(Account.PINNED_MECHANISM_KEY, -1);
 904            if (pinnedMechanism > saslMechanism.getPriority()) {
 905                Log.e(Config.LOGTAG, "Auth failed. Authentication mechanism " + saslMechanism.getMechanism() +
 906                        " has lower priority (" + saslMechanism.getPriority() +
 907                        ") than pinned priority (" + pinnedMechanism +
 908                        "). Possible downgrade attack?");
 909                throw new StateChangingException(Account.State.DOWNGRADE_ATTACK);
 910            }
 911            Log.d(Config.LOGTAG, account.getJid().toString() + ": Authenticating with " + saslMechanism.getMechanism());
 912            auth.setAttribute("mechanism", saslMechanism.getMechanism());
 913            if (!saslMechanism.getClientFirstMessage().isEmpty()) {
 914                auth.setContent(saslMechanism.getClientFirstMessage());
 915            }
 916            tagWriter.writeElement(auth);
 917        } else {
 918            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to find supported SASL mechanism in " + mechanisms);
 919            throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
 920        }
 921    }
 922
 923    private List<String> extractMechanisms(final Element stream) {
 924        final ArrayList<String> mechanisms = new ArrayList<>(stream
 925                .getChildren().size());
 926        for (final Element child : stream.getChildren()) {
 927            mechanisms.add(child.getContent());
 928        }
 929        return mechanisms;
 930    }
 931
 932
 933    private void register() {
 934        final String preAuth = account.getKey(Account.PRE_AUTH_REGISTRATION_TOKEN);
 935        if (preAuth != null && features.invite()) {
 936            final IqPacket preAuthRequest = new IqPacket(IqPacket.TYPE.SET);
 937            preAuthRequest.addChild("preauth", Namespace.PARS).setAttribute("token", preAuth);
 938            sendUnmodifiedIqPacket(preAuthRequest, (account, response) -> {
 939                if (response.getType() == IqPacket.TYPE.RESULT) {
 940                    sendRegistryRequest();
 941                } else {
 942                    final String error = response.getErrorCondition();
 943                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": failed to pre auth. " + error);
 944                    throw new StateChangingError(Account.State.REGISTRATION_INVALID_TOKEN);
 945                }
 946            }, true);
 947        } else {
 948            sendRegistryRequest();
 949        }
 950    }
 951
 952    private void sendRegistryRequest() {
 953        final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
 954        register.query(Namespace.REGISTER);
 955        register.setTo(account.getDomain());
 956        sendUnmodifiedIqPacket(register, (account, packet) -> {
 957            if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
 958                return;
 959            }
 960            if (packet.getType() == IqPacket.TYPE.ERROR) {
 961                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
 962            }
 963            final Element query = packet.query(Namespace.REGISTER);
 964            if (query.hasChild("username") && (query.hasChild("password"))) {
 965                final IqPacket register1 = new IqPacket(IqPacket.TYPE.SET);
 966                final Element username = new Element("username").setContent(account.getUsername());
 967                final Element password = new Element("password").setContent(account.getPassword());
 968                register1.query(Namespace.REGISTER).addChild(username);
 969                register1.query().addChild(password);
 970                register1.setFrom(account.getJid().asBareJid());
 971                sendUnmodifiedIqPacket(register1, registrationResponseListener, true);
 972            } else if (query.hasChild("x", Namespace.DATA)) {
 973                final Data data = Data.parse(query.findChild("x", Namespace.DATA));
 974                final Element blob = query.findChild("data", "urn:xmpp:bob");
 975                final String id = packet.getId();
 976                InputStream is;
 977                if (blob != null) {
 978                    try {
 979                        final String base64Blob = blob.getContent();
 980                        final byte[] strBlob = Base64.decode(base64Blob, Base64.DEFAULT);
 981                        is = new ByteArrayInputStream(strBlob);
 982                    } catch (Exception e) {
 983                        is = null;
 984                    }
 985                } else {
 986                    final boolean useTor = mXmppConnectionService.useTorToConnect() || account.isOnion();
 987                    try {
 988                        final String url = data.getValue("url");
 989                        final String fallbackUrl = data.getValue("captcha-fallback-url");
 990                        if (url != null) {
 991                            is = HttpConnectionManager.open(url, useTor);
 992                        } else if (fallbackUrl != null) {
 993                            is = HttpConnectionManager.open(fallbackUrl, useTor);
 994                        } else {
 995                            is = null;
 996                        }
 997                    } catch (final IOException e) {
 998                        Log.d(Config.LOGTAG,account.getJid().asBareJid()+": unable to fetch captcha", e);
 999                        is = null;
1000                    }
1001                }
1002
1003                if (is != null) {
1004                    Bitmap captcha = BitmapFactory.decodeStream(is);
1005                    try {
1006                        if (mXmppConnectionService.displayCaptchaRequest(account, id, data, captcha)) {
1007                            return;
1008                        }
1009                    } catch (Exception e) {
1010                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1011                    }
1012                }
1013                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1014            } else if (query.hasChild("instructions") || query.hasChild("x", Namespace.OOB)) {
1015                final String instructions = query.findChildContent("instructions");
1016                final Element oob = query.findChild("x", Namespace.OOB);
1017                final String url = oob == null ? null : oob.findChildContent("url");
1018                if (url != null) {
1019                    setAccountCreationFailed(url);
1020                } else if (instructions != null) {
1021                    final Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(instructions);
1022                    if (matcher.find()) {
1023                        setAccountCreationFailed(instructions.substring(matcher.start(), matcher.end()));
1024                    }
1025                }
1026                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1027            }
1028        }, true);
1029    }
1030
1031    private void setAccountCreationFailed(final String url) {
1032        final HttpUrl httpUrl = url == null ? null : HttpUrl.parse(url);
1033        if (httpUrl != null && httpUrl.isHttps()) {
1034            this.redirectionUrl = httpUrl;
1035            throw new StateChangingError(Account.State.REGISTRATION_WEB);
1036        }
1037        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
1038    }
1039
1040    public HttpUrl getRedirectionUrl() {
1041        return this.redirectionUrl;
1042    }
1043
1044    public void resetEverything() {
1045        resetAttemptCount(true);
1046        resetStreamId();
1047        clearIqCallbacks();
1048        this.stanzasSent = 0;
1049        mStanzaQueue.clear();
1050        this.redirectionUrl = null;
1051        synchronized (this.disco) {
1052            disco.clear();
1053        }
1054        synchronized (this.commands) {
1055            this.commands.clear();
1056        }
1057    }
1058
1059    private void sendBindRequest() {
1060        try {
1061            mXmppConnectionService.restoredFromDatabaseLatch.await();
1062        } catch (InterruptedException e) {
1063            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": interrupted while waiting for DB restore during bind");
1064            return;
1065        }
1066        clearIqCallbacks();
1067        if (account.getJid().isBareJid()) {
1068            account.setResource(this.createNewResource());
1069        } else {
1070            fixResource(mXmppConnectionService, account);
1071        }
1072        final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
1073        final String resource = Config.USE_RANDOM_RESOURCE_ON_EVERY_BIND ? nextRandomId() : account.getResource();
1074        iq.addChild("bind", Namespace.BIND).addChild("resource").setContent(resource);
1075        this.sendUnmodifiedIqPacket(iq, (account, packet) -> {
1076            if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1077                return;
1078            }
1079            final Element bind = packet.findChild("bind");
1080            if (bind != null && packet.getType() == IqPacket.TYPE.RESULT) {
1081                isBound = true;
1082                final Element jid = bind.findChild("jid");
1083                if (jid != null && jid.getContent() != null) {
1084                    try {
1085                        Jid assignedJid = Jid.ofEscaped(jid.getContent());
1086                        if (!account.getJid().getDomain().equals(assignedJid.getDomain())) {
1087                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server tried to re-assign domain to " + assignedJid.getDomain());
1088                            throw new StateChangingError(Account.State.BIND_FAILURE);
1089                        }
1090                        if (account.setJid(assignedJid)) {
1091                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": jid changed during bind. updating database");
1092                            mXmppConnectionService.databaseBackend.updateAccount(account);
1093                        }
1094                        if (streamFeatures.hasChild("session")
1095                                && !streamFeatures.findChild("session").hasChild("optional")) {
1096                            sendStartSession();
1097                        } else {
1098                            sendPostBindInitialization();
1099                        }
1100                        return;
1101                    } catch (final IllegalArgumentException e) {
1102                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server reported invalid jid (" + jid.getContent() + ") on bind");
1103                    }
1104                } else {
1105                    Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure. (no jid)");
1106                }
1107            } else {
1108                Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure (" + packet.toString());
1109            }
1110            final Element error = packet.findChild("error");
1111            if (packet.getType() == IqPacket.TYPE.ERROR && error != null && error.hasChild("conflict")) {
1112                account.setResource(createNewResource());
1113            }
1114            throw new StateChangingError(Account.State.BIND_FAILURE);
1115        }, true);
1116    }
1117
1118    private void clearIqCallbacks() {
1119        final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.TIMEOUT);
1120        final ArrayList<OnIqPacketReceived> callbacks = new ArrayList<>();
1121        synchronized (this.packetCallbacks) {
1122            if (this.packetCallbacks.size() == 0) {
1123                return;
1124            }
1125            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": clearing " + this.packetCallbacks.size() + " iq callbacks");
1126            final Iterator<Pair<IqPacket, OnIqPacketReceived>> iterator = this.packetCallbacks.values().iterator();
1127            while (iterator.hasNext()) {
1128                Pair<IqPacket, OnIqPacketReceived> entry = iterator.next();
1129                callbacks.add(entry.second);
1130                iterator.remove();
1131            }
1132        }
1133        for (OnIqPacketReceived callback : callbacks) {
1134            try {
1135                callback.onIqPacketReceived(account, failurePacket);
1136            } catch (StateChangingError error) {
1137                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": caught StateChangingError(" + error.state.toString() + ") while clearing callbacks");
1138                //ignore
1139            }
1140        }
1141        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": done clearing iq callbacks. " + this.packetCallbacks.size() + " left");
1142    }
1143
1144    public void sendDiscoTimeout() {
1145        if (mWaitForDisco.compareAndSet(true, false)) {
1146            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": finalizing bind after disco timeout");
1147            finalizeBind();
1148        }
1149    }
1150
1151    private void sendStartSession() {
1152        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending legacy session to outdated server");
1153        final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET);
1154        startSession.addChild("session", "urn:ietf:params:xml:ns:xmpp-session");
1155        this.sendUnmodifiedIqPacket(startSession, (account, packet) -> {
1156            if (packet.getType() == IqPacket.TYPE.RESULT) {
1157                sendPostBindInitialization();
1158            } else if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1159                throw new StateChangingError(Account.State.SESSION_FAILURE);
1160            }
1161        }, true);
1162    }
1163
1164    private void sendPostBindInitialization() {
1165        smVersion = 0;
1166        if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
1167            smVersion = 3;
1168        } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) {
1169            smVersion = 2;
1170        }
1171        if (smVersion != 0) {
1172            synchronized (this.mStanzaQueue) {
1173                final EnablePacket enable = new EnablePacket(smVersion);
1174                tagWriter.writeStanzaAsync(enable);
1175                stanzasSent = 0;
1176                mStanzaQueue.clear();
1177            }
1178        }
1179        features.carbonsEnabled = false;
1180        features.blockListRequested = false;
1181        synchronized (this.disco) {
1182            this.disco.clear();
1183        }
1184        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": starting service discovery");
1185        mPendingServiceDiscoveries.set(0);
1186        if (smVersion == 0 || Patches.DISCO_EXCEPTIONS.contains(account.getJid().getDomain().toEscapedString())) {
1187            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": do not wait for service discovery");
1188            mWaitForDisco.set(false);
1189        } else {
1190            mWaitForDisco.set(true);
1191        }
1192        lastDiscoStarted = SystemClock.elapsedRealtime();
1193        mXmppConnectionService.scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
1194        Element caps = streamFeatures.findChild("c");
1195        final String hash = caps == null ? null : caps.getAttribute("hash");
1196        final String ver = caps == null ? null : caps.getAttribute("ver");
1197        ServiceDiscoveryResult discoveryResult = null;
1198        if (hash != null && ver != null) {
1199            discoveryResult = mXmppConnectionService.getCachedServiceDiscoveryResult(new Pair<>(hash, ver));
1200        }
1201        final boolean requestDiscoItemsFirst = !account.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY);
1202        if (requestDiscoItemsFirst) {
1203            sendServiceDiscoveryItems(account.getDomain());
1204        }
1205        if (discoveryResult == null) {
1206            sendServiceDiscoveryInfo(account.getDomain());
1207        } else {
1208            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server caps came from cache");
1209            disco.put(account.getDomain(), discoveryResult);
1210        }
1211        discoverMamPreferences();
1212        sendServiceDiscoveryInfo(account.getJid().asBareJid());
1213        if (!requestDiscoItemsFirst) {
1214            sendServiceDiscoveryItems(account.getDomain());
1215        }
1216
1217        if (!mWaitForDisco.get()) {
1218            finalizeBind();
1219        }
1220        this.lastSessionStarted = SystemClock.elapsedRealtime();
1221    }
1222
1223    private void sendServiceDiscoveryInfo(final Jid jid) {
1224        mPendingServiceDiscoveries.incrementAndGet();
1225        final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1226        iq.setTo(jid);
1227        iq.query("http://jabber.org/protocol/disco#info");
1228        this.sendIqPacket(iq, (account, packet) -> {
1229            if (packet.getType() == IqPacket.TYPE.RESULT) {
1230                boolean advancedStreamFeaturesLoaded;
1231                synchronized (XmppConnection.this.disco) {
1232                    ServiceDiscoveryResult result = new ServiceDiscoveryResult(packet);
1233                    if (jid.equals(account.getDomain())) {
1234                        mXmppConnectionService.databaseBackend.insertDiscoveryResult(result);
1235                    }
1236                    disco.put(jid, result);
1237                    advancedStreamFeaturesLoaded = disco.containsKey(account.getDomain())
1238                            && disco.containsKey(account.getJid().asBareJid());
1239                }
1240                if (advancedStreamFeaturesLoaded && (jid.equals(account.getDomain()) || jid.equals(account.getJid().asBareJid()))) {
1241                    enableAdvancedStreamFeatures();
1242                }
1243            } else if (packet.getType() == IqPacket.TYPE.ERROR) {
1244                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not query disco info for " + jid.toString());
1245                final boolean serverOrAccount = jid.equals(account.getDomain()) || jid.equals(account.getJid().asBareJid());
1246                final boolean advancedStreamFeaturesLoaded;
1247                if (serverOrAccount) {
1248                    synchronized (XmppConnection.this.disco) {
1249                        disco.put(jid, ServiceDiscoveryResult.empty());
1250                        advancedStreamFeaturesLoaded = disco.containsKey(account.getDomain()) && disco.containsKey(account.getJid().asBareJid());
1251                    }
1252                } else {
1253                    advancedStreamFeaturesLoaded = false;
1254                }
1255                if (advancedStreamFeaturesLoaded) {
1256                    enableAdvancedStreamFeatures();
1257                }
1258            }
1259            if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1260                if (mPendingServiceDiscoveries.decrementAndGet() == 0
1261                        && mWaitForDisco.compareAndSet(true, false)) {
1262                    finalizeBind();
1263                }
1264            }
1265        });
1266    }
1267
1268    private void discoverMamPreferences() {
1269        IqPacket request = new IqPacket(IqPacket.TYPE.GET);
1270        request.addChild("prefs", MessageArchiveService.Version.MAM_2.namespace);
1271        sendIqPacket(request, (account, response) -> {
1272            if (response.getType() == IqPacket.TYPE.RESULT) {
1273                Element prefs = response.findChild("prefs", MessageArchiveService.Version.MAM_2.namespace);
1274                isMamPreferenceAlways = "always".equals(prefs == null ? null : prefs.getAttribute("default"));
1275            }
1276        });
1277    }
1278
1279    private void discoverCommands() {
1280        final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
1281        request.setTo(account.getDomain());
1282        request.addChild("query", Namespace.DISCO_ITEMS).setAttribute("node", Namespace.COMMANDS);
1283        sendIqPacket(request, (account, response) -> {
1284            if (response.getType() == IqPacket.TYPE.RESULT) {
1285                final Element query = response.findChild("query", Namespace.DISCO_ITEMS);
1286                if (query == null) {
1287                    return;
1288                }
1289                final HashMap<String, Jid> commands = new HashMap<>();
1290                for (final Element child : query.getChildren()) {
1291                    if ("item".equals(child.getName())) {
1292                        final String node = child.getAttribute("node");
1293                        final Jid jid = child.getAttributeAsJid("jid");
1294                        if (node != null && jid != null) {
1295                            commands.put(node, jid);
1296                        }
1297                    }
1298                }
1299                Log.d(Config.LOGTAG, commands.toString());
1300                synchronized (this.commands) {
1301                    this.commands.clear();
1302                    this.commands.putAll(commands);
1303                }
1304            }
1305        });
1306    }
1307
1308    public boolean isMamPreferenceAlways() {
1309        return isMamPreferenceAlways;
1310    }
1311
1312    private void finalizeBind() {
1313        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": online with resource " + account.getResource());
1314        if (bindListener != null) {
1315            bindListener.onBind(account);
1316        }
1317        changeStatus(Account.State.ONLINE);
1318    }
1319
1320    private void enableAdvancedStreamFeatures() {
1321        if (getFeatures().blocking() && !features.blockListRequested) {
1322            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Requesting block list");
1323            this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
1324        }
1325        for (final OnAdvancedStreamFeaturesLoaded listener : advancedStreamFeaturesLoadedListeners) {
1326            listener.onAdvancedStreamFeaturesAvailable(account);
1327        }
1328        if (getFeatures().carbons() && !features.carbonsEnabled) {
1329            sendEnableCarbons();
1330        }
1331        if (getFeatures().commands()) {
1332            discoverCommands();
1333        }
1334    }
1335
1336    private void sendServiceDiscoveryItems(final Jid server) {
1337        mPendingServiceDiscoveries.incrementAndGet();
1338        final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1339        iq.setTo(server.getDomain());
1340        iq.query("http://jabber.org/protocol/disco#items");
1341        this.sendIqPacket(iq, (account, packet) -> {
1342            if (packet.getType() == IqPacket.TYPE.RESULT) {
1343                final HashSet<Jid> items = new HashSet<>();
1344                final List<Element> elements = packet.query().getChildren();
1345                for (final Element element : elements) {
1346                    if (element.getName().equals("item")) {
1347                        final Jid jid = InvalidJid.getNullForInvalid(element.getAttributeAsJid("jid"));
1348                        if (jid != null && !jid.equals(account.getDomain())) {
1349                            items.add(jid);
1350                        }
1351                    }
1352                }
1353                for (Jid jid : items) {
1354                    sendServiceDiscoveryInfo(jid);
1355                }
1356            } else {
1357                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not query disco items of " + server);
1358            }
1359            if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1360                if (mPendingServiceDiscoveries.decrementAndGet() == 0
1361                        && mWaitForDisco.compareAndSet(true, false)) {
1362                    finalizeBind();
1363                }
1364            }
1365        });
1366    }
1367
1368    private void sendEnableCarbons() {
1369        final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
1370        iq.addChild("enable", "urn:xmpp:carbons:2");
1371        this.sendIqPacket(iq, (account, packet) -> {
1372            if (!packet.hasChild("error")) {
1373                Log.d(Config.LOGTAG, account.getJid().asBareJid()
1374                        + ": successfully enabled carbons");
1375                features.carbonsEnabled = true;
1376            } else {
1377                Log.d(Config.LOGTAG, account.getJid().asBareJid()
1378                        + ": error enableing carbons " + packet.toString());
1379            }
1380        });
1381    }
1382
1383    private void processStreamError(final Tag currentTag) throws IOException {
1384        final Element streamError = tagReader.readElement(currentTag);
1385        if (streamError == null) {
1386            return;
1387        }
1388        if (streamError.hasChild("conflict")) {
1389            account.setResource(createNewResource());
1390            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": switching resource due to conflict (" + account.getResource() + ")");
1391            throw new IOException();
1392        } else if (streamError.hasChild("host-unknown")) {
1393            throw new StateChangingException(Account.State.HOST_UNKNOWN);
1394        } else if (streamError.hasChild("policy-violation")) {
1395            this.lastConnect = SystemClock.elapsedRealtime();
1396            final String text = streamError.findChildContent("text");
1397            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": policy violation. " + text);
1398            failPendingMessages(text);
1399            throw new StateChangingException(Account.State.POLICY_VIOLATION);
1400        } else {
1401            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": stream error " + streamError.toString());
1402            throw new StateChangingException(Account.State.STREAM_ERROR);
1403        }
1404    }
1405
1406    private void failPendingMessages(final String error) {
1407        synchronized (this.mStanzaQueue) {
1408            for (int i = 0; i < mStanzaQueue.size(); ++i) {
1409                final AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
1410                if (stanza instanceof MessagePacket) {
1411                    final MessagePacket packet = (MessagePacket) stanza;
1412                    final String id = packet.getId();
1413                    final Jid to = packet.getTo();
1414                    mXmppConnectionService.markMessage(account,
1415                            to.asBareJid(),
1416                            id,
1417                            Message.STATUS_SEND_FAILED,
1418                            error);
1419                }
1420            }
1421        }
1422    }
1423
1424    private void sendStartStream() throws IOException {
1425        final Tag stream = Tag.start("stream:stream");
1426        stream.setAttribute("to", account.getServer());
1427        stream.setAttribute("version", "1.0");
1428        stream.setAttribute("xml:lang", LocalizedContent.STREAM_LANGUAGE);
1429        stream.setAttribute("xmlns", "jabber:client");
1430        stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
1431        tagWriter.writeTag(stream);
1432    }
1433
1434    private String createNewResource() {
1435        return mXmppConnectionService.getString(R.string.app_name) + '.' + nextRandomId(true);
1436    }
1437
1438    private String nextRandomId() {
1439        return nextRandomId(false);
1440    }
1441
1442    private String nextRandomId(boolean s) {
1443        return CryptoHelper.random(s ? 3 : 9, mXmppConnectionService.getRNG());
1444    }
1445
1446    public String sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
1447        packet.setFrom(account.getJid());
1448        return this.sendUnmodifiedIqPacket(packet, callback, false);
1449    }
1450
1451    public synchronized String sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback, boolean force) {
1452        if (packet.getId() == null) {
1453            packet.setAttribute("id", nextRandomId());
1454        }
1455        if (callback != null) {
1456            synchronized (this.packetCallbacks) {
1457                packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
1458            }
1459        }
1460        this.sendPacket(packet, force);
1461        return packet.getId();
1462    }
1463
1464    public void sendMessagePacket(final MessagePacket packet) {
1465        this.sendPacket(packet);
1466    }
1467
1468    public void sendPresencePacket(final PresencePacket packet) {
1469        this.sendPacket(packet);
1470    }
1471
1472    private synchronized void sendPacket(final AbstractStanza packet) {
1473        sendPacket(packet, false);
1474    }
1475
1476    private synchronized void sendPacket(final AbstractStanza packet, final boolean force) {
1477        if (stanzasSent == Integer.MAX_VALUE) {
1478            resetStreamId();
1479            disconnect(true);
1480            return;
1481        }
1482        synchronized (this.mStanzaQueue) {
1483            if (force || isBound) {
1484                tagWriter.writeStanzaAsync(packet);
1485            } else {
1486                Log.d(Config.LOGTAG, account.getJid().asBareJid() + " do not write stanza to unbound stream " + packet.toString());
1487            }
1488            if (packet instanceof AbstractAcknowledgeableStanza) {
1489                AbstractAcknowledgeableStanza stanza = (AbstractAcknowledgeableStanza) packet;
1490
1491                if (this.mStanzaQueue.size() != 0) {
1492                    int currentHighestKey = this.mStanzaQueue.keyAt(this.mStanzaQueue.size() - 1);
1493                    if (currentHighestKey != stanzasSent) {
1494                        throw new AssertionError("Stanza count messed up");
1495                    }
1496                }
1497
1498                ++stanzasSent;
1499                this.mStanzaQueue.append(stanzasSent, stanza);
1500                if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) {
1501                    if (Config.EXTENDED_SM_LOGGING) {
1502                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": requesting ack for message stanza #" + stanzasSent);
1503                    }
1504                    tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
1505                }
1506            }
1507        }
1508    }
1509
1510    public void sendPing() {
1511        if (!r()) {
1512            final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1513            iq.setFrom(account.getJid());
1514            iq.addChild("ping", Namespace.PING);
1515            this.sendIqPacket(iq, null);
1516        }
1517        this.lastPingSent = SystemClock.elapsedRealtime();
1518    }
1519
1520    public void setOnMessagePacketReceivedListener(
1521            final OnMessagePacketReceived listener) {
1522        this.messageListener = listener;
1523    }
1524
1525    public void setOnUnregisteredIqPacketReceivedListener(
1526            final OnIqPacketReceived listener) {
1527        this.unregisteredIqListener = listener;
1528    }
1529
1530    public void setOnPresencePacketReceivedListener(
1531            final OnPresencePacketReceived listener) {
1532        this.presenceListener = listener;
1533    }
1534
1535    public void setOnJinglePacketReceivedListener(
1536            final OnJinglePacketReceived listener) {
1537        this.jingleListener = listener;
1538    }
1539
1540    public void setOnStatusChangedListener(final OnStatusChanged listener) {
1541        this.statusListener = listener;
1542    }
1543
1544    public void setOnBindListener(final OnBindListener listener) {
1545        this.bindListener = listener;
1546    }
1547
1548    public void setOnMessageAcknowledgeListener(final OnMessageAcknowledged listener) {
1549        this.acknowledgedListener = listener;
1550    }
1551
1552    public void addOnAdvancedStreamFeaturesAvailableListener(final OnAdvancedStreamFeaturesLoaded listener) {
1553        this.advancedStreamFeaturesLoadedListeners.add(listener);
1554    }
1555
1556    private void forceCloseSocket() {
1557        FileBackend.close(this.socket);
1558        FileBackend.close(this.tagReader);
1559    }
1560
1561    public void interrupt() {
1562        if (this.mThread != null) {
1563            this.mThread.interrupt();
1564        }
1565    }
1566
1567    public void disconnect(final boolean force) {
1568        interrupt();
1569        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": disconnecting force=" + force);
1570        if (force) {
1571            forceCloseSocket();
1572        } else {
1573            final TagWriter currentTagWriter = this.tagWriter;
1574            if (currentTagWriter.isActive()) {
1575                currentTagWriter.finish();
1576                final Socket currentSocket = this.socket;
1577                final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch;
1578                try {
1579                    currentTagWriter.await(1, TimeUnit.SECONDS);
1580                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": closing stream");
1581                    currentTagWriter.writeTag(Tag.end("stream:stream"));
1582                    if (streamCountDownLatch != null) {
1583                        if (streamCountDownLatch.await(1, TimeUnit.SECONDS)) {
1584                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": remote ended stream");
1585                        } else {
1586                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": remote has not closed socket. force closing");
1587                        }
1588                    }
1589                } catch (InterruptedException e) {
1590                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": interrupted while gracefully closing stream");
1591                } catch (final IOException e) {
1592                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": io exception during disconnect (" + e.getMessage() + ")");
1593                } finally {
1594                    FileBackend.close(currentSocket);
1595                }
1596            } else {
1597                forceCloseSocket();
1598            }
1599        }
1600    }
1601
1602    private void resetStreamId() {
1603        this.streamId = null;
1604    }
1605
1606    private List<Entry<Jid, ServiceDiscoveryResult>> findDiscoItemsByFeature(final String feature) {
1607        synchronized (this.disco) {
1608            final List<Entry<Jid, ServiceDiscoveryResult>> items = new ArrayList<>();
1609            for (final Entry<Jid, ServiceDiscoveryResult> cursor : this.disco.entrySet()) {
1610                if (cursor.getValue().getFeatures().contains(feature)) {
1611                    items.add(cursor);
1612                }
1613            }
1614            return items;
1615        }
1616    }
1617
1618    public Jid findDiscoItemByFeature(final String feature) {
1619        final List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(feature);
1620        if (items.size() >= 1) {
1621            return items.get(0).getKey();
1622        }
1623        return null;
1624    }
1625
1626    public boolean r() {
1627        if (getFeatures().sm()) {
1628            this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
1629            return true;
1630        } else {
1631            return false;
1632        }
1633    }
1634
1635    public List<String> getMucServersWithholdAccount() {
1636        final List<String> servers = getMucServers();
1637        servers.remove(account.getDomain().toEscapedString());
1638        return servers;
1639    }
1640
1641    public List<String> getMucServers() {
1642        List<String> servers = new ArrayList<>();
1643        synchronized (this.disco) {
1644            for (final Entry<Jid, ServiceDiscoveryResult> cursor : disco.entrySet()) {
1645                final ServiceDiscoveryResult value = cursor.getValue();
1646                if (value.getFeatures().contains("http://jabber.org/protocol/muc")
1647                        && value.hasIdentity("conference", "text")
1648                        && !value.getFeatures().contains("jabber:iq:gateway")
1649                        && !value.hasIdentity("conference", "irc")) {
1650                    servers.add(cursor.getKey().toString());
1651                }
1652            }
1653        }
1654        return servers;
1655    }
1656
1657    public String getMucServer() {
1658        List<String> servers = getMucServers();
1659        return servers.size() > 0 ? servers.get(0) : null;
1660    }
1661
1662    public int getTimeToNextAttempt() {
1663        final int additionalTime = account.getLastErrorStatus() == Account.State.POLICY_VIOLATION ? 3 : 0;
1664        final int interval = Math.min((int) (25 * Math.pow(1.3, (additionalTime + attempt))), 300);
1665        final int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
1666        return interval - secondsSinceLast;
1667    }
1668
1669    public int getAttempt() {
1670        return this.attempt;
1671    }
1672
1673    public Features getFeatures() {
1674        return this.features;
1675    }
1676
1677    public long getLastSessionEstablished() {
1678        final long diff = SystemClock.elapsedRealtime() - this.lastSessionStarted;
1679        return System.currentTimeMillis() - diff;
1680    }
1681
1682    public long getLastConnect() {
1683        return this.lastConnect;
1684    }
1685
1686    public long getLastPingSent() {
1687        return this.lastPingSent;
1688    }
1689
1690    public long getLastDiscoStarted() {
1691        return this.lastDiscoStarted;
1692    }
1693
1694    public long getLastPacketReceived() {
1695        return this.lastPacketReceived;
1696    }
1697
1698    public void sendActive() {
1699        this.sendPacket(new ActivePacket());
1700    }
1701
1702    public void sendInactive() {
1703        this.sendPacket(new InactivePacket());
1704    }
1705
1706    public void resetAttemptCount(boolean resetConnectTime) {
1707        this.attempt = 0;
1708        if (resetConnectTime) {
1709            this.lastConnect = 0;
1710        }
1711    }
1712
1713    public void setInteractive(boolean interactive) {
1714        this.mInteractive = interactive;
1715    }
1716
1717    public Identity getServerIdentity() {
1718        synchronized (this.disco) {
1719            ServiceDiscoveryResult result = disco.get(account.getJid().getDomain());
1720            if (result == null) {
1721                return Identity.UNKNOWN;
1722            }
1723            for (final ServiceDiscoveryResult.Identity id : result.getIdentities()) {
1724                if (id.getType().equals("im") && id.getCategory().equals("server") && id.getName() != null) {
1725                    switch (id.getName()) {
1726                        case "Prosody":
1727                            return Identity.PROSODY;
1728                        case "ejabberd":
1729                            return Identity.EJABBERD;
1730                        case "Slack-XMPP":
1731                            return Identity.SLACK;
1732                    }
1733                }
1734            }
1735        }
1736        return Identity.UNKNOWN;
1737    }
1738
1739    private IqGenerator getIqGenerator() {
1740        return mXmppConnectionService.getIqGenerator();
1741    }
1742
1743    public enum Identity {
1744        FACEBOOK,
1745        SLACK,
1746        EJABBERD,
1747        PROSODY,
1748        NIMBUZZ,
1749        UNKNOWN
1750    }
1751
1752    private class MyKeyManager implements X509KeyManager {
1753        @Override
1754        public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) {
1755            return account.getPrivateKeyAlias();
1756        }
1757
1758        @Override
1759        public String chooseServerAlias(String s, Principal[] principals, Socket socket) {
1760            return null;
1761        }
1762
1763        @Override
1764        public X509Certificate[] getCertificateChain(String alias) {
1765            Log.d(Config.LOGTAG, "getting certificate chain");
1766            try {
1767                return KeyChain.getCertificateChain(mXmppConnectionService, alias);
1768            } catch (Exception e) {
1769                Log.d(Config.LOGTAG, e.getMessage());
1770                return new X509Certificate[0];
1771            }
1772        }
1773
1774        @Override
1775        public String[] getClientAliases(String s, Principal[] principals) {
1776            final String alias = account.getPrivateKeyAlias();
1777            return alias != null ? new String[]{alias} : new String[0];
1778        }
1779
1780        @Override
1781        public String[] getServerAliases(String s, Principal[] principals) {
1782            return new String[0];
1783        }
1784
1785        @Override
1786        public PrivateKey getPrivateKey(String alias) {
1787            try {
1788                return KeyChain.getPrivateKey(mXmppConnectionService, alias);
1789            } catch (Exception e) {
1790                return null;
1791            }
1792        }
1793    }
1794
1795    private static class StateChangingError extends Error {
1796        private final Account.State state;
1797
1798        public StateChangingError(Account.State state) {
1799            this.state = state;
1800        }
1801    }
1802
1803    private static class StateChangingException extends IOException {
1804        private final Account.State state;
1805
1806        public StateChangingException(Account.State state) {
1807            this.state = state;
1808        }
1809    }
1810
1811    public class Features {
1812        XmppConnection connection;
1813        private boolean carbonsEnabled = false;
1814        private boolean encryptionEnabled = false;
1815        private boolean blockListRequested = false;
1816
1817        public Features(final XmppConnection connection) {
1818            this.connection = connection;
1819        }
1820
1821        private boolean hasDiscoFeature(final Jid server, final String feature) {
1822            synchronized (XmppConnection.this.disco) {
1823                return connection.disco.containsKey(server) &&
1824                        connection.disco.get(server).getFeatures().contains(feature);
1825            }
1826        }
1827
1828        public boolean carbons() {
1829            return hasDiscoFeature(account.getDomain(), "urn:xmpp:carbons:2");
1830        }
1831
1832        public boolean commands() {
1833            return hasDiscoFeature(account.getDomain(), Namespace.COMMANDS);
1834        }
1835
1836        public boolean easyOnboardingInvites() {
1837            synchronized (commands) {
1838                return commands.containsKey(Namespace.EASY_ONBOARDING_INVITE);
1839            }
1840        }
1841
1842        public boolean bookmarksConversion() {
1843            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.BOOKMARKS_CONVERSION) && pepPublishOptions();
1844        }
1845
1846        public boolean avatarConversion() {
1847            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.AVATAR_CONVERSION) && pepPublishOptions();
1848        }
1849
1850        public boolean blocking() {
1851            return hasDiscoFeature(account.getDomain(), Namespace.BLOCKING);
1852        }
1853
1854        public boolean spamReporting() {
1855            return hasDiscoFeature(account.getDomain(), "urn:xmpp:reporting:reason:spam:0");
1856        }
1857
1858        public boolean flexibleOfflineMessageRetrieval() {
1859            return hasDiscoFeature(account.getDomain(), Namespace.FLEXIBLE_OFFLINE_MESSAGE_RETRIEVAL);
1860        }
1861
1862        public boolean register() {
1863            return hasDiscoFeature(account.getDomain(), Namespace.REGISTER);
1864        }
1865
1866        public boolean invite() {
1867            return connection.streamFeatures != null && connection.streamFeatures.hasChild("register", Namespace.INVITE);
1868        }
1869
1870        public boolean sm() {
1871            return streamId != null
1872                    || (connection.streamFeatures != null && connection.streamFeatures.hasChild("sm"));
1873        }
1874
1875        public boolean csi() {
1876            return connection.streamFeatures != null && connection.streamFeatures.hasChild("csi", "urn:xmpp:csi:0");
1877        }
1878
1879        public boolean pep() {
1880            synchronized (XmppConnection.this.disco) {
1881                ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid());
1882                return info != null && info.hasIdentity("pubsub", "pep");
1883            }
1884        }
1885
1886        public boolean pepPersistent() {
1887            synchronized (XmppConnection.this.disco) {
1888                ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid());
1889                return info != null && info.getFeatures().contains("http://jabber.org/protocol/pubsub#persistent-items");
1890            }
1891        }
1892
1893        public boolean pepPublishOptions() {
1894            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.PUBSUB_PUBLISH_OPTIONS);
1895        }
1896
1897        public boolean pepOmemoWhitelisted() {
1898            return hasDiscoFeature(account.getJid().asBareJid(), AxolotlService.PEP_OMEMO_WHITELISTED);
1899        }
1900
1901        public boolean mam() {
1902            return MessageArchiveService.Version.has(getAccountFeatures());
1903        }
1904
1905        public List<String> getAccountFeatures() {
1906            ServiceDiscoveryResult result = connection.disco.get(account.getJid().asBareJid());
1907            return result == null ? Collections.emptyList() : result.getFeatures();
1908        }
1909
1910        public boolean push() {
1911            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.PUSH)
1912                    || hasDiscoFeature(account.getDomain(), Namespace.PUSH);
1913        }
1914
1915        public boolean rosterVersioning() {
1916            return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
1917        }
1918
1919        public void setBlockListRequested(boolean value) {
1920            this.blockListRequested = value;
1921        }
1922
1923        public boolean httpUpload(long filesize) {
1924            if (Config.DISABLE_HTTP_UPLOAD) {
1925                return false;
1926            } else {
1927                for (String namespace : new String[]{Namespace.HTTP_UPLOAD, Namespace.HTTP_UPLOAD_LEGACY}) {
1928                    List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(namespace);
1929                    if (items.size() > 0) {
1930                        try {
1931                            long maxsize = Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(namespace, "max-file-size"));
1932                            if (filesize <= maxsize) {
1933                                return true;
1934                            } else {
1935                                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": http upload is not available for files with size " + filesize + " (max is " + maxsize + ")");
1936                                return false;
1937                            }
1938                        } catch (Exception e) {
1939                            return true;
1940                        }
1941                    }
1942                }
1943                return false;
1944            }
1945        }
1946
1947        public boolean useLegacyHttpUpload() {
1948            return findDiscoItemByFeature(Namespace.HTTP_UPLOAD) == null && findDiscoItemByFeature(Namespace.HTTP_UPLOAD_LEGACY) != null;
1949        }
1950
1951        public long getMaxHttpUploadSize() {
1952            for (String namespace : new String[]{Namespace.HTTP_UPLOAD, Namespace.HTTP_UPLOAD_LEGACY}) {
1953                List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(namespace);
1954                if (items.size() > 0) {
1955                    try {
1956                        return Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(namespace, "max-file-size"));
1957                    } catch (Exception e) {
1958                        //ignored
1959                    }
1960                }
1961            }
1962            return -1;
1963        }
1964
1965        public boolean stanzaIds() {
1966            return hasDiscoFeature(account.getJid().asBareJid(), Namespace.STANZA_IDS);
1967        }
1968
1969        public boolean bookmarks2() {
1970            return Config.USE_BOOKMARKS2 /* || hasDiscoFeature(account.getJid().asBareJid(), Namespace.BOOKMARKS2_COMPAT)*/;
1971        }
1972
1973        public boolean externalServiceDiscovery() {
1974            return hasDiscoFeature(account.getDomain(), Namespace.EXTERNAL_SERVICE_DISCOVERY);
1975        }
1976    }
1977}