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