1
0
Fork 0

basic client

master
Matthieu Lalonde 15 years ago committed by Mathieu Lecarme
parent ac4af8b2c4
commit 126c1dde7c

@ -3,20 +3,12 @@ var sys = require('sys'),
colors = require('colors'),
events = require('events');
var Client = function(params, callback) {
var BasicClient = function(params) {
events.EventEmitter.call(this);
this.color = (params.color != null) ? params.color : 'blue';
this.debug = true;
this._iq = 0;
var jabber = this;
this.jid = new xmpp.JID(params.jid);
this.host = (params.host == null) ? this.jid.domain : params.host;
this.rooms = {};
this._isReady = false;
this._iq = 0;
this._iqHandler = {};
this._iqCallback = {};
this.presences = {};
this.roster = {};
this.xmpp = new xmpp.Client(params);
this.xmpp.addListener('rawStanza', function(stanza) {
//sys.debug("RAW: "[jabber.color] + stanza.toString().white);
@ -29,6 +21,47 @@ var Client = function(params, callback) {
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 = 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());
};
var Client = function(params, callback) {
BasicClient.call(this, params, callback);
var jabber = this;
this.color = (params.color != null) ? params.color : 'blue';
this.debug = true;
this.rooms = {};
this._isReady = false;
this._iqHandler = {};
this._iqCallback = {};
this.presences = {};
this.roster = {};
this.xmpp.addListener('stanza', function(stanza) {
//sys.debug('STANZA: '[jabber.color] + ('<' + stanza.name + '> ').bold[jabber.color] + stanza);
if(stanza.name == 'iq') {
@ -153,7 +186,7 @@ var Client = function(params, callback) {
});
};
sys.inherits(Client, events.EventEmitter);
sys.inherits(Client, BasicClient);
exports.Client = Client;
Client.prototype._debug = function(txt) {
@ -166,13 +199,6 @@ Client.prototype.registerIqHandler = function(xmlns, action) {
this._iqHandler[xmlns] = action;
};
Client.prototype.message = function(to, message) {
this.xmpp.send(new xmpp.Element('message', {
to: to,
type: 'chat'})
.c('body').t(message));
};
Client.prototype.askForRoster = function(callback) {
var jabber = this;
this.iq(null, new xmpp.Element('query', {xmlns: 'jabber:iq:roster'}), function(iq) {
@ -191,19 +217,6 @@ Client.prototype.askForRoster = function(callback) {
});
};
Client.prototype.iq = function(to, query, callback) {
var n = 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());
};
Client.prototype.iqSet = function(to, query, callback) {
var n = this._iq++;
if(callback != null) {
@ -228,10 +241,6 @@ Client.prototype.resultIq = function(iqGet, result) {
}).cnode(result).tree());
};
Client.prototype.presence = function(type) {
this.xmpp.send(new xmpp.Element('presence', (type != null) ? {type: type} : {}).tree());
};
Client.prototype.canonicalRoomName = function(room) {
if(room.indexOf('@') > 0) {
return room;

Loading…
Cancel
Save