use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method updateOccupant.
public Presence updateOccupant(UpdateOccupantRequest updateRequest) throws NotAllowedException {
Presence result = null;
List<MUCRole> occupants = occupantsByNickname.get(updateRequest.getNickname().toLowerCase());
if (occupants == null || occupants.size() == 0) {
Log.debug("Failed to update information of local room occupant; nickname: " + updateRequest.getNickname());
} else {
for (MUCRole occupant : occupants) {
if (updateRequest.isAffiliationChanged()) {
occupant.setAffiliation(updateRequest.getAffiliation());
}
occupant.setRole(updateRequest.getRole());
// Notify the the cluster nodes to update the occupant
CacheFactory.doClusterTask(new UpdateOccupant(this, occupant));
if (result == null) {
result = occupant.getPresence();
}
}
}
return result;
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method addMember.
@Override
public List<Presence> addMember(JID jid, String nickname, MUCRole sendRole) throws ForbiddenException, ConflictException {
final JID bareJID = jid.asBareJID();
lock.writeLock().lock();
try {
MUCRole.Affiliation oldAffiliation = (members.containsKey(bareJID) ? MUCRole.Affiliation.member : MUCRole.Affiliation.none);
if (isMembersOnly()) {
if (!canOccupantsInvite()) {
if (MUCRole.Affiliation.admin != sendRole.getAffiliation() && MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException();
}
}
} else {
if (MUCRole.Affiliation.admin != sendRole.getAffiliation() && MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException();
}
}
// Check if the desired nickname is already reserved for another member
if (nickname != null && nickname.trim().length() > 0 && members.containsValue(nickname.toLowerCase())) {
if (!nickname.equals(members.get(bareJID))) {
throw new ConflictException();
}
} else if (isLoginRestrictedToNickname() && (nickname == null || nickname.trim().length() == 0)) {
throw new ConflictException();
}
// Check that the room always has an owner
if (owners.contains(bareJID) && owners.size() == 1) {
throw new ConflictException();
}
// Check if user is already an member
if (members.containsKey(bareJID)) {
// Do nothing
return Collections.emptyList();
}
// Associate the reserved nickname with the bareJID. If nickname is null then associate an
// empty string
members.put(bareJID, (nickname == null ? "" : nickname.toLowerCase()));
// Remove the user from other affiliation lists
if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.Affiliation.owner;
} else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.Affiliation.admin;
} else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.Affiliation.outcast;
}
// Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB(this, bareJID, nickname, MUCRole.Affiliation.member, oldAffiliation);
} finally {
lock.writeLock().unlock();
}
// Update other cluster nodes with new member
CacheFactory.doClusterTask(new AddMember(this, jid.toBareJID(), (nickname == null ? "" : nickname)));
// based on the group(s) of the affected user(s)
return applyAffiliationChange(getRole(), bareJID, null);
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method changeOccupantRole.
/**
* Updates the presence of the given user with the new role information. Do nothing if the given
* jid is not present in the room.
*
* @param jid the full jid of the user to update his/her role.
* @param newRole the new role for the JID.
* @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.
*/
private Presence changeOccupantRole(JID jid, MUCRole.Role newRole) throws NotAllowedException {
// Try looking the role in the bare JID list
MUCRole role = occupantsByFullJID.get(jid);
// }
if (role != null) {
if (role.isLocal()) {
// Update the presence with the new role
role.setRole(newRole);
// Notify the other cluster nodes to update the occupant
CacheFactory.doClusterTask(new UpdateOccupant(this, role));
// Prepare a new presence to be sent to all the room occupants
return role.getPresence().createCopy();
} else {
// Ask the cluster node hosting the occupant to make the changes. Note that if the change
// is not allowed a NotAllowedException will be thrown
Element element = (Element) CacheFactory.doSynchronousClusterTask(new UpdateOccupantRequest(this, role.getNickname(), null, newRole), role.getNodeID().toByteArray());
if (element != null) {
// Prepare a new presence to be sent to all the room occupants
return new Presence(element, true);
} else {
throw new NotAllowedException();
}
}
}
return null;
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class MultiUserChatServiceImpl method getChatUser.
/**
* Obtain a chat user by XMPPAddress. Only returns users that are connected to this JVM.
*
* @param userjid The XMPPAddress of the user.
* @param roomName name of the room to receive the packet.
* @return The chatuser corresponding to that XMPPAddress.
*/
private MUCUser getChatUser(JID userjid, String roomName) {
if (router == null) {
throw new IllegalStateException("Not initialized");
}
LocalMUCUser user;
synchronized (userjid.toString().intern()) {
user = users.get(userjid);
if (user == null) {
if (roomName != null) {
// Check if the JID belong to a user hosted in another cluster node
LocalMUCRoom localMUCRoom = localMUCRoomManager.getRoom(roomName);
if (localMUCRoom != null) {
MUCRole occupant = localMUCRoom.getOccupantByFullJID(userjid);
if (occupant != null && !occupant.isLocal()) {
return new RemoteMUCUser(userjid, localMUCRoom);
}
}
}
user = new LocalMUCUser(this, router, userjid);
users.put(userjid, user);
}
}
return user;
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method addAdmin.
@Override
public List<Presence> addAdmin(JID jid, MUCRole sendRole) throws ForbiddenException, ConflictException {
final JID bareJID = jid.asBareJID();
lock.writeLock().lock();
try {
MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
if (MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException();
}
// Check that the room always has an owner
if (owners.contains(bareJID) && owners.size() == 1) {
throw new ConflictException();
}
// Check if user is already an admin
if (admins.contains(bareJID)) {
// Do nothing
return Collections.emptyList();
}
admins.add(bareJID);
// Remove the user from other affiliation lists
if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.Affiliation.owner;
} else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.Affiliation.member;
} else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.Affiliation.outcast;
}
// Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB(this, bareJID, null, MUCRole.Affiliation.admin, oldAffiliation);
} finally {
lock.writeLock().unlock();
}
// Update other cluster nodes with new affiliation
CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.admin));
// based on the group(s) of the affected user(s)
return applyAffiliationChange(getRole(), bareJID, null);
}
Aggregations