use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.
the class LocalMUCRoom method occupantUpdated.
public void occupantUpdated(UpdateOccupant update) {
List<MUCRole> occupants = occupantsByNickname.get(update.getNickname().toLowerCase());
if (occupants == null || occupants.size() == 0) {
Log.debug("LocalMUCRoom: Failed to update information of room occupant. Occupant nickname: " + update.getNickname());
} else {
for (MUCRole occupant : occupants) {
if (!occupant.isLocal()) {
occupant.setPresence(update.getPresence());
try {
occupant.setRole(update.getRole());
occupant.setAffiliation(update.getAffiliation());
} catch (NotAllowedException e) {
// Ignore. Should never happen with remote roles
}
} else {
Log.error(MessageFormat.format("Ignoring update of local occupant with info from a remote occupant. " + "Occupant nickname: {0} new role: {1} new affiliation: {2}", update.getNickname(), update.getRole(), update.getAffiliation()));
}
}
}
}
use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.
the class LocalMUCRoom method changeOccupantAffiliation.
/**
* Updates all the presences of the given user with the new affiliation and role information. Do
* nothing if the given jid is not present in the room. If the user has joined the room from
* several client resources, all his/her occupants' presences will be updated.
*
* @param jid the bare jid of the user to update his/her role.
* @param newAffiliation the new affiliation for the JID.
* @param newRole the new role for the JID.
* @return the list of updated presences of all the client resources that the client used to
* join the room.
* @throws NotAllowedException If trying to change the moderator role to an owner or an admin or
* if trying to ban an owner or an administrator.
*/
private List<Presence> changeOccupantAffiliation(MUCRole senderRole, JID jid, MUCRole.Affiliation newAffiliation, MUCRole.Role newRole) throws NotAllowedException {
List<Presence> presences = new ArrayList<>();
// Get all the roles (i.e. occupants) of this user based on his/her bare JID
JID bareJID = jid.asBareJID();
List<MUCRole> roles = occupantsByBareJID.get(bareJID);
if (roles == null) {
return presences;
}
// Collect all the updated presences of these roles
for (MUCRole role : roles) {
// Update the presence with the new affiliation and role
if (role.isLocal()) {
role.setAffiliation(newAffiliation);
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
presences.add(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(), newAffiliation, newRole), role.getNodeID().toByteArray());
if (element != null) {
// Prepare a new presence to be sent to all the room occupants
presences.add(new Presence(element, true));
} else {
throw new NotAllowedException();
}
}
}
// Answer all the updated presences
return presences;
}
use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.
the class LocalMUCRoom method setMembersOnly.
@Override
public List<Presence> setMembersOnly(boolean membersOnly) {
List<Presence> presences = new ArrayList<>();
if (membersOnly && !this.membersOnly) {
// of the room
for (MUCRole occupant : occupantsByFullJID.values()) {
if (occupant.getAffiliation().compareTo(MUCRole.Affiliation.member) > 0) {
try {
presences.add(kickOccupant(occupant.getRoleAddress(), null, null, LocaleUtils.getLocalizedString("muc.roomIsNowMembersOnly")));
} catch (NotAllowedException e) {
Log.error(e.getMessage(), e);
}
}
}
}
this.membersOnly = membersOnly;
return presences;
}
use of org.jivesoftware.openfire.muc.NotAllowedException 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.NotAllowedException in project Openfire by igniterealtime.
the class LocalMUCRoomManager method kickOccupantBecauseOfNicknameCollision.
/**
* Kick a user out of a room for reason of nickname collision.
* @param room The room to kick the user out of.
* @param nickBeingAddedToRoom The nickname that is the cause of the problem.
* @param userToBeKicked The full jid of the user to be kicked.
* @param occupantManager The occupant manager that contains local occupant registration.
*/
private void kickOccupantBecauseOfNicknameCollision(MUCRoom room, String nickBeingAddedToRoom, JID userToBeKicked, @Nonnull OccupantManager occupantManager) {
Log.info("Occupant {} of room {} with nickname {} has to be kicked out because the nickname clashes with another user in the same room.", userToBeKicked, room.getName(), nickBeingAddedToRoom);
// Kick the user from all the rooms that he/she had previously joined.
try {
final Presence kickedPresence = room.kickOccupant(userToBeKicked, null, null, "Nickname clash with other user in the same room.");
Log.trace("Kick presence to be sent to room: {}", kickedPresence);
// Send the updated presence to the room occupants, but only those on this local node.
room.send(kickedPresence, room.getRole());
Log.debug("Kicked occupant '{}' out of room '{}'.", userToBeKicked, room.getName());
} catch (final NotAllowedException e) {
// Do nothing since we cannot kick owners or admins
Log.debug("Occupant '{}' not kicked out of room '{}' because of '{}'.", userToBeKicked, room.getName(), e.getMessage());
}
}
Aggregations