Search in sources :

Example 26 with MUCRole

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

the class LocalMUCRoom method broadcast.

public void broadcast(BroadcastPresenceRequest presenceRequest) {
    String jid = null;
    Presence presence = presenceRequest.getPresence();
    JID to = presence.getTo();
    Element frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
    // is not a moderator
    if (!canAnyoneDiscoverJID()) {
        jid = frag.element("item").attributeValue("jid");
    }
    for (MUCRole occupant : occupantsByFullJID.values()) {
        if (!occupant.isLocal()) {
            continue;
        }
        // is not a moderator
        if (!canAnyoneDiscoverJID()) {
            if (MUCRole.Role.moderator == occupant.getRole()) {
                frag.element("item").addAttribute("jid", jid);
            } else {
                frag.element("item").addAttribute("jid", null);
            }
        }
        // Some status codes should only be included in the "self-presence", which is only sent to the user, but not to other occupants.
        if (occupant.getPresence().getFrom().equals(to)) {
            Presence selfPresence = presence.createCopy();
            Element fragSelfPresence = selfPresence.getChildElement("x", "http://jabber.org/protocol/muc#user");
            fragSelfPresence.addElement("status").addAttribute("code", "110");
            // http://xmpp.org/registrar/mucstatus.html
            if (presenceRequest.isJoinPresence()) {
                boolean isRoomNew = isLocked() && creationDate.getTime() == lockedTime;
                if (canAnyoneDiscoverJID()) {
                    // // XEP-0045: Example 26.
                    // If the user is entering a room that is non-anonymous (i.e., which informs all occupants of each occupant's full JID as shown above), the service MUST warn the user by including a status code of "100" in the initial presence that the room sends to the new occupant
                    fragSelfPresence.addElement("status").addAttribute("code", "100");
                }
                if (isRoomNew) {
                    fragSelfPresence.addElement("status").addAttribute("code", "201");
                }
            }
            occupant.send(selfPresence);
        } else {
            occupant.send(presence);
        }
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 27 with MUCRole

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

the class LocalMUCRoom method applyAffiliationChange.

/**
     * Evaluate the given JID to determine what the appropriate affiliation should be
     * after a change has been made. Each affected user will be granted the highest
     * affiliation they now possess, either explicitly or implicitly via membership
     * in one or more groups. If the JID is a user, the effective affiliation is
     * applied to each presence corresponding to that user. If the given JID is a group,
     * each user in the group is evaluated to determine what their new affiliations will
     * be. The returned presence updates will be broadcast to the occupants of the room.
     * 
     * @param senderRole Typically the room itself, or an owner/admin
     * @param affiliationJID The JID for the user or group that has been changed
     * @param reason An optional reason to explain why a user was kicked from the room
     * @return List of presence updates to be delivered to the room's occupants
     */
private List<Presence> applyAffiliationChange(MUCRole senderRole, final JID affiliationJID, String reason) {
    // Update the presence(s) for the new affiliation and inform all occupants
    List<JID> affectedOccupants = new ArrayList<>();
    // first, determine which actual (user) JIDs are affected by the affiliation change
    if (GroupJID.isGroup(affiliationJID)) {
        try {
            Group group = GroupManager.getInstance().getGroup(affiliationJID);
            // if so, calculate a new affiliation (if any) for the occupant(s)
            for (JID groupMember : group.getAll()) {
                if (occupantsByBareJID.containsKey(groupMember)) {
                    affectedOccupants.add(groupMember);
                }
            }
        } catch (GroupNotFoundException gnfe) {
            Log.error("Error updating group presences for " + affiliationJID, gnfe);
        }
    } else {
        if (occupantsByBareJID.containsKey(affiliationJID)) {
            affectedOccupants.add(affiliationJID);
        }
    }
    // now update each of the affected occupants with a new role/affiliation
    MUCRole.Role newRole;
    MUCRole.Affiliation newAffiliation;
    List<Presence> updatedPresences = new ArrayList<>();
    // new role/affiliation may be granted via group membership
    for (JID occupantJID : affectedOccupants) {
        Log.info("Applying affiliation change for " + occupantJID);
        boolean kickMember = false, isOutcast = false;
        if (owners.includes(occupantJID)) {
            newRole = MUCRole.Role.moderator;
            newAffiliation = MUCRole.Affiliation.owner;
        } else if (admins.includes(occupantJID)) {
            newRole = MUCRole.Role.moderator;
            newAffiliation = MUCRole.Affiliation.admin;
        } else // outcast trumps member when an affiliation is changed
        if (outcasts.includes(occupantJID)) {
            newAffiliation = MUCRole.Affiliation.outcast;
            newRole = MUCRole.Role.none;
            kickMember = true;
            isOutcast = true;
        } else if (members.includesKey(occupantJID)) {
            newRole = MUCRole.Role.participant;
            newAffiliation = MUCRole.Affiliation.member;
        } else if (isMembersOnly()) {
            newRole = MUCRole.Role.none;
            newAffiliation = MUCRole.Affiliation.none;
            kickMember = true;
        } else {
            newRole = isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant;
            newAffiliation = MUCRole.Affiliation.none;
        }
        Log.info("New affiliation: " + newAffiliation);
        try {
            List<Presence> thisOccupant = changeOccupantAffiliation(senderRole, occupantJID, newAffiliation, newRole);
            if (kickMember) {
                // a status code of 301 indicates the user was removed as an outcast
                for (Presence presence : thisOccupant) {
                    presence.setType(Presence.Type.unavailable);
                    presence.setStatus(null);
                    Element x = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
                    if (reason != null && reason.trim().length() > 0) {
                        x.element("item").addElement("reason").setText(reason);
                    }
                    x.addElement("status").addAttribute("code", isOutcast ? "301" : "321");
                    kickPresence(presence, senderRole.getUserAddress(), senderRole.getNickname());
                }
            }
            updatedPresences.addAll(thisOccupant);
        } catch (NotAllowedException e) {
            Log.error("Error updating presences for " + occupantJID, e);
        }
    }
    return updatedPresences;
}
Also used : Group(org.jivesoftware.openfire.group.Group) 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) MUCRole(org.jivesoftware.openfire.muc.MUCRole) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 28 with MUCRole

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

the class LocalMUCRoom method broadcast.

public void broadcast(BroadcastMessageRequest messageRequest) {
    Message message = messageRequest.getMessage();
    // Add message to the room history
    roomHistory.addMessage(message);
    // Send message to occupants connected to this JVM
    for (MUCRole occupant : occupantsByFullJID.values()) {
        // other cluster nodes
        if (occupant.isLocal() && !occupant.isVoiceOnly()) {
            occupant.send(message);
        }
    }
    if (messageRequest.isOriginator() && isLogEnabled()) {
        MUCRole senderRole = null;
        JID senderAddress;
        // convert the MUC nickname/role JID back into a real user JID
        if (message.getFrom() != null && message.getFrom().getResource() != null) {
            // get the first MUCRole for the sender
            List<MUCRole> occupants = occupantsByNickname.get(message.getFrom().getResource().toLowerCase());
            senderRole = occupants == null ? null : occupants.get(0);
        }
        if (senderRole == null) {
            // The room itself is sending the message
            senderAddress = getRole().getRoleAddress();
        } else {
            // An occupant is sending the message
            senderAddress = senderRole.getUserAddress();
        }
        // Log the conversation
        mucService.logConversation(this, message, senderAddress);
    }
    mucService.messageBroadcastedTo(messageRequest.getOccupants());
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) Message(org.xmpp.packet.Message) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID)

Example 29 with MUCRole

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

the class LocalMUCRoom method sendInitialPresences.

/**
     * Sends presence of existing occupants to new occupant.
     *
     * @param joinRole the role of the new occupant in the room.
     */
private void sendInitialPresences(LocalMUCRole joinRole) {
    for (MUCRole occupant : occupantsByFullJID.values()) {
        if (occupant == joinRole) {
            continue;
        }
        Presence occupantPresence = occupant.getPresence();
        // Skip to the next occupant if we cannot send presence of this occupant
        if (hasToCheckRoleToBroadcastPresence()) {
            Element frag = occupantPresence.getChildElement("x", "http://jabber.org/protocol/muc#user");
            // Check if we can broadcast the presence for this role
            if (!canBroadcastPresence(frag.element("item").attributeValue("role"))) {
                continue;
            }
        }
        // is not a moderator
        if (!canAnyoneDiscoverJID() && MUCRole.Role.moderator != joinRole.getRole()) {
            occupantPresence = occupantPresence.createCopy();
            Element frag = occupantPresence.getChildElement("x", "http://jabber.org/protocol/muc#user");
            frag.element("item").addAttribute("jid", null);
        }
        joinRole.send(occupantPresence);
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 30 with MUCRole

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

the class LocalMUCRoom method kickPresence.

/**
     * Kicks the occupant from the room. This means that the occupant will receive an unavailable
     * presence with the actor that initiated the kick (if any). The occupant will also be removed
     * from the occupants lists.
     *
     * @param kickPresence the presence of the occupant to kick from the room.
     * @param actorJID The JID of the actor that initiated the kick or <tt>null</tt> if the info
     * @param nick The actor nickname.
     * was not provided.
     */
private void kickPresence(Presence kickPresence, JID actorJID, String nick) {
    // Get the role(s) to kick
    List<MUCRole> occupants = new ArrayList<>(occupantsByNickname.get(kickPresence.getFrom().getResource().toLowerCase()));
    for (MUCRole kickedRole : occupants) {
        // Add the actor's JID that kicked this user from the room
        if (actorJID != null && actorJID.toString().length() > 0) {
            Element frag = kickPresence.getChildElement("x", "http://jabber.org/protocol/muc#user");
            Element actor = frag.element("item").addElement("actor");
            actor.addAttribute("jid", actorJID.toBareJID());
            if (nick != null) {
                actor.addAttribute("nick", nick);
            }
        }
        // Send the unavailable presence to the banned user
        kickedRole.send(kickPresence);
        // Remove the occupant from the room's occupants lists
        OccupantLeftEvent event = new OccupantLeftEvent(this, kickedRole);
        event.setOriginator(true);
        event.run();
        // Remove the occupant from the room's occupants lists
        event = new OccupantLeftEvent(this, kickedRole);
        CacheFactory.doClusterTask(event);
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) OccupantLeftEvent(org.jivesoftware.openfire.muc.cluster.OccupantLeftEvent) Element(org.dom4j.Element) ArrayList(java.util.ArrayList)

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