Merge branch 'text_themes' of https://github.com/CoryChaplin/KiwiIRC into CoryChaplin...
[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.model = {};
8 _kiwi.view = {};
9 _kiwi.applets = {};
10
11
12 /**
13 * A global container for third party access
14 * Will be used to access a limited subset of kiwi functionality
15 * and data (think: plugins)
16 */
17 _kiwi.global = {
18 build_version: '', // Kiwi IRC version this is built from (Set from index.html)
19 settings: undefined, // Instance of _kiwi.model.DataStore
20 plugins: undefined,
21 utils: undefined, // TODO: Re-usable methods
22 user: undefined, // TODO: Limited user methods
23 server: undefined, // TODO: Limited server methods
24
25 // TODO: think of a better term for this as it will also refer to queries
26 channels: undefined, // TODO: Limited access to panels list
27
28 addMediaMessageType: function(match, buildHtml) {
29 _kiwi.view.MediaMessage.addType(match, buildHtml);
30 },
31
32 // Event managers for plugins
33 components: {
34 EventComponent: function(event_source, proxy_event_name) {
35 function proxyEvent(event_name, event_data) {
36 if (proxy_event_name !== 'all') {
37 event_data = event_name.event_data;
38 event_name = event_name.event_name;
39 }
40
41 this.trigger(event_name, event_data);
42 }
43
44 // The event we are to proxy
45 proxy_event_name = proxy_event_name || 'all';
46
47
48 _.extend(this, Backbone.Events);
49 this._source = event_source;
50
51 // Proxy the events to this dispatcher
52 event_source.on(proxy_event_name, proxyEvent, this);
53
54 // Clean up this object
55 this.dispose = function () {
56 event_source.off(proxy_event_name, proxyEvent);
57 this.off();
58 delete this.event_source;
59 };
60 },
61
62 Network: function(connection_id) {
63 var connection_event;
64
65 if (typeof connection_id !== 'undefined') {
66 connection_event = 'connection:' + connection_id.toString();
67 }
68
69 var obj = new this.EventComponent(_kiwi.gateway, connection_event);
70 var funcs = {
71 kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic',
72 part: 'part', join: 'join', action: 'action', ctcp: 'ctcp',
73 notice: 'notice', msg: 'privmsg', changeNick: 'changeNick',
74 channelInfo: 'channelInfo', mode: 'mode'
75 };
76
77 // Proxy each gateway method
78 _.each(funcs, function(gateway_fn, func_name) {
79 obj[func_name] = function() {
80 var fn_name = gateway_fn;
81
82 // Add connection_id to the argument list
83 var args = Array.prototype.slice.call(arguments, 0);
84 args.unshift(connection_id);
85
86 // Call the gateway function on behalf of this connection
87 return _kiwi.gateway[fn_name].apply(_kiwi.gateway, args);
88 };
89 });
90
91 return obj;
92 },
93
94 ControlInput: function() {
95 var obj = new this.EventComponent(_kiwi.app.controlbox);
96 var funcs = {
97 run: 'processInput', addPluginIcon: 'addPluginIcon'
98 };
99
100 _.each(funcs, function(controlbox_fn, func_name) {
101 obj[func_name] = function() {
102 var fn_name = controlbox_fn;
103 return _kiwi.app.controlbox[fn_name].apply(_kiwi.app.controlbox, arguments);
104 };
105 });
106
107 return obj;
108 }
109 },
110
111 // Entry point to start the kiwi application
112 init: function (opts, callback) {
113 var jobs, locale, localeLoaded, textThemeLoaded, text_theme;
114 opts = opts || {};
115
116 jobs = new JobManager();
117 jobs.onFinish(function(locale, s, xhr) {
118 _kiwi.app = new _kiwi.model.Application(opts);
119
120 // Start the client up
121 _kiwi.app.initializeInterfaces();
122
123 // Now everything has started up, load the plugin manager for third party plugins
124 _kiwi.global.plugins = new _kiwi.model.PluginManager();
125
126 callback();
127 });
128
129 textThemeLoaded = function(text_theme, s, xhr) {
130 opts.text_theme = text_theme;
131
132 jobs.finishJob('load_text_theme');
133 };
134
135 localeLoaded = function(locale, s, xhr) {
136 if (locale) {
137 _kiwi.global.i18n = new Jed(locale);
138 } else {
139 _kiwi.global.i18n = new Jed();
140 }
141
142 jobs.finishJob('load_locale');
143 };
144
145 // Set up the settings datastore
146 _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');
147 _kiwi.global.settings.load();
148
149 // Set the window title
150 window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC';
151
152 jobs.registerJob('load_locale');
153 locale = _kiwi.global.settings.get('locale');
154 if (!locale) {
155 $.getJSON(opts.base_path + '/assets/locales/magic.json', localeLoaded);
156 } else {
157 $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', localeLoaded);
158 }
159
160 jobs.registerJob('load_text_theme');
161 text_theme = opts.server_settings.client.settings.text_theme || 'default';
162 $.getJSON(opts.base_path + '/assets/text_themes/' + text_theme + '.json', textThemeLoaded);
163 },
164
165 start: function() {
166 _kiwi.app.showStartup();
167 },
168
169 // Allow plugins to change the startup applet
170 registerStartupApplet: function(startup_applet_name) {
171 _kiwi.app.startup_applet_name = startup_applet_name;
172 },
173
174 /**
175 * Open a new IRC connection
176 * @param {Object} connection_details {nick, host, port, ssl, password, options}
177 * @param {Function} callback function(err, network){}
178 */
179 newIrcConnection: function(connection_details, callback) {
180 _kiwi.gateway.newConnection(connection_details, callback);
181 },
182
183
184 /**
185 * Taking settings from the server and URL, extract the default server/channel/nick settings
186 */
187 defaultServerSettings: function () {
188 var parts;
189 var defaults = {
190 nick: '',
191 server: '',
192 port: 6667,
193 ssl: false,
194 channel: '',
195 channel_key: ''
196 };
197 var uricheck;
198
199
200 /**
201 * Get any settings set by the server
202 * These settings may be changed in the server selection dialog or via URL parameters
203 */
204 if (_kiwi.app.server_settings.client) {
205 if (_kiwi.app.server_settings.client.nick)
206 defaults.nick = _kiwi.app.server_settings.client.nick;
207
208 if (_kiwi.app.server_settings.client.server)
209 defaults.server = _kiwi.app.server_settings.client.server;
210
211 if (_kiwi.app.server_settings.client.port)
212 defaults.port = _kiwi.app.server_settings.client.port;
213
214 if (_kiwi.app.server_settings.client.ssl)
215 defaults.ssl = _kiwi.app.server_settings.client.ssl;
216
217 if (_kiwi.app.server_settings.client.channel)
218 defaults.channel = _kiwi.app.server_settings.client.channel;
219
220 if (_kiwi.app.server_settings.client.channel_key)
221 defaults.channel_key = _kiwi.app.server_settings.client.channel_key;
222 }
223
224
225
226 /**
227 * Get any settings passed in the URL
228 * These settings may be changed in the server selection dialog
229 */
230
231 // Any query parameters first
232 if (getQueryVariable('nick'))
233 defaults.nick = getQueryVariable('nick');
234
235 if (window.location.hash)
236 defaults.channel = window.location.hash;
237
238
239 // Process the URL part by part, extracting as we go
240 parts = window.location.pathname.toString().replace(_kiwi.app.get('base_path'), '').split('/');
241
242 if (parts.length > 0) {
243 parts.shift();
244
245 if (parts.length > 0 && parts[0]) {
246 // 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.
247 uricheck = parts[0].substr(0, 7).toLowerCase();
248 if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {
249 parts[0] = decodeURIComponent(parts[0]);
250 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
251 uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);
252 /*
253 uricheck[1] = ssl (optional)
254 uricheck[2] = host
255 uricheck[3] = port (optional)
256 uricheck[4] = channel (optional)
257 uricheck[5] = channel key (optional, channel must also be set)
258 */
259 if (uricheck) {
260 if (typeof uricheck[1] !== 'undefined') {
261 defaults.ssl = true;
262 if (defaults.port === 6667) {
263 defaults.port = 6697;
264 }
265 }
266 defaults.server = uricheck[2];
267 if (typeof uricheck[3] !== 'undefined') {
268 defaults.port = uricheck[3];
269 }
270 if (typeof uricheck[4] !== 'undefined') {
271 defaults.channel = '#' + uricheck[4];
272 if (typeof uricheck[5] !== 'undefined') {
273 defaults.channel_key = uricheck[5];
274 }
275 }
276 }
277 parts = [];
278 } else {
279 // Extract the port+ssl if we find one
280 if (parts[0].search(/:/) > 0) {
281 defaults.port = parts[0].substring(parts[0].search(/:/) + 1);
282 defaults.server = parts[0].substring(0, parts[0].search(/:/));
283 if (defaults.port[0] === '+') {
284 defaults.port = parseInt(defaults.port.substring(1), 10);
285 defaults.ssl = true;
286 } else {
287 defaults.ssl = false;
288 }
289
290 } else {
291 defaults.server = parts[0];
292 }
293
294 parts.shift();
295 }
296 }
297
298 if (parts.length > 0 && parts[0]) {
299 defaults.channel = '#' + parts[0];
300 parts.shift();
301 }
302 }
303
304 // If any settings have been given by the server.. override any auto detected settings
305 /**
306 * Get any server restrictions as set in the server config
307 * These settings can not be changed in the server selection dialog
308 */
309 if (_kiwi.app.server_settings && _kiwi.app.server_settings.connection) {
310 if (_kiwi.app.server_settings.connection.server) {
311 defaults.server = _kiwi.app.server_settings.connection.server;
312 }
313
314 if (_kiwi.app.server_settings.connection.port) {
315 defaults.port = _kiwi.app.server_settings.connection.port;
316 }
317
318 if (_kiwi.app.server_settings.connection.ssl) {
319 defaults.ssl = _kiwi.app.server_settings.connection.ssl;
320 }
321
322 if (_kiwi.app.server_settings.connection.channel) {
323 defaults.channel = _kiwi.app.server_settings.connection.channel;
324 }
325
326 if (_kiwi.app.server_settings.connection.channel_key) {
327 defaults.channel_key = _kiwi.app.server_settings.connection.channel_key;
328 }
329
330 if (_kiwi.app.server_settings.connection.nick) {
331 defaults.nick = _kiwi.app.server_settings.connection.nick;
332 }
333 }
334
335 // Set any random numbers if needed
336 defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());
337
338 if (getQueryVariable('encoding'))
339 defaults.encoding = getQueryVariable('encoding');
340
341 return defaults;
342 },
343 };
344
345
346
347 // If within a closure, expose the kiwi globals
348 if (typeof global !== 'undefined') {
349 global.kiwi = _kiwi.global;
350 } else {
351 // Not within a closure so set a var in the current scope
352 var kiwi = _kiwi.global;
353 }