Java Code Examples for org.xmpp.packet.Presence#addChildElement()

The following examples show how to use org.xmpp.packet.Presence#addChildElement() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Openfire   File: IQMUCvCardHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void sendVCardUpdateNotification( final MUCRoom room, String hash )
{
    Log.debug("Sending vcard-temp update notification to all occupants of room {}, using hash {}", room.getName(), hash);
    final Presence notification = new Presence();
    notification.setFrom(room.getJID());
    final Element x = notification.addChildElement("x", "vcard-temp:x:update");
    final Element photo = x.addElement("photo");
    photo.setText(hash);

    for ( final MUCRole occupant : room.getOccupants() )
    {
        occupant.send(notification);
    }
}
 
Example 2
Source Project: Openfire   File: LocalMUCRoom.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sends presence of a leaving occupant to other occupants.
 *
 * @param leaveRole the role of the occupant that is leaving.
 */
void sendLeavePresenceToExistingOccupants(MUCRole leaveRole) {
    // Send the presence of this new occupant to existing occupants
    Log.trace( "Send presence of leaving occupant '{}' to existing occupants of room '{}'.", leaveRole.getUserAddress(), leaveRole.getChatRoom().getJID() );
    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");

        // Check to see if the user's original presence is one we should broadcast
        // a leave packet for. Need to check the original presence because we just
        // 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
                if (JOIN_PRESENCE_ENABLE.getValue()) {
                    broadcastPresence(presence, false, leaveRole);
                }
            }
        }
    }
    catch (Exception e) {
        Log.error( "An exception occurred while sending leave presence of occupant '"+leaveRole.getUserAddress()+"' to the other occupants of room: '"+leaveRole.getChatRoom().getJID()+"'.", e);
    }
}
 
Example 3
Source Project: Openfire   File: LocalMUCRoom.java    License: Apache License 2.0 4 votes vote down vote up
public void destroyRoom(DestroyRoomRequest destroyRequest) {
    JID alternateJID = destroyRequest.getAlternateJID();
    String reason = destroyRequest.getReason();
    Collection<MUCRole> removedRoles = new CopyOnWriteArrayList<>();
    lock.writeLock().lock();
    try {
        boolean hasRemoteOccupants = false;
        // Remove each occupant
        for (MUCRole leaveRole : occupantsByFullJID.values()) {

            if (leaveRole != null) {
                // Add the removed occupant to the list of removed occupants. We are keeping a
                // list of removed occupants to process later outside of the lock.
                if (leaveRole.isLocal()) {
                    removedRoles.add(leaveRole);
                }
                else {
                    hasRemoteOccupants = true;
                }
                removeOccupantRole(leaveRole);

                if (  destroyRequest.isOriginator() ) {
                    // Fire event that occupant left the room
                    MUCEventDispatcher.occupantLeft(leaveRole.getRoleAddress(), leaveRole.getUserAddress(), leaveRole.getNickname());
                }
            }
        }
        endTime = System.currentTimeMillis();
        // Set that the room has been destroyed
        isDestroyed = true;
        if (destroyRequest.isOriginator()) {
            if (hasRemoteOccupants) {
                // Ask other cluster nodes to remove occupants since room is being destroyed
                CacheFactory.doClusterTask(new DestroyRoomRequest(this, alternateJID, reason));
            }
            // Removes the room from the list of rooms hosted in the service
            mucService.removeChatRoom(name);
        }
    }
    finally {
        lock.writeLock().unlock();
    }
    // Send an unavailable presence to each removed occupant
    for (MUCRole removedRole : removedRoles) {
        try {
            // Send a presence stanza of type "unavailable" to the occupant
            Presence presence = createPresence(Presence.Type.unavailable);
            presence.setFrom(removedRole.getRoleAddress());

            // A fragment containing the x-extension for room destruction.
            Element fragment = presence.addChildElement("x",
                    "http://jabber.org/protocol/muc#user");
            Element item = fragment.addElement("item");
            item.addAttribute("affiliation", "none");
            item.addAttribute("role", "none");
            if (alternateJID != null) {
                fragment.addElement("destroy").addAttribute("jid", alternateJID.toString());
            }
            if (reason != null && reason.length() > 0) {
                Element destroy = fragment.element("destroy");
                if (destroy == null) {
                    destroy = fragment.addElement("destroy");
                }
                destroy.addElement("reason").setText(reason);
            }
            removedRole.send(presence);
        }
        catch (Exception e) {
            Log.error(e.getMessage(), e);
        }
    }
    if (destroyRequest.isOriginator()) {
        // Remove the room from the DB if the room was persistent
        MUCPersistenceManager.deleteFromDB(this);
        // Fire event that the room has been destroyed
        MUCEventDispatcher.roomDestroyed(getRole().getRoleAddress());
    }
}