Search in sources :

Example 36 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class IQMessageCarbonsHandler method handleIQ.

@Override
public IQ handleIQ(IQ packet) {
    Element enable = packet.getChildElement();
    if (XMPPServer.getInstance().isLocal(packet.getFrom())) {
        if (enable.getName().equals("enable")) {
            ClientSession clientSession = sessionManager.getSession(packet.getFrom());
            clientSession.setMessageCarbonsEnabled(true);
            return IQ.createResultIQ(packet);
        } else if (enable.getName().equals("disable")) {
            ClientSession clientSession = sessionManager.getSession(packet.getFrom());
            clientSession.setMessageCarbonsEnabled(false);
            return IQ.createResultIQ(packet);
        } else {
            IQ error = IQ.createResultIQ(packet);
            error.setError(PacketError.Condition.bad_request);
            return error;
        }
    } else {
        // if the request is from a client that is not hosted on this server.
        IQ error = IQ.createResultIQ(packet);
        error.setError(PacketError.Condition.not_allowed);
        return error;
    }
}
Also used : Element(org.dom4j.Element) ClientSession(org.jivesoftware.openfire.session.ClientSession) IQ(org.xmpp.packet.IQ)

Example 37 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class PEPService method sendNotification.

@Override
public void sendNotification(Node node, Message message, JID recipientJID) {
    message.setTo(recipientJID);
    message.setFrom(getAddress());
    message.setID(StringUtils.randomString(8));
    // If the recipient subscribed with a bare JID and this PEPService can retrieve
    // presence information for the recipient, collect all of their full JIDs and
    // send the notification to each below.
    Set<JID> recipientFullJIDs = new HashSet<>();
    if (XMPPServer.getInstance().isLocal(recipientJID)) {
        if (recipientJID.getResource() == null) {
            for (ClientSession clientSession : SessionManager.getInstance().getSessions(recipientJID.getNode())) {
                recipientFullJIDs.add(clientSession.getAddress());
            }
        }
    } else {
        // Since recipientJID is not local, try to get presence info from cached known remote
        // presences.
        // TODO: OF-605 the old code depends on a cache that would contain presence state on all (?!) JIDS on all (?!)
        // remote domains. As we cannot depend on this information to be correct (even if we could ensure that this
        // potentially unlimited amount of data would indeed be manageable in the first place), this code was removed.
        recipientFullJIDs.add(recipientJID);
    }
    if (recipientFullJIDs.isEmpty()) {
        router.route(message);
        return;
    }
    for (JID recipientFullJID : recipientFullJIDs) {
        // to the service owner.
        try {
            JID publisher = null;
            // Get the ID of the node that had an item published to or retracted from.
            Element itemsElement = message.getElement().element("event").element("items");
            String nodeID = itemsElement.attributeValue("node");
            // Get the ID of the item that was published or retracted.
            String itemID = null;
            Element itemElement = itemsElement.element("item");
            if (itemElement == null) {
                Element retractElement = itemsElement.element("retract");
                if (retractElement != null) {
                    itemID = retractElement.attributeValue("id");
                }
            } else {
                itemID = itemElement.attributeValue("id");
            }
            // Check if the recipientFullJID is interested in notifications for this node.
            // If the recipient has not yet requested any notification filtering, continue and send
            // the notification.
            EntityCapabilities entityCaps = entityCapsManager.getEntityCapabilities(recipientFullJID);
            if (entityCaps != null) {
                if (!entityCaps.containsFeature(nodeID + "+notify")) {
                    return;
                }
            }
            // This full JID will be used as the "replyto" address in the addressing extension.
            if (node.isCollectionNode()) {
                for (Node leafNode : node.getNodes()) {
                    if (leafNode.getNodeID().equals(nodeID)) {
                        publisher = leafNode.getPublishedItem(itemID).getPublisher();
                        // Ensure the recipientJID has access to receive notifications for items published to the leaf node.
                        AccessModel accessModel = leafNode.getAccessModel();
                        if (!accessModel.canAccessItems(leafNode, recipientFullJID, publisher)) {
                            return;
                        }
                        break;
                    }
                }
            } else {
                publisher = node.getPublishedItem(itemID).getPublisher();
            }
            // Ensure the recipient is subscribed to the service owner's (publisher's) presence.
            if (canProbePresence(publisher, recipientFullJID)) {
                Element addresses = DocumentHelper.createElement(QName.get("addresses", "http://jabber.org/protocol/address"));
                Element address = addresses.addElement("address");
                address.addAttribute("type", "replyto");
                address.addAttribute("jid", publisher.toString());
                Message extendedMessage = message.createCopy();
                extendedMessage.addExtension(new PacketExtension(addresses));
                extendedMessage.setTo(recipientFullJID);
                router.route(extendedMessage);
            }
        } catch (IndexOutOfBoundsException e) {
        // Do not add addressing extension to message.
        } catch (UserNotFoundException e) {
            // Do not add addressing extension to message.
            router.route(message);
        } catch (NullPointerException e) {
            try {
                if (canProbePresence(getAddress(), recipientFullJID)) {
                    message.setTo(recipientFullJID);
                }
            } catch (UserNotFoundException e1) {
            // Do nothing
            }
            router.route(message);
        }
    }
}
Also used : PacketExtension(org.xmpp.packet.PacketExtension) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) AccessModel(org.jivesoftware.openfire.pubsub.models.AccessModel) Element(org.dom4j.Element) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode) Node(org.jivesoftware.openfire.pubsub.Node) EntityCapabilities(org.jivesoftware.openfire.entitycaps.EntityCapabilities) ClientSession(org.jivesoftware.openfire.session.ClientSession) HashSet(java.util.HashSet)

Example 38 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession 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 39 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession 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 40 with ClientSession

use of org.jivesoftware.openfire.session.ClientSession in project Openfire by igniterealtime.

the class SparkManager method handleClientVersion.

/**
     * Handles the IQ version reply. If only a given list of clients are allowed to connect
     * then the reply will be analyzed. If the client is not present in the list, no name
     * was responsed or an IQ error was returned (e.g. IQ version not supported) then
     * the client session will be terminated.
     *
     * @param iq the IQ version reply sent by the client.
     */
private void handleClientVersion(IQ iq) {
    final String clientsAllowed = JiveGlobals.getProperty("clients.allowed", "all");
    final boolean disconnectIfNoMatch = !"all".equals(clientsAllowed);
    if ("all".equals(clientsAllowed) || !disconnectIfNoMatch) {
        // There is nothing to do here. Just return.
        return;
    }
    // Get the client session of the user that sent the IQ version response
    ClientSession session = sessionManager.getSession(iq.getFrom());
    if (session == null) {
        // Do nothing if the session no longer exists
        return;
    }
    if (IQ.Type.result == iq.getType()) {
        // Get list of allowed clients to connect
        final List<String> clients = new ArrayList<String>();
        StringTokenizer clientTokens = new StringTokenizer(clientsAllowed, ",");
        while (clientTokens.hasMoreTokens()) {
            clients.add(clientTokens.nextToken().toLowerCase());
        }
        final String otherClientsAllowed = JiveGlobals.getProperty("other.clients.allowed", "");
        clientTokens = new StringTokenizer(otherClientsAllowed, ",");
        while (clientTokens.hasMoreTokens()) {
            clients.add(clientTokens.nextToken().toLowerCase().trim());
        }
        Element child = iq.getChildElement();
        String clientName = child.elementTextTrim("name");
        boolean disconnect = true;
        if (clientName != null) {
            // Check if the client should be disconnected
            for (String c : clients) {
                if (clientName.toLowerCase().contains(c)) {
                    disconnect = false;
                    break;
                }
            }
        } else {
            // Always disconnect clients that didn't provide their name
            disconnect = true;
        }
        if (disconnect) {
            closeSession(session, clientName != null ? clientName : "Unknown");
        }
    } else {
        // If the session is invalid. Close the connection.
        closeSession(session, "Unknown");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ClientSession(org.jivesoftware.openfire.session.ClientSession) Element(org.dom4j.Element) ArrayList(java.util.ArrayList)

Aggregations

ClientSession (org.jivesoftware.openfire.session.ClientSession)49 JID (org.xmpp.packet.JID)15 Element (org.dom4j.Element)14 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)14 IQ (org.xmpp.packet.IQ)12 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)8 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)7 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)7 StreamError (org.xmpp.packet.StreamError)7 DataForm (org.xmpp.forms.DataForm)6 FormField (org.xmpp.forms.FormField)6 Message (org.xmpp.packet.Message)6 Presence (org.xmpp.packet.Presence)6 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)5 StringprepException (gnu.inet.encoding.StringprepException)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)3 SessionEntities (org.jivesoftware.openfire.plugin.rest.entity.SessionEntities)3 SQLException (java.sql.SQLException)2