Kinjal
Kinjal

Reputation: 1193

Block user in android using xmpp + smack + openfire

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(),
 "[email protected]");

Upvotes: 2

Views: 1668

Answers (2)

Satish Solanki
Satish Solanki

Reputation: 11

    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;
}

Upvotes: 1

Dev
Dev

Reputation: 2325

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

Upvotes: 1

Related Questions