sync/src/main.js

145 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-08-13 22:16:42 -07:00
import Config from './config';
import * as Switches from './switches';
2017-08-13 22:16:42 -07:00
import { eventlog } from './logger';
require('source-map-support').install();
2022-09-01 20:17:21 -07:00
import * as bannedChannels from './cli/banned-channels';
2017-08-13 22:16:42 -07:00
const LOGGER = require('@calzoneman/jsli')('main');
2020-12-02 18:09:49 -08:00
try {
Config.load('config.yaml');
} catch (e) {
LOGGER.fatal(
"Failed to load configuration: %s",
e
);
process.exit(1);
}
2017-08-13 22:16:42 -07:00
const sv = require('./server').init();
if (!Config.get('debug')) {
process.on('uncaughtException', error => {
LOGGER.fatal('Uncaught exception: %s', error.stack);
});
process.on('SIGINT', () => {
LOGGER.info('Caught SIGINT; shutting down');
sv.shutdown();
});
}
2022-09-01 20:17:21 -07:00
async function handleCliCmd(cmd) {
try {
switch (cmd.command) {
case 'ban-channel':
return await bannedChannels.handleBanChannel(cmd);
default:
throw new Error(`Unrecognized command "${cmd.command}"`);
}
} catch (error) {
return { status: 'error', error: String(error) };
}
}
2017-08-13 22:16:42 -07:00
// TODO: this can probably just be part of servsock.js
// servsock should also be refactored to send replies instead of
// relying solely on tailing logs
2022-09-01 20:17:21 -07:00
function handleLine(line, client) {
try {
let cmd = JSON.parse(line);
handleCliCmd(cmd).then(res => {
client.write(JSON.stringify(res) + '\n');
}).catch(error => {
LOGGER.error(`Unexpected error in handleCliCmd: ${error.stack}`);
});
} catch (_error) {
}
2017-08-13 22:16:42 -07:00
if (line === '/reload') {
LOGGER.info('Reloading config');
2020-12-02 18:09:49 -08:00
try {
Config.load('config.yaml');
} catch (e) {
LOGGER.error(
"Failed to load configuration: %s",
e
);
}
2018-03-05 21:46:58 -08:00
require('./web/pug').clearCache();
2017-08-13 22:16:42 -07:00
} else if (line.indexOf('/switch') === 0) {
const args = line.split(' ');
args.shift();
if (args.length === 1) {
LOGGER.info('Switch ' + args[0] + ' is ' +
(Switches.isActive(args[0]) ? 'ON' : 'OFF'));
} else if (args.length === 2) {
Switches.setActive(args[0], args[1].toLowerCase() === 'on' ? true : false);
LOGGER.info('Switch ' + args[0] + ' is now ' +
(Switches.isActive(args[0]) ? 'ON' : 'OFF'));
}
} else if (line.indexOf('/reload-partitions') === 0) {
sv.reloadPartitionMap();
} else if (line.indexOf('/save') === 0) {
sv.forceSave();
2017-08-13 22:16:42 -07:00
} else if (line.indexOf('/unloadchan') === 0) {
const args = line.split(/\s+/); args.shift();
if (args.length) {
const name = args.shift();
const chan = sv.getChannel(name);
const users = Array.prototype.slice.call(chan.users);
chan.emit('empty');
users.forEach(function (u) {
u.kick('Channel shutting down');
});
eventlog.log('[acp] ' + 'SYSTEM' + ' forced unload of ' + name);
}
} else if (line.indexOf('/reloadcert') === 0) {
sv.reloadCertificateData();
}
}
// Go Go Gadget Service Socket
if (Config.get('service-socket.enabled')) {
LOGGER.info('Opening service socket');
const ServiceSocket = require('./servsock');
const sock = new ServiceSocket();
2018-03-05 21:46:58 -08:00
sock.init(
2022-09-01 20:17:21 -07:00
(line, client) => {
2018-03-05 21:46:58 -08:00
try {
2022-09-01 20:17:21 -07:00
handleLine(line, client);
2018-03-05 21:46:58 -08:00
} catch (error) {
LOGGER.error(
'Error in UNIX socket command handler: %s',
error.stack
);
}
},
Config.get('service-socket.socket')
);
2017-08-13 22:16:42 -07:00
}
let stdinbuf = '';
process.stdin.on('data', function (data) {
stdinbuf += data;
if (stdinbuf.indexOf('\n') !== -1) {
let line = stdinbuf.substring(0, stdinbuf.indexOf('\n'));
stdinbuf = stdinbuf.substring(stdinbuf.indexOf('\n') + 1);
2018-03-05 21:46:58 -08:00
try {
handleLine(line);
} catch (error) {
LOGGER.error('Command line input handler failed: %s', error.stack);
}
2017-08-13 22:16:42 -07:00
}
});
// Hi I'm Mr POSIX! Look at me!
process.on('SIGUSR2', () => {
sv.reloadCertificateData();
});
require('bluebird');
2018-04-07 15:30:30 -07:00
process.on('unhandledRejection', function (reason, _promise) {
LOGGER.error('Unhandled rejection: %s', reason.stack);
2017-08-13 22:16:42 -07:00
});