mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 06:12:05 +01:00
[muc] Introduce MucMessageInterceptor
This commit is contained in:
parent
b8f23eb371
commit
4381d04831
3 changed files with 68 additions and 2 deletions
|
@ -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);
|
||||||
|
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
@ -156,10 +157,13 @@ public class MultiUserChat {
|
||||||
*/
|
*/
|
||||||
private volatile boolean processedReflectedSelfPresence;
|
private volatile boolean processedReflectedSelfPresence;
|
||||||
|
|
||||||
|
private CopyOnWriteArrayList<MucMessageInterceptor> messageInterceptors;
|
||||||
|
|
||||||
MultiUserChat(XMPPConnection connection, EntityBareJid room, MultiUserChatManager multiUserChatManager) {
|
MultiUserChat(XMPPConnection connection, EntityBareJid room, MultiUserChatManager multiUserChatManager) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.room = room;
|
this.room = room;
|
||||||
this.multiUserChatManager = multiUserChatManager;
|
this.multiUserChatManager = multiUserChatManager;
|
||||||
|
this.messageInterceptors = MultiUserChatManager.getMessageInterceptors();
|
||||||
|
|
||||||
fromRoomFilter = FromMatchesFilter.create(room);
|
fromRoomFilter = FromMatchesFilter.create(room);
|
||||||
fromRoomGroupchatFilter = new AndFilter(fromRoomFilter, MessageTypeFilter.GROUPCHAT);
|
fromRoomGroupchatFilter = new AndFilter(fromRoomFilter, MessageTypeFilter.GROUPCHAT);
|
||||||
|
@ -778,8 +782,7 @@ public class MultiUserChat {
|
||||||
MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF),
|
MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF),
|
||||||
new AndFilter(fromRoomFilter, PresenceTypeFilter.ERROR)));
|
new AndFilter(fromRoomFilter, PresenceTypeFilter.ERROR)));
|
||||||
|
|
||||||
boolean supportsStableId = mucServiceDiscoInfo.containsFeature(MultiUserChatConstants.STABLE_ID_FEATURE);
|
if (serviceSupportsStableIds()) {
|
||||||
if (supportsStableId) {
|
|
||||||
reflectedLeavePresenceFilters.add(new StanzaIdFilter(leavePresence));
|
reflectedLeavePresenceFilters.add(new StanzaIdFilter(leavePresence));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2035,6 +2038,10 @@ public class MultiUserChat {
|
||||||
* @throws InterruptedException if the calling thread was interrupted.
|
* @throws InterruptedException if the calling thread was interrupted.
|
||||||
*/
|
*/
|
||||||
public void sendMessage(MessageBuilder messageBuilder) throws NotConnectedException, InterruptedException {
|
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();
|
Message message = messageBuilder.to(room).ofType(Message.Type.groupchat).build();
|
||||||
connection.sendStanza(message);
|
connection.sendStanza(message);
|
||||||
}
|
}
|
||||||
|
@ -2118,6 +2125,14 @@ public class MultiUserChat {
|
||||||
return messageListeners.remove(listener);
|
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"
|
* 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
|
* 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;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean serviceSupportsStableIds() {
|
||||||
|
return mucServiceDiscoInfo.containsFeature(MultiUserChatConstants.STABLE_ID_FEATURE);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "MUC: " + room + "(" + connection.getUser() + ")";
|
return "MUC: " + room + "(" + connection.getUser() + ")";
|
||||||
|
|
|
@ -20,10 +20,12 @@ import java.lang.ref.WeakReference;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
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<>(
|
private static final ExpirationCache<DomainBareJid, DiscoverInfo> KNOWN_MUC_SERVICES = new ExpirationCache<>(
|
||||||
100, 1000 * 60 * 60 * 24);
|
100, 1000 * 60 * 60 * 24);
|
||||||
|
|
||||||
|
private static final Set<MucMessageInterceptor> DEFAULT_MESSAGE_INTERCEPTORS = new HashSet<>();
|
||||||
|
|
||||||
private final Set<InvitationListener> invitationsListeners = new CopyOnWriteArraySet<InvitationListener>();
|
private final Set<InvitationListener> invitationsListeners = new CopyOnWriteArraySet<InvitationListener>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -275,6 +279,18 @@ public final class MultiUserChatManager extends Manager {
|
||||||
return multiUserChat;
|
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) {
|
private MultiUserChat createNewMucAndAddToMap(EntityBareJid jid) {
|
||||||
MultiUserChat multiUserChat = new MultiUserChat(connection(), jid, this);
|
MultiUserChat multiUserChat = new MultiUserChat(connection(), jid, this);
|
||||||
multiUserChats.put(jid, new WeakReference<MultiUserChat>(multiUserChat));
|
multiUserChats.put(jid, new WeakReference<MultiUserChat>(multiUserChat));
|
||||||
|
@ -535,4 +551,10 @@ public final class MultiUserChatManager extends Manager {
|
||||||
void removeJoinedRoom(EntityBareJid room) {
|
void removeJoinedRoom(EntityBareJid room) {
|
||||||
joinedRooms.remove(room);
|
joinedRooms.remove(room);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CopyOnWriteArrayList<MucMessageInterceptor> getMessageInterceptors() {
|
||||||
|
synchronized (DEFAULT_MESSAGE_INTERCEPTORS) {
|
||||||
|
return new CopyOnWriteArrayList<>(DEFAULT_MESSAGE_INTERCEPTORS);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue