Search in sources :

Example 11 with MUCRole

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

Example 12 with MUCRole

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);
}
Also used : ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) ConflictException(org.jivesoftware.openfire.muc.ConflictException) AddMember(org.jivesoftware.openfire.muc.cluster.AddMember)

Example 13 with MUCRole

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;
}
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 14 with MUCRole

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

Example 15 with MUCRole

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);
}
Also used : ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) ConflictException(org.jivesoftware.openfire.muc.ConflictException) AddAffiliation(org.jivesoftware.openfire.muc.cluster.AddAffiliation)

Aggregations

MUCRole (org.jivesoftware.openfire.muc.MUCRole)31 GroupJID (org.jivesoftware.openfire.group.GroupJID)15 JID (org.xmpp.packet.JID)14 ArrayList (java.util.ArrayList)10 Presence (org.xmpp.packet.Presence)10 Element (org.dom4j.Element)9 ForbiddenException (org.jivesoftware.openfire.muc.ForbiddenException)9 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)9 NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)8 ConflictException (org.jivesoftware.openfire.muc.ConflictException)6 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)6 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)5 AddAffiliation (org.jivesoftware.openfire.muc.cluster.AddAffiliation)4 UpdateOccupant (org.jivesoftware.openfire.muc.cluster.UpdateOccupant)3 NotFoundException (org.jivesoftware.util.NotFoundException)3 Message (org.xmpp.packet.Message)3 IOException (java.io.IOException)2 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)2 Group (org.jivesoftware.openfire.group.Group)2 CannotBeInvitedException (org.jivesoftware.openfire.muc.CannotBeInvitedException)2