Commit 069be623 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Refactoring.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@919 b35dd754-fafc-0310-a699-88a17e54d16e
parent ae341f28
...@@ -12,17 +12,41 @@ ...@@ -12,17 +12,41 @@
package org.jivesoftware.messenger; package org.jivesoftware.messenger;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.JID;
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.handler.IQHandler;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.LocaleUtils;
import org.dom4j.Element;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* <p>Route iq packets throughout the server.</p> * Routes iq packets throughout the server. Routing is based on the recipient
* <p>Routing is based on the recipient and sender addresses. The typical * and sender addresses. The typical packet will often be routed twice, once
* packet will often be routed twice, once from the sender to some internal * from the sender to some internal server component for handling or processing,
* server component for handling or processing, and then back to the router * and then back to the router to be delivered to it's final destination.
* to be delivered to it's final destination.</p>
* *
* @author Iain Shigeoka * @author Iain Shigeoka
*/ */
public interface IQRouter { public class IQRouter extends BasicModule {
private RoutingTable routingTable;
private List<IQHandler> iqHandlers = new ArrayList<IQHandler>();
private Map<String, IQHandler> namespace2Handlers = new ConcurrentHashMap<String, IQHandler>();
private SessionManager sessionManager;
/**
* Creates a packet router.
*/
public IQRouter() {
super("XMPP IQ Router");
}
/** /**
* <p>Performs the actual packet routing.</p> * <p>Performs the actual packet routing.</p>
...@@ -37,5 +61,187 @@ public interface IQRouter { ...@@ -37,5 +61,187 @@ public interface IQRouter {
* @param packet The packet to route * @param packet The packet to route
* @throws NullPointerException If the packet is null * @throws NullPointerException If the packet is null
*/ */
public void route(IQ packet); public void route(IQ packet) {
if (packet == null) {
throw new NullPointerException();
}
Session session = sessionManager.getSession(packet.getFrom());
if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED
|| (isLocalServer(packet.getTo())
&& ("jabber:iq:auth".equals(packet.getChildElement().getNamespaceURI())
|| "jabber:iq:register".equals(packet.getChildElement().getNamespaceURI())))
) {
handle(packet);
}
else {
packet.setTo(sessionManager.getSession(packet.getFrom()).getAddress());
packet.setError(PacketError.Condition.not_authorized);
try {
sessionManager.getSession(packet.getFrom()).process(packet);
}
catch (UnauthorizedException ue) {
Log.error(ue);
}
}
}
/**
* <p>Adds a new IQHandler to the list of registered handler. The new IQHandler will be
* responsible for handling IQ packet whose namespace matches the namespace of the
* IQHandler.</p>
*
* An IllegalArgumentException may be thrown if the IQHandler to register was already provided
* by the server. The server provides a certain list of IQHandlers when the server is
* started up.
*
* @param handler the IQHandler to add to the list of registered handler.
*/
public void addHandler(IQHandler handler) {
if (iqHandlers.contains(handler)) {
throw new IllegalArgumentException("IQHandler already provided by the server");
}
// Register the handler as the handler of the namespace
namespace2Handlers.put(handler.getInfo().getNamespace(), handler);
}
/**
* <p>Removes an IQHandler from the list of registered handler. The IQHandler to remove was
* responsible for handling IQ packet whose namespace matches the namespace of the
* IQHandler.</p>
*
* An IllegalArgumentException may be thrown if the IQHandler to remove was already provided
* by the server. The server provides a certain list of IQHandlers when the server is
* started up.
*
* @param handler the IQHandler to remove from the list of registered handler.
*/
public void removeHandler(IQHandler handler) {
if (iqHandlers.contains(handler)) {
throw new IllegalArgumentException("Cannot remove an IQHandler provided by the server");
}
// Unregister the handler as the handler of the namespace
namespace2Handlers.remove(handler.getInfo().getNamespace());
}
public void initialize(XMPPServer server) {
super.initialize(server);
routingTable = server.getRoutingTable();
iqHandlers.addAll(server.getIQHandlers());
sessionManager = server.getSessionManager();
}
private boolean isLocalServer(JID recipientJID) {
return recipientJID == null || recipientJID.getDomain() == null
|| "".equals(recipientJID.getDomain()) || recipientJID.getResource() == null
|| "".equals(recipientJID.getResource());
}
private void handle(IQ packet) {
JID recipientJID = packet.getTo();
try {
if (isLocalServer(recipientJID)) {
Element childElement = packet.getChildElement();
String namespace = null;
if (childElement != null) {
namespace = childElement.getNamespaceURI();
}
if (namespace == null) {
// Do nothing. We can't handle queries outside of a valid namespace
Log.warn("Unknown packet " + packet);
}
else {
IQHandler handler = getHandler(namespace);
if (handler == null) {
IQ reply = IQ.createResultIQ(packet);
if (recipientJID == null) {
// Answer an error since the server can't handle the requested namespace
reply.setError(PacketError.Condition.service_unavailable);
}
else if (recipientJID.getNode() == null || "".equals(recipientJID.getNode())) {
// Answer an error if JID is of the form <domain>
reply.setError(PacketError.Condition.feature_not_implemented);
}
else {
// JID is of the form <node@domain>
try {
// Let a "service" handle this packet otherwise return an error
// Useful for MUC where node refers to a room and domain is the
// MUC service.
ChannelHandler route = routingTable.getRoute(recipientJID);
if (route instanceof BasicModule) {
route.process(packet);
return;
}
}
catch (NoSuchRouteException e) {
// do nothing
}
// Answer an error since the server can't handle packets sent to a node
reply.setError(PacketError.Condition.service_unavailable);
}
Session session = sessionManager.getSession(packet.getFrom());
if (session != null) {
session.getConnection().deliver(reply);
}
else {
Log.warn("Packet could not be delivered " + packet);
}
}
else {
handler.process(packet);
}
}
}
else {
// JID is of the form <node@domain/resource>
ChannelHandler route = routingTable.getRoute(recipientJID);
route.process(packet);
}
}
catch (NoSuchRouteException e) {
Log.info("Packet sent to unreachable address " + packet);
Session session = sessionManager.getSession(packet.getFrom());
if (session != null) {
try {
packet.setError(PacketError.Condition.service_unavailable);
session.getConnection().deliver(packet);
}
catch (UnauthorizedException ex) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
}
}
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
try {
Session session = sessionManager.getSession(packet.getFrom());
if (session != null) {
Connection conn = session.getConnection();
if (conn != null) {
conn.close();
}
}
}
catch (UnauthorizedException e1) {
// do nothing
}
}
}
private IQHandler getHandler(String namespace) {
IQHandler handler = namespace2Handlers.get(namespace);
if (handler == null) {
for (IQHandler handlerCandidate : iqHandlers) {
IQHandlerInfo handlerInfo = handlerCandidate.getInfo();
if (handlerInfo != null && namespace.equalsIgnoreCase(handlerInfo.getNamespace())) {
handler = handlerCandidate;
namespace2Handlers.put(namespace, handler);
break;
}
}
}
return handler;
}
} }
...@@ -20,7 +20,7 @@ import org.xmpp.packet.Packet; ...@@ -20,7 +20,7 @@ import org.xmpp.packet.Packet;
* *
* @author Iain Shigeoka * @author Iain Shigeoka
*/ */
public interface PacketRouter extends IQRouter, MessageRouter, PresenceRouter { public interface PacketRouter extends MessageRouter, PresenceRouter {
/** /**
* <p>Routes the given packet based on packet recipient and sender.</p> * <p>Routes the given packet based on packet recipient and sender.</p>
......
...@@ -168,7 +168,7 @@ public class XMPPServer { ...@@ -168,7 +168,7 @@ public class XMPPServer {
name = "127.0.0.1"; name = "127.0.0.1";
} }
version = new Version(2, 1, 1, Version.ReleaseStatus.Beta, -1); version = new Version(2, 1, 1, Version.ReleaseStatus.Release, -1);
if ("true".equals(JiveGlobals.getXMLProperty("setup"))) { if ("true".equals(JiveGlobals.getXMLProperty("setup"))) {
setupMode = false; setupMode = false;
} }
...@@ -234,7 +234,7 @@ public class XMPPServer { ...@@ -234,7 +234,7 @@ public class XMPPServer {
loadModule(PresenceManagerImpl.class.getName()); loadModule(PresenceManagerImpl.class.getName());
loadModule(SessionManager.class.getName()); loadModule(SessionManager.class.getName());
loadModule(PacketRouterImpl.class.getName()); loadModule(PacketRouterImpl.class.getName());
loadModule(IQRouterImpl.class.getName()); loadModule(IQRouter.class.getName());
loadModule(MessageRouterImpl.class.getName()); loadModule(MessageRouterImpl.class.getName());
loadModule(PresenceRouterImpl.class.getName()); loadModule(PresenceRouterImpl.class.getName());
loadModule(PacketTransporterImpl.class.getName()); loadModule(PacketTransporterImpl.class.getName());
...@@ -762,7 +762,7 @@ public class XMPPServer { ...@@ -762,7 +762,7 @@ public class XMPPServer {
* @return the <code>IQRouter</code> registered with this server. * @return the <code>IQRouter</code> registered with this server.
*/ */
public IQRouter getIQRouter() { public IQRouter getIQRouter() {
return (IQRouter) modules.get(IQRouterImpl.class); return (IQRouter) modules.get(IQRouter.class);
} }
/** /**
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.messenger.muc;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;
import org.xmpp.packet.IQ;
/**
* Interface for any object that can accept chat messages and presence
* for delivery.
*
* @author Gaston Dombiak
*/
public interface ChatDeliverer {
/**
* Sends a packet to the user.
*
* @param packet The packet to send
*/
void send(Message packet);
/**
* Sends a packet to the user.
*
* @param packet The packet to send
*/
void send(Presence packet);
/**
* Sends a packet to the user.
*
* @param packet The packet to send
*/
void send(IQ packet);
}
...@@ -13,6 +13,7 @@ package org.jivesoftware.messenger.muc; ...@@ -13,6 +13,7 @@ package org.jivesoftware.messenger.muc;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.xmpp.packet.Packet;
import org.dom4j.Element; import org.dom4j.Element;
/** /**
...@@ -24,56 +25,129 @@ import org.dom4j.Element; ...@@ -24,56 +25,129 @@ import org.dom4j.Element;
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public interface MUCRole extends ChatDeliverer { public interface MUCRole {
/** public enum Role {
* Runs moderated discussions. Is allowed to kick users, grant and revoke voice, etc.
*/ /**
int MODERATOR = 0; * Runs moderated discussions. Is allowed to kick users, grant and revoke voice, etc.
/** */
* A normal occupant of the room. An occupant who does not have administrative privileges; in moderator(0),
* a moderated room, a participant is further defined as having voice
*/ /**
int PARTICIPANT = 1; * A normal occupant of the room. An occupant who does not have administrative privileges; in
/** * a moderated room, a participant is further defined as having voice
* An occupant who does not have voice (can't speak in the room) */
*/ participant(1),
int VISITOR = 2;
/** /**
* An occupant who does not permission to stay in the room (was banned) * An occupant who does not have voice (can't speak in the room)
*/ */
int NONE_ROLE = 3; visitor(2),
/**
/** * An occupant who does not permission to stay in the room (was banned)
* Owner of the room */
*/ none(3);
int OWNER = 10;
/** private int value;
* Administrator of the room
*/ Role(int value) {
int ADMINISTRATOR = 20; this.value = value;
/** }
* A user who is on the "whitelist" for a members-only room or who is registered with an
* open room. /**
*/ * Returns the value for the role.
int MEMBER = 30; *
/** * @return the value.
* A user who has been banned from a room. */
*/ public int getValue() {
int OUTCAST = 40; return value;
/** }
* A user who doesn't have an affiliation. This kind of users can register with members-only
* rooms and may enter an open room. /**
*/ * Returns the affiliation associated with the specified value.
int NONE = 50; *
* @param value the value.
* @return the associated affiliation.
*/
public static Role valueOf(int value) {
switch (value) {
case 0: return moderator;
case 1: return participant;
case 2: return visitor;
default: return none;
}
}
}
public enum Affiliation {
/**
* Owner of the room.
*/
owner(10),
/**
* Administrator of the room.
*/
admin(20),
/**
* A user who is on the "whitelist" for a members-only room or who is registered
* with an open room.
*/
member(30),
/**
* A user who has been banned from a room.
*/
outcast(40),
/**
* A user who doesn't have an affiliation. This kind of users can register with members-only
* rooms and may enter an open room.
*/
none(50);
private int value;
Affiliation(int value) {
this.value = value;
}
/**
* Returns the value for the role.
*
* @return the value.
*/
public int getValue() {
return value;
}
/**
* Returns the affiliation associated with the specified value.
*
* @param value the value.
* @return the associated affiliation.
*/
public static Affiliation valueOf(int value) {
switch (value) {
case 10: return owner;
case 20: return admin;
case 30: return member;
case 40: return outcast;
default: return none;
}
}
}
/** /**
* Obtain the current presence status of a user in a chatroom. * Obtain the current presence status of a user in a chatroom.
* *
* @return The presence of the user in the room. * @return The presence of the user in the room.
*/ */
Presence getPresence(); public Presence getPresence();
/** /**
* Returns the extended presence information that includes information about roles, * Returns the extended presence information that includes information about roles,
...@@ -82,14 +156,14 @@ public interface MUCRole extends ChatDeliverer { ...@@ -82,14 +156,14 @@ public interface MUCRole extends ChatDeliverer {
* @return the extended presence information that includes information about roles, * @return the extended presence information that includes information about roles,
* affiliations. * affiliations.
*/ */
Element getExtendedPresenceInformation(); public Element getExtendedPresenceInformation();
/** /**
* Set the current presence status of a user in a chatroom. * Set the current presence status of a user in a chatroom.
* *
* @param presence The presence of the user in the room. * @param presence The presence of the user in the room.
*/ */
void setPresence(Presence presence); public void setPresence(Presence presence);
/** /**
* Call this method to promote or demote a user's role in a chatroom. * Call this method to promote or demote a user's role in a chatroom.
...@@ -103,78 +177,69 @@ public interface MUCRole extends ChatDeliverer { ...@@ -103,78 +177,69 @@ public interface MUCRole extends ChatDeliverer {
* @throws NotAllowedException Thrown if trying to change the moderator role to an owner or * @throws NotAllowedException Thrown if trying to change the moderator role to an owner or
* administrator. * administrator.
*/ */
void setRole(int newRole) throws NotAllowedException; public void setRole(Role newRole) throws NotAllowedException;
/** /**
* Obtain the role state of the user. * Obtain the role state of the user.
* *
* @return The role status of this user. * @return The role status of this user.
*/ */
int getRole(); public Role getRole();
/**
* Obtain the role state of the user as a String. This string representation will be
* incorporated into the extended packet information.
*
* @return The role status of this user.
*/
public String getRoleAsString();
/** /**
* Call this method to promote or demote a user's affiliation in a chatroom. * Call this method to promote or demote a user's affiliation in a chatroom.
* *
* @param newAffiliation The new affiliation that the user will play. * @param newAffiliation the new affiliation that the user will play.
* @throws NotAllowedException Thrown if trying to ban an owner or an administrator. * @throws NotAllowedException thrown if trying to ban an owner or an administrator.
*/ */
public void setAffiliation(int newAffiliation) throws NotAllowedException; public void setAffiliation(Affiliation newAffiliation) throws NotAllowedException;
/** /**
* Obtain the affiliation state of the user. * Obtain the affiliation state of the user.
* *
* @return The affiliation status of this user. * @return The affiliation status of this user.
*/ */
public int getAffiliation(); public Affiliation getAffiliation();
/**
* Obtain the affiliation state of the user as a String. This string representation will be
* incorporated into the extended packet information.
*
* @return The affiliation status of this user.
*/
public String getAffiliationAsString();
/** /**
* Obtain the nickname for the user in the chatroom. * Obtain the nickname for the user in the chatroom.
* *
* @return The user's nickname in the room or null if invisible. * @return The user's nickname in the room or null if invisible.
*/ */
String getNickname(); public String getNickname();
/** /**
* Changes the nickname of the occupant within the room to the new nickname. * Changes the nickname of the occupant within the room to the new nickname.
* *
* @param nickname the new nickname of the occupant in the room. * @param nickname the new nickname of the occupant in the room.
*/ */
void changeNickname(String nickname); public void changeNickname(String nickname);
/** /**
* Obtain the chat user that plays this role. * Obtain the chat user that plays this role.
* *
* @return The chatuser playing this role. * @return The chatuser playing this role.
*/ */
MUCUser getChatUser(); public MUCUser getChatUser();
/** /**
* Obtain the chat room that hosts this user's role. * Obtain the chat room that hosts this user's role.
* *
* @return The chatroom hosting this role. * @return The chatroom hosting this role.
*/ */
MUCRoom getChatRoom(); public MUCRoom getChatRoom();
/** /**
* Obtain the XMPPAddress representing this role in a room: room@server/nickname * Obtain the XMPPAddress representing this role in a room: room@server/nickname
* *
* @return The Jabber ID that represents this role in the room. * @return The Jabber ID that represents this role in the room.
*/ */
JID getRoleAddress(); public JID getRoleAddress();
}
/**
* Sends a packet to the user.
*
* @param packet The packet to send
*/
public void send(Packet packet);
}
\ No newline at end of file
...@@ -25,6 +25,7 @@ import org.jivesoftware.messenger.user.UserNotFoundException; ...@@ -25,6 +25,7 @@ import org.jivesoftware.messenger.user.UserNotFoundException;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.xmpp.packet.Message; import org.xmpp.packet.Message;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
/** /**
...@@ -33,7 +34,7 @@ import org.xmpp.packet.JID; ...@@ -33,7 +34,7 @@ import org.xmpp.packet.JID;
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public interface MUCRoom extends ChatDeliverer { public interface MUCRoom {
/** /**
* Get the name of this room. * Get the name of this room.
...@@ -182,7 +183,7 @@ public interface MUCRoom extends ChatDeliverer { ...@@ -182,7 +183,7 @@ public interface MUCRoom extends ChatDeliverer {
* @param bareJID The bare jid of the user of which you'd like to obtain his affiliation. * @param bareJID The bare jid of the user of which you'd like to obtain his affiliation.
* @return the affiliation state of the user in the room. * @return the affiliation state of the user in the room.
*/ */
int getAffiliation(String bareJID); MUCRole.Affiliation getAffiliation(String bareJID);
/** /**
* Joins the room using the given nickname. * Joins the room using the given nickname.
...@@ -844,4 +845,11 @@ public interface MUCRoom extends ChatDeliverer { ...@@ -844,4 +845,11 @@ public interface MUCRoom extends ChatDeliverer {
* @param from the JID of the invitee that is rejecting the invitation. * @param from the JID of the invitee that is rejecting the invitation.
*/ */
public void sendInvitationRejection(JID to, String reason, JID from); public void sendInvitationRejection(JID to, String reason, JID from);
/**
* Sends a packet to the user.
*
* @param packet The packet to send
*/
public void send(Packet packet);
} }
\ No newline at end of file
...@@ -125,8 +125,8 @@ public class IQAdminHandler { ...@@ -125,8 +125,8 @@ public class IQAdminHandler {
Element metaData; Element metaData;
if ("outcast".equals(affiliation)) { if ("outcast".equals(affiliation)) {
// The client is requesting the list of outcasts // The client is requesting the list of outcasts
if (MUCRole.ADMINISTRATOR != senderRole.getAffiliation() if (MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.OWNER != senderRole.getAffiliation()) { && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (String jid : room.getOutcasts()) { for (String jid : room.getOutcasts()) {
...@@ -140,8 +140,8 @@ public class IQAdminHandler { ...@@ -140,8 +140,8 @@ public class IQAdminHandler {
// The client is requesting the list of members // The client is requesting the list of members
// In a members-only room members can get the list of members // In a members-only room members can get the list of members
if (!room.isMembersOnly() if (!room.isMembersOnly()
&& MUCRole.ADMINISTRATOR != senderRole.getAffiliation() && MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.OWNER != senderRole.getAffiliation()) { && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (String jid : room.getMembers()) { for (String jid : room.getMembers()) {
...@@ -151,7 +151,7 @@ public class IQAdminHandler { ...@@ -151,7 +151,7 @@ public class IQAdminHandler {
try { try {
List<MUCRole> roles = room.getOccupantsByBareJID(jid); List<MUCRole> roles = room.getOccupantsByBareJID(jid);
MUCRole role = roles.get(0); MUCRole role = roles.get(0);
metaData.addAttribute("role", role.getRoleAsString()); metaData.addAttribute("role", role.getRole().toString());
metaData.addAttribute("nick", role.getNickname()); metaData.addAttribute("nick", role.getNickname());
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
...@@ -161,8 +161,8 @@ public class IQAdminHandler { ...@@ -161,8 +161,8 @@ public class IQAdminHandler {
} }
else if ("moderator".equals(roleAttribute)) { else if ("moderator".equals(roleAttribute)) {
// The client is requesting the list of moderators // The client is requesting the list of moderators
if (MUCRole.ADMINISTRATOR != senderRole.getAffiliation() if (MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.OWNER != senderRole.getAffiliation()) { && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (MUCRole role : room.getModerators()) { for (MUCRole role : room.getModerators()) {
...@@ -170,12 +170,12 @@ public class IQAdminHandler { ...@@ -170,12 +170,12 @@ public class IQAdminHandler {
metaData.addAttribute("role", "moderator"); metaData.addAttribute("role", "moderator");
metaData.addAttribute("jid", role.getChatUser().getAddress().toString()); metaData.addAttribute("jid", role.getChatUser().getAddress().toString());
metaData.addAttribute("nick", role.getNickname()); metaData.addAttribute("nick", role.getNickname());
metaData.addAttribute("affiliation", role.getAffiliationAsString()); metaData.addAttribute("affiliation", role.getAffiliation().toString());
} }
} }
else if ("participant".equals(roleAttribute)) { else if ("participant".equals(roleAttribute)) {
// The client is requesting the list of participants // The client is requesting the list of participants
if (MUCRole.MODERATOR != senderRole.getRole()) { if (MUCRole.Role.moderator != senderRole.getRole()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
for (MUCRole role : room.getParticipants()) { for (MUCRole role : room.getParticipants()) {
...@@ -183,7 +183,7 @@ public class IQAdminHandler { ...@@ -183,7 +183,7 @@ public class IQAdminHandler {
metaData.addAttribute("role", "participant"); metaData.addAttribute("role", "participant");
metaData.addAttribute("jid", role.getChatUser().getAddress().toString()); metaData.addAttribute("jid", role.getChatUser().getAddress().toString());
metaData.addAttribute("nick", role.getNickname()); metaData.addAttribute("nick", role.getNickname());
metaData.addAttribute("affiliation", role.getAffiliationAsString()); metaData.addAttribute("affiliation", role.getAffiliation().toString());
} }
} }
else { else {
...@@ -238,7 +238,7 @@ public class IQAdminHandler { ...@@ -238,7 +238,7 @@ public class IQAdminHandler {
} }
else if ("member".equals(target)) { else if ("member".equals(target)) {
// Add the user as a member of the room based on the bare JID // Add the user as a member of the room based on the bare JID
boolean hadAffiliation = room.getAffiliation(jid.toBareJID()) != MUCRole.NONE; boolean hadAffiliation = room.getAffiliation(jid.toBareJID()) != MUCRole.Affiliation.none;
presences.addAll(room.addMember(jid.toBareJID(), nick, senderRole)); presences.addAll(room.addMember(jid.toBareJID(), nick, senderRole));
// If the user had an affiliation don't send an invitation. Otherwise // If the user had an affiliation don't send an invitation. Otherwise
// send an invitation if the room is members-only // send an invitation if the room is members-only
...@@ -257,7 +257,7 @@ public class IQAdminHandler { ...@@ -257,7 +257,7 @@ public class IQAdminHandler {
} }
else { else {
// Kick the user from the room // Kick the user from the room
if (MUCRole.MODERATOR != senderRole.getRole()) { if (MUCRole.Role.moderator != senderRole.getRole()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
presences.add(room.kickOccupant(jid, presences.add(room.kickOccupant(jid,
......
...@@ -74,7 +74,7 @@ public class IQOwnerHandler { ...@@ -74,7 +74,7 @@ public class IQOwnerHandler {
*/ */
public void handleIQ(IQ packet, MUCRole role) throws ForbiddenException, ConflictException { public void handleIQ(IQ packet, MUCRole role) throws ForbiddenException, ConflictException {
// Only owners can send packets with the namespace "http://jabber.org/protocol/muc#owner" // Only owners can send packets with the namespace "http://jabber.org/protocol/muc#owner"
if (MUCRole.OWNER != role.getAffiliation()) { if (MUCRole.Affiliation.owner != role.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
...@@ -159,7 +159,7 @@ public class IQOwnerHandler { ...@@ -159,7 +159,7 @@ public class IQOwnerHandler {
try { try {
List<MUCRole> roles = room.getOccupantsByBareJID(jid); List<MUCRole> roles = room.getOccupantsByBareJID(jid);
role = roles.get(0); role = roles.get(0);
ownerMetaData.addAttribute("role", role.getRoleAsString()); ownerMetaData.addAttribute("role", role.getRole().toString());
ownerMetaData.addAttribute("nick", role.getNickname()); ownerMetaData.addAttribute("nick", role.getNickname());
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
...@@ -179,7 +179,7 @@ public class IQOwnerHandler { ...@@ -179,7 +179,7 @@ public class IQOwnerHandler {
try { try {
List<MUCRole> roles = room.getOccupantsByBareJID(jid); List<MUCRole> roles = room.getOccupantsByBareJID(jid);
role = roles.get(0); role = roles.get(0);
adminMetaData.addAttribute("role", role.getRoleAsString()); adminMetaData.addAttribute("role", role.getRole().toString());
adminMetaData.addAttribute("nick", role.getNickname()); adminMetaData.addAttribute("nick", role.getNickname());
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
...@@ -247,7 +247,7 @@ public class IQOwnerHandler { ...@@ -247,7 +247,7 @@ public class IQOwnerHandler {
} }
else if ("member".equals(targetAffiliation)) { else if ("member".equals(targetAffiliation)) {
// Add the new user as a member of the room // Add the new user as a member of the room
boolean hadAffiliation = room.getAffiliation(bareJID) != MUCRole.NONE; boolean hadAffiliation = room.getAffiliation(bareJID) != MUCRole.Affiliation.none;
presences.addAll(room.addMember(bareJID, null, senderRole)); presences.addAll(room.addMember(bareJID, null, senderRole));
// If the user had an affiliation don't send an invitation. Otherwise // If the user had an affiliation don't send an invitation. Otherwise
// send an invitation if the room is members-only // send an invitation if the room is members-only
......
...@@ -223,16 +223,16 @@ public class MUCPersistenceManager { ...@@ -223,16 +223,16 @@ public class MUCPersistenceManager {
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String jid = rs.getString(1); String jid = rs.getString(1);
int affiliation = rs.getInt(2); MUCRole.Affiliation affiliation = MUCRole.Affiliation.valueOf(rs.getInt(2));
try { try {
switch (affiliation) { switch (affiliation) {
case MUCRole.OWNER: case owner:
room.addOwner(jid, room.getRole()); room.addOwner(jid, room.getRole());
break; break;
case MUCRole.ADMINISTRATOR: case admin:
room.addAdmin(jid, room.getRole()); room.addAdmin(jid, room.getRole());
break; break;
case MUCRole.OUTCAST: case outcast:
room.addOutcast(jid, null, room.getRole()); room.addOutcast(jid, null, room.getRole());
break; break;
default: default:
...@@ -498,7 +498,7 @@ public class MUCPersistenceManager { ...@@ -498,7 +498,7 @@ public class MUCPersistenceManager {
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String jid = rs.getString(2); String jid = rs.getString(2);
int affiliation = rs.getInt(3); MUCRole.Affiliation affiliation = MUCRole.Affiliation.valueOf(rs.getInt(3));
room = (MUCRoomImpl) rooms.get(rs.getLong(1)); room = (MUCRoomImpl) rooms.get(rs.getLong(1));
// Skip to the next position if the room does not exist // Skip to the next position if the room does not exist
if (room == null) { if (room == null) {
...@@ -506,13 +506,13 @@ public class MUCPersistenceManager { ...@@ -506,13 +506,13 @@ public class MUCPersistenceManager {
} }
try { try {
switch (affiliation) { switch (affiliation) {
case MUCRole.OWNER: case owner:
room.addOwner(jid, room.getRole()); room.addOwner(jid, room.getRole());
break; break;
case MUCRole.ADMINISTRATOR: case admin:
room.addAdmin(jid, room.getRole()); room.addAdmin(jid, room.getRole());
break; break;
case MUCRole.OUTCAST: case outcast:
room.addOutcast(jid, null, room.getRole()); room.addOutcast(jid, null, room.getRole());
break; break;
default: default:
...@@ -676,13 +676,13 @@ public class MUCPersistenceManager { ...@@ -676,13 +676,13 @@ public class MUCPersistenceManager {
* @param oldAffiliation the previous affiliation of the user in the room. * @param oldAffiliation the previous affiliation of the user in the room.
*/ */
public static void saveAffiliationToDB(MUCRoom room, String bareJID, String nickname, public static void saveAffiliationToDB(MUCRoom room, String bareJID, String nickname,
int newAffiliation, int oldAffiliation) MUCRole.Affiliation newAffiliation, MUCRole.Affiliation oldAffiliation)
{ {
if (!room.isPersistent() || !room.wasSavedToDB()) { if (!room.isPersistent() || !room.wasSavedToDB()) {
return; return;
} }
if (MUCRole.NONE == oldAffiliation) { if (MUCRole.Affiliation.none == oldAffiliation) {
if (MUCRole.MEMBER == newAffiliation) { if (MUCRole.Affiliation.member == newAffiliation) {
// Add the user to the members table // Add the user to the members table
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
...@@ -713,7 +713,7 @@ public class MUCPersistenceManager { ...@@ -713,7 +713,7 @@ public class MUCPersistenceManager {
pstmt = con.prepareStatement(ADD_AFFILIATION); pstmt = con.prepareStatement(ADD_AFFILIATION);
pstmt.setLong(1, room.getID()); pstmt.setLong(1, room.getID());
pstmt.setString(2, bareJID); pstmt.setString(2, bareJID);
pstmt.setInt(3, newAffiliation); pstmt.setInt(3, newAffiliation.getValue());
pstmt.executeUpdate(); pstmt.executeUpdate();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
...@@ -728,7 +728,9 @@ public class MUCPersistenceManager { ...@@ -728,7 +728,9 @@ public class MUCPersistenceManager {
} }
} }
else { else {
if (MUCRole.MEMBER == newAffiliation && MUCRole.MEMBER == oldAffiliation) { if (MUCRole.Affiliation.member == newAffiliation &&
MUCRole.Affiliation.member == oldAffiliation)
{
// Update the member's data in the member table. // Update the member's data in the member table.
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
...@@ -750,7 +752,7 @@ public class MUCPersistenceManager { ...@@ -750,7 +752,7 @@ public class MUCPersistenceManager {
catch (Exception e) { Log.error(e); } catch (Exception e) { Log.error(e); }
} }
} }
else if (MUCRole.MEMBER == newAffiliation) { else if (MUCRole.Affiliation.member == newAffiliation) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
boolean abortTransaction = false; boolean abortTransaction = false;
...@@ -780,7 +782,7 @@ public class MUCPersistenceManager { ...@@ -780,7 +782,7 @@ public class MUCPersistenceManager {
DbConnectionManager.closeTransactionConnection(con, abortTransaction); DbConnectionManager.closeTransactionConnection(con, abortTransaction);
} }
} }
else if (MUCRole.MEMBER == oldAffiliation) { else if (MUCRole.Affiliation.member == oldAffiliation) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
boolean abortTransaction = false; boolean abortTransaction = false;
...@@ -795,7 +797,7 @@ public class MUCPersistenceManager { ...@@ -795,7 +797,7 @@ public class MUCPersistenceManager {
pstmt = con.prepareStatement(ADD_AFFILIATION); pstmt = con.prepareStatement(ADD_AFFILIATION);
pstmt.setLong(1, room.getID()); pstmt.setLong(1, room.getID());
pstmt.setString(2, bareJID); pstmt.setString(2, bareJID);
pstmt.setInt(3, newAffiliation); pstmt.setInt(3, newAffiliation.getValue());
pstmt.executeUpdate(); pstmt.executeUpdate();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
...@@ -815,7 +817,7 @@ public class MUCPersistenceManager { ...@@ -815,7 +817,7 @@ public class MUCPersistenceManager {
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_AFFILIATION); pstmt = con.prepareStatement(UPDATE_AFFILIATION);
pstmt.setInt(1, newAffiliation); pstmt.setInt(1, newAffiliation.getValue());
pstmt.setLong(2, room.getID()); pstmt.setLong(2, room.getID());
pstmt.setString(3, bareJID); pstmt.setString(3, bareJID);
pstmt.executeUpdate(); pstmt.executeUpdate();
...@@ -840,9 +842,11 @@ public class MUCPersistenceManager { ...@@ -840,9 +842,11 @@ public class MUCPersistenceManager {
* @param bareJID The bareJID of the user to remove his affiliation. * @param bareJID The bareJID of the user to remove his affiliation.
* @param oldAffiliation the previous affiliation of the user in the room. * @param oldAffiliation the previous affiliation of the user in the room.
*/ */
public static void removeAffiliationFromDB(MUCRoom room, String bareJID, int oldAffiliation) { public static void removeAffiliationFromDB(MUCRoom room, String bareJID,
MUCRole.Affiliation oldAffiliation)
{
if (room.isPersistent() && room.wasSavedToDB()) { if (room.isPersistent() && room.wasSavedToDB()) {
if (MUCRole.MEMBER == oldAffiliation) { if (MUCRole.Affiliation.member == oldAffiliation) {
// Remove the user from the members table // Remove the user from the members table
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
......
...@@ -21,17 +21,13 @@ import org.jivesoftware.messenger.muc.MUCUser; ...@@ -21,17 +21,13 @@ import org.jivesoftware.messenger.muc.MUCUser;
import org.jivesoftware.messenger.muc.MultiUserChatServer; import org.jivesoftware.messenger.muc.MultiUserChatServer;
import org.jivesoftware.messenger.muc.NotAllowedException; import org.jivesoftware.messenger.muc.NotAllowedException;
import org.jivesoftware.util.ElementUtil; import org.jivesoftware.util.ElementUtil;
import org.xmpp.packet.IQ; import org.xmpp.packet.*;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;
/** /**
* Simple in-memory implementation of a role in a chatroom * Simple in-memory implementation of a role in a chatroom
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
// TODO Review name of this class that represents role and affiliation at the same time.!!!!
public class MUCRoleImpl implements MUCRole { public class MUCRoleImpl implements MUCRole {
/** /**
...@@ -60,14 +56,14 @@ public class MUCRoleImpl implements MUCRole { ...@@ -60,14 +56,14 @@ public class MUCRoleImpl implements MUCRole {
private MultiUserChatServer server; private MultiUserChatServer server;
/** /**
* The role ID. * The role.
*/ */
private int role; private MUCRole.Role role;
/** /**
* The affiliation ID. * The affiliation.
*/ */
private int affiliation; private MUCRole.Affiliation affiliation;
/** /**
* The router used to send packets from this role. * The router used to send packets from this role.
...@@ -97,7 +93,7 @@ public class MUCRoleImpl implements MUCRole { ...@@ -97,7 +93,7 @@ public class MUCRoleImpl implements MUCRole {
* @param packetRouter the packet router for sending messages from this role. * @param packetRouter the packet router for sending messages from this role.
*/ */
public MUCRoleImpl(MultiUserChatServer chatserver, MUCRoomImpl chatroom, String nickname, public MUCRoleImpl(MultiUserChatServer chatserver, MUCRoomImpl chatroom, String nickname,
int role, int affiliation, MUCUserImpl chatuser, Presence presence, MUCRole.Role role, MUCRole.Affiliation affiliation, MUCUserImpl chatuser, Presence presence,
PacketRouter packetRouter) PacketRouter packetRouter)
{ {
this.room = chatroom; this.room = chatroom;
...@@ -137,49 +133,36 @@ public class MUCRoleImpl implements MUCRole { ...@@ -137,49 +133,36 @@ public class MUCRoleImpl implements MUCRole {
} }
} }
public void setRole(int newRole) throws NotAllowedException { public void setRole(MUCRole.Role newRole) throws NotAllowedException {
// Don't allow to change the role to an owner or admin unless the new role is moderator // Don't allow to change the role to an owner or admin unless the new role is moderator
if (MUCRole.OWNER == affiliation || MUCRole.ADMINISTRATOR == affiliation) { if (MUCRole.Affiliation.owner == affiliation || MUCRole.Affiliation.admin == affiliation) {
if (MUCRole.MODERATOR != newRole) { if (MUCRole.Role.moderator != newRole) {
throw new NotAllowedException(); throw new NotAllowedException();
} }
} }
// A moderator cannot be kicked from a room // A moderator cannot be kicked from a room
if (MUCRole.MODERATOR == role && MUCRole.NONE_ROLE == newRole) { if (MUCRole.Role.moderator == role && MUCRole.Role.none == newRole) {
throw new NotAllowedException(); throw new NotAllowedException();
} }
// TODO A moderator MUST NOT be able to revoke voice from a user whose affiliation is at or // TODO A moderator MUST NOT be able to revoke voice from a user whose affiliation is at or
// above the moderator's level. // TODO above the moderator's level.
role = newRole; role = newRole;
if (MUCRole.NONE_ROLE == role) { if (MUCRole.Role.none == role) {
presence.setType(Presence.Type.unavailable); presence.setType(Presence.Type.unavailable);
presence.setStatus(null); presence.setStatus(null);
} }
calculateExtendedInformation(); calculateExtendedInformation();
} }
public int getRole() { public MUCRole.Role getRole() {
return role; return role;
} }
public String getRoleAsString() { public void setAffiliation(MUCRole.Affiliation newAffiliation) throws NotAllowedException {
if (MUCRole.MODERATOR == role) {
return "moderator";
}
else if (MUCRole.PARTICIPANT == role) {
return "participant";
}
else if (MUCRole.VISITOR == role) {
return "visitor";
}
return "none";
}
public void setAffiliation(int newAffiliation) throws NotAllowedException {
// Don't allow to ban an owner or an admin // Don't allow to ban an owner or an admin
if (MUCRole.OWNER == affiliation || MUCRole.ADMINISTRATOR == affiliation) { if (MUCRole.Affiliation.owner == affiliation || MUCRole.Affiliation.admin== affiliation) {
if (MUCRole.OUTCAST == newAffiliation) { if (MUCRole.Affiliation.outcast == newAffiliation) {
throw new NotAllowedException(); throw new NotAllowedException();
} }
} }
...@@ -188,26 +171,10 @@ public class MUCRoleImpl implements MUCRole { ...@@ -188,26 +171,10 @@ public class MUCRoleImpl implements MUCRole {
calculateExtendedInformation(); calculateExtendedInformation();
} }
public int getAffiliation() { public MUCRole.Affiliation getAffiliation() {
return affiliation; return affiliation;
} }
public String getAffiliationAsString() {
if (MUCRole.OWNER == affiliation) {
return "owner";
}
else if (MUCRole.ADMINISTRATOR == affiliation) {
return "admin";
}
else if (MUCRole.MEMBER == affiliation) {
return "member";
}
else if (MUCRole.OUTCAST == affiliation) {
return "outcast";
}
return "none";
}
public String getNickname() { public String getNickname() {
return nick; return nick;
} }
...@@ -235,17 +202,7 @@ public class MUCRoleImpl implements MUCRole { ...@@ -235,17 +202,7 @@ public class MUCRoleImpl implements MUCRole {
presence.setFrom(jid); presence.setFrom(jid);
} }
public void send(Presence packet) { public void send(Packet packet) {
packet.setTo(user.getAddress());
router.route(packet);
}
public void send(Message packet) {
packet.setTo(user.getAddress());
router.route(packet);
}
public void send(IQ packet) {
packet.setTo(user.getAddress()); packet.setTo(user.getAddress());
router.route(packet); router.route(packet);
} }
...@@ -256,7 +213,7 @@ public class MUCRoleImpl implements MUCRole { ...@@ -256,7 +213,7 @@ public class MUCRoleImpl implements MUCRole {
*/ */
private void calculateExtendedInformation() { private void calculateExtendedInformation() {
ElementUtil.setProperty(extendedInformation, "x.item:jid", user.getAddress().toString()); ElementUtil.setProperty(extendedInformation, "x.item:jid", user.getAddress().toString());
ElementUtil.setProperty(extendedInformation, "x.item:affiliation", getAffiliationAsString()); ElementUtil.setProperty(extendedInformation, "x.item:affiliation", affiliation.toString());
ElementUtil.setProperty(extendedInformation, "x.item:role", getRoleAsString()); ElementUtil.setProperty(extendedInformation, "x.item:role", role.toString());
} }
} }
\ No newline at end of file
...@@ -371,20 +371,20 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -371,20 +371,20 @@ public class MUCRoomImpl implements MUCRoom {
return answer; return answer;
} }
public int getAffiliation(String bareJID) { public MUCRole.Affiliation getAffiliation(String bareJID) {
if (owners.contains(bareJID)) { if (owners.contains(bareJID)) {
return MUCRole.OWNER; return MUCRole.Affiliation.owner;
} }
else if (admins.contains(bareJID)) { else if (admins.contains(bareJID)) {
return MUCRole.ADMINISTRATOR; return MUCRole.Affiliation.admin;
} }
else if (members.containsKey(bareJID)) { else if (members.containsKey(bareJID)) {
return MUCRole.MEMBER; return MUCRole.Affiliation.member;
} }
else if (outcasts.contains(bareJID)) { else if (outcasts.contains(bareJID)) {
return MUCRole.OUTCAST; return MUCRole.Affiliation.outcast;
} }
return MUCRole.NONE; return MUCRole.Affiliation.none;
} }
public MUCRole joinRoom(String nickname, String password, HistoryRequest historyRequest, public MUCRole joinRoom(String nickname, String password, HistoryRequest historyRequest,
...@@ -425,28 +425,28 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -425,28 +425,28 @@ public class MUCRoomImpl implements MUCRoom {
} }
// Set the corresponding role based on the user's affiliation // Set the corresponding role based on the user's affiliation
int role; MUCRole.Role role;
int affiliation; MUCRole.Affiliation affiliation;
if (isOwner) { if (isOwner) {
// The user is an owner. Set the role and affiliation accordingly. // The user is an owner. Set the role and affiliation accordingly.
role = MUCRole.MODERATOR; role = MUCRole.Role.moderator;
affiliation = MUCRole.OWNER; affiliation = MUCRole.Affiliation.owner;
} }
else if (server.getSysadmins().contains(user.getAddress().toBareJID())) { else if (server.getSysadmins().contains(user.getAddress().toBareJID())) {
// The user is a system administrator of the MUC service. Treat him as an owner // The user is a system administrator of the MUC service. Treat him as an owner
// although he won't appear in the list of owners // although he won't appear in the list of owners
role = MUCRole.MODERATOR; role = MUCRole.Role.moderator;
affiliation = MUCRole.OWNER; affiliation = MUCRole.Affiliation.owner;
} }
else if (admins.contains(user.getAddress().toBareJID())) { else if (admins.contains(user.getAddress().toBareJID())) {
// The user is an admin. Set the role and affiliation accordingly. // The user is an admin. Set the role and affiliation accordingly.
role = MUCRole.MODERATOR; role = MUCRole.Role.moderator;
affiliation = MUCRole.ADMINISTRATOR; affiliation = MUCRole.Affiliation.admin;
} }
else if (members.containsKey(user.getAddress().toBareJID())) { else if (members.containsKey(user.getAddress().toBareJID())) {
// The user is a member. Set the role and affiliation accordingly. // The user is a member. Set the role and affiliation accordingly.
role = MUCRole.PARTICIPANT; role = MUCRole.Role.participant;
affiliation = MUCRole.MEMBER; affiliation = MUCRole.Affiliation.member;
} }
else if (outcasts.contains(user.getAddress().toBareJID())) { else if (outcasts.contains(user.getAddress().toBareJID())) {
// The user is an outcast. Raise a "Forbidden" exception. // The user is an outcast. Raise a "Forbidden" exception.
...@@ -459,8 +459,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -459,8 +459,8 @@ public class MUCRoomImpl implements MUCRoom {
// "Registration Required" exception. // "Registration Required" exception.
throw new RegistrationRequiredException(); throw new RegistrationRequiredException();
} }
role = (isModerated() ? MUCRole.VISITOR : MUCRole.PARTICIPANT); role = (isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant);
affiliation = MUCRole.NONE; affiliation = MUCRole.Affiliation.none;
} }
// Create a new role for this user in this room // Create a new role for this user in this room
joinRole = joinRole =
...@@ -562,7 +562,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -562,7 +562,7 @@ public class MUCRoomImpl implements MUCRoom {
} }
// Don't include the occupant's JID if the room is semi-anon and the new occupant // Don't include the occupant's JID if the room is semi-anon and the new occupant
// is not a moderator // is not a moderator
if (!canAnyoneDiscoverJID() && MUCRole.MODERATOR != joinRole.getRole()) { if (!canAnyoneDiscoverJID() && MUCRole.Role.moderator != joinRole.getRole()) {
Element frag = occupantPresence.getChildElement("x", Element frag = occupantPresence.getChildElement("x",
"http://jabber.org/protocol/muc#user"); "http://jabber.org/protocol/muc#user");
frag.element("item").addAttribute("jid", null); frag.element("item").addAttribute("jid", null);
...@@ -715,7 +715,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -715,7 +715,7 @@ public class MUCRoomImpl implements MUCRoom {
public void sendPublicMessage(Message message, MUCRole senderRole) throws ForbiddenException { public void sendPublicMessage(Message message, MUCRole senderRole) throws ForbiddenException {
// Check that if the room is moderated then the sender of the message has to have voice // Check that if the room is moderated then the sender of the message has to have voice
if (isModerated() && senderRole.getRole() > MUCRole.PARTICIPANT) { if (isModerated() && senderRole.getRole().compareTo(MUCRole.Role.participant) > 0) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
// Send the message to all occupants // Send the message to all occupants
...@@ -735,22 +735,21 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -735,22 +735,21 @@ public class MUCRoomImpl implements MUCRoom {
} }
} }
public void send(Message packet) { public void send(Packet packet) {
// normal groupchat if (packet instanceof Message) {
roomHistory.addMessage(packet); roomHistory.addMessage((Message)packet);
broadcast(packet); broadcast((Message)packet);
} }
else if (packet instanceof Presence) {
public void send(Presence packet) { broadcastPresence((Presence)packet);
broadcastPresence(packet); }
} else if (packet instanceof IQ) {
packet = packet.createCopy();
public void send(IQ packet) { packet.setError(PacketError.Condition.bad_request);
packet = packet.createCopy(); packet.setTo(packet.getFrom());
packet.setError(PacketError.Condition.bad_request); packet.setFrom(role.getRoleAddress());
packet.setTo(packet.getFrom()); router.route(packet);
packet.setFrom(role.getRoleAddress()); }
router.route(packet);
} }
/** /**
...@@ -795,7 +794,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -795,7 +794,7 @@ public class MUCRoomImpl implements MUCRoom {
// Don't include the occupant's JID if the room is semi-anon and the new occupant // Don't include the occupant's JID if the room is semi-anon and the new occupant
// is not a moderator // is not a moderator
if (!canAnyoneDiscoverJID()) { if (!canAnyoneDiscoverJID()) {
if (MUCRole.MODERATOR == occupant.getRole()) { if (MUCRole.Role.moderator == occupant.getRole()) {
frag.element("item").addAttribute("jid", jid); frag.element("item").addAttribute("jid", jid);
} }
else { else {
...@@ -852,26 +851,18 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -852,26 +851,18 @@ public class MUCRoomImpl implements MUCRoom {
public void setPresence(Presence presence) { public void setPresence(Presence presence) {
} }
public void setRole(int newRole) { public void setRole(MUCRole.Role newRole) {
}
public int getRole() {
return MUCRole.MODERATOR;
}
public String getRoleAsString() {
return "moderator";
} }
public void setAffiliation(int newAffiliation) { public MUCRole.Role getRole() {
return MUCRole.Role.moderator;
} }
public int getAffiliation() { public void setAffiliation(MUCRole.Affiliation newAffiliation) {
return MUCRole.OWNER;
} }
public String getAffiliationAsString() { public MUCRole.Affiliation getAffiliation() {
return "owner"; return MUCRole.Affiliation.owner;
} }
public String getNickname() { public String getNickname() {
...@@ -895,15 +886,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -895,15 +886,7 @@ public class MUCRoomImpl implements MUCRoom {
return crJID; return crJID;
} }
public void send(Message packet) { public void send(Packet packet) {
room.send(packet);
}
public void send(Presence packet) {
room.send(packet);
}
public void send(IQ packet) {
room.send(packet); room.send(packet);
} }
...@@ -928,7 +911,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -928,7 +911,7 @@ public class MUCRoomImpl implements MUCRoom {
* @throws NotAllowedException If trying to change the moderator role to an owner or an admin or * @throws NotAllowedException If trying to change the moderator role to an owner or an admin or
* if trying to ban an owner or an administrator. * if trying to ban an owner or an administrator.
*/ */
private List<Presence> changeOccupantAffiliation(String bareJID, int newAffiliation, int newRole) private List<Presence> changeOccupantAffiliation(String bareJID, MUCRole.Affiliation newAffiliation, MUCRole.Role newRole)
throws NotAllowedException { throws NotAllowedException {
List<Presence> presences = new ArrayList<Presence>(); List<Presence> presences = new ArrayList<Presence>();
// Get all the roles (i.e. occupants) of this user based on his/her bare JID // Get all the roles (i.e. occupants) of this user based on his/her bare JID
...@@ -957,7 +940,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -957,7 +940,7 @@ public class MUCRoomImpl implements MUCRoom {
* @return the updated presence of the user or null if none. * @return the updated presence of the user or null if none.
* @throws NotAllowedException If trying to change the moderator role to an owner or an admin. * @throws NotAllowedException If trying to change the moderator role to an owner or an admin.
*/ */
private Presence changeOccupantRole(JID jid, int newRole) throws NotAllowedException { private Presence changeOccupantRole(JID jid, MUCRole.Role newRole) throws NotAllowedException {
// Try looking the role in the bare JID list // Try looking the role in the bare JID list
MUCRole role = occupantsByFullJID.get(jid); MUCRole role = occupantsByFullJID.get(jid);
if (role != null) { if (role != null) {
...@@ -974,31 +957,32 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -974,31 +957,32 @@ public class MUCRoomImpl implements MUCRoom {
} }
public List<Presence> addOwner(String bareJID, MUCRole sendRole) throws ForbiddenException { public List<Presence> addOwner(String bareJID, MUCRole sendRole) throws ForbiddenException {
int oldAffiliation = MUCRole.NONE; MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
if (MUCRole.OWNER != sendRole.getAffiliation()) { if (MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
owners.add(bareJID); owners.add(bareJID);
// Remove the user from other affiliation lists // Remove the user from other affiliation lists
if (removeAdmin(bareJID)) { if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.ADMINISTRATOR; oldAffiliation = MUCRole.Affiliation.admin;
} }
else if (removeMember(bareJID)) { else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.MEMBER; oldAffiliation = MUCRole.Affiliation.member;
} }
else if (removeOutcast(bareJID)) { else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.OUTCAST; oldAffiliation = MUCRole.Affiliation.outcast;
} }
// Update the DB if the room is persistent // Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB( MUCPersistenceManager.saveAffiliationToDB(
this, this,
bareJID, bareJID,
null, null,
MUCRole.OWNER, MUCRole.Affiliation.owner,
oldAffiliation); oldAffiliation);
// Update the presence with the new affiliation and inform all occupants // Update the presence with the new affiliation and inform all occupants
try { try {
return changeOccupantAffiliation(bareJID, MUCRole.OWNER, MUCRole.MODERATOR); return changeOccupantAffiliation(bareJID, MUCRole.Affiliation.owner,
MUCRole.Role.moderator);
} }
catch (NotAllowedException e) { catch (NotAllowedException e) {
// We should never receive this exception....in theory // We should never receive this exception....in theory
...@@ -1012,8 +996,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1012,8 +996,8 @@ public class MUCRoomImpl implements MUCRoom {
public List<Presence> addAdmin(String bareJID, MUCRole sendRole) throws ForbiddenException, public List<Presence> addAdmin(String bareJID, MUCRole sendRole) throws ForbiddenException,
ConflictException { ConflictException {
int oldAffiliation = MUCRole.NONE; MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
if (MUCRole.OWNER != sendRole.getAffiliation()) { if (MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
// Check that the room always has an owner // Check that the room always has an owner
...@@ -1023,24 +1007,25 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1023,24 +1007,25 @@ public class MUCRoomImpl implements MUCRoom {
admins.add(bareJID); admins.add(bareJID);
// Remove the user from other affiliation lists // Remove the user from other affiliation lists
if (removeOwner(bareJID)) { if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.OWNER; oldAffiliation = MUCRole.Affiliation.owner;
} }
else if (removeMember(bareJID)) { else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.MEMBER; oldAffiliation = MUCRole.Affiliation.member;
} }
else if (removeOutcast(bareJID)) { else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.OUTCAST; oldAffiliation = MUCRole.Affiliation.outcast;
} }
// Update the DB if the room is persistent // Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB( MUCPersistenceManager.saveAffiliationToDB(
this, this,
bareJID, bareJID,
null, null,
MUCRole.ADMINISTRATOR, MUCRole.Affiliation.admin,
oldAffiliation); oldAffiliation);
// Update the presence with the new affiliation and inform all occupants // Update the presence with the new affiliation and inform all occupants
try { try {
return changeOccupantAffiliation(bareJID, MUCRole.ADMINISTRATOR, MUCRole.MODERATOR); return changeOccupantAffiliation(bareJID, MUCRole.Affiliation.admin,
MUCRole.Role.moderator);
} }
catch (NotAllowedException e) { catch (NotAllowedException e) {
// We should never receive this exception....in theory // We should never receive this exception....in theory
...@@ -1054,18 +1039,19 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1054,18 +1039,19 @@ public class MUCRoomImpl implements MUCRoom {
public List<Presence> addMember(String bareJID, String nickname, MUCRole sendRole) public List<Presence> addMember(String bareJID, String nickname, MUCRole sendRole)
throws ForbiddenException, ConflictException { throws ForbiddenException, ConflictException {
int oldAffiliation = (members.containsKey(bareJID) ? MUCRole.MEMBER : MUCRole.NONE); MUCRole.Affiliation oldAffiliation = (members.containsKey(bareJID) ?
MUCRole.Affiliation.member : MUCRole.Affiliation.none);
if (isMembersOnly()) { if (isMembersOnly()) {
if (!canOccupantsInvite()) { if (!canOccupantsInvite()) {
if (MUCRole.ADMINISTRATOR != sendRole.getAffiliation() if (MUCRole.Affiliation.admin != sendRole.getAffiliation()
&& MUCRole.OWNER != sendRole.getAffiliation()) { && MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
} }
} }
else { else {
if (MUCRole.ADMINISTRATOR != sendRole.getAffiliation() if (MUCRole.Affiliation.admin != sendRole.getAffiliation()
&& MUCRole.OWNER != sendRole.getAffiliation()) { && MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
} }
...@@ -1084,24 +1070,25 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1084,24 +1070,25 @@ public class MUCRoomImpl implements MUCRoom {
members.put(bareJID, (nickname == null ? "" : nickname)); members.put(bareJID, (nickname == null ? "" : nickname));
// Remove the user from other affiliation lists // Remove the user from other affiliation lists
if (removeOwner(bareJID)) { if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.OWNER; oldAffiliation = MUCRole.Affiliation.owner;
} }
else if (removeAdmin(bareJID)) { else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.ADMINISTRATOR; oldAffiliation = MUCRole.Affiliation.admin;
} }
else if (removeOutcast(bareJID)) { else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.OUTCAST; oldAffiliation = MUCRole.Affiliation.outcast;
} }
// Update the DB if the room is persistent // Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB( MUCPersistenceManager.saveAffiliationToDB(
this, this,
bareJID, bareJID,
nickname, nickname,
MUCRole.MEMBER, MUCRole.Affiliation.member,
oldAffiliation); oldAffiliation);
// Update the presence with the new affiliation and inform all occupants // Update the presence with the new affiliation and inform all occupants
try { try {
return changeOccupantAffiliation(bareJID, MUCRole.MEMBER, MUCRole.PARTICIPANT); return changeOccupantAffiliation(bareJID, MUCRole.Affiliation.member,
MUCRole.Role.participant);
} }
catch (NotAllowedException e) { catch (NotAllowedException e) {
// We should never receive this exception....in theory // We should never receive this exception....in theory
...@@ -1117,9 +1104,9 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1117,9 +1104,9 @@ public class MUCRoomImpl implements MUCRoom {
public List<Presence> addOutcast(String bareJID, String reason, MUCRole senderRole) public List<Presence> addOutcast(String bareJID, String reason, MUCRole senderRole)
throws NotAllowedException, ForbiddenException, ConflictException { throws NotAllowedException, ForbiddenException, ConflictException {
int oldAffiliation = MUCRole.NONE; MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
if (MUCRole.ADMINISTRATOR != senderRole.getAffiliation() if (MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.OWNER != senderRole.getAffiliation()) { && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
// Check that the room always has an owner // Check that the room always has an owner
...@@ -1134,8 +1121,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1134,8 +1121,8 @@ public class MUCRoomImpl implements MUCRoom {
} }
List<Presence> updatedPresences = changeOccupantAffiliation( List<Presence> updatedPresences = changeOccupantAffiliation(
bareJID, bareJID,
MUCRole.OUTCAST, MUCRole.Affiliation.outcast,
MUCRole.NONE_ROLE); MUCRole.Role.none);
Element frag; Element frag;
// Add the status code and reason why the user was banned to the presences that will // Add the status code and reason why the user was banned to the presences that will
// be sent to the room occupants (the banned user will not receive this presences) // be sent to the room occupants (the banned user will not receive this presences)
...@@ -1157,20 +1144,20 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1157,20 +1144,20 @@ public class MUCRoomImpl implements MUCRoom {
outcasts.add(bareJID); outcasts.add(bareJID);
// Remove the user from other affiliation lists // Remove the user from other affiliation lists
if (removeOwner(bareJID)) { if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.OWNER; oldAffiliation = MUCRole.Affiliation.owner;
} }
else if (removeAdmin(bareJID)) { else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.ADMINISTRATOR; oldAffiliation = MUCRole.Affiliation.admin;
} }
else if (removeMember(bareJID)) { else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.MEMBER; oldAffiliation = MUCRole.Affiliation.member;
} }
// Update the DB if the room is persistent // Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB( MUCPersistenceManager.saveAffiliationToDB(
this, this,
bareJID, bareJID,
null, null,
MUCRole.OUTCAST, MUCRole.Affiliation.outcast,
oldAffiliation); oldAffiliation);
return updatedPresences; return updatedPresences;
} }
...@@ -1181,9 +1168,9 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1181,9 +1168,9 @@ public class MUCRoomImpl implements MUCRoom {
public List<Presence> addNone(String bareJID, MUCRole senderRole) throws ForbiddenException, public List<Presence> addNone(String bareJID, MUCRole senderRole) throws ForbiddenException,
ConflictException { ConflictException {
int oldAffiliation = MUCRole.NONE; MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
if (MUCRole.ADMINISTRATOR != senderRole.getAffiliation() if (MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.OWNER != senderRole.getAffiliation()) { && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
// Check that the room always has an owner // Check that the room always has an owner
...@@ -1195,30 +1182,30 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1195,30 +1182,30 @@ public class MUCRoomImpl implements MUCRoom {
owners.contains(bareJID); owners.contains(bareJID);
// Remove the user from ALL the affiliation lists // Remove the user from ALL the affiliation lists
if (removeOwner(bareJID)) { if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.OWNER; oldAffiliation = MUCRole.Affiliation.owner;
} }
else if (removeAdmin(bareJID)) { else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.ADMINISTRATOR; oldAffiliation = MUCRole.Affiliation.admin;
} }
else if (removeMember(bareJID)) { else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.MEMBER; oldAffiliation = MUCRole.Affiliation.member;
} }
else if (removeOutcast(bareJID)) { else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.OUTCAST; oldAffiliation = MUCRole.Affiliation.outcast;
} }
// Remove the affiliation of this user from the DB if the room is persistent // Remove the affiliation of this user from the DB if the room is persistent
MUCPersistenceManager.removeAffiliationFromDB(this, bareJID, oldAffiliation); MUCPersistenceManager.removeAffiliationFromDB(this, bareJID, oldAffiliation);
// Update the presence with the new affiliation and inform all occupants // Update the presence with the new affiliation and inform all occupants
try { try {
int newRole; MUCRole.Role newRole;
if (isMembersOnly() && wasMember) { if (isMembersOnly() && wasMember) {
newRole = MUCRole.NONE_ROLE; newRole = MUCRole.Role.none;
} }
else { else {
newRole = isModerated() ? MUCRole.VISITOR : MUCRole.PARTICIPANT; newRole = isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant;
} }
updatedPresences = changeOccupantAffiliation(bareJID, MUCRole.NONE, newRole); updatedPresences = changeOccupantAffiliation(bareJID, MUCRole.Affiliation.none, newRole);
if (isMembersOnly() && wasMember) { if (isMembersOnly() && wasMember) {
// If the room is members-only, remove the user from the room including a status // If the room is members-only, remove the user from the room including a status
// code of 321 to indicate that the user was removed because of an affiliation // code of 321 to indicate that the user was removed because of an affiliation
...@@ -1266,8 +1253,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1266,8 +1253,8 @@ public class MUCRoomImpl implements MUCRoom {
} }
public void changeSubject(Message packet, MUCRole role) throws ForbiddenException { public void changeSubject(Message packet, MUCRole role) throws ForbiddenException {
if ((canOccupantsChangeSubject() && role.getRole() < MUCRole.VISITOR) || if ((canOccupantsChangeSubject() && role.getRole().compareTo(MUCRole.Role.visitor) < 0) ||
MUCRole.MODERATOR == role.getRole()) { MUCRole.Role.moderator == role.getRole()) {
// Do nothing if the new subject is the same as the existing one // Do nothing if the new subject is the same as the existing one
if (packet.getSubject().equals(subject)) { if (packet.getSubject().equals(subject)) {
return; return;
...@@ -1295,8 +1282,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1295,8 +1282,8 @@ public class MUCRoomImpl implements MUCRoom {
public void sendInvitation(JID to, String reason, MUCRole senderRole, List<Element> extensions) public void sendInvitation(JID to, String reason, MUCRole senderRole, List<Element> extensions)
throws ForbiddenException { throws ForbiddenException {
if (!isMembersOnly() || canOccupantsInvite() if (!isMembersOnly() || canOccupantsInvite()
|| MUCRole.ADMINISTRATOR == senderRole.getAffiliation() || MUCRole.Affiliation.admin == senderRole.getAffiliation()
|| MUCRole.OWNER == senderRole.getAffiliation()) { || MUCRole.Affiliation.owner == senderRole.getAffiliation()) {
// If the room is not members-only OR if the room is members-only and anyone can send // If the room is not members-only OR if the room is members-only and anyone can send
// invitations or the sender is an admin or an owner, then send the invitation // invitations or the sender is an admin or an owner, then send the invitation
Message message = new Message(); Message message = new Message();
...@@ -1383,7 +1370,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1383,7 +1370,7 @@ public class MUCRoomImpl implements MUCRoom {
public Collection<MUCRole> getModerators() { public Collection<MUCRole> getModerators() {
List<MUCRole> moderators = new ArrayList<MUCRole>(); List<MUCRole> moderators = new ArrayList<MUCRole>();
for (MUCRole role : occupants.values()) { for (MUCRole role : occupants.values()) {
if (MUCRole.MODERATOR == role.getRole()) { if (MUCRole.Role.moderator == role.getRole()) {
moderators.add(role); moderators.add(role);
} }
} }
...@@ -1393,7 +1380,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1393,7 +1380,7 @@ public class MUCRoomImpl implements MUCRoom {
public Collection<MUCRole> getParticipants() { public Collection<MUCRole> getParticipants() {
List<MUCRole> participants = new ArrayList<MUCRole>(); List<MUCRole> participants = new ArrayList<MUCRole>();
for (MUCRole role : occupants.values()) { for (MUCRole role : occupants.values()) {
if (MUCRole.PARTICIPANT == role.getRole()) { if (MUCRole.Role.participant == role.getRole()) {
participants.add(role); participants.add(role);
} }
} }
...@@ -1401,13 +1388,13 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1401,13 +1388,13 @@ public class MUCRoomImpl implements MUCRoom {
} }
public Presence addModerator(JID jid, MUCRole senderRole) throws ForbiddenException { public Presence addModerator(JID jid, MUCRole senderRole) throws ForbiddenException {
if (MUCRole.ADMINISTRATOR != senderRole.getAffiliation() if (MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.OWNER != senderRole.getAffiliation()) { && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
// Update the presence with the new role and inform all occupants // Update the presence with the new role and inform all occupants
try { try {
return changeOccupantRole(jid, MUCRole.MODERATOR); return changeOccupantRole(jid, MUCRole.Role.moderator);
} }
catch (NotAllowedException e) { catch (NotAllowedException e) {
// We should never receive this exception....in theory // We should never receive this exception....in theory
...@@ -1417,11 +1404,11 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1417,11 +1404,11 @@ public class MUCRoomImpl implements MUCRoom {
public Presence addParticipant(JID jid, String reason, MUCRole senderRole) public Presence addParticipant(JID jid, String reason, MUCRole senderRole)
throws NotAllowedException, ForbiddenException { throws NotAllowedException, ForbiddenException {
if (MUCRole.MODERATOR != senderRole.getRole()) { if (MUCRole.Role.moderator != senderRole.getRole()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
// Update the presence with the new role and inform all occupants // Update the presence with the new role and inform all occupants
Presence updatedPresence = changeOccupantRole(jid, MUCRole.PARTICIPANT); Presence updatedPresence = changeOccupantRole(jid, MUCRole.Role.participant);
if (updatedPresence != null) { if (updatedPresence != null) {
Element frag = updatedPresence.getChildElement( Element frag = updatedPresence.getChildElement(
"x", "http://jabber.org/protocol/muc#user"); "x", "http://jabber.org/protocol/muc#user");
...@@ -1435,16 +1422,16 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1435,16 +1422,16 @@ public class MUCRoomImpl implements MUCRoom {
public Presence addVisitor(JID jid, MUCRole senderRole) throws NotAllowedException, public Presence addVisitor(JID jid, MUCRole senderRole) throws NotAllowedException,
ForbiddenException { ForbiddenException {
if (MUCRole.MODERATOR != senderRole.getRole()) { if (MUCRole.Role.moderator != senderRole.getRole()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
return changeOccupantRole(jid, MUCRole.VISITOR); return changeOccupantRole(jid, MUCRole.Role.visitor);
} }
public Presence kickOccupant(JID jid, JID actorJID, String reason) public Presence kickOccupant(JID jid, JID actorJID, String reason)
throws NotAllowedException { throws NotAllowedException {
// Update the presence with the new role and inform all occupants // Update the presence with the new role and inform all occupants
Presence updatedPresence = changeOccupantRole(jid, MUCRole.NONE_ROLE); Presence updatedPresence = changeOccupantRole(jid, MUCRole.Role.none);
if (updatedPresence != null) { if (updatedPresence != null) {
Element frag = updatedPresence.getChildElement( Element frag = updatedPresence.getChildElement(
"x", "http://jabber.org/protocol/muc#user"); "x", "http://jabber.org/protocol/muc#user");
...@@ -1540,7 +1527,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1540,7 +1527,7 @@ public class MUCRoomImpl implements MUCRoom {
// If the room was not members-only and now it is, kick occupants that aren't member // If the room was not members-only and now it is, kick occupants that aren't member
// of the room // of the room
for (MUCRole occupant : occupants.values()) { for (MUCRole occupant : occupants.values()) {
if (occupant.getAffiliation() > MUCRole.MEMBER) { if (occupant.getAffiliation().compareTo(MUCRole.Affiliation.member) > 0) {
try { try {
presences.add(kickOccupant(occupant.getRoleAddress(), null, presences.add(kickOccupant(occupant.getRoleAddress(), null,
LocaleUtils.getLocalizedString("muc.roomIsNowMembersOnly"))); LocaleUtils.getLocalizedString("muc.roomIsNowMembersOnly")));
...@@ -1643,7 +1630,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1643,7 +1630,7 @@ public class MUCRoomImpl implements MUCRoom {
} }
public void lock(MUCRole senderRole) throws ForbiddenException { public void lock(MUCRole senderRole) throws ForbiddenException {
if (MUCRole.OWNER != senderRole.getAffiliation()) { if (MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
if (isLocked()) { if (isLocked()) {
...@@ -1662,7 +1649,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1662,7 +1649,7 @@ public class MUCRoomImpl implements MUCRoom {
} }
public void unlock(MUCRole senderRole) throws ForbiddenException { public void unlock(MUCRole senderRole) throws ForbiddenException {
if (MUCRole.OWNER != senderRole.getAffiliation()) { if (MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
if (!isLocked()) { if (!isLocked()) {
...@@ -1748,8 +1735,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1748,8 +1735,8 @@ public class MUCRoomImpl implements MUCRoom {
this, this,
owner, owner,
null, null,
MUCRole.OWNER, MUCRole.Affiliation.owner,
MUCRole.NONE); MUCRole.Affiliation.none);
} }
// Save the existing room admins to the DB // Save the existing room admins to the DB
for (String admin : admins) { for (String admin : admins) {
...@@ -1757,14 +1744,14 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1757,14 +1744,14 @@ public class MUCRoomImpl implements MUCRoom {
this, this,
admin, admin,
null, null,
MUCRole.ADMINISTRATOR, MUCRole.Affiliation.admin,
MUCRole.NONE); MUCRole.Affiliation.none);
} }
// Save the existing room members to the DB // Save the existing room members to the DB
for (Iterator it=members.keySet().iterator(); it.hasNext();) { for (Iterator it=members.keySet().iterator(); it.hasNext();) {
String bareJID = (String)it.next(); String bareJID = (String)it.next();
MUCPersistenceManager.saveAffiliationToDB(this, bareJID, (String) members MUCPersistenceManager.saveAffiliationToDB(this, bareJID, (String) members
.get(bareJID), MUCRole.MEMBER, MUCRole.NONE); .get(bareJID), MUCRole.Affiliation.member, MUCRole.Affiliation.none);
} }
// Save the existing room outcasts to the DB // Save the existing room outcasts to the DB
for (String outcast : outcasts) { for (String outcast : outcasts) {
...@@ -1772,8 +1759,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1772,8 +1759,8 @@ public class MUCRoomImpl implements MUCRoom {
this, this,
outcast, outcast,
null, null,
MUCRole.OUTCAST, MUCRole.Affiliation.outcast,
MUCRole.NONE); MUCRole.Affiliation.none);
} }
} }
} }
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.messenger.spi;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.handler.IQHandler;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
import org.dom4j.Element;
/**
* Generic presence routing base class.
*
* @author Iain Shigeoka
*/
public class IQRouterImpl extends BasicModule implements IQRouter {
private RoutingTable routingTable;
private List<IQHandler> iqHandlers = new ArrayList<IQHandler>();
private Map<String, IQHandler> namespace2Handlers = new ConcurrentHashMap<String, IQHandler>();
private SessionManager sessionManager;
/**
* Creates a packet router.
*/
public IQRouterImpl() {
super("XMPP IQ Router");
}
public void route(IQ packet) {
if (packet == null) {
throw new NullPointerException();
}
Session session = sessionManager.getSession(packet.getFrom());
if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED
|| (isLocalServer(packet.getTo())
&& ("jabber:iq:auth".equals(packet.getChildElement().getNamespaceURI())
|| "jabber:iq:register".equals(packet.getChildElement().getNamespaceURI())))
) {
handle(packet);
}
else {
packet.setTo(sessionManager.getSession(packet.getFrom()).getAddress());
packet.setError(PacketError.Condition.not_authorized);
try {
sessionManager.getSession(packet.getFrom()).process(packet);
}
catch (UnauthorizedException ue) {
Log.error(ue);
}
}
}
private boolean isLocalServer(JID recipientJID) {
return recipientJID == null || recipientJID.getDomain() == null
|| "".equals(recipientJID.getDomain()) || recipientJID.getResource() == null
|| "".equals(recipientJID.getResource());
}
private void handle(IQ packet) {
JID recipientJID = packet.getTo();
try {
if (isLocalServer(recipientJID)) {
Element childElement = packet.getChildElement();
String namespace = null;
if (childElement != null) {
namespace = childElement.getNamespaceURI();
}
if (namespace == null) {
// Do nothing. We can't handle queries outside of a valid namespace
Log.warn("Unknown packet " + packet);
}
else {
IQHandler handler = getHandler(namespace);
if (handler == null) {
IQ reply = IQ.createResultIQ(packet);
if (recipientJID == null) {
// Answer an error since the server can't handle the requested namespace
reply.setError(PacketError.Condition.service_unavailable);
}
else if (recipientJID.getNode() == null || "".equals(recipientJID.getNode())) {
// Answer an error if JID is of the form <domain>
reply.setError(PacketError.Condition.feature_not_implemented);
}
else {
// JID is of the form <node@domain>
try {
// Let a "service" handle this packet otherwise return an error
// Useful for MUC where node refers to a room and domain is the
// MUC service.
ChannelHandler route = routingTable.getRoute(recipientJID);
if (route instanceof BasicModule) {
route.process(packet);
return;
}
}
catch (NoSuchRouteException e) {
// do nothing
}
// Answer an error since the server can't handle packets sent to a node
reply.setError(PacketError.Condition.service_unavailable);
}
Session session = sessionManager.getSession(packet.getFrom());
if (session != null) {
session.getConnection().deliver(reply);
}
else {
Log.warn("Packet could not be delivered " + packet);
}
}
else {
handler.process(packet);
}
}
}
else {
// JID is of the form <node@domain/resource>
ChannelHandler route = routingTable.getRoute(recipientJID);
route.process(packet);
}
}
catch (NoSuchRouteException e) {
Log.info("Packet sent to unreachable address " + packet);
Session session = sessionManager.getSession(packet.getFrom());
if (session != null) {
try {
packet.setError(PacketError.Condition.service_unavailable);
session.getConnection().deliver(packet);
}
catch (UnauthorizedException ex) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
}
}
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
try {
Session session = sessionManager.getSession(packet.getFrom());
if (session != null) {
Connection conn = session.getConnection();
if (conn != null) {
conn.close();
}
}
}
catch (UnauthorizedException e1) {
// do nothing
}
}
}
private IQHandler getHandler(String namespace) {
IQHandler handler = namespace2Handlers.get(namespace);
if (handler == null) {
for (IQHandler handlerCandidate : iqHandlers) {
IQHandlerInfo handlerInfo = handlerCandidate.getInfo();
if (handlerInfo != null && namespace.equalsIgnoreCase(handlerInfo.getNamespace())) {
handler = handlerCandidate;
namespace2Handlers.put(namespace, handler);
break;
}
}
}
return handler;
}
/**
* <p>Adds a new IQHandler to the list of registered handler. The new IQHandler will be
* responsible for handling IQ packet whose namespace matches the namespace of the
* IQHandler.</p>
*
* An IllegalArgumentException may be thrown if the IQHandler to register was already provided
* by the server. The server provides a certain list of IQHandlers when the server is
* started up.
*
* @param handler the IQHandler to add to the list of registered handler.
*/
public void addHandler(IQHandler handler) {
if (iqHandlers.contains(handler)) {
throw new IllegalArgumentException("IQHandler already provided by the server");
}
// Register the handler as the handler of the namespace
namespace2Handlers.put(handler.getInfo().getNamespace(), handler);
}
/**
* <p>Removes an IQHandler from the list of registered handler. The IQHandler to remove was
* responsible for handling IQ packet whose namespace matches the namespace of the
* IQHandler.</p>
*
* An IllegalArgumentException may be thrown if the IQHandler to remove was already provided
* by the server. The server provides a certain list of IQHandlers when the server is
* started up.
*
* @param handler the IQHandler to remove from the list of registered handler.
*/
public void removeHandler(IQHandler handler) {
if (iqHandlers.contains(handler)) {
throw new IllegalArgumentException("Cannot remove an IQHandler provided by the server");
}
// Unregister the handler as the handler of the namespace
namespace2Handlers.remove(handler.getInfo().getNamespace());
}
public void initialize(XMPPServer server) {
super.initialize(server);
routingTable = server.getRoutingTable();
iqHandlers.addAll(server.getIQHandlers());
sessionManager = server.getSessionManager();
}
}
...@@ -24,6 +24,7 @@ import org.xmpp.packet.IQ; ...@@ -24,6 +24,7 @@ import org.xmpp.packet.IQ;
* @author Iain Shigeoka * @author Iain Shigeoka
*/ */
public class PacketRouterImpl extends BasicModule implements PacketRouter { public class PacketRouterImpl extends BasicModule implements PacketRouter {
private IQRouter iqRouter; private IQRouter iqRouter;
private PresenceRouter presenceRouter; private PresenceRouter presenceRouter;
private MessageRouter messageRouter; private MessageRouter messageRouter;
......
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