Search in sources :

Example 16 with MUCRole

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

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

the class IQAdminHandler method addAffiliationToResult.

private Element addAffiliationToResult(String affiliation, Element parent, JID jid) {
    Element result = parent.addElement("item", "http://jabber.org/protocol/muc#admin");
    result.addAttribute("affiliation", affiliation);
    result.addAttribute("jid", jid.toString());
    try {
        List<MUCRole> roles = room.getOccupantsByBareJID(jid);
        MUCRole role = roles.get(0);
        result.addAttribute("role", role.getRole().toString());
        result.addAttribute("nick", role.getNickname());
    } catch (UserNotFoundException e) {
    // the JID is note currently an occupant
    }
    return result;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) MUCRole(org.jivesoftware.openfire.muc.MUCRole) Element(org.dom4j.Element)

Example 18 with MUCRole

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

the class GetNewMemberRoomsRequest method run.

@Override
public void run() {
    rooms = new ArrayList<>();
    // Get all services that have local occupants and include them in the reply
    for (MultiUserChatService mucService : XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatServices()) {
        // Get rooms that have local occupants and include them in the reply
        for (MUCRoom room : mucService.getChatRooms()) {
            LocalMUCRoom localRoom = (LocalMUCRoom) room;
            Collection<MUCRole> localOccupants = new ArrayList<>();
            for (MUCRole occupant : room.getOccupants()) {
                if (occupant.isLocal()) {
                    localOccupants.add(occupant);
                }
            }
            if (!localOccupants.isEmpty()) {
                rooms.add(new RoomInfo(localRoom, localOccupants));
            }
        }
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) MUCRoom(org.jivesoftware.openfire.muc.MUCRoom) LocalMUCRoom(org.jivesoftware.openfire.muc.spi.LocalMUCRoom) LocalMUCRoom(org.jivesoftware.openfire.muc.spi.LocalMUCRoom) ArrayList(java.util.ArrayList) MultiUserChatService(org.jivesoftware.openfire.muc.MultiUserChatService)

Example 19 with MUCRole

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

the class MultiUserChatServiceImpl method getChatRoom.

@Override
public MUCRoom getChatRoom(String roomName, JID userjid) throws NotAllowedException {
    LocalMUCRoom room;
    boolean loaded = false;
    boolean created = false;
    synchronized (roomName.intern()) {
        room = localMUCRoomManager.getRoom(roomName);
        if (room == null) {
            room = new LocalMUCRoom(this, roomName, router);
            // If the room is persistent load the configuration values from the DB
            try {
                // Try to load the room's configuration from the database (if the room is
                // persistent but was added to the DB after the server was started up or the
                // room may be an old room that was not present in memory)
                MUCPersistenceManager.loadFromDB(room);
                loaded = true;
            } catch (IllegalArgumentException e) {
                // (or was deleted somehow and is expected to exist by a delegate).
                if (mucEventDelegate != null && mucEventDelegate.shouldRecreate(roomName, userjid)) {
                    if (mucEventDelegate.loadConfig(room)) {
                        loaded = true;
                        if (room.isPersistent()) {
                            MUCPersistenceManager.saveToDB(room);
                        }
                    } else {
                        // not allow room creation
                        throw new NotAllowedException();
                    }
                } else {
                    // The room does not exist so check for creation permissions
                    // Room creation is always allowed for sysadmin
                    final JID bareJID = userjid.asBareJID();
                    if (isRoomCreationRestricted() && !sysadmins.includes(bareJID)) {
                        // The room creation is only allowed for certain JIDs
                        if (!allowedToCreate.includes(bareJID)) {
                            // an exception
                            throw new NotAllowedException();
                        }
                    }
                    room.addFirstOwner(userjid);
                    created = true;
                }
            }
            localMUCRoomManager.addRoom(roomName, room);
        }
    }
    if (created) {
        // Fire event that a new room has been created
        MUCEventDispatcher.roomCreated(room.getRole().getRoleAddress());
    }
    if (loaded || created) {
        // Notify other cluster nodes that a new room is available
        CacheFactory.doClusterTask(new RoomAvailableEvent(room));
        for (MUCRole role : room.getOccupants()) {
            if (role instanceof LocalMUCRole) {
                CacheFactory.doClusterTask(new OccupantAddedEvent(room, role));
            }
        }
    }
    return room;
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) GroupJID(org.jivesoftware.openfire.group.GroupJID) RoomAvailableEvent(org.jivesoftware.openfire.muc.cluster.RoomAvailableEvent) OccupantAddedEvent(org.jivesoftware.openfire.muc.cluster.OccupantAddedEvent)

Example 20 with MUCRole

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

the class LocalMUCRoom method removeOccupantRole.

/**
     * Removes the role of the occupant from all the internal occupants collections. The role will
     * also be removed from the user's roles.
     *
     * @param leaveRole the role to remove.
     * @param originator true if this JVM is the one that originated the event.
     */
private void removeOccupantRole(MUCRole leaveRole, boolean originator) {
    JID userAddress = leaveRole.getUserAddress();
    // Notify the user that he/she is no longer in the room
    leaveRole.destroy();
    // Update the tables of occupants based on the bare and full JID
    JID bareJID = userAddress.asBareJID();
    String nickname = leaveRole.getNickname();
    List<MUCRole> occupants = occupantsByNickname.get(nickname.toLowerCase());
    if (occupants != null) {
        occupants.remove(leaveRole);
        if (occupants.isEmpty()) {
            occupantsByNickname.remove(nickname.toLowerCase());
        }
    }
    List<MUCRole> list = occupantsByBareJID.get(bareJID);
    if (list != null) {
        list.remove(leaveRole);
        if (list.isEmpty()) {
            occupantsByBareJID.remove(bareJID);
        }
    }
    occupantsByFullJID.remove(userAddress);
    if (originator) {
        // Fire event that occupant left the room
        MUCEventDispatcher.occupantLeft(getRole().getRoleAddress(), userAddress);
    }
}
Also used : MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID)

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