Complete rewrite of the service socket client

Add one more command to the service commandline
This commit is contained in:
Xaekai 2016-07-20 03:01:34 -07:00
parent 9559035118
commit 670cb97e79
2 changed files with 99 additions and 27 deletions

View File

@ -97,6 +97,18 @@ function handleLine(line) {
} }
}) })
} }
} else if (line.indexOf("/unloadchan") === 0) {
var args = line.split(/\s+/); args.shift();
if(args.length){
var name = args.shift();
var chan = sv.getChannel(name);
var users = Array.prototype.slice.call(chan.users);
chan.emit("empty");
users.forEach(function (u) {
u.kick("Channel shutting down");
});
Logger.eventlog.log("[acp] " + "SYSTEM" + " forced unload of " + name);
}
} }
} }

View File

@ -3,6 +3,23 @@
** CyTube Service Socket Commandline ** CyTube Service Socket Commandline
*/ */
const readline = require('readline');
const spawn = require('child_process').spawn;
const util = require('util');
const net = require('net');
const fs = require('fs');
const COMPLETIONS = [
"/delete_old_tables",
"/gc",
"/globalban",
"/reload",
"/reload-partitions",
"/switch",
"/unglobalban",
"/unloadchan"
];
var Config = require("./lib/config"); var Config = require("./lib/config");
Config.load("config.yaml"); Config.load("config.yaml");
@ -12,13 +29,56 @@ if(!Config.get("service-socket.enabled")){
} }
const SOCKETFILE = Config.get("service-socket.socket"); const SOCKETFILE = Config.get("service-socket.socket");
var net = require('net');
var client = net.createConnection(SOCKETFILE) // Wipe the TTY
.on('connect', () => { process.stdout.write('\x1Bc');
console.log("Connected.");
}) var commandline, eventlog, syslog;
.on('data', (msg) => { var client = net.createConnection(SOCKETFILE).on('connect', () => {
commandline = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: tabcomplete
});
commandline.setPrompt("> ", 2);
commandline.on("line", function(line) {
if(line === 'exit'){ return cleanup(); }
if(line === 'quit'){ return cleanup(); }
if(line.match(/^\/globalban/) && line.split(/\s+/).length === 2){
console.log('You must provide a reason')
return commandline.prompt();
}
client.write(line);
commandline.prompt();
});
commandline.on('close', function() {
return cleanup();
});
commandline.on("SIGINT", function() {
commandline.clearLine();
commandline.question("Terminate connection? ", function(answer) {
return answer.match(/^y(es)?$/i) ? cleanup() : commandline.output.write("> ");
});
});
commandline.prompt();
console.log = function() { cmdouthndlr("log", arguments); }
console.warn = function() { cmdouthndlr("warn", arguments); }
console.error = function() { cmdouthndlr("error", arguments); }
// console.info is reserved in this script for the exit message
// this prevents an extraneous final prompt from readline on terminate
eventlog = spawn('tail', ['-f', 'events.log']);
eventlog.stdout.on('data', function (data) {
console.log(data.toString().replace(/^(.+)$/mg, 'events: $1'));
});
syslog = spawn('tail', ['-f', 'sys.log']);
syslog.stdout.on('data', function (data) {
console.log(data.toString().replace(/^(.+)$/mg, 'sys: $1'));
});
}).on('data', (msg) => {
msg = msg.toString(); msg = msg.toString();
if(msg === '__disconnect'){ if(msg === '__disconnect'){
@ -27,30 +87,30 @@ var client = net.createConnection(SOCKETFILE)
} }
// Generic message handler // Generic message handler
console.info('Server:', data) console.log('server: ', data)
})
.on('error', (data) => {
console.error('Unable to connect to Service Socket.');
process.exit(1);
})
;
var inputbuffer = ""; }).on('error', (data) => {
process.stdin.on("data", (data) => { console.error('Unable to connect to Service Socket.', data);
inputbuffer += data; process.exit(1);
if (inputbuffer.indexOf("\n") !== -1) { });
var line = inputbuffer.substring(0, inputbuffer.indexOf("\n"));
inputbuffer = inputbuffer.substring(inputbuffer.indexOf("\n") + 1); function cmdouthndlr(type, args) {
// Let the client escape var t = Math.ceil((commandline.line.length + 3) / process.stdout.columns);
if(line === 'exit'){ return cleanup(); } var text = util.format.apply(console, args);
if(line === 'quit'){ return cleanup(); } commandline.output.write("\n\x1B[" + t + "A\x1B[0J");
client.write(line); commandline.output.write(text + "\n");
} commandline.output.write(Array(t).join("\n\x1B[E"));
}); commandline._refreshLine();
}
function cleanup(){ function cleanup(){
console.log('\n',"Terminating.",'\n'); console.info('\n',"Terminating.",'\n');
eventlog.stdin.end();
syslog.stdin.end();
client.end(); client.end();
process.exit(0); process.exit(0);
} }
process.on('SIGINT', cleanup);
function tabcomplete(line) {
return [COMPLETIONS.filter((cv)=>{ return cv.indexOf(line) == 0; }), line];
}