Search in sources :

Example 6 with TransportSession

use of net.sf.kraken.session.TransportSession in project Openfire by igniterealtime.

the class BaseTransport method sendOfflineMessage.

/**
     * Sends an offline message through the component manager.
     *
     * @param to Who the message is for.
     * @param from Who the message is from.
     * @param msg Message to be send.
     * @param type Type of message to be sent.
     * @param time Time when the message was originally sent.
     * @param reason Reason for offline message (can be null)
     */
public void sendOfflineMessage(JID to, JID from, String msg, Message.Type type, Date time, String reason) {
    Message m = new Message();
    m.setType(type);
    m.setFrom(from);
    m.setTo(to);
    m.setBody(net.sf.kraken.util.StringUtils.removeInvalidXMLCharacters(msg));
    if (msg.length() == 0) {
        Log.debug("Dropping empty message packet.");
        return;
    }
    Element delay = m.addChildElement("delay", NameSpace.DELAY);
    //        delay.addAttribute("from", from.toBareJID());
    delay.addAttribute("stamp", UTC_FORMAT.format(time));
    if (reason != null) {
        delay.addCDATA(reason);
    }
    Element offline = m.addChildElement("offline", NameSpace.OFFLINE);
    offline.addElement("item").addAttribute("node", UTC_FORMAT.format(time));
    Element x = m.addChildElement("x", NameSpace.X_DELAY);
    //        x.addAttribute("from", from.toBareJID());
    x.addAttribute("stamp", UTC_FORMAT.format(time));
    if (reason != null) {
        x.addCDATA(reason);
    }
    try {
        TransportSession session = sessionManager.getSession(to);
        if (session.getDetachTimestamp() != 0) {
            // This is a detached session then, so lets store the packet instead of delivering.
            session.storePendingPacket(m);
            return;
        }
    } catch (NotFoundException e) {
    // No session?  That's "fine", allow it through, it's probably something from the transport itself.
    }
    sendPacket(m);
}
Also used : Element(org.dom4j.Element) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) TransportSession(net.sf.kraken.session.TransportSession)

Example 7 with TransportSession

use of net.sf.kraken.session.TransportSession 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)

Example 8 with TransportSession

use of net.sf.kraken.session.TransportSession in project Openfire by igniterealtime.

the class RegistrationHandler method deleteRegistration.

/**
     * Removes a registration from this transport.
     *
     * @param jid JID of user to add registration to.
     * @throws UserNotFoundException if registration or roster not found.
     */
public void deleteRegistration(JID jid) throws UserNotFoundException {
    Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(jid, parent.transportType);
    if (registrations.isEmpty()) {
        throw new UserNotFoundException("User was not registered.");
    }
    // Log out any active sessions.
    try {
        TransportSession session = parent.sessionManager.getSession(jid);
        if (session.isLoggedIn()) {
            parent.registrationLoggedOut(session);
        }
        parent.sessionManager.removeSession(jid);
    } catch (NotFoundException e) {
    // Ok then.
    }
    // For now, we're going to have to just nuke all of these. Sorry.
    for (final Registration reg : registrations) {
        RegistrationManager.getInstance().deleteRegistration(reg);
    }
    // Clean up the user's contact list.
    try {
        parent.cleanUpRoster(jid, false, true);
    } catch (UserNotFoundException e) {
        throw new UserNotFoundException("Unable to find roster.");
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) TransportSession(net.sf.kraken.session.TransportSession)

Aggregations

TransportSession (net.sf.kraken.session.TransportSession)8 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)8 NotFoundException (org.jivesoftware.util.NotFoundException)8 Element (org.dom4j.Element)4 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Registration (net.sf.kraken.registration.Registration)2 TransportBuddy (net.sf.kraken.roster.TransportBuddy)2 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)2 JID (org.xmpp.packet.JID)2 Lock (java.util.concurrent.locks.Lock)1 KrakenPlugin (net.sf.kraken.KrakenPlugin)1 PacketException (org.jivesoftware.openfire.PacketException)1 PluginManager (org.jivesoftware.openfire.container.PluginManager)1 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)1 DataForm (org.xmpp.forms.DataForm)1 FormField (org.xmpp.forms.FormField)1 IQ (org.xmpp.packet.IQ)1 Message (org.xmpp.packet.Message)1