changeset 25:ba0edeb9ba05 default tip

Put the repository more in evidence, add an in-memory s2s blacklist and fix a few minor stuff.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 02 Dec 2012 13:19:54 +0100
parents e32ac29df320
children
files avatar.js index.xhtml
diffstat 2 files changed, 35 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/avatar.js
+++ b/avatar.js
@@ -26,12 +26,14 @@ var util = require('util');
 var fs = require('fs');
 var http = require('http');
 
+var blacklist = {};
+
 if (config.useGravatar)
 	var hash = require('crypto').createHash;
 
 var xmpp = require('node-xmpp');
 var conn = new xmpp.Client(config);
-var Element = require('ltx').Element;
+var Element = xmpp.Element;
 
 Element.prototype.getAttribute = function(name) {
 	return this.attrs[name];
@@ -129,7 +131,7 @@ if (config.useGravatar)
 			var ext;
 			var type = r.headers['content-type']
 			for (var i in config.extensions)
-				if (type == config.extensions[i])
+				if (type === config.extensions[i])
 					ext = i;
 
 			var file = fs.createWriteStream(config.directory + '/' + to + '.' + ext);
@@ -180,13 +182,18 @@ function onIq(stanza) {
 	var res = sent[id].res;
 	delete sent[id];
 
-	if (type == 'error') {
+	if (type === 'error') {
 		try {
-			var err = stanza.getChild('error').getChild().name;
+			var err = stanza.getChild('error').children[0].name;
 		} catch (e) {
 			var err = 'none';
 		}
 
+		if (err === 'remote-server-not-found') {
+			var server = to.substr(to.indexOf('@') + 1);
+			blacklist[server] = true;
+		}
+
 		return noAvatar(res, to, 'Error during query of this user’s vCard: “'+err+'”.');
 	}
 
@@ -209,7 +216,7 @@ function onIq(stanza) {
 
 		var ext;
 		for (var i in config.extensions)
-			if (type == config.extensions[i])
+			if (type === config.extensions[i])
 				ext = i;
 
 		// Here we don’t try to guess the extension even if the option is set.
@@ -230,7 +237,7 @@ function onIq(stanza) {
 }
 
 function onError(stanza) {
-	if (stanza.getAttribute('type') == 'error')
+	if (stanza.getAttribute('type') === 'error')
 		return;
 
 	var from = stanza.getAttribute('to');
@@ -258,12 +265,18 @@ var getVCard = function(jid, res) {
 
 var showImage = function(jid, res) {
 	var extension = jids[jid];
-	var file = config.directory+'/'+jid+'.'+extension;
+	var file = config.directory + '/' + jid + '.' + extension;
 	var now = new Date;
-	res.writeHead(200, {'Content-Type': config.extensions[extension], Expires: new Date(config.expire * 1000 + now.getTime())});
+
+	res.writeHead(200, {
+		'Content-Type': config.extensions[extension],
+		'Expires': new Date(config.expire * 1000 + now.getTime())
+	});
+
 	fs.readFile(file, function(err, data) {
 		res.end(data);
 	});
+
 	fs.stat(file, function(err, stats) {
 		if (err) {
 			console.log('Error when stat on “'+file+'”.');
@@ -275,6 +288,7 @@ var showImage = function(jid, res) {
 		if (now - last > config.expire * 1000)
 			getVCard(jid, res);
 	});
+
 	return;
 }
 
@@ -289,7 +303,12 @@ fs.readdir(config.directory, function(er
 });
 
 http.createServer(function (req, res) {
-	util.log('Connection from ' + (req.headers['x-forwarded-for'] || req.client.remoteAddress) + ' (' + req.headers['user-agent'] + ') to ' + req.method.toLocaleLowerCase() + ' “' + req.url + '”.');
+	util.log('Connection from ' +
+	         (req.headers['x-forwarded-for'] || req.client.remoteAddress) +
+		 ' (' + req.headers['user-agent'] +
+		 ') to ' + req.method.toLocaleLowerCase() +
+		 ' “' + req.url +
+		 '” from ' + (req.headers['referer'] || 'here') + '.');
 
 	var easterEggs = {
 		source: {
@@ -342,9 +361,14 @@ http.createServer(function (req, res) {
 		return;
 	}
 
+	var server = jid.substr(jid.indexOf('@') + 1);
+
 	if (jid in jids) {
 		showImage(jid, res);
 		return;
+	} else if (server in blacklist) {
+		noAvatar(res, jid, 's2s error');
+		return;
 	}
 
 	getVCard(jid, res);
--- a/index.xhtml
+++ b/index.xhtml
@@ -19,8 +19,8 @@
 		<nav>
 			<ul>
 				<li><a href="README">README</a></li>
-				<li><a href="source/code">source code</a></li>
-				<li><a href="http://hg.linkmauve.fr/avatar">Mercurial repository</a></li>
+				<li><a href="http://hg.linkmauve.fr/eldonilo/avatar">Mercurial repository</a></li>
+				<li><a href="source/code">Currently running source code</a></li>
 			</ul>
 		</nav>
 	</body>