XMPP阻止消息

时间:2014-02-27 17:32:35

标签: xmpp strophe

我正在尝试阻止我的XMPP客户端中的通信(建立在strophe.js之上)。问题是它只阻止我的消息发送给我试图“静音”的联系人但是不阻止来自该联系人的任何传入消息。

这是逻辑(基于http://xmpp.org/rfcs/rfc3921.html#privacy):

1)将“bill@domain.me”添加到我的“阻止”列表

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)激活此列表

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

可能是什么问题?

2 个答案:

答案 0 :(得分:0)

我可能错了但是我所理解的是它的应用方式。

如果你检查列表,你正在添加jids,你会发现它们都在那里:

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

但是,如果您想阻止收到的消息,则每次收到消息时,您都必须手动检查发件人对该列表的jids。

答案 1 :(得分:0)

您还必须向该好友发送取消订阅的状态。并且必须放置一个处理程序来处理状态type =“ unsubscribed”。在该处理程序上,发送退订状态。

阻止朋友的代码

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

退订处理程序的代码

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