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_' + Math
.ceil(Math
.random() * 10000).toString(),
150 server
: 'irc.kiwiirc.com',
153 channel
: window
.location
.hash
|| '#kiwiirc',
158 // Process the URL part by part, extracting as we go
159 parts
= window
.location
.pathname
.toString().replace(this.get('base_path'), '').split('/');
161 if (parts
.length
> 0) {
164 if (parts
.length
> 0 && parts
[0]) {
165 // 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.
166 uricheck
= parts
[0].substr(0, 7).toLowerCase();
167 if ((uricheck
=== 'ircs%3a') || (uricheck
.substr(0,6) === 'irc%3a')) {
168 parts
[0] = decodeURIComponent(parts
[0]);
169 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
170 uricheck
= /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts
[0]);
172 uricheck[1] = ssl (optional)
174 uricheck[3] = port (optional)
175 uricheck[4] = channel (optional)
176 uricheck[5] = channel key (optional, channel must also be set)
179 if (typeof uricheck
[1] !== 'undefined') {
181 if (defaults
.port
=== 6667) {
182 defaults
.port
= 6697;
185 defaults
.server
= uricheck
[2];
186 if (typeof uricheck
[3] !== 'undefined') {
187 defaults
.port
= uricheck
[3];
189 if (typeof uricheck
[4] !== 'undefined') {
190 defaults
.channel
= '#' + uricheck
[4];
191 if (typeof uricheck
[5] !== 'undefined') {
192 defaults
.channel_key
= uricheck
[5];
198 // Extract the port+ssl if we find one
199 if (parts
[0].search(/:/) > 0) {
200 defaults
.port
= parts
[0].substring(parts
[0].search(/:/) + 1);
201 defaults
.server
= parts
[0].substring(0, parts
[0].search(/:/));
202 if (defaults
.port
[0] === '+') {
203 defaults
.port
= parseInt(defaults
.port
.substring(1), 10);
206 defaults
.ssl
= false;
210 defaults
.server
= parts
[0];
217 if (parts
.length
> 0 && parts
[0]) {
218 defaults
.channel
= '#' + parts
[0];
223 // If any settings have been given by the server.. override any auto detected settings
224 if (this.server_settings
&& this.server_settings
.connection
) {
225 if (this.server_settings
.connection
.server
) {
226 defaults
.server
= this.server_settings
.connection
.server
;
229 if (this.server_settings
.connection
.port
) {
230 defaults
.port
= this.server_settings
.connection
.port
;
233 if (this.server_settings
.connection
.ssl
) {
234 defaults
.ssl
= this.server_settings
.connection
.ssl
;
237 if (this.server_settings
.connection
.channel
) {
238 defaults
.channel
= this.server_settings
.connection
.channel
;
241 if (this.server_settings
.connection
.nick
) {
242 defaults
.nick
= this.server_settings
.connection
.nick
;
246 // Set any random numbers if needed
247 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
249 // Populate the server select box with defaults
250 this.panels
.server
.server_login
.populateFields(defaults
);
254 this.bindGatewayCommands = function (gw
) {
255 gw
.on('onmotd', function (event
) {
256 that
.panels
.server
.addMsg(_kiwi
.gateway
.get('name'), event
.msg
, 'motd');
260 gw
.on('onconnect', function (event
) {
261 that
.view
.barsShow();
263 if (auto_connect_details
.channel
) {
264 that
.controlbox
.processInput('/JOIN ' + auto_connect_details
.channel
+ ' ' + auto_connect_details
.channel_key
);
272 gw
.on('disconnect', function (event
) {
273 var msg
= 'You have been disconnected. Attempting to reconnect for you..';
274 that
.message
.text(msg
, {timeout
: 10000});
276 // Mention the disconnection on every channel
277 $.each(_kiwi
.app
.panels
.models
, function (idx
, panel
) {
278 if (!panel
|| !panel
.isChannel()) return;
279 panel
.addMsg('', msg
, 'action quit');
281 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action quit');
285 gw
.on('reconnecting', function (event
) {
286 msg
= 'You have been disconnected. Attempting to reconnect again in ' + (event
.delay
/1000) + ' seconds..';
287 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action quit');
289 gw
.on('connect', function (event
) {
290 if (gw_stat
!== 1) return;
292 var msg
= 'It\'s OK, you\'re connected again :)';
293 that
.message
.text(msg
, {timeout
: 5000});
295 // Mention the disconnection on every channel
296 $.each(_kiwi
.app
.panels
.models
, function (idx
, panel
) {
297 if (!panel
|| !panel
.isChannel()) return;
298 panel
.addMsg('', msg
, 'action join');
300 _kiwi
.app
.panels
.server
.addMsg('', msg
, 'action join');
307 gw
.on('onjoin', function (event
) {
308 var c
, members
, user
;
309 c
= that
.panels
.getByName(event
.channel
);
311 c
= new _kiwi
.model
.Channel({name
: event
.channel
});
315 members
= c
.get('members');
316 if (!members
) return;
318 user
= new _kiwi
.model
.Member({nick
: event
.nick
, ident
: event
.ident
, hostname
: event
.hostname
});
320 // TODO: highlight the new channel in some way
324 gw
.on('onpart', function (event
) {
325 var channel
, members
, user
,
328 part_options
.type
= 'part';
329 part_options
.message
= event
.message
|| '';
331 channel
= that
.panels
.getByName(event
.channel
);
332 if (!channel
) return;
334 // If this is us, close the panel
335 if (event
.nick
=== _kiwi
.gateway
.get('nick')) {
340 members
= channel
.get('members');
341 if (!members
) return;
343 user
= members
.getByNick(event
.nick
);
346 members
.remove(user
, part_options
);
350 gw
.on('onquit', function (event
) {
354 quit_options
.type
= 'quit';
355 quit_options
.message
= event
.message
|| '';
357 $.each(that
.panels
.models
, function (index
, panel
) {
358 if (!panel
.isChannel()) return;
360 member
= panel
.get('members').getByNick(event
.nick
);
362 panel
.get('members').remove(member
, quit_options
);
368 gw
.on('onkick', function (event
) {
369 var channel
, members
, user
,
372 part_options
.type
= 'kick';
373 part_options
.by
= event
.nick
;
374 part_options
.message
= event
.message
|| '';
376 channel
= that
.panels
.getByName(event
.channel
);
377 if (!channel
) return;
379 members
= channel
.get('members');
380 if (!members
) return;
382 user
= members
.getByNick(event
.kicked
);
385 members
.remove(user
, part_options
);
387 if (event
.kicked
=== _kiwi
.gateway
.get('nick')) {
394 gw
.on('onmsg', function (event
) {
396 is_pm
= (event
.channel
== _kiwi
.gateway
.get('nick'));
399 // If a panel isn't found for this PM, create one
400 panel
= that
.panels
.getByName(event
.nick
);
402 panel
= new _kiwi
.model
.Query({name
: event
.nick
});
403 that
.panels
.add(panel
);
407 // If a panel isn't found for this channel, reroute to the
409 panel
= that
.panels
.getByName(event
.channel
);
411 panel
= that
.panels
.server
;
415 panel
.addMsg(event
.nick
, event
.msg
);
419 gw
.on('onnotice', function (event
) {
422 // Find a panel for the destination(channel) or who its from
423 panel
= that
.panels
.getByName(event
.target
) || that
.panels
.getByName(event
.nick
);
425 panel
= that
.panels
.server
;
428 panel
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
432 gw
.on('onaction', function (event
) {
434 is_pm
= (event
.channel
== _kiwi
.gateway
.get('nick'));
437 // If a panel isn't found for this PM, create one
438 panel
= that
.panels
.getByName(event
.nick
);
440 panel
= new _kiwi
.model
.Channel({name
: event
.nick
});
441 that
.panels
.add(panel
);
445 // If a panel isn't found for this channel, reroute to the
447 panel
= that
.panels
.getByName(event
.channel
);
449 panel
= that
.panels
.server
;
453 panel
.addMsg('', '* ' + event
.nick
+ ' ' + event
.msg
, 'action');
457 gw
.on('ontopic', function (event
) {
459 c
= that
.panels
.getByName(event
.channel
);
462 // Set the channels topic
463 c
.set('topic', event
.topic
);
465 // If this is the active channel, update the topic bar too
466 if (c
.get('name') === _kiwi
.app
.panels
.active
.get('name')) {
467 that
.topicbar
.setCurrentTopic(event
.topic
);
472 gw
.on('ontopicsetby', function (event
) {
474 c
= that
.panels
.getByName(event
.channel
);
477 when
= formatDate(new Date(event
.when
* 1000));
478 c
.addMsg('', 'Topic set by ' + event
.nick
+ ' at ' + when
, 'topic');
482 gw
.on('onuserlist', function (event
) {
484 channel
= that
.panels
.getByName(event
.channel
);
486 // If we didn't find a channel for this, may aswell leave
487 if (!channel
) return;
489 channel
.temp_userlist
= channel
.temp_userlist
|| [];
490 _
.each(event
.users
, function (item
) {
491 var user
= new _kiwi
.model
.Member({nick
: item
.nick
, modes
: item
.modes
});
492 channel
.temp_userlist
.push(user
);
497 gw
.on('onuserlist_end', function (event
) {
499 channel
= that
.panels
.getByName(event
.channel
);
501 // If we didn't find a channel for this, may aswell leave
502 if (!channel
) return;
504 // Update the members list with the new list
505 channel
.get('members').reset(channel
.temp_userlist
|| []);
507 // Clear the temporary userlist
508 delete channel
.temp_userlist
;
512 gw
.on('onmode', function (event
) {
513 var channel
, i
, prefixes
, members
, member
, find_prefix
;
515 // Build a nicely formatted string to be displayed to a regular human
516 function friendlyModeString (event_modes
, alt_target
) {
517 var modes
= {}, return_string
;
519 // If no default given, use the main event info
521 event_modes
= event
.modes
;
522 alt_target
= event
.target
;
525 // Reformat the mode object to make it easier to work with
526 _
.each(event_modes
, function (mode
){
527 var param
= mode
.param
|| alt_target
|| '';
529 // Make sure we have some modes for this param
531 modes
[param
] = {'+':'', '-':''};
534 modes
[param
][mode
.mode
[0]] += mode
.mode
.substr(1);
537 // Put the string together from each mode
539 _
.each(modes
, function (modeset
, param
) {
541 if (modeset
['+']) str
+= '+' + modeset
['+'];
542 if (modeset
['-']) str
+= '-' + modeset
['-'];
543 return_string
.push(str
+ ' ' + param
);
545 return_string
= return_string
.join(', ');
547 return return_string
;
551 channel
= that
.panels
.getByName(event
.target
);
553 prefixes
= _kiwi
.gateway
.get('user_prefixes');
554 find_prefix = function (p
) {
555 return event
.modes
[i
].mode
[1] === p
.mode
;
557 for (i
= 0; i
< event
.modes
.length
; i
++) {
558 if (_
.any(prefixes
, find_prefix
)) {
560 members
= channel
.get('members');
562 member
= members
.getByNick(event
.modes
[i
].param
);
564 console
.log('MODE command recieved for unknown member %s on channel %s', event
.modes
[i
].param
, event
.target
);
567 if (event
.modes
[i
].mode
[0] === '+') {
568 member
.addMode(event
.modes
[i
].mode
[1]);
569 } else if (event
.modes
[i
].mode
[0] === '-') {
570 member
.removeMode(event
.modes
[i
].mode
[1]);
573 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
576 // Channel mode being set
577 // TODO: Store this somewhere?
578 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');
582 channel
.addMsg('', '== ' + event
.nick
+ ' sets mode ' + friendlyModeString(), 'action mode');
584 // This is probably a mode being set on us.
585 if (event
.target
.toLowerCase() === _kiwi
.gateway
.get("nick").toLowerCase()) {
586 that
.panels
.server
.addMsg('', '== ' + event
.nick
+ ' set mode ' + friendlyModeString(), 'action mode');
588 console
.log('MODE command recieved for unknown target %s: ', event
.target
, event
);
594 gw
.on('onnick', function (event
) {
597 $.each(that
.panels
.models
, function (index
, panel
) {
598 if (!panel
.isChannel()) return;
600 member
= panel
.get('members').getByNick(event
.nick
);
602 member
.set('nick', event
.newnick
);
603 panel
.addMsg('', '== ' + event
.nick
+ ' is now known as ' + event
.newnick
, 'action nick');
609 gw
.on('onwhois', function (event
) {
610 /*globals secondsToTime */
611 var logon_date
, idle_time
= '', panel
;
617 if (typeof event
.idle
!== 'undefined') {
618 idle_time
= secondsToTime(parseInt(event
.idle
, 10));
619 idle_time
= idle_time
.h
.toString().lpad(2, "0") + ':' + idle_time
.m
.toString().lpad(2, "0") + ':' + idle_time
.s
.toString().lpad(2, "0");
622 panel
= _kiwi
.app
.panels
.active
;
624 panel
.addMsg(event
.nick
, 'is ' + event
.nick
+ '!' + event
.ident
+ '@' + event
.host
+ ' * ' + event
.msg
, 'whois');
625 } else if (event
.chans
) {
626 panel
.addMsg(event
.nick
, 'on ' + event
.chans
, 'whois');
627 } else if (event
.irc_server
) {
628 panel
.addMsg(event
.nick
, 'using ' + event
.server
, 'whois');
629 } else if (event
.msg
) {
630 panel
.addMsg(event
.nick
, event
.msg
, 'whois');
631 } else if (event
.logon
) {
632 logon_date
= new Date();
633 logon_date
.setTime(event
.logon
* 1000);
634 logon_date
= formatDate(logon_date
);
636 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
+ ', signed on ' + logon_date
, 'whois');
638 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
, 'whois');
642 gw
.on('onaway', function (event
) {
643 $.each(that
.panels
.models
, function (index
, panel
) {
644 if (!panel
.isChannel()) return;
646 member
= panel
.get('members').getByNick(event
.nick
);
648 member
.set('away', !(!event
.trailing
));
654 gw
.on('onlist_start', function (data
) {
655 if (_kiwi
.app
.channel_list
) {
656 _kiwi
.app
.channel_list
.close();
657 delete _kiwi
.app
.channel_list
;
660 var panel
= new _kiwi
.model
.Applet(),
661 applet
= new _kiwi
.applets
.Chanlist();
665 _kiwi
.app
.panels
.add(panel
);
668 _kiwi
.app
.channel_list
= applet
;
672 gw
.on('onlist_channel', function (data
) {
673 // TODO: Put this listener within the applet itself
674 _kiwi
.app
.channel_list
.addChannel(data
.chans
);
678 gw
.on('onlist_end', function (data
) {
679 // TODO: Put this listener within the applet itself
680 delete _kiwi
.app
.channel_list
;
684 gw
.on('onirc_error', function (data
) {
687 if (data
.channel
!== undefined && !(panel
= _kiwi
.app
.panels
.getByName(data
.channel
))) {
688 panel
= _kiwi
.app
.panels
.server
;
691 switch (data
.error
) {
692 case 'banned_from_channel':
693 panel
.addMsg(' ', '== You are banned from ' + data
.channel
+ '. ' + data
.reason
, 'status');
694 _kiwi
.app
.message
.text('You are banned from ' + data
.channel
+ '. ' + data
.reason
);
696 case 'bad_channel_key':
697 panel
.addMsg(' ', '== Bad channel key for ' + data
.channel
, 'status');
698 _kiwi
.app
.message
.text('Bad channel key or password for ' + data
.channel
);
700 case 'invite_only_channel':
701 panel
.addMsg(' ', '== ' + data
.channel
+ ' is invite only.', 'status');
702 _kiwi
.app
.message
.text(data
.channel
+ ' is invite only');
704 case 'channel_is_full':
705 panel
.addMsg(' ', '== ' + data
.channel
+ ' is full.', 'status');
706 _kiwi
.app
.message
.text(data
.channel
+ ' is full');
708 case 'chanop_privs_needed':
709 panel
.addMsg(' ', '== ' + data
.reason
, 'status');
710 _kiwi
.app
.message
.text(data
.reason
+ ' (' + data
.channel
+ ')');
713 tmp
= _kiwi
.app
.panels
.getByName(data
.nick
);
715 tmp
.addMsg(' ', '== ' + data
.nick
+ ': ' + data
.reason
, 'status');
717 _kiwi
.app
.panels
.server
.addMsg(' ', '== ' + data
.nick
+ ': ' + data
.reason
, 'status');
720 case 'nickname_in_use':
721 _kiwi
.app
.panels
.server
.addMsg(' ', '== The nickname ' + data
.nick
+ ' is already in use. Please select a new nickname', 'status');
722 if (_kiwi
.app
.panels
.server
!== _kiwi
.app
.panels
.active
) {
723 _kiwi
.app
.message
.text('The nickname "' + data
.nick
+ '" is already in use. Please select a new nickname');
726 // Only show the nickchange component if the controlbox is open
727 if (that
.controlbox
.$el
.css('display') !== 'none') {
728 (new _kiwi
.view
.NickChangeBox()).render();
733 // We don't know what data contains, so don't do anything with it.
734 //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');
742 * Bind to certain commands that may be typed into the control box
744 this.bindControllboxCommands = function (controlbox
) {
746 $.extend(controlbox
.preprocessor
.aliases
, {
749 '/me': '/action $1+',
753 // Op related aliases
754 '/op': '/quote mode $channel +o $1+',
755 '/deop': '/quote mode $channel -o $1+',
756 '/hop': '/quote mode $channel +h $1+',
757 '/dehop': '/quote mode $channel -h $1+',
758 '/voice': '/quote mode $channel +v $1+',
759 '/devoice': '/quote mode $channel -v $1+',
760 '/k': '/kick $channel $1+',
763 '/slap': '/me slaps $1 around a bit with a large trout'
766 controlbox
.on('unknown_command', unknownCommand
);
768 controlbox
.on('command', allCommands
);
769 controlbox
.on('command_msg', msgCommand
);
771 controlbox
.on('command_action', actionCommand
);
773 controlbox
.on('command_join', joinCommand
);
775 controlbox
.on('command_part', partCommand
);
777 controlbox
.on('command_nick', function (ev
) {
778 _kiwi
.gateway
.changeNick(ev
.params
[0]);
781 controlbox
.on('command_query', queryCommand
);
783 controlbox
.on('command_topic', topicCommand
);
785 controlbox
.on('command_notice', noticeCommand
);
787 controlbox
.on('command_quote', quoteCommand
);
789 controlbox
.on('command_kick', kickCommand
);
792 controlbox
.on('command_css', function (ev
) {
793 var queryString
= '?reload=' + new Date().getTime();
794 $('link[rel="stylesheet"]').each(function () {
795 this.href
= this.href
.replace(/\?.*|$/, queryString
);
799 controlbox
.on('command_js', function (ev
) {
800 if (!ev
.params
[0]) return;
801 $script(ev
.params
[0] + '?' + (new Date().getTime()));
805 controlbox
.on('command_set', function (ev
) {
806 if (!ev
.params
[0]) return;
808 var setting
= ev
.params
[0],
811 // Do we have a second param to set a value?
815 value
= ev
.params
.join(' ');
816 _kiwi
.global
.settings
.set(setting
, value
);
819 // Read the value to the user
820 _kiwi
.app
.panels
.active
.addMsg('', setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
));
824 controlbox
.on('command_save', function (ev
) {
825 _kiwi
.global
.settings
.save();
826 _kiwi
.app
.panels
.active
.addMsg('', 'Settings have been saved');
830 controlbox
.on('command_alias', function (ev
) {
833 // No parameters passed so list them
835 $.each(controlbox
.preprocessor
.aliases
, function (name
, rule
) {
836 _kiwi
.app
.panels
.server
.addMsg(' ', name
+ ' => ' + rule
);
841 // Deleting an alias?
842 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
844 if (name
[0] !== '/') name
= '/' + name
;
845 delete controlbox
.preprocessor
.aliases
[name
];
852 rule
= ev
.params
.join(' ');
854 // Make sure the name starts with a slash
855 if (name
[0] !== '/') name
= '/' + name
;
857 // Now actually add the alias
858 controlbox
.preprocessor
.aliases
[name
] = rule
;
861 controlbox
.on('command_applet', appletCommand
);
862 controlbox
.on('command_settings', settingsCommand
);
865 // A fallback action. Send a raw command to the server
866 function unknownCommand (ev
) {
867 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
868 console
.log('RAW: ' + raw_cmd
);
869 _kiwi
.gateway
.raw(raw_cmd
);
872 function allCommands (ev
) {}
874 function joinCommand (ev
) {
875 var channel
, channel_names
;
877 channel_names
= ev
.params
.join(' ').split(',');
879 $.each(channel_names
, function (index
, channel_name
) {
880 // Trim any whitespace off the name
881 channel_name
= channel_name
.trim();
883 // Check if we have the panel already. If not, create it
884 channel
= that
.panels
.getByName(channel_name
);
886 channel
= new _kiwi
.model
.Channel({name
: channel_name
});
887 _kiwi
.app
.panels
.add(channel
);
890 _kiwi
.gateway
.join(channel_name
);
893 if (channel
) channel
.view
.show();
897 function queryCommand (ev
) {
898 var destination
, panel
;
900 destination
= ev
.params
[0];
902 // Check if we have the panel already. If not, create it
903 panel
= that
.panels
.getByName(destination
);
905 panel
= new _kiwi
.model
.Query({name
: destination
});
906 _kiwi
.app
.panels
.add(panel
);
909 if (panel
) panel
.view
.show();
913 function msgCommand (ev
) {
914 var destination
= ev
.params
[0],
915 panel
= that
.panels
.getByName(destination
) || that
.panels
.server
;
919 panel
.addMsg(_kiwi
.gateway
.get('nick'), ev
.params
.join(' '));
920 _kiwi
.gateway
.privmsg(destination
, ev
.params
.join(' '));
923 function actionCommand (ev
) {
924 if (_kiwi
.app
.panels
.active
=== _kiwi
.app
.panels
.server
) {
928 var panel
= _kiwi
.app
.panels
.active
;
929 panel
.addMsg('', '* ' + _kiwi
.gateway
.get('nick') + ' ' + ev
.params
.join(' '), 'action');
930 _kiwi
.gateway
.action(panel
.get('name'), ev
.params
.join(' '));
933 function partCommand (ev
) {
934 if (ev
.params
.length
=== 0) {
935 _kiwi
.gateway
.part(_kiwi
.app
.panels
.active
.get('name'));
937 _
.each(ev
.params
, function (channel
) {
938 _kiwi
.gateway
.part(channel
);
941 // TODO: More responsive = close tab now, more accurate = leave until part event
942 //_kiwi.app.panels.remove(_kiwi.app.panels.active);
945 function topicCommand (ev
) {
948 if (ev
.params
.length
=== 0) return;
950 if (that
.isChannelName(ev
.params
[0])) {
951 channel_name
= ev
.params
[0];
954 channel_name
= _kiwi
.app
.panels
.active
.get('name');
957 _kiwi
.gateway
.topic(channel_name
, ev
.params
.join(' '));
960 function noticeCommand (ev
) {
963 // Make sure we have a destination and some sort of message
964 if (ev
.params
.length
<= 1) return;
966 destination
= ev
.params
[0];
969 _kiwi
.gateway
.notice(destination
, ev
.params
.join(' '));
972 function quoteCommand (ev
) {
973 var raw
= ev
.params
.join(' ');
974 _kiwi
.gateway
.raw(raw
);
977 function kickCommand (ev
) {
978 var nick
, panel
= _kiwi
.app
.panels
.active
;
980 if (!panel
.isChannel()) return;
982 // Make sure we have a nick
983 if (ev
.params
.length
=== 0) return;
988 _kiwi
.gateway
.kick(panel
.get('name'), nick
, ev
.params
.join(' '));
991 function settingsCommand (ev
) {
992 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
993 settings
.view
.show();
996 function appletCommand (ev
) {
997 if (!ev
.params
[0]) return;
999 var panel
= new _kiwi
.model
.Applet();
1002 // Url and name given
1003 panel
.load(ev
.params
[0], ev
.params
[1]);
1005 // Load a pre-loaded applet
1006 if (_kiwi
.applets
[ev
.params
[0]]) {
1007 panel
.load(new _kiwi
.applets
[ev
.params
[0]]());
1009 _kiwi
.app
.panels
.server
.addMsg('', 'Applet "' + ev
.params
[0] + '" does not exist');
1014 _kiwi
.app
.panels
.add(panel
);
1022 this.isChannelName = function (channel_name
) {
1023 var channel_prefix
= _kiwi
.gateway
.get('channel_prefix');
1025 if (!channel_name
|| !channel_name
.length
) return false;
1026 return (channel_prefix
.indexOf(channel_name
[0]) > -1);
1032 model
= Backbone
.Model
.extend(new model());
1034 return new model(arguments
);