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 {
public IQ handleIQ(IQ packet) throws UnauthorizedException, XMLStreamException {
Session session = packet.getOriginatingSession();
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())) {
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())) {
try {
reply = packet.createResult();
// Keep a registry of the updated presences
List presences = new ArrayList();
reply = packet.createResult();
XMPPFragment iq = packet.getChildFragment();
Element formElement = ((XMPPDOMFragment)iq).getRootElement().element("x");
// Check if a form was used to provide the registration info
if (formElement != null) {
// Get the sent form
XDataFormImpl registrationForm = new XDataFormImpl();
registrationForm.parse(formElement);
// Get the desired nickname sent in the form
Iterator values = registrationForm.getField("muc#user_roomnick").getValues();
String nickname = (values.hasNext() ? (String)values.next() : null);
// 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)
// Get the target room
MUCRoom room = mucServer.getChatRoom(packet.getRecipient().getNamePrep());
if (room != null) {
// Keep a registry of the updated presences
List presences = new ArrayList();
MetaDataFragment metaData = MetaDataFragment.convertToMetaData(iq);
if (metaData.includesProperty("query.remove")) {
// The user is deleting his registration
presences.addAll(room.addNone(packet.getSender().toBareStringPrep(),
room.getRole()));
}
else {
// The user is trying to register with a room
Element formElement = ((XMPPDOMFragment)iq).getRootElement().element("x");
// Check if a form was used to provide the registration info
if (formElement != null) {
// Get the sent form
XDataFormImpl registrationForm = new XDataFormImpl();
registrationForm.parse(formElement);
// Get the desired nickname sent in the form
Iterator values = registrationForm.getField("muc#user_roomnick").getValues();
String nickname = (values.hasNext() ? (String)values.next() : null);
// 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
presences.addAll(room.addMember(
packet.getSender().toBareStringPrep(),
presences.addAll(room.addMember(packet.getSender().toBareStringPrep(),
nickname,
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 {
reply.setError(XMPPError.Code.NOT_FOUND);
reply.setError(XMPPError.Code.BAD_REQUEST);
}
}
else {
reply.setError(XMPPError.Code.BAD_REQUEST);
// 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
}
}
catch (ForbiddenException e) {
reply = packet.createResult();
......
......@@ -77,6 +77,8 @@ public class MUCPersistenceManager {
"DELETE FROM mucMember WHERE roomID=?";
private static final String ADD_MEMBER =
"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 =
"DELETE FROM mucMember WHERE roomID=? AND jid=?";
private static final String ADD_AFFILIATION =
......@@ -528,7 +530,29 @@ public class MUCPersistenceManager {
}
}
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;
PreparedStatement pstmt = null;
boolean abortTransaction = false;
......
......@@ -1115,7 +1115,7 @@ public class MUCRoomImpl implements MUCRoom {
public List addMember(String bareJID, String nickname, MUCRole sendRole)
throws ForbiddenException, ConflictException {
int oldAffiliation = MUCRole.NONE;
int oldAffiliation = (members.containsKey(bareJID) ? MUCRole.MEMBER : MUCRole.NONE);
if (isInvitationRequiredToEnter()) {
if (!canOccupantsInvite()) {
if (MUCRole.ADMINISTRATOR != sendRole.getAffiliation()
......@@ -1253,7 +1253,6 @@ public class MUCRoomImpl implements MUCRoom {
if (owners.contains(bareJID) && owners.size() == 1) {
throw new ConflictException();
}
String actorJID = senderRole.getChatUser().getAddress().toBareStringPrep();
List updatedPresences = null;
boolean wasMember = members.containsKey(bareJID);
// Remove the user from ALL the affiliation lists
......@@ -1309,6 +1308,9 @@ public class MUCRoomImpl implements MUCRoom {
// different client resources, he/she will be kicked from all the client
// resources.
// Effectively kick the occupant from the room
MUCUser senderUser = senderRole.getChatUser();
String actorJID = (senderUser == null ?
null : senderUser.getAddress().toBareStringPrep());
kickPresence(presence, actorJID);
}
}
......@@ -1570,6 +1572,8 @@ public class MUCRoomImpl implements MUCRoom {
* from the occupants lists.
*
* @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) {
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