1 // Holds anything kiwi client specific (ie. front, gateway, _kiwi.plugs..)
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)
18 build_version
: '', // Kiwi IRC version this is built from (Set from index.html)
19 settings
: undefined, // Instance of _kiwi.model.DataStore
21 utils
: {}, // TODO: Re-usable methods
23 addMediaMessageType: function(match
, buildHtml
) {
24 _kiwi
.view
.MediaMessage
.addType(match
, buildHtml
);
27 // Event managers for plugins
29 EventComponent: function(event_source
, proxy_event_name
) {
30 function proxyEvent(event_name
, event_data
) {
31 if (proxy_event_name
!== 'all') {
32 event_data
= event_name
.event_data
;
33 event_name
= event_name
.event_name
;
36 this.trigger(event_name
, event_data
);
39 // The event we are to proxy
40 proxy_event_name
= proxy_event_name
|| 'all';
43 _
.extend(this, Backbone
.Events
);
44 this._source
= event_source
;
46 // Proxy the events to this dispatcher
47 event_source
.on(proxy_event_name
, proxyEvent
, this);
49 // Clean up this object
50 this.dispose = function () {
51 event_source
.off(proxy_event_name
, proxyEvent
);
53 delete this.event_source
;
57 Network: function(connection_id
) {
60 if (typeof connection_id
!== 'undefined') {
61 connection_event
= 'connection:' + connection_id
.toString();
64 var obj
= new this.EventComponent(_kiwi
.gateway
, connection_event
);
66 kiwi
: 'kiwi', raw
: 'raw', kick
: 'kick', topic
: 'topic',
67 part
: 'part', join
: 'join', action
: 'action', ctcp
: 'ctcp',
68 notice
: 'notice', msg
: 'privmsg', changeNick
: 'changeNick',
69 channelInfo
: 'channelInfo', mode
: 'mode'
72 // Proxy each gateway method
73 _
.each(funcs
, function(gateway_fn
, func_name
) {
74 obj
[func_name
] = function() {
75 var fn_name
= gateway_fn
;
77 // Add connection_id to the argument list
78 var args
= Array
.prototype.slice
.call(arguments
, 0);
79 args
.unshift(connection_id
);
81 // Call the gateway function on behalf of this connection
82 return _kiwi
.gateway
[fn_name
].apply(_kiwi
.gateway
, args
);
89 ControlInput: function() {
90 var obj
= new this.EventComponent(_kiwi
.app
.controlbox
);
92 run
: 'processInput', addPluginIcon
: 'addPluginIcon'
95 _
.each(funcs
, function(controlbox_fn
, func_name
) {
96 obj
[func_name
] = function() {
97 var fn_name
= controlbox_fn
;
98 return _kiwi
.app
.controlbox
[fn_name
].apply(_kiwi
.app
.controlbox
, arguments
);
106 // Entry point to start the kiwi application
107 init: function (opts
, callback
) {
108 var jobs
, locale
, localeLoaded
, textThemeLoaded
, text_theme
;
111 jobs
= new JobManager();
112 jobs
.onFinish(function(locale
, s
, xhr
) {
113 _kiwi
.app
= new _kiwi
.model
.Application(opts
);
115 // Start the client up
116 _kiwi
.app
.initializeInterfaces();
118 // Now everything has started up, load the plugin manager for third party plugins
119 _kiwi
.global
.plugins
= new _kiwi
.model
.PluginManager();
124 textThemeLoaded = function(text_theme
, s
, xhr
) {
125 opts
.text_theme
= text_theme
;
127 jobs
.finishJob('load_text_theme');
130 localeLoaded = function(locale
, s
, xhr
) {
132 _kiwi
.global
.i18n
= new Jed(locale
);
134 _kiwi
.global
.i18n
= new Jed();
137 jobs
.finishJob('load_locale');
140 // Set up the settings datastore
141 _kiwi
.global
.settings
= _kiwi
.model
.DataStore
.instance('kiwi.settings');
142 _kiwi
.global
.settings
.load();
144 // Set the window title
145 window
.document
.title
= opts
.server_settings
.client
.window_title
|| 'Kiwi IRC';
147 jobs
.registerJob('load_locale');
148 locale
= _kiwi
.global
.settings
.get('locale');
150 $.getJSON(opts
.base_path
+ '/assets/locales/magic.json', localeLoaded
);
152 $.getJSON(opts
.base_path
+ '/assets/locales/' + locale
+ '.json', localeLoaded
);
155 jobs
.registerJob('load_text_theme');
156 text_theme
= opts
.server_settings
.client
.settings
.text_theme
|| 'default';
157 $.getJSON(opts
.base_path
+ '/assets/text_themes/' + text_theme
+ '.json', textThemeLoaded
);
161 _kiwi
.app
.showStartup();
164 // Allow plugins to change the startup applet
165 registerStartupApplet: function(startup_applet_name
) {
166 _kiwi
.app
.startup_applet_name
= startup_applet_name
;
170 * Open a new IRC connection
171 * @param {Object} connection_details {nick, host, port, ssl, password, options}
172 * @param {Function} callback function(err, network){}
174 newIrcConnection: function(connection_details
, callback
) {
175 _kiwi
.gateway
.newConnection(connection_details
, callback
);
180 * Taking settings from the server and URL, extract the default server/channel/nick settings
182 defaultServerSettings: function () {
196 * Get any settings set by the server
197 * These settings may be changed in the server selection dialog or via URL parameters
199 if (_kiwi
.app
.server_settings
.client
) {
200 if (_kiwi
.app
.server_settings
.client
.nick
)
201 defaults
.nick
= _kiwi
.app
.server_settings
.client
.nick
;
203 if (_kiwi
.app
.server_settings
.client
.server
)
204 defaults
.server
= _kiwi
.app
.server_settings
.client
.server
;
206 if (_kiwi
.app
.server_settings
.client
.port
)
207 defaults
.port
= _kiwi
.app
.server_settings
.client
.port
;
209 if (_kiwi
.app
.server_settings
.client
.ssl
)
210 defaults
.ssl
= _kiwi
.app
.server_settings
.client
.ssl
;
212 if (_kiwi
.app
.server_settings
.client
.channel
)
213 defaults
.channel
= _kiwi
.app
.server_settings
.client
.channel
;
215 if (_kiwi
.app
.server_settings
.client
.channel_key
)
216 defaults
.channel_key
= _kiwi
.app
.server_settings
.client
.channel_key
;
222 * Get any settings passed in the URL
223 * These settings may be changed in the server selection dialog
226 // Any query parameters first
227 if (getQueryVariable('nick'))
228 defaults
.nick
= getQueryVariable('nick');
230 if (window
.location
.hash
)
231 defaults
.channel
= window
.location
.hash
;
234 // Process the URL part by part, extracting as we go
235 parts
= window
.location
.pathname
.toString().replace(_kiwi
.app
.get('base_path'), '').split('/');
237 if (parts
.length
> 0) {
240 if (parts
.length
> 0 && parts
[0]) {
241 // 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.
242 uricheck
= parts
[0].substr(0, 7).toLowerCase();
243 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
244 parts
[0] = decodeURIComponent(parts
[0]);
245 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
246 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
248 uricheck[1] = ssl (optional)
250 uricheck[3] = port (optional)
251 uricheck[4] = channel (optional)
252 uricheck[5] = channel key (optional, channel must also be set)
255 if (typeof uricheck
[1] !== 'undefined') {
257 if (defaults
.port
=== 6667) {
258 defaults
.port
= 6697;
261 defaults
.server
= uricheck
[2];
262 if (typeof uricheck
[3] !== 'undefined') {
263 defaults
.port
= uricheck
[3];
265 if (typeof uricheck
[4] !== 'undefined') {
266 defaults
.channel
= '#' + uricheck
[4];
267 if (typeof uricheck
[5] !== 'undefined') {
268 defaults
.channel_key
= uricheck
[5];
274 // Extract the port+ssl if we find one
275 if (parts
[0].search(/:/) > 0) {
276 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
277 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
278 if (defaults
.port
[0] === '+') {
279 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
282 defaults
.ssl
= false;
286 defaults
.server
= parts
[0];
293 if (parts
.length
> 0 && parts
[0]) {
294 defaults
.channel
= '#' + parts
[0];
299 // If any settings have been given by the server.. override any auto detected settings
301 * Get any server restrictions as set in the server config
302 * These settings can not be changed in the server selection dialog
304 if (_kiwi
.app
.server_settings
&& _kiwi
.app
.server_settings
.connection
) {
305 if (_kiwi
.app
.server_settings
.connection
.server
) {
306 defaults
.server
= _kiwi
.app
.server_settings
.connection
.server
;
309 if (_kiwi
.app
.server_settings
.connection
.port
) {
310 defaults
.port
= _kiwi
.app
.server_settings
.connection
.port
;
313 if (_kiwi
.app
.server_settings
.connection
.ssl
) {
314 defaults
.ssl
= _kiwi
.app
.server_settings
.connection
.ssl
;
317 if (_kiwi
.app
.server_settings
.connection
.channel
) {
318 defaults
.channel
= _kiwi
.app
.server_settings
.connection
.channel
;
321 if (_kiwi
.app
.server_settings
.connection
.channel_key
) {
322 defaults
.channel_key
= _kiwi
.app
.server_settings
.connection
.channel_key
;
325 if (_kiwi
.app
.server_settings
.connection
.nick
) {
326 defaults
.nick
= _kiwi
.app
.server_settings
.connection
.nick
;
330 // Set any random numbers if needed
331 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
333 if (getQueryVariable('encoding'))
334 defaults
.encoding
= getQueryVariable('encoding');
342 // If within a closure, expose the kiwi globals
343 if (typeof global
!== 'undefined') {
344 global
.kiwi
= _kiwi
.global
;
346 // Not within a closure so set a var in the current scope
347 var kiwi
= _kiwi
.global
;