Commit e028926a authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Added ability to modify and delete existing room registration.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@120 b35dd754-fafc-0310-a699-88a17e54d16e
parent 496dfb3f
...@@ -120,56 +120,82 @@ public class IQMUCRegisterHandler extends IQHandler { ...@@ -120,56 +120,82 @@ public class IQMUCRegisterHandler extends IQHandler {
public IQ handleIQ(IQ packet) throws UnauthorizedException, XMLStreamException { public IQ handleIQ(IQ packet) throws UnauthorizedException, XMLStreamException {
Session session = packet.getOriginatingSession(); Session session = packet.getOriginatingSession();
IQ reply = null; IQ reply = null;
// Get the target room
MUCRoom room = mucServer.getChatRoom(packet.getRecipient().getNamePrep());
if (room == null) {
// The room doesn't exist so answer a NOT_FOUND error
reply = packet.createResult();
reply.setError(XMPPError.Code.NOT_FOUND);
return reply;
}
if (IQ.GET.equals(packet.getType())) { if (IQ.GET.equals(packet.getType())) {
reply = packet.createResult(); reply = packet.createResult();
reply.setChildFragment(probeResult); String nickname = room.getReservedNickname(packet.getSender().toBareStringPrep());
if (nickname != null) {
// The user is already registered with the room so answer a completed form
MetaDataFragment currentRegistration = (MetaDataFragment) probeResult
.createDeepCopy();
currentRegistration.setProperty("query.registered", null);
XDataFormImpl form = (XDataFormImpl) currentRegistration.getFragment("x",
"jabber:x:data");
form.getField("muc#user_roomnick").addValue(nickname);
reply.setChildFragment(currentRegistration);
}
else {
// The user is not registered with the room so answer an empty form
reply.setChildFragment(probeResult);
}
} }
else if (IQ.SET.equals(packet.getType())) { else if (IQ.SET.equals(packet.getType())) {
try { try {
reply = packet.createResult(); // Keep a registry of the updated presences
List presences = new ArrayList();
reply = packet.createResult();
XMPPFragment iq = packet.getChildFragment(); XMPPFragment iq = packet.getChildFragment();
Element formElement = ((XMPPDOMFragment)iq).getRootElement().element("x"); MetaDataFragment metaData = MetaDataFragment.convertToMetaData(iq);
// Check if a form was used to provide the registration info
if (formElement != null) { if (metaData.includesProperty("query.remove")) {
// Get the sent form // The user is deleting his registration
XDataFormImpl registrationForm = new XDataFormImpl(); presences.addAll(room.addNone(packet.getSender().toBareStringPrep(),
registrationForm.parse(formElement); room.getRole()));
// Get the desired nickname sent in the form }
Iterator values = registrationForm.getField("muc#user_roomnick").getValues(); else {
String nickname = (values.hasNext() ? (String)values.next() : null); // The user is trying to register with a room
Element formElement = ((XMPPDOMFragment)iq).getRootElement().element("x");
// TODO The rest of the fields of the form are ignored. If we have a requirement // Check if a form was used to provide the registration info
// in the future where we need those fields we'll have to change if (formElement != null) {
// MUCRoom.addMember in order to receive a RegistrationInfo (new class) // Get the sent form
XDataFormImpl registrationForm = new XDataFormImpl();
// Get the target room registrationForm.parse(formElement);
MUCRoom room = mucServer.getChatRoom(packet.getRecipient().getNamePrep()); // Get the desired nickname sent in the form
if (room != null) { Iterator values = registrationForm.getField("muc#user_roomnick").getValues();
// Keep a registry of the updated presences String nickname = (values.hasNext() ? (String)values.next() : null);
List presences = new ArrayList();
// TODO The rest of the fields of the form are ignored. If we have a
// requirement in the future where we need those fields we'll have to change
// MUCRoom.addMember in order to receive a RegistrationInfo (new class)
// Add the new member to the members list // Add the new member to the members list
presences.addAll(room.addMember( presences.addAll(room.addMember(packet.getSender().toBareStringPrep(),
packet.getSender().toBareStringPrep(),
nickname, nickname,
room.getRole())); room.getRole()));
// Send the updated presences to the room occupants
try {
for (Iterator it = presences.iterator(); it.hasNext();) {
room.send((Presence)it.next());
}
}
catch (UnauthorizedException e) {
// Do nothing
}
} }
else { else {
reply.setError(XMPPError.Code.NOT_FOUND); reply.setError(XMPPError.Code.BAD_REQUEST);
} }
} }
else { // Send the updated presences to the room occupants
reply.setError(XMPPError.Code.BAD_REQUEST); try {
for (Iterator it = presences.iterator(); it.hasNext();) {
room.send((Presence)it.next());
}
}
catch (UnauthorizedException e) {
// Do nothing
} }
} }
catch (ForbiddenException e) { catch (ForbiddenException e) {
reply = packet.createResult(); reply = packet.createResult();
......
...@@ -77,6 +77,8 @@ public class MUCPersistenceManager { ...@@ -77,6 +77,8 @@ public class MUCPersistenceManager {
"DELETE FROM mucMember WHERE roomID=?"; "DELETE FROM mucMember WHERE roomID=?";
private static final String ADD_MEMBER = private static final String ADD_MEMBER =
"INSERT INTO mucMember (roomID,jid,nickname) VALUES (?,?,?)"; "INSERT INTO mucMember (roomID,jid,nickname) VALUES (?,?,?)";
private static final String UPDATE_MEMBER =
"UPDATE mucMember SET nickname=? WHERE roomID=? AND jid=?";
private static final String DELETE_MEMBER = private static final String DELETE_MEMBER =
"DELETE FROM mucMember WHERE roomID=? AND jid=?"; "DELETE FROM mucMember WHERE roomID=? AND jid=?";
private static final String ADD_AFFILIATION = private static final String ADD_AFFILIATION =
...@@ -528,7 +530,29 @@ public class MUCPersistenceManager { ...@@ -528,7 +530,29 @@ public class MUCPersistenceManager {
} }
} }
else { else {
if (MUCRole.MEMBER == newAffiliation) { if (MUCRole.MEMBER == newAffiliation && MUCRole.MEMBER == oldAffiliation) {
// Update the member's data in the member table.
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_MEMBER);
pstmt.setString(1, nickname);
pstmt.setLong(2, room.getID());
pstmt.setString(3, bareJID);
pstmt.execute();
}
catch (SQLException sqle) {
Log.error(sqle);
}
finally {
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) { Log.error(e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
}
}
else if (MUCRole.MEMBER == newAffiliation) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
boolean abortTransaction = false; boolean abortTransaction = false;
......
...@@ -1115,7 +1115,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1115,7 +1115,7 @@ public class MUCRoomImpl implements MUCRoom {
public List addMember(String bareJID, String nickname, MUCRole sendRole) public List addMember(String bareJID, String nickname, MUCRole sendRole)
throws ForbiddenException, ConflictException { throws ForbiddenException, ConflictException {
int oldAffiliation = MUCRole.NONE; int oldAffiliation = (members.containsKey(bareJID) ? MUCRole.MEMBER : MUCRole.NONE);
if (isInvitationRequiredToEnter()) { if (isInvitationRequiredToEnter()) {
if (!canOccupantsInvite()) { if (!canOccupantsInvite()) {
if (MUCRole.ADMINISTRATOR != sendRole.getAffiliation() if (MUCRole.ADMINISTRATOR != sendRole.getAffiliation()
...@@ -1253,7 +1253,6 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1253,7 +1253,6 @@ public class MUCRoomImpl implements MUCRoom {
if (owners.contains(bareJID) && owners.size() == 1) { if (owners.contains(bareJID) && owners.size() == 1) {
throw new ConflictException(); throw new ConflictException();
} }
String actorJID = senderRole.getChatUser().getAddress().toBareStringPrep();
List updatedPresences = null; List updatedPresences = null;
boolean wasMember = members.containsKey(bareJID); boolean wasMember = members.containsKey(bareJID);
// Remove the user from ALL the affiliation lists // Remove the user from ALL the affiliation lists
...@@ -1309,6 +1308,9 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1309,6 +1308,9 @@ public class MUCRoomImpl implements MUCRoom {
// different client resources, he/she will be kicked from all the client // different client resources, he/she will be kicked from all the client
// resources. // resources.
// Effectively kick the occupant from the room // Effectively kick the occupant from the room
MUCUser senderUser = senderRole.getChatUser();
String actorJID = (senderUser == null ?
null : senderUser.getAddress().toBareStringPrep());
kickPresence(presence, actorJID); kickPresence(presence, actorJID);
} }
} }
...@@ -1570,6 +1572,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1570,6 +1572,8 @@ public class MUCRoomImpl implements MUCRoom {
* from the occupants lists. * from the occupants lists.
* *
* @param kickPresence the presence of the occupant to kick from the room. * @param kickPresence the presence of the occupant to kick from the room.
* @param actorJID The JID of the actor that initiated the kick or <tt>null</tt> if the info
* was not provided.
*/ */
private void kickPresence(Presence kickPresence, String actorJID) { private void kickPresence(Presence kickPresence, String actorJID) {
MUCRole kickedRole; MUCRole kickedRole;
......
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