Created
May 22, 2013 14:37
-
-
Save hakobe/5628019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var util = require('util'); | |
var events = require('events'); | |
var Message = require('./message'); | |
function Client() { | |
var self = this; | |
events.EventEmitter.call(this); | |
self.on('raw', function(message) { | |
console.log(message.toRaw()); | |
self.emit( message.command.toLowerCase(), message ); | |
}); | |
} | |
util.inherits( Client, events.EventEmitter ); | |
Client.prototype.connect = function(name, channel) { | |
var self = this; | |
self.connection = net.connect({ | |
host : 'hubbard.freenode.net', | |
port : 6667, | |
}, function() { | |
self.connection.write(new Message( null, 'NICK', [name] ).toRaw()); | |
self.connection.write(new Message( null, 'USER', [name, '0', '*', name] ).toRaw()); | |
self.connection.write(new Message( null, 'JOIN', [channel] ).toRaw()); | |
}); | |
var buffer = ''; | |
self.connection.on('data', function(data) { | |
buffer += data; | |
var lines = buffer.split("\r\n"); | |
buffer = lines.pop(); | |
lines.forEach( function(line) { | |
self.emit( 'raw', Message.parse( line ) ) | |
}); | |
}); | |
}; | |
var client = new Client(); | |
client.connect('hakobe____', '#hakobe932'); | |
client.on('privmsg', function(message) { | |
if (message.params[1] === 'time') { | |
client.connection.write( | |
new Message( | |
null, | |
'PRIVMSG', | |
[ message.params[0], new Date().toString() ] | |
).toRaw() | |
); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Message(prefix, command, params) { | |
this.prefix = prefix; | |
this.command = command; | |
this.params = params || []; // Array | |
this.parseUser(); | |
this.parseServer(); | |
} | |
Message.parse = function(raw) { | |
var m; | |
m = raw.match(/^(?::([^ ]+)[ ]+)?([^ ]+)(.*)/); | |
var prefix = m[1]; | |
var command = m[2]; | |
var rawParams = m[3]; | |
m = rawParams.trim().match(/^(.*?)(?:^|\s+):(.*)$/); | |
var middle = (m ? m[1] : rawParams).trim(); | |
var trailing = m ? m[2] : null; | |
var params = middle === '' ? [] : middle.split(' '); | |
if (trailing) { | |
params.push(trailing); | |
} | |
return new Message( prefix, command, params ); | |
} | |
Message.prototype.toRaw = function() { | |
var result = ''; | |
if (this.prefix) { | |
result += ':' + this.prefix + ' '; | |
} | |
result += this.command + ' '; | |
var params = this.params.concat(); | |
var trailing = params[ params.length - 1]; | |
if ( | |
typeof(trailing) !== 'undefined' | |
&& (trailing.match(/\s/) || trailing === '') | |
) { | |
params[ params.length - 1] = ':' + trailing; | |
} | |
result += params.join(' '); | |
result += "\r\n"; | |
return result; | |
}; | |
Message.prototype.parseUser = function() { | |
if (!this.prefix) { return; } | |
var m; | |
m = this.prefix.match(/^(\S+?)(?:!|$)/); | |
if (m) { this.nick = m[1]; } | |
m = this.prefix.match(/!(\S+?)(?:@|$)/); | |
if (m) { this.user = m[1]; } | |
m = this.prefix.match(/@(\S+)$/); | |
if (m) { this.host = m[1]; } | |
}; | |
Message.prototype.parseServer = function() { | |
if (!this.prefix) { return; } | |
if (/[!@]/.test(this.prefix)) { return; } | |
this.server = this.prefix; | |
}; | |
module.exports = Message; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment