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();
35 // Set any default settings before anything else is applied
36 if (this.server_settings
&& this.server_settings
.client
&& this.server_settings
.client
.settings
) {
37 this.applyDefaultClientSettings(this.server_settings
.client
.settings
);
42 this.start = function () {
43 // Only debug if set in the querystring
44 if (!getQueryVariable('debug')) {
51 _kiwi
.gateway
= new _kiwi
.model
.Gateway();
52 this.bindGatewayCommands(_kiwi
.gateway
);
54 this.initializeClient();
55 this.initializeGlobals();
57 this.view
.barsHide(true);
59 this.showIntialConenctionDialog();
63 this.detectKiwiServer = function () {
64 // If running from file, default to localhost:7777 by default
65 if (window
.location
.protocol
=== 'file:') {
66 this.kiwi_server
= 'http://localhost:7778';
68 // Assume the kiwi server is on the same server
69 this.kiwi_server
= window
.location
.protocol
+ '//' + window
.location
.host
;
74 this.showIntialConenctionDialog = function() {
75 var connection_dialog
= new _kiwi
.model
.NewConnection();
76 this.populateDefaultServerSettings(connection_dialog
);
78 connection_dialog
.view
.$el
.addClass('initial');
79 this.view
.$el
.find('.panel_container:first').append(connection_dialog
.view
.$el
);
81 var $info
= $($('#tmpl_new_connection_info').html().trim());
84 connection_dialog
.view
.infoBoxSet($info
);
85 connection_dialog
.view
.infoBoxShow();
88 // TODO: Shouldn't really be here but it's not working in the view.. :/
89 // Hack for firefox browers: Focus is not given on this event loop iteration
90 setTimeout(function(){
91 connection_dialog
.view
.$el
.find('.nick').select();
94 // Once connected, close this dialog and remove its own event
96 connection_dialog
.view
.$el
.slideUp(function() {
97 connection_dialog
.view
.dispose();
98 connection_dialog
= null;
100 _kiwi
.gateway
.off('onconnect', fn
);
104 _kiwi
.gateway
.on('onconnect', fn
);
108 this.initializeClient = function () {
109 this.view
= new _kiwi
.view
.Application({model
: this, el
: this.get('container')});
111 // Applets panel list
112 this.applet_panels
= new _kiwi
.model
.PanelList();
113 this.applet_panels
.view
.$el
.addClass('panellist applets');
114 this.view
.$el
.find('.tabs').append(this.applet_panels
.view
.$el
);
117 * Set the UI components up
119 this.controlbox
= new _kiwi
.view
.ControlBox({el
: $('#kiwi .controlbox')[0]});
120 this.bindControllboxCommands(this.controlbox
);
122 this.topicbar
= new _kiwi
.view
.TopicBar({el
: this.view
.$el
.find('.topic')[0]});
124 new _kiwi
.view
.AppToolbar({el
: _kiwi
.app
.view
.$el
.find('.toolbar .app_tools')[0]});
126 this.message
= new _kiwi
.view
.StatusMessage({el
: this.view
.$el
.find('.status_message')[0]});
128 this.resize_handle
= new _kiwi
.view
.ResizeHandler({el
: this.view
.$el
.find('.memberlists_resize_handle')[0]});
130 // Rejigg the UI sizes
131 this.view
.doLayout();
135 this.initializeGlobals = function () {
136 _kiwi
.global
.connections
= this.connections
;
138 _kiwi
.global
.panels
= this.panels
;
139 _kiwi
.global
.panels
.applets
= this.applet_panels
;
141 _kiwi
.global
.components
.Applet
= _kiwi
.model
.Applet
;
142 _kiwi
.global
.components
.Panel
=_kiwi
.model
.Panel
;
146 this.applyDefaultClientSettings = function (settings
) {
147 _
.each(settings
, function (value
, setting
) {
148 if (typeof _kiwi
.global
.settings
.get(setting
) === 'undefined') {
149 _kiwi
.global
.settings
.set(setting
, value
);
155 this.populateDefaultServerSettings = function (new_connection_dialog
) {
169 * Get any settings set by the server
170 * These settings may be changed in the server selection dialog or via URL parameters
172 if (this.server_settings
.client
) {
173 if (this.server_settings
.client
.nick
)
174 defaults
.nick
= this.server_settings
.client
.nick
;
176 if (this.server_settings
.client
.server
)
177 defaults
.server
= this.server_settings
.client
.server
;
179 if (this.server_settings
.client
.port
)
180 defaults
.port
= this.server_settings
.client
.port
;
182 if (this.server_settings
.client
.ssl
)
183 defaults
.ssl
= this.server_settings
.client
.ssl
;
185 if (this.server_settings
.client
.channel
)
186 defaults
.channel
= this.server_settings
.client
.channel
;
188 if (this.server_settings
.client
.channel_key
)
189 defaults
.channel_key
= this.server_settings
.client
.channel_key
;
195 * Get any settings passed in the URL
196 * These settings may be changed in the server selection dialog
199 // Any query parameters first
200 if (getQueryVariable('nick'))
201 defaults
.nick
= getQueryVariable('nick');
203 if (window
.location
.hash
)
204 defaults
.channel
= window
.location
.hash
;
207 // Process the URL part by part, extracting as we go
208 parts
= window
.location
.pathname
.toString().replace(this.get('base_path'), '').split('/');
210 if (parts
.length
> 0) {
213 if (parts
.length
> 0 && parts
[0]) {
214 // 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.
215 uricheck
= parts
[0].substr(0, 7).toLowerCase();
216 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
217 parts
[0] = decodeURIComponent(parts
[0]);
218 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
219 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
221 uricheck[1] = ssl (optional)
223 uricheck[3] = port (optional)
224 uricheck[4] = channel (optional)
225 uricheck[5] = channel key (optional, channel must also be set)
228 if (typeof uricheck
[1] !== 'undefined') {
230 if (defaults
.port
=== 6667) {
231 defaults
.port
= 6697;
234 defaults
.server
= uricheck
[2];
235 if (typeof uricheck
[3] !== 'undefined') {
236 defaults
.port
= uricheck
[3];
238 if (typeof uricheck
[4] !== 'undefined') {
239 defaults
.channel
= '#' + uricheck
[4];
240 if (typeof uricheck
[5] !== 'undefined') {
241 defaults
.channel_key
= uricheck
[5];
247 // Extract the port+ssl if we find one
248 if (parts
[0].search(/:/) > 0) {
249 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
250 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
251 if (defaults
.port
[0] === '+') {
252 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
255 defaults
.ssl
= false;
259 defaults
.server
= parts
[0];
266 if (parts
.length
> 0 && parts
[0]) {
267 defaults
.channel
= '#' + parts
[0];
272 // If any settings have been given by the server.. override any auto detected settings
274 * Get any server restrictions as set in the server config
275 * These settings can not be changed in the server selection dialog
277 if (this.server_settings
&& this.server_settings
.connection
) {
278 if (this.server_settings
.connection
.server
) {
279 defaults
.server
= this.server_settings
.connection
.server
;
282 if (this.server_settings
.connection
.port
) {
283 defaults
.port
= this.server_settings
.connection
.port
;
286 if (this.server_settings
.connection
.ssl
) {
287 defaults
.ssl
= this.server_settings
.connection
.ssl
;
290 if (this.server_settings
.connection
.channel
) {
291 defaults
.channel
= this.server_settings
.connection
.channel
;
294 if (this.server_settings
.connection
.channel_key
) {
295 defaults
.channel_key
= this.server_settings
.connection
.channel_key
;
298 if (this.server_settings
.connection
.nick
) {
299 defaults
.nick
= this.server_settings
.connection
.nick
;
303 // Set any random numbers if needed
304 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
306 // Populate the server select box with defaults
307 new_connection_dialog
.view
.populateFields(defaults
);
311 this.panels
= (function() {
312 var fn = function(panel_type
) {
315 // Default panel type
316 panel_type
= panel_type
|| 'connections';
318 switch (panel_type
) {
320 panels
= this.connections
.panels();
323 panels
= this.applet_panels
.models
;
327 // Active panels / server
328 panels
.active
= this.connections
.active_panel
;
329 panels
.server
= this.connections
.active_connection
?
330 this.connections
.active_connection
.panels
.server
:
336 _
.extend(fn
, Backbone
.Events
);
342 this.bindGatewayCommands = function (gw
) {
343 gw
.on('onconnect', function (event
) {
344 that
.view
.barsShow();
349 * Handle the reconnections to the kiwi server
354 gw
.on('disconnect', function (event
) {
355 var msg
= 'You have been disconnected. Attempting to reconnect for you..';
356 that
.message
.text(msg
, {timeout
: 10000});
358 that
.view
.$el
.removeClass('connected');
360 // Mention the disconnection on every channel
361 _kiwi
.app
.connections
.forEach(function(connection
) {
362 connection
.panels
.server
.addMsg('', msg
, 'action quit');
364 connection
.panels
.forEach(function(panel
) {
365 if (!panel
.isChannel())
368 panel
.addMsg('', msg
, 'action quit');
376 gw
.on('reconnecting', function (event
) {
377 var msg
= 'You have been disconnected. Attempting to reconnect again in ' + (event
.delay
/1000) + ' seconds..';
379 // Only need to mention the repeating re-connection messages on server panels
380 _kiwi
.app
.connections
.forEach(function(connection
) {
381 connection
.panels
.server
.addMsg('', msg
, 'action quit');
386 gw
.on('onconnect', function (event
) {
387 that
.view
.$el
.addClass('connected');
388 if (gw_stat
!== 1) return;
390 var msg
= 'It\'s OK, you\'re connected again :)';
391 that
.message
.text(msg
, {timeout
: 5000});
393 // Mention the disconnection on every channel
394 _kiwi
.app
.connections
.forEach(function(connection
) {
395 connection
.panels
.server
.addMsg('', msg
, 'action join');
397 connection
.panels
.forEach(function(panel
) {
398 if (!panel
.isChannel())
401 panel
.addMsg('', msg
, 'action join');
414 * Bind to certain commands that may be typed into the control box
416 this.bindControllboxCommands = function (controlbox
) {
418 $.extend(controlbox
.preprocessor
.aliases
, {
421 '/me': '/action $1+',
425 '/raw': '/quote $1+',
427 // Op related aliases
428 '/op': '/quote mode $channel +o $1+',
429 '/deop': '/quote mode $channel -o $1+',
430 '/hop': '/quote mode $channel +h $1+',
431 '/dehop': '/quote mode $channel -h $1+',
432 '/voice': '/quote mode $channel +v $1+',
433 '/devoice': '/quote mode $channel -v $1+',
434 '/k': '/kick $channel $1+',
437 '/slap': '/me slaps $1 around a bit with a large trout'
440 controlbox
.on('unknown_command', unknownCommand
);
442 controlbox
.on('command', allCommands
);
443 controlbox
.on('command:msg', msgCommand
);
445 controlbox
.on('command:action', actionCommand
);
447 controlbox
.on('command:join', joinCommand
);
449 controlbox
.on('command:part', partCommand
);
451 controlbox
.on('command:nick', function (ev
) {
452 _kiwi
.gateway
.changeNick(null, ev
.params
[0]);
455 controlbox
.on('command:query', queryCommand
);
457 controlbox
.on('command:topic', topicCommand
);
459 controlbox
.on('command:notice', noticeCommand
);
461 controlbox
.on('command:quote', quoteCommand
);
463 controlbox
.on('command:kick', kickCommand
);
465 controlbox
.on('command:clear', clearCommand
);
467 controlbox
.on('command:ctcp', ctcpCommand
);
469 controlbox
.on('command:server', serverCommand
);
471 controlbox
.on('command:whois', whoisCommand
);
473 controlbox
.on('command:whowas', whowasCommand
);
475 controlbox
.on('command:encoding', encodingCommand
);
477 controlbox
.on('command:css', function (ev
) {
478 var queryString
= '?reload=' + new Date().getTime();
479 $('link[rel="stylesheet"]').each(function () {
480 this.href
= this.href
.replace(/\?.*|$/, queryString
);
484 controlbox
.on('command:js', function (ev
) {
485 if (!ev
.params
[0]) return;
486 $script(ev
.params
[0] + '?' + (new Date().getTime()));
490 controlbox
.on('command:set', function (ev
) {
491 if (!ev
.params
[0]) return;
493 var setting
= ev
.params
[0],
496 // Do we have a second param to set a value?
500 value
= ev
.params
.join(' ');
502 // If we're setting a true boolean value..
503 if (value
=== 'true')
506 // If we're setting a false boolean value..
507 if (value
=== 'false')
510 // If we're setting a number..
511 if (parseInt(value
, 10).toString() === value
)
512 value
= parseInt(value
, 10);
514 _kiwi
.global
.settings
.set(setting
, value
);
517 // Read the value to the user
518 _kiwi
.app
.panels().active
.addMsg('', setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
).toString());
522 controlbox
.on('command:save', function (ev
) {
523 _kiwi
.global
.settings
.save();
524 _kiwi
.app
.panels().active
.addMsg('', 'Settings have been saved');
528 controlbox
.on('command:alias', function (ev
) {
531 // No parameters passed so list them
533 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
534 _kiwi
.app
.panels().server
.addMsg(' ', name
+ ' => ' + rule
);
539 // Deleting an alias?
540 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
542 if (name
[0] !== '/') name
= '/' + name
;
543 delete controlbox
.preprocessor
.aliases
[name
];
550 rule
= ev
.params
.join(' ');
552 // Make sure the name starts with a slash
553 if (name
[0] !== '/') name
= '/' + name
;
555 // Now actually add the alias
556 controlbox
.preprocessor
.aliases
[name
] = rule
;
560 controlbox
.on('command:ignore', function (ev
) {
561 var list
= _kiwi
.gateway
.get('ignore_list');
563 // No parameters passed so list them
565 if (list
.length
> 0) {
566 _kiwi
.app
.panels().active
.addMsg(' ', 'Ignored nicks:');
567 $.each(list
, function (idx
, ignored_pattern
) {
568 _kiwi
.app
.panels().active
.addMsg(' ', ignored_pattern
);
571 _kiwi
.app
.panels().active
.addMsg(' ', 'Not ignoring anybody');
576 // We have a parameter, so add it
577 list
.push(ev
.params
[0]);
578 _kiwi
.gateway
.set('ignore_list', list
);
579 _kiwi
.app
.panels().active
.addMsg(' ', 'Ignoring ' + ev
.params
[0]);
583 controlbox
.on('command:unignore', function (ev
) {
584 var list
= _kiwi
.gateway
.get('ignore_list');
587 _kiwi
.app
.panels().active
.addMsg(' ', 'Specifiy which nick you wish to stop ignoring');
591 list
= _
.reject(list
, function(pattern
) {
592 return pattern
=== ev
.params
[0];
595 _kiwi
.gateway
.set('ignore_list', list
);
597 _kiwi
.app
.panels().active
.addMsg(' ', 'Stopped ignoring ' + ev
.params
[0]);
601 controlbox
.on('command:applet', appletCommand
);
602 controlbox
.on('command:settings', settingsCommand
);
603 controlbox
.on('command:script', scriptCommand
);
606 // A fallback action. Send a raw command to the server
607 function unknownCommand (ev
) {
608 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
609 console
.log('RAW: ' + raw_cmd
);
610 _kiwi
.gateway
.raw(null, raw_cmd
);
613 function allCommands (ev
) {}
615 function joinCommand (ev
) {
616 var panels
, channel_names
;
618 channel_names
= ev
.params
.join(' ').split(',');
619 panels
= that
.connections
.active_connection
.createAndJoinChannels(channel_names
);
621 // Show the last channel if we have one
623 panels
[panels
.length
- 1].view
.show();
626 function queryCommand (ev
) {
627 var destination
, panel
;
629 destination
= ev
.params
[0];
631 // Check if we have the panel already. If not, create it
632 panel
= that
.connections
.active_connection
.panels
.getByName(destination
);
634 panel
= new _kiwi
.model
.Query({name
: destination
});
635 that
.connections
.active_connection
.panels
.add(panel
);
638 if (panel
) panel
.view
.show();
642 function msgCommand (ev
) {
643 var destination
= ev
.params
[0],
644 panel
= that
.connections
.active_connection
.panels
.getByName(destination
) || that
.panels().server
;
648 panel
.addMsg(_kiwi
.app
.connections
.active_connection
.get('nick'), ev
.params
.join(' '));
649 _kiwi
.gateway
.privmsg(null, destination
, ev
.params
.join(' '));
652 function actionCommand (ev
) {
653 if (_kiwi
.app
.panels().active
.isServer()) {
657 var panel
= _kiwi
.app
.panels().active
;
658 panel
.addMsg('', '* ' + _kiwi
.app
.connections
.active_connection
.get('nick') + ' ' + ev
.params
.join(' '), 'action');
659 _kiwi
.gateway
.action(null, panel
.get('name'), ev
.params
.join(' '));
662 function partCommand (ev
) {
663 if (ev
.params
.length
=== 0) {
664 _kiwi
.gateway
.part(null, _kiwi
.app
.panels().active
.get('name'));
666 _
.each(ev
.params
, function (channel
) {
667 _kiwi
.gateway
.part(null, channel
);
672 function topicCommand (ev
) {
675 if (ev
.params
.length
=== 0) return;
677 if (that
.isChannelName(ev
.params
[0])) {
678 channel_name
= ev
.params
[0];
681 channel_name
= _kiwi
.app
.panels().active
.get('name');
684 _kiwi
.gateway
.topic(null, channel_name
, ev
.params
.join(' '));
687 function noticeCommand (ev
) {
690 // Make sure we have a destination and some sort of message
691 if (ev
.params
.length
<= 1) return;
693 destination
= ev
.params
[0];
696 _kiwi
.gateway
.notice(null, destination
, ev
.params
.join(' '));
699 function quoteCommand (ev
) {
700 var raw
= ev
.params
.join(' ');
701 _kiwi
.gateway
.raw(null, raw
);
704 function kickCommand (ev
) {
705 var nick
, panel
= _kiwi
.app
.panels().active
;
707 if (!panel
.isChannel()) return;
709 // Make sure we have a nick
710 if (ev
.params
.length
=== 0) return;
715 _kiwi
.gateway
.kick(null, panel
.get('name'), nick
, ev
.params
.join(' '));
718 function clearCommand (ev
) {
719 // Can't clear a server or applet panel
720 if (_kiwi
.app
.panels().active
.isServer() || _kiwi
.app
.panels().active
.isApplet()) {
724 if (_kiwi
.app
.panels().active
.clearMessages
) {
725 _kiwi
.app
.panels().active
.clearMessages();
729 function ctcpCommand(ev
) {
732 // Make sure we have a target and a ctcp type (eg. version, time)
733 if (ev
.params
.length
< 2) return;
735 target
= ev
.params
[0];
741 _kiwi
.gateway
.ctcp(null, true, type
, target
, ev
.params
.join(' '));
744 function settingsCommand (ev
) {
745 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
746 settings
.view
.show();
749 function scriptCommand (ev
) {
750 var editor
= _kiwi
.model
.Applet
.loadOnce('kiwi_script_editor');
754 function appletCommand (ev
) {
755 if (!ev
.params
[0]) return;
757 var panel
= new _kiwi
.model
.Applet();
760 // Url and name given
761 panel
.load(ev
.params
[0], ev
.params
[1]);
763 // Load a pre-loaded applet
764 if (_kiwi
.applets
[ev
.params
[0]]) {
765 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
767 _kiwi
.app
.panels().server
.addMsg('', 'Applet "' + ev
.params
[0] + '" does not exist');
772 _kiwi
.app
.connections
.active_connection
.panels
.add(panel
);
777 function whoisCommand (ev
) {
782 } else if (_kiwi
.app
.panels().active
.isQuery()) {
783 nick
= _kiwi
.app
.panels().active
.get('name');
787 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOIS ' + nick
+ ' ' + nick
);
791 function whowasCommand (ev
) {
796 } else if (_kiwi
.app
.panels().active
.isQuery()) {
797 nick
= _kiwi
.app
.panels().active
.get('name');
801 _kiwi
.app
.connections
.active_connection
.gateway
.raw('WHOWAS ' + nick
);
804 function encodingCommand (ev
) {
806 _kiwi
.gateway
.setEncoding(null, ev
.params
[0], function (success
) {
808 _kiwi
.app
.panels().active
.addMsg('', "Encoding modified to "+ev
.params
[0]);
810 _kiwi
.app
.panels().active
.addMsg('', ev
.params
[0]+' is not a valid encoding');
814 _kiwi
.app
.panels().active
.addMsg('', 'Encoding not specified');
815 _kiwi
.app
.panels().active
.addMsg('', 'Usage: /encoding [NEW-ENCODING]');
819 function serverCommand (ev
) {
820 var server
, port
, ssl
, password
, nick
,
823 // If no server address given, show the new connection dialog
825 tmp
= new _kiwi
.view
.MenuBox('New Connection');
826 tmp
.addItem('new_connection', new _kiwi
.model
.NewConnection().view
.$el
);
829 // Center screen the dialog
831 top
: (that
.view
.$el
.height() / 2) - (tmp
.$el
.height() / 2),
832 left
: (that
.view
.$el
.width() / 2) - (tmp
.$el
.width() / 2)
838 // Port given in 'host:port' format and no specific port given after a space
839 if (ev
.params
[0].indexOf(':') > 0) {
840 tmp
= ev
.params
[0].split(':');
844 password
= ev
.params
[1] || undefined;
847 // Server + port given as 'host port'
848 server
= ev
.params
[0];
849 port
= ev
.params
[1] || 6667;
851 password
= ev
.params
[2] || undefined;
854 // + in the port means SSL
855 if (port
.toString()[0] === '+') {
857 port
= parseInt(port
.substring(1), 10);
862 // Default port if one wasn't found
865 // Use the same nick as we currently have
866 nick
= _kiwi
.app
.connections
.active_connection
.get('nick');
868 _kiwi
.app
.panels().active
.addMsg('', 'Connecting to ' + server
+ ':' + port
.toString() + '..');
870 _kiwi
.gateway
.newConnection({
876 }, function(err
, new_connection
) {
878 _kiwi
.app
.panels().active
.addMsg('', 'Error connecting to ' + server
+ ':' + port
.toString() + ' (' + err
.toString() + ')');
886 this.isChannelName = function (channel_name
) {
887 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
889 if (!channel_name
|| !channel_name
.length
) return false;
890 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
897 model
= Backbone
.Model
.extend(new model());
899 return new model(arguments
);