Search in sources :

Example 6 with NotAllowedException

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

the class MultiUserChatServiceImpl method checkForTimedOutUsers.

private void checkForTimedOutUsers() {
    final long deadline = System.currentTimeMillis() - user_idle;
    for (LocalMUCUser user : users.values()) {
        try {
            // the list of users
            if (!user.isJoined()) {
                removeUser(user.getAddress());
                continue;
            }
            // Do nothing if this feature is disabled (i.e USER_IDLE equals -1)
            if (user_idle == -1) {
                continue;
            }
            if (user.getLastPacketTime() < deadline) {
                // Kick the user from all the rooms that he/she had previuosly joined
                MUCRoom room;
                Presence kickedPresence;
                for (LocalMUCRole role : user.getRoles()) {
                    room = role.getChatRoom();
                    try {
                        kickedPresence = room.kickOccupant(user.getAddress(), null, null, null);
                        // Send the updated presence to the room occupants
                        room.send(kickedPresence);
                    } catch (NotAllowedException e) {
                    // Do nothing since we cannot kick owners or admins
                    }
                }
            }
        } catch (Throwable e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }
}
Also used : MUCRoom(org.jivesoftware.openfire.muc.MUCRoom) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException)

Example 7 with NotAllowedException

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

use of org.jivesoftware.openfire.muc.NotAllowedException 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)

Aggregations

NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)8 MUCRole (org.jivesoftware.openfire.muc.MUCRole)6 Element (org.dom4j.Element)4 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)4 Presence (org.xmpp.packet.Presence)4 ArrayList (java.util.ArrayList)3 GroupJID (org.jivesoftware.openfire.group.GroupJID)3 JID (org.xmpp.packet.JID)3 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)2 UpdateOccupant (org.jivesoftware.openfire.muc.cluster.UpdateOccupant)2 UpdateOccupantRequest (org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest)2 List (java.util.List)1 Group (org.jivesoftware.openfire.group.Group)1 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)1 MultiUserChatService (org.jivesoftware.openfire.muc.MultiUserChatService)1 OccupantAddedEvent (org.jivesoftware.openfire.muc.cluster.OccupantAddedEvent)1 RoomAvailableEvent (org.jivesoftware.openfire.muc.cluster.RoomAvailableEvent)1