Mercurial > eldonilo > avatar
comparison avatar.js @ 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 |
comparison
equal
deleted
inserted
replaced
12:a0ed785d1b8d | 13:38ddd5888b8d |
---|---|
21 'use strict'; | 21 'use strict'; |
22 | 22 |
23 var config = require('./configuration'); | 23 var config = require('./configuration'); |
24 | 24 |
25 var util = require('util'); | 25 var util = require('util'); |
26 var fs = require('fs'); | |
27 var http = require('http'); | |
26 | 28 |
27 var xmpp = require('node-xmpp'); | 29 var xmpp = require('node-xmpp'); |
28 var conn = new xmpp.Component(config); | 30 var conn = new xmpp.Client(config); |
31 var Element = require('ltx').Element; | |
32 | |
33 Element.prototype.getAttribute = function(name) { | |
34 return this.attrs[name]; | |
35 }; | |
36 | |
37 process.addListener('uncaughtException', function (err) { | |
38 console.log('[41;1mUncaught exception (' + err + '), this should never happen:[0m\n' + err.stack); | |
39 }); | |
29 | 40 |
30 (function() { | 41 (function() { |
31 var send = conn.send; | 42 var send = conn.send; |
32 conn.send = function(s) { | 43 conn.send = function(s) { |
33 util.log('Sent: [1;32m' + s + '[0m'); | 44 util.log('Sent: [1;32m' + s + '[0m'); |
45 onIq(stanza); | 56 onIq(stanza); |
46 else | 57 else |
47 onError(stanza); | 58 onError(stanza); |
48 }); | 59 }); |
49 | 60 |
50 conn._uniqueId = 42; | 61 var getUniqueId = (function() { |
51 conn.getUniqueId = function(suffix) { | 62 var id = 0; |
52 return ++this._uniqueId + (suffix?(":"+suffix):""); | 63 return function() { |
53 }; | 64 return ++id; |
54 | 65 }; |
55 var Element = require('ltx').Element; | 66 })(); |
56 | |
57 Element.prototype.getAttribute = function(name) { | |
58 return this.attrs[name]; | |
59 }; | |
60 | |
61 var fs = require('fs'); | |
62 var http = require('http'); | |
63 | |
64 process.addListener('uncaughtException', function (err) { | |
65 console.log('\x1b[41;1mUncaught exception (' + err + '), this should never happen:\x1b[0m\n' + err.stack); | |
66 }); | |
67 | 67 |
68 var extensions = { | 68 var extensions = { |
69 png: 'image/png', | 69 png: 'image/png', |
70 svg: 'image/svg+xml', | 70 svg: 'image/svg+xml', |
71 jpg: 'image/jpeg', | 71 jpg: 'image/jpeg', |
115 try { | 115 try { |
116 var err = stanza.getChild('error').getChild().name; | 116 var err = stanza.getChild('error').getChild().name; |
117 } catch (e) { | 117 } catch (e) { |
118 var err = 'none'; | 118 var err = 'none'; |
119 } | 119 } |
120 svgError(res, 'Error during query of this user’s vCard: “'+err+'”.'); | 120 return svgError(res, 'Error during query of this user’s vCard: “'+err+'”.'); |
121 return; | |
122 } | 121 } |
123 | 122 |
124 var vCard = stanza.getChild('vCard', 'vcard-temp'); | 123 var vCard = stanza.getChild('vCard', 'vcard-temp'); |
125 if (!vCard) { | 124 if (!vCard) |
126 svgError(res, 'Error: this user doesn’t have a vCard.'); | 125 return svgError(res, 'Error: this user doesn’t have a vCard.'); |
127 return; | |
128 } | |
129 | 126 |
130 try { | 127 try { |
131 var photo = vCard.getChild('PHOTO', 'vcard-temp'); | 128 var photo = vCard.getChild('PHOTO', 'vcard-temp'); |
132 var base64 = photo.getChild('BINVAL', 'vcard-temp').getText(); | 129 var base64 = photo.getChild('BINVAL', 'vcard-temp').getText(); |
133 | 130 |
134 try { | 131 try { |
135 var type = photo.getChild('TYPE', 'vcard-temp').getText(); | 132 var type = photo.getChild('TYPE', 'vcard-temp').getText(); |
136 } catch (e) { | 133 } catch (e) { |
137 if (config.guessType) | 134 if (config.guessType) |
138 type = 'image/png'; // FIXME: use magic. | 135 type = 'image/png'; // FIXME: use magic. |
139 else { | 136 else |
140 svgError(res, 'Error: this user’s vCard doesn’t specify the MIME type of its avatar.'); | 137 return svgError(res, 'Error: this user’s vCard doesn’t specify the MIME type of its avatar.'); |
141 return; | |
142 } | |
143 } | 138 } |
144 | 139 |
145 var ext; | 140 var ext; |
146 for (var i in extensions) | 141 for (var i in extensions) |
147 if (type == extensions[i]) | 142 if (type == extensions[i]) |
148 ext = i; | 143 ext = i; |
149 | 144 |
150 // Here we don’t try to guess the extension even if the option is set. | 145 // Here we don’t try to guess the extension even if the option is set. |
151 if (ext === undefined) { | 146 if (ext === undefined) { |
152 console.log('Unknown MIME type: '+type); | 147 console.log('Unknown MIME type: '+type); |
153 svgError(res, 'Error: this user’s avatar is in an unknown format.'); | 148 return svgError(res, 'Error: this user’s avatar is in an unknown format.'); |
154 return; | |
155 } | 149 } |
156 | 150 |
157 var binval = new Buffer(base64.replace(/\n/g, ''), 'base64'); | 151 var binval = new Buffer(base64.replace(/\n/g, ''), 'base64'); |
158 | 152 |
159 fs.writeFile(config.directory+'/'+to+'.'+ext, binval, function() { | 153 fs.writeFile(config.directory+'/'+to+'.'+ext, binval, function() { |
179 | 173 |
180 conn.send(makeError(response)); | 174 conn.send(makeError(response)); |
181 } | 175 } |
182 | 176 |
183 var getVCard = function(jid, res) { | 177 var getVCard = function(jid, res) { |
184 var id = conn.getUniqueId(); | 178 var id = getUniqueId(); |
185 | 179 |
186 var toSend = new Element('iq', {to: jid, from: config.jid, type: 'get', id: id}) | 180 var toSend = new Element('iq', {to: jid, from: config.jid, type: 'get', id: id}) |
187 .c('vCard', {xmlns: 'vcard-temp'}) | 181 .c('vCard', {xmlns: 'vcard-temp'}) |
188 .up(); | 182 .up(); |
189 | 183 |
244 var ee = easterEggs[i]; | 238 var ee = easterEggs[i]; |
245 var file = ee.file || i; | 239 var file = ee.file || i; |
246 var re = ee.re || new RegExp(config.webRoot + file + '$'); | 240 var re = ee.re || new RegExp(config.webRoot + file + '$'); |
247 if (re.test(req.url)) { | 241 if (re.test(req.url)) { |
248 fs.readFile(file, function(err, content) { | 242 fs.readFile(file, function(err, content) { |
249 if (err) { | 243 if (err) |
250 svgError(res, 'Error: ' + (ee.error || file + ' unavailable.')); | 244 return svgError(res, 'Error: ' + (ee.error || file + ' unavailable.')); |
251 return; | |
252 } | |
253 res.writeHead(200, {'Content-Type': ee.mime || 'text/plain'}); | 245 res.writeHead(200, {'Content-Type': ee.mime || 'text/plain'}); |
254 res.end(content); | 246 res.end(content); |
255 }); | 247 }); |
256 return; | 248 return; |
257 } | 249 } |