1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 02:27:58 +01:00

[muc] Introduce MucMessageInterceptor

This commit is contained in:
Florian Schmaus 2021-03-23 21:21:19 +01:00
parent b8f23eb371
commit 4381d04831
3 changed files with 68 additions and 2 deletions

View file

@ -0,0 +1,25 @@
/**
*
* Copyright 2021 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;
import org.jivesoftware.smack.packet.MessageBuilder;
public interface MucMessageInterceptor {
void intercept(MessageBuilder messageBuilder, MultiUserChat multiUserChat);
}

View file

@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -156,10 +157,13 @@ public class MultiUserChat {
*/
private volatile boolean processedReflectedSelfPresence;
private CopyOnWriteArrayList<MucMessageInterceptor> messageInterceptors;
MultiUserChat(XMPPConnection connection, EntityBareJid room, MultiUserChatManager multiUserChatManager) {
this.connection = connection;
this.room = room;
this.multiUserChatManager = multiUserChatManager;
this.messageInterceptors = MultiUserChatManager.getMessageInterceptors();
fromRoomFilter = FromMatchesFilter.create(room);
fromRoomGroupchatFilter = new AndFilter(fromRoomFilter, MessageTypeFilter.GROUPCHAT);
@ -778,8 +782,7 @@ public class MultiUserChat {
MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF),
new AndFilter(fromRoomFilter, PresenceTypeFilter.ERROR)));
boolean supportsStableId = mucServiceDiscoInfo.containsFeature(MultiUserChatConstants.STABLE_ID_FEATURE);
if (supportsStableId) {
if (serviceSupportsStableIds()) {
reflectedLeavePresenceFilters.add(new StanzaIdFilter(leavePresence));
}
@ -2035,6 +2038,10 @@ public class MultiUserChat {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void sendMessage(MessageBuilder messageBuilder) throws NotConnectedException, InterruptedException {
for (MucMessageInterceptor interceptor : messageInterceptors) {
interceptor.intercept(messageBuilder, this);
}
Message message = messageBuilder.to(room).ofType(Message.Type.groupchat).build();
connection.sendStanza(message);
}
@ -2118,6 +2125,14 @@ public class MultiUserChat {
return messageListeners.remove(listener);
}
public boolean addMessageInterceptor(MucMessageInterceptor interceptor) {
return messageInterceptors.add(interceptor);
}
public boolean removeMessageInterceptor(MucMessageInterceptor interceptor) {
return messageInterceptors.remove(interceptor);
}
/**
* Changes the subject within the room. As a default, only users with a role of "moderator"
* are allowed to change the subject in a room. Although some rooms may be configured to
@ -2539,6 +2554,10 @@ public class MultiUserChat {
return connection;
}
public boolean serviceSupportsStableIds() {
return mucServiceDiscoInfo.containsFeature(MultiUserChatConstants.STABLE_ID_FEATURE);
}
@Override
public String toString() {
return "MUC: " + room + "(" + connection.getUser() + ")";

View file

@ -20,10 +20,12 @@ import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -140,6 +142,8 @@ public final class MultiUserChatManager extends Manager {
private static final ExpirationCache<DomainBareJid, DiscoverInfo> KNOWN_MUC_SERVICES = new ExpirationCache<>(
100, 1000 * 60 * 60 * 24);
private static final Set<MucMessageInterceptor> DEFAULT_MESSAGE_INTERCEPTORS = new HashSet<>();
private final Set<InvitationListener> invitationsListeners = new CopyOnWriteArraySet<InvitationListener>();
/**
@ -275,6 +279,18 @@ public final class MultiUserChatManager extends Manager {
return multiUserChat;
}
public static boolean addDefaultMessageInterceptor(MucMessageInterceptor messageInterceptor) {
synchronized (DEFAULT_MESSAGE_INTERCEPTORS) {
return DEFAULT_MESSAGE_INTERCEPTORS.add(messageInterceptor);
}
}
public static boolean removeDefaultMessageInterceptor(MucMessageInterceptor messageInterceptor) {
synchronized (DEFAULT_MESSAGE_INTERCEPTORS) {
return DEFAULT_MESSAGE_INTERCEPTORS.remove(messageInterceptor);
}
}
private MultiUserChat createNewMucAndAddToMap(EntityBareJid jid) {
MultiUserChat multiUserChat = new MultiUserChat(connection(), jid, this);
multiUserChats.put(jid, new WeakReference<MultiUserChat>(multiUserChat));
@ -535,4 +551,10 @@ public final class MultiUserChatManager extends Manager {
void removeJoinedRoom(EntityBareJid room) {
joinedRooms.remove(room);
}
static CopyOnWriteArrayList<MucMessageInterceptor> getMessageInterceptors() {
synchronized (DEFAULT_MESSAGE_INTERCEPTORS) {
return new CopyOnWriteArrayList<>(DEFAULT_MESSAGE_INTERCEPTORS);
}
}
}