Using Privacy to Block All Users


I’'m interested in using the Privacy classes to block users from sending a client messages.

After having looked at the Privacy classes I can only figure out how to positively block users, i.e. I have to know the name/jid of the user before I can block them by doing something like:

PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1);


I’'d like to do something different: block all users, except the ones I allow – like have a default ‘‘deny all’’ privacy rule, except users I explicity say can contact me…

Can I do this? Have I missed something?

Thanks for any help you could offer,



I assume that you need more than one item in a list to do this, like:

<iq type=''result'' id=''getlist3'' to=''''>
  <query xmlns=''jabber:iq:privacy''>
    <list name=''private''>
      <item type=''subscription''
      <item action=''deny'' order=''15''></item>

with source code like

PrivacyItem item;
List<PrivacyItem> list = new Vector();
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION , true, 1);
item = new PrivacyItem(null , false, 2);
list.add(item); PrivacyListManager x = PrivacyListManager.getInstanceFor(con);
x.createPrivacyList("private", list);

while you can of course specify just some JIDs instead of SUBSCRIPTION=both.


Hi LG, thanks for the reply.

I’‘ve tested the code in my client and it seems to work, though I don’'t really understand how.

Just do I’'ve got this straight, the first PrivacyItem allows subscription to/from (“both”) the client? From whom?

I’'m confused about the second PrivacyItem though - passing null as the first argument, then false as the second to the constructor does what?

I’‘m sorry if these are strange questions, but I’‘m not really sure what’'s going on here! Could you explain a bit more?

Thanks again,


Hi Ed,

I did use subscription.both as an example. This one will make sure that only users which are on your roster can talk to you.

2nd PrivacyItem: I just tried it with “null” as I don’'t want to specify something there. For “false” you may want to read the Javadoc …/javadoc/org/jivesoftware/smack/PrivacyList.html

PrivacyItem(String type, boolean allow, int order)

So allow=false means block and order=2 means that this rule will be applied before rule 3 but before rule 1.

As this rules matches and blocks everything a following rule 3 will never match.