Commit 41654d3a authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Add support for disabling/enabling the roster service. JM-583

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3509 b35dd754-fafc-0310-a699-88a17e54d16e
parent 38f4de4c
......@@ -378,7 +378,7 @@ public class SessionManager extends BasicModule {
conn.registerCloseListener(clientSessionListener, session);
// Add to pre-authenticated sessions.
preAuthenticatedSessions.put(session.getAddress().toString(), session);
preAuthenticatedSessions.put(session.getAddress().getResource(), session);
return session;
}
......@@ -533,9 +533,8 @@ public class SessionManager extends BasicModule {
sessions.put(username, resources);
}
resources.addSession(session);
// Remove the pre-Authenticated session but remember to use the temporary JID as the key
preAuthenticatedSessions.remove(new JID(null, session.getAddress().getDomain(),
session.getStreamID().toString()).toString());
// Remove the pre-Authenticated session but remember to use the temporary ID as the key
preAuthenticatedSessions.remove(session.getStreamID().toString());
// Fire session created event.
SessionEventDispatcher.dispatchEvent(session,
......@@ -680,8 +679,7 @@ public class SessionManager extends BasicModule {
* @param priority The new priority for the session
*/
public void changePriority(JID sender, int priority) {
if (sender.getNode() == null ||
!UserManager.getInstance().isRegisteredUser(sender.getNode())) {
if (sender.getNode() == null || !userManager.isRegisteredUser(sender.getNode())) {
// Do nothing if the session belongs to an anonymous user
return;
}
......@@ -720,8 +718,7 @@ public class SessionManager extends BasicModule {
ClientSession session = null;
String resource = recipient.getResource();
String username = recipient.getNode();
if (resource != null &&
(username == null || !UserManager.getInstance().isRegisteredUser(username))) {
if (resource != null && (username == null || !userManager.isRegisteredUser(username))) {
session = anonymousSessions.get(resource);
if (session == null){
session = getSession(recipient);
......@@ -856,15 +853,17 @@ public class SessionManager extends BasicModule {
ClientSession session = null;
// Initially Check preAuthenticated Sessions
session = preAuthenticatedSessions.get(jid);
if(session != null){
return session;
if (resource != null) {
session = preAuthenticatedSessions.get(resource);
if(session != null){
return session;
}
}
if (resource == null) {
return null;
}
if (username == null || !UserManager.getInstance().isRegisteredUser(username)) {
if (username == null || !userManager.isRegisteredUser(username)) {
session = anonymousSessions.get(resource);
}
else {
......@@ -1106,7 +1105,7 @@ public class SessionManager extends BasicModule {
}
public int getSessionCount(String username) {
if (username == null || !UserManager.getInstance().isRegisteredUser(username)) {
if (username == null || !userManager.isRegisteredUser(username)) {
return 0;
}
int sessionCount = 0;
......@@ -1221,7 +1220,8 @@ public class SessionManager extends BasicModule {
}
else {
// If this is a non-anonymous session then remove the session from the SessionMap
if (session.getAddress() != null && session.getAddress().getNode() != null) {
if (session.getAddress() != null &&
userManager.isRegisteredUser(session.getAddress().getNode())) {
String username = session.getAddress().getNode();
synchronized (username.intern()) {
sessionMap = sessions.get(username);
......@@ -1250,14 +1250,14 @@ public class SessionManager extends BasicModule {
}
else if (preAuthenticatedSessions.containsValue(session)) {
// Remove the session from the pre-Authenticated sessions list
preAuthenticatedSessions.remove(session.getAddress().toString());
preAuthenticatedSessions.remove(session.getAddress().getResource());
}
}
public void addAnonymousSession(ClientSession session) {
anonymousSessions.put(session.getAddress().getResource(), session);
// Remove the session from the pre-Authenticated sessions list
preAuthenticatedSessions.remove(session.getAddress().toString());
preAuthenticatedSessions.remove(session.getAddress().getResource());
// Fire session event.
SessionEventDispatcher.dispatchEvent(session,
......@@ -1435,7 +1435,7 @@ public class SessionManager extends BasicModule {
Message packet = createServerMessage(subject, body);
try {
if (address == null || address.getNode() == null ||
!UserManager.getInstance().isRegisteredUser(address)) {
!userManager.isRegisteredUser(address)) {
broadcast(packet);
}
else if (address.getResource() == null || address.getResource().length() < 1) {
......
......@@ -18,6 +18,7 @@ import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.disco.ServerFeaturesProvider;
import org.jivesoftware.wildfire.roster.Roster;
import org.jivesoftware.wildfire.roster.RosterItem;
import org.jivesoftware.wildfire.roster.RosterManager;
import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
......@@ -168,10 +169,11 @@ public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider
IQ.Type type = packet.getType();
try {
if ((sender.getNode() == null ||
!UserManager.getInstance().isRegisteredUser(sender.getNode())) &&
if ((sender.getNode() == null || !RosterManager.isRosterServiceEnabled() ||
!userManager.isRegisteredUser(sender.getNode())) &&
IQ.Type.get == type) {
// If anonymous user asks for his roster then return an empty roster
// If anonymous user asks for his roster or roster service is disabled then
// return an empty roster
IQ reply = IQ.createResultIQ(packet);
reply.setChildElement("query", "jabber:iq:roster");
return reply;
......
......@@ -11,16 +11,17 @@
package org.jivesoftware.wildfire.handler;
import org.jivesoftware.util.ConcurrentHashSet;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.wildfire.roster.Roster;
import org.jivesoftware.wildfire.roster.RosterItem;
import org.jivesoftware.wildfire.roster.RosterManager;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.jivesoftware.util.ConcurrentHashSet;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.*;
import java.util.*;
......@@ -74,6 +75,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
private PacketDeliverer deliverer;
private OfflineMessageStore messageStore;
private SessionManager sessionManager;
private UserManager userManager;
public PresenceUpdateHandler() {
super("Presence update handler");
......@@ -186,19 +188,23 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
private void initSession(ClientSession session) throws UserNotFoundException {
// Only user sessions need to be authenticated
if (session.getAddress().getNode() != null && !"".equals(session.getAddress().getNode())) {
if (userManager.isRegisteredUser(session.getAddress().getNode())) {
String username = session.getAddress().getNode();
Roster roster = rosterManager.getRoster(username);
for (RosterItem item : roster.getRosterItems()) {
if (item.getRecvStatus() == RosterItem.RECV_SUBSCRIBE) {
session.process(createSubscribePresence(item.getJid(), true));
}
else if (item.getRecvStatus() == RosterItem.RECV_UNSUBSCRIBE) {
session.process(createSubscribePresence(item.getJid(), false));
}
if (item.getSubStatus() == RosterItem.SUB_TO
|| item.getSubStatus() == RosterItem.SUB_BOTH) {
presenceManager.probePresence(session.getAddress(), item.getJid());
// Send pending subscription requests to user if roster service is enabled
if (RosterManager.isRosterServiceEnabled()) {
Roster roster = rosterManager.getRoster(username);
for (RosterItem item : roster.getRosterItems()) {
if (item.getRecvStatus() == RosterItem.RECV_SUBSCRIBE) {
session.process(createSubscribePresence(item.getJid(), true));
}
else if (item.getRecvStatus() == RosterItem.RECV_UNSUBSCRIBE) {
session.process(createSubscribePresence(item.getJid(), false));
}
if (item.getSubStatus() == RosterItem.SUB_TO
|| item.getSubStatus() == RosterItem.SUB_BOTH) {
presenceManager.probePresence(session.getAddress(), item.getJid());
}
}
}
if (session.canFloodOfflineMessages()) {
......@@ -240,6 +246,10 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
return;
}
if (localServer.isLocal(update.getFrom())) {
// Do nothing if roster service is disabled
if (!RosterManager.isRosterServiceEnabled()) {
return;
}
// Local updates can simply run through the roster of the local user
String name = update.getFrom().getNode();
try {
......@@ -310,28 +320,34 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
WeakHashMap<ChannelHandler, Set<String>> map;
String name = update.getFrom().getNode();
if (name != null && !"".equals(name)) {
try {
Roster roster = rosterManager.getRoster(name);
// If the directed presence was sent to an entity that is not in the user's
// roster, keep a registry of this so that when the user goes offline we will
// be able to send the unavailable presence to the entity
RosterItem rosterItem = null;
// Keep track of all directed presences if roster service is disabled
if (!RosterManager.isRosterServiceEnabled()) {
keepTrack = true;
}
else {
try {
rosterItem = roster.getRosterItem(update.getTo());
Roster roster = rosterManager.getRoster(name);
// If the directed presence was sent to an entity that is not in the user's
// roster, keep a registry of this so that when the user goes offline we
// will be able to send the unavailable presence to the entity
RosterItem rosterItem = null;
try {
rosterItem = roster.getRosterItem(update.getTo());
}
catch (UserNotFoundException e) {}
if (rosterItem == null ||
RosterItem.SUB_NONE == rosterItem.getSubStatus() ||
RosterItem.SUB_TO == rosterItem.getSubStatus()) {
keepTrack = true;
}
}
catch (UserNotFoundException e) {}
if (rosterItem == null || RosterItem.SUB_NONE == rosterItem.getSubStatus() ||
RosterItem.SUB_TO == rosterItem.getSubStatus()) {
keepTrack = true;
catch (UserNotFoundException e) {
Log.warn("Presence being sent from unknown user " + name, e);
}
catch (PacketException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
catch (UserNotFoundException e) {
Log.warn("Presence being sent from unknown user " + name, e);
}
catch (PacketException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
else if (update.getFrom().getResource() != null){
// Keep always track of anonymous users directed presences
......@@ -451,6 +467,7 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
deliverer = server.getPacketDeliverer();
messageStore = server.getOfflineMessageStore();
sessionManager = server.getSessionManager();
userManager = server.getUserManager();
}
}
......@@ -11,21 +11,25 @@
package org.jivesoftware.wildfire.roster;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.jivesoftware.wildfire.user.User;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.wildfire.ChannelHandler;
import org.jivesoftware.wildfire.RoutingTable;
import org.jivesoftware.wildfire.SharedGroupException;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.event.GroupEventListener;
import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.wildfire.event.GroupEventDispatcher;
import org.jivesoftware.wildfire.event.GroupEventListener;
import org.jivesoftware.wildfire.group.Group;
import org.jivesoftware.wildfire.group.GroupManager;
import org.jivesoftware.wildfire.group.GroupNotFoundException;
import org.jivesoftware.wildfire.user.User;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import java.util.*;
......@@ -46,6 +50,16 @@ public class RosterManager extends BasicModule implements GroupEventListener {
private XMPPServer server;
private RoutingTable routingTable;
/**
* Returns true if the roster service is enabled. When disabled it is not possible to
* retrieve users rosters or broadcast presence packets to roster contacts.
*
* @return true if the roster service is enabled.
*/
public static boolean isRosterServiceEnabled() {
return JiveGlobals.getBooleanProperty("xmpp.client.roster.active", true);
}
public RosterManager() {
super("Roster Manager");
// Add the new instance as a listener of group events
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment