e4b6523d7d2e57e6e19b78e9ca85da993211adaf
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
20 plugins
: undefined, // Instance of _kiwi.model.PluginManager
21 events
: undefined, // Instance of PluginInterface
22 utils
: {}, // TODO: Re-usable methods
24 addMediaMessageType: function(match
, buildHtml
) {
25 _kiwi
.view
.MediaMessage
.addType(match
, buildHtml
);
28 // Event managers for plugins
30 EventComponent: function(event_source
, proxy_event_name
) {
31 function proxyEvent(event_name
, event_data
) {
32 if (proxy_event_name
!== 'all') {
33 event_data
= event_name
.event_data
;
34 event_name
= event_name
.event_name
;
37 this.trigger(event_name
, event_data
);
40 // The event we are to proxy
41 proxy_event_name
= proxy_event_name
|| 'all';
44 _
.extend(this, Backbone
.Events
);
45 this._source
= event_source
;
47 // Proxy the events to this dispatcher
48 event_source
.on(proxy_event_name
, proxyEvent
, this);
50 // Clean up this object
51 this.dispose = function () {
52 event_source
.off(proxy_event_name
, proxyEvent
);
54 delete this.event_source
;
58 Network: function(connection_id
) {
61 if (typeof connection_id
!== 'undefined') {
62 connection_event
= 'connection:' + connection_id
.toString();
65 var obj
= new this.EventComponent(_kiwi
.gateway
, connection_event
);
67 kiwi
: 'kiwi', raw
: 'raw', kick
: 'kick', topic
: 'topic',
68 part
: 'part', join
: 'join', action
: 'action', ctcp
: 'ctcp',
69 ctcpRequest
: 'ctcpRequest', ctcpResponse
: 'ctcpResponse',
70 notice
: 'notice', msg
: 'privmsg', changeNick
: 'changeNick',
71 channelInfo
: 'channelInfo', mode
: 'mode'
74 // Proxy each gateway method
75 _
.each(funcs
, function(gateway_fn
, func_name
) {
76 obj
[func_name
] = function() {
77 var fn_name
= gateway_fn
;
79 // Add connection_id to the argument list
80 var args
= Array
.prototype.slice
.call(arguments
, 0);
81 args
.unshift(connection_id
);
83 // Call the gateway function on behalf of this connection
84 return _kiwi
.gateway
[fn_name
].apply(_kiwi
.gateway
, args
);
91 ControlInput: function() {
92 var obj
= new this.EventComponent(_kiwi
.app
.controlbox
);
94 run
: 'processInput', addPluginIcon
: 'addPluginIcon'
97 _
.each(funcs
, function(controlbox_fn
, func_name
) {
98 obj
[func_name
] = function() {
99 var fn_name
= controlbox_fn
;
100 return _kiwi
.app
.controlbox
[fn_name
].apply(_kiwi
.app
.controlbox
, arguments
);
108 // Entry point to start the kiwi application
109 init: function (opts
, callback
) {
110 var jobs
, locale
, localeLoaded
, textThemeLoaded
, text_theme
;
113 jobs
= new JobManager();
114 jobs
.onFinish(function(locale
, s
, xhr
) {
115 _kiwi
.app
= new _kiwi
.model
.Application(opts
);
117 // Start the client up
118 _kiwi
.app
.initializeInterfaces();
120 // Event emitter to let plugins interface with parts of kiwi
121 _kiwi
.global
.events
= new PluginInterface();
123 // Let plugins interface with the RPC calls
124 _kiwi
.globals
.rpcCall
= _kiwi
.gateway
.rpcCall
;
126 // Now everything has started up, load the plugin manager for third party plugins
127 _kiwi
.global
.plugins
= new _kiwi
.model
.PluginManager();
132 textThemeLoaded = function(text_theme
, s
, xhr
) {
133 opts
.text_theme
= text_theme
;
135 jobs
.finishJob('load_text_theme');
138 localeLoaded = function(locale
, s
, xhr
) {
140 _kiwi
.global
.i18n
= new Jed(locale
);
142 _kiwi
.global
.i18n
= new Jed();
145 jobs
.finishJob('load_locale');
148 // Set up the settings datastore
149 _kiwi
.global
.settings
= _kiwi
.model
.DataStore
.instance('kiwi.settings');
150 _kiwi
.global
.settings
.load();
152 // Set the window title
153 window
.document
.title
= opts
.server_settings
.client
.window_title
|| 'Kiwi IRC';
155 jobs
.registerJob('load_locale');
156 locale
= _kiwi
.global
.settings
.get('locale');
158 $.getJSON(opts
.base_path
+ '/assets/locales/magic.json', localeLoaded
);
160 $.getJSON(opts
.base_path
+ '/assets/locales/' + locale
+ '.json', localeLoaded
);
163 jobs
.registerJob('load_text_theme');
164 text_theme
= opts
.server_settings
.client
.settings
.text_theme
|| 'default';
165 $.getJSON(opts
.base_path
+ '/assets/text_themes/' + text_theme
+ '.json', textThemeLoaded
);
169 _kiwi
.app
.showStartup();
172 // Allow plugins to change the startup applet
173 registerStartupApplet: function(startup_applet_name
) {
174 _kiwi
.app
.startup_applet_name
= startup_applet_name
;
178 * Open a new IRC connection
179 * @param {Object} connection_details {nick, host, port, ssl, password, options}
180 * @param {Function} callback function(err, network){}
182 newIrcConnection: function(connection_details
, callback
) {
183 _kiwi
.gateway
.newConnection(connection_details
, callback
);
188 * Taking settings from the server and URL, extract the default server/channel/nick settings
190 defaultServerSettings: function () {
204 * Get any settings set by the server
205 * These settings may be changed in the server selection dialog or via URL parameters
207 if (_kiwi
.app
.server_settings
.client
) {
208 if (_kiwi
.app
.server_settings
.client
.nick
)
209 defaults
.nick
= _kiwi
.app
.server_settings
.client
.nick
;
211 if (_kiwi
.app
.server_settings
.client
.server
)
212 defaults
.server
= _kiwi
.app
.server_settings
.client
.server
;
214 if (_kiwi
.app
.server_settings
.client
.port
)
215 defaults
.port
= _kiwi
.app
.server_settings
.client
.port
;
217 if (_kiwi
.app
.server_settings
.client
.ssl
)
218 defaults
.ssl
= _kiwi
.app
.server_settings
.client
.ssl
;
220 if (_kiwi
.app
.server_settings
.client
.channel
)
221 defaults
.channel
= _kiwi
.app
.server_settings
.client
.channel
;
223 if (_kiwi
.app
.server_settings
.client
.channel_key
)
224 defaults
.channel_key
= _kiwi
.app
.server_settings
.client
.channel_key
;
230 * Get any settings passed in the URL
231 * These settings may be changed in the server selection dialog
234 // Any query parameters first
235 if (getQueryVariable('nick'))
236 defaults
.nick
= getQueryVariable('nick');
238 if (window
.location
.hash
)
239 defaults
.channel
= window
.location
.hash
;
242 // Process the URL part by part, extracting as we go
243 parts
= window
.location
.pathname
.toString().replace(_kiwi
.app
.get('base_path'), '').split('/');
245 if (parts
.length
> 0) {
248 if (parts
.length
> 0 && parts
[0]) {
249 // 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.
250 uricheck
= parts
[0].substr(0, 7).toLowerCase();
251 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
252 parts
[0] = decodeURIComponent(parts
[0]);
253 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
254 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
256 uricheck[1] = ssl (optional)
258 uricheck[3] = port (optional)
259 uricheck[4] = channel (optional)
260 uricheck[5] = channel key (optional, channel must also be set)
263 if (typeof uricheck
[1] !== 'undefined') {
265 if (defaults
.port
=== 6667) {
266 defaults
.port
= 6697;
269 defaults
.server
= uricheck
[2];
270 if (typeof uricheck
[3] !== 'undefined') {
271 defaults
.port
= uricheck
[3];
273 if (typeof uricheck
[4] !== 'undefined') {
274 defaults
.channel
= '#' + uricheck
[4];
275 if (typeof uricheck
[5] !== 'undefined') {
276 defaults
.channel_key
= uricheck
[5];
282 // Extract the port+ssl if we find one
283 if (parts
[0].search(/:/) > 0) {
284 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
285 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
286 if (defaults
.port
[0] === '+') {
287 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
290 defaults
.ssl
= false;
294 defaults
.server
= parts
[0];
301 if (parts
.length
> 0 && parts
[0]) {
302 defaults
.channel
= '#' + parts
[0];
307 // If any settings have been given by the server.. override any auto detected settings
309 * Get any server restrictions as set in the server config
310 * These settings can not be changed in the server selection dialog
312 if (_kiwi
.app
.server_settings
&& _kiwi
.app
.server_settings
.connection
) {
313 if (_kiwi
.app
.server_settings
.connection
.server
) {
314 defaults
.server
= _kiwi
.app
.server_settings
.connection
.server
;
317 if (_kiwi
.app
.server_settings
.connection
.port
) {
318 defaults
.port
= _kiwi
.app
.server_settings
.connection
.port
;
321 if (_kiwi
.app
.server_settings
.connection
.ssl
) {
322 defaults
.ssl
= _kiwi
.app
.server_settings
.connection
.ssl
;
325 if (_kiwi
.app
.server_settings
.connection
.channel
) {
326 defaults
.channel
= _kiwi
.app
.server_settings
.connection
.channel
;
329 if (_kiwi
.app
.server_settings
.connection
.channel_key
) {
330 defaults
.channel_key
= _kiwi
.app
.server_settings
.connection
.channel_key
;
333 if (_kiwi
.app
.server_settings
.connection
.nick
) {
334 defaults
.nick
= _kiwi
.app
.server_settings
.connection
.nick
;
338 // Set any random numbers if needed
339 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
341 if (getQueryVariable('encoding'))
342 defaults
.encoding
= getQueryVariable('encoding');
350 // If within a closure, expose the kiwi globals
351 if (typeof global
!== 'undefined') {
352 global
.kiwi
= _kiwi
.global
;
354 // Not within a closure so set a var in the current scope
355 var kiwi
= _kiwi
.global
;