Created
April 23, 2015 23:58
-
-
Save metalaureate/08fb273eed35ac0b4f37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mutualSubscribe(source, friend, callback) { | |
var XMPP = require('stanza.io'); | |
var friend_jid = new XMPP.JID(friend.toLowerCase() + '@idelog.me'); | |
var source_jid = new XMPP.JID(source.toLowerCase() + '@idelog.me'); | |
var source_client = XMPP.createClient({ | |
jid: source_jid, | |
password: source.split("").reverse().join(""), | |
transport: 'bosh', | |
boshURL: config.get('xmpp.bosh_url') | |
}); | |
var friend_client = XMPP.createClient({ | |
jid: friend_jid, | |
password: friend.split("").reverse().join(""), | |
transport: 'bosh', | |
boshURL: config.get('xmpp.bosh_url') | |
}); | |
function notAlreadySubscribed(jid, client, callback) { | |
var sendSubscribe = true; | |
client.getRoster(function (error, roster) { | |
var contact = _.find(roster.roster.items, function (r) { | |
return r.jid.local == friend_jid.local; // pres.from.local | |
}); | |
if (contact) { | |
sendSubscribe = (contact.subscription !== 'both' || contact.subscription !== 'to') && !contact.subscriptionRequested; | |
} | |
}); | |
callback(null, sendSubscribe); | |
} | |
source_client.on('subscribe', function (pres) { | |
source_client.acceptSubscription(pres.from); // accept the subscription we received | |
notAlreadySubscribed(pres.from, source_client, function (err, sendSubscribe) { | |
source_client.subscribe(pres.from); // send a subscribe the other way | |
}); | |
}); | |
friend_client.on('subscribe', function (pres) { | |
friend_client.acceptSubscription(pres.from); // accept the subscription we received | |
notAlreadySubscribed(pres.from, friend_client, function (err, sendSubscribe) { | |
friend_client.subscribe(pres.from); // send a subscribe the other way | |
}); | |
}); | |
source_client.on('session:started', function () { // STEP 1 | |
console.log('xmpp session started for ' + source_jid.local); | |
source_client.sendPresence(); | |
source_client.subscribe(friend_jid); | |
friend_client.connect(); //-- friend logs in after source has sent invite | |
}); | |
friend_client.on('session:started', function () { // STEP 1 | |
console.log('xmpp session started for ' + friend_client.local); | |
friend_client.sendPresence(); | |
friend_client.subscribe(source_jid); | |
}); | |
source_client.on('subscribed', function (result) { // STEP 4 | |
console.log('xmpp', source_jid.local + ' is now subscribed to ' + friend_jid.local); | |
// clearTimeout(source_session_timeout); | |
source_client.disconnect(); | |
}); | |
friend_client.on('subscribed', function (result) { // STEP 4 | |
console.log('xmpp', friend_jid.local + ' is now subscribed to ' + source_jid.local); | |
// clearTimeout(friend_session_timeout); | |
friend_client.disconnect(); | |
}); | |
friend_client.on('session:end', function () { // STEP 5 | |
console.log(friend_jid.local, 'xmpp session ended'); | |
// -- check if we really succeeded | |
callback(null, null); | |
}); | |
source_client.on('session:end', function () { // STEP 5 | |
console.log(source_jid.local, 'xmpp session ended'); | |
}); | |
source_client.connect(); | |
} | |
mutualSubscribe('gunwinggu', 'metalaureate', function (error, result) { | |
console.log('roster processed'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment