GroupChatInvitation.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 2020 Paul Schaub.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.muc.packet;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.packet.Stanza;
  21. import org.jivesoftware.smack.util.EqualsUtil;
  22. import org.jivesoftware.smack.util.HashCode;
  23. import org.jivesoftware.smack.util.Objects;
  24. import org.jivesoftware.smack.util.XmlStringBuilder;

  25. import org.jxmpp.jid.EntityBareJid;

  26. /**
  27.  * A group chat invitation stanza extension, which is used to invite other
  28.  * users to a group chat room.
  29.  *
  30.  * This implementation now conforms to XEP-0249: Direct MUC Invitations,
  31.  * while staying backwards compatible to legacy MUC invitations.
  32.  *
  33.  * @author Matt Tucker
  34.  * @author Paul Schaub
  35.  */
  36. public class GroupChatInvitation implements ExtensionElement {

  37.     /**
  38.      * Element name of the stanza extension.
  39.      */
  40.     public static final String ELEMENT = "x";

  41.     /**
  42.      * Namespace of the stanza extension.
  43.      */
  44.     public static final String NAMESPACE = "jabber:x:conference";

  45.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  46.     public static final String ATTR_CONTINUE = "continue";
  47.     public static final String ATTR_JID = "jid";
  48.     public static final String ATTR_PASSWORD = "password";
  49.     public static final String ATTR_REASON = "reason";
  50.     public static final String ATTR_THREAD = "thread";

  51.     private final EntityBareJid roomAddress;
  52.     private final String reason;
  53.     private final String password;
  54.     private final String thread;
  55.     private final boolean continueAsOneToOneChat;

  56.     /**
  57.      * Creates a new group chat invitation to the specified room address.
  58.      * GroupChat room addresses are in the form <code>room@service</code>,
  59.      * where <code>service</code> is the name of group chat server, such as
  60.      * <code>chat.example.com</code>.
  61.      *
  62.      * @param roomAddress the address of the group chat room.
  63.      */
  64.     public GroupChatInvitation(EntityBareJid roomAddress) {
  65.         this(roomAddress, null, null, false, null);
  66.     }

  67.     /**
  68.      * Creates a new group chat invitation to the specified room address.
  69.      * GroupChat room addresses are in the form <code>room@service</code>,
  70.      * where <code>service</code> is the name of group chat server, such as
  71.      * <code>chat.example.com</code>.
  72.      *
  73.      * @param roomAddress the address of the group chat room.
  74.      * @param reason the purpose for the invitation
  75.      * @param password specifies a password needed for entry
  76.      * @param continueAsOneToOneChat specifies if the groupchat room continues a one-to-one chat having the designated thread
  77.      * @param thread the thread to continue
  78.      */
  79.     public GroupChatInvitation(EntityBareJid roomAddress,
  80.                                String reason,
  81.                                String password,
  82.                                boolean continueAsOneToOneChat,
  83.                                String thread) {
  84.         this.roomAddress = Objects.requireNonNull(roomAddress);
  85.         this.reason = reason;
  86.         this.password = password;
  87.         this.continueAsOneToOneChat = continueAsOneToOneChat;
  88.         this.thread = thread;
  89.     }

  90.     /**
  91.      * Returns the purpose for the invitation.
  92.      *
  93.      * @return the address of the group chat room.
  94.      */
  95.     public String getReason() {
  96.         return reason;
  97.     }

  98.     /**
  99.      * Returns the password needed for entry.
  100.      *
  101.      * @return the password needed for entry
  102.      */
  103.     public String getPassword() {
  104.         return password;
  105.     }

  106.     /**
  107.      * Returns the thread to continue.
  108.      *
  109.      * @return the thread to continue.
  110.      */
  111.     public String getThread() {
  112.         return thread;
  113.     }

  114.     /**
  115.      * Returns whether the groupchat room continues a one-to-one chat.
  116.      *
  117.      * @return whether the groupchat room continues a one-to-one chat.
  118.      */
  119.     public boolean continueAsOneToOneChat() {
  120.         return continueAsOneToOneChat;
  121.     }

  122.     /**
  123.      * Returns the address of the group chat room. GroupChat room addresses
  124.      * are in the form <code>room@service</code>, where <code>service</code> is
  125.      * the name of group chat server, such as <code>chat.example.com</code>.
  126.      *
  127.      * @return the address of the group chat room.
  128.      */
  129.     public EntityBareJid getRoomAddress() {
  130.         return roomAddress;
  131.     }

  132.     @Override
  133.     public String getElementName() {
  134.         return ELEMENT;
  135.     }

  136.     @Override
  137.     public String getNamespace() {
  138.         return NAMESPACE;
  139.     }

  140.     @Override
  141.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  142.         XmlStringBuilder xml = new XmlStringBuilder(this);
  143.         xml.jidAttribute(getRoomAddress());
  144.         xml.optAttribute(ATTR_REASON, getReason());
  145.         xml.optAttribute(ATTR_PASSWORD, getPassword());
  146.         xml.optAttribute(ATTR_THREAD, getThread());
  147.         xml.optBooleanAttribute(ATTR_CONTINUE, continueAsOneToOneChat());

  148.         xml.closeEmptyElement();
  149.         return xml;
  150.     }

  151.     @Override
  152.     public boolean equals(Object obj) {
  153.         return EqualsUtil.equals(this, obj, (equalsBuilder, other) -> equalsBuilder
  154.                         .append(getRoomAddress(), other.getRoomAddress())
  155.                         .append(getPassword(), other.getPassword())
  156.                         .append(getReason(), other.getReason())
  157.         .append(continueAsOneToOneChat(), other.continueAsOneToOneChat())
  158.         .append(getThread(), other.getThread()));
  159.     }

  160.     @Override
  161.     public int hashCode() {
  162.         return HashCode.builder()
  163.                 .append(getRoomAddress())
  164.                 .append(getPassword())
  165.                 .append(getReason())
  166.                 .append(continueAsOneToOneChat())
  167.                 .append(getThread())
  168.                 .build();
  169.     }

  170.     /**
  171.      * Get the group chat invitation from the given stanza.
  172.      * @param packet TODO javadoc me please
  173.      * @return the GroupChatInvitation or null
  174.      */
  175.     public static GroupChatInvitation from(Stanza packet) {
  176.         return packet.getExtension(GroupChatInvitation.class);
  177.     }

  178. }