Search in sources :

Example 1 with AdHocCommand

use of org.jivesoftware.openfire.commands.AdHocCommand in project Openfire by igniterealtime.

the class IQDiscoInfoHandler method handleIQ.

public IQ handleIQ(IQ packet) {
    if (packet.getType() == IQ.Type.result) {
        List<Element> features = packet.getChildElement().elements("feature");
        // Detect if this item is the MUC service
        for (Element feature : features) {
            String variable = feature.attributeValue("var");
            if ("http://jabber.org/protocol/muc".equals(variable)) {
                workgroupManager.setMUCServiceName(packet.getFrom().getDomain());
            }
        }
        return null;
    }
    // Create a copy of the sent pack that will be used as the reply
    // we only need to add the requested info to the reply if any, otherwise add
    // a not found error
    IQ reply = IQ.createResultIQ(packet);
    // Check if the disco#info was sent to the workgroup service itself
    if (workgroupManager.getAddress().equals(packet.getTo())) {
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");
        reply.setChildElement(iq.createCopy());
        Element queryElement = reply.getChildElement();
        if (node == null) {
            // Create and add a the identity of the workgroup service
            Element identity = queryElement.addElement("identity");
            identity.addAttribute("category", "collaboration");
            // TODO Get the name from a property
            identity.addAttribute("name", "Fastpath");
            identity.addAttribute("type", "workgroup");
            // Create and add a the feature provided by the workgroup service
            Element feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/workgroup");
            // Create and add a the disco#info feature
            feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/disco#info");
            // Indicate that we can provide information about the software version being used
            feature = queryElement.addElement("feature");
            feature.addAttribute("var", "jabber:iq:version");
            // Indicate that we support ad-hoc commands
            feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/commands");
            // Add the features provided by the features providers
            for (DiscoFeaturesProvider provider : featuresProviders) {
                for (String newFeature : provider.getFeatures()) {
                    feature = queryElement.addElement("feature");
                    feature.addAttribute("var", newFeature);
                }
            }
        } else if ("http://jabber.org/protocol/commands".equals(node)) {
            // Create and add a the identity of the workgroup service
            Element identity = queryElement.addElement("identity");
            identity.addAttribute("category", "collaboration");
            // TODO Get the name from a property
            identity.addAttribute("name", "Fastpath");
            identity.addAttribute("type", "workgroup");
            // Create and add a the disco#info feature
            Element feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/disco#info");
            // Indicate that we support ad-hoc commands
            feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/commands");
        } else {
            // Check if the node matches a supported command
            boolean found = false;
            for (AdHocCommand command : commandManager.getCommands()) {
                if (node.equals(command.getCode())) {
                    found = true;
                    // Only include commands that the sender can invoke (i.e. has enough permissions)
                    if (command.hasPermission(packet.getFrom())) {
                        // Create and add a the identity of the command
                        Element identity = queryElement.addElement("identity");
                        identity.addAttribute("category", "automation");
                        identity.addAttribute("name", command.getLabel());
                        identity.addAttribute("type", "command-node");
                        // Indicate that we support ad-hoc commands
                        Element feature = queryElement.addElement("feature");
                        feature.addAttribute("var", "http://jabber.org/protocol/commands");
                    } else {
                        // Return Forbidden error
                        reply.setError(PacketError.Condition.forbidden);
                    }
                }
            }
            if (!found) {
                // Return item_not_found error
                reply.setError(PacketError.Condition.item_not_found);
            }
        }
    } else {
        // Check if the disco#info was sent to a given workgroup
        try {
            Workgroup workgroup = workgroupManager.getWorkgroup(packet.getTo());
            Element iq = packet.getChildElement();
            reply.setChildElement(iq.createCopy());
            Element queryElement = reply.getChildElement();
            // Create and add a the identity of the workgroup service
            Element identity = queryElement.addElement("identity");
            identity.addAttribute("category", "collaboration");
            identity.addAttribute("name", workgroup.getJID().getNode());
            identity.addAttribute("type", "workgroup");
            // Create and add a the disco#info feature
            Element feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/disco#info");
            Element form = queryElement.addElement("x", "jabber:x:data");
            form.addAttribute("type", "result");
            // Add static field
            Element field = form.addElement("field");
            field.addAttribute("var", "FORM_TYPE");
            field.addAttribute("type", "hidden");
            field.addElement("value").setText("http://jabber.org/protocol/workgroup#workgroupinfo");
            // Add workgroup description
            field = form.addElement("field");
            field.addAttribute("var", "workgroup#description");
            field.addAttribute("label", "Description");
            field.addElement("value").setText(workgroup.getDescription() == null ? "" : workgroup.getDescription());
            // Add workgroup online status
            field = form.addElement("field");
            field.addAttribute("var", "workgroup#online");
            field.addAttribute("label", "Status");
            field.addElement("value").setText(workgroup.getStatus().name());
        } catch (UserNotFoundException e) {
            // If we didn't find a workgroup then answer a not found error
            reply.setChildElement(packet.getChildElement().createCopy());
            reply.setError(PacketError.Condition.item_not_found);
        }
    }
    return reply;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) AdHocCommand(org.jivesoftware.openfire.commands.AdHocCommand) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Workgroup(org.jivesoftware.xmpp.workgroup.Workgroup)

Example 2 with AdHocCommand

use of org.jivesoftware.openfire.commands.AdHocCommand in project Openfire by igniterealtime.

the class IQDiscoItemsHandler method handleIQ.

public IQ handleIQ(IQ packet) {
    if (packet.getType() == IQ.Type.result) {
        List<Element> items = packet.getChildElement().elements("item");
        // Send a disco#info to each discovered item
        for (Element item : items) {
            String jid = item.attributeValue("jid");
            IQ disco = new IQ(IQ.Type.get);
            disco.setTo(jid);
            disco.setFrom(packet.getTo());
            disco.setChildElement("query", "http://jabber.org/protocol/disco#info");
            workgroupManager.send(disco);
        }
        return null;
    }
    // Create a copy of the sent pack that will be used as the reply
    // we only need to add the requested info to the reply if any, otherwise add
    // a not found error
    IQ reply = IQ.createResultIQ(packet);
    if (IQ.Type.set == packet.getType()) {
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.bad_request);
        return reply;
    }
    // Check if the disco#items was sent to the workgroup service itself
    if (workgroupManager.getAddress().equals(packet.getTo())) {
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");
        reply.setChildElement(iq.createCopy());
        Element queryElement = reply.getChildElement();
        if (node == null) {
            // Add the hosted workgroups to the reply
            for (Workgroup workgroup : workgroupManager.getWorkgroups()) {
                Element item = queryElement.addElement("item");
                item.addAttribute("jid", workgroup.getJID().toString());
                item.addAttribute("name", workgroup.getJID().getNode());
            }
        } else if ("http://jabber.org/protocol/commands".equals(node)) {
            for (AdHocCommand command : commandManager.getCommands()) {
                // Only include commands that the sender can invoke (i.e. has enough permissions)
                if (command.hasPermission(packet.getFrom())) {
                    Element item = queryElement.addElement("item");
                    item.addAttribute("jid", workgroupManager.getAddress().toString());
                    item.addAttribute("node", command.getCode());
                    item.addAttribute("name", command.getLabel());
                }
            }
        } else {
            // Unknown node. Service not available
            reply.setError(PacketError.Condition.service_unavailable);
        }
    } else {
        // Answer an error if the user is trying to discover items of a workgroup
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.not_acceptable);
    }
    return reply;
}
Also used : AdHocCommand(org.jivesoftware.openfire.commands.AdHocCommand) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Workgroup(org.jivesoftware.xmpp.workgroup.Workgroup)

Aggregations

Element (org.dom4j.Element)2 AdHocCommand (org.jivesoftware.openfire.commands.AdHocCommand)2 Workgroup (org.jivesoftware.xmpp.workgroup.Workgroup)2 IQ (org.xmpp.packet.IQ)2 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1