Search in sources :

Example 26 with Packet

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

the class BaseMUCTransport method processPacket.

/**
     * Handles all incoming presence stanzas.
     *
     * @param packet The presence packet to be processed.
     * @return list of packets that will be sent back to the presence requester.
     */
private List<Packet> processPacket(Presence packet) {
    Log.debug("Received presence packet: " + packet.toXML());
    List<Packet> reply = new ArrayList<Packet>();
    JID from = packet.getFrom();
    JID to = packet.getTo();
    if (packet.getType() == Presence.Type.error) {
        // We don't want to do anything with this.  Ignore it.
        return reply;
    }
    try {
        TransportSession<B> session = getTransport().getSessionManager().getSession(from);
        if (!session.isLoggedIn()) {
            Message m = new Message();
            m.setError(Condition.service_unavailable);
            m.setTo(from);
            m.setFrom(getJID());
            m.setBody(LocaleUtils.getLocalizedString("gateway.base.notloggedin", "kraken", Arrays.asList(getTransport().getType().toString().toUpperCase())));
            reply.add(m);
        } else if (to.getNode() == null) {
        // Ignore undirected presence.
        } else if (to.getResource() != null) {
            // Presence to a specific resource.
            if (packet.getType() == Presence.Type.unavailable) {
                // Handle logout.
                try {
                    MUCTransportSession mucSession = session.getMUCSessionManager().getSession(to.getNode());
                    mucSession.leaveRoom();
                    session.getMUCSessionManager().removeSession(to.getNode());
                } catch (NotFoundException e) {
                // Not found?  Well then no problem.
                }
            } else {
                // Handle login.
                try {
                    MUCTransportSession mucSession = session.getMUCSessionManager().getSession(to.getNode());
                    // Active session.
                    mucSession.updateStatus(this.getTransport().getPresenceType(packet));
                } catch (NotFoundException e) {
                    // No current session, lets create one.
                    MUCTransportSession mucSession = createRoom(session, to.getNode(), to.getResource());
                    session.getMUCSessionManager().storeSession(to.getNode(), mucSession);
                    mucSession.enterRoom();
                }
            }
        } else {
            // Presence to the room itself.  Return error as per protocol.
            Presence p = new Presence();
            p.setError(Condition.jid_malformed);
            p.setType(Presence.Type.error);
            p.setTo(from);
            p.setFrom(to);
            reply.add(p);
        }
    } catch (NotFoundException e) {
        Log.debug("Unable to find session.");
        Message m = new Message();
        m.setError(Condition.service_unavailable);
        m.setTo(from);
        m.setFrom(getJID());
        m.setBody(LocaleUtils.getLocalizedString("gateway.base.notloggedin", "kraken", Arrays.asList(getTransport().getType().toString().toUpperCase())));
        reply.add(m);
    }
    return reply;
}
Also used : Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) ArrayList(java.util.ArrayList) NotFoundException(org.jivesoftware.util.NotFoundException) Presence(org.xmpp.packet.Presence)

Example 27 with Packet

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

the class BaseMUCTransport method handleDiscoInfo.

/**
     * Handle service discovery info request.
     *
     * @param packet An IQ packet in the disco info namespace.
     * @return A list of IQ packets to be returned to the user.
     */
private List<Packet> handleDiscoInfo(IQ packet) {
    List<Packet> reply = new ArrayList<Packet>();
    JID from = packet.getFrom();
    JID to = packet.getTo();
    if (packet.getTo().getNode() == null) {
        // Requested info from transport itself.
        IQ result = IQ.createResultIQ(packet);
        if (from.getNode() == null || getTransport().permissionManager.hasAccess(from)) {
            Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_INFO));
            response.addElement("identity").addAttribute("category", "conference").addAttribute("type", "text").addAttribute("name", this.getDescription());
            response.addElement("feature").addAttribute("var", NameSpace.DISCO_INFO);
            response.addElement("feature").addAttribute("var", NameSpace.DISCO_ITEMS);
            response.addElement("feature").addAttribute("var", NameSpace.MUC);
            result.setChildElement(response);
        } else {
            result.setError(PacketError.Condition.forbidden);
        }
        reply.add(result);
    } else {
        // Ah, a request for information about a room.
        IQ result = IQ.createResultIQ(packet);
        try {
            TransportSession<B> session = getTransport().getSessionManager().getSession(from);
            if (session.isLoggedIn()) {
                storePendingRequest(packet);
                session.getRoomInfo(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) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) IQ(org.xmpp.packet.IQ) NotFoundException(org.jivesoftware.util.NotFoundException)

Example 28 with Packet

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

the class Reject method doAction.

@Override
public Packet doAction(Packet packet) throws PacketRejectedException {
    SessionManager sessionManager = SessionManager.getInstance();
    ClientSession clientSession = sessionManager.getSession(packet.getFrom());
    Packet rejectPacket;
    String pfFrom = JiveGlobals.getProperty("pf.From", "packetfilter");
    if (packet instanceof Message) {
        Message in = (Message) packet.createCopy();
        if (clientSession != null && in.getBody() != null) {
            in.setFrom(new JID(pfFrom));
            String rejectMessage = JiveGlobals.getProperty("pf.rejectMessage", "Your message was rejected by the packet filter");
            in.setBody(rejectMessage);
            in.setType(Message.Type.error);
            in.setTo(packet.getFrom());
            String rejectSubject = JiveGlobals.getProperty("pf.rejectSubject", "Rejected");
            in.setSubject(rejectSubject);
            clientSession.process(in);
        }
    } else if (packet instanceof Presence) {
        rejectPacket = new Presence();
        rejectPacket.setTo(packet.getFrom());
        rejectPacket.setError(PacketError.Condition.forbidden);
    } else if (packet instanceof IQ) {
        rejectPacket = new IQ();
        rejectPacket.setTo(packet.getFrom());
        rejectPacket.setError(PacketError.Condition.forbidden);
    }
    if (doLog()) {
        Log.info("Rejecting packet from " + packet.getFrom() + " to " + packet.getTo());
    }
    throw new PacketRejectedException();
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) SessionManager(org.jivesoftware.openfire.SessionManager) ClientSession(org.jivesoftware.openfire.session.ClientSession) IQ(org.xmpp.packet.IQ) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) Presence(org.xmpp.packet.Presence)

Example 29 with Packet

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

the class StanzaIDUtilTest method testUseOriginIdElement.

/**
 * Test if {@link StanzaIDUtil#ensureUniqueAndStableStanzaID(Packet, JID)} uses a different value, if the provided
 * data has an origin-id value.
 */
@Test
public void testUseOriginIdElement() throws Exception {
    // Setup fixture.
    final Packet input = new Message();
    final JID self = new JID("foobar");
    final String expected = "de305d54-75b4-431b-adb2-eb6b9e546013";
    final Element toOverwrite = input.getElement().addElement("origin-id", "urn:xmpp:sid:0");
    toOverwrite.addAttribute("id", expected);
    // Execute system under test.
    final Packet result = StanzaIDUtil.ensureUniqueAndStableStanzaID(input, self);
    // Verify results.
    Assert.assertNotNull(result);
    final Element stanzaIDElement = result.getElement().element(QName.get("stanza-id", "urn:xmpp:sid:0"));
    Assert.assertNotNull(stanzaIDElement);
    try {
        UUID.fromString(stanzaIDElement.attributeValue("id"));
    } catch (IllegalArgumentException ex) {
        Assert.fail();
    }
    Assert.assertNotEquals(expected, stanzaIDElement.attributeValue("id"));
    assertEquals(self.toString(), stanzaIDElement.attributeValue("by"));
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) Test(org.junit.Test)

Example 30 with Packet

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

the class StanzaIDUtilTest method testOverwriteStanzaIDElement.

/**
 * Test if {@link StanzaIDUtil#ensureUniqueAndStableStanzaID(Packet, JID)} overwrites a stanza-id
 * element when another is present with the same 'by' value.
 */
@Test
public void testOverwriteStanzaIDElement() throws Exception {
    // Setup fixture.
    final Packet input = new Message();
    final JID self = new JID("foobar");
    final String notExpected = "de305d54-75b4-431b-adb2-eb6b9e546013";
    final Element toOverwrite = input.getElement().addElement("stanza-id", "urn:xmpp:sid:0");
    toOverwrite.addAttribute("by", self.toString());
    toOverwrite.addAttribute("id", notExpected);
    // Execute system under test.
    final Packet result = StanzaIDUtil.ensureUniqueAndStableStanzaID(input, self);
    // Verify results.
    Assert.assertNotNull(result);
    final Element stanzaIDElement = result.getElement().element(QName.get("stanza-id", "urn:xmpp:sid:0"));
    Assert.assertNotNull(stanzaIDElement);
    try {
        UUID.fromString(stanzaIDElement.attributeValue("id"));
    } catch (IllegalArgumentException ex) {
        Assert.fail();
    }
    Assert.assertNotEquals(notExpected, stanzaIDElement.attributeValue("id"));
    assertEquals(self.toString(), stanzaIDElement.attributeValue("by"));
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) Test(org.junit.Test)

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