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