68993a5e65ff0080e9c47e6abb29684c7cc99d44
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
: undefined, // TODO: Re-usable methods
22 user
: undefined, // TODO: Limited user methods
23 server
: undefined, // TODO: Limited server methods
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
28 addMediaMessageType: function(match
, buildHtml
) {
29 _kiwi
.view
.MediaMessage
.addType(match
, buildHtml
);
32 // Event managers for plugins
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
;
41 this.trigger(event_name
, event_data
);
44 // The event we are to proxy
45 proxy_event_name
= proxy_event_name
|| 'all';
48 _
.extend(this, Backbone
.Events
);
49 this._source
= event_source
;
51 // Proxy the events to this dispatcher
52 event_source
.on(proxy_event_name
, proxyEvent
, this);
54 // Clean up this object
55 this.dispose = function () {
56 event_source
.off(proxy_event_name
, proxyEvent
);
58 delete this.event_source
;
62 Network: function(connection_id
) {
65 if (typeof connection_id
!== 'undefined') {
66 connection_event
= 'connection:' + connection_id
.toString();
69 var obj
= new this.EventComponent(_kiwi
.gateway
, connection_event
);
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'
77 // Proxy each gateway method
78 _
.each(funcs
, function(gateway_fn
, func_name
) {
79 obj
[func_name
] = function() {
80 var fn_name
= gateway_fn
;
82 // Add connection_id to the argument list
83 var args
= Array
.prototype.slice
.call(arguments
, 0);
84 args
.unshift(connection_id
);
86 // Call the gateway function on behalf of this connection
87 return _kiwi
.gateway
[fn_name
].apply(_kiwi
.gateway
, args
);
94 ControlInput: function() {
95 var obj
= new this.EventComponent(_kiwi
.app
.controlbox
);
97 run
: 'processInput', addPluginIcon
: 'addPluginIcon'
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
);
111 // Entry point to start the kiwi application
112 init: function (opts
, callback
) {
113 var continueStart
, locale
, igniteTextTheme
, text_theme
;
116 continueInit = function (locale
, s
, xhr
) {
118 _kiwi
.global
.i18n
= new Jed(locale
);
120 _kiwi
.global
.i18n
= new Jed();
123 _kiwi
.app
= new _kiwi
.model
.Application(opts
);
125 // Start the client up
126 _kiwi
.app
.initializeInterfaces();
128 // Now everything has started up, load the plugin manager for third party plugins
129 _kiwi
.global
.plugins
= new _kiwi
.model
.PluginManager();
131 callback
&& callback();
134 igniteTextTheme = function(text_theme
, s
, xhr
) {
135 _kiwi
.global
.text_theme
= new _kiwi
.view
.TextTheme(text_theme
);
137 callback
&& callback();
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 locale
= _kiwi
.global
.settings
.get('locale');
149 $.getJSON(opts
.base_path
+ '/assets/locales/magic.json', continueInit
);
151 $.getJSON(opts
.base_path
+ '/assets/locales/' + locale
+ '.json', continueInit
);
154 text_theme
= opts
.text_theme
;
156 $.getJSON(opts
.base_path
+ '/assets/text_themes/default.json', igniteTextTheme
);
158 $.getJSON(opts
.base_path
+ '/assets/text_themes/' + text_theme
+ '.json', igniteTextTheme
);
163 _kiwi
.app
.showStartup();
166 // Allow plugins to change the startup applet
167 registerStartupApplet: function(startup_applet_name
) {
168 _kiwi
.app
.startup_applet_name
= startup_applet_name
;
172 * Open a new IRC connection
173 * @param {Object} connection_details {nick, host, port, ssl, password, options}
174 * @param {Function} callback function(err, network){}
176 newIrcConnection: function(connection_details
, callback
) {
177 _kiwi
.gateway
.newConnection(connection_details
, callback
);
182 * Taking settings from the server and URL, extract the default server/channel/nick settings
184 defaultServerSettings: function () {
198 * Get any settings set by the server
199 * These settings may be changed in the server selection dialog or via URL parameters
201 if (_kiwi
.app
.server_settings
.client
) {
202 if (_kiwi
.app
.server_settings
.client
.nick
)
203 defaults
.nick
= _kiwi
.app
.server_settings
.client
.nick
;
205 if (_kiwi
.app
.server_settings
.client
.server
)
206 defaults
.server
= _kiwi
.app
.server_settings
.client
.server
;
208 if (_kiwi
.app
.server_settings
.client
.port
)
209 defaults
.port
= _kiwi
.app
.server_settings
.client
.port
;
211 if (_kiwi
.app
.server_settings
.client
.ssl
)
212 defaults
.ssl
= _kiwi
.app
.server_settings
.client
.ssl
;
214 if (_kiwi
.app
.server_settings
.client
.channel
)
215 defaults
.channel
= _kiwi
.app
.server_settings
.client
.channel
;
217 if (_kiwi
.app
.server_settings
.client
.channel_key
)
218 defaults
.channel_key
= _kiwi
.app
.server_settings
.client
.channel_key
;
224 * Get any settings passed in the URL
225 * These settings may be changed in the server selection dialog
228 // Any query parameters first
229 if (getQueryVariable('nick'))
230 defaults
.nick
= getQueryVariable('nick');
232 if (window
.location
.hash
)
233 defaults
.channel
= window
.location
.hash
;
236 // Process the URL part by part, extracting as we go
237 parts
= window
.location
.pathname
.toString().replace(_kiwi
.app
.get('base_path'), '').split('/');
239 if (parts
.length
> 0) {
242 if (parts
.length
> 0 && parts
[0]) {
243 // 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.
244 uricheck
= parts
[0].substr(0, 7).toLowerCase();
245 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
246 parts
[0] = decodeURIComponent(parts
[0]);
247 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
248 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
250 uricheck[1] = ssl (optional)
252 uricheck[3] = port (optional)
253 uricheck[4] = channel (optional)
254 uricheck[5] = channel key (optional, channel must also be set)
257 if (typeof uricheck
[1] !== 'undefined') {
259 if (defaults
.port
=== 6667) {
260 defaults
.port
= 6697;
263 defaults
.server
= uricheck
[2];
264 if (typeof uricheck
[3] !== 'undefined') {
265 defaults
.port
= uricheck
[3];
267 if (typeof uricheck
[4] !== 'undefined') {
268 defaults
.channel
= '#' + uricheck
[4];
269 if (typeof uricheck
[5] !== 'undefined') {
270 defaults
.channel_key
= uricheck
[5];
276 // Extract the port+ssl if we find one
277 if (parts
[0].search(/:/) > 0) {
278 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
279 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
280 if (defaults
.port
[0] === '+') {
281 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
284 defaults
.ssl
= false;
288 defaults
.server
= parts
[0];
295 if (parts
.length
> 0 && parts
[0]) {
296 defaults
.channel
= '#' + parts
[0];
301 // If any settings have been given by the server.. override any auto detected settings
303 * Get any server restrictions as set in the server config
304 * These settings can not be changed in the server selection dialog
306 if (_kiwi
.app
.server_settings
&& _kiwi
.app
.server_settings
.connection
) {
307 if (_kiwi
.app
.server_settings
.connection
.server
) {
308 defaults
.server
= _kiwi
.app
.server_settings
.connection
.server
;
311 if (_kiwi
.app
.server_settings
.connection
.port
) {
312 defaults
.port
= _kiwi
.app
.server_settings
.connection
.port
;
315 if (_kiwi
.app
.server_settings
.connection
.ssl
) {
316 defaults
.ssl
= _kiwi
.app
.server_settings
.connection
.ssl
;
319 if (_kiwi
.app
.server_settings
.connection
.channel
) {
320 defaults
.channel
= _kiwi
.app
.server_settings
.connection
.channel
;
323 if (_kiwi
.app
.server_settings
.connection
.channel_key
) {
324 defaults
.channel_key
= _kiwi
.app
.server_settings
.connection
.channel_key
;
327 if (_kiwi
.app
.server_settings
.connection
.nick
) {
328 defaults
.nick
= _kiwi
.app
.server_settings
.connection
.nick
;
332 // Set any random numbers if needed
333 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
335 if (getQueryVariable('encoding'))
336 defaults
.encoding
= getQueryVariable('encoding');
344 // If within a closure, expose the kiwi globals
345 if (typeof global
!== 'undefined') {
346 global
.kiwi
= _kiwi
.global
;
348 // Not within a closure so set a var in the current scope
349 var kiwi
= _kiwi
.global
;