导航菜单

页面标题

页面副标题

bbinstant v6.24.0 - MultiUserChatManager.java 源代码

正在查看: bbinstant v6.24.0 应用的 MultiUserChatManager.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.jivesoftware.smackx.muc;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.Async;
import org.jivesoftware.smackx.disco.AbstractNodeInformationProvider;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.muc.MultiUserChatException;
import org.jivesoftware.smackx.muc.packet.MUCInitialPresence;
import org.jivesoftware.smackx.muc.packet.MUCUser;
import org.jxmpp.jid.b;
import org.jxmpp.jid.e;
import org.jxmpp.jid.g;
import org.jxmpp.jid.i;
import org.jxmpp.jid.parts.d;

public final class MultiUserChatManager extends Manager {
    private static final String DISCO_NODE = "http://jabber.org/protocol/muc#rooms";
    private static final Map<XMPPConnection, MultiUserChatManager> INSTANCES;
    private static final StanzaFilter INVITATION_FILTER;
    private static final Logger LOGGER = Logger.getLogger(MultiUserChatManager.class.getName());
    private AutoJoinFailedCallback autoJoinFailedCallback;
    private boolean autoJoinOnReconnect;
    private final Set<InvitationListener> invitationsListeners;
    private final Set<e> joinedRooms;
    private final Map<e, WeakReference<MultiUserChat>> multiUserChats;

    static {
        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
            @Override
            public void connectionCreated(XMPPConnection xMPPConnection) {
                ServiceDiscoveryManager.getInstanceFor(xMPPConnection).addFeature(MUCInitialPresence.NAMESPACE);
                final WeakReference weakReference = new WeakReference(xMPPConnection);
                ServiceDiscoveryManager.getInstanceFor(xMPPConnection).setNodeInformationProvider(MultiUserChatManager.DISCO_NODE, new AbstractNodeInformationProvider() {
                    @Override
                    public List<DiscoverItems.Item> getNodeItems() {
                        XMPPConnection xMPPConnection2 = (XMPPConnection) weakReference.get();
                        if (xMPPConnection2 == null) {
                            return Collections.emptyList();
                        }
                        Set<e> joinedRooms = MultiUserChatManager.getInstanceFor(xMPPConnection2).getJoinedRooms();
                        ArrayList arrayList = new ArrayList();
                        Iterator<e> it = joinedRooms.iterator();
                        while (it.hasNext()) {
                            arrayList.add(new DiscoverItems.Item(it.next()));
                        }
                        return arrayList;
                    }
                });
            }
        });
        INSTANCES = new WeakHashMap();
        INVITATION_FILTER = new AndFilter(StanzaTypeFilter.MESSAGE, new StanzaExtensionFilter(new MUCUser()), new NotFilter(MessageTypeFilter.ERROR));
    }

    private MultiUserChatManager(XMPPConnection xMPPConnection) {
        super(xMPPConnection);
        this.invitationsListeners = new CopyOnWriteArraySet();
        this.joinedRooms = new HashSet();
        this.multiUserChats = new HashMap();
        xMPPConnection.addAsyncStanzaListener(new StanzaListener() {
            @Override
            public void processStanza(Stanza stanza) {
                Message message = (Message) stanza;
                MUCUser from = MUCUser.from(message);
                if (from.getInvite() != null) {
                    e asEntityBareJidIfPossible = message.getFrom().asEntityBareJidIfPossible();
                    if (asEntityBareJidIfPossible == null) {
                        MultiUserChatManager.LOGGER.warning("Invite to non bare JID: '" + ((Object) message.toXML()) + "'");
                        return;
                    }
                    MultiUserChat multiUserChat = MultiUserChatManager.this.getMultiUserChat(asEntityBareJidIfPossible);
                    XMPPConnection connection = MultiUserChatManager.this.connection();
                    MUCUser.Invite invite = from.getInvite();
                    g from2 = invite.getFrom();
                    String reason = invite.getReason();
                    String password = from.getPassword();
                    Iterator it = MultiUserChatManager.this.invitationsListeners.iterator();
                    while (it.hasNext()) {
                        ((InvitationListener) it.next()).invitationReceived(connection, multiUserChat, from2, reason, password, message, invite);
                    }
                }
            }
        }, INVITATION_FILTER);
        xMPPConnection.addConnectionListener(new AbstractConnectionListener() {
            @Override
            public void authenticated(XMPPConnection xMPPConnection2, boolean z) {
                if (!z && MultiUserChatManager.this.autoJoinOnReconnect) {
                    final Set<e> joinedRooms = MultiUserChatManager.this.getJoinedRooms();
                    if (joinedRooms.isEmpty()) {
                        return;
                    }
                    Async.go(new Runnable() {
                        @Override
                        public void run() {
                            d nickname;
                            AutoJoinFailedCallback autoJoinFailedCallback = MultiUserChatManager.this.autoJoinFailedCallback;
                            Iterator it = joinedRooms.iterator();
                            while (it.hasNext()) {
                                MultiUserChat multiUserChat = MultiUserChatManager.this.getMultiUserChat((e) it.next());
                                if (!multiUserChat.isJoined() || (nickname = multiUserChat.getNickname()) == null) {
                                    return;
                                }
                                try {
                                    multiUserChat.leave();
                                    try {
                                        multiUserChat.join(nickname);
                                    } catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | MultiUserChatException.NotAMucServiceException e) {
                                        if (autoJoinFailedCallback != null) {
                                            autoJoinFailedCallback.autoJoinFailed(multiUserChat, e);
                                            return;
                                        } else {
                                            MultiUserChatManager.LOGGER.log(Level.WARNING, "Could not leave room", (Throwable) e);
                                            return;
                                        }
                                    }
                                } catch (InterruptedException | SmackException.NotConnectedException e2) {
                                    if (autoJoinFailedCallback != null) {
                                        autoJoinFailedCallback.autoJoinFailed(multiUserChat, e2);
                                        return;
                                    } else {
                                        MultiUserChatManager.LOGGER.log(Level.WARNING, "Could not leave room", (Throwable) e2);
                                        return;
                                    }
                                }
                            }
                        }
                    });
                }
            }
        });
    }

    private MultiUserChat createNewMucAndAddToMap(e eVar) {
        MultiUserChat multiUserChat = new MultiUserChat(connection(), eVar, this);
        this.multiUserChats.put(eVar, new WeakReference<>(multiUserChat));
        return multiUserChat;
    }

    public static synchronized MultiUserChatManager getInstanceFor(XMPPConnection xMPPConnection) {
        MultiUserChatManager multiUserChatManager;
        synchronized (MultiUserChatManager.class) {
            Map<XMPPConnection, MultiUserChatManager> map = INSTANCES;
            multiUserChatManager = map.get(xMPPConnection);
            if (multiUserChatManager == null) {
                multiUserChatManager = new MultiUserChatManager(xMPPConnection);
                map.put(xMPPConnection, multiUserChatManager);
            }
        }
        return multiUserChatManager;
    }

    public void addInvitationListener(InvitationListener invitationListener) {
        this.invitationsListeners.add(invitationListener);
    }

    void addJoinedRoom(e eVar) {
        this.joinedRooms.add(eVar);
    }

    public void decline(e eVar, e eVar2, String str) throws SmackException.NotConnectedException, InterruptedException {
        Stanza message = new Message(eVar);
        MUCUser mUCUser = new MUCUser();
        mUCUser.setDecline(new MUCUser.Decline(str, eVar2));
        message.addExtension(mUCUser);
        connection().sendStanza(message);
    }

    public List<HostedRoom> getHostedRooms(b bVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, MultiUserChatException.NotAMucServiceException {
        if (!providesMucService(bVar)) {
            throw new MultiUserChatException.NotAMucServiceException(bVar);
        }
        List<DiscoverItems.Item> items = ServiceDiscoveryManager.getInstanceFor(connection()).discoverItems(bVar).getItems();
        ArrayList arrayList = new ArrayList(items.size());
        Iterator<DiscoverItems.Item> it = items.iterator();
        while (it.hasNext()) {
            arrayList.add(new HostedRoom(it.next()));
        }
        return arrayList;
    }

    public Set<e> getJoinedRooms() {
        return Collections.unmodifiableSet(this.joinedRooms);
    }

    public synchronized MultiUserChat getMultiUserChat(e eVar) {
        WeakReference<MultiUserChat> weakReference = this.multiUserChats.get(eVar);
        if (weakReference == null) {
            return createNewMucAndAddToMap(eVar);
        }
        MultiUserChat multiUserChat = weakReference.get();
        if (multiUserChat != null) {
            return multiUserChat;
        }
        return createNewMucAndAddToMap(eVar);
    }

    public RoomInfo getRoomInfo(e eVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return new RoomInfo(ServiceDiscoveryManager.getInstanceFor(connection()).discoverInfo(eVar));
    }

    public List<b> getXMPPServiceDomains() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return ServiceDiscoveryManager.getInstanceFor(connection()).findServices(MUCInitialPresence.NAMESPACE, false, false);
    }

    public boolean isServiceEnabled(i iVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(iVar, MUCInitialPresence.NAMESPACE);
    }

    public boolean providesMucService(b bVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(bVar, MUCInitialPresence.NAMESPACE);
    }

    public void removeInvitationListener(InvitationListener invitationListener) {
        this.invitationsListeners.remove(invitationListener);
    }

    void removeJoinedRoom(e eVar) {
        this.joinedRooms.remove(eVar);
    }

    public void setAutoJoinFailedCallback(AutoJoinFailedCallback autoJoinFailedCallback) {
        this.autoJoinFailedCallback = autoJoinFailedCallback;
        if (autoJoinFailedCallback != null) {
            setAutoJoinOnReconnect(true);
        }
    }

    public void setAutoJoinOnReconnect(boolean z) {
        this.autoJoinOnReconnect = z;
    }

    public List<e> getJoinedRooms(g gVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
        List<DiscoverItems.Item> items = ServiceDiscoveryManager.getInstanceFor(connection()).discoverItems(gVar, DISCO_NODE).getItems();
        ArrayList arrayList = new ArrayList(items.size());
        for (DiscoverItems.Item item : items) {
            e asEntityBareJidIfPossible = item.getEntityID().asEntityBareJidIfPossible();
            if (asEntityBareJidIfPossible == null) {
                LOGGER.warning("Not a bare JID: " + ((Object) item.getEntityID()));
            } else {
                arrayList.add(asEntityBareJidIfPossible);
            }
        }
        return arrayList;
    }
}