1 var url
= require('url'),
3 crypto
= require('crypto'),
4 node_static
= require('node-static'),
6 config
= require('./configuration.js');
9 // Cache for settings.json
10 var cached_settings
= {
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
= '';
30 var HttpHandler = function (config
) {
31 var public_html
= config
.public_html
|| 'client/';
32 this.file_server
= new node_static
.Server(public_html
);
35 module
.exports
.HttpHandler
= HttpHandler
;
39 HttpHandler
.prototype.serve = function (request
, response
) {
40 // The incoming requests base path (ie. /kiwiclient)
41 var base_path
= global
.config
.http_base_path
|| '/kiwi',
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);
49 // Build the regex to match the base_path
50 base_path_regex
= base_path
.replace(/[-/\\^$*+?.()|[\]{}]/g
, '\\$&');
52 // Any asset request to head into the asset dir
53 request
.url
= request
.url
.replace(base_path
+ '/assets/', '/assets/');
55 // Any requests for /client to load the index file
56 if (request
.url
.match(new RegExp('^' + base_path_regex
+ '([/$]|$)', 'i'))) {
60 // If the 'magic' translation is requested, figure out the best language to use from
61 // the Accept-Language HTTP header. If nothing is suitible, fallback to our en-gb default translation
62 if (request
.url
.substr(0, 16) === '/assets/locales/') {
63 if (request
.url
=== '/assets/locales/magic.json') {
64 return serveMagicLocale
.call(this, request
, response
);
66 response
.setHeader('Content-Language', request
.url
.substr(16, request
.url
.indexOf('.') - 16));
68 } else if (request
.url
.substr(0, 21) === '/assets/settings.json') {
69 return serveSettings
.call(this, request
, response
);
72 this.file_server
.serve(request
, response
, function (err
) {
74 response
.writeHead(err
.status
, err
.headers
);
82 * Handle the /assets/locales/magic.json request
83 * Find the closest translation we have for the language
86 var serveMagicLocale = function (request
, response
) {
88 default_locale_id
= 'en-gb';
90 if (request
.headers
['accept-language']) {
91 fs
.readdir('client/assets/locales', function (err
, files
) {
94 langs
= request
.headers
['accept-language'].split(','), // Example: en-gb,en;q=0.5
95 found_locale
= default_locale_id
;
97 // Get a list of the available translations we have
98 files
.forEach(function (file
) {
99 if (file
.substr(-5) === '.json') {
100 available
.push(file
.slice(0, -5));
104 // Sanitise the browsers accepted languages and the qualities
105 for (i
= 0; i
< langs
.length
; i
++) {
106 langs
[i
]= langs
[i
].split(';q=');
107 langs
[i
][0] = langs
[i
][0].toLowerCase();
108 langs
[i
][1] = (typeof langs
[i
][1] === 'string') ? parseFloat(langs
[i
][1]) : 1.0;
111 // Sort the accepted languages by quality
112 langs
.sort(function (a
, b
) {
116 // Serve the first language we have a translation for
117 for (i
= 0; i
< langs
.length
; i
++) {
118 if (langs
[i
][0] === '*') {
120 } else if (_
.contains(available
, langs
[i
][0])) {
121 found_locale
= langs
[i
][0];
126 return serveLocale
.call(that
, request
, response
, found_locale
);
130 // No accept-language specified in the request so send the default
131 return serveLocale
.call(this, request
, response
, default_locale_id
);
138 * Send a locale to the browser
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
);
149 * Handle the settings.json request
151 function serveSettings(request
, response
) {
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
) {
164 settings
= cached_settings
[debug
? 'debug' : 'production'];
166 // Generate the settings if we don't have them cached as yet
167 if (settings
.settings
=== '') {
168 generateSettings(request
, debug
, function (err
, settings
) {
170 response
.statusCode
= 500;
173 sendSettings
.call(this, request
, response
, settings
);
178 sendSettings
.call(this, request
, response
, settings
);
184 * Send the the settings to the browser
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();
192 response
.writeHead(200, {
193 'ETag': settings
.hash
,
194 'Content-Type': 'application/json'
196 response
.end(settings
.settings
);
201 * Generate a settings object for the client.
202 * Settings include available translations, default client config, etc
204 function generateSettings(request
, debug
, callback
) {
213 'libs/backbone.min.js',
219 vars
.scripts
= vars
.scripts
.concat([
222 'src/models/application.js',
223 'src/models/gateway.js'
226 'src/models/newconnection.js',
227 'src/models/panellist.js',
228 'src/models/networkpanellist.js',
229 'src/models/panel.js',
230 'src/models/member.js',
231 'src/models/memberlist.js',
232 'src/models/network.js'
236 'src/models/query.js',
237 'src/models/channel.js',
238 'src/models/server.js',
239 'src/models/applet.js'
243 'src/applets/settings.js',
244 'src/applets/chanlist.js',
245 'src/applets/scripteditor.js'
249 'src/models/pluginmanager.js',
250 'src/models/datastore.js',
251 'src/helpers/utils.js'
254 // Some views extend these, so make sure they're loaded beforehand
260 'src/views/channel.js',
261 'src/views/applet.js',
262 'src/views/application.js',
263 'src/views/apptoolbar.js',
264 'src/views/controlbox.js',
265 'src/views/favicon.js',
266 'src/views/mediamessage.js',
267 'src/views/member.js',
268 'src/views/memberlist.js',
269 'src/views/menubox.js',
270 'src/views/networktabs.js',
271 'src/views/nickchangebox.js',
272 'src/views/resizehandler.js',
273 'src/views/serverselect.js',
274 'src/views/statusmessage.js',
276 'src/views/topicbar.js',
277 'src/views/userbox.js'
281 vars
.scripts
.push('kiwi.min.js');
284 // Any restricted server mode set?
285 if (config
.get().restrict_server
) {
286 vars
.server_settings
= {
288 server
: config
.get().restrict_server
,
289 port
: config
.get().restrict_server_port
|| 6667,
290 ssl
: config
.get().restrict_server_ssl
,
291 channel
: config
.get().restrict_server_channel
,
292 nick
: config
.get().restrict_server_nick
,
298 // Any client default settings?
299 if (config
.get().client
) {
300 vars
.server_settings
.client
= config
.get().client
;
303 // Any client plugins?
304 if (config
.get().client_plugins
&& config
.get().client_plugins
.length
> 0) {
305 vars
.client_plugins
= config
.get().client_plugins
;
308 // Get a list of available translations
309 fs
.readFile(__dirname
+ '/../client/assets/src/translations/translations.json', function (err
, translations
) {
311 return callback(err
);
314 var translation_files
;
315 translations
= JSON
.parse(translations
);
316 fs
.readdir(__dirname
+ '/../client/assets/src/translations/', function (err
, pofiles
) {
319 return callback(err
);
322 pofiles
.forEach(function (file
) {
323 var locale
= file
.slice(0, -3);
324 if ((file
.slice(-3) === '.po') && (locale
!== 'template')) {
325 vars
.translations
.push({tag
: locale
, language
: translations
[locale
]});
329 settings
= cached_settings
[debug
?'debug':'production'];
330 settings
.settings
= JSON
.stringify(vars
);
331 settings
.hash
= crypto
.createHash('md5').update(settings
.settings
).digest('hex');
333 return callback(null, settings
);