Created
April 24, 2015 20:04
-
-
Save metalaureate/f8a4c80e52c85d8fc8bd 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 = friend.toLowerCase() + '@idelog.me'; | |
var source_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) { | |
if (error) throw error; | |
var contact = (roster.roster) ? _.find(roster.roster.items, function (r) { | |
return r.jid.bare == jid; // pres.from.local | |
}) : null; | |
if (contact) { | |
sendSubscribe = (contact.subscription !== 'both' ) && !contact.subscriptionRequested; //|| contact.subscription !== 'from' | |
} | |
}); | |
callback(null, sendSubscribe); | |
} | |
source_client.on('subscribe', function (pres) { | |
source_client.acceptSubscription(pres.from.bare); // accept the subscription we received | |
console.log('4. ' + source + ' accepts subscription from ' + pres.from.bare); | |
/* | |
notAlreadySubscribed(pres.from.bare, source_client, function (err, sendSubscribe) { | |
if (sendSubscribe) { | |
console.log('4b. ' + source + ' sends subscribe to ' + pres.from.bare); | |
source_client.subscribe(pres.from.bare); // send a subscribe the other way | |
} | |
}); | |
*/ | |
}); | |
friend_client.on('subscribe', function (pres) { | |
friend_client.acceptSubscription(pres.from.bare); // accept the subscription we received | |
console.log('2. ' + friend + ' accepts subscription from ' + pres.from.bare); | |
notAlreadySubscribed(pres.from.bare, friend_client, function (err, sendSubscribe) { | |
if (sendSubscribe) { | |
console.log('3. ' + friend + ' sends subscribe to ' + pres.from.bare); | |
friend_client.subscribe(pres.from.bare); // send a subscribe the other way | |
} | |
}); | |
}); | |
source_client.on('session:started', function () { // STEP 1 | |
console.log('xmpp session started for ' + source_jid); | |
source_client.sendPresence(); | |
console.log('1.' + source + ' sends subscribe to ' + friend_jid); | |
source_client.subscribe(friend_jid); | |
}); | |
friend_client.on('session:started', function () { // STEP 1 | |
console.log('xmpp session started for ' + friend_jid); | |
friend_client.sendPresence(); | |
// friend_client.subscribe(source_jid); | |
}); | |
source_client.on('subscribed', function (result) { // STEP 4 | |
console.log('!!!', '6. ' + friend_jid + ' is now subscribed to ' + source_jid); | |
}); | |
friend_client.on('subscribed', function (result) { // STEP 4 | |
console.log('!!!', '5. ' + source_jid + ' is now subscribed to ' + friend_jid); | |
// source_client.sendPresence(); | |
// source_client.disconnect(); | |
// friend_client.disconnect(); | |
// clearTimeout(friend_session_timeout); | |
}); | |
friend_client.on('session:end', function () { // STEP 5 | |
console.log(friend_jid, 'xmpp session ended'); | |
// -- check if we really succeeded | |
callback(null, null); | |
}); | |
source_client.on('session:end', function () { // STEP 5 | |
console.log(source_jid, 'xmpp session ended'); | |
//friend_client.disconnect(); | |
}); | |
source_client.connect(); | |
friend_client.connect(); | |
} | |
mutualSubscribe('metalaureate', 'www', function (error, result) { | |
console.log('subscription processed'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment