2018-04-16 02:25:56 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const Log = require('../../logger.js').log;
|
|
|
|
const { ServerModule } = require('../../server_module.js');
|
|
|
|
const Config = require('../../config.js').get;
|
|
|
|
const { Errors } = require('../../enig_error.js');
|
2018-04-16 02:25:56 +00:00
|
|
|
const {
|
2018-06-22 05:15:04 +00:00
|
|
|
splitTextAtTerms,
|
|
|
|
isAnsi,
|
2022-06-05 20:04:25 +00:00
|
|
|
stripAnsiControlCodes,
|
2022-08-21 19:27:32 +00:00
|
|
|
wildcardMatch,
|
2022-06-05 20:04:25 +00:00
|
|
|
} = require('../../string_util.js');
|
2018-04-16 02:25:56 +00:00
|
|
|
const {
|
2018-06-22 05:15:04 +00:00
|
|
|
getMessageConferenceByTag,
|
|
|
|
getMessageAreaByTag,
|
|
|
|
getMessageListForArea,
|
2022-08-21 19:27:32 +00:00
|
|
|
getAvailableMessageAreasByConfTag,
|
2022-06-05 20:04:25 +00:00
|
|
|
} = require('../../message_area.js');
|
|
|
|
const { sortAreasOrConfs } = require('../../conf_area_util.js');
|
|
|
|
const AnsiPrep = require('../../ansi_prep.js');
|
|
|
|
const { wordWrapText } = require('../../word_wrap.js');
|
|
|
|
const { stripMciColorCodes } = require('../../color_codes.js');
|
2018-04-16 02:25:56 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const net = require('net');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const paths = require('path');
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
const ModuleInfo = (exports.moduleInfo = {
|
|
|
|
name: 'Gopher',
|
|
|
|
desc: 'A RFC-1436-ish Gopher Server',
|
|
|
|
author: 'NuSkooler',
|
|
|
|
packageName: 'codes.l33t.enigma.gopher.server',
|
|
|
|
notes: 'https://tools.ietf.org/html/rfc1436',
|
|
|
|
});
|
|
|
|
|
|
|
|
const Message = require('../../message.js');
|
2018-04-16 02:25:56 +00:00
|
|
|
|
|
|
|
const ItemTypes = {
|
2022-06-05 20:04:25 +00:00
|
|
|
Invalid: '', // not really a type, of course!
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// Canonical, RFC-1436
|
2022-06-05 20:04:25 +00:00
|
|
|
TextFile: '0',
|
|
|
|
SubMenu: '1',
|
|
|
|
CCSONameserver: '2',
|
|
|
|
Error: '3',
|
|
|
|
BinHexFile: '4',
|
|
|
|
DOSFile: '5',
|
|
|
|
UuEncodedFile: '6',
|
|
|
|
FullTextSearch: '7',
|
|
|
|
Telnet: '8',
|
|
|
|
BinaryFile: '9',
|
|
|
|
AltServer: '+',
|
|
|
|
GIFFile: 'g',
|
|
|
|
ImageFile: 'I',
|
|
|
|
Telnet3270: 'T',
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// Non-canonical
|
2022-06-05 20:04:25 +00:00
|
|
|
HtmlFile: 'h',
|
|
|
|
InfoMessage: 'i',
|
|
|
|
SoundFile: 's',
|
2018-04-16 02:25:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class GopherModule extends ServerModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.routes = new Map(); // selector->generator => gopher item
|
|
|
|
this.log = Log.child({ server: 'Gopher' });
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:19:26 +00:00
|
|
|
createServer(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.enabled) {
|
2018-12-27 09:19:26 +00:00
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const config = Config();
|
|
|
|
this.publicHostname = config.contentServers.gopher.publicHostname;
|
2022-06-05 20:04:25 +00:00
|
|
|
this.publicPort = config.contentServers.gopher.publicPort;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.addRoute(
|
|
|
|
/^\/?msgarea(\/[a-z0-9_-]+(\/[a-z0-9_-]+)?(\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(_raw)?)?)?\/?\r\n$/,
|
|
|
|
this.messageAreaGenerator
|
|
|
|
);
|
2020-11-27 07:54:56 +00:00
|
|
|
this.addRoute(/^(\/?[^\t\r\n]*)\r\n$/, this.staticGenerator);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.server = net.createServer(socket => {
|
2018-06-22 05:15:04 +00:00
|
|
|
socket.setEncoding('ascii');
|
|
|
|
|
|
|
|
socket.on('data', data => {
|
2021-02-05 02:27:57 +00:00
|
|
|
// sanitize a bit - bots like to inject garbage
|
2021-02-05 02:32:28 +00:00
|
|
|
data = data.replace(/[^ -~\t\r\n]/g, '');
|
2021-02-05 02:27:57 +00:00
|
|
|
if (data) {
|
|
|
|
this.routeRequest(data, socket);
|
|
|
|
} else {
|
|
|
|
this.notFoundGenerator('**invalid selector**', res => {
|
|
|
|
return socket.end(`${res}`);
|
|
|
|
});
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('ECONNRESET' !== err.code) {
|
|
|
|
// normal
|
|
|
|
this.log.trace({ error: err.message }, 'Socket error');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-12-27 09:19:26 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:46:16 +00:00
|
|
|
listen(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.enabled) {
|
2018-12-27 09:46:16 +00:00
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const config = Config();
|
|
|
|
const port = parseInt(config.contentServers.gopher.port);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (isNaN(port)) {
|
|
|
|
this.log.warn(
|
|
|
|
{ port: config.contentServers.gopher.port, server: ModuleInfo.name },
|
2022-08-21 19:27:32 +00:00
|
|
|
'Invalid Gopher port'
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
|
|
|
return cb(
|
|
|
|
Errors.Invalid(`Invalid port: ${config.contentServers.gopher.port}`)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 02:24:52 +00:00
|
|
|
return this.server.listen(port, config.contentServers.gopher.address, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get enabled() {
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
_.get(Config(), 'contentServers.gopher.enabled', false) && this.isConfigured()
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isConfigured() {
|
2018-06-23 03:26:46 +00:00
|
|
|
// public hostname & port must be set; responses contain them!
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
_.isString(_.get(config, 'contentServers.gopher.publicHostname')) &&
|
|
|
|
_.isNumber(_.get(config, 'contentServers.gopher.publicPort'))
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addRoute(selectorRegExp, generatorHandler) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(selectorRegExp)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
try {
|
|
|
|
selectorRegExp = new RegExp(`${selectorRegExp}\r\n`);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.log.warn({ pattern: selectorRegExp }, 'Invalid RegExp for selector');
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.routes.set(selectorRegExp, generatorHandler.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
routeRequest(selector, socket) {
|
|
|
|
let match;
|
2022-06-05 20:04:25 +00:00
|
|
|
for (let [regex, gen] of this.routes) {
|
2018-06-22 05:15:04 +00:00
|
|
|
match = selector.match(regex);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (match) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return gen(match, res => {
|
|
|
|
return socket.end(`${res}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.notFoundGenerator(selector, res => {
|
|
|
|
return socket.end(`${res}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
makeItem(itemType, text, selector, hostname, port) {
|
2022-06-05 20:04:25 +00:00
|
|
|
selector = selector || ''; // e.g. for info
|
2018-06-22 05:15:04 +00:00
|
|
|
hostname = hostname || this.publicHostname;
|
|
|
|
port = port || this.publicPort;
|
|
|
|
return `${itemType}${text}\t${selector}\t${hostname}\t${port}\r\n`;
|
|
|
|
}
|
|
|
|
|
2020-11-27 07:54:56 +00:00
|
|
|
staticGenerator(selectorMatch, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.log.debug(
|
|
|
|
{ selector: selectorMatch[1] || '(gophermap)' },
|
|
|
|
'Serving static content'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-11-27 07:54:56 +00:00
|
|
|
const requestedPath = selectorMatch[1];
|
|
|
|
let path = this.resolveContentPath(requestedPath);
|
|
|
|
if (!path) {
|
|
|
|
return cb('Not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.stat(path, (err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
return cb('Not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
let isGopherMap = false;
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
path = paths.join(path, 'gophermap');
|
|
|
|
isGopherMap = true;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 07:54:56 +00:00
|
|
|
fs.readFile(path, isGopherMap ? 'utf8' : null, (err, content) => {
|
|
|
|
if (err) {
|
|
|
|
let content = 'You have reached an ENiGMA½ Gopher server!\r\n';
|
2022-06-05 20:04:25 +00:00
|
|
|
content += this.makeItem(
|
|
|
|
ItemTypes.SubMenu,
|
|
|
|
'Public Message Area',
|
|
|
|
'/msgarea'
|
|
|
|
);
|
2020-11-27 07:54:56 +00:00
|
|
|
return cb(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isGopherMap) {
|
|
|
|
// Convert any UNIX style LF's to DOS CRLF's
|
|
|
|
content = content.replace(/\r?\n/g, '\r\n');
|
|
|
|
|
|
|
|
// variable support
|
|
|
|
content = content
|
|
|
|
.replace(/{publicHostname}/g, this.publicHostname)
|
|
|
|
.replace(/{publicPort}/g, this.publicPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(content);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-27 07:54:56 +00:00
|
|
|
resolveContentPath(requestPath) {
|
|
|
|
const staticRoot = _.get(Config(), 'contentServers.gopher.staticRoot');
|
|
|
|
const path = paths.resolve(staticRoot, `.${requestPath}`);
|
|
|
|
if (path.startsWith(staticRoot)) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
notFoundGenerator(selector, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.log.debug({ selector }, 'Serving not found content');
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb('Not found');
|
|
|
|
}
|
|
|
|
|
2022-08-21 20:02:47 +00:00
|
|
|
_getConfigForConferenceTag(confTag) {
|
|
|
|
const sysConfig = Config();
|
|
|
|
let config = _.get(sysConfig, [
|
2022-06-05 20:04:25 +00:00
|
|
|
'contentServers',
|
|
|
|
'gopher',
|
2022-08-21 20:02:47 +00:00
|
|
|
'exposedConfAreas',
|
2022-06-05 20:04:25 +00:00
|
|
|
confTag,
|
|
|
|
]);
|
2022-08-21 20:02:47 +00:00
|
|
|
if (config) {
|
|
|
|
return [config, false]; // new
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
_.get(sysConfig, ['contentServers', 'gopher', 'messageConferences', confTag]),
|
|
|
|
true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
isAreaAndConfExposed(confTag, areaTag) {
|
|
|
|
const [confConfig, isLegacy] = this._getConfigForConferenceTag(confTag);
|
|
|
|
|
|
|
|
if (isLegacy) {
|
|
|
|
return Array.isArray(confConfig) && confConfig.includes(areaTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(confConfig.include)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let exposed = false;
|
|
|
|
for (let rule of confConfig.include) {
|
|
|
|
if (wildcardMatch(areaTag, rule)) {
|
|
|
|
exposed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// may still be excluded
|
|
|
|
for (let rule of confConfig.exclude || []) {
|
|
|
|
if (wildcardMatch(areaTag, rule)) {
|
|
|
|
exposed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exposed;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prepareMessageBody(body, cb) {
|
2018-11-21 04:02:30 +00:00
|
|
|
//
|
|
|
|
// From RFC-1436:
|
|
|
|
// "User display strings are intended to be displayed on a line on a
|
|
|
|
// typical screen for a user's viewing pleasure. While many screens can
|
|
|
|
// accommodate 80 character lines, some space is needed to display a tag
|
|
|
|
// of some sort to tell the user what sort of item this is. Because of
|
|
|
|
// this, the user display string should be kept under 70 characters in
|
|
|
|
// length. Clients may truncate to a length convenient to them."
|
|
|
|
//
|
|
|
|
// Messages on BBSes however, have generally been <= 79 characters. If we
|
|
|
|
// start wrapping earlier, things will generally be OK except:
|
|
|
|
// * When we're doing with FTN-style quoted lines
|
|
|
|
// * When dealing with ANSI/ASCII art
|
|
|
|
//
|
|
|
|
// Anyway, the spec says "should" and not MUST or even SHOULD! ...so, to
|
|
|
|
// to follow the KISS principle: Wrap at 79.
|
|
|
|
//
|
|
|
|
const WordWrapColumn = 79;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (isAnsi(body)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
AnsiPrep(
|
|
|
|
body,
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
cols: WordWrapColumn, // See notes above
|
|
|
|
forceLineTerm: true, // Ensure each line is term'd
|
|
|
|
asciiMode: true, // Export to ASCII
|
|
|
|
fillLines: false, // Don't fill up to |cols|
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
(err, prepped) => {
|
|
|
|
return cb(prepped || body);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
2018-12-15 08:55:38 +00:00
|
|
|
const cleaned = stripMciColorCodes(
|
2022-06-05 20:04:25 +00:00
|
|
|
stripAnsiControlCodes(body, { all: true })
|
2018-12-15 08:55:38 +00:00
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
const prepped = splitTextAtTerms(cleaned)
|
|
|
|
.map(l =>
|
|
|
|
(wordWrapText(l, { width: WordWrapColumn }).wrapped || []).join('\n')
|
|
|
|
)
|
|
|
|
.join('\n');
|
2018-11-21 04:02:30 +00:00
|
|
|
|
|
|
|
return cb(prepped);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shortenSubject(subject) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return _.truncate(subject, { length: 30 });
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messageAreaGenerator(selectorMatch, cb) {
|
2022-08-21 21:41:37 +00:00
|
|
|
this.log.trace({ selector: selectorMatch[0] }, 'Message area request');
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Selector should be:
|
|
|
|
// /msgarea - list confs
|
|
|
|
// /msgarea/conftag - list areas in conf
|
|
|
|
// /msgarea/conftag/areatag - list messages in area
|
|
|
|
// /msgarea/conftag/areatag/<UUID> - message as text
|
|
|
|
// /msgarea/conftag/areatag/<UUID>_raw - full message as text + headers
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (selectorMatch[3] || selectorMatch[4]) {
|
2022-08-21 19:27:32 +00:00
|
|
|
// message selector - display message
|
2018-06-23 03:26:46 +00:00
|
|
|
// message
|
2018-06-22 05:15:04 +00:00
|
|
|
//const raw = selectorMatch[4] ? true : false;
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: support 'raw'
|
2022-06-05 20:04:25 +00:00
|
|
|
const msgUuid = selectorMatch[3].replace(/\r\n|\//g, '');
|
|
|
|
const confTag = selectorMatch[1].substr(1).split('/')[0];
|
|
|
|
const areaTag = selectorMatch[2].replace(/\r\n|\//g, '');
|
2022-08-21 20:02:47 +00:00
|
|
|
return this._displayMessage(selectorMatch, msgUuid, confTag, areaTag, cb);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (selectorMatch[2]) {
|
2022-08-21 19:27:32 +00:00
|
|
|
// conf/area selector -- list messages in area
|
2022-06-05 20:04:25 +00:00
|
|
|
const confTag = selectorMatch[1].substr(1).split('/')[0];
|
|
|
|
const areaTag = selectorMatch[2].replace(/\r\n|\//g, '');
|
|
|
|
const area = getMessageAreaByTag(areaTag);
|
2022-08-21 20:02:47 +00:00
|
|
|
return this._listMessagesInArea(selectorMatch, confTag, areaTag, area, cb);
|
2022-08-21 19:27:32 +00:00
|
|
|
} else if (selectorMatch[1]) {
|
|
|
|
// message conference selector -- list areas in this conference
|
|
|
|
const confTag = selectorMatch[1].replace(/\r\n|\//g, '');
|
2022-08-21 20:02:47 +00:00
|
|
|
return this._listExposedMessageConferenceAreas(selectorMatch, confTag, cb);
|
2022-08-21 19:27:32 +00:00
|
|
|
} else {
|
|
|
|
// message area base selector -- list exposed message conferences
|
|
|
|
return this._listExposedMessageConferences(cb);
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
_makeAvailableMessageConferencesResponse(messageConferences, cb) {
|
|
|
|
sortAreasOrConfs(messageConferences);
|
|
|
|
|
|
|
|
const response = [
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, '-'.repeat(70)),
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, 'Available Message Conferences'),
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, '-'.repeat(70)),
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, ''),
|
|
|
|
...messageConferences.map(conf =>
|
|
|
|
this.makeItem(
|
|
|
|
ItemTypes.SubMenu,
|
|
|
|
`${conf.name} ${conf.desc ? '- ' + conf.desc : ''}`,
|
|
|
|
`/msgarea/${conf.confTag}`
|
|
|
|
)
|
|
|
|
),
|
|
|
|
].join('');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 21:41:37 +00:00
|
|
|
this.log.debug('Gopher serving message conference list');
|
2022-08-21 19:27:32 +00:00
|
|
|
return cb(response);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
_exposedMessageConferenceTags(obj) {
|
|
|
|
return Object.keys(obj || {})
|
|
|
|
.map(confTag =>
|
|
|
|
Object.assign({ confTag }, getMessageConferenceByTag(confTag))
|
|
|
|
)
|
|
|
|
.filter(conf => conf); // remove any baddies
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
_noExposedMessageConferences(cb) {
|
|
|
|
return cb(
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, 'No message conferences available')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// newer format
|
|
|
|
_listExposedMessageConferences(cb) {
|
|
|
|
let exposedConfs = _.get(Config(), 'contentServers.gopher.exposedConfAreas');
|
|
|
|
if (!_.isObject(exposedConfs)) {
|
|
|
|
return this._listExposedMessageConferencesLegacy(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
exposedConfs = this._exposedMessageConferenceTags(exposedConfs);
|
|
|
|
if (0 === exposedConfs.length) {
|
|
|
|
return this._noExposedMessageConferences(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._makeAvailableMessageConferencesResponse(exposedConfs, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// older deprecated format
|
|
|
|
_listExposedMessageConferencesLegacy(cb) {
|
|
|
|
const exposedConfs = this._exposedMessageConferenceTags(
|
|
|
|
_.get(Config(), 'contentServers.gopher.messageConferences')
|
|
|
|
);
|
|
|
|
|
|
|
|
if (0 === exposedConfs.length) {
|
|
|
|
return this._noExposedMessageConferences(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._makeAvailableMessageConferencesResponse(exposedConfs, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
_makeAvailableMessageAreasResponse(exposedConf, exposedAreas, cb) {
|
|
|
|
// ensure nothing private is present
|
|
|
|
exposedAreas = exposedAreas.filter(
|
|
|
|
area => area && !Message.isPrivateAreaTag(area.areaTag)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (0 === exposedAreas.length) {
|
|
|
|
return cb(this.makeItem(ItemTypes.InfoMessage, 'No message areas available'));
|
|
|
|
}
|
|
|
|
|
|
|
|
sortAreasOrConfs(exposedAreas);
|
|
|
|
|
|
|
|
const response = [
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, '-'.repeat(70)),
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, `Message areas in ${exposedConf.name}`),
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, '-'.repeat(70)),
|
|
|
|
...exposedAreas.map(area =>
|
|
|
|
this.makeItem(
|
|
|
|
ItemTypes.SubMenu,
|
|
|
|
`${area.name} ${area.desc ? '- ' + area.desc : ''}`,
|
|
|
|
`/msgarea/${exposedConf.confTag}/${area.areaTag}`
|
|
|
|
)
|
|
|
|
),
|
|
|
|
].join('');
|
|
|
|
|
2022-08-21 21:41:37 +00:00
|
|
|
this.log.debug(
|
|
|
|
{ confTag: exposedConf.confTag },
|
|
|
|
'Gopher serving message area list'
|
|
|
|
);
|
2022-08-21 19:27:32 +00:00
|
|
|
return cb(response);
|
|
|
|
}
|
|
|
|
|
2022-08-21 20:02:47 +00:00
|
|
|
_listExposedMessageConferenceAreas(selectorMatch, confTag, cb) {
|
2022-08-21 19:27:32 +00:00
|
|
|
//
|
|
|
|
// New system -- exposedConfAreas:
|
|
|
|
// We have a required array |include| of area tags that may
|
|
|
|
// contain wildcards and a _optional_ |exclude| array that
|
|
|
|
// overrides any includes
|
|
|
|
//
|
|
|
|
// Deprecated -- messageConferences:
|
|
|
|
// The key should point to an array of area tags
|
|
|
|
//
|
2022-08-21 20:02:47 +00:00
|
|
|
const [confConfig, isLegacy] = this._getConfigForConferenceTag(confTag);
|
2022-08-21 19:27:32 +00:00
|
|
|
const messageConference = getMessageConferenceByTag(confTag); // we need the actual conf!
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
if (!messageConference) {
|
|
|
|
return this.notFoundGenerator(selectorMatch, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
let areas;
|
|
|
|
if (isLegacy) {
|
2022-09-21 14:26:52 +00:00
|
|
|
areas = (confConfig || []).map(areaTag =>
|
2022-08-21 19:27:32 +00:00
|
|
|
Object.assign({ areaTag }, getMessageAreaByTag(areaTag))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// new system is more complex here, but nicer for the +op to manage
|
|
|
|
areas = getAvailableMessageAreasByConfTag(confTag);
|
|
|
|
if (!Array.isArray(confConfig.include)) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, 'No message areas available')
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
// filters |areas| down to what |includes| matches
|
|
|
|
areas = _.filter(areas, (area, areaTag) => {
|
2022-08-21 20:03:19 +00:00
|
|
|
for (let rule of confConfig.include) {
|
|
|
|
if (wildcardMatch(areaTag, rule)) {
|
2022-08-21 19:27:32 +00:00
|
|
|
area.areaTag = areaTag;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// now filter out any excludes, if present
|
|
|
|
if (Array.isArray(confConfig.exclude)) {
|
|
|
|
areas = _.filter(areas, area => {
|
2022-08-21 20:03:19 +00:00
|
|
|
for (let rule of confConfig.exclude) {
|
|
|
|
if (wildcardMatch(area.areaTag, rule)) {
|
2022-08-21 19:27:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._makeAvailableMessageAreasResponse(messageConference, areas, cb);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 20:02:47 +00:00
|
|
|
_listMessagesInArea(selectorMatch, confTag, areaTag, area, cb) {
|
2022-08-21 19:27:32 +00:00
|
|
|
if (Message.isPrivateAreaTag(areaTag)) {
|
|
|
|
this.log.warn({ areaTag }, `Gopher attempted access to private "${areaTag}"`);
|
|
|
|
return cb(this.makeItem(ItemTypes.InfoMessage, 'Area is private'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!area || !this.isAreaAndConfExposed(confTag, areaTag)) {
|
|
|
|
this.log.warn(
|
|
|
|
{ confTag, areaTag },
|
|
|
|
`Gopher attempted access to non-exposed "${confTag}"/"${areaTag}"`
|
|
|
|
);
|
|
|
|
return this.notFoundGenerator(selectorMatch, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
resultType: 'messageList',
|
|
|
|
sort: 'messageId',
|
|
|
|
order: 'descending', // we want newest messages first for Gopher
|
|
|
|
};
|
|
|
|
|
|
|
|
return getMessageListForArea(null, areaTag, filter, (err, msgList) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
const response = [
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, '-'.repeat(70)),
|
2022-08-21 19:27:32 +00:00
|
|
|
this.makeItem(ItemTypes.InfoMessage, `Messages in ${area.name}`),
|
|
|
|
this.makeItem(ItemTypes.InfoMessage, '(newest first)'),
|
2018-06-22 05:15:04 +00:00
|
|
|
this.makeItem(ItemTypes.InfoMessage, '-'.repeat(70)),
|
2022-08-21 19:27:32 +00:00
|
|
|
...msgList.map(msg =>
|
2022-06-05 20:04:25 +00:00
|
|
|
this.makeItem(
|
2022-08-21 19:27:32 +00:00
|
|
|
ItemTypes.TextFile,
|
|
|
|
`${moment(msg.modTimestamp).format(
|
|
|
|
'YYYY-MM-DD hh:mma'
|
|
|
|
)}: ${this.shortenSubject(msg.subject)} (${
|
|
|
|
msg.fromUserName
|
|
|
|
} to ${msg.toUserName})`,
|
|
|
|
`/msgarea/${confTag}/${areaTag}/${msg.messageUuid}`
|
2022-06-05 20:04:25 +00:00
|
|
|
)
|
|
|
|
),
|
2018-06-22 05:15:04 +00:00
|
|
|
].join('');
|
|
|
|
|
2022-08-21 21:41:37 +00:00
|
|
|
this.log.debug({ confTag, areaTag }, 'Gopher serving message list');
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(response);
|
2022-08-21 19:27:32 +00:00
|
|
|
});
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
|
2022-08-21 20:02:47 +00:00
|
|
|
_displayMessage(selectorMatch, msgUuid, confTag, areaTag, cb) {
|
2022-08-21 19:27:32 +00:00
|
|
|
const message = new Message();
|
|
|
|
|
|
|
|
return message.load({ uuid: msgUuid }, err => {
|
|
|
|
if (err) {
|
|
|
|
this.log.debug(
|
|
|
|
{ uuid: msgUuid },
|
|
|
|
'Attempted access to non-existent message UUID!'
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2022-08-21 19:27:32 +00:00
|
|
|
return this.notFoundGenerator(selectorMatch, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
if (
|
|
|
|
message.areaTag !== areaTag ||
|
|
|
|
!this.isAreaAndConfExposed(confTag, areaTag)
|
|
|
|
) {
|
|
|
|
this.log.warn(
|
|
|
|
{ areaTag },
|
|
|
|
`Gopher attempted access to non-exposed "${confTag}"/"${areaTag}"`
|
|
|
|
);
|
|
|
|
return this.notFoundGenerator(selectorMatch, cb);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
if (Message.isPrivateAreaTag(areaTag)) {
|
|
|
|
this.log.warn(
|
|
|
|
{ areaTag },
|
|
|
|
`Gopher attempted access to message in private "${areaTag}"`
|
|
|
|
);
|
|
|
|
return this.notFoundGenerator(selectorMatch, cb);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-08-21 19:27:32 +00:00
|
|
|
this.prepareMessageBody(message.message, msgBody => {
|
|
|
|
const response = `${'-'.repeat(70)}
|
|
|
|
To : ${message.toUserName}
|
|
|
|
From : ${message.fromUserName}
|
|
|
|
When : ${moment(message.modTimestamp).format('dddd, MMMM Do YYYY, h:mm:ss a (UTCZ)')}
|
|
|
|
Subject: ${message.subject}
|
|
|
|
ID : ${message.messageUuid} (${message.messageId})
|
|
|
|
${'-'.repeat(70)}
|
|
|
|
${msgBody}
|
|
|
|
`;
|
2022-08-21 21:41:37 +00:00
|
|
|
this.log.debug(
|
|
|
|
{
|
|
|
|
confTag,
|
|
|
|
areaTag,
|
|
|
|
uuid: message.messageUuid,
|
2022-08-21 21:43:53 +00:00
|
|
|
},
|
|
|
|
`Gopher serving message "${message.subject}"`
|
2022-08-21 21:41:37 +00:00
|
|
|
);
|
2022-08-21 19:27:32 +00:00
|
|
|
return cb(response);
|
|
|
|
});
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
};
|