2015-07-06 09:55:25 +02:00
|
|
|
/**
|
|
|
|
*
|
2016-07-08 11:18:20 +02:00
|
|
|
* Copyright 2015-2016 Florian Schmaus
|
2015-07-06 09:55:25 +02:00
|
|
|
*
|
|
|
|
* 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.Date;
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2015-07-06 09:55:25 +02:00
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
2019-10-24 15:45:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.PresenceBuilder;
|
|
|
|
import org.jivesoftware.smack.util.Consumer;
|
2015-07-06 09:55:25 +02:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-07-06 09:55:25 +02:00
|
|
|
import org.jivesoftware.smackx.muc.packet.MUCInitialPresence;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-07-06 09:55:25 +02:00
|
|
|
import org.jxmpp.jid.EntityFullJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.jxmpp.jid.parts.Resourcepart;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The configuration used to enter a MUC room. This configuration is usually used when joining an
|
|
|
|
* existing room. When creating a new room, only the Nickname setting is relevant.
|
2016-07-08 11:18:20 +02:00
|
|
|
* <p>
|
|
|
|
* A builder for this can be obtained by calling {@link MultiUserChat#getEnterConfigurationBuilder(Resourcepart)}.
|
|
|
|
* </p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-07-06 09:55:25 +02:00
|
|
|
* @author Florian Schmaus
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public final class MucEnterConfiguration {
|
|
|
|
|
|
|
|
private final Resourcepart nickname;
|
|
|
|
private final String password;
|
|
|
|
private final int maxChars;
|
|
|
|
private final int maxStanzas;
|
|
|
|
private final int seconds;
|
|
|
|
private final Date since;
|
|
|
|
private final long timeout;
|
|
|
|
private final Presence joinPresence;
|
|
|
|
|
|
|
|
MucEnterConfiguration(Builder builder) {
|
|
|
|
nickname = builder.nickname;
|
|
|
|
password = builder.password;
|
|
|
|
maxChars = builder.maxChars;
|
|
|
|
maxStanzas = builder.maxStanzas;
|
|
|
|
seconds = builder.seconds;
|
|
|
|
since = builder.since;
|
|
|
|
timeout = builder.timeout;
|
|
|
|
|
|
|
|
if (builder.joinPresence == null) {
|
2019-10-24 15:45:08 +02:00
|
|
|
joinPresence = builder.joinPresenceBuilder.ofType(Presence.Type.available).build();
|
2015-07-06 09:55:25 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
joinPresence = builder.joinPresence.clone();
|
|
|
|
}
|
|
|
|
// Indicate the the client supports MUC
|
|
|
|
joinPresence.addExtension(new MUCInitialPresence(password, maxChars, maxStanzas, seconds,
|
|
|
|
since));
|
|
|
|
}
|
|
|
|
|
|
|
|
Presence getJoinPresence(MultiUserChat multiUserChat) {
|
2018-08-02 14:37:59 +02:00
|
|
|
final EntityFullJid jid = JidCreate.entityFullFrom(multiUserChat.getRoom(), nickname);
|
2015-07-06 09:55:25 +02:00
|
|
|
joinPresence.setTo(jid);
|
|
|
|
return joinPresence;
|
|
|
|
}
|
|
|
|
|
|
|
|
long getTimeout() {
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final class Builder {
|
|
|
|
private final Resourcepart nickname;
|
|
|
|
|
|
|
|
private String password;
|
|
|
|
private int maxChars = -1;
|
|
|
|
private int maxStanzas = -1;
|
|
|
|
private int seconds = -1;
|
|
|
|
private Date since;
|
|
|
|
private long timeout;
|
2019-10-24 15:45:08 +02:00
|
|
|
|
|
|
|
private final PresenceBuilder joinPresenceBuilder;
|
2015-07-06 09:55:25 +02:00
|
|
|
private Presence joinPresence;
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
Builder(Resourcepart nickname, XMPPConnection connection) {
|
2015-07-06 09:55:25 +02:00
|
|
|
this.nickname = Objects.requireNonNull(nickname, "Nickname must not be null");
|
2019-10-24 15:45:08 +02:00
|
|
|
|
|
|
|
timeout = connection.getReplyTimeout();
|
2015-07-06 09:55:25 +02:00
|
|
|
timeoutAfter(timeout);
|
2019-10-24 15:45:08 +02:00
|
|
|
|
|
|
|
joinPresenceBuilder = connection.getStanzaFactory().buildPresenceStanza();
|
2015-07-06 09:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the presence used to join the MUC room.
|
|
|
|
* <p>
|
2017-01-30 11:59:09 +01:00
|
|
|
* The 'to' value of the given presence will be overridden and the given presence must be of type
|
|
|
|
* 'available', otherwise an {@link IllegalArgumentException} will be thrown.
|
2015-07-06 09:55:25 +02:00
|
|
|
* <p>
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param presence TODO javadoc me please
|
2015-07-06 09:55:25 +02:00
|
|
|
* @return a reference to this builder.
|
2019-10-24 15:45:08 +02:00
|
|
|
* @deprecated use {@link #withPresence(Consumer)} instead.
|
2015-07-06 09:55:25 +02:00
|
|
|
*/
|
2019-10-24 15:45:08 +02:00
|
|
|
@Deprecated
|
|
|
|
// TODO: Remove in Smack 4.5.
|
2015-07-06 09:55:25 +02:00
|
|
|
public Builder withPresence(Presence presence) {
|
2017-01-30 11:59:09 +01:00
|
|
|
if (presence.getType() != Presence.Type.available) {
|
|
|
|
throw new IllegalArgumentException("Presence must be of type 'available'");
|
2015-07-06 09:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
joinPresence = presence;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
/**
|
|
|
|
* Set the presence used to join the MUC room.
|
|
|
|
* <p>
|
|
|
|
* The consumer must not modify the presence type, otherwise an {@link IllegalArgumentException} will be thrown.
|
|
|
|
* <p>
|
|
|
|
*
|
|
|
|
* @param presenceBuilderConsumer a consumer which will be passed the presence build.
|
|
|
|
* @return a reference to this builder.
|
|
|
|
* @since 4.5
|
|
|
|
*/
|
|
|
|
public Builder withPresence(Consumer<? super PresenceBuilder> presenceBuilderConsumer) {
|
|
|
|
presenceBuilderConsumer.accept(joinPresenceBuilder);
|
|
|
|
|
|
|
|
if (joinPresenceBuilder.getType() != Presence.Type.available) {
|
|
|
|
throw new IllegalArgumentException("Presence must be of type 'available'");
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-07-06 09:55:25 +02:00
|
|
|
/**
|
|
|
|
* Use the given password to join the MUC room.
|
|
|
|
*
|
|
|
|
* @param password the password used to join.
|
|
|
|
* @return a reference to this builder.
|
|
|
|
*/
|
|
|
|
public Builder withPassword(String password) {
|
|
|
|
this.password = password;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the timeout used when joining the MUC room.
|
|
|
|
*
|
|
|
|
* @param timeout the timeout to use when joining.
|
|
|
|
* @return a reference to this builder.
|
|
|
|
*/
|
|
|
|
public Builder timeoutAfter(long timeout) {
|
|
|
|
if (timeout <= 0) {
|
|
|
|
throw new IllegalArgumentException("timeout must be positive");
|
|
|
|
}
|
|
|
|
this.timeout = timeout;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request that that MUC is going to sent us no history when joining.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-07-06 09:55:25 +02:00
|
|
|
* @return a reference to this builder.
|
|
|
|
*/
|
|
|
|
public Builder requestNoHistory() {
|
|
|
|
maxChars = 0;
|
|
|
|
maxStanzas = -1;
|
|
|
|
seconds = -1;
|
|
|
|
since = null;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the total number of characters to receive in the history.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-07-06 09:55:25 +02:00
|
|
|
* @param maxChars the total number of characters to receive in the history.
|
|
|
|
* @return a reference to this builder.
|
|
|
|
*/
|
|
|
|
public Builder requestMaxCharsHistory(int maxChars) {
|
|
|
|
this.maxChars = maxChars;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the total number of messages to receive in the history.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-07-06 09:55:25 +02:00
|
|
|
* @param maxStanzas the total number of messages to receive in the history.
|
|
|
|
* @return a reference to this builder.
|
|
|
|
*/
|
|
|
|
public Builder requestMaxStanzasHistory(int maxStanzas) {
|
|
|
|
this.maxStanzas = maxStanzas;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Sets the number of seconds to use to filter the messages received during that time.
|
|
|
|
* In other words, only the messages received in the last "X" seconds will be included in
|
2015-07-06 09:55:25 +02:00
|
|
|
* the history.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
|
|
|
* @param seconds the number of seconds to use to filter the messages received during
|
2015-07-06 09:55:25 +02:00
|
|
|
* that time.
|
|
|
|
* @return a reference to this builder.
|
|
|
|
*/
|
|
|
|
public Builder requestHistorySince(int seconds) {
|
|
|
|
this.seconds = seconds;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Sets the since date to use to filter the messages received during that time.
|
|
|
|
* In other words, only the messages received since the datetime specified will be
|
2015-07-06 09:55:25 +02:00
|
|
|
* included in the history.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-07-06 09:55:25 +02:00
|
|
|
* @param since the since date to use to filter the messages received during that time.
|
|
|
|
* @return a reference to this builder.
|
|
|
|
*/
|
|
|
|
public Builder requestHistorySince(Date since) {
|
|
|
|
this.since = since;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a new {@link MucEnterConfiguration} with the current builder.
|
|
|
|
*
|
|
|
|
* @return a new {@code MucEnterConfiguration}.
|
|
|
|
*/
|
|
|
|
public MucEnterConfiguration build() {
|
|
|
|
return new MucEnterConfiguration(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|