From 126c1dde7ce74383eb08e47278cbd05eeeb8c198 Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Wed, 11 Aug 2010 10:12:24 +0200 Subject: [PATCH] basic client --- lib/xmpp-client/client.js | 79 ++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index ef1276c..6281e0f 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -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;