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

The following examples show how to use org.xmpp.packet.Presence#getTo() . 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 File: PresenceManagerImpl.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public void userAvailable(Presence presence) {
    // Delete the last unavailable presence of this user since the user is now
    // available. Only perform this operation if this is an available presence sent to
    // THE SERVER and the presence belongs to a local user.
    if (presence.getTo() == null && server.isLocal(presence.getFrom())) {
        String username = presence.getFrom().getNode();
        if (username == null || !userManager.isRegisteredUser(username)) {
            // Ignore anonymous users
            return;
        }

        // Optimization: only delete the unavailable presence information if this
        // is the first session created on the server.
        if (sessionManager.getSessionCount(username) > 1) {
            return;
        }

        deleteOfflinePresenceFromDB(username);

        // Remove data from cache.
        offlinePresenceCache.remove(username);
        lastActivityCache.remove(username);
    }
}
 
Example 2
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Broadcasts the specified presence to all room occupants. If the presence belongs to a
 * user whose role cannot be broadcast then the presence will only be sent to the presence's
 * user. On the other hand, the JID of the user that sent the presence won't be included if the
 * room is semi-anon and the target occupant is not a moderator.
 *
 * @param presence the presence to broadcast.
 * @param isJoinPresence If the presence is sent in the context of joining the room.
 * @param sender The role of the entity that initiated the presence broadcast
 */
private void broadcastPresence(Presence presence, boolean isJoinPresence, @Nonnull MUCRole sender) {
    if (presence == null) {
        return;
    }

    // Some clients send a presence update to the room, rather than to their own nickname.
    if ( JiveGlobals.getBooleanProperty("xmpp.muc.presence.overwrite-to-room", true) && presence.getTo() != null && presence.getTo().getResource() == null && sender.getRoleAddress() != null) {
        presence.setTo( sender.getRoleAddress() );
    }

    if (!shouldBroadcastPresence(presence)) {
        // Just send the presence to the sender of the presence
        sender.send(presence);
        return;
    }

    // If FMUC is active, propagate the presence through FMUC first. Note that when a master-slave mode is active,
    // we need to wait for an echo back, before the message can be broadcasted locally. The 'propagate' method will
    // return a CompletableFuture object that is completed as soon as processing can continue.
    fmucHandler.propagate( presence, sender )
        .thenRunAsync( () -> {
            // Broadcast presence to occupants hosted by other cluster nodes
            BroadcastPresenceRequest request = new BroadcastPresenceRequest(this, sender, presence, isJoinPresence);
            CacheFactory.doClusterTask(request);

            // Broadcast presence to occupants connected to this JVM
            request = new BroadcastPresenceRequest(this, sender, presence, isJoinPresence);
            request.setOriginator(true);
            request.run();
        }
    );
}
 
Example 3
Source File: PresenceManagerImpl.java    From Openfire with Apache License 2.0 4 votes vote down vote up
@Override
public void userUnavailable(Presence presence) {
    // Only save the last presence status and keep track of the time when the user went
    // offline if this is an unavailable presence sent to THE SERVER and the presence belongs
    // to a local user.
    if (presence.getTo() == null && server.isLocal(presence.getFrom())) {
        String username = presence.getFrom().getNode();
        if (username == null || !userManager.isRegisteredUser(username)) {
            // Ignore anonymous users
            return;
        }

        // If the user has any remaining sessions, don't record the offline info.
        if (sessionManager.getActiveSessionCount(username) > 0) {
            return;
        }

        String offlinePresence = null;
        // Save the last unavailable presence of this user if the presence contains any
        // child element such as <status>.
        if (!presence.getElement().elements().isEmpty()) {
            offlinePresence = presence.toXML();
        }
        // Keep track of the time when the user went offline
        java.util.Date offlinePresenceDate = new java.util.Date();

        boolean addedToCache;
        if (offlinePresence == null) {
            addedToCache = !NULL_STRING.equals(offlinePresenceCache.put(username, NULL_STRING));
        }
        else {
            addedToCache = !offlinePresence.equals(offlinePresenceCache.put(username, offlinePresence));
        }
        if (!addedToCache) {
            return;
        }
        Log.debug( "Recording 'last activity' for user '{}'.", username);
        lastActivityCache.put(username, offlinePresenceDate.getTime());

        writeToDatabase(username, offlinePresence, offlinePresenceDate);
    }
}