e20b387bb7ede259f6cfb8c17a6c981dd3b69efc
[KiwiIRC.git] / client / src / misc / clientuicommands.js
1 (function() {
2
3 function ClientUiCommands(app, controlbox) {
4 this.app = app;
5 this.controlbox = controlbox;
6
7 this.addDefaultAliases();
8 this.bindCommand(fn_to_bind);
9 }
10
11 _kiwi.misc.ClientUiCommands = ClientUiCommands;
12
13
14 // Add the default user command aliases
15 ClientUiCommands.prototype.addDefaultAliases = function() {
16 $.extend(this.controlbox.preprocessor.aliases, {
17 // General aliases
18 '/p': '/part $1+',
19 '/me': '/action $1+',
20 '/j': '/join $1+',
21 '/q': '/query $1+',
22 '/w': '/whois $1+',
23 '/raw': '/quote $1+',
24
25 // Op related 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+',
35
36 // Misc aliases
37 '/slap': '/me slaps $1 around a bit with a large trout'
38 });
39 };
40
41
42 /**
43 * Add a new command action
44 * @var command Object {'command:the_command': fn}
45 */
46 ClientUiCommands.prototype.bindCommand = function(command) {
47 var that = this;
48
49 _.each(command, function(fn, event_name) {
50 that.controlbox.on(event_name, _.bind(fn, that));
51 });
52 };
53
54
55
56
57 /**
58 * Default functions to bind to controlbox events
59 **/
60
61 var fn_to_bind = {
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
87 };
88
89
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);
94 });
95 };
96
97
98 fn_to_bind['command:js'] = function (ev) {
99 if (!ev.params[0]) return;
100 $script(ev.params[0] + '?' + (new Date().getTime()));
101 };
102
103
104 fn_to_bind['command:set'] = function (ev) {
105 if (!ev.params[0]) return;
106
107 var setting = ev.params[0],
108 value;
109
110 // Do we have a second param to set a value?
111 if (ev.params[1]) {
112 ev.params.shift();
113
114 value = ev.params.join(' ');
115
116 // If we're setting a true boolean value..
117 if (value === 'true')
118 value = true;
119
120 // If we're setting a false boolean value..
121 if (value === 'false')
122 value = false;
123
124 // If we're setting a number..
125 if (parseInt(value, 10).toString() === value)
126 value = parseInt(value, 10);
127
128 _kiwi.global.settings.set(setting, value);
129 }
130
131 // Read the value to the user
132 this.app.panels().active.addMsg('', styleText('set_setting', {text: setting + ' = ' + _kiwi.global.settings.get(setting).toString()}));
133 };
134
135
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')}));
139 };
140
141
142 fn_to_bind['command:alias'] = function (ev) {
143 var that = this,
144 name, rule;
145
146 // No parameters passed so list them
147 if (!ev.params[1]) {
148 $.each(this.controlbox.preprocessor.aliases, function (name, rule) {
149 that.app.panels().server.addMsg(' ', styleText('list_aliases', {text: name + ' => ' + rule}));
150 });
151 return;
152 }
153
154 // Deleting an alias?
155 if (ev.params[0] === 'del' || ev.params[0] === 'delete') {
156 name = ev.params[1];
157 if (name[0] !== '/') name = '/' + name;
158 delete this.controlbox.preprocessor.aliases[name];
159 return;
160 }
161
162 // Add the alias
163 name = ev.params[0];
164 ev.params.shift();
165 rule = ev.params.join(' ');
166
167 // Make sure the name starts with a slash
168 if (name[0] !== '/') name = '/' + name;
169
170 // Now actually add the alias
171 this.controlbox.preprocessor.aliases[name] = rule;
172 };
173
174
175 fn_to_bind['command:ignore'] = function (ev) {
176 var that = this,
177 list = this.app.connections.active_connection.get('ignore_list');
178
179 // No parameters passed so list them
180 if (!ev.params[0]) {
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}));
185 });
186 } else {
187 this.app.panels().active.addMsg(' ', styleText('ignore_none', {text: translateText('client_models_application_ignore_none')}));
188 }
189 return;
190 }
191
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]])}));
196 };
197
198
199 fn_to_bind['command:unignore'] = function (ev) {
200 var list = this.app.connections.active_connection.get('ignore_list');
201
202 if (!ev.params[0]) {
203 this.app.panels().active.addMsg(' ', styleText('ignore_stop_notice', {text: translateText('client_models_application_ignore_stop_notice')}));
204 return;
205 }
206
207 list = _.reject(list, function(pattern) {
208 return pattern === ev.params[0];
209 });
210
211 this.app.connections.active_connection.set('ignore_list', list);
212
213 this.app.panels().active.addMsg(' ', styleText('ignore_stopped', {text: translateText('client_models_application_ignore_stopped', [ev.params[0]])}));
214 };
215
216
217
218
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);
223 }
224
225
226 function allCommands (ev) {}
227
228
229 function joinCommand (ev) {
230 var panels, channel_names;
231
232 channel_names = ev.params.join(' ').split(',');
233 panels = this.app.connections.active_connection.createAndJoinChannels(channel_names);
234
235 // Show the last channel if we have one
236 if (panels.length)
237 panels[panels.length - 1].view.show();
238 }
239
240
241 function queryCommand (ev) {
242 var destination, message, panel;
243
244 destination = ev.params[0];
245 ev.params.shift();
246
247 message = ev.params.join(' ');
248
249 // Check if we have the panel already. If not, create it
250 panel = this.app.connections.active_connection.panels.getByName(destination);
251 if (!panel) {
252 panel = new _kiwi.model.Query({name: destination});
253 this.app.connections.active_connection.panels.add(panel);
254 }
255
256 if (panel) panel.view.show();
257
258 if (message) {
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');
261 }
262
263 }
264
265
266 function msgCommand (ev) {
267 var message,
268 destination = ev.params[0],
269 panel = this.app.connections.active_connection.panels.getByName(destination) || this.app.panels().server;
270
271 ev.params.shift();
272 message = formatToIrcMsg(ev.params.join(' '));
273
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);
276 }
277
278
279 function actionCommand (ev) {
280 if (this.app.panels().active.isServer()) {
281 return;
282 }
283
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(' '));
287 }
288
289
290 function partCommand (ev) {
291 var that = this,
292 chans,
293 msg;
294 if (ev.params.length === 0) {
295 this.app.connections.active_connection.gateway.part(this.app.panels().active.get('name'));
296 } else {
297 chans = ev.params[0].split(',');
298 msg = ev.params[1];
299 _.each(chans, function (channel) {
300 that.connections.active_connection.gateway.part(channel, msg);
301 });
302 }
303 }
304
305
306 function cycleCommand (ev) {
307 var that = this,
308 chan_name;
309
310 if (ev.params.length === 0) {
311 chan_name = this.app.panels().active.get('name');
312 } else {
313 chan_name = ev.params[0];
314 }
315
316 this.app.connections.active_connection.gateway.part(chan_name);
317
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();
323 }, 1000);
324 }
325
326
327 function nickCommand (ev) {
328 this.app.connections.active_connection.gateway.changeNick(ev.params[0]);
329 }
330
331
332 function topicCommand (ev) {
333 var channel_name;
334
335 if (ev.params.length === 0) return;
336
337 if (this.app.connections.active_connection.isChannelName(ev.params[0])) {
338 channel_name = ev.params[0];
339 ev.params.shift();
340 } else {
341 channel_name = this.app.panels().active.get('name');
342 }
343
344 this.app.connections.active_connection.gateway.topic(channel_name, ev.params.join(' '));
345 }
346
347
348 function noticeCommand (ev) {
349 var destination;
350
351 // Make sure we have a destination and some sort of message
352 if (ev.params.length <= 1) return;
353
354 destination = ev.params[0];
355 ev.params.shift();
356
357 this.app.connections.active_connection.gateway.notice(destination, ev.params.join(' '));
358 }
359
360
361 function quoteCommand (ev) {
362 var raw = ev.params.join(' ');
363 this.app.connections.active_connection.gateway.raw(raw);
364 }
365
366
367 function kickCommand (ev) {
368 var nick, panel = this.app.panels().active;
369
370 if (!panel.isChannel()) return;
371
372 // Make sure we have a nick
373 if (ev.params.length === 0) return;
374
375 nick = ev.params[0];
376 ev.params.shift();
377
378 this.app.connections.active_connection.gateway.kick(panel.get('name'), nick, ev.params.join(' '));
379 }
380
381
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()) {
385 return;
386 }
387
388 if (this.app.panels().active.clearMessages) {
389 this.app.panels().active.clearMessages();
390 }
391 }
392
393
394 function ctcpCommand(ev) {
395 var target, type;
396
397 // Make sure we have a target and a ctcp type (eg. version, time)
398 if (ev.params.length < 2) return;
399
400 target = ev.params[0];
401 ev.params.shift();
402
403 type = ev.params[0];
404 ev.params.shift();
405
406 this.app.connections.active_connection.gateway.ctcpRequest(type, target, ev.params.join(' '));
407 }
408
409
410 function settingsCommand (ev) {
411 var settings = _kiwi.model.Applet.loadOnce('kiwi_settings');
412 settings.view.show();
413 }
414
415
416 function scriptCommand (ev) {
417 var editor = _kiwi.model.Applet.loadOnce('kiwi_script_editor');
418 editor.view.show();
419 }
420
421
422 function appletCommand (ev) {
423 if (!ev.params[0]) return;
424
425 var panel = new _kiwi.model.Applet();
426
427 if (ev.params[1]) {
428 // Url and name given
429 panel.load(ev.params[0], ev.params[1]);
430 } else {
431 // Load a pre-loaded applet
432 if (this.applets[ev.params[0]]) {
433 panel.load(new this.applets[ev.params[0]]());
434 } else {
435 this.app.panels().server.addMsg('', styleText('applet_notfound', {text: translateText('client_models_application_applet_notfound', [ev.params[0]])}));
436 return;
437 }
438 }
439
440 this.app.connections.active_connection.panels.add(panel);
441 panel.view.show();
442 }
443
444
445 function inviteCommand (ev) {
446 var nick, channel;
447
448 // A nick must be specified
449 if (!ev.params[0])
450 return;
451
452 // Can only invite into channels
453 if (!this.app.panels().active.isChannel())
454 return;
455
456 nick = ev.params[0];
457 channel = this.app.panels().active.get('name');
458
459 this.app.connections.active_connection.gateway.raw('INVITE ' + nick + ' ' + channel);
460
461 this.app.panels().active.addMsg('', styleText('channel_has_been_invited', {nick: nick, text: translateText('client_models_application_has_been_invited', [channel])}), 'action');
462 }
463
464
465 function whoisCommand (ev) {
466 var nick;
467
468 if (ev.params[0]) {
469 nick = ev.params[0];
470 } else if (this.app.panels().active.isQuery()) {
471 nick = this.app.panels().active.get('name');
472 }
473
474 if (nick)
475 this.app.connections.active_connection.gateway.raw('WHOIS ' + nick + ' ' + nick);
476 }
477
478
479 function whowasCommand (ev) {
480 var nick;
481
482 if (ev.params[0]) {
483 nick = ev.params[0];
484 } else if (this.app.panels().active.isQuery()) {
485 nick = this.app.panels().active.get('name');
486 }
487
488 if (nick)
489 this.app.connections.active_connection.gateway.raw('WHOWAS ' + nick);
490 }
491
492
493 function encodingCommand (ev) {
494 var that = this;
495
496 if (ev.params[0]) {
497 _kiwi.gateway.setEncoding(null, ev.params[0], function (success) {
498 if (success) {
499 that.app.panels().active.addMsg('', styleText('encoding_changed', {text: translateText('client_models_application_encoding_changed', [ev.params[0]])}));
500 } else {
501 that.app.panels().active.addMsg('', styleText('encoding_invalid', {text: translateText('client_models_application_encoding_invalid', [ev.params[0]])}));
502 }
503 });
504 } else {
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')}));
507 }
508 }
509
510
511 function channelCommand (ev) {
512 var active_panel = this.app.panels().active;
513
514 if (!active_panel.isChannel())
515 return;
516
517 new _kiwi.model.ChannelInfo({channel: this.app.panels().active});
518 }
519
520
521 function quitCommand (ev) {
522 var network = this.app.connections.active_connection;
523
524 if (!network)
525 return;
526
527 network.gateway.quit(ev.params.join(' '));
528 }
529
530
531 function serverCommand (ev) {
532 var that = this,
533 server, port, ssl, password, nick,
534 tmp;
535
536 // If no server address given, show the new connection dialog
537 if (!ev.params[0]) {
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);
540 tmp.show();
541
542 // Center screen the dialog
543 tmp.$el.offset({
544 top: (this.app.view.$el.height() / 2) - (tmp.$el.height() / 2),
545 left: (this.app.view.$el.width() / 2) - (tmp.$el.width() / 2)
546 });
547
548 return;
549 }
550
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(':');
554 server = tmp[0];
555 port = tmp[1];
556
557 password = ev.params[1] || undefined;
558
559 } else {
560 // Server + port given as 'host port'
561 server = ev.params[0];
562 port = ev.params[1] || 6667;
563
564 password = ev.params[2] || undefined;
565 }
566
567 // + in the port means SSL
568 if (port.toString()[0] === '+') {
569 ssl = true;
570 port = parseInt(port.substring(1), 10);
571 } else {
572 ssl = false;
573 }
574
575 // Default port if one wasn't found
576 port = port || 6667;
577
578 // Use the same nick as we currently have
579 nick = this.app.connections.active_connection.get('nick');
580
581 this.app.panels().active.addMsg('', styleText('server_connecting', {text: translateText('client_models_application_connection_connecting', [server, port.toString()])}));
582
583 _kiwi.gateway.newConnection({
584 nick: nick,
585 host: server,
586 port: port,
587 ssl: ssl,
588 password: password
589 }, function(err, new_connection) {
590 var translated_err_text = {text: translateText('client_models_application_connection_error', [server, port.toString(), err.toString()])};
591
592 if (err) {
593 that.app.panels().active.addMsg('', styleText('server_connecting_error', translated_err_text));
594 }
595 });
596 }
597
598 })();