Search in sources :

Example 6 with Packet

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

the class BaseMUCTransport method processPacket.

/**
     * Handles all incoming iq stanzas.
     *
     * @param packet The iq packet to be processed.
     * @return list of packets that will be sent back to the IQ requester.
     */
private List<Packet> processPacket(IQ packet) {
    Log.debug("Received iq packet: " + packet.toXML());
    List<Packet> reply = new ArrayList<Packet>();
    if (packet.getType() == IQ.Type.error) {
        // Lets not start a loop.  Ignore.
        return reply;
    }
    String xmlns = null;
    Element child = (packet).getChildElement();
    if (child != null) {
        xmlns = child.getNamespaceURI();
    }
    if (xmlns == null) {
        // No namespace defined.
        Log.debug("No XMLNS:" + packet.toString());
        IQ error = IQ.createResultIQ(packet);
        error.setError(PacketError.Condition.bad_request);
        reply.add(error);
        return reply;
    }
    if (xmlns.equals(NameSpace.DISCO_INFO)) {
        reply.addAll(handleDiscoInfo(packet));
    } else if (xmlns.equals(NameSpace.DISCO_ITEMS)) {
        reply.addAll(handleDiscoItems(packet));
    } else if (xmlns.equals(NameSpace.MUC_ADMIN)) {
        reply.addAll(handleMUCAdmin(packet));
    } else if (xmlns.equals(NameSpace.MUC_USER)) {
        reply.addAll(handleMUCUser(packet));
    } else {
        Log.debug("Unable to handle iq request: " + xmlns);
        IQ error = IQ.createResultIQ(packet);
        error.setError(PacketError.Condition.service_unavailable);
        reply.add(error);
    }
    return reply;
}
Also used : Packet(org.xmpp.packet.Packet) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) IQ(org.xmpp.packet.IQ)

Example 7 with Packet

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

the class BaseMUCTransport method handleMUCAdmin.

/**
     * Handle MUC admin requests.
     *
     * @param packet An IQ packet in the MUC admin namespace.
     * @return A list of IQ packets to be returned to the user.
     */
private List<Packet> handleMUCAdmin(IQ packet) {
    List<Packet> reply = new ArrayList<Packet>();
    JID from = packet.getFrom();
    JID to = packet.getTo();
    Element query = (packet).getChildElement();
    Element item = query.element("item");
    String nick = item.attribute("nick").getText();
    String role = item.attribute("role").getText();
    try {
        TransportSession<B> session = getTransport().getSessionManager().getSession(from);
        if (session.isLoggedIn()) {
            try {
                MUCTransportSession mucSession = session.getMUCSessionManager().getSession(to.getNode());
                if (packet.getTo().getNode() == null) {
                // Targeted at a room.
                } else {
                    // Targeted at a specific user.
                    if (nick != null && role != null) {
                        if (role.equals("none")) {
                            // This is a kick
                            String reason = "";
                            Element reasonElem = item.element("reason");
                            if (reasonElem != null) {
                                reason = reasonElem.getText();
                            }
                            mucSession.kickUser(nick, reason);
                        }
                    }
                }
            } catch (NotFoundException e) {
            // Not found?  No active session then.
            }
        }
    } catch (NotFoundException e) {
    // Not found?  No active session then.
    }
    return reply;
}
Also used : Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) NotFoundException(org.jivesoftware.util.NotFoundException)

Example 8 with Packet

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

the class BaseMUCTransport method handleDiscoItems.

/**
     * Handle service discovery items request.
     *
     * @param packet An IQ packet in the disco items namespace.
     * @return A list of IQ packets to be returned to the user.
     */
private List<Packet> handleDiscoItems(IQ packet) {
    List<Packet> reply = new ArrayList<Packet>();
    JID from = packet.getFrom();
    JID to = packet.getTo();
    if (packet.getTo().getNode() == null) {
        // A request for a list of rooms
        IQ result = IQ.createResultIQ(packet);
        if (JiveGlobals.getBooleanProperty("plugin.gateway." + getTransport().getType() + ".roomlist", false)) {
            try {
                TransportSession<B> session = getTransport().getSessionManager().getSession(from);
                if (session.isLoggedIn()) {
                    storePendingRequest(packet);
                    session.getRooms();
                }
            } catch (NotFoundException e) {
                // Not found?  No active session then.
                result.setError(PacketError.Condition.forbidden);
                reply.add(result);
            }
        } else {
            // Time to lie and tell them we have no rooms
            sendRooms(from, new ArrayList<MUCTransportRoom>());
        }
    } else {
        // Ah, a request for members of a room.
        IQ result = IQ.createResultIQ(packet);
        try {
            TransportSession<B> session = getTransport().getSessionManager().getSession(from);
            if (session.isLoggedIn()) {
                storePendingRequest(packet);
                session.getRoomMembers(getTransport().convertJIDToID(to));
            } else {
                // Not logged in?  Not logged in then.
                result.setError(PacketError.Condition.forbidden);
                reply.add(result);
            }
        } catch (NotFoundException e) {
            // Not found?  No active session then.
            result.setError(PacketError.Condition.forbidden);
            reply.add(result);
        }
    }
    return reply;
}
Also used : Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList) IQ(org.xmpp.packet.IQ) NotFoundException(org.jivesoftware.util.NotFoundException)

Example 9 with Packet

use of org.xmpp.packet.Packet in project jitsi-videobridge by jitsi.

the class ComponentImpl method send.

/**
 * Implements a helper to send <tt>org.jivesoftware.smack.packet.IQ</tt>s.
 *
 * @param iq the <tt>org.jivesoftware.smack.packet.IQ</tt> to send
 * @throws Exception if an error occurs during the conversion of the
 * specified <tt>iq</tt> to an <tt>org.xmpp.packet.IQ</tt> instance or while
 * sending the specified <tt>iq</tt>
 */
public void send(org.jivesoftware.smack.packet.IQ iq) throws Exception {
    try {
        /*
             * The javadoc on ComponentManager.sendPacket(Component,Packet)
             * which is invoked by AbstractComponent.send(Packet) says that the
             * value of the from property of the Packet must not be null;
             * otherwise, an IllegalArgumentException will be thrown.
             */
        Jid from = iq.getFrom();
        if ((from == null) || (from.length() == 0)) {
            JID fromJID = getJID();
            if (fromJID != null)
                iq.setFrom(JidCreate.from(fromJID.toString()));
        }
        Packet packet = IQUtils.convert(iq);
        send(packet);
        if (logger.isDebugEnabled()) {
            logger.debug("SENT: " + packet.toXML());
        }
    } catch (Exception e) {
        logger.error("Failed to send an IQ with id=" + (iq == null ? "null" : iq.getStanzaId()), e);
        throw e;
    }
}
Also used : Packet(org.xmpp.packet.Packet)

Example 10 with Packet

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

the class MUCRole method augmentOutboundStanzaWithFMUCData.

/**
 * When sending data to a user that joined the room through FMUC (when the user is a user that is local to a remote
 * chatroom that joined our room as a 'joining FMUC node'), then we'll need to add an 'fmuc' element to all data
 * that we send it.
 *
 * The data that is to be added must include the 'from' address representing the FMUC role for the occupant that
 * sent the stanza. We either use the reported FMUC address as passed down from other FMUC nodes, or we use the
 * address of users connected locally to our server.
 *
 * This method will add an 'fmuc' child element to the stanza when the user is a user that joined through FMUC (is
 * a member of a room that federates with the MUC room local to our server).
 *
 * If the provided stanza already contains an FMUC element with relevant data, this data is left unchanged.
 *
 * @param packet The stanza to augment
 */
public void augmentOutboundStanzaWithFMUCData(@Nonnull Packet packet) {
    if (!isRemoteFmuc()) {
        Log.trace("Recipient '{}' is not in a remote FMUC room. No need to augment stanza with FMUC data.", this.getUserAddress());
        return;
    }
    Log.trace("Sending data to recipient '{}' that has joined local room '{}' through a federated remote room (FMUC). Outbound stanza is required to include FMUC data: {}", this.getUserAddress(), this.getChatRoom().getJID(), packet);
    // Data that was sent to us from a(nother) FMUC node might already have this value. If not, we need to ensure that this is added.
    Element fmuc = packet.getElement().element(QName.get("fmuc", "http://isode.com/protocol/fmuc"));
    if (fmuc == null) {
        fmuc = packet.getElement().addElement(QName.get("fmuc", "http://isode.com/protocol/fmuc"));
    }
    if (fmuc.attributeValue("from") != null && !fmuc.attributeValue("from").trim().isEmpty()) {
        Log.trace("Outbound stanza already includes FMUC data. No need to include further data.");
        return;
    }
    final JID reportingFmucAddress;
    if (packet.getFrom().getResource() == null) {
        Log.trace("Sender is the room itself: '{}'", packet.getFrom());
        reportingFmucAddress = this.getChatRoom().getRole().getRoleAddress();
    } else {
        Log.trace("Sender is an occupant of the room: '{}'", packet.getFrom());
        // Determine the role of the entity that sent the message.
        final Set<MUCRole> sender = new HashSet<>();
        try {
            sender.addAll(this.getChatRoom().getOccupantsByNickname(packet.getFrom().getResource()));
        } catch (UserNotFoundException e) {
            Log.trace("Unable to identify occupant '{}'", packet.getFrom());
        }
        // If this users is user joined through FMUC, use the FMUC-reported address, otherwise, use the local address.
        switch(sender.size()) {
            case 0:
                Log.warn("Cannot add required FMUC data to outbound stanza. Unable to determine the role of the sender of stanza sent over FMUC: {}", packet);
                return;
            case 1:
                final MUCRole role = sender.iterator().next();
                if (role.isRemoteFmuc()) {
                    reportingFmucAddress = role.getReportedFmucAddress();
                } else {
                    reportingFmucAddress = role.getUserAddress();
                }
                break;
            default:
                // The user has more than one role, which probably means it joined the room from more than one device.
                // At this point in the code flow, we can't determine anymore which full JID caused the stanza to be sent.
                // As a fallback, send the _bare_ JID of the user (which should be equal for all its resources).
                // TODO verify if the compromise is acceptable in the XEP.
                final Set<JID> bareJids = sender.stream().map(r -> {
                    if (r.isRemoteFmuc()) {
                        return r.getReportedFmucAddress().asBareJID();
                    } else {
                        return r.getUserAddress().asBareJID();
                    }
                }).collect(Collectors.toSet());
                if (bareJids.size() == 1) {
                    Log.warn("Sender '{}' has more than one role in room '{}', indicating that the user joined the room from more than one device. Using its bare instead of full JID for FMUC reporting.", packet.getFrom(), this.getChatRoom().getJID());
                    reportingFmucAddress = bareJids.iterator().next().asBareJID();
                } else {
                    throw new IllegalStateException("Unable to deduce one FMUC address for occupant address '" + packet.getFrom() + "'.");
                }
                break;
        }
    }
    Log.trace("Adding 'from' FMUC data to outbound stanza, using FMUC address of sender of data, that has been determined to be '{}'.", reportingFmucAddress);
    fmuc.addAttribute("from", reportingFmucAddress.toString());
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Presence(org.xmpp.packet.Presence) DefaultElement(org.dom4j.tree.DefaultElement) Logger(org.slf4j.Logger) Externalizable(java.io.Externalizable) ObjectOutput(java.io.ObjectOutput) LoggerFactory(org.slf4j.LoggerFactory) DocumentHelper(org.dom4j.DocumentHelper) Set(java.util.Set) IOException(java.io.IOException) JID(org.xmpp.packet.JID) Collectors(java.util.stream.Collectors) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) CannotCalculateSizeException(org.jivesoftware.util.cache.CannotCalculateSizeException) CacheSizes(org.jivesoftware.util.cache.CacheSizes) HashSet(java.util.HashSet) ElementUtil(org.jivesoftware.util.ElementUtil) ExternalizableUtil(org.jivesoftware.util.cache.ExternalizableUtil) Packet(org.xmpp.packet.Packet) Element(org.dom4j.Element) QName(org.dom4j.QName) XMPPServer(org.jivesoftware.openfire.XMPPServer) ObjectInput(java.io.ObjectInput) Nonnull(javax.annotation.Nonnull) Cacheable(org.jivesoftware.util.cache.Cacheable) JID(org.xmpp.packet.JID) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) HashSet(java.util.HashSet)

Aggregations

Packet (org.xmpp.packet.Packet)33 JID (org.xmpp.packet.JID)17 Element (org.dom4j.Element)16 Message (org.xmpp.packet.Message)16 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)10 IQ (org.xmpp.packet.IQ)9 Presence (org.xmpp.packet.Presence)7 NotFoundException (org.jivesoftware.util.NotFoundException)5 PacketInterceptor (org.jivesoftware.openfire.interceptor.PacketInterceptor)3 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)3 Session (org.jivesoftware.openfire.session.Session)3 Date (java.sql.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 DefaultElement (org.dom4j.tree.DefaultElement)2 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)2