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