2003-01-16 16:42:18 +01:00
|
|
|
/**
|
|
|
|
*
|
2021-01-28 22:05:47 +01:00
|
|
|
* Copyright 2003-2007 Jive Software, 2016-2021 Florian Schmaus.
|
2003-01-16 16:42:18 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:53:30 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-16 16:42:18 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-16 16:42:18 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-01-16 16:42:18 +01:00
|
|
|
*/
|
|
|
|
|
2015-01-22 13:53:50 +01:00
|
|
|
package org.jivesoftware.smack.roster;
|
2003-01-16 16:42:18 +01:00
|
|
|
|
2014-02-23 21:08:35 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
2015-01-22 23:52:49 +01:00
|
|
|
import java.util.LinkedHashSet;
|
2014-02-23 21:08:35 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2015-01-22 13:53:50 +01:00
|
|
|
import java.util.WeakHashMap;
|
2014-02-23 21:08:35 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2015-01-22 23:57:14 +01:00
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2014-03-14 01:48:33 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2014-02-23 21:08:35 +01:00
|
|
|
|
2018-04-08 21:21:50 +02:00
|
|
|
import org.jivesoftware.smack.AsyncButOrdered;
|
2015-01-22 13:53:50 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
2020-05-13 22:01:48 +02:00
|
|
|
import org.jivesoftware.smack.ConnectionListener;
|
2015-01-22 13:53:50 +01:00
|
|
|
import org.jivesoftware.smack.Manager;
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
2015-03-14 15:19:52 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotLoggedInException;
|
2017-08-16 13:46:42 +02:00
|
|
|
import org.jivesoftware.smack.SmackFuture;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.StanzaListener;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2015-01-22 13:53:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2017-01-29 11:08:45 +01:00
|
|
|
import org.jivesoftware.smack.filter.AndFilter;
|
2015-05-21 20:12:39 +02:00
|
|
|
import org.jivesoftware.smack.filter.PresenceTypeFilter;
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.filter.StanzaFilter;
|
|
|
|
import org.jivesoftware.smack.filter.StanzaTypeFilter;
|
2017-01-29 11:08:45 +01:00
|
|
|
import org.jivesoftware.smack.filter.ToMatchesFilter;
|
2015-01-08 11:01:35 +01:00
|
|
|
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
2006-07-18 07:14:33 +02:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
2019-10-24 15:45:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.PresenceBuilder;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2019-10-24 15:45:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaBuilder;
|
2018-04-07 21:25:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaError.Condition;
|
2015-05-21 20:12:39 +02:00
|
|
|
import org.jivesoftware.smack.roster.SubscribeListener.SubscribeAnswer;
|
2015-01-22 13:53:50 +01:00
|
|
|
import org.jivesoftware.smack.roster.packet.RosterPacket;
|
|
|
|
import org.jivesoftware.smack.roster.packet.RosterPacket.Item;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.roster.packet.RosterVer;
|
2015-03-14 15:19:52 +01:00
|
|
|
import org.jivesoftware.smack.roster.packet.SubscriptionPreApproval;
|
2015-01-22 13:53:50 +01:00
|
|
|
import org.jivesoftware.smack.roster.rosterstore.RosterStore;
|
2017-08-16 13:46:42 +02:00
|
|
|
import org.jivesoftware.smack.util.ExceptionCallback;
|
2015-01-26 16:35:47 +01:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
2017-08-16 13:46:42 +02:00
|
|
|
import org.jivesoftware.smack.util.SuccessCallback;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-05-27 22:34:43 +02:00
|
|
|
import org.jxmpp.jid.BareJid;
|
2015-05-27 19:29:51 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
2017-03-07 08:50:22 +01:00
|
|
|
import org.jxmpp.jid.EntityFullJid;
|
2015-05-27 19:29:51 +02:00
|
|
|
import org.jxmpp.jid.FullJid;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jxmpp.jid.Jid;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.jxmpp.jid.parts.Resourcepart;
|
2015-12-01 18:15:33 +01:00
|
|
|
import org.jxmpp.util.cache.LruCache;
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2003-01-16 16:42:18 +01:00
|
|
|
/**
|
2003-03-10 00:42:51 +01:00
|
|
|
* Represents a user's roster, which is the collection of users a person receives
|
2015-04-03 19:15:35 +02:00
|
|
|
* presence updates for. Roster items are categorized into groups for easier management.
|
2017-12-25 12:51:41 +01:00
|
|
|
*
|
2018-03-09 14:15:46 +01:00
|
|
|
* Other users may attempt to subscribe to this user using a subscription request. Three
|
2003-06-17 19:20:30 +02:00
|
|
|
* modes are supported for handling these requests: <ul>
|
2007-02-20 18:02:39 +01:00
|
|
|
* <li>{@link SubscriptionMode#accept_all accept_all} -- accept all subscription requests.</li>
|
|
|
|
* <li>{@link SubscriptionMode#reject_all reject_all} -- reject all subscription requests.</li>
|
|
|
|
* <li>{@link SubscriptionMode#manual manual} -- manually process all subscription requests.</li>
|
2006-07-18 08:47:38 +02:00
|
|
|
* </ul>
|
2003-06-17 19:20:30 +02:00
|
|
|
*
|
2003-01-16 16:42:18 +01:00
|
|
|
* @author Matt Tucker
|
2015-01-22 13:53:50 +01:00
|
|
|
* @see #getInstanceFor(XMPPConnection)
|
2003-01-16 16:42:18 +01:00
|
|
|
*/
|
2015-04-06 10:45:12 +02:00
|
|
|
public final class Roster extends Manager {
|
2003-01-16 16:42:18 +01:00
|
|
|
|
2014-03-14 01:48:33 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(Roster.class.getName());
|
2014-04-29 14:51:01 +02:00
|
|
|
|
2015-01-22 13:53:50 +01:00
|
|
|
static {
|
|
|
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
|
|
|
@Override
|
|
|
|
public void connectionCreated(XMPPConnection connection) {
|
|
|
|
getInstanceFor(connection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Map<XMPPConnection, Roster> INSTANCES = new WeakHashMap<>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the roster for the user.
|
|
|
|
* <p>
|
|
|
|
* This method will never return <code>null</code>, instead if the user has not yet logged into
|
2015-04-06 13:56:32 +02:00
|
|
|
* the server all modifying methods of the returned roster object
|
2015-05-27 22:34:43 +02:00
|
|
|
* like {@link Roster#createEntry(BareJid, String, String[])},
|
2015-01-22 13:53:50 +01:00
|
|
|
* {@link Roster#removeEntry(RosterEntry)} , etc. except adding or removing
|
|
|
|
* {@link RosterListener}s will throw an IllegalStateException.
|
2015-04-06 13:56:32 +02:00
|
|
|
* </p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2016-04-15 10:23:34 +02:00
|
|
|
* @param connection the connection the roster should be retrieved for.
|
2015-01-22 13:53:50 +01:00
|
|
|
* @return the user's roster.
|
|
|
|
*/
|
|
|
|
public static synchronized Roster getInstanceFor(XMPPConnection connection) {
|
|
|
|
Roster roster = INSTANCES.get(connection);
|
|
|
|
if (roster == null) {
|
|
|
|
roster = new Roster(connection);
|
|
|
|
INSTANCES.put(connection, roster);
|
|
|
|
}
|
|
|
|
return roster;
|
|
|
|
}
|
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
private static final StanzaFilter PRESENCE_PACKET_FILTER = StanzaTypeFilter.PRESENCE;
|
2014-04-29 14:51:01 +02:00
|
|
|
|
2017-01-29 11:08:45 +01:00
|
|
|
private static final StanzaFilter OUTGOING_USER_UNAVAILABLE_PRESENCE = new AndFilter(PresenceTypeFilter.UNAVAILABLE, ToMatchesFilter.MATCH_NO_TO_SET);
|
|
|
|
|
2015-01-22 13:53:50 +01:00
|
|
|
private static boolean rosterLoadedAtLoginDefault = true;
|
|
|
|
|
2004-01-18 15:17:13 +01:00
|
|
|
/**
|
2006-07-18 07:14:33 +02:00
|
|
|
* The default subscription processing mode to use when a Roster is created. By default
|
2016-12-28 23:42:30 +01:00
|
|
|
* all subscription requests are automatically rejected.
|
2004-01-18 15:17:13 +01:00
|
|
|
*/
|
2016-12-28 23:42:30 +01:00
|
|
|
private static SubscriptionMode defaultSubscriptionMode = SubscriptionMode.reject_all;
|
2004-01-18 15:17:13 +01:00
|
|
|
|
2015-12-19 13:30:15 +01:00
|
|
|
/**
|
|
|
|
* The initial maximum size of the map holding presence information of entities without an Roster entry. Currently
|
|
|
|
* {@value #INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE}.
|
|
|
|
*/
|
|
|
|
public static final int INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE = 1024;
|
2015-12-01 18:15:33 +01:00
|
|
|
|
|
|
|
private static int defaultNonRosterPresenceMapMaxSize = INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE;
|
|
|
|
|
2015-01-22 13:53:50 +01:00
|
|
|
private RosterStore rosterStore;
|
2021-01-28 22:05:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The groups of this roster.
|
|
|
|
* <p>
|
|
|
|
* Note that we use {@link ConcurrentHashMap} also as static type of this field, since we use the fact that the same
|
|
|
|
* thread can modify this collection, e.g. remove items, while iterating over it. This is done, for example in
|
|
|
|
* {@link #deleteEntry(Collection, RosterEntry)}. If we do not denote the static type to ConcurrentHashMap, but
|
|
|
|
* {@link Map} instead, then error prone would report a ModifyCollectionInEnhancedForLoop but.
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
private final ConcurrentHashMap<String, RosterGroup> groups = new ConcurrentHashMap<>();
|
2015-01-26 16:35:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Concurrent hash map from JID to its roster entry.
|
|
|
|
*/
|
2015-05-27 22:34:43 +02:00
|
|
|
private final Map<BareJid, RosterEntry> entries = new ConcurrentHashMap<>();
|
2015-01-26 16:35:47 +01:00
|
|
|
|
2015-01-22 23:57:14 +01:00
|
|
|
private final Set<RosterEntry> unfiledEntries = new CopyOnWriteArraySet<>();
|
2015-01-22 23:52:49 +01:00
|
|
|
private final Set<RosterListener> rosterListeners = new LinkedHashSet<>();
|
2015-02-14 17:15:02 +01:00
|
|
|
|
2016-06-30 17:01:46 +02:00
|
|
|
private final Set<PresenceEventListener> presenceEventListeners = new CopyOnWriteArraySet<>();
|
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
/**
|
|
|
|
* A map of JIDs to another Map of Resourceparts to Presences. The 'inner' map may contain
|
|
|
|
* {@link Resourcepart#EMPTY} if there are no other Presences available.
|
|
|
|
*/
|
2015-06-06 09:00:19 +02:00
|
|
|
private final Map<BareJid, Map<Resourcepart, Presence>> presenceMap = new ConcurrentHashMap<>();
|
2015-01-05 21:42:35 +01:00
|
|
|
|
2015-12-01 18:15:33 +01:00
|
|
|
/**
|
|
|
|
* Like {@link presenceMap} but for presences of entities not in our Roster.
|
|
|
|
*/
|
|
|
|
// TODO Ideally we want here to use a LRU cache like Map which will evict all superfluous items
|
|
|
|
// if their maximum size is lowered below the current item count. LruCache does not provide
|
|
|
|
// this.
|
|
|
|
private final LruCache<BareJid, Map<Resourcepart, Presence>> nonRosterPresenceMap = new LruCache<>(
|
|
|
|
defaultNonRosterPresenceMapMaxSize);
|
|
|
|
|
2015-02-17 15:24:33 +01:00
|
|
|
/**
|
|
|
|
* Listeners called when the Roster was loaded.
|
|
|
|
*/
|
|
|
|
private final Set<RosterLoadedListener> rosterLoadedListeners = new LinkedHashSet<>();
|
|
|
|
|
2015-01-26 16:35:47 +01:00
|
|
|
/**
|
|
|
|
* Mutually exclude roster listener invocation and changing the {@link entries} map. Also used
|
|
|
|
* to synchronize access to either the roster listeners or the entries map.
|
|
|
|
*/
|
|
|
|
private final Object rosterListenersAndEntriesLock = new Object();
|
|
|
|
|
2015-07-03 17:35:20 +02:00
|
|
|
private enum RosterState {
|
|
|
|
uninitialized,
|
|
|
|
loading,
|
|
|
|
loaded,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current state of the roster.
|
|
|
|
*/
|
|
|
|
private RosterState rosterState = RosterState.uninitialized;
|
2015-01-05 21:42:35 +01:00
|
|
|
|
2014-04-29 14:12:49 +02:00
|
|
|
private final PresencePacketListener presencePacketListener = new PresencePacketListener();
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2015-01-22 13:53:50 +01:00
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-01-22 13:53:50 +01:00
|
|
|
*/
|
|
|
|
private boolean rosterLoadedAtLogin = rosterLoadedAtLoginDefault;
|
|
|
|
|
2006-07-18 08:47:38 +02:00
|
|
|
private SubscriptionMode subscriptionMode = getDefaultSubscriptionMode();
|
2004-01-18 15:17:13 +01:00
|
|
|
|
2016-07-31 14:30:31 +02:00
|
|
|
private final Set<SubscribeListener> subscribeListeners = new CopyOnWriteArraySet<>();
|
|
|
|
|
|
|
|
private SubscriptionMode previousSubscriptionMode;
|
2015-05-21 20:12:39 +02:00
|
|
|
|
2004-01-18 15:17:13 +01:00
|
|
|
/**
|
2006-07-18 07:14:33 +02:00
|
|
|
* Returns the default subscription processing mode to use when a new Roster is created. The
|
|
|
|
* subscription processing mode dictates what action Smack will take when subscription
|
|
|
|
* requests from other users are made. The default subscription mode
|
2017-08-26 19:16:07 +02:00
|
|
|
* is {@link SubscriptionMode#reject_all}.
|
2006-07-18 07:14:33 +02:00
|
|
|
*
|
2004-01-18 15:17:13 +01:00
|
|
|
* @return the default subscription mode to use for new Rosters
|
|
|
|
*/
|
2006-07-18 08:47:38 +02:00
|
|
|
public static SubscriptionMode getDefaultSubscriptionMode() {
|
2004-01-19 19:52:43 +01:00
|
|
|
return defaultSubscriptionMode;
|
2004-01-18 15:17:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-18 07:14:33 +02:00
|
|
|
* Sets the default subscription processing mode to use when a new Roster is created. The
|
|
|
|
* subscription processing mode dictates what action Smack will take when subscription
|
|
|
|
* requests from other users are made. The default subscription mode
|
2017-08-26 19:16:07 +02:00
|
|
|
* is {@link SubscriptionMode#reject_all}.
|
2004-01-18 15:17:13 +01:00
|
|
|
*
|
|
|
|
* @param subscriptionMode the default subscription mode to use for new Rosters.
|
|
|
|
*/
|
2006-07-18 08:47:38 +02:00
|
|
|
public static void setDefaultSubscriptionMode(SubscriptionMode subscriptionMode) {
|
2004-01-19 19:52:43 +01:00
|
|
|
defaultSubscriptionMode = subscriptionMode;
|
2004-01-18 15:17:13 +01:00
|
|
|
}
|
2003-06-17 19:20:30 +02:00
|
|
|
|
2018-04-08 21:21:50 +02:00
|
|
|
private final AsyncButOrdered<BareJid> asyncButOrdered = new AsyncButOrdered<>();
|
|
|
|
|
2003-04-13 06:51:37 +02:00
|
|
|
/**
|
|
|
|
* Creates a new roster.
|
|
|
|
*
|
|
|
|
* @param connection an XMPP connection.
|
|
|
|
*/
|
2015-01-22 13:53:50 +01:00
|
|
|
private Roster(final XMPPConnection connection) {
|
|
|
|
super(connection);
|
2015-01-05 21:42:35 +01:00
|
|
|
|
|
|
|
// Note that we use sync packet listeners because RosterListeners should be invoked in the same order as the
|
|
|
|
// roster stanzas arrive.
|
2003-02-10 06:01:01 +01:00
|
|
|
// Listen for any roster packets.
|
2015-01-08 11:01:35 +01:00
|
|
|
connection.registerIQRequestHandler(new RosterPushListener());
|
2003-04-14 06:10:47 +02:00
|
|
|
// Listen for any presence packets.
|
2015-03-01 10:28:15 +01:00
|
|
|
connection.addSyncStanzaListener(presencePacketListener, PRESENCE_PACKET_FILTER);
|
2014-03-14 01:48:33 +01:00
|
|
|
|
2015-05-21 20:12:39 +02:00
|
|
|
connection.addAsyncStanzaListener(new StanzaListener() {
|
2017-11-06 22:38:13 +01:00
|
|
|
@SuppressWarnings("fallthrough")
|
2015-05-21 20:12:39 +02:00
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public void processStanza(Stanza stanza) throws NotConnectedException,
|
2017-11-06 22:38:13 +01:00
|
|
|
InterruptedException, NotLoggedInException {
|
2015-05-21 20:12:39 +02:00
|
|
|
Presence presence = (Presence) stanza;
|
|
|
|
Jid from = presence.getFrom();
|
|
|
|
SubscribeAnswer subscribeAnswer = null;
|
|
|
|
switch (subscriptionMode) {
|
|
|
|
case manual:
|
2016-07-31 14:30:31 +02:00
|
|
|
for (SubscribeListener subscribeListener : subscribeListeners) {
|
|
|
|
subscribeAnswer = subscribeListener.processSubscribe(from, presence);
|
|
|
|
if (subscribeAnswer != null) {
|
|
|
|
break;
|
|
|
|
}
|
2015-05-21 20:12:39 +02:00
|
|
|
}
|
|
|
|
if (subscribeAnswer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case accept_all:
|
|
|
|
// Accept all subscription requests.
|
|
|
|
subscribeAnswer = SubscribeAnswer.Approve;
|
|
|
|
break;
|
|
|
|
case reject_all:
|
|
|
|
// Reject all subscription requests.
|
|
|
|
subscribeAnswer = SubscribeAnswer.Deny;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:13 +01:00
|
|
|
if (subscribeAnswer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence.Type type;
|
2017-11-06 22:38:13 +01:00
|
|
|
switch (subscribeAnswer) {
|
|
|
|
case ApproveAndAlsoRequestIfRequired:
|
|
|
|
BareJid bareFrom = from.asBareJid();
|
|
|
|
RosterUtil.askForSubscriptionIfRequired(Roster.this, bareFrom);
|
|
|
|
// The fall through is intended.
|
|
|
|
case Approve:
|
2019-10-24 15:45:08 +02:00
|
|
|
type = Presence.Type.subscribed;
|
2017-11-06 22:38:13 +01:00
|
|
|
break;
|
|
|
|
case Deny:
|
2019-10-24 15:45:08 +02:00
|
|
|
type = Presence.Type.unsubscribed;
|
2017-11-06 22:38:13 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new AssertionError();
|
2015-05-21 20:12:39 +02:00
|
|
|
}
|
2017-11-06 22:38:13 +01:00
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence response = connection.getStanzaFactory().buildPresenceStanza()
|
|
|
|
.ofType(type)
|
|
|
|
.to(presence.getFrom())
|
|
|
|
.build();
|
2015-05-21 20:12:39 +02:00
|
|
|
connection.sendStanza(response);
|
|
|
|
}
|
|
|
|
}, PresenceTypeFilter.SUBSCRIBE);
|
|
|
|
|
2006-09-14 21:14:51 +02:00
|
|
|
// Listen for connection events
|
2020-05-13 22:01:48 +02:00
|
|
|
connection.addConnectionListener(new ConnectionListener() {
|
2015-01-07 19:56:44 +01:00
|
|
|
|
|
|
|
@Override
|
2015-01-07 20:19:04 +01:00
|
|
|
public void authenticated(XMPPConnection connection, boolean resumed) {
|
2015-01-22 13:53:50 +01:00
|
|
|
if (!isRosterLoadedAtLogin())
|
2014-07-18 14:38:48 +02:00
|
|
|
return;
|
2015-01-07 20:19:04 +01:00
|
|
|
// We are done here if the connection was resumed
|
|
|
|
if (resumed) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-30 17:01:46 +02:00
|
|
|
|
|
|
|
// Ensure that all available presences received so far in a eventually existing previous session are
|
|
|
|
// marked 'offline'.
|
|
|
|
setOfflinePresencesAndResetLoaded();
|
|
|
|
|
2014-07-18 14:38:48 +02:00
|
|
|
try {
|
|
|
|
Roster.this.reload();
|
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException | SmackException e) {
|
2014-07-18 14:38:48 +02:00
|
|
|
LOGGER.log(Level.SEVERE, "Could not reload Roster", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 19:56:44 +01:00
|
|
|
@Override
|
2016-04-15 17:41:50 +02:00
|
|
|
public void connectionClosed() {
|
2007-02-19 09:35:05 +01:00
|
|
|
// Changes the presence available contacts to unavailable
|
2015-01-05 21:42:35 +01:00
|
|
|
setOfflinePresencesAndResetLoaded();
|
2007-02-19 09:35:05 +01:00
|
|
|
}
|
|
|
|
|
2014-03-14 01:48:33 +01:00
|
|
|
});
|
2017-01-03 10:48:11 +01:00
|
|
|
|
2018-02-28 15:02:55 +01:00
|
|
|
connection.addStanzaSendingListener(new StanzaListener() {
|
2017-01-03 10:48:11 +01:00
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public void processStanza(Stanza stanzav) throws NotConnectedException, InterruptedException {
|
2017-01-03 10:48:11 +01:00
|
|
|
// Once we send an unavailable presence, the server is allowed to suppress sending presence status
|
|
|
|
// information to us as optimization (RFC 6121 § 4.4.2). Thus XMPP clients which are unavailable, should
|
|
|
|
// consider the presence information of their contacts as not up-to-date. We make the user obvious of
|
|
|
|
// this situation by setting the presences of all contacts to unavailable (while keeping the roster
|
|
|
|
// state).
|
|
|
|
setOfflinePresences();
|
|
|
|
}
|
2017-01-29 11:08:45 +01:00
|
|
|
}, OUTGOING_USER_UNAVAILABLE_PRESENCE);
|
2017-01-03 10:48:11 +01:00
|
|
|
|
2014-03-14 01:48:33 +01:00
|
|
|
// If the connection is already established, call reload
|
|
|
|
if (connection.isAuthenticated()) {
|
|
|
|
try {
|
2015-05-15 17:02:52 +02:00
|
|
|
reloadAndWait();
|
2014-03-14 01:48:33 +01:00
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException | SmackException e) {
|
2014-03-14 01:48:33 +01:00
|
|
|
LOGGER.log(Level.SEVERE, "Could not reload Roster", e);
|
|
|
|
}
|
2010-08-15 18:32:09 +02:00
|
|
|
}
|
2017-01-03 10:48:11 +01:00
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 18:15:33 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the user presences (a map from resource to {@link Presence}) for a given XMPP entity represented by their bare JID.
|
|
|
|
*
|
|
|
|
* @param entity the entity
|
|
|
|
* @return the user presences
|
|
|
|
*/
|
|
|
|
private Map<Resourcepart, Presence> getPresencesInternal(BareJid entity) {
|
|
|
|
Map<Resourcepart, Presence> entityPresences = presenceMap.get(entity);
|
|
|
|
if (entityPresences == null) {
|
2017-01-02 09:07:57 +01:00
|
|
|
entityPresences = nonRosterPresenceMap.lookup(entity);
|
2015-12-01 18:15:33 +01:00
|
|
|
}
|
|
|
|
return entityPresences;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the user presences (a map from resource to {@link Presence}) for a given XMPP entity represented by their bare JID.
|
|
|
|
*
|
|
|
|
* @param entity the entity
|
|
|
|
* @return the user presences
|
|
|
|
*/
|
|
|
|
private synchronized Map<Resourcepart, Presence> getOrCreatePresencesInternal(BareJid entity) {
|
|
|
|
Map<Resourcepart, Presence> entityPresences = getPresencesInternal(entity);
|
|
|
|
if (entityPresences == null) {
|
|
|
|
if (contains(entity)) {
|
2018-03-09 16:00:18 +01:00
|
|
|
entityPresences = new ConcurrentHashMap<>();
|
2015-12-01 18:15:33 +01:00
|
|
|
presenceMap.put(entity, entityPresences);
|
|
|
|
}
|
|
|
|
else {
|
2018-03-09 16:00:18 +01:00
|
|
|
LruCache<Resourcepart, Presence> nonRosterEntityPresences = new LruCache<>(32);
|
|
|
|
nonRosterPresenceMap.put(entity, nonRosterEntityPresences);
|
|
|
|
entityPresences = nonRosterEntityPresences;
|
2015-12-01 18:15:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return entityPresences;
|
|
|
|
}
|
|
|
|
|
2003-06-17 19:20:30 +02:00
|
|
|
/**
|
|
|
|
* Returns the subscription processing mode, which dictates what action
|
|
|
|
* Smack will take when subscription requests from other users are made.
|
2017-08-26 19:16:07 +02:00
|
|
|
* The default subscription mode is {@link SubscriptionMode#reject_all}.
|
2015-04-03 19:15:35 +02:00
|
|
|
* <p>
|
2003-10-03 02:31:29 +02:00
|
|
|
* If using the manual mode, a PacketListener should be registered that
|
2004-03-11 16:55:44 +01:00
|
|
|
* listens for Presence packets that have a type of
|
2006-07-17 10:39:08 +02:00
|
|
|
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
|
2015-04-03 19:15:35 +02:00
|
|
|
* </p>
|
2003-06-17 19:20:30 +02:00
|
|
|
*
|
|
|
|
* @return the subscription mode.
|
|
|
|
*/
|
2006-07-18 08:47:38 +02:00
|
|
|
public SubscriptionMode getSubscriptionMode() {
|
2003-06-17 19:20:30 +02:00
|
|
|
return subscriptionMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the subscription processing mode, which dictates what action
|
|
|
|
* Smack will take when subscription requests from other users are made.
|
2017-08-26 19:16:07 +02:00
|
|
|
* The default subscription mode is {@link SubscriptionMode#reject_all}.
|
2015-04-03 19:15:35 +02:00
|
|
|
* <p>
|
2003-10-03 02:31:29 +02:00
|
|
|
* If using the manual mode, a PacketListener should be registered that
|
2004-03-11 16:55:44 +01:00
|
|
|
* listens for Presence packets that have a type of
|
2006-07-17 10:39:08 +02:00
|
|
|
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
|
2015-04-03 19:15:35 +02:00
|
|
|
* </p>
|
2003-06-17 19:20:30 +02:00
|
|
|
*
|
|
|
|
* @param subscriptionMode the subscription mode.
|
|
|
|
*/
|
2006-07-18 08:47:38 +02:00
|
|
|
public void setSubscriptionMode(SubscriptionMode subscriptionMode) {
|
2003-06-17 19:20:30 +02:00
|
|
|
this.subscriptionMode = subscriptionMode;
|
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
|
|
|
* Reloads the entire roster from the server. This is an asynchronous operation,
|
|
|
|
* which means the method will return immediately, and the roster will be
|
|
|
|
* reloaded at a later point when the server responds to the reload request.
|
2014-03-14 01:48:33 +01:00
|
|
|
* @throws NotLoggedInException If not logged in.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2003-02-10 06:01:01 +01:00
|
|
|
*/
|
2017-05-23 16:45:04 +02:00
|
|
|
public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException {
|
2015-04-06 14:00:27 +02:00
|
|
|
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
|
2010-08-15 18:32:09 +02:00
|
|
|
|
2013-11-10 15:02:57 +01:00
|
|
|
RosterPacket packet = new RosterPacket();
|
2014-09-11 09:49:16 +02:00
|
|
|
if (rosterStore != null && isRosterVersioningSupported()) {
|
2013-11-10 15:02:57 +01:00
|
|
|
packet.setVersion(rosterStore.getRosterVersion());
|
|
|
|
}
|
2015-07-03 17:35:20 +02:00
|
|
|
rosterState = RosterState.loading;
|
2017-08-16 13:46:42 +02:00
|
|
|
|
|
|
|
SmackFuture<IQ, Exception> future = connection.sendIqRequestAsync(packet);
|
|
|
|
|
|
|
|
future.onSuccess(new RosterResultListener()).onError(new ExceptionCallback<Exception>() {
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
@Override
|
|
|
|
public void processException(Exception exception) {
|
2015-07-03 17:35:20 +02:00
|
|
|
rosterState = RosterState.uninitialized;
|
2022-02-07 14:36:45 +01:00
|
|
|
Level logLevel = Level.SEVERE;
|
2015-07-03 12:57:30 +02:00
|
|
|
if (exception instanceof NotConnectedException) {
|
|
|
|
logLevel = Level.FINE;
|
2022-02-07 14:36:45 +01:00
|
|
|
} else if (exception instanceof XMPPErrorException) {
|
|
|
|
Condition condition = ((XMPPErrorException) exception).getStanzaError().getCondition();
|
|
|
|
if (condition == Condition.feature_not_implemented || condition == Condition.service_unavailable) {
|
|
|
|
logLevel = Level.FINE;
|
|
|
|
}
|
2015-07-03 12:57:30 +02:00
|
|
|
}
|
2017-08-16 13:46:42 +02:00
|
|
|
LOGGER.log(logLevel, "Exception reloading roster", exception);
|
2015-12-09 10:49:15 +01:00
|
|
|
for (RosterLoadedListener listener : rosterLoadedListeners) {
|
|
|
|
listener.onRosterLoadingFailed(exception);
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
2017-08-16 13:46:42 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
});
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 11:45:25 +01:00
|
|
|
/**
|
|
|
|
* Reload the roster and block until it is reloaded.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotLoggedInException if the XMPP connection is not authenticated.
|
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2015-01-26 11:45:25 +01:00
|
|
|
* @since 4.1
|
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public void reloadAndWait() throws NotLoggedInException, NotConnectedException, InterruptedException {
|
2015-01-22 13:53:50 +01:00
|
|
|
reload();
|
|
|
|
waitUntilLoaded();
|
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2015-01-26 11:45:25 +01:00
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Set the roster store, may cause a roster reload.
|
2015-01-26 11:45:25 +01:00
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param rosterStore TODO javadoc me please
|
2015-01-26 11:45:25 +01:00
|
|
|
* @return true if the roster reload was initiated, false otherwise.
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
2015-01-22 13:53:50 +01:00
|
|
|
public boolean setRosterStore(RosterStore rosterStore) {
|
|
|
|
this.rosterStore = rosterStore;
|
|
|
|
try {
|
|
|
|
reload();
|
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException | NotLoggedInException | NotConnectedException e) {
|
2015-01-22 13:53:50 +01:00
|
|
|
LOGGER.log(Level.FINER, "Could not reload roster", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-28 22:05:47 +01:00
|
|
|
boolean waitUntilLoaded() throws InterruptedException {
|
2017-01-12 20:57:19 +01:00
|
|
|
long waitTime = connection().getReplyTimeout();
|
2015-07-03 14:42:45 +02:00
|
|
|
long start = System.currentTimeMillis();
|
2015-07-03 17:35:20 +02:00
|
|
|
while (!isLoaded()) {
|
2015-01-05 21:42:35 +01:00
|
|
|
if (waitTime <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
synchronized (this) {
|
2015-07-03 17:35:20 +02:00
|
|
|
if (!isLoaded()) {
|
2015-02-14 09:43:44 +01:00
|
|
|
wait(waitTime);
|
2015-01-05 21:42:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
waitTime -= now - start;
|
|
|
|
start = now;
|
|
|
|
}
|
|
|
|
return isLoaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the roster is loaded.
|
|
|
|
*
|
|
|
|
* @return true if the roster is loaded.
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
|
|
|
public boolean isLoaded() {
|
2015-07-03 17:35:20 +02:00
|
|
|
return rosterState == RosterState.loaded;
|
2015-01-05 21:42:35 +01:00
|
|
|
}
|
|
|
|
|
2003-04-13 06:51:37 +02:00
|
|
|
/**
|
|
|
|
* Adds a listener to this roster. The listener will be fired anytime one or more
|
|
|
|
* changes to the roster are pushed from the server.
|
|
|
|
*
|
|
|
|
* @param rosterListener a roster listener.
|
2015-01-22 23:52:49 +01:00
|
|
|
* @return true if the listener was not already added.
|
2015-01-26 16:35:47 +01:00
|
|
|
* @see #getEntriesAndAddListener(RosterListener, RosterEntries)
|
2003-04-13 06:51:37 +02:00
|
|
|
*/
|
2015-01-22 23:52:49 +01:00
|
|
|
public boolean addRosterListener(RosterListener rosterListener) {
|
2015-01-26 16:35:47 +01:00
|
|
|
synchronized (rosterListenersAndEntriesLock) {
|
|
|
|
return rosterListeners.add(rosterListener);
|
|
|
|
}
|
2003-04-13 06:51:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a listener from this roster. The listener will be fired anytime one or more
|
|
|
|
* changes to the roster are pushed from the server.
|
|
|
|
*
|
|
|
|
* @param rosterListener a roster listener.
|
2015-01-22 23:52:49 +01:00
|
|
|
* @return true if the listener was active and got removed.
|
2003-04-13 06:51:37 +02:00
|
|
|
*/
|
2015-01-22 23:52:49 +01:00
|
|
|
public boolean removeRosterListener(RosterListener rosterListener) {
|
2015-01-26 16:35:47 +01:00
|
|
|
synchronized (rosterListenersAndEntriesLock) {
|
|
|
|
return rosterListeners.remove(rosterListener);
|
|
|
|
}
|
2003-04-13 06:51:37 +02:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:24:33 +01:00
|
|
|
/**
|
2019-04-06 02:36:32 +02:00
|
|
|
* Add a roster loaded listener. Roster loaded listeners are invoked once the {@link Roster}
|
|
|
|
* was successfully loaded.
|
2015-02-17 15:24:33 +01:00
|
|
|
*
|
|
|
|
* @param rosterLoadedListener the listener to add.
|
|
|
|
* @return true if the listener was not already added.
|
|
|
|
* @see RosterLoadedListener
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
|
|
|
public boolean addRosterLoadedListener(RosterLoadedListener rosterLoadedListener) {
|
|
|
|
synchronized (rosterLoadedListener) {
|
|
|
|
return rosterLoadedListeners.add(rosterLoadedListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a roster loaded listener.
|
|
|
|
*
|
|
|
|
* @param rosterLoadedListener the listener to remove.
|
|
|
|
* @return true if the listener was active and got removed.
|
|
|
|
* @see RosterLoadedListener
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
|
|
|
public boolean removeRosterLoadedListener(RosterLoadedListener rosterLoadedListener) {
|
|
|
|
synchronized (rosterLoadedListener) {
|
|
|
|
return rosterLoadedListeners.remove(rosterLoadedListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 02:36:32 +02:00
|
|
|
/**
|
|
|
|
* Add a {@link PresenceEventListener}. Such a listener will be fired whenever certain
|
|
|
|
* presence events happen.<p>
|
|
|
|
* Among those events are:
|
|
|
|
* <ul>
|
|
|
|
* <li> 'available' presence received
|
|
|
|
* <li> 'unavailable' presence received
|
|
|
|
* <li> 'error' presence received
|
|
|
|
* <li> 'subscribed' presence received
|
|
|
|
* <li> 'unsubscribed' presence received
|
|
|
|
* </ul>
|
|
|
|
* @param presenceEventListener listener to add.
|
|
|
|
* @return true if the listener was not already added.
|
|
|
|
*/
|
2016-06-30 17:01:46 +02:00
|
|
|
public boolean addPresenceEventListener(PresenceEventListener presenceEventListener) {
|
|
|
|
return presenceEventListeners.add(presenceEventListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean removePresenceEventListener(PresenceEventListener presenceEventListener) {
|
|
|
|
return presenceEventListeners.remove(presenceEventListener);
|
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
2015-04-03 19:15:35 +02:00
|
|
|
* Creates a new group.
|
|
|
|
* <p>
|
2003-08-21 05:31:50 +02:00
|
|
|
* Note: you must add at least one entry to the group for the group to be kept
|
|
|
|
* after a logout/login. This is due to the way that XMPP stores group information.
|
2015-04-03 19:15:35 +02:00
|
|
|
* </p>
|
2003-02-10 06:01:01 +01:00
|
|
|
*
|
|
|
|
* @param name the name of the group.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @return a new group, or null if the group already exists
|
2003-02-10 06:01:01 +01:00
|
|
|
*/
|
2014-04-29 14:12:49 +02:00
|
|
|
public RosterGroup createGroup(String name) {
|
2015-01-22 13:53:50 +01:00
|
|
|
final XMPPConnection connection = connection();
|
2006-10-08 01:16:20 +02:00
|
|
|
if (groups.containsKey(name)) {
|
2014-03-17 19:21:20 +01:00
|
|
|
return groups.get(name);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2006-10-08 01:16:20 +02:00
|
|
|
RosterGroup group = new RosterGroup(name, connection);
|
|
|
|
groups.put(name, group);
|
|
|
|
return group;
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2003-01-16 16:42:18 +01:00
|
|
|
|
2003-03-24 00:03:19 +01:00
|
|
|
/**
|
2004-03-11 15:33:48 +01:00
|
|
|
* Creates a new roster entry and presence subscription. The server will asynchronously
|
2003-04-25 22:12:17 +02:00
|
|
|
* update the roster with the subscription status.
|
2003-03-24 00:03:19 +01:00
|
|
|
*
|
2007-02-20 18:02:39 +01:00
|
|
|
* @param user the user. (e.g. johndoe@jabber.org)
|
|
|
|
* @param name the nickname of the user.
|
2019-07-19 18:10:36 +02:00
|
|
|
* @param groups the list of group names the entry will belong to, or <code>null</code> if the
|
2007-02-20 18:02:39 +01:00
|
|
|
* the roster entry won't belong to a group.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws NoResponseException if there was no response from the server.
|
|
|
|
* @throws XMPPErrorException if an XMPP exception occurs.
|
|
|
|
* @throws NotLoggedInException If not logged in.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2019-03-16 21:13:00 +01:00
|
|
|
* @deprecated use {@link #createItemAndRequestSubscription(BareJid, String, String[])} instead.
|
2003-03-24 00:03:19 +01:00
|
|
|
*/
|
2019-03-16 21:13:00 +01:00
|
|
|
// TODO: Remove in Smack 4.5.
|
|
|
|
@Deprecated
|
2015-05-27 22:34:43 +02:00
|
|
|
public void createEntry(BareJid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2019-03-16 21:13:00 +01:00
|
|
|
createItemAndRequestSubscription(user, name, groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new roster item. The server will asynchronously update the roster with the subscription status.
|
|
|
|
* <p>
|
|
|
|
* There will be no presence subscription request. Consider using
|
|
|
|
* {@link #createItemAndRequestSubscription(BareJid, String, String[])} if you also want to request a presence
|
|
|
|
* subscription from the contact.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param jid the XMPP address of the contact (e.g. johndoe@jabber.org)
|
|
|
|
* @param name the nickname of the user.
|
2019-07-19 18:10:36 +02:00
|
|
|
* @param groups the list of group names the entry will belong to, or <code>null</code> if the the roster entry won't
|
2019-03-16 21:13:00 +01:00
|
|
|
* belong to a group.
|
|
|
|
* @throws NoResponseException if there was no response from the server.
|
|
|
|
* @throws XMPPErrorException if an XMPP exception occurs.
|
|
|
|
* @throws NotLoggedInException If not logged in.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2019-03-16 21:13:00 +01:00
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
public void createItem(BareJid jid, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2015-04-06 14:00:27 +02:00
|
|
|
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
|
2010-08-15 18:32:09 +02:00
|
|
|
|
2003-04-25 22:12:17 +02:00
|
|
|
// Create and send roster entry creation packet.
|
|
|
|
RosterPacket rosterPacket = new RosterPacket();
|
2014-06-06 02:20:45 +02:00
|
|
|
rosterPacket.setType(IQ.Type.set);
|
2019-03-16 21:13:00 +01:00
|
|
|
RosterPacket.Item item = new RosterPacket.Item(jid, name);
|
2003-04-25 22:12:17 +02:00
|
|
|
if (groups != null) {
|
2006-07-18 07:14:33 +02:00
|
|
|
for (String group : groups) {
|
2010-02-18 15:33:45 +01:00
|
|
|
if (group != null && group.trim().length() > 0) {
|
2006-07-18 07:14:33 +02:00
|
|
|
item.addGroupName(group);
|
2004-03-11 15:33:48 +01:00
|
|
|
}
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rosterPacket.addRosterItem(item);
|
2021-05-11 22:03:24 +02:00
|
|
|
connection.sendIqRequestAndWaitForResponse(rosterPacket);
|
2019-03-16 21:13:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new roster entry and presence subscription. The server will asynchronously
|
|
|
|
* update the roster with the subscription status.
|
|
|
|
*
|
|
|
|
* @param jid the XMPP address of the contact (e.g. johndoe@jabber.org)
|
|
|
|
* @param name the nickname of the user.
|
2019-07-19 18:10:36 +02:00
|
|
|
* @param groups the list of group names the entry will belong to, or <code>null</code> if the
|
2019-03-16 21:13:00 +01:00
|
|
|
* the roster entry won't belong to a group.
|
|
|
|
* @throws NoResponseException if there was no response from the server.
|
|
|
|
* @throws XMPPErrorException if an XMPP exception occurs.
|
|
|
|
* @throws NotLoggedInException If not logged in.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2019-03-16 21:13:00 +01:00
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
public void createItemAndRequestSubscription(BareJid jid, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
|
|
|
createItem(jid, name, groups);
|
2003-04-25 22:12:17 +02:00
|
|
|
|
2019-03-16 21:13:00 +01:00
|
|
|
sendSubscriptionRequest(jid);
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 15:19:52 +01:00
|
|
|
/**
|
|
|
|
* Creates a new pre-approved roster entry and presence subscription. The server will
|
|
|
|
* asynchronously update the roster with the subscription status.
|
|
|
|
*
|
|
|
|
* @param user the user. (e.g. johndoe@jabber.org)
|
|
|
|
* @param name the nickname of the user.
|
2019-07-19 18:10:36 +02:00
|
|
|
* @param groups the list of group names the entry will belong to, or <code>null</code> if the
|
2015-03-14 15:19:52 +01:00
|
|
|
* the roster entry won't belong to a group.
|
|
|
|
* @throws NoResponseException if there was no response from the server.
|
|
|
|
* @throws XMPPErrorException if an XMPP exception occurs.
|
|
|
|
* @throws NotLoggedInException if not logged in.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2015-03-14 15:19:52 +01:00
|
|
|
* @throws FeatureNotSupportedException if pre-approving is not supported.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
2015-05-27 22:34:43 +02:00
|
|
|
public void preApproveAndCreateEntry(BareJid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
|
2015-03-14 15:19:52 +01:00
|
|
|
preApprove(user);
|
2019-03-16 21:13:00 +01:00
|
|
|
createItemAndRequestSubscription(user, name, groups);
|
2015-03-14 15:19:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pre-approve user presence subscription.
|
|
|
|
*
|
|
|
|
* @param user the user. (e.g. johndoe@jabber.org)
|
|
|
|
* @throws NotLoggedInException if not logged in.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2015-03-14 15:19:52 +01:00
|
|
|
* @throws FeatureNotSupportedException if pre-approving is not supported.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
2016-06-30 17:01:46 +02:00
|
|
|
public void preApprove(BareJid user) throws NotLoggedInException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
|
2015-03-14 15:19:52 +01:00
|
|
|
final XMPPConnection connection = connection();
|
|
|
|
if (!isSubscriptionPreApprovalSupported()) {
|
|
|
|
throw new FeatureNotSupportedException("Pre-approving");
|
|
|
|
}
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence presencePacket = connection.getStanzaFactory().buildPresenceStanza()
|
|
|
|
.ofType(Presence.Type.subscribed)
|
|
|
|
.to(user)
|
|
|
|
.build();
|
2015-03-14 15:19:52 +01:00
|
|
|
connection.sendStanza(presencePacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for subscription pre-approval support.
|
|
|
|
*
|
|
|
|
* @return true if subscription pre-approval is supported by the server.
|
|
|
|
* @throws NotLoggedInException if not logged in.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public boolean isSubscriptionPreApprovalSupported() throws NotLoggedInException {
|
2015-04-06 14:00:27 +02:00
|
|
|
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
|
2015-03-14 15:19:52 +01:00
|
|
|
return connection.hasFeature(SubscriptionPreApproval.ELEMENT, SubscriptionPreApproval.NAMESPACE);
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:01:46 +02:00
|
|
|
public void sendSubscriptionRequest(BareJid jid) throws NotLoggedInException, NotConnectedException, InterruptedException {
|
|
|
|
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
|
|
|
|
|
|
|
|
// Create a presence subscription packet and send.
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence presencePacket = connection.getStanzaFactory().buildPresenceStanza()
|
|
|
|
.ofType(Presence.Type.subscribe)
|
|
|
|
.to(jid)
|
|
|
|
.build();
|
2016-06-30 17:01:46 +02:00
|
|
|
connection.sendStanza(presencePacket);
|
|
|
|
}
|
|
|
|
|
2015-05-21 20:12:39 +02:00
|
|
|
/**
|
2016-07-31 14:30:31 +02:00
|
|
|
* Add a subscribe listener, which is invoked on incoming subscription requests and if
|
|
|
|
* {@link SubscriptionMode} is set to {@link SubscriptionMode#manual}. This also sets subscription
|
2015-05-21 20:12:39 +02:00
|
|
|
* mode to {@link SubscriptionMode#manual}.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2016-07-31 14:30:31 +02:00
|
|
|
* @param subscribeListener the subscribe listener to add.
|
|
|
|
* @return <code>true</code> if the listener was not already added.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public boolean addSubscribeListener(SubscribeListener subscribeListener) {
|
|
|
|
Objects.requireNonNull(subscribeListener, "SubscribeListener argument must not be null");
|
|
|
|
if (subscriptionMode != SubscriptionMode.manual) {
|
|
|
|
previousSubscriptionMode = subscriptionMode;
|
2016-10-31 12:24:05 +01:00
|
|
|
subscriptionMode = SubscriptionMode.manual;
|
2016-07-31 14:30:31 +02:00
|
|
|
}
|
|
|
|
return subscribeListeners.add(subscribeListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a subscribe listener. Also restores the previous subscription mode
|
|
|
|
* state, if the last listener got removed.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param subscribeListener TODO javadoc me please
|
2016-07-31 14:30:31 +02:00
|
|
|
* the subscribe listener to remove.
|
|
|
|
* @return <code>true</code> if the listener registered and got removed.
|
2015-05-21 20:12:39 +02:00
|
|
|
* @since 4.2
|
|
|
|
*/
|
2016-07-31 14:30:31 +02:00
|
|
|
public boolean removeSubscribeListener(SubscribeListener subscribeListener) {
|
|
|
|
boolean removed = subscribeListeners.remove(subscribeListener);
|
|
|
|
if (removed && subscribeListeners.isEmpty()) {
|
|
|
|
setSubscriptionMode(previousSubscriptionMode);
|
2015-05-21 20:12:39 +02:00
|
|
|
}
|
2016-07-31 14:30:31 +02:00
|
|
|
return removed;
|
2015-05-21 20:12:39 +02:00
|
|
|
}
|
|
|
|
|
2003-09-24 20:07:02 +02:00
|
|
|
/**
|
2006-07-18 07:14:33 +02:00
|
|
|
* Removes a roster entry from the roster. The roster entry will also be removed from the
|
2003-11-05 18:51:06 +01:00
|
|
|
* unfiled entries or from any roster group where it could belong and will no longer be part
|
2014-02-23 08:47:32 +01:00
|
|
|
* of the roster. Note that this is a synchronous call -- Smack must wait for the server
|
2005-03-30 03:53:51 +02:00
|
|
|
* to send an updated subscription status.
|
2003-09-24 20:07:02 +02:00
|
|
|
*
|
|
|
|
* @param entry a roster entry.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException if an XMPP error occurs.
|
|
|
|
* @throws NotLoggedInException if not logged in.
|
|
|
|
* @throws NoResponseException SmackException if there was no response from the server.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2003-09-24 20:07:02 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2015-04-06 14:00:27 +02:00
|
|
|
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
|
2010-08-15 18:32:09 +02:00
|
|
|
|
2003-09-24 20:07:02 +02:00
|
|
|
// Only remove the entry if it's in the entry list.
|
2017-12-13 23:10:11 +01:00
|
|
|
// The actual removal logic takes place in RosterPacketListenerProcess>>Packet(Packet)
|
2015-05-27 22:34:43 +02:00
|
|
|
if (!entries.containsKey(entry.getJid())) {
|
2006-10-08 01:16:20 +02:00
|
|
|
return;
|
2003-09-24 20:07:02 +02:00
|
|
|
}
|
2005-03-30 03:53:51 +02:00
|
|
|
RosterPacket packet = new RosterPacket();
|
2014-06-06 02:20:45 +02:00
|
|
|
packet.setType(IQ.Type.set);
|
2005-03-30 03:53:51 +02:00
|
|
|
RosterPacket.Item item = RosterEntry.toRosterItem(entry);
|
|
|
|
// Set the item type as REMOVE so that the server will delete the entry
|
2007-01-08 00:03:16 +01:00
|
|
|
item.setItemType(RosterPacket.ItemType.remove);
|
2005-03-30 03:53:51 +02:00
|
|
|
packet.addRosterItem(item);
|
2021-05-11 22:03:24 +02:00
|
|
|
connection.sendIqRequestAndWaitForResponse(packet);
|
2003-09-24 20:07:02 +02:00
|
|
|
}
|
|
|
|
|
2003-08-05 04:33:11 +02:00
|
|
|
/**
|
|
|
|
* Returns a count of the entries in the roster.
|
|
|
|
*
|
|
|
|
* @return the number of entries in the roster.
|
|
|
|
*/
|
|
|
|
public int getEntryCount() {
|
2006-07-17 10:39:08 +02:00
|
|
|
return getEntries().size();
|
2003-08-05 04:33:11 +02:00
|
|
|
}
|
|
|
|
|
2015-01-26 16:35:47 +01:00
|
|
|
/**
|
|
|
|
* Add a roster listener and invoke the roster entries with all entries of the roster.
|
|
|
|
* <p>
|
|
|
|
* The method guarantees that the listener is only invoked after
|
2016-01-10 19:00:26 +01:00
|
|
|
* {@link RosterEntries#rosterEntries(Collection)} has been invoked, and that all roster events
|
2017-12-13 23:10:11 +01:00
|
|
|
* that happen while <code>rosterEntries(Collection) </code> is called are queued until the
|
2015-01-26 16:35:47 +01:00
|
|
|
* method returns.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* This guarantee makes this the ideal method to e.g. populate a UI element with the roster while
|
|
|
|
* installing a {@link RosterListener} to listen for subsequent roster events.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param rosterListener the listener to install
|
|
|
|
* @param rosterEntries the roster entries callback interface
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
|
|
|
public void getEntriesAndAddListener(RosterListener rosterListener, RosterEntries rosterEntries) {
|
|
|
|
Objects.requireNonNull(rosterListener, "listener must not be null");
|
|
|
|
Objects.requireNonNull(rosterEntries, "rosterEntries must not be null");
|
|
|
|
|
|
|
|
synchronized (rosterListenersAndEntriesLock) {
|
2016-01-10 19:00:26 +01:00
|
|
|
rosterEntries.rosterEntries(entries.values());
|
2015-01-26 16:35:47 +01:00
|
|
|
addRosterListener(rosterListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 04:33:11 +02:00
|
|
|
/**
|
2014-11-29 13:36:56 +01:00
|
|
|
* Returns a set of all entries in the roster, including entries
|
2006-07-17 10:39:08 +02:00
|
|
|
* that don't belong to any groups.
|
2003-08-05 04:33:11 +02:00
|
|
|
*
|
|
|
|
* @return all entries in the roster.
|
|
|
|
*/
|
2014-11-29 13:36:56 +01:00
|
|
|
public Set<RosterEntry> getEntries() {
|
2015-01-26 16:35:47 +01:00
|
|
|
Set<RosterEntry> allEntries;
|
|
|
|
synchronized (rosterListenersAndEntriesLock) {
|
|
|
|
allEntries = new HashSet<>(entries.size());
|
|
|
|
for (RosterEntry entry : entries.values()) {
|
|
|
|
allEntries.add(entry);
|
|
|
|
}
|
2003-08-05 04:33:11 +02:00
|
|
|
}
|
2014-11-29 13:36:56 +01:00
|
|
|
return allEntries;
|
2003-08-05 04:33:11 +02:00
|
|
|
}
|
|
|
|
|
2003-04-25 22:12:17 +02:00
|
|
|
/**
|
2003-08-21 05:31:50 +02:00
|
|
|
* Returns a count of the unfiled entries in the roster. An unfiled entry is
|
|
|
|
* an entry that doesn't belong to any groups.
|
|
|
|
*
|
|
|
|
* @return the number of unfiled entries in the roster.
|
|
|
|
*/
|
|
|
|
public int getUnfiledEntryCount() {
|
2006-10-08 01:16:20 +02:00
|
|
|
return unfiledEntries.size();
|
2003-08-21 05:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-22 23:57:14 +01:00
|
|
|
* Returns an unmodifiable set for the unfiled roster entries. An unfiled entry is
|
2003-08-21 05:31:50 +02:00
|
|
|
* an entry that doesn't belong to any groups.
|
2003-04-25 22:12:17 +02:00
|
|
|
*
|
2006-07-18 08:47:38 +02:00
|
|
|
* @return the unfiled roster entries.
|
2003-04-25 22:12:17 +02:00
|
|
|
*/
|
2015-01-22 23:57:14 +01:00
|
|
|
public Set<RosterEntry> getUnfiledEntries() {
|
|
|
|
return Collections.unmodifiableSet(unfiledEntries);
|
2003-03-24 00:03:19 +01:00
|
|
|
}
|
|
|
|
|
2003-09-03 05:00:45 +02:00
|
|
|
/**
|
|
|
|
* Returns the roster entry associated with the given XMPP address or
|
2019-07-19 18:10:36 +02:00
|
|
|
* <code>null</code> if the user is not an entry in the roster.
|
2003-09-03 05:00:45 +02:00
|
|
|
*
|
2015-06-06 09:00:19 +02:00
|
|
|
* @param jid the XMPP address of the user (eg "jsmith@example.com"). The address could be
|
2007-02-20 18:02:39 +01:00
|
|
|
* in any valid format (e.g. "domain/resource", "user@domain" or "user@domain/resource").
|
2019-07-19 18:10:36 +02:00
|
|
|
* @return the roster entry or <code>null</code> if it does not exist.
|
2003-09-03 05:00:45 +02:00
|
|
|
*/
|
2015-06-06 09:00:19 +02:00
|
|
|
public RosterEntry getEntry(BareJid jid) {
|
|
|
|
if (jid == null) {
|
2003-09-03 05:00:45 +02:00
|
|
|
return null;
|
|
|
|
}
|
2015-06-06 09:00:19 +02:00
|
|
|
return entries.get(jid);
|
2003-09-03 05:00:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specified XMPP address is an entry in the roster.
|
|
|
|
*
|
2015-06-06 09:00:19 +02:00
|
|
|
* @param jid the XMPP address of the user (eg "jsmith@example.com"). The
|
|
|
|
* address must be a bare JID e.g. "domain/resource" or
|
|
|
|
* "user@domain".
|
2003-09-03 05:00:45 +02:00
|
|
|
* @return true if the XMPP address is an entry in the roster.
|
|
|
|
*/
|
2015-06-06 09:00:19 +02:00
|
|
|
public boolean contains(BareJid jid) {
|
|
|
|
return getEntry(jid) != null;
|
2003-09-03 05:00:45 +02:00
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Returns the roster group with the specified name, or <code>null</code> if the
|
2003-02-10 06:01:01 +01:00
|
|
|
* group doesn't exist.
|
|
|
|
*
|
|
|
|
* @param name the name of the group.
|
|
|
|
* @return the roster group with the specified name.
|
|
|
|
*/
|
|
|
|
public RosterGroup getGroup(String name) {
|
2007-02-20 18:02:39 +01:00
|
|
|
return groups.get(name);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2003-01-16 16:42:18 +01:00
|
|
|
|
2003-03-24 00:03:19 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of the groups in the roster.
|
|
|
|
*
|
|
|
|
* @return the number of groups in the roster.
|
|
|
|
*/
|
|
|
|
public int getGroupCount() {
|
2006-07-17 10:39:08 +02:00
|
|
|
return groups.size();
|
2003-03-24 00:03:19 +01:00
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
2010-08-15 18:32:09 +02:00
|
|
|
* Returns an unmodifiable collections of all the roster groups.
|
2003-02-10 06:01:01 +01:00
|
|
|
*
|
|
|
|
* @return an iterator for all roster groups.
|
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
public Collection<RosterGroup> getGroups() {
|
|
|
|
return Collections.unmodifiableCollection(groups.values());
|
2003-01-16 16:42:18 +01:00
|
|
|
}
|
|
|
|
|
2003-04-25 22:12:17 +02:00
|
|
|
/**
|
2007-02-12 01:56:47 +01:00
|
|
|
* Returns the presence info for a particular user. If the user is offline, or
|
|
|
|
* if no presence data is available (such as when you are not subscribed to the
|
2015-02-23 22:17:31 +01:00
|
|
|
* user's presence updates), unavailable presence will be returned.
|
2019-08-30 12:08:30 +02:00
|
|
|
*
|
2007-02-12 01:56:47 +01:00
|
|
|
* If the user has several presences (one for each resource), then the presence with
|
|
|
|
* highest priority will be returned. If multiple presences have the same priority,
|
|
|
|
* the one with the "most available" presence mode will be returned. In order,
|
2008-10-31 05:04:15 +01:00
|
|
|
* that's {@link org.jivesoftware.smack.packet.Presence.Mode#chat free to chat},
|
|
|
|
* {@link org.jivesoftware.smack.packet.Presence.Mode#available available},
|
|
|
|
* {@link org.jivesoftware.smack.packet.Presence.Mode#away away},
|
|
|
|
* {@link org.jivesoftware.smack.packet.Presence.Mode#xa extended away}, and
|
2019-08-30 12:08:30 +02:00
|
|
|
* {@link org.jivesoftware.smack.packet.Presence.Mode#dnd do not disturb}.
|
|
|
|
*
|
2015-02-23 22:17:31 +01:00
|
|
|
* <p>
|
2006-05-30 23:53:50 +02:00
|
|
|
* Note that presence information is received asynchronously. So, just after logging
|
2007-02-12 01:56:47 +01:00
|
|
|
* in to the server, presence values for users in the roster may be unavailable
|
2006-05-30 23:53:50 +02:00
|
|
|
* even if they are actually online. In other words, the value returned by this
|
|
|
|
* method should only be treated as a snapshot in time, and may not accurately reflect
|
|
|
|
* other user's presence instant by instant. If you need to track presence over time,
|
|
|
|
* such as when showing a visual representation of the roster, consider using a
|
|
|
|
* {@link RosterListener}.
|
2015-02-23 22:17:31 +01:00
|
|
|
* </p>
|
2003-04-25 22:12:17 +02:00
|
|
|
*
|
2015-06-06 09:00:19 +02:00
|
|
|
* @param jid the XMPP address of the user (eg "jsmith@example.com"). The
|
|
|
|
* address must be a bare JID e.g. "domain/resource" or
|
|
|
|
* "user@domain".
|
2007-02-12 01:56:47 +01:00
|
|
|
* @return the user's current presence, or unavailable presence if the user is offline
|
2007-02-20 18:02:39 +01:00
|
|
|
* or if no presence information is available..
|
2003-04-25 22:12:17 +02:00
|
|
|
*/
|
2015-12-01 18:15:33 +01:00
|
|
|
public Presence getPresence(BareJid jid) {
|
|
|
|
Map<Resourcepart, Presence> userPresences = getPresencesInternal(jid);
|
2003-11-28 22:49:25 +01:00
|
|
|
if (userPresences == null) {
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence presence = synthesizeUnvailablePresence(jid);
|
2007-02-20 18:02:39 +01:00
|
|
|
return presence;
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Find the resource with the highest priority
|
|
|
|
// Might be changed to use the resource with the highest availability instead.
|
|
|
|
Presence presence = null;
|
2014-11-27 18:09:18 +01:00
|
|
|
// This is used in case no available presence is found
|
|
|
|
Presence unavailable = null;
|
2003-11-28 22:49:25 +01:00
|
|
|
|
2018-10-11 12:29:32 +02:00
|
|
|
for (Presence p : userPresences.values()) {
|
2008-11-21 05:54:59 +01:00
|
|
|
if (!p.isAvailable()) {
|
2014-11-27 18:09:18 +01:00
|
|
|
unavailable = p;
|
2008-11-21 05:54:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-02-12 01:56:47 +01:00
|
|
|
// Chose presence with highest priority first.
|
2006-11-30 00:32:04 +01:00
|
|
|
if (presence == null || p.getPriority() > presence.getPriority()) {
|
2003-11-28 22:49:25 +01:00
|
|
|
presence = p;
|
|
|
|
}
|
2007-02-12 01:56:47 +01:00
|
|
|
// If equal priority, choose "most available" by the mode value.
|
|
|
|
else if (p.getPriority() == presence.getPriority()) {
|
2007-02-12 02:11:09 +01:00
|
|
|
Presence.Mode pMode = p.getMode();
|
|
|
|
// Default to presence mode of available.
|
|
|
|
if (pMode == null) {
|
|
|
|
pMode = Presence.Mode.available;
|
|
|
|
}
|
|
|
|
Presence.Mode presenceMode = presence.getMode();
|
|
|
|
// Default to presence mode of available.
|
|
|
|
if (presenceMode == null) {
|
|
|
|
presenceMode = Presence.Mode.available;
|
|
|
|
}
|
|
|
|
if (pMode.compareTo(presenceMode) < 0) {
|
2007-02-12 01:56:47 +01:00
|
|
|
presence = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (presence == null) {
|
2014-11-27 18:09:18 +01:00
|
|
|
if (unavailable != null) {
|
2020-06-14 17:38:51 +02:00
|
|
|
return unavailable;
|
2014-11-27 18:09:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-10-24 15:45:08 +02:00
|
|
|
presence = synthesizeUnvailablePresence(jid);
|
2014-11-27 18:09:18 +01:00
|
|
|
return presence;
|
|
|
|
}
|
2007-02-12 01:56:47 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-06-14 17:38:51 +02:00
|
|
|
return presence;
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
/**
|
2007-02-12 01:56:47 +01:00
|
|
|
* Returns the presence info for a particular user's resource, or unavailable presence
|
|
|
|
* if the user is offline or if no presence information is available, such as
|
2003-11-28 22:49:25 +01:00
|
|
|
* when you are not subscribed to the user's presence updates.
|
|
|
|
*
|
2007-02-12 01:56:47 +01:00
|
|
|
* @param userWithResource a fully qualified XMPP ID including a resource (user@domain/resource).
|
|
|
|
* @return the user's current presence, or unavailable presence if the user is offline
|
2007-02-20 18:02:39 +01:00
|
|
|
* or if no presence information is available.
|
2003-11-28 22:49:25 +01:00
|
|
|
*/
|
2015-05-27 19:29:51 +02:00
|
|
|
public Presence getPresenceResource(FullJid userWithResource) {
|
2015-06-06 09:00:19 +02:00
|
|
|
BareJid key = userWithResource.asBareJid();
|
2015-02-14 17:15:02 +01:00
|
|
|
Resourcepart resource = userWithResource.getResourcepart();
|
2015-12-01 18:15:33 +01:00
|
|
|
Map<Resourcepart, Presence> userPresences = getPresencesInternal(key);
|
2003-11-28 22:49:25 +01:00
|
|
|
if (userPresences == null) {
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence presence = synthesizeUnvailablePresence(userWithResource);
|
2007-02-20 18:02:39 +01:00
|
|
|
return presence;
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2007-02-13 17:21:24 +01:00
|
|
|
Presence presence = userPresences.get(resource);
|
|
|
|
if (presence == null) {
|
2019-10-24 15:45:08 +02:00
|
|
|
presence = synthesizeUnvailablePresence(userWithResource);
|
2007-02-20 18:02:39 +01:00
|
|
|
return presence;
|
2007-02-13 17:21:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-06-14 17:38:51 +02:00
|
|
|
return presence;
|
2007-02-13 17:21:24 +01:00
|
|
|
}
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 19:33:14 +01:00
|
|
|
/**
|
|
|
|
* Returns a List of Presence objects for all of a user's current presences if no presence information is available,
|
|
|
|
* such as when you are not subscribed to the user's presence updates.
|
|
|
|
*
|
2015-02-12 12:13:19 +01:00
|
|
|
* @param bareJid an XMPP ID, e.g. jdoe@example.com.
|
2014-12-29 19:33:14 +01:00
|
|
|
* @return a List of Presence objects for all the user's current presences, or an unavailable presence if no
|
|
|
|
* presence information is available.
|
|
|
|
*/
|
2015-06-06 09:00:19 +02:00
|
|
|
public List<Presence> getAllPresences(BareJid bareJid) {
|
2015-12-01 18:15:33 +01:00
|
|
|
Map<Resourcepart, Presence> userPresences = getPresencesInternal(bareJid);
|
2014-12-29 19:33:14 +01:00
|
|
|
List<Presence> res;
|
|
|
|
if (userPresences == null) {
|
|
|
|
// Create an unavailable presence if none was found
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence unavailable = synthesizeUnvailablePresence(bareJid);
|
2014-12-29 19:33:14 +01:00
|
|
|
res = new ArrayList<>(Arrays.asList(unavailable));
|
|
|
|
} else {
|
2015-02-23 22:16:49 +01:00
|
|
|
res = new ArrayList<>(userPresences.values().size());
|
|
|
|
for (Presence presence : userPresences.values()) {
|
2020-06-14 17:38:51 +02:00
|
|
|
res.add(presence);
|
2015-02-23 22:16:49 +01:00
|
|
|
}
|
2014-12-29 19:33:14 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a List of all <b>available</b> Presence Objects for the given bare JID. If there are no available
|
|
|
|
* presences, then the empty list will be returned.
|
|
|
|
*
|
|
|
|
* @param bareJid the bare JID from which the presences should be retrieved.
|
|
|
|
* @return available presences for the bare JID.
|
|
|
|
*/
|
2015-06-06 09:00:19 +02:00
|
|
|
public List<Presence> getAvailablePresences(BareJid bareJid) {
|
2014-12-29 19:33:14 +01:00
|
|
|
List<Presence> allPresences = getAllPresences(bareJid);
|
|
|
|
List<Presence> res = new ArrayList<>(allPresences.size());
|
|
|
|
for (Presence presence : allPresences) {
|
|
|
|
if (presence.isAvailable()) {
|
2015-02-23 22:16:49 +01:00
|
|
|
// No need to clone presence here, getAllPresences already returns clones
|
2014-12-29 19:33:14 +01:00
|
|
|
res.add(presence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-11-28 22:49:25 +01:00
|
|
|
/**
|
2014-04-14 14:09:53 +02:00
|
|
|
* Returns a List of Presence objects for all of a user's current presences
|
2007-02-19 09:35:05 +01:00
|
|
|
* or an unavailable presence if the user is unavailable (offline) or if no presence
|
|
|
|
* information is available, such as when you are not subscribed to the user's presence
|
|
|
|
* updates.
|
2003-11-28 22:49:25 +01:00
|
|
|
*
|
2015-06-06 09:00:19 +02:00
|
|
|
* @param jid an XMPP ID, e.g. jdoe@example.com.
|
2014-04-14 14:09:53 +02:00
|
|
|
* @return a List of Presence objects for all the user's current presences,
|
2007-02-20 18:02:39 +01:00
|
|
|
* or an unavailable presence if the user is offline or if no presence information
|
|
|
|
* is available.
|
2003-11-28 22:49:25 +01:00
|
|
|
*/
|
2015-06-06 09:00:19 +02:00
|
|
|
public List<Presence> getPresences(BareJid jid) {
|
2014-04-14 14:09:53 +02:00
|
|
|
List<Presence> res;
|
2015-12-01 18:15:33 +01:00
|
|
|
Map<Resourcepart, Presence> userPresences = getPresencesInternal(jid);
|
2003-11-28 22:49:25 +01:00
|
|
|
if (userPresences == null) {
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence presence = synthesizeUnvailablePresence(jid);
|
2014-04-09 08:26:28 +02:00
|
|
|
res = Arrays.asList(presence);
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-12-13 23:10:11 +01:00
|
|
|
List<Presence> answer = new ArrayList<>();
|
2014-11-27 18:09:18 +01:00
|
|
|
// Used in case no available presence is found
|
|
|
|
Presence unavailable = null;
|
2008-11-21 05:54:59 +01:00
|
|
|
for (Presence presence : userPresences.values()) {
|
|
|
|
if (presence.isAvailable()) {
|
2020-06-14 17:38:51 +02:00
|
|
|
answer.add(presence);
|
2008-11-21 05:54:59 +01:00
|
|
|
}
|
2014-11-27 18:09:18 +01:00
|
|
|
else {
|
|
|
|
unavailable = presence;
|
|
|
|
}
|
2008-11-21 05:54:59 +01:00
|
|
|
}
|
|
|
|
if (!answer.isEmpty()) {
|
2014-04-09 08:26:28 +02:00
|
|
|
res = answer;
|
2008-11-21 05:54:59 +01:00
|
|
|
}
|
2014-11-27 18:09:18 +01:00
|
|
|
else if (unavailable != null) {
|
2020-06-14 17:38:51 +02:00
|
|
|
res = Arrays.asList(unavailable);
|
2014-11-27 18:09:18 +01:00
|
|
|
}
|
2008-11-21 05:54:59 +01:00
|
|
|
else {
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence presence = synthesizeUnvailablePresence(jid);
|
2014-04-09 08:26:28 +02:00
|
|
|
res = Arrays.asList(presence);
|
2008-11-21 05:54:59 +01:00
|
|
|
}
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
2015-02-23 22:16:49 +01:00
|
|
|
return res;
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
|
|
|
|
2015-01-19 16:14:14 +01:00
|
|
|
/**
|
|
|
|
* Check if the given JID is subscribed to the user's presence.
|
|
|
|
* <p>
|
|
|
|
* If the JID is subscribed to the user's presence then it is allowed to see the presence and
|
2015-01-27 09:45:50 +01:00
|
|
|
* will get notified about presence changes. Also returns true, if the JID is the service
|
|
|
|
* name of the XMPP connection (the "XMPP domain"), i.e. the XMPP service is treated like
|
|
|
|
* having an implicit subscription to the users presence.
|
2015-01-19 16:14:14 +01:00
|
|
|
* </p>
|
|
|
|
* Note that if the roster is not loaded, then this method will always return false.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param jid TODO javadoc me please
|
2015-01-19 16:14:14 +01:00
|
|
|
* @return true if the given JID is allowed to see the users presence.
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public boolean isSubscribedToMyPresence(Jid jid) {
|
2015-06-06 09:00:19 +02:00
|
|
|
if (jid == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BareJid bareJid = jid.asBareJid();
|
|
|
|
if (connection().getXMPPServiceDomain().equals(bareJid)) {
|
2015-01-27 09:45:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-06 09:00:19 +02:00
|
|
|
RosterEntry entry = getEntry(bareJid);
|
2015-01-19 16:14:14 +01:00
|
|
|
if (entry == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-20 20:57:04 +02:00
|
|
|
return entry.canSeeMyPresence();
|
2015-01-19 16:14:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 14:35:09 +01:00
|
|
|
/**
|
|
|
|
* Check if the XMPP entity this roster belongs to is subscribed to the presence of the given JID.
|
|
|
|
*
|
|
|
|
* @param jid the jid to check.
|
|
|
|
* @return <code>true</code> if we are subscribed to the presence of the given jid.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public boolean iAmSubscribedTo(Jid jid) {
|
|
|
|
if (jid == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BareJid bareJid = jid.asBareJid();
|
|
|
|
RosterEntry entry = getEntry(bareJid);
|
|
|
|
if (entry == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return entry.canSeeHisPresence();
|
|
|
|
}
|
|
|
|
|
2016-02-17 09:22:40 +01:00
|
|
|
/**
|
|
|
|
* Sets if the roster will be loaded from the server when logging in for newly created instances
|
|
|
|
* of {@link Roster}.
|
|
|
|
*
|
|
|
|
* @param rosterLoadedAtLoginDefault if the roster will be loaded from the server when logging in.
|
|
|
|
* @see #setRosterLoadedAtLogin(boolean)
|
|
|
|
* @since 4.1.7
|
|
|
|
*/
|
|
|
|
public static void setRosterLoadedAtLoginDefault(boolean rosterLoadedAtLoginDefault) {
|
|
|
|
Roster.rosterLoadedAtLoginDefault = rosterLoadedAtLoginDefault;
|
|
|
|
}
|
|
|
|
|
2015-01-22 13:53:50 +01:00
|
|
|
/**
|
|
|
|
* Sets if the roster will be loaded from the server when logging in. This
|
|
|
|
* is the common behaviour for clients but sometimes clients may want to differ this
|
|
|
|
* or just never do it if not interested in rosters.
|
|
|
|
*
|
|
|
|
* @param rosterLoadedAtLogin if the roster will be loaded from the server when logging in.
|
|
|
|
*/
|
|
|
|
public void setRosterLoadedAtLogin(boolean rosterLoadedAtLogin) {
|
|
|
|
this.rosterLoadedAtLogin = rosterLoadedAtLogin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the roster will be loaded from the server when logging in. This
|
|
|
|
* is the common behavior for clients but sometimes clients may want to differ this
|
|
|
|
* or just never do it if not interested in rosters.
|
|
|
|
*
|
|
|
|
* @return true if the roster will be loaded from the server when logging in.
|
|
|
|
* @see <a href="http://xmpp.org/rfcs/rfc6121.html#roster-login">RFC 6121 2.2 - Retrieving the Roster on Login</a>
|
|
|
|
*/
|
|
|
|
public boolean isRosterLoadedAtLogin() {
|
|
|
|
return rosterLoadedAtLogin;
|
|
|
|
}
|
|
|
|
|
|
|
|
RosterStore getRosterStore() {
|
|
|
|
return rosterStore;
|
|
|
|
}
|
|
|
|
|
2007-02-19 09:35:05 +01:00
|
|
|
/**
|
|
|
|
* Changes the presence of available contacts offline by simulating an unavailable
|
2017-01-03 10:48:11 +01:00
|
|
|
* presence sent from the server.
|
2007-02-19 09:35:05 +01:00
|
|
|
*/
|
2017-01-03 10:48:11 +01:00
|
|
|
private void setOfflinePresences() {
|
2015-02-14 17:15:02 +01:00
|
|
|
outerloop: for (Jid user : presenceMap.keySet()) {
|
|
|
|
Map<Resourcepart, Presence> resources = presenceMap.get(user);
|
2007-02-19 09:35:05 +01:00
|
|
|
if (resources != null) {
|
2015-02-14 17:15:02 +01:00
|
|
|
for (Resourcepart resource : resources.keySet()) {
|
2019-10-24 15:45:08 +02:00
|
|
|
PresenceBuilder presenceBuilder = StanzaBuilder.buildPresence()
|
|
|
|
.ofType(Presence.Type.unavailable);
|
2015-05-27 19:29:51 +02:00
|
|
|
EntityBareJid bareUserJid = user.asEntityBareJidIfPossible();
|
2015-02-14 17:15:02 +01:00
|
|
|
if (bareUserJid == null) {
|
|
|
|
LOGGER.warning("Can not transform user JID to bare JID: '" + user + "'");
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-24 15:45:08 +02:00
|
|
|
presenceBuilder.from(JidCreate.fullFrom(bareUserJid, resource));
|
2015-01-05 21:42:35 +01:00
|
|
|
try {
|
2019-10-24 15:45:08 +02:00
|
|
|
presencePacketListener.processStanza(presenceBuilder.build());
|
2015-01-05 21:42:35 +01:00
|
|
|
}
|
|
|
|
catch (NotConnectedException e) {
|
|
|
|
throw new IllegalStateException(
|
2017-12-13 23:10:11 +01:00
|
|
|
"presencePacketListener should never throw a NotConnectedException when processStanza is called with a presence of type unavailable",
|
2015-01-05 21:42:35 +01:00
|
|
|
e);
|
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException e) {
|
|
|
|
break outerloop;
|
|
|
|
}
|
2007-02-19 09:35:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-03 10:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the presence of available contacts offline by simulating an unavailable
|
|
|
|
* presence sent from the server. After a disconnection, every Presence is set
|
|
|
|
* to offline.
|
|
|
|
*/
|
|
|
|
private void setOfflinePresencesAndResetLoaded() {
|
|
|
|
setOfflinePresences();
|
2015-07-03 17:35:20 +02:00
|
|
|
rosterState = RosterState.uninitialized;
|
2007-02-19 09:35:05 +01:00
|
|
|
}
|
|
|
|
|
2003-04-13 06:51:37 +02:00
|
|
|
/**
|
2005-11-04 19:31:21 +01:00
|
|
|
* Fires roster changed event to roster listeners indicating that the
|
|
|
|
* specified collections of contacts have been added, updated or deleted
|
|
|
|
* from the roster.
|
|
|
|
*
|
2007-02-20 18:02:39 +01:00
|
|
|
* @param addedEntries the collection of address of the added contacts.
|
2005-11-04 19:31:21 +01:00
|
|
|
* @param updatedEntries the collection of address of the updated contacts.
|
|
|
|
* @param deletedEntries the collection of address of the deleted contacts.
|
2003-04-13 06:51:37 +02:00
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
private void fireRosterChangedEvent(final Collection<Jid> addedEntries, final Collection<Jid> updatedEntries,
|
|
|
|
final Collection<Jid> deletedEntries) {
|
2015-01-26 16:35:47 +01:00
|
|
|
synchronized (rosterListenersAndEntriesLock) {
|
|
|
|
for (RosterListener listener : rosterListeners) {
|
|
|
|
if (!addedEntries.isEmpty()) {
|
|
|
|
listener.entriesAdded(addedEntries);
|
|
|
|
}
|
|
|
|
if (!updatedEntries.isEmpty()) {
|
|
|
|
listener.entriesUpdated(updatedEntries);
|
|
|
|
}
|
|
|
|
if (!deletedEntries.isEmpty()) {
|
|
|
|
listener.entriesDeleted(deletedEntries);
|
|
|
|
}
|
2005-11-04 20:51:29 +01:00
|
|
|
}
|
2003-04-13 06:51:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-21 05:31:50 +02:00
|
|
|
/**
|
|
|
|
* Fires roster presence changed event to roster listeners.
|
2006-10-08 01:16:20 +02:00
|
|
|
*
|
2007-02-20 18:02:39 +01:00
|
|
|
* @param presence the presence change.
|
2003-08-21 05:31:50 +02:00
|
|
|
*/
|
2015-01-05 21:42:35 +01:00
|
|
|
private void fireRosterPresenceEvent(final Presence presence) {
|
2015-01-26 16:35:47 +01:00
|
|
|
synchronized (rosterListenersAndEntriesLock) {
|
|
|
|
for (RosterListener listener : rosterListeners) {
|
|
|
|
listener.presenceChanged(presence);
|
|
|
|
}
|
2003-08-21 05:31:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
private void addUpdateEntry(Collection<Jid> addedEntries, Collection<Jid> updatedEntries,
|
|
|
|
Collection<Jid> unchangedEntries, RosterPacket.Item item, RosterEntry entry) {
|
2015-01-26 16:35:47 +01:00
|
|
|
RosterEntry oldEntry;
|
|
|
|
synchronized (rosterListenersAndEntriesLock) {
|
2015-05-27 22:34:43 +02:00
|
|
|
oldEntry = entries.put(item.getJid(), entry);
|
2015-01-26 16:35:47 +01:00
|
|
|
}
|
2013-11-10 15:02:57 +01:00
|
|
|
if (oldEntry == null) {
|
2015-12-01 18:15:33 +01:00
|
|
|
BareJid jid = item.getJid();
|
|
|
|
addedEntries.add(jid);
|
|
|
|
// Move the eventually existing presences from nonRosterPresenceMap to presenceMap.
|
|
|
|
move(jid, nonRosterPresenceMap, presenceMap);
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
RosterPacket.Item oldItem = RosterEntry.toRosterItem(oldEntry);
|
|
|
|
if (!oldEntry.equalsDeep(entry) || !item.getGroupNames().equals(oldItem.getGroupNames())) {
|
2015-05-27 22:34:43 +02:00
|
|
|
updatedEntries.add(item.getJid());
|
2016-10-31 12:24:05 +01:00
|
|
|
oldEntry.updateItem(item);
|
2014-05-17 10:43:30 +02:00
|
|
|
} else {
|
|
|
|
// Record the entry as unchanged, so that it doesn't end up as deleted entry
|
2015-05-27 22:34:43 +02:00
|
|
|
unchangedEntries.add(item.getJid());
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the entry as unfiled if it does not belong to any groups.
|
|
|
|
if (item.getGroupNames().isEmpty()) {
|
2014-04-29 15:15:59 +02:00
|
|
|
unfiledEntries.add(entry);
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
unfiledEntries.remove(entry);
|
|
|
|
}
|
|
|
|
|
2015-01-26 16:24:32 +01:00
|
|
|
// Add the entry/user to the groups
|
2017-12-13 23:10:11 +01:00
|
|
|
List<String> newGroupNames = new ArrayList<>();
|
2013-11-10 15:02:57 +01:00
|
|
|
for (String groupName : item.getGroupNames()) {
|
|
|
|
// Add the group name to the list.
|
|
|
|
newGroupNames.add(groupName);
|
|
|
|
|
|
|
|
// Add the entry to the group.
|
|
|
|
RosterGroup group = getGroup(groupName);
|
|
|
|
if (group == null) {
|
|
|
|
group = createGroup(groupName);
|
|
|
|
groups.put(groupName, group);
|
|
|
|
}
|
|
|
|
// Add the entry.
|
|
|
|
group.addEntryLocal(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove user from the remaining groups.
|
2017-12-13 23:10:11 +01:00
|
|
|
List<String> oldGroupNames = new ArrayList<>();
|
2017-05-23 16:45:04 +02:00
|
|
|
for (RosterGroup group : getGroups()) {
|
2013-11-10 15:02:57 +01:00
|
|
|
oldGroupNames.add(group.getName());
|
|
|
|
}
|
|
|
|
oldGroupNames.removeAll(newGroupNames);
|
|
|
|
|
|
|
|
for (String groupName : oldGroupNames) {
|
|
|
|
RosterGroup group = getGroup(groupName);
|
|
|
|
group.removeEntryLocal(entry);
|
|
|
|
if (group.getEntryCount() == 0) {
|
|
|
|
groups.remove(groupName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
private void deleteEntry(Collection<Jid> deletedEntries, RosterEntry entry) {
|
2015-12-01 18:15:33 +01:00
|
|
|
BareJid user = entry.getJid();
|
2013-11-10 15:02:57 +01:00
|
|
|
entries.remove(user);
|
|
|
|
unfiledEntries.remove(entry);
|
2015-12-01 18:15:33 +01:00
|
|
|
// Move the presences from the presenceMap to the nonRosterPresenceMap.
|
|
|
|
move(user, presenceMap, nonRosterPresenceMap);
|
2013-11-10 15:02:57 +01:00
|
|
|
deletedEntries.add(user);
|
|
|
|
|
2021-01-28 22:05:47 +01:00
|
|
|
for (Map.Entry<String, RosterGroup> e : groups.entrySet()) {
|
2013-11-10 15:02:57 +01:00
|
|
|
RosterGroup group = e.getValue();
|
|
|
|
group.removeEntryLocal(entry);
|
|
|
|
if (group.getEntryCount() == 0) {
|
|
|
|
groups.remove(e.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 14:14:12 +01:00
|
|
|
/**
|
|
|
|
* Removes all the groups with no entries.
|
|
|
|
*
|
|
|
|
* This is used by {@link RosterPushListener} and {@link RosterResultListener} to
|
|
|
|
* cleanup groups after removing contacts.
|
|
|
|
*/
|
|
|
|
private void removeEmptyGroups() {
|
|
|
|
// We have to do this because RosterGroup.removeEntry removes the entry immediately
|
|
|
|
// (locally) and the group could remain empty.
|
|
|
|
// TODO Check the performance/logic for rosters with large number of groups
|
|
|
|
for (RosterGroup group : getGroups()) {
|
|
|
|
if (group.getEntryCount() == 0) {
|
|
|
|
groups.remove(group.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:15:33 +01:00
|
|
|
/**
|
|
|
|
* Move presences from 'entity' from one presence map to another.
|
|
|
|
*
|
|
|
|
* @param entity the entity
|
|
|
|
* @param from the map to move presences from
|
|
|
|
* @param to the map to move presences to
|
|
|
|
*/
|
|
|
|
private static void move(BareJid entity, Map<BareJid, Map<Resourcepart, Presence>> from, Map<BareJid, Map<Resourcepart, Presence>> to) {
|
|
|
|
Map<Resourcepart, Presence> presences = from.remove(entity);
|
|
|
|
if (presences != null && !presences.isEmpty()) {
|
|
|
|
to.put(entity, presences);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 14:14:12 +01:00
|
|
|
/**
|
|
|
|
* Ignore ItemTypes as of RFC 6121, 2.1.2.5.
|
|
|
|
*
|
|
|
|
* This is used by {@link RosterPushListener} and {@link RosterResultListener}.
|
|
|
|
* */
|
2014-05-17 09:45:20 +02:00
|
|
|
private static boolean hasValidSubscriptionType(RosterPacket.Item item) {
|
2015-01-05 23:53:00 +01:00
|
|
|
switch (item.getItemType()) {
|
|
|
|
case none:
|
|
|
|
case from:
|
|
|
|
case to:
|
|
|
|
case both:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-04 14:14:12 +01:00
|
|
|
}
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
private static Presence synthesizeUnvailablePresence(Jid from) {
|
|
|
|
return StanzaBuilder.buildPresence()
|
|
|
|
.ofType(Presence.Type.unavailable)
|
|
|
|
.from(from)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:04:58 +01:00
|
|
|
/**
|
|
|
|
* Check if the server supports roster versioning.
|
|
|
|
*
|
|
|
|
* @return true if the server supports roster versioning, false otherwise.
|
|
|
|
*/
|
|
|
|
public boolean isRosterVersioningSupported() {
|
2015-01-22 13:53:50 +01:00
|
|
|
return connection().hasFeature(RosterVer.ELEMENT, RosterVer.NAMESPACE);
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
2006-07-18 08:47:38 +02:00
|
|
|
/**
|
|
|
|
* An enumeration for the subscription mode options.
|
|
|
|
*/
|
|
|
|
public enum SubscriptionMode {
|
|
|
|
|
|
|
|
/**
|
2017-08-26 19:16:07 +02:00
|
|
|
* Automatically accept all subscription and unsubscription requests.
|
|
|
|
* This is suitable for simple clients. More complex clients will
|
2006-07-18 08:47:38 +02:00
|
|
|
* likely wish to handle subscription requests manually.
|
2007-02-20 18:02:39 +01:00
|
|
|
*/
|
2006-07-18 08:47:38 +02:00
|
|
|
accept_all,
|
|
|
|
|
|
|
|
/**
|
2017-08-26 19:16:07 +02:00
|
|
|
* Automatically reject all subscription requests. This is the default mode.
|
2006-07-18 08:47:38 +02:00
|
|
|
*/
|
|
|
|
reject_all,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscription requests are ignored, which means they must be manually
|
|
|
|
* processed by registering a listener for presence packets and then looking
|
|
|
|
* for any presence requests that have the type Presence.Type.SUBSCRIBE or
|
|
|
|
* Presence.Type.UNSUBSCRIBE.
|
|
|
|
*/
|
|
|
|
manual
|
|
|
|
}
|
|
|
|
|
2003-04-14 06:10:47 +02:00
|
|
|
/**
|
|
|
|
* Listens for all presence packets and processes them.
|
|
|
|
*/
|
2015-03-01 10:28:15 +01:00
|
|
|
private class PresencePacketListener implements StanzaListener {
|
2007-02-20 18:02:39 +01:00
|
|
|
|
2015-01-23 15:40:40 +01:00
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public void processStanza(Stanza packet) throws NotConnectedException, InterruptedException {
|
2015-07-03 14:24:13 +02:00
|
|
|
// Try to ensure that the roster is loaded when processing presence stanzas. While the
|
|
|
|
// presence listener is synchronous, the roster result listener is not, which means that
|
|
|
|
// the presence listener may be invoked with a not yet loaded roster.
|
2015-07-03 17:35:20 +02:00
|
|
|
if (rosterState == RosterState.loading) {
|
|
|
|
try {
|
|
|
|
waitUntilLoaded();
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
LOGGER.log(Level.INFO, "Presence listener was interrupted", e);
|
|
|
|
|
|
|
|
}
|
2015-07-03 14:42:45 +02:00
|
|
|
}
|
2015-09-07 09:31:51 +02:00
|
|
|
if (!isLoaded() && rosterLoadedAtLogin) {
|
2016-05-25 22:53:44 +02:00
|
|
|
LOGGER.warning("Roster not loaded while processing " + packet);
|
2015-07-03 14:24:13 +02:00
|
|
|
}
|
2018-04-08 21:21:50 +02:00
|
|
|
final Presence presence = (Presence) packet;
|
|
|
|
final Jid from = presence.getFrom();
|
|
|
|
|
2019-04-01 22:02:36 +02:00
|
|
|
final BareJid key;
|
|
|
|
if (from != null) {
|
|
|
|
key = from.asBareJid();
|
|
|
|
} else {
|
|
|
|
XMPPConnection connection = connection();
|
|
|
|
if (connection == null) {
|
|
|
|
LOGGER.finest("Connection was null while trying to handle exotic presence stanza: " + presence);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Assume the presence come "from the users account on the server" since no from was set (RFC 6120 §
|
|
|
|
// 8.1.2.1 4.). Note that getUser() may return null, but should never return null in this case as where
|
|
|
|
// connected.
|
|
|
|
EntityFullJid myJid = connection.getUser();
|
|
|
|
if (myJid == null) {
|
|
|
|
LOGGER.info(
|
|
|
|
"Connection had no local address in Roster's presence listener."
|
|
|
|
+ " Possibly we received a presence without from before being authenticated."
|
|
|
|
+ " Presence: " + presence);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOGGER.info("Exotic presence stanza without from received: " + presence);
|
|
|
|
key = myJid.asBareJid();
|
|
|
|
}
|
2018-04-08 21:21:50 +02:00
|
|
|
|
|
|
|
asyncButOrdered.performAsyncButOrdered(key, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Resourcepart fromResource = Resourcepart.EMPTY;
|
|
|
|
BareJid bareFrom = null;
|
|
|
|
FullJid fullFrom = null;
|
|
|
|
if (from != null) {
|
|
|
|
fromResource = from.getResourceOrNull();
|
|
|
|
if (fromResource == null) {
|
|
|
|
fromResource = Resourcepart.EMPTY;
|
|
|
|
bareFrom = from.asBareJid();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fullFrom = from.asFullJidIfPossible();
|
|
|
|
// We know that this must be a full JID in this case.
|
2019-07-24 09:18:39 +02:00
|
|
|
assert fullFrom != null;
|
2018-04-08 21:21:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Map<Resourcepart, Presence> userPresences;
|
|
|
|
// If an "available" presence, add it to the presence map. Each presence
|
|
|
|
// map will hold for a particular user a map with the presence
|
|
|
|
// packets saved for each resource.
|
|
|
|
switch (presence.getType()) {
|
|
|
|
case available:
|
|
|
|
// Get the user presence map
|
|
|
|
userPresences = getOrCreatePresencesInternal(key);
|
|
|
|
// See if an offline presence was being stored in the map. If so, remove
|
|
|
|
// it since we now have an online presence.
|
|
|
|
userPresences.remove(Resourcepart.EMPTY);
|
|
|
|
// Add the new presence, using the resources as a key.
|
|
|
|
userPresences.put(fromResource, presence);
|
|
|
|
// If the user is in the roster, fire an event.
|
|
|
|
if (contains(key)) {
|
|
|
|
fireRosterPresenceEvent(presence);
|
|
|
|
}
|
|
|
|
for (PresenceEventListener presenceEventListener : presenceEventListeners) {
|
|
|
|
presenceEventListener.presenceAvailable(fullFrom, presence);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// If an "unavailable" packet.
|
|
|
|
case unavailable:
|
|
|
|
// If no resource, this is likely an offline presence as part of
|
|
|
|
// a roster presence flood. In that case, we store it.
|
|
|
|
userPresences = getOrCreatePresencesInternal(key);
|
|
|
|
if (from.hasNoResource()) {
|
|
|
|
// Get the user presence map
|
|
|
|
userPresences.put(Resourcepart.EMPTY, presence);
|
|
|
|
}
|
|
|
|
// Otherwise, this is a normal offline presence.
|
|
|
|
else {
|
|
|
|
// Store the offline presence, as it may include extra information
|
|
|
|
// such as the user being on vacation.
|
|
|
|
userPresences.put(fromResource, presence);
|
|
|
|
}
|
|
|
|
// If the user is in the roster, fire an event.
|
|
|
|
if (contains(key)) {
|
|
|
|
fireRosterPresenceEvent(presence);
|
|
|
|
}
|
2016-06-30 17:01:46 +02:00
|
|
|
|
2018-04-08 21:21:50 +02:00
|
|
|
// Ensure that 'from' is a full JID before invoking the presence unavailable
|
|
|
|
// listeners. Usually unavailable presences always have a resourcepart, i.e. are
|
|
|
|
// full JIDs, but RFC 6121 § 4.5.4 has an implementation note that unavailable
|
|
|
|
// presences from a bare JID SHOULD be treated as applying to all resources. I don't
|
|
|
|
// think any client or server ever implemented that, I do think that this
|
|
|
|
// implementation note is a terrible idea since it adds another corner case in
|
|
|
|
// client code, instead of just having the invariant
|
|
|
|
// "unavailable presences are always from the full JID".
|
|
|
|
if (fullFrom != null) {
|
|
|
|
for (PresenceEventListener presenceEventListener : presenceEventListeners) {
|
|
|
|
presenceEventListener.presenceUnavailable(fullFrom, presence);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOGGER.fine("Unavailable presence from bare JID: " + presence);
|
|
|
|
}
|
2016-06-30 17:01:46 +02:00
|
|
|
|
2018-04-08 21:21:50 +02:00
|
|
|
break;
|
|
|
|
// Error presence packets from a bare JID mean we invalidate all existing
|
|
|
|
// presence info for the user.
|
|
|
|
case error:
|
|
|
|
// No need to act on error presences send without from, i.e.
|
|
|
|
// directly send from the users XMPP service, or where the from
|
|
|
|
// address is not a bare JID
|
|
|
|
if (from == null || !from.isEntityBareJid()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
userPresences = getOrCreatePresencesInternal(key);
|
|
|
|
// Any other presence data is invalidated by the error packet.
|
|
|
|
userPresences.clear();
|
|
|
|
|
|
|
|
// Set the new presence using the empty resource as a key.
|
|
|
|
userPresences.put(Resourcepart.EMPTY, presence);
|
|
|
|
// If the user is in the roster, fire an event.
|
|
|
|
if (contains(key)) {
|
|
|
|
fireRosterPresenceEvent(presence);
|
|
|
|
}
|
|
|
|
for (PresenceEventListener presenceEventListener : presenceEventListeners) {
|
|
|
|
presenceEventListener.presenceError(from, presence);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case subscribed:
|
|
|
|
for (PresenceEventListener presenceEventListener : presenceEventListeners) {
|
|
|
|
presenceEventListener.presenceSubscribed(bareFrom, presence);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case unsubscribed:
|
|
|
|
for (PresenceEventListener presenceEventListener : presenceEventListeners) {
|
|
|
|
presenceEventListener.presenceUnsubscribed(bareFrom, presence);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-06-30 17:01:46 +02:00
|
|
|
}
|
2014-12-22 16:12:34 +01:00
|
|
|
}
|
2018-04-08 21:21:50 +02:00
|
|
|
});
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
2003-04-14 06:10:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-10 15:02:57 +01:00
|
|
|
/**
|
2016-10-03 13:31:37 +02:00
|
|
|
* Handles Roster results as described in <a href="https://tools.ietf.org/html/rfc6121#section-2.1.4">RFC 6121 2.1.4</a>.
|
2013-11-10 15:02:57 +01:00
|
|
|
*/
|
2017-08-16 13:46:42 +02:00
|
|
|
private class RosterResultListener implements SuccessCallback<IQ> {
|
2013-11-10 15:02:57 +01:00
|
|
|
|
|
|
|
@Override
|
2017-08-16 13:46:42 +02:00
|
|
|
public void onSuccess(IQ packet) {
|
2015-01-22 13:53:50 +01:00
|
|
|
final XMPPConnection connection = connection();
|
2019-08-22 10:50:03 +02:00
|
|
|
LOGGER.log(Level.FINE, "RosterResultListener received {0}", packet);
|
2015-02-14 17:15:02 +01:00
|
|
|
Collection<Jid> addedEntries = new ArrayList<>();
|
|
|
|
Collection<Jid> updatedEntries = new ArrayList<>();
|
|
|
|
Collection<Jid> deletedEntries = new ArrayList<>();
|
|
|
|
Collection<Jid> unchangedEntries = new ArrayList<>();
|
2014-03-04 14:14:12 +01:00
|
|
|
|
2014-04-29 14:12:49 +02:00
|
|
|
if (packet instanceof RosterPacket) {
|
2014-05-17 10:43:30 +02:00
|
|
|
// Non-empty roster result. This stanza contains all the roster elements.
|
|
|
|
RosterPacket rosterPacket = (RosterPacket) packet;
|
2014-03-04 14:14:12 +01:00
|
|
|
|
2014-05-17 10:43:30 +02:00
|
|
|
// Ignore items without valid subscription type
|
2017-12-13 23:10:11 +01:00
|
|
|
ArrayList<Item> validItems = new ArrayList<>();
|
2014-05-17 10:43:30 +02:00
|
|
|
for (RosterPacket.Item item : rosterPacket.getRosterItems()) {
|
|
|
|
if (hasValidSubscriptionType(item)) {
|
|
|
|
validItems.add(item);
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 14:14:12 +01:00
|
|
|
|
2014-05-17 10:43:30 +02:00
|
|
|
for (RosterPacket.Item item : validItems) {
|
2015-09-29 22:35:50 +02:00
|
|
|
RosterEntry entry = new RosterEntry(item, Roster.this, connection);
|
2014-05-17 10:43:30 +02:00
|
|
|
addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
|
|
|
|
}
|
2014-03-04 14:14:12 +01:00
|
|
|
|
2014-05-17 10:43:30 +02:00
|
|
|
// Delete all entries which where not added or updated
|
2015-02-14 17:15:02 +01:00
|
|
|
Set<Jid> toDelete = new HashSet<>();
|
2014-05-17 10:43:30 +02:00
|
|
|
for (RosterEntry entry : entries.values()) {
|
2015-05-27 22:34:43 +02:00
|
|
|
toDelete.add(entry.getJid());
|
2014-05-17 10:43:30 +02:00
|
|
|
}
|
|
|
|
toDelete.removeAll(addedEntries);
|
|
|
|
toDelete.removeAll(updatedEntries);
|
|
|
|
toDelete.removeAll(unchangedEntries);
|
2015-02-14 17:15:02 +01:00
|
|
|
for (Jid user : toDelete) {
|
2014-05-17 10:43:30 +02:00
|
|
|
deleteEntry(deletedEntries, entries.get(user));
|
|
|
|
}
|
2014-03-04 14:14:12 +01:00
|
|
|
|
2014-05-17 10:43:30 +02:00
|
|
|
if (rosterStore != null) {
|
2015-01-06 09:42:16 +01:00
|
|
|
String version = rosterPacket.getVersion();
|
2014-05-17 10:43:30 +02:00
|
|
|
rosterStore.resetEntries(validItems, version);
|
|
|
|
}
|
2014-03-04 14:14:12 +01:00
|
|
|
|
2014-05-17 10:43:30 +02:00
|
|
|
removeEmptyGroups();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Empty roster result as defined in RFC6121 2.6.3. An empty roster result basically
|
|
|
|
// means that rosterver was used and the roster hasn't changed (much) since the
|
|
|
|
// version we presented the server. So we simply load the roster from the store and
|
|
|
|
// await possible further roster pushes.
|
2015-09-30 10:29:01 +02:00
|
|
|
List<RosterPacket.Item> storedItems = rosterStore.getEntries();
|
|
|
|
if (storedItems == null) {
|
|
|
|
// The roster store was corrupted. Reset the store and reload the roster without using a roster version.
|
|
|
|
rosterStore.resetStore();
|
|
|
|
try {
|
|
|
|
reload();
|
|
|
|
} catch (NotLoggedInException | NotConnectedException
|
|
|
|
| InterruptedException e) {
|
|
|
|
LOGGER.log(Level.FINE,
|
|
|
|
"Exception while trying to load the roster after the roster store was corrupted",
|
|
|
|
e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (RosterPacket.Item item : storedItems) {
|
2015-09-29 22:35:50 +02:00
|
|
|
RosterEntry entry = new RosterEntry(item, Roster.this, connection);
|
2014-05-17 10:43:30 +02:00
|
|
|
addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 14:14:12 +01:00
|
|
|
|
2015-07-03 17:35:20 +02:00
|
|
|
rosterState = RosterState.loaded;
|
2014-05-17 10:43:30 +02:00
|
|
|
synchronized (Roster.this) {
|
|
|
|
Roster.this.notifyAll();
|
|
|
|
}
|
|
|
|
// Fire event for roster listeners.
|
|
|
|
fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);
|
2015-02-17 15:24:33 +01:00
|
|
|
|
|
|
|
// Call the roster loaded listeners after the roster events have been fired. This is
|
2017-12-13 23:10:11 +01:00
|
|
|
// important because the user may call getEntriesAndAddListener() in onRosterLoaded(),
|
2015-02-17 15:24:33 +01:00
|
|
|
// and if the order would be the other way around, the roster listener added by
|
|
|
|
// getEntriesAndAddListener() would be invoked with information that was already
|
2017-12-13 23:10:11 +01:00
|
|
|
// available at the time getEntriesAndAddListener() was called.
|
2015-02-17 15:24:33 +01:00
|
|
|
try {
|
|
|
|
synchronized (rosterLoadedListeners) {
|
|
|
|
for (RosterLoadedListener rosterLoadedListener : rosterLoadedListeners) {
|
|
|
|
rosterLoadedListener.onRosterLoaded(Roster.this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
LOGGER.log(Level.WARNING, "RosterLoadedListener threw exception", e);
|
|
|
|
}
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
2014-03-04 14:14:12 +01:00
|
|
|
* Listens for all roster pushes and processes them.
|
2003-02-10 06:01:01 +01:00
|
|
|
*/
|
2015-04-06 10:45:12 +02:00
|
|
|
private final class RosterPushListener extends AbstractIqRequestHandler {
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2015-01-08 11:01:35 +01:00
|
|
|
private RosterPushListener() {
|
2021-01-28 22:05:47 +01:00
|
|
|
super(RosterPacket.ELEMENT, RosterPacket.NAMESPACE, IQ.Type.set, Mode.sync);
|
2015-01-08 11:01:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IQ handleIQRequest(IQ iqRequest) {
|
2015-01-22 13:53:50 +01:00
|
|
|
final XMPPConnection connection = connection();
|
2015-01-08 11:01:35 +01:00
|
|
|
RosterPacket rosterPacket = (RosterPacket) iqRequest;
|
2005-11-04 19:31:21 +01:00
|
|
|
|
2017-07-02 22:11:14 +02:00
|
|
|
EntityFullJid ourFullJid = connection.getUser();
|
|
|
|
if (ourFullJid == null) {
|
2017-03-07 08:50:22 +01:00
|
|
|
LOGGER.warning("Ignoring roster push " + iqRequest + " while " + connection
|
|
|
|
+ " has no bound resource. This may be a server bug.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-03-04 14:14:12 +01:00
|
|
|
// Roster push (RFC 6121, 2.1.6)
|
|
|
|
// A roster push with a non-empty from not matching our address MUST be ignored
|
2017-07-02 22:11:14 +02:00
|
|
|
EntityBareJid ourBareJid = ourFullJid.asEntityBareJid();
|
2015-02-14 17:15:02 +01:00
|
|
|
Jid from = rosterPacket.getFrom();
|
2017-07-02 22:11:14 +02:00
|
|
|
if (from != null) {
|
|
|
|
if (from.equals(ourFullJid)) {
|
|
|
|
// Since RFC 6121 roster pushes are no longer allowed to
|
|
|
|
// origin from the full JID as it was the case with RFC
|
|
|
|
// 3921. Log a warning an continue processing the push.
|
|
|
|
// See also SMACK-773.
|
|
|
|
LOGGER.warning(
|
|
|
|
"Received roster push from full JID. This behavior is since RFC 6121 not longer standard compliant. "
|
|
|
|
+ "Please ask your server vendor to fix this and comply to RFC 6121 § 2.1.6. IQ roster push stanza: "
|
|
|
|
+ iqRequest);
|
|
|
|
} else if (!from.equals(ourBareJid)) {
|
|
|
|
LOGGER.warning("Ignoring roster push with a non matching 'from' ourJid='" + ourBareJid + "' from='"
|
|
|
|
+ from + "'");
|
|
|
|
return IQ.createErrorResponse(iqRequest, Condition.service_unavailable);
|
|
|
|
}
|
2014-03-04 14:14:12 +01:00
|
|
|
}
|
2003-10-03 21:30:45 +02:00
|
|
|
|
2014-03-04 14:14:12 +01:00
|
|
|
// A roster push must contain exactly one entry
|
|
|
|
Collection<Item> items = rosterPacket.getRosterItems();
|
|
|
|
if (items.size() != 1) {
|
2017-12-13 23:10:11 +01:00
|
|
|
LOGGER.warning("Ignoring roster push with not exactly one entry. size=" + items.size());
|
2015-12-08 08:22:50 +01:00
|
|
|
return IQ.createErrorResponse(iqRequest, Condition.bad_request);
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
Collection<Jid> addedEntries = new ArrayList<>();
|
|
|
|
Collection<Jid> updatedEntries = new ArrayList<>();
|
|
|
|
Collection<Jid> deletedEntries = new ArrayList<>();
|
|
|
|
Collection<Jid> unchangedEntries = new ArrayList<>();
|
2003-09-24 20:07:02 +02:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
// We assured above that the size of items is exactly 1, therefore we are able to
|
2014-05-17 10:43:30 +02:00
|
|
|
// safely retrieve this single item here.
|
2014-03-04 14:14:12 +01:00
|
|
|
Item item = items.iterator().next();
|
2015-09-29 22:35:50 +02:00
|
|
|
RosterEntry entry = new RosterEntry(item, Roster.this, connection);
|
2015-01-06 09:42:16 +01:00
|
|
|
String version = rosterPacket.getVersion();
|
2013-11-10 15:02:57 +01:00
|
|
|
|
|
|
|
if (item.getItemType().equals(RosterPacket.ItemType.remove)) {
|
|
|
|
deleteEntry(deletedEntries, entry);
|
|
|
|
if (rosterStore != null) {
|
2015-05-27 22:34:43 +02:00
|
|
|
rosterStore.removeEntry(entry.getJid(), version);
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (hasValidSubscriptionType(item)) {
|
2014-05-17 10:43:30 +02:00
|
|
|
addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
|
2013-11-10 15:02:57 +01:00
|
|
|
if (rosterStore != null) {
|
|
|
|
rosterStore.addEntry(item, version);
|
|
|
|
}
|
|
|
|
}
|
2014-05-17 10:43:30 +02:00
|
|
|
|
|
|
|
removeEmptyGroups();
|
|
|
|
|
|
|
|
// Fire event for roster listeners.
|
|
|
|
fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);
|
2015-01-08 11:01:35 +01:00
|
|
|
|
|
|
|
return IQ.createResultIQ(rosterPacket);
|
2013-11-10 15:02:57 +01:00
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2015-12-01 18:15:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default maximum size of the non-Roster presence map.
|
|
|
|
* <p>
|
|
|
|
* The roster will only store this many presence entries for entities non in the Roster. The
|
|
|
|
* default is {@value #INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE}.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param maximumSize the maximum size
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public static void setDefaultNonRosterPresenceMapMaxSize(int maximumSize) {
|
|
|
|
defaultNonRosterPresenceMapMaxSize = maximumSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the maximum size of the non-Roster presence map.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param maximumSize TODO javadoc me please
|
2015-12-01 18:15:33 +01:00
|
|
|
* @since 4.2
|
|
|
|
* @see #setDefaultNonRosterPresenceMapMaxSize(int)
|
|
|
|
*/
|
|
|
|
public void setNonRosterPresenceMapMaxSize(int maximumSize) {
|
|
|
|
nonRosterPresenceMap.setMaxCacheSize(maximumSize);
|
|
|
|
}
|
2016-06-30 17:01:46 +02:00
|
|
|
|
2008-11-19 21:54:05 +01:00
|
|
|
}
|