Search in sources :

Example 31 with Presence

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

the class PresenceSubscribeHandler method process.

@Override
public void process(Presence presence) throws PacketException {
    if (presence == null) {
        throw new IllegalArgumentException("Argument 'presence' cannot be null.");
    }
    final Presence.Type type = presence.getType();
    if (type != Presence.Type.subscribe && type != Presence.Type.unsubscribe && type != Presence.Type.subscribed && type != Presence.Type.unsubscribed) {
        throw new IllegalArgumentException("Packet processed by PresenceSubscribeHandler is " + "not of a subscription-related type, but: " + type);
    }
    // RFC-6121 paragraph 3: "When a server processes or generates an outbound presence stanza
    // of type "subscribe", "subscribed", "unsubscribe", or "unsubscribed", the server MUST stamp
    // the outgoing presence stanza with the bare JID <localpart@domainpart> of the sending entity,
    // not the full JID <localpart@domainpart/resourcepart>."
    presence.setFrom(presence.getFrom().toBareJID());
    // JID and modify the 'to' address accordingly.
    if (presence.getTo() != null) {
        presence.setTo(presence.getTo().toBareJID());
    }
    final JID senderJID = presence.getFrom();
    final JID recipientJID = presence.getTo();
    try {
        // Reject presence subscription requests sent to the local server itself.
        if (recipientJID == null || recipientJID.toString().equals(serverName)) {
            if (type == Presence.Type.subscribe) {
                Presence reply = new Presence();
                reply.setTo(senderJID);
                reply.setFrom(recipientJID);
                reply.setType(Presence.Type.unsubscribed);
                deliverer.deliver(reply);
            }
            return;
        }
        try {
            Roster senderRoster = getRoster(senderJID);
            if (senderRoster != null) {
                manageSub(recipientJID, true, type, senderRoster);
            }
            Roster recipientRoster = getRoster(recipientJID);
            boolean recipientSubChanged = false;
            if (recipientRoster != null) {
                recipientSubChanged = manageSub(senderJID, false, type, recipientRoster);
            }
            // and the recipient user has not changed its subscription state.
            if (!(type == Presence.Type.subscribed && recipientRoster != null && !recipientSubChanged)) {
                // See http://tools.ietf.org/html/rfc3921#section-7 and/or OF-38 
                if (type == Presence.Type.subscribe && recipientRoster != null && !recipientSubChanged) {
                    try {
                        RosterItem.SubType subType = recipientRoster.getRosterItem(senderJID).getSubStatus();
                        if (subType == RosterItem.SUB_FROM || subType == RosterItem.SUB_BOTH) {
                            return;
                        }
                    } catch (UserNotFoundException e) {
                        // Weird case: Roster item does not exist. Should never happen
                        Log.error("User does not exist while trying to update roster item. " + "This should never happen (this indicates a programming " + "logic error). Processing stanza: " + presence.toString(), e);
                    }
                }
                // Try to obtain a handler for the packet based on the routes. If the handler is
                // a module, the module will be able to handle the packet. If the handler is a
                // Session the packet will be routed to the client. If a route cannot be found
                // then the packet will be delivered based on its recipient and sender.
                List<JID> jids = routingTable.getRoutes(recipientJID, null);
                if (!jids.isEmpty()) {
                    for (JID jid : jids) {
                        Presence presenteToSend = presence.createCopy();
                        // Stamp the presence with the user's bare JID as the 'from' address,
                        // as required by section 8.2.5 of RFC 3921
                        presenteToSend.setFrom(senderJID.toBareJID());
                        routingTable.routePacket(jid, presenteToSend, false);
                    }
                } else {
                    deliverer.deliver(presence.createCopy());
                }
                if (type == Presence.Type.subscribed) {
                    // Send the presence of the local user to the remote user. The remote user
                    // subscribed to the presence of the local user and the local user accepted
                    JID prober = localServer.isLocal(recipientJID) ? recipientJID.asBareJID() : recipientJID;
                    if (presenceManager.canProbePresence(prober, senderJID.getNode())) {
                        presenceManager.probePresence(prober, senderJID);
                        PresenceEventDispatcher.subscribedToPresence(recipientJID, senderJID);
                    } else {
                        Presence nonProbablePresence = new Presence();
                        nonProbablePresence.setStatus("unavailable");
                        nonProbablePresence.setFrom(senderJID);
                        nonProbablePresence.setTo(recipientJID);
                        presenceManager.handleProbe(nonProbablePresence);
                    }
                }
            }
            if (type == Presence.Type.unsubscribed) {
                // Send unavailable presence from all of the local user's available resources
                // to the remote user
                presenceManager.sendUnavailableFromSessions(recipientJID, senderJID);
                PresenceEventDispatcher.unsubscribedToPresence(senderJID, recipientJID);
            }
        } catch (SharedGroupException e) {
            Presence result = presence.createCopy();
            JID sender = result.getFrom();
            result.setFrom(presence.getTo());
            result.setTo(sender);
            result.setError(PacketError.Condition.not_acceptable);
            deliverer.deliver(result);
        }
    } catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) JID(org.xmpp.packet.JID) Roster(org.jivesoftware.openfire.roster.Roster) Presence(org.xmpp.packet.Presence) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) PacketException(org.jivesoftware.openfire.PacketException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 32 with Presence

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

the class PresenceUpdateHandler method broadcastUnavailableForDirectedPresences.

/**
     * Sends an unavailable presence to the entities that received a directed (available) presence
     * by the user that is now going offline.
     *
     * @param update the unavailable presence sent by the user.
     */
private void broadcastUnavailableForDirectedPresences(Presence update) {
    JID from = update.getFrom();
    if (from == null) {
        return;
    }
    if (localServer.isLocal(from)) {
        // Remove the registry of directed presences of this user
        Collection<DirectedPresence> directedPresences = null;
        Lock lock = CacheFactory.getLock(from.toString(), directedPresencesCache);
        try {
            lock.lock();
            directedPresences = directedPresencesCache.remove(from.toString());
        } finally {
            lock.unlock();
        }
        if (directedPresences != null) {
            // Iterate over all the entities that the user sent a directed presence
            for (DirectedPresence directedPresence : directedPresences) {
                for (String receiver : directedPresence.getReceivers()) {
                    Presence presence = update.createCopy();
                    presence.setTo(receiver);
                    localServer.getPresenceRouter().route(presence);
                }
            }
            localDirectedPresences.remove(from.toString());
        }
    }
}
Also used : JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) Lock(java.util.concurrent.locks.Lock)

Example 33 with Presence

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

the class PubSubEngine method probePresences.

private void probePresences(final PubSubService service) {
    Set<JID> affiliates = new HashSet<>();
    for (Node node : service.getNodes()) {
        affiliates.addAll(node.getPresenceBasedSubscribers());
    }
    for (JID jid : affiliates) {
        // Send probe presence
        Presence subscription = new Presence(Presence.Type.probe);
        subscription.setTo(jid);
        subscription.setFrom(service.getAddress());
        service.send(subscription);
    }
}
Also used : JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) HashSet(java.util.HashSet)

Example 34 with Presence

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

the class ClusterListener method cleanupDirectedPresences.

private void cleanupDirectedPresences(NodeID nodeID) {
    // Remove traces of directed presences sent from node that is gone to entities hosted in this JVM
    Map<String, Collection<String>> senders = nodePresences.remove(nodeID);
    if (senders != null) {
        for (Map.Entry<String, Collection<String>> entry : senders.entrySet()) {
            String sender = entry.getKey();
            Collection<String> receivers = entry.getValue();
            for (String receiver : receivers) {
                try {
                    Presence presence = new Presence(Presence.Type.unavailable);
                    presence.setFrom(sender);
                    presence.setTo(receiver);
                    XMPPServer.getInstance().getPresenceRouter().route(presence);
                } catch (PacketException e) {
                    Log.error(e);
                }
            }
        }
    }
}
Also used : Presence(org.xmpp.packet.Presence) DirectedPresence(org.jivesoftware.openfire.handler.DirectedPresence) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 35 with Presence

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

the class BroadcastPlugin method processPacket.

public void processPacket(Packet packet) {
    boolean canProceed = false;
    Group group = null;
    String toNode = packet.getTo().getNode();
    // Check if user is allowed to send packet to this service[+group]
    boolean targetAll = "all".equals(toNode);
    if (targetAll) {
        // See if the user is allowed to send the packet.
        JID address = new JID(packet.getFrom().toBareJID());
        if (allowedUsers.isEmpty() || allowedUsers.contains(address)) {
            canProceed = true;
        }
    } else {
        try {
            if (toNode != null) {
                group = groupManager.getGroup(toNode);
                boolean isGroupUser = group.isUser(packet.getFrom()) || group.isUser(new JID(packet.getFrom().toBareJID()));
                if (disableGroupPermissions || (groupMembersAllowed && isGroupUser) || allowedUsers.contains(new JID(packet.getFrom().toBareJID()))) {
                    canProceed = true;
                }
            }
        } catch (GroupNotFoundException e) {
        // Ignore.
        }
    }
    if (packet instanceof Message) {
        // Respond to incoming messages
        Message message = (Message) packet;
        processMessage(message, targetAll, group, canProceed);
    } else if (packet instanceof Presence) {
        // Respond to presence subscription request or presence probe
        Presence presence = (Presence) packet;
        processPresence(canProceed, presence);
    } else if (packet instanceof IQ) {
        // Handle disco packets
        IQ iq = (IQ) packet;
        // Ignore IQs of type ERROR or RESULT
        if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) {
            return;
        }
        processIQ(iq, targetAll, group, canProceed);
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

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