1 _kiwi
.model
.Application = function () {
2 // Set to a reference to this object within initialize()
5 // The auto connect details entered into the server select box
6 var auto_connect_details
= {};
9 var model = function () {
10 /** Instance of _kiwi.model.PanelList */
13 /** _kiwi.view.Application */
16 /** _kiwi.view.StatusMessage */
19 /* Address for the kiwi server */
20 this.kiwi_server
= null;
22 this.initialize = function (options
) {
25 if (options
[0].container
) {
26 this.set('container', options
[0].container
);
29 // The base url to the kiwi server
30 this.set('base_path', options
[0].base_path
? options
[0].base_path
: '/kiwi');
32 // Any options sent down from the server
33 this.server_settings
= options
[0].server_settings
|| {};
35 // Best guess at where the kiwi server is
36 this.detectKiwiServer();
39 this.start = function () {
40 // Only debug if set in the querystring
41 if (!getQueryVariable('debug')) {
48 _kiwi
.gateway
= new _kiwi
.model
.Gateway();
49 this.bindGatewayCommands(_kiwi
.gateway
);
51 this.initializeClient();
52 this.initializeGlobals();
54 this.view
.barsHide(true);
56 this.panels
.server
.server_login
.bind('server_connect', function (event
) {
57 var server_login
= this,
59 auto_connect_details
= event
;
61 server_login
.networkConnecting();
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());
66 $script(transport_path
, function () {
71 _kiwi
.gateway
.set('kiwi_server', that
.kiwi_server
+ '/kiwi');
72 _kiwi
.gateway
.set('nick', event
.nick
);
74 _kiwi
.gateway
.connect(event
.server
, event
.port
, event
.ssl
, event
.password
, function (error
) {
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();
90 function kiwiServerNotFound (e
) {
91 that
.panels
.server
.server_login
.showError();
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';
100 // Assume the kiwi server is on the same server
101 this.kiwi_server
= window
.location
.protocol
+ '//' + window
.location
.host
;
106 this.initializeClient = function () {
107 this.view
= new _kiwi
.view
.Application({model
: this, el
: this.get('container')});
110 * Set the UI components up
112 this.panels
= new _kiwi
.model
.PanelList();
114 this.controlbox
= new _kiwi
.view
.ControlBox({el
: $('#controlbox')[0]});
115 this.bindControllboxCommands(this.controlbox
);
117 this.topicbar
= new _kiwi
.view
.TopicBar({el
: $('#topic')[0]});
119 new _kiwi
.view
.AppToolbar({el
: $('#toolbar .app_tools')[0]});
121 this.message
= new _kiwi
.view
.StatusMessage({el
: $('#status_message')[0]});
123 this.resize_handle
= new _kiwi
.view
.ResizeHandler({el
: $('#memberlists_resize_handle')[0]});
125 this.panels
.server
.view
.show();
127 // Rejigg the UI sizes
128 this.view
.doLayout();
130 this.populateDefaultServerSettings();
134 this.initializeGlobals = function () {
135 _kiwi
.global
.control
= this.controlbox
;
136 _kiwi
.global
.gateway
= _kiwi
.gateway
;
137 _kiwi
.global
.panels
= this.panels
;
139 _kiwi
.global
.components
= {
140 Applet
: _kiwi
.model
.Applet
,
141 Panel
: _kiwi
.model
.Panel
146 this.populateDefaultServerSettings = function () {
149 nick
: getQueryVariable('nick') || 'kiwi_?',
150 server
: 'irc.kiwiirc.com',
153 channel
: window
.location
.hash
|| '#kiwiirc',
160 * Get any settings set by the server
161 * These settings may be changed in the server selection dialog
163 if (this.server_settings
.client
) {
164 if (this.server_settings
.client
.nick
)
165 defaults
.nick
= this.server_settings
.client
.nick
;
167 if (this.server_settings
.client
.server
)
168 defaults
.server
= this.server_settings
.client
.server
;
170 if (this.server_settings
.client
.port
)
171 defaults
.port
= this.server_settings
.client
.port
;
173 if (this.server_settings
.client
.ssl
)
174 defaults
.ssl
= this.server_settings
.client
.ssl
;
176 if (this.server_settings
.client
.channel
)
177 defaults
.channel
= this.server_settings
.client
.channel
;
183 * Get any settings passed in the URL
184 * These settings may be changed in the server selection dialog
187 // Process the URL part by part, extracting as we go
188 parts
= window
.location
.pathname
.toString().replace(this.get('base_path'), '').split('/');
190 if (parts
.length
> 0) {
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]);
201 uricheck[1] = ssl (optional)
203 uricheck[3] = port (optional)
204 uricheck[4] = channel (optional)
205 uricheck[5] = channel key (optional, channel must also be set)
208 if (typeof uricheck
[1] !== 'undefined') {
210 if (defaults
.port
=== 6667) {
211 defaults
.port
= 6697;
214 defaults
.server
= uricheck
[2];
215 if (typeof uricheck
[3] !== 'undefined') {
216 defaults
.port
= uricheck
[3];
218 if (typeof uricheck
[4] !== 'undefined') {
219 defaults
.channel
= '#' + uricheck
[4];
220 if (typeof uricheck
[5] !== 'undefined') {
221 defaults
.channel_key
= uricheck
[5];
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);
235 defaults
.ssl
= false;
239 defaults
.server
= parts
[0];
246 if (parts
.length
> 0 && parts
[0]) {
247 defaults
.channel
= '#' + parts
[0];
252 // If any settings have been given by the server.. override any auto detected settings
254 * Get any server restrictions as set in the server config
255 * These settings can not changed in the server selection dialog
257 if (this.server_settings
&& this.server_settings
.connection
) {
258 if (this.server_settings
.connection
.server
) {
259 defaults
.server
= this.server_settings
.connection
.server
;
262 if (this.server_settings
.connection
.port
) {
263 defaults
.port
= this.server_settings
.connection
.port
;
266 if (this.server_settings
.connection
.ssl
) {
267 defaults
.ssl
= this.server_settings
.connection
.ssl
;
270 if (this.server_settings
.connection
.channel
) {
271 defaults
.channel
= this.server_settings
.connection
.channel
;
274 if (this.server_settings
.connection
.nick
) {
275 defaults
.nick
= this.server_settings
.connection
.nick
;
279 // Set any random numbers if needed
280 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
282 // Populate the server select box with defaults
283 this.panels
.server
.server_login
.populateFields(defaults
);
287 this.bindGatewayCommands = function (gw
) {
288 gw
.on('onmotd', function (event
) {
289 that
.panels
.server
.addMsg(_kiwi
.gateway
.get('name'), event
.msg
, 'motd');
293 gw
.on('onconnect', function (event
) {
294 that
.view
.barsShow();
296 if (auto_connect_details
.channel
) {
297 that
.controlbox
.processInput('/JOIN ' + auto_connect_details
.channel
+ ' ' + auto_connect_details
.channel_key
);
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});
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');
314 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action quit');
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');
322 gw
.on('connect', function (event
) {
323 if (gw_stat
!== 1) return;
325 var msg
= 'It\'s OK, you\'re connected again :)';
326 that
.message
.text(msg
, {timeout
: 5000});
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');
333 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action join');
340 gw
.on('onjoin', function (event
) {
341 var c
, members
, user
;
342 c
= that
.panels
.getByName(event
.channel
);
344 c
= new _kiwi
.model
.Channel({name
: event
.channel
});
348 members
= c
.get('members');
349 if (!members
) return;
351 user
= new _kiwi
.model
.Member({nick
: event
.nick
, ident
: event
.ident
, hostname
: event
.hostname
});
353 // TODO: highlight the new channel in some way
357 gw
.on('onpart', function (event
) {
358 var channel
, members
, user
,
361 part_options
.type
= 'part';
362 part_options
.message
= event
.message
|| '';
364 channel
= that
.panels
.getByName(event
.channel
);
365 if (!channel
) return;
367 // If this is us, close the panel
368 if (event
.nick
=== _kiwi
.gateway
.get('nick')) {
373 members
= channel
.get('members');
374 if (!members
) return;
376 user
= members
.getByNick(event
.nick
);
379 members
.remove(user
, part_options
);
383 gw
.on('onquit', function (event
) {
387 quit_options
.type
= 'quit';
388 quit_options
.message
= event
.message
|| '';
390 $.each(that
.panels
.models
, function (index
, panel
) {
391 if (!panel
.isChannel()) return;
393 member
= panel
.get('members').getByNick(event
.nick
);
395 panel
.get('members').remove(member
, quit_options
);
401 gw
.on('onkick', function (event
) {
402 var channel
, members
, user
,
405 part_options
.type
= 'kick';
406 part_options
.by
= event
.nick
;
407 part_options
.message
= event
.message
|| '';
409 channel
= that
.panels
.getByName(event
.channel
);
410 if (!channel
) return;
412 members
= channel
.get('members');
413 if (!members
) return;
415 user
= members
.getByNick(event
.kicked
);
418 members
.remove(user
, part_options
);
420 if (event
.kicked
=== _kiwi
.gateway
.get('nick')) {
427 gw
.on('onmsg', function (event
) {
429 is_pm
= (event
.channel
== _kiwi
.gateway
.get('nick'));
432 // If a panel isn't found for this PM, create one
433 panel
= that
.panels
.getByName(event
.nick
);
435 panel
= new _kiwi
.model
.Query({name
: event
.nick
});
436 that
.panels
.add(panel
);
440 // If a panel isn't found for this channel, reroute to the
442 panel
= that
.panels
.getByName(event
.channel
);
444 panel
= that
.panels
.server
;
448 panel
.addMsg(event
.nick
, event
.msg
);
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());
460 gw
.on('onnotice', function (event
) {
463 // Find a panel for the destination(channel) or who its from
464 panel
= that
.panels
.getByName(event
.target
) || that
.panels
.getByName(event
.nick
);
466 panel
= that
.panels
.server
;
469 panel
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
473 gw
.on('onaction', function (event
) {
475 is_pm
= (event
.channel
== _kiwi
.gateway
.get('nick'));
478 // If a panel isn't found for this PM, create one
479 panel
= that
.panels
.getByName(event
.nick
);
481 panel
= new _kiwi
.model
.Channel({name
: event
.nick
});
482 that
.panels
.add(panel
);
486 // If a panel isn't found for this channel, reroute to the
488 panel
= that
.panels
.getByName(event
.channel
);
490 panel
= that
.panels
.server
;
494 panel
.addMsg('', '* ' + event
.nick
+ ' ' + event
.msg
, 'action');
498 gw
.on('ontopic', function (event
) {
500 c
= that
.panels
.getByName(event
.channel
);
503 // Set the channels topic
504 c
.set('topic', event
.topic
);
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
);
513 gw
.on('ontopicsetby', function (event
) {
515 c
= that
.panels
.getByName(event
.channel
);
518 when
= formatDate(new Date(event
.when
* 1000));
519 c
.addMsg('', 'Topic set by ' + event
.nick
+ ' at ' + when
, 'topic');
523 gw
.on('onuserlist', function (event
) {
525 channel
= that
.panels
.getByName(event
.channel
);
527 // If we didn't find a channel for this, may aswell leave
528 if (!channel
) return;
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
);
538 gw
.on('onuserlist_end', function (event
) {
540 channel
= that
.panels
.getByName(event
.channel
);
542 // If we didn't find a channel for this, may aswell leave
543 if (!channel
) return;
545 // Update the members list with the new list
546 channel
.get('members').reset(channel
.temp_userlist
|| []);
548 // Clear the temporary userlist
549 delete channel
.temp_userlist
;
553 gw
.on('onmode', function (event
) {
554 var channel
, i
, prefixes
, members
, member
, find_prefix
;
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
;
560 // If no default given, use the main event info
562 event_modes
= event
.modes
;
563 alt_target
= event
.target
;
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
|| '';
570 // Make sure we have some modes for this param
572 modes
[param
] = {'+':'', '-':''};
575 modes
[param
][mode
.mode
[0]] += mode
.mode
.substr(1);
578 // Put the string together from each mode
580 _
.each(modes
, function (modeset
, param
) {
582 if (modeset
['+']) str
+= '+' + modeset
['+'];
583 if (modeset
['-']) str
+= '-' + modeset
['-'];
584 return_string
.push(str
+ ' ' + param
);
586 return_string
= return_string
.join(', ');
588 return return_string
;
592 channel
= that
.panels
.getByName(event
.target
);
594 prefixes
= _kiwi
.gateway
.get('user_prefixes');
595 find_prefix = function (p
) {
596 return event
.modes
[i
].mode
[1] === p
.mode
;
598 for (i
= 0; i
< event
.modes
.length
; i
++) {
599 if (_
.any(prefixes
, find_prefix
)) {
601 members
= channel
.get('members');
603 member
= members
.getByNick(event
.modes
[i
].param
);
605 console
.log('MODE command recieved for unknown member %s on channel %s', event
.modes
[i
].param
, event
.target
);
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]);
614 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
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');
623 channel
.addMsg('', '== ' + event
.nick
+ ' sets mode ' + friendlyModeString(), 'action mode');
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');
629 console
.log('MODE command recieved for unknown target %s: ', event
.target
, event
);
635 gw
.on('onnick', function (event
) {
638 $.each(that
.panels
.models
, function (index
, panel
) {
639 if (!panel
.isChannel()) return;
641 member
= panel
.get('members').getByNick(event
.nick
);
643 member
.set('nick', event
.newnick
);
644 panel
.addMsg('', '== ' + event
.nick
+ ' is now known as ' + event
.newnick
, 'action nick');
650 gw
.on('onwhois', function (event
) {
651 /*globals secondsToTime */
652 var logon_date
, idle_time
= '', panel
;
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");
663 panel
= _kiwi
.app
.panels
.active
;
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
);
677 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
+ ', signed on ' + logon_date
, 'whois');
679 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
, 'whois');
683 gw
.on('onaway', function (event
) {
684 $.each(that
.panels
.models
, function (index
, panel
) {
685 if (!panel
.isChannel()) return;
687 member
= panel
.get('members').getByNick(event
.nick
);
689 member
.set('away', !(!event
.trailing
));
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
;
701 var panel
= new _kiwi
.model
.Applet(),
702 applet
= new _kiwi
.applets
.Chanlist();
706 _kiwi
.app
.panels
.add(panel
);
709 _kiwi
.app
.channel_list
= applet
;
713 gw
.on('onlist_channel', function (data
) {
714 // TODO: Put this listener within the applet itself
715 _kiwi
.app
.channel_list
.addChannel(data
.chans
);
719 gw
.on('onlist_end', function (data
) {
720 // TODO: Put this listener within the applet itself
721 delete _kiwi
.app
.channel_list
;
725 gw
.on('onirc_error', function (data
) {
728 if (data
.channel
!== undefined && !(panel
= _kiwi
.app
.panels
.getByName(data
.channel
))) {
729 panel
= _kiwi
.app
.panels
.server
;
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
);
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
);
741 case 'invite_only_channel':
742 panel
.addMsg(' ', '== ' + data
.channel
+ ' is invite only.', 'status');
743 _kiwi
.app
.message
.text(data
.channel
+ ' is invite only');
745 case 'channel_is_full':
746 panel
.addMsg(' ', '== ' + data
.channel
+ ' is full.', 'status');
747 _kiwi
.app
.message
.text(data
.channel
+ ' is full');
749 case 'chanop_privs_needed':
750 panel
.addMsg(' ', '== ' + data
.reason
, 'status');
751 _kiwi
.app
.message
.text(data
.reason
+ ' (' + data
.channel
+ ')');
754 tmp
= _kiwi
.app
.panels
.getByName(data
.nick
);
756 tmp
.addMsg(' ', '== ' + data
.nick
+ ': ' + data
.reason
, 'status');
758 _kiwi
.app
.panels
.server
.addMsg(' ', '== ' + data
.nick
+ ': ' + data
.reason
, 'status');
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');
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();
774 // We don't know what data contains, so don't do anything with it.
775 //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');
783 * Bind to certain commands that may be typed into the control box
785 this.bindControllboxCommands = function (controlbox
) {
787 $.extend(controlbox
.preprocessor
.aliases
, {
790 '/me': '/action $1+',
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+',
804 '/slap': '/me slaps $1 around a bit with a large trout'
807 controlbox
.on('unknown_command', unknownCommand
);
809 controlbox
.on('command', allCommands
);
810 controlbox
.on('command_msg', msgCommand
);
812 controlbox
.on('command_action', actionCommand
);
814 controlbox
.on('command_join', joinCommand
);
816 controlbox
.on('command_part', partCommand
);
818 controlbox
.on('command_nick', function (ev
) {
819 _kiwi
.gateway
.changeNick(ev
.params
[0]);
822 controlbox
.on('command_query', queryCommand
);
824 controlbox
.on('command_topic', topicCommand
);
826 controlbox
.on('command_notice', noticeCommand
);
828 controlbox
.on('command_quote', quoteCommand
);
830 controlbox
.on('command_kick', kickCommand
);
832 controlbox
.on('command_clear', clearCommand
);
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
);
842 controlbox
.on('command_js', function (ev
) {
843 if (!ev
.params
[0]) return;
844 $script(ev
.params
[0] + '?' + (new Date().getTime()));
848 controlbox
.on('command_set', function (ev
) {
849 if (!ev
.params
[0]) return;
851 var setting
= ev
.params
[0],
854 // Do we have a second param to set a value?
858 value
= ev
.params
.join(' ');
859 _kiwi
.global
.settings
.set(setting
, value
);
862 // Read the value to the user
863 _kiwi
.app
.panels
.active
.addMsg('', setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
));
867 controlbox
.on('command_save', function (ev
) {
868 _kiwi
.global
.settings
.save();
869 _kiwi
.app
.panels
.active
.addMsg('', 'Settings have been saved');
873 controlbox
.on('command_alias', function (ev
) {
876 // No parameters passed so list them
878 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
879 _kiwi
.app
.panels
.server
.addMsg(' ', name
+ ' => ' + rule
);
884 // Deleting an alias?
885 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
887 if (name
[0] !== '/') name
= '/' + name
;
888 delete controlbox
.preprocessor
.aliases
[name
];
895 rule
= ev
.params
.join(' ');
897 // Make sure the name starts with a slash
898 if (name
[0] !== '/') name
= '/' + name
;
900 // Now actually add the alias
901 controlbox
.preprocessor
.aliases
[name
] = rule
;
904 controlbox
.on('command_applet', appletCommand
);
905 controlbox
.on('command_settings', settingsCommand
);
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
);
915 function allCommands (ev
) {}
917 function joinCommand (ev
) {
918 var channel
, channel_names
;
920 channel_names
= ev
.params
.join(' ').split(',');
922 $.each(channel_names
, function (index
, channel_name
) {
923 // Trim any whitespace off the name
924 channel_name
= channel_name
.trim();
926 // Check if we have the panel already. If not, create it
927 channel
= that
.panels
.getByName(channel_name
);
929 channel
= new _kiwi
.model
.Channel({name
: channel_name
});
930 _kiwi
.app
.panels
.add(channel
);
933 _kiwi
.gateway
.join(channel_name
);
936 if (channel
) channel
.view
.show();
940 function queryCommand (ev
) {
941 var destination
, panel
;
943 destination
= ev
.params
[0];
945 // Check if we have the panel already. If not, create it
946 panel
= that
.panels
.getByName(destination
);
948 panel
= new _kiwi
.model
.Query({name
: destination
});
949 _kiwi
.app
.panels
.add(panel
);
952 if (panel
) panel
.view
.show();
956 function msgCommand (ev
) {
957 var destination
= ev
.params
[0],
958 panel
= that
.panels
.getByName(destination
) || that
.panels
.server
;
962 panel
.addMsg(_kiwi
.gateway
.get('nick'), ev
.params
.join(' '));
963 _kiwi
.gateway
.privmsg(destination
, ev
.params
.join(' '));
966 function actionCommand (ev
) {
967 if (_kiwi
.app
.panels
.active
=== _kiwi
.app
.panels
.server
) {
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(' '));
976 function partCommand (ev
) {
977 if (ev
.params
.length
=== 0) {
978 _kiwi
.gateway
.part(_kiwi
.app
.panels
.active
.get('name'));
980 _
.each(ev
.params
, function (channel
) {
981 _kiwi
.gateway
.part(channel
);
984 // TODO: More responsive = close tab now, more accurate = leave until part event
985 //_kiwi.app.panels.remove(_kiwi.app.panels.active);
988 function topicCommand (ev
) {
991 if (ev
.params
.length
=== 0) return;
993 if (that
.isChannelName(ev
.params
[0])) {
994 channel_name
= ev
.params
[0];
997 channel_name
= _kiwi
.app
.panels
.active
.get('name');
1000 _kiwi
.gateway
.topic(channel_name
, ev
.params
.join(' '));
1003 function noticeCommand (ev
) {
1006 // Make sure we have a destination and some sort of message
1007 if (ev
.params
.length
<= 1) return;
1009 destination
= ev
.params
[0];
1012 _kiwi
.gateway
.notice(destination
, ev
.params
.join(' '));
1015 function quoteCommand (ev
) {
1016 var raw
= ev
.params
.join(' ');
1017 _kiwi
.gateway
.raw(raw
);
1020 function kickCommand (ev
) {
1021 var nick
, panel
= _kiwi
.app
.panels
.active
;
1023 if (!panel
.isChannel()) return;
1025 // Make sure we have a nick
1026 if (ev
.params
.length
=== 0) return;
1028 nick
= ev
.params
[0];
1031 _kiwi
.gateway
.kick(panel
.get('name'), nick
, ev
.params
.join(' '));
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()) {
1040 if (_kiwi
.app
.panels
.active
.clearMessages
) {
1041 _kiwi
.app
.panels
.active
.clearMessages();
1045 function settingsCommand (ev
) {
1046 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
1047 settings
.view
.show();
1050 function appletCommand (ev
) {
1051 if (!ev
.params
[0]) return;
1053 var panel
= new _kiwi
.model
.Applet();
1056 // Url and name given
1057 panel
.load(ev
.params
[0], ev
.params
[1]);
1059 // Load a pre-loaded applet
1060 if (_kiwi
.applets
[ev
.params
[0]]) {
1061 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
1063 _kiwi
.app
.panels
.server
.addMsg('', 'Applet "' + ev
.params
[0] + '" does not exist');
1068 _kiwi
.app
.panels
.add(panel
);
1076 this.isChannelName = function (channel_name
) {
1077 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
1079 if (!channel_name
|| !channel_name
.length
) return false;
1080 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
1086 model
= Backbone
.Model
.extend(new model());
1088 return new model(arguments
);