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