changeset 12:a0ed785d1b8d

Remove xmpp.js compatibility and fix the node-xmpp one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 03 Jun 2011 18:32:26 +0200
parents 1cb2279e3f30
children 38ddd5888b8d
files avatar.js
diffstat 1 files changed, 42 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/avatar.js
+++ b/avatar.js
@@ -22,49 +22,41 @@
 
 var config = require('./configuration');
 
-try {
-	var xmpp = require('node-xmpp');
-	var conn = new xmpp.Component(config);
+var util = require('util');
+
+var xmpp = require('node-xmpp');
+var conn = new xmpp.Component(config);
 
-	conn.on('stanza', function (stanza) {
-		if (stanza.is('iq'))
-			onIq(stanza);
-		else
-			onError(stanza);
-	});
+(function() {
+	var send = conn.send;
+	conn.send = function(s) {
+		util.log('Sent: ' + s + '');
+		send.call(conn, s);
+	};
+})();
 
-	conn._uniqueId = 42;
-	conn.getUniqueId = function(suffix) {
-		return ++this._uniqueId + (suffix?(":"+suffix):"");
-	};
-
-	var Element = require('ltx').Element;
-} catch (e) {
-	var xmpp = require('xmpp');
-	var conn = new xmpp.Connection(config.host, config.port);
+conn.on('online', function () {
+	console.log('Connected');
+});
 
-	conn.log = function (_, m) { console.log(m); };
-
-	conn.connect(config.jid, config.password, function (status, condition) {
-		if(status == xmpp.Status.CONNECTED) {
-			conn.addHandler(onIq, null, 'iq', null, null,  null);
-			conn.addHandler(onError, null, 'message', null, null,  null);
-			conn.addHandler(onError, null, 'presence', null, null,  null);
-		} else
-			conn.log(xmpp.LogLevel.DEBUG, "New connection status: " + status + (condition?(" ("+condition+")"):""));
-	});
+conn.on('stanza', function (stanza) {
+	util.log('Recv: ' + stanza + '');
+	if (stanza.is('iq'))
+		onIq(stanza);
+	else
+		onError(stanza);
+});
 
-	xmpp.StanzaBuilder.prototype.cnode = function (stanza)
-	{
-		var parent = this.last_node[this.last_node.length-1];
-		parent.tags.push(stanza);
-		parent.children.push(stanza);
-		this.last_node.push(stanza);
-		return this;
-	};
+conn._uniqueId = 42;
+conn.getUniqueId = function(suffix) {
+	return ++this._uniqueId + (suffix?(":"+suffix):"");
+};
 
-	var Element = xmpp.StanzaBuilder;
-}
+var Element = require('ltx').Element;
+
+Element.prototype.getAttribute = function(name) {
+	return this.attrs[name];
+};
 
 var fs = require('fs');
 var http = require('http');
@@ -88,18 +80,17 @@ var svgError = function(res, message) {
 	res.writeHead(500, {'Content-Type': 'image/svg+xml'});
 	res.write('<?xml version="1.0" encoding="UTF-8"?>\n');
 	res.write('<svg xmlns="http://www.w3.org/2000/svg" viewBox="-32 -36 64 64">\n');
-	res.write('\t<desc>'+message+'</desc>\n');
+	res.write('\t<title>'+message+'</title>\n');
 	res.write('\t<rect x="-32" y="-36" width="64" height="64" fill="white"/>\n');
 	res.write('\t<text font-family="sans-serif" font-weight="bold" text-anchor="middle">Error</text>\n');
 	res.end('</svg>\n');
 }
 
 var makeError = function(response) {
-	response.attr.type = 'error';
+	response.attrs.type = 'error';
 
-	var error = new Element('error', {type: 'cancel'});
-	error.c('feature-not-implemented', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
-	response.cnode(error);
+	response.c('error', {type: 'cancel'})
+		.c('feature-not-implemented', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'});
 
 	return response;
 }
@@ -110,16 +101,12 @@ function onIq(stanza) {
 	var to = stanza.getAttribute('from');
 	var id = stanza.getAttribute('id');
 
-	var response;
+	var response = new Element('iq', {to: to, from: from, type: 'result'});
 	if (id)
-		response = new Element('iq', {to: to, from: from, type: 'result', id: id});
-	else
-		response = new Element('iq', {to: to, from: from, type: 'result'});
+		response.attrs.id = id;
 
-	if (!sent[id]) {
-		conn.send(makeError(response));
-		return;
-	}
+	if (!sent[id])
+		return conn.send(makeError(response));
 
 	var res = sent[id];
 	delete sent[id];
@@ -186,11 +173,9 @@ function onError(stanza) {
 	var to = stanza.getAttribute('from');
 	var id = stanza.getAttribute('id');
 
-	var response;
+	var response = new Element(stanza.name, {to: to, from: from});
 	if (id)
-		response = new Element(stanza.name, {to: to, from: from, id: id});
-	else
-		response = new Element(stanza.name, {to: to, from: from});
+		response.attrs.id = id;
 
 	conn.send(makeError(response));
 }
@@ -199,7 +184,8 @@ var getVCard = function(jid, res) {
 	var id = conn.getUniqueId();
 
 	var toSend = new Element('iq', {to: jid, from: config.jid, type: 'get', id: id})
-		.c('vCard', {xmlns: 'vcard-temp'});
+		.c('vCard', {xmlns: 'vcard-temp'})
+	.up();
 
 	conn.send(toSend);