MucEnterConfiguration.java

  1. /**
  2.  *
  3.  * Copyright 2015-2024 Florian Schmaus
  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;

  18. import java.util.Date;

  19. import org.jivesoftware.smack.XMPPConnection;
  20. import org.jivesoftware.smack.packet.Presence;
  21. import org.jivesoftware.smack.packet.PresenceBuilder;
  22. import org.jivesoftware.smack.util.Consumer;
  23. import org.jivesoftware.smack.util.Objects;

  24. import org.jivesoftware.smackx.muc.packet.MUCInitialPresence;

  25. import org.jxmpp.jid.EntityFullJid;
  26. import org.jxmpp.jid.impl.JidCreate;
  27. import org.jxmpp.jid.parts.Resourcepart;

  28. /**
  29.  * The configuration used to enter a MUC room. This configuration is usually used when joining an
  30.  * existing room. When creating a new room, only the Nickname setting is relevant.
  31.  * <p>
  32.  * A builder for this can be obtained by calling {@link MultiUserChat#getEnterConfigurationBuilder(Resourcepart)}.
  33.  * </p>
  34.  *
  35.  * @author Florian Schmaus
  36.  * @since 4.2
  37.  */
  38. public final class MucEnterConfiguration {

  39.     private final Resourcepart nickname;
  40.     private final String password;
  41.     private final int maxChars;
  42.     private final int maxStanzas;
  43.     private final int seconds;
  44.     private final Date since;
  45.     private final long timeout;
  46.     private final Presence joinPresence;

  47.     MucEnterConfiguration(Builder builder) {
  48.         nickname = builder.nickname;
  49.         password = builder.password;
  50.         maxChars = builder.maxChars;
  51.         maxStanzas = builder.maxStanzas;
  52.         seconds = builder.seconds;
  53.         since = builder.since;
  54.         timeout = builder.timeout;

  55.         final PresenceBuilder joinPresenceBuilder = builder.joinPresenceBuilder.ofType(Presence.Type.available);

  56.         // Indicate the client supports MUC
  57.         joinPresenceBuilder.addExtension(new MUCInitialPresence(password, maxChars, maxStanzas, seconds,
  58.                         since));
  59.         joinPresence = joinPresenceBuilder.build();
  60.     }

  61.     Presence getJoinPresence(MultiUserChat multiUserChat) {
  62.         final EntityFullJid jid = JidCreate.entityFullFrom(multiUserChat.getRoom(), nickname);
  63.         joinPresence.setTo(jid);
  64.         return joinPresence;
  65.     }

  66.     long getTimeout() {
  67.         return timeout;
  68.     }

  69.     public static final class Builder {
  70.         private final Resourcepart nickname;

  71.         private String password;
  72.         private int maxChars = -1;
  73.         private int maxStanzas = -1;
  74.         private int seconds = -1;
  75.         private Date since;
  76.         private long timeout;

  77.         private final PresenceBuilder joinPresenceBuilder;

  78.         Builder(Resourcepart nickname, XMPPConnection connection) {
  79.             this.nickname = Objects.requireNonNull(nickname, "Nickname must not be null");

  80.             timeout = connection.getReplyTimeout();
  81.             timeoutAfter(timeout);

  82.             joinPresenceBuilder = connection.getStanzaFactory().buildPresenceStanza();
  83.         }

  84.         /**
  85.          * Set the presence used to join the MUC room.
  86.          * <p>
  87.          * The consumer must not modify the presence type, otherwise an {@link IllegalArgumentException} will be thrown.
  88.          * <p>
  89.          *
  90.          * @param presenceBuilderConsumer a consumer which will be passed the presence build.
  91.          * @return a reference to this builder.
  92.          * @since 4.4.0
  93.          */
  94.         public Builder withPresence(Consumer<? super PresenceBuilder> presenceBuilderConsumer) {
  95.             presenceBuilderConsumer.accept(joinPresenceBuilder);

  96.             if (joinPresenceBuilder.getType() != Presence.Type.available) {
  97.                 throw new IllegalArgumentException("Presence must be of type 'available'");
  98.             }

  99.             return this;
  100.         }

  101.         /**
  102.          * Use the given password to join the MUC room.
  103.          *
  104.          * @param password the password used to join.
  105.          * @return a reference to this builder.
  106.          */
  107.         public Builder withPassword(String password) {
  108.             this.password = password;
  109.             return this;
  110.         }

  111.         /**
  112.          * Set the timeout used when joining the MUC room.
  113.          *
  114.          * @param timeout the timeout to use when joining.
  115.          * @return a reference to this builder.
  116.          */
  117.         public Builder timeoutAfter(long timeout) {
  118.             if (timeout <= 0) {
  119.                 throw new IllegalArgumentException("timeout must be positive");
  120.             }
  121.             this.timeout = timeout;
  122.             return this;
  123.         }

  124.         /**
  125.          * Request that that MUC is going to sent us no history when joining.
  126.          *
  127.          * @return a reference to this builder.
  128.          */
  129.         public Builder requestNoHistory() {
  130.             maxChars = 0;
  131.             maxStanzas = -1;
  132.             seconds = -1;
  133.             since = null;
  134.             return this;
  135.         }

  136.         /**
  137.          * Sets the total number of characters to receive in the history.
  138.          *
  139.          * @param maxChars the total number of characters to receive in the history.
  140.          * @return a reference to this builder.
  141.          */
  142.         public Builder requestMaxCharsHistory(int maxChars) {
  143.             this.maxChars = maxChars;
  144.             return this;
  145.         }

  146.         /**
  147.          * Sets the total number of messages to receive in the history.
  148.          *
  149.          * @param maxStanzas the total number of messages to receive in the history.
  150.          * @return a reference to this builder.
  151.          */
  152.         public Builder requestMaxStanzasHistory(int maxStanzas) {
  153.             this.maxStanzas = maxStanzas;
  154.             return this;
  155.         }

  156.         /**
  157.          * Sets the number of seconds to use to filter the messages received during that time.
  158.          * In other words, only the messages received in the last "X" seconds will be included in
  159.          * the history.
  160.          *
  161.          * @param seconds the number of seconds to use to filter the messages received during
  162.          * that time.
  163.          * @return a reference to this builder.
  164.          */
  165.         public Builder requestHistorySince(int seconds) {
  166.             this.seconds = seconds;
  167.             return this;
  168.         }

  169.         /**
  170.          * Sets the since date to use to filter the messages received during that time.
  171.          * In other words, only the messages received since the datetime specified will be
  172.          * included in the history.
  173.          *
  174.          * @param since the since date to use to filter the messages received during that time.
  175.          * @return a reference to this builder.
  176.          */
  177.         public Builder requestHistorySince(Date since) {
  178.             this.since = since;
  179.             return this;
  180.         }

  181.         /**
  182.          * Build a new {@link MucEnterConfiguration} with the current builder.
  183.          *
  184.          * @return a new {@code MucEnterConfiguration}.
  185.          */
  186.         public MucEnterConfiguration build() {
  187.             return new MucEnterConfiguration(this);
  188.         }

  189.     }
  190. }