socket.io resource now base_path + '/transport'. #97
[KiwiIRC.git] / client / dev / model_application.js
1 kiwi.model.Application = function () {
2 // Set to a reference to this object within initialize()
3 var that = null;
4
5 // The auto connect details entered into the server select box
6 var auto_connect_details = {};
7
8
9 var model = function () {
10 /** Instance of kiwi.model.PanelList */
11 this.panels = null;
12
13 /** kiwi.view.Application */
14 this.view = null;
15
16 /** kiwi.view.StatusMessage */
17 this.message = null;
18
19 /* Address for the kiwi server */
20 this.kiwi_server = null;
21
22 this.initialize = function (options) {
23 that = this;
24
25 if (options[0].container) {
26 this.set('container', options[0].container);
27 }
28
29 // Best guess at where the kiwi server is
30 this.detectKiwiServer();
31 };
32
33 this.start = function () {
34 // Only debug if set in the querystring
35 if (!getQueryVariable('debug')) {
36 manageDebug(false);
37 } else {
38 //manageDebug(true);
39 }
40
41 // Set the gateway up
42 kiwi.gateway = new kiwi.model.Gateway();
43 this.bindGatewayCommands(kiwi.gateway);
44
45 this.initializeClient();
46 this.initializeGlobals();
47
48 this.view.barsHide(true);
49
50 this.panels.server.server_login.bind('server_connect', function (event) {
51 var server_login = this;
52 auto_connect_details = event;
53
54 server_login.networkConnecting();
55
56 $script(that.kiwi_server + '/socket.io/socket.io.js?ts='+(new Date().getTime()), function () {
57 if (!window.io) {
58 kiwiServerNotFound();
59 return;
60 }
61 kiwi.gateway.set('kiwi_server', that.kiwi_server + '/kiwi');
62 kiwi.gateway.set('nick', event.nick);
63
64 kiwi.gateway.connect(event.server, event.port, event.ssl, event.password, function () {});
65 });
66 });
67
68 // TODO: Shouldn't really be here but it's not working in the view.. :/
69 // Hack for firefox browers: Focus is not given on this event loop iteration
70 setTimeout(function(){
71 kiwi.app.panels.server.server_login.$el.find('.nick').select();
72 }, 0);
73 };
74
75
76 function kiwiServerNotFound (e) {
77 that.panels.server.server_login.showError();
78 }
79
80
81 this.detectKiwiServer = function () {
82 // If running from file, default to localhost:7777 by default
83 if (window.location.protocol === 'file:') {
84 this.kiwi_server = 'http://localhost:7778';
85 } else {
86 // Assume the kiwi server is on the same server
87 this.kiwi_server = window.location.protocol + '//' + window.location.host;
88 }
89 };
90
91
92 this.initializeClient = function () {
93 this.view = new kiwi.view.Application({model: this, el: this.get('container')});
94
95 /**
96 * Set the UI components up
97 */
98 this.panels = new kiwi.model.PanelList();
99
100 this.controlbox = new kiwi.view.ControlBox({el: $('#controlbox')[0]});
101 this.bindControllboxCommands(this.controlbox);
102
103 this.topicbar = new kiwi.view.TopicBar({el: $('#topic')[0]});
104
105 this.message = new kiwi.view.StatusMessage({el: $('#status_message')[0]});
106
107 this.resize_handle = new kiwi.view.ResizeHandler({el: $('#memberlists_resize_handle')[0]});
108
109 this.panels.server.view.show();
110
111 // Rejigg the UI sizes
112 this.view.doLayout();
113
114 this.populateDefaultServerSettings();
115 };
116
117
118 this.initializeGlobals = function () {
119 kiwi.global.control = this.controlbox;
120 };
121
122
123 this.populateDefaultServerSettings = function () {
124 var parts;
125 var defaults = {
126 nick: getQueryVariable('nick') || 'kiwi_' + Math.ceil(Math.random() * 10000).toString(),
127 server: 'irc.kiwiirc.com',
128 port: 6667,
129 ssl: false,
130 channel: window.location.hash || '#kiwiirc'
131 };
132
133 // Process the URL part by part, extracting as we go
134 parts = window.location.pathname.toString().split('/');
135 parts.shift();
136
137 if (parts.length > 0 && parts[0].toLowerCase() === 'client') {
138 parts.shift();
139
140 if (parts.length > 0 && parts[0]) {
141 // TODO: Extract the port from this hostname
142 defaults.server = parts[0];
143 parts.shift();
144 }
145
146 if (parts.length > 0 && parts[0]) {
147 defaults.channel = '#' + parts[0];
148 parts.shift();
149 }
150 }
151
152 // Set any random numbers if needed
153 defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());
154
155 // Populate the server select box with defaults
156 this.panels.server.server_login.populateFields(defaults);
157 };
158
159
160 this.bindGatewayCommands = function (gw) {
161 gw.on('onmotd', function (event) {
162 that.panels.server.addMsg(kiwi.gateway.get('name'), event.msg, 'motd');
163 });
164
165
166 gw.on('onconnect', function (event) {
167 that.view.barsShow();
168
169 if (auto_connect_details.channel) {
170 that.controlbox.processInput('/JOIN ' + auto_connect_details.channel);
171 }
172 });
173
174
175 (function () {
176 var gw_stat = 0;
177
178 gw.on('disconnect', function (event) {
179 var msg = 'You have been disconnected. Attempting to reconnect for you..';
180 that.message.text(msg, {timeout: 10000});
181
182 // Mention the disconnection on every channel
183 $.each(kiwi.app.panels.models, function (idx, panel) {
184 if (!panel || !panel.isChannel()) return;
185 panel.addMsg('', msg, 'action quit');
186 });
187 kiwi.app.panels.server.addMsg('', msg, 'action quit');
188
189 gw_stat = 1;
190 });
191 gw.on('reconnecting', function (event) {
192 msg = 'You have been disconnected. Attempting to reconnect again in ' + (event.delay/1000) + ' seconds..';
193 kiwi.app.panels.server.addMsg('', msg, 'action quit');
194 });
195 gw.on('connect', function (event) {
196 if (gw_stat !== 1) return;
197
198 var msg = 'It\'s OK, you\'re connected again :)';
199 that.message.text(msg, {timeout: 5000});
200
201 // Mention the disconnection on every channel
202 $.each(kiwi.app.panels.models, function (idx, panel) {
203 if (!panel || !panel.isChannel()) return;
204 panel.addMsg('', msg, 'action join');
205 });
206 kiwi.app.panels.server.addMsg('', msg, 'action join');
207
208 gw_stat = 0;
209 });
210 })();
211
212
213 gw.on('onjoin', function (event) {
214 var c, members, user;
215 c = that.panels.getByName(event.channel);
216 if (!c) {
217 c = new kiwi.model.Channel({name: event.channel});
218 that.panels.add(c);
219 }
220
221 members = c.get('members');
222 if (!members) return;
223
224 user = new kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname});
225 members.add(user);
226 // TODO: highlight the new channel in some way
227 });
228
229
230 gw.on('onpart', function (event) {
231 var channel, members, user,
232 part_options = {};
233
234 part_options.type = 'part';
235 part_options.message = event.message || '';
236
237 channel = that.panels.getByName(event.channel);
238 if (!channel) return;
239
240 // If this is us, close the panel
241 if (event.nick === kiwi.gateway.get('nick')) {
242 channel.close();
243 return;
244 }
245
246 members = channel.get('members');
247 if (!members) return;
248
249 user = members.getByNick(event.nick);
250 if (!user) return;
251
252 members.remove(user, part_options);
253 });
254
255
256 gw.on('onquit', function (event) {
257 var member, members,
258 quit_options = {};
259
260 quit_options.type = 'quit';
261 quit_options.message = event.message || '';
262
263 $.each(that.panels.models, function (index, panel) {
264 if (!panel.isChannel()) return;
265
266 member = panel.get('members').getByNick(event.nick);
267 if (member) {
268 panel.get('members').remove(member, quit_options);
269 }
270 });
271 });
272
273
274 gw.on('onkick', function (event) {
275 var channel, members, user,
276 part_options = {};
277
278 part_options.type = 'kick';
279 part_options.by = event.nick;
280 part_options.message = event.message || '';
281
282 channel = that.panels.getByName(event.channel);
283 if (!channel) return;
284
285 members = channel.get('members');
286 if (!members) return;
287
288 user = members.getByNick(event.kicked);
289 if (!user) return;
290
291 members.remove(user, part_options);
292
293 if (event.kicked === kiwi.gateway.get('nick')) {
294 members.reset([]);
295 }
296
297 });
298
299
300 gw.on('onmsg', function (event) {
301 var panel,
302 is_pm = (event.channel == kiwi.gateway.get('nick'));
303
304 if (is_pm) {
305 // If a panel isn't found for this PM, create one
306 panel = that.panels.getByName(event.nick);
307 if (!panel) {
308 panel = new kiwi.model.Channel({name: event.nick});
309 that.panels.add(panel);
310 }
311
312 } else {
313 // If a panel isn't found for this channel, reroute to the
314 // server panel
315 panel = that.panels.getByName(event.channel);
316 if (!panel) {
317 panel = that.panels.server;
318 }
319 }
320
321 panel.addMsg(event.nick, event.msg);
322 });
323
324
325 gw.on('onnotice', function (event) {
326 var panel;
327
328 // Find a panel for the destination(channel) or who its from
329 panel = that.panels.getByName(event.target) || that.panels.getByName(event.nick);
330 if (!panel) {
331 panel = that.panels.server;
332 }
333
334 panel.addMsg('[' + (event.nick||'') + ']', event.msg);
335 });
336
337
338 gw.on('onaction', function (event) {
339 var panel,
340 is_pm = (event.channel == kiwi.gateway.get('nick'));
341
342 if (is_pm) {
343 // If a panel isn't found for this PM, create one
344 panel = that.panels.getByName(event.nick);
345 if (!panel) {
346 panel = new kiwi.model.Channel({name: event.nick});
347 that.panels.add(panel);
348 }
349
350 } else {
351 // If a panel isn't found for this channel, reroute to the
352 // server panel
353 panel = that.panels.getByName(event.channel);
354 if (!panel) {
355 panel = that.panels.server;
356 }
357 }
358
359 panel.addMsg('', '* ' + event.nick + ' ' + event.msg, 'action');
360 });
361
362
363 gw.on('ontopic', function (event) {
364 var c;
365 c = that.panels.getByName(event.channel);
366 if (!c) return;
367
368 // Set the channels topic
369 c.set('topic', event.topic);
370
371 // If this is the active channel, update the topic bar too
372 if (c.get('name') === kiwi.app.panels.active.get('name')) {
373 that.topicbar.setCurrentTopic(event.topic);
374 }
375 });
376
377
378 gw.on('ontopicsetby', function (event) {
379 var c, when;
380 c = that.panels.getByName(event.channel);
381 if (!c) return;
382
383 when = formatDate(new Date(event.when * 1000));
384 c.addMsg('', 'Topic set by ' + event.nick + ' at ' + when, 'topic');
385 });
386
387
388 gw.on('onuserlist', function (event) {
389 var channel;
390 channel = that.panels.getByName(event.channel);
391
392 // If we didn't find a channel for this, may aswell leave
393 if (!channel) return;
394
395 channel.temp_userlist = channel.temp_userlist || [];
396 _.each(event.users, function (item) {
397 var user = new kiwi.model.Member({nick: item.nick, modes: item.modes});
398 channel.temp_userlist.push(user);
399 });
400 });
401
402
403 gw.on('onuserlist_end', function (event) {
404 var channel;
405 channel = that.panels.getByName(event.channel);
406
407 // If we didn't find a channel for this, may aswell leave
408 if (!channel) return;
409
410 // Update the members list with the new list
411 channel.get('members').reset(channel.temp_userlist || []);
412
413 // Clear the temporary userlist
414 delete channel.temp_userlist;
415 });
416
417
418 gw.on('onmode', function (event) {
419 var channel, i, prefixes, members, member, find_prefix;
420
421 // Build a nicely formatted string to be displayed to a regular human
422 function friendlyModeString (event_modes, alt_target) {
423 var modes = {}, return_string;
424
425 // If no default given, use the main event info
426 if (!event_modes) {
427 event_modes = event.modes;
428 alt_target = event.target;
429 }
430
431 // Reformat the mode object to make it easier to work with
432 _.each(event_modes, function (mode){
433 var param = mode.param || alt_target || '';
434
435 // Make sure we have some modes for this param
436 if (!modes[param]) {
437 modes[param] = {'+':'', '-':''};
438 }
439
440 modes[param][mode.mode[0]] += mode.mode.substr(1);
441 });
442
443 // Put the string together from each mode
444 return_string = [];
445 _.each(modes, function (modeset, param) {
446 var str = '';
447 if (modeset['+']) str += '+' + modeset['+'];
448 if (modeset['-']) str += '-' + modeset['-'];
449 return_string.push(str + ' ' + param);
450 });
451 return_string = return_string.join(', ');
452
453 return return_string;
454 }
455
456
457 channel = that.panels.getByName(event.target);
458 if (channel) {
459 prefixes = kiwi.gateway.get('user_prefixes');
460 find_prefix = function (p) {
461 return event.modes[i].mode[1] === p.mode;
462 };
463 for (i = 0; i < event.modes.length; i++) {
464 if (_.any(prefixes, find_prefix)) {
465 if (!members) {
466 members = channel.get('members');
467 }
468 member = members.getByNick(event.modes[i].param);
469 if (!member) {
470 console.log('MODE command recieved for unknown member %s on channel %s', event.modes[i].param, event.target);
471 return;
472 } else {
473 if (event.modes[i].mode[0] === '+') {
474 member.addMode(event.modes[i].mode[1]);
475 } else if (event.modes[i].mode[0] === '-') {
476 member.removeMode(event.modes[i].mode[1]);
477 }
478 members.sort();
479 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
480 }
481 } else {
482 // Channel mode being set
483 // TODO: Store this somewhere?
484 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');
485 }
486 }
487
488 channel.addMsg('', '== ' + event.nick + ' sets mode ' + friendlyModeString(), 'action mode');
489 } else {
490 // This is probably a mode being set on us.
491 if (event.target.toLowerCase() === kiwi.gateway.get("nick").toLowerCase()) {
492 that.panels.server.addMsg('', '== ' + event.nick + ' set mode ' + friendlyModeString(), 'action mode');
493 } else {
494 console.log('MODE command recieved for unknown target %s: ', event.target, event);
495 }
496 }
497 });
498
499
500 gw.on('onnick', function (event) {
501 var member;
502
503 $.each(that.panels.models, function (index, panel) {
504 if (!panel.isChannel()) return;
505
506 member = panel.get('members').getByNick(event.nick);
507 if (member) {
508 member.set('nick', event.newnick);
509 panel.addMsg('', '== ' + event.nick + ' is now known as ' + event.newnick, 'action nick');
510 }
511 });
512 });
513
514
515 gw.on('onwhois', function (event) {
516 /*globals secondsToTime */
517 var logon_date, idle_time = '', panel;
518
519 if (event.end) {
520 return;
521 }
522
523 if (typeof event.idle !== 'undefined') {
524 idle_time = secondsToTime(parseInt(event.idle, 10));
525 idle_time = idle_time.h.toString().lpad(2, "0") + ':' + idle_time.m.toString().lpad(2, "0") + ':' + idle_time.s.toString().lpad(2, "0");
526 }
527
528 panel = kiwi.app.panels.active;
529 if (event.ident) {
530 panel.addMsg(event.nick, 'is ' + event.nick + '!' + event.ident + '@' + event.host + ' * ' + event.msg, 'whois');
531 } else if (event.chans) {
532 panel.addMsg(event.nick, 'on ' + event.chans, 'whois');
533 } else if (event.server) {
534 panel.addMsg(event.nick, 'using ' + event.server, 'whois');
535 } else if (event.msg) {
536 panel.addMsg(event.nick, event.msg, 'whois');
537 } else if (event.logon) {
538 logon_date = new Date();
539 logon_date.setTime(event.logon * 1000);
540 logon_date = formatDate(logon_date);
541
542 panel.addMsg(event.nick, 'idle for ' + idle_time + ', signed on ' + logon_date, 'whois');
543 } else {
544 panel.addMsg(event.nick, 'idle for ' + idle_time, 'whois');
545 }
546 });
547
548
549 gw.on('onlist_start', function (data) {
550 if (kiwi.app.channel_list) {
551 kiwi.app.channel_list.close();
552 delete kiwi.app.channel_list;
553 }
554
555 var panel = new kiwi.model.Applet(),
556 applet = new kiwi.applets.Chanlist();
557
558 panel.load(applet);
559
560 kiwi.app.panels.add(panel);
561 panel.view.show();
562
563 kiwi.app.channel_list = applet;
564 });
565
566
567 gw.on('onlist_channel', function (data) {
568 // TODO: Put this listener within the applet itself
569 kiwi.app.channel_list.addChannel(data.chans);
570 });
571
572
573 gw.on('onlist_end', function (data) {
574 // TODO: Put this listener within the applet itself
575 delete kiwi.app.channel_list;
576 });
577
578
579 gw.on('onirc_error', function (data) {
580 var panel, tmp;
581
582 if (data.channel !== undefined && !(panel = kiwi.app.panels.getByName(data.channel))) {
583 panel = kiwi.app.panels.server;
584 }
585
586 switch (data.error) {
587 case 'banned_from_channel':
588 panel.addMsg(' ', '== You are banned from ' + data.channel + '. ' + data.reason, 'status');
589 kiwi.app.message.text('You are banned from ' + data.channel + '. ' + data.reason);
590 break;
591 case 'bad_channel_key':
592 panel.addMsg(' ', '== Bad channel key for ' + data.channel, 'status');
593 kiwi.app.message.text('Bad channel key or password for ' + data.channel);
594 break;
595 case 'invite_only_channel':
596 panel.addMsg(' ', '== ' + data.channel + ' is invite only.', 'status');
597 kiwi.app.message.text(data.channel + ' is invite only');
598 break;
599 case 'channel_is_full':
600 panel.addMsg(' ', '== ' + data.channel + ' is full.', 'status');
601 kiwi.app.message.text(data.channel + ' is full');
602 break;
603 case 'chanop_privs_needed':
604 panel.addMsg(' ', '== ' + data.reason, 'status');
605 kiwi.app.message.text(data.reason + ' (' + data.channel + ')');
606 break;
607 case 'no_such_nick':
608 tmp = kiwi.app.panels.getByName(data.nick);
609 if (tmp) {
610 tmp.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status');
611 } else {
612 kiwi.app.panels.server.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status');
613 }
614 break;
615 case 'nickname_in_use':
616 kiwi.app.panels.server.addMsg(' ', '== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status');
617 if (kiwi.app.panels.server !== kiwi.app.panels.active) {
618 kiwi.app.message.text('The nickname "' + data.nick + '" is already in use. Please select a new nickname');
619 }
620
621 // Only show the nickchange component if the controlbox is open
622 if (that.controlbox.$el.css('display') !== 'none') {
623 (new kiwi.view.NickChangeBox()).render();
624 }
625
626 break;
627 default:
628 // We don't know what data contains, so don't do anything with it.
629 //kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');
630 }
631 });
632 };
633
634
635
636 /**
637 * Bind to certain commands that may be typed into the control box
638 */
639 this.bindControllboxCommands = function (controlbox) {
640 // Default aliases
641 $.extend(controlbox.preprocessor.aliases, {
642 '/p': '/part $1+',
643 '/me': '/action $1+',
644 '/j': '/join $1+',
645 '/q': '/query $1+',
646 '/k': '/kick $1+',
647
648 '/slap': '/me throws the juciest, sweetest kiwi at $1. Hits right in the kisser!',
649 '/throw': '/slap $1+'
650 });
651
652 controlbox.on('unknown_command', unknownCommand);
653
654 controlbox.on('command', allCommands);
655 controlbox.on('command_msg', msgCommand);
656
657 controlbox.on('command_action', actionCommand);
658
659 controlbox.on('command_join', joinCommand);
660
661 controlbox.on('command_part', partCommand);
662
663 controlbox.on('command_nick', function (ev) {
664 kiwi.gateway.changeNick(ev.params[0]);
665 });
666
667 controlbox.on('command_query', queryCommand);
668
669 controlbox.on('command_topic', topicCommand);
670
671 controlbox.on('command_notice', noticeCommand);
672
673 controlbox.on('command_quote', quoteCommand);
674
675 controlbox.on('command_kick', kickCommand);
676
677
678 controlbox.on('command_css', function (ev) {
679 var queryString = '?reload=' + new Date().getTime();
680 $('link[rel="stylesheet"]').each(function () {
681 this.href = this.href.replace(/\?.*|$/, queryString);
682 });
683 });
684
685 controlbox.on('command_js', function (ev) {
686 if (!ev.params[0]) return;
687 $script(ev.params[0] + '?' + (new Date().getTime()));
688 });
689
690 controlbox.on('command_alias', function (ev) {
691 var name, rule;
692
693 // No parameters passed so list them
694 if (!ev.params[1]) {
695 $.each(controlbox.preprocessor.aliases, function (name, rule) {
696 kiwi.app.panels.server.addMsg(' ', name + ' => ' + rule);
697 });
698 return;
699 }
700
701 // Deleting an alias?
702 if (ev.params[0] === 'del' || ev.params[0] === 'delete') {
703 name = ev.params[1];
704 if (name[0] !== '/') name = '/' + name;
705 delete controlbox.preprocessor.aliases[name];
706 return;
707 }
708
709 // Add the alias
710 name = ev.params[0];
711 ev.params.shift();
712 rule = ev.params.join(' ');
713
714 // Make sure the name starts with a slash
715 if (name[0] !== '/') name = '/' + name;
716
717 // Now actually add the alias
718 controlbox.preprocessor.aliases[name] = rule;
719 });
720
721 controlbox.on('command_applet', appletCommand);
722 controlbox.on('command_settings', settingsCommand);
723 };
724
725 // A fallback action. Send a raw command to the server
726 function unknownCommand (ev) {
727 var raw_cmd = ev.command + ' ' + ev.params.join(' ');
728 console.log('RAW: ' + raw_cmd);
729 kiwi.gateway.raw(raw_cmd);
730 }
731
732 function allCommands (ev) {}
733
734 function joinCommand (ev) {
735 var channel, channel_names;
736
737 channel_names = ev.params.join(' ').split(',');
738
739 $.each(channel_names, function (index, channel_name) {
740 // Trim any whitespace off the name
741 channel_name = channel_name.trim();
742
743 // Check if we have the panel already. If not, create it
744 channel = that.panels.getByName(channel_name);
745 if (!channel) {
746 channel = new kiwi.model.Channel({name: channel_name});
747 kiwi.app.panels.add(channel);
748 }
749
750 kiwi.gateway.join(channel_name);
751 });
752
753 if (channel) channel.view.show();
754
755 }
756
757 function queryCommand (ev) {
758 var destination, panel;
759
760 destination = ev.params[0];
761
762 // Check if we have the panel already. If not, create it
763 panel = that.panels.getByName(destination);
764 if (!panel) {
765 panel = new kiwi.model.Channel({name: destination});
766 kiwi.app.panels.add(panel);
767 }
768
769 if (panel) panel.view.show();
770
771 }
772
773 function msgCommand (ev) {
774 var destination = ev.params[0],
775 panel = that.panels.getByName(destination) || that.panels.server;
776
777 ev.params.shift();
778
779 panel.addMsg(kiwi.gateway.get('nick'), ev.params.join(' '));
780 kiwi.gateway.privmsg(destination, ev.params.join(' '));
781 }
782
783 function actionCommand (ev) {
784 if (kiwi.app.panels.active === kiwi.app.panels.server) {
785 return;
786 }
787
788 var panel = kiwi.app.panels.active;
789 panel.addMsg('', '* ' + kiwi.gateway.get('nick') + ' ' + ev.params.join(' '), 'action');
790 kiwi.gateway.action(panel.get('name'), ev.params.join(' '));
791 }
792
793 function partCommand (ev) {
794 if (ev.params.length === 0) {
795 kiwi.gateway.part(kiwi.app.panels.active.get('name'));
796 } else {
797 _.each(ev.params, function (channel) {
798 kiwi.gateway.part(channel);
799 });
800 }
801 // TODO: More responsive = close tab now, more accurate = leave until part event
802 //kiwi.app.panels.remove(kiwi.app.panels.active);
803 }
804
805 function topicCommand (ev) {
806 var channel_name;
807
808 if (ev.params.length === 0) return;
809
810 if (that.isChannelName(ev.params[0])) {
811 channel_name = ev.params[0];
812 ev.params.shift();
813 } else {
814 channel_name = kiwi.app.panels.active.get('name');
815 }
816
817 kiwi.gateway.topic(channel_name, ev.params.join(' '));
818 }
819
820 function noticeCommand (ev) {
821 var destination;
822
823 // Make sure we have a destination and some sort of message
824 if (ev.params.length <= 1) return;
825
826 destination = ev.params[0];
827 ev.params.shift();
828
829 kiwi.gateway.notice(destination, ev.params.join(' '));
830 }
831
832 function quoteCommand (ev) {
833 var raw = ev.params.join(' ');
834 kiwi.gateway.raw(raw);
835 }
836
837 function kickCommand (ev) {
838 var nick, panel = kiwi.app.panels.active;
839
840 if (!panel.isChannel()) return;
841
842 // Make sure we have a nick
843 if (ev.params.length === 0) return;
844
845 nick = ev.params[0];
846 ev.params.shift();
847
848 kiwi.gateway.kick(panel.get('name'), nick, ev.params.join(' '));
849 }
850
851 function settingsCommand (ev) {
852 var panel = new kiwi.model.Applet();
853 panel.load(new kiwi.applets.Settings());
854
855 kiwi.app.panels.add(panel);
856 panel.view.show();
857 }
858
859 function appletCommand (ev) {
860 if (!ev.params[0]) return;
861
862 var panel = new kiwi.model.Applet();
863
864 if (ev.params[1]) {
865 // Url and name given
866 panel.load(ev.params[0], ev.params[1]);
867 } else {
868 // Load a pre-loaded applet
869 if (kiwi.applets[ev.params[0]]) {
870 panel.load(new kiwi.applets[ev.params[0]]());
871 } else {
872 kiwi.app.panels.server.addMsg('', 'Applet "' + ev.params[0] + '" does not exist');
873 return;
874 }
875 }
876
877 kiwi.app.panels.add(panel);
878 panel.view.show();
879 }
880
881
882
883
884
885 this.isChannelName = function (channel_name) {
886 var channel_prefix = kiwi.gateway.get('channel_prefix');
887
888 if (!channel_name || !channel_name.length) return false;
889 return (channel_prefix.indexOf(channel_name[0]) > -1);
890 };
891
892 };
893
894
895 model = Backbone.Model.extend(new model());
896
897 return new model(arguments);
898 };