Search in sources :

Example 51 with FormField

use of org.xmpp.forms.FormField in project Openfire by igniterealtime.

the class LeafNode method postConfigure.

@Override
void postConfigure(DataForm completedForm) {
    List<String> values;
    if (!persistPublishedItems) {
        // Always save the last published item when not configured to use persistent items
        maxPublishedItems = 1;
    } else {
        FormField field = completedForm.getField("pubsub#max_items");
        if (field != null) {
            values = field.getValues();
            maxPublishedItems = values.size() > 0 ? Integer.parseInt(values.get(0)) : 50;
        }
    }
}
Also used : FormField(org.xmpp.forms.FormField)

Example 52 with FormField

use of org.xmpp.forms.FormField in project Openfire by igniterealtime.

the class CreateWorkgroup method addStageInformation.

@Override
protected void addStageInformation(SessionData data, Element command) {
    DataForm form = new DataForm(DataForm.Type.form);
    form.setTitle("Adding a new workgroup");
    form.addInstruction("Fill out this form to add a workgroup.");
    FormField field = form.addField();
    field.setType(FormField.Type.hidden);
    field.setVariable("FORM_TYPE");
    field.addValue("http://jabber.org/protocol/admin");
    field = form.addField();
    field.setType(FormField.Type.text_single);
    field.setLabel("The name of the workgroup to be added");
    field.setVariable("name");
    field.setRequired(true);
    field = form.addField();
    field.setType(FormField.Type.text_multi);
    field.setLabel("Username of the members");
    field.setVariable("members");
    field = form.addField();
    field.setType(FormField.Type.text_single);
    field.setLabel("Description");
    field.setVariable("description");
    // Add the form to the command
    command.add(form.getElement());
}
Also used : DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 53 with FormField

use of org.xmpp.forms.FormField in project Openfire by igniterealtime.

the class DeleteWorkgroup method addStageInformation.

@Override
protected void addStageInformation(SessionData data, Element command) {
    DataForm form = new DataForm(DataForm.Type.form);
    form.setTitle("Delete workgroup");
    form.addInstruction("Fill out this form to delete a workgroup.");
    FormField field = form.addField();
    field.setType(FormField.Type.hidden);
    field.setVariable("FORM_TYPE");
    field.addValue("http://jabber.org/protocol/admin");
    field = form.addField();
    field.setType(FormField.Type.jid_single);
    field.setLabel("Workgroup's JID");
    field.setVariable("workgroup");
    field.setRequired(true);
    // Add the form to the command
    command.add(form.getElement());
}
Also used : DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 54 with FormField

use of org.xmpp.forms.FormField in project Openfire by igniterealtime.

the class IQChatSearchHandler method handleIQ.

public void handleIQ(IQ packet) {
    try {
        // Check that the sender of this IQ is an agent
        workgroupManager.getAgentManager().getAgent(packet.getFrom());
        Element iq = packet.getChildElement();
        IQ reply = IQ.createResultIQ(packet);
        if (iq.elements().isEmpty()) {
            reply.setChildElement(iq.createCopy());
            // Send the search form to the agent
            reply.addExtension(searchForm.createCopy());
            workgroupManager.send(reply);
        } else {
            // Send the result of the search to the agent
            Date startDate = null;
            Date endDate = null;
            Collection<Workgroup> workgroups = WorkgroupManager.getInstance().getWorkgroups();
            JID agentJID = null;
            String queryString = null;
            // Get the search parameters from the completed form
            DataForm submitedForm = (DataForm) packet.getExtension(DataForm.ELEMENT_NAME, DataForm.NAMESPACE);
            for (FormField field : submitedForm.getFields()) {
                if ("date/start".equals(field.getVariable())) {
                    try {
                        startDate = DataForm.parseDate(field.getValues().get(0));
                    } catch (ParseException e) {
                        Log.debug("Invalid startDate " + field.getValues().get(0), e);
                    }
                } else if ("date/end".equals(field.getVariable())) {
                    try {
                        endDate = DataForm.parseDate(field.getValues().get(0));
                    } catch (ParseException e) {
                        Log.debug("Invalid endDate " + field.getValues().get(0), e);
                    }
                } else if ("workgroups".equals(field.getVariable())) {
                    if (!field.getValues().isEmpty()) {
                        workgroups = new ArrayList<Workgroup>();
                        for (String value : field.getValues()) {
                            try {
                                workgroups.add(WorkgroupManager.getInstance().getWorkgroup(new JID(value)));
                            } catch (UserNotFoundException e) {
                                Log.debug("Invalid workgroup JID " + value, e);
                            }
                        }
                    } else {
                        // Search in all the workgroups since no one was specified
                        workgroups = WorkgroupManager.getInstance().getWorkgroups();
                    }
                } else if ("agent".equals(field.getVariable())) {
                    agentJID = new JID(field.getValues().get(0));
                } else if ("queryString".equals(field.getVariable())) {
                    queryString = field.getValues().get(0);
                }
            }
            // Build the response
            DataForm searchResults = resultForm.createCopy();
            // Perform the search
            for (Workgroup workgroup : workgroups) {
                ChatSearch search = new ChatSearch(workgroup, startDate, endDate, agentJID, queryString);
                for (QueryResult result : search.getResults()) {
                    Map<String, Object> fields = new LinkedHashMap<String, Object>();
                    fields.put("workgroup", result.getWorkgroup().getJID().toBareJID());
                    fields.put("sessionID", result.getSessionID());
                    fields.put("startDate", result.getStartDate());
                    fields.put("agentJIDs", result.getAgentJIDs());
                    fields.put("relevance", result.getRelevance());
                    // Add Metadata
                    Map<String, String> metadata = getMetadataMap(result.getSessionID());
                    if (metadata.containsKey("question")) {
                        fields.put("question", metadata.get("question"));
                    }
                    if (metadata.containsKey("email")) {
                        fields.put("email", metadata.get("email"));
                    }
                    if (metadata.containsKey("username")) {
                        fields.put("username", metadata.get("username"));
                    }
                    searchResults.addItemFields(fields);
                }
            }
            reply.setChildElement(iq.getName(), iq.getNamespaceURI());
            reply.addExtension(searchResults);
            workgroupManager.send(reply);
        }
    } catch (AgentNotFoundException e) {
        IQ reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(new PacketError(PacketError.Condition.not_authorized));
        workgroupManager.send(reply);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) AgentNotFoundException(org.jivesoftware.xmpp.workgroup.AgentNotFoundException) PacketError(org.xmpp.packet.PacketError) Workgroup(org.jivesoftware.xmpp.workgroup.Workgroup) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) DataForm(org.xmpp.forms.DataForm) ParseException(java.text.ParseException) FormField(org.xmpp.forms.FormField)

Example 55 with FormField

use of org.xmpp.forms.FormField in project Openfire by igniterealtime.

the class RegistrationHandler method setRegistrationForm.

/**
     * Handles a IQ-register 'set' request, which is to be interpreted as a
     * request to create a new registration.
     *
     * @param packet the IQ-register 'set' stanza.
     * @throws UnauthorizedException if the user isn't allowed to register.
     */
private void setRegistrationForm(IQ packet) throws UnauthorizedException {
    final JID from = packet.getFrom();
    final boolean registered;
    Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType);
    if (registrations.iterator().hasNext()) {
        registered = true;
    } else {
        registered = false;
    }
    if (!registered && !parent.permissionManager.hasAccess(from)) {
        // registered.
        throw new UnauthorizedException(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyacls", "kraken"));
    }
    // Parse the input variables
    String username = null;
    String password = null;
    String nickname = null;
    try {
        if (packet.getChildElement().element("x") != null) {
            final DataForm form = new DataForm(packet.getChildElement().element("x"));
            final List<FormField> fields = form.getFields();
            for (final FormField field : fields) {
                final String var = field.getVariable();
                if (var.equals("username")) {
                    username = field.getValues().get(0);
                } else if (var.equals("password")) {
                    password = field.getValues().get(0);
                } else if (var.equals("nick")) {
                    nickname = field.getValues().get(0);
                }
            }
        }
    }// specific subclasses instead).
     catch (Exception ex) {
        // No with data form apparently
        Log.info("Most likely, no dataform was present " + "in the IQ-register request.", ex);
    }
    // input variables could also exist in the non-extended elements
    final Element userEl = packet.getChildElement().element("username");
    final Element passEl = packet.getChildElement().element("password");
    final Element nickEl = packet.getChildElement().element("nick");
    if (userEl != null) {
        username = userEl.getTextTrim();
    }
    if (passEl != null) {
        password = passEl.getTextTrim();
    }
    if (nickEl != null) {
        nickname = nickEl.getTextTrim();
    }
    username = (username == null || username.equals("")) ? null : username;
    password = (password == null || password.equals("")) ? null : password;
    nickname = (nickname == null || nickname.equals("")) ? null : nickname;
    // verify that we've got wat we need.
    if (username == null || (parent.isPasswordRequired() && password == null) || (parent.isNicknameRequired() && nickname == null)) {
        // Invalid information from stanza, lets yell.
        Log.info("Cannot process IQ register request, as it " + "fails to provide all data that's required: " + packet.toXML());
        final IQ result = IQ.createResultIQ(packet);
        result.setError(Condition.bad_request);
        parent.sendPacket(result);
        return;
    }
    // Check if the client supports our proprietary 'rosterless' mode.
    final boolean rosterlessMode;
    final Element x = packet.getChildElement().element("x");
    if (x != null && x.getNamespaceURI() != null && x.getNamespaceURI().equals(NameSpace.IQ_GATEWAY_REGISTER)) {
        rosterlessMode = true;
        Log.info("Registering " + packet.getFrom() + " as " + username + " in rosterless mode.");
    } else {
        rosterlessMode = false;
        Log.info("Registering " + packet.getFrom() + " as " + username + " (without making use of rosterless mode).");
    }
    // Here's where the true magic lies: create the registration!
    try {
        addNewRegistration(from, username, password, nickname, rosterlessMode);
        registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType);
        Registration registration = registrations.iterator().next();
        TransportSession session = parent.registrationLoggedIn(registration, from, PresenceType.available, "", -1);
        session.setRegistrationPacket(packet);
        session.detachSession();
        parent.getSessionManager().storeSession(from, session);
    //final IQ result = IQ.createResultIQ(packet);
    // I believe this shouldn't be included. Leaving it around just in
    // case.
    // Element response =
    // DocumentHelper.createElement(QName.get("query", IQ_REGISTER));
    // result.setChildElement(response);
    //parent.sendPacket(result);
    } catch (UserNotFoundException e) {
        Log.warn("Someone attempted to register with the gateway " + "who is not registered with the server: " + from);
        final IQ eresult = IQ.createResultIQ(packet);
        eresult.setError(Condition.forbidden);
        parent.sendPacket(eresult);
        final Message em = new Message();
        em.setType(Message.Type.error);
        em.setTo(packet.getFrom());
        em.setFrom(packet.getTo());
        em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniednoacct", "kraken"));
        parent.sendPacket(em);
    } catch (IllegalAccessException e) {
        Log.warn("Someone who is not a user of this server " + "tried to register with the transport: " + from);
        final IQ eresult = IQ.createResultIQ(packet);
        eresult.setError(Condition.forbidden);
        parent.sendPacket(eresult);
        final Message em = new Message();
        em.setType(Message.Type.error);
        em.setTo(packet.getFrom());
        em.setFrom(packet.getTo());
        em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyhost", "kraken"));
        parent.sendPacket(em);
    } catch (IllegalArgumentException e) {
        Log.warn("Someone attempted to register with the " + "gateway with an invalid username: " + from);
        final IQ eresult = IQ.createResultIQ(packet);
        eresult.setError(Condition.bad_request);
        parent.sendPacket(eresult);
        final Message em = new Message();
        em.setType(Message.Type.error);
        em.setTo(packet.getFrom());
        em.setFrom(packet.getTo());
        em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbadusername", "kraken"));
        parent.sendPacket(em);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) TransportSession(net.sf.kraken.session.TransportSession) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Aggregations

FormField (org.xmpp.forms.FormField)92 DataForm (org.xmpp.forms.DataForm)80 Element (org.dom4j.Element)21 JID (org.xmpp.packet.JID)13 IQ (org.xmpp.packet.IQ)7 HashSet (java.util.HashSet)6 ClientSession (org.jivesoftware.openfire.session.ClientSession)6 ArrayList (java.util.ArrayList)4 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)4 ParseException (java.text.ParseException)3 Date (java.util.Date)3 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)3 Group (org.jivesoftware.openfire.group.Group)3 Test (org.junit.Test)3 List (java.util.List)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 PacketException (org.jivesoftware.openfire.PacketException)2 GroupJID (org.jivesoftware.openfire.group.GroupJID)2 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)2 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)2