package org.jivesoftware.smack.packet; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.List; import java.util.Set; /** * A Privacy IQ Packet, is used by the {@see PrivacyListManager} and {@see PrivacyProvider} to allow * and block communications from other users. It contains the appropriate structure to suit * user-defined privacy lists. Different configured Privacy packages are used in the Server – * Manager communication in order to: * * Privacy Items can handle different kind of blocking communications based on JID, group, * subscription type or globally {@see PrivacyItem} * * @author Francisco Vives */ public class Privacy extends IQ { /** declineActiveList is true when the user declines the use of the active list **/ private boolean declineActiveList=false; /** activeName is the name associated with the active list set for the session **/ private String activeName; /** declineDefaultList is true when the user declines the use of the default list **/ private boolean declineDefaultList=false; /** defaultName is the name of the default list that applies to the user as a whole **/ private String defaultName; /** itemLists holds the set of privacy items classified in lists. It is a map where the * key is the name of the list and the value a collection with privacy items. **/ private Map itemLists = new HashMap(); /** * Set or update a privacy list with {@link PrivacyItem}. * * @param listName the name of the new privacy list. * @param listItem the {@link PrivacyItem} that rules the list. * @return the privacy List. */ public List setPrivacyList(String listName, List listItem) { // Add new list to the itemLists this.getItemLists().put(listName, listItem); return listItem; } /** * Set the active list based on the default list. * * @return the active List. */ public List setActivePrivacyList() { this.setActiveName(this.getDefaultName()); return (List) this.getItemLists().get(this.getActiveName()); } /** * Deletes an existing privacy list. If the privacy list being deleted was the default list * then the user will end up with no default list. Therefore, the user will have to set a new * default list. * * @param listName the name of the list being deleted. */ public void deletePrivacyList(String listName) { // Remove the list from the cache this.getItemLists().remove(listName); // Check if deleted list was the default list if (this.getDefaultName() != null && listName.equals(this.getDefaultName())) { this.setDefaultName(null); } } /** * Returns the active privacy list or null if none was found. * * @return list with {@link PrivacyItem} or null if none was found. */ public List getActivePrivacyList() { // Check if we have the default list if (this.getActiveName() == null) { return null; } else { return (List) this.getItemLists().get(this.getActiveName()); } } /** * Returns the default privacy list or null if none was found. * * @return list with {@link PrivacyItem} or null if none was found. */ public List getDefaultPrivacyList() { // Check if we have the default list if (this.getDefaultName() == null) { return null; } else { return (List) this.getItemLists().get(this.getDefaultName()); } } /** * Returns a specific privacy list. * * @param listName the name of the list to get. * @return a List with {@link PrivacyItem} */ public List getPrivacyList(String listName) { return (List) this.getItemLists().get(listName); } /** * Returns the privacy item in the specified order. * * @param order the order of the element. * @return a List with {@link PrivacyItem} */ public PrivacyItem getItem(String listName, int order) { Iterator values = getPrivacyList(listName).iterator(); PrivacyItem itemFound = null; while (itemFound == null && values.hasNext()) { PrivacyItem element = (PrivacyItem) values.next(); if (element.getOrder() == order) { itemFound = element; } } return itemFound; } /** * Sets a given privacy list as the new user default list. * * @param newDefault the new default privacy list. * @return if the default list was changed. */ public boolean changeDefaultList(String newDefault) { if (this.getItemLists().containsKey(newDefault)) { this.setDefaultName(newDefault); return true; } else { return false; } } /** * Remove the list. * * @param listName name of the list to remove. */ public void deleteList(String listName) { this.getItemLists().remove(listName); } /** * Returns the name associated with the active list set for the session. Communications * will be verified against the active list.

* * @return the name of the active list. */ public String getActiveName() { return activeName; } /** * Sets the name associated with the active list set for the session. Communications * will be verified against the active list.

* * @param activeName is the name of the active list. */ public void setActiveName(String activeName) { this.activeName = activeName; } /** * Returns the name of the default list that applies to the user as a whole. Default list is * processed if there is no active list set for the target session/resource to which a stanza * is addressed, or if there are no current sessions for the user. * * @return the name of the default list. */ public String getDefaultName() { return defaultName; } /** * Sets the name of the default list that applies to the user as a whole. Default list is * processed if there is no active list set for the target session/resource to which a stanza * is addressed, or if there are no current sessions for the user. * * If there is no default list set, then all Privacy Items are processed. * * @param defaultName is the name of the default list. */ public void setDefaultName(String defaultName) { this.defaultName = defaultName; } /** * Returns the collection of privacy list that the user holds. A Privacy List contains a set of * rules that define if communication with the list owner is allowed or denied. * Users may have zero, one or more privacy items. * * @return a map where the key is the name of the list and the value the * collection of privacy items. */ public Map getItemLists() { return itemLists; } /** * Returns whether the receiver allows or declines the use of an active list. * * @return the decline status of the list. */ public boolean isDeclineActiveList() { return declineActiveList; } /** * Sets whether the receiver allows or declines the use of an active list. * * @param declineActiveList indicates if the receiver declines the use of an active list. */ public void setDeclineActiveList(boolean declineActiveList) { this.declineActiveList = declineActiveList; } /** * Returns whether the receiver allows or declines the use of a default list. * * @return the decline status of the list. */ public boolean isDeclineDefaultList() { return declineDefaultList; } /** * Sets whether the receiver allows or declines the use of a default list. * * @param declineActiveList indicates if the receiver declines the use of a default list. */ public void setDeclineDefaultList(boolean declineDefaultList) { this.declineDefaultList = declineDefaultList; } /** * Returns all the list names the user has defined to group restrictions. * * @return a Set with Strings containing every list names. */ public Set getPrivacyListNames() { return this.itemLists.keySet(); } public String getChildElementXML() { StringBuffer buf = new StringBuffer(); buf.append(""); // Add the active tag if (this.isDeclineActiveList()) { buf.append(""); } else { if (this.getActiveName() != null) { buf.append(""); } } // Add the default tag if (this.isDeclineDefaultList()) { buf.append(""); } else { if (this.getDefaultName() != null) { buf.append(""); } } // Add the list with their privacy items int listNameSize = this.getItemLists().size(); Iterator listItemsPairs = this.getItemLists().entrySet().iterator(); for (int i = 0; i < listNameSize; i++) { Map.Entry entry = (Map.Entry) listItemsPairs.next(); String listName = (String) entry.getKey(); List items = (List) entry.getValue(); // Begin the list tag if (items.isEmpty()) { buf.append(""); } else { buf.append(""); } for (Iterator itemIterator = items.iterator(); itemIterator.hasNext();) { PrivacyItem item = (PrivacyItem) itemIterator.next(); // Append the item xml representation buf.append(item.toXML()); } // Close the list tag if (!items.isEmpty()) { buf.append(""); } } // Add packet extensions, if any are defined. buf.append(getExtensionsXML()); buf.append(""); String generatedXML = buf.toString(); return generatedXML; } }