Search in sources :

Example 41 with Presence

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

the class IQAdminHandler method handleItemsElement.

/**
     * Handles packets that includes item elements. Depending on the item's attributes the
     * interpretation of the request may differ. For example, an item that only contains the
     * "affiliation" attribute is requesting the list of participants or members. Whilst if the item
     * contains the affiliation together with a jid means that the client is changing the
     * affiliation of the requested jid.
     *
     * @param senderRole the role of the user that sent the request packet.
     * @param itemsList  the list of items sent by the client.
     * @param reply      the iq packet that will be sent back as a reply to the client's request.
     * @throws ForbiddenException If the user is not allowed to perform his request.
     * @throws ConflictException If the desired room nickname is already reserved for the room or
     *                           if the room was going to lose all of its owners.
     * @throws NotAllowedException Thrown if trying to ban an owner or an administrator.
     * @throws CannotBeInvitedException If the user being invited as a result of being added to a members-only room still does not have permission
     */
private void handleItemsElement(MUCRole senderRole, List<Element> itemsList, IQ reply) throws ForbiddenException, ConflictException, NotAllowedException, CannotBeInvitedException {
    Element item;
    String affiliation;
    String roleAttribute;
    boolean hasJID = itemsList.get(0).attributeValue("jid") != null;
    boolean hasNick = itemsList.get(0).attributeValue("nick") != null;
    // Check if the client is requesting or changing the list of moderators/members/etc.
    if (!hasJID && !hasNick) {
        // The client is requesting the list of moderators/members/participants/outcasts
        // Create the result that will hold an item for each
        // moderator/member/participant/outcast
        Element result = reply.setChildElement("query", "http://jabber.org/protocol/muc#admin");
        for (Object anItem : itemsList) {
            item = (Element) anItem;
            affiliation = item.attributeValue("affiliation");
            roleAttribute = item.attributeValue("role");
            Element metaData;
            if ("outcast".equals(affiliation)) {
                // The client is requesting the list of outcasts
                if (MUCRole.Affiliation.admin != senderRole.getAffiliation() && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
                    throw new ForbiddenException();
                }
                for (JID jid : room.getOutcasts()) {
                    if (GroupJID.isGroup(jid)) {
                        try {
                            // add each group member to the result (clients don't understand groups)
                            Group group = GroupManager.getInstance().getGroup(jid);
                            for (JID groupMember : group.getAll()) {
                                metaData = addAffiliationToResult(affiliation, result, groupMember);
                            }
                        } catch (GroupNotFoundException gnfe) {
                            logger.warn("Invalid group JID in the outcast list: " + jid);
                        }
                    } else {
                        metaData = addAffiliationToResult(affiliation, result, jid);
                    }
                }
            } else if ("member".equals(affiliation)) {
                // In a members-only room members can get the list of members
                if (!room.isMembersOnly() && MUCRole.Affiliation.admin != senderRole.getAffiliation() && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
                    throw new ForbiddenException();
                }
                for (JID jid : room.getMembers()) {
                    if (GroupJID.isGroup(jid)) {
                        try {
                            // add each group member to the result (clients don't understand groups)
                            Group group = GroupManager.getInstance().getGroup(jid);
                            for (JID groupMember : group.getAll()) {
                                metaData = addAffiliationToResult(affiliation, result, groupMember);
                            }
                        } catch (GroupNotFoundException gnfe) {
                            logger.warn("Invalid group JID in the member list: " + jid);
                        }
                    } else {
                        metaData = addAffiliationToResult(affiliation, result, jid);
                    }
                }
            } else if ("moderator".equals(roleAttribute)) {
                // The client is requesting the list of moderators
                if (MUCRole.Affiliation.admin != senderRole.getAffiliation() && MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
                    throw new ForbiddenException();
                }
                for (MUCRole role : room.getModerators()) {
                    metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin");
                    metaData.addAttribute("role", "moderator");
                    metaData.addAttribute("jid", role.getUserAddress().toString());
                    metaData.addAttribute("nick", role.getNickname());
                    metaData.addAttribute("affiliation", role.getAffiliation().toString());
                }
            } else if ("participant".equals(roleAttribute)) {
                // The client is requesting the list of participants
                if (MUCRole.Role.moderator != senderRole.getRole()) {
                    throw new ForbiddenException();
                }
                for (MUCRole role : room.getParticipants()) {
                    metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin");
                    metaData.addAttribute("role", "participant");
                    metaData.addAttribute("jid", role.getUserAddress().toString());
                    metaData.addAttribute("nick", role.getNickname());
                    metaData.addAttribute("affiliation", role.getAffiliation().toString());
                }
            } else if ("owner".equals(affiliation)) {
                // The client is requesting the list of owners
                for (JID jid : room.getOwners()) {
                    if (GroupJID.isGroup(jid)) {
                        try {
                            // add each group member to the result (clients don't understand groups)
                            Group group = GroupManager.getInstance().getGroup(jid);
                            for (JID groupMember : group.getAll()) {
                                metaData = addAffiliationToResult(affiliation, result, groupMember);
                            }
                        } catch (GroupNotFoundException gnfe) {
                            logger.warn("Invalid group JID in the owner list: " + jid);
                        }
                    } else {
                        metaData = addAffiliationToResult(affiliation, result, jid);
                    }
                }
            } else if ("admin".equals(affiliation)) {
                // The client is requesting the list of admins
                for (JID jid : room.getAdmins()) {
                    if (GroupJID.isGroup(jid)) {
                        try {
                            // add each group member to the result (clients don't understand groups)
                            Group group = GroupManager.getInstance().getGroup(jid);
                            for (JID groupMember : group.getAll()) {
                                metaData = addAffiliationToResult(affiliation, result, groupMember);
                            }
                        } catch (GroupNotFoundException gnfe) {
                            logger.warn("Invalid group JID in the admin list: " + jid);
                        }
                    } else {
                        metaData = addAffiliationToResult(affiliation, result, jid);
                    }
                }
            } else {
                reply.setError(PacketError.Condition.bad_request);
            }
        }
    } else {
        // The client is modifying the list of moderators/members/participants/outcasts
        String nick;
        String target;
        boolean hasAffiliation;
        // Keep a registry of the updated presences
        List<Presence> presences = new ArrayList<>(itemsList.size());
        // Collect the new affiliations or roles for the specified jids
        for (Object anItem : itemsList) {
            try {
                item = (Element) anItem;
                affiliation = item.attributeValue("affiliation");
                hasAffiliation = affiliation != null;
                target = (hasAffiliation ? affiliation : item.attributeValue("role"));
                List<JID> jids = new ArrayList<>();
                // jid could be of the form "full JID" or "bare JID" depending if we are
                // going to change a role or an affiliation
                nick = item.attributeValue("nick");
                if (hasJID) {
                    // could be a group JID
                    jids.add(GroupJID.fromString(item.attributeValue("jid")));
                } else {
                    // Get the JID based on the requested nick
                    for (MUCRole role : room.getOccupantsByNickname(nick)) {
                        if (!jids.contains(role.getUserAddress())) {
                            jids.add(role.getUserAddress());
                        }
                    }
                }
                for (JID jid : jids) {
                    if ("moderator".equals(target)) {
                        // Add the user as a moderator of the room based on the full JID
                        presences.add(room.addModerator(jid, senderRole));
                    } else if ("owner".equals(target)) {
                        presences.addAll(room.addOwner(jid, senderRole));
                    } else if ("admin".equals(target)) {
                        presences.addAll(room.addAdmin(jid, senderRole));
                    } else if ("participant".equals(target)) {
                        // Add the user as a participant of the room based on the full JID
                        presences.add(room.addParticipant(jid, item.elementTextTrim("reason"), senderRole));
                    } else if ("visitor".equals(target)) {
                        // Add the user as a visitor of the room based on the full JID
                        presences.add(room.addVisitor(jid, senderRole));
                    } else if ("member".equals(target)) {
                        // Add the user as a member of the room based on the bare JID
                        boolean hadAffiliation = room.getAffiliation(jid) != MUCRole.Affiliation.none;
                        presences.addAll(room.addMember(jid, nick, senderRole));
                        // are not disabled system-wide xmpp.muc.skipInvite
                        if (!skipInvite && !hadAffiliation && room.isMembersOnly()) {
                            List<JID> invitees = new ArrayList<>();
                            if (GroupJID.isGroup(jid)) {
                                try {
                                    Group group = GroupManager.getInstance().getGroup(jid);
                                    for (JID inGroup : group.getAll()) {
                                        invitees.add(inGroup);
                                    }
                                } catch (GroupNotFoundException gnfe) {
                                    logger.error("Failed to send invitations for group members", gnfe);
                                }
                            } else {
                                invitees.add(jid);
                            }
                            for (JID invitee : invitees) {
                                room.sendInvitation(invitee, null, senderRole, null);
                            }
                        }
                    } else if ("outcast".equals(target)) {
                        // Add the user as an outcast of the room based on the bare JID
                        presences.addAll(room.addOutcast(jid, item.elementTextTrim("reason"), senderRole));
                    } else if ("none".equals(target)) {
                        if (hasAffiliation) {
                            // Set that this jid has a NONE affiliation based on the bare JID
                            presences.addAll(room.addNone(jid, senderRole));
                        } else {
                            // Kick the user from the room
                            if (MUCRole.Role.moderator != senderRole.getRole()) {
                                throw new ForbiddenException();
                            }
                            presences.add(room.kickOccupant(jid, senderRole.getUserAddress(), senderRole.getNickname(), item.elementTextTrim("reason")));
                        }
                    } else {
                        reply.setError(PacketError.Condition.bad_request);
                    }
                }
            } catch (UserNotFoundException e) {
            // Do nothing
            }
        }
        // Send the updated presences to the room occupants
        for (Presence presence : presences) {
            room.send(presence);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Group(org.jivesoftware.openfire.group.Group) ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) MUCRole(org.jivesoftware.openfire.muc.MUCRole) Presence(org.xmpp.packet.Presence) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 42 with Presence

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

the class ChangeNickname method readExternal.

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    super.readExternal(in);
    Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
    presence = new Presence(packetElement, true);
    oldNick = ExternalizableUtil.getInstance().readSafeUTF(in);
    newNick = ExternalizableUtil.getInstance().readSafeUTF(in);
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence)

Example 43 with Presence

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

the class OccupantAddedEvent method readExternal.

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    super.readExternal(in);
    Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
    presence = new Presence(packetElement, true);
    role = ExternalizableUtil.getInstance().readInt(in);
    affiliation = ExternalizableUtil.getInstance().readInt(in);
    voiceOnly = ExternalizableUtil.getInstance().readBoolean(in);
    roleAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
    userAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
    nodeID = NodeID.getInstance(ExternalizableUtil.getInstance().readByteArray(in));
    sendPresence = ExternalizableUtil.getInstance().readBoolean(in);
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence)

Example 44 with Presence

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

the class UpdateOccupant method readExternal.

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    super.readExternal(in);
    Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
    presence = new Presence(packetElement, true);
    nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
    role = ExternalizableUtil.getInstance().readInt(in);
    affiliation = ExternalizableUtil.getInstance().readInt(in);
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence)

Example 45 with Presence

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

the class UpdatePresence method readExternal.

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    super.readExternal(in);
    Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
    presence = new Presence(packetElement, true);
    nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence)

Aggregations

Presence (org.xmpp.packet.Presence)109 JID (org.xmpp.packet.JID)38 Element (org.dom4j.Element)34 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)20 Message (org.xmpp.packet.Message)17 IQ (org.xmpp.packet.IQ)16 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)14 NotFoundException (org.jivesoftware.util.NotFoundException)12 ArrayList (java.util.ArrayList)11 MUCRole (org.jivesoftware.openfire.muc.MUCRole)10 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)9 DefaultElement (org.dom4j.tree.DefaultElement)8 IOException (java.io.IOException)7 SQLException (java.sql.SQLException)7 GroupJID (org.jivesoftware.openfire.group.GroupJID)7 NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)7 Date (java.util.Date)6 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)6 ConflictException (org.jivesoftware.openfire.muc.ConflictException)6 ForbiddenException (org.jivesoftware.openfire.muc.ForbiddenException)6