Created
June 13, 2011 17:05
-
-
Save ss23/1023194 to your computer and use it in GitHub Desktop.
This file contains 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 con = require('net').Socket(); | |
con.on('connect', connected); | |
con.setEncoding('utf8'); | |
con.connect(6667, 'irc.ss23.geek.nz'); | |
function connected() { | |
// Connected! | |
// As soon as we receive some data, we can write our NICK / USER | |
con.once('data', function() { | |
send('NICK ss23bot'); | |
send('USER ss23 ss23`bot ss23`bot :ss23`bot'); | |
}); | |
// Parse incoming data | |
con.on('data', function(data) { | |
// The socket will receive data in 'chunks', so we need to split it off, then process it | |
var lines = data.split('\n'); | |
for (i in lines) { | |
lines[i] = lines[i].replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); | |
if (lines[i] != '') { | |
require('util').log('Adding'); | |
process.nextTick( | |
(function(x) { | |
parse(lines[x]); | |
})(i) | |
); | |
} | |
} | |
}); | |
con.on('end', function() { | |
console.log('Connection Closed'); | |
}); | |
con.on('timeout', function() { | |
console.log('Connection Closed (timeout)'); | |
}); | |
con.on('close', function(had_error) { | |
console.log('Closed: ' + had_error); | |
}); | |
}; | |
function parse(msg) { | |
// Parse the incoming message | |
log(msg, false); | |
} | |
function send(msg) { | |
con.write(msg + '\n'); | |
log(msg, true); | |
} | |
/** | |
* Log a message. Like a fancified console.log | |
* @param string msg Message being logged | |
* @param bool send Is this message being sent or received? | |
*/ | |
function log(msg, send) { | |
// Oh, how I miss you default parameters! | |
send = typeof(send) != 'boolean' ? false : send; | |
// We can write to a file here too | |
var date = new Date(); | |
var date_out = '[' + date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + ']'; | |
if (send) { | |
console.log(date_out + '-> ' + msg); | |
} else { | |
console.log(date_out + '<- ' + msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment