# HG changeset patch # User Emmanuel Gil Peyrot # Date 1307118746 -7200 # Node ID a0ed785d1b8d80c6a4b2aaaf282afe440f5838ea # Parent 1cb2279e3f30fbe4f30f3fdac80651188b938d5e Remove xmpp.js compatibility and fix the node-xmpp one. diff --git a/avatar.js b/avatar.js --- 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('\n'); res.write('\n'); - res.write('\t'+message+'\n'); + res.write('\t'+message+'\n'); res.write('\t\n'); res.write('\tError\n'); res.end('\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);