1

I'm using node-xmpp-server as a xmpp server for a chat application.As a client i'm using Spark.I want to add contacts to roster.How can i do this?I've run on ejabbberd and iti work but i need to implement a code.Thanks for any advice!

My server code:

var startServer = function(done) {
    // Sets up the server.
    server = new xmpp.C2S.TCPServer({
        port: 5222,
        domain: 'localhost'
    })

    // On connection event. When a client connects.
    server.on('connection', function(client) {
        // That's the way you add mods to a given server.

        // Allows the developer to register the jid against anything they want
        client.on('register', function(opts, cb) {
            console.log('REGISTER')
            console.log(cb);
            cb(false)
        })

        // Allows the developer to authenticate users against anything they want.
        client.on('authenticate', function(opts, cb) {
            //console.log('server:', opts.username, opts.password, 'AUTHENTICATING')
            if (opts.password === 'secret') {
                //console.log('server:', opts.username, 'AUTH OK')
                cb(null, opts)
            }
            else {
                //console.log('server:', opts.username, 'AUTH FAIL')
                cb(false)
            }
        })

        client.on('online', function() {
            console.log('ONLINE')
        })

        // Stanza handling
        client.on('stanza', function(stanza) {
            console.log('server:', client.jid.local, 'stanza', stanza.toString())
            //var from = stanza.attrs.from
           // stanza.attrs.from = stanza.attrs.to
            //stanza.attrs.to = from
           if (stanza.is('message') && (stanza.attrs.type !== 'error')) {

            client.send(stanza);
           }
           else {
            client.send(stanza);
        }
        })

        // On Disconnect event. When a client disconnects
        client.on('disconnect', function() {
            console.log('server:', client.jid.local, 'DISCONNECT')
        })

    })

    server.on('listening', done)
}

startServer(function() {



})
Worker1
  • 195
  • 10

1 Answers1

0

This is a super basic example, you can extend it as you want:

First of all, you need to look for the iq stanzas, with get type thus we add:

client.on('stanza', function (stanza) {
    console.log('server:', client.jid.local, 'stanza', stanza.toString())
    if (stanza.is('message') && (stanza.attrs.type !== 'error')) {
        client.send(stanza);
    }
    else if (stanza.is('iq') && stanza.attrs.type == 'get') {
        ...
    }
    else {
        client.send(stanza);
    }
});

We now have to loop over all the children of this request, and look for a node with the name attribute equal query, and the xmlns attribute equal jabber:iq:roster:

...
else if (stanza.is('iq') && stanza.attrs.type == 'get') {
    for (var i = 0; i < stanza.children.length; i++) {
        if (stanza.children[i].name == 'query' && stanza.children[i].attrs.xmlns == 'jabber:iq:roster') {

            // We create 5 fake users    
            for (var j = 0; j < 5; j++) {

                // This is a roster request, we create the response node
                var roster = new xmpp.Element('iq', {
                    id: stanza.attrs.id, // We copy the ID
                    to: stanza.attrs.from, // We send it back to the sender
                    type: 'set'
                });

                roster.c('query', {xmlns: 'jabber:iq:roster', ver: 'ver13'})// We add a children tag `query`, with the two attribute xmlns and ver
                    .c('item', { // We add a children 'Item'
                        jid: 'name' + j + '@test.com', // We create the jid
                        name: 'User ' + j, // We generate its name
                        subscription: 'both'
                    }).c('group').t('YourGroup'); // We add it to the 'YourGroup' group

                client.send(roster); // We send it back to the client
            }

        }
    }
    client.send(stanza);
}
else
...

In this example, I created 5 'fake' users and sent them to the user, you can apply this kind of loop to a real user list for instance.

A little screenshot:

Roster

As you can see, it create a 'YourGroup' group, and it adds all the 5 users to it.

User 0

When you try to start a conversation with one of this users, you can see the correctly generated jid in the top bar.

Hope it helps.

Alekos Filini
  • 324
  • 2
  • 7
  • I will try ant if it works i will match like a good answer ..I see you are familiar with xmpp..can you look at this question? http://stackoverflow.com/questions/32135501/node-xmpp-messages-are-not-sent-to-correct-destination – Worker1 Aug 25 '15 at 09:49
  • @Worker1 yeah, just give me a second – Alekos Filini Aug 25 '15 at 10:23