Mercurial > eldonilo > avatar
annotate avatar.js @ 17:32f7526da5fe
Remove the hardcoded index.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 10 Jul 2011 15:39:48 +0200 |
parents | 27eaec05cfd0 |
children | 92d6e1b40df5 |
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; | |
16
27eaec05cfd0
Expire the avatar after a configurable amount of time.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
15
diff
changeset
|
192 var now = new Date; |
27eaec05cfd0
Expire the avatar after a configurable amount of time.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
15
diff
changeset
|
193 res.writeHead(200, {'Content-Type': extensions[extension], Expires: new Date(config.expire * 1000 + now.getTime())}); |
0 | 194 fs.readFile(file, function(err, data) { |
195 res.end(data); | |
196 }); | |
197 fs.stat(file, function(err, stats) { | |
198 if (err) { | |
199 console.log('Error when stat on “'+file+'”.'); | |
200 return; | |
201 } | |
202 | |
203 var last = new Date(stats.mtime); | |
204 | |
16
27eaec05cfd0
Expire the avatar after a configurable amount of time.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
15
diff
changeset
|
205 if (now - last > config.expire * 1000) |
3
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 | |
14
7fdae201c1e2
Don’t exit if the data directory is missing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
13
diff
changeset
|
211 fs.readdir(config.directory, function(err, files) { |
0 | 212 if (err) |
14
7fdae201c1e2
Don’t exit if the data directory is missing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
13
diff
changeset
|
213 return fs.mkdir(config.directory, 488); // Actually 0750 |
0 | 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: {}, | |
17
32f7526da5fe
Remove the hardcoded index.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
233 'index.xhtml': { |
32f7526da5fe
Remove the hardcoded index.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
234 re: /\/(index.xhtml)?$/, |
32f7526da5fe
Remove the hardcoded index.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
235 mime: 'application/xhtml+xml', |
32f7526da5fe
Remove the hardcoded index.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
236 error: '<h1 xmlns="http://www.w3.org/1999/xhtml">Index file not available.</h1>' |
32f7526da5fe
Remove the hardcoded index.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
237 } |
0 | 238 }; |
239 | |
240 req.setEncoding('utf-8'); | |
241 | |
242 for (var i in easterEggs) { | |
243 var ee = easterEggs[i]; | |
244 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
|
245 var re = ee.re || new RegExp(config.webRoot + file + '$'); |
0 | 246 if (re.test(req.url)) { |
247 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
|
248 if (err) |
38ddd5888b8d
It’s now a client instead of a component. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
12
diff
changeset
|
249 return svgError(res, 'Error: ' + (ee.error || file + ' unavailable.')); |
0 | 250 res.writeHead(200, {'Content-Type': ee.mime || 'text/plain'}); |
251 res.end(content); | |
252 }); | |
253 return; | |
254 } | |
255 } | |
256 | |
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
|
257 var jid = unescape(req.url.replace(new RegExp(config.webRoot), '')); |
0 | 258 |
259 if (jid === 'redirect') { | |
260 if (req.method !== 'POST') { | |
261 res.writeHead(404, {'Content-Type': 'text/plain'}); | |
262 res.end('Error: redirect unavailable.'); | |
263 return; | |
264 } | |
265 | |
266 req.on('data', function(content) { | |
267 var jid = unescape(content.toString()).replace(/^jid=/, ''); | |
268 res.writeHead(301, {'Location': jid}); | |
269 res.end(); | |
270 }); | |
271 return; | |
272 } | |
273 | |
274 if (jid in jids) { | |
275 showImage(jid, res); | |
276 return; | |
277 } | |
278 | |
3
805f24754ff1
Don’t delete avatar on error, only allow update.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
279 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
|
280 }).listen(config.webPort, config.webHost); |