Mercurial > eldonilo > avatar
annotate 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 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env node |
2 | |
3 /* | |
4 * Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
5 * | |
6 * This file is the source code of an XMPP avatar retriever. | |
7 * | |
8 * This program is free software: you can redistribute it and/or modify | |
9 * it under the terms of the GNU Affero General Public License as | |
10 * published by the Free Software Foundation, version 3 of the License. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 'use strict'; | |
22 | |
23 var config = require('./configuration'); | |
24 | |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
25 var util = require('util'); |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
26 var fs = require('fs'); |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
27 var http = require('http'); |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
28 |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
29 var xmpp = require('node-xmpp'); |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
30 var conn = new xmpp.Client(config); |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
31 var Element = require('ltx').Element; |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
32 |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
33 Element.prototype.getAttribute = function(name) { |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
34 return this.attrs[name]; |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
35 }; |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
36 |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
37 process.addListener('uncaughtException', function (err) { |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
38 console.log('[41;1mUncaught exception (' + err + '), this should never happen:[0m\n' + err.stack); |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
39 }); |
0 | 40 |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
41 (function() { |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
42 var send = conn.send; |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
43 conn.send = function(s) { |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
44 util.log('Sent: [1;32m' + s + '[0m'); |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
45 send.call(conn, s); |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
46 }; |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
47 })(); |
0 | 48 |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
49 conn.on('online', function () { |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
50 console.log('Connected'); |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
51 }); |
0 | 52 |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
53 conn.on('stanza', function (stanza) { |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
54 util.log('Recv: [1;34m' + stanza + '[0m'); |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
55 if (stanza.is('iq')) |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
56 onIq(stanza); |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
57 else |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
58 onError(stanza); |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
59 }); |
0 | 60 |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
61 var getUniqueId = (function() { |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
62 var id = 0; |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
63 return function() { |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
64 return ++id; |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
65 }; |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
66 })(); |
0 | 67 |
68 var extensions = { | |
69 png: 'image/png', | |
70 svg: 'image/svg+xml', | |
71 jpg: 'image/jpeg', | |
72 gif: 'image/gif' | |
73 } | |
74 | |
75 var jids = {}; | |
76 | |
77 var sent = {}; | |
78 | |
2
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
79 var svgError = function(res, message) { |
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
80 res.writeHead(500, {'Content-Type': 'image/svg+xml'}); |
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
81 res.write('<?xml version="1.0" encoding="UTF-8"?>\n'); |
6
3b799c33ab16
More avatar-like error.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
82 res.write('<svg xmlns="http://www.w3.org/2000/svg" viewBox="-32 -36 64 64">\n'); |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
83 res.write('\t<title>'+message+'</title>\n'); |
6
3b799c33ab16
More avatar-like error.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
84 res.write('\t<rect x="-32" y="-36" width="64" height="64" fill="white"/>\n'); |
3b799c33ab16
More avatar-like error.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
85 res.write('\t<text font-family="sans-serif" font-weight="bold" text-anchor="middle">Error</text>\n'); |
2
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
86 res.end('</svg>\n'); |
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
87 } |
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
88 |
0 | 89 var makeError = function(response) { |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
90 response.attrs.type = 'error'; |
0 | 91 |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
92 response.c('error', {type: 'cancel'}) |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
93 .c('feature-not-implemented', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}); |
0 | 94 |
95 return response; | |
96 } | |
97 | |
98 function onIq(stanza) { | |
99 var type = stanza.getAttribute('type'); | |
100 var from = stanza.getAttribute('to'); | |
101 var to = stanza.getAttribute('from'); | |
102 var id = stanza.getAttribute('id'); | |
103 | |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
104 var response = new Element('iq', {to: to, from: from, type: 'result'}); |
0 | 105 if (id) |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
106 response.attrs.id = id; |
0 | 107 |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
108 if (!sent[id]) |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
109 return conn.send(makeError(response)); |
0 | 110 |
111 var res = sent[id]; | |
112 delete sent[id]; | |
113 | |
114 if (type == 'error') { | |
115 try { | |
116 var err = stanza.getChild('error').getChild().name; | |
117 } catch (e) { | |
118 var err = 'none'; | |
119 } | |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
120 return svgError(res, 'Error during query of this user’s vCard: “'+err+'”.'); |
0 | 121 } |
122 | |
123 var vCard = stanza.getChild('vCard', 'vcard-temp'); | |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
124 if (!vCard) |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
125 return svgError(res, 'Error: this user doesn’t have a vCard.'); |
0 | 126 |
127 try { | |
128 var photo = vCard.getChild('PHOTO', 'vcard-temp'); | |
129 var base64 = photo.getChild('BINVAL', 'vcard-temp').getText(); | |
130 | |
1
0c19fadfc12d
Serve the right error message when the TYPE of the PHOTO is missing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
131 try { |
0c19fadfc12d
Serve the right error message when the TYPE of the PHOTO is missing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
132 var type = photo.getChild('TYPE', 'vcard-temp').getText(); |
0c19fadfc12d
Serve the right error message when the TYPE of the PHOTO is missing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
133 } catch (e) { |
5
9b2f17ea1594
Add an option to guess the type of an avatar even if it is unspecified.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
134 if (config.guessType) |
9b2f17ea1594
Add an option to guess the type of an avatar even if it is unspecified.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
135 type = 'image/png'; // FIXME: use magic. |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
136 else |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
137 return svgError(res, 'Error: this user’s vCard doesn’t specify the MIME type of its avatar.'); |
1
0c19fadfc12d
Serve the right error message when the TYPE of the PHOTO is missing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
138 } |
0c19fadfc12d
Serve the right error message when the TYPE of the PHOTO is missing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
139 |
0 | 140 var ext; |
141 for (var i in extensions) | |
142 if (type == extensions[i]) | |
143 ext = i; | |
144 | |
5
9b2f17ea1594
Add an option to guess the type of an avatar even if it is unspecified.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
145 // Here we don’t try to guess the extension even if the option is set. |
0 | 146 if (ext === undefined) { |
5
9b2f17ea1594
Add an option to guess the type of an avatar even if it is unspecified.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
147 console.log('Unknown MIME type: '+type); |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
148 return svgError(res, 'Error: this user’s avatar is in an unknown format.'); |
0 | 149 } |
150 | |
151 var binval = new Buffer(base64.replace(/\n/g, ''), 'base64'); | |
152 | |
153 fs.writeFile(config.directory+'/'+to+'.'+ext, binval, function() { | |
154 jids[to] = ext; | |
155 showImage(to, res); | |
156 }); | |
157 } catch (e) { | |
2
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
158 svgError(res, 'Error: this user doesn’t have an avatar in his/her vCard.'); |
0 | 159 } |
160 } | |
161 | |
162 function onError(stanza) { | |
163 if (stanza.getAttribute('type') == 'error') | |
164 return; | |
165 | |
166 var from = stanza.getAttribute('to'); | |
167 var to = stanza.getAttribute('from'); | |
168 var id = stanza.getAttribute('id'); | |
169 | |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
170 var response = new Element(stanza.name, {to: to, from: from}); |
0 | 171 if (id) |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
172 response.attrs.id = id; |
0 | 173 |
174 conn.send(makeError(response)); | |
175 } | |
176 | |
3
805f24754ff1
Don’t delete avatar on error, only allow update.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
177 var getVCard = function(jid, res) { |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
178 var id = getUniqueId(); |
0 | 179 |
180 var toSend = new Element('iq', {to: jid, from: config.jid, type: 'get', id: id}) | |
12
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
181 .c('vCard', {xmlns: 'vcard-temp'}) |
a0ed785d1b8d
Remove xmpp.js compatibility and fix the node-xmpp one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
182 .up(); |
0 | 183 |
184 conn.send(toSend); | |
185 | |
3
805f24754ff1
Don’t delete avatar on error, only allow update.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
186 sent[id] = res; |
0 | 187 } |
188 | |
189 var showImage = function(jid, res) { | |
190 var extension = jids[jid]; | |
191 var file = config.directory+'/'+jid+'.'+extension; | |
192 res.writeHead(200, {'Content-Type': extensions[extension]}); | |
193 fs.readFile(file, function(err, data) { | |
194 res.end(data); | |
195 }); | |
196 fs.stat(file, function(err, stats) { | |
197 if (err) { | |
198 console.log('Error when stat on “'+file+'”.'); | |
199 return; | |
200 } | |
201 | |
202 var last = new Date(stats.mtime); | |
203 var now = new Date(); | |
204 | |
3
805f24754ff1
Don’t delete avatar on error, only allow update.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
205 if (now - last > 24*60*60*1000) |
805f24754ff1
Don’t delete avatar on error, only allow update.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
206 getVCard(jid, res); |
0 | 207 }); |
208 return; | |
209 } | |
210 | |
211 fs.readdir('data', function(err, files) { | |
212 if (err) | |
213 process.exit('1'); | |
214 | |
2
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
215 for (var i in files) { |
0 | 216 var tab = /(.*)\.([a-z]{3})/.exec(files[i]); |
217 jids[tab[1]] = tab[2]; | |
218 } | |
219 }); | |
220 | |
221 http.createServer(function (req, res) { | |
7
51cda3a6e1c3
Better handling of proxy connections; add an option to specify the root on the webserver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
222 console.log('Connection from ' + (req.headers['x-forwarded-for'] || req.client.remoteAddress) + ' (' + req.headers['user-agent'] + ') to get “' + req.url + '”.'); |
0 | 223 |
224 var easterEggs = { | |
225 source: { | |
7
51cda3a6e1c3
Better handling of proxy connections; add an option to specify the root on the webserver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
226 re: new RegExp(config.webRoot + 'source/code$'), |
0 | 227 file: process.argv[1], |
228 mime: 'application/ecmascript', | |
229 error: 'source code unavailable! oO' | |
230 }, | |
231 README: {}, | |
232 COPYING: {}, | |
233 }; | |
234 | |
235 req.setEncoding('utf-8'); | |
236 | |
237 for (var i in easterEggs) { | |
238 var ee = easterEggs[i]; | |
239 var file = ee.file || i; | |
7
51cda3a6e1c3
Better handling of proxy connections; add an option to specify the root on the webserver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
240 var re = ee.re || new RegExp(config.webRoot + file + '$'); |
0 | 241 if (re.test(req.url)) { |
242 fs.readFile(file, function(err, content) { | |
13
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
243 if (err) |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
244 return svgError(res, 'Error: ' + (ee.error || file + ' unavailable.')); |
0 | 245 res.writeHead(200, {'Content-Type': ee.mime || 'text/plain'}); |
246 res.end(content); | |
247 }); | |
248 return; | |
249 } | |
250 } | |
251 | |
7
51cda3a6e1c3
Better handling of proxy connections; add an option to specify the root on the webserver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
252 var jid = unescape(req.url.replace(new RegExp(config.webRoot), '')); |
0 | 253 |
254 if (jid === '') { | |
255 res.writeHead(200, {'Content-Type': 'application/xhtml+xml'}); | |
256 res.write('<?xml version="1.0" encoding="utf-8"?>\n'); | |
257 res.write('<!DOCTYPE html>\n'); | |
258 res.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n'); | |
259 res.write('\t<head>\n'); | |
260 res.write('\t\t<title>JavaScript XMPP Avatar Retriever</title>\n'); | |
261 res.write('\t</head>\n'); | |
262 res.write('\t<body>\n'); | |
263 res.write('\t\t<header><h1>JavaScript XMPP Avatar Retriever</h1></header>\n'); | |
264 res.write('\t\t<p>Put any JID and get its avatar. :)</p>\n'); | |
265 res.write('\t\t<form action="redirect" method="post">\n'); | |
266 res.write('\t\t\t<p>\n'); | |
267 res.write('\t\t\t\t<input name="jid" type="text" placeholder="you@yourserver.tld"/>\n'); | |
268 res.write('\t\t\t\t<input type="submit"/>\n'); | |
269 res.write('\t\t\t</p>\n'); | |
270 res.write('\t\t</form>\n'); | |
2
ad496e8a5e66
Use an image for errors instead of text (unusable in <img/>).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
271 res.write('\t\t<footer><p>(<a href="README">README</a>, <a href="source/code">source code</a>, <a href="http://hg.linkmauve.fr/avatar">Mercurial repository</a>)</p></footer>\n'); |
0 | 272 res.write('\t</body>\n'); |
273 res.end('</html>\n'); | |
274 return; | |
275 } | |
276 | |
277 if (jid === 'redirect') { | |
278 if (req.method !== 'POST') { | |
279 res.writeHead(404, {'Content-Type': 'text/plain'}); | |
280 res.end('Error: redirect unavailable.'); | |
281 return; | |
282 } | |
283 | |
284 req.on('data', function(content) { | |
285 console.log(content); | |
286 var jid = unescape(content.toString()).replace(/^jid=/, ''); | |
287 res.writeHead(301, {'Location': jid}); | |
288 res.end(); | |
289 }); | |
290 return; | |
291 } | |
292 | |
293 if (jid in jids) { | |
294 showImage(jid, res); | |
295 return; | |
296 } | |
297 | |
3
805f24754ff1
Don’t delete avatar on error, only allow update.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
298 getVCard(jid, res); |
9
9360b78c7a5b
Add WebHost configuration, and default it to both IPv6 and IPv4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
299 }).listen(config.webPort, config.webHost); |