001/**
002 *
003 * Copyright the original author or authors
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.privacy.packet;
018
019import org.jivesoftware.smack.util.NumberUtil;
020
021/**
022 * A privacy item acts a rule that when matched defines if a stanza(/packet) should be blocked or not.
023 *
024 * Privacy Items can handle different kind of blocking communications based on JID, group,
025 * subscription type or globally by:<ul>
026 * <li>Allowing or blocking messages.
027 * <li>Allowing or blocking inbound presence notifications.
028 * <li>Allowing or blocking outbound presence notifications.
029 * <li>Allowing or blocking IQ stanzas.
030 * <li>Allowing or blocking all communications.
031 * </ul>
032 * @author Francisco Vives
033 */
034public class PrivacyItem {
035    /**
036     * Value for subscription type rules.
037     */
038    public static final String SUBSCRIPTION_BOTH = "both";
039    public static final String SUBSCRIPTION_TO = "to";
040    public static final String SUBSCRIPTION_FROM = "from";
041    public static final String SUBSCRIPTION_NONE = "none";
042
043    /** allow is the action associated with the item, it can allow or deny the communication. */
044    private final boolean allow;
045
046    /**
047     * order is a unsigned 32-bit integer that is unique among all items in the list.
048     **/
049    private final long order;
050
051    /**
052     * Type defines if the rule is based on JIDs, roster groups or presence subscription types.
053     * Available values are: [jid|group|subscription]
054     */
055    private final Type type;
056
057    /**
058     * The value hold the element identifier to apply the action. If the type is "jid", then the
059     * 'value' attribute MUST contain a valid Jabber ID. If the type is "group", then the
060     * 'value' attribute SHOULD contain the name of a group in the user's roster. If the type is
061     * "subscription", then the 'value' attribute MUST be one of "both", "to", "from", or
062     * "none".
063     */
064    private final String value;
065
066    /** blocks incoming IQ stanzas. */
067    private boolean filterIQ = false;
068    /** filterMessage blocks incoming message stanzas. */
069    private boolean filterMessage = false;
070    /** blocks incoming presence notifications. */
071    private boolean filterPresenceIn = false;
072    /** blocks outgoing presence notifications. */
073    private boolean filterPresenceOut = false;
074
075    /**
076     * Creates a new fall-through privacy item.
077     *
078     * This is usually the last item in a privacy list and has no 'type' attribute.
079     *
080     * @param allow true if this is an allow item
081     * @param order the order of this privacy item
082     */
083    public PrivacyItem(boolean allow, long order) {
084        this(null, null, allow, order);
085    }
086
087    /**
088     * Creates a new privacy item.
089     *
090     * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
091     * If the type is "group", then the 'value' attribute SHOULD contain the name of a group
092     * in the user's roster.
093     * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
094     * "from", or "none".
095     *
096     * @param type the type.
097     * @param value the value of the privacy item
098     * @param allow true if this is an allow item
099     * @param order the order of this privacy item
100     */
101    public PrivacyItem(Type type, String value, boolean allow, long order) {
102        NumberUtil.checkIfInUInt32Range(order);
103        this.type = type;
104        this.value = value;
105        this.allow = allow;
106        this.order = order;
107    }
108
109    /**
110     * Returns the action associated with the item, it MUST be filled and will allow or deny
111     * the communication.
112     *
113     * @return the allow communication status.
114     */
115    public boolean isAllow() {
116                return allow;
117        }
118
119    /**
120     * Returns whether the receiver allow or deny incoming IQ stanzas or not.
121     *
122     * @return the iq filtering status.
123     */
124    public boolean isFilterIQ() {
125                return filterIQ;
126        }
127
128    /**
129     * Sets whether the receiver allows or denies incoming IQ stanzas or not.
130     *
131     * @param filterIQ indicates if the receiver allows or denies incoming IQ stanzas.
132     */
133    public void setFilterIQ(boolean filterIQ) {
134                this.filterIQ = filterIQ;
135        }
136
137    /**
138     * Returns whether the receiver allows or denies incoming messages or not.
139     *
140     * @return the message filtering status.
141     */
142    public boolean isFilterMessage() {
143                return filterMessage;
144        }
145
146    /**
147     * Sets wheather the receiver allows or denies incoming messages or not.
148     *
149     * @param filterMessage indicates if the receiver allows or denies incoming messages or not.
150     */
151    public void setFilterMessage(boolean filterMessage) {
152                this.filterMessage = filterMessage;
153        }
154
155    /**
156     * Returns whether the receiver allows or denies incoming presence or not.
157     *
158     * @return the iq filtering incoming presence status.
159     */
160    public boolean isFilterPresenceIn() {
161                return filterPresenceIn;
162        }
163
164    /**
165     * Sets whether the receiver allows or denies incoming presence or not.
166     *
167     * @param filterPresenceIn indicates if the receiver allows or denies filtering incoming presence.
168     */
169    public void setFilterPresenceIn(boolean filterPresenceIn) {
170                this.filterPresenceIn = filterPresenceIn;
171        }
172
173    /**
174     * Returns whether the receiver allows or denies incoming presence or not.
175     *
176     * @return the iq filtering incoming presence status.
177     */
178    public boolean isFilterPresenceOut() {
179                return filterPresenceOut;
180        }
181
182    /**
183     * Sets whether the receiver allows or denies outgoing presence or not.
184     *
185     * @param filterPresenceOut indicates if the receiver allows or denies filtering outgoing presence
186     */
187    public void setFilterPresenceOut(boolean filterPresenceOut) {
188                this.filterPresenceOut = filterPresenceOut;
189        }
190
191    /**
192     * Returns the order where the receiver is processed. List items are processed in
193     * ascending order.
194     *
195     * The order MUST be filled and its value MUST be a non-negative integer
196     * that is unique among all items in the list.
197     *
198     * @return the order number.
199     */
200    public long getOrder() {
201                return order;
202        }
203
204    /**
205     * Returns the type hold the kind of communication it will allow or block.
206     * It MUST be filled with one of these values: jid, group or subscription.
207     *
208     * @return the type of communication it represent.
209     */
210    public Type getType() {
211        return type;
212        }
213
214    /**
215     * Returns the element identifier to apply the action.
216     *
217     * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
218     * If the type is "group", then the 'value' attribute SHOULD contain the name of a group
219     * in the user's roster.
220     * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
221     * "from", or "none".
222     *
223     * @return the identifier to apply the action.
224     */
225    public String getValue() {
226        return value;
227        }
228
229    /**
230     * Returns whether the receiver allows or denies every kind of communication.
231     *
232     * When filterIQ, filterMessage, filterPresenceIn and filterPresenceOut are not set
233     * the receiver will block all communications.
234     *
235     * @return the all communications status.
236     */
237    public boolean isFilterEverything() {
238                return !(this.isFilterIQ() || this.isFilterMessage() || this.isFilterPresenceIn()
239                                || this.isFilterPresenceOut());
240        }
241
242        /**
243         * Answer an xml representation of the receiver according to the RFC 3921.
244         *
245         * @return the text xml representation.
246     */
247    public String toXML() {
248        StringBuilder buf = new StringBuilder();
249        buf.append("<item");
250        if (this.isAllow()) {
251                buf.append(" action=\"allow\"");
252        } else {
253                buf.append(" action=\"deny\"");
254        }
255        buf.append(" order=\"").append(getOrder()).append("\"");
256        if (getType() != null) {
257            buf.append(" type=\"").append(getType()).append("\"");
258        }
259        if (getValue() != null) {
260            buf.append(" value=\"").append(getValue()).append("\"");
261        }
262        if (isFilterEverything()) {
263                buf.append("/>");
264        } else {
265                buf.append(">");
266                if (this.isFilterIQ()) {
267                buf.append("<iq/>");
268            }
269                if (this.isFilterMessage()) {
270                buf.append("<message/>");
271            }
272                if (this.isFilterPresenceIn()) {
273                buf.append("<presence-in/>");
274            }
275                if (this.isFilterPresenceOut()) {
276                buf.append("<presence-out/>");
277            }
278                buf.append("</item>");
279        }
280        return buf.toString();
281    }
282
283    /**
284     * Type defines if the rule is based on JIDs, roster groups or presence subscription types.
285     */
286    public static enum Type {
287        /**
288         * JID being analyzed should belong to a roster group of the list's owner.
289         */
290        group,
291        /**
292         * JID being analyzed should have a resource match, domain match or bare JID match.
293         */
294        jid,
295        /**
296         * JID being analyzed should belong to a contact present in the owner's roster with the
297         * specified subscription status.
298         */
299        subscription
300    }
301}