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