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