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