Question

I am trying to block communications in my XMPP client (Built on top of strophe.js). The problem is it only blocks my messages to a contact I am trying to "mute" BUT doesn't block any incoming messages from that contact.

Here is the logic (based on http://xmpp.org/rfcs/rfc3921.html#privacy):

1) Add "bill@domain.me" to my "block" list

var recipient = "bill@domain.me"
var block = $iq({type: 'set'}).c('query', {xmlns: 'jabber:iq:privacy'}).
c('list', {name: 'block'}).
c('item', {type: 'jid', value: recipient, action: 'deny', order: 1}).
c('message');

2) Make this list active

var setListActive = $iq({type: 'set'}).c('query', {xmlns: 'jabber:iq:privacy'}).c("active", {name: "block"});           
SGN.connection.sendIQ(setListActive);

What could be the problem?

Was it helpful?

Solution

I might be wrong but what I've understood is it is the way it should work.

If you check the list you were adding jids to you'll see that they are all there:

var getMyPrivacyList = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:privacy'}).c("list", {name: "block"});          
APP.connection.sendIQ(getMyPrivacyList,function success(response) { console.log(response) });

However if you want to block incoming messages you'd have to manually check senders's jids against this list every time message comes in.

OTHER TIPS

you have to send unsubscribe presence to that buddy as well. and have to put an handler to handle presence type="unsubscribed". on that handler, send unsubscribe presence.

code for blocking friend

const blockFriend_Stanza=function(fromJid, friendJid,cb_success){
    connection.send($pres({ to: friendJid, type: "unavailable", }));
    connection.send($pres({ to: friendJid, type: "unsubscribe" }));


    let UnblockIq=$iq({ 'type': 'set', 'id': 'blockFriend', from: fromJid  })
                .c("block", {xmlns: "urn:xmpp:blocking"}).c("item", {jid: friendJid});
    connection.sendIQ(UnblockIq,
        response=>{ 
            console.log("onClick_lnkBlockFriend",response)
            if(response.getAttribute("type")=="result"){
                cb_success();                
            }
        },
        error=>{ 
            console.log("onClick_lnkBlockFriend Error",error)
        }
    );

    let updateRosterIq=$iq({ 'type': 'set', 'id': 'acceptedReq' })
        .c("query", {xmlns: Strophe.NS.ROSTER}).c("item", {jid: friendJid,ask:null, subscription: "remove"});
    connection.sendIQ(updateRosterIq,response=>{ console.log("onClick_lnkBlockFriend_rosterRemoval",response)},error=>{ console.log("onClick_lnkBlockFriend_rosterRemoval Error",error)});
}

Code for unsubscribe handler

function onPresence(presence) {
    console.log("presense obj",presence);
    var presence_type = $(presence).attr('type'); // unavailable, subscribed, etc...
    var from = $(presence).attr('from'); // presence coming from jabber_id
    var to = $(presence).attr('to'); // presence coming to jabber_id


    if(presence_type == 'unsubscribe')
    {
        connection.send($pres({ to: from, type: "unsubscribed" }));
        connection.send($pres({ to: from, type: "unavailable", }));        
    }

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top