Skip to content

Commit

Permalink
Complete rewrite of the service socket client
Browse files Browse the repository at this point in the history
Add one more command to the service commandline
  • Loading branch information
Xaekai committed Jul 20, 2016
1 parent 9559035 commit f848af2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 26 deletions.
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
111 changes: 85 additions & 26 deletions servcmd.sh.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
** 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");
Config.load("config.yaml");

Expand All @@ -12,13 +29,55 @@ if(!Config.get("service-socket.enabled")){
}

const SOCKETFILE = Config.get("service-socket.socket");
var net = require('net');

var client = net.createConnection(SOCKETFILE)
.on('connect', () => {
console.log("Connected.");
})
.on('data', (msg) => {
// Wipe the TTY
process.stdout.write('\x1Bc');

var commandline, eventlog, syslog;
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.info = function() { cmdouthndlr("info", arguments); }
console.error = function() { cmdouthndlr("error", arguments); }

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();

if(msg === '__disconnect'){
Expand All @@ -27,30 +86,30 @@ var client = net.createConnection(SOCKETFILE)
}

// Generic message handler
console.info('Server:', data)
})
.on('error', (data) => {
console.error('Unable to connect to Service Socket.');
console.info('server: ', data)

}).on('error', (data) => {
console.error('Unable to connect to Service Socket.', data);
process.exit(1);
})
;

var inputbuffer = "";
process.stdin.on("data", (data) => {
inputbuffer += data;
if (inputbuffer.indexOf("\n") !== -1) {
var line = inputbuffer.substring(0, inputbuffer.indexOf("\n"));
inputbuffer = inputbuffer.substring(inputbuffer.indexOf("\n") + 1);
// Let the client escape
if(line === 'exit'){ return cleanup(); }
if(line === 'quit'){ return cleanup(); }
client.write(line);
}
});
});

function cmdouthndlr(type, args) {
var t = Math.ceil((commandline.line.length + 3) / process.stdout.columns);
var text = util.format.apply(console, args);
commandline.output.write("\n\x1B[" + t + "A\x1B[0J");
commandline.output.write(text + "\n");
commandline.output.write(Array(t).join("\n\x1B[E"));
commandline._refreshLine();
}

function cleanup(){
console.log('\n',"Terminating.",'\n');
eventlog.stdin.end();
syslog.stdin.end();
client.end();
process.exit(0);
}
process.on('SIGINT', cleanup);

function tabcomplete(line) {
return [COMPLETIONS.filter((cv)=>{ return cv.indexOf(line) == 0; }), line];
}

0 comments on commit f848af2

Please sign in to comment.