1 // Holds anything kiwi client specific (ie. front, gateway, _kiwi.plugs..)
14 * A global container for third party access
15 * Will be used to access a limited subset of kiwi functionality
16 * and data (think: plugins)
19 build_version
: '', // Kiwi IRC version this is built from (Set from index.html)
20 settings
: undefined, // Instance of _kiwi.model.DataStore
21 plugins
: undefined, // Instance of _kiwi.model.PluginManager
22 events
: undefined, // Instance of PluginInterface
23 utils
: {}, // References to misc. re-usable helpers / functions
25 initUtils: function() {
26 this.utils
.randomString
= randomString
;
27 this.utils
.secondsToTime
= secondsToTime
;
28 this.utils
.parseISO8601
= parseISO8601
;
29 this.utils
.escapeRegex
= escapeRegex
;
30 this.utils
.formatIRCMsg
= formatIRCMsg
;
31 this.utils
.styleText
= styleText
;
32 this.utils
.hsl2rgb
= hsl2rgb
;
36 _kiwi
.gateway
.rpc
.call
.call(_kiwi
.gateway
.rpc
, arguments
);
39 addMediaMessageType: function(match
, buildHtml
) {
40 _kiwi
.view
.MediaMessage
.addType(match
, buildHtml
);
43 // Event managers for plugins
45 EventComponent: function(event_source
, proxy_event_name
) {
47 * proxyEvent() listens for events then re-triggers them on its own
48 * event emitter. Why? So we can .off() on this emitter without
49 * effecting the source of events. Handy for plugins that we don't
50 * trust meddling with the core events.
52 * If listening for 'all' events the arguments are as follows:
53 * 1. Name of the triggered event
55 * For all other events, we only have one argument:
58 * When this is used via `new kiwi.components.Network()`, this listens
59 * for 'all' events so the first argument is the event name which is
60 * the connection ID. We don't want to re-trigger this event name so
61 * we need to juggle the arguments to find the real event name we want
64 function proxyEvent(event_name
, event_data
) {
65 if (proxy_event_name
== 'all') {
67 event_data
= event_name
.event_data
;
68 event_name
= event_name
.event_name
;
71 this.trigger(event_name
, event_data
);
74 // The event we are to proxy
75 proxy_event_name
= proxy_event_name
|| 'all';
77 _
.extend(this, Backbone
.Events
);
78 this._source
= event_source
;
80 // Proxy the events to this dispatcher
81 event_source
.on(proxy_event_name
, proxyEvent
, this);
83 // Clean up this object
84 this.dispose = function () {
85 event_source
.off(proxy_event_name
, proxyEvent
);
87 delete this.event_source
;
91 Network: function(connection_id
) {
94 // If no connection id given, use all connections
95 if (typeof connection_id
!== 'undefined') {
96 connection_event
= 'connection:' + connection_id
.toString();
98 connection_event
= 'connection';
101 // Helper to get the network object
102 var getNetwork = function() {
103 var network
= typeof connection_id
=== 'undefined' ?
104 _kiwi
.app
.connections
.active_connection
:
105 _kiwi
.app
.connections
.getByConnectionId(connection_id
);
112 // Create the return object (events proxy from the gateway)
113 var obj
= new this.EventComponent(_kiwi
.gateway
, connection_event
);
115 // Proxy several gateway functions onto the return object
117 kiwi
: 'kiwi', raw
: 'raw', kick
: 'kick', topic
: 'topic',
118 part
: 'part', join
: 'join', action
: 'action', ctcp
: 'ctcp',
119 ctcpRequest
: 'ctcpRequest', ctcpResponse
: 'ctcpResponse',
120 notice
: 'notice', msg
: 'privmsg', changeNick
: 'changeNick',
121 channelInfo
: 'channelInfo', mode
: 'mode', quit
: 'quit'
124 _
.each(funcs
, function(gateway_fn
, func_name
) {
125 obj
[func_name
] = function() {
126 var fn_name
= gateway_fn
;
128 // Add connection_id to the argument list
129 var args
= Array
.prototype.slice
.call(arguments
, 0);
130 args
.unshift(connection_id
);
132 // Call the gateway function on behalf of this connection
133 return _kiwi
.gateway
[fn_name
].apply(_kiwi
.gateway
, args
);
137 // Add the networks getters/setters
138 obj
.get = function(name
) {
139 var network
, restricted_keys
;
141 network
= getNetwork();
149 if (restricted_keys
.indexOf(name
) > -1) {
153 return network
.get(name
);
156 obj
.set = function() {
157 var network
= getNetwork();
162 return network
.set.apply(network
, arguments
);
168 ControlInput: function() {
169 var obj
= new this.EventComponent(_kiwi
.app
.controlbox
);
171 run
: 'processInput', addPluginIcon
: 'addPluginIcon'
174 _
.each(funcs
, function(controlbox_fn
, func_name
) {
175 obj
[func_name
] = function() {
176 var fn_name
= controlbox_fn
;
177 return _kiwi
.app
.controlbox
[fn_name
].apply(_kiwi
.app
.controlbox
, arguments
);
185 // Entry point to start the kiwi application
186 init: function (opts
, callback
) {
187 var localePromise
, themePromise
,
193 // Set up the settings datastore
194 _kiwi
.global
.settings
= _kiwi
.model
.DataStore
.instance('kiwi.settings');
195 _kiwi
.global
.settings
.load();
197 // Set the window title
198 window
.document
.title
= opts
.server_settings
.client
.window_title
|| 'Kiwi IRC';
200 localePromise
= new Promise(function (resolve
) {
201 var locale
= _kiwi
.global
.settings
.get('locale') || 'magic';
202 $.getJSON(opts
.base_path
+ '/assets/locales/' + locale
+ '.json', function (locale
) {
204 that
.i18n
= new Jed(locale
);
206 that
.i18n
= new Jed();
212 themePromise
= new Promise(function (resolve
) {
213 var text_theme
= opts
.server_settings
.client
.settings
.text_theme
|| 'default';
214 $.getJSON(opts
.base_path
+ '/assets/text_themes/' + text_theme
+ '.json', function(text_theme
) {
215 opts
.text_theme
= text_theme
;
221 Promise
.all([localePromise
, themePromise
]).then(function () {
222 _kiwi
.app
= new _kiwi
.model
.Application(opts
);
224 // Start the client up
225 _kiwi
.app
.initializeInterfaces();
227 // Event emitter to let plugins interface with parts of kiwi
228 _kiwi
.global
.events
= new PluginInterface();
230 // Now everything has started up, load the plugin manager for third party plugins
231 _kiwi
.global
.plugins
= new _kiwi
.model
.PluginManager();
238 _kiwi
.app
.showStartup();
241 // Allow plugins to change the startup applet
242 registerStartupApplet: function(startup_applet_name
) {
243 _kiwi
.app
.startup_applet_name
= startup_applet_name
;
247 * Open a new IRC connection
248 * @param {Object} connection_details {nick, host, port, ssl, password, options}
249 * @param {Function} callback function(err, network){}
251 newIrcConnection: function(connection_details
, callback
) {
252 _kiwi
.gateway
.newConnection(connection_details
, callback
);
257 * Taking settings from the server and URL, extract the default server/channel/nick settings
259 defaultServerSettings: function () {
273 * Get any settings set by the server
274 * These settings may be changed in the server selection dialog or via URL parameters
276 if (_kiwi
.app
.server_settings
.client
) {
277 if (_kiwi
.app
.server_settings
.client
.nick
)
278 defaults
.nick
= _kiwi
.app
.server_settings
.client
.nick
;
280 if (_kiwi
.app
.server_settings
.client
.server
)
281 defaults
.server
= _kiwi
.app
.server_settings
.client
.server
;
283 if (_kiwi
.app
.server_settings
.client
.port
)
284 defaults
.port
= _kiwi
.app
.server_settings
.client
.port
;
286 if (_kiwi
.app
.server_settings
.client
.ssl
)
287 defaults
.ssl
= _kiwi
.app
.server_settings
.client
.ssl
;
289 if (_kiwi
.app
.server_settings
.client
.channel
)
290 defaults
.channel
= _kiwi
.app
.server_settings
.client
.channel
;
292 if (_kiwi
.app
.server_settings
.client
.channel_key
)
293 defaults
.channel_key
= _kiwi
.app
.server_settings
.client
.channel_key
;
299 * Get any settings passed in the URL
300 * These settings may be changed in the server selection dialog
303 // Any query parameters first
304 if (getQueryVariable('nick'))
305 defaults
.nick
= getQueryVariable('nick');
307 if (window
.location
.hash
)
308 defaults
.channel
= window
.location
.hash
;
311 // Process the URL part by part, extracting as we go
312 parts
= window
.location
.pathname
.toString().replace(_kiwi
.app
.get('base_path'), '').split('/');
314 if (parts
.length
> 0) {
317 if (parts
.length
> 0 && parts
[0]) {
318 // 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.
319 uricheck
= parts
[0].substr(0, 7).toLowerCase();
320 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
321 parts
[0] = decodeURIComponent(parts
[0]);
322 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
323 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
325 uricheck[1] = ssl (optional)
327 uricheck[3] = port (optional)
328 uricheck[4] = channel (optional)
329 uricheck[5] = channel key (optional, channel must also be set)
332 if (typeof uricheck
[1] !== 'undefined') {
334 if (defaults
.port
=== 6667) {
335 defaults
.port
= 6697;
338 defaults
.server
= uricheck
[2];
339 if (typeof uricheck
[3] !== 'undefined') {
340 defaults
.port
= uricheck
[3];
342 if (typeof uricheck
[4] !== 'undefined') {
343 defaults
.channel
= '#' + uricheck
[4];
344 if (typeof uricheck
[5] !== 'undefined') {
345 defaults
.channel_key
= uricheck
[5];
351 // Extract the port+ssl if we find one
352 if (parts
[0].search(/:/) > 0) {
353 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
354 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
355 if (defaults
.port
[0] === '+') {
356 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
359 defaults
.ssl
= false;
363 defaults
.server
= parts
[0];
370 if (parts
.length
> 0 && parts
[0]) {
371 defaults
.channel
= '#' + parts
[0];
376 // If any settings have been given by the server.. override any auto detected settings
378 * Get any server restrictions as set in the server config
379 * These settings can not be changed in the server selection dialog
381 if (_kiwi
.app
.server_settings
&& _kiwi
.app
.server_settings
.connection
) {
382 if (_kiwi
.app
.server_settings
.connection
.server
) {
383 defaults
.server
= _kiwi
.app
.server_settings
.connection
.server
;
386 if (_kiwi
.app
.server_settings
.connection
.port
) {
387 defaults
.port
= _kiwi
.app
.server_settings
.connection
.port
;
390 if (_kiwi
.app
.server_settings
.connection
.ssl
) {
391 defaults
.ssl
= _kiwi
.app
.server_settings
.connection
.ssl
;
394 if (_kiwi
.app
.server_settings
.connection
.channel
) {
395 defaults
.channel
= _kiwi
.app
.server_settings
.connection
.channel
;
398 if (_kiwi
.app
.server_settings
.connection
.channel_key
) {
399 defaults
.channel_key
= _kiwi
.app
.server_settings
.connection
.channel_key
;
402 if (_kiwi
.app
.server_settings
.connection
.nick
) {
403 defaults
.nick
= _kiwi
.app
.server_settings
.connection
.nick
;
407 // Set any random numbers if needed
408 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
410 if (getQueryVariable('encoding'))
411 defaults
.encoding
= getQueryVariable('encoding');
419 // If within a closure, expose the kiwi globals
420 if (typeof global
!== 'undefined') {
421 global
.kiwi
= _kiwi
.global
;
423 // Not within a closure so set a var in the current scope
424 var kiwi
= _kiwi
.global
;