Last active
September 1, 2015 13:29
-
-
Save LunaSquee/6f103553c276a426b43d to your computer and use it in GitHub Desktop.
IRC utilities for node.js
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 net = require('net') | |
var readline = require('readline'); | |
var connected = false; | |
var socket = null; | |
var reconclock = null; | |
function spawn(ADDR, PORT, NICK) { | |
console.log("CREATE SOCK "+ADDR+":"+PORT); | |
socket = net.connect(PORT, ADDR, function() { | |
socket.write('USER icy 8 * :Icypone\r\n'); | |
socket.write('NICK '+NICK+'\r\n'); | |
connected = true; | |
}); | |
socket.setEncoding("utf8"); | |
socket.setTimeout(0); | |
socket.isCloseRequested = false; | |
var buffer = ""; | |
socket.on('data', function(chunk) { | |
buffer += chunk; | |
var data = buffer.split("\r\n"); | |
buffer = data.pop(); | |
data.forEach(function(line) { | |
if(line.indexOf("PING ") === 0) { | |
socket.write("PONG "+line.substring(5)+"\r\n"); | |
return; | |
} | |
var t = parseMsg(line, false); | |
console.log(t); | |
if(t.command === '005') { | |
socket.write("JOIN #BronyTalk\r\n"); | |
} | |
}); | |
}); | |
socket.on('error', function(data) { | |
console.log(data); | |
}); | |
socket.on('close', function(data) { | |
console.log("SERVER DISCONN "+ADDR+":"+PORT); | |
if(socket.isCloseRequested) { | |
process.exit(0); | |
} else { | |
socket.destroy(); | |
console.log("SERVER RECONN INIT "+ADDR+":"+PORT+" 10 SEC"); | |
reconclock = setTimeout(function() {spawn(ADDR, PORT, NICK);}, 10000); | |
} | |
connected = false; | |
}); | |
} | |
function parseMsg(input, stripColors) { | |
input = input.indexOf(":") === 0 ? input.substring(1) : input; | |
if (stripColors) { | |
input = input.replace(/[\x02\x1f\x16\x0f]|\x03\d{0,2}(?:,\d{0,2})?/g, ""); | |
} | |
var master = input.split(" ") | |
var prefix = master[0]; | |
var command = master[1] || null; | |
var arguments = master.slice(2).join(" "); | |
var trailing = null; | |
var sender = {"host": null, "user": null, "nick": null}; | |
if(arguments.indexOf(' :') != -1) { | |
var trailing = arguments.substring(arguments.indexOf(" :") + 2); | |
arguments = arguments.substring(0, arguments.indexOf(' :')); | |
} | |
arguments = arguments.split(' '); | |
if(prefix.indexOf("@") != -1) { | |
var rep = prefix.split("@"); | |
var per = rep[0].split("!"); | |
sender.host = rep[1]; | |
sender.user = per[1]; | |
sender.nick = per[0]; | |
} else { | |
sender.host = prefix; | |
} | |
return {"prefix":prefix, "command":command, "args":arguments, "trailing":trailing, "sender":sender,"raw":input}; | |
} | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.setPrompt(""); | |
rl.on('line', function (line) { | |
if (line === '') { | |
return; | |
} else { | |
if(connected) { | |
socket.write(line+"\r\n"); | |
} | |
} | |
rl.prompt(true); | |
}); | |
rl.on('SIGINT', function() { | |
rl.setPrompt(""); | |
if(connected === false) {process.exit(0); return;} | |
socket.write('QUIT :sigint or some shit\r\n', function() { | |
socket.isCloseRequested = true; | |
}); | |
}); | |
spawn("irc.canternet.org", 6667, "icyponie"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment