Search in sources :

Example 1 with NotAllowedException

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()));
            }
        }
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException)

Example 2 with NotAllowedException

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;
}
Also used : UpdateOccupant(org.jivesoftware.openfire.muc.cluster.UpdateOccupant) MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) UpdateOccupantRequest(org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 3 with NotAllowedException

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;
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) ArrayList(java.util.ArrayList) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 4 with NotAllowedException

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;
}
Also used : UpdateOccupant(org.jivesoftware.openfire.muc.cluster.UpdateOccupant) MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) Element(org.dom4j.Element) UpdateOccupantRequest(org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 5 with NotAllowedException

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());
    }
}
Also used : NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) Presence(org.xmpp.packet.Presence)

Aggregations

NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)12 Element (org.dom4j.Element)6 MUCRole (org.jivesoftware.openfire.muc.MUCRole)6 Presence (org.xmpp.packet.Presence)5 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)4 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)4 ArrayList (java.util.ArrayList)3 Lock (java.util.concurrent.locks.Lock)3 JID (org.xmpp.packet.JID)3 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)2 GroupJID (org.jivesoftware.openfire.group.GroupJID)2 ConflictException (org.jivesoftware.openfire.muc.ConflictException)2 ForbiddenException (org.jivesoftware.openfire.muc.ForbiddenException)2 NotAcceptableException (org.jivesoftware.openfire.muc.NotAcceptableException)2 RegistrationRequiredException (org.jivesoftware.openfire.muc.RegistrationRequiredException)2 RoomLockedException (org.jivesoftware.openfire.muc.RoomLockedException)2 ServiceUnavailableException (org.jivesoftware.openfire.muc.ServiceUnavailableException)2 UpdateOccupant (org.jivesoftware.openfire.muc.cluster.UpdateOccupant)2 UpdateOccupantRequest (org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest)2 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)2