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 ctcpRequest
: 'ctcpRequest', ctcpResponse
: 'ctcpResponse',
69 notice
: 'notice', msg
: 'privmsg', changeNick
: 'changeNick',
70 channelInfo
: 'channelInfo', mode
: 'mode'
73 // Proxy each gateway method
74 _
.each(funcs
, function(gateway_fn
, func_name
) {
75 obj
[func_name
] = function() {
76 var fn_name
= gateway_fn
;
78 // Add connection_id to the argument list
79 var args
= Array
.prototype.slice
.call(arguments
, 0);
80 args
.unshift(connection_id
);
82 // Call the gateway function on behalf of this connection
83 return _kiwi
.gateway
[fn_name
].apply(_kiwi
.gateway
, args
);
90 ControlInput: function() {
91 var obj
= new this.EventComponent(_kiwi
.app
.controlbox
);
93 run
: 'processInput', addPluginIcon
: 'addPluginIcon'
96 _
.each(funcs
, function(controlbox_fn
, func_name
) {
97 obj
[func_name
] = function() {
98 var fn_name
= controlbox_fn
;
99 return _kiwi
.app
.controlbox
[fn_name
].apply(_kiwi
.app
.controlbox
, arguments
);
107 // Entry point to start the kiwi application
108 init: function (opts
, callback
) {
109 var jobs
, locale
, localeLoaded
, textThemeLoaded
, text_theme
;
112 jobs
= new JobManager();
113 jobs
.onFinish(function(locale
, s
, xhr
) {
114 _kiwi
.app
= new _kiwi
.model
.Application(opts
);
116 // Start the client up
117 _kiwi
.app
.initializeInterfaces();
119 // Now everything has started up, load the plugin manager for third party plugins
120 _kiwi
.global
.plugins
= new _kiwi
.model
.PluginManager();
125 textThemeLoaded = function(text_theme
, s
, xhr
) {
126 opts
.text_theme
= text_theme
;
128 jobs
.finishJob('load_text_theme');
131 localeLoaded = function(locale
, s
, xhr
) {
133 _kiwi
.global
.i18n
= new Jed(locale
);
135 _kiwi
.global
.i18n
= new Jed();
138 jobs
.finishJob('load_locale');
141 // Set up the settings datastore
142 _kiwi
.global
.settings
= _kiwi
.model
.DataStore
.instance('kiwi.settings');
143 _kiwi
.global
.settings
.load();
145 // Set the window title
146 window
.document
.title
= opts
.server_settings
.client
.window_title
|| 'Kiwi IRC';
148 jobs
.registerJob('load_locale');
149 locale
= _kiwi
.global
.settings
.get('locale');
151 $.getJSON(opts
.base_path
+ '/assets/locales/magic.json', localeLoaded
);
153 $.getJSON(opts
.base_path
+ '/assets/locales/' + locale
+ '.json', localeLoaded
);
156 jobs
.registerJob('load_text_theme');
157 text_theme
= opts
.server_settings
.client
.settings
.text_theme
|| 'default';
158 $.getJSON(opts
.base_path
+ '/assets/text_themes/' + text_theme
+ '.json', textThemeLoaded
);
162 _kiwi
.app
.showStartup();
165 // Allow plugins to change the startup applet
166 registerStartupApplet: function(startup_applet_name
) {
167 _kiwi
.app
.startup_applet_name
= startup_applet_name
;
171 * Open a new IRC connection
172 * @param {Object} connection_details {nick, host, port, ssl, password, options}
173 * @param {Function} callback function(err, network){}
175 newIrcConnection: function(connection_details
, callback
) {
176 _kiwi
.gateway
.newConnection(connection_details
, callback
);
181 * Taking settings from the server and URL, extract the default server/channel/nick settings
183 defaultServerSettings: function () {
197 * Get any settings set by the server
198 * These settings may be changed in the server selection dialog or via URL parameters
200 if (_kiwi
.app
.server_settings
.client
) {
201 if (_kiwi
.app
.server_settings
.client
.nick
)
202 defaults
.nick
= _kiwi
.app
.server_settings
.client
.nick
;
204 if (_kiwi
.app
.server_settings
.client
.server
)
205 defaults
.server
= _kiwi
.app
.server_settings
.client
.server
;
207 if (_kiwi
.app
.server_settings
.client
.port
)
208 defaults
.port
= _kiwi
.app
.server_settings
.client
.port
;
210 if (_kiwi
.app
.server_settings
.client
.ssl
)
211 defaults
.ssl
= _kiwi
.app
.server_settings
.client
.ssl
;
213 if (_kiwi
.app
.server_settings
.client
.channel
)
214 defaults
.channel
= _kiwi
.app
.server_settings
.client
.channel
;
216 if (_kiwi
.app
.server_settings
.client
.channel_key
)
217 defaults
.channel_key
= _kiwi
.app
.server_settings
.client
.channel_key
;
223 * Get any settings passed in the URL
224 * These settings may be changed in the server selection dialog
227 // Any query parameters first
228 if (getQueryVariable('nick'))
229 defaults
.nick
= getQueryVariable('nick');
231 if (window
.location
.hash
)
232 defaults
.channel
= window
.location
.hash
;
235 // Process the URL part by part, extracting as we go
236 parts
= window
.location
.pathname
.toString().replace(_kiwi
.app
.get('base_path'), '').split('/');
238 if (parts
.length
> 0) {
241 if (parts
.length
> 0 && parts
[0]) {
242 // 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.
243 uricheck
= parts
[0].substr(0, 7).toLowerCase();
244 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
245 parts
[0] = decodeURIComponent(parts
[0]);
246 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
247 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
249 uricheck[1] = ssl (optional)
251 uricheck[3] = port (optional)
252 uricheck[4] = channel (optional)
253 uricheck[5] = channel key (optional, channel must also be set)
256 if (typeof uricheck
[1] !== 'undefined') {
258 if (defaults
.port
=== 6667) {
259 defaults
.port
= 6697;
262 defaults
.server
= uricheck
[2];
263 if (typeof uricheck
[3] !== 'undefined') {
264 defaults
.port
= uricheck
[3];
266 if (typeof uricheck
[4] !== 'undefined') {
267 defaults
.channel
= '#' + uricheck
[4];
268 if (typeof uricheck
[5] !== 'undefined') {
269 defaults
.channel_key
= uricheck
[5];
275 // Extract the port+ssl if we find one
276 if (parts
[0].search(/:/) > 0) {
277 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
278 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
279 if (defaults
.port
[0] === '+') {
280 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
283 defaults
.ssl
= false;
287 defaults
.server
= parts
[0];
294 if (parts
.length
> 0 && parts
[0]) {
295 defaults
.channel
= '#' + parts
[0];
300 // If any settings have been given by the server.. override any auto detected settings
302 * Get any server restrictions as set in the server config
303 * These settings can not be changed in the server selection dialog
305 if (_kiwi
.app
.server_settings
&& _kiwi
.app
.server_settings
.connection
) {
306 if (_kiwi
.app
.server_settings
.connection
.server
) {
307 defaults
.server
= _kiwi
.app
.server_settings
.connection
.server
;
310 if (_kiwi
.app
.server_settings
.connection
.port
) {
311 defaults
.port
= _kiwi
.app
.server_settings
.connection
.port
;
314 if (_kiwi
.app
.server_settings
.connection
.ssl
) {
315 defaults
.ssl
= _kiwi
.app
.server_settings
.connection
.ssl
;
318 if (_kiwi
.app
.server_settings
.connection
.channel
) {
319 defaults
.channel
= _kiwi
.app
.server_settings
.connection
.channel
;
322 if (_kiwi
.app
.server_settings
.connection
.channel_key
) {
323 defaults
.channel_key
= _kiwi
.app
.server_settings
.connection
.channel_key
;
326 if (_kiwi
.app
.server_settings
.connection
.nick
) {
327 defaults
.nick
= _kiwi
.app
.server_settings
.connection
.nick
;
331 // Set any random numbers if needed
332 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
334 if (getQueryVariable('encoding'))
335 defaults
.encoding
= getQueryVariable('encoding');
343 // If within a closure, expose the kiwi globals
344 if (typeof global
!== 'undefined') {
345 global
.kiwi
= _kiwi
.global
;
347 // Not within a closure so set a var in the current scope
348 var kiwi
= _kiwi
.global
;