2

In my chat application. I am using Smack library and Openfire server. I want to block particular user.

I am trying to implement a function which will block a particular user but its not work for me.and it will not give any error or exception.

My code is

public void XMPPAddNewPrivacyList(XMPPConnection connection, String userName) {

        String listName = "newList";
        List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();

        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.toString(),
                false, 1);
        item.setValue(userName);
        privacyItems.add(item);

        // Create the new list.

        try {
            PrivacyListManager privacyManager = new PrivacyListManager(connection);
            privacyManager = PrivacyListManager
                    .getInstanceFor(connection);
            privacyManager.createPrivacyList(listName, privacyItems);

        } catch (XMPPException e) {
            System.out.println("PRIVACY_ERROR: " + e);
        }
    }

  XMPPAddNewPrivacyList(XmppConnection.getInstance().getConnection(),
 "91xxxxxxxxxx@xxx.com");
Dev
  • 2,326
  • 24
  • 45
Kinjal
  • 1,195
  • 4
  • 13
  • 24
  • what version of smack and openfire you are using??? – Dev Jun 08 '15 at 12:10
  • did your problem got resolved??? – Dev Jun 09 '15 at 07:35
  • No this PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, userName,false, 1l) constructor not in PrivacyItem class – Kinjal Jun 09 '15 at 07:47
  • which version of smack and openfire you are using??? – Dev Jun 09 '15 at 07:58
  • openfire 3.9.3 and qsmack library. – Kinjal Jun 09 '15 at 11:16
  • 1
    instate of qsmack please try Smack 4.1.0 it is more stable and have more features than qsmack you can refer this for more information https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide – Dev Jun 09 '15 at 11:22

2 Answers2

1

With Smack 4.1.0 and Openfire 3.10.0 you can achieve Block user like below

public void XMPPAddNewPrivacyList(XMPPConnection connection, String userName) {

        String listName = "newList";
        List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();

        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid,
            userName,false, 1l);
    privacyItems.add(item);
    // Create the new list.

    try {
        PrivacyListManager privacyManager; 
        privacyManager = PrivacyListManager
                .getInstanceFor(connection);
        privacyManager.createPrivacyList(listName, privacyItems);

    } catch (XMPPException e) {
        System.out.println("PRIVACY_ERROR: " + e);
    }
    }

now if you call above function like this

XMPPAddNewPrivacyList(XmppConnection.getInstance().getConnection(),
 "91xxxxxxxxxx");    

In smack debugger you can observer below iq stanza

<iq id="5W6tl-27" type="set">
  <query xmlns="jabber:iq:privacy">
    <list name="newList">
      <item action="deny" order="1" type="jid" value="91xxxxxxxxxx"/>
    </list>
  </query>
</iq>

<iq to="xyz@test-xmpp-abc/Smack" id="5W6tl-27" type="result">
  <query xmlns="jabber:iq:privacy">
    <list name="newList">
      <item action="deny" order="1" type="jid" value="91xxxxxxxxxx"/>
    </list>
  </query>
</iq>

Hope this will resolve your problem

Dev
  • 2,326
  • 24
  • 45
1
    public List<String> getBlockedUserList(String userId) { 

    List<String> privacyList = new ArrayList<String>();
    try {
        PrivacyListManager privacyManager = PrivacyListManager
                .getInstanceFor(XMPPUtils.INSTANCE.connection);
        if (privacyManager == null) {
            return privacyList;
        }
        String ser = "@" + XMPPUtils.INSTANCE.XMPPService;
        PrivacyList plist = null;
        try {
            plist = privacyManager.getPrivacyList("public");
        } catch (NoResponseException e) {
            e.printStackTrace();
        } catch (NotConnectedException e) {
            e.printStackTrace();
        }
        if (plist != null) {// No blacklisted or is not listed, direct getPrivacyList error
            List<PrivacyItem> items = plist.getItems();
            for (PrivacyItem item : items) {


                String from = item.getValue().substring(0,
                        item.getValue().indexOf(ser));

                if (userId.equals(from)) {

                    item.isAllow();
                }
                // privacyList.add(from);


            }
        } else {
            return privacyList;
        }
    } catch (XMPPException ex) {
    }
    return privacyList;
}