Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Command store and better command handler

This doesn't add and functionality right now, but it can be used for
functionalities like autocomplete, help text.

Signed-off-by: Rohan Jain <crodjer@gmail.com>
  • Loading branch information...
commit 80c335b5c47ce7465aa64e69f61a56cca1680769 1 parent 8fe5ae4
Rohan Jain crodjer authored
Showing with 113 additions and 60 deletions.
  1. +109 −56 assets/js/client.js
  2. +4 −4 assets/js/views/chat.js
165 assets/js/client.js
View
@@ -349,66 +349,119 @@ $(function() {
}
});
- irc.handleCommand = function(commandText) {
- switch (commandText[0]) {
- case '/join':
- irc.socket.emit('join', commandText[1]);
- break;
- case '/wc':
- case '/close':
- case '/part':
- if (commandText[1]) {
- irc.socket.emit('part', commandText[1]);
- irc.appView.channelList.channelTabs[0].setActive();
- } else {
- irc.socket.emit('part', irc.chatWindows.getActive().get('name'));
- irc.appView.channelList.channelTabs[0].setActive();
+ irc.commands = (function(){
+ var commandStore = {
+ 'default': function(args, command){
+ // Do nothing, override it;
+ },
+ };
+
+ that = {
+ add: function(name, handler, helpText){
+ commandStore[name] = {
+ handler: handler,
+ help: helpText
+ };
+ },
+
+ alias: function(name, toName){
+ commandStore[name] = commandStore[toName];
+ },
+
+ lookup: function(string){
+ var command, results = [];
+ string = string.toLowerCase();
+
+ for (command in commandStore) {
+ if (commandStore.hasOwnProperty(command)){
+ if (command.toLowerCase().indexOf(string) === 0 && string.replace(/\s+/g, '')) {
+ results.push(command);
+ }
+ }
}
- break;
+ return results.sort();
+ },
- case '/nick':
- if (commandText[1]) {
- irc.socket.emit('nick', {nick : commandText[1]});
- }
- break;
- case '/topic':
- if (commandText[2]) {
- irc.socket.emit('topic', {name: commandText[1], topic: commandText[2]});
- } else {
- irc.socket.emit('topic', {name: irc.chatWindows.getActive().get('name'),
- topic: commandText[1]});
- }
- break;
- case '/whois':
- if (commandText[1]){
- irc.socket.emit('whois', {nick: commandText[1]});
- }
- break;
- case '/me':
- irc.socket.emit('action', {
- target: irc.chatWindows.getActive().get('name'),
- message: commandText.splice(1).join(" ")
- });
- break;
- case '/query':
- case '/privmsg':
- case '/msg':
- var target = commandText[1].toLowerCase();
- var myNick = irc.me.get('nick');
- if (typeof irc.chatWindows.getByName(target) === 'undefined') {
- irc.chatWindows.add({name: target, type: 'pm'});
+ handle: function(args){
+ var command = args[0];
+ var handler = commandStore[command] && commandStore[command].handler;
+
+ args.splice(0, 1);
+
+ if (typeof(handler) === 'function'){
+ handler(args, command);
}
- irc.socket.emit('getOldMessages',{channelName: target, skip:0, amount: 50});
- irc.socket.emit('say', {
- target: target,
- message: commandText.splice(2).join(" ")
- });
- break;
- default:
- commandText[0] = commandText[0].substr(1).toUpperCase();
- irc.socket.emit('command', commandText);
+ }
+ };
+
+ return that;
+
+ }());
+
+ irc.commands.add('join', function(args){
+ irc.socket.emit('join', args[0]);
+ });
+
+ irc.commands.add('part', function(args){
+ if (args[0]) {
+ irc.socket.emit('part', args[0]);
+ irc.appView.channelList.channelTabs[0].setActive();
+ } else {
+ irc.socket.emit('part', irc.chatWindows.getActive().get('name'));
+ irc.appView.channelList.channelTabs[0].setActive();
+ }
+ });
+ irc.commands.alias('wc', 'part');
+ irc.commands.alias('close', 'part');
+
+ irc.commands.add('nick', function(args){
+ if (args[0]) {
+ irc.socket.emit('nick', {nick : args[0]});
}
- };
+ });
+
+ irc.commands.add('topic', function(args){
+ if (args[1]) {
+ irc.socket.emit('topic', {name: args[0], topic: args[1]});
+ } else {
+ irc.socket.emit('topic', {name: irc.chatWindows.getActive().get('name'),
+ topic: args[0]});
+ }
+ });
+
+ irc.commands.add('whois', function(args){
+ if (args[0]){
+ irc.socket.emit('whois', {nick: args[0]});
+ }
+ });
+
+ irc.commands.add('me', function(args){
+ irc.socket.emit('action', {
+ target: irc.chatWindows.getActive().get('name'),
+ message: args.join(" ")
+ });
+ });
+
+ irc.commands.add('query', function(args){
+ var target = args[0].toLowerCase();
+ var myNick = irc.me.get('nick');
+ if (typeof irc.chatWindows.getByName(target) === 'undefined') {
+ irc.chatWindows.add({name: target, type: 'pm'});
+ }
+ irc.socket.emit('getOldMessages',{channelName: target, skip:0, amount: 50});
+ irc.socket.emit('say', {
+ target: target,
+ message: args.splice(1).join(" ")
+ });
+ });
+ irc.commands.alias('msg', 'query');
+ irc.commands.alias('privmsg', 'query');
+
+ irc.commands.add('default', function(args, command){
+ command = command.substr(1).toUpperCase();
+ args.unshift(command);
+ irc.socket.emit('command', args);
+ });
});
8 assets/js/views/chat.js
View
@@ -39,8 +39,8 @@ var ChatView = Backbone.View.extend({
$('#chat-button').click( function(){
var message = $('#chat-input').val();
if (message.substr(0, 1) === '/') {
- var commandText = message.split(' ');
- irc.handleCommand(commandText);
+ var commandText = message.substr(1).split(' ');
+ irc.commands.handle(commandText);
} else {
irc.socket.emit('say', {target: irc.chatWindows.getActive().get('name'), message:message});
}
@@ -75,8 +75,8 @@ var ChatView = Backbone.View.extend({
var message = $(this).val();
// Handle IRC commands
if (message.substr(0, 1) === '/') {
- var commandText = message.split(' ');
- irc.handleCommand(commandText);
+ var commandText = message.substr(1).split(' ');
+ irc.commands.handle(commandText);
} else {
// Send the message
console.log(irc.chatWindows.getActive().get('name'));
Please sign in to comment.
Something went wrong with that request. Please try again.