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