diff options
Diffstat (limited to 'src/main/java/eu/siacs')
-rw-r--r-- | src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 7b59c812..4dd7cfc7 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -90,8 +90,10 @@ public class XmppConnection implements Runnable { private int smVersion = 3; private SparseArray<String> messageReceipts = new SparseArray<>(); - private boolean usingCompression = false; - private boolean usingEncryption = false; + private boolean enabledCompression = false; + private boolean enabledEncryption = false; + private boolean enabledCarbons = false; + private int stanzasReceived = 0; private int stanzasSent = 0; private long lastPaketReceived = 0; @@ -140,8 +142,8 @@ public class XmppConnection implements Runnable { protected void connect() { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting"); - usingCompression = false; - usingEncryption = false; + enabledCompression = false; + enabledEncryption = false; lastConnect = SystemClock.elapsedRealtime(); lastPingSent = SystemClock.elapsedRealtime(); this.attempt++; @@ -355,8 +357,9 @@ public class XmppConnection implements Runnable { } catch (final NumberFormatException ignored) { } + sendServiceDiscoveryInfo(account.getServer()); + sendServiceDiscoveryItems(account.getServer()); sendInitialPing(); - } else if (nextTag.isStart("r")) { tagReader.readElement(nextTag); AckPacket ack = new AckPacket(this.stanzasReceived, smVersion); @@ -536,7 +539,7 @@ public class XmppConnection implements Runnable { sendStartStream(); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": compression enabled"); - usingCompression = true; + enabledCompression = true; processStream(tagReader.readTag()); } @@ -605,7 +608,7 @@ public class XmppConnection implements Runnable { sendStartStream(); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS connection established"); - usingEncryption = true; + enabledEncryption = true; processStream(tagReader.readTag()); sslSocket.close(); } catch (final NoSuchAlgorithmException | KeyManagementException e1) { @@ -616,20 +619,20 @@ public class XmppConnection implements Runnable { private void processStreamFeatures(Tag currentTag) throws XmlPullParserException, IOException { this.streamFeatures = tagReader.readElement(currentTag); - if (this.streamFeatures.hasChild("starttls") && !usingEncryption) { + if (this.streamFeatures.hasChild("starttls") && !enabledEncryption) { sendStartTLS(); } else if (compressionAvailable()) { sendCompressionZlib(); } else if (this.streamFeatures.hasChild("register") && account.isOptionSet(Account.OPTION_REGISTER) - && usingEncryption) { + && enabledEncryption) { sendRegistryRequest(); } else if (!this.streamFeatures.hasChild("register") && account.isOptionSet(Account.OPTION_REGISTER)) { changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED); disconnect(true); } else if (this.streamFeatures.hasChild("mechanisms") - && shouldAuthenticate && usingEncryption) { + && shouldAuthenticate && enabledEncryption) { final List<String> mechanisms = extractMechanisms(streamFeatures .findChild("mechanisms")); final Element auth = new Element("auth"); @@ -787,6 +790,8 @@ public class XmppConnection implements Runnable { stanzasSent = 0; messageReceipts.clear(); } + enabledCarbons = false; + disco.clear(); sendServiceDiscoveryInfo(account.getServer()); sendServiceDiscoveryItems(account.getServer()); if (bindListener != null) { @@ -812,32 +817,40 @@ public class XmppConnection implements Runnable { } private void sendServiceDiscoveryInfo(final Jid server) { - final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); - iq.setTo(server.toDomainJid()); - iq.query("http://jabber.org/protocol/disco#info"); - this.sendIqPacket(iq, new OnIqPacketReceived() { + if (disco.containsKey(server.toDomainJid().toString())) { + if (account.getServer().equals(server.toDomainJid())) { + enableAdvancedStreamFeatures(); + } + } else { + final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); + iq.setTo(server.toDomainJid()); + iq.query("http://jabber.org/protocol/disco#info"); + this.sendIqPacket(iq, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - final List<Element> elements = packet.query().getChildren(); - final List<String> features = new ArrayList<>(); - for (Element element : elements) { - if (element.getName().equals("feature")) { - features.add(element.getAttribute("var")); + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + final List<Element> elements = packet.query().getChildren(); + final List<String> features = new ArrayList<>(); + for (Element element : elements) { + if (element.getName().equals("feature")) { + features.add(element.getAttribute("var")); + } } - } - disco.put(server.toDomainJid().toString(), features); + disco.put(server.toDomainJid().toString(), features); - if (account.getServer().equals(server.toDomainJid())) { - enableAdvancedStreamFeatures(); + if (account.getServer().equals(server.toDomainJid())) { + enableAdvancedStreamFeatures(); + } } - } - }); + }); + } } private void enableAdvancedStreamFeatures() { if (getFeatures().carbons()) { - sendEnableCarbons(); + if (!enabledCarbons) { + sendEnableCarbons(); + } } } @@ -874,6 +887,7 @@ public class XmppConnection implements Runnable { if (!packet.hasChild("error")) { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": successfully enabled carbons"); + enabledCarbons = true; } else { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": error enableing carbons " + packet.toString()); @@ -1147,7 +1161,7 @@ public class XmppConnection implements Runnable { } public boolean compression() { - return connection.usingCompression; + return connection.enabledCompression; } } } |