Remove trailing comma in app.js
[KiwiIRC.git] / client / src / app.js
1 // Holds anything kiwi client specific (ie. front, gateway, _kiwi.plugs..)
2 /**
3 * @namespace
4 */
5 var _kiwi = {};
6
7 _kiwi.misc = {};
8 _kiwi.model = {};
9 _kiwi.view = {};
10 _kiwi.applets = {};
11
12
13 /**
14 * A global container for third party access
15 * Will be used to access a limited subset of kiwi functionality
16 * and data (think: plugins)
17 */
18 _kiwi.global = {
19 build_version: '', // Kiwi IRC version this is built from (Set from index.html)
20 settings: undefined, // Instance of _kiwi.model.DataStore
21 plugins: undefined, // Instance of _kiwi.model.PluginManager
22 events: undefined, // Instance of PluginInterface
23 utils: {}, // References to misc. re-usable helpers / functions
24
25 initUtils: function() {
26 this.utils.randomString = randomString;
27 this.utils.secondsToTime = secondsToTime;
28 this.utils.parseISO8601 = parseISO8601;
29 this.utils.escapeRegex = escapeRegex;
30 this.utils.formatIRCMsg = formatIRCMsg;
31 this.utils.styleText = styleText;
32 this.utils.hsl2rgb = hsl2rgb;
33 },
34
35 rpc: function() {
36 _kiwi.gateway.rpc.call.call(_kiwi.gateway.rpc, arguments);
37 },
38
39 addMediaMessageType: function(match, buildHtml) {
40 _kiwi.view.MediaMessage.addType(match, buildHtml);
41 },
42
43 // Event managers for plugins
44 components: {
45 EventComponent: function(event_source, proxy_event_name) {
46 /*
47 * proxyEvent() listens for events then re-triggers them on its own
48 * event emitter. Why? So we can .off() on this emitter without
49 * effecting the source of events. Handy for plugins that we don't
50 * trust meddling with the core events.
51 *
52 * If listening for 'all' events the arguments are as follows:
53 * 1. Name of the triggered event
54 * 2. The event data
55 * For all other events, we only have one argument:
56 * 1. The event data
57 *
58 * When this is used via `new kiwi.components.Network()`, this listens
59 * for 'all' events so the first argument is the event name which is
60 * the connection ID. We don't want to re-trigger this event name so
61 * we need to juggle the arguments to find the real event name we want
62 * to emit.
63 */
64 function proxyEvent(event_name, event_data) {
65 if (proxy_event_name == 'all') {
66 } else {
67 event_data = event_name.event_data;
68 event_name = event_name.event_name;
69 }
70
71 this.trigger(event_name, event_data);
72 }
73
74 // The event we are to proxy
75 proxy_event_name = proxy_event_name || 'all';
76
77 _.extend(this, Backbone.Events);
78 this._source = event_source;
79
80 // Proxy the events to this dispatcher
81 event_source.on(proxy_event_name, proxyEvent, this);
82
83 // Clean up this object
84 this.dispose = function () {
85 event_source.off(proxy_event_name, proxyEvent);
86 this.off();
87 delete this.event_source;
88 };
89 },
90
91 Network: function(connection_id) {
92 var connection_event;
93
94 // If no connection id given, use all connections
95 if (typeof connection_id !== 'undefined') {
96 connection_event = 'connection:' + connection_id.toString();
97 } else {
98 connection_event = 'connection';
99 }
100
101 // Helper to get the network object
102 var getNetwork = function() {
103 var network = typeof connection_id === 'undefined' ?
104 _kiwi.app.connections.active_connection :
105 _kiwi.app.connections.getByConnectionId(connection_id);
106
107 return network ?
108 network :
109 undefined;
110 };
111
112 // Create the return object (events proxy from the gateway)
113 var obj = new this.EventComponent(_kiwi.gateway, connection_event);
114
115 // Proxy several gateway functions onto the return object
116 var funcs = {
117 kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic',
118 part: 'part', join: 'join', action: 'action', ctcp: 'ctcp',
119 ctcpRequest: 'ctcpRequest', ctcpResponse: 'ctcpResponse',
120 notice: 'notice', msg: 'privmsg', changeNick: 'changeNick',
121 channelInfo: 'channelInfo', mode: 'mode', quit: 'quit'
122 };
123
124 _.each(funcs, function(gateway_fn, func_name) {
125 obj[func_name] = function() {
126 var fn_name = gateway_fn;
127
128 // Add connection_id to the argument list
129 var args = Array.prototype.slice.call(arguments, 0);
130 args.unshift(connection_id);
131
132 // Call the gateway function on behalf of this connection
133 return _kiwi.gateway[fn_name].apply(_kiwi.gateway, args);
134 };
135 });
136
137 // Add the networks getters/setters
138 obj.get = function() {
139 var network = getNetwork();
140 if (!network) {
141 return;
142 }
143
144 return network.get.apply(network, arguments);
145 };
146
147 obj.set = function() {
148 var network = getNetwork();
149 if (!network) {
150 return;
151 }
152
153 return network.set.apply(network, arguments);
154 };
155
156 return obj;
157 },
158
159 ControlInput: function() {
160 var obj = new this.EventComponent(_kiwi.app.controlbox);
161 var funcs = {
162 run: 'processInput', addPluginIcon: 'addPluginIcon'
163 };
164
165 _.each(funcs, function(controlbox_fn, func_name) {
166 obj[func_name] = function() {
167 var fn_name = controlbox_fn;
168 return _kiwi.app.controlbox[fn_name].apply(_kiwi.app.controlbox, arguments);
169 };
170 });
171
172 return obj;
173 }
174 },
175
176 // Entry point to start the kiwi application
177 init: function (opts, callback) {
178 var jobs, locale, localeLoaded, textThemeLoaded, text_theme;
179 opts = opts || {};
180
181 this.initUtils();
182
183 jobs = new JobManager();
184 jobs.onFinish(function(locale, s, xhr) {
185 _kiwi.app = new _kiwi.model.Application(opts);
186
187 // Start the client up
188 _kiwi.app.initializeInterfaces();
189
190 // Event emitter to let plugins interface with parts of kiwi
191 _kiwi.global.events = new PluginInterface();
192
193 // Now everything has started up, load the plugin manager for third party plugins
194 _kiwi.global.plugins = new _kiwi.model.PluginManager();
195
196 callback();
197 });
198
199 textThemeLoaded = function(text_theme, s, xhr) {
200 opts.text_theme = text_theme;
201
202 jobs.finishJob('load_text_theme');
203 };
204
205 localeLoaded = function(locale, s, xhr) {
206 if (locale) {
207 _kiwi.global.i18n = new Jed(locale);
208 } else {
209 _kiwi.global.i18n = new Jed();
210 }
211
212 jobs.finishJob('load_locale');
213 };
214
215 // Set up the settings datastore
216 _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');
217 _kiwi.global.settings.load();
218
219 // Set the window title
220 window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC';
221
222 jobs.registerJob('load_locale');
223 locale = _kiwi.global.settings.get('locale');
224 if (!locale) {
225 $.getJSON(opts.base_path + '/assets/locales/magic.json', localeLoaded);
226 } else {
227 $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', localeLoaded);
228 }
229
230 jobs.registerJob('load_text_theme');
231 text_theme = opts.server_settings.client.settings.text_theme || 'default';
232 $.getJSON(opts.base_path + '/assets/text_themes/' + text_theme + '.json', textThemeLoaded);
233 },
234
235 start: function() {
236 _kiwi.app.showStartup();
237 },
238
239 // Allow plugins to change the startup applet
240 registerStartupApplet: function(startup_applet_name) {
241 _kiwi.app.startup_applet_name = startup_applet_name;
242 },
243
244 /**
245 * Open a new IRC connection
246 * @param {Object} connection_details {nick, host, port, ssl, password, options}
247 * @param {Function} callback function(err, network){}
248 */
249 newIrcConnection: function(connection_details, callback) {
250 _kiwi.gateway.newConnection(connection_details, callback);
251 },
252
253
254 /**
255 * Taking settings from the server and URL, extract the default server/channel/nick settings
256 */
257 defaultServerSettings: function () {
258 var parts;
259 var defaults = {
260 nick: '',
261 server: '',
262 port: 6667,
263 ssl: false,
264 channel: '',
265 channel_key: ''
266 };
267 var uricheck;
268
269
270 /**
271 * Get any settings set by the server
272 * These settings may be changed in the server selection dialog or via URL parameters
273 */
274 if (_kiwi.app.server_settings.client) {
275 if (_kiwi.app.server_settings.client.nick)
276 defaults.nick = _kiwi.app.server_settings.client.nick;
277
278 if (_kiwi.app.server_settings.client.server)
279 defaults.server = _kiwi.app.server_settings.client.server;
280
281 if (_kiwi.app.server_settings.client.port)
282 defaults.port = _kiwi.app.server_settings.client.port;
283
284 if (_kiwi.app.server_settings.client.ssl)
285 defaults.ssl = _kiwi.app.server_settings.client.ssl;
286
287 if (_kiwi.app.server_settings.client.channel)
288 defaults.channel = _kiwi.app.server_settings.client.channel;
289
290 if (_kiwi.app.server_settings.client.channel_key)
291 defaults.channel_key = _kiwi.app.server_settings.client.channel_key;
292 }
293
294
295
296 /**
297 * Get any settings passed in the URL
298 * These settings may be changed in the server selection dialog
299 */
300
301 // Any query parameters first
302 if (getQueryVariable('nick'))
303 defaults.nick = getQueryVariable('nick');
304
305 if (window.location.hash)
306 defaults.channel = window.location.hash;
307
308
309 // Process the URL part by part, extracting as we go
310 parts = window.location.pathname.toString().replace(_kiwi.app.get('base_path'), '').split('/');
311
312 if (parts.length > 0) {
313 parts.shift();
314
315 if (parts.length > 0 && parts[0]) {
316 // Check to see if we're dealing with an irc: uri, or whether we need to extract the server/channel info from the HTTP URL path.
317 uricheck = parts[0].substr(0, 7).toLowerCase();
318 if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {
319 parts[0] = decodeURIComponent(parts[0]);
320 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
321 uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);
322 /*
323 uricheck[1] = ssl (optional)
324 uricheck[2] = host
325 uricheck[3] = port (optional)
326 uricheck[4] = channel (optional)
327 uricheck[5] = channel key (optional, channel must also be set)
328 */
329 if (uricheck) {
330 if (typeof uricheck[1] !== 'undefined') {
331 defaults.ssl = true;
332 if (defaults.port === 6667) {
333 defaults.port = 6697;
334 }
335 }
336 defaults.server = uricheck[2];
337 if (typeof uricheck[3] !== 'undefined') {
338 defaults.port = uricheck[3];
339 }
340 if (typeof uricheck[4] !== 'undefined') {
341 defaults.channel = '#' + uricheck[4];
342 if (typeof uricheck[5] !== 'undefined') {
343 defaults.channel_key = uricheck[5];
344 }
345 }
346 }
347 parts = [];
348 } else {
349 // Extract the port+ssl if we find one
350 if (parts[0].search(/:/) > 0) {
351 defaults.port = parts[0].substring(parts[0].search(/:/) + 1);
352 defaults.server = parts[0].substring(0, parts[0].search(/:/));
353 if (defaults.port[0] === '+') {
354 defaults.port = parseInt(defaults.port.substring(1), 10);
355 defaults.ssl = true;
356 } else {
357 defaults.ssl = false;
358 }
359
360 } else {
361 defaults.server = parts[0];
362 }
363
364 parts.shift();
365 }
366 }
367
368 if (parts.length > 0 && parts[0]) {
369 defaults.channel = '#' + parts[0];
370 parts.shift();
371 }
372 }
373
374 // If any settings have been given by the server.. override any auto detected settings
375 /**
376 * Get any server restrictions as set in the server config
377 * These settings can not be changed in the server selection dialog
378 */
379 if (_kiwi.app.server_settings && _kiwi.app.server_settings.connection) {
380 if (_kiwi.app.server_settings.connection.server) {
381 defaults.server = _kiwi.app.server_settings.connection.server;
382 }
383
384 if (_kiwi.app.server_settings.connection.port) {
385 defaults.port = _kiwi.app.server_settings.connection.port;
386 }
387
388 if (_kiwi.app.server_settings.connection.ssl) {
389 defaults.ssl = _kiwi.app.server_settings.connection.ssl;
390 }
391
392 if (_kiwi.app.server_settings.connection.channel) {
393 defaults.channel = _kiwi.app.server_settings.connection.channel;
394 }
395
396 if (_kiwi.app.server_settings.connection.channel_key) {
397 defaults.channel_key = _kiwi.app.server_settings.connection.channel_key;
398 }
399
400 if (_kiwi.app.server_settings.connection.nick) {
401 defaults.nick = _kiwi.app.server_settings.connection.nick;
402 }
403 }
404
405 // Set any random numbers if needed
406 defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());
407
408 if (getQueryVariable('encoding'))
409 defaults.encoding = getQueryVariable('encoding');
410
411 return defaults;
412 },
413 };
414
415
416
417 // If within a closure, expose the kiwi globals
418 if (typeof global !== 'undefined') {
419 global.kiwi = _kiwi.global;
420 } else {
421 // Not within a closure so set a var in the current scope
422 var kiwi = _kiwi.global;
423 }