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