001/**
002 *
003 * Copyright © 2015 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.muc.bookmarkautojoin;
018
019import java.util.List;
020import java.util.Map;
021import java.util.WeakHashMap;
022import java.util.logging.Level;
023import java.util.logging.Logger;
024
025import org.jivesoftware.smack.AbstractConnectionListener;
026import org.jivesoftware.smack.ConnectionCreationListener;
027import org.jivesoftware.smack.Manager;
028import org.jivesoftware.smack.SmackException.NoResponseException;
029import org.jivesoftware.smack.SmackException.NotConnectedException;
030import org.jivesoftware.smack.XMPPConnection;
031import org.jivesoftware.smack.XMPPConnectionRegistry;
032import org.jivesoftware.smack.XMPPException.XMPPErrorException;
033import org.jivesoftware.smackx.bookmarks.BookmarkManager;
034import org.jivesoftware.smackx.bookmarks.BookmarkedConference;
035import org.jivesoftware.smackx.muc.MultiUserChat;
036import org.jivesoftware.smackx.muc.MultiUserChat.MucCreateConfigFormHandle;
037import org.jivesoftware.smackx.muc.MultiUserChatException.NotAMucServiceException;
038import org.jivesoftware.smackx.muc.MultiUserChatManager;
039import org.jxmpp.jid.parts.Resourcepart;
040
041/**
042 * Autojoin bookmarked Multi-User Chat conferences.
043 *
044 * @see <a href="http://xmpp.org/extensions/xep-0048.html">XEP-48: Bookmarks</a>
045 *
046 */
047public final class MucBookmarkAutojoinManager extends Manager {
048
049    private static final Logger LOGGER = Logger.getLogger(MucBookmarkAutojoinManager.class.getName());
050
051    private static final Map<XMPPConnection, MucBookmarkAutojoinManager> INSTANCES = new WeakHashMap<>();
052
053    private static boolean autojoinEnabledDefault = false;
054
055    static {
056        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
057            @Override
058            public void connectionCreated(XMPPConnection connection) {
059                getInstanceFor(connection);
060            }
061        });
062    }
063
064    public static void setAutojoinPerDefault(boolean autojoin) {
065        autojoinEnabledDefault = autojoin;
066    }
067
068    public static synchronized MucBookmarkAutojoinManager getInstanceFor(XMPPConnection connection) {
069        MucBookmarkAutojoinManager mbam = INSTANCES.get(connection);
070        if (mbam == null) {
071            mbam = new MucBookmarkAutojoinManager(connection);
072            INSTANCES.put(connection, mbam);
073        }
074        return mbam;
075    }
076
077    private final MultiUserChatManager multiUserChatManager;
078    private final BookmarkManager bookmarkManager;
079
080    private boolean autojoinEnabled = autojoinEnabledDefault;
081
082    private MucBookmarkAutojoinManager(XMPPConnection connection) {
083        super(connection);
084        multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);
085        bookmarkManager = BookmarkManager.getBookmarkManager(connection);
086        connection.addConnectionListener(new AbstractConnectionListener() {
087            @Override
088            public void authenticated(XMPPConnection connection, boolean resumed) {
089                if (!autojoinEnabled) {
090                    return;
091                }
092                // TODO handle resumed case?
093                autojoinBookmarkedConferences();
094            }
095        });
096    }
097
098    public void setAutojoinEnabled(boolean autojoin) {
099        autojoinEnabled = autojoin;
100    }
101
102    public void autojoinBookmarkedConferences() {
103        List<BookmarkedConference> bookmarkedConferences;
104        try {
105            bookmarkedConferences = bookmarkManager.getBookmarkedConferences();
106        }
107        catch (NotConnectedException | InterruptedException e) {
108            LOGGER.log(Level.FINER, "Could not get MUC bookmarks", e);
109            return;
110        }
111        catch (NoResponseException | XMPPErrorException e) {
112            LOGGER.log(Level.WARNING, "Could not get MUC bookmarks", e);
113            return;
114        }
115
116        final XMPPConnection connection = connection();
117        Resourcepart defaultNick = connection.getUser().getResourcepart();
118
119        for (BookmarkedConference bookmarkedConference : bookmarkedConferences) {
120            if (!bookmarkedConference.isAutoJoin()) {
121                continue;
122            }
123            Resourcepart nick = bookmarkedConference.getNickname();
124            if (nick == null) {
125                nick = defaultNick;
126            }
127            String password = bookmarkedConference.getPassword();
128            MultiUserChat muc = multiUserChatManager.getMultiUserChat(bookmarkedConference.getJid());
129            try {
130                MucCreateConfigFormHandle handle = muc.createOrJoinIfNecessary(nick, password);
131                if (handle != null) {
132                    handle.makeInstant();
133                }
134            }
135            catch (NotConnectedException | InterruptedException e) {
136                LOGGER.log(Level.FINER, "Could not autojoin bookmarked MUC", e);
137                // abort here
138                break;
139            }
140            catch (NotAMucServiceException | NoResponseException | XMPPErrorException e) {
141                // Do no abort, just log,
142                LOGGER.log(Level.WARNING, "Could not autojoin bookmarked MUC", e);
143            }
144        }
145    }
146}