XEP-0016: Privacy Lists allow you to block communication with other entities, either based on their JID, subscription state or roster group.
You can get all existing privacy lists from the server like this:
PrivacyListManager privacyListManager = xmppClient.getManager(PrivacyListManager.class); Collection<PrivacyList> privacyLists = privacyListManager.getPrivacyLists().getResult();
Creating or updating a privacy list use the same protocol, therefore there’s only one method for both use cases.
Collection<PrivacyRule> rules = Collections.singleton(PrivacyRule.blockMessagesFrom(Jid.of("juliet@example.com"), 1)); PrivacyList privacyList = new PrivacyList("listName", rules); privacyListManager.createOrUpdateList(privacyList);
Default lists apply to the user as a whole, and are processed if there is no active list set for the target session.
privacyListManager.setDefaultList("listName");
Active lists only affect the current session for which they are activated and only for the duration of the session.
privacyListManager.setActiveList("listName");
Whenever a privacy list is updated, the server will notify all connected resources about the updated list. You can listen for these “pushes” in the following way:
privacyListManager.addPrivacyListListener(e -> { // e.getListName() has been updated. });
A common use case for Privacy Lists is to block communication with another user.
For this use case you should add a privacy rule for the user, which denies all communication.
new PrivacyList("name", Collections.singleton(PrivacyRule.blockAllCommunicationWith(Jid.of("tybalt@example.com"), 1)));
Privacy rules can also be defined more granular. E.g. if you only want to block IQ stanzas, but allow presences and messages, you would define the rule like:
PrivacyRule privacyRule = PrivacyRule.blockIQFrom(Jid.of("tybalt@example.com"), 1);
XEP-0126: Invisibility defines a recommendation for using Privacy Lists for invisibility.
You can create an “invisibility” list with a handful of static factory methods, e.g.:
PrivacyList invisibilityList = PrivacyList.createInvisibilityList();