1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.9 KiB

14 years ago
var sys = require('sys'),
xmpp = require('node-xmpp'),
colors = require('colors'),
events = require('events');
function Pubsub(client, to) {
this.client = client;
if(to == null) {
to = 'pubsub.' + this.client.jid.domain;
}
this.to = to;
14 years ago
this._eventCallback = {};
var pubsub = this;
this.client.addListener('pubsub:event', function(from, node, event_, stanza) {
if(from == pubsub.to) {
// sys.debug('a pubsub event'.yellow);
14 years ago
var cb = pubsub._eventCallback[node];
if(cb != null) {
cb.call(pubsub, event_, stanza);
}
}
});
14 years ago
};
sys.inherits(Pubsub, events.EventEmitter);
exports.Pubsub = Pubsub;
Pubsub.prototype.createTree = function() {
/*sys.debug(new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
14 years ago
.c('create', {node: '/home/' + this.client.jid.domain + '/user'}).up()
.c('configure')
.tree());*/
14 years ago
this.client.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('create', {node: '/home/' + this.client.jid.domain + '/user'}).up()
.c('configure')
.tree(),
function(stanza) {
// sys.debug(stanza.toString().yellow);
// sys.debug(stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub').toString.yellow);
14 years ago
}
);
};
Pubsub.prototype.disco = function(callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info'}),
function(iq) {
callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#info'));
}
);
};
Pubsub.prototype.discoNode = function(node, callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info', node: node}),
function(iq) {
callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#info'));
}
);
};
Pubsub.prototype.subscriptions = function(callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}).c('subscriptions'),
function(iq) {
callback.call(jabber, iq
.getChild('pubsub', 'http://jabber.org/protocol/pubsub')
.getChild('subscriptions')
.getChildren('subscription')
);
}
);
};
Pubsub.prototype.nodeSubscriptions = function(node, callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}).c('subscriptions', {node: node}),
function(iq) {
callback.call(jabber, iq
.getChild('pubsub', 'http://jabber.org/protocol/pubsub')
.getChild('subscriptions')
.getChildren('subscription')
);
}
);
};
Pubsub.prototype.discoNodeItems = function(node, callback) {
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: node}),
function(iq) {
callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#items').getChildren('item'));
}
);
};
Pubsub.prototype.discoNodes = function(callback) {
14 years ago
var jabber = this.client;
this.client.iq(this.to,
new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items'}),
function(iq) {
callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#items').getChildren('item'));
}
);
};
Pubsub.prototype.node = function(node, callback) {
var exist = false;
var pubsub = this;
this.discoNodes(function(items) {
14 years ago
items.forEach(function(item) {
if(item.attrs.node == node) { exist = true; }
});
if(! exist) { pubsub.createNode(node, callback); }
else { callback.call(pubsub.client); }
});
};
Pubsub.prototype.createNode = function(node, callback) {
var jabber = this.client;
this.client.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('create', {node: node}).up()
.c('configure')
.tree(),
function(stanza) {
var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub');
if(pubsub != null) {
// sys.debug(pubsub.toString().yellow);
14 years ago
callback.call(jabber);
}
}
);
};
Pubsub.prototype.publish = function(node, content) {
14 years ago
this.client.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('publish', {node: node})
.c('item')
.cnode(content)
.tree(),
function(iq) {
// sys.debug('PUBLISH : ' + iq);
// sys.debug('just published'.yellow);
}
);
14 years ago
};
Pubsub.prototype.suscribe = function(node, onMessage, onSuscribed) {
14 years ago
var jabber = this.client;
var pubsub = this;
14 years ago
jabber.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('subscribe', {
14 years ago
node: node,
jid: jabber.jid.user + '@' + jabber.jid.domain
})
.tree(),
function(iq) {
// sys.debug(('Suscribe to ' + node).yellow);
14 years ago
pubsub._eventCallback[node] = onMessage;
var s = iq.getChild('pubsub', 'http://jabber.org/protocol/pubsub').attrs;
onSuscribed.call(jabber, s.subscription, s.subid);
14 years ago
}
);
};