Search in sources :

Example 66 with IQ

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

the class IQPrivacyHandler method deleteList.

private IQ deleteList(IQ packet, JID from, String listName) {
    ClientSession currentSession;
    IQ result = IQ.createResultIQ(packet);
    Element childElement = packet.getChildElement().createCopy();
    result.setChildElement(childElement);
    // Get the list to delete
    PrivacyList list = manager.getPrivacyList(from.getNode(), listName);
    if (list == null) {
        // List to delete was not found
        result.setError(PacketError.Condition.item_not_found);
        return result;
    } else {
        currentSession = sessionManager.getSession(from);
        // Check if the list is being used by another session
        for (ClientSession session : sessionManager.getSessions(from.getNode())) {
            if (currentSession == session) {
                // Ignore the active session for this checking
                continue;
            }
            if (list.equals(session.getDefaultList()) || list.equals(session.getActiveList())) {
                // List to delete is being used by another session so return a conflict error
                result.setError(PacketError.Condition.conflict);
                return result;
            }
        }
    }
    // Remove the list from the active session (if it was being used)
    if (list.equals(currentSession.getDefaultList())) {
        currentSession.setDefaultList(null);
    }
    if (list.equals(currentSession.getActiveList())) {
        currentSession.setActiveList(null);
    }
    manager.deletePrivacyList(from.getNode(), listName);
    return result;
}
Also used : ClientSession(org.jivesoftware.openfire.session.ClientSession) Element(org.dom4j.Element) PrivacyList(org.jivesoftware.openfire.privacy.PrivacyList) IQ(org.xmpp.packet.IQ)

Example 67 with IQ

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

the class IQPrivacyHandler method handleIQ.

@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    IQ.Type type = packet.getType();
    JID from = packet.getFrom();
    if (from.getNode() == null || !UserManager.getInstance().isRegisteredUser(from, false)) {
        // Service is unavailable for anonymous users
        IQ result = IQ.createResultIQ(packet);
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.service_unavailable);
        return result;
    }
    IQ result = null;
    if (type.equals(IQ.Type.get)) {
        // User wants to retrieve a privacy list or the list of privacy list
        Element child = packet.getChildElement();
        List elements = child.elements();
        if (elements.isEmpty()) {
            // User requested names of privacy lists
            result = getPrivacyListsNames(packet, from);
        } else {
            // User requested a privacy list
            result = getPrivacyList(packet, from);
        }
    } else if (type.equals(IQ.Type.set)) {
        Element child = packet.getChildElement();
        Element activeList = child.element("active");
        Element defaultList = child.element("default");
        if (activeList != null) {
            // Active list handling
            String listName = activeList.attributeValue("name");
            if (listName != null) {
                // User wants to set or change the active list currently being applied by
                // the server to this session
                result = setActiveList(packet, from, listName);
            } else {
                // User wants to decline the use of any active list for this session
                result = declineActiveList(packet, from);
            }
        } else if (defaultList != null) {
            // Default list handling
            String listName = defaultList.attributeValue("name");
            if (listName != null) {
                // User wants to set or change its default list (i.e. which applies
                // to the user as a whole, not only the sending resource)
                result = setDefaultList(packet, from, listName);
            } else {
                // User wants to decline the use of a default list
                result = declineDefaultList(packet, from);
            }
        } else {
            // Privacy list handling (create/edit/delete)
            Element list = child.element("list");
            String listName = list.attributeValue("name");
            List items = list.elements();
            if (!items.isEmpty()) {
                // User wants to create or edit a privacy list
                result = updateOrCreateList(packet, from, list);
            } else {
                // User wants to delete a privacy list
                result = deleteList(packet, from, listName);
            }
        }
    }
    return result;
}
Also used : JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) List(java.util.List) PrivacyList(org.jivesoftware.openfire.privacy.PrivacyList)

Example 68 with IQ

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

the class IQPrivacyHandler method getPrivacyListsNames.

/**
 * Returns the IQ packet containing the active and default lists and the lists
 * defined by the user.
 *
 * @param packet IQ packet requesting the lists.
 * @param from sender of the IQ packet.
 * @return the IQ packet containing the active and default lists and the lists
 *         defined by the user.
 */
private IQ getPrivacyListsNames(IQ packet, JID from) {
    IQ result = IQ.createResultIQ(packet);
    Element childElement = packet.getChildElement().createCopy();
    result.setChildElement(childElement);
    Map<String, Boolean> privacyLists = provider.getPrivacyLists(from.getNode());
    // Add the default list
    for (String listName : privacyLists.keySet()) {
        if (privacyLists.get(listName)) {
            childElement.addElement("default").addAttribute("name", listName);
        }
    }
    // Add the active list (only if there is an active list for the session)
    ClientSession session = sessionManager.getSession(from);
    if (session != null && session.getActiveList() != null) {
        childElement.addElement("active").addAttribute("name", session.getActiveList().getName());
    }
    // Add a list element for each privacy list
    for (String listName : privacyLists.keySet()) {
        childElement.addElement("list").addAttribute("name", listName);
    }
    return result;
}
Also used : Element(org.dom4j.Element) ClientSession(org.jivesoftware.openfire.session.ClientSession) IQ(org.xmpp.packet.IQ)

Example 69 with IQ

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

the class IQRosterHandler method handleIQ.

/**
 * Handles all roster queries. There are two major types of queries:
 *
 * <ul>
 *      <li>Roster remove - A forced removal of items from a roster. Roster
 *      removals are the only roster queries allowed to
 *      directly affect the roster from another user.
 *      </li>
 *      <li>Roster management - A local user looking up or updating their
 *      roster.
 *      </li>
 * </ul>
 *
 * @param packet The update packet
 * @return The reply or null if no reply
 */
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
    try {
        IQ returnPacket;
        org.xmpp.packet.Roster roster = (org.xmpp.packet.Roster) packet;
        JID recipientJID = packet.getTo();
        // The packet is bound for the server and must be roster management
        if (recipientJID == null || recipientJID.equals(packet.getFrom().asBareJID())) {
            returnPacket = manageRoster(roster);
        } else {
            returnPacket = IQ.createResultIQ(packet);
            // The server MUST return a <forbidden/> stanza error to the client if the sender of the roster set is not authorized to update the roster
            // (where typically only an authenticated resource of the account itself is authorized).
            returnPacket.setError(PacketError.Condition.forbidden);
        }
        return returnPacket;
    } catch (SharedGroupException e) {
        IQ result = IQ.createResultIQ(packet);
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.not_acceptable);
        return result;
    } catch (UnauthorizedException e) {
        IQ result = IQ.createResultIQ(packet);
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.not_authorized);
        return result;
    } catch (Exception e) {
        if (e.getCause() instanceof IDNAException || e.getCause() instanceof IllegalArgumentException) {
            Log.warn(LocaleUtils.getLocalizedString("admin.error") + e.getMessage());
            IQ result = IQ.createResultIQ(packet);
            result.setChildElement(packet.getChildElement().createCopy());
            result.setError(PacketError.Condition.jid_malformed);
            return result;
        } else {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
            IQ result = IQ.createResultIQ(packet);
            result.setChildElement(packet.getChildElement().createCopy());
            result.setError(PacketError.Condition.internal_server_error);
            return result;
        }
    }
}
Also used : IDNAException(gnu.inet.encoding.IDNAException) Roster(org.jivesoftware.openfire.roster.Roster) JID(org.xmpp.packet.JID) IQ(org.xmpp.packet.IQ) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IDNAException(gnu.inet.encoding.IDNAException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 70 with IQ

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

the class IQBlockingHandler method pushBlocklistUpdates.

/**
 * Sends an IQ-set with the newly blocked JIDs to all resources of the user that have requested the blocklist.
 *
 * @param user      The for which updates are to be broadcasted (cannot be null).
 * @param newBlocks The JIDs for which an update needs to be sent (cannot be null, can be empty).
 */
protected void pushBlocklistUpdates(User user, List<JID> newBlocks) {
    if (newBlocks.isEmpty()) {
        return;
    }
    Log.debug("Pushing blocklist updates to all resources of user '{}' that have previously requested the blocklist.", user.getUsername());
    final Collection<ClientSession> sessions = sessionManager.getSessions(user.getUsername());
    for (final ClientSession session : sessions) {
        if (session.hasRequestedBlocklist()) {
            final IQ iq = new IQ(IQ.Type.set);
            iq.setTo(session.getAddress());
            final Element block = iq.setChildElement("block", NAMESPACE);
            for (final JID newBlock : newBlocks) {
                block.addElement("item").addAttribute("jid", newBlock.toString());
            }
            XMPPServer.getInstance().getPacketRouter().route(iq);
        }
    }
}
Also used : JID(org.xmpp.packet.JID) ClientSession(org.jivesoftware.openfire.session.ClientSession) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ)

Aggregations

IQ (org.xmpp.packet.IQ)208 Element (org.dom4j.Element)141 JID (org.xmpp.packet.JID)49 PacketError (org.xmpp.packet.PacketError)35 Presence (org.xmpp.packet.Presence)19 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)18 Message (org.xmpp.packet.Message)17 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)16 ClientSession (org.jivesoftware.openfire.session.ClientSession)14 DataForm (org.xmpp.forms.DataForm)13 ArrayList (java.util.ArrayList)11 AgentNotFoundException (org.jivesoftware.xmpp.workgroup.AgentNotFoundException)10 Packet (org.xmpp.packet.Packet)10 PacketException (org.jivesoftware.openfire.PacketException)9 User (org.jivesoftware.openfire.user.User)8 List (java.util.List)7 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)7 Iterator (java.util.Iterator)6 Test (org.junit.Test)6 FormField (org.xmpp.forms.FormField)6