Search in sources :

Example 1 with MUCRole

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

the class LocalMUCRoom method occupantAdded.

public void occupantAdded(OccupantAddedEvent event) {
    // Create a proxy for the occupant that joined the room from another cluster node
    RemoteMUCRole joinRole = new RemoteMUCRole(mucService, event);
    JID bareJID = event.getUserAddress().asBareJID();
    String nickname = event.getNickname();
    List<MUCRole> occupants = occupantsByNickname.get(nickname.toLowerCase());
    // Do not add new occupant with one with same nickname already exists
    if (occupants == null) {
        occupants = new ArrayList<>();
        occupantsByNickname.put(nickname.toLowerCase(), occupants);
    } else {
        // sanity check; make sure the nickname is owned by the same JID
        if (occupants.size() > 0) {
            JID existingJID = occupants.get(0).getUserAddress().asBareJID();
            if (!bareJID.equals(existingJID)) {
                Log.warn(MessageFormat.format("Conflict detected; {0} requested nickname '{1}'; already being used by {2}", bareJID, nickname, existingJID));
                return;
            }
        }
    }
    // Add the new user as an occupant of this room
    occupants.add(joinRole);
    // Update the tables of occupants based on the bare and full JID
    List<MUCRole> list = occupantsByBareJID.get(bareJID);
    if (list == null) {
        list = new ArrayList<>();
        occupantsByBareJID.put(bareJID, list);
    }
    list.add(joinRole);
    occupantsByFullJID.put(event.getUserAddress(), joinRole);
    // Update the date when the last occupant left the room
    setEmptyDate(null);
    if (event.isOriginator()) {
        // Fire event that occupant joined the room
        MUCEventDispatcher.occupantJoined(getRole().getRoleAddress(), event.getUserAddress(), joinRole.getNickname());
    }
    // Check if we need to send presences of the new occupant to occupants hosted by this JVM
    if (event.isSendPresence()) {
        for (MUCRole occupant : occupantsByFullJID.values()) {
            if (occupant.isLocal()) {
                occupant.send(event.getPresence().createCopy());
            }
        }
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID)

Example 2 with MUCRole

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

the class LocalMUCRoom method addOwner.

@Override
public List<Presence> addOwner(JID jid, MUCRole sendRole) throws ForbiddenException {
    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 if user is already an owner (explicitly)
        if (owners.contains(bareJID)) {
            // Do nothing
            return Collections.emptyList();
        }
        owners.add(bareJID);
        // Remove the user from other affiliation lists
        if (removeAdmin(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.admin;
        } 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.owner, oldAffiliation);
    } finally {
        lock.writeLock().unlock();
    }
    // Update other cluster nodes with new affiliation
    CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.owner));
    // 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) AddAffiliation(org.jivesoftware.openfire.muc.cluster.AddAffiliation)

Example 3 with MUCRole

use of org.jivesoftware.openfire.muc.MUCRole 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 4 with MUCRole

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

the class LocalMUCRoom method broadcastPresence.

/**
     * Broadcasts the specified presence to all room occupants. If the presence belongs to a
     * user whose role cannot be broadcast then the presence will only be sent to the presence's
     * user. On the other hand, the JID of the user that sent the presence won't be included if the
     * room is semi-anon and the target occupant is not a moderator.
     *
     * @param presence the presence to broadcast.
     * @param isJoinPresence If the presence is sent in the context of joining the room.
     */
private void broadcastPresence(Presence presence, boolean isJoinPresence) {
    if (presence == null) {
        return;
    }
    if (!shouldBroadcastPresence(presence)) {
        // Just send the presence to the sender of the presence
        try {
            for (MUCRole occupant : getOccupantsByNickname(presence.getFrom().getResource())) {
                occupant.send(presence);
            }
        } catch (UserNotFoundException e) {
        // Do nothing
        }
        return;
    }
    // Broadcast presence to occupants hosted by other cluster nodes
    BroadcastPresenceRequest request = new BroadcastPresenceRequest(this, presence, isJoinPresence);
    CacheFactory.doClusterTask(request);
    // Broadcast presence to occupants connected to this JVM
    request = new BroadcastPresenceRequest(this, presence, isJoinPresence);
    request.setOriginator(true);
    request.run();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) BroadcastPresenceRequest(org.jivesoftware.openfire.muc.cluster.BroadcastPresenceRequest)

Example 5 with MUCRole

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

the class LocalMUCRoom method leaveRoom.

public void leaveRoom(OccupantLeftEvent event) {
    MUCRole leaveRole = event.getRole();
    if (leaveRole == null) {
        return;
    }
    lock.writeLock().lock();
    try {
        // Removes the role from the room
        removeOccupantRole(leaveRole, event.isOriginator());
        // not persistent
        if (occupantsByFullJID.isEmpty() && !isPersistent()) {
            endTime = System.currentTimeMillis();
            if (event.isOriginator()) {
                mucService.removeChatRoom(name);
                // Fire event that the room has been destroyed
                MUCEventDispatcher.roomDestroyed(getRole().getRoleAddress());
            }
        }
        if (occupantsByFullJID.isEmpty()) {
            // Update the date when the last occupant left the room
            setEmptyDate(new Date());
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) Date(java.util.Date)

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