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