1
0
Fork 0
master
Matthieu Lalonde 14 years ago committed by Mathieu Lecarme
parent 0761cd3b87
commit 681fc3f890

@ -66,6 +66,7 @@ var BasicClient = function(params, callback) {
} }
}); });
this.xmpp.addListener('online', function() { this.xmpp.addListener('online', function() {
jabber.emit('online');
callback.apply(jabber); callback.apply(jabber);
}); });
}; };

@ -3,56 +3,8 @@ var sys = require('sys'),
colors = require('colors'), colors = require('colors'),
events = require('events'), events = require('events'),
Room = require('./room').Room, Room = require('./room').Room,
Pubsub = require('./pubsub').Pubsub; Pubsub = require('./pubsub').Pubsub,
BasicClient = require('./basic-client').BasicClient;
var BasicClient = function(params) {
events.EventEmitter.call(this);
this._iq = 0;
var jabber = this;
this.jid = new xmpp.JID(params.jid);
this.host = (params.host == null) ? this.jid.domain : params.host;
this.xmpp = new xmpp.Client(params);
this.xmpp.addListener('rawStanza', function(stanza) {
//sys.debug("RAW: "[jabber.color] + stanza.toString().white);
});
this.xmpp.addListener('authFail', function() {
sys.error("[Error] Jabber : Authentication failure");
process.exit(1);
});
this.xmpp.addListener('error', function(e) {
sys.error(e);
process.exit(1);
});
};
sys.inherits(BasicClient, events.EventEmitter);
exports.BasicClient = BasicClient;
BasicClient.prototype.message = function(to, message) {
this.xmpp.send(new xmpp.Element('message', {
to: to,
type: 'chat'})
.c('body').t(message));
};
BasicClient.prototype.presence = function(type) {
this.xmpp.send(new xmpp.Element('presence', (type != null) ? {type: type} : {}).tree());
};
BasicClient.prototype.iq = function(to, query, callback) {
var n = 'node' + this._iq++;
this._iqCallback[n] = callback;
var attrs = {
type: 'get',
id: n
};
if(to != null) {
attrs.to = to;
};
this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree());
return n;
};
var Client = function(params, callback) { var Client = function(params, callback) {
BasicClient.call(this, params, callback); BasicClient.call(this, params, callback);
@ -117,7 +69,6 @@ var Client = function(params, callback) {
var event_ = stanza.getChild('event', 'http://jabber.org/protocol/pubsub#event'); var event_ = stanza.getChild('event', 'http://jabber.org/protocol/pubsub#event');
sys.debug(event_.toString().green); sys.debug(event_.toString().green);
if(event_ != null) { if(event_ != null) {
sys.debug('beuha'.green);
jabber.emit('pubsub:event', from, event_.getChild('items').attrs.node, event_, stanza); jabber.emit('pubsub:event', from, event_.getChild('items').attrs.node, event_, stanza);
} else { } else {
jabber.emit('message', from, stanza.getChild('body').getText(), stanza); jabber.emit('message', from, stanza.getChild('body').getText(), stanza);

@ -0,0 +1,32 @@
var sys = require('sys'),
xmpp = require('node-xmpp'),
colors = require('colors'),
BasicClient = require('./basic-client').BasicClient;
var Client = function(params, callback) {
var jabber = this;
this.roster = {};
this.presences = {};
BasicClient.call(this, params, function() {
this.presence();
this.askForRoster(function(roster) {
callback.apply(this);
});
});
};
sys.inherits(Client, BasicClient);
exports.Client = Client;
Client.prototype.askForRoster = function(callback) {
var jabber = this;
this.iq(null, new xmpp.Element('query', {xmlns: 'jabber:iq:roster'}), function(iq) {
iq.getChild('query', 'jabber:iq:roster').getChildren('item').forEach(function(child) {
jabber.roster[child.attrs.jid] = {
name: child.attrs.jid,
subscription: child.attrs.subscription};
});
jabber.emit('roster', jabber.roster);
callback.call(jabber, jabber.roster);
});
};
Loading…
Cancel
Save