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();
52 this.view
.barsHide(true);
54 this.showIntialConenctionDialog();
58 this.detectKiwiServer = function () {
59 // If running from file, default to localhost:7777 by default
60 if (window
.location
.protocol
=== 'file:') {
61 this.kiwi_server
= 'http://localhost:7778';
63 // Assume the kiwi server is on the same server
64 this.kiwi_server
= window
.location
.protocol
+ '//' + window
.location
.host
;
69 this.showIntialConenctionDialog = function() {
70 var connection_dialog
= new _kiwi
.model
.NewConnection();
71 this.populateDefaultServerSettings(connection_dialog
);
73 connection_dialog
.view
.$el
.addClass('initial');
74 this.view
.$el
.find('.panel_container:first').append(connection_dialog
.view
.$el
);
76 var $info
= $($('#tmpl_new_connection_info').html().trim());
79 connection_dialog
.view
.infoBoxSet($info
);
80 connection_dialog
.view
.infoBoxShow();
83 // TODO: Shouldn't really be here but it's not working in the view.. :/
84 // Hack for firefox browers: Focus is not given on this event loop iteration
85 setTimeout(function(){
86 connection_dialog
.view
.$el
.find('.nick').select();
89 // Once connected, close this dialog and remove its own event
91 connection_dialog
.view
.$el
.slideUp(function() {
92 connection_dialog
.view
.dispose();
93 connection_dialog
= null;
95 _kiwi
.gateway
.off('onconnect', fn
);
99 _kiwi
.gateway
.on('onconnect', fn
);
103 this.initializeClient = function () {
104 this.view
= new _kiwi
.view
.Application({model
: this, el
: this.get('container')});
106 // Applets panel list
107 this.applet_panels
= new _kiwi
.model
.PanelList();
108 this.applet_panels
.view
.$el
.addClass('panellist applets');
109 this.view
.$el
.find('.tabs').append(this.applet_panels
.view
.$el
);
112 * Set the UI components up
114 this.controlbox
= new _kiwi
.view
.ControlBox({el
: $('#kiwi .controlbox')[0]});
115 this.bindControllboxCommands(this.controlbox
);
117 this.topicbar
= new _kiwi
.view
.TopicBar({el
: this.view
.$el
.find('.topic')[0]});
119 new _kiwi
.view
.AppToolbar({el
: _kiwi
.app
.view
.$el
.find('.toolbar .app_tools')[0]});
121 this.message
= new _kiwi
.view
.StatusMessage({el
: this.view
.$el
.find('.status_message')[0]});
123 this.resize_handle
= new _kiwi
.view
.ResizeHandler({el
: this.view
.$el
.find('.memberlists_resize_handle')[0]});
125 // Rejigg the UI sizes
126 this.view
.doLayout();
130 this.initializeGlobals = function () {
131 _kiwi
.global
.connections
= this.connections
;
133 _kiwi
.global
.panels
= this.panels
;
134 _kiwi
.global
.panels
.applets
= this.applet_panels
;
136 _kiwi
.global
.components
.Applet
= _kiwi
.model
.Applet
;
137 _kiwi
.global
.components
.Panel
=_kiwi
.model
.Panel
;
141 this.populateDefaultServerSettings = function (new_connection_dialog
) {
155 * Get any settings set by the server
156 * These settings may be changed in the server selection dialog or via URL parameters
158 if (this.server_settings
.client
) {
159 if (this.server_settings
.client
.nick
)
160 defaults
.nick
= this.server_settings
.client
.nick
;
162 if (this.server_settings
.client
.server
)
163 defaults
.server
= this.server_settings
.client
.server
;
165 if (this.server_settings
.client
.port
)
166 defaults
.port
= this.server_settings
.client
.port
;
168 if (this.server_settings
.client
.ssl
)
169 defaults
.ssl
= this.server_settings
.client
.ssl
;
171 if (this.server_settings
.client
.channel
)
172 defaults
.channel
= this.server_settings
.client
.channel
;
178 * Get any settings passed in the URL
179 * These settings may be changed in the server selection dialog
182 // Any query parameters first
183 if (getQueryVariable('nick'))
184 defaults
.nick
= getQueryVariable('nick');
186 if (window
.location
.hash
)
187 defaults
.channel
= window
.location
.hash
;
190 // Process the URL part by part, extracting as we go
191 parts
= window
.location
.pathname
.toString().replace(this.get('base_path'), '').split('/');
193 if (parts
.length
> 0) {
196 if (parts
.length
> 0 && parts
[0]) {
197 // 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.
198 uricheck
= parts
[0].substr(0, 7).toLowerCase();
199 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
200 parts
[0] = decodeURIComponent(parts
[0]);
201 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
202 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
204 uricheck[1] = ssl (optional)
206 uricheck[3] = port (optional)
207 uricheck[4] = channel (optional)
208 uricheck[5] = channel key (optional, channel must also be set)
211 if (typeof uricheck
[1] !== 'undefined') {
213 if (defaults
.port
=== 6667) {
214 defaults
.port
= 6697;
217 defaults
.server
= uricheck
[2];
218 if (typeof uricheck
[3] !== 'undefined') {
219 defaults
.port
= uricheck
[3];
221 if (typeof uricheck
[4] !== 'undefined') {
222 defaults
.channel
= '#' + uricheck
[4];
223 if (typeof uricheck
[5] !== 'undefined') {
224 defaults
.channel_key
= uricheck
[5];
230 // Extract the port+ssl if we find one
231 if (parts
[0].search(/:/) > 0) {
232 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
233 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
234 if (defaults
.port
[0] === '+') {
235 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
238 defaults
.ssl
= false;
242 defaults
.server
= parts
[0];
249 if (parts
.length
> 0 && parts
[0]) {
250 defaults
.channel
= '#' + parts
[0];
255 // If any settings have been given by the server.. override any auto detected settings
257 * Get any server restrictions as set in the server config
258 * These settings can not be changed in the server selection dialog
260 if (this.server_settings
&& this.server_settings
.connection
) {
261 if (this.server_settings
.connection
.server
) {
262 defaults
.server
= this.server_settings
.connection
.server
;
265 if (this.server_settings
.connection
.port
) {
266 defaults
.port
= this.server_settings
.connection
.port
;
269 if (this.server_settings
.connection
.ssl
) {
270 defaults
.ssl
= this.server_settings
.connection
.ssl
;
273 if (this.server_settings
.connection
.channel
) {
274 defaults
.channel
= this.server_settings
.connection
.channel
;
277 if (this.server_settings
.connection
.nick
) {
278 defaults
.nick
= this.server_settings
.connection
.nick
;
282 // Set any random numbers if needed
283 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
285 // Populate the server select box with defaults
286 new_connection_dialog
.view
.populateFields(defaults
);
290 this.panels
= (function() {
291 var fn = function(panel_type
) {
294 // Default panel type
295 panel_type
= panel_type
|| 'connections';
297 switch (panel_type
) {
299 panels
= this.connections
.panels();
302 panels
= this.applet_panels
.models
;
306 // Active panels / server
307 panels
.active
= this.connections
.active_panel
;
308 panels
.server
= this.connections
.active_connection
?
309 this.connections
.active_connection
.panels
.server
:
315 _
.extend(fn
, Backbone
.Events
);
321 this.bindGatewayCommands = function (gw
) {
322 gw
.on('onconnect', function (event
) {
323 that
.view
.barsShow();
328 * Handle the reconnections to the kiwi server
333 gw
.on('disconnect', function (event
) {
334 var msg
= 'You have been disconnected. Attempting to reconnect for you..';
335 that
.message
.text(msg
, {timeout
: 10000});
337 that
.view
.$el
.removeClass('connected');
339 // Mention the disconnection on every channel
340 _kiwi
.app
.connections
.forEach(function(connection
) {
341 connection
.panels
.server
.addMsg('', msg
, 'action quit');
343 connection
.panels
.forEach(function(panel
) {
344 if (!panel
.isChannel())
347 panel
.addMsg('', msg
, 'action quit');
355 gw
.on('reconnecting', function (event
) {
356 var msg
= 'You have been disconnected. Attempting to reconnect again in ' + (event
.delay
/1000) + ' seconds..';
358 // Only need to mention the repeating re-connection messages on server panels
359 _kiwi
.app
.connections
.forEach(function(connection
) {
360 connection
.panels
.server
.addMsg('', msg
, 'action quit');
365 gw
.on('onconnect', function (event
) {
366 that
.view
.$el
.addClass('connected');
367 if (gw_stat
!== 1) return;
369 var msg
= 'It\'s OK, you\'re connected again :)';
370 that
.message
.text(msg
, {timeout
: 5000});
372 // Mention the disconnection on every channel
373 _kiwi
.app
.connections
.forEach(function(connection
) {
374 connection
.panels
.server
.addMsg('', msg
, 'action join');
376 connection
.panels
.forEach(function(panel
) {
377 if (!panel
.isChannel())
380 panel
.addMsg('', msg
, 'action join');
393 * Bind to certain commands that may be typed into the control box
395 this.bindControllboxCommands = function (controlbox
) {
397 $.extend(controlbox
.preprocessor
.aliases
, {
400 '/me': '/action $1+',
404 // Op related aliases
405 '/op': '/quote mode $channel +o $1+',
406 '/deop': '/quote mode $channel -o $1+',
407 '/hop': '/quote mode $channel +h $1+',
408 '/dehop': '/quote mode $channel -h $1+',
409 '/voice': '/quote mode $channel +v $1+',
410 '/devoice': '/quote mode $channel -v $1+',
411 '/k': '/kick $channel $1+',
414 '/slap': '/me slaps $1 around a bit with a large trout'
417 controlbox
.on('unknown_command', unknownCommand
);
419 controlbox
.on('command', allCommands
);
420 controlbox
.on('command:msg', msgCommand
);
422 controlbox
.on('command:action', actionCommand
);
424 controlbox
.on('command:join', joinCommand
);
426 controlbox
.on('command:part', partCommand
);
428 controlbox
.on('command:nick', function (ev
) {
429 _kiwi
.gateway
.changeNick(null, ev
.params
[0]);
432 controlbox
.on('command:query', queryCommand
);
434 controlbox
.on('command:topic', topicCommand
);
436 controlbox
.on('command:notice', noticeCommand
);
438 controlbox
.on('command:quote', quoteCommand
);
440 controlbox
.on('command:kick', kickCommand
);
442 controlbox
.on('command:clear', clearCommand
);
444 controlbox
.on('command:ctcp', ctcpCommand
);
446 controlbox
.on('command:server', serverCommand
);
449 controlbox
.on('command:css', function (ev
) {
450 var queryString
= '?reload=' + new Date().getTime();
451 $('link[rel="stylesheet"]').each(function () {
452 this.href
= this.href
.replace(/\?.*|$/, queryString
);
456 controlbox
.on('command:js', function (ev
) {
457 if (!ev
.params
[0]) return;
458 $script(ev
.params
[0] + '?' + (new Date().getTime()));
462 controlbox
.on('command:set', function (ev
) {
463 if (!ev
.params
[0]) return;
465 var setting
= ev
.params
[0],
468 // Do we have a second param to set a value?
472 value
= ev
.params
.join(' ');
474 // If we're setting a true boolean value..
475 if (value
=== 'true')
478 // If we're setting a false boolean value..
479 if (value
=== 'false')
482 // If we're setting a number..
483 if (parseInt(value
, 10).toString() === value
)
484 value
= parseInt(value
, 10);
486 _kiwi
.global
.settings
.set(setting
, value
);
489 // Read the value to the user
490 _kiwi
.app
.panels().active
.addMsg('', setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
).toString());
494 controlbox
.on('command:save', function (ev
) {
495 _kiwi
.global
.settings
.save();
496 _kiwi
.app
.panels().active
.addMsg('', 'Settings have been saved');
500 controlbox
.on('command:alias', function (ev
) {
503 // No parameters passed so list them
505 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
506 _kiwi
.app
.panels().server
.addMsg(' ', name
+ ' => ' + rule
);
511 // Deleting an alias?
512 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
514 if (name
[0] !== '/') name
= '/' + name
;
515 delete controlbox
.preprocessor
.aliases
[name
];
522 rule
= ev
.params
.join(' ');
524 // Make sure the name starts with a slash
525 if (name
[0] !== '/') name
= '/' + name
;
527 // Now actually add the alias
528 controlbox
.preprocessor
.aliases
[name
] = rule
;
532 controlbox
.on('command:ignore', function (ev
) {
533 var list
= _kiwi
.gateway
.get('ignore_list');
535 // No parameters passed so list them
537 if (list
.length
> 0) {
538 _kiwi
.app
.panels().active
.addMsg(' ', 'Ignored nicks:');
539 $.each(list
, function (idx
, ignored_pattern
) {
540 _kiwi
.app
.panels().active
.addMsg(' ', ignored_pattern
);
543 _kiwi
.app
.panels().active
.addMsg(' ', 'Not ignoring anybody');
548 // We have a parameter, so add it
549 list
.push(ev
.params
[0]);
550 _kiwi
.gateway
.set('ignore_list', list
);
551 _kiwi
.app
.panels().active
.addMsg(' ', 'Ignoring ' + ev
.params
[0]);
555 controlbox
.on('command:unignore', function (ev
) {
556 var list
= _kiwi
.gateway
.get('ignore_list');
559 _kiwi
.app
.panels().active
.addMsg(' ', 'Specifiy which nick you wish to stop ignoring');
563 list
= _
.reject(list
, function(pattern
) {
564 return pattern
=== ev
.params
[0];
567 _kiwi
.gateway
.set('ignore_list', list
);
569 _kiwi
.app
.panels().active
.addMsg(' ', 'Stopped ignoring ' + ev
.params
[0]);
573 controlbox
.on('command:applet', appletCommand
);
574 controlbox
.on('command:settings', settingsCommand
);
575 controlbox
.on('command:script', scriptCommand
);
578 // A fallback action. Send a raw command to the server
579 function unknownCommand (ev
) {
580 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
581 console
.log('RAW: ' + raw_cmd
);
582 _kiwi
.gateway
.raw(null, raw_cmd
);
585 function allCommands (ev
) {}
587 function joinCommand (ev
) {
588 var panels
, channel_names
;
590 channel_names
= ev
.params
.join(' ').split(',');
591 panels
= that
.connections
.active_connection
.createAndJoinChannels(channel_names
);
593 // Show the last channel if we have one
595 panels
[panels
.length
- 1].view
.show();
598 function queryCommand (ev
) {
599 var destination
, panel
;
601 destination
= ev
.params
[0];
603 // Check if we have the panel already. If not, create it
604 panel
= that
.connections
.active_connection
.panels
.getByName(destination
);
606 panel
= new _kiwi
.model
.Query({name
: destination
});
607 that
.connections
.active_connection
.panels
.add(panel
);
610 if (panel
) panel
.view
.show();
614 function msgCommand (ev
) {
615 var destination
= ev
.params
[0],
616 panel
= that
.connections
.active_connection
.panels
.getByName(destination
) || that
.panels().server
;
620 panel
.addMsg(_kiwi
.app
.connections
.active_connection
.get('nick'), ev
.params
.join(' '));
621 _kiwi
.gateway
.privmsg(null, destination
, ev
.params
.join(' '));
624 function actionCommand (ev
) {
625 if (_kiwi
.app
.panels().active
.isServer()) {
629 var panel
= _kiwi
.app
.panels().active
;
630 panel
.addMsg('', '* ' + _kiwi
.app
.connections
.active_connection
.get('nick') + ' ' + ev
.params
.join(' '), 'action');
631 _kiwi
.gateway
.action(null, panel
.get('name'), ev
.params
.join(' '));
634 function partCommand (ev
) {
635 if (ev
.params
.length
=== 0) {
636 _kiwi
.gateway
.part(null, _kiwi
.app
.panels().active
.get('name'));
638 _
.each(ev
.params
, function (channel
) {
639 _kiwi
.gateway
.part(null, channel
);
644 function topicCommand (ev
) {
647 if (ev
.params
.length
=== 0) return;
649 if (that
.isChannelName(ev
.params
[0])) {
650 channel_name
= ev
.params
[0];
653 channel_name
= _kiwi
.app
.panels().active
.get('name');
656 _kiwi
.gateway
.topic(null, channel_name
, ev
.params
.join(' '));
659 function noticeCommand (ev
) {
662 // Make sure we have a destination and some sort of message
663 if (ev
.params
.length
<= 1) return;
665 destination
= ev
.params
[0];
668 _kiwi
.gateway
.notice(null, destination
, ev
.params
.join(' '));
671 function quoteCommand (ev
) {
672 var raw
= ev
.params
.join(' ');
673 _kiwi
.gateway
.raw(null, raw
);
676 function kickCommand (ev
) {
677 var nick
, panel
= _kiwi
.app
.panels().active
;
679 if (!panel
.isChannel()) return;
681 // Make sure we have a nick
682 if (ev
.params
.length
=== 0) return;
687 _kiwi
.gateway
.kick(null, panel
.get('name'), nick
, ev
.params
.join(' '));
690 function clearCommand (ev
) {
691 // Can't clear a server or applet panel
692 if (_kiwi
.app
.panels().active
.isServer() || _kiwi
.app
.panels().active
.isApplet()) {
696 if (_kiwi
.app
.panels().active
.clearMessages
) {
697 _kiwi
.app
.panels().active
.clearMessages();
701 function ctcpCommand(ev
) {
704 // Make sure we have a target and a ctcp type (eg. version, time)
705 if (ev
.params
.length
< 2) return;
707 target
= ev
.params
[0];
713 _kiwi
.gateway
.ctcp(null, true, type
, target
, ev
.params
.join(' '));
716 function settingsCommand (ev
) {
717 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
718 settings
.view
.show();
721 function scriptCommand (ev
) {
722 var editor
= _kiwi
.model
.Applet
.loadOnce('kiwi_script_editor');
726 function appletCommand (ev
) {
727 if (!ev
.params
[0]) return;
729 var panel
= new _kiwi
.model
.Applet();
732 // Url and name given
733 panel
.load(ev
.params
[0], ev
.params
[1]);
735 // Load a pre-loaded applet
736 if (_kiwi
.applets
[ev
.params
[0]]) {
737 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
739 _kiwi
.app
.panels().server
.addMsg('', 'Applet "' + ev
.params
[0] + '" does not exist');
744 _kiwi
.app
.connections
.active_connection
.panels
.add(panel
);
749 function serverCommand (ev
) {
750 var server
, port
, ssl
, password
, nick
,
753 // If no server address given, show the new connection dialog
755 tmp
= new _kiwi
.view
.MenuBox('New Connection');
756 tmp
.addItem('new_connection', new _kiwi
.model
.NewConnection().view
.$el
);
759 // Center screen the dialog
761 top
: (that
.view
.$el
.height() / 2) - (tmp
.$el
.height() / 2),
762 left
: (that
.view
.$el
.width() / 2) - (tmp
.$el
.width() / 2)
768 // Port given in 'host:port' format and no specific port given after a space
769 if (ev
.params
[0].indexOf(':') > 0) {
770 tmp
= ev
.params
[0].split(':');
774 password
= ev
.params
[1] || undefined;
777 // Server + port given as 'host port'
778 server
= ev
.params
[0];
779 port
= ev
.params
[1] || 6667;
781 password
= ev
.params
[2] || undefined;
784 // + in the port means SSL
785 if (port
.toString()[0] === '+') {
787 port
= parseInt(port
.substring(1), 10);
792 // Default port if one wasn't found
795 // Use the same nick as we currently have
796 nick
= _kiwi
.app
.connections
.active_connection
.get('nick');
798 _kiwi
.app
.panels().active
.addMsg('', 'Connecting to ' + server
+ ':' + port
.toString() + '..');
800 _kiwi
.gateway
.newConnection({
806 }, function(err
, new_connection
) {
808 _kiwi
.app
.panels().active
.addMsg('', 'Error connecting to ' + server
+ ':' + port
.toString() + ' (' + err
.toString() + ')');
816 this.isChannelName = function (channel_name
) {
817 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
819 if (!channel_name
|| !channel_name
.length
) return false;
820 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
827 model
= Backbone
.Model
.extend(new model());
829 return new model(arguments
);