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 org.jivesoftware.smack.util.NumberUtil;

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

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

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

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

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

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

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

  78.     /**
  79.      * Creates a new privacy item.
  80.      *
  81.      * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
  82.      * If the type is "group", then the 'value' attribute SHOULD contain the name of a group
  83.      * in the user's roster.
  84.      * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
  85.      * "from", or "none".
  86.      *
  87.      * @param type the type.
  88.      * @param value the value of the privacy item
  89.      * @param allow true if this is an allow item
  90.      * @param order the order of this privacy item
  91.      */
  92.     public PrivacyItem(Type type, String value, boolean allow, long order) {
  93.         NumberUtil.checkIfInUInt32Range(order);
  94.         this.type = type;
  95.         this.value = value;
  96.         this.allow = allow;
  97.         this.order = order;
  98.     }

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

  116.     /**
  117.      * Returns the action associated with the item, it MUST be filled and will allow or deny
  118.      * the communication.
  119.      *
  120.      * @return the allow communication status.
  121.      */
  122.     public boolean isAllow() {
  123.         return allow;
  124.     }

  125.     /**
  126.      * Returns whether the receiver allow or deny incoming IQ stanzas or not.
  127.      *
  128.      * @return the iq filtering status.
  129.      */
  130.     public boolean isFilterIQ() {
  131.         return filterIQ;
  132.     }

  133.     /**
  134.      * Sets whether the receiver allows or denies incoming IQ stanzas or not.
  135.      *
  136.      * @param filterIQ indicates if the receiver allows or denies incoming IQ stanzas.
  137.      */
  138.     public void setFilterIQ(boolean filterIQ) {
  139.         this.filterIQ = filterIQ;
  140.     }

  141.     /**
  142.      * Returns whether the receiver allows or denies incoming messages or not.
  143.      *
  144.      * @return the message filtering status.
  145.      */
  146.     public boolean isFilterMessage() {
  147.         return filterMessage;
  148.     }

  149.     /**
  150.      * Sets wheather the receiver allows or denies incoming messages or not.
  151.      *
  152.      * @param filterMessage indicates if the receiver allows or denies incoming messages or not.
  153.      */
  154.     public void setFilterMessage(boolean filterMessage) {
  155.         this.filterMessage = filterMessage;
  156.     }

  157.     /**
  158.      * Returns whether the receiver allows or denies incoming presence or not.
  159.      *
  160.      * @return the iq filtering incoming presence status.
  161.      */
  162.     public boolean isFilterPresenceIn() {
  163.         return filterPresenceIn;
  164.     }

  165.     /**
  166.      * Sets whether the receiver allows or denies incoming presence or not.
  167.      *
  168.      * @param filterPresenceIn indicates if the receiver allows or denies filtering incoming presence.
  169.      */
  170.     public void setFilterPresenceIn(boolean filterPresenceIn) {
  171.         this.filterPresenceIn = filterPresenceIn;
  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.      * Sets whether the receiver allows or denies outgoing presence or not.
  183.      *
  184.      * @param filterPresenceOut indicates if the receiver allows or denies filtering outgoing presence
  185.      */
  186.     public void setFilterPresenceOut(boolean filterPresenceOut) {
  187.         this.filterPresenceOut = filterPresenceOut;
  188.     }

  189.     /**
  190.      * Returns the order where the receiver is processed. List items are processed in
  191.      * ascending order.
  192.      *
  193.      * The order MUST be filled and its value MUST be a non-negative integer
  194.      * that is unique among all items in the list.
  195.      *
  196.      * @return the order number.
  197.      */
  198.     public long getOrder() {
  199.         return order;
  200.     }

  201.     /**
  202.      * Returns the type hold the kind of communication it will allow or block.
  203.      * It MUST be filled with one of these values: jid, group or subscription.
  204.      *
  205.      * @return the type of communication it represent.
  206.      */
  207.     public Type getType() {
  208.         return type;
  209.     }

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

  224.     /**
  225.      * Returns whether the receiver allows or denies every kind of communication.
  226.      *
  227.      * When filterIQ, filterMessage, filterPresenceIn and filterPresenceOut are not set
  228.      * the receiver will block all communications.
  229.      *
  230.      * @return the all communications status.
  231.      */
  232.     public boolean isFilterEverything() {
  233.         return !(this.isFilterIQ() || this.isFilterMessage() || this.isFilterPresenceIn()
  234.                 || this.isFilterPresenceOut());
  235.     }

  236.     /**
  237.      * Answer an xml representation of the receiver according to the RFC 3921.
  238.      *
  239.      * @return the text xml representation.
  240.      */
  241.     public String toXML() {
  242.         StringBuilder buf = new StringBuilder();
  243.         buf.append("<item");
  244.         if (this.isAllow()) {
  245.             buf.append(" action=\"allow\"");
  246.         } else {
  247.             buf.append(" action=\"deny\"");
  248.         }
  249.         buf.append(" order=\"").append(getOrder()).append("\"");
  250.         if (getType() != null) {
  251.             buf.append(" type=\"").append(getType()).append("\"");
  252.         }
  253.         if (getValue() != null) {
  254.             buf.append(" value=\"").append(getValue()).append("\"");
  255.         }
  256.         if (isFilterEverything()) {
  257.             buf.append("/>");
  258.         } else {
  259.             buf.append(">");
  260.             if (this.isFilterIQ()) {
  261.                 buf.append("<iq/>");
  262.             }
  263.             if (this.isFilterMessage()) {
  264.                 buf.append("<message/>");
  265.             }
  266.             if (this.isFilterPresenceIn()) {
  267.                 buf.append("<presence-in/>");
  268.             }
  269.             if (this.isFilterPresenceOut()) {
  270.                 buf.append("<presence-out/>");
  271.             }
  272.             buf.append("</item>");
  273.         }
  274.         return buf.toString();
  275.     }

  276.     /**
  277.      * Type defines if the rule is based on JIDs, roster groups or presence subscription types.
  278.      */
  279.     public static enum Type {
  280.         /**
  281.          * JID being analyzed should belong to a roster group of the list's owner.
  282.          */
  283.         group,
  284.         /**
  285.          * JID being analyzed should have a resource match, domain match or bare JID match.
  286.          */
  287.         jid,
  288.         /**
  289.          * JID being analyzed should belong to a contact present in the owner's roster with the
  290.          * specified subscription status.
  291.          */
  292.         subscription
  293.     }
  294. }