Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #170 from crodjer/master

Better comand handling, whois command
  • Loading branch information...
commit be164f8f8418852aa9e6472c74f10d09914ba2d1 2 parents 8dcd4f5 + 80c335b
@thedjpetersen authored
View
181 assets/js/client.js
@@ -149,6 +149,27 @@ $(function() {
irc.chatWindows.getByName('status').stream.add(message);
});
+ // Whois data
+ irc.socket.on('whois', function(data){
+ var key, message;
+ var stream = irc.chatWindows.getByName('status').stream;
+ if (data.info){
+ var info = {
+ " ": data.info.nick + ' (' + (data.info.user + '@' + data.info.host) + ')',
+ realname: data.info.realname,
+ server: data.info.server + ' (' + data.info.serverinfo + ')',
+ account: data.info.account
+ };
+
+ for (key in info){
+ if (info.hasOwnProperty(key)){
+ message = new Message({sender: key, raw: info[key], type: 'whois'});
+ stream.add(message);
+ }
+ }
+ }
+ });
+
irc.socket.on('message', function(data) {
var chatWindow = irc.chatWindows.getByName(data.to.toLowerCase());
var type = 'message';
@@ -328,61 +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 '/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);
+ });
});
View
8 assets/js/views/chat.js
@@ -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'));
View
1  lib/irclink.js
@@ -58,6 +58,7 @@ var IRCLink = function(hostname, port, ssl, selfSigned, nick, realName, password
'pm': ['nick', 'text'],
'registered': ['message'],
'motd': ['motd'],
+ 'whois': ['info'],
'error': ['message'],
'netError': ['message']
};
View
5 lib/socket.js
@@ -136,6 +136,11 @@ module.exports = function(socket, connections) {
);
});
+ socket.removeAllListeners('whois');
+ socket.on('whois', function(data) {
+ connection.client.whois(data.nick);
+ });
+
socket.removeAllListeners('topic');
socket.on('topic', function(data){
connection.client.send('TOPIC ', data.name, data.topic);
Please sign in to comment.
Something went wrong with that request. Please try again.