e20b387bb7ede259f6cfb8c17a6c981dd3b69efc
3 function ClientUiCommands(app
, controlbox
) {
5 this.controlbox
= controlbox
;
7 this.addDefaultAliases();
8 this.bindCommand(fn_to_bind
);
11 _kiwi
.misc
.ClientUiCommands
= ClientUiCommands
;
14 // Add the default user command aliases
15 ClientUiCommands
.prototype.addDefaultAliases = function() {
16 $.extend(this.controlbox
.preprocessor
.aliases
, {
26 '/op': '/quote mode $channel +o $1+',
27 '/deop': '/quote mode $channel -o $1+',
28 '/hop': '/quote mode $channel +h $1+',
29 '/dehop': '/quote mode $channel -h $1+',
30 '/voice': '/quote mode $channel +v $1+',
31 '/devoice': '/quote mode $channel -v $1+',
32 '/k': '/kick $channel $1+',
33 '/ban': '/quote mode $channel +b $1+',
34 '/unban': '/quote mode $channel -b $1+',
37 '/slap': '/me slaps $1 around a bit with a large trout'
43 * Add a new command action
44 * @var command Object {'command:the_command': fn}
46 ClientUiCommands
.prototype.bindCommand = function(command
) {
49 _
.each(command
, function(fn
, event_name
) {
50 that
.controlbox
.on(event_name
, _
.bind(fn
, that
));
58 * Default functions to bind to controlbox events
62 'unknown_command': unknownCommand
,
63 'command': allCommands
,
64 'command:msg': msgCommand
,
65 'command:action': actionCommand
,
66 'command:join': joinCommand
,
67 'command:part': partCommand
,
68 'command:cycle': cycleCommand
,
69 'command:nick': nickCommand
,
70 'command:query': queryCommand
,
71 'command:invite': inviteCommand
,
72 'command:topic': topicCommand
,
73 'command:notice': noticeCommand
,
74 'command:quote': quoteCommand
,
75 'command:kick': kickCommand
,
76 'command:clear': clearCommand
,
77 'command:ctcp': ctcpCommand
,
78 'command:quit': quitCommand
,
79 'command:server': serverCommand
,
80 'command:whois': whoisCommand
,
81 'command:whowas': whowasCommand
,
82 'command:encoding': encodingCommand
,
83 'command:channel': channelCommand
,
84 'command:applet': appletCommand
,
85 'command:settings': settingsCommand
,
86 'command:script': scriptCommand
90 fn_to_bind
['command:css'] = function (ev
) {
91 var queryString
= '?reload=' + new Date().getTime();
92 $('link[rel="stylesheet"]').each(function () {
93 this.href
= this.href
.replace(/\?.*|$/, queryString
);
98 fn_to_bind
['command:js'] = function (ev
) {
99 if (!ev
.params
[0]) return;
100 $script(ev
.params
[0] + '?' + (new Date().getTime()));
104 fn_to_bind
['command:set'] = function (ev
) {
105 if (!ev
.params
[0]) return;
107 var setting
= ev
.params
[0],
110 // Do we have a second param to set a value?
114 value
= ev
.params
.join(' ');
116 // If we're setting a true boolean value..
117 if (value
=== 'true')
120 // If we're setting a false boolean value..
121 if (value
=== 'false')
124 // If we're setting a number..
125 if (parseInt(value
, 10).toString() === value
)
126 value
= parseInt(value
, 10);
128 _kiwi
.global
.settings
.set(setting
, value
);
131 // Read the value to the user
132 this.app
.panels().active
.addMsg('', styleText('set_setting', {text
: setting
+ ' = ' + _kiwi
.global
.settings
.get(setting
).toString()}));
136 fn_to_bind
['command:save'] = function (ev
) {
137 _kiwi
.global
.settings
.save();
138 this.app
.panels().active
.addMsg('', styleText('settings_saved', {text
: translateText('client_models_application_settings_saved')}));
142 fn_to_bind
['command:alias'] = function (ev
) {
146 // No parameters passed so list them
148 $.each(this.controlbox
.preprocessor
.aliases
, function (name
, rule
) {
149 that
.app
.panels().server
.addMsg(' ', styleText('list_aliases', {text
: name
+ ' => ' + rule
}));
154 // Deleting an alias?
155 if (ev
.params
[0] === 'del' || ev
.params
[0] === 'delete') {
157 if (name
[0] !== '/') name
= '/' + name
;
158 delete this.controlbox
.preprocessor
.aliases
[name
];
165 rule
= ev
.params
.join(' ');
167 // Make sure the name starts with a slash
168 if (name
[0] !== '/') name
= '/' + name
;
170 // Now actually add the alias
171 this.controlbox
.preprocessor
.aliases
[name
] = rule
;
175 fn_to_bind
['command:ignore'] = function (ev
) {
177 list
= this.app
.connections
.active_connection
.get('ignore_list');
179 // No parameters passed so list them
181 if (list
.length
> 0) {
182 this.app
.panels().active
.addMsg(' ', styleText('ignore_title', {text
: translateText('client_models_application_ignore_title')}));
183 $.each(list
, function (idx
, ignored_pattern
) {
184 that
.app
.panels().active
.addMsg(' ', styleText('ignored_pattern', {text
: ignored_pattern
}));
187 this.app
.panels().active
.addMsg(' ', styleText('ignore_none', {text
: translateText('client_models_application_ignore_none')}));
192 // We have a parameter, so add it
193 list
.push(ev
.params
[0]);
194 this.app
.connections
.active_connection
.set('ignore_list', list
);
195 this.app
.panels().active
.addMsg(' ', styleText('ignore_nick', {text
: translateText('client_models_application_ignore_nick', [ev
.params
[0]])}));
199 fn_to_bind
['command:unignore'] = function (ev
) {
200 var list
= this.app
.connections
.active_connection
.get('ignore_list');
203 this.app
.panels().active
.addMsg(' ', styleText('ignore_stop_notice', {text
: translateText('client_models_application_ignore_stop_notice')}));
207 list
= _
.reject(list
, function(pattern
) {
208 return pattern
=== ev
.params
[0];
211 this.app
.connections
.active_connection
.set('ignore_list', list
);
213 this.app
.panels().active
.addMsg(' ', styleText('ignore_stopped', {text
: translateText('client_models_application_ignore_stopped', [ev
.params
[0]])}));
219 // A fallback action. Send a raw command to the server
220 function unknownCommand (ev
) {
221 var raw_cmd
= ev
.command
+ ' ' + ev
.params
.join(' ');
222 this.app
.connections
.active_connection
.gateway
.raw(raw_cmd
);
226 function allCommands (ev
) {}
229 function joinCommand (ev
) {
230 var panels
, channel_names
;
232 channel_names
= ev
.params
.join(' ').split(',');
233 panels
= this.app
.connections
.active_connection
.createAndJoinChannels(channel_names
);
235 // Show the last channel if we have one
237 panels
[panels
.length
- 1].view
.show();
241 function queryCommand (ev
) {
242 var destination
, message
, panel
;
244 destination
= ev
.params
[0];
247 message
= ev
.params
.join(' ');
249 // Check if we have the panel already. If not, create it
250 panel
= this.app
.connections
.active_connection
.panels
.getByName(destination
);
252 panel
= new _kiwi
.model
.Query({name
: destination
});
253 this.app
.connections
.active_connection
.panels
.add(panel
);
256 if (panel
) panel
.view
.show();
259 this.app
.connections
.active_connection
.gateway
.msg(panel
.get('name'), message
);
260 panel
.addMsg(this.app
.connections
.active_connection
.get('nick'), styleText('privmsg', {text
: message
}), 'privmsg');
266 function msgCommand (ev
) {
268 destination
= ev
.params
[0],
269 panel
= this.app
.connections
.active_connection
.panels
.getByName(destination
) || this.app
.panels().server
;
272 message
= formatToIrcMsg(ev
.params
.join(' '));
274 panel
.addMsg(this.app
.connections
.active_connection
.get('nick'), styleText('privmsg', {text
: message
}), 'privmsg');
275 this.app
.connections
.active_connection
.gateway
.msg(destination
, message
);
279 function actionCommand (ev
) {
280 if (this.app
.panels().active
.isServer()) {
284 var panel
= this.app
.panels().active
;
285 panel
.addMsg('', styleText('action', {nick
: this.app
.connections
.active_connection
.get('nick'), text
: ev
.params
.join(' ')}), 'action');
286 this.app
.connections
.active_connection
.gateway
.action(panel
.get('name'), ev
.params
.join(' '));
290 function partCommand (ev
) {
294 if (ev
.params
.length
=== 0) {
295 this.app
.connections
.active_connection
.gateway
.part(this.app
.panels().active
.get('name'));
297 chans
= ev
.params
[0].split(',');
299 _
.each(chans
, function (channel
) {
300 that
.connections
.active_connection
.gateway
.part(channel
, msg
);
306 function cycleCommand (ev
) {
310 if (ev
.params
.length
=== 0) {
311 chan_name
= this.app
.panels().active
.get('name');
313 chan_name
= ev
.params
[0];
316 this.app
.connections
.active_connection
.gateway
.part(chan_name
);
318 // Wait for a second to give the network time to register the part command
319 setTimeout(function() {
320 // Use createAndJoinChannels() here as it auto-creates panels instead of waiting for the network
321 that
.app
.connections
.active_connection
.createAndJoinChannels(chan_name
);
322 that
.app
.connections
.active_connection
.panels
.getByName(chan_name
).show();
327 function nickCommand (ev
) {
328 this.app
.connections
.active_connection
.gateway
.changeNick(ev
.params
[0]);
332 function topicCommand (ev
) {
335 if (ev
.params
.length
=== 0) return;
337 if (this.app
.connections
.active_connection
.isChannelName(ev
.params
[0])) {
338 channel_name
= ev
.params
[0];
341 channel_name
= this.app
.panels().active
.get('name');
344 this.app
.connections
.active_connection
.gateway
.topic(channel_name
, ev
.params
.join(' '));
348 function noticeCommand (ev
) {
351 // Make sure we have a destination and some sort of message
352 if (ev
.params
.length
<= 1) return;
354 destination
= ev
.params
[0];
357 this.app
.connections
.active_connection
.gateway
.notice(destination
, ev
.params
.join(' '));
361 function quoteCommand (ev
) {
362 var raw
= ev
.params
.join(' ');
363 this.app
.connections
.active_connection
.gateway
.raw(raw
);
367 function kickCommand (ev
) {
368 var nick
, panel
= this.app
.panels().active
;
370 if (!panel
.isChannel()) return;
372 // Make sure we have a nick
373 if (ev
.params
.length
=== 0) return;
378 this.app
.connections
.active_connection
.gateway
.kick(panel
.get('name'), nick
, ev
.params
.join(' '));
382 function clearCommand (ev
) {
383 // Can't clear a server or applet panel
384 if (this.app
.panels().active
.isServer() || this.app
.panels().active
.isApplet()) {
388 if (this.app
.panels().active
.clearMessages
) {
389 this.app
.panels().active
.clearMessages();
394 function ctcpCommand(ev
) {
397 // Make sure we have a target and a ctcp type (eg. version, time)
398 if (ev
.params
.length
< 2) return;
400 target
= ev
.params
[0];
406 this.app
.connections
.active_connection
.gateway
.ctcpRequest(type
, target
, ev
.params
.join(' '));
410 function settingsCommand (ev
) {
411 var settings
= _kiwi
.model
.Applet
.loadOnce('kiwi_settings');
412 settings
.view
.show();
416 function scriptCommand (ev
) {
417 var editor
= _kiwi
.model
.Applet
.loadOnce('kiwi_script_editor');
422 function appletCommand (ev
) {
423 if (!ev
.params
[0]) return;
425 var panel
= new _kiwi
.model
.Applet();
428 // Url and name given
429 panel
.load(ev
.params
[0], ev
.params
[1]);
431 // Load a pre-loaded applet
432 if (this.applets
[ev
.params
[0]]) {
433 panel
.load(new this.applets
[ev
.params
[0]]());
435 this.app
.panels().server
.addMsg('', styleText('applet_notfound', {text
: translateText('client_models_application_applet_notfound', [ev
.params
[0]])}));
440 this.app
.connections
.active_connection
.panels
.add(panel
);
445 function inviteCommand (ev
) {
448 // A nick must be specified
452 // Can only invite into channels
453 if (!this.app
.panels().active
.isChannel())
457 channel
= this.app
.panels().active
.get('name');
459 this.app
.connections
.active_connection
.gateway
.raw('INVITE ' + nick
+ ' ' + channel
);
461 this.app
.panels().active
.addMsg('', styleText('channel_has_been_invited', {nick
: nick
, text
: translateText('client_models_application_has_been_invited', [channel
])}), 'action');
465 function whoisCommand (ev
) {
470 } else if (this.app
.panels().active
.isQuery()) {
471 nick
= this.app
.panels().active
.get('name');
475 this.app
.connections
.active_connection
.gateway
.raw('WHOIS ' + nick
+ ' ' + nick
);
479 function whowasCommand (ev
) {
484 } else if (this.app
.panels().active
.isQuery()) {
485 nick
= this.app
.panels().active
.get('name');
489 this.app
.connections
.active_connection
.gateway
.raw('WHOWAS ' + nick
);
493 function encodingCommand (ev
) {
497 _kiwi
.gateway
.setEncoding(null, ev
.params
[0], function (success
) {
499 that
.app
.panels().active
.addMsg('', styleText('encoding_changed', {text
: translateText('client_models_application_encoding_changed', [ev
.params
[0]])}));
501 that
.app
.panels().active
.addMsg('', styleText('encoding_invalid', {text
: translateText('client_models_application_encoding_invalid', [ev
.params
[0]])}));
505 this.app
.panels().active
.addMsg('', styleText('client_models_application_encoding_notspecified', {text
: translateText('client_models_application_encoding_notspecified')}));
506 this.app
.panels().active
.addMsg('', styleText('client_models_application_encoding_usage', {text
: translateText('client_models_application_encoding_usage')}));
511 function channelCommand (ev
) {
512 var active_panel
= this.app
.panels().active
;
514 if (!active_panel
.isChannel())
517 new _kiwi
.model
.ChannelInfo({channel
: this.app
.panels().active
});
521 function quitCommand (ev
) {
522 var network
= this.app
.connections
.active_connection
;
527 network
.gateway
.quit(ev
.params
.join(' '));
531 function serverCommand (ev
) {
533 server
, port
, ssl
, password
, nick
,
536 // If no server address given, show the new connection dialog
538 tmp
= new _kiwi
.view
.MenuBox(_kiwi
.global
.i18n
.translate('client_models_application_connection_create').fetch());
539 tmp
.addItem('new_connection', new _kiwi
.model
.NewConnection().view
.$el
);
542 // Center screen the dialog
544 top
: (this.app
.view
.$el
.height() / 2) - (tmp
.$el
.height() / 2),
545 left
: (this.app
.view
.$el
.width() / 2) - (tmp
.$el
.width() / 2)
551 // Port given in 'host:port' format and no specific port given after a space
552 if (ev
.params
[0].indexOf(':') > 0) {
553 tmp
= ev
.params
[0].split(':');
557 password
= ev
.params
[1] || undefined;
560 // Server + port given as 'host port'
561 server
= ev
.params
[0];
562 port
= ev
.params
[1] || 6667;
564 password
= ev
.params
[2] || undefined;
567 // + in the port means SSL
568 if (port
.toString()[0] === '+') {
570 port
= parseInt(port
.substring(1), 10);
575 // Default port if one wasn't found
578 // Use the same nick as we currently have
579 nick
= this.app
.connections
.active_connection
.get('nick');
581 this.app
.panels().active
.addMsg('', styleText('server_connecting', {text
: translateText('client_models_application_connection_connecting', [server
, port
.toString()])}));
583 _kiwi
.gateway
.newConnection({
589 }, function(err
, new_connection
) {
590 var translated_err_text
= {text
: translateText('client_models_application_connection_error', [server
, port
.toString(), err
.toString()])};
593 that
.app
.panels().active
.addMsg('', styleText('server_connecting_error', translated_err_text
));