Skip to content

Instantly share code, notes, and snippets.

@codler
Created April 14, 2013 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codler/5383971 to your computer and use it in GitHub Desktop.
Save codler/5383971 to your computer and use it in GitHub Desktop.
IRC bot, Node.js v0.6, 2011-12-15 Read and write to both irc and file. Two-way communication.
var util = require('util'),
net = require('net');
fs = require('fs');
vm = require('vm');
repl = require('repl');
var socket = new net.Socket();
socket.setNoDelay(true);
socket.setEncoding('ascii');
var buffer = {
b: new Buffer(4096),
size: 0
};
var textFile = '';
var channel = '#someChannel';
var logfile = 'C:\\irc_chatt_bot.txt';
fs.readFile(logfile, 'ascii', function (err, data) {
if (err) throw err;
textFile = data.replace(/\r/g, '').split('\n');
});
fs.watch(logfile, function (event, filename) {
console.log('event is: ' + event);
if (filename) {
console.log('filename provided: ' + filename);
// find new lines
fs.readFile(logfile, 'ascii', function (err, data) {
if (err) return;
//if (err) throw err;
comp(data.replace(/\r/g, '').split('\n'));
});
} else {
console.log('filename not provided');
}
});
function comp(n) {
var clone = n.slice(0);
for(var i = textFile.length; i--; ) {
var index = n.indexOf(textFile[i]);
if (index > -1) {
n.splice(index, 1);
}
}
for(var i = 0; i < n.length; i++) {
//console.log('new' + n[i]);
if (n[i].length > 0) {
send("PRIVMSG " + sanitize(channel) + " :\x01ACTION " + sanitize(n[i]) + "\x01");
}
}
textFile = clone;
}
function send(data) {
if(!data || data.length == 0) {
console.log("ERROR tried to send no data");
return;
} else if (data.length > 510) {
console.log("ERROR tried to send data > 510 chars in length: " + data);
return;
}
socket.write(data + '\r\n', 'ascii', function () {
console.log("-> " + data);
});
}
socket.on('data', function (data) {
data = data.replace('\r', '');
var newlineIdx;
while ((newlineIdx = data.indexOf('\n')) > -1) {
if (buffer.size > 0) {
data = buffer.b.toString('ascii', 0, buffer.size) + data;
newlineIdx += buffer.size;
buffer.size = 0;
}
handle(data.substr(0, newlineIdx));
data = data.slice(newlineIdx + 1);
}
if (data.length > 0) {
buffer.b.write(data, buffer.size, 'ascii');
buffer.size += data.length;
}
});
function handle(data) {
console.log("<- " + data);
match = /^PING :(.+)/i.exec(data);
if (match) {
send("PONG :" + match[1]);
}
if (data.indexOf('Welcome to the QuakeNet IRC Network') > -1) {
send("JOIN :" + sanitize(channel));
}
var replyTo = null;
if (data.indexOf('PRIVMSG') > -1) {
var dest = (/^:([^!]+)!.*PRIVMSG ([^ ]+) /i).exec(data);
match = /PRIVMSG [^ ]+ :say (.*)/i.exec(data);
if (match) {
console.log(dest[1] + ': ' + match[1]);
textFile.push(dest[1] + ': ' + match[1]);
//writeStream.write('\r\n' + dest[1] + ': ' + match[1]);
fs.open(logfile, 'a', 666, function( e, id ) {
fs.write( id, '\r\n' + dest[1] + ': ' + match[1], null, 'ascii', function(){
fs.close(id, function(){
console.log('file closed');
//send("PRIVMSG " + sanitize(channel) + " :\x01ACTION " + sanitize(match[1]) + "\x01");
});
});
});
}
}
}
function sanitize(data) {
if (!data) {
return data;
}
/* Note:
* 0x00 (null character) is invalid
* 0x01 signals a CTCP message, which we shouldn't ever need to do
* 0x02 is bold in mIRC (and thus other GUI clients)
* 0x03 precedes a color code in mIRC (and thus other GUI clients)
* 0x04 thru 0x19 are invalid control codes, except for:
* 0x16 is "reverse" (swaps fg and bg colors) in mIRC
*/
return data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/[^\x02-\x03|\x16|\x20-\x7e]/g, "");
}
function connect(host, port, nickname, username, realname) {
var port = port || 6667;
socket.connect(port, host, function () {
send('NICK ' + sanitize(nickname));
send('USER ' + sanitize(username) + ' tolmoon tolsun :' + sanitize(realname));
});
}
connect('se.quakenet.org', 6667, 'somenickname', 'someusername', 'somerealname');
repl.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment