001/** 002 * 003 * Copyright 2018 Miguel Hincapie. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.chat_markers.filter; 018 019import org.jivesoftware.smack.filter.StanzaExtensionFilter; 020import org.jivesoftware.smack.packet.ExtensionElement; 021import org.jivesoftware.smack.packet.Stanza; 022import org.jivesoftware.smackx.chatstates.ChatState; 023import org.jivesoftware.smackx.chatstates.ChatStateManager; 024 025/** 026 * Chat Markers Manager class (XEP-0333). 027 * 028 * @author Miguel Hincapie 029 * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat 030 * Markers</a> 031 */ 032public final class EligibleForChatMarkerFilter extends StanzaExtensionFilter { 033 034 public static final EligibleForChatMarkerFilter INSTANCE = new EligibleForChatMarkerFilter(ChatStateManager.NAMESPACE); 035 036 private EligibleForChatMarkerFilter(String namespace) { 037 super(namespace); 038 } 039 040 /** 041 * From XEP-0333, Protocol Format: The Chat Marker MUST have an 'id' which is the 'id' of the 042 * message being marked.<br> 043 * In order to make Chat Markers works together with XEP-0085 as it said in 044 * 8.5 Interaction with Chat States, only messages with <tt>active</tt> chat 045 * state are accepted. 046 * 047 * @param message to be analyzed. 048 * @return true if the message contains a stanza Id. 049 * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat Markers</a> 050 */ 051 @Override 052 public boolean accept(Stanza message) { 053 if (!message.hasStanzaIdSet()) { 054 return false; 055 } 056 057 if (super.accept(message)) { 058 ExtensionElement extension = message.getExtension(ChatStateManager.NAMESPACE); 059 String chatStateElementName = extension.getElementName(); 060 061 ChatState state; 062 try { 063 state = ChatState.valueOf(chatStateElementName); 064 return (state == ChatState.active); 065 } 066 catch (Exception ex) { 067 return false; 068 } 069 } 070 071 return true; 072 } 073}