3 _kiwi
.model
.Application
= Backbone
.Model
.extend({
4 /** _kiwi.view.Application */
7 /** _kiwi.view.StatusMessage */
10 /* Address for the kiwi server */
13 initialize: function (options
) {
14 if (options
.container
) {
15 this.set('container', options
.container
);
18 // The base url to the kiwi server
19 this.set('base_path', options
.base_path
? options
.base_path
: '/kiwi');
21 // Path for the settings.json file
22 this.set('settings_path', options
.settings_path
?
23 options
.settings_path
:
24 this.get('base_path') + '/assets/settings.json'
27 // Any options sent down from the server
28 this.server_settings
= options
.server_settings
|| {};
29 this.translations
= options
.translations
|| {};
30 this.themes
= options
.themes
|| [];
32 // Best guess at where the kiwi server is if not already specified
33 this.kiwi_server
= options
.kiwi_server
|| this.detectKiwiServer();
35 // The applet to initially load
36 this.startup_applet_name
= options
.startup
|| 'kiwi_startup';
38 // Set any default settings before anything else is applied
39 if (this.server_settings
&& this.server_settings
.client
&& this.server_settings
.client
.settings
) {
40 this.applyDefaultClientSettings(this.server_settings
.client
.settings
);
47 _kiwi
.gateway
= new _kiwi
.model
.Gateway();
48 this.bindGatewayCommands(_kiwi
.gateway
);
50 this.initializeClient();
51 this.initializeGlobals();
53 this.view
.barsHide(true);
59 detectKiwiServer: function () {
60 // If running from file, default to localhost:7777 by default
61 if (window
.location
.protocol
=== 'file:') {
62 return 'http://localhost:7778';
64 // Assume the kiwi server is on the same server
65 return window
.location
.protocol
+ '//' + window
.location
.host
;
70 showStartup: function() {
71 this.startup_applet
= _kiwi
.model
.Applet
.load(this.startup_applet_name
, {no_tab
: true});
72 this.startup_applet
.tab
= this.view
.$('.console');
73 this.startup_applet
.view
.show();
77 initializeClient: function () {
78 this.view
= new _kiwi
.view
.Application({model
: this, el
: this.get('container')});
80 // Takes instances of model_network
81 this.connections
= new _kiwi
.model
.NetworkPanelList();
84 this.applet_panels
= new _kiwi
.model
.PanelList();
85 this.applet_panels
.view
.$el
.addClass('panellist applets');
86 this.view
.$el
.find('.tabs').append(this.applet_panels
.view
.$el
);
89 * Set the UI components up
91 this.controlbox
= new _kiwi
.view
.ControlBox({el
: $('#kiwi .controlbox')[0]});
92 this.bindControllboxCommands(this.controlbox
);
94 this.topicbar
= new _kiwi
.view
.TopicBar({el
: this.view
.$el
.find('.topic')[0]});
96 new _kiwi
.view
.AppToolbar({el
: _kiwi
.app
.view
.$el
.find('.toolbar .app_tools')[0]});
97 new _kiwi
.view
.ChannelTools({el
: _kiwi
.app
.view
.$el
.find('.channel_tools')[0]});
99 this.message
= new _kiwi
.view
.StatusMessage({el
: this.view
.$el
.find('.status_message')[0]});
101 this.resize_handle
= new _kiwi
.view
.ResizeHandler({el
: this.view
.$el
.find('.memberlists_resize_handle')[0]});
103 // Rejigg the UI sizes
104 this.view
.doLayout();
108 initializeGlobals: function () {
109 _kiwi
.global
.connections
= this.connections
;
111 _kiwi
.global
.panels
= this.panels
;
112 _kiwi
.global
.panels
.applets
= this.applet_panels
;
114 _kiwi
.global
.components
.Applet
= _kiwi
.model
.Applet
;
115 _kiwi
.global
.components
.Panel
=_kiwi
.model
.Panel
;
119 applyDefaultClientSettings: function (settings
) {
120 _
.each(settings
, function (value
, setting
) {
121 if (typeof _kiwi
.global
.settings
.get(setting
) === 'undefined') {
122 _kiwi
.global
.settings
.set(setting
, value
);
128 panels
: (function() {
131 var fn = function(panel_type
) {
134 // Default panel type
135 panel_type
= panel_type
|| 'connections';
137 switch (panel_type
) {
139 panels
= this.connections
.panels();
142 panels
= this.applet_panels
.models
;
146 // Active panels / server
147 panels
.active
= active_panel
;
148 panels
.server
= this.connections
.active_connection
?
149 this.connections
.active_connection
.panels
.server
:
155 _
.extend(fn
, Backbone
.Events
);
157 // Keep track of the active panel. Channel/query/server or applet
158 fn
.bind('active', function (new_active_panel
) {
159 active_panel
= new_active_panel
;
166 bindGatewayCommands: function (gw
) {
169 gw
.on('onconnect', function (event
) {
170 that
.view
.barsShow();
175 * Handle the reconnections to the kiwi server
178 // 0 = non-reconnecting state. 1 = reconnecting state.
181 // If the current or upcoming disconnect was planned
182 var unplanned_disconnect
= false;
184 gw
.on('disconnect', function (event
) {
185 unplanned_disconnect
= !gw
.disconnect_requested
;
187 if (unplanned_disconnect
) {
188 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnecting').fetch() + '...';
189 that
.message
.text(msg
, {timeout
: 10000});
192 that
.view
.$el
.removeClass('connected');
194 // Mention the disconnection on every channel
195 _kiwi
.app
.connections
.forEach(function(connection
) {
196 connection
.panels
.server
.addMsg('', msg
, 'action quit');
198 connection
.panels
.forEach(function(panel
) {
199 if (!panel
.isChannel())
202 panel
.addMsg('', msg
, 'action quit');
210 gw
.on('reconnecting', function (event
) {
211 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnect_in_x_seconds').fetch(event
.delay
/1000) + '...';
213 // Only need to mention the repeating re-connection messages on server panels
214 _kiwi
.app
.connections
.forEach(function(connection
) {
215 connection
.panels
.server
.addMsg('', msg
, 'action quit');
220 gw
.on('onconnect', function (event
) {
221 that
.view
.$el
.addClass('connected');
222 if (gw_stat
!== 1) return;
224 if (unplanned_disconnect
) {
225 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnect_successfully').fetch() + ':)';
226 that
.message
.text(msg
, {timeout
: 5000});
229 // Mention the re-connection on every channel
230 _kiwi
.app
.connections
.forEach(function(connection
) {
231 connection
.panels
.server
.addMsg('', msg
, 'action join');
233 connection
.panels
.forEach(function(panel
) {
234 if (!panel
.isChannel())
237 panel
.addMsg('', msg
, 'action join');
246 gw
.on('kiwi:reconfig', function () {
247 $.getJSON(that
.get('settings_path'), function (data
) {
248 that
.server_settings
= data
.server_settings
|| {};
249 that
.translations
= data
.translations
|| {};
254 gw
.on('kiwi:jumpserver', function (data
) {
256 // No server set? Then nowhere to jump to.
257 if (typeof data
.kiwi_server
=== 'undefined')
260 serv
= data
.kiwi_server
;
262 // Strip any trailing slash from the end
263 if (serv
[serv
.length
-1] === '/')
264 serv
= serv
.substring(0, serv
.length
-1);
266 // Force the jumpserver now?
268 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once
269 var jump_server_interval
= Math
.random() * (360 - 300) + 300;
271 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect
272 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_jumpserver_prepare').fetch();
273 that
.message
.text(msg
, {timeout
: 10000});
275 setTimeout(function forcedReconnect() {
276 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_jumpserver_reconnect').fetch();
277 that
.message
.text(msg
, {timeout
: 8000});
279 setTimeout(function forcedReconnectPartTwo() {
280 _kiwi
.app
.kiwi_server
= serv
;
282 _kiwi
.gateway
.reconnect(function() {
283 // Reconnect all the IRC connections
284 that
.connections
.forEach(function(con
){ con
.reconnect(); });
288 }, jump_server_interval
* 1000);
296 * Bind to certain commands that may be typed into the control box
298 bindControllboxCommands: function (controlbox
) {
302 $.extend(controlbox
.preprocessor
.aliases
, {
305 '/me': '/action $1+',
309 '/raw': '/quote $1+',
311 // Op related aliases
312 '/op': '/quote mode $channel +o $1+',
313 '/deop': '/quote mode $channel -o $1+',
314 '/hop': '/quote mode $channel +h $1+',
315 '/dehop': '/quote mode $channel -h $1+',
316 '/voice': '/quote mode $channel +v $1+',
317 '/devoice': '/quote mode $channel -v $1+',
318 '/k': '/kick $channel $1+',
319 '/ban': '/quote mode $channel +b $1+',
320 '/unban': '/quote mode $channel -b $1+',
323 '/slap': '/me slaps $1 around a bit with a large trout'
326 // Functions to bind to controlbox events
328 'unknown_command': unknownCommand
,
329 'command': allCommands
,
330 'command:msg': msgCommand
,
331 'command:action': actionCommand
,
332 'command:join': joinCommand
,
333 'command:part': partCommand
,
334 'command:nick': nickCommand
,
335 'command:query': queryCommand
,
336 'command:invite': inviteCommand
,
337 'command:topic': topicCommand
,
338 'command:notice': noticeCommand
,
339 'command:quote': quoteCommand
,
340 'command:kick': kickCommand
,
341 'command:clear': clearCommand
,
342 'command:ctcp': ctcpCommand
,
343 'command:server': serverCommand
,
344 'command:whois': whoisCommand
,
345 'command:whowas': whowasCommand
,
346 'command:encoding': encodingCommand
,
347 'command:channel': channelCommand
,
348 'command:applet': appletCommand
,
349 'command:settings': settingsCommand
,
350 'command:script': scriptCommand
353 fn_to_bind
['command:css'] = function (ev
) {
354 var queryString
= '?reload=' + new Date().getTime();
355 $('link[rel="stylesheet"]').each(function () {
356 this.href
= this.href
.replace(/\?.*|$/, queryString
);
360 fn_to_bind
['command:js'] = function (ev
) {
361 if (!ev
.params
[0]) return;
362 $script(ev
.params
[0] + '?' + (new Date().getTime()));
366 fn_to_bind
['command:set'] = function (ev
) {
367 if (!ev
.params
[0]) return;
369 var setting
= ev
.params
[0],
372 // Do we have a second param to set a value?
376 value
= ev
.params
.join(' ');
378 // If we're setting a true boolean value..
379 if (value
=== 'true')
382 // If we're setting a false boolean value..
383 if (value
=== 'false')
386 // If we're setting a number..
387 if (parseInt(value
, 10).toString() === value
)
388 value
= parseInt(value
, 10);
390 _kiwi
.global
.settings
.set(setting
, value
);
393 // Read the value to the user
394 _kiwi
.app
.panels().active
.addMsg('', setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
).toString());
398 fn_to_bind
['command:save'] = function (ev
) {
399 _kiwi
.global
.settings
.save();
400 _kiwi
.app
.panels().active
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_settings_saved').fetch());
404 fn_to_bind
['command:alias'] = function (ev
) {
407 // No parameters passed so list them
409 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
410 _kiwi
.app
.panels().server
.addMsg(' ', name
+ ' => ' + rule
);
415 // Deleting an alias?
416 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
418 if (name
[0] !== '/') name
= '/' + name
;
419 delete controlbox
.preprocessor
.aliases
[name
];
426 rule
= ev
.params
.join(' ');
428 // Make sure the name starts with a slash
429 if (name
[0] !== '/') name
= '/' + name
;
431 // Now actually add the alias
432 controlbox
.preprocessor
.aliases
[name
] = rule
;
436 fn_to_bind
['command:ignore'] = function (ev
) {
437 var list
= this.connections
.active_connection
.get('ignore_list');
439 // No parameters passed so list them
441 if (list
.length
> 0) {
442 _kiwi
.app
.panels().active
.addMsg(' ', _kiwi
.global
.i18n
.translate('client_models_application_ignore_title').fetch() + ':');
443 $.each(list
, function (idx
, ignored_pattern
) {
444 _kiwi
.app
.panels().active
.addMsg(' ', ignored_pattern
);
447 _kiwi
.app
.panels().active
.addMsg(' ', _kiwi
.global
.i18n
.translate('client_models_application_ignore_none').fetch());
452 // We have a parameter, so add it
453 list
.push(ev
.params
[0]);
454 this.connections
.active_connection
.set('ignore_list', list
);
455 _kiwi
.app
.panels().active
.addMsg(' ', _kiwi
.global
.i18n
.translate('client_models_application_ignore_nick').fetch(ev
.params
[0]));
459 fn_to_bind
['command:unignore'] = function (ev
) {
460 var list
= this.connections
.active_connection
.get('ignore_list');
463 _kiwi
.app
.panels().active
.addMsg(' ', _kiwi
.global
.i18n
.translate('client_models_application_ignore_stop_notice').fetch());
467 list
= _
.reject(list
, function(pattern
) {
468 return pattern
=== ev
.params
[0];
471 this.connections
.active_connection
.set('ignore_list', list
);
473 _kiwi
.app
.panels().active
.addMsg(' ', _kiwi
.global
.i18n
.translate('client_models_application_ignore_stopped').fetch(ev
.params
[0]));
477 _
.each(fn_to_bind
, function(fn
, event_name
) {
478 controlbox
.on(event_name
, _
.bind(fn
, that
));
483 isChannelName: function (channel_name
) {
484 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
486 if (!channel_name
|| !channel_name
.length
) return false;
487 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
494 // A fallback action. Send a raw command to the server
495 function unknownCommand (ev
) {
496 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
497 console
.log('RAW: ' + raw_cmd
);
498 this.connections
.active_connection
.gateway
.raw(raw_cmd
);
501 function allCommands (ev
) {}
503 function joinCommand (ev
) {
504 var panels
, channel_names
;
506 channel_names
= ev
.params
.join(' ').split(',');
507 panels
= this.connections
.active_connection
.createAndJoinChannels(channel_names
);
509 // Show the last channel if we have one
511 panels
[panels
.length
- 1].view
.show();
514 function queryCommand (ev
) {
515 var destination
, message
, panel
;
517 destination
= ev
.params
[0];
520 message
= ev
.params
.join(' ');
522 // Check if we have the panel already. If not, create it
523 panel
= this.connections
.active_connection
.panels
.getByName(destination
);
525 panel
= new _kiwi
.model
.Query({name
: destination
});
526 this.connections
.active_connection
.panels
.add(panel
);
529 if (panel
) panel
.view
.show();
532 this.connections
.active_connection
.gateway
.msg(panel
.get('name'), message
);
533 panel
.addMsg(_kiwi
.app
.connections
.active_connection
.get('nick'), message
);
538 function msgCommand (ev
) {
540 destination
= ev
.params
[0],
541 panel
= this.connections
.active_connection
.panels
.getByName(destination
) || this.panels().server
;
544 message
= formatToIrcMsg(ev
.params
.join(' '));
546 panel
.addMsg(_kiwi
.app
.connections
.active_connection
.get('nick'), message
);
547 this.connections
.active_connection
.gateway
.msg(destination
, message
);
550 function actionCommand (ev
) {
551 if (_kiwi
.app
.panels().active
.isServer()) {
555 var panel
= _kiwi
.app
.panels().active
;
556 panel
.addMsg('', '* ' + _kiwi
.app
.connections
.active_connection
.get('nick') + ' ' + ev
.params
.join(' '), 'action');
557 this.connections
.active_connection
.gateway
.action(panel
.get('name'), ev
.params
.join(' '));
560 function partCommand (ev
) {
563 if (ev
.params
.length
=== 0) {
564 this.connections
.active_connection
.gateway
.part(_kiwi
.app
.panels().active
.get('name'));
566 _
.each(ev
.params
, function (channel
) {
567 that
.connections
.active_connection
.gateway
.part(channel
);
572 function nickCommand (ev
) {
573 this.connections
.active_connection
.gateway
.changeNick(ev
.params
[0]);
576 function topicCommand (ev
) {
579 if (ev
.params
.length
=== 0) return;
581 if (this.isChannelName(ev
.params
[0])) {
582 channel_name
= ev
.params
[0];
585 channel_name
= _kiwi
.app
.panels().active
.get('name');
588 this.connections
.active_connection
.gateway
.topic(channel_name
, ev
.params
.join(' '));
591 function noticeCommand (ev
) {
594 // Make sure we have a destination and some sort of message
595 if (ev
.params
.length
<= 1) return;
597 destination
= ev
.params
[0];
600 this.connections
.active_connection
.gateway
.notice(destination
, ev
.params
.join(' '));
603 function quoteCommand (ev
) {
604 var raw
= ev
.params
.join(' ');
605 this.connections
.active_connection
.gateway
.raw(raw
);
608 function kickCommand (ev
) {
609 var nick
, panel
= _kiwi
.app
.panels().active
;
611 if (!panel
.isChannel()) return;
613 // Make sure we have a nick
614 if (ev
.params
.length
=== 0) return;
619 this.connections
.active_connection
.gateway
.kick(panel
.get('name'), nick
, ev
.params
.join(' '));
622 function clearCommand (ev
) {
623 // Can't clear a server or applet panel
624 if (_kiwi
.app
.panels().active
.isServer() || _kiwi
.app
.panels().active
.isApplet()) {
628 if (_kiwi
.app
.panels().active
.clearMessages
) {
629 _kiwi
.app
.panels().active
.clearMessages();
633 function ctcpCommand(ev
) {
636 // Make sure we have a target and a ctcp type (eg. version, time)
637 if (ev
.params
.length
< 2) return;
639 target
= ev
.params
[0];
645 this.connections
.active_connection
.gateway
.ctcp(true, type
, target
, ev
.params
.join(' '));
648 function settingsCommand (ev
) {
649 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
650 settings
.view
.show();
653 function scriptCommand (ev
) {
654 var editor
= _kiwi
.model
.Applet
.loadOnce('kiwi_script_editor');
658 function appletCommand (ev
) {
659 if (!ev
.params
[0]) return;
661 var panel
= new _kiwi
.model
.Applet();
664 // Url and name given
665 panel
.load(ev
.params
[0], ev
.params
[1]);
667 // Load a pre-loaded applet
668 if (_kiwi
.applets
[ev
.params
[0]]) {
669 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
671 _kiwi
.app
.panels().server
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_applet_notfound').fetch(ev
.params
[0]));
676 _kiwi
.app
.connections
.active_connection
.panels
.add(panel
);
682 function inviteCommand (ev
) {
685 // A nick must be specified
689 // Can only invite into channels
690 if (!_kiwi
.app
.panels().active
.isChannel())
694 channel
= _kiwi
.app
.panels().active
.get('name');
696 _kiwi
.app
.connections
.active_connection
.gateway
.raw('INVITE ' + nick
+ ' ' + channel
);
698 _kiwi
.app
.panels().active
.addMsg('', '== ' + nick
+ ' has been invited to ' + channel
, 'action');
702 function whoisCommand (ev
) {
707 } else if (_kiwi
.app
.panels().active
.isQuery()) {
708 nick
= _kiwi
.app
.panels().active
.get('name');
712 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOIS ' + nick
+ ' ' + nick
);
716 function whowasCommand (ev
) {
721 } else if (_kiwi
.app
.panels().active
.isQuery()) {
722 nick
= _kiwi
.app
.panels().active
.get('name');
726 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOWAS ' + nick
);
729 function encodingCommand (ev
) {
731 _kiwi
.gateway
.setEncoding(null, ev
.params
[0], function (success
) {
733 _kiwi
.app
.panels().active
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_encoding_changed').fetch(ev
.params
[0]));
735 _kiwi
.app
.panels().active
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_encoding_invalid').fetch(ev
.params
[0]));
739 _kiwi
.app
.panels().active
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_encoding_notspecified').fetch());
740 _kiwi
.app
.panels().active
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_encoding_usage').fetch());
744 function channelCommand (ev
) {
745 var active_panel
= _kiwi
.app
.panels().active
;
747 if (!active_panel
.isChannel())
750 new _kiwi
.model
.ChannelInfo({channel
: _kiwi
.app
.panels().active
});
753 function serverCommand (ev
) {
754 var server
, port
, ssl
, password
, nick
,
757 // If no server address given, show the new connection dialog
759 tmp
= new _kiwi
.view
.MenuBox(_kiwi
.global
.i18n
.translate('client_models_application_connection_create').fetch());
760 tmp
.addItem('new_connection', new _kiwi
.model
.NewConnection().view
.$el
);
763 // Center screen the dialog
765 top
: (this.view
.$el
.height() / 2) - (tmp
.$el
.height() / 2),
766 left
: (this.view
.$el
.width() / 2) - (tmp
.$el
.width() / 2)
772 // Port given in 'host:port' format and no specific port given after a space
773 if (ev
.params
[0].indexOf(':') > 0) {
774 tmp
= ev
.params
[0].split(':');
778 password
= ev
.params
[1] || undefined;
781 // Server + port given as 'host port'
782 server
= ev
.params
[0];
783 port
= ev
.params
[1] || 6667;
785 password
= ev
.params
[2] || undefined;
788 // + in the port means SSL
789 if (port
.toString()[0] === '+') {
791 port
= parseInt(port
.substring(1), 10);
796 // Default port if one wasn't found
799 // Use the same nick as we currently have
800 nick
= _kiwi
.app
.connections
.active_connection
.get('nick');
802 _kiwi
.app
.panels().active
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_connection_connecting').fetch(server
, port
.toString()));
804 _kiwi
.gateway
.newConnection({
810 }, function(err
, new_connection
) {
812 _kiwi
.app
.panels().active
.addMsg('', _kiwi
.global
.i18n
.translate('client_models_application_connection_error').fetch(server
, port
.toString(), err
.toString()));