public
Last active

Node.js IRC bot

  • Download Gist
bot.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
var net = require('net'),
irc = {}, config;
 
config = {
user: {
nick: '',
user: '',
real: '',
pass: ''
},
server: {
addr: '',
port: 6667
},
chans: ['#webdevRefinery', '#test']
}
 
irc.socket = new net.Socket();
 
irc.socket.on('data', function(data)
{
data = data.split('\n');
for (var i = 0; i < data.length; i++)
{
console.log('RECV -', data[i]);
if (data !== '')
{
irc.handle(data[i].slice(0, -1));
}
}
});
 
irc.socket.on('connect', function()
{
console.log('Established connection, registering and shit...');
irc.on(/^PING :(.+)$/i, function(info)
{
irc.raw('PONG :' + info[1]);
});
setTimeout(function()
{
irc.raw('NICK ' + config.user.nick);
irc.raw('USER ' + config.user.user + ' 8 * :' + config.user.real);
setTimeout(function()
{
for (var i = 0; i < config.chans.length; i++)
{
irc.join(config.chans[i]);
}
}, 2000);
}, 1000);
});
 
irc.socket.setEncoding('ascii');
irc.socket.setNoDelay();
irc.socket.connect(config.server.port, config.server.addr);
 
//handles incoming messages
irc.handle = function(data)
{
var i, info;
for (i = 0; i < irc.listeners.length; i++)
{
info = irc.listeners[i][0].exec(data);
if (info)
{
irc.listeners[i][1](info, data);
if (irc.listeners[i][2])
{
irc.listeners.splice(i, 1);
}
}
}
}
 
irc.listeners = [];
irc.on = function(data, callback)
{
irc.listeners.push([data, callback, false])
}
irc.on_once = function(data, callback)
{
irc.listeners.push([data, callback, true]);
}
 
irc.raw = function(data)
{
irc.socket.write(data + '\n', 'ascii', function()
{
console.log('SENT -', data);
});
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.