From 2870c4da1aedf41926972dd60227c3d62cdaa123 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 16 Jun 2012 14:24:33 +0200 Subject: o Polishing the dynobot a bit. --- Repl.js | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Repl.js (limited to 'Repl.js') diff --git a/Repl.js b/Repl.js new file mode 100644 index 0000000..cce93d0 --- /dev/null +++ b/Repl.js @@ -0,0 +1,82 @@ +var rl = require('readline'); + +function quit() { + i.close(); + process.stdin.destroy(); + process.exit(0); +} + +var i; + +module.exports.init = function() { + process.stdin.resume(); + i = rl.createInterface(process.stdin, process.stdout, null); +} + +module.exports.setupRepl = function() { +} + +module.exports.setupSimulatorRepl = function(config) { + var from = 'alice!~alice@example.com'; + var channel = config.channel; + + function updatePrompt() { + var prefix = 'from=' + from; + prefix += channel ? ', channel=' + channel : ''; + prefix += '> '; + i.setPrompt(prefix, prefix.length); + i.prompt(); + } + + i.on('line', function (cmd) { + cmd = cmd.trim(); + if(cmd.length == 0) { + i.prompt(); + return; + } + var parts = cmd.split(' '); + switch(parts[0]) { + case '/quit': + case '/q': + quit(); + break; + case '/raw': + case '/r': + parts.shift(); + stdinPipe.send(parts.join(' ')); + updatePrompt(); + break; + case '/join': + case '/j': + channel = parts[1]; + updatePrompt(); + break; + case '/from': + case '/f': + from = parts[1]; + updatePrompt(); + break; + case '/notice': + case '/n': + if(channel) { + parts.shift(); + stdinPipe.send(':' + from + ' NOTICE ' + channel + ' :' + parts.join(' ')); + } + else { + info('You have to /j a channel first'); + } + break; + default: + stdinPipe.send(':' + from + ' PRIVMSG ' + channel + ' :' + parts.join(' ')); + updatePrompt(); + break; + } + }).on('close', function() { + quit(); + }); + updatePrompt(); + + setTimeout(function() { + stdinPipe.send(':irc.foo.bar 001 this :Welcome to Some Internet Relay Chat Network this'); + }, 1000); +} -- cgit v1.2.3