001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2016-2017 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 */
017
018package org.jivesoftware.smack.roster;
019
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.LinkedHashSet;
026import java.util.List;
027import java.util.Map;
028import java.util.Map.Entry;
029import java.util.Set;
030import java.util.WeakHashMap;
031import java.util.concurrent.ConcurrentHashMap;
032import java.util.concurrent.CopyOnWriteArraySet;
033import java.util.logging.Level;
034import java.util.logging.Logger;
035
036import org.jivesoftware.smack.AbstractConnectionListener;
037import org.jivesoftware.smack.ConnectionCreationListener;
038import org.jivesoftware.smack.ExceptionCallback;
039import org.jivesoftware.smack.Manager;
040import org.jivesoftware.smack.SmackException;
041import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
042import org.jivesoftware.smack.SmackException.NoResponseException;
043import org.jivesoftware.smack.SmackException.NotConnectedException;
044import org.jivesoftware.smack.SmackException.NotLoggedInException;
045import org.jivesoftware.smack.StanzaListener;
046import org.jivesoftware.smack.XMPPConnection;
047import org.jivesoftware.smack.XMPPConnectionRegistry;
048import org.jivesoftware.smack.XMPPException.XMPPErrorException;
049import org.jivesoftware.smack.filter.AndFilter;
050import org.jivesoftware.smack.filter.PresenceTypeFilter;
051import org.jivesoftware.smack.filter.StanzaFilter;
052import org.jivesoftware.smack.filter.StanzaTypeFilter;
053import org.jivesoftware.smack.filter.ToMatchesFilter;
054import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
055import org.jivesoftware.smack.packet.IQ;
056import org.jivesoftware.smack.packet.IQ.Type;
057import org.jivesoftware.smack.packet.Presence;
058import org.jivesoftware.smack.packet.Stanza;
059import org.jivesoftware.smack.packet.XMPPError.Condition;
060import org.jivesoftware.smack.roster.SubscribeListener.SubscribeAnswer;
061import org.jivesoftware.smack.roster.packet.RosterPacket;
062import org.jivesoftware.smack.roster.packet.RosterPacket.Item;
063import org.jivesoftware.smack.roster.packet.RosterVer;
064import org.jivesoftware.smack.roster.packet.SubscriptionPreApproval;
065import org.jivesoftware.smack.roster.rosterstore.RosterStore;
066import org.jivesoftware.smack.util.Objects;
067
068import org.jxmpp.jid.BareJid;
069import org.jxmpp.jid.EntityBareJid;
070import org.jxmpp.jid.EntityFullJid;
071import org.jxmpp.jid.FullJid;
072import org.jxmpp.jid.Jid;
073import org.jxmpp.jid.impl.JidCreate;
074import org.jxmpp.jid.parts.Resourcepart;
075import org.jxmpp.util.cache.LruCache;
076
077/**
078 * Represents a user's roster, which is the collection of users a person receives
079 * presence updates for. Roster items are categorized into groups for easier management.
080 *
081 * Other users may attempt to subscribe to this user using a subscription request. Three
082 * modes are supported for handling these requests: <ul>
083 * <li>{@link SubscriptionMode#accept_all accept_all} -- accept all subscription requests.</li>
084 * <li>{@link SubscriptionMode#reject_all reject_all} -- reject all subscription requests.</li>
085 * <li>{@link SubscriptionMode#manual manual} -- manually process all subscription requests.</li>
086 * </ul>
087 *
088 * @author Matt Tucker
089 * @see #getInstanceFor(XMPPConnection)
090 */
091public final class Roster extends Manager {
092
093    private static final Logger LOGGER = Logger.getLogger(Roster.class.getName());
094
095    static {
096        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
097            @Override
098            public void connectionCreated(XMPPConnection connection) {
099                getInstanceFor(connection);
100            }
101        });
102    }
103
104    private static final Map<XMPPConnection, Roster> INSTANCES = new WeakHashMap<>();
105
106    /**
107     * Returns the roster for the user.
108     * <p>
109     * This method will never return <code>null</code>, instead if the user has not yet logged into
110     * the server all modifying methods of the returned roster object
111     * like {@link Roster#createEntry(BareJid, String, String[])},
112     * {@link Roster#removeEntry(RosterEntry)} , etc. except adding or removing
113     * {@link RosterListener}s will throw an IllegalStateException.
114     * </p>
115     * 
116     * @param connection the connection the roster should be retrieved for.
117     * @return the user's roster.
118     */
119    public static synchronized Roster getInstanceFor(XMPPConnection connection) {
120        Roster roster = INSTANCES.get(connection);
121        if (roster == null) {
122            roster = new Roster(connection);
123            INSTANCES.put(connection, roster);
124        }
125        return roster;
126    }
127
128    private static final StanzaFilter PRESENCE_PACKET_FILTER = StanzaTypeFilter.PRESENCE;
129
130    private static final StanzaFilter OUTGOING_USER_UNAVAILABLE_PRESENCE = new AndFilter(PresenceTypeFilter.UNAVAILABLE, ToMatchesFilter.MATCH_NO_TO_SET);
131
132    private static boolean rosterLoadedAtLoginDefault = true;
133
134    /**
135     * The default subscription processing mode to use when a Roster is created. By default
136     * all subscription requests are automatically rejected.
137     */
138    private static SubscriptionMode defaultSubscriptionMode = SubscriptionMode.reject_all;
139
140    /**
141     * The initial maximum size of the map holding presence information of entities without an Roster entry. Currently
142     * {@value #INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE}.
143     */
144    public static final int INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE = 1024;
145
146    private static int defaultNonRosterPresenceMapMaxSize = INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE;
147
148    private RosterStore rosterStore;
149    private final Map<String, RosterGroup> groups = new ConcurrentHashMap<>();
150
151    /**
152     * Concurrent hash map from JID to its roster entry.
153     */
154    private final Map<BareJid, RosterEntry> entries = new ConcurrentHashMap<>();
155
156    private final Set<RosterEntry> unfiledEntries = new CopyOnWriteArraySet<>();
157    private final Set<RosterListener> rosterListeners = new LinkedHashSet<>();
158
159    private final Set<PresenceEventListener> presenceEventListeners = new CopyOnWriteArraySet<>();
160
161    /**
162     * A map of JIDs to another Map of Resourceparts to Presences. The 'inner' map may contain
163     * {@link Resourcepart#EMPTY} if there are no other Presences available.
164     */
165    private final Map<BareJid, Map<Resourcepart, Presence>> presenceMap = new ConcurrentHashMap<>();
166
167    /**
168     * Like {@link presenceMap} but for presences of entities not in our Roster.
169     */
170    // TODO Ideally we want here to use a LRU cache like Map which will evict all superfluous items
171    // if their maximum size is lowered below the current item count. LruCache does not provide
172    // this.
173    private final LruCache<BareJid, Map<Resourcepart, Presence>> nonRosterPresenceMap = new LruCache<>(
174                    defaultNonRosterPresenceMapMaxSize);
175
176    /**
177     * Listeners called when the Roster was loaded.
178     */
179    private final Set<RosterLoadedListener> rosterLoadedListeners = new LinkedHashSet<>();
180
181    /**
182     * Mutually exclude roster listener invocation and changing the {@link entries} map. Also used
183     * to synchronize access to either the roster listeners or the entries map.
184     */
185    private final Object rosterListenersAndEntriesLock = new Object();
186
187    private enum RosterState {
188        uninitialized,
189        loading,
190        loaded,
191    }
192
193    /**
194     * The current state of the roster.
195     */
196    private RosterState rosterState = RosterState.uninitialized;
197
198    private final PresencePacketListener presencePacketListener = new PresencePacketListener();
199
200    /**
201     * 
202     */
203    private boolean rosterLoadedAtLogin = rosterLoadedAtLoginDefault;
204
205    private SubscriptionMode subscriptionMode = getDefaultSubscriptionMode();
206
207    private final Set<SubscribeListener> subscribeListeners = new CopyOnWriteArraySet<>();
208
209    private SubscriptionMode previousSubscriptionMode;
210
211    /**
212     * Returns the default subscription processing mode to use when a new Roster is created. The
213     * subscription processing mode dictates what action Smack will take when subscription
214     * requests from other users are made. The default subscription mode
215     * is {@link SubscriptionMode#reject_all}.
216     *
217     * @return the default subscription mode to use for new Rosters
218     */
219    public static SubscriptionMode getDefaultSubscriptionMode() {
220        return defaultSubscriptionMode;
221    }
222
223    /**
224     * Sets the default subscription processing mode to use when a new Roster is created. The
225     * subscription processing mode dictates what action Smack will take when subscription
226     * requests from other users are made. The default subscription mode
227     * is {@link SubscriptionMode#reject_all}.
228     *
229     * @param subscriptionMode the default subscription mode to use for new Rosters.
230     */
231    public static void setDefaultSubscriptionMode(SubscriptionMode subscriptionMode) {
232        defaultSubscriptionMode = subscriptionMode;
233    }
234
235    /**
236     * Creates a new roster.
237     *
238     * @param connection an XMPP connection.
239     */
240    private Roster(final XMPPConnection connection) {
241        super(connection);
242
243        // Note that we use sync packet listeners because RosterListeners should be invoked in the same order as the
244        // roster stanzas arrive.
245        // Listen for any roster packets.
246        connection.registerIQRequestHandler(new RosterPushListener());
247        // Listen for any presence packets.
248        connection.addSyncStanzaListener(presencePacketListener, PRESENCE_PACKET_FILTER);
249
250        connection.addAsyncStanzaListener(new StanzaListener() {
251            @SuppressWarnings("fallthrough")
252            @Override
253            public void processStanza(Stanza stanza) throws NotConnectedException,
254                            InterruptedException, NotLoggedInException {
255                Presence presence = (Presence) stanza;
256                Jid from = presence.getFrom();
257                SubscribeAnswer subscribeAnswer = null;
258                switch (subscriptionMode) {
259                case manual:
260                    for (SubscribeListener subscribeListener : subscribeListeners) {
261                        subscribeAnswer = subscribeListener.processSubscribe(from, presence);
262                        if (subscribeAnswer != null) {
263                            break;
264                        }
265                    }
266                    if (subscribeAnswer == null) {
267                        return;
268                    }
269                    break;
270                case accept_all:
271                    // Accept all subscription requests.
272                    subscribeAnswer = SubscribeAnswer.Approve;
273                    break;
274                case reject_all:
275                    // Reject all subscription requests.
276                    subscribeAnswer = SubscribeAnswer.Deny;
277                    break;
278                }
279
280                if (subscribeAnswer == null) {
281                    return;
282                }
283
284                Presence response;
285                switch (subscribeAnswer) {
286                case ApproveAndAlsoRequestIfRequired:
287                    BareJid bareFrom = from.asBareJid();
288                    RosterUtil.askForSubscriptionIfRequired(Roster.this, bareFrom);
289                    // The fall through is intended.
290                case Approve:
291                    response = new Presence(Presence.Type.subscribed);
292                    break;
293                case Deny:
294                    response = new Presence(Presence.Type.unsubscribed);
295                    break;
296                default:
297                    throw new AssertionError();
298                }
299
300                response.setTo(presence.getFrom());
301                connection.sendStanza(response);
302            }
303        }, PresenceTypeFilter.SUBSCRIBE);
304
305        // Listen for connection events
306        connection.addConnectionListener(new AbstractConnectionListener() {
307
308            @Override
309            public void authenticated(XMPPConnection connection, boolean resumed) {
310                if (!isRosterLoadedAtLogin())
311                    return;
312                // We are done here if the connection was resumed
313                if (resumed) {
314                    return;
315                }
316
317                // Ensure that all available presences received so far in a eventually existing previous session are
318                // marked 'offline'.
319                setOfflinePresencesAndResetLoaded();
320
321                try {
322                    Roster.this.reload();
323                }
324                catch (InterruptedException | SmackException e) {
325                    LOGGER.log(Level.SEVERE, "Could not reload Roster", e);
326                    return;
327                }
328            }
329
330            @Override
331            public void connectionClosed() {
332                // Changes the presence available contacts to unavailable
333                setOfflinePresencesAndResetLoaded();
334            }
335
336        });
337
338        connection.addStanzaSendingListener(new StanzaListener() {
339            @Override
340            public void processStanza(Stanza stanzav) throws NotConnectedException, InterruptedException {
341                // Once we send an unavailable presence, the server is allowed to suppress sending presence status
342                // information to us as optimization (RFC 6121 § 4.4.2). Thus XMPP clients which are unavailable, should
343                // consider the presence information of their contacts as not up-to-date. We make the user obvious of
344                // this situation by setting the presences of all contacts to unavailable (while keeping the roster
345                // state).
346                setOfflinePresences();
347            }
348        }, OUTGOING_USER_UNAVAILABLE_PRESENCE);
349
350        // If the connection is already established, call reload
351        if (connection.isAuthenticated()) {
352            try {
353                reloadAndWait();
354            }
355            catch (InterruptedException | SmackException e) {
356                LOGGER.log(Level.SEVERE, "Could not reload Roster", e);
357            }
358        }
359
360    }
361
362    /**
363     * Retrieve the user presences (a map from resource to {@link Presence}) for a given XMPP entity represented by their bare JID.
364     *
365     * @param entity the entity
366     * @return the user presences
367     */
368    private Map<Resourcepart, Presence> getPresencesInternal(BareJid entity) {
369        Map<Resourcepart, Presence> entityPresences = presenceMap.get(entity);
370        if (entityPresences == null) {
371            entityPresences = nonRosterPresenceMap.lookup(entity);
372        }
373        return entityPresences;
374    }
375
376    /**
377     * Retrieve the user presences (a map from resource to {@link Presence}) for a given XMPP entity represented by their bare JID.
378     *
379     * @param entity the entity
380     * @return the user presences
381     */
382    private synchronized Map<Resourcepart, Presence> getOrCreatePresencesInternal(BareJid entity) {
383        Map<Resourcepart, Presence> entityPresences = getPresencesInternal(entity);
384        if (entityPresences == null) {
385            if (contains(entity)) {
386                entityPresences = new ConcurrentHashMap<>();
387                presenceMap.put(entity, entityPresences);
388            }
389            else {
390                LruCache<Resourcepart, Presence> nonRosterEntityPresences = new LruCache<>(32);
391                nonRosterPresenceMap.put(entity, nonRosterEntityPresences);
392                entityPresences = nonRosterEntityPresences;
393            }
394        }
395        return entityPresences;
396    }
397
398    /**
399     * Returns the subscription processing mode, which dictates what action
400     * Smack will take when subscription requests from other users are made.
401     * The default subscription mode is {@link SubscriptionMode#reject_all}.
402     * <p>
403     * If using the manual mode, a PacketListener should be registered that
404     * listens for Presence packets that have a type of
405     * {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
406     * </p>
407     *
408     * @return the subscription mode.
409     */
410    public SubscriptionMode getSubscriptionMode() {
411        return subscriptionMode;
412    }
413
414    /**
415     * Sets the subscription processing mode, which dictates what action
416     * Smack will take when subscription requests from other users are made.
417     * The default subscription mode is {@link SubscriptionMode#reject_all}.
418     * <p>
419     * If using the manual mode, a PacketListener should be registered that
420     * listens for Presence packets that have a type of
421     * {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
422     * </p>
423     *
424     * @param subscriptionMode the subscription mode.
425     */
426    public void setSubscriptionMode(SubscriptionMode subscriptionMode) {
427        this.subscriptionMode = subscriptionMode;
428    }
429
430    /**
431     * Reloads the entire roster from the server. This is an asynchronous operation,
432     * which means the method will return immediately, and the roster will be
433     * reloaded at a later point when the server responds to the reload request.
434     * @throws NotLoggedInException If not logged in.
435     * @throws NotConnectedException 
436     * @throws InterruptedException 
437     */
438    public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException {
439        final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
440
441        RosterPacket packet = new RosterPacket();
442        if (rosterStore != null && isRosterVersioningSupported()) {
443            packet.setVersion(rosterStore.getRosterVersion());
444        }
445        rosterState = RosterState.loading;
446        connection.sendIqWithResponseCallback(packet, new RosterResultListener(), new ExceptionCallback() {
447            @Override
448            public void processException(Exception exception) {
449                rosterState = RosterState.uninitialized;
450                Level logLevel;
451                if (exception instanceof NotConnectedException) {
452                    logLevel = Level.FINE;
453                } else {
454                    logLevel = Level.SEVERE;
455                }
456                LOGGER.log(logLevel, "Exception reloading roster" , exception);
457                for (RosterLoadedListener listener : rosterLoadedListeners) {
458                    listener.onRosterLoadingFailed(exception);
459                }
460            }
461        });
462    }
463
464    /**
465     * Reload the roster and block until it is reloaded.
466     *
467     * @throws NotLoggedInException
468     * @throws NotConnectedException
469     * @throws InterruptedException 
470     * @since 4.1
471     */
472    public void reloadAndWait() throws NotLoggedInException, NotConnectedException, InterruptedException {
473        reload();
474        waitUntilLoaded();
475    }
476
477    /**
478     * Set the roster store, may cause a roster reload.
479     *
480     * @param rosterStore
481     * @return true if the roster reload was initiated, false otherwise.
482     * @since 4.1
483     */
484    public boolean setRosterStore(RosterStore rosterStore) {
485        this.rosterStore = rosterStore;
486        try {
487            reload();
488        }
489        catch (InterruptedException | NotLoggedInException | NotConnectedException e) {
490            LOGGER.log(Level.FINER, "Could not reload roster", e);
491            return false;
492        }
493        return true;
494    }
495
496    protected boolean waitUntilLoaded() throws InterruptedException {
497        long waitTime = connection().getReplyTimeout();
498        long start = System.currentTimeMillis();
499        while (!isLoaded()) {
500            if (waitTime <= 0) {
501                break;
502            }
503            synchronized (this) {
504                if (!isLoaded()) {
505                    wait(waitTime);
506                }
507            }
508            long now = System.currentTimeMillis();
509            waitTime -= now - start;
510            start = now;
511        }
512        return isLoaded();
513    }
514
515    /**
516     * Check if the roster is loaded.
517     *
518     * @return true if the roster is loaded.
519     * @since 4.1
520     */
521    public boolean isLoaded() {
522        return rosterState == RosterState.loaded;
523    }
524
525    /**
526     * Adds a listener to this roster. The listener will be fired anytime one or more
527     * changes to the roster are pushed from the server.
528     *
529     * @param rosterListener a roster listener.
530     * @return true if the listener was not already added.
531     * @see #getEntriesAndAddListener(RosterListener, RosterEntries)
532     */
533    public boolean addRosterListener(RosterListener rosterListener) {
534        synchronized (rosterListenersAndEntriesLock) {
535            return rosterListeners.add(rosterListener);
536        }
537    }
538
539    /**
540     * Removes a listener from this roster. The listener will be fired anytime one or more
541     * changes to the roster are pushed from the server.
542     *
543     * @param rosterListener a roster listener.
544     * @return true if the listener was active and got removed.
545     */
546    public boolean removeRosterListener(RosterListener rosterListener) {
547        synchronized (rosterListenersAndEntriesLock) {
548            return rosterListeners.remove(rosterListener);
549        }
550    }
551
552    /**
553     * Add a roster loaded listener.
554     *
555     * @param rosterLoadedListener the listener to add.
556     * @return true if the listener was not already added.
557     * @see RosterLoadedListener
558     * @since 4.1
559     */
560    public boolean addRosterLoadedListener(RosterLoadedListener rosterLoadedListener) {
561        synchronized (rosterLoadedListener) {
562            return rosterLoadedListeners.add(rosterLoadedListener);
563        }
564    }
565
566    /**
567     * Remove a roster loaded listener.
568     *
569     * @param rosterLoadedListener the listener to remove.
570     * @return true if the listener was active and got removed.
571     * @see RosterLoadedListener
572     * @since 4.1
573     */
574    public boolean removeRosterLoadedListener(RosterLoadedListener rosterLoadedListener) {
575        synchronized (rosterLoadedListener) {
576            return rosterLoadedListeners.remove(rosterLoadedListener);
577        }
578    }
579
580    public boolean addPresenceEventListener(PresenceEventListener presenceEventListener) {
581        return presenceEventListeners.add(presenceEventListener);
582    }
583
584    public boolean removePresenceEventListener(PresenceEventListener presenceEventListener) {
585        return presenceEventListeners.remove(presenceEventListener);
586    }
587
588    /**
589     * Creates a new group.
590     * <p>
591     * Note: you must add at least one entry to the group for the group to be kept
592     * after a logout/login. This is due to the way that XMPP stores group information.
593     * </p>
594     *
595     * @param name the name of the group.
596     * @return a new group, or null if the group already exists
597     */
598    public RosterGroup createGroup(String name) {
599        final XMPPConnection connection = connection();
600        if (groups.containsKey(name)) {
601            return groups.get(name);
602        }
603
604        RosterGroup group = new RosterGroup(name, connection);
605        groups.put(name, group);
606        return group;
607    }
608
609    /**
610     * Creates a new roster entry and presence subscription. The server will asynchronously
611     * update the roster with the subscription status.
612     *
613     * @param user   the user. (e.g. johndoe@jabber.org)
614     * @param name   the nickname of the user.
615     * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
616     *               the roster entry won't belong to a group.
617     * @throws NoResponseException if there was no response from the server.
618     * @throws XMPPErrorException if an XMPP exception occurs.
619     * @throws NotLoggedInException If not logged in.
620     * @throws NotConnectedException 
621     * @throws InterruptedException 
622     */
623    public void createEntry(BareJid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
624        final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
625
626        // Create and send roster entry creation packet.
627        RosterPacket rosterPacket = new RosterPacket();
628        rosterPacket.setType(IQ.Type.set);
629        RosterPacket.Item item = new RosterPacket.Item(user, name);
630        if (groups != null) {
631            for (String group : groups) {
632                if (group != null && group.trim().length() > 0) {
633                    item.addGroupName(group);
634                }
635            }
636        }
637        rosterPacket.addRosterItem(item);
638        connection.createStanzaCollectorAndSend(rosterPacket).nextResultOrThrow();
639
640        sendSubscriptionRequest(user);
641    }
642
643    /**
644     * Creates a new pre-approved roster entry and presence subscription. The server will
645     * asynchronously update the roster with the subscription status.
646     *
647     * @param user   the user. (e.g. johndoe@jabber.org)
648     * @param name   the nickname of the user.
649     * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
650     *               the roster entry won't belong to a group.
651     * @throws NoResponseException if there was no response from the server.
652     * @throws XMPPErrorException if an XMPP exception occurs.
653     * @throws NotLoggedInException if not logged in.
654     * @throws NotConnectedException
655     * @throws InterruptedException
656     * @throws FeatureNotSupportedException if pre-approving is not supported.
657     * @since 4.2
658     */
659    public void preApproveAndCreateEntry(BareJid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
660        preApprove(user);
661        createEntry(user, name, groups);
662    }
663
664    /**
665     * Pre-approve user presence subscription.
666     *
667     * @param user the user. (e.g. johndoe@jabber.org)
668     * @throws NotLoggedInException if not logged in.
669     * @throws NotConnectedException
670     * @throws InterruptedException
671     * @throws FeatureNotSupportedException if pre-approving is not supported.
672     * @since 4.2
673     */
674    public void preApprove(BareJid user) throws NotLoggedInException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
675        final XMPPConnection connection = connection();
676        if (!isSubscriptionPreApprovalSupported()) {
677            throw new FeatureNotSupportedException("Pre-approving");
678        }
679
680        Presence presencePacket = new Presence(Presence.Type.subscribed);
681        presencePacket.setTo(user);
682        connection.sendStanza(presencePacket);
683    }
684
685    /**
686     * Check for subscription pre-approval support.
687     *
688     * @return true if subscription pre-approval is supported by the server.
689     * @throws NotLoggedInException if not logged in.
690     * @since 4.2
691     */
692    public boolean isSubscriptionPreApprovalSupported() throws NotLoggedInException {
693        final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
694        return connection.hasFeature(SubscriptionPreApproval.ELEMENT, SubscriptionPreApproval.NAMESPACE);
695    }
696
697    public void sendSubscriptionRequest(BareJid jid) throws NotLoggedInException, NotConnectedException, InterruptedException {
698        final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
699
700        // Create a presence subscription packet and send.
701        Presence presencePacket = new Presence(Presence.Type.subscribe);
702        presencePacket.setTo(jid);
703        connection.sendStanza(presencePacket);
704    }
705
706    /**
707     * Add a subscribe listener, which is invoked on incoming subscription requests and if
708     * {@link SubscriptionMode} is set to {@link SubscriptionMode#manual}. This also sets subscription
709     * mode to {@link SubscriptionMode#manual}.
710     * 
711     * @param subscribeListener the subscribe listener to add.
712     * @return <code>true</code> if the listener was not already added.
713     * @since 4.2
714     */
715    public boolean addSubscribeListener(SubscribeListener subscribeListener) {
716        Objects.requireNonNull(subscribeListener, "SubscribeListener argument must not be null");
717        if (subscriptionMode != SubscriptionMode.manual) {
718            previousSubscriptionMode = subscriptionMode;
719            subscriptionMode = SubscriptionMode.manual;
720        }
721        return subscribeListeners.add(subscribeListener);
722    }
723
724    /**
725     * Remove a subscribe listener. Also restores the previous subscription mode
726     * state, if the last listener got removed.
727     *
728     * @param subscribeListener
729     *            the subscribe listener to remove.
730     * @return <code>true</code> if the listener registered and got removed.
731     * @since 4.2
732     */
733    public boolean removeSubscribeListener(SubscribeListener subscribeListener) {
734        boolean removed = subscribeListeners.remove(subscribeListener);
735        if (removed && subscribeListeners.isEmpty()) {
736            setSubscriptionMode(previousSubscriptionMode);
737        }
738        return removed;
739    }
740
741    /**
742     * Removes a roster entry from the roster. The roster entry will also be removed from the
743     * unfiled entries or from any roster group where it could belong and will no longer be part
744     * of the roster. Note that this is a synchronous call -- Smack must wait for the server
745     * to send an updated subscription status.
746     *
747     * @param entry a roster entry.
748     * @throws XMPPErrorException if an XMPP error occurs.
749     * @throws NotLoggedInException if not logged in.
750     * @throws NoResponseException SmackException if there was no response from the server.
751     * @throws NotConnectedException 
752     * @throws InterruptedException 
753     */
754    public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
755        final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
756
757        // Only remove the entry if it's in the entry list.
758        // The actual removal logic takes place in RosterPacketListenerProcess>>Packet(Packet)
759        if (!entries.containsKey(entry.getJid())) {
760            return;
761        }
762        RosterPacket packet = new RosterPacket();
763        packet.setType(IQ.Type.set);
764        RosterPacket.Item item = RosterEntry.toRosterItem(entry);
765        // Set the item type as REMOVE so that the server will delete the entry
766        item.setItemType(RosterPacket.ItemType.remove);
767        packet.addRosterItem(item);
768        connection.createStanzaCollectorAndSend(packet).nextResultOrThrow();
769    }
770
771    /**
772     * Returns a count of the entries in the roster.
773     *
774     * @return the number of entries in the roster.
775     */
776    public int getEntryCount() {
777        return getEntries().size();
778    }
779
780    /**
781     * Add a roster listener and invoke the roster entries with all entries of the roster.
782     * <p>
783     * The method guarantees that the listener is only invoked after
784     * {@link RosterEntries#rosterEntries(Collection)} has been invoked, and that all roster events
785     * that happen while <code>rosterEntries(Collection) </code> is called are queued until the
786     * method returns.
787     * </p>
788     * <p>
789     * This guarantee makes this the ideal method to e.g. populate a UI element with the roster while
790     * installing a {@link RosterListener} to listen for subsequent roster events.
791     * </p>
792     *
793     * @param rosterListener the listener to install
794     * @param rosterEntries the roster entries callback interface
795     * @since 4.1
796     */
797    public void getEntriesAndAddListener(RosterListener rosterListener, RosterEntries rosterEntries) {
798        Objects.requireNonNull(rosterListener, "listener must not be null");
799        Objects.requireNonNull(rosterEntries, "rosterEntries must not be null");
800
801        synchronized (rosterListenersAndEntriesLock) {
802            rosterEntries.rosterEntries(entries.values());
803            addRosterListener(rosterListener);
804        }
805    }
806
807    /**
808     * Returns a set of all entries in the roster, including entries
809     * that don't belong to any groups.
810     *
811     * @return all entries in the roster.
812     */
813    public Set<RosterEntry> getEntries() {
814        Set<RosterEntry> allEntries;
815        synchronized (rosterListenersAndEntriesLock) {
816            allEntries = new HashSet<>(entries.size());
817            for (RosterEntry entry : entries.values()) {
818                allEntries.add(entry);
819            }
820        }
821        return allEntries;
822    }
823
824    /**
825     * Returns a count of the unfiled entries in the roster. An unfiled entry is
826     * an entry that doesn't belong to any groups.
827     *
828     * @return the number of unfiled entries in the roster.
829     */
830    public int getUnfiledEntryCount() {
831        return unfiledEntries.size();
832    }
833
834    /**
835     * Returns an unmodifiable set for the unfiled roster entries. An unfiled entry is
836     * an entry that doesn't belong to any groups.
837     *
838     * @return the unfiled roster entries.
839     */
840    public Set<RosterEntry> getUnfiledEntries() {
841        return Collections.unmodifiableSet(unfiledEntries);
842    }
843
844    /**
845     * Returns the roster entry associated with the given XMPP address or
846     * <tt>null</tt> if the user is not an entry in the roster.
847     *
848     * @param jid the XMPP address of the user (eg "jsmith@example.com"). The address could be
849     *             in any valid format (e.g. "domain/resource", "user@domain" or "user@domain/resource").
850     * @return the roster entry or <tt>null</tt> if it does not exist.
851     */
852    public RosterEntry getEntry(BareJid jid) {
853        if (jid == null) {
854            return null;
855        }
856        return entries.get(jid);
857    }
858
859    /**
860     * Returns true if the specified XMPP address is an entry in the roster.
861     *
862     * @param jid the XMPP address of the user (eg "jsmith@example.com"). The
863     *             address must be a bare JID e.g. "domain/resource" or
864     *             "user@domain".
865     * @return true if the XMPP address is an entry in the roster.
866     */
867    public boolean contains(BareJid jid) {
868        return getEntry(jid) != null;
869    }
870
871    /**
872     * Returns the roster group with the specified name, or <tt>null</tt> if the
873     * group doesn't exist.
874     *
875     * @param name the name of the group.
876     * @return the roster group with the specified name.
877     */
878    public RosterGroup getGroup(String name) {
879        return groups.get(name);
880    }
881
882    /**
883     * Returns the number of the groups in the roster.
884     *
885     * @return the number of groups in the roster.
886     */
887    public int getGroupCount() {
888        return groups.size();
889    }
890
891    /**
892     * Returns an unmodifiable collections of all the roster groups.
893     *
894     * @return an iterator for all roster groups.
895     */
896    public Collection<RosterGroup> getGroups() {
897        return Collections.unmodifiableCollection(groups.values());
898    }
899
900    /**
901     * Returns the presence info for a particular user. If the user is offline, or
902     * if no presence data is available (such as when you are not subscribed to the
903     * user's presence updates), unavailable presence will be returned.
904     * <p>
905     * If the user has several presences (one for each resource), then the presence with
906     * highest priority will be returned. If multiple presences have the same priority,
907     * the one with the "most available" presence mode will be returned. In order,
908     * that's {@link org.jivesoftware.smack.packet.Presence.Mode#chat free to chat},
909     * {@link org.jivesoftware.smack.packet.Presence.Mode#available available},
910     * {@link org.jivesoftware.smack.packet.Presence.Mode#away away},
911     * {@link org.jivesoftware.smack.packet.Presence.Mode#xa extended away}, and
912     * {@link org.jivesoftware.smack.packet.Presence.Mode#dnd do not disturb}.<p>
913     * </p>
914     * <p>
915     * Note that presence information is received asynchronously. So, just after logging
916     * in to the server, presence values for users in the roster may be unavailable
917     * even if they are actually online. In other words, the value returned by this
918     * method should only be treated as a snapshot in time, and may not accurately reflect
919     * other user's presence instant by instant. If you need to track presence over time,
920     * such as when showing a visual representation of the roster, consider using a
921     * {@link RosterListener}.
922     * </p>
923     *
924     * @param jid the XMPP address of the user (eg "jsmith@example.com"). The
925     *             address must be a bare JID e.g. "domain/resource" or
926     *             "user@domain".
927     * @return the user's current presence, or unavailable presence if the user is offline
928     *         or if no presence information is available..
929     */
930    public Presence getPresence(BareJid jid) {
931        Map<Resourcepart, Presence> userPresences = getPresencesInternal(jid);
932        if (userPresences == null) {
933            Presence presence = new Presence(Presence.Type.unavailable);
934            presence.setFrom(jid);
935            return presence;
936        }
937        else {
938            // Find the resource with the highest priority
939            // Might be changed to use the resource with the highest availability instead.
940            Presence presence = null;
941            // This is used in case no available presence is found
942            Presence unavailable = null;
943
944            for (Resourcepart resource : userPresences.keySet()) {
945                Presence p = userPresences.get(resource);
946                if (!p.isAvailable()) {
947                    unavailable = p;
948                    continue;
949                }
950                // Chose presence with highest priority first.
951                if (presence == null || p.getPriority() > presence.getPriority()) {
952                    presence = p;
953                }
954                // If equal priority, choose "most available" by the mode value.
955                else if (p.getPriority() == presence.getPriority()) {
956                    Presence.Mode pMode = p.getMode();
957                    // Default to presence mode of available.
958                    if (pMode == null) {
959                        pMode = Presence.Mode.available;
960                    }
961                    Presence.Mode presenceMode = presence.getMode();
962                    // Default to presence mode of available.
963                    if (presenceMode == null) {
964                        presenceMode = Presence.Mode.available;
965                    }
966                    if (pMode.compareTo(presenceMode) < 0) {
967                        presence = p;
968                    }
969                }
970            }
971            if (presence == null) {
972                if (unavailable != null) {
973                    return unavailable.clone();
974                }
975                else {
976                    presence = new Presence(Presence.Type.unavailable);
977                    presence.setFrom(jid);
978                    return presence;
979                }
980            }
981            else {
982                return presence.clone();
983            }
984        }
985    }
986
987    /**
988     * Returns the presence info for a particular user's resource, or unavailable presence
989     * if the user is offline or if no presence information is available, such as
990     * when you are not subscribed to the user's presence updates.
991     *
992     * @param userWithResource a fully qualified XMPP ID including a resource (user@domain/resource).
993     * @return the user's current presence, or unavailable presence if the user is offline
994     *         or if no presence information is available.
995     */
996    public Presence getPresenceResource(FullJid userWithResource) {
997        BareJid key = userWithResource.asBareJid();
998        Resourcepart resource = userWithResource.getResourcepart();
999        Map<Resourcepart, Presence> userPresences = getPresencesInternal(key);
1000        if (userPresences == null) {
1001            Presence presence = new Presence(Presence.Type.unavailable);
1002            presence.setFrom(userWithResource);
1003            return presence;
1004        }
1005        else {
1006            Presence presence = userPresences.get(resource);
1007            if (presence == null) {
1008                presence = new Presence(Presence.Type.unavailable);
1009                presence.setFrom(userWithResource);
1010                return presence;
1011            }
1012            else {
1013                return presence.clone();
1014            }
1015        }
1016    }
1017
1018    /**
1019     * Returns a List of Presence objects for all of a user's current presences if no presence information is available,
1020     * such as when you are not subscribed to the user's presence updates.
1021     *
1022     * @param bareJid an XMPP ID, e.g. jdoe@example.com.
1023     * @return a List of Presence objects for all the user's current presences, or an unavailable presence if no
1024     *         presence information is available.
1025     */
1026    public List<Presence> getAllPresences(BareJid bareJid) {
1027        Map<Resourcepart, Presence> userPresences = getPresencesInternal(bareJid);
1028        List<Presence> res;
1029        if (userPresences == null) {
1030            // Create an unavailable presence if none was found
1031            Presence unavailable = new Presence(Presence.Type.unavailable);
1032            unavailable.setFrom(bareJid);
1033            res = new ArrayList<>(Arrays.asList(unavailable));
1034        } else {
1035            res = new ArrayList<>(userPresences.values().size());
1036            for (Presence presence : userPresences.values()) {
1037                res.add(presence.clone());
1038            }
1039        }
1040        return res;
1041    }
1042
1043    /**
1044     * Returns a List of all <b>available</b> Presence Objects for the given bare JID. If there are no available
1045     * presences, then the empty list will be returned.
1046     *
1047     * @param bareJid the bare JID from which the presences should be retrieved.
1048     * @return available presences for the bare JID.
1049     */
1050    public List<Presence> getAvailablePresences(BareJid bareJid) {
1051        List<Presence> allPresences = getAllPresences(bareJid);
1052        List<Presence> res = new ArrayList<>(allPresences.size());
1053        for (Presence presence : allPresences) {
1054            if (presence.isAvailable()) {
1055                // No need to clone presence here, getAllPresences already returns clones
1056                res.add(presence);
1057            }
1058        }
1059        return res;
1060    }
1061
1062    /**
1063     * Returns a List of Presence objects for all of a user's current presences
1064     * or an unavailable presence if the user is unavailable (offline) or if no presence
1065     * information is available, such as when you are not subscribed to the user's presence
1066     * updates.
1067     *
1068     * @param jid an XMPP ID, e.g. jdoe@example.com.
1069     * @return a List of Presence objects for all the user's current presences,
1070     *         or an unavailable presence if the user is offline or if no presence information
1071     *         is available.
1072     */
1073    public List<Presence> getPresences(BareJid jid) {
1074        List<Presence> res;
1075        Map<Resourcepart, Presence> userPresences = getPresencesInternal(jid);
1076        if (userPresences == null) {
1077            Presence presence = new Presence(Presence.Type.unavailable);
1078            presence.setFrom(jid);
1079            res = Arrays.asList(presence);
1080        }
1081        else {
1082            List<Presence> answer = new ArrayList<>();
1083            // Used in case no available presence is found
1084            Presence unavailable = null;
1085            for (Presence presence : userPresences.values()) {
1086                if (presence.isAvailable()) {
1087                    answer.add(presence.clone());
1088                }
1089                else {
1090                    unavailable = presence;
1091                }
1092            }
1093            if (!answer.isEmpty()) {
1094                res = answer;
1095            }
1096            else if (unavailable != null) {
1097                res = Arrays.asList(unavailable.clone());
1098            }
1099            else {
1100                Presence presence = new Presence(Presence.Type.unavailable);
1101                presence.setFrom(jid);
1102                res = Arrays.asList(presence);
1103            }
1104        }
1105        return res;
1106    }
1107
1108    /**
1109     * Check if the given JID is subscribed to the user's presence.
1110     * <p>
1111     * If the JID is subscribed to the user's presence then it is allowed to see the presence and
1112     * will get notified about presence changes. Also returns true, if the JID is the service
1113     * name of the XMPP connection (the "XMPP domain"), i.e. the XMPP service is treated like
1114     * having an implicit subscription to the users presence.
1115     * </p>
1116     * Note that if the roster is not loaded, then this method will always return false.
1117     * 
1118     * @param jid
1119     * @return true if the given JID is allowed to see the users presence.
1120     * @since 4.1
1121     */
1122    public boolean isSubscribedToMyPresence(Jid jid) {
1123        if (jid == null) {
1124            return false;
1125        }
1126        BareJid bareJid = jid.asBareJid();
1127        if (connection().getXMPPServiceDomain().equals(bareJid)) {
1128            return true;
1129        }
1130        RosterEntry entry = getEntry(bareJid);
1131        if (entry == null) {
1132            return false;
1133        }
1134        return entry.canSeeMyPresence();
1135    }
1136
1137    /**
1138     * Check if the XMPP entity this roster belongs to is subscribed to the presence of the given JID.
1139     *
1140     * @param jid the jid to check.
1141     * @return <code>true</code> if we are subscribed to the presence of the given jid.
1142     * @since 4.2
1143     */
1144    public boolean iAmSubscribedTo(Jid jid) {
1145        if (jid == null) {
1146            return false;
1147        }
1148        BareJid bareJid = jid.asBareJid();
1149        RosterEntry entry = getEntry(bareJid);
1150        if (entry == null) {
1151            return false;
1152        }
1153        return entry.canSeeHisPresence();
1154    }
1155
1156    /**
1157     * Sets if the roster will be loaded from the server when logging in for newly created instances
1158     * of {@link Roster}.
1159     *
1160     * @param rosterLoadedAtLoginDefault if the roster will be loaded from the server when logging in.
1161     * @see #setRosterLoadedAtLogin(boolean)
1162     * @since 4.1.7
1163     */
1164    public static void setRosterLoadedAtLoginDefault(boolean rosterLoadedAtLoginDefault) {
1165        Roster.rosterLoadedAtLoginDefault = rosterLoadedAtLoginDefault;
1166    }
1167
1168    /**
1169     * Sets if the roster will be loaded from the server when logging in. This
1170     * is the common behaviour for clients but sometimes clients may want to differ this
1171     * or just never do it if not interested in rosters.
1172     *
1173     * @param rosterLoadedAtLogin if the roster will be loaded from the server when logging in.
1174     */
1175    public void setRosterLoadedAtLogin(boolean rosterLoadedAtLogin) {
1176        this.rosterLoadedAtLogin = rosterLoadedAtLogin;
1177    }
1178
1179    /**
1180     * Returns true if the roster will be loaded from the server when logging in. This
1181     * is the common behavior for clients but sometimes clients may want to differ this
1182     * or just never do it if not interested in rosters.
1183     *
1184     * @return true if the roster will be loaded from the server when logging in.
1185     * @see <a href="http://xmpp.org/rfcs/rfc6121.html#roster-login">RFC 6121 2.2 - Retrieving the Roster on Login</a>
1186     */
1187    public boolean isRosterLoadedAtLogin() {
1188        return rosterLoadedAtLogin;
1189    }
1190
1191    RosterStore getRosterStore() {
1192        return rosterStore;
1193    }
1194
1195    /**
1196     * Changes the presence of available contacts offline by simulating an unavailable
1197     * presence sent from the server.
1198     */
1199    private void setOfflinePresences() {
1200        Presence packetUnavailable;
1201        outerloop: for (Jid user : presenceMap.keySet()) {
1202            Map<Resourcepart, Presence> resources = presenceMap.get(user);
1203            if (resources != null) {
1204                for (Resourcepart resource : resources.keySet()) {
1205                    packetUnavailable = new Presence(Presence.Type.unavailable);
1206                    EntityBareJid bareUserJid = user.asEntityBareJidIfPossible();
1207                    if (bareUserJid == null) {
1208                        LOGGER.warning("Can not transform user JID to bare JID: '" + user + "'");
1209                        continue;
1210                    }
1211                    packetUnavailable.setFrom(JidCreate.fullFrom(bareUserJid, resource));
1212                    try {
1213                        presencePacketListener.processStanza(packetUnavailable);
1214                    }
1215                    catch (NotConnectedException e) {
1216                        throw new IllegalStateException(
1217                                        "presencePacketListener should never throw a NotConnectedException when processStanza is called with a presence of type unavailable",
1218                                        e);
1219                    }
1220                    catch (InterruptedException e) {
1221                        break outerloop;
1222                    }
1223                }
1224            }
1225        }
1226    }
1227
1228    /**
1229     * Changes the presence of available contacts offline by simulating an unavailable
1230     * presence sent from the server. After a disconnection, every Presence is set
1231     * to offline.
1232     */
1233    private void setOfflinePresencesAndResetLoaded() {
1234        setOfflinePresences();
1235        rosterState = RosterState.uninitialized;
1236    }
1237
1238    /**
1239     * Fires roster changed event to roster listeners indicating that the
1240     * specified collections of contacts have been added, updated or deleted
1241     * from the roster.
1242     *
1243     * @param addedEntries   the collection of address of the added contacts.
1244     * @param updatedEntries the collection of address of the updated contacts.
1245     * @param deletedEntries the collection of address of the deleted contacts.
1246     */
1247    private void fireRosterChangedEvent(final Collection<Jid> addedEntries, final Collection<Jid> updatedEntries,
1248                    final Collection<Jid> deletedEntries) {
1249        synchronized (rosterListenersAndEntriesLock) {
1250            for (RosterListener listener : rosterListeners) {
1251                if (!addedEntries.isEmpty()) {
1252                    listener.entriesAdded(addedEntries);
1253                }
1254                if (!updatedEntries.isEmpty()) {
1255                    listener.entriesUpdated(updatedEntries);
1256                }
1257                if (!deletedEntries.isEmpty()) {
1258                    listener.entriesDeleted(deletedEntries);
1259                }
1260            }
1261        }
1262    }
1263
1264    /**
1265     * Fires roster presence changed event to roster listeners.
1266     *
1267     * @param presence the presence change.
1268     */
1269    private void fireRosterPresenceEvent(final Presence presence) {
1270        synchronized (rosterListenersAndEntriesLock) {
1271            for (RosterListener listener : rosterListeners) {
1272                listener.presenceChanged(presence);
1273            }
1274        }
1275    }
1276
1277    private void addUpdateEntry(Collection<Jid> addedEntries, Collection<Jid> updatedEntries,
1278                    Collection<Jid> unchangedEntries, RosterPacket.Item item, RosterEntry entry) {
1279        RosterEntry oldEntry;
1280        synchronized (rosterListenersAndEntriesLock) {
1281            oldEntry = entries.put(item.getJid(), entry);
1282        }
1283        if (oldEntry == null) {
1284            BareJid jid = item.getJid();
1285            addedEntries.add(jid);
1286            // Move the eventually existing presences from nonRosterPresenceMap to presenceMap.
1287            move(jid, nonRosterPresenceMap, presenceMap);
1288        }
1289        else {
1290            RosterPacket.Item oldItem = RosterEntry.toRosterItem(oldEntry);
1291            if (!oldEntry.equalsDeep(entry) || !item.getGroupNames().equals(oldItem.getGroupNames())) {
1292                updatedEntries.add(item.getJid());
1293                oldEntry.updateItem(item);
1294            } else {
1295                // Record the entry as unchanged, so that it doesn't end up as deleted entry
1296                unchangedEntries.add(item.getJid());
1297            }
1298        }
1299
1300        // Mark the entry as unfiled if it does not belong to any groups.
1301        if (item.getGroupNames().isEmpty()) {
1302            unfiledEntries.add(entry);
1303        }
1304        else {
1305            unfiledEntries.remove(entry);
1306        }
1307
1308        // Add the entry/user to the groups
1309        List<String> newGroupNames = new ArrayList<>();
1310        for (String groupName : item.getGroupNames()) {
1311            // Add the group name to the list.
1312            newGroupNames.add(groupName);
1313
1314            // Add the entry to the group.
1315            RosterGroup group = getGroup(groupName);
1316            if (group == null) {
1317                group = createGroup(groupName);
1318                groups.put(groupName, group);
1319            }
1320            // Add the entry.
1321            group.addEntryLocal(entry);
1322        }
1323
1324        // Remove user from the remaining groups.
1325        List<String> oldGroupNames = new ArrayList<>();
1326        for (RosterGroup group : getGroups()) {
1327            oldGroupNames.add(group.getName());
1328        }
1329        oldGroupNames.removeAll(newGroupNames);
1330
1331        for (String groupName : oldGroupNames) {
1332            RosterGroup group = getGroup(groupName);
1333            group.removeEntryLocal(entry);
1334            if (group.getEntryCount() == 0) {
1335                groups.remove(groupName);
1336            }
1337        }
1338    }
1339
1340    private void deleteEntry(Collection<Jid> deletedEntries, RosterEntry entry) {
1341        BareJid user = entry.getJid();
1342        entries.remove(user);
1343        unfiledEntries.remove(entry);
1344        // Move the presences from the presenceMap to the nonRosterPresenceMap.
1345        move(user, presenceMap, nonRosterPresenceMap);
1346        deletedEntries.add(user);
1347
1348        for (Entry<String,RosterGroup> e : groups.entrySet()) {
1349            RosterGroup group = e.getValue();
1350            group.removeEntryLocal(entry);
1351            if (group.getEntryCount() == 0) {
1352                groups.remove(e.getKey());
1353            }
1354        }
1355    }
1356
1357    /**
1358     * Removes all the groups with no entries.
1359     *
1360     * This is used by {@link RosterPushListener} and {@link RosterResultListener} to
1361     * cleanup groups after removing contacts.
1362     */
1363    private void removeEmptyGroups() {
1364        // We have to do this because RosterGroup.removeEntry removes the entry immediately
1365        // (locally) and the group could remain empty.
1366        // TODO Check the performance/logic for rosters with large number of groups
1367        for (RosterGroup group : getGroups()) {
1368            if (group.getEntryCount() == 0) {
1369                groups.remove(group.getName());
1370            }
1371        }
1372    }
1373
1374    /**
1375     * Move presences from 'entity' from one presence map to another.
1376     *
1377     * @param entity the entity
1378     * @param from the map to move presences from
1379     * @param to the map to move presences to
1380     */
1381    private static void move(BareJid entity, Map<BareJid, Map<Resourcepart, Presence>> from, Map<BareJid, Map<Resourcepart, Presence>> to) {
1382        Map<Resourcepart, Presence> presences = from.remove(entity);
1383        if (presences != null && !presences.isEmpty()) {
1384            to.put(entity, presences);
1385        }
1386    }
1387
1388    /**
1389     * Ignore ItemTypes as of RFC 6121, 2.1.2.5.
1390     *
1391     * This is used by {@link RosterPushListener} and {@link RosterResultListener}.
1392     * */
1393    private static boolean hasValidSubscriptionType(RosterPacket.Item item) {
1394        switch (item.getItemType()) {
1395            case none:
1396            case from:
1397            case to:
1398            case both:
1399                return true;
1400            default:
1401                return false;
1402        }
1403    }
1404
1405    /**
1406     * Check if the server supports roster versioning.
1407     *
1408     * @return true if the server supports roster versioning, false otherwise.
1409     */
1410    public boolean isRosterVersioningSupported() {
1411        return connection().hasFeature(RosterVer.ELEMENT, RosterVer.NAMESPACE);
1412    }
1413
1414    /**
1415     * An enumeration for the subscription mode options.
1416     */
1417    public enum SubscriptionMode {
1418
1419        /**
1420         * Automatically accept all subscription and unsubscription requests.
1421         * This is suitable for simple clients. More complex clients will
1422         * likely wish to handle subscription requests manually.
1423         */
1424        accept_all,
1425
1426        /**
1427         * Automatically reject all subscription requests. This is the default mode.
1428         */
1429        reject_all,
1430
1431        /**
1432         * Subscription requests are ignored, which means they must be manually
1433         * processed by registering a listener for presence packets and then looking
1434         * for any presence requests that have the type Presence.Type.SUBSCRIBE or
1435         * Presence.Type.UNSUBSCRIBE.
1436         */
1437        manual
1438    }
1439
1440    /**
1441     * Listens for all presence packets and processes them.
1442     */
1443    private class PresencePacketListener implements StanzaListener {
1444
1445        @Override
1446        public void processStanza(Stanza packet) throws NotConnectedException, InterruptedException {
1447            // Try to ensure that the roster is loaded when processing presence stanzas. While the
1448            // presence listener is synchronous, the roster result listener is not, which means that
1449            // the presence listener may be invoked with a not yet loaded roster.
1450            if (rosterState == RosterState.loading) {
1451                try {
1452                    waitUntilLoaded();
1453                }
1454                catch (InterruptedException e) {
1455                    LOGGER.log(Level.INFO, "Presence listener was interrupted", e);
1456
1457                }
1458            }
1459            if (!isLoaded() && rosterLoadedAtLogin) {
1460                LOGGER.warning("Roster not loaded while processing " + packet);
1461            }
1462            Presence presence = (Presence) packet;
1463            Jid from = presence.getFrom();
1464            Resourcepart fromResource = Resourcepart.EMPTY;
1465            BareJid bareFrom = null;
1466            FullJid fullFrom = null;
1467            if (from != null) {
1468                fromResource = from.getResourceOrNull();
1469                if (fromResource == null) {
1470                    fromResource = Resourcepart.EMPTY;
1471                    bareFrom = from.asBareJid();
1472                }
1473                else {
1474                    fullFrom = from.asFullJidIfPossible();
1475                    // We know that this must be a full JID in this case.
1476                    assert (fullFrom != null);
1477                }
1478            }
1479
1480            BareJid key = from != null ? from.asBareJid() : null;
1481            Map<Resourcepart, Presence> userPresences;
1482
1483            // If an "available" presence, add it to the presence map. Each presence
1484            // map will hold for a particular user a map with the presence
1485            // packets saved for each resource.
1486            switch (presence.getType()) {
1487            case available:
1488                // Get the user presence map
1489                userPresences = getOrCreatePresencesInternal(key);
1490                // See if an offline presence was being stored in the map. If so, remove
1491                // it since we now have an online presence.
1492                userPresences.remove(Resourcepart.EMPTY);
1493                // Add the new presence, using the resources as a key.
1494                userPresences.put(fromResource, presence);
1495                // If the user is in the roster, fire an event.
1496                if (contains(key)) {
1497                    fireRosterPresenceEvent(presence);
1498                }
1499                for (PresenceEventListener presenceEventListener : presenceEventListeners) {
1500                    presenceEventListener.presenceAvailable(fullFrom, presence);
1501                }
1502                break;
1503            // If an "unavailable" packet.
1504            case unavailable:
1505                // If no resource, this is likely an offline presence as part of
1506                // a roster presence flood. In that case, we store it.
1507                userPresences = getOrCreatePresencesInternal(key);
1508                if (from.hasNoResource()) {
1509                    // Get the user presence map
1510                    userPresences.put(Resourcepart.EMPTY, presence);
1511                }
1512                // Otherwise, this is a normal offline presence.
1513                else {
1514                    // Store the offline presence, as it may include extra information
1515                    // such as the user being on vacation.
1516                    userPresences.put(fromResource, presence);
1517                }
1518                // If the user is in the roster, fire an event.
1519                if (contains(key)) {
1520                    fireRosterPresenceEvent(presence);
1521                }
1522
1523                // Ensure that 'from' is a full JID before invoking the presence unavailable
1524                // listeners. Usually unavailable presences always have a resourcepart, i.e. are
1525                // full JIDs, but RFC 6121 § 4.5.4 has an implementation note that unavailable
1526                // presences from a bare JID SHOULD be treated as applying to all resources. I don't
1527                // think any client or server ever implemented that, I do think that this
1528                // implementation note is a terrible idea since it adds another corner case in
1529                // client code, instead of just having the invariant
1530                // "unavailable presences are always from the full JID".
1531                if (fullFrom != null) {
1532                    for (PresenceEventListener presenceEventListener : presenceEventListeners) {
1533                        presenceEventListener.presenceUnavailable(fullFrom, presence);
1534                    }
1535                } else {
1536                    LOGGER.fine("Unavailable presence from bare JID: " + presence);
1537                }
1538
1539                break;
1540            // Error presence packets from a bare JID mean we invalidate all existing
1541            // presence info for the user.
1542            case error:
1543                // No need to act on error presences send without from, i.e.
1544                // directly send from the users XMPP service, or where the from
1545                // address is not a bare JID
1546                if (from == null || !from.isEntityBareJid()) {
1547                    break;
1548                }
1549                userPresences = getOrCreatePresencesInternal(key);
1550                // Any other presence data is invalidated by the error packet.
1551                userPresences.clear();
1552
1553                // Set the new presence using the empty resource as a key.
1554                userPresences.put(Resourcepart.EMPTY, presence);
1555                // If the user is in the roster, fire an event.
1556                if (contains(key)) {
1557                    fireRosterPresenceEvent(presence);
1558                }
1559                for (PresenceEventListener presenceEventListener : presenceEventListeners) {
1560                    presenceEventListener.presenceError(from, presence);
1561                }
1562                break;
1563            case subscribed:
1564                for (PresenceEventListener presenceEventListener : presenceEventListeners) {
1565                    presenceEventListener.presenceSubscribed(bareFrom, presence);
1566                }
1567                break;
1568            case unsubscribed:
1569                for (PresenceEventListener presenceEventListener : presenceEventListeners) {
1570                    presenceEventListener.presenceUnsubscribed(bareFrom, presence);
1571                }
1572                break;
1573            default:
1574                break;
1575            }
1576        }
1577    }
1578
1579    /**
1580     * Handles Roster results as described in <a href="https://tools.ietf.org/html/rfc6121#section-2.1.4">RFC 6121 2.1.4</a>.
1581     */
1582    private class RosterResultListener implements StanzaListener {
1583
1584        @Override
1585        public void processStanza(Stanza packet) {
1586            final XMPPConnection connection = connection();
1587            LOGGER.log(Level.FINE, "RosterResultListener received {}", packet);
1588            Collection<Jid> addedEntries = new ArrayList<>();
1589            Collection<Jid> updatedEntries = new ArrayList<>();
1590            Collection<Jid> deletedEntries = new ArrayList<>();
1591            Collection<Jid> unchangedEntries = new ArrayList<>();
1592
1593            if (packet instanceof RosterPacket) {
1594                // Non-empty roster result. This stanza contains all the roster elements.
1595                RosterPacket rosterPacket = (RosterPacket) packet;
1596
1597                // Ignore items without valid subscription type
1598                ArrayList<Item> validItems = new ArrayList<>();
1599                for (RosterPacket.Item item : rosterPacket.getRosterItems()) {
1600                    if (hasValidSubscriptionType(item)) {
1601                        validItems.add(item);
1602                    }
1603                }
1604
1605                for (RosterPacket.Item item : validItems) {
1606                    RosterEntry entry = new RosterEntry(item, Roster.this, connection);
1607                    addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
1608                }
1609
1610                // Delete all entries which where not added or updated
1611                Set<Jid> toDelete = new HashSet<>();
1612                for (RosterEntry entry : entries.values()) {
1613                    toDelete.add(entry.getJid());
1614                }
1615                toDelete.removeAll(addedEntries);
1616                toDelete.removeAll(updatedEntries);
1617                toDelete.removeAll(unchangedEntries);
1618                for (Jid user : toDelete) {
1619                    deleteEntry(deletedEntries, entries.get(user));
1620                }
1621
1622                if (rosterStore != null) {
1623                    String version = rosterPacket.getVersion();
1624                    rosterStore.resetEntries(validItems, version);
1625                }
1626
1627                removeEmptyGroups();
1628            }
1629            else {
1630                // Empty roster result as defined in RFC6121 2.6.3. An empty roster result basically
1631                // means that rosterver was used and the roster hasn't changed (much) since the
1632                // version we presented the server. So we simply load the roster from the store and
1633                // await possible further roster pushes.
1634                List<RosterPacket.Item> storedItems = rosterStore.getEntries();
1635                if (storedItems == null) {
1636                    // The roster store was corrupted. Reset the store and reload the roster without using a roster version.
1637                    rosterStore.resetStore();
1638                    try {
1639                        reload();
1640                    } catch (NotLoggedInException | NotConnectedException
1641                            | InterruptedException e) {
1642                        LOGGER.log(Level.FINE,
1643                                "Exception while trying to load the roster after the roster store was corrupted",
1644                                e);
1645                    }
1646                    return;
1647                }
1648                for (RosterPacket.Item item : storedItems) {
1649                    RosterEntry entry = new RosterEntry(item, Roster.this, connection);
1650                    addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
1651                }
1652            }
1653
1654            rosterState = RosterState.loaded;
1655            synchronized (Roster.this) {
1656                Roster.this.notifyAll();
1657            }
1658            // Fire event for roster listeners.
1659            fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);
1660
1661            // Call the roster loaded listeners after the roster events have been fired. This is
1662            // important because the user may call getEntriesAndAddListener() in onRosterLoaded(),
1663            // and if the order would be the other way around, the roster listener added by
1664            // getEntriesAndAddListener() would be invoked with information that was already
1665            // available at the time getEntriesAndAddListener() was called.
1666            try {
1667                synchronized (rosterLoadedListeners) {
1668                    for (RosterLoadedListener rosterLoadedListener : rosterLoadedListeners) {
1669                        rosterLoadedListener.onRosterLoaded(Roster.this);
1670                    }
1671                }
1672            }
1673            catch (Exception e) {
1674                LOGGER.log(Level.WARNING, "RosterLoadedListener threw exception", e);
1675            }
1676        }
1677    }
1678
1679    /**
1680     * Listens for all roster pushes and processes them.
1681     */
1682    private final class RosterPushListener extends AbstractIqRequestHandler {
1683
1684        private RosterPushListener() {
1685            super(RosterPacket.ELEMENT, RosterPacket.NAMESPACE, Type.set, Mode.sync);
1686        }
1687
1688        @Override
1689        public IQ handleIQRequest(IQ iqRequest) {
1690            final XMPPConnection connection = connection();
1691            RosterPacket rosterPacket = (RosterPacket) iqRequest;
1692
1693            EntityFullJid ourFullJid = connection.getUser();
1694            if (ourFullJid == null) {
1695                LOGGER.warning("Ignoring roster push " + iqRequest + " while " + connection
1696                                + " has no bound resource. This may be a server bug.");
1697                return null;
1698            }
1699
1700            // Roster push (RFC 6121, 2.1.6)
1701            // A roster push with a non-empty from not matching our address MUST be ignored
1702            EntityBareJid ourBareJid = ourFullJid.asEntityBareJid();
1703            Jid from = rosterPacket.getFrom();
1704            if (from != null) {
1705                if (from.equals(ourFullJid)) {
1706                    // Since RFC 6121 roster pushes are no longer allowed to
1707                    // origin from the full JID as it was the case with RFC
1708                    // 3921. Log a warning an continue processing the push.
1709                    // See also SMACK-773.
1710                    LOGGER.warning(
1711                            "Received roster push from full JID. This behavior is since RFC 6121 not longer standard compliant. "
1712                                    + "Please ask your server vendor to fix this and comply to RFC 6121 § 2.1.6. IQ roster push stanza: "
1713                                    + iqRequest);
1714                } else if (!from.equals(ourBareJid)) {
1715                    LOGGER.warning("Ignoring roster push with a non matching 'from' ourJid='" + ourBareJid + "' from='"
1716                            + from + "'");
1717                    return IQ.createErrorResponse(iqRequest, Condition.service_unavailable);
1718                }
1719            }
1720
1721            // A roster push must contain exactly one entry
1722            Collection<Item> items = rosterPacket.getRosterItems();
1723            if (items.size() != 1) {
1724                LOGGER.warning("Ignoring roster push with not exactly one entry. size=" + items.size());
1725                return IQ.createErrorResponse(iqRequest, Condition.bad_request);
1726            }
1727
1728            Collection<Jid> addedEntries = new ArrayList<>();
1729            Collection<Jid> updatedEntries = new ArrayList<>();
1730            Collection<Jid> deletedEntries = new ArrayList<>();
1731            Collection<Jid> unchangedEntries = new ArrayList<>();
1732
1733            // We assured above that the size of items is exactly 1, therefore we are able to
1734            // safely retrieve this single item here.
1735            Item item = items.iterator().next();
1736            RosterEntry entry = new RosterEntry(item, Roster.this, connection);
1737            String version = rosterPacket.getVersion();
1738
1739            if (item.getItemType().equals(RosterPacket.ItemType.remove)) {
1740                deleteEntry(deletedEntries, entry);
1741                if (rosterStore != null) {
1742                    rosterStore.removeEntry(entry.getJid(), version);
1743                }
1744            }
1745            else if (hasValidSubscriptionType(item)) {
1746                addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
1747                if (rosterStore != null) {
1748                    rosterStore.addEntry(item, version);
1749                }
1750            }
1751
1752            removeEmptyGroups();
1753
1754            // Fire event for roster listeners.
1755            fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);
1756
1757            return IQ.createResultIQ(rosterPacket);
1758        }
1759    }
1760
1761    /**
1762     * Set the default maximum size of the non-Roster presence map.
1763     * <p>
1764     * The roster will only store this many presence entries for entities non in the Roster. The
1765     * default is {@value #INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE}.
1766     * </p>
1767     *
1768     * @param maximumSize the maximum size
1769     * @since 4.2
1770     */
1771    public static void setDefaultNonRosterPresenceMapMaxSize(int maximumSize) {
1772        defaultNonRosterPresenceMapMaxSize = maximumSize;
1773    }
1774
1775    /**
1776     * Set the maximum size of the non-Roster presence map.
1777     *
1778     * @param maximumSize
1779     * @since 4.2
1780     * @see #setDefaultNonRosterPresenceMapMaxSize(int)
1781     */
1782    public void setNonRosterPresenceMapMaxSize(int maximumSize) {
1783        nonRosterPresenceMap.setMaxCacheSize(maximumSize);
1784    }
1785
1786}