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);
if (secret != null) {
Data data = new Data(); Data data = new Data();
data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS); data.setFormType(Namespace.PUBSUB_PUBLISH_OPTIONS);
data.put("secret", secret); data.put("secret", secret);
data.submit(); data.submit();
enable.addChild(data); 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;
@ -382,6 +383,29 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
response = mXmppConnectionService.getIqGenerator().entityTimeResponse(packet); response = mXmppConnectionService.getIqGenerator().entityTimeResponse(packet);
} }
mXmppConnectionService.sendIqPacket(account, response, null); mXmppConnectionService.sendIqPacket(account, response, null);
} else if (packet.hasChild("pubsub", Namespace.PUBSUB) && packet.getType() == IqPacket.TYPE.SET) {
final Jid server = packet.getFrom();
final Element pubsub = packet.findChild("pubsub", Namespace.PUBSUB);
final Element publish = pubsub == null ? null : pubsub.findChild("publish");
final String node = publish == null ? null : publish.getAttribute("node");
final Element item = publish == null ? null : publish.findChild("item");
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 { } else {
if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) { if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR); final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);

View File

@ -2536,6 +2536,9 @@ public class XmppConnectionService extends Service {
if (followedInvite && conversation.getBookmark() == null) { if (followedInvite && conversation.getBookmark() == null) {
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);
@ -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() {