changeset 13:38ddd5888b8d

It’s now a client instead of a component. :)
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 03 Jun 2011 21:45:59 +0200
parents a0ed785d1b8d
children 7fdae201c1e2
files avatar.js configuration.js.example
diffstat 2 files changed, 28 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/avatar.js
+++ b/avatar.js
@@ -23,9 +23,20 @@
 var config = require('./configuration');
 
 var util = require('util');
+var fs = require('fs');
+var http = require('http');
 
 var xmpp = require('node-xmpp');
-var conn = new xmpp.Component(config);
+var conn = new xmpp.Client(config);
+var Element = require('ltx').Element;
+
+Element.prototype.getAttribute = function(name) {
+	return this.attrs[name];
+};
+
+process.addListener('uncaughtException', function (err) {
+	console.log('Uncaught exception (' + err + '), this should never happen:\n' + err.stack);
+});
 
 (function() {
 	var send = conn.send;
@@ -47,23 +58,12 @@ conn.on('stanza', function (stanza) {
 		onError(stanza);
 });
 
-conn._uniqueId = 42;
-conn.getUniqueId = function(suffix) {
-	return ++this._uniqueId + (suffix?(":"+suffix):"");
-};
-
-var Element = require('ltx').Element;
-
-Element.prototype.getAttribute = function(name) {
-	return this.attrs[name];
-};
-
-var fs = require('fs');
-var http = require('http');
-
-process.addListener('uncaughtException', function (err) {
-	console.log('\x1b[41;1mUncaught exception (' + err + '), this should never happen:\x1b[0m\n' + err.stack);
-});
+var getUniqueId = (function() {
+	var id = 0;
+	return function() {
+		return ++id;
+	};
+})();
 
 var extensions = {
 	png: 'image/png',
@@ -117,15 +117,12 @@ function onIq(stanza) {
 		} catch (e) {
 			var err = 'none';
 		}
-		svgError(res, 'Error during query of this user’s vCard: “'+err+'”.');
-		return;
+		return svgError(res, 'Error during query of this user’s vCard: “'+err+'”.');
 	}
 
 	var vCard = stanza.getChild('vCard', 'vcard-temp');
-	if (!vCard) {
-		svgError(res, 'Error: this user doesn’t have a vCard.');
-		return;
-	}
+	if (!vCard)
+		return svgError(res, 'Error: this user doesn’t have a vCard.');
 
 	try {
 		var photo = vCard.getChild('PHOTO', 'vcard-temp');
@@ -136,10 +133,8 @@ function onIq(stanza) {
 		} catch (e) {
 			if (config.guessType)
 				type = 'image/png'; // FIXME: use magic.
-			else {
-				svgError(res, 'Error: this user’s vCard doesn’t specify the MIME type of its avatar.');
-				return;
-			}
+			else
+				return svgError(res, 'Error: this user’s vCard doesn’t specify the MIME type of its avatar.');
 		}
 
 		var ext;
@@ -150,8 +145,7 @@ function onIq(stanza) {
 		// Here we don’t try to guess the extension even if the option is set.
 		if (ext === undefined) {
 			console.log('Unknown MIME type: '+type);
-			svgError(res, 'Error: this user’s avatar is in an unknown format.');
-			return;
+			return svgError(res, 'Error: this user’s avatar is in an unknown format.');
 		}
 
 		var binval = new Buffer(base64.replace(/\n/g, ''), 'base64');
@@ -181,7 +175,7 @@ function onError(stanza) {
 }
 
 var getVCard = function(jid, res) {
-	var id = conn.getUniqueId();
+	var id = getUniqueId();
 
 	var toSend = new Element('iq', {to: jid, from: config.jid, type: 'get', id: id})
 		.c('vCard', {xmlns: 'vcard-temp'})
@@ -246,10 +240,8 @@ http.createServer(function (req, res) {
 		var re = ee.re || new RegExp(config.webRoot + file + '$');
 		if (re.test(req.url)) {
 			fs.readFile(file, function(err, content) {
-				if (err) {
-					svgError(res, 'Error: ' + (ee.error || file + ' unavailable.'));
-					return;
-				}
+				if (err)
+					return svgError(res, 'Error: ' + (ee.error || file + ' unavailable.'));
 				res.writeHead(200, {'Content-Type': ee.mime || 'text/plain'});
 				res.end(content);
 			});
--- a/configuration.js.example
+++ b/configuration.js.example
@@ -20,16 +20,10 @@
 
 var config = exports;
 
-// The JID and password of the component, that have to be configured in
-// the host.
+// The JID and password of the account used.
 config.jid = 'avatar.example.org';
 config.password = 'hellohello';
 
-// The hostname or IP address and the port of the XMPP server hosting
-// the component.
-config.host = 'localhost';
-config.port = 5347;
-
 // Root of the webservice, useful if you want to proxy it.
 config.webRoot = '^/avatar/';