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
.panels
= this.panels
;
137 _kiwi
.global
.components
.Applet
= _kiwi
.model
.Applet
;
138 _kiwi
.global
.components
.Panel
=_kiwi
.model
.Panel
;
142 this.populateDefaultServerSettings = function () {
145 nick
: getQueryVariable('nick') || '',
149 channel
: window
.location
.hash
|| '#chat',
156 * Get any settings set by the server
157 * These settings may be changed in the server selection dialog
159 if (this.server_settings
.client
) {
160 if (this.server_settings
.client
.nick
)
161 defaults
.nick
= this.server_settings
.client
.nick
;
163 if (this.server_settings
.client
.server
)
164 defaults
.server
= this.server_settings
.client
.server
;
166 if (this.server_settings
.client
.port
)
167 defaults
.port
= this.server_settings
.client
.port
;
169 if (this.server_settings
.client
.ssl
)
170 defaults
.ssl
= this.server_settings
.client
.ssl
;
172 if (this.server_settings
.client
.channel
)
173 defaults
.channel
= this.server_settings
.client
.channel
;
179 * Get any settings passed in the URL
180 * These settings may be changed in the server selection dialog
183 // Process the URL part by part, extracting as we go
184 parts
= window
.location
.pathname
.toString().replace(this.get('base_path'), '').split('/');
186 if (parts
.length
> 0) {
189 if (parts
.length
> 0 && parts
[0]) {
190 // 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.
191 uricheck
= parts
[0].substr(0, 7).toLowerCase();
192 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
193 parts
[0] = decodeURIComponent(parts
[0]);
194 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
195 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
197 uricheck[1] = ssl (optional)
199 uricheck[3] = port (optional)
200 uricheck[4] = channel (optional)
201 uricheck[5] = channel key (optional, channel must also be set)
204 if (typeof uricheck
[1] !== 'undefined') {
206 if (defaults
.port
=== 6667) {
207 defaults
.port
= 6697;
210 defaults
.server
= uricheck
[2];
211 if (typeof uricheck
[3] !== 'undefined') {
212 defaults
.port
= uricheck
[3];
214 if (typeof uricheck
[4] !== 'undefined') {
215 defaults
.channel
= '#' + uricheck
[4];
216 if (typeof uricheck
[5] !== 'undefined') {
217 defaults
.channel_key
= uricheck
[5];
223 // Extract the port+ssl if we find one
224 if (parts
[0].search(/:/) > 0) {
225 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
226 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
227 if (defaults
.port
[0] === '+') {
228 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
231 defaults
.ssl
= false;
235 defaults
.server
= parts
[0];
242 if (parts
.length
> 0 && parts
[0]) {
243 defaults
.channel
= '#' + parts
[0];
248 // If any settings have been given by the server.. override any auto detected settings
250 * Get any server restrictions as set in the server config
251 * These settings can not be changed in the server selection dialog
253 if (this.server_settings
&& this.server_settings
.connection
) {
254 if (this.server_settings
.connection
.server
) {
255 defaults
.server
= this.server_settings
.connection
.server
;
258 if (this.server_settings
.connection
.port
) {
259 defaults
.port
= this.server_settings
.connection
.port
;
262 if (this.server_settings
.connection
.ssl
) {
263 defaults
.ssl
= this.server_settings
.connection
.ssl
;
266 if (this.server_settings
.connection
.channel
) {
267 defaults
.channel
= this.server_settings
.connection
.channel
;
270 if (this.server_settings
.connection
.nick
) {
271 defaults
.nick
= this.server_settings
.connection
.nick
;
275 // Set any random numbers if needed
276 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
278 // Populate the server select box with defaults
279 this.panels
.server
.server_login
.populateFields(defaults
);
283 this.bindGatewayCommands = function (gw
) {
284 gw
.on('onmotd', function (event
) {
285 that
.panels
.server
.addMsg(_kiwi
.gateway
.get('name'), event
.msg
, 'motd');
289 gw
.on('onconnect', function (event
) {
290 that
.view
.barsShow();
292 if (auto_connect_details
.channel
) {
293 that
.controlbox
.processInput('/JOIN ' + auto_connect_details
.channel
+ ' ' + auto_connect_details
.channel_key
);
301 gw
.on('disconnect', function (event
) {
302 var msg
= 'You have been disconnected. Attempting to reconnect for you..';
303 that
.message
.text(msg
, {timeout
: 10000});
305 // Mention the disconnection on every channel
306 $.each(_kiwi
.app
.panels
.models
, function (idx
, panel
) {
307 if (!panel
|| !panel
.isChannel()) return;
308 panel
.addMsg('', msg
, 'action quit');
310 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action quit');
314 gw
.on('reconnecting', function (event
) {
315 msg
= 'You have been disconnected. Attempting to reconnect again in ' + (event
.delay
/1000) + ' seconds..';
316 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action quit');
318 gw
.on('connect', function (event
) {
319 if (gw_stat
!== 1) return;
321 var msg
= 'It\'s OK, you\'re connected again :)';
322 that
.message
.text(msg
, {timeout
: 5000});
324 // Mention the disconnection on every channel
325 $.each(_kiwi
.app
.panels
.models
, function (idx
, panel
) {
326 if (!panel
|| !panel
.isChannel()) return;
327 panel
.addMsg('', msg
, 'action join');
329 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action join');
336 gw
.on('onjoin', function (event
) {
337 var c
, members
, user
;
338 c
= that
.panels
.getByName(event
.channel
);
340 c
= new _kiwi
.model
.Channel({name
: event
.channel
});
344 members
= c
.get('members');
345 if (!members
) return;
347 user
= new _kiwi
.model
.Member({nick
: event
.nick
, ident
: event
.ident
, hostname
: event
.hostname
});
349 // TODO: highlight the new channel in some way
353 gw
.on('onpart', function (event
) {
354 var channel
, members
, user
,
357 part_options
.type
= 'part';
358 part_options
.message
= event
.message
|| '';
360 channel
= that
.panels
.getByName(event
.channel
);
361 if (!channel
) return;
363 // If this is us, close the panel
364 if (event
.nick
=== _kiwi
.gateway
.get('nick')) {
369 members
= channel
.get('members');
370 if (!members
) return;
372 user
= members
.getByNick(event
.nick
);
375 members
.remove(user
, part_options
);
379 gw
.on('onquit', function (event
) {
383 quit_options
.type
= 'quit';
384 quit_options
.message
= event
.message
|| '';
386 $.each(that
.panels
.models
, function (index
, panel
) {
387 if (!panel
.isChannel()) return;
389 member
= panel
.get('members').getByNick(event
.nick
);
391 panel
.get('members').remove(member
, quit_options
);
397 gw
.on('onkick', function (event
) {
398 var channel
, members
, user
,
401 part_options
.type
= 'kick';
402 part_options
.by
= event
.nick
;
403 part_options
.message
= event
.message
|| '';
405 channel
= that
.panels
.getByName(event
.channel
);
406 if (!channel
) return;
408 members
= channel
.get('members');
409 if (!members
) return;
411 user
= members
.getByNick(event
.kicked
);
414 members
.remove(user
, part_options
);
416 if (event
.kicked
=== _kiwi
.gateway
.get('nick')) {
423 gw
.on('onmsg', function (event
) {
425 is_pm
= (event
.channel
== _kiwi
.gateway
.get('nick'));
428 // If a panel isn't found for this PM, create one
429 panel
= that
.panels
.getByName(event
.nick
);
431 panel
= new _kiwi
.model
.Query({name
: event
.nick
});
432 that
.panels
.add(panel
);
436 // If a panel isn't found for this channel, reroute to the
438 panel
= that
.panels
.getByName(event
.channel
);
440 panel
= that
.panels
.server
;
444 panel
.addMsg(event
.nick
, event
.msg
);
448 gw
.on('onctcp_request', function (event
) {
449 // Reply to a TIME ctcp
450 if (event
.msg
.toUpperCase() === 'TIME') {
451 gw
.ctcp(false, event
.type
, event
.nick
, (new Date()).toString());
456 gw
.on('onctcp_response', function (event
) {
457 that
.panels
.server
.addMsg('[' + event
.nick
+ ']', 'CTCP ' + event
.msg
);
461 gw
.on('onnotice', function (event
) {
464 // Find a panel for the destination(channel) or who its from
465 panel
= that
.panels
.getByName(event
.target
) || that
.panels
.getByName(event
.nick
);
467 panel
= that
.panels
.server
;
470 panel
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
474 gw
.on('onaction', function (event
) {
476 is_pm
= (event
.channel
== _kiwi
.gateway
.get('nick'));
479 // If a panel isn't found for this PM, create one
480 panel
= that
.panels
.getByName(event
.nick
);
482 panel
= new _kiwi
.model
.Channel({name
: event
.nick
});
483 that
.panels
.add(panel
);
487 // If a panel isn't found for this channel, reroute to the
489 panel
= that
.panels
.getByName(event
.channel
);
491 panel
= that
.panels
.server
;
495 panel
.addMsg('', '* ' + event
.nick
+ ' ' + event
.msg
, 'action');
499 gw
.on('ontopic', function (event
) {
501 c
= that
.panels
.getByName(event
.channel
);
504 // Set the channels topic
505 c
.set('topic', event
.topic
);
507 // If this is the active channel, update the topic bar too
508 if (c
.get('name') === _kiwi
.app
.panels
.active
.get('name')) {
509 that
.topicbar
.setCurrentTopic(event
.topic
);
514 gw
.on('ontopicsetby', function (event
) {
516 c
= that
.panels
.getByName(event
.channel
);
519 when
= formatDate(new Date(event
.when
* 1000));
520 c
.addMsg('', 'Topic set by ' + event
.nick
+ ' at ' + when
, 'topic');
524 gw
.on('onuserlist', function (event
) {
526 channel
= that
.panels
.getByName(event
.channel
);
528 // If we didn't find a channel for this, may aswell leave
529 if (!channel
) return;
531 channel
.temp_userlist
= channel
.temp_userlist
|| [];
532 _
.each(event
.users
, function (item
) {
533 var user
= new _kiwi
.model
.Member({nick
: item
.nick
, modes
: item
.modes
});
534 channel
.temp_userlist
.push(user
);
539 gw
.on('onuserlist_end', function (event
) {
541 channel
= that
.panels
.getByName(event
.channel
);
543 // If we didn't find a channel for this, may aswell leave
544 if (!channel
) return;
546 // Update the members list with the new list
547 channel
.get('members').reset(channel
.temp_userlist
|| []);
549 // Clear the temporary userlist
550 delete channel
.temp_userlist
;
554 gw
.on('onmode', function (event
) {
555 var channel
, i
, prefixes
, members
, member
, find_prefix
;
557 // Build a nicely formatted string to be displayed to a regular human
558 function friendlyModeString (event_modes
, alt_target
) {
559 var modes
= {}, return_string
;
561 // If no default given, use the main event info
563 event_modes
= event
.modes
;
564 alt_target
= event
.target
;
567 // Reformat the mode object to make it easier to work with
568 _
.each(event_modes
, function (mode
){
569 var param
= mode
.param
|| alt_target
|| '';
571 // Make sure we have some modes for this param
573 modes
[param
] = {'+':'', '-':''};
576 modes
[param
][mode
.mode
[0]] += mode
.mode
.substr(1);
579 // Put the string together from each mode
581 _
.each(modes
, function (modeset
, param
) {
583 if (modeset
['+']) str
+= '+' + modeset
['+'];
584 if (modeset
['-']) str
+= '-' + modeset
['-'];
585 return_string
.push(str
+ ' ' + param
);
587 return_string
= return_string
.join(', ');
589 return return_string
;
593 channel
= that
.panels
.getByName(event
.target
);
595 prefixes
= _kiwi
.gateway
.get('user_prefixes');
596 find_prefix = function (p
) {
597 return event
.modes
[i
].mode
[1] === p
.mode
;
599 for (i
= 0; i
< event
.modes
.length
; i
++) {
600 if (_
.any(prefixes
, find_prefix
)) {
602 members
= channel
.get('members');
604 member
= members
.getByNick(event
.modes
[i
].param
);
606 console
.log('MODE command recieved for unknown member %s on channel %s', event
.modes
[i
].param
, event
.target
);
609 if (event
.modes
[i
].mode
[0] === '+') {
610 member
.addMode(event
.modes
[i
].mode
[1]);
611 } else if (event
.modes
[i
].mode
[0] === '-') {
612 member
.removeMode(event
.modes
[i
].mode
[1]);
615 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
618 // Channel mode being set
619 // TODO: Store this somewhere?
620 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');
624 channel
.addMsg('', '== ' + event
.nick
+ ' sets mode ' + friendlyModeString(), 'action mode');
626 // This is probably a mode being set on us.
627 if (event
.target
.toLowerCase() === _kiwi
.gateway
.get("nick").toLowerCase()) {
628 that
.panels
.server
.addMsg('', '== ' + event
.nick
+ ' set mode ' + friendlyModeString(), 'action mode');
630 console
.log('MODE command recieved for unknown target %s: ', event
.target
, event
);
636 gw
.on('onnick', function (event
) {
639 $.each(that
.panels
.models
, function (index
, panel
) {
640 if (!panel
.isChannel()) return;
642 member
= panel
.get('members').getByNick(event
.nick
);
644 member
.set('nick', event
.newnick
);
645 panel
.addMsg('', '== ' + event
.nick
+ ' is now known as ' + event
.newnick
, 'action nick');
651 gw
.on('onwhois', function (event
) {
652 /*globals secondsToTime */
653 var logon_date
, idle_time
= '', panel
;
659 if (typeof event
.idle
!== 'undefined') {
660 idle_time
= secondsToTime(parseInt(event
.idle
, 10));
661 idle_time
= idle_time
.h
.toString().lpad(2, "0") + ':' + idle_time
.m
.toString().lpad(2, "0") + ':' + idle_time
.s
.toString().lpad(2, "0");
664 panel
= _kiwi
.app
.panels
.active
;
666 panel
.addMsg(event
.nick
, event
.nick
+ ' [' + event
.nick
+ '!' + event
.ident
+ '@' + event
.host
+ '] * ' + event
.msg
, 'whois');
667 } else if (event
.chans
) {
668 panel
.addMsg(event
.nick
, 'Channels: ' + event
.chans
, 'whois');
669 } else if (event
.irc_server
) {
670 panel
.addMsg(event
.nick
, 'Connected to server: ' + event
.irc_server
, 'whois');
671 } else if (event
.msg
) {
672 panel
.addMsg(event
.nick
, event
.msg
, 'whois');
673 } else if (event
.logon
) {
674 logon_date
= new Date();
675 logon_date
.setTime(event
.logon
* 1000);
676 logon_date
= formatDate(logon_date
);
678 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
+ ', signed on ' + logon_date
, 'whois');
680 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
, 'whois');
684 gw
.on('onaway', function (event
) {
685 $.each(that
.panels
.models
, function (index
, panel
) {
686 if (!panel
.isChannel()) return;
688 member
= panel
.get('members').getByNick(event
.nick
);
690 member
.set('away', !(!event
.trailing
));
696 gw
.on('onlist_start', function (data
) {
697 if (_kiwi
.app
.channel_list
) {
698 _kiwi
.app
.channel_list
.close();
699 delete _kiwi
.app
.channel_list
;
702 var panel
= new _kiwi
.model
.Applet(),
703 applet
= new _kiwi
.applets
.Chanlist();
707 _kiwi
.app
.panels
.add(panel
);
710 _kiwi
.app
.channel_list
= applet
;
714 gw
.on('onlist_channel', function (data
) {
715 // TODO: Put this listener within the applet itself
716 _kiwi
.app
.channel_list
.addChannel(data
.chans
);
720 gw
.on('onlist_end', function (data
) {
721 // TODO: Put this listener within the applet itself
722 delete _kiwi
.app
.channel_list
;
726 gw
.on('onirc_error', function (data
) {
729 if (data
.channel
!== undefined && !(panel
= _kiwi
.app
.panels
.getByName(data
.channel
))) {
730 panel
= _kiwi
.app
.panels
.server
;
733 switch (data
.error
) {
734 case 'banned_from_channel':
735 panel
.addMsg(' ', '== You are banned from ' + data
.channel
+ '. ' + data
.reason
, 'status');
736 _kiwi
.app
.message
.text('You are banned from ' + data
.channel
+ '. ' + data
.reason
);
738 case 'bad_channel_key':
739 panel
.addMsg(' ', '== Bad channel key for ' + data
.channel
, 'status');
740 _kiwi
.app
.message
.text('Bad channel key or password for ' + data
.channel
);
742 case 'invite_only_channel':
743 panel
.addMsg(' ', '== ' + data
.channel
+ ' is invite only.', 'status');
744 _kiwi
.app
.message
.text(data
.channel
+ ' is invite only');
746 case 'channel_is_full':
747 panel
.addMsg(' ', '== ' + data
.channel
+ ' is full.', 'status');
748 _kiwi
.app
.message
.text(data
.channel
+ ' is full');
750 case 'chanop_privs_needed':
751 panel
.addMsg(' ', '== ' + data
.reason
, 'status');
752 _kiwi
.app
.message
.text(data
.reason
+ ' (' + data
.channel
+ ')');
755 tmp
= _kiwi
.app
.panels
.getByName(data
.nick
);
757 tmp
.addMsg(' ', '== ' + data
.nick
+ ': ' + data
.reason
, 'status');
759 _kiwi
.app
.panels
.server
.addMsg(' ', '== ' + data
.nick
+ ': ' + data
.reason
, 'status');
762 case 'nickname_in_use':
763 _kiwi
.app
.panels
.server
.addMsg(' ', '== The nickname ' + data
.nick
+ ' is already in use. Please select a new nickname', 'status');
764 if (_kiwi
.app
.panels
.server
!== _kiwi
.app
.panels
.active
) {
765 _kiwi
.app
.message
.text('The nickname "' + data
.nick
+ '" is already in use. Please select a new nickname');
768 // Only show the nickchange component if the controlbox is open
769 if (that
.controlbox
.$el
.css('display') !== 'none') {
770 (new _kiwi
.view
.NickChangeBox()).render();
773 case 'password_mismatch':
774 _kiwi
.app
.panels
.server
.addMsg(' ', '== Incorrect password given', 'status');
777 // We don't know what data contains, so don't do anything with it.
778 //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');
786 * Bind to certain commands that may be typed into the control box
788 this.bindControllboxCommands = function (controlbox
) {
790 $.extend(controlbox
.preprocessor
.aliases
, {
793 '/me': '/action $1+',
797 // Op related aliases
798 '/op': '/quote mode $channel +o $1+',
799 '/deop': '/quote mode $channel -o $1+',
800 '/hop': '/quote mode $channel +h $1+',
801 '/dehop': '/quote mode $channel -h $1+',
802 '/voice': '/quote mode $channel +v $1+',
803 '/devoice': '/quote mode $channel -v $1+',
804 '/k': '/kick $channel $1+',
807 '/slap': '/me slaps $1 around a bit with a large trout'
810 controlbox
.on('unknown_command', unknownCommand
);
812 controlbox
.on('command', allCommands
);
813 controlbox
.on('command:msg', msgCommand
);
815 controlbox
.on('command:action', actionCommand
);
817 controlbox
.on('command:join', joinCommand
);
819 controlbox
.on('command:part', partCommand
);
821 controlbox
.on('command:nick', function (ev
) {
822 _kiwi
.gateway
.changeNick(ev
.params
[0]);
825 controlbox
.on('command:query', queryCommand
);
827 controlbox
.on('command:topic', topicCommand
);
829 controlbox
.on('command:notice', noticeCommand
);
831 controlbox
.on('command:quote', quoteCommand
);
833 controlbox
.on('command:kick', kickCommand
);
835 controlbox
.on('command:clear', clearCommand
);
837 controlbox
.on('command_ctcp', ctcpCommand
);
840 controlbox
.on('command:css', function (ev
) {
841 var queryString
= '?reload=' + new Date().getTime();
842 $('link[rel="stylesheet"]').each(function () {
843 this.href
= this.href
.replace(/\?.*|$/, queryString
);
847 controlbox
.on('command:js', function (ev
) {
848 if (!ev
.params
[0]) return;
849 $script(ev
.params
[0] + '?' + (new Date().getTime()));
853 controlbox
.on('command:set', function (ev
) {
854 if (!ev
.params
[0]) return;
856 var setting
= ev
.params
[0],
859 // Do we have a second param to set a value?
863 value
= ev
.params
.join(' ');
864 _kiwi
.global
.settings
.set(setting
, value
);
867 // Read the value to the user
868 _kiwi
.app
.panels
.active
.addMsg('', setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
));
872 controlbox
.on('command:save', function (ev
) {
873 _kiwi
.global
.settings
.save();
874 _kiwi
.app
.panels
.active
.addMsg('', 'Settings have been saved');
878 controlbox
.on('command:alias', function (ev
) {
881 // No parameters passed so list them
883 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
884 _kiwi
.app
.panels
.server
.addMsg(' ', name
+ ' => ' + rule
);
889 // Deleting an alias?
890 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
892 if (name
[0] !== '/') name
= '/' + name
;
893 delete controlbox
.preprocessor
.aliases
[name
];
900 rule
= ev
.params
.join(' ');
902 // Make sure the name starts with a slash
903 if (name
[0] !== '/') name
= '/' + name
;
905 // Now actually add the alias
906 controlbox
.preprocessor
.aliases
[name
] = rule
;
909 controlbox
.on('command:applet', appletCommand
);
910 controlbox
.on('command:settings', settingsCommand
);
911 controlbox
.on('command:script', scriptCommand
);
914 // A fallback action. Send a raw command to the server
915 function unknownCommand (ev
) {
916 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
917 console
.log('RAW: ' + raw_cmd
);
918 _kiwi
.gateway
.raw(raw_cmd
);
921 function allCommands (ev
) {}
923 function joinCommand (ev
) {
924 var channel
, channel_names
;
926 channel_names
= ev
.params
.join(' ').split(',');
928 $.each(channel_names
, function (index
, channel_name
) {
929 // Trim any whitespace off the name
930 channel_name
= channel_name
.trim();
932 // Check if we have the panel already. If not, create it
933 channel
= that
.panels
.getByName(channel_name
);
935 channel
= new _kiwi
.model
.Channel({name
: channel_name
});
936 _kiwi
.app
.panels
.add(channel
);
939 _kiwi
.gateway
.join(channel_name
);
942 if (channel
) channel
.view
.show();
946 function queryCommand (ev
) {
947 var destination
, panel
;
949 destination
= ev
.params
[0];
951 // Check if we have the panel already. If not, create it
952 panel
= that
.panels
.getByName(destination
);
954 panel
= new _kiwi
.model
.Query({name
: destination
});
955 _kiwi
.app
.panels
.add(panel
);
958 if (panel
) panel
.view
.show();
962 function msgCommand (ev
) {
963 var destination
= ev
.params
[0],
964 panel
= that
.panels
.getByName(destination
) || that
.panels
.server
;
968 panel
.addMsg(_kiwi
.gateway
.get('nick'), ev
.params
.join(' '));
969 _kiwi
.gateway
.privmsg(destination
, ev
.params
.join(' '));
972 function actionCommand (ev
) {
973 if (_kiwi
.app
.panels
.active
=== _kiwi
.app
.panels
.server
) {
977 var panel
= _kiwi
.app
.panels
.active
;
978 panel
.addMsg('', '* ' + _kiwi
.gateway
.get('nick') + ' ' + ev
.params
.join(' '), 'action');
979 _kiwi
.gateway
.action(panel
.get('name'), ev
.params
.join(' '));
982 function partCommand (ev
) {
983 if (ev
.params
.length
=== 0) {
984 _kiwi
.gateway
.part(_kiwi
.app
.panels
.active
.get('name'));
986 _
.each(ev
.params
, function (channel
) {
987 _kiwi
.gateway
.part(channel
);
990 // TODO: More responsive = close tab now, more accurate = leave until part event
991 //_kiwi.app.panels.remove(_kiwi.app.panels.active);
994 function topicCommand (ev
) {
997 if (ev
.params
.length
=== 0) return;
999 if (that
.isChannelName(ev
.params
[0])) {
1000 channel_name
= ev
.params
[0];
1003 channel_name
= _kiwi
.app
.panels
.active
.get('name');
1006 _kiwi
.gateway
.topic(channel_name
, ev
.params
.join(' '));
1009 function noticeCommand (ev
) {
1012 // Make sure we have a destination and some sort of message
1013 if (ev
.params
.length
<= 1) return;
1015 destination
= ev
.params
[0];
1018 _kiwi
.gateway
.notice(destination
, ev
.params
.join(' '));
1021 function quoteCommand (ev
) {
1022 var raw
= ev
.params
.join(' ');
1023 _kiwi
.gateway
.raw(raw
);
1026 function kickCommand (ev
) {
1027 var nick
, panel
= _kiwi
.app
.panels
.active
;
1029 if (!panel
.isChannel()) return;
1031 // Make sure we have a nick
1032 if (ev
.params
.length
=== 0) return;
1034 nick
= ev
.params
[0];
1037 _kiwi
.gateway
.kick(panel
.get('name'), nick
, ev
.params
.join(' '));
1040 function clearCommand (ev
) {
1041 // Can't clear a server or applet panel
1042 if (_kiwi
.app
.panels
.active
.isServer() || _kiwi
.app
.panels
.active
.isApplet()) {
1046 if (_kiwi
.app
.panels
.active
.clearMessages
) {
1047 _kiwi
.app
.panels
.active
.clearMessages();
1051 function ctcpCommand(ev
) {
1054 // Make sure we have a target and a ctcp type (eg. version, time)
1055 if (ev
.params
.length
< 2) return;
1057 target
= ev
.params
[0];
1060 type
= ev
.params
[0];
1063 _kiwi
.gateway
.ctcp(true, type
, target
, ev
.params
.join(' '));
1066 function settingsCommand (ev
) {
1067 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
1068 settings
.view
.show();
1071 function scriptCommand (ev
) {
1072 var editor
= _kiwi
.model
.Applet
.loadOnce('kiwi_script_editor');
1076 function appletCommand (ev
) {
1077 if (!ev
.params
[0]) return;
1079 var panel
= new _kiwi
.model
.Applet();
1082 // Url and name given
1083 panel
.load(ev
.params
[0], ev
.params
[1]);
1085 // Load a pre-loaded applet
1086 if (_kiwi
.applets
[ev
.params
[0]]) {
1087 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
1089 _kiwi
.app
.panels
.server
.addMsg('', 'Applet "' + ev
.params
[0] + '" does not exist');
1094 _kiwi
.app
.panels
.add(panel
);
1102 this.isChannelName = function (channel_name
) {
1103 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
1105 if (!channel_name
|| !channel_name
.length
) return false;
1106 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
1112 model
= Backbone
.Model
.extend(new model());
1114 return new model(arguments
);