Created
June 23, 2014 00:31
-
-
Save t3h2mas/f68701479b157a1edd05 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 blessed = require('blessed'); | |
var screen = blessed.screen(); | |
// FOR THE GLORY OF THE LOG | |
var fs = require('fs'); | |
var log = require('npmlog'); | |
var stream = fs.createWriteStream('loggit.duh'); | |
log.stream = stream; | |
log.heading = "node irclient"; | |
var irc = require('irc'); | |
var client = new irc.Client('irc.esper.net', 'nodecli', { | |
channels: ['#smth'], | |
}); | |
var box = blessed.box({ | |
bottom: 0, | |
left: 'center', | |
height: 3, | |
width: '100%', | |
tags: true, | |
}); | |
screen.append(box); | |
var buffer = blessed.Text({ | |
top: 0, | |
width: '100%', | |
height: '93%', | |
border: { | |
type: 'line' | |
} | |
}); | |
screen.append(buffer) | |
var form = blessed.Form({ | |
}); | |
box.append(form); | |
var text = blessed.Textbox({ | |
name: 'input', | |
inputOnFocus: true, | |
keys: true, | |
border: { | |
type: 'line' | |
} | |
}); | |
box.append(text); | |
text.key('enter', function (ch, key) { | |
// do work with text.getValue() | |
msg = text.getValue() | |
if (msg == '/quit') | |
process.exit(0); | |
buffer.setText(buffer.getText() + '\n' + 'nodecli: ' + msg); | |
client.say('#smth', msg); | |
text.clearValue() | |
text.focus() | |
screen.render(); | |
}); | |
// irc | |
client.on('message', function (from, to, message) { | |
buffer.setText(buffer.getText() + '\n' + to + ':' + from + ' > ' + message); | |
screen.render(); | |
}); | |
client.on('join', function (channel, nick, message) { // test this | |
bufferAppend('* JOIN ' + channel + ' *'); | |
}); | |
client.on('names', function (channel, nicks) { | |
var current = buffer.getText() + '\n' | |
var i = 0; | |
log.info('debug', 'nicks: %j', nicks); | |
Object.keys(nicks).forEach(function (nick) { | |
current = current + ', ' + nick; | |
i++; | |
if (i == 8) { // hardcoded | |
current = current + '\n'; | |
i = 0; | |
} | |
}); | |
buffer.setText(current + '\n'); | |
screen.render(); | |
}); | |
client.on('registered', function (msg) { | |
bufferAppend('* CONNECTED ' + msg.server + ' *'); | |
}); | |
// BUG -- MOTD fills buffer, buffer won't scroll. | |
//client.on('motd', function (motd) { | |
// bufferAppend(motd); | |
//}); | |
screen.key(['escape', 'C-c'], function (ch, key) { | |
cleanup(); | |
}); | |
function bufferAppend(message) { | |
buffer.setText(buffer.getText() + '\n' + message); | |
screen.render() | |
}; | |
function cleanup(msg) { | |
var m = msg || 'hasta luego \o'; | |
// BUG HERE. what happens when there are no channels? | |
client.chans.forEach(function (chan) { | |
client.part(chan, m, function () {}); | |
}); | |
client.disconnect('BUHBYE'); | |
log.info('die', 'disconnected'); | |
stream.end(); | |
} | |
text.focus(); | |
screen.render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment