happybot/ok/ircrepl.js
2018-08-14 19:24:26 -04:00

124 lines
3.3 KiB
JavaScript

#!/usr/bin/env node
var ok = require('./ircoK');
var fs = require('fs');
var os = require('os');
var readline = require('readline');
var conv = require('./ircconvert');
const spawnSync = require('child_process').spawnSync;
const crypto = require('crypto');
const happydir = '/tmp/happyok';
// web stuff
function curl(x) {
let url = conv.tojs(x);
let res = spawnSync('curl', ['-s', '-L', '--url', url], {'timeout': 5000});
if (res.stderr != "") {
throw new Error(res.stderr);
}
return res.stdout.toString('utf-8');
}
function readText(x) {
return conv.tok(curl(x));
}
function readJSON(x) {
return conv.tok(JSON.parse(curl(x)));
}
ok.setIO('0:', 1, readText);
ok.setIO('1:', 1, readJSON);
var env;
var printed = '';
// printing shit
function print(x, y) {
// ignore x for now
var s = conv.tojs(y);
if (Array.isArray(s)) { s = s.join('\n') + '\n'; }
if (typeof s !== 'string') { throw Error('ERROR: type'); }
printed += s;
return y;
}
ok.setIO('0:', 2, print);
ok.setIO('0:', 3, print);
ok.setIO('0:', 4, print);
ok.setIO('0:', 5, print);
// run user prelude file if exists
function resetEnv() {
env = ok.extendedEnv();
try {
var preludeFile = os.homedir() + "/offtopiabday/okrc.k"
var program = fs.readFileSync(preludeFile, 'utf8');
ok.run(ok.parse(program), env)
} catch (err) {
if (err.code != 'ENOENT') throw err
}
}
resetEnv();
// actual REPL
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function (line) {
if (line == "reset") {
resetEnv();
} else if (line == "manual") {
process.stdout.write("https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md\n");
} else if (line == "reference" || line == "ref") {
process.stdout.write("http://kparc.com/k.txt\n");
} else if (line == "okrc" || line == "rc") {
process.stdout.write("https://zgrep.org/taxreturns/okrc.txt\n");
} else if (line.replace(/\s|;/g, '') == '2+2') {
process.stdout.write("5\n");
} else {
var output = '';
printed = '';
var showtime = false;
if (line.lastIndexOf("\\t") == 0) {
line = line.slice(2);
showtime = true;
}
try {
if (line.trim()) {
var starttime = new Date().getTime();
output = ok.format(ok.run(ok.parse(line), env));
output = output.replace(/\n\s*/g, ";");
output += "\n";
if (showtime) {
var endtime = new Date().getTime();
output += "completed in "+(endtime-starttime)+"ms.\n";
}
}
} catch (err) {
output += err.message + '\n';
}
if (output.trim() || printed.trim()) {
// add newline where necessary
if (printed.slice(-1) != '\n') { printed += '\n'; }
if (! printed.trim()) { printed = printed.trim(); }
if (! output.trim()) { output = output.trim(); }
output = printed + output;
spl = output.split('\n');
if ( spl.length - 1 > 4 || Math.max.apply(null, spl.map(x => x.length)) > 400 ) {
// we need to pastebin it
// make dir
if (!fs.existsSync(happydir)) { fs.mkdirSync(happydir); }
// make fname
var fn = crypto.createHash('md5').update(output).digest('hex');
fn = fn.slice(0,10) + '.txt';
// write fname
fs.writeFileSync(happydir + '/' + fn, output);
output = 'https://zgrep.org/ok/' + fn + '\n'
}
// output checking code goes here.
process.stdout.write(output);
}
}
});