Client: Apply default settings before applying any others
[KiwiIRC.git] / client / assets / src / models / application.js
1 _kiwi.model.Application = function () {
2 // Set to a reference to this object within initialize()
3 var that = null;
4
5
6 var model = function () {
7 /** _kiwi.view.Application */
8 this.view = null;
9
10 /** _kiwi.view.StatusMessage */
11 this.message = null;
12
13 /* Address for the kiwi server */
14 this.kiwi_server = null;
15
16 this.initialize = function (options) {
17 that = this;
18
19 if (options[0].container) {
20 this.set('container', options[0].container);
21 }
22
23 // The base url to the kiwi server
24 this.set('base_path', options[0].base_path ? options[0].base_path : '/kiwi');
25
26 // Any options sent down from the server
27 this.server_settings = options[0].server_settings || {};
28
29 // Best guess at where the kiwi server is
30 this.detectKiwiServer();
31
32 // Takes instances of model_network
33 this.connections = new _kiwi.model.NetworkPanelList();
34
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);
38 }
39 };
40
41
42 this.start = function () {
43 // Only debug if set in the querystring
44 if (!getQueryVariable('debug')) {
45 manageDebug(false);
46 } else {
47 //manageDebug(true);
48 }
49
50 // Set the gateway up
51 _kiwi.gateway = new _kiwi.model.Gateway();
52 this.bindGatewayCommands(_kiwi.gateway);
53
54 this.initializeClient();
55 this.initializeGlobals();
56
57 this.view.barsHide(true);
58
59 this.showIntialConenctionDialog();
60 };
61
62
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';
67 } else {
68 // Assume the kiwi server is on the same server
69 this.kiwi_server = window.location.protocol + '//' + window.location.host;
70 }
71 };
72
73
74 this.showIntialConenctionDialog = function() {
75 var connection_dialog = new _kiwi.model.NewConnection();
76 this.populateDefaultServerSettings(connection_dialog);
77
78 connection_dialog.view.$el.addClass('initial');
79 this.view.$el.find('.panel_container:first').append(connection_dialog.view.$el);
80
81 var $info = $($('#tmpl_new_connection_info').html().trim());
82
83 if ($info.html()) {
84 connection_dialog.view.infoBoxSet($info);
85 connection_dialog.view.infoBoxShow();
86 }
87
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();
92 }, 0);
93
94 // Once connected, close this dialog and remove its own event
95 var fn = function() {
96 connection_dialog.view.$el.slideUp(function() {
97 connection_dialog.view.dispose();
98 connection_dialog = null;
99
100 _kiwi.gateway.off('onconnect', fn);
101 });
102
103 };
104 _kiwi.gateway.on('onconnect', fn);
105 };
106
107
108 this.initializeClient = function () {
109 this.view = new _kiwi.view.Application({model: this, el: this.get('container')});
110
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);
115
116 /**
117 * Set the UI components up
118 */
119 this.controlbox = new _kiwi.view.ControlBox({el: $('#kiwi .controlbox')[0]});
120 this.bindControllboxCommands(this.controlbox);
121
122 this.topicbar = new _kiwi.view.TopicBar({el: this.view.$el.find('.topic')[0]});
123
124 new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});
125
126 this.message = new _kiwi.view.StatusMessage({el: this.view.$el.find('.status_message')[0]});
127
128 this.resize_handle = new _kiwi.view.ResizeHandler({el: this.view.$el.find('.memberlists_resize_handle')[0]});
129
130 // Rejigg the UI sizes
131 this.view.doLayout();
132 };
133
134
135 this.initializeGlobals = function () {
136 _kiwi.global.connections = this.connections;
137
138 _kiwi.global.panels = this.panels;
139 _kiwi.global.panels.applets = this.applet_panels;
140
141 _kiwi.global.components.Applet = _kiwi.model.Applet;
142 _kiwi.global.components.Panel =_kiwi.model.Panel;
143 };
144
145
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);
150 }
151 });
152 };
153
154
155 this.populateDefaultServerSettings = function (new_connection_dialog) {
156 var parts;
157 var defaults = {
158 nick: '',
159 server: '',
160 port: 6667,
161 ssl: false,
162 channel: '#chat',
163 channel_key: ''
164 };
165 var uricheck;
166
167
168 /**
169 * Get any settings set by the server
170 * These settings may be changed in the server selection dialog or via URL parameters
171 */
172 if (this.server_settings.client) {
173 if (this.server_settings.client.nick)
174 defaults.nick = this.server_settings.client.nick;
175
176 if (this.server_settings.client.server)
177 defaults.server = this.server_settings.client.server;
178
179 if (this.server_settings.client.port)
180 defaults.port = this.server_settings.client.port;
181
182 if (this.server_settings.client.ssl)
183 defaults.ssl = this.server_settings.client.ssl;
184
185 if (this.server_settings.client.channel)
186 defaults.channel = this.server_settings.client.channel;
187
188 if (this.server_settings.client.channel_key)
189 defaults.channel_key = this.server_settings.client.channel_key;
190 }
191
192
193
194 /**
195 * Get any settings passed in the URL
196 * These settings may be changed in the server selection dialog
197 */
198
199 // Any query parameters first
200 if (getQueryVariable('nick'))
201 defaults.nick = getQueryVariable('nick');
202
203 if (window.location.hash)
204 defaults.channel = window.location.hash;
205
206
207 // Process the URL part by part, extracting as we go
208 parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/');
209
210 if (parts.length > 0) {
211 parts.shift();
212
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]);
220 /*
221 uricheck[1] = ssl (optional)
222 uricheck[2] = host
223 uricheck[3] = port (optional)
224 uricheck[4] = channel (optional)
225 uricheck[5] = channel key (optional, channel must also be set)
226 */
227 if (uricheck) {
228 if (typeof uricheck[1] !== 'undefined') {
229 defaults.ssl = true;
230 if (defaults.port === 6667) {
231 defaults.port = 6697;
232 }
233 }
234 defaults.server = uricheck[2];
235 if (typeof uricheck[3] !== 'undefined') {
236 defaults.port = uricheck[3];
237 }
238 if (typeof uricheck[4] !== 'undefined') {
239 defaults.channel = '#' + uricheck[4];
240 if (typeof uricheck[5] !== 'undefined') {
241 defaults.channel_key = uricheck[5];
242 }
243 }
244 }
245 parts = [];
246 } else {
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);
253 defaults.ssl = true;
254 } else {
255 defaults.ssl = false;
256 }
257
258 } else {
259 defaults.server = parts[0];
260 }
261
262 parts.shift();
263 }
264 }
265
266 if (parts.length > 0 && parts[0]) {
267 defaults.channel = '#' + parts[0];
268 parts.shift();
269 }
270 }
271
272 // If any settings have been given by the server.. override any auto detected settings
273 /**
274 * Get any server restrictions as set in the server config
275 * These settings can not be changed in the server selection dialog
276 */
277 if (this.server_settings && this.server_settings.connection) {
278 if (this.server_settings.connection.server) {
279 defaults.server = this.server_settings.connection.server;
280 }
281
282 if (this.server_settings.connection.port) {
283 defaults.port = this.server_settings.connection.port;
284 }
285
286 if (this.server_settings.connection.ssl) {
287 defaults.ssl = this.server_settings.connection.ssl;
288 }
289
290 if (this.server_settings.connection.channel) {
291 defaults.channel = this.server_settings.connection.channel;
292 }
293
294 if (this.server_settings.connection.channel_key) {
295 defaults.channel_key = this.server_settings.connection.channel_key;
296 }
297
298 if (this.server_settings.connection.nick) {
299 defaults.nick = this.server_settings.connection.nick;
300 }
301 }
302
303 // Set any random numbers if needed
304 defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());
305
306 // Populate the server select box with defaults
307 new_connection_dialog.view.populateFields(defaults);
308 };
309
310
311 this.panels = (function() {
312 var fn = function(panel_type) {
313 var panels;
314
315 // Default panel type
316 panel_type = panel_type || 'connections';
317
318 switch (panel_type) {
319 case 'connections':
320 panels = this.connections.panels();
321 break;
322 case 'applets':
323 panels = this.applet_panels.models;
324 break;
325 }
326
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 :
331 null;
332
333 return panels;
334 };
335
336 _.extend(fn, Backbone.Events);
337
338 return fn;
339 })();
340
341
342 this.bindGatewayCommands = function (gw) {
343 gw.on('onconnect', function (event) {
344 that.view.barsShow();
345 });
346
347
348 /**
349 * Handle the reconnections to the kiwi server
350 */
351 (function () {
352 var gw_stat = 0;
353
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});
357
358 that.view.$el.removeClass('connected');
359
360 // Mention the disconnection on every channel
361 _kiwi.app.connections.forEach(function(connection) {
362 connection.panels.server.addMsg('', msg, 'action quit');
363
364 connection.panels.forEach(function(panel) {
365 if (!panel.isChannel())
366 return;
367
368 panel.addMsg('', msg, 'action quit');
369 });
370 });
371
372 gw_stat = 1;
373 });
374
375
376 gw.on('reconnecting', function (event) {
377 var msg = 'You have been disconnected. Attempting to reconnect again in ' + (event.delay/1000) + ' seconds..';
378
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');
382 });
383 });
384
385
386 gw.on('onconnect', function (event) {
387 that.view.$el.addClass('connected');
388 if (gw_stat !== 1) return;
389
390 var msg = 'It\'s OK, you\'re connected again :)';
391 that.message.text(msg, {timeout: 5000});
392
393 // Mention the disconnection on every channel
394 _kiwi.app.connections.forEach(function(connection) {
395 connection.panels.server.addMsg('', msg, 'action join');
396
397 connection.panels.forEach(function(panel) {
398 if (!panel.isChannel())
399 return;
400
401 panel.addMsg('', msg, 'action join');
402 });
403 });
404
405 gw_stat = 0;
406 });
407 })();
408
409 };
410
411
412
413 /**
414 * Bind to certain commands that may be typed into the control box
415 */
416 this.bindControllboxCommands = function (controlbox) {
417 // Default aliases
418 $.extend(controlbox.preprocessor.aliases, {
419 // General aliases
420 '/p': '/part $1+',
421 '/me': '/action $1+',
422 '/j': '/join $1+',
423 '/q': '/query $1+',
424 '/w': '/whois $1+',
425 '/raw': '/quote $1+',
426
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+',
435
436 // Misc aliases
437 '/slap': '/me slaps $1 around a bit with a large trout'
438 });
439
440 controlbox.on('unknown_command', unknownCommand);
441
442 controlbox.on('command', allCommands);
443 controlbox.on('command:msg', msgCommand);
444
445 controlbox.on('command:action', actionCommand);
446
447 controlbox.on('command:join', joinCommand);
448
449 controlbox.on('command:part', partCommand);
450
451 controlbox.on('command:nick', function (ev) {
452 _kiwi.gateway.changeNick(null, ev.params[0]);
453 });
454
455 controlbox.on('command:query', queryCommand);
456
457 controlbox.on('command:topic', topicCommand);
458
459 controlbox.on('command:notice', noticeCommand);
460
461 controlbox.on('command:quote', quoteCommand);
462
463 controlbox.on('command:kick', kickCommand);
464
465 controlbox.on('command:clear', clearCommand);
466
467 controlbox.on('command:ctcp', ctcpCommand);
468
469 controlbox.on('command:server', serverCommand);
470
471 controlbox.on('command:whois', whoisCommand);
472
473 controlbox.on('command:whowas', whowasCommand);
474
475 controlbox.on('command:encoding', encodingCommand);
476
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);
481 });
482 });
483
484 controlbox.on('command:js', function (ev) {
485 if (!ev.params[0]) return;
486 $script(ev.params[0] + '?' + (new Date().getTime()));
487 });
488
489
490 controlbox.on('command:set', function (ev) {
491 if (!ev.params[0]) return;
492
493 var setting = ev.params[0],
494 value;
495
496 // Do we have a second param to set a value?
497 if (ev.params[1]) {
498 ev.params.shift();
499
500 value = ev.params.join(' ');
501
502 // If we're setting a true boolean value..
503 if (value === 'true')
504 value = true;
505
506 // If we're setting a false boolean value..
507 if (value === 'false')
508 value = false;
509
510 // If we're setting a number..
511 if (parseInt(value, 10).toString() === value)
512 value = parseInt(value, 10);
513
514 _kiwi.global.settings.set(setting, value);
515 }
516
517 // Read the value to the user
518 _kiwi.app.panels().active.addMsg('', setting + ' = ' + _kiwi.global.settings.get(setting).toString());
519 });
520
521
522 controlbox.on('command:save', function (ev) {
523 _kiwi.global.settings.save();
524 _kiwi.app.panels().active.addMsg('', 'Settings have been saved');
525 });
526
527
528 controlbox.on('command:alias', function (ev) {
529 var name, rule;
530
531 // No parameters passed so list them
532 if (!ev.params[1]) {
533 $.each(controlbox.preprocessor.aliases, function (name, rule) {
534 _kiwi.app.panels().server.addMsg(' ', name + ' => ' + rule);
535 });
536 return;
537 }
538
539 // Deleting an alias?
540 if (ev.params[0] === 'del' || ev.params[0] === 'delete') {
541 name = ev.params[1];
542 if (name[0] !== '/') name = '/' + name;
543 delete controlbox.preprocessor.aliases[name];
544 return;
545 }
546
547 // Add the alias
548 name = ev.params[0];
549 ev.params.shift();
550 rule = ev.params.join(' ');
551
552 // Make sure the name starts with a slash
553 if (name[0] !== '/') name = '/' + name;
554
555 // Now actually add the alias
556 controlbox.preprocessor.aliases[name] = rule;
557 });
558
559
560 controlbox.on('command:ignore', function (ev) {
561 var list = _kiwi.gateway.get('ignore_list');
562
563 // No parameters passed so list them
564 if (!ev.params[0]) {
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);
569 });
570 } else {
571 _kiwi.app.panels().active.addMsg(' ', 'Not ignoring anybody');
572 }
573 return;
574 }
575
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]);
580 });
581
582
583 controlbox.on('command:unignore', function (ev) {
584 var list = _kiwi.gateway.get('ignore_list');
585
586 if (!ev.params[0]) {
587 _kiwi.app.panels().active.addMsg(' ', 'Specifiy which nick you wish to stop ignoring');
588 return;
589 }
590
591 list = _.reject(list, function(pattern) {
592 return pattern === ev.params[0];
593 });
594
595 _kiwi.gateway.set('ignore_list', list);
596
597 _kiwi.app.panels().active.addMsg(' ', 'Stopped ignoring ' + ev.params[0]);
598 });
599
600
601 controlbox.on('command:applet', appletCommand);
602 controlbox.on('command:settings', settingsCommand);
603 controlbox.on('command:script', scriptCommand);
604 };
605
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);
611 }
612
613 function allCommands (ev) {}
614
615 function joinCommand (ev) {
616 var panels, channel_names;
617
618 channel_names = ev.params.join(' ').split(',');
619 panels = that.connections.active_connection.createAndJoinChannels(channel_names);
620
621 // Show the last channel if we have one
622 if (panels.length)
623 panels[panels.length - 1].view.show();
624 }
625
626 function queryCommand (ev) {
627 var destination, panel;
628
629 destination = ev.params[0];
630
631 // Check if we have the panel already. If not, create it
632 panel = that.connections.active_connection.panels.getByName(destination);
633 if (!panel) {
634 panel = new _kiwi.model.Query({name: destination});
635 that.connections.active_connection.panels.add(panel);
636 }
637
638 if (panel) panel.view.show();
639
640 }
641
642 function msgCommand (ev) {
643 var destination = ev.params[0],
644 panel = that.connections.active_connection.panels.getByName(destination) || that.panels().server;
645
646 ev.params.shift();
647
648 panel.addMsg(_kiwi.app.connections.active_connection.get('nick'), ev.params.join(' '));
649 _kiwi.gateway.privmsg(null, destination, ev.params.join(' '));
650 }
651
652 function actionCommand (ev) {
653 if (_kiwi.app.panels().active.isServer()) {
654 return;
655 }
656
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(' '));
660 }
661
662 function partCommand (ev) {
663 if (ev.params.length === 0) {
664 _kiwi.gateway.part(null, _kiwi.app.panels().active.get('name'));
665 } else {
666 _.each(ev.params, function (channel) {
667 _kiwi.gateway.part(null, channel);
668 });
669 }
670 }
671
672 function topicCommand (ev) {
673 var channel_name;
674
675 if (ev.params.length === 0) return;
676
677 if (that.isChannelName(ev.params[0])) {
678 channel_name = ev.params[0];
679 ev.params.shift();
680 } else {
681 channel_name = _kiwi.app.panels().active.get('name');
682 }
683
684 _kiwi.gateway.topic(null, channel_name, ev.params.join(' '));
685 }
686
687 function noticeCommand (ev) {
688 var destination;
689
690 // Make sure we have a destination and some sort of message
691 if (ev.params.length <= 1) return;
692
693 destination = ev.params[0];
694 ev.params.shift();
695
696 _kiwi.gateway.notice(null, destination, ev.params.join(' '));
697 }
698
699 function quoteCommand (ev) {
700 var raw = ev.params.join(' ');
701 _kiwi.gateway.raw(null, raw);
702 }
703
704 function kickCommand (ev) {
705 var nick, panel = _kiwi.app.panels().active;
706
707 if (!panel.isChannel()) return;
708
709 // Make sure we have a nick
710 if (ev.params.length === 0) return;
711
712 nick = ev.params[0];
713 ev.params.shift();
714
715 _kiwi.gateway.kick(null, panel.get('name'), nick, ev.params.join(' '));
716 }
717
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()) {
721 return;
722 }
723
724 if (_kiwi.app.panels().active.clearMessages) {
725 _kiwi.app.panels().active.clearMessages();
726 }
727 }
728
729 function ctcpCommand(ev) {
730 var target, type;
731
732 // Make sure we have a target and a ctcp type (eg. version, time)
733 if (ev.params.length < 2) return;
734
735 target = ev.params[0];
736 ev.params.shift();
737
738 type = ev.params[0];
739 ev.params.shift();
740
741 _kiwi.gateway.ctcp(null, true, type, target, ev.params.join(' '));
742 }
743
744 function settingsCommand (ev) {
745 var settings = _kiwi.model.Applet.loadOnce('kiwi_settings');
746 settings.view.show();
747 }
748
749 function scriptCommand (ev) {
750 var editor = _kiwi.model.Applet.loadOnce('kiwi_script_editor');
751 editor.view.show();
752 }
753
754 function appletCommand (ev) {
755 if (!ev.params[0]) return;
756
757 var panel = new _kiwi.model.Applet();
758
759 if (ev.params[1]) {
760 // Url and name given
761 panel.load(ev.params[0], ev.params[1]);
762 } else {
763 // Load a pre-loaded applet
764 if (_kiwi.applets[ev.params[0]]) {
765 panel.load(new _kiwi.applets[ev.params[0]]());
766 } else {
767 _kiwi.app.panels().server.addMsg('', 'Applet "' + ev.params[0] + '" does not exist');
768 return;
769 }
770 }
771
772 _kiwi.app.connections.active_connection.panels.add(panel);
773 panel.view.show();
774 }
775
776
777 function whoisCommand (ev) {
778 var nick;
779
780 if (ev.params[0]) {
781 nick = ev.params[0];
782 } else if (_kiwi.app.panels().active.isQuery()) {
783 nick = _kiwi.app.panels().active.get('name');
784 }
785
786 if (nick)
787 _kiwi.app.connections.active_connection.gateway.raw('WHOIS ' + nick + ' ' + nick);
788 }
789
790
791 function whowasCommand (ev) {
792 var nick;
793
794 if (ev.params[0]) {
795 nick = ev.params[0];
796 } else if (_kiwi.app.panels().active.isQuery()) {
797 nick = _kiwi.app.panels().active.get('name');
798 }
799
800 if (nick)
801 _kiwi.app.connections.active_connection.gateway.raw('WHOWAS ' + nick);
802 }
803
804 function encodingCommand (ev) {
805 if (ev.params[0]) {
806 _kiwi.gateway.setEncoding(null, ev.params[0], function (success) {
807 if (success) {
808 _kiwi.app.panels().active.addMsg('', "Encoding modified to "+ev.params[0]);
809 } else {
810 _kiwi.app.panels().active.addMsg('', ev.params[0]+' is not a valid encoding');
811 }
812 });
813 } else {
814 _kiwi.app.panels().active.addMsg('', 'Encoding not specified');
815 _kiwi.app.panels().active.addMsg('', 'Usage: /encoding [NEW-ENCODING]');
816 }
817 }
818
819 function serverCommand (ev) {
820 var server, port, ssl, password, nick,
821 tmp;
822
823 // If no server address given, show the new connection dialog
824 if (!ev.params[0]) {
825 tmp = new _kiwi.view.MenuBox('New Connection');
826 tmp.addItem('new_connection', new _kiwi.model.NewConnection().view.$el);
827 tmp.show();
828
829 // Center screen the dialog
830 tmp.$el.offset({
831 top: (that.view.$el.height() / 2) - (tmp.$el.height() / 2),
832 left: (that.view.$el.width() / 2) - (tmp.$el.width() / 2)
833 });
834
835 return;
836 }
837
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(':');
841 server = tmp[0];
842 port = tmp[1];
843
844 password = ev.params[1] || undefined;
845
846 } else {
847 // Server + port given as 'host port'
848 server = ev.params[0];
849 port = ev.params[1] || 6667;
850
851 password = ev.params[2] || undefined;
852 }
853
854 // + in the port means SSL
855 if (port.toString()[0] === '+') {
856 ssl = true;
857 port = parseInt(port.substring(1), 10);
858 } else {
859 ssl = false;
860 }
861
862 // Default port if one wasn't found
863 port = port || 6667;
864
865 // Use the same nick as we currently have
866 nick = _kiwi.app.connections.active_connection.get('nick');
867
868 _kiwi.app.panels().active.addMsg('', 'Connecting to ' + server + ':' + port.toString() + '..');
869
870 _kiwi.gateway.newConnection({
871 nick: nick,
872 host: server,
873 port: port,
874 ssl: ssl,
875 password: password
876 }, function(err, new_connection) {
877 if (err)
878 _kiwi.app.panels().active.addMsg('', 'Error connecting to ' + server + ':' + port.toString() + ' (' + err.toString() + ')');
879 });
880 }
881
882
883
884
885
886 this.isChannelName = function (channel_name) {
887 var channel_prefix = _kiwi.gateway.get('channel_prefix');
888
889 if (!channel_name || !channel_name.length) return false;
890 return (channel_prefix.indexOf(channel_name[0]) > -1);
891 };
892
893
894 };
895
896
897 model = Backbone.Model.extend(new model());
898
899 return new model(arguments);
900 };