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
);
45 initializeInterfaces: function () {
47 _kiwi
.gateway
= new _kiwi
.model
.Gateway();
48 this.bindGatewayCommands(_kiwi
.gateway
);
50 this.initializeClient();
51 this.initializeGlobals();
53 this.view
.barsHide(true);
57 detectKiwiServer: function () {
58 // If running from file, default to localhost:7777 by default
59 if (window
.location
.protocol
=== 'file:') {
60 return 'http://localhost:7778';
62 // Assume the kiwi server is on the same server
63 return window
.location
.protocol
+ '//' + window
.location
.host
;
68 showStartup: function() {
69 this.startup_applet
= _kiwi
.model
.Applet
.load(this.startup_applet_name
, {no_tab
: true});
70 this.startup_applet
.tab
= this.view
.$('.console');
71 this.startup_applet
.view
.show();
75 initializeClient: function () {
76 this.view
= new _kiwi
.view
.Application({model
: this, el
: this.get('container')});
78 // Takes instances of model_network
79 this.connections
= new _kiwi
.model
.NetworkPanelList();
82 this.applet_panels
= new _kiwi
.model
.PanelList();
83 this.applet_panels
.view
.$el
.addClass('panellist applets');
84 this.view
.$el
.find('.tabs').append(this.applet_panels
.view
.$el
);
87 * Set the UI components up
89 this.controlbox
= new _kiwi
.view
.ControlBox({el
: $('#kiwi .controlbox')[0]});
90 this.bindControllboxCommands(this.controlbox
);
92 this.topicbar
= new _kiwi
.view
.TopicBar({el
: this.view
.$el
.find('.topic')[0]});
94 new _kiwi
.view
.AppToolbar({el
: _kiwi
.app
.view
.$el
.find('.toolbar .app_tools')[0]});
95 new _kiwi
.view
.ChannelTools({el
: _kiwi
.app
.view
.$el
.find('.channel_tools')[0]});
97 this.message
= new _kiwi
.view
.StatusMessage({el
: this.view
.$el
.find('.status_message')[0]});
99 this.resize_handle
= new _kiwi
.view
.ResizeHandler({el
: this.view
.$el
.find('.memberlists_resize_handle')[0]});
101 // Rejigg the UI sizes
102 this.view
.doLayout();
106 initializeGlobals: function () {
107 _kiwi
.global
.connections
= this.connections
;
109 _kiwi
.global
.panels
= this.panels
;
110 _kiwi
.global
.panels
.applets
= this.applet_panels
;
112 _kiwi
.global
.components
.Applet
= _kiwi
.model
.Applet
;
113 _kiwi
.global
.components
.Panel
=_kiwi
.model
.Panel
;
117 applyDefaultClientSettings: function (settings
) {
118 _
.each(settings
, function (value
, setting
) {
119 if (typeof _kiwi
.global
.settings
.get(setting
) === 'undefined') {
120 _kiwi
.global
.settings
.set(setting
, value
);
126 panels
: (function() {
129 var fn = function(panel_type
) {
132 // Default panel type
133 panel_type
= panel_type
|| 'connections';
135 switch (panel_type
) {
137 panels
= this.connections
.panels();
140 panels
= this.applet_panels
.models
;
144 // Active panels / server
145 panels
.active
= active_panel
;
146 panels
.server
= this.connections
.active_connection
?
147 this.connections
.active_connection
.panels
.server
:
153 _
.extend(fn
, Backbone
.Events
);
155 // Keep track of the active panel. Channel/query/server or applet
156 fn
.bind('active', function (new_active_panel
) {
157 active_panel
= new_active_panel
;
164 bindGatewayCommands: function (gw
) {
167 gw
.on('onconnect', function (event
) {
168 that
.view
.barsShow();
173 * Handle the reconnections to the kiwi server
176 // 0 = non-reconnecting state. 1 = reconnecting state.
179 // If the current or upcoming disconnect was planned
180 var unplanned_disconnect
= false;
182 gw
.on('disconnect', function (event
) {
183 unplanned_disconnect
= !gw
.disconnect_requested
;
185 if (unplanned_disconnect
) {
186 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnecting').fetch() + '...';
187 that
.message
.text(msg
, {timeout
: 10000});
190 that
.view
.$el
.removeClass('connected');
192 // Mention the disconnection on every channel
193 _kiwi
.app
.connections
.forEach(function(connection
) {
194 connection
.panels
.server
.addMsg('', styleText('quit', {'%T': msg
}), 'action quit');
196 connection
.panels
.forEach(function(panel
) {
197 if (!panel
.isChannel())
200 panel
.addMsg('', styleText('quit', {'%T': msg
}), 'action quit');
208 gw
.on('reconnecting', function (event
) {
209 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnect_in_x_seconds').fetch(event
.delay
/1000) + '...';
211 // Only need to mention the repeating re-connection messages on server panels
212 _kiwi
.app
.connections
.forEach(function(connection
) {
213 connection
.panels
.server
.addMsg('', styleText('quit', {'%T': msg
}), 'action quit');
218 gw
.on('onconnect', function (event
) {
219 that
.view
.$el
.addClass('connected');
220 if (gw_stat
!== 1) return;
222 if (unplanned_disconnect
) {
223 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnect_successfully').fetch() + ':)';
224 that
.message
.text(msg
, {timeout
: 5000});
227 // Mention the re-connection on every channel
228 _kiwi
.app
.connections
.forEach(function(connection
) {
229 connection
.panels
.server
.addMsg('', styleText('rejoin', {'%T': msg
}), 'action join');
231 connection
.panels
.forEach(function(panel
) {
232 if (!panel
.isChannel())
235 panel
.addMsg('', styleText('rejoin', {'%T': msg
}), 'action join');
244 gw
.on('kiwi:reconfig', function () {
245 $.getJSON(that
.get('settings_path'), function (data
) {
246 that
.server_settings
= data
.server_settings
|| {};
247 that
.translations
= data
.translations
|| {};
252 gw
.on('kiwi:jumpserver', function (data
) {
254 // No server set? Then nowhere to jump to.
255 if (typeof data
.kiwi_server
=== 'undefined')
258 serv
= data
.kiwi_server
;
260 // Strip any trailing slash from the end
261 if (serv
[serv
.length
-1] === '/')
262 serv
= serv
.substring(0, serv
.length
-1);
264 // Force the jumpserver now?
266 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once
267 var jump_server_interval
= Math
.random() * (360 - 300) + 300;
269 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect
270 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_jumpserver_prepare').fetch();
271 that
.message
.text(msg
, {timeout
: 10000});
273 setTimeout(function forcedReconnect() {
274 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_jumpserver_reconnect').fetch();
275 that
.message
.text(msg
, {timeout
: 8000});
277 setTimeout(function forcedReconnectPartTwo() {
278 _kiwi
.app
.kiwi_server
= serv
;
280 _kiwi
.gateway
.reconnect(function() {
281 // Reconnect all the IRC connections
282 that
.connections
.forEach(function(con
){ con
.reconnect(); });
286 }, jump_server_interval
* 1000);
294 * Bind to certain commands that may be typed into the control box
296 bindControllboxCommands: function (controlbox
) {
300 $.extend(controlbox
.preprocessor
.aliases
, {
303 '/me': '/action $1+',
307 '/raw': '/quote $1+',
309 // Op related aliases
310 '/op': '/quote mode $channel +o $1+',
311 '/deop': '/quote mode $channel -o $1+',
312 '/hop': '/quote mode $channel +h $1+',
313 '/dehop': '/quote mode $channel -h $1+',
314 '/voice': '/quote mode $channel +v $1+',
315 '/devoice': '/quote mode $channel -v $1+',
316 '/k': '/kick $channel $1+',
317 '/ban': '/quote mode $channel +b $1+',
318 '/unban': '/quote mode $channel -b $1+',
321 '/slap': '/me slaps $1 around a bit with a large trout'
324 // Functions to bind to controlbox events
326 'unknown_command': unknownCommand
,
327 'command': allCommands
,
328 'command:msg': msgCommand
,
329 'command:action': actionCommand
,
330 'command:join': joinCommand
,
331 'command:part': partCommand
,
332 'command:nick': nickCommand
,
333 'command:query': queryCommand
,
334 'command:invite': inviteCommand
,
335 'command:topic': topicCommand
,
336 'command:notice': noticeCommand
,
337 'command:quote': quoteCommand
,
338 'command:kick': kickCommand
,
339 'command:clear': clearCommand
,
340 'command:ctcp': ctcpCommand
,
341 'command:server': serverCommand
,
342 'command:whois': whoisCommand
,
343 'command:whowas': whowasCommand
,
344 'command:encoding': encodingCommand
,
345 'command:channel': channelCommand
,
346 'command:applet': appletCommand
,
347 'command:settings': settingsCommand
,
348 'command:script': scriptCommand
351 fn_to_bind
['command:css'] = function (ev
) {
352 var queryString
= '?reload=' + new Date().getTime();
353 $('link[rel="stylesheet"]').each(function () {
354 this.href
= this.href
.replace(/\?.*|$/, queryString
);
358 fn_to_bind
['command:js'] = function (ev
) {
359 if (!ev
.params
[0]) return;
360 $script(ev
.params
[0] + '?' + (new Date().getTime()));
364 fn_to_bind
['command:set'] = function (ev
) {
365 if (!ev
.params
[0]) return;
367 var setting
= ev
.params
[0],
370 // Do we have a second param to set a value?
374 value
= ev
.params
.join(' ');
376 // If we're setting a true boolean value..
377 if (value
=== 'true')
380 // If we're setting a false boolean value..
381 if (value
=== 'false')
384 // If we're setting a number..
385 if (parseInt(value
, 10).toString() === value
)
386 value
= parseInt(value
, 10);
388 _kiwi
.global
.settings
.set(setting
, value
);
391 // Read the value to the user
392 _kiwi
.app
.panels().active
.addMsg('', styleText('set_setting', {'%T': setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
).toString()}));
396 fn_to_bind
['command:save'] = function (ev
) {
397 _kiwi
.global
.settings
.save();
398 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_settings_saved', {'%T': translateText('client_models_application_settings_saved')}));
402 fn_to_bind
['command:alias'] = function (ev
) {
405 // No parameters passed so list them
407 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
408 _kiwi
.app
.panels().server
.addMsg(' ', styleText('list_aliases', {'%T': name
+ ' => ' + rule
}));
413 // Deleting an alias?
414 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
416 if (name
[0] !== '/') name
= '/' + name
;
417 delete controlbox
.preprocessor
.aliases
[name
];
424 rule
= ev
.params
.join(' ');
426 // Make sure the name starts with a slash
427 if (name
[0] !== '/') name
= '/' + name
;
429 // Now actually add the alias
430 controlbox
.preprocessor
.aliases
[name
] = rule
;
434 fn_to_bind
['command:ignore'] = function (ev
) {
435 var list
= this.connections
.active_connection
.get('ignore_list');
437 // No parameters passed so list them
439 if (list
.length
> 0) {
440 _kiwi
.app
.panels().active
.addMsg(' ', styleText('client_models_application_ignore_title', {'%T': translateText('client_models_application_ignore_title')}));
441 $.each(list
, function (idx
, ignored_pattern
) {
442 _kiwi
.app
.panels().active
.addMsg(' ', styleText('ignored_pattern', {'%T': ignored_pattern
}));
445 _kiwi
.app
.panels().active
.addMsg(' ', styleText('client_models_application_ignore_none', {'%T': translateText('client_models_application_ignore_none')}));
450 // We have a parameter, so add it
451 list
.push(ev
.params
[0]);
452 this.connections
.active_connection
.set('ignore_list', list
);
453 _kiwi
.app
.panels().active
.addMsg(' ', styleText('client_models_application_ignore_nick', {'%T': translateText('client_models_application_ignore_nick', [ev
.params
[0]])}));
457 fn_to_bind
['command:unignore'] = function (ev
) {
458 var list
= this.connections
.active_connection
.get('ignore_list');
461 _kiwi
.app
.panels().active
.addMsg(' ', styleText('client_models_application_ignore_stop_notice', {'%T': translateText('client_models_application_ignore_stop_notice')}));
465 list
= _
.reject(list
, function(pattern
) {
466 return pattern
=== ev
.params
[0];
469 this.connections
.active_connection
.set('ignore_list', list
);
471 _kiwi
.app
.panels().active
.addMsg(' ', styleText('client_models_application_ignore_stopped', {'%T': translateText('client_models_application_ignore_stopped', [ev
.params
[0]])}));
475 _
.each(fn_to_bind
, function(fn
, event_name
) {
476 controlbox
.on(event_name
, _
.bind(fn
, that
));
481 isChannelName: function (channel_name
) {
482 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
484 if (!channel_name
|| !channel_name
.length
) return false;
485 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
492 // A fallback action. Send a raw command to the server
493 function unknownCommand (ev
) {
494 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
495 console
.log('RAW: ' + raw_cmd
);
496 this.connections
.active_connection
.gateway
.raw(raw_cmd
);
499 function allCommands (ev
) {}
501 function joinCommand (ev
) {
502 var panels
, channel_names
;
504 channel_names
= ev
.params
.join(' ').split(',');
505 panels
= this.connections
.active_connection
.createAndJoinChannels(channel_names
);
507 // Show the last channel if we have one
509 panels
[panels
.length
- 1].view
.show();
512 function queryCommand (ev
) {
513 var destination
, message
, panel
;
515 destination
= ev
.params
[0];
518 message
= ev
.params
.join(' ');
520 // Check if we have the panel already. If not, create it
521 panel
= this.connections
.active_connection
.panels
.getByName(destination
);
523 panel
= new _kiwi
.model
.Query({name
: destination
});
524 this.connections
.active_connection
.panels
.add(panel
);
527 if (panel
) panel
.view
.show();
530 this.connections
.active_connection
.gateway
.msg(panel
.get('name'), message
);
531 panel
.addMsg(_kiwi
.app
.connections
.active_connection
.get('nick'), styleText('query', {'%T': message
}));
536 function msgCommand (ev
) {
538 destination
= ev
.params
[0],
539 panel
= this.connections
.active_connection
.panels
.getByName(destination
) || this.panels().server
;
542 message
= formatToIrcMsg(ev
.params
.join(' '));
544 panel
.addMsg(_kiwi
.app
.connections
.active_connection
.get('nick'), styleText('msg', {'%T': message
}));
545 this.connections
.active_connection
.gateway
.msg(destination
, message
);
548 function actionCommand (ev
) {
549 if (_kiwi
.app
.panels().active
.isServer()) {
553 var panel
= _kiwi
.app
.panels().active
;
554 panel
.addMsg('', styleText('action', {'%N': _kiwi
.app
.connections
.active_connection
.get('nick'), '%T': ev
.params
.join(' ')}), 'action');
555 this.connections
.active_connection
.gateway
.action(panel
.get('name'), ev
.params
.join(' '));
558 function partCommand (ev
) {
561 if (ev
.params
.length
=== 0) {
562 this.connections
.active_connection
.gateway
.part(_kiwi
.app
.panels().active
.get('name'));
564 _
.each(ev
.params
, function (channel
) {
565 that
.connections
.active_connection
.gateway
.part(channel
);
570 function nickCommand (ev
) {
571 this.connections
.active_connection
.gateway
.changeNick(ev
.params
[0]);
574 function topicCommand (ev
) {
577 if (ev
.params
.length
=== 0) return;
579 if (this.isChannelName(ev
.params
[0])) {
580 channel_name
= ev
.params
[0];
583 channel_name
= _kiwi
.app
.panels().active
.get('name');
586 this.connections
.active_connection
.gateway
.topic(channel_name
, ev
.params
.join(' '));
589 function noticeCommand (ev
) {
592 // Make sure we have a destination and some sort of message
593 if (ev
.params
.length
<= 1) return;
595 destination
= ev
.params
[0];
598 this.connections
.active_connection
.gateway
.notice(destination
, ev
.params
.join(' '));
601 function quoteCommand (ev
) {
602 var raw
= ev
.params
.join(' ');
603 this.connections
.active_connection
.gateway
.raw(raw
);
606 function kickCommand (ev
) {
607 var nick
, panel
= _kiwi
.app
.panels().active
;
609 if (!panel
.isChannel()) return;
611 // Make sure we have a nick
612 if (ev
.params
.length
=== 0) return;
617 this.connections
.active_connection
.gateway
.kick(panel
.get('name'), nick
, ev
.params
.join(' '));
620 function clearCommand (ev
) {
621 // Can't clear a server or applet panel
622 if (_kiwi
.app
.panels().active
.isServer() || _kiwi
.app
.panels().active
.isApplet()) {
626 if (_kiwi
.app
.panels().active
.clearMessages
) {
627 _kiwi
.app
.panels().active
.clearMessages();
631 function ctcpCommand(ev
) {
634 // Make sure we have a target and a ctcp type (eg. version, time)
635 if (ev
.params
.length
< 2) return;
637 target
= ev
.params
[0];
643 this.connections
.active_connection
.gateway
.ctcp(true, type
, target
, ev
.params
.join(' '));
646 function settingsCommand (ev
) {
647 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
648 settings
.view
.show();
651 function scriptCommand (ev
) {
652 var editor
= _kiwi
.model
.Applet
.loadOnce('kiwi_script_editor');
656 function appletCommand (ev
) {
657 if (!ev
.params
[0]) return;
659 var panel
= new _kiwi
.model
.Applet();
662 // Url and name given
663 panel
.load(ev
.params
[0], ev
.params
[1]);
665 // Load a pre-loaded applet
666 if (_kiwi
.applets
[ev
.params
[0]]) {
667 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
669 _kiwi
.app
.panels().server
.addMsg('', styleText('client_models_application_applet_notfound', {'%T': translateText('client_models_application_applet_notfound', [ev
.params
[0]])}));
674 _kiwi
.app
.connections
.active_connection
.panels
.add(panel
);
680 function inviteCommand (ev
) {
683 // A nick must be specified
687 // Can only invite into channels
688 if (!_kiwi
.app
.panels().active
.isChannel())
692 channel
= _kiwi
.app
.panels().active
.get('name');
694 _kiwi
.app
.connections
.active_connection
.gateway
.raw('INVITE ' + nick
+ ' ' + channel
);
696 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_has_been_invited', {'%N': nick
, '%T': translateText('client_models_application_has_been_invited', [channel
])}), 'action');
700 function whoisCommand (ev
) {
705 } else if (_kiwi
.app
.panels().active
.isQuery()) {
706 nick
= _kiwi
.app
.panels().active
.get('name');
710 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOIS ' + nick
+ ' ' + nick
);
714 function whowasCommand (ev
) {
719 } else if (_kiwi
.app
.panels().active
.isQuery()) {
720 nick
= _kiwi
.app
.panels().active
.get('name');
724 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOWAS ' + nick
);
727 function encodingCommand (ev
) {
729 _kiwi
.gateway
.setEncoding(null, ev
.params
[0], function (success
) {
731 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_encoding_changed', {'%T': translateText('client_models_application_encoding_changed', [ev
.params
[0]])}));
733 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_encoding_invalid', {'%T': translateText('client_models_application_encoding_invalid', [ev
.params
[0]])}));
737 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_encoding_notspecified', {'%T': translateText('client_models_application_encoding_notspecified')}));
738 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_encoding_usage', {'%T': translateText('client_models_application_encoding_usage')}));
742 function channelCommand (ev
) {
743 var active_panel
= _kiwi
.app
.panels().active
;
745 if (!active_panel
.isChannel())
748 new _kiwi
.model
.ChannelInfo({channel
: _kiwi
.app
.panels().active
});
751 function serverCommand (ev
) {
752 var server
, port
, ssl
, password
, nick
,
755 // If no server address given, show the new connection dialog
757 tmp
= new _kiwi
.view
.MenuBox(_kiwi
.global
.i18n
.translate('client_models_application_connection_create').fetch());
758 tmp
.addItem('new_connection', new _kiwi
.model
.NewConnection().view
.$el
);
761 // Center screen the dialog
763 top
: (this.view
.$el
.height() / 2) - (tmp
.$el
.height() / 2),
764 left
: (this.view
.$el
.width() / 2) - (tmp
.$el
.width() / 2)
770 // Port given in 'host:port' format and no specific port given after a space
771 if (ev
.params
[0].indexOf(':') > 0) {
772 tmp
= ev
.params
[0].split(':');
776 password
= ev
.params
[1] || undefined;
779 // Server + port given as 'host port'
780 server
= ev
.params
[0];
781 port
= ev
.params
[1] || 6667;
783 password
= ev
.params
[2] || undefined;
786 // + in the port means SSL
787 if (port
.toString()[0] === '+') {
789 port
= parseInt(port
.substring(1), 10);
794 // Default port if one wasn't found
797 // Use the same nick as we currently have
798 nick
= _kiwi
.app
.connections
.active_connection
.get('nick');
800 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_connection_connecting', {'%T': translateText('client_models_application_connection_connecting', [server
, port
.toString()])}));
802 _kiwi
.gateway
.newConnection({
808 }, function(err
, new_connection
) {
810 _kiwi
.app
.panels().active
.addMsg('', styleText('client_models_application_connection_error', {'%T': translateText('client_models_application_connection_error', [server
, port
.toString(), err
.toString()])}));