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