Search in sources :

Example 21 with JID

use of org.xmpp.packet.JID 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 22 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class LocalMUCRoom method joinRoom.

@Override
public LocalMUCRole joinRoom(String nickname, String password, HistoryRequest historyRequest, LocalMUCUser user, Presence presence) throws UnauthorizedException, UserAlreadyExistsException, RoomLockedException, ForbiddenException, RegistrationRequiredException, ConflictException, ServiceUnavailableException, NotAcceptableException {
    if (((MultiUserChatServiceImpl) mucService).getMUCDelegate() != null) {
        if (!((MultiUserChatServiceImpl) mucService).getMUCDelegate().joiningRoom(this, user.getAddress())) {
            // Delegate said no, reject join.
            throw new UnauthorizedException();
        }
    }
    LocalMUCRole joinRole = null;
    lock.writeLock().lock();
    boolean clientOnlyJoin = false;
    // A "client only join" here is one where the client is already joined, but has re-joined.
    try {
        // If the room has a limit of max user then check if the limit has been reached
        if (!canJoinRoom(user)) {
            throw new ServiceUnavailableException();
        }
        final JID bareJID = user.getAddress().asBareJID();
        boolean isOwner = owners.includes(bareJID);
        // If the room is locked and this user is not an owner raise a RoomLocked exception
        if (isLocked()) {
            if (!isOwner) {
                throw new RoomLockedException();
            }
        }
        // Check if the nickname is already used in the room
        if (occupantsByNickname.containsKey(nickname.toLowerCase())) {
            List<MUCRole> occupants = occupantsByNickname.get(nickname.toLowerCase());
            MUCRole occupant = occupants.size() > 0 ? occupants.get(0) : null;
            if (occupant != null && !occupant.getUserAddress().toBareJID().equals(bareJID.toBareJID())) {
                // Nickname is already used, and not by the same JID
                throw new UserAlreadyExistsException();
            }
            if (occupant.getUserAddress().equals(user.getAddress())) {
                // This user is already an occupant. The client thinks it isn't. (Or else this is a broken gmail).
                clientOnlyJoin = true;
            }
        }
        // Unauthorized exception
        if (isPasswordProtected()) {
            if (password == null || !password.equals(getPassword())) {
                throw new UnauthorizedException();
            }
        }
        // raise a ConflictException
        if (members.containsValue(nickname.toLowerCase())) {
            if (!nickname.toLowerCase().equals(members.get(bareJID))) {
                throw new ConflictException();
            }
        }
        if (isLoginRestrictedToNickname()) {
            String reservedNickname = members.get(bareJID);
            if (reservedNickname != null && !nickname.toLowerCase().equals(reservedNickname)) {
                throw new NotAcceptableException();
            }
        }
        // Set the corresponding role based on the user's affiliation
        MUCRole.Role role;
        MUCRole.Affiliation affiliation;
        if (isOwner) {
            // The user is an owner. Set the role and affiliation accordingly.
            role = MUCRole.Role.moderator;
            affiliation = MUCRole.Affiliation.owner;
        } else if (mucService.isSysadmin(bareJID)) {
            // The user is a system administrator of the MUC service. Treat him as an owner
            // although he won't appear in the list of owners
            role = MUCRole.Role.moderator;
            affiliation = MUCRole.Affiliation.owner;
        } else if (admins.includes(bareJID)) {
            // The user is an admin. Set the role and affiliation accordingly.
            role = MUCRole.Role.moderator;
            affiliation = MUCRole.Affiliation.admin;
        } else // explicit outcast status has higher precedence than member status
        if (outcasts.contains(bareJID)) {
            // The user is an outcast. Raise a "Forbidden" exception.
            throw new ForbiddenException();
        } else if (members.includesKey(bareJID)) {
            // The user is a member. Set the role and affiliation accordingly.
            role = MUCRole.Role.participant;
            affiliation = MUCRole.Affiliation.member;
        } else // this checks if the user is an outcast implicitly (via a group)
        if (outcasts.includes(bareJID)) {
            // The user is an outcast. Raise a "Forbidden" exception.
            throw new ForbiddenException();
        } else {
            // The user has no affiliation (i.e. NONE). Set the role accordingly.
            if (isMembersOnly()) {
                // "Registration Required" exception.
                throw new RegistrationRequiredException();
            }
            role = (isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant);
            affiliation = MUCRole.Affiliation.none;
        }
        if (!clientOnlyJoin) {
            // Create a new role for this user in this room
            joinRole = new LocalMUCRole(mucService, this, nickname, role, affiliation, user, presence, router);
            // Add the new user as an occupant of this room
            List<MUCRole> occupants = occupantsByNickname.get(nickname.toLowerCase());
            if (occupants == null) {
                occupants = new ArrayList<>();
                occupantsByNickname.put(nickname.toLowerCase(), occupants);
            }
            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(user.getAddress(), joinRole);
        } else {
            // Grab the existing one.
            joinRole = (LocalMUCRole) occupantsByFullJID.get(user.getAddress());
        }
    } finally {
        lock.writeLock().unlock();
    }
    // Notify other cluster nodes that a new occupant joined the room
    CacheFactory.doClusterTask(new OccupantAddedEvent(this, joinRole));
    // Send presence of existing occupants to new occupant
    sendInitialPresences(joinRole);
    // It is assumed that the room is new based on the fact that it's locked and
    // that it was locked when it was created.
    boolean isRoomNew = isLocked() && creationDate.getTime() == lockedTime;
    try {
        // Send the presence of this new occupant to existing occupants
        Presence joinPresence = joinRole.getPresence().createCopy();
        broadcastPresence(joinPresence, true);
    } catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
    // confirmed" message
    if (!isRoomNew && isLocked()) {
        // http://xmpp.org/extensions/xep-0045.html#enter-locked
        Presence presenceItemNotFound = new Presence(Presence.Type.error);
        presenceItemNotFound.setError(PacketError.Condition.item_not_found);
        presenceItemNotFound.setFrom(role.getRoleAddress());
        joinRole.send(presenceItemNotFound);
    }
    if (historyRequest == null) {
        Iterator<Message> history = roomHistory.getMessageHistory();
        while (history.hasNext()) {
            joinRole.send(history.next());
        }
    } else {
        historyRequest.sendHistory(joinRole, roomHistory);
    }
    Message roomSubject = roomHistory.getChangedSubject();
    if (roomSubject != null) {
        joinRole.send(roomSubject);
    }
    if (!clientOnlyJoin) {
        // Update the date when the last occupant left the room
        setEmptyDate(null);
        // Fire event that occupant joined the room
        MUCEventDispatcher.occupantJoined(getRole().getRoleAddress(), user.getAddress(), joinRole.getNickname());
    }
    return joinRole;
}
Also used : ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) ConflictException(org.jivesoftware.openfire.muc.ConflictException) OccupantAddedEvent(org.jivesoftware.openfire.muc.cluster.OccupantAddedEvent) ServiceUnavailableException(org.jivesoftware.openfire.muc.ServiceUnavailableException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) RoomLockedException(org.jivesoftware.openfire.muc.RoomLockedException) CannotBeInvitedException(org.jivesoftware.openfire.muc.CannotBeInvitedException) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) NotFoundException(org.jivesoftware.util.NotFoundException) ConflictException(org.jivesoftware.openfire.muc.ConflictException) RegistrationRequiredException(org.jivesoftware.openfire.muc.RegistrationRequiredException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NotAcceptableException(org.jivesoftware.openfire.muc.NotAcceptableException) ServiceUnavailableException(org.jivesoftware.openfire.muc.ServiceUnavailableException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAcceptableException(org.jivesoftware.openfire.muc.NotAcceptableException) RoomLockedException(org.jivesoftware.openfire.muc.RoomLockedException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence) RegistrationRequiredException(org.jivesoftware.openfire.muc.RegistrationRequiredException)

Example 23 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class LocalMUCRoom method affiliationAdded.

public void affiliationAdded(AddAffiliation affiliation) {
    JID affiliationJID = affiliation.getBareJID();
    switch(affiliation.getAffiliation()) {
        case owner:
            removeMember(affiliationJID);
            removeAdmin(affiliationJID);
            removeOutcast(affiliationJID);
            owners.add(affiliationJID);
            break;
        case admin:
            removeMember(affiliationJID);
            removeOwner(affiliationJID);
            removeOutcast(affiliationJID);
            admins.add(affiliationJID);
            break;
        case outcast:
            removeMember(affiliationJID);
            removeAdmin(affiliationJID);
            removeOwner(affiliationJID);
            outcasts.add(affiliationJID);
            break;
        case none:
        default:
            removeMember(affiliationJID);
            removeAdmin(affiliationJID);
            removeOwner(affiliationJID);
            removeOutcast(affiliationJID);
            break;
    }
}
Also used : GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID)

Example 24 with JID

use of org.xmpp.packet.JID in project Openfire by igniterealtime.

the class LocalMUCRoom method getReservedNickname.

@Override
public String getReservedNickname(JID jid) {
    final JID bareJID = jid.asBareJID();
    String answer = members.get(bareJID);
    if (answer == null || answer.trim().length() == 0) {
        return null;
    }
    return answer;
}
Also used : GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID)

Example 25 with JID

use of org.xmpp.packet.JID 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)

Aggregations

JID (org.xmpp.packet.JID)330 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)76 Element (org.dom4j.Element)70 ArrayList (java.util.ArrayList)55 IQ (org.xmpp.packet.IQ)38 Presence (org.xmpp.packet.Presence)38 SQLException (java.sql.SQLException)36 PreparedStatement (java.sql.PreparedStatement)31 Connection (java.sql.Connection)30 Group (org.jivesoftware.openfire.group.Group)30 Date (java.util.Date)28 ResultSet (java.sql.ResultSet)27 Message (org.xmpp.packet.Message)25 GroupJID (org.jivesoftware.openfire.group.GroupJID)23 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)23 NotFoundException (org.jivesoftware.util.NotFoundException)22 Roster (org.jivesoftware.openfire.roster.Roster)21 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)20 RosterItem (org.jivesoftware.openfire.roster.RosterItem)19 User (org.jivesoftware.openfire.user.User)17