Search in sources :

Example 41 with Message

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

the class ContentFilterTest method testFilterMessageBody.

@Test
public void testFilterMessageBody() {
    // filter on the word fox
    filter.setPatterns("fox");
    // test message
    Message message = new Message();
    message.setBody("the quick brown fox jumped over the lazy dog");
    boolean matched = filter.filter(message);
    // matches should be found
    assertTrue(matched);
    // content has not changed as there is no content mask
    assertEquals("the quick brown fox jumped over the lazy dog", message.getBody());
    assertNull(message.getSubject());
}
Also used : Message(org.xmpp.packet.Message) Test(org.junit.Test)

Example 42 with Message

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

the class OfflineMessageStrategy method bounce.

private void bounce(Message message) {
    // Do nothing if the sender was the server itself
    if (message.getFrom() == null || message.getFrom().equals(serverAddress)) {
        return;
    }
    try {
        // Generate a rejection response to the sender
        Message errorResponse = message.createCopy();
        // return an error stanza to the sender, which SHOULD be <service-unavailable/>
        errorResponse.setError(PacketError.Condition.service_unavailable);
        errorResponse.setFrom(message.getTo());
        errorResponse.setTo(message.getFrom());
        // Send the response
        router.route(errorResponse);
        // Inform listeners that an offline message was bounced
        if (!listeners.isEmpty()) {
            for (OfflineMessageListener listener : listeners) {
                listener.messageBounced(message);
            }
        }
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : Message(org.xmpp.packet.Message)

Example 43 with Message

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

the class OfflineMessageStrategy method storeOffline.

public void storeOffline(Message message) {
    if (message != null) {
        // Do nothing if the message was sent to the server itself, an anonymous user or a non-existent user
        // Also ignore message carbons
        JID recipientJID = message.getTo();
        if (recipientJID == null || serverAddress.equals(recipientJID) || recipientJID.getNode() == null || message.getExtension("received", "urn:xmpp:carbons:2") != null || !UserManager.getInstance().isRegisteredUser(recipientJID.getNode())) {
            return;
        }
        // Do not store messages if communication is blocked
        PrivacyList list = PrivacyListManager.getInstance().getDefaultPrivacyList(recipientJID.getNode());
        if (list != null && list.shouldBlockPacket(message)) {
            Message result = message.createCopy();
            result.setTo(message.getFrom());
            result.setFrom(message.getTo());
            result.setError(PacketError.Condition.service_unavailable);
            XMPPServer.getInstance().getRoutingTable().routePacket(message.getFrom(), result, true);
            return;
        }
        // 8.5.2.2.  No Available or Connected Resources
        if (recipientJID.getResource() == null) {
            if (message.getType() == Message.Type.headline || message.getType() == Message.Type.error) {
                // For a message stanza of type "headline" or "error", the server MUST silently ignore the message.
                return;
            } else // // For a message stanza of type "groupchat", the server MUST return an error to the sender, which SHOULD be <service-unavailable/>.
            if (message.getType() == Message.Type.groupchat) {
                bounce(message);
                return;
            }
        } else {
            // or (b) return an error stanza to the sender, which SHOULD be <service-unavailable/>.
            if (message.getType() == Message.Type.normal || message.getType() == Message.Type.groupchat || message.getType() == Message.Type.headline) {
                // Depending on the OfflineMessageStragey, we may silently ignore or bounce
                if (type == Type.bounce) {
                    bounce(message);
                }
                // Either bounce or silently ignore, never store such messages
                return;
            } else // For a message stanza of type "error", the server MUST silently ignore the stanza.
            if (message.getType() == Message.Type.error) {
                return;
            }
        }
        switch(type) {
            case bounce:
                bounce(message);
                break;
            case store:
                store(message);
                break;
            case store_and_bounce:
                if (underQuota(message)) {
                    store(message);
                } else {
                    Log.debug("Unable to store, as user is over storage quota. Bouncing message instead: " + message.toXML());
                    bounce(message);
                }
                break;
            case store_and_drop:
                if (underQuota(message)) {
                    store(message);
                } else {
                    Log.debug("Unable to store, as user is over storage quota. Silently dropping message: " + message.toXML());
                }
                break;
            case drop:
                // Drop essentially means silently ignore/do nothing
                break;
        }
    }
}
Also used : JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) PrivacyList(org.jivesoftware.openfire.privacy.PrivacyList)

Example 44 with Message

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

the class SessionManager method createServerMessage.

private Message createServerMessage(String subject, String body) {
    Message message = new Message();
    message.setFrom(serverAddress);
    if (subject != null) {
        message.setSubject(subject);
    }
    message.setBody(body);
    return message;
}
Also used : Message(org.xmpp.packet.Message)

Example 45 with Message

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

the class MUCRoomHistory method addOldMessage.

/**
     * Creates a new message and adds it to the history. The new message will be created based on
     * the provided information. This information will likely come from the database when loading
     * the room history from the database.
     *
     * @param senderJID the sender's JID of the message to add to the history.
     * @param nickname the sender's nickname of the message to add to the history.
     * @param sentDate the date when the message was sent to the room.
     * @param subject the subject included in the message.
     * @param body the body of the message.
     */
public void addOldMessage(String senderJID, String nickname, Date sentDate, String subject, String body, String stanza) {
    Message message = new Message();
    message.setType(Message.Type.groupchat);
    if (stanza != null) {
        // payload initialized as XML string from DB
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        try {
            Element element = xmlReader.read(new StringReader(stanza)).getRootElement();
            for (Element child : (List<Element>) element.elements()) {
                Namespace ns = child.getNamespace();
                if (ns == null || ns.getURI().equals("jabber:client") || ns.getURI().equals("jabber:server")) {
                    continue;
                }
                Element added = message.addChildElement(child.getName(), child.getNamespaceURI());
                if (!child.getText().isEmpty()) {
                    added.setText(child.getText());
                }
                for (Attribute attr : (List<Attribute>) child.attributes()) {
                    added.addAttribute(attr.getQName(), attr.getValue());
                }
                for (Element el : (List<Element>) child.elements()) {
                    added.add(el.createCopy());
                }
            }
            if (element.attribute("id") != null) {
                message.setID(element.attributeValue("id"));
            }
        } catch (Exception ex) {
            Log.error("Failed to parse payload XML", ex);
        }
    }
    message.setSubject(subject);
    message.setBody(body);
    // Set the sender of the message
    if (nickname != null && nickname.trim().length() > 0) {
        JID roomJID = room.getRole().getRoleAddress();
        // Recreate the sender address based on the nickname and room's JID
        message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(), nickname, true));
    } else {
        // Set the room as the sender of the message
        message.setFrom(room.getRole().getRoleAddress());
    }
    // Add the delay information to the message
    Element delayInformation = message.addChildElement("delay", "urn:xmpp:delay");
    delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(sentDate));
    if (room.canAnyoneDiscoverJID()) {
        // Set the Full JID as the "from" attribute
        delayInformation.addAttribute("from", senderJID);
    } else {
        // Set the Room JID as the "from" attribute
        delayInformation.addAttribute("from", room.getRole().getRoleAddress().toString());
    }
    historyStrategy.addMessage(message);
}
Also used : Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) List(java.util.List) Namespace(org.dom4j.Namespace) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Aggregations

Message (org.xmpp.packet.Message)111 Element (org.dom4j.Element)35 JID (org.xmpp.packet.JID)25 Test (org.junit.Test)23 Presence (org.xmpp.packet.Presence)18 IQ (org.xmpp.packet.IQ)16 ArrayList (java.util.ArrayList)10 Packet (org.xmpp.packet.Packet)10 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)9 Date (java.util.Date)6 ClientSession (org.jivesoftware.openfire.session.ClientSession)6 NotFoundException (org.jivesoftware.util.NotFoundException)6 StringReader (java.io.StringReader)4 List (java.util.List)4 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)4 User (org.jivesoftware.openfire.user.User)4 IOException (java.io.IOException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 SAXReader (org.dom4j.io.SAXReader)3 DefaultElement (org.dom4j.tree.DefaultElement)3