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.

219 lines
6.0 KiB

14 years ago
/*
A very simple xmpp client
*/
var sys = require('sys'),
xmpp = require('node-xmpp'),
util = require('util'),
colors = require('colors'),
events = require('events');
14 years ago
var BasicClient = function(params, callback) {
events.EventEmitter.call(this);
this.idle = (new Date().getTime());
14 years ago
this._iq = 0;
14 years ago
this._iqCallback = {};
this._iqHandler = {};
14 years ago
var jabber = this;
this.params = params;
14 years ago
var jid = new xmpp.JID(params.jid);
this.params.host = (params.host == null) ? jid.domain : params.host;
this.params.nickname = (params.resource == null) ? jid.user : params.resource;
14 years ago
this.xmpp = new xmpp.Client(params);
/*
14 years ago
this.xmpp.addListener('rawStanza', function(stanza) {
sys.debug("RAW: "[jabber.color] + stanza.toString().white);
14 years ago
});
*/
this.xmpp.addListener('authFail', function(e) {
jabber.emit('authFail', e);
14 years ago
});
this.xmpp.addListener('error', function(e) {
jabber.emit('error', e);
14 years ago
});
14 years ago
this.xmpp.addListener('stanza', function(stanza) {
switch(stanza.name) {
case 'iq':
switch(stanza.attrs.type) {
case 'error':
var q = stanza.getChild('query');
if(q !== undefined) {
if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) {
return jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza);
}
}
if (typeof jabber._iqCallback[stanza.attrs.id] !== "undefined") {
jabber._iqCallback[stanza.attrs.id].error.apply(jabber, [stanza]);
}
14 years ago
break;
case 'result':
if (typeof jabber._iqCallback[stanza.attrs.id] === "undefined") {
var q = stanza.getChild('query');
if(q !== undefined) {
if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) {
return jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza);
}
}
} else if (typeof jabber._iqCallback[stanza.attrs.id] !== "undefined") {
jabber._iqCallback[stanza.attrs.id].success.apply(jabber, [stanza]);
}
14 years ago
break;
default:
jabber.emit('iq', stanza);
14 years ago
var q = stanza.getChild('query');
14 years ago
if(q == undefined) {
var q = stanza.getChild('time');
if(q !== undefined) {
if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) {
jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza);
} else {
jabber.emit('iq:unknow', stanza);
}
} else {
if (typeof jabber._iqCallback[stanza.attrs.id] !== "undefined") {
jabber._iqCallback[stanza.attrs.id].success.apply(jabber, [stanza]);
} else if(stanza.attrs.xmlns != null && jabber._iqHandler[stanza.attrs.xmlns] != null) {
jabber._iqHandler[stanza.attrs.xmlns].call(jabber, stanza);
} else {
jabber.emit('iq:unknow', stanza);
}
}
14 years ago
} else {
if (typeof jabber._iqCallback[stanza.attrs.id] !== "undefined") {
jabber._iqCallback[stanza.attrs.id].success.apply(jabber, [stanza]);
} else if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) {
14 years ago
jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza);
} else {
jabber.emit('iq:unknow', stanza);
}
14 years ago
}
14 years ago
break;
}
break;
case 'presence':
var newPresence = false;
14 years ago
if(stanza.attrs.type == 'error') {
jabber.emit('presence:error', stanza);
} else {
// Skip self presence
if (jabber.jid.user + "@" + jabber.jid.domain + "/" + jabber.jid.resource === stanza.attrs.from) {
return false;
}
try {
// This is a presences for a conference
if (stanza.attrs.from.indexOf('conference') === -1 || stanza.attrs.from.indexOf('conference') < stanza.attrs.from.indexOf('@')) {
var user = stanza.attrs.from.toString();
if (stanza.attrs.type === "unavailable") {
if (typeof jabber.presences[user] !== "undefined") {
delete jabber.presences[user];
}
} else {
jabber.presences[user] = stanza;
}
}
jabber.emit('presence', stanza);
} catch (err) {
console.log("Something went wrong parsing presences: " + err.toString());
console.log(stanza.toString());
console.log(err.stack);
}
14 years ago
}
break;
case 'message':
jabber.emit('message', stanza);
break;
default:
console.log(stanza.toString());
break;
14 years ago
}
});
14 years ago
this.xmpp.addListener('online', function() {
14 years ago
jabber.jid = this.jid;
14 years ago
jabber.emit('online');
14 years ago
callback.apply(jabber);
});
};
sys.inherits(BasicClient, events.EventEmitter);
exports.BasicClient = BasicClient;
BasicClient.prototype.unidle = function() {
this.idle = (new Date().getTime());
};
14 years ago
BasicClient.prototype.message = function(to, message) {
this.xmpp.send(new xmpp.Element('message', {
to: to,
type: 'chat'})
.c('body').t(message));
this.unidle();
14 years ago
};
BasicClient.prototype.presence = function(status, message)
{
if (typeof status === "undefined")
{
var status = "";
}
if (typeof message === "undefined")
{
var message = "";
}
this.xmpp.send(new xmpp.Element('presence', { type: 'chat'}).
c('show').t(status).up().
c('status').t(message)
);
14 years ago
};
BasicClient.prototype.iq = function(to, query, callback, error, type) {
14 years ago
if(error == undefined) error = function(stanza) { sys.error((this.jid + " : " + stanza.toString()).red);};
14 years ago
var n = 'node' + this._iq++;
14 years ago
this._iqCallback[n] = {};
this._iqCallback[n].success = callback;
this._iqCallback[n].error = error;
if (typeof type === "undefined") {
var type = "get";
}
14 years ago
var attrs = {
type: type,
14 years ago
id: n
};
if(to != null) {
attrs.to = to;
};
this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree());
return n;
};
14 years ago
14 years ago
/*
Answer an iq query
*/
BasicClient.prototype.resultIq = function(iqGet, result) {
if (typeof iqGet === "undefined" || typeof result === "string") {
this.xmpp.send(result);
} else {
this.xmpp.send(new xmpp.Element('iq', {
type: 'result',
from: iqGet.attrs.to,
to: iqGet.attrs.from,
id: iqGet.attrs.id
}).cnode(result).tree());
}
14 years ago
};
14 years ago
BasicClient.prototype.registerIqHandler = function(xmlns, action) {
this._iqHandler[xmlns] = action;
};