Skip to content

Instantly share code, notes, and snippets.

@metalaureate
Created April 24, 2015 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metalaureate/a1f7a0a94264b9960d01 to your computer and use it in GitHub Desktop.
Save metalaureate/a1f7a0a94264b9960d01 to your computer and use it in GitHub Desktop.
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;
console.log('roster','looking up '+jid +' for '+client.jid.bare,contact);
if (contact) {
sendSubscribe = (contact.subscription !== 'both' || (contact.subscription !== 'to')) && !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);
});
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();
});
source_client.on('subscribed', function (result) { // STEP 4
console.log('!!!', '6. ' + source + ' is now subscribed to ' + friend);
});
friend_client.on('subscribed', function (result) { // STEP 4
console.log('!!!', '5. ' + friend + ' is now subscribed to ' + source);
// 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