Search in sources :

Example 71 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class LocalClientSession method setPresence.

/**
     * Set the presence of this session
     *
     * @param presence The presence for the session
     */
@Override
public void setPresence(Presence presence) {
    Presence oldPresence = this.presence;
    this.presence = presence;
    if (oldPresence.isAvailable() && !this.presence.isAvailable()) {
        // The client is no longer available
        sessionManager.sessionUnavailable(this);
        // Mark that the session is no longer initialized. This means that if the user sends
        // an available presence again the session will be initialized again thus receiving
        // offline messages and offline presence subscription requests
        setInitialized(false);
        // Notify listeners that the session is no longer available
        PresenceEventDispatcher.unavailableSession(this, presence);
    } else if (!oldPresence.isAvailable() && this.presence.isAvailable()) {
        // The client is available
        sessionManager.sessionAvailable(this, presence);
        wasAvailable = true;
        // Notify listeners that the session is now available
        PresenceEventDispatcher.availableSession(this, presence);
    } else if (this.presence.isAvailable() && oldPresence.getPriority() != this.presence.getPriority()) {
        // The client has changed the priority of his presence
        sessionManager.changePriority(this, oldPresence.getPriority());
        // Notify listeners that the priority of the session/resource has changed
        PresenceEventDispatcher.presenceChanged(this, presence);
    } else if (this.presence.isAvailable()) {
        // Notify listeners that the show or status value of the presence has changed
        PresenceEventDispatcher.presenceChanged(this, presence);
    }
    if (ClusterManager.isClusteringStarted()) {
        // Track information about the session and share it with other cluster nodes
        Cache<String, ClientSessionInfo> cache = SessionManager.getInstance().getSessionInfoCache();
        cache.put(getAddress().toString(), new ClientSessionInfo(this));
    }
}
Also used : Presence(org.xmpp.packet.Presence)

Example 72 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class PresenceManagerImpl method probePresence.

@Override
public void probePresence(JID prober, JID probee) {
    try {
        if (server.isLocal(probee)) {
            // Local probers should receive presences of probee in all connected resources
            Collection<JID> proberFullJIDs = new ArrayList<>();
            if (prober.getResource() == null && server.isLocal(prober)) {
                for (ClientSession session : sessionManager.getSessions(prober.getNode())) {
                    proberFullJIDs.add(session.getAddress());
                }
            } else {
                proberFullJIDs.add(prober);
            }
            // If the probee is a local user then don't send a probe to the contact's server.
            // But instead just send the contact's presence to the prober
            Collection<ClientSession> sessions = sessionManager.getSessions(probee.getNode());
            if (sessions.isEmpty()) {
                // If the probee is not online then try to retrieve his last unavailable
                // presence which may contain particular information and send it to the
                // prober
                String presenceXML = offlinePresenceCache.get(probee.getNode());
                if (presenceXML == null) {
                    loadOfflinePresence(probee.getNode());
                }
                presenceXML = offlinePresenceCache.get(probee.getNode());
                if (presenceXML != null && !NULL_STRING.equals(presenceXML)) {
                    try {
                        // Parse the element
                        Document element = DocumentHelper.parseText(presenceXML);
                        // Create the presence from the parsed element
                        Presence presencePacket = new Presence(element.getRootElement());
                        presencePacket.setFrom(probee.toBareJID());
                        // Check if default privacy list of the probee blocks the
                        // outgoing presence
                        PrivacyList list = PrivacyListManager.getInstance().getDefaultPrivacyList(probee.getNode());
                        // Send presence to all prober's resources
                        for (JID receipient : proberFullJIDs) {
                            presencePacket.setTo(receipient);
                            if (list == null || !list.shouldBlockPacket(presencePacket)) {
                                // Send the presence to the prober
                                deliverer.deliver(presencePacket);
                            }
                        }
                    } catch (Exception e) {
                        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
                    }
                }
            } else {
                // probee is connected
                for (ClientSession session : sessions) {
                    // Create presence to send from probee to prober
                    Presence presencePacket = session.getPresence().createCopy();
                    presencePacket.setFrom(session.getAddress());
                    // Check if a privacy list of the probee blocks the outgoing presence
                    PrivacyList list = session.getActiveList();
                    list = list == null ? session.getDefaultList() : list;
                    // Send presence to all prober's resources
                    for (JID receipient : proberFullJIDs) {
                        presencePacket.setTo(receipient);
                        if (list != null) {
                            if (list.shouldBlockPacket(presencePacket)) {
                                // Default list blocked outgoing presence so skip this session
                                continue;
                            }
                        }
                        try {
                            deliverer.deliver(presencePacket);
                        } catch (Exception e) {
                            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
                        }
                    }
                }
            }
        } else {
            if (routingTable.hasComponentRoute(probee)) {
                // If the probee belongs to a component then ask the component to process the
                // probe presence
                Presence presence = new Presence();
                presence.setType(Presence.Type.probe);
                presence.setFrom(prober);
                presence.setTo(probee);
                routingTable.routePacket(probee, presence, true);
            } else {
                /*String serverDomain = server.getServerInfo().getName();
                    if (!probee.getDomain().contains(serverDomain)) {*/
                if (server.isRemote(probee)) {
                    // Send the probe presence to the remote server
                    Presence probePresence = new Presence();
                    probePresence.setType(Presence.Type.probe);
                    probePresence.setFrom(prober);
                    probePresence.setTo(probee.toBareJID());
                    // Send the probe presence
                    deliverer.deliver(probePresence);
                } else {
                    // The probee may be related to a component that has not yet been connected so
                    // we will keep a registry of this presence probe. The component will answer
                    // this presence probe when he becomes online
                    componentManager.addPresenceRequest(prober, probee);
                }
            }
        }
    } catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
}
Also used : JID(org.xmpp.packet.JID) ClientSession(org.jivesoftware.openfire.session.ClientSession) PrivacyList(org.jivesoftware.openfire.privacy.PrivacyList) Presence(org.xmpp.packet.Presence) Document(org.dom4j.Document) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) SQLException(java.sql.SQLException) DocumentException(org.dom4j.DocumentException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 73 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class PresenceManagerImpl method handleProbe.

@Override
public void handleProbe(Presence packet) throws UnauthorizedException {
    String username = packet.getTo().getNode();
    try {
        Roster roster = rosterManager.getRoster(username);
        RosterItem item = roster.getRosterItem(packet.getFrom());
        if (item.getSubStatus() == RosterItem.SUB_FROM || item.getSubStatus() == RosterItem.SUB_BOTH) {
            probePresence(packet.getFrom(), packet.getTo());
        } else {
            PacketError.Condition error = PacketError.Condition.not_authorized;
            if ((item.getSubStatus() == RosterItem.SUB_NONE && item.getRecvStatus() != RosterItem.RECV_SUBSCRIBE) || (item.getSubStatus() == RosterItem.SUB_TO && item.getRecvStatus() != RosterItem.RECV_SUBSCRIBE)) {
                error = PacketError.Condition.forbidden;
            }
            Presence presenceToSend = new Presence();
            presenceToSend.setError(error);
            presenceToSend.setTo(packet.getFrom());
            presenceToSend.setFrom(packet.getTo());
            deliverer.deliver(presenceToSend);
        }
    } catch (UserNotFoundException e) {
        Presence presenceToSend = new Presence();
        presenceToSend.setError(PacketError.Condition.forbidden);
        presenceToSend.setTo(packet.getFrom());
        presenceToSend.setFrom(packet.getTo());
        deliverer.deliver(presenceToSend);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) PacketError(org.xmpp.packet.PacketError) Presence(org.xmpp.packet.Presence)

Example 74 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class PresenceManagerImpl method sendUnavailableFromSessions.

@Override
public void sendUnavailableFromSessions(JID recipientJID, JID userJID) {
    if (XMPPServer.getInstance().isLocal(userJID) && userManager.isRegisteredUser(userJID.getNode())) {
        for (ClientSession session : sessionManager.getSessions(userJID.getNode())) {
            // Do not send an unavailable presence if the user sent a direct available presence
            if (presenceUpdateHandler.hasDirectPresence(session.getAddress(), recipientJID)) {
                continue;
            }
            Presence presencePacket = new Presence();
            presencePacket.setType(Presence.Type.unavailable);
            presencePacket.setFrom(session.getAddress());
            // Ensure that unavailable presence is sent to all receipient's resources
            Collection<JID> recipientFullJIDs = new ArrayList<>();
            if (server.isLocal(recipientJID)) {
                for (ClientSession targetSession : sessionManager.getSessions(recipientJID.getNode())) {
                    recipientFullJIDs.add(targetSession.getAddress());
                }
            } else {
                recipientFullJIDs.add(recipientJID);
            }
            for (JID jid : recipientFullJIDs) {
                presencePacket.setTo(jid);
                try {
                    deliverer.deliver(presencePacket);
                } catch (Exception e) {
                    Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
                }
            }
        }
    }
}
Also used : JID(org.xmpp.packet.JID) ClientSession(org.jivesoftware.openfire.session.ClientSession) Presence(org.xmpp.packet.Presence) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) SQLException(java.sql.SQLException) DocumentException(org.dom4j.DocumentException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 75 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class RosterManager method sendSubscribeRequest.

private void sendSubscribeRequest(JID sender, JID recipient, boolean isSubscribe) {
    Presence presence = new Presence();
    presence.setFrom(sender);
    presence.setTo(recipient);
    if (isSubscribe) {
        presence.setType(Presence.Type.subscribe);
    } else {
        presence.setType(Presence.Type.unsubscribe);
    }
    routingTable.routePacket(recipient, presence, false);
}
Also used : Presence(org.xmpp.packet.Presence)

Aggregations

Presence (org.xmpp.packet.Presence)109 JID (org.xmpp.packet.JID)38 Element (org.dom4j.Element)34 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)20 Message (org.xmpp.packet.Message)17 IQ (org.xmpp.packet.IQ)16 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)14 NotFoundException (org.jivesoftware.util.NotFoundException)12 ArrayList (java.util.ArrayList)11 MUCRole (org.jivesoftware.openfire.muc.MUCRole)10 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)9 DefaultElement (org.dom4j.tree.DefaultElement)8 IOException (java.io.IOException)7 SQLException (java.sql.SQLException)7 GroupJID (org.jivesoftware.openfire.group.GroupJID)7 NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)7 Date (java.util.Date)6 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)6 ConflictException (org.jivesoftware.openfire.muc.ConflictException)6 ForbiddenException (org.jivesoftware.openfire.muc.ForbiddenException)6