Search in sources :

Example 1 with OccupantLeftEvent

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

the class LocalMUCRoom method leaveRoom.

@Override
public void leaveRoom(MUCRole leaveRole) {
    if (leaveRole.isLocal()) {
        // Ask other cluster nodes to remove occupant from room
        OccupantLeftEvent event = new OccupantLeftEvent(this, leaveRole);
        CacheFactory.doClusterTask(event);
    }
    try {
        Presence originalPresence = leaveRole.getPresence();
        Presence presence = originalPresence.createCopy();
        presence.setType(Presence.Type.unavailable);
        presence.setStatus(null);
        // Change (or add) presence information about roles and affiliations
        Element childElement = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
        if (childElement == null) {
            childElement = presence.addChildElement("x", "http://jabber.org/protocol/muc#user");
        }
        Element item = childElement.element("item");
        if (item == null) {
            item = childElement.addElement("item");
        }
        item.addAttribute("role", "none");
        // set the role to "none" above, which is always broadcast.
        if (!shouldBroadcastPresence(originalPresence)) {
            // Inform the leaving user that he/she has left the room
            leaveRole.send(presence);
        } else {
            if (getOccupantsByNickname(leaveRole.getNickname()).size() <= 1) {
                // Inform the rest of the room occupants that the user has left the room
                broadcastPresence(presence, false);
            }
        }
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
    // Remove occupant from room and destroy room if empty and not persistent
    OccupantLeftEvent event = new OccupantLeftEvent(this, leaveRole);
    event.setOriginator(true);
    event.run();
}
Also used : OccupantLeftEvent(org.jivesoftware.openfire.muc.cluster.OccupantLeftEvent) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence) 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)

Example 2 with OccupantLeftEvent

use of org.jivesoftware.openfire.muc.cluster.OccupantLeftEvent 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

Element (org.dom4j.Element)2 OccupantLeftEvent (org.jivesoftware.openfire.muc.cluster.OccupantLeftEvent)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)1 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)1 CannotBeInvitedException (org.jivesoftware.openfire.muc.CannotBeInvitedException)1 ConflictException (org.jivesoftware.openfire.muc.ConflictException)1 ForbiddenException (org.jivesoftware.openfire.muc.ForbiddenException)1 MUCRole (org.jivesoftware.openfire.muc.MUCRole)1 NotAcceptableException (org.jivesoftware.openfire.muc.NotAcceptableException)1 NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)1 RegistrationRequiredException (org.jivesoftware.openfire.muc.RegistrationRequiredException)1 RoomLockedException (org.jivesoftware.openfire.muc.RoomLockedException)1 ServiceUnavailableException (org.jivesoftware.openfire.muc.ServiceUnavailableException)1 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)1 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)1 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1 NotFoundException (org.jivesoftware.util.NotFoundException)1 Presence (org.xmpp.packet.Presence)1