Commit | Line | Data |
---|---|---|
bd299b17 | 1 | var url = require('url'), |
0fa2ca42 | 2 | fs = require('fs'), |
cbcd1a23 | 3 | crypto = require('crypto'), |
0fa2ca42 | 4 | node_static = require('node-static'), |
cbcd1a23 JA |
5 | _ = require('lodash'), |
6 | config = require('./configuration.js'); | |
a8bf3ea4 | 7 | |
a8bf3ea4 | 8 | |
3eb6b5fd D |
9 | // Cache for settings.json |
10 | var cached_settings = { | |
11 | debug: { | |
12 | hash: '', | |
13 | settings: '' | |
14 | }, | |
15 | production: { | |
16 | hash: '', | |
17 | settings: '' | |
18 | } | |
19 | }; | |
20 | ||
21 | // Clear the settings cache when the settings change | |
22 | config.on('loaded', function () { | |
23 | cached_settings.debug.settings = cached_settings.production.settings = ''; | |
24 | cached_settings.debug.hash = cached_settings.production.hash = ''; | |
25 | }); | |
26 | ||
27 | ||
28 | ||
a8bf3ea4 | 29 | |
186531ed D |
30 | var HttpHandler = function (config) { |
31 | var public_html = config.public_html || 'client/'; | |
32 | this.file_server = new node_static.Server(public_html); | |
a8bf3ea4 JA |
33 | }; |
34 | ||
186531ed D |
35 | module.exports.HttpHandler = HttpHandler; |
36 | ||
37 | ||
38 | ||
39 | HttpHandler.prototype.serve = function (request, response) { | |
b65ad8f1 | 40 | // The incoming requests base path (ie. /kiwiclient) |
b737610b | 41 | var base_path = global.config.http_base_path || '/kiwi', |
b65ad8f1 D |
42 | base_path_regex; |
43 | ||
44 | // Trim of any trailing slashes | |
45 | if (base_path.substr(base_path.length - 1) === '/') { | |
46 | base_path = base_path.substr(0, base_path.length - 1); | |
47 | } | |
0fa2ca42 | 48 | |
b65ad8f1 D |
49 | // Build the regex to match the base_path |
50 | base_path_regex = base_path.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
0ae87edb D |
51 | |
52 | // Any asset request to head into the asset dir | |
b65ad8f1 | 53 | request.url = request.url.replace(base_path + '/assets/', '/assets/'); |
0ae87edb | 54 | |
b075a0d6 | 55 | // Any requests for /client to load the index file |
b65ad8f1 | 56 | if (request.url.match(new RegExp('^' + base_path_regex + '([/$]|$)', 'i'))) { |
b075a0d6 D |
57 | request.url = '/'; |
58 | } | |
59 | ||
0fa2ca42 | 60 | // If the 'magic' translation is requested, figure out the best language to use from |
b722f3c6 | 61 | // the Accept-Language HTTP header. If nothing is suitible, fallback to our en-gb default translation |
28cde487 JA |
62 | if (request.url.substr(0, 16) === '/assets/locales/') { |
63 | if (request.url === '/assets/locales/magic.json') { | |
64 | return serveMagicLocale.call(this, request, response); | |
65 | } else { | |
66 | response.setHeader('Content-Language', request.url.substr(16, request.url.indexOf('.') - 16)); | |
67 | } | |
cbcd1a23 JA |
68 | } else if (request.url.substr(0, 21) === '/assets/settings.json') { |
69 | return serveSettings.call(this, request, response); | |
0fa2ca42 | 70 | } |
0ae87edb | 71 | |
186531ed | 72 | this.file_server.serve(request, response, function (err) { |
a8bf3ea4 | 73 | if (err) { |
bd299b17 JA |
74 | response.writeHead(err.status, err.headers); |
75 | response.end(); | |
a8bf3ea4 JA |
76 | } |
77 | }); | |
0fa2ca42 JA |
78 | }; |
79 | ||
3eb6b5fd D |
80 | |
81 | /** | |
82 | * Handle the /assets/locales/magic.json request | |
83 | * Find the closest translation we have for the language | |
84 | * set in the browser. | |
85 | **/ | |
0fa2ca42 | 86 | var serveMagicLocale = function (request, response) { |
3eb6b5fd D |
87 | var that = this, |
88 | default_locale_id = 'en-gb'; | |
8e1ab29d | 89 | |
0fa2ca42 | 90 | if (request.headers['accept-language']) { |
28cde487 JA |
91 | fs.readdir('client/assets/locales', function (err, files) { |
92 | var available = [], | |
93 | i = 0, | |
3eb6b5fd D |
94 | langs = request.headers['accept-language'].split(','), // Example: en-gb,en;q=0.5 |
95 | found_locale = default_locale_id; | |
28cde487 | 96 | |
8e1ab29d | 97 | // Get a list of the available translations we have |
0fa2ca42 | 98 | files.forEach(function (file) { |
28cde487 JA |
99 | if (file.substr(-5) === '.json') { |
100 | available.push(file.slice(0, -5)); | |
0fa2ca42 JA |
101 | } |
102 | }); | |
0fa2ca42 | 103 | |
8e1ab29d | 104 | // Sanitise the browsers accepted languages and the qualities |
28cde487 JA |
105 | for (i = 0; i < langs.length; i++) { |
106 | langs[i]= langs[i].split(';q='); | |
8e1ab29d | 107 | langs[i][0] = langs[i][0].toLowerCase(); |
28cde487 | 108 | langs[i][1] = (typeof langs[i][1] === 'string') ? parseFloat(langs[i][1]) : 1.0; |
0fa2ca42 | 109 | } |
8e1ab29d D |
110 | |
111 | // Sort the accepted languages by quality | |
28cde487 JA |
112 | langs.sort(function (a, b) { |
113 | return b[1] - a[1]; | |
114 | }); | |
8e1ab29d D |
115 | |
116 | // Serve the first language we have a translation for | |
28cde487 JA |
117 | for (i = 0; i < langs.length; i++) { |
118 | if (langs[i][0] === '*') { | |
119 | break; | |
120 | } else if (_.contains(available, langs[i][0])) { | |
3eb6b5fd D |
121 | found_locale = langs[i][0]; |
122 | break; | |
28cde487 JA |
123 | } |
124 | } | |
8e1ab29d | 125 | |
3eb6b5fd | 126 | return serveLocale.call(that, request, response, found_locale); |
28cde487 | 127 | }); |
d2831ddd | 128 | } else { |
3eb6b5fd D |
129 | |
130 | // No accept-language specified in the request so send the default | |
131 | return serveLocale.call(this, request, response, default_locale_id); | |
0fa2ca42 | 132 | } |
3eb6b5fd | 133 | |
d2831ddd | 134 | }; |
cbc8feae | 135 | |
3eb6b5fd D |
136 | |
137 | /** | |
138 | * Send a locale to the browser | |
139 | */ | |
140 | var serveLocale = function (request, response, locale_id) { | |
141 | this.file_server.serveFile('/assets/locales/' + locale_id + '.json', 200, { | |
142 | Vary: 'Accept-Language', | |
143 | 'Content-Language': locale_id | |
144 | }, request, response); | |
0fa2ca42 | 145 | }; |
cbcd1a23 | 146 | |
3eb6b5fd D |
147 | |
148 | /** | |
149 | * Handle the settings.json request | |
150 | */ | |
151 | function serveSettings(request, response) { | |
152 | var referrer_url, | |
153 | debug = false, | |
154 | settings; | |
155 | ||
156 | // Check the referrer for a debug option | |
157 | if (request.headers['referer']) { | |
158 | referrer_url = url.parse(request.headers['referer'], true); | |
159 | if (referrer_url.query && referrer_url.query.debug) { | |
160 | debug = true; | |
161 | } | |
498afd32 | 162 | } |
cbe80532 | 163 | |
3eb6b5fd D |
164 | settings = cached_settings[debug ? 'debug' : 'production']; |
165 | ||
166 | // Generate the settings if we don't have them cached as yet | |
167 | if (settings.settings === '') { | |
168 | generateSettings(request, debug, function (err, settings) { | |
169 | if (err) { | |
170 | response.statusCode = 500; | |
171 | response.end(); | |
172 | } else { | |
173 | sendSettings.call(this, request, response, settings); | |
174 | } | |
175 | }); | |
cbe80532 | 176 | |
3eb6b5fd D |
177 | } else { |
178 | sendSettings.call(this, request, response, settings); | |
179 | } | |
180 | } | |
181 | ||
182 | ||
183 | /** | |
184 | * Send the the settings to the browser | |
185 | */ | |
186 | function sendSettings(request, response, settings) { | |
187 | if (request.headers['if-none-match'] && request.headers['if-none-match'] === settings.hash) { | |
188 | response.writeHead(304, 'Not Modified'); | |
189 | return response.end(); | |
190 | } | |
191 | ||
192 | response.writeHead(200, { | |
193 | 'ETag': settings.hash, | |
194 | 'Content-Type': 'application/json' | |
195 | }); | |
196 | response.end(settings.settings); | |
197 | } | |
198 | ||
199 | ||
200 | /** | |
201 | * Generate a settings object for the client. | |
202 | * Settings include available translations, default client config, etc | |
203 | */ | |
498afd32 | 204 | function generateSettings(request, debug, callback) { |
cbcd1a23 JA |
205 | var vars = { |
206 | server_settings: {}, | |
207 | client_plugins: [], | |
208 | translations: [], | |
209 | scripts: [ | |
210 | [ | |
211 | 'libs/lodash.min.js' | |
212 | ], | |
33432128 | 213 | ['libs/backbone.min.js', 'libs/jed.js'] |
cbcd1a23 | 214 | ] |
498afd32 | 215 | }; |
cbcd1a23 | 216 | |
498afd32 | 217 | if (debug) { |
cbcd1a23 | 218 | vars.scripts = vars.scripts.concat([ |
33432128 D |
219 | [ |
220 | 'src/app.js', | |
221 | 'libs/engine.io.js', | |
222 | 'libs/engine.io.tools.js' | |
223 | ], | |
cbcd1a23 JA |
224 | [ |
225 | 'src/models/application.js', | |
226 | 'src/models/gateway.js' | |
227 | ], | |
228 | [ | |
229 | 'src/models/newconnection.js', | |
230 | 'src/models/panellist.js', | |
231 | 'src/models/networkpanellist.js', | |
232 | 'src/models/panel.js', | |
233 | 'src/models/member.js', | |
234 | 'src/models/memberlist.js', | |
235 | 'src/models/network.js' | |
236 | ], | |
d99e7823 | 237 | |
cbcd1a23 JA |
238 | [ |
239 | 'src/models/query.js', | |
240 | 'src/models/channel.js', | |
cbcd1a23 JA |
241 | 'src/models/applet.js' |
242 | ], | |
243 | ||
244 | [ | |
f8508c7a | 245 | 'src/models/server.js', // Depends on models/channel.js |
cbcd1a23 JA |
246 | 'src/applets/settings.js', |
247 | 'src/applets/chanlist.js', | |
248 | 'src/applets/scripteditor.js' | |
249 | ], | |
250 | ||
251 | [ | |
252 | 'src/models/pluginmanager.js', | |
253 | 'src/models/datastore.js', | |
254 | 'src/helpers/utils.js' | |
255 | ], | |
256 | ||
257 | // Some views extend these, so make sure they're loaded beforehand | |
258 | [ | |
259 | 'src/views/panel.js' | |
260 | ], | |
261 | ||
262 | [ | |
263 | 'src/views/channel.js', | |
264 | 'src/views/applet.js', | |
265 | 'src/views/application.js', | |
266 | 'src/views/apptoolbar.js', | |
267 | 'src/views/controlbox.js', | |
268 | 'src/views/favicon.js', | |
269 | 'src/views/mediamessage.js', | |
270 | 'src/views/member.js', | |
271 | 'src/views/memberlist.js', | |
272 | 'src/views/menubox.js', | |
273 | 'src/views/networktabs.js', | |
274 | 'src/views/nickchangebox.js', | |
275 | 'src/views/resizehandler.js', | |
276 | 'src/views/serverselect.js', | |
277 | 'src/views/statusmessage.js', | |
278 | 'src/views/tabs.js', | |
279 | 'src/views/topicbar.js', | |
280 | 'src/views/userbox.js' | |
281 | ] | |
282 | ]); | |
283 | } else { | |
33432128 | 284 | vars.scripts.push(['kiwi.min.js', 'libs/engine.io.bundle.min.js']); |
cbcd1a23 JA |
285 | } |
286 | ||
287 | // Any restricted server mode set? | |
288 | if (config.get().restrict_server) { | |
289 | vars.server_settings = { | |
290 | connection: { | |
291 | server: config.get().restrict_server, | |
292 | port: config.get().restrict_server_port || 6667, | |
293 | ssl: config.get().restrict_server_ssl, | |
294 | channel: config.get().restrict_server_channel, | |
295 | nick: config.get().restrict_server_nick, | |
296 | allow_change: false | |
297 | } | |
298 | }; | |
299 | } | |
300 | ||
301 | // Any client default settings? | |
302 | if (config.get().client) { | |
303 | vars.server_settings.client = config.get().client; | |
304 | } | |
305 | ||
306 | // Any client plugins? | |
307 | if (config.get().client_plugins && config.get().client_plugins.length > 0) { | |
308 | vars.client_plugins = config.get().client_plugins; | |
309 | } | |
310 | ||
3eb6b5fd | 311 | // Get a list of available translations |
cbcd1a23 JA |
312 | fs.readFile(__dirname + '/../client/assets/src/translations/translations.json', function (err, translations) { |
313 | if (err) { | |
cbe80532 | 314 | return callback(err); |
cbcd1a23 JA |
315 | } |
316 | ||
317 | var translation_files; | |
318 | translations = JSON.parse(translations); | |
319 | fs.readdir(__dirname + '/../client/assets/src/translations/', function (err, pofiles) { | |
498afd32 | 320 | var hash, settings; |
cbcd1a23 | 321 | if (err) { |
cbe80532 | 322 | return callback(err); |
cbcd1a23 JA |
323 | } |
324 | ||
325 | pofiles.forEach(function (file) { | |
326 | var locale = file.slice(0, -3); | |
327 | if ((file.slice(-3) === '.po') && (locale !== 'template')) { | |
328 | vars.translations.push({tag: locale, language: translations[locale]}); | |
329 | } | |
330 | }); | |
331 | ||
498afd32 JA |
332 | settings = cached_settings[debug?'debug':'production']; |
333 | settings.settings = JSON.stringify(vars); | |
342de126 | 334 | settings.hash = crypto.createHash('md5').update(settings.settings).digest('hex'); |
cbcd1a23 | 335 | |
498afd32 | 336 | return callback(null, settings); |
cbe80532 JA |
337 | }); |
338 | }); | |
339 | } | |
cbcd1a23 | 340 |