1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 17:19:39 +02:00

Only add Entity Capabilities extension to available presences

Also don't override eventually send presences on
updateLocalEntityCaps(), instead save the last sent Presence stanza and
re-send that stanza.

SMACK-669.
This commit is contained in:
Florian Schmaus 2015-05-21 22:04:26 +02:00
parent c120bc1cbc
commit 385798f9ba
3 changed files with 44 additions and 12 deletions

View file

@ -30,6 +30,7 @@ import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PresenceTypeFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.StanzaTypeFilter;
@ -94,7 +95,6 @@ public class EntityCapsManager extends Manager {
ELEMENT, NAMESPACE));
private static final StanzaFilter PRESENCES_WITHOUT_CAPS = new AndFilter(new StanzaTypeFilter(Presence.class), new NotFilter(new StanzaExtensionFilter(
ELEMENT, NAMESPACE)));
private static final StanzaFilter PRESENCES = StanzaTypeFilter.PRESENCE;
/**
* Map of "node + '#' + hash" to DiscoverInfo data
@ -262,7 +262,7 @@ public class EntityCapsManager extends Manager {
private boolean entityCapsEnabled;
private CapsVersionAndHash currentCapsVersion;
private boolean presenceSend = false;
private volatile Presence presenceSend;
/**
* The entity node String used by this EntityCapsManager instance.
@ -291,7 +291,7 @@ public class EntityCapsManager extends Manager {
// Reset presenceSend when the connection was not resumed
if (!resumed) {
presenceSend = false;
presenceSend = null;
}
}
private void processCapsStreamFeatureIfAvailable(XMPPConnection connection) {
@ -339,23 +339,26 @@ public class EntityCapsManager extends Manager {
connection.addPacketSendingListener(new StanzaListener() {
@Override
public void processPacket(Stanza packet) {
presenceSend = true;
presenceSend = (Presence) packet;
}
}, PRESENCES);
}, PresenceTypeFilter.AVAILABLE);
// Intercept presence packages and add caps data when intended.
// XEP-0115 specifies that a client SHOULD include entity capabilities
// with every presence notification it sends.
StanzaListener packetInterceptor = new StanzaListener() {
public void processPacket(Stanza packet) {
if (!entityCapsEnabled)
if (!entityCapsEnabled) {
// Be sure to not send stanzas with the caps extension if it's not enabled
packet.removeExtension(CapsExtension.ELEMENT, CapsExtension.NAMESPACE);
return;
}
CapsVersionAndHash capsVersionAndHash = getCapsVersionAndHash();
CapsExtension caps = new CapsExtension(entityNode, capsVersionAndHash.version, capsVersionAndHash.hash);
packet.addExtension(caps);
packet.overrideExtension(caps);
}
};
connection.addPacketInterceptor(packetInterceptor, PRESENCES);
connection.addPacketInterceptor(packetInterceptor, PresenceTypeFilter.AVAILABLE);
// It's important to do this as last action. Since it changes the
// behavior of the SDM in some ways
sdm.setEntityCapsManager(this);
@ -502,15 +505,14 @@ public class EntityCapsManager extends Manager {
}
});
// Send an empty presence, and let the packet interceptor
// Re-send the last sent presence, and let the stanza interceptor
// add a <c/> node to it.
// See http://xmpp.org/extensions/xep-0115.html#advertise
// We only send a presence packet if there was already one send
// to respect ConnectionConfiguration.isSendPresence()
if (connection != null && connection.isAuthenticated() && presenceSend) {
Presence presence = new Presence(Presence.Type.available);
if (connection != null && connection.isAuthenticated() && presenceSend != null) {
try {
connection.sendStanza(presence);
connection.sendStanza(presenceSend.cloneWithNewId());
}
catch (NotConnectedException e) {
LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e);