Text theme regression fixes.
[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 fn = function(panel_type) {
313 var panels;
314
315 // Default panel type
316 panel_type = panel_type || 'connections';
317
318 switch (panel_type) {
319 case 'connections':
320 panels = this.connections.panels();
321 break;
322 case 'applets':
323 panels = this.applet_panels.models;
324 break;
325 }
326
327 // Active panels / server
328 panels.active = this.connections.active_panel;
329 panels.server = this.connections.active_connection ?
330 this.connections.active_connection.panels.server :
331 null;
332
333 return panels;
334 };
335
336 _.extend(fn, Backbone.Events);
337
338 return fn;
339 })(),
340
341
342 bindGatewayCommands: function (gw) {
343 var that = this;
344
345 gw.on('onconnect', function (event) {
346 that.view.barsShow();
347 });
348
349
350 /**
351 * Handle the reconnections to the kiwi server
352 */
353 (function () {
354 // 0 = non-reconnecting state. 1 = reconnecting state.
355 var gw_stat = 0;
356
357 // If the current or upcoming disconnect was planned
358 var unplanned_disconnect = false;
359
360 gw.on('disconnect', function (event) {
361 unplanned_disconnect = !gw.disconnect_requested;
362
363 if (unplanned_disconnect) {
364 var msg = _kiwi.global.i18n.translate('client_models_application_reconnecting').fetch() + '...';
365 that.message.text(msg, {timeout: 10000});
366 }
367
368 that.view.$el.removeClass('connected');
369
370 // Mention the disconnection on every channel
371 _kiwi.app.connections.forEach(function(connection) {
372 connection.panels.server.addMsg('', styleText('quit', {'%T': msg}), 'action quit');
373
374 connection.panels.forEach(function(panel) {
375 if (!panel.isChannel())
376 return;
377
378 panel.addMsg('', styleText('quit', {'%T': msg}), 'action quit');
379 });
380 });
381
382 gw_stat = 1;
383 });
384
385
386 gw.on('reconnecting', function (event) {
387 var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_in_x_seconds').fetch(event.delay/1000) + '...';
388
389 // Only need to mention the repeating re-connection messages on server panels
390 _kiwi.app.connections.forEach(function(connection) {
391 connection.panels.server.addMsg('', styleText('quit', {'%T': msg}), 'action quit');
392 });
393 });
394
395
396 gw.on('onconnect', function (event) {
397 that.view.$el.addClass('connected');
398 if (gw_stat !== 1) return;
399
400 if (unplanned_disconnect) {
401 var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_successfully').fetch() + ':)';
402 that.message.text(msg, {timeout: 5000});
403 }
404
405 // Mention the re-connection on every channel
406 _kiwi.app.connections.forEach(function(connection) {
407 connection.panels.server.addMsg('', styleText('rejoin', {'%T': msg}), 'action join');
408
409 connection.panels.forEach(function(panel) {
410 if (!panel.isChannel())
411 return;
412
413 panel.addMsg('', styleText('rejoin', {'%T': msg}), 'action join');
414 });
415 });
416
417 gw_stat = 0;
418 });
419 })();
420
421
422 gw.on('kiwi:reconfig', function () {
423 $.getJSON(that.get('settings_path'), function (data) {
424 that.server_settings = data.server_settings || {};
425 that.translations = data.translations || {};
426 });
427 });
428
429
430 gw.on('kiwi:jumpserver', function (data) {
431 var serv;
432 // No server set? Then nowhere to jump to.
433 if (typeof data.kiwi_server === 'undefined')
434 return;
435
436 serv = data.kiwi_server;
437
438 // Strip any trailing slash from the end
439 if (serv[serv.length-1] === '/')
440 serv = serv.substring(0, serv.length-1);
441
442 // Force the jumpserver now?
443 if (data.force) {
444 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once
445 var jump_server_interval = Math.random() * (360 - 300) + 300;
446
447 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect
448 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();
449 that.message.text(msg, {timeout: 10000});
450
451 setTimeout(function forcedReconnect() {
452 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();
453 that.message.text(msg, {timeout: 8000});
454
455 setTimeout(function forcedReconnectPartTwo() {
456 _kiwi.app.kiwi_server = serv;
457
458 _kiwi.gateway.reconnect(function() {
459 // Reconnect all the IRC connections
460 that.connections.forEach(function(con){ con.reconnect(); });
461 });
462 }, 5000);
463
464 }, jump_server_interval * 1000);
465 }
466 });
467 },
468
469
470
471 /**
472 * Bind to certain commands that may be typed into the control box
473 */
474 bindControllboxCommands: function (controlbox) {
475 var that = this;
476
477 // Default aliases
478 $.extend(controlbox.preprocessor.aliases, {
479 // General aliases
480 '/p': '/part $1+',
481 '/me': '/action $1+',
482 '/j': '/join $1+',
483 '/q': '/query $1+',
484 '/w': '/whois $1+',
485 '/raw': '/quote $1+',
486
487 // Op related aliases
488 '/op': '/quote mode $channel +o $1+',
489 '/deop': '/quote mode $channel -o $1+',
490 '/hop': '/quote mode $channel +h $1+',
491 '/dehop': '/quote mode $channel -h $1+',
492 '/voice': '/quote mode $channel +v $1+',
493 '/devoice': '/quote mode $channel -v $1+',
494 '/k': '/kick $channel $1+',
495 '/ban': '/quote mode $channel +b $1+',
496 '/unban': '/quote mode $channel -b $1+',
497
498 // Misc aliases
499 '/slap': '/me slaps $1 around a bit with a large trout'
500 });
501
502 // Functions to bind to controlbox events
503 var fn_to_bind = {
504 'unknown_command': unknownCommand,
505 'command': allCommands,
506 'command:msg': msgCommand,
507 'command:action': actionCommand,
508 'command:join': joinCommand,
509 'command:part': partCommand,
510 'command:nick': nickCommand,
511 'command:query': queryCommand,
512 'command:invite': inviteCommand,
513 'command:topic': topicCommand,
514 'command:notice': noticeCommand,
515 'command:quote': quoteCommand,
516 'command:kick': kickCommand,
517 'command:clear': clearCommand,
518 'command:ctcp': ctcpCommand,
519 'command:server': serverCommand,
520 'command:whois': whoisCommand,
521 'command:whowas': whowasCommand,
522 'command:encoding': encodingCommand,
523 'command:channel': channelCommand,
524 'command:applet': appletCommand,
525 'command:settings': settingsCommand,
526 'command:script': scriptCommand
527 };
528
529 fn_to_bind['command:css'] = function (ev) {
530 var queryString = '?reload=' + new Date().getTime();
531 $('link[rel="stylesheet"]').each(function () {
532 this.href = this.href.replace(/\?.*|$/, queryString);
533 });
534 };
535
536 fn_to_bind['command:js'] = function (ev) {
537 if (!ev.params[0]) return;
538 $script(ev.params[0] + '?' + (new Date().getTime()));
539 };
540
541
542 fn_to_bind['command:set'] = function (ev) {
543 if (!ev.params[0]) return;
544
545 var setting = ev.params[0],
546 value;
547
548 // Do we have a second param to set a value?
549 if (ev.params[1]) {
550 ev.params.shift();
551
552 value = ev.params.join(' ');
553
554 // If we're setting a true boolean value..
555 if (value === 'true')
556 value = true;
557
558 // If we're setting a false boolean value..
559 if (value === 'false')
560 value = false;
561
562 // If we're setting a number..
563 if (parseInt(value, 10).toString() === value)
564 value = parseInt(value, 10);
565
566 _kiwi.global.settings.set(setting, value);
567 }
568
569 // Read the value to the user
570 _kiwi.app.panels().active.addMsg('', styleText('set_setting', {'%T': setting + ' = ' + _kiwi.global.settings.get(setting).toString()}));
571 };
572
573
574 fn_to_bind['command:save'] = function (ev) {
575 _kiwi.global.settings.save();
576 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_settings_saved', {'%T': translateText('client_models_application_settings_saved')}));
577 };
578
579
580 fn_to_bind['command:alias'] = function (ev) {
581 var name, rule;
582
583 // No parameters passed so list them
584 if (!ev.params[1]) {
585 $.each(controlbox.preprocessor.aliases, function (name, rule) {
586 _kiwi.app.panels().server.addMsg(' ', styleText('list_aliases', {'%T': name + ' => ' + rule}));
587 });
588 return;
589 }
590
591 // Deleting an alias?
592 if (ev.params[0] === 'del' || ev.params[0] === 'delete') {
593 name = ev.params[1];
594 if (name[0] !== '/') name = '/' + name;
595 delete controlbox.preprocessor.aliases[name];
596 return;
597 }
598
599 // Add the alias
600 name = ev.params[0];
601 ev.params.shift();
602 rule = ev.params.join(' ');
603
604 // Make sure the name starts with a slash
605 if (name[0] !== '/') name = '/' + name;
606
607 // Now actually add the alias
608 controlbox.preprocessor.aliases[name] = rule;
609 };
610
611
612 fn_to_bind['command:ignore'] = function (ev) {
613 var list = this.connections.active_connection.get('ignore_list');
614
615 // No parameters passed so list them
616 if (!ev.params[0]) {
617 if (list.length > 0) {
618 _kiwi.app.panels().active.addMsg(' ', styleText('client_models_application_ignore_title', {'%T': translateText('client_models_application_ignore_title')}));
619 $.each(list, function (idx, ignored_pattern) {
620 _kiwi.app.panels().active.addMsg(' ', styleText('ignored_pattern', {'%T': ignored_pattern}));
621 });
622 } else {
623 _kiwi.app.panels().active.addMsg(' ', styleText('client_models_application_ignore_none', {'%T': translateText('client_models_application_ignore_none')}));
624 }
625 return;
626 }
627
628 // We have a parameter, so add it
629 list.push(ev.params[0]);
630 this.connections.active_connection.set('ignore_list', list);
631 _kiwi.app.panels().active.addMsg(' ', styleText('client_models_application_ignore_nick', {'%T': translateText('client_models_application_ignore_nick', [ev.params[0]])}));
632 };
633
634
635 fn_to_bind['command:unignore'] = function (ev) {
636 var list = this.connections.active_connection.get('ignore_list');
637
638 if (!ev.params[0]) {
639 _kiwi.app.panels().active.addMsg(' ', styleText('client_models_application_ignore_stop_notice', {'%T': translateText('client_models_application_ignore_stop_notice')}));
640 return;
641 }
642
643 list = _.reject(list, function(pattern) {
644 return pattern === ev.params[0];
645 });
646
647 this.connections.active_connection.set('ignore_list', list);
648
649 _kiwi.app.panels().active.addMsg(' ', styleText('client_models_application_ignore_stopped', {'%T': translateText('client_models_application_ignore_stopped', [ev.params[0]])}));
650 };
651
652
653 _.each(fn_to_bind, function(fn, event_name) {
654 controlbox.on(event_name, _.bind(fn, that));
655 });
656 },
657
658
659 isChannelName: function (channel_name) {
660 var channel_prefix = _kiwi.gateway.get('channel_prefix');
661
662 if (!channel_name || !channel_name.length) return false;
663 return (channel_prefix.indexOf(channel_name[0]) > -1);
664 }
665 });
666
667
668
669
670 // A fallback action. Send a raw command to the server
671 function unknownCommand (ev) {
672 var raw_cmd = ev.command + ' ' + ev.params.join(' ');
673 console.log('RAW: ' + raw_cmd);
674 this.connections.active_connection.gateway.raw(raw_cmd);
675 }
676
677 function allCommands (ev) {}
678
679 function joinCommand (ev) {
680 var panels, channel_names;
681
682 channel_names = ev.params.join(' ').split(',');
683 panels = this.connections.active_connection.createAndJoinChannels(channel_names);
684
685 // Show the last channel if we have one
686 if (panels.length)
687 panels[panels.length - 1].view.show();
688 }
689
690 function queryCommand (ev) {
691 var destination, message, panel;
692
693 destination = ev.params[0];
694 ev.params.shift();
695
696 message = ev.params.join(' ');
697
698 // Check if we have the panel already. If not, create it
699 panel = this.connections.active_connection.panels.getByName(destination);
700 if (!panel) {
701 panel = new _kiwi.model.Query({name: destination});
702 this.connections.active_connection.panels.add(panel);
703 }
704
705 if (panel) panel.view.show();
706
707 if (message) {
708 this.connections.active_connection.gateway.msg(panel.get('name'), message);
709 panel.addMsg(_kiwi.app.connections.active_connection.get('nick'), styleText('query', {'%T': message}));
710 }
711
712 }
713
714 function msgCommand (ev) {
715 var message,
716 destination = ev.params[0],
717 panel = this.connections.active_connection.panels.getByName(destination) || this.panels().server;
718
719 ev.params.shift();
720 message = formatToIrcMsg(ev.params.join(' '));
721
722 panel.addMsg(_kiwi.app.connections.active_connection.get('nick'), styleText('msg', {'%T': message}));
723 this.connections.active_connection.gateway.msg(destination, message);
724 }
725
726 function actionCommand (ev) {
727 if (_kiwi.app.panels().active.isServer()) {
728 return;
729 }
730
731 var panel = _kiwi.app.panels().active;
732 panel.addMsg('', styleText('action', {'%T': _kiwi.app.connections.active_connection.get('nick') + ' ' + ev.params.join(' ')}), 'action');
733 this.connections.active_connection.gateway.action(panel.get('name'), ev.params.join(' '));
734 }
735
736 function partCommand (ev) {
737 if (ev.params.length === 0) {
738 this.connections.active_connection.gateway.part(_kiwi.app.panels().active.get('name'));
739 } else {
740 _.each(ev.params, function (channel) {
741 this.connections.active_connection.gateway.part(channel);
742 });
743 }
744 }
745
746 function nickCommand (ev) {
747 this.connections.active_connection.gateway.changeNick(ev.params[0]);
748 }
749
750 function topicCommand (ev) {
751 var channel_name;
752
753 if (ev.params.length === 0) return;
754
755 if (this.isChannelName(ev.params[0])) {
756 channel_name = ev.params[0];
757 ev.params.shift();
758 } else {
759 channel_name = _kiwi.app.panels().active.get('name');
760 }
761
762 this.connections.active_connection.gateway.topic(channel_name, ev.params.join(' '));
763 }
764
765 function noticeCommand (ev) {
766 var destination;
767
768 // Make sure we have a destination and some sort of message
769 if (ev.params.length <= 1) return;
770
771 destination = ev.params[0];
772 ev.params.shift();
773
774 this.connections.active_connection.gateway.notice(destination, ev.params.join(' '));
775 }
776
777 function quoteCommand (ev) {
778 var raw = ev.params.join(' ');
779 this.connections.active_connection.gateway.raw(raw);
780 }
781
782 function kickCommand (ev) {
783 var nick, panel = _kiwi.app.panels().active;
784
785 if (!panel.isChannel()) return;
786
787 // Make sure we have a nick
788 if (ev.params.length === 0) return;
789
790 nick = ev.params[0];
791 ev.params.shift();
792
793 this.connections.active_connection.gateway.kick(panel.get('name'), nick, ev.params.join(' '));
794 }
795
796 function clearCommand (ev) {
797 // Can't clear a server or applet panel
798 if (_kiwi.app.panels().active.isServer() || _kiwi.app.panels().active.isApplet()) {
799 return;
800 }
801
802 if (_kiwi.app.panels().active.clearMessages) {
803 _kiwi.app.panels().active.clearMessages();
804 }
805 }
806
807 function ctcpCommand(ev) {
808 var target, type;
809
810 // Make sure we have a target and a ctcp type (eg. version, time)
811 if (ev.params.length < 2) return;
812
813 target = ev.params[0];
814 ev.params.shift();
815
816 type = ev.params[0];
817 ev.params.shift();
818
819 this.connections.active_connection.gateway.ctcp(true, type, target, ev.params.join(' '));
820 }
821
822 function settingsCommand (ev) {
823 var settings = _kiwi.model.Applet.loadOnce('kiwi_settings');
824 settings.view.show();
825 }
826
827 function scriptCommand (ev) {
828 var editor = _kiwi.model.Applet.loadOnce('kiwi_script_editor');
829 editor.view.show();
830 }
831
832 function appletCommand (ev) {
833 if (!ev.params[0]) return;
834
835 var panel = new _kiwi.model.Applet();
836
837 if (ev.params[1]) {
838 // Url and name given
839 panel.load(ev.params[0], ev.params[1]);
840 } else {
841 // Load a pre-loaded applet
842 if (_kiwi.applets[ev.params[0]]) {
843 panel.load(new _kiwi.applets[ev.params[0]]());
844 } else {
845 _kiwi.app.panels().server.addMsg('', styleText('client_models_application_applet_notfound', {'%T': translateText('client_models_application_applet_notfound', [ev.params[0]])}));
846 return;
847 }
848 }
849
850 _kiwi.app.connections.active_connection.panels.add(panel);
851 panel.view.show();
852 }
853
854
855
856 function inviteCommand (ev) {
857 var nick, channel;
858
859 // A nick must be specified
860 if (!ev.params[0])
861 return;
862
863 // Can only invite into channels
864 if (!_kiwi.app.panels().active.isChannel())
865 return;
866
867 nick = ev.params[0];
868 channel = _kiwi.app.panels().active.get('name');
869
870 _kiwi.app.connections.active_connection.gateway.raw('INVITE ' + nick + ' ' + channel);
871
872 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_has_been_invited', {'%N': nick, '%T': translateText('client_models_application_has_been_invited', [channel])}), 'action');
873 }
874
875
876 function whoisCommand (ev) {
877 var nick;
878
879 if (ev.params[0]) {
880 nick = ev.params[0];
881 } else if (_kiwi.app.panels().active.isQuery()) {
882 nick = _kiwi.app.panels().active.get('name');
883 }
884
885 if (nick)
886 _kiwi.app.connections.active_connection.gateway.raw('WHOIS ' + nick + ' ' + nick);
887 }
888
889
890 function whowasCommand (ev) {
891 var nick;
892
893 if (ev.params[0]) {
894 nick = ev.params[0];
895 } else if (_kiwi.app.panels().active.isQuery()) {
896 nick = _kiwi.app.panels().active.get('name');
897 }
898
899 if (nick)
900 _kiwi.app.connections.active_connection.gateway.raw('WHOWAS ' + nick);
901 }
902
903 function encodingCommand (ev) {
904 if (ev.params[0]) {
905 _kiwi.gateway.setEncoding(null, ev.params[0], function (success) {
906 if (success) {
907 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_encoding_changed', {'%T': translateText('client_models_application_encoding_changed', [ev.params[0]])}));
908 } else {
909 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_encoding_invalid', {'%T': translateText('client_models_application_encoding_invalid', [ev.params[0]])}));
910 }
911 });
912 } else {
913 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_encoding_notspecified', {'%T': translateText('client_models_application_encoding_notspecified')}));
914 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_encoding_usage', {'%T': translateText('client_models_application_encoding_usage')}));
915 }
916 }
917
918 function channelCommand (ev) {
919 var active_panel = _kiwi.app.panels().active;
920
921 if (!active_panel.isChannel())
922 return;
923
924 new _kiwi.model.ChannelInfo({channel: _kiwi.app.panels().active});
925 }
926
927 function serverCommand (ev) {
928 var server, port, ssl, password, nick,
929 tmp;
930
931 // If no server address given, show the new connection dialog
932 if (!ev.params[0]) {
933 tmp = new _kiwi.view.MenuBox(_kiwi.global.i18n.translate('client_models_application_connection_create').fetch());
934 tmp.addItem('new_connection', new _kiwi.model.NewConnection().view.$el);
935 tmp.show();
936
937 // Center screen the dialog
938 tmp.$el.offset({
939 top: (this.view.$el.height() / 2) - (tmp.$el.height() / 2),
940 left: (this.view.$el.width() / 2) - (tmp.$el.width() / 2)
941 });
942
943 return;
944 }
945
946 // Port given in 'host:port' format and no specific port given after a space
947 if (ev.params[0].indexOf(':') > 0) {
948 tmp = ev.params[0].split(':');
949 server = tmp[0];
950 port = tmp[1];
951
952 password = ev.params[1] || undefined;
953
954 } else {
955 // Server + port given as 'host port'
956 server = ev.params[0];
957 port = ev.params[1] || 6667;
958
959 password = ev.params[2] || undefined;
960 }
961
962 // + in the port means SSL
963 if (port.toString()[0] === '+') {
964 ssl = true;
965 port = parseInt(port.substring(1), 10);
966 } else {
967 ssl = false;
968 }
969
970 // Default port if one wasn't found
971 port = port || 6667;
972
973 // Use the same nick as we currently have
974 nick = _kiwi.app.connections.active_connection.get('nick');
975
976 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_connection_connecting', {'%T': translateText('client_models_application_connection_connecting', [server, port.toString()])}));
977
978 _kiwi.gateway.newConnection({
979 nick: nick,
980 host: server,
981 port: port,
982 ssl: ssl,
983 password: password
984 }, function(err, new_connection) {
985 if (err)
986 _kiwi.app.panels().active.addMsg('', styleText('client_models_application_connection_error', {'%T': translateText('client_models_application_connection_error', [server, port.toString(), err.toString()])}));
987 });
988 }
989
990 })();