if(room.isInRoom(newOccupantJid)) {
// user is already in room, change nick
logger.debug("{} has requested to change nick in room {}", newOccupantJid, roomJid);
// occupant is already in room
Occupant occupant = room.findOccupantByJID(newOccupantJid);
if(nick.equals(occupant.getName())) {
// nick unchanged, change show and status
for(Occupant receiver : room.getOccupants()) {
sendChangeShowStatus(occupant, receiver, room, getInnerElementText(stanza, "show"),
getInnerElementText(stanza, "status"), serverRuntimeContext);
}
} else {
if(room.isInRoom(nick)) {
// user with this nick is already in room
return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "cancel", "conflict");
}
String oldNick = occupant.getName();
// update the nick
occupant.setName(nick);
// send out unavailable presences to all existing occupants
for(Occupant receiver : room.getOccupants()) {
sendChangeNickUnavailable(occupant, oldNick, receiver, room, serverRuntimeContext);
}
// send out available presences to all existing occupants
for(Occupant receiver : room.getOccupants()) {
sendChangeNickAvailable(occupant, receiver, room, serverRuntimeContext);
}
}
} else {
logger.debug("{} has requested to enter room {}", newOccupantJid, roomJid);
if(room.isInRoom(nick)) {
// user with this nick is already in room
return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "cancel", "conflict");
}
// check password if password protected
if(room.isRoomType(RoomType.PasswordProtected)) {
X x = X.fromStanza(stanza);
String password = null;
if(x != null) {
password = x.getPasswordValue();
}
if(password == null || !password.equals(room.getPassword())) {
// password missing or not matching
return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "auth", "not-authorized");
}
}
Occupant newOccupant = room.addOccupant(newOccupantJid, nick);
if(newOccupant == null) {
// outcast
return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "auth", "forbidden");
}
if(room.isRoomType(RoomType.MembersOnly) && newOccupant.getAffiliation() == Affiliation.None) {
// non-member can not enter members only room
return createPresenceErrorStanza(roomJid, newOccupantJid, stanza.getID(), "auth", "registration-required");
}
// relay presence of all existing room occupants to the now joined occupant