Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ashward
Created July 18, 2013 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashward/6029790 to your computer and use it in GitHub Desktop.
Save ashward/6029790 to your computer and use it in GitHub Desktop.
OF-633: Current OfflineMessageStore logic discards valid MUC invites This code actually implements the recommendations from XEP-0160 into the decision as to whether or not to store a message offline. Also includes a message hint from XEP-0334
Index: src/java/org/jivesoftware/openfire/OfflineMessageStore.java
===================================================================
--- src/java/org/jivesoftware/openfire/OfflineMessageStore.java (revision 13713)
+++ src/java/org/jivesoftware/openfire/OfflineMessageStore.java (working copy)
@@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
@@ -36,6 +37,7 @@
import org.dom4j.DocumentException;
import org.dom4j.Element;
+import org.dom4j.QName;
import org.dom4j.io.SAXReader;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.database.SequenceManager;
@@ -126,14 +128,11 @@
if (message == null) {
return;
}
- // ignore empty bodied message (typically chat-state notifications).
- if (message.getBody() == null || message.getBody().length() == 0) {
- // allow empty pubsub messages (OF-191)
- if (message.getChildElement("event", "http://jabber.org/protocol/pubsub#event") == null)
- {
- return;
- }
+
+ if(!shouldStoreMessage(message)) {
+ return;
}
+
JID recipient = message.getTo();
String username = recipient.getNode();
// If the username is null (such as when an anonymous user), don't store.
@@ -470,4 +469,63 @@
// Remove this module as a user event listener
UserEventDispatcher.removeListener(this);
}
+
+ /**
+ * Decide whether a message should be stored offline according to XEP-0160 and XEP-0334.
+ *
+ * @param message
+ * @return <code>true</code> if the message should be stored offline, <code>false</code> otherwise.
+ */
+ private boolean shouldStoreMessage(final Message message) {
+ // XEP-0334: Implement the <no-store/> hint to override offline storage
+ if(message.getChildElement("no-store", "urn:xmpp:hints") != null) {
+ return false;
+ }
+
+ switch(message.getType()) {
+ case chat:
+ // XEP-0160: "chat" message types SHOULD be stored offline unless they only contain chat state notifications
+
+ // Iterate through the child elements to see if we can find anything that's not a chat state notification or
+ // real time text notification
+ Iterator<?> it = message.getElement().elementIterator();
+
+ while(it.hasNext()) {
+ Object item = it.next();
+
+ if(item instanceof Element) {
+ Element el = (Element) item;
+
+ if(
+ !el.getNamespaceURI().equals("http://jabber.org/protocol/chatstates")
+ && !(el.getQName().equals(QName.get("rtt", "urn:xmpp:rtt:0")))
+ ) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+
+ case groupchat:
+ case headline:
+ // XEP-0160: "groupchat" message types SHOULD NOT be stored offline
+ // XEP-0160: "headline" message types SHOULD NOT be stored offline
+ return false;
+
+ case error:
+ // XEP-0160: "error" message types SHOULD NOT be stored offline,
+ // although a server MAY store advanced message processing errors offline
+ if (message.getChildElement("amp", "http://jabber.org/protocol/amp") == null) {
+ return false;
+ }
+ break;
+
+ default:
+ // XEP-0160: "normal" message types or messages without a type SHOULD be stored offline
+ break;
+ }
+
+ return true;
+ }
}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment