mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-10 18:59:41 +02:00
Move privacy lists from core to extensions
This commit is contained in:
parent
2ad517b6dd
commit
38a3531ec6
16 changed files with 42 additions and 65 deletions
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.jivesoftware.smackx.privacy;
|
||||
|
||||
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A privacy list represents a list of contacts that is a read only class used to represent a set of allowed or blocked communications.
|
||||
* Basically it can:<ul>
|
||||
*
|
||||
* <li>Handle many {@link org.jivesoftware.smackx.privacy.packet.PrivacyItem}.</li>
|
||||
* <li>Answer if it is the default list.</li>
|
||||
* <li>Answer if it is the active list.</li>
|
||||
* </ul>
|
||||
*
|
||||
* {@link PrivacyItem Privacy Items} can handle different kind of blocking communications based on JID, group,
|
||||
* subscription type or globally.
|
||||
*
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
public class PrivacyList {
|
||||
|
||||
/** Holds if it is an active list or not **/
|
||||
private boolean isActiveList;
|
||||
/** Holds if it is an default list or not **/
|
||||
private boolean isDefaultList;
|
||||
/** Holds the list name used to print **/
|
||||
private String listName;
|
||||
/** Holds the list of {@see PrivacyItem} **/
|
||||
private List<PrivacyItem> items;
|
||||
|
||||
protected PrivacyList(boolean isActiveList, boolean isDefaultList,
|
||||
String listName, List<PrivacyItem> privacyItems) {
|
||||
super();
|
||||
this.isActiveList = isActiveList;
|
||||
this.isDefaultList = isDefaultList;
|
||||
this.listName = listName;
|
||||
this.items = privacyItems;
|
||||
}
|
||||
|
||||
public boolean isActiveList() {
|
||||
return isActiveList;
|
||||
}
|
||||
|
||||
public boolean isDefaultList() {
|
||||
return isDefaultList;
|
||||
}
|
||||
|
||||
public List<PrivacyItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return listName;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smackx.privacy;
|
||||
|
||||
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Interface to implement classes to listen for server events about privacy communication.
|
||||
* Listeners are registered with the {@link PrivacyListManager}.
|
||||
*
|
||||
* @see PrivacyListManager#addListener
|
||||
*
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
public interface PrivacyListListener {
|
||||
|
||||
/**
|
||||
* Set or update a privacy list with PrivacyItem.
|
||||
*
|
||||
* @param listName the name of the new or updated privacy list.
|
||||
* @param listItem the PrivacyItems that rules the list.
|
||||
*/
|
||||
public void setPrivacyList(String listName, List<PrivacyItem> listItem);
|
||||
|
||||
/**
|
||||
* A privacy list has been modified by another. It gets notified.
|
||||
*
|
||||
* @param listName the name of the updated privacy list.
|
||||
*/
|
||||
public void updatedPrivacyList(String listName);
|
||||
|
||||
}
|
|
@ -0,0 +1,444 @@
|
|||
/**
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smackx.privacy;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.PacketCollector;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.SmackConfiguration;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.*;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smackx.privacy.packet.Privacy;
|
||||
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A PrivacyListManager is used by XMPP clients to block or allow communications from other
|
||||
* users. Use the manager to: <ul>
|
||||
* <li>Retrieve privacy lists.
|
||||
* <li>Add, remove, and edit privacy lists.
|
||||
* <li>Set, change, or decline active lists.
|
||||
* <li>Set, change, or decline the default list (i.e., the list that is active by default).
|
||||
* </ul>
|
||||
* Privacy Items can handle different kind of permission communications based on JID, group,
|
||||
* subscription type or globally (@see PrivacyItem).
|
||||
*
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
public class PrivacyListManager {
|
||||
|
||||
// Keep the list of instances of this class.
|
||||
private static Map<Connection, PrivacyListManager> instances = Collections
|
||||
.synchronizedMap(new WeakHashMap<Connection, PrivacyListManager>());
|
||||
|
||||
private WeakReference<Connection> connection;
|
||||
private final List<PrivacyListListener> listeners = new ArrayList<PrivacyListListener>();
|
||||
PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET),
|
||||
new PacketExtensionFilter("query", "jabber:iq:privacy"));
|
||||
|
||||
static {
|
||||
// Create a new PrivacyListManager on every established connection. In the init()
|
||||
// method of PrivacyListManager, we'll add a listener that will delete the
|
||||
// instance when the connection is closed.
|
||||
Connection.addConnectionCreationListener(new ConnectionCreationListener() {
|
||||
public void connectionCreated(Connection connection) {
|
||||
getInstanceFor(connection);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Creates a new privacy manager to maintain the communication privacy. Note: no
|
||||
* information is sent to or received from the server until you attempt to
|
||||
* get or set the privacy communication.<p>
|
||||
*
|
||||
* @param connection the XMPP connection.
|
||||
*/
|
||||
private PrivacyListManager(final Connection connection) {
|
||||
this.connection = new WeakReference<Connection>(connection);
|
||||
// Register the new instance and associate it with the connection
|
||||
instances.put(connection, this);
|
||||
|
||||
connection.addPacketListener(new PacketListener() {
|
||||
public void processPacket(Packet packet) {
|
||||
|
||||
if (packet == null || packet.getError() != null) {
|
||||
return;
|
||||
}
|
||||
// The packet is correct.
|
||||
Privacy privacy = (Privacy) packet;
|
||||
|
||||
// Notifies the event to the listeners.
|
||||
synchronized (listeners) {
|
||||
for (PrivacyListListener listener : listeners) {
|
||||
// Notifies the created or updated privacy lists
|
||||
for (Map.Entry<String,List<PrivacyItem>> entry : privacy.getItemLists().entrySet()) {
|
||||
String listName = entry.getKey();
|
||||
List<PrivacyItem> items = entry.getValue();
|
||||
if (items.isEmpty()) {
|
||||
listener.updatedPrivacyList(listName);
|
||||
} else {
|
||||
listener.setPrivacyList(listName, items);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send a result package acknowledging the reception of a privacy package.
|
||||
|
||||
// Prepare the IQ packet to send
|
||||
IQ iq = new IQ() {
|
||||
public String getChildElementXML() {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
iq.setType(IQ.Type.RESULT);
|
||||
iq.setFrom(packet.getFrom());
|
||||
iq.setPacketID(packet.getPacketID());
|
||||
|
||||
// Send create & join packet.
|
||||
connection.sendPacket(iq);
|
||||
}
|
||||
}, packetFilter); }
|
||||
|
||||
/** Answer the connection userJID that owns the privacy.
|
||||
* @return the userJID that owns the privacy
|
||||
*/
|
||||
private String getUser() {
|
||||
return connection.get().getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PrivacyListManager instance associated with a given Connection.
|
||||
*
|
||||
* @param connection the connection used to look for the proper PrivacyListManager.
|
||||
* @return the PrivacyListManager associated with a given Connection.
|
||||
*/
|
||||
public static synchronized PrivacyListManager getInstanceFor(Connection connection) {
|
||||
PrivacyListManager plm = instances.get(connection);
|
||||
if (plm == null) plm = new PrivacyListManager(connection);
|
||||
return plm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the {@link Privacy} packet to the server in order to know some privacy content and then
|
||||
* waits for the answer.
|
||||
*
|
||||
* @param requestPrivacy is the {@link Privacy} packet configured properly whose XML
|
||||
* will be sent to the server.
|
||||
* @return a new {@link Privacy} with the data received from the server.
|
||||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||
*/
|
||||
private Privacy getRequest(Privacy requestPrivacy) throws XMPPException {
|
||||
Connection connection = PrivacyListManager.this.connection.get();
|
||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
||||
// The request is a get iq type
|
||||
requestPrivacy.setType(Privacy.Type.GET);
|
||||
requestPrivacy.setFrom(this.getUser());
|
||||
|
||||
// Filter packets looking for an answer from the server.
|
||||
PacketFilter responseFilter = new PacketIDFilter(requestPrivacy.getPacketID());
|
||||
PacketCollector response = connection.createPacketCollector(responseFilter);
|
||||
|
||||
// Send create & join packet.
|
||||
connection.sendPacket(requestPrivacy);
|
||||
|
||||
// Wait up to a certain number of seconds for a reply.
|
||||
Privacy privacyAnswer =
|
||||
(Privacy) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
||||
|
||||
// Stop queuing results
|
||||
response.cancel();
|
||||
|
||||
// Interprete the result and answer the privacy only if it is valid
|
||||
if (privacyAnswer == null) {
|
||||
throw new XMPPException("No response from server.");
|
||||
}
|
||||
else if (privacyAnswer.getError() != null) {
|
||||
throw new XMPPException(privacyAnswer.getError());
|
||||
}
|
||||
return privacyAnswer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the {@link Privacy} packet to the server in order to modify the server privacy and
|
||||
* waits for the answer.
|
||||
*
|
||||
* @param requestPrivacy is the {@link Privacy} packet configured properly whose xml will be sent
|
||||
* to the server.
|
||||
* @return a new {@link Privacy} with the data received from the server.
|
||||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||
*/
|
||||
private Packet setRequest(Privacy requestPrivacy) throws XMPPException {
|
||||
Connection connection = PrivacyListManager.this.connection.get();
|
||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
||||
// The request is a get iq type
|
||||
requestPrivacy.setType(Privacy.Type.SET);
|
||||
requestPrivacy.setFrom(this.getUser());
|
||||
|
||||
// Filter packets looking for an answer from the server.
|
||||
PacketFilter responseFilter = new PacketIDFilter(requestPrivacy.getPacketID());
|
||||
PacketCollector response = connection.createPacketCollector(responseFilter);
|
||||
|
||||
// Send create & join packet.
|
||||
connection.sendPacket(requestPrivacy);
|
||||
|
||||
// Wait up to a certain number of seconds for a reply.
|
||||
Packet privacyAnswer = response.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
||||
|
||||
// Stop queuing results
|
||||
response.cancel();
|
||||
|
||||
// Interprete the result and answer the privacy only if it is valid
|
||||
if (privacyAnswer == null) {
|
||||
throw new XMPPException("No response from server.");
|
||||
} else if (privacyAnswer.getError() != null) {
|
||||
throw new XMPPException(privacyAnswer.getError());
|
||||
}
|
||||
return privacyAnswer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer a privacy containing the list structre without {@link PrivacyItem}.
|
||||
*
|
||||
* @return a Privacy with the list names.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
private Privacy getPrivacyWithListNames() throws XMPPException {
|
||||
|
||||
// The request of the list is an empty privacy message
|
||||
Privacy request = new Privacy();
|
||||
|
||||
// Send the package to the server and get the answer
|
||||
return getRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer the active privacy list.
|
||||
*
|
||||
* @return the privacy list of the active list.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public PrivacyList getActiveList() throws XMPPException {
|
||||
Privacy privacyAnswer = this.getPrivacyWithListNames();
|
||||
String listName = privacyAnswer.getActiveName();
|
||||
boolean isDefaultAndActive = privacyAnswer.getActiveName() != null
|
||||
&& privacyAnswer.getDefaultName() != null
|
||||
&& privacyAnswer.getActiveName().equals(
|
||||
privacyAnswer.getDefaultName());
|
||||
return new PrivacyList(true, isDefaultAndActive, listName, getPrivacyListItems(listName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer the default privacy list.
|
||||
*
|
||||
* @return the privacy list of the default list.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public PrivacyList getDefaultList() throws XMPPException {
|
||||
Privacy privacyAnswer = this.getPrivacyWithListNames();
|
||||
String listName = privacyAnswer.getDefaultName();
|
||||
boolean isDefaultAndActive = privacyAnswer.getActiveName() != null
|
||||
&& privacyAnswer.getDefaultName() != null
|
||||
&& privacyAnswer.getActiveName().equals(
|
||||
privacyAnswer.getDefaultName());
|
||||
return new PrivacyList(isDefaultAndActive, true, listName, getPrivacyListItems(listName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer the privacy list items under listName with the allowed and blocked permissions.
|
||||
*
|
||||
* @param listName the name of the list to get the allowed and blocked permissions.
|
||||
* @return a list of privacy items under the list listName.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
private List<PrivacyItem> getPrivacyListItems(String listName) throws XMPPException {
|
||||
|
||||
// The request of the list is an privacy message with an empty list
|
||||
Privacy request = new Privacy();
|
||||
request.setPrivacyList(listName, new ArrayList<PrivacyItem>());
|
||||
|
||||
// Send the package to the server and get the answer
|
||||
Privacy privacyAnswer = getRequest(request);
|
||||
|
||||
return privacyAnswer.getPrivacyList(listName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer the privacy list items under listName with the allowed and blocked permissions.
|
||||
*
|
||||
* @param listName the name of the list to get the allowed and blocked permissions.
|
||||
* @return a privacy list under the list listName.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public PrivacyList getPrivacyList(String listName) throws XMPPException {
|
||||
|
||||
return new PrivacyList(false, false, listName, getPrivacyListItems(listName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Answer every privacy list with the allowed and blocked permissions.
|
||||
*
|
||||
* @return an array of privacy lists.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public PrivacyList[] getPrivacyLists() throws XMPPException {
|
||||
Privacy privacyAnswer = this.getPrivacyWithListNames();
|
||||
Set<String> names = privacyAnswer.getPrivacyListNames();
|
||||
PrivacyList[] lists = new PrivacyList[names.size()];
|
||||
boolean isActiveList;
|
||||
boolean isDefaultList;
|
||||
int index=0;
|
||||
for (String listName : names) {
|
||||
isActiveList = listName.equals(privacyAnswer.getActiveName());
|
||||
isDefaultList = listName.equals(privacyAnswer.getDefaultName());
|
||||
lists[index] = new PrivacyList(isActiveList, isDefaultList,
|
||||
listName, getPrivacyListItems(listName));
|
||||
index = index + 1;
|
||||
}
|
||||
return lists;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set or change the active list to listName.
|
||||
*
|
||||
* @param listName the list name to set as the active one.
|
||||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||
*/
|
||||
public void setActiveListName(String listName) throws XMPPException {
|
||||
|
||||
// The request of the list is an privacy message with an empty list
|
||||
Privacy request = new Privacy();
|
||||
request.setActiveName(listName);
|
||||
|
||||
// Send the package to the server
|
||||
setRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Client declines the use of active lists.
|
||||
*
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public void declineActiveList() throws XMPPException {
|
||||
|
||||
// The request of the list is an privacy message with an empty list
|
||||
Privacy request = new Privacy();
|
||||
request.setDeclineActiveList(true);
|
||||
|
||||
// Send the package to the server
|
||||
setRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or change the default list to listName.
|
||||
*
|
||||
* @param listName the list name to set as the default one.
|
||||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||
*/
|
||||
public void setDefaultListName(String listName) throws XMPPException {
|
||||
|
||||
// The request of the list is an privacy message with an empty list
|
||||
Privacy request = new Privacy();
|
||||
request.setDefaultName(listName);
|
||||
|
||||
// Send the package to the server
|
||||
setRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Client declines the use of default lists.
|
||||
*
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public void declineDefaultList() throws XMPPException {
|
||||
|
||||
// The request of the list is an privacy message with an empty list
|
||||
Privacy request = new Privacy();
|
||||
request.setDeclineDefaultList(true);
|
||||
|
||||
// Send the package to the server
|
||||
setRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* The client has created a new list. It send the new one to the server.
|
||||
*
|
||||
* @param listName the list that has changed its content.
|
||||
* @param privacyItems a List with every privacy item in the list.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public void createPrivacyList(String listName, List<PrivacyItem> privacyItems) throws XMPPException {
|
||||
|
||||
this.updatePrivacyList(listName, privacyItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* The client has edited an existing list. It updates the server content with the resulting
|
||||
* list of privacy items. The {@link PrivacyItem} list MUST contain all elements in the
|
||||
* list (not the "delta").
|
||||
*
|
||||
* @param listName the list that has changed its content.
|
||||
* @param privacyItems a List with every privacy item in the list.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public void updatePrivacyList(String listName, List<PrivacyItem> privacyItems) throws XMPPException {
|
||||
|
||||
// Build the privacy package to add or update the new list
|
||||
Privacy request = new Privacy();
|
||||
request.setPrivacyList(listName, privacyItems);
|
||||
|
||||
// Send the package to the server
|
||||
setRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a privacy list.
|
||||
*
|
||||
* @param listName the list that has changed its content.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public void deletePrivacyList(String listName) throws XMPPException {
|
||||
|
||||
// The request of the list is an privacy message with an empty list
|
||||
Privacy request = new Privacy();
|
||||
request.setPrivacyList(listName, new ArrayList<PrivacyItem>());
|
||||
|
||||
// Send the package to the server
|
||||
setRequest(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a packet listener that will be notified of any new update in the user
|
||||
* privacy communication.
|
||||
*
|
||||
* @param listener a packet listener.
|
||||
*/
|
||||
public void addListener(PrivacyListListener listener) {
|
||||
// Keep track of the listener so that we can manually deliver extra
|
||||
// messages to it later if needed.
|
||||
synchronized (listeners) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,323 @@
|
|||
/**
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smackx.privacy.packet;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
|
||||
/**
|
||||
* A Privacy IQ Packet, is used by the {@link org.jivesoftware.smackx.privacy.PrivacyListManager}
|
||||
* and {@link org.jivesoftware.smackx.privacy.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 & manager communication in order to:
|
||||
* <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,
|
||||
* subscription type or globally {@link 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<String, List<PrivacyItem>> itemLists = new HashMap<String, List<PrivacyItem>>();
|
||||
|
||||
/**
|
||||
* Set or update a privacy list with privacy items.
|
||||
*
|
||||
* @param listName the name of the new privacy list.
|
||||
* @param listItem the {@link PrivacyItem} that rules the list.
|
||||
* @return the privacy List.
|
||||
*/
|
||||
public List<PrivacyItem> setPrivacyList(String listName, List<PrivacyItem> 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<PrivacyItem> setActivePrivacyList() {
|
||||
this.setActiveName(this.getDefaultName());
|
||||
return 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 <tt>null</tt> if none was found.
|
||||
*
|
||||
* @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
|
||||
*/
|
||||
public List<PrivacyItem> getActivePrivacyList() {
|
||||
// Check if we have the default list
|
||||
if (this.getActiveName() == null) {
|
||||
return null;
|
||||
} else {
|
||||
return this.getItemLists().get(this.getActiveName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public List<PrivacyItem> getDefaultPrivacyList() {
|
||||
// Check if we have the default list
|
||||
if (this.getDefaultName() == null) {
|
||||
return null;
|
||||
} else {
|
||||
return 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<PrivacyItem> getPrivacyList(String listName) {
|
||||
return this.getItemLists().get(listName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the privacy item in the specified order.
|
||||
*
|
||||
* @param listName the name of the privacy list.
|
||||
* @param order the order of the element.
|
||||
* @return a List with {@link PrivacyItem}
|
||||
*/
|
||||
public PrivacyItem getItem(String listName, int order) {
|
||||
Iterator<PrivacyItem> values = getPrivacyList(listName).iterator();
|
||||
PrivacyItem itemFound = null;
|
||||
while (itemFound == null && values.hasNext()) {
|
||||
PrivacyItem element = 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<String, List<PrivacyItem>> 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 declineDefaultList 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<String> getPrivacyListNames() {
|
||||
return this.itemLists.keySet();
|
||||
}
|
||||
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
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
|
||||
for (Map.Entry<String, List<PrivacyItem>> entry : this.getItemLists().entrySet()) {
|
||||
String listName = entry.getKey();
|
||||
List<PrivacyItem> items = entry.getValue();
|
||||
// Begin the list tag
|
||||
if (items.isEmpty()) {
|
||||
buf.append("<list name=\"").append(listName).append("\"/>");
|
||||
} else {
|
||||
buf.append("<list name=\"").append(listName).append("\">");
|
||||
}
|
||||
for (PrivacyItem item : items) {
|
||||
// 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>");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,459 @@
|
|||
/**
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.jivesoftware.smackx.privacy.packet;
|
||||
|
||||
/**
|
||||
* A privacy item acts a rule that when matched defines if a packet should be blocked or not.
|
||||
*
|
||||
* Privacy Items can handle different kind of blocking communications based on JID, group,
|
||||
* subscription type or globally by:<ul>
|
||||
* <li>Allowing or blocking messages.
|
||||
* <li>Allowing or blocking inbound presence notifications.
|
||||
* <li>Allowing or blocking outbound presence notifications.
|
||||
* <li>Allowing or blocking IQ stanzas.
|
||||
* <li>Allowing or blocking all communications.
|
||||
* </ul>
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
public class PrivacyItem {
|
||||
/** allow is the action associated with the item, it can allow or deny the communication. */
|
||||
private boolean allow;
|
||||
/** order is a non-negative integer that is unique among all items in the list. */
|
||||
private int order;
|
||||
/** rule hold the kind of communication ([jid|group|subscription]) it will allow or block and
|
||||
* identifier to apply the action.
|
||||
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
|
||||
* If the type is "group", then the 'value' attribute SHOULD contain the name of a group
|
||||
* in the user's roster.
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
|
||||
* "from", or "none". */
|
||||
private PrivacyRule rule;
|
||||
|
||||
/** blocks incoming IQ stanzas. */
|
||||
private boolean filterIQ = false;
|
||||
/** filterMessage blocks incoming message stanzas. */
|
||||
private boolean filterMessage = false;
|
||||
/** blocks incoming presence notifications. */
|
||||
private boolean filterPresence_in = false;
|
||||
/** blocks outgoing presence notifications. */
|
||||
private boolean filterPresence_out = false;
|
||||
|
||||
/**
|
||||
* Creates a new privacy item.
|
||||
*
|
||||
* @param type the type.
|
||||
*/
|
||||
public PrivacyItem(String type, boolean allow, int order) {
|
||||
this.setRule(PrivacyRule.fromString(type));
|
||||
this.setAllow(allow);
|
||||
this.setOrder(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the action associated with the item, it MUST be filled and will allow or deny
|
||||
* the communication.
|
||||
*
|
||||
* @return the allow communication status.
|
||||
*/
|
||||
public boolean isAllow() {
|
||||
return allow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the action associated with the item, it can allow or deny the communication.
|
||||
*
|
||||
* @param allow indicates if the receiver allow or deny the communication.
|
||||
*/
|
||||
private void setAllow(boolean allow) {
|
||||
this.allow = allow;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the receiver allow or deny incoming IQ stanzas or not.
|
||||
*
|
||||
* @return the iq filtering status.
|
||||
*/
|
||||
public boolean isFilterIQ() {
|
||||
return filterIQ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets whether the receiver allows or denies incoming IQ stanzas or not.
|
||||
*
|
||||
* @param filterIQ indicates if the receiver allows or denies incoming IQ stanzas.
|
||||
*/
|
||||
public void setFilterIQ(boolean filterIQ) {
|
||||
this.filterIQ = filterIQ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the receiver allows or denies incoming messages or not.
|
||||
*
|
||||
* @return the message filtering status.
|
||||
*/
|
||||
public boolean isFilterMessage() {
|
||||
return filterMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets wheather the receiver allows or denies incoming messages or not.
|
||||
*
|
||||
* @param filterMessage indicates if the receiver allows or denies incoming messages or not.
|
||||
*/
|
||||
public void setFilterMessage(boolean filterMessage) {
|
||||
this.filterMessage = filterMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the receiver allows or denies incoming presence or not.
|
||||
*
|
||||
* @return the iq filtering incoming presence status.
|
||||
*/
|
||||
public boolean isFilterPresence_in() {
|
||||
return filterPresence_in;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets whether the receiver allows or denies incoming presence or not.
|
||||
*
|
||||
* @param filterPresence_in indicates if the receiver allows or denies filtering incoming presence.
|
||||
*/
|
||||
public void setFilterPresence_in(boolean filterPresence_in) {
|
||||
this.filterPresence_in = filterPresence_in;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the receiver allows or denies incoming presence or not.
|
||||
*
|
||||
* @return the iq filtering incoming presence status.
|
||||
*/
|
||||
public boolean isFilterPresence_out() {
|
||||
return filterPresence_out;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets whether the receiver allows or denies outgoing presence or not.
|
||||
*
|
||||
* @param filterPresence_out indicates if the receiver allows or denies filtering outgoing presence
|
||||
*/
|
||||
public void setFilterPresence_out(boolean filterPresence_out) {
|
||||
this.filterPresence_out = filterPresence_out;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the order where the receiver is processed. List items are processed in
|
||||
* ascending order.
|
||||
*
|
||||
* The order MUST be filled and its value MUST be a non-negative integer
|
||||
* that is unique among all items in the list.
|
||||
*
|
||||
* @return the order number.
|
||||
*/
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the order where the receiver is processed.
|
||||
*
|
||||
* The order MUST be filled and its value MUST be a non-negative integer
|
||||
* that is unique among all items in the list.
|
||||
*
|
||||
* @param order indicates the order in the list.
|
||||
*/
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the element identifier to apply the action.
|
||||
*
|
||||
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
|
||||
* If the type is "group", then the 'value' attribute SHOULD contain the name of a group
|
||||
* in the user's roster.
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
|
||||
* "from", or "none".
|
||||
*
|
||||
* @param value is the identifier to apply the action.
|
||||
*/
|
||||
public void setValue(String value) {
|
||||
if (!(this.getRule() == null && value == null)) {
|
||||
this.getRule().setValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type hold the kind of communication it will allow or block.
|
||||
* It MUST be filled with one of these values: jid, group or subscription.
|
||||
*
|
||||
* @return the type of communication it represent.
|
||||
*/
|
||||
public Type getType() {
|
||||
if (this.getRule() == null) {
|
||||
return null;
|
||||
} else {
|
||||
return this.getRule().getType();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element identifier to apply the action.
|
||||
*
|
||||
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
|
||||
* If the type is "group", then the 'value' attribute SHOULD contain the name of a group
|
||||
* in the user's roster.
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
|
||||
* "from", or "none".
|
||||
*
|
||||
* @return the identifier to apply the action.
|
||||
*/
|
||||
public String getValue() {
|
||||
if (this.getRule() == null) {
|
||||
return null;
|
||||
} else {
|
||||
return this.getRule().getValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the receiver allows or denies every kind of communication.
|
||||
*
|
||||
* When filterIQ, filterMessage, filterPresence_in and filterPresence_out are not set
|
||||
* the receiver will block all communications.
|
||||
*
|
||||
* @return the all communications status.
|
||||
*/
|
||||
public boolean isFilterEverything() {
|
||||
return !(this.isFilterIQ() || this.isFilterMessage() || this.isFilterPresence_in()
|
||||
|| this.isFilterPresence_out());
|
||||
}
|
||||
|
||||
|
||||
private PrivacyRule getRule() {
|
||||
return rule;
|
||||
}
|
||||
|
||||
private void setRule(PrivacyRule rule) {
|
||||
this.rule = rule;
|
||||
}
|
||||
/**
|
||||
* Answer an xml representation of the receiver according to the RFC 3921.
|
||||
*
|
||||
* @return the text xml representation.
|
||||
*/
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<item");
|
||||
if (this.isAllow()) {
|
||||
buf.append(" action=\"allow\"");
|
||||
} else {
|
||||
buf.append(" action=\"deny\"");
|
||||
}
|
||||
buf.append(" order=\"").append(getOrder()).append("\"");
|
||||
if (getType() != null) {
|
||||
buf.append(" type=\"").append(getType()).append("\"");
|
||||
}
|
||||
if (getValue() != null) {
|
||||
buf.append(" value=\"").append(getValue()).append("\"");
|
||||
}
|
||||
if (isFilterEverything()) {
|
||||
buf.append("/>");
|
||||
} else {
|
||||
buf.append(">");
|
||||
if (this.isFilterIQ()) {
|
||||
buf.append("<iq/>");
|
||||
}
|
||||
if (this.isFilterMessage()) {
|
||||
buf.append("<message/>");
|
||||
}
|
||||
if (this.isFilterPresence_in()) {
|
||||
buf.append("<presence-in/>");
|
||||
}
|
||||
if (this.isFilterPresence_out()) {
|
||||
buf.append("<presence-out/>");
|
||||
}
|
||||
buf.append("</item>");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Privacy Rule represents the kind of action to apply.
|
||||
* It holds the kind of communication ([jid|group|subscription]) it will allow or block and
|
||||
* identifier to apply the action.
|
||||
*/
|
||||
|
||||
public static class PrivacyRule {
|
||||
/**
|
||||
* Type defines if the rule is based on JIDs, roster groups or presence subscription types.
|
||||
* Available values are: [jid|group|subscription]
|
||||
*/
|
||||
private Type type;
|
||||
/**
|
||||
* The value hold the element identifier to apply the action.
|
||||
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
|
||||
* If the type is "group", then the 'value' attribute SHOULD contain the name of a group
|
||||
* in the user's roster.
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
|
||||
* "from", or "none".
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both",
|
||||
* "to", "from", or "none"
|
||||
*/
|
||||
public static final String SUBSCRIPTION_BOTH = "both";
|
||||
public static final String SUBSCRIPTION_TO = "to";
|
||||
public static final String SUBSCRIPTION_FROM = "from";
|
||||
public static final String SUBSCRIPTION_NONE = "none";
|
||||
|
||||
/**
|
||||
* Returns the type constant associated with the String value.
|
||||
*/
|
||||
protected static PrivacyRule fromString(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
PrivacyRule rule = new PrivacyRule();
|
||||
rule.setType(Type.valueOf(value.toLowerCase()));
|
||||
return rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type hold the kind of communication it will allow or block.
|
||||
* It MUST be filled with one of these values: jid, group or subscription.
|
||||
*
|
||||
* @return the type of communication it represent.
|
||||
*/
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the action associated with the item, it can allow or deny the communication.
|
||||
*
|
||||
* @param type indicates if the receiver allows or denies the communication.
|
||||
*/
|
||||
private void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element identifier to apply the action.
|
||||
*
|
||||
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
|
||||
* If the type is "group", then the 'value' attribute SHOULD contain the name of a group
|
||||
* in the user's roster.
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
|
||||
* "from", or "none".
|
||||
*
|
||||
* @return the identifier to apply the action.
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the element identifier to apply the action.
|
||||
*
|
||||
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
|
||||
* If the type is "group", then the 'value' attribute SHOULD contain the name of a group
|
||||
* in the user's roster.
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both", "to",
|
||||
* "from", or "none".
|
||||
*
|
||||
* @param value is the identifier to apply the action.
|
||||
*/
|
||||
protected void setValue(String value) {
|
||||
if (this.isSuscription()) {
|
||||
setSuscriptionValue(value);
|
||||
} else {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the element identifier to apply the action.
|
||||
*
|
||||
* The 'value' attribute MUST be one of "both", "to", "from", or "none".
|
||||
*
|
||||
* @param value is the identifier to apply the action.
|
||||
*/
|
||||
private void setSuscriptionValue(String value) {
|
||||
String setValue;
|
||||
if (value == null) {
|
||||
// Do nothing
|
||||
}
|
||||
if (SUBSCRIPTION_BOTH.equalsIgnoreCase(value)) {
|
||||
setValue = SUBSCRIPTION_BOTH;
|
||||
}
|
||||
else if (SUBSCRIPTION_TO.equalsIgnoreCase(value)) {
|
||||
setValue = SUBSCRIPTION_TO;
|
||||
}
|
||||
else if (SUBSCRIPTION_FROM.equalsIgnoreCase(value)) {
|
||||
setValue = SUBSCRIPTION_FROM;
|
||||
}
|
||||
else if (SUBSCRIPTION_NONE.equalsIgnoreCase(value)) {
|
||||
setValue = SUBSCRIPTION_NONE;
|
||||
}
|
||||
// Default to available.
|
||||
else {
|
||||
setValue = null;
|
||||
}
|
||||
this.value = setValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the receiver represents a subscription rule.
|
||||
*
|
||||
* @return if the receiver represents a subscription rule.
|
||||
*/
|
||||
public boolean isSuscription () {
|
||||
return this.getType() == Type.subscription;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Type defines if the rule is based on JIDs, roster groups or presence subscription types.
|
||||
*/
|
||||
public static enum Type {
|
||||
/**
|
||||
* JID being analyzed should belong to a roster group of the list's owner.
|
||||
*/
|
||||
group,
|
||||
/**
|
||||
* JID being analyzed should have a resource match, domain match or bare JID match.
|
||||
*/
|
||||
jid,
|
||||
/**
|
||||
* JID being analyzed should belong to a contact present in the owner's roster with
|
||||
* the specified subscription status.
|
||||
*/
|
||||
subscription
|
||||
}
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
/**
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.jivesoftware.smackx.privacy.provider;
|
||||
|
||||
import org.jivesoftware.smack.packet.DefaultPacketExtension;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.provider.IQProvider;
|
||||
import org.jivesoftware.smackx.privacy.packet.Privacy;
|
||||
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The PrivacyProvider parses {@link Privacy} packets. {@link Privacy}
|
||||
* Parses the <tt>query</tt> sub-document and creates an instance of {@link Privacy}.
|
||||
* For each <tt>item</tt> in the <tt>list</tt> element, it creates an instance
|
||||
* of {@link PrivacyItem} and {@link org.jivesoftware.smackx.privacy.packet.PrivacyItem.PrivacyRule}.
|
||||
*
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
public class PrivacyProvider implements IQProvider {
|
||||
|
||||
public PrivacyProvider() {
|
||||
}
|
||||
|
||||
public IQ parseIQ(XmlPullParser parser) throws Exception {
|
||||
Privacy privacy = new Privacy();
|
||||
/* privacy.addExtension(PacketParserUtils.parsePacketExtension(parser
|
||||
.getName(), parser.getNamespace(), parser)); */
|
||||
privacy.addExtension(new DefaultPacketExtension(parser.getName(), parser.getNamespace()));
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("active")) {
|
||||
String activeName = parser.getAttributeValue("", "name");
|
||||
if (activeName == null) {
|
||||
privacy.setDeclineActiveList(true);
|
||||
} else {
|
||||
privacy.setActiveName(activeName);
|
||||
}
|
||||
}
|
||||
else if (parser.getName().equals("default")) {
|
||||
String defaultName = parser.getAttributeValue("", "name");
|
||||
if (defaultName == null) {
|
||||
privacy.setDeclineDefaultList(true);
|
||||
} else {
|
||||
privacy.setDefaultName(defaultName);
|
||||
}
|
||||
}
|
||||
else if (parser.getName().equals("list")) {
|
||||
parseList(parser, privacy);
|
||||
}
|
||||
}
|
||||
else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("query")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return privacy;
|
||||
}
|
||||
|
||||
// Parse the list complex type
|
||||
public void parseList(XmlPullParser parser, Privacy privacy) throws Exception {
|
||||
boolean done = false;
|
||||
String listName = parser.getAttributeValue("", "name");
|
||||
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("item")) {
|
||||
items.add(parseItem(parser));
|
||||
}
|
||||
}
|
||||
else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("list")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
privacy.setPrivacyList(listName, items);
|
||||
}
|
||||
|
||||
// Parse the list complex type
|
||||
public PrivacyItem parseItem(XmlPullParser parser) throws Exception {
|
||||
boolean done = false;
|
||||
// Retrieves the required attributes
|
||||
String actionValue = parser.getAttributeValue("", "action");
|
||||
String orderValue = parser.getAttributeValue("", "order");
|
||||
String type = parser.getAttributeValue("", "type");
|
||||
|
||||
/*
|
||||
* According the action value it sets the allow status. The fall-through action is assumed
|
||||
* to be "allow"
|
||||
*/
|
||||
boolean allow = true;
|
||||
if ("allow".equalsIgnoreCase(actionValue)) {
|
||||
allow = true;
|
||||
} else if ("deny".equalsIgnoreCase(actionValue)) {
|
||||
allow = false;
|
||||
}
|
||||
// Set the order number
|
||||
int order = Integer.parseInt(orderValue);
|
||||
|
||||
// Create the privacy item
|
||||
PrivacyItem item = new PrivacyItem(type, allow, order);
|
||||
item.setValue(parser.getAttributeValue("", "value"));
|
||||
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("iq")) {
|
||||
item.setFilterIQ(true);
|
||||
}
|
||||
if (parser.getName().equals("message")) {
|
||||
item.setFilterMessage(true);
|
||||
}
|
||||
if (parser.getName().equals("presence-in")) {
|
||||
item.setFilterPresence_in(true);
|
||||
}
|
||||
if (parser.getName().equals("presence-out")) {
|
||||
item.setFilterPresence_out(true);
|
||||
}
|
||||
}
|
||||
else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("item")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
}
|
|
@ -449,4 +449,11 @@
|
|||
<className>org.jivesoftware.smackx.ping.provider.PingProvider</className>
|
||||
</iqProvider>
|
||||
|
||||
<!-- Privacy -->
|
||||
<iqProvider>
|
||||
<elementName>query</elementName>
|
||||
<namespace>jabber:iq:privacy</namespace>
|
||||
<className>org.jivesoftware.smackx.privacy.provider.PrivacyProvider</className>
|
||||
</iqProvider>
|
||||
|
||||
</smackProviders>
|
||||
|
|
|
@ -9,5 +9,6 @@
|
|||
<className>org.jivesoftware.smackx.iqlast.LastActivityManager</className>
|
||||
<className>org.jivesoftware.smackx.commands.AdHocCommandManager</className>
|
||||
<className>org.jivesoftware.smackx.ping.PingManager</className>
|
||||
<className>org.jivesoftware.smackx.privacy.PrivacyListManager</className>
|
||||
</startupClasses>
|
||||
</smack>
|
Loading…
Add table
Add a link
Reference in a new issue