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