From 6ba9218c7779f4051c68b2c47d58306eb41f4c9e Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 12 Jun 2018 11:14:38 +0200 Subject: [PATCH] Introduce myRoomJid EntityFullJid in MultiUserChat --- .../smackx/muc/MultiUserChat.java | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java index f2f2488b8..cdf1adc50 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -57,7 +57,7 @@ import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Stanza; -import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.packet.DiscoverInfo; @@ -149,7 +149,7 @@ public class MultiUserChat { private final StanzaListener declinesListener; private String subject; - private Resourcepart nickname; + private EntityFullJid myRoomJid; private boolean joined = false; private StanzaCollector messageCollector; @@ -208,7 +208,7 @@ public class MultiUserChat { LOGGER.warning("Presence not from a full JID: " + presence.getFrom()); return; } - final String myRoomJID = MultiUserChat.this.room + "/" + nickname; + final EntityFullJid myRoomJID = myRoomJid; final boolean isUserStatusModification = presence.getFrom().equals(myRoomJID); asyncButOrdered.performAsyncButOrdered(MultiUserChat.this, new Runnable() { @@ -383,7 +383,9 @@ public class MultiUserChat { // This presence must be send from a full JID. We use the resourcepart of this JID as nick, since the room may // performed roomnick rewriting - this.nickname = presence.getFrom().asEntityFullJidIfPossible().getResourcepart(); + Resourcepart receivedNickname = presence.getFrom().getResourceOrThrow(); + setNickname(receivedNickname); + joined = true; // Update the list of joined rooms @@ -391,6 +393,10 @@ public class MultiUserChat { return presence; } + private void setNickname(Resourcepart nickname) { + this.myRoomJid = JidCreate.entityFullFrom(room, nickname); + } + /** * Get a new MUC enter configuration builder. * @@ -486,6 +492,7 @@ public class MultiUserChat { * @deprecated use {@link #createOrJoin(MucEnterConfiguration)} instead. */ @Deprecated + // TODO Remove in Smack 4.4 public MucCreateConfigFormHandle createOrJoin(Resourcepart nickname, String password, @SuppressWarnings("deprecation") DiscussionHistory history, long timeout) throws NoResponseException, XMPPErrorException, InterruptedException, MucAlreadyJoinedException, NotConnectedException, NotAMucServiceException { MucEnterConfiguration.Builder builder = getEnterConfigurationBuilder(nickname).withPassword( @@ -684,6 +691,7 @@ public class MultiUserChat { * @deprecated use {@link #join(MucEnterConfiguration)} instead. */ @Deprecated + // TODO Remove in Smack 4.4 public void join( Resourcepart nickname, String password, @@ -759,7 +767,7 @@ public class MultiUserChat { // We leave a room by sending a presence packet where the "to" // field is in the form "roomName@service/nickname" Presence leavePresence = new Presence(Presence.Type.unavailable); - leavePresence.setTo(JidCreate.fullFrom(room, nickname)); + leavePresence.setTo(myRoomJid); connection.sendStanza(leavePresence); } @@ -1096,7 +1104,11 @@ public class MultiUserChat { * @return the nickname currently being used. */ public Resourcepart getNickname() { - return nickname; + final EntityFullJid myRoomJid = this.myRoomJid; + if (myRoomJid == null) { + return null; + } + return myRoomJid.getResourcepart(); } /** @@ -1114,7 +1126,7 @@ public class MultiUserChat { * @throws MucNotJoinedException */ public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException { - StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank."); + Objects.requireNonNull(nickname, "Nickname must not be null or blank."); // Check that we already have joined the room before attempting to change the // nickname. if (!joined) { @@ -1137,7 +1149,8 @@ public class MultiUserChat { // exception will be thrown response.nextResultOrThrow(); - this.nickname = nickname; + // TODO: Shouldn't this handle nickname rewriting by the MUC service? + setNickname(nickname); } /** @@ -1152,7 +1165,11 @@ public class MultiUserChat { * @throws MucNotJoinedException */ public void changeAvailabilityStatus(String status, Presence.Mode mode) throws NotConnectedException, InterruptedException, MucNotJoinedException { - StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank."); + final EntityFullJid myRoomJid = this.myRoomJid; + if (myRoomJid == null) { + throw new MucNotJoinedException(this); + } + // Check that we already have joined the room before attempting to change the // availability status. if (!joined) { @@ -1163,7 +1180,7 @@ public class MultiUserChat { Presence joinPresence = new Presence(Presence.Type.available); joinPresence.setStatus(status); joinPresence.setMode(mode); - joinPresence.setTo(JidCreate.fullFrom(room, nickname)); + joinPresence.setTo(myRoomJid); // Send join packet. connection.sendStanza(joinPresence); @@ -2398,7 +2415,7 @@ public class MultiUserChat { // Reset occupant information. occupantsMap.clear(); - nickname = null; + myRoomJid = null; userHasLeft(); } else { @@ -2418,7 +2435,7 @@ public class MultiUserChat { // Reset occupant information. occupantsMap.clear(); - nickname = null; + myRoomJid = null; userHasLeft(); } } @@ -2437,7 +2454,7 @@ public class MultiUserChat { // Reset occupant information. occupantsMap.clear(); - nickname = null; + myRoomJid = null; userHasLeft(); } }