60d7ee366bf584ee916f65e75343dda91cb24753
1 _kiwi
.model
.Application = function () {
2 // Set to a reference to this object within initialize()
6 var model = function () {
7 /** _kiwi.view.Application */
10 /** _kiwi.view.StatusMessage */
13 /* Address for the kiwi server */
14 this.kiwi_server
= null;
16 this.initialize = function (options
) {
19 if (options
[0].container
) {
20 this.set('container', options
[0].container
);
23 // The base url to the kiwi server
24 this.set('base_path', options
[0].base_path
? options
[0].base_path
: '/kiwi');
26 // Any options sent down from the server
27 this.server_settings
= options
[0].server_settings
|| {};
29 // Best guess at where the kiwi server is
30 this.detectKiwiServer();
32 // Takes instances of model_network
33 this.connections
= new _kiwi
.model
.NetworkPanelList();
37 this.start = function () {
38 // Only debug if set in the querystring
39 if (!getQueryVariable('debug')) {
46 _kiwi
.gateway
= new _kiwi
.model
.Gateway();
47 this.bindGatewayCommands(_kiwi
.gateway
);
49 this.initializeClient();
50 this.initializeGlobals();
51 this.applyDefaultClientSettings(this.server_settings
.client
.settings
);
53 this.view
.barsHide(true);
55 this.showIntialConenctionDialog();
59 this.detectKiwiServer = function () {
60 // If running from file, default to localhost:7777 by default
61 if (window
.location
.protocol
=== 'file:') {
62 this.kiwi_server
= 'http://localhost:7778';
64 // Assume the kiwi server is on the same server
65 this.kiwi_server
= window
.location
.protocol
+ '//' + window
.location
.host
;
70 this.showIntialConenctionDialog = function() {
71 var connection_dialog
= new _kiwi
.model
.NewConnection();
72 this.populateDefaultServerSettings(connection_dialog
);
74 connection_dialog
.view
.$el
.addClass('initial');
75 this.view
.$el
.find('.panel_container:first').append(connection_dialog
.view
.$el
);
77 var $info
= $($('#tmpl_new_connection_info').html().trim());
80 connection_dialog
.view
.infoBoxSet($info
);
81 connection_dialog
.view
.infoBoxShow();
84 // TODO: Shouldn't really be here but it's not working in the view.. :/
85 // Hack for firefox browers: Focus is not given on this event loop iteration
86 setTimeout(function(){
87 connection_dialog
.view
.$el
.find('.nick').select();
90 // Once connected, close this dialog and remove its own event
92 connection_dialog
.view
.$el
.slideUp(function() {
93 connection_dialog
.view
.dispose();
94 connection_dialog
= null;
96 _kiwi
.gateway
.off('onconnect', fn
);
100 _kiwi
.gateway
.on('onconnect', fn
);
104 this.initializeClient = function () {
105 this.view
= new _kiwi
.view
.Application({model
: this, el
: this.get('container')});
107 // Applets panel list
108 this.applet_panels
= new _kiwi
.model
.PanelList();
109 this.applet_panels
.view
.$el
.addClass('panellist applets');
110 this.view
.$el
.find('.tabs').append(this.applet_panels
.view
.$el
);
113 * Set the UI components up
115 this.controlbox
= new _kiwi
.view
.ControlBox({el
: $('#kiwi .controlbox')[0]});
116 this.bindControllboxCommands(this.controlbox
);
118 this.topicbar
= new _kiwi
.view
.TopicBar({el
: this.view
.$el
.find('.topic')[0]});
120 new _kiwi
.view
.AppToolbar({el
: _kiwi
.app
.view
.$el
.find('.toolbar .app_tools')[0]});
122 this.message
= new _kiwi
.view
.StatusMessage({el
: this.view
.$el
.find('.status_message')[0]});
124 this.resize_handle
= new _kiwi
.view
.ResizeHandler({el
: this.view
.$el
.find('.memberlists_resize_handle')[0]});
126 // Rejigg the UI sizes
127 this.view
.doLayout();
131 this.initializeGlobals = function () {
132 _kiwi
.global
.connections
= this.connections
;
134 _kiwi
.global
.panels
= this.panels
;
135 _kiwi
.global
.panels
.applets
= this.applet_panels
;
137 _kiwi
.global
.components
.Applet
= _kiwi
.model
.Applet
;
138 _kiwi
.global
.components
.Panel
=_kiwi
.model
.Panel
;
142 this.applyDefaultClientSettings = function (settings
) {
143 _
.each(settings
, function (value
, setting
) {
144 if (typeof _kiwi
.global
.settings
.get(setting
) === 'undefined') {
145 _kiwi
.global
.settings
.set(setting
, value
);
151 this.populateDefaultServerSettings = function (new_connection_dialog
) {
165 * Get any settings set by the server
166 * These settings may be changed in the server selection dialog or via URL parameters
168 if (this.server_settings
.client
) {
169 if (this.server_settings
.client
.nick
)
170 defaults
.nick
= this.server_settings
.client
.nick
;
172 if (this.server_settings
.client
.server
)
173 defaults
.server
= this.server_settings
.client
.server
;
175 if (this.server_settings
.client
.port
)
176 defaults
.port
= this.server_settings
.client
.port
;
178 if (this.server_settings
.client
.ssl
)
179 defaults
.ssl
= this.server_settings
.client
.ssl
;
181 if (this.server_settings
.client
.channel
)
182 defaults
.channel
= this.server_settings
.client
.channel
;
184 if (this.server_settings
.client
.channel_key
)
185 defaults
.channel_key
= this.server_settings
.client
.channel_key
;
191 * Get any settings passed in the URL
192 * These settings may be changed in the server selection dialog
195 // Any query parameters first
196 if (getQueryVariable('nick'))
197 defaults
.nick
= getQueryVariable('nick');
199 if (window
.location
.hash
)
200 defaults
.channel
= window
.location
.hash
;
203 // Process the URL part by part, extracting as we go
204 parts
= window
.location
.pathname
.toString().replace(this.get('base_path'), '').split('/');
206 if (parts
.length
> 0) {
209 if (parts
.length
> 0 && parts
[0]) {
210 // 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.
211 uricheck
= parts
[0].substr(0, 7).toLowerCase();
212 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
213 parts
[0] = decodeURIComponent(parts
[0]);
214 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
215 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
217 uricheck[1] = ssl (optional)
219 uricheck[3] = port (optional)
220 uricheck[4] = channel (optional)
221 uricheck[5] = channel key (optional, channel must also be set)
224 if (typeof uricheck
[1] !== 'undefined') {
226 if (defaults
.port
=== 6667) {
227 defaults
.port
= 6697;
230 defaults
.server
= uricheck
[2];
231 if (typeof uricheck
[3] !== 'undefined') {
232 defaults
.port
= uricheck
[3];
234 if (typeof uricheck
[4] !== 'undefined') {
235 defaults
.channel
= '#' + uricheck
[4];
236 if (typeof uricheck
[5] !== 'undefined') {
237 defaults
.channel_key
= uricheck
[5];
243 // Extract the port+ssl if we find one
244 if (parts
[0].search(/:/) > 0) {
245 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
246 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
247 if (defaults
.port
[0] === '+') {
248 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
251 defaults
.ssl
= false;
255 defaults
.server
= parts
[0];
262 if (parts
.length
> 0 && parts
[0]) {
263 defaults
.channel
= '#' + parts
[0];
268 // If any settings have been given by the server.. override any auto detected settings
270 * Get any server restrictions as set in the server config
271 * These settings can not be changed in the server selection dialog
273 if (this.server_settings
&& this.server_settings
.connection
) {
274 if (this.server_settings
.connection
.server
) {
275 defaults
.server
= this.server_settings
.connection
.server
;
278 if (this.server_settings
.connection
.port
) {
279 defaults
.port
= this.server_settings
.connection
.port
;
282 if (this.server_settings
.connection
.ssl
) {
283 defaults
.ssl
= this.server_settings
.connection
.ssl
;
286 if (this.server_settings
.connection
.channel
) {
287 defaults
.channel
= this.server_settings
.connection
.channel
;
290 if (this.server_settings
.connection
.channel_key
) {
291 defaults
.channel_key
= this.server_settings
.connection
.channel_key
;
294 if (this.server_settings
.connection
.nick
) {
295 defaults
.nick
= this.server_settings
.connection
.nick
;
299 // Set any random numbers if needed
300 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
302 // Populate the server select box with defaults
303 new_connection_dialog
.view
.populateFields(defaults
);
307 this.panels
= (function() {
308 var fn = function(panel_type
) {
311 // Default panel type
312 panel_type
= panel_type
|| 'connections';
314 switch (panel_type
) {
316 panels
= this.connections
.panels();
319 panels
= this.applet_panels
.models
;
323 // Active panels / server
324 panels
.active
= this.connections
.active_panel
;
325 panels
.server
= this.connections
.active_connection
?
326 this.connections
.active_connection
.panels
.server
:
332 _
.extend(fn
, Backbone
.Events
);
338 this.bindGatewayCommands = function (gw
) {
339 gw
.on('onconnect', function (event
) {
340 that
.view
.barsShow();
345 * Handle the reconnections to the kiwi server
350 gw
.on('disconnect', function (event
) {
351 var msg
= 'You have been disconnected. Attempting to reconnect for you..';
352 that
.message
.text(msg
, {timeout
: 10000});
354 that
.view
.$el
.removeClass('connected');
356 // Mention the disconnection on every channel
357 _kiwi
.app
.connections
.forEach(function(connection
) {
358 connection
.panels
.server
.addMsg('', msg
, 'action quit');
360 connection
.panels
.forEach(function(panel
) {
361 if (!panel
.isChannel())
364 panel
.addMsg('', msg
, 'action quit');
372 gw
.on('reconnecting', function (event
) {
373 var msg
= 'You have been disconnected. Attempting to reconnect again in ' + (event
.delay
/1000) + ' seconds..';
375 // Only need to mention the repeating re-connection messages on server panels
376 _kiwi
.app
.connections
.forEach(function(connection
) {
377 connection
.panels
.server
.addMsg('', msg
, 'action quit');
382 gw
.on('onconnect', function (event
) {
383 that
.view
.$el
.addClass('connected');
384 if (gw_stat
!== 1) return;
386 var msg
= 'It\'s OK, you\'re connected again :)';
387 that
.message
.text(msg
, {timeout
: 5000});
389 // Mention the disconnection on every channel
390 _kiwi
.app
.connections
.forEach(function(connection
) {
391 connection
.panels
.server
.addMsg('', msg
, 'action join');
393 connection
.panels
.forEach(function(panel
) {
394 if (!panel
.isChannel())
397 panel
.addMsg('', msg
, 'action join');
410 * Bind to certain commands that may be typed into the control box
412 this.bindControllboxCommands = function (controlbox
) {
414 $.extend(controlbox
.preprocessor
.aliases
, {
417 '/me': '/action $1+',
421 '/raw': '/quote $1+',
423 // Op related aliases
424 '/op': '/quote mode $channel +o $1+',
425 '/deop': '/quote mode $channel -o $1+',
426 '/hop': '/quote mode $channel +h $1+',
427 '/dehop': '/quote mode $channel -h $1+',
428 '/voice': '/quote mode $channel +v $1+',
429 '/devoice': '/quote mode $channel -v $1+',
430 '/k': '/kick $channel $1+',
433 '/slap': '/me slaps $1 around a bit with a large trout'
436 controlbox
.on('unknown_command', unknownCommand
);
438 controlbox
.on('command', allCommands
);
439 controlbox
.on('command:msg', msgCommand
);
441 controlbox
.on('command:action', actionCommand
);
443 controlbox
.on('command:join', joinCommand
);
445 controlbox
.on('command:part', partCommand
);
447 controlbox
.on('command:nick', function (ev
) {
448 _kiwi
.gateway
.changeNick(null, ev
.params
[0]);
451 controlbox
.on('command:query', queryCommand
);
453 controlbox
.on('command:topic', topicCommand
);
455 controlbox
.on('command:notice', noticeCommand
);
457 controlbox
.on('command:quote', quoteCommand
);
459 controlbox
.on('command:kick', kickCommand
);
461 controlbox
.on('command:clear', clearCommand
);
463 controlbox
.on('command:ctcp', ctcpCommand
);
465 controlbox
.on('command:server', serverCommand
);
467 controlbox
.on('command:whois', whoisCommand
);
469 controlbox
.on('command:whowas', whowasCommand
);
471 controlbox
.on('command:encoding', encodingCommand
);
473 controlbox
.on('command:css', function (ev
) {
474 var queryString
= '?reload=' + new Date().getTime();
475 $('link[rel="stylesheet"]').each(function () {
476 this.href
= this.href
.replace(/\?.*|$/, queryString
);
480 controlbox
.on('command:js', function (ev
) {
481 if (!ev
.params
[0]) return;
482 $script(ev
.params
[0] + '?' + (new Date().getTime()));
486 controlbox
.on('command:set', function (ev
) {
487 if (!ev
.params
[0]) return;
489 var setting
= ev
.params
[0],
492 // Do we have a second param to set a value?
496 value
= ev
.params
.join(' ');
498 // If we're setting a true boolean value..
499 if (value
=== 'true')
502 // If we're setting a false boolean value..
503 if (value
=== 'false')
506 // If we're setting a number..
507 if (parseInt(value
, 10).toString() === value
)
508 value
= parseInt(value
, 10);
510 _kiwi
.global
.settings
.set(setting
, value
);
513 // Read the value to the user
514 _kiwi
.app
.panels().active
.addMsg('', setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
).toString());
518 controlbox
.on('command:save', function (ev
) {
519 _kiwi
.global
.settings
.save();
520 _kiwi
.app
.panels().active
.addMsg('', 'Settings have been saved');
524 controlbox
.on('command:alias', function (ev
) {
527 // No parameters passed so list them
529 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
530 _kiwi
.app
.panels().server
.addMsg(' ', name
+ ' => ' + rule
);
535 // Deleting an alias?
536 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
538 if (name
[0] !== '/') name
= '/' + name
;
539 delete controlbox
.preprocessor
.aliases
[name
];
546 rule
= ev
.params
.join(' ');
548 // Make sure the name starts with a slash
549 if (name
[0] !== '/') name
= '/' + name
;
551 // Now actually add the alias
552 controlbox
.preprocessor
.aliases
[name
] = rule
;
556 controlbox
.on('command:ignore', function (ev
) {
557 var list
= _kiwi
.gateway
.get('ignore_list');
559 // No parameters passed so list them
561 if (list
.length
> 0) {
562 _kiwi
.app
.panels().active
.addMsg(' ', 'Ignored nicks:');
563 $.each(list
, function (idx
, ignored_pattern
) {
564 _kiwi
.app
.panels().active
.addMsg(' ', ignored_pattern
);
567 _kiwi
.app
.panels().active
.addMsg(' ', 'Not ignoring anybody');
572 // We have a parameter, so add it
573 list
.push(ev
.params
[0]);
574 _kiwi
.gateway
.set('ignore_list', list
);
575 _kiwi
.app
.panels().active
.addMsg(' ', 'Ignoring ' + ev
.params
[0]);
579 controlbox
.on('command:unignore', function (ev
) {
580 var list
= _kiwi
.gateway
.get('ignore_list');
583 _kiwi
.app
.panels().active
.addMsg(' ', 'Specifiy which nick you wish to stop ignoring');
587 list
= _
.reject(list
, function(pattern
) {
588 return pattern
=== ev
.params
[0];
591 _kiwi
.gateway
.set('ignore_list', list
);
593 _kiwi
.app
.panels().active
.addMsg(' ', 'Stopped ignoring ' + ev
.params
[0]);
597 controlbox
.on('command:applet', appletCommand
);
598 controlbox
.on('command:settings', settingsCommand
);
599 controlbox
.on('command:script', scriptCommand
);
602 // A fallback action. Send a raw command to the server
603 function unknownCommand (ev
) {
604 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
605 console
.log('RAW: ' + raw_cmd
);
606 _kiwi
.gateway
.raw(null, raw_cmd
);
609 function allCommands (ev
) {}
611 function joinCommand (ev
) {
612 var panels
, channel_names
;
614 channel_names
= ev
.params
.join(' ').split(',');
615 panels
= that
.connections
.active_connection
.createAndJoinChannels(channel_names
);
617 // Show the last channel if we have one
619 panels
[panels
.length
- 1].view
.show();
622 function queryCommand (ev
) {
623 var destination
, panel
;
625 destination
= ev
.params
[0];
627 // Check if we have the panel already. If not, create it
628 panel
= that
.connections
.active_connection
.panels
.getByName(destination
);
630 panel
= new _kiwi
.model
.Query({name
: destination
});
631 that
.connections
.active_connection
.panels
.add(panel
);
634 if (panel
) panel
.view
.show();
638 function msgCommand (ev
) {
639 var destination
= ev
.params
[0],
640 panel
= that
.connections
.active_connection
.panels
.getByName(destination
) || that
.panels().server
;
644 panel
.addMsg(_kiwi
.app
.connections
.active_connection
.get('nick'), ev
.params
.join(' '));
645 _kiwi
.gateway
.privmsg(null, destination
, ev
.params
.join(' '));
648 function actionCommand (ev
) {
649 if (_kiwi
.app
.panels().active
.isServer()) {
653 var panel
= _kiwi
.app
.panels().active
;
654 panel
.addMsg('', '* ' + _kiwi
.app
.connections
.active_connection
.get('nick') + ' ' + ev
.params
.join(' '), 'action');
655 _kiwi
.gateway
.action(null, panel
.get('name'), ev
.params
.join(' '));
658 function partCommand (ev
) {
659 if (ev
.params
.length
=== 0) {
660 _kiwi
.gateway
.part(null, _kiwi
.app
.panels().active
.get('name'));
662 _
.each(ev
.params
, function (channel
) {
663 _kiwi
.gateway
.part(null, channel
);
668 function topicCommand (ev
) {
671 if (ev
.params
.length
=== 0) return;
673 if (that
.isChannelName(ev
.params
[0])) {
674 channel_name
= ev
.params
[0];
677 channel_name
= _kiwi
.app
.panels().active
.get('name');
680 _kiwi
.gateway
.topic(null, channel_name
, ev
.params
.join(' '));
683 function noticeCommand (ev
) {
686 // Make sure we have a destination and some sort of message
687 if (ev
.params
.length
<= 1) return;
689 destination
= ev
.params
[0];
692 _kiwi
.gateway
.notice(null, destination
, ev
.params
.join(' '));
695 function quoteCommand (ev
) {
696 var raw
= ev
.params
.join(' ');
697 _kiwi
.gateway
.raw(null, raw
);
700 function kickCommand (ev
) {
701 var nick
, panel
= _kiwi
.app
.panels().active
;
703 if (!panel
.isChannel()) return;
705 // Make sure we have a nick
706 if (ev
.params
.length
=== 0) return;
711 _kiwi
.gateway
.kick(null, panel
.get('name'), nick
, ev
.params
.join(' '));
714 function clearCommand (ev
) {
715 // Can't clear a server or applet panel
716 if (_kiwi
.app
.panels().active
.isServer() || _kiwi
.app
.panels().active
.isApplet()) {
720 if (_kiwi
.app
.panels().active
.clearMessages
) {
721 _kiwi
.app
.panels().active
.clearMessages();
725 function ctcpCommand(ev
) {
728 // Make sure we have a target and a ctcp type (eg. version, time)
729 if (ev
.params
.length
< 2) return;
731 target
= ev
.params
[0];
737 _kiwi
.gateway
.ctcp(null, true, type
, target
, ev
.params
.join(' '));
740 function settingsCommand (ev
) {
741 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
742 settings
.view
.show();
745 function scriptCommand (ev
) {
746 var editor
= _kiwi
.model
.Applet
.loadOnce('kiwi_script_editor');
750 function appletCommand (ev
) {
751 if (!ev
.params
[0]) return;
753 var panel
= new _kiwi
.model
.Applet();
756 // Url and name given
757 panel
.load(ev
.params
[0], ev
.params
[1]);
759 // Load a pre-loaded applet
760 if (_kiwi
.applets
[ev
.params
[0]]) {
761 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
763 _kiwi
.app
.panels().server
.addMsg('', 'Applet "' + ev
.params
[0] + '" does not exist');
768 _kiwi
.app
.connections
.active_connection
.panels
.add(panel
);
773 function whoisCommand (ev
) {
778 } else if (_kiwi
.app
.panels().active
.isQuery()) {
779 nick
= _kiwi
.app
.panels().active
.get('name');
783 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOIS ' + nick
+ ' ' + nick
);
787 function whowasCommand (ev
) {
792 } else if (_kiwi
.app
.panels().active
.isQuery()) {
793 nick
= _kiwi
.app
.panels().active
.get('name');
797 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOWAS ' + nick
);
800 function encodingCommand (ev
) {
802 _kiwi
.gateway
.setEncoding(null, ev
.params
[0], function (success
) {
804 _kiwi
.app
.panels().active
.addMsg('', "Encoding modified to "+ev
.params
[0]);
806 _kiwi
.app
.panels().active
.addMsg('', ev
.params
[0]+' is not a valid encoding');
810 _kiwi
.app
.panels().active
.addMsg('', 'Encoding not specified');
811 _kiwi
.app
.panels().active
.addMsg('', 'Usage: /encoding [NEW-ENCODING]');
815 function serverCommand (ev
) {
816 var server
, port
, ssl
, password
, nick
,
819 // If no server address given, show the new connection dialog
821 tmp
= new _kiwi
.view
.MenuBox('New Connection');
822 tmp
.addItem('new_connection', new _kiwi
.model
.NewConnection().view
.$el
);
825 // Center screen the dialog
827 top
: (that
.view
.$el
.height() / 2) - (tmp
.$el
.height() / 2),
828 left
: (that
.view
.$el
.width() / 2) - (tmp
.$el
.width() / 2)
834 // Port given in 'host:port' format and no specific port given after a space
835 if (ev
.params
[0].indexOf(':') > 0) {
836 tmp
= ev
.params
[0].split(':');
840 password
= ev
.params
[1] || undefined;
843 // Server + port given as 'host port'
844 server
= ev
.params
[0];
845 port
= ev
.params
[1] || 6667;
847 password
= ev
.params
[2] || undefined;
850 // + in the port means SSL
851 if (port
.toString()[0] === '+') {
853 port
= parseInt(port
.substring(1), 10);
858 // Default port if one wasn't found
861 // Use the same nick as we currently have
862 nick
= _kiwi
.app
.connections
.active_connection
.get('nick');
864 _kiwi
.app
.panels().active
.addMsg('', 'Connecting to ' + server
+ ':' + port
.toString() + '..');
866 _kiwi
.gateway
.newConnection({
872 }, function(err
, new_connection
) {
874 _kiwi
.app
.panels().active
.addMsg('', 'Error connecting to ' + server
+ ':' + port
.toString() + ' (' + err
.toString() + ')');
882 this.isChannelName = function (channel_name
) {
883 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
885 if (!channel_name
|| !channel_name
.length
) return false;
886 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
893 model
= Backbone
.Model
.extend(new model());
895 return new model(arguments
);