2003-01-16 16:42:18 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-16 16:42:18 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketIDFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
|
import org.jivesoftware.smack.packet.RosterPacket;
|
2003-04-14 06:10:47 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2003-01-16 16:42:18 +01:00
|
|
|
import java.util.*;
|
2006-07-17 10:39:08 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2006-10-08 01:16:20 +02:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
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
|
2003-04-25 22:12:17 +02:00
|
|
|
* presence updates for. Roster items are categorized into groups for easier management.<p>
|
2007-02-20 18:02:39 +01:00
|
|
|
* <p/>
|
2003-06-17 19:20:30 +02:00
|
|
|
* Others users may attempt to subscribe to this user using a subscription request. Three
|
|
|
|
* 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
|
2010-02-09 12:55:56 +01:00
|
|
|
* @see Connection#getRoster()
|
2003-01-16 16:42:18 +01:00
|
|
|
*/
|
2007-02-19 09:35:05 +01:00
|
|
|
public class Roster {
|
2003-01-16 16:42:18 +01:00
|
|
|
|
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
|
|
|
|
* all subscription requests are automatically accepted.
|
2004-01-18 15:17:13 +01:00
|
|
|
*/
|
2006-07-18 08:47:38 +02:00
|
|
|
private static SubscriptionMode defaultSubscriptionMode = SubscriptionMode.accept_all;
|
2004-01-18 15:17:13 +01:00
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private Connection connection;
|
2006-07-18 07:14:33 +02:00
|
|
|
private final Map<String, RosterGroup> groups;
|
2007-12-27 16:54:56 +01:00
|
|
|
private final Map<String,RosterEntry> entries;
|
2006-07-18 07:14:33 +02:00
|
|
|
private final List<RosterEntry> unfiledEntries;
|
|
|
|
private final List<RosterListener> rosterListeners;
|
|
|
|
private Map<String, Map<String, Presence>> presenceMap;
|
2003-04-07 07:40:28 +02:00
|
|
|
// The roster is marked as initialized when at least a single roster packet
|
|
|
|
// has been recieved and processed.
|
|
|
|
boolean rosterInitialized = false;
|
2007-02-19 09:35:05 +01:00
|
|
|
private PresencePacketListener presencePacketListener;
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2006-07-18 08:47:38 +02:00
|
|
|
private SubscriptionMode subscriptionMode = getDefaultSubscriptionMode();
|
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
|
2006-07-18 08:47:38 +02:00
|
|
|
* is {@link SubscriptionMode#accept_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
|
2006-07-18 08:47:38 +02:00
|
|
|
* is {@link SubscriptionMode#accept_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
|
|
|
|
2003-04-13 06:51:37 +02:00
|
|
|
/**
|
|
|
|
* Creates a new roster.
|
|
|
|
*
|
|
|
|
* @param connection an XMPP connection.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
Roster(final Connection connection) {
|
2003-02-10 06:01:01 +01:00
|
|
|
this.connection = connection;
|
2007-02-20 18:02:39 +01:00
|
|
|
groups = new ConcurrentHashMap<String, RosterGroup>();
|
2006-10-08 01:16:20 +02:00
|
|
|
unfiledEntries = new CopyOnWriteArrayList<RosterEntry>();
|
2007-12-27 16:54:56 +01:00
|
|
|
entries = new ConcurrentHashMap<String,RosterEntry>();
|
2006-10-08 01:16:20 +02:00
|
|
|
rosterListeners = new CopyOnWriteArrayList<RosterListener>();
|
|
|
|
presenceMap = new ConcurrentHashMap<String, Map<String, Presence>>();
|
2003-02-10 06:01:01 +01:00
|
|
|
// Listen for any roster packets.
|
2003-04-14 06:10:47 +02:00
|
|
|
PacketFilter rosterFilter = new PacketTypeFilter(RosterPacket.class);
|
|
|
|
connection.addPacketListener(new RosterPacketListener(), rosterFilter);
|
|
|
|
// Listen for any presence packets.
|
2003-04-14 06:18:15 +02:00
|
|
|
PacketFilter presenceFilter = new PacketTypeFilter(Presence.class);
|
2007-02-19 09:35:05 +01:00
|
|
|
presencePacketListener = new PresencePacketListener();
|
|
|
|
connection.addPacketListener(presencePacketListener, presenceFilter);
|
2006-09-14 21:14:51 +02:00
|
|
|
// Listen for connection events
|
2007-02-19 09:35:05 +01:00
|
|
|
connection.addConnectionListener(new ConnectionListener() {
|
|
|
|
public void connectionClosed() {
|
|
|
|
// Changes the presence available contacts to unavailable
|
|
|
|
setOfflinePresences();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void connectionClosedOnError(Exception e) {
|
|
|
|
// Changes the presence available contacts to unavailable
|
|
|
|
setOfflinePresences();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectingIn(int seconds) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionFailed(Exception e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionSuccessful() {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
});
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2006-07-18 08:47:38 +02:00
|
|
|
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
|
2007-02-20 18:02:39 +01: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}.
|
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.
|
2006-07-18 08:47:38 +02:00
|
|
|
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
|
2007-02-20 18:02:39 +01: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}.
|
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.
|
|
|
|
*/
|
|
|
|
public void reload() {
|
|
|
|
connection.sendPacket(new RosterPacket());
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2003-05-21 15:21:24 +02:00
|
|
|
public void addRosterListener(RosterListener rosterListener) {
|
2006-10-08 01:16:20 +02:00
|
|
|
if (!rosterListeners.contains(rosterListener)) {
|
2007-02-20 18:02:39 +01:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public void removeRosterListener(RosterListener rosterListener) {
|
2006-10-08 01:16:20 +02:00
|
|
|
rosterListeners.remove(rosterListener);
|
2003-04-13 06:51:37 +02:00
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
2003-08-21 05:31:50 +02:00
|
|
|
* Creates a new group.<p>
|
2007-02-20 18:02:39 +01:00
|
|
|
* <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.
|
2003-02-10 06:01:01 +01:00
|
|
|
*
|
|
|
|
* @param name the name of the group.
|
|
|
|
* @return a new group.
|
|
|
|
*/
|
|
|
|
public RosterGroup createGroup(String name) {
|
2006-10-08 01:16:20 +02:00
|
|
|
if (groups.containsKey(name)) {
|
|
|
|
throw new IllegalArgumentException("Group with name " + name + " alread exists.");
|
2003-02-10 06:01:01 +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.
|
2003-08-21 05:31:50 +02:00
|
|
|
* @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
|
2007-02-20 18:02:39 +01:00
|
|
|
* the roster entry won't belong to a group.
|
2006-10-08 01:16:20 +02:00
|
|
|
* @throws XMPPException if an XMPP exception occurs.
|
2003-03-24 00:03:19 +01:00
|
|
|
*/
|
2007-02-20 18:02:39 +01:00
|
|
|
public void createEntry(String user, String name, String[] groups) throws XMPPException {
|
2003-04-25 22:12:17 +02:00
|
|
|
// Create and send roster entry creation packet.
|
|
|
|
RosterPacket rosterPacket = new RosterPacket();
|
|
|
|
rosterPacket.setType(IQ.Type.SET);
|
|
|
|
RosterPacket.Item item = new RosterPacket.Item(user, name);
|
|
|
|
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);
|
2003-12-20 13:20:30 +01:00
|
|
|
// Wait up to a certain number of seconds for a reply from the server.
|
2003-04-25 22:12:17 +02:00
|
|
|
PacketCollector collector = connection.createPacketCollector(
|
|
|
|
new PacketIDFilter(rosterPacket.getPacketID()));
|
|
|
|
connection.sendPacket(rosterPacket);
|
2007-02-20 18:02:39 +01:00
|
|
|
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
2005-03-30 03:53:51 +02:00
|
|
|
collector.cancel();
|
2003-04-25 22:12:17 +02:00
|
|
|
if (response == null) {
|
|
|
|
throw new XMPPException("No response from the server.");
|
|
|
|
}
|
|
|
|
// If the server replied with an error, throw an exception.
|
|
|
|
else if (response.getType() == IQ.Type.ERROR) {
|
|
|
|
throw new XMPPException(response.getError());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a presence subscription packet and send.
|
2006-07-17 10:39:08 +02:00
|
|
|
Presence presencePacket = new Presence(Presence.Type.subscribe);
|
2003-04-25 22:12:17 +02:00
|
|
|
presencePacket.setTo(user);
|
|
|
|
connection.sendPacket(presencePacket);
|
|
|
|
}
|
|
|
|
|
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
|
2005-03-30 03:53:51 +02:00
|
|
|
* of the roster. Note that this is an asynchronous call -- Smack must wait for the server
|
|
|
|
* to send an updated subscription status.
|
2003-09-24 20:07:02 +02:00
|
|
|
*
|
|
|
|
* @param entry a roster entry.
|
2006-10-08 01:16:20 +02:00
|
|
|
* @throws XMPPException if an XMPP error occurs.
|
2003-09-24 20:07:02 +02:00
|
|
|
*/
|
2005-03-30 03:53:51 +02:00
|
|
|
public void removeEntry(RosterEntry entry) throws XMPPException {
|
2003-09-24 20:07:02 +02:00
|
|
|
// Only remove the entry if it's in the entry list.
|
|
|
|
// The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
|
2007-12-27 16:54:56 +01:00
|
|
|
if (!entries.containsKey(entry.getUser())) {
|
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();
|
|
|
|
packet.setType(IQ.Type.SET);
|
|
|
|
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);
|
|
|
|
PacketCollector collector = connection.createPacketCollector(
|
2007-02-20 18:02:39 +01:00
|
|
|
new PacketIDFilter(packet.getPacketID()));
|
2005-03-30 03:53:51 +02:00
|
|
|
connection.sendPacket(packet);
|
2007-02-20 18:02:39 +01:00
|
|
|
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
2005-03-30 03:53:51 +02:00
|
|
|
collector.cancel();
|
|
|
|
if (response == null) {
|
|
|
|
throw new XMPPException("No response from the server.");
|
|
|
|
}
|
|
|
|
// If the server replied with an error, throw an exception.
|
|
|
|
else if (response.getType() == IQ.Type.ERROR) {
|
|
|
|
throw new XMPPException(response.getError());
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* Returns an unmodifiable collection of all entries in the roster, including entries
|
|
|
|
* that don't belong to any groups.
|
2003-08-05 04:33:11 +02:00
|
|
|
*
|
|
|
|
* @return all entries in the roster.
|
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
public Collection<RosterEntry> getEntries() {
|
|
|
|
Set<RosterEntry> allEntries = new HashSet<RosterEntry>();
|
2003-08-27 23:53:56 +02:00
|
|
|
// Loop through all roster groups and add their entries to the answer
|
2006-07-18 04:42:22 +02:00
|
|
|
for (RosterGroup rosterGroup : getGroups()) {
|
2006-07-17 10:39:08 +02:00
|
|
|
allEntries.addAll(rosterGroup.getEntries());
|
2003-08-05 04:33:11 +02:00
|
|
|
}
|
2003-08-27 23:53:56 +02:00
|
|
|
// Add the roster unfiled entries to the answer
|
2006-10-08 01:16:20 +02:00
|
|
|
allEntries.addAll(unfiledEntries);
|
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
return Collections.unmodifiableCollection(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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-18 08:47:38 +02:00
|
|
|
* Returns an unmodifiable collection 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
|
|
|
*/
|
2006-07-18 08:47:38 +02:00
|
|
|
public Collection<RosterEntry> getUnfiledEntries() {
|
2006-10-08 01:16:20 +02:00
|
|
|
return Collections.unmodifiableList(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
|
|
|
|
* <tt>null</tt> if the user is not an entry in the roster.
|
|
|
|
*
|
2004-10-23 07:07:12 +02:00
|
|
|
* @param user 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").
|
2003-09-03 05:00:45 +02:00
|
|
|
* @return the roster entry or <tt>null</tt> if it does not exist.
|
|
|
|
*/
|
|
|
|
public RosterEntry getEntry(String user) {
|
|
|
|
if (user == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2007-12-27 16:54:56 +01:00
|
|
|
return entries.get(user.toLowerCase());
|
2003-09-03 05:00:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specified XMPP address is an entry in the roster.
|
|
|
|
*
|
2007-02-19 09:35:05 +01:00
|
|
|
* @param user the XMPP address of the user (eg "jsmith@example.com"). The
|
2007-02-20 18:02:39 +01:00
|
|
|
* address could be in any valid format (e.g. "domain/resource",
|
|
|
|
* "user@domain" or "user@domain/resource").
|
2003-09-03 05:00:45 +02:00
|
|
|
* @return true if the XMPP address is an entry in the roster.
|
|
|
|
*/
|
|
|
|
public boolean contains(String user) {
|
2006-01-24 19:14:41 +01:00
|
|
|
return getEntry(user) != null;
|
2003-09-03 05:00:45 +02:00
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
|
|
|
* Returns the roster group with the specified name, or <tt>null</tt> if the
|
|
|
|
* 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
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* Returns an unmodiable 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
|
|
|
|
* user's presence updates), unavailable presence will be returned.<p>
|
2007-02-20 18:02:39 +01:00
|
|
|
* <p/>
|
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
|
|
|
|
* {@link org.jivesoftware.smack.packet.Presence.Mode#dnd do not disturb}.<p>
|
2007-02-20 18:02:39 +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}.
|
2003-04-25 22:12:17 +02:00
|
|
|
*
|
2007-02-12 01:56:47 +01:00
|
|
|
* @param user an XMPP ID. The address could be in any valid format (e.g.
|
2007-02-20 18:02:39 +01:00
|
|
|
* "domain/resource", "user@domain" or "user@domain/resource"). Any resource
|
|
|
|
* information that's part of the ID will be discarded.
|
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
|
|
|
*/
|
|
|
|
public Presence getPresence(String user) {
|
2007-02-12 01:56:47 +01:00
|
|
|
String key = getPresenceMapKey(StringUtils.parseBareAddress(user));
|
2006-07-18 07:14:33 +02:00
|
|
|
Map<String, Presence> userPresences = presenceMap.get(key);
|
2003-11-28 22:49:25 +01:00
|
|
|
if (userPresences == null) {
|
2007-02-20 18:02:39 +01:00
|
|
|
Presence presence = new Presence(Presence.Type.unavailable);
|
|
|
|
presence.setFrom(user);
|
|
|
|
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;
|
|
|
|
|
2006-11-30 00:32:04 +01:00
|
|
|
for (String resource : userPresences.keySet()) {
|
|
|
|
Presence p = userPresences.get(resource);
|
2008-11-21 05:54:59 +01:00
|
|
|
if (!p.isAvailable()) {
|
|
|
|
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) {
|
2007-02-20 18:02:39 +01:00
|
|
|
presence = new Presence(Presence.Type.unavailable);
|
|
|
|
presence.setFrom(user);
|
|
|
|
return presence;
|
2007-02-12 01:56:47 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
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
|
|
|
*/
|
2007-02-12 01:56:47 +01:00
|
|
|
public Presence getPresenceResource(String userWithResource) {
|
|
|
|
String key = getPresenceMapKey(userWithResource);
|
|
|
|
String resource = StringUtils.parseResource(userWithResource);
|
2006-07-18 07:14:33 +02:00
|
|
|
Map<String, Presence> userPresences = presenceMap.get(key);
|
2003-11-28 22:49:25 +01:00
|
|
|
if (userPresences == null) {
|
2007-02-20 18:02:39 +01:00
|
|
|
Presence presence = new Presence(Presence.Type.unavailable);
|
|
|
|
presence.setFrom(userWithResource);
|
|
|
|
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) {
|
2007-02-20 18:02:39 +01:00
|
|
|
presence = new Presence(Presence.Type.unavailable);
|
|
|
|
presence.setFrom(userWithResource);
|
|
|
|
return presence;
|
2007-02-13 17:21:24 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return presence;
|
|
|
|
}
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-02-12 01:56:47 +01:00
|
|
|
* Returns an iterator (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
|
|
|
*
|
2007-02-12 01:56:47 +01:00
|
|
|
* @param user a XMPP ID, e.g. jdoe@example.com.
|
2004-11-05 06:25:24 +01:00
|
|
|
* @return an iterator (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
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public Iterator<Presence> getPresences(String user) {
|
2004-10-23 07:07:12 +02:00
|
|
|
String key = getPresenceMapKey(user);
|
2006-07-18 07:14:33 +02:00
|
|
|
Map<String, Presence> userPresences = presenceMap.get(key);
|
2003-11-28 22:49:25 +01:00
|
|
|
if (userPresences == null) {
|
2007-02-20 18:02:39 +01:00
|
|
|
Presence presence = new Presence(Presence.Type.unavailable);
|
|
|
|
presence.setFrom(user);
|
|
|
|
return Arrays.asList(presence).iterator();
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-11-21 05:54:59 +01:00
|
|
|
Collection<Presence> answer = new ArrayList<Presence>();
|
|
|
|
for (Presence presence : userPresences.values()) {
|
|
|
|
if (presence.isAvailable()) {
|
|
|
|
answer.add(presence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!answer.isEmpty()) {
|
|
|
|
return answer.iterator();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Presence presence = new Presence(Presence.Type.unavailable);
|
|
|
|
presence.setFrom(user);
|
|
|
|
return Arrays.asList(presence).iterator();
|
|
|
|
}
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
|
|
|
|
2004-10-23 07:07:12 +02:00
|
|
|
/**
|
2007-02-19 09:35:05 +01:00
|
|
|
* Cleans up all resources used by the roster.
|
|
|
|
*/
|
|
|
|
void cleanup() {
|
|
|
|
rosterListeners.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the key to use in the presenceMap for a fully qualified XMPP ID.
|
|
|
|
* The roster can contain any valid address format such us "domain/resource",
|
|
|
|
* "user@domain" or "user@domain/resource". If the roster contains an entry
|
|
|
|
* associated with the fully qualified XMPP ID then use the fully qualified XMPP
|
|
|
|
* ID as the key in presenceMap, otherwise use the bare address. Note: When the
|
|
|
|
* key in presenceMap is a fully qualified XMPP ID, the userPresences is useless
|
|
|
|
* since it will always contain one entry for the user.
|
2004-10-23 07:07:12 +02:00
|
|
|
*
|
2007-02-19 09:35:05 +01:00
|
|
|
* @param user the bare or fully qualified XMPP ID, e.g. jdoe@example.com or
|
2007-02-20 18:02:39 +01:00
|
|
|
* jdoe@example.com/Work.
|
2007-02-19 09:35:05 +01:00
|
|
|
* @return the key to use in the presenceMap for the fully qualified XMPP ID.
|
2004-10-23 07:07:12 +02:00
|
|
|
*/
|
|
|
|
private String getPresenceMapKey(String user) {
|
2006-01-24 19:14:41 +01:00
|
|
|
if (user == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2004-10-23 07:07:12 +02:00
|
|
|
String key = user;
|
|
|
|
if (!contains(user)) {
|
|
|
|
key = StringUtils.parseBareAddress(user);
|
|
|
|
}
|
2006-01-24 19:14:41 +01:00
|
|
|
return key.toLowerCase();
|
2004-10-23 07:07:12 +02:00
|
|
|
}
|
|
|
|
|
2007-02-19 09:35:05 +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 setOfflinePresences() {
|
|
|
|
Presence packetUnavailable;
|
|
|
|
for (String user : presenceMap.keySet()) {
|
|
|
|
Map<String, Presence> resources = presenceMap.get(user);
|
|
|
|
if (resources != null) {
|
|
|
|
for (String resource : resources.keySet()) {
|
|
|
|
packetUnavailable = new Presence(Presence.Type.unavailable);
|
|
|
|
packetUnavailable.setFrom(user + "/" + resource);
|
|
|
|
presencePacketListener.processPacket(packetUnavailable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2007-02-09 07:21:29 +01:00
|
|
|
private void fireRosterChangedEvent(Collection<String> addedEntries, Collection<String> updatedEntries,
|
2007-02-20 18:02:39 +01:00
|
|
|
Collection<String> deletedEntries) {
|
2006-10-08 01:16:20 +02:00
|
|
|
for (RosterListener listener : rosterListeners) {
|
2005-11-04 20:51:29 +01:00
|
|
|
if (!addedEntries.isEmpty()) {
|
2006-07-18 07:14:33 +02:00
|
|
|
listener.entriesAdded(addedEntries);
|
2005-11-04 20:51:29 +01:00
|
|
|
}
|
|
|
|
if (!updatedEntries.isEmpty()) {
|
2006-07-18 07:14:33 +02:00
|
|
|
listener.entriesUpdated(updatedEntries);
|
2005-11-04 20:51:29 +01:00
|
|
|
}
|
|
|
|
if (!deletedEntries.isEmpty()) {
|
2006-07-18 07:14:33 +02:00
|
|
|
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
|
|
|
*/
|
2007-02-12 01:56:47 +01:00
|
|
|
private void fireRosterPresenceEvent(Presence presence) {
|
2006-10-08 01:16:20 +02:00
|
|
|
for (RosterListener listener : rosterListeners) {
|
2007-02-12 01:56:47 +01:00
|
|
|
listener.presenceChanged(presence);
|
2003-08-21 05:31:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-18 08:47:38 +02:00
|
|
|
/**
|
|
|
|
* An enumeration for the subscription mode options.
|
|
|
|
*/
|
|
|
|
public enum SubscriptionMode {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically accept all subscription and unsubscription requests. This is
|
|
|
|
* the default mode and is suitable for simple client. More complex client will
|
|
|
|
* 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,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically reject all subscription requests.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
private class PresencePacketListener implements PacketListener {
|
2007-02-20 18:02:39 +01:00
|
|
|
|
2003-04-14 06:10:47 +02:00
|
|
|
public void processPacket(Packet packet) {
|
2007-02-20 18:02:39 +01:00
|
|
|
Presence presence = (Presence) packet;
|
2003-04-25 22:12:17 +02:00
|
|
|
String from = presence.getFrom();
|
2004-10-23 07:07:12 +02:00
|
|
|
String key = getPresenceMapKey(from);
|
|
|
|
|
2007-02-19 09:35:05 +01:00
|
|
|
// 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.
|
2006-07-17 10:39:08 +02:00
|
|
|
if (presence.getType() == Presence.Type.available) {
|
2006-07-18 07:14:33 +02:00
|
|
|
Map<String, Presence> userPresences;
|
2003-11-28 22:49:25 +01:00
|
|
|
// Get the user presence map
|
|
|
|
if (presenceMap.get(key) == null) {
|
2006-12-05 00:52:59 +01:00
|
|
|
userPresences = new ConcurrentHashMap<String, Presence>();
|
2003-11-28 22:49:25 +01:00
|
|
|
presenceMap.put(key, userPresences);
|
|
|
|
}
|
2004-11-05 06:25:24 +01:00
|
|
|
else {
|
2006-07-18 07:14:33 +02:00
|
|
|
userPresences = presenceMap.get(key);
|
2004-11-05 06:25:24 +01:00
|
|
|
}
|
2007-02-19 09:35:05 +01:00
|
|
|
// See if an offline presence was being stored in the map. If so, remove
|
|
|
|
// it since we now have an online presence.
|
|
|
|
userPresences.remove("");
|
2004-11-05 06:25:24 +01:00
|
|
|
// Add the new presence, using the resources as a key.
|
2007-02-19 09:35:05 +01:00
|
|
|
userPresences.put(StringUtils.parseResource(from), presence);
|
2003-08-21 05:31:50 +02:00
|
|
|
// If the user is in the roster, fire an event.
|
2007-12-27 16:54:56 +01:00
|
|
|
RosterEntry entry = entries.get(key);
|
2008-11-19 21:54:05 +01:00
|
|
|
if (entry != null) {
|
2007-12-27 16:54:56 +01:00
|
|
|
fireRosterPresenceEvent(presence);
|
2008-11-19 21:54:05 +01:00
|
|
|
}
|
2003-04-14 06:10:47 +02:00
|
|
|
}
|
2007-02-19 09:35:05 +01:00
|
|
|
// If an "unavailable" packet.
|
2006-07-17 10:39:08 +02:00
|
|
|
else if (presence.getType() == Presence.Type.unavailable) {
|
2007-02-19 09:35:05 +01:00
|
|
|
// If no resource, this is likely an offline presence as part of
|
|
|
|
// a roster presence flood. In that case, we store it.
|
|
|
|
if ("".equals(StringUtils.parseResource(from))) {
|
|
|
|
Map<String, Presence> userPresences;
|
|
|
|
// Get the user presence map
|
|
|
|
if (presenceMap.get(key) == null) {
|
|
|
|
userPresences = new ConcurrentHashMap<String, Presence>();
|
|
|
|
presenceMap.put(key, userPresences);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
userPresences = presenceMap.get(key);
|
2004-11-05 06:25:24 +01:00
|
|
|
}
|
2007-02-19 09:35:05 +01:00
|
|
|
userPresences.put("", presence);
|
|
|
|
}
|
|
|
|
// Otherwise, this is a normal offline presence.
|
|
|
|
else if (presenceMap.get(key) != null) {
|
|
|
|
Map<String, Presence> userPresences = presenceMap.get(key);
|
2007-05-11 00:48:24 +02:00
|
|
|
// Store the offline presence, as it may include extra information
|
|
|
|
// such as the user being on vacation.
|
|
|
|
userPresences.put(StringUtils.parseResource(from), presence);
|
2003-11-28 22:49:25 +01:00
|
|
|
}
|
2003-08-21 05:31:50 +02:00
|
|
|
// If the user is in the roster, fire an event.
|
2007-12-27 16:54:56 +01:00
|
|
|
RosterEntry entry = entries.get(key);
|
2008-11-19 21:54:05 +01:00
|
|
|
if (entry != null) {
|
2007-12-27 16:54:56 +01:00
|
|
|
fireRosterPresenceEvent(presence);
|
2008-11-19 21:54:05 +01:00
|
|
|
}
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
2006-07-17 10:39:08 +02:00
|
|
|
else if (presence.getType() == Presence.Type.subscribe) {
|
2006-07-18 08:47:38 +02:00
|
|
|
if (subscriptionMode == SubscriptionMode.accept_all) {
|
2003-06-17 19:20:30 +02:00
|
|
|
// Accept all subscription requests.
|
2006-07-17 10:39:08 +02:00
|
|
|
Presence response = new Presence(Presence.Type.subscribed);
|
2003-06-17 19:20:30 +02:00
|
|
|
response.setTo(presence.getFrom());
|
|
|
|
connection.sendPacket(response);
|
|
|
|
}
|
2006-07-18 08:47:38 +02:00
|
|
|
else if (subscriptionMode == SubscriptionMode.reject_all) {
|
2003-06-17 19:20:30 +02:00
|
|
|
// Reject all subscription requests.
|
2006-07-17 10:39:08 +02:00
|
|
|
Presence response = new Presence(Presence.Type.unsubscribed);
|
2003-06-17 19:20:30 +02:00
|
|
|
response.setTo(presence.getFrom());
|
|
|
|
connection.sendPacket(response);
|
|
|
|
}
|
|
|
|
// Otherwise, in manual mode so ignore.
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
2006-07-17 10:39:08 +02:00
|
|
|
else if (presence.getType() == Presence.Type.unsubscribe) {
|
2006-07-18 08:47:38 +02:00
|
|
|
if (subscriptionMode != SubscriptionMode.manual) {
|
2005-10-27 17:22:14 +02:00
|
|
|
// Acknowledge and accept unsubscription notification so that the
|
2006-07-18 08:47:38 +02:00
|
|
|
// server will stop sending notifications saying that the contact
|
2005-10-27 17:22:14 +02:00
|
|
|
// has unsubscribed to our presence.
|
2006-07-17 10:39:08 +02:00
|
|
|
Presence response = new Presence(Presence.Type.unsubscribed);
|
2005-10-27 17:22:14 +02:00
|
|
|
response.setTo(presence.getFrom());
|
|
|
|
connection.sendPacket(response);
|
|
|
|
}
|
|
|
|
// Otherwise, in manual mode so ignore.
|
|
|
|
}
|
2008-11-19 21:54:05 +01:00
|
|
|
// Error presence packets from a bare JID mean we invalidate all existing
|
|
|
|
// presence info for the user.
|
|
|
|
else if (presence.getType() == Presence.Type.error &&
|
|
|
|
"".equals(StringUtils.parseResource(from)))
|
|
|
|
{
|
|
|
|
Map<String, Presence> userPresences;
|
|
|
|
if (!presenceMap.containsKey(key)) {
|
|
|
|
userPresences = new ConcurrentHashMap<String, Presence>();
|
|
|
|
presenceMap.put(key, userPresences);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
userPresences = presenceMap.get(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("", presence);
|
|
|
|
// If the user is in the roster, fire an event.
|
|
|
|
RosterEntry entry = entries.get(key);
|
|
|
|
if (entry != null) {
|
|
|
|
fireRosterPresenceEvent(presence);
|
|
|
|
}
|
|
|
|
}
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
2003-04-14 06:10:47 +02:00
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
|
|
|
* Listens for all roster packets and processes them.
|
|
|
|
*/
|
2003-04-13 06:51:37 +02:00
|
|
|
private class RosterPacketListener implements PacketListener {
|
2003-02-10 06:01:01 +01:00
|
|
|
|
|
|
|
public void processPacket(Packet packet) {
|
2005-11-04 19:31:21 +01:00
|
|
|
// Keep a registry of the entries that were added, deleted or updated. An event
|
|
|
|
// will be fired for each affected entry
|
2006-07-18 07:14:33 +02:00
|
|
|
Collection<String> addedEntries = new ArrayList<String>();
|
|
|
|
Collection<String> updatedEntries = new ArrayList<String>();
|
|
|
|
Collection<String> deletedEntries = new ArrayList<String>();
|
2005-11-04 19:31:21 +01:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
RosterPacket rosterPacket = (RosterPacket) packet;
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterPacket.Item item : rosterPacket.getRosterItems()) {
|
2003-04-25 22:12:17 +02:00
|
|
|
RosterEntry entry = new RosterEntry(item.getUser(), item.getName(),
|
2010-03-24 21:31:06 +01:00
|
|
|
item.getItemType(), item.getItemStatus(), Roster.this, connection);
|
2003-08-21 05:31:50 +02:00
|
|
|
|
2003-09-24 20:07:02 +02:00
|
|
|
// If the packet is of the type REMOVE then remove the entry
|
2007-01-08 00:03:16 +01:00
|
|
|
if (RosterPacket.ItemType.remove.equals(item.getItemType())) {
|
2003-09-24 20:07:02 +02:00
|
|
|
// Remove the entry from the entry list.
|
2007-12-27 16:54:56 +01:00
|
|
|
if (entries.containsKey(item.getUser())) {
|
|
|
|
entries.remove(item.getUser());
|
2003-09-24 20:07:02 +02:00
|
|
|
}
|
|
|
|
// Remove the entry from the unfiled entry list.
|
2006-10-08 01:16:20 +02:00
|
|
|
if (unfiledEntries.contains(entry)) {
|
|
|
|
unfiledEntries.remove(entry);
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
2004-04-21 23:10:42 +02:00
|
|
|
// Removing the user from the roster, so remove any presence information
|
|
|
|
// about them.
|
|
|
|
String key = StringUtils.parseName(item.getUser()) + "@" +
|
|
|
|
StringUtils.parseServer(item.getUser());
|
|
|
|
presenceMap.remove(key);
|
2005-11-04 19:31:21 +01:00
|
|
|
// Keep note that an entry has been removed
|
|
|
|
deletedEntries.add(item.getUser());
|
2003-04-25 22:12:17 +02:00
|
|
|
}
|
2003-08-21 05:31:50 +02:00
|
|
|
else {
|
2003-09-24 20:07:02 +02:00
|
|
|
// Make sure the entry is in the entry list.
|
2007-12-27 16:54:56 +01:00
|
|
|
if (!entries.containsKey(item.getUser())) {
|
|
|
|
entries.put(item.getUser(), entry);
|
2005-11-04 19:31:21 +01:00
|
|
|
// Keep note that an entry has been added
|
|
|
|
addedEntries.add(item.getUser());
|
2004-11-05 06:25:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2003-09-25 15:00:48 +02:00
|
|
|
// If the entry was in then list then update its state with the new values
|
2007-12-27 16:54:56 +01:00
|
|
|
entries.put(item.getUser(), entry);
|
|
|
|
|
2005-11-04 19:31:21 +01:00
|
|
|
// Keep note that an entry has been updated
|
|
|
|
updatedEntries.add(item.getUser());
|
2003-09-24 20:07:02 +02:00
|
|
|
}
|
|
|
|
// If the roster entry belongs to any groups, remove it from the
|
|
|
|
// list of unfiled entries.
|
2006-07-17 10:39:08 +02:00
|
|
|
if (!item.getGroupNames().isEmpty()) {
|
2006-10-08 01:16:20 +02:00
|
|
|
unfiledEntries.remove(entry);
|
2003-09-24 20:07:02 +02:00
|
|
|
}
|
|
|
|
// Otherwise add it to the list of unfiled entries.
|
|
|
|
else {
|
2006-10-08 01:16:20 +02:00
|
|
|
if (!unfiledEntries.contains(entry)) {
|
|
|
|
unfiledEntries.add(entry);
|
2003-08-21 05:31:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-10-03 21:30:45 +02:00
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
// Find the list of groups that the user currently belongs to.
|
2006-07-17 10:39:08 +02:00
|
|
|
List<String> currentGroupNames = new ArrayList<String>();
|
2010-02-11 15:26:28 +01:00
|
|
|
for (RosterGroup group: getGroups()) {
|
|
|
|
if (group.contains(entry)) {
|
|
|
|
currentGroupNames.add(group.getName());
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2003-10-03 21:30:45 +02:00
|
|
|
|
2003-09-24 20:07:02 +02:00
|
|
|
// If the packet is not of the type REMOVE then add the entry to the groups
|
2007-01-08 00:03:16 +01:00
|
|
|
if (!RosterPacket.ItemType.remove.equals(item.getItemType())) {
|
2003-09-24 20:07:02 +02:00
|
|
|
// Create the new list of groups the user belongs to.
|
2006-07-17 10:39:08 +02:00
|
|
|
List<String> newGroupNames = new ArrayList<String>();
|
|
|
|
for (String groupName : item.getGroupNames()) {
|
2003-09-24 20:07:02 +02:00
|
|
|
// Add the group name to the list.
|
|
|
|
newGroupNames.add(groupName);
|
2003-10-03 21:30:45 +02:00
|
|
|
|
2003-09-24 20:07:02 +02:00
|
|
|
// 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);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-09-24 20:07:02 +02:00
|
|
|
// We have the list of old and new group names. We now need to
|
|
|
|
// remove the entry from the all the groups it may no longer belong
|
|
|
|
// to. We do this by subracting the new group set from the old.
|
2006-07-18 07:14:33 +02:00
|
|
|
for (String newGroupName : newGroupNames) {
|
|
|
|
currentGroupNames.remove(newGroupName);
|
2003-09-24 20:07:02 +02:00
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2003-09-24 20:07:02 +02:00
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
// Loop through any groups that remain and remove the entries.
|
2004-01-18 15:17:13 +01:00
|
|
|
// This is neccessary for the case of remote entry removals.
|
2006-07-18 07:14:33 +02:00
|
|
|
for (String groupName : currentGroupNames) {
|
2004-01-18 15:17:13 +01:00
|
|
|
RosterGroup group = getGroup(groupName);
|
|
|
|
group.removeEntryLocal(entry);
|
|
|
|
if (group.getEntryCount() == 0) {
|
2006-10-08 01:16:20 +02:00
|
|
|
groups.remove(groupName);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
}
|
2006-09-14 21:14:51 +02:00
|
|
|
// Remove all the groups with no entries. 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
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterGroup group : getGroups()) {
|
2004-01-18 15:17:13 +01:00
|
|
|
if (group.getEntryCount() == 0) {
|
2006-07-17 10:39:08 +02:00
|
|
|
groups.remove(group.getName());
|
2004-01-18 15:17:13 +01:00
|
|
|
}
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2003-04-07 07:40:28 +02:00
|
|
|
|
|
|
|
// Mark the roster as initialized.
|
2005-06-28 19:48:04 +02:00
|
|
|
synchronized (Roster.this) {
|
2005-03-30 03:53:51 +02:00
|
|
|
rosterInitialized = true;
|
2005-06-28 19:48:04 +02:00
|
|
|
Roster.this.notifyAll();
|
2005-03-30 03:53:51 +02:00
|
|
|
}
|
2004-01-12 17:04:23 +01:00
|
|
|
|
|
|
|
// Fire event for roster listeners.
|
2005-11-04 19:31:21 +01:00
|
|
|
fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
}
|
2008-11-19 21:54:05 +01:00
|
|
|
}
|