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