2007-01-08 00:03:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* $Revision$
|
|
|
|
|
* $Date$
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2006-2007 Jive Software.
|
|
|
|
|
*
|
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2006-07-19 21:24:00 +02:00
|
|
|
|
package org.jivesoftware.smack.packet;
|
|
|
|
|
|
2006-09-13 22:56:47 +02:00
|
|
|
|
import java.util.*;
|
2006-07-19 21:24:00 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2007-01-08 00:03:16 +01:00
|
|
|
|
* A Privacy IQ Packet, is used by the {@link org.jivesoftware.smack.PrivacyListManager}
|
|
|
|
|
* and {@link org.jivesoftware.smack.provider.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 <EFBFBD> Manager communication in order to:
|
2006-07-19 21:24:00 +02:00
|
|
|
|
* <ul>
|
|
|
|
|
* <li>Retrieving one's privacy lists.
|
|
|
|
|
* <li>Adding, removing, and editing one's privacy lists.
|
|
|
|
|
* <li>Setting, changing, or declining active lists.
|
|
|
|
|
* <li>Setting, changing, or declining the default list (i.e., the list that is active by default).
|
|
|
|
|
* </ul>
|
|
|
|
|
* Privacy Items can handle different kind of blocking communications based on JID, group,
|
2007-01-08 00:03:16 +01:00
|
|
|
|
* subscription type or globally {@link PrivacyItem}
|
2006-07-19 21:24:00 +02:00
|
|
|
|
*
|
|
|
|
|
* @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. **/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
private Map<String, List<PrivacyItem>> itemLists = new HashMap<String, List<PrivacyItem>>();
|
2006-07-19 21:24:00 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2007-01-08 00:03:16 +01:00
|
|
|
|
* Set or update a privacy list with privacy items.
|
2006-07-19 21:24:00 +02:00
|
|
|
|
*
|
|
|
|
|
* @param listName the name of the new privacy list.
|
|
|
|
|
* @param listItem the {@link PrivacyItem} that rules the list.
|
|
|
|
|
* @return the privacy List.
|
|
|
|
|
*/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
public List setPrivacyList(String listName, List<PrivacyItem> listItem) {
|
2006-07-19 21:24:00 +02:00
|
|
|
|
// 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.
|
|
|
|
|
*/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
public List<PrivacyItem> setActivePrivacyList() {
|
2006-07-19 21:24:00 +02:00
|
|
|
|
this.setActiveName(this.getDefaultName());
|
2006-09-13 22:56:47 +02:00
|
|
|
|
return this.getItemLists().get(this.getActiveName());
|
2006-07-19 21:24:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 <tt>null</tt> if none was found.
|
|
|
|
|
*
|
|
|
|
|
* @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
|
|
|
|
|
*/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
public List<PrivacyItem> getActivePrivacyList() {
|
2006-07-19 21:24:00 +02:00
|
|
|
|
// Check if we have the default list
|
|
|
|
|
if (this.getActiveName() == null) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
2006-09-13 22:56:47 +02:00
|
|
|
|
return this.getItemLists().get(this.getActiveName());
|
2006-07-19 21:24:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the default privacy list or <tt>null</tt> if none was found.
|
|
|
|
|
*
|
|
|
|
|
* @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
|
|
|
|
|
*/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
public List<PrivacyItem> getDefaultPrivacyList() {
|
2006-07-19 21:24:00 +02:00
|
|
|
|
// Check if we have the default list
|
|
|
|
|
if (this.getDefaultName() == null) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
2006-09-13 22:56:47 +02:00
|
|
|
|
return this.getItemLists().get(this.getDefaultName());
|
2006-07-19 21:24:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a specific privacy list.
|
|
|
|
|
*
|
|
|
|
|
* @param listName the name of the list to get.
|
|
|
|
|
* @return a List with {@link PrivacyItem}
|
|
|
|
|
*/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
public List<PrivacyItem> getPrivacyList(String listName) {
|
|
|
|
|
return this.getItemLists().get(listName);
|
2006-07-19 21:24:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the privacy item in the specified order.
|
|
|
|
|
*
|
2007-01-08 00:03:16 +01:00
|
|
|
|
* @param listName the name of the privacy list.
|
2006-07-19 21:24:00 +02:00
|
|
|
|
* @param order the order of the element.
|
|
|
|
|
* @return a List with {@link PrivacyItem}
|
|
|
|
|
*/
|
|
|
|
|
public PrivacyItem getItem(String listName, int order) {
|
2006-09-13 22:56:47 +02:00
|
|
|
|
Iterator<PrivacyItem> values = getPrivacyList(listName).iterator();
|
2006-07-19 21:24:00 +02:00
|
|
|
|
PrivacyItem itemFound = null;
|
|
|
|
|
while (itemFound == null && values.hasNext()) {
|
2006-09-13 22:56:47 +02:00
|
|
|
|
PrivacyItem element = values.next();
|
2006-07-19 21:24:00 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-08 00:03:16 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the list.
|
|
|
|
|
*
|
|
|
|
|
* @param listName name of the list to remove.
|
|
|
|
|
*/
|
2006-07-19 21:24:00 +02:00
|
|
|
|
public void deleteList(String listName) {
|
|
|
|
|
this.getItemLists().remove(listName);
|
|
|
|
|
}
|
2007-01-08 00:03:16 +01:00
|
|
|
|
|
2006-07-19 21:24:00 +02:00
|
|
|
|
/**
|
|
|
|
|
* Returns the name associated with the active list set for the session. Communications
|
2007-01-08 00:03:16 +01:00
|
|
|
|
* will be verified against the active list.
|
2006-07-19 21:24:00 +02:00
|
|
|
|
*
|
|
|
|
|
* @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
|
2007-01-08 00:03:16 +01:00
|
|
|
|
* will be verified against the active list.
|
2006-07-19 21:24:00 +02:00
|
|
|
|
*
|
|
|
|
|
* @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.
|
|
|
|
|
*/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
public Map<String, List<PrivacyItem>> getItemLists() {
|
2006-07-19 21:24:00 +02:00
|
|
|
|
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.
|
|
|
|
|
*
|
2006-09-13 22:56:47 +02:00
|
|
|
|
* @param declineDefaultList indicates if the receiver declines the use of a default list.
|
2006-07-19 21:24:00 +02:00
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2006-09-13 22:56:47 +02:00
|
|
|
|
public Set<String> getPrivacyListNames() {
|
2006-07-19 21:24:00 +02:00
|
|
|
|
return this.itemLists.keySet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getChildElementXML() {
|
|
|
|
|
StringBuffer buf = new StringBuffer();
|
|
|
|
|
buf.append("<query xmlns=\"jabber:iq:privacy\">");
|
|
|
|
|
|
|
|
|
|
// Add the active tag
|
|
|
|
|
if (this.isDeclineActiveList()) {
|
|
|
|
|
buf.append("<active/>");
|
|
|
|
|
} else {
|
|
|
|
|
if (this.getActiveName() != null) {
|
|
|
|
|
buf.append("<active name=\"").append(this.getActiveName()).append("\"/>");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Add the default tag
|
|
|
|
|
if (this.isDeclineDefaultList()) {
|
|
|
|
|
buf.append("<default/>");
|
|
|
|
|
} else {
|
|
|
|
|
if (this.getDefaultName() != null) {
|
|
|
|
|
buf.append("<default name=\"").append(this.getDefaultName()).append("\"/>");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the list with their privacy items
|
2006-09-13 22:56:47 +02:00
|
|
|
|
for (Map.Entry<String, List<PrivacyItem>> entry : this.getItemLists().entrySet()) {
|
|
|
|
|
String listName = entry.getKey();
|
|
|
|
|
List<PrivacyItem> items = entry.getValue();
|
2006-07-19 21:24:00 +02:00
|
|
|
|
// Begin the list tag
|
|
|
|
|
if (items.isEmpty()) {
|
|
|
|
|
buf.append("<list name=\"").append(listName).append("\"/>");
|
|
|
|
|
} else {
|
|
|
|
|
buf.append("<list name=\"").append(listName).append("\">");
|
|
|
|
|
}
|
2006-09-13 22:56:47 +02:00
|
|
|
|
for (PrivacyItem item : items) {
|
2006-07-19 21:24:00 +02:00
|
|
|
|
// Append the item xml representation
|
|
|
|
|
buf.append(item.toXML());
|
|
|
|
|
}
|
|
|
|
|
// Close the list tag
|
|
|
|
|
if (!items.isEmpty()) {
|
|
|
|
|
buf.append("</list>");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add packet extensions, if any are defined.
|
|
|
|
|
buf.append(getExtensionsXML());
|
|
|
|
|
buf.append("</query>");
|
2006-09-13 22:56:47 +02:00
|
|
|
|
return buf.toString();
|
2006-07-19 21:24:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-08 00:03:16 +01:00
|
|
|
|
}
|