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