From 664a1411904b90572ba243157439e3c22d1f96c9 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 18 Jul 2020 12:55:26 +0200 Subject: [PATCH] [extensions] Improved Support for Direct MUC Invitations (XEP-0249) [flow: rebase of paul's initial submission which required adjustments] Co-authored-by: Florian Schmaus --- .../muc/DirectMucInvitationListener.java | 25 ++++ .../muc/DirectMucInvitationManager.java | 111 ++++++++++++++++++ .../smackx/muc/MultiUserChatManager.java | 4 + .../muc/packet/GroupChatInvitation.java | 70 ++++++----- .../provider/GroupChatInvitationProvider.java | 23 ++-- .../GroupChatInvitationElementTest.java | 69 +++++++++++ .../org/jivesoftware/smackx/package-info.java | 6 - 7 files changed, 263 insertions(+), 45 deletions(-) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationListener.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationManager.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitationElementTest.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationListener.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationListener.java new file mode 100644 index 0000000000..e8ef1af5e1 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationListener.java @@ -0,0 +1,25 @@ +/** + * + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muc; + +import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smackx.muc.packet.GroupChatInvitation; + +public interface DirectMucInvitationListener { + + void invitationReceived(GroupChatInvitation invitation, Stanza stanza); +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationManager.java new file mode 100644 index 0000000000..b05f10df0d --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/DirectMucInvitationManager.java @@ -0,0 +1,111 @@ +/** + * + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muc; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +import org.jivesoftware.smack.Manager; +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPConnectionRegistry; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.filter.StanzaExtensionFilter; +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.MessageBuilder; +import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; +import org.jivesoftware.smackx.muc.packet.GroupChatInvitation; + +import org.jxmpp.jid.EntityBareJid; + +/** + * Smacks API for XEP-0249: Direct MUC Invitations. + * Use this instead of {@link org.jivesoftware.smackx.muc.packet.MUCUser.Invite}. + * + * To invite a user to a group chat, use {@link #inviteToMuc(MultiUserChat, EntityBareJid)}. + * + * In order to listen for incoming invitations, register a {@link DirectMucInvitationListener} using + * {@link #addInvitationListener(DirectMucInvitationListener)}. + * + * @see Direct MUC Invitations + */ +public final class DirectMucInvitationManager extends Manager { + + private static final Map INSTANCES = new WeakHashMap<>(); + private final List directMucInvitationListeners = new ArrayList<>(); + private final ServiceDiscoveryManager serviceDiscoveryManager; + + static { + XMPPConnectionRegistry.addConnectionCreationListener(DirectMucInvitationManager::getInstanceFor); + } + + public static synchronized DirectMucInvitationManager getInstanceFor(XMPPConnection connection) { + DirectMucInvitationManager manager = INSTANCES.get(connection); + if (manager == null) { + manager = new DirectMucInvitationManager(connection); + INSTANCES.put(connection, manager); + } + return manager; + } + + private DirectMucInvitationManager(XMPPConnection connection) { + super(connection); + serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); + + connection().addAsyncStanzaListener(stanza -> { + GroupChatInvitation invitation = stanza.getExtension(GroupChatInvitation.class); + for (DirectMucInvitationListener listener : directMucInvitationListeners) { + listener.invitationReceived(invitation, stanza); + } + }, new StanzaExtensionFilter(GroupChatInvitation.ELEMENT, GroupChatInvitation.NAMESPACE)); + serviceDiscoveryManager.addFeature(GroupChatInvitation.NAMESPACE); + } + + public void inviteToMuc(MultiUserChat muc, EntityBareJid user) + throws SmackException.NotConnectedException, InterruptedException { + inviteToMuc(muc, user, null, null, false, null); + } + + public void inviteToMuc(MultiUserChat muc, EntityBareJid user, String password, String reason, boolean continueAsOneToOneChat, String thread) + throws SmackException.NotConnectedException, InterruptedException { + inviteToMuc(user, new GroupChatInvitation(muc.getRoom(), reason, password, continueAsOneToOneChat, thread)); + } + + public void inviteToMuc(EntityBareJid jid, GroupChatInvitation invitation) throws SmackException.NotConnectedException, InterruptedException { + Message invitationMessage = MessageBuilder.buildMessage() + .to(jid) + .addExtension(invitation) + .build(); + connection().sendStanza(invitationMessage); + } + + public boolean userSupportsInvitations(EntityBareJid jid) + throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, + SmackException.NoResponseException { + return serviceDiscoveryManager.supportsFeature(jid, GroupChatInvitation.NAMESPACE); + } + + public synchronized void addInvitationListener(DirectMucInvitationListener listener) { + directMucInvitationListeners.add(listener); + } + + public synchronized void removeInvitationListener(DirectMucInvitationListener listener) { + directMucInvitationListeners.remove(listener); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java index 00b088095b..47de2163de 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java @@ -83,6 +83,10 @@ * further attempts will be made for the other rooms. *

* + * Note: + * For inviting other users to a group chat or listening for such invitations, take a look at the + * {@link DirectMucInvitationManager} which provides an implementation of XEP-0249: Direct MUC Invitations. + * * @see XEP-0045: Multi-User Chat */ public final class MultiUserChatManager extends Manager { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java index dc6660ae99..e243d46edd 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2007 Jive Software. + * Copyright 2003-2007 Jive Software, 2020 Paul Schaub. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.util.EqualsUtil; +import org.jivesoftware.smack.util.HashCode; import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.XmlStringBuilder; @@ -27,32 +29,13 @@ /** * A group chat invitation stanza extension, which is used to invite other - * users to a group chat room. To invite a user to a group chat room, address - * a new message to the user and set the room name appropriately, as in the - * following code example: + * users to a group chat room. * - *
- * Message message = new Message("user@chat.example.com");
- * message.setBody("Join me for a group chat!");
- * message.addExtension(new GroupChatInvitation("room@chat.example.com"););
- * con.sendStanza(message);
- * 
- * - * To listen for group chat invitations, use a StanzaExtensionFilter for the - * x element name and jabber:x:conference namespace, as in the - * following code example: - * - *
- * PacketFilter filter = new StanzaExtensionFilter("x", "jabber:x:conference");
- * // Create a stanza collector or stanza listeners using the filter...
- * 
- * - * Note: this protocol is outdated now that the Multi-User Chat (MUC) XEP is available - * (XEP-45). However, most - * existing clients still use this older protocol. Once MUC support becomes more - * widespread, this API may be deprecated. + * This implementation now conforms to XEP-0249: Direct MUC Invitations, + * while staying backwards compatible to legacy MUC invitations. * * @author Matt Tucker + * @author Paul Schaub */ public class GroupChatInvitation implements ExtensionElement { @@ -68,6 +51,12 @@ public class GroupChatInvitation implements ExtensionElement { public static final QName QNAME = new QName(NAMESPACE, ELEMENT); + public static final String ATTR_CONTINUE = "continue"; + public static final String ATTR_JID = "jid"; + public static final String ATTR_PASSWORD = "password"; + public static final String ATTR_REASON = "reason"; + public static final String ATTR_THREAD = "thread"; + private final EntityBareJid roomAddress; private final String reason; private final String password; @@ -170,18 +159,37 @@ public String getNamespace() { @Override public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { XmlStringBuilder xml = new XmlStringBuilder(this); - xml.attribute("jid", getRoomAddress()); - xml.optAttribute("reason", getReason()); - xml.optAttribute("password", getPassword()); - xml.optAttribute("thread", getThread()); - - if (continueAsOneToOneChat()) - xml.optBooleanAttribute("continue", true); + xml.jidAttribute(getRoomAddress()); + xml.optAttribute(ATTR_REASON, getReason()); + xml.optAttribute(ATTR_PASSWORD, getPassword()); + xml.optAttribute(ATTR_THREAD, getThread()); + xml.optBooleanAttribute(ATTR_CONTINUE, continueAsOneToOneChat()); xml.closeEmptyElement(); return xml; } + @Override + public boolean equals(Object obj) { + return EqualsUtil.equals(this, obj, (equalsBuilder, other) -> equalsBuilder + .append(getRoomAddress(), other.getRoomAddress()) + .append(getPassword(), other.getPassword()) + .append(getReason(), other.getReason()) + .append(continueAsOneToOneChat(), other.continueAsOneToOneChat()) + .append(getThread(), other.getThread())); + } + + @Override + public int hashCode() { + return HashCode.builder() + .append(getRoomAddress()) + .append(getPassword()) + .append(getReason()) + .append(continueAsOneToOneChat()) + .append(getThread()) + .build(); + } + /** * Get the group chat invitation from the given stanza. * @param packet TODO javadoc me please diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/GroupChatInvitationProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/GroupChatInvitationProvider.java index 31f6472c88..00ddabaf9c 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/GroupChatInvitationProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/GroupChatInvitationProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2007 Jive Software, 2022 Florian Schmaus. + * Copyright 2003-2007 Jive Software, 2020 Paul Schaub, 2022-2023 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,12 @@ */ package org.jivesoftware.smackx.muc.provider; +import static org.jivesoftware.smackx.muc.packet.GroupChatInvitation.ATTR_CONTINUE; +import static org.jivesoftware.smackx.muc.packet.GroupChatInvitation.ATTR_PASSWORD; +import static org.jivesoftware.smackx.muc.packet.GroupChatInvitation.ATTR_REASON; +import static org.jivesoftware.smackx.muc.packet.GroupChatInvitation.ATTR_THREAD; + import java.io.IOException; -import java.text.ParseException; import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.parsing.SmackParsingException; @@ -33,11 +37,14 @@ public class GroupChatInvitationProvider extends ExtensionElementProvider"; + + GroupChatInvitation invitation = new GroupChatInvitation(mucJid, + "Hey Hecate, this is the place for all good witches!", + "cauldronburn", + true, + "e0ffe42b28561960c6b12b944a092794b9683a38"); + assertXmlSimilar(expectedXml, invitation.toXML()); + + GroupChatInvitation parsed = TEST_PROVIDER.parse(TestUtils.getParser(expectedXml)); + assertEquals(invitation, parsed); + } + + @Test + public void serializeMinimalElementTest() throws XmlPullParserException, IOException, SmackParsingException { + final String expectedXml = ""; + + GroupChatInvitation invitation = new GroupChatInvitation(mucJid); + assertXmlSimilar(expectedXml, invitation.toXML()); + + GroupChatInvitation parsed = TEST_PROVIDER.parse(TestUtils.getParser(expectedXml)); + assertEquals(invitation, parsed); + } +} diff --git a/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java b/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java index 8b35500284..803afc7da0 100644 --- a/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java +++ b/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java @@ -584,12 +584,6 @@ * Multi-User Chats for mobile XMPP applications and specific environment. * * - * Group Chat Invitations - * - * - * Send invitations to other users to join a group chat room. - * - * * Jive Properties * *