From fcc62ad131656e4f4cbdd17af90aec4bc96b6e93 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 23 Apr 2015 23:10:41 +0200 Subject: [PATCH] Add support for MUC roomnick rewrite SMACK-646 --- .../smackx/muc/MultiUserChat.java | 12 +++-- .../muc/filter/MUCUserStatusCodeFilter.java | 51 +++++++++++++++++++ .../smackx/muc/filter/package-info.java | 21 ++++++++ .../smackx/muc/packet/MUCUser.java | 6 +++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/MUCUserStatusCodeFilter.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/package-info.java 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 0d8a09e64..646672e36 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 @@ -60,6 +60,7 @@ import org.jivesoftware.smackx.iqregister.packet.Registration; import org.jivesoftware.smackx.muc.MultiUserChatException.MucAlreadyJoinedException; import org.jivesoftware.smackx.muc.MultiUserChatException.MucNotJoinedException; import org.jivesoftware.smackx.muc.MultiUserChatException.MissingMucCreationAcknowledgeException; +import org.jivesoftware.smackx.muc.filter.MUCUserStatusCodeFilter; import org.jivesoftware.smackx.muc.packet.Destroy; import org.jivesoftware.smackx.muc.packet.MUCAdmin; import org.jivesoftware.smackx.muc.packet.MUCInitialPresence; @@ -308,9 +309,6 @@ public class MultiUserChat { } joinPresence.addExtension(mucInitialPresence); - // Wait for a presence packet back from the server. - StanzaFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(jid), new StanzaTypeFilter(Presence.class)); - // Setup the messageListeners and presenceListeners *before* the join presence is send. connection.addSyncStanzaListener(messageListener, fromRoomGroupchatFilter); connection.addSyncStanzaListener(presenceListener, new AndFilter(fromRoomFilter, @@ -323,6 +321,10 @@ public class MultiUserChat { StanzaTypeFilter.PRESENCE)); messageCollector = connection.createPacketCollector(fromRoomGroupchatFilter); + // Wait for a presence packet back from the server. + // Use a bare JID filter, since the room may rewrite the nickname. + StanzaFilter responseFilter = new AndFilter(FromMatchesFilter.createBare(jid), new StanzaTypeFilter( + Presence.class), MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF); Presence presence; try { presence = connection.createPacketCollectorAndSend(responseFilter, joinPresence).nextResultOrThrow(timeout); @@ -333,7 +335,9 @@ public class MultiUserChat { throw e; } - this.nickname = nickname; + // 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().asFullJidIfPossible().getResourcepart(); joined = true; // Update the list of joined rooms diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/MUCUserStatusCodeFilter.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/MUCUserStatusCodeFilter.java new file mode 100644 index 000000000..ac6a2d50a --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/MUCUserStatusCodeFilter.java @@ -0,0 +1,51 @@ +/** + * + * Copyright 2015 Florian Schmaus + * + * 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.filter; + +import org.jivesoftware.smack.filter.StanzaFilter; +import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smackx.muc.packet.MUCUser; + +public class MUCUserStatusCodeFilter implements StanzaFilter { + + public static final MUCUserStatusCodeFilter STATUS_110_PRESENCE_TO_SELF = new MUCUserStatusCodeFilter( + MUCUser.Status.PRESENCE_TO_SELF_110); + + private final MUCUser.Status status; + + public MUCUserStatusCodeFilter(MUCUser.Status status) { + this.status = status; + } + + public MUCUserStatusCodeFilter(int statusCode) { + this(MUCUser.Status.create(statusCode)); + } + + @Override + public boolean accept(Stanza stanza) { + MUCUser mucUser = MUCUser.from(stanza); + if (mucUser == null) { + return false; + } + return mucUser.getStatus().contains(status); + } + + @Override + public String toString() { + return getClass().getSimpleName() + ": status=" + status; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/package-info.java new file mode 100644 index 000000000..972663c10 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/filter/package-info.java @@ -0,0 +1,21 @@ +/** + * + * Copyright 2015 Florian Schmaus + * + * 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. + */ + +/** + * Stanza filters for Multi-User Chat. + */ +package org.jivesoftware.smackx.muc.filter; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java index e9ed4c8bc..dcaa2eab7 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java @@ -418,6 +418,7 @@ public class MUCUser implements ExtensionElement { private static final Map statusMap = new HashMap(8); + public static final Status PRESENCE_TO_SELF_110 = Status.create(110); public static final Status ROOM_CREATED_201 = Status.create(201); public static final Status BANNED_301 = Status.create(301); public static final Status NEW_NICKNAME_303 = Status.create(303); @@ -467,6 +468,11 @@ public class MUCUser implements ExtensionElement { return xml; } + @Override + public String toString() { + return code.toString(); + } + @Override public boolean equals(Object other) { if (other == null) {