Commit 7ec010be authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@593 b35dd754-fafc-0310-a699-88a17e54d16e
parent 7d1390b1
...@@ -22,6 +22,8 @@ import java.util.TimeZone; ...@@ -22,6 +22,8 @@ import java.util.TimeZone;
import org.jivesoftware.messenger.muc.spi.MUCRoleImpl; import org.jivesoftware.messenger.muc.spi.MUCRoleImpl;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.dom4j.Element;
import org.xmpp.packet.Message;
/** /**
* Represents the amount of history requested by an occupant while joining a room. There are * Represents the amount of history requested by an occupant while joining a room. There are
...@@ -48,21 +50,22 @@ public class HistoryRequest { ...@@ -48,21 +50,22 @@ public class HistoryRequest {
private int seconds = -1; private int seconds = -1;
private Date since; private Date since;
public HistoryRequest(MetaDataFragment userFragment) { public HistoryRequest(Element userFragment) {
if (userFragment.includesProperty("x.history")) { Element history = userFragment.element("history");
if (userFragment.includesProperty("x.history:maxchars")) { if (history != null) {
this.maxChars = Integer.parseInt(userFragment.getProperty("x.history:maxchars")); if (history.attribute("maxchars") != null) {
this.maxChars = Integer.parseInt(history.attributeValue("maxchars"));
} }
if (userFragment.includesProperty("x.history:maxstanzas")) { if (history.attribute("maxstanzas") != null) {
this.maxStanzas = Integer.parseInt(userFragment.getProperty("x.history:maxstanzas")); this.maxStanzas = Integer.parseInt(history.attributeValue("maxstanzas"));
} }
if (userFragment.includesProperty("x.history:seconds")) { if (history.attribute("seconds") != null) {
this.seconds = Integer.parseInt(userFragment.getProperty("x.history:seconds")); this.seconds = Integer.parseInt(history.attributeValue("seconds"));
} }
if (userFragment.includesProperty("x.history:since")) { if (history.attribute("since") != null) {
try { try {
// parse utc into Date // parse utc into Date
this.since = formatter.parse(userFragment.getProperty("x.history:since")); this.since = formatter.parse(history.attributeValue("since"));
} }
catch(ParseException pe) { catch(ParseException pe) {
Log.error("Error parsing date from history management", pe); Log.error("Error parsing date from history management", pe);
...@@ -142,7 +145,7 @@ public class HistoryRequest { ...@@ -142,7 +145,7 @@ public class HistoryRequest {
Message message; Message message;
int accumulatedChars = 0; int accumulatedChars = 0;
int accumulatedStanzas = 0; int accumulatedStanzas = 0;
MetaDataFragment delayInformation; Element delayInformation;
LinkedList historyToSend = new LinkedList(); LinkedList historyToSend = new LinkedList();
ListIterator iterator = roomHistory.getReverseMessageHistory(); ListIterator iterator = roomHistory.getReverseMessageHistory();
while (iterator.hasPrevious()) { while (iterator.hasPrevious()) {
...@@ -161,13 +164,10 @@ public class HistoryRequest { ...@@ -161,13 +164,10 @@ public class HistoryRequest {
} }
if (getSeconds() > -1 || getSince() != null) { if (getSeconds() > -1 || getSince() != null) {
delayInformation = (MetaDataFragment) message.getFragment( delayInformation = message.getChildElement("x", "jabber:x:delay");
"x",
"jabber:x:delay");
try { try {
// Get the date when the historic message was sent // Get the date when the historic message was sent
Date delayedDate = delayedFormatter.parse(delayInformation Date delayedDate = delayedFormatter.parse(delayInformation.attributeValue("stamp"));
.getProperty("x:stamp"));
if (getSince() != null && delayedDate.before(getSince())) { if (getSince() != null && delayedDate.before(getSince())) {
// Stop collecting history since we have exceded a limit // Stop collecting history since we have exceded a limit
break; break;
......
...@@ -92,7 +92,7 @@ public class MUCUserImpl implements MUCUser { ...@@ -92,7 +92,7 @@ public class MUCUserImpl implements MUCUser {
* *
* @param packet the packet to be bounced. * @param packet the packet to be bounced.
*/ */
private void sendErrorPacket(Packet packet, PacketError error) { private void sendErrorPacket(Packet packet, PacketError.Condition error) {
packet = packet.createCopy(); packet = packet.createCopy();
packet.setError(error); packet.setError(error);
packet.setFrom(packet.getTo()); packet.setFrom(packet.getTo());
...@@ -161,30 +161,30 @@ public class MUCUserImpl implements MUCUser { ...@@ -161,30 +161,30 @@ public class MUCUserImpl implements MUCUser {
if (declinedInvitation) { if (declinedInvitation) {
Element info = userInfo.element("decline"); Element info = userInfo.element("decline");
server.getChatRoom(group).sendInvitationRejection( server.getChatRoom(group).sendInvitationRejection(
info.attributeValue("to"), new JID(info.attributeValue("to")),
info.elementTextTrim("reason"), info.elementTextTrim("reason"),
packet.getFrom()); packet.getFrom());
} }
else { else {
// The sender is not an occupant of the room // The sender is not an occupant of the room
sendErrorPacket(packet, XMPPError.Code.NOT_ACCEPTABLE); sendErrorPacket(packet, PacketError.Condition.not_acceptable);
} }
} }
else { else {
// The sender is not an occupant of a NON-EXISTENT room!!! // The sender is not an occupant of a NON-EXISTENT room!!!
sendErrorPacket(packet, XMPPError.Code.NOT_FOUND); sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
} }
} }
else { else {
// Check and reject conflicting packets with conflicting roles // Check and reject conflicting packets with conflicting roles
// In other words, another user already has this nickname // In other words, another user already has this nickname
if (!role.getChatUser().getAddress().equals(packet.getSender())) { if (!role.getChatUser().getAddress().equals(packet.getFrom())) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
else { else {
try { try {
if (packet.getSubject() != null && packet.getSubject().trim().length() > 0 if (packet.getSubject() != null && packet.getSubject().trim().length() > 0
&& Message.GROUP_CHAT == packet.getType()) { && Message.Type.groupchat == packet.getType()) {
// An occupant is trying to change the room's subject // An occupant is trying to change the room's subject
role.getChatRoom().changeSubject(packet, role); role.getChatRoom().changeSubject(packet, role);
...@@ -193,32 +193,31 @@ public class MUCUserImpl implements MUCUser { ...@@ -193,32 +193,31 @@ public class MUCUserImpl implements MUCUser {
// An occupant is trying to send a private, send public message, // An occupant is trying to send a private, send public message,
// invite someone to the room or reject an invitation // invite someone to the room or reject an invitation
Message.Type type = packet.getType(); Message.Type type = packet.getType();
String resource = packet.getRecipient().getResource(); String resource = packet.getTo().getResource();
if (resource == null || resource.trim().length() == 0) { if (resource == null || resource.trim().length() == 0) {
resource = null; resource = null;
} }
if (resource == null && Message.GROUP_CHAT == type) { if (resource == null && Message.Type.groupchat == type) {
// An occupant is trying to send a public message // An occupant is trying to send a public message
role.getChatRoom().sendPublicMessage(packet, role); role.getChatRoom().sendPublicMessage(packet, role);
} }
else if (resource != null else if (resource != null
&& (Message.CHAT == type || Message.NORMAL == type)) { && (Message.Type.chat == type || Message.Type.normal == type)) {
// An occupant is trying to send a private message // An occupant is trying to send a private message
role.getChatRoom().sendPrivateMessage(packet, role); role.getChatRoom().sendPrivateMessage(packet, role);
} }
else if (resource == null && Message.NORMAL == type) { else if (resource == null && Message.Type.normal == type) {
// An occupant could be sending an invitation or declining an // An occupant could be sending an invitation or declining an
// invitation // invitation
XMPPDOMFragment userInfo = (XMPPDOMFragment) packet.getFragment( Element userInfo = packet.getChildElement(
"x", "x",
"http://jabber.org/protocol/muc#user"); "http://jabber.org/protocol/muc#user");
// Real real real UGLY TRICK!!! Will and MUST be solved when // Real real real UGLY TRICK!!! Will and MUST be solved when
// persistence will be added. Replace locking with transactions! // persistence will be added. Replace locking with transactions!
MUCRoomImpl room = (MUCRoomImpl) role.getChatRoom(); MUCRoomImpl room = (MUCRoomImpl) role.getChatRoom();
if (userInfo != null if (userInfo != null && userInfo.element("invite") != null) {
&& userInfo.getRootElement().element("invite") != null) {
// An occupant is sending an invitation // An occupant is sending an invitation
Element info = userInfo.getRootElement().element("invite"); Element info = userInfo.element("invite");
// Add the user as a member of the room if the room is // Add the user as a member of the room if the room is
// members only // members only
...@@ -233,35 +232,33 @@ public class MUCUserImpl implements MUCUser { ...@@ -233,35 +232,33 @@ public class MUCUserImpl implements MUCUser {
} }
// Send the invitation to the user // Send the invitation to the user
room.sendInvitation(info.attributeValue("to"), info room.sendInvitation(new JID(info.attributeValue("to")),
.elementTextTrim("reason"), role, packet info.elementTextTrim("reason"), role);
.getOriginatingSession());
} }
else if (userInfo != null else if (userInfo != null
&& userInfo.getRootElement().element("decline") != null) { && userInfo.element("decline") != null) {
// An occupant has declined an invitation // An occupant has declined an invitation
Element info = userInfo.getRootElement().element("decline"); Element info = userInfo.element("decline");
room.sendInvitationRejection(info.attributeValue("to"), info room.sendInvitationRejection(new JID(info.attributeValue("to")),
.elementTextTrim("reason"), packet.getSender(), packet info.elementTextTrim("reason"), packet.getFrom());
.getOriginatingSession());
} }
else { else {
sendErrorPacket(packet, XMPPError.Code.BAD_REQUEST); sendErrorPacket(packet, PacketError.Condition.bad_request);
} }
} }
else { else {
sendErrorPacket(packet, XMPPError.Code.BAD_REQUEST); sendErrorPacket(packet, PacketError.Condition.bad_request);
} }
} }
} }
catch (ForbiddenException e) { catch (ForbiddenException e) {
sendErrorPacket(packet, XMPPError.Code.FORBIDDEN); sendErrorPacket(packet, PacketError.Condition.forbidden);
} }
catch (NotFoundException e) { catch (NotFoundException e) {
sendErrorPacket(packet, XMPPError.Code.NOT_FOUND); sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
} }
catch (ConflictException e) { catch (ConflictException e) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
} }
} }
...@@ -270,8 +267,8 @@ public class MUCUserImpl implements MUCUser { ...@@ -270,8 +267,8 @@ public class MUCUserImpl implements MUCUser {
public void process(IQ packet) { public void process(IQ packet) {
lastPacketTime = System.currentTimeMillis(); lastPacketTime = System.currentTimeMillis();
XMPPAddress recipient = packet.getRecipient(); JID recipient = packet.getTo();
String group = recipient.getName(); String group = recipient.getNode();
if (group == null) { if (group == null) {
// Ignore packets to the groupchat server // Ignore packets to the groupchat server
// In the future, we'll need to support TYPE_IQ queries to the server for MUC // In the future, we'll need to support TYPE_IQ queries to the server for MUC
...@@ -287,33 +284,33 @@ public class MUCUserImpl implements MUCUser { ...@@ -287,33 +284,33 @@ public class MUCUserImpl implements MUCUser {
else { else {
// Check and reject conflicting packets with conflicting roles // Check and reject conflicting packets with conflicting roles
// In other words, another user already has this nickname // In other words, another user already has this nickname
if (!role.getChatUser().getAddress().equals(packet.getSender())) { if (!role.getChatUser().getAddress().equals(packet.getFrom())) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
else { else {
try { try {
if ("query".equals(packet.getChildName()) if ("query".equals(packet.getElement().getNamespacePrefix())
&& "http://jabber.org/protocol/muc#owner".equals(packet && "http://jabber.org/protocol/muc#owner".equals(packet
.getChildNamespace())) { .getElement().getNamespaceURI())) {
role.getChatRoom().getIQOwnerHandler().handleIQ(packet, role); role.getChatRoom().getIQOwnerHandler().handleIQ(packet, role);
} }
else if ("query".equals(packet.getChildName()) else if ("query".equals(packet.getElement().getNamespacePrefix())
&& "http://jabber.org/protocol/muc#admin".equals(packet && "http://jabber.org/protocol/muc#admin".equals(packet
.getChildNamespace())) { .getElement().getNamespaceURI())) {
role.getChatRoom().getIQAdminHandler().handleIQ(packet, role); role.getChatRoom().getIQAdminHandler().handleIQ(packet, role);
} }
else { else {
sendErrorPacket(packet, XMPPError.Code.BAD_REQUEST); sendErrorPacket(packet, PacketError.Condition.bad_request);
} }
} }
catch (ForbiddenException e) { catch (ForbiddenException e) {
sendErrorPacket(packet, XMPPError.Code.FORBIDDEN); sendErrorPacket(packet, PacketError.Condition.forbidden);
} }
catch (ConflictException e) { catch (ConflictException e) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
catch (NotAllowedException e) { catch (NotAllowedException e) {
sendErrorPacket(packet, XMPPError.Code.NOT_ALLOWED); sendErrorPacket(packet, PacketError.Condition.not_allowed);
} }
} }
} }
...@@ -322,11 +319,11 @@ public class MUCUserImpl implements MUCUser { ...@@ -322,11 +319,11 @@ public class MUCUserImpl implements MUCUser {
public void process(Presence packet) { public void process(Presence packet) {
lastPacketTime = System.currentTimeMillis(); lastPacketTime = System.currentTimeMillis();
XMPPAddress recipient = packet.getRecipient(); JID recipient = packet.getTo();
String group = recipient.getNamePrep(); String group = recipient.getNode();
if (group == null) { if (group == null) {
if (Presence.UNAVAILABLE == packet.getType()) { if (Presence.Type.unavailable == packet.getType()) {
server.removeUser(packet.getSender()); server.removeUser(packet.getFrom());
} }
} }
else { else {
...@@ -336,20 +333,19 @@ public class MUCUserImpl implements MUCUser { ...@@ -336,20 +333,19 @@ public class MUCUserImpl implements MUCUser {
// properly addressed and we drop it silently // properly addressed and we drop it silently
if (recipient.getResource() != null if (recipient.getResource() != null
&& recipient.getResource().trim().length() > 0) { && recipient.getResource().trim().length() > 0) {
if (packet.getType() == Presence.AVAILABLE if (packet.isAvailable()) {
|| Presence.INVISIBLE == packet.getType()) {
try { try {
// Get or create the room // Get or create the room
MUCRoom room = server.getChatRoom(group, packet.getSender()); MUCRoom room = server.getChatRoom(group, packet.getFrom());
// User must support MUC in order to create a room // User must support MUC in order to create a room
MetaDataFragment mucInfo = (MetaDataFragment) packet.getFragment("x", Element mucInfo = packet.getChildElement("x",
"http://jabber.org/protocol/muc"); "http://jabber.org/protocol/muc");
HistoryRequest historyRequest = null; HistoryRequest historyRequest = null;
String password = null; String password = null;
// Check for password & requested history if client supports MUC // Check for password & requested history if client supports MUC
if (mucInfo != null) { if (mucInfo != null) {
password = mucInfo.getProperty("x.password"); password = mucInfo.elementTextTrim("password");
if (mucInfo.includesProperty("x.history")) { if (mucInfo.element("history") != null) {
historyRequest = new HistoryRequest(mucInfo); historyRequest = new HistoryRequest(mucInfo);
} }
} }
...@@ -366,25 +362,25 @@ public class MUCUserImpl implements MUCUser { ...@@ -366,25 +362,25 @@ public class MUCUserImpl implements MUCUser {
} }
} }
catch (UnauthorizedException e) { catch (UnauthorizedException e) {
sendErrorPacket(packet, XMPPError.Code.UNAUTHORIZED); sendErrorPacket(packet, PacketError.Condition.not_authorized);
} }
catch (NotAllowedException e) { catch (NotAllowedException e) {
sendErrorPacket(packet, XMPPError.Code.NOT_ALLOWED); sendErrorPacket(packet, PacketError.Condition.not_allowed);
} }
catch (UserAlreadyExistsException e) { catch (UserAlreadyExistsException e) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
catch (RoomLockedException e) { catch (RoomLockedException e) {
sendErrorPacket(packet, XMPPError.Code.NOT_FOUND); sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
} }
catch (ForbiddenException e) { catch (ForbiddenException e) {
sendErrorPacket(packet, XMPPError.Code.FORBIDDEN); sendErrorPacket(packet, PacketError.Condition.forbidden);
} }
catch (RegistrationRequiredException e) { catch (RegistrationRequiredException e) {
sendErrorPacket(packet, XMPPError.Code.REGISTRATION_REQUIRED); sendErrorPacket(packet, PacketError.Condition.registration_required);
} }
catch (ConflictException e) { catch (ConflictException e) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
} }
else { else {
...@@ -393,10 +389,9 @@ public class MUCUserImpl implements MUCUser { ...@@ -393,10 +389,9 @@ public class MUCUserImpl implements MUCUser {
} }
} }
else { else {
if (packet.getType() == Presence.AVAILABLE if (packet.isAvailable()) {
|| Presence.INVISIBLE == packet.getType()) {
// A resource is required in order to join a room // A resource is required in order to join a room
sendErrorPacket(packet, XMPPError.Code.BAD_REQUEST); sendErrorPacket(packet, PacketError.Condition.bad_request);
} }
// TODO: send error message to user (can't send packets to group you haven't // TODO: send error message to user (can't send packets to group you haven't
// joined) // joined)
...@@ -405,11 +400,11 @@ public class MUCUserImpl implements MUCUser { ...@@ -405,11 +400,11 @@ public class MUCUserImpl implements MUCUser {
else { else {
// Check and reject conflicting packets with conflicting roles // Check and reject conflicting packets with conflicting roles
// In other words, another user already has this nickname // In other words, another user already has this nickname
if (!role.getChatUser().getAddress().equals(packet.getSender())) { if (!role.getChatUser().getAddress().equals(packet.getFrom())) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
else { else {
if (Presence.UNAVAILABLE == packet.getType()) { if (Presence.Type.unavailable == packet.getType()) {
try { try {
roles.remove(group.toLowerCase()); roles.remove(group.toLowerCase());
role.getChatRoom().leaveRoom(role.getNickname()); role.getChatRoom().leaveRoom(role.getNickname());
...@@ -431,8 +426,8 @@ public class MUCUserImpl implements MUCUser { ...@@ -431,8 +426,8 @@ public class MUCUserImpl implements MUCUser {
// Occupant has changed his availability status // Occupant has changed his availability status
role.setPresence(packet); role.setPresence(packet);
Presence presence = (Presence) role.getPresence() Presence presence = (Presence) role.getPresence()
.createDeepCopy(); .createCopy();
presence.setSender(role.getRoleAddress()); presence.setFrom(role.getRoleAddress());
role.getChatRoom().send(presence); role.getChatRoom().send(presence);
} }
else { else {
...@@ -441,23 +436,19 @@ public class MUCUserImpl implements MUCUser { ...@@ -441,23 +436,19 @@ public class MUCUserImpl implements MUCUser {
// Answer a conflic error if the new nickname is taken // Answer a conflic error if the new nickname is taken
if (role.getChatRoom().hasOccupant(resource)) { if (role.getChatRoom().hasOccupant(resource)) {
sendErrorPacket(packet, XMPPError.Code.CONFLICT); sendErrorPacket(packet, PacketError.Condition.conflict);
} }
else { else {
// Send "unavailable" presence for the old nickname // Send "unavailable" presence for the old nickname
Presence presence = (Presence) role.getPresence() Presence presence = (Presence) role.getPresence().createCopy();
.createDeepCopy();
// Switch the presence to OFFLINE // Switch the presence to OFFLINE
presence.setVisible(false); presence.setType(Presence.Type.unavailable);
presence.setAvailable(false); presence.setFrom(role.getRoleAddress());
presence.setSender(role.getRoleAddress());
// Add the new nickname and status 303 as properties // Add the new nickname and status 303 as properties
MetaDataFragment frag = (MetaDataFragment) presence Element frag = presence.getChildElement("x",
.getFragment( "http://jabber.org/protocol/muc#user");
"x", frag.element("item").addAttribute("nick", resource);
"http://jabber.org/protocol/muc#user"); frag.element("status").addAttribute("code", "303");
frag.setProperty("x.item:nick", resource);
frag.setProperty("x.status:code", "303");
role.getChatRoom().send(presence); role.getChatRoom().send(presence);
// Send availability presence for the new nickname // Send availability presence for the new nickname
...@@ -465,8 +456,8 @@ public class MUCUserImpl implements MUCUser { ...@@ -465,8 +456,8 @@ public class MUCUserImpl implements MUCUser {
role.setPresence(packet); role.setPresence(packet);
role.changeNickname(resource); role.changeNickname(resource);
role.getChatRoom().nicknameChanged(oldNick, resource); role.getChatRoom().nicknameChanged(oldNick, resource);
presence = (Presence) role.getPresence().createDeepCopy(); presence = (Presence) role.getPresence().createCopy();
presence.setSender(role.getRoleAddress()); presence.setFrom(role.getRoleAddress());
role.getChatRoom().send(presence); role.getChatRoom().send(presence);
} }
} }
......
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