PrivacyItem.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.privacy.packet;

  18. import java.util.Objects;

  19. import org.jivesoftware.smack.datatypes.UInt32;

  20. /**
  21.  * A privacy item acts a rule that when matched defines if a stanza should be blocked or not.
  22.  *
  23.  * Privacy Items can handle different kind of blocking communications based on JID, group,
  24.  * subscription type or globally by:<ul>
  25.  * <li>Allowing or blocking messages.
  26.  * <li>Allowing or blocking inbound presence notifications.
  27.  * <li>Allowing or blocking outbound presence notifications.
  28.  * <li>Allowing or blocking IQ stanzas.
  29.  * <li>Allowing or blocking all communications.
  30.  * </ul>
  31.  * @author Francisco Vives
  32.  */
  33. public class PrivacyItem {
  34.     /**
  35.      * Value for subscription type rules.
  36.      */
  37.     public static final String SUBSCRIPTION_BOTH = "both";
  38.     public static final String SUBSCRIPTION_TO = "to";
  39.     public static final String SUBSCRIPTION_FROM = "from";
  40.     public static final String SUBSCRIPTION_NONE = "none";

  41.     /** allow is the action associated with the item, it can allow or deny the communication. */
  42.     private final boolean allow;

  43.     /**
  44.      * order is a unsigned 32-bit integer that is unique among all items in the list.
  45.      **/
  46.     private final UInt32 order;

  47.     /**
  48.      * Type defines if the rule is based on JIDs, roster groups or presence subscription types.
  49.      * Available values are: [jid|group|subscription]
  50.      */
  51.     private final Type type;

  52.     /**
  53.      * The value hold the element identifier to apply the action. If the type is "jid", then the
  54.      * 'value' attribute MUST contain a valid Jabber ID. If the type is "group", then the
  55.      * 'value' attribute SHOULD contain the name of a group in the user's roster. If the type is
  56.      * "subscription", then the 'value' attribute MUST be one of "both", "to", "from", or
  57.      * "none".
  58.      */
  59.     private final String value;

  60.     /** blocks incoming IQ stanzas. */
  61.     private boolean filterIQ = false;
  62.     /** filterMessage blocks incoming message stanzas. */
  63.     private boolean filterMessage = false;
  64.     /** blocks incoming presence notifications. */
  65.     private boolean filterPresenceIn = false;
  66.     /** blocks outgoing presence notifications. */
  67.     private boolean filterPresenceOut = false;

  68.     public PrivacyItem(boolean allow, long order) {
  69.         this(null, null, allow, UInt32.from(order));
  70.     }

  71.     /**
  72.      * Creates a new fall-through privacy item.
  73.      *
  74.      * This is usually the last item in a privacy list and has no 'type' attribute.
  75.      *
  76.      * @param allow true if this is an allow item
  77.      * @param order the order of this privacy item
  78.      */
  79.     public PrivacyItem(boolean allow, UInt32 order) {
  80.         this(null, null, allow, order);
  81.     }

  82.     public PrivacyItem(Type type, String value, boolean allow, long order) {
  83.         this(type, value, allow, UInt32.from(order));
  84.     }

  85.     /**
  86.      * Creates a new privacy item.
  87.      *
  88.      * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
  89.      * If the type is "group", then the 'value' attribute SHOULD contain the name of a group
  90.      * in the user's roster.
  91.      * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
  92.      * "from", or "none".
  93.      *
  94.      * @param type the type.
  95.      * @param value the value of the privacy item
  96.      * @param allow true if this is an allow item
  97.      * @param order the order of this privacy item
  98.      */
  99.     public PrivacyItem(Type type, String value, boolean allow, UInt32 order) {
  100.         this.type = type;
  101.         this.value = value;
  102.         this.allow = allow;
  103.         this.order = Objects.requireNonNull(order);
  104.     }

  105.     /**
  106.      * Creates a new privacy item.
  107.      *
  108.      * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
  109.      * If the type is "group", then the 'value' attribute SHOULD contain the name of a group
  110.      * in the user's roster.
  111.      * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
  112.      * "from", or "none".
  113.      *
  114.      * @param type the type.
  115.      * @param value the value of the privacy item
  116.      * @param allow true if this is an allow item
  117.      * @param order the order of this privacy item
  118.      */
  119.     public PrivacyItem(Type type, CharSequence value, boolean allow, long order) {
  120.         this(type, value != null ? value.toString() : null, allow, order);
  121.     }

  122.     /**
  123.      * Returns the action associated with the item, it MUST be filled and will allow or deny
  124.      * the communication.
  125.      *
  126.      * @return the allow communication status.
  127.      */
  128.     public boolean isAllow() {
  129.         return allow;
  130.     }

  131.     /**
  132.      * Returns whether the receiver allow or deny incoming IQ stanzas or not.
  133.      *
  134.      * @return the iq filtering status.
  135.      */
  136.     public boolean isFilterIQ() {
  137.         return filterIQ;
  138.     }

  139.     /**
  140.      * Sets whether the receiver allows or denies incoming IQ stanzas or not.
  141.      *
  142.      * @param filterIQ indicates if the receiver allows or denies incoming IQ stanzas.
  143.      */
  144.     public void setFilterIQ(boolean filterIQ) {
  145.         this.filterIQ = filterIQ;
  146.     }

  147.     /**
  148.      * Returns whether the receiver allows or denies incoming messages or not.
  149.      *
  150.      * @return the message filtering status.
  151.      */
  152.     public boolean isFilterMessage() {
  153.         return filterMessage;
  154.     }

  155.     /**
  156.      * Sets whether the receiver allows or denies incoming messages or not.
  157.      *
  158.      * @param filterMessage indicates if the receiver allows or denies incoming messages or not.
  159.      */
  160.     public void setFilterMessage(boolean filterMessage) {
  161.         this.filterMessage = filterMessage;
  162.     }

  163.     /**
  164.      * Returns whether the receiver allows or denies incoming presence or not.
  165.      *
  166.      * @return the iq filtering incoming presence status.
  167.      */
  168.     public boolean isFilterPresenceIn() {
  169.         return filterPresenceIn;
  170.     }

  171.     /**
  172.      * Sets whether the receiver allows or denies incoming presence or not.
  173.      *
  174.      * @param filterPresenceIn indicates if the receiver allows or denies filtering incoming presence.
  175.      */
  176.     public void setFilterPresenceIn(boolean filterPresenceIn) {
  177.         this.filterPresenceIn = filterPresenceIn;
  178.     }

  179.     /**
  180.      * Returns whether the receiver allows or denies incoming presence or not.
  181.      *
  182.      * @return the iq filtering incoming presence status.
  183.      */
  184.     public boolean isFilterPresenceOut() {
  185.         return filterPresenceOut;
  186.     }

  187.     /**
  188.      * Sets whether the receiver allows or denies outgoing presence or not.
  189.      *
  190.      * @param filterPresenceOut indicates if the receiver allows or denies filtering outgoing presence
  191.      */
  192.     public void setFilterPresenceOut(boolean filterPresenceOut) {
  193.         this.filterPresenceOut = filterPresenceOut;
  194.     }

  195.     /**
  196.      * Returns the order where the receiver is processed. List items are processed in
  197.      * ascending order.
  198.      *
  199.      * The order MUST be filled and its value MUST be a non-negative integer
  200.      * that is unique among all items in the list.
  201.      *
  202.      * @return the order number.
  203.      */
  204.     public UInt32 getOrder() {
  205.         return order;
  206.     }

  207.     /**
  208.      * Returns the type hold the kind of communication it will allow or block.
  209.      * It MUST be filled with one of these values: jid, group or subscription.
  210.      *
  211.      * @return the type of communication it represent.
  212.      */
  213.     public Type getType() {
  214.         return type;
  215.     }

  216.     /**
  217.      * Returns the element identifier to apply the action.
  218.      *
  219.      * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
  220.      * If the type is "group", then the 'value' attribute SHOULD contain the name of a group
  221.      * in the user's roster.
  222.      * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
  223.      * "from", or "none".
  224.      *
  225.      * @return the identifier to apply the action.
  226.      */
  227.     public String getValue() {
  228.         return value;
  229.     }

  230.     /**
  231.      * Returns whether the receiver allows or denies every kind of communication.
  232.      *
  233.      * When filterIQ, filterMessage, filterPresenceIn and filterPresenceOut are not set
  234.      * the receiver will block all communications.
  235.      *
  236.      * @return the all communications status.
  237.      */
  238.     public boolean isFilterEverything() {
  239.         return !(this.isFilterIQ() || this.isFilterMessage() || this.isFilterPresenceIn()
  240.                 || this.isFilterPresenceOut());
  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.      * Type defines if the rule is based on JIDs, roster groups or presence subscription types.
  284.      */
  285.     public enum Type {
  286.         /**
  287.          * JID being analyzed should belong to a roster group of the list's owner.
  288.          */
  289.         group,
  290.         /**
  291.          * JID being analyzed should have a resource match, domain match or bare JID match.
  292.          */
  293.         jid,
  294.         /**
  295.          * JID being analyzed should belong to a contact present in the owner's roster with the
  296.          * specified subscription status.
  297.          */
  298.         subscription
  299.     }
  300. }