implement client support for muc push

Staying connected to a MUC room hosted on a remote server can be challenging.

If a server reboots it will usually send a shut down notification to all
participants. However even if a client knows that a server was shut down it
doesn’t know when it comes up again. In some corner cases that shut down
notification might not even be delivered successfully leaving the client in a
state where it thinks it is connected but it really isn’t.

The possible work around implemented in this commit is to register the clients
full JID (user@domain.tld/Conversations.r4nd) as an App Server according to
XEP-0357 with the room. (Conversations checks for the push:0 namespace on the
room.)

After cycling through a reboot the first message send to a room will trigger
pubsub notifications to each registered full JID. This event will be used to
trigger a XEP-0410 ping and if necessary a subsequent rejoin of the MUC.

If the resource has become unavailable during down time of the MUC server the
user’s server will respond with an IQ error which in turn leads to the MUC
server disabling that push target.

Leaving a MUC will send a `disable` command. If sending that disable command
failed for some reason (network outage) and the client receives a pubsub
notification for a room it is no longer joined in it will respond with an
item-not-found IQ error which also disables subsequent pushes from the server.

Note: We 0410-ping before a join to avoid unnecessary full joins which can be
quite costly. Further client side optimazations will also surpress pings when
a ping is already in flight to further save traffic.
This commit is contained in:
Daniel Gultsch 2019-06-24 18:16:03 +02:00
parent b6d059ed89
commit e467fe341e
6 changed files with 424 additions and 349 deletions

View File

@ -17,6 +17,7 @@ import eu.siacs.conversations.services.AvatarService;
import eu.siacs.conversations.services.MessageArchiveService; import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.utils.JidHelper; import eu.siacs.conversations.utils.JidHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.chatstate.ChatState; import eu.siacs.conversations.xmpp.chatstate.ChatState;
import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.forms.Data;
import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.forms.Field;
@ -113,6 +114,10 @@ public class MucOptions {
return MessageArchiveService.Version.has(getFeatures()); return MessageArchiveService.Version.has(getFeatures());
} }
public boolean push() {
return getFeatures().contains(Namespace.PUSH);
}
public boolean updateConfiguration(ServiceDiscoveryResult serviceDiscoveryResult) { public boolean updateConfiguration(ServiceDiscoveryResult serviceDiscoveryResult) {
this.serviceDiscoveryResult = serviceDiscoveryResult; this.serviceDiscoveryResult = serviceDiscoveryResult;
String name; String name;

View File

@ -436,16 +436,26 @@ public class IqGenerator extends AbstractGenerator {
return packet; return packet;
} }
public IqPacket enablePush(Jid jid, String node, String secret) { public IqPacket enablePush(final Jid jid, final String node, final String secret) {
IqPacket packet = new IqPacket(IqPacket.TYPE.SET); IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
Element enable = packet.addChild("enable", "urn:xmpp:push:0"); Element enable = packet.addChild("enable", Namespace.PUSH);
enable.setAttribute("jid", jid.toString()); enable.setAttribute("jid", jid.toString());
enable.setAttribute("node", node); enable.setAttribute("node", node);
Data data = new Data(); if (secret != null) {
data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS); Data data = new Data();
data.put("secret", secret); data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
data.submit(); data.put("secret", secret);
enable.addChild(data); data.submit();
enable.addChild(data);
}
return packet;
}
public IqPacket disablePush(final Jid jid, final String node) {
IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
Element disable = packet.addChild("disable", Namespace.PUSH);
disable.setAttribute("jid", jid.toString());
disable.setAttribute("node", node);
return packet; return packet;
} }

View File

@ -26,6 +26,7 @@ import eu.siacs.conversations.Config;
import eu.siacs.conversations.crypto.axolotl.AxolotlService; import eu.siacs.conversations.crypto.axolotl.AxolotlService;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
@ -37,360 +38,383 @@ import rocks.xmpp.addr.Jid;
public class IqParser extends AbstractParser implements OnIqPacketReceived { public class IqParser extends AbstractParser implements OnIqPacketReceived {
public IqParser(final XmppConnectionService service) { public IqParser(final XmppConnectionService service) {
super(service); super(service);
} }
private void rosterItems(final Account account, final Element query) { private void rosterItems(final Account account, final Element query) {
final String version = query.getAttribute("ver"); final String version = query.getAttribute("ver");
if (version != null) { if (version != null) {
account.getRoster().setVersion(version); account.getRoster().setVersion(version);
} }
for (final Element item : query.getChildren()) { for (final Element item : query.getChildren()) {
if (item.getName().equals("item")) { if (item.getName().equals("item")) {
final Jid jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid")); final Jid jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid"));
if (jid == null) { if (jid == null) {
continue; continue;
} }
final String name = item.getAttribute("name"); final String name = item.getAttribute("name");
final String subscription = item.getAttribute("subscription"); final String subscription = item.getAttribute("subscription");
final Contact contact = account.getRoster().getContact(jid); final Contact contact = account.getRoster().getContact(jid);
boolean bothPre = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM); boolean bothPre = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM);
if (!contact.getOption(Contact.Options.DIRTY_PUSH)) { if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
contact.setServerName(name); contact.setServerName(name);
contact.parseGroupsFromElement(item); contact.parseGroupsFromElement(item);
} }
if ("remove".equals(subscription)) { if ("remove".equals(subscription)) {
contact.resetOption(Contact.Options.IN_ROSTER); contact.resetOption(Contact.Options.IN_ROSTER);
contact.resetOption(Contact.Options.DIRTY_DELETE); contact.resetOption(Contact.Options.DIRTY_DELETE);
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT); contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
} else { } else {
contact.setOption(Contact.Options.IN_ROSTER); contact.setOption(Contact.Options.IN_ROSTER);
contact.resetOption(Contact.Options.DIRTY_PUSH); contact.resetOption(Contact.Options.DIRTY_PUSH);
contact.parseSubscriptionFromElement(item); contact.parseSubscriptionFromElement(item);
} }
boolean both = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM); boolean both = contact.getOption(Contact.Options.TO) && contact.getOption(Contact.Options.FROM);
if ((both != bothPre) && both) { if ((both != bothPre) && both) {
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": gained mutual presence subscription with "+contact.getJid()); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": gained mutual presence subscription with " + contact.getJid());
AxolotlService axolotlService = account.getAxolotlService(); AxolotlService axolotlService = account.getAxolotlService();
if (axolotlService != null) { if (axolotlService != null) {
axolotlService.clearErrorsInFetchStatusMap(contact.getJid()); axolotlService.clearErrorsInFetchStatusMap(contact.getJid());
} }
} }
mXmppConnectionService.getAvatarService().clear(contact); mXmppConnectionService.getAvatarService().clear(contact);
} }
} }
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateRosterUi(); mXmppConnectionService.updateRosterUi();
mXmppConnectionService.getShortcutService().refresh(); mXmppConnectionService.getShortcutService().refresh();
mXmppConnectionService.syncRoster(account); mXmppConnectionService.syncRoster(account);
} }
public String avatarData(final IqPacket packet) { public String avatarData(final IqPacket packet) {
final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB); final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB);
if (pubsub == null) { if (pubsub == null) {
return null; return null;
} }
final Element items = pubsub.findChild("items"); final Element items = pubsub.findChild("items");
if (items == null) { if (items == null) {
return null; return null;
} }
return super.avatarData(items); return super.avatarData(items);
} }
public Element getItem(final IqPacket packet) { public Element getItem(final IqPacket packet) {
final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB); final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB);
if (pubsub == null) { if (pubsub == null) {
return null; return null;
} }
final Element items = pubsub.findChild("items"); final Element items = pubsub.findChild("items");
if (items == null) { if (items == null) {
return null; return null;
} }
return items.findChild("item"); return items.findChild("item");
} }
@NonNull @NonNull
public Set<Integer> deviceIds(final Element item) { public Set<Integer> deviceIds(final Element item) {
Set<Integer> deviceIds = new HashSet<>(); Set<Integer> deviceIds = new HashSet<>();
if (item != null) { if (item != null) {
final Element list = item.findChild("list"); final Element list = item.findChild("list");
if (list != null) { if (list != null) {
for (Element device : list.getChildren()) { for (Element device : list.getChildren()) {
if (!device.getName().equals("device")) { if (!device.getName().equals("device")) {
continue; continue;
} }
try { try {
Integer id = Integer.valueOf(device.getAttribute("id")); Integer id = Integer.valueOf(device.getAttribute("id"));
deviceIds.add(id); deviceIds.add(id);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Encountered invalid <device> node in PEP ("+e.getMessage()+"):" + device.toString()+ ", skipping..."); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Encountered invalid <device> node in PEP (" + e.getMessage() + "):" + device.toString() + ", skipping...");
continue; continue;
} }
} }
} }
} }
return deviceIds; return deviceIds;
} }
public Integer signedPreKeyId(final Element bundle) { public Integer signedPreKeyId(final Element bundle) {
final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic"); final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic");
if(signedPreKeyPublic == null) { if (signedPreKeyPublic == null) {
return null; return null;
} }
try { try {
return Integer.valueOf(signedPreKeyPublic.getAttribute("signedPreKeyId")); return Integer.valueOf(signedPreKeyPublic.getAttribute("signedPreKeyId"));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return null; return null;
} }
} }
public ECPublicKey signedPreKeyPublic(final Element bundle) { public ECPublicKey signedPreKeyPublic(final Element bundle) {
ECPublicKey publicKey = null; ECPublicKey publicKey = null;
final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic"); final Element signedPreKeyPublic = bundle.findChild("signedPreKeyPublic");
if(signedPreKeyPublic == null) { if (signedPreKeyPublic == null) {
return null; return null;
} }
try { try {
publicKey = Curve.decodePoint(Base64.decode(signedPreKeyPublic.getContent(),Base64.DEFAULT), 0); publicKey = Curve.decodePoint(Base64.decode(signedPreKeyPublic.getContent(), Base64.DEFAULT), 0);
} catch (Throwable e) { } catch (Throwable e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Invalid signedPreKeyPublic in PEP: " + e.getMessage()); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid signedPreKeyPublic in PEP: " + e.getMessage());
} }
return publicKey; return publicKey;
} }
public byte[] signedPreKeySignature(final Element bundle) { public byte[] signedPreKeySignature(final Element bundle) {
final Element signedPreKeySignature = bundle.findChild("signedPreKeySignature"); final Element signedPreKeySignature = bundle.findChild("signedPreKeySignature");
if(signedPreKeySignature == null) { if (signedPreKeySignature == null) {
return null; return null;
} }
try { try {
return Base64.decode(signedPreKeySignature.getContent(), Base64.DEFAULT); return Base64.decode(signedPreKeySignature.getContent(), Base64.DEFAULT);
} catch (Throwable e) { } catch (Throwable e) {
Log.e(Config.LOGTAG,AxolotlService.LOGPREFIX+" : Invalid base64 in signedPreKeySignature"); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : Invalid base64 in signedPreKeySignature");
return null; return null;
} }
} }
public IdentityKey identityKey(final Element bundle) { public IdentityKey identityKey(final Element bundle) {
IdentityKey identityKey = null; IdentityKey identityKey = null;
final Element identityKeyElement = bundle.findChild("identityKey"); final Element identityKeyElement = bundle.findChild("identityKey");
if(identityKeyElement == null) { if (identityKeyElement == null) {
return null; return null;
} }
try { try {
identityKey = new IdentityKey(Base64.decode(identityKeyElement.getContent(), Base64.DEFAULT), 0); identityKey = new IdentityKey(Base64.decode(identityKeyElement.getContent(), Base64.DEFAULT), 0);
} catch (Throwable e) { } catch (Throwable e) {
Log.e(Config.LOGTAG,AxolotlService.LOGPREFIX+" : "+"Invalid identityKey in PEP: "+e.getMessage()); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid identityKey in PEP: " + e.getMessage());
} }
return identityKey; return identityKey;
} }
public Map<Integer, ECPublicKey> preKeyPublics(final IqPacket packet) { public Map<Integer, ECPublicKey> preKeyPublics(final IqPacket packet) {
Map<Integer, ECPublicKey> preKeyRecords = new HashMap<>(); Map<Integer, ECPublicKey> preKeyRecords = new HashMap<>();
Element item = getItem(packet); Element item = getItem(packet);
if (item == null) { if (item == null) {
Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Couldn't find <item> in bundle IQ packet: " + packet); Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Couldn't find <item> in bundle IQ packet: " + packet);
return null; return null;
} }
final Element bundleElement = item.findChild("bundle"); final Element bundleElement = item.findChild("bundle");
if(bundleElement == null) { if (bundleElement == null) {
return null; return null;
} }
final Element prekeysElement = bundleElement.findChild("prekeys"); final Element prekeysElement = bundleElement.findChild("prekeys");
if(prekeysElement == null) { if (prekeysElement == null) {
Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Couldn't find <prekeys> in bundle IQ packet: " + packet); Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Couldn't find <prekeys> in bundle IQ packet: " + packet);
return null; return null;
} }
for(Element preKeyPublicElement : prekeysElement.getChildren()) { for (Element preKeyPublicElement : prekeysElement.getChildren()) {
if(!preKeyPublicElement.getName().equals("preKeyPublic")){ if (!preKeyPublicElement.getName().equals("preKeyPublic")) {
Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Encountered unexpected tag in prekeys list: " + preKeyPublicElement); Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Encountered unexpected tag in prekeys list: " + preKeyPublicElement);
continue; continue;
} }
Integer preKeyId = null; Integer preKeyId = null;
try { try {
preKeyId = Integer.valueOf(preKeyPublicElement.getAttribute("preKeyId")); preKeyId = Integer.valueOf(preKeyPublicElement.getAttribute("preKeyId"));
final ECPublicKey preKeyPublic = Curve.decodePoint(Base64.decode(preKeyPublicElement.getContent(), Base64.DEFAULT), 0); final ECPublicKey preKeyPublic = Curve.decodePoint(Base64.decode(preKeyPublicElement.getContent(), Base64.DEFAULT), 0);
preKeyRecords.put(preKeyId, preKeyPublic); preKeyRecords.put(preKeyId, preKeyPublic);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"could not parse preKeyId from preKey "+preKeyPublicElement.toString()); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "could not parse preKeyId from preKey " + preKeyPublicElement.toString());
} catch (Throwable e) { } catch (Throwable e) {
Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Invalid preKeyPublic (ID="+preKeyId+") in PEP: "+ e.getMessage()+", skipping..."); Log.e(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + "Invalid preKeyPublic (ID=" + preKeyId + ") in PEP: " + e.getMessage() + ", skipping...");
} }
} }
return preKeyRecords; return preKeyRecords;
} }
public Pair<X509Certificate[],byte[]> verification(final IqPacket packet) { public Pair<X509Certificate[], byte[]> verification(final IqPacket packet) {
Element item = getItem(packet); Element item = getItem(packet);
Element verification = item != null ? item.findChild("verification",AxolotlService.PEP_PREFIX) : null; Element verification = item != null ? item.findChild("verification", AxolotlService.PEP_PREFIX) : null;
Element chain = verification != null ? verification.findChild("chain") : null; Element chain = verification != null ? verification.findChild("chain") : null;
Element signature = verification != null ? verification.findChild("signature") : null; Element signature = verification != null ? verification.findChild("signature") : null;
if (chain != null && signature != null) { if (chain != null && signature != null) {
List<Element> certElements = chain.getChildren(); List<Element> certElements = chain.getChildren();
X509Certificate[] certificates = new X509Certificate[certElements.size()]; X509Certificate[] certificates = new X509Certificate[certElements.size()];
try { try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
int i = 0; int i = 0;
for(Element cert : certElements) { for (Element cert : certElements) {
certificates[i] = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(Base64.decode(cert.getContent(),Base64.DEFAULT))); certificates[i] = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(Base64.decode(cert.getContent(), Base64.DEFAULT)));
++i; ++i;
} }
return new Pair<>(certificates,Base64.decode(signature.getContent(),Base64.DEFAULT)); return new Pair<>(certificates, Base64.decode(signature.getContent(), Base64.DEFAULT));
} catch (CertificateException e) { } catch (CertificateException e) {
return null; return null;
} }
} else { } else {
return null; return null;
} }
} }
public PreKeyBundle bundle(final IqPacket bundle) { public PreKeyBundle bundle(final IqPacket bundle) {
Element bundleItem = getItem(bundle); Element bundleItem = getItem(bundle);
if(bundleItem == null) { if (bundleItem == null) {
return null; return null;
} }
final Element bundleElement = bundleItem.findChild("bundle"); final Element bundleElement = bundleItem.findChild("bundle");
if(bundleElement == null) { if (bundleElement == null) {
return null; return null;
} }
ECPublicKey signedPreKeyPublic = signedPreKeyPublic(bundleElement); ECPublicKey signedPreKeyPublic = signedPreKeyPublic(bundleElement);
Integer signedPreKeyId = signedPreKeyId(bundleElement); Integer signedPreKeyId = signedPreKeyId(bundleElement);
byte[] signedPreKeySignature = signedPreKeySignature(bundleElement); byte[] signedPreKeySignature = signedPreKeySignature(bundleElement);
IdentityKey identityKey = identityKey(bundleElement); IdentityKey identityKey = identityKey(bundleElement);
if(signedPreKeyId == null || signedPreKeyPublic == null || identityKey == null) { if (signedPreKeyId == null || signedPreKeyPublic == null || identityKey == null) {
return null; return null;
} }
return new PreKeyBundle(0, 0, 0, null, return new PreKeyBundle(0, 0, 0, null,
signedPreKeyId, signedPreKeyPublic, signedPreKeySignature, identityKey); signedPreKeyId, signedPreKeyPublic, signedPreKeySignature, identityKey);
} }
public List<PreKeyBundle> preKeys(final IqPacket preKeys) { public List<PreKeyBundle> preKeys(final IqPacket preKeys) {
List<PreKeyBundle> bundles = new ArrayList<>(); List<PreKeyBundle> bundles = new ArrayList<>();
Map<Integer, ECPublicKey> preKeyPublics = preKeyPublics(preKeys); Map<Integer, ECPublicKey> preKeyPublics = preKeyPublics(preKeys);
if ( preKeyPublics != null) { if (preKeyPublics != null) {
for (Integer preKeyId : preKeyPublics.keySet()) { for (Integer preKeyId : preKeyPublics.keySet()) {
ECPublicKey preKeyPublic = preKeyPublics.get(preKeyId); ECPublicKey preKeyPublic = preKeyPublics.get(preKeyId);
bundles.add(new PreKeyBundle(0, 0, preKeyId, preKeyPublic, bundles.add(new PreKeyBundle(0, 0, preKeyId, preKeyPublic,
0, null, null, null)); 0, null, null, null));
} }
} }
return bundles; return bundles;
} }
@Override @Override
public void onIqPacketReceived(final Account account, final IqPacket packet) { public void onIqPacketReceived(final Account account, final IqPacket packet) {
final boolean isGet = packet.getType() == IqPacket.TYPE.GET; final boolean isGet = packet.getType() == IqPacket.TYPE.GET;
if (packet.getType() == IqPacket.TYPE.ERROR || packet.getType() == IqPacket.TYPE.TIMEOUT) { if (packet.getType() == IqPacket.TYPE.ERROR || packet.getType() == IqPacket.TYPE.TIMEOUT) {
return; return;
} }
if (packet.hasChild("query", Namespace.ROSTER) && packet.fromServer(account)) { if (packet.hasChild("query", Namespace.ROSTER) && packet.fromServer(account)) {
final Element query = packet.findChild("query"); final Element query = packet.findChild("query");
// If this is in response to a query for the whole roster: // If this is in response to a query for the whole roster:
if (packet.getType() == IqPacket.TYPE.RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.getRoster().markAllAsNotInRoster(); account.getRoster().markAllAsNotInRoster();
} }
this.rosterItems(account, query); this.rosterItems(account, query);
} else if ((packet.hasChild("block", Namespace.BLOCKING) || packet.hasChild("blocklist", Namespace.BLOCKING)) && } else if ((packet.hasChild("block", Namespace.BLOCKING) || packet.hasChild("blocklist", Namespace.BLOCKING)) &&
packet.fromServer(account)) { packet.fromServer(account)) {
// Block list or block push. // Block list or block push.
Log.d(Config.LOGTAG, "Received blocklist update from server"); Log.d(Config.LOGTAG, "Received blocklist update from server");
final Element blocklist = packet.findChild("blocklist", Namespace.BLOCKING); final Element blocklist = packet.findChild("blocklist", Namespace.BLOCKING);
final Element block = packet.findChild("block", Namespace.BLOCKING); final Element block = packet.findChild("block", Namespace.BLOCKING);
final Collection<Element> items = blocklist != null ? blocklist.getChildren() : final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
(block != null ? block.getChildren() : null); (block != null ? block.getChildren() : null);
// If this is a response to a blocklist query, clear the block list and replace with the new one. // If this is a response to a blocklist query, clear the block list and replace with the new one.
// Otherwise, just update the existing blocklist. // Otherwise, just update the existing blocklist.
if (packet.getType() == IqPacket.TYPE.RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.clearBlocklist(); account.clearBlocklist();
account.getXmppConnection().getFeatures().setBlockListRequested(true); account.getXmppConnection().getFeatures().setBlockListRequested(true);
} }
if (items != null) { if (items != null) {
final Collection<Jid> jids = new ArrayList<>(items.size()); final Collection<Jid> jids = new ArrayList<>(items.size());
// Create a collection of Jids from the packet // Create a collection of Jids from the packet
for (final Element item : items) { for (final Element item : items) {
if (item.getName().equals("item")) { if (item.getName().equals("item")) {
final Jid jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid")); final Jid jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid"));
if (jid != null) { if (jid != null) {
jids.add(jid); jids.add(jid);
} }
} }
} }
account.getBlocklist().addAll(jids); account.getBlocklist().addAll(jids);
if (packet.getType() == IqPacket.TYPE.SET) { if (packet.getType() == IqPacket.TYPE.SET) {
boolean removed = false; boolean removed = false;
for(Jid jid : jids) { for (Jid jid : jids) {
removed |= mXmppConnectionService.removeBlockedConversations(account,jid); removed |= mXmppConnectionService.removeBlockedConversations(account, jid);
} }
if (removed) { if (removed) {
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
} }
} }
} }
// Update the UI // Update the UI
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED); mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
if (packet.getType() == IqPacket.TYPE.SET) { if (packet.getType() == IqPacket.TYPE.SET) {
final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT); final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
mXmppConnectionService.sendIqPacket(account, response, null); mXmppConnectionService.sendIqPacket(account, response, null);
} }
} else if (packet.hasChild("unblock", Namespace.BLOCKING) && } else if (packet.hasChild("unblock", Namespace.BLOCKING) &&
packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) { packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) {
Log.d(Config.LOGTAG, "Received unblock update from server"); Log.d(Config.LOGTAG, "Received unblock update from server");
final Collection<Element> items = packet.findChild("unblock", Namespace.BLOCKING).getChildren(); final Collection<Element> items = packet.findChild("unblock", Namespace.BLOCKING).getChildren();
if (items.size() == 0) { if (items.size() == 0) {
// No children to unblock == unblock all // No children to unblock == unblock all
account.getBlocklist().clear(); account.getBlocklist().clear();
} else { } else {
final Collection<Jid> jids = new ArrayList<>(items.size()); final Collection<Jid> jids = new ArrayList<>(items.size());
for (final Element item : items) { for (final Element item : items) {
if (item.getName().equals("item")) { if (item.getName().equals("item")) {
final Jid jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid")); final Jid jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("jid"));
if (jid != null) { if (jid != null) {
jids.add(jid); jids.add(jid);
} }
} }
} }
account.getBlocklist().removeAll(jids); account.getBlocklist().removeAll(jids);
} }
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED); mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT); final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
mXmppConnectionService.sendIqPacket(account, response, null); mXmppConnectionService.sendIqPacket(account, response, null);
} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb") } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
|| packet.hasChild("data", "http://jabber.org/protocol/ibb") || packet.hasChild("data", "http://jabber.org/protocol/ibb")
|| packet.hasChild("close","http://jabber.org/protocol/ibb")) { || packet.hasChild("close", "http://jabber.org/protocol/ibb")) {
mXmppConnectionService.getJingleConnectionManager() mXmppConnectionService.getJingleConnectionManager()
.deliverIbbPacket(account, packet); .deliverIbbPacket(account, packet);
} else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) { } else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(account, packet); final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(account, packet);
mXmppConnectionService.sendIqPacket(account, response, null); mXmppConnectionService.sendIqPacket(account, response, null);
} else if (packet.hasChild("query","jabber:iq:version") && isGet) { } else if (packet.hasChild("query", "jabber:iq:version") && isGet) {
final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet); final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet);
mXmppConnectionService.sendIqPacket(account,response,null); mXmppConnectionService.sendIqPacket(account, response, null);
} else if (packet.hasChild("ping", "urn:xmpp:ping") && isGet) { } else if (packet.hasChild("ping", "urn:xmpp:ping") && isGet) {
final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT); final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
mXmppConnectionService.sendIqPacket(account, response, null); mXmppConnectionService.sendIqPacket(account, response, null);
} else if (packet.hasChild("time","urn:xmpp:time") && isGet) { } else if (packet.hasChild("time", "urn:xmpp:time") && isGet) {
final IqPacket response; final IqPacket response;
if (mXmppConnectionService.useTorToConnect() || account.isOnion()) { if (mXmppConnectionService.useTorToConnect() || account.isOnion()) {
response = packet.generateResponse(IqPacket.TYPE.ERROR); response = packet.generateResponse(IqPacket.TYPE.ERROR);
final Element error = response.addChild("error"); final Element error = response.addChild("error");
error.setAttribute("type","cancel"); error.setAttribute("type", "cancel");
error.addChild("not-allowed","urn:ietf:params:xml:ns:xmpp-stanzas"); error.addChild("not-allowed", "urn:ietf:params:xml:ns:xmpp-stanzas");
} else { } else {
response = mXmppConnectionService.getIqGenerator().entityTimeResponse(packet); response = mXmppConnectionService.getIqGenerator().entityTimeResponse(packet);
} }
mXmppConnectionService.sendIqPacket(account,response, null); mXmppConnectionService.sendIqPacket(account, response, null);
} else { } else if (packet.hasChild("pubsub", Namespace.PUBSUB) && packet.getType() == IqPacket.TYPE.SET) {
if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) { final Jid server = packet.getFrom();
final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR); final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB);
final Element error = response.addChild("error"); final Element publish = pubsub == null ? null : pubsub.findChild("publish");
error.setAttribute("type", "cancel"); final String node = publish == null ? null : publish.getAttribute("node");
error.addChild("feature-not-implemented","urn:ietf:params:xml:ns:xmpp-stanzas"); final Element item = publish == null ? null : publish.findChild("item");
account.getXmppConnection().sendIqPacket(response, null); final Element notification = item == null ? null : item.findChild("notification", Namespace.PUSH);
} if (notification != null && node != null && server != null) {
} final Conversation conversation = mXmppConnectionService.findConversationByUuid(node);
} if (conversation != null && conversation.getAccount() == account && conversation.getJid().getDomain().equals(server.getDomain())) {
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": received muc push event for "+conversation.getJid().asBareJid());
mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
mXmppConnectionService.mucSelfPingAndRejoin(conversation);
} else {
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": received push event for unknown conference from "+server);
final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
final Element error = response.addChild("error");
error.setAttribute("type", "cancel");
error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
mXmppConnectionService.sendIqPacket(account, response, null);
}
}
} else {
if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
final Element error = response.addChild("error");
error.setAttribute("type", "cancel");
error.addChild("feature-not-implemented", "urn:ietf:params:xml:ns:xmpp-stanzas");
account.getXmppConnection().sendIqPacket(response, null);
}
}
}
} }

View File

@ -2537,6 +2537,9 @@ public class XmppConnectionService extends Service {
saveConversationAsBookmark(conversation, null); saveConversationAsBookmark(conversation, null);
} }
} }
if (mucOptions.push()) {
enableMucPush(conversation);
}
synchronized (account.inProgressConferenceJoins) { synchronized (account.inProgressConferenceJoins) {
account.inProgressConferenceJoins.remove(conversation); account.inProgressConferenceJoins.remove(conversation);
sendUnsentMessages(conversation); sendUnsentMessages(conversation);
@ -2580,6 +2583,35 @@ public class XmppConnectionService extends Service {
} }
} }
private void enableMucPush(final Conversation conversation) {
final Account account = conversation.getAccount();
final Jid room = conversation.getJid().asBareJid();
final IqPacket enable = mIqGenerator.enablePush(conversation.getAccount().getJid(), conversation.getUuid(), null);
enable.setTo(room);
sendIqPacket(account, enable, (a, response) -> {
if (response.getType() == IqPacket.TYPE.RESULT) {
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": enabled push for muc "+room);
} else if (response.getType() == IqPacket.TYPE.ERROR) {
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": unable to enable push for muc "+room+" "+response.getError());
}
});
}
private void disableMucPush(final Conversation conversation) {
final Account account = conversation.getAccount();
final Jid room = conversation.getJid().asBareJid();
final IqPacket disable = mIqGenerator.disablePush(conversation.getAccount().getJid(), conversation.getUuid());
disable.setTo(room);
sendIqPacket(account, disable, (a, response) -> {
if (response.getType() == IqPacket.TYPE.RESULT) {
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": disabled push for muc "+room);
} else if (response.getType() == IqPacket.TYPE.ERROR) {
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": unable to disable push for muc "+room+" "+response.getError());
}
});
}
private void fetchConferenceMembers(final Conversation conversation) { private void fetchConferenceMembers(final Conversation conversation) {
final Account account = conversation.getAccount(); final Account account = conversation.getAccount();
final AxolotlService axolotlService = account.getAxolotlService(); final AxolotlService axolotlService = account.getAxolotlService();
@ -2759,6 +2791,9 @@ public class XmppConnectionService extends Service {
account.pendingConferenceJoins.remove(conversation); account.pendingConferenceJoins.remove(conversation);
account.pendingConferenceLeaves.remove(conversation); account.pendingConferenceLeaves.remove(conversation);
if (account.getStatus() == Account.State.ONLINE || now) { if (account.getStatus() == Account.State.ONLINE || now) {
if (conversation.getMucOptions().push()) {
disableMucPush(conversation);
}
sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions())); sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions()));
conversation.getMucOptions().setOffline(); conversation.getMucOptions().setOffline();
Bookmark bookmark = conversation.getBookmark(); Bookmark bookmark = conversation.getBookmark();

View File

@ -28,4 +28,5 @@ public final class Namespace {
public static final String JINGLE_TRANSPORTS_S5B = "urn:xmpp:jingle:transports:s5b:1"; public static final String JINGLE_TRANSPORTS_S5B = "urn:xmpp:jingle:transports:s5b:1";
public static final String JINGLE_TRANSPORTS_IBB = "urn:xmpp:jingle:transports:ibb:1"; public static final String JINGLE_TRANSPORTS_IBB = "urn:xmpp:jingle:transports:ibb:1";
public static final String PING = "urn:xmpp:ping"; public static final String PING = "urn:xmpp:ping";
public static final String PUSH = "urn:xmpp:push:0";
} }

View File

@ -1819,8 +1819,8 @@ public class XmppConnection implements Runnable {
} }
public boolean push() { public boolean push() {
return hasDiscoFeature(account.getJid().asBareJid(), "urn:xmpp:push:0") return hasDiscoFeature(account.getJid().asBareJid(), Namespace.PUSH)
|| hasDiscoFeature(Jid.of(account.getServer()), "urn:xmpp:push:0"); || hasDiscoFeature(Jid.of(account.getServer()), Namespace.PUSH);
} }
public boolean rosterVersioning() { public boolean rosterVersioning() {