Search in sources :

Example 11 with NotAllowedException

use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.

the class MultiUserChatServiceImpl method tryRemoveOccupantFromRoom.

/**
 * Removes an occupant from a room.
 *
 * When the system is 'busy', the occupant will not be removed to prevent threads blocking. In such cases, a message
 * is added to the log files, but the occupant remains in the room.
 *
 * @param occupant The occupant to be removed
 * @param reason A human-readable reason for the removal (to be shared with the occupant as well as other occupants of the room).
 */
private void tryRemoveOccupantFromRoom(@Nonnull final OccupantManager.Occupant occupant, @Nonnull final String reason) {
    final Lock lock = getChatRoomLock(occupant.getRoomName());
    if (!lock.tryLock()) {
        // Don't block on locked rooms, as we're processing many of them. We'll get them in the next round.
        Log.info("Skip removing as a cluster-wide mutex for the room could not immediately be obtained: {}, should have been removed, because: {}", occupant, reason);
        return;
    }
    try {
        final MUCRoom room = getChatRoom(occupant.getRoomName());
        if (room == null) {
            // Room was recently removed? Mismatch between MUCUser#getRooms() and MUCRoom#localMUCRoomManager?
            Log.info("Skip removing {} as the room no longer exists.", occupant);
            return;
        }
        if (!room.hasOccupant(occupant.getRealJID())) {
            // Occupant no longer in room? Mismatch between MUCUser#getRooms() and MUCRoom#localMUCRoomManager?
            Log.debug("Skip removing {} as this occupant no longer is in the room.", occupant);
            return;
        }
        // Kick the user from the room that he/she had previously joined.
        Log.debug("Removing/kicking {}: {}", occupant, reason);
        room.kickOccupant(occupant.getRealJID(), null, null, reason);
        // Ensure that other cluster nodes see any changes that might have been applied.
        syncChatRoom(room);
    } catch (final NotAllowedException e) {
        // Do nothing since we cannot kick owners or admins
        Log.debug("Skip removing {}, because it's not allowed (this user likely is an owner of admin of the room).", occupant, e);
    } finally {
        lock.unlock();
    }
}
Also used : MUCRoom(org.jivesoftware.openfire.muc.MUCRoom) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) AutoCloseableReentrantLock(org.jivesoftware.util.AutoCloseableReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 12 with NotAllowedException

use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.

the class MultiUserChatServiceImpl method processRoomJoinRequest.

/**
 * Process a request to join a room.
 *
 * This method might be invoked for a room that does not yet exist (when the presence is a room-creation request).
 *
 * @param packet   The stanza representing the nickname-change request.
 * @param roomName The name of the room that the stanza was addressed to.
 * @param room     The room that the stanza was addressed to, if it exists.
 * @param nickname The requested nickname.
 * @return the room that handled the request
 */
private MUCRoom processRoomJoinRequest(@Nonnull final Presence packet, @Nonnull final String roomName, @Nullable MUCRoom room, @Nullable String nickname) {
    Log.trace("Processing join request from '{}' for room '{}'", packet.getFrom(), roomName);
    if (nickname == null) {
        Log.debug("Request from '{}' to join room '{}' rejected: request did not specify a nickname", packet.getFrom(), roomName);
        // If the user does not specify a room nickname (note the bare JID on the 'from' address in the following example), the service MUST return a <jid-malformed/> error
        if (packet.getType() != Presence.Type.error) {
            sendErrorPacket(packet, PacketError.Condition.jid_malformed, "A nickname (resource-part) is required in order to join a room.");
        }
        return null;
    }
    if (!packet.isAvailable()) {
        Log.debug("Request from '{}' to join room '{}' rejected: request unexpectedly provided a presence stanza of type '{}'. Expected none.", packet.getFrom(), roomName, packet.getType());
        if (packet.getType() != Presence.Type.error) {
            sendErrorPacket(packet, PacketError.Condition.unexpected_request, "Unexpected stanza type: " + packet.getType());
        }
        return null;
    }
    if (room == null) {
        try {
            // Create the room
            room = getChatRoom(roomName, packet.getFrom());
        } catch (NotAllowedException e) {
            Log.debug("Request from '{}' to join room '{}' rejected: user does not have permission to create a new room.", packet.getFrom(), roomName, e);
            sendErrorPacket(packet, PacketError.Condition.not_allowed, "You do not have permission to create a new room.");
            return null;
        }
    }
    try {
        // User must support MUC in order to create a room
        HistoryRequest historyRequest = null;
        String password = null;
        // Check for password & requested history if client supports MUC
        final Element mucInfo = packet.getChildElement("x", "http://jabber.org/protocol/muc");
        if (mucInfo != null) {
            password = mucInfo.elementTextTrim("password");
            if (mucInfo.element("history") != null) {
                historyRequest = new HistoryRequest(mucInfo);
            }
        }
        // The user joins the room
        final MUCRole role = room.joinRoom(nickname, password, historyRequest, packet.getFrom(), packet.createCopy());
        // unlock the room thus creating an "instant" room
        if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) {
            room.unlock(role);
        }
    } catch (UnauthorizedException e) {
        Log.debug("Request from '{}' to join room '{}' rejected: user not authorized to create or join the room.", packet.getFrom(), roomName, e);
        sendErrorPacket(packet, PacketError.Condition.not_authorized, "You're not authorized to create or join the room.");
    } catch (ServiceUnavailableException e) {
        Log.debug("Request from '{}' to join room '{}' rejected: the maximum number of users of the room has been reached.", packet.getFrom(), roomName, e);
        sendErrorPacket(packet, PacketError.Condition.service_unavailable, "The maximum number of users of the room has been reached.");
    } catch (UserAlreadyExistsException | ConflictException e) {
        Log.debug("Request from '{}' to join room '{}' rejected: the requested nickname '{}' is being used by someone else in the room.", packet.getFrom(), roomName, nickname, e);
        sendErrorPacket(packet, PacketError.Condition.conflict, "The nickname that is being used is used by someone else.");
    } catch (RoomLockedException e) {
        // If a user attempts to enter a room while it is "locked" (i.e., before the room creator provides an initial configuration and therefore before the room officially exists), the service MUST refuse entry and return an <item-not-found/> error to the user
        Log.debug("Request from '{}' to join room '{}' rejected: room is locked.", packet.getFrom(), roomName, e);
        sendErrorPacket(packet, PacketError.Condition.item_not_found, "This room is locked (it might not have been configured yet).");
    } catch (ForbiddenException e) {
        Log.debug("Request from '{}' to join room '{}' rejected: user not authorized join the room.", packet.getFrom(), roomName, e);
        sendErrorPacket(packet, PacketError.Condition.forbidden, "You're not allowed to join this room.");
    } catch (RegistrationRequiredException e) {
        Log.debug("Request from '{}' to join room '{}' rejected: room is member-only, user is not a member.", packet.getFrom(), roomName, e);
        sendErrorPacket(packet, PacketError.Condition.registration_required, "This is a member-only room. Membership is required.");
    } catch (NotAcceptableException e) {
        Log.debug("Request from '{}' to join room '{}' rejected: user attempts to use nickname '{}' which is different from the reserved nickname.", packet.getFrom(), roomName, nickname, e);
        sendErrorPacket(packet, PacketError.Condition.not_acceptable, "You're trying to join with a nickname different than the reserved nickname.");
    }
    return room;
}
Also used : ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) ConflictException(org.jivesoftware.openfire.muc.ConflictException) Element(org.dom4j.Element) HistoryRequest(org.jivesoftware.openfire.muc.HistoryRequest) ServiceUnavailableException(org.jivesoftware.openfire.muc.ServiceUnavailableException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAcceptableException(org.jivesoftware.openfire.muc.NotAcceptableException) RoomLockedException(org.jivesoftware.openfire.muc.RoomLockedException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) RegistrationRequiredException(org.jivesoftware.openfire.muc.RegistrationRequiredException)

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