3 ** CyTube Service Socket Commandline
6 const readline = require('readline');
7 const spawn = require('child_process').spawn;
8 const util = require('util');
9 const net = require('net');
10 const fs = require('fs');
24 var Config = require("./lib/config");
25 Config.load("config.yaml");
27 if(!Config.get("service-socket.enabled")){
28 console.error('The Service Socket is not enabled.');
32 const SOCKETFILE = Config.get("service-socket.socket");
35 process.stdout.write('\x1Bc');
37 var commandline, eventlog, syslog, errorlog;
38 var client = net.createConnection(SOCKETFILE).on('connect', () => {
39 commandline = readline.createInterface({
41 output: process.stdout,
42 completer: tabcomplete
44 commandline.setPrompt("> ", 2);
45 commandline.on("line", function(line) {
46 if(line === 'exit'){ return cleanup(); }
47 if(line === 'quit'){ return cleanup(); }
48 if(line.match(/^\/globalban/) && line.split(/\s+/).length === 2){
49 console.log('You must provide a reason')
50 return commandline.prompt();
55 commandline.on('close', function() {
58 commandline.on("SIGINT", function() {
59 commandline.clearLine();
60 commandline.question("Terminate connection? ", function(answer) {
61 return answer.match(/^y(es)?$/i) ? cleanup() : commandline.output.write("> ");
66 console.log = function() { cmdouthndlr("log", arguments); }
67 console.warn = function() { cmdouthndlr("warn", arguments); }
68 console.error = function() { cmdouthndlr("error", arguments); }
69 // console.info is reserved in this script for the exit message
70 // this prevents an extraneous final prompt from readline on terminate
72 eventlog = spawn('tail', ['-f', 'events.log']);
73 eventlog.stdout.on('data', function (data) {
74 console.log(data.toString().replace(/^(.+)$/mg, 'events: $1'));
77 syslog = spawn('tail', ['-f', 'sys.log']);
78 syslog.stdout.on('data', function (data) {
79 console.log(data.toString().replace(/^(.+)$/mg, 'sys: $1'));
82 errorlog = spawn('tail', ['-f', 'error.log']);
83 errorlog.stdout.on('data', function (data) {
84 console.log(data.toString().replace(/^(.+)$/mg, 'error: $1'));
87 }).on('data', (msg) => {
90 if(msg === '__disconnect'){
91 console.log('Server shutting down.');
95 // Generic message handler
96 console.log('server: ', data)
98 }).on('error', (data) => {
99 console.error('Unable to connect to Service Socket.', data);
103 function cmdouthndlr(type, args) {
104 var t = Math.ceil((commandline.line.length + 3) / process.stdout.columns);
105 var text = util.format.apply(console, args);
106 commandline.output.write("\n\x1B[" + t + "A\x1B[0J");
107 commandline.output.write(text + "\n");
108 commandline.output.write(Array(t).join("\n\x1B[E"));
109 commandline._refreshLine();
113 console.info('\n',"Terminating.",'\n');
114 eventlog.kill('SIGTERM');
115 syslog.kill('SIGTERM');
120 function tabcomplete(line) {
121 return [COMPLETIONS.filter((cv)=>{ return cv.indexOf(line) == 0; }), line];