NetMail avail to oputil & export - WIP

This commit is contained in:
Bryan Ashby 2017-12-31 17:54:11 -07:00
parent 1c5a00313b
commit fc40641eeb
12 changed files with 961 additions and 420 deletions

View file

@ -8,7 +8,7 @@ const argv = require('./oputil_common.js').argv;
const initConfigAndDatabases = require('./oputil_common.js').initConfigAndDatabases;
const getHelpFor = require('./oputil_help.js').getHelpFor;
const getAreaAndStorage = require('./oputil_common.js').getAreaAndStorage;
const Errors = require('../../core/enig_error.js').Errors;
const Errors = require('../enig_error.js').Errors;
const async = require('async');
const fs = require('graceful-fs');

View file

@ -84,6 +84,14 @@ general information:
FILENAME_WC filename with * and ? wildcard support. may match 0:n entries
SHA full or partial SHA-256
FILE_ID a file identifier. see file.sqlite3
`,
MessageBase :
`usage: oputil.js mb <action> [<args>]
actions:
areafix CMD1 CMD2 ... ADDR sends an AreaFix NetMail to ADDR with the supplied command(s)
one or more commands may be supplied. commands that are multi
part such as "%COMPRESS ZIP" should be quoted.
`
};

View file

@ -7,6 +7,7 @@ const argv = require('./oputil_common.js').argv;
const printUsageAndSetExitCode = require('./oputil_common.js').printUsageAndSetExitCode;
const handleUserCommand = require('./oputil_user.js').handleUserCommand;
const handleFileBaseCommand = require('./oputil_file_base.js').handleFileBaseCommand;
const handleMessageBaseCommand = require('./oputil_message_base.js').handleMessageBaseCommand;
const handleConfigCommand = require('./oputil_config.js').handleConfigCommand;
const getHelpFor = require('./oputil_help.js').getHelpFor;
@ -26,19 +27,10 @@ module.exports = function() {
}
switch(argv._[0]) {
case 'user' :
handleUserCommand();
break;
case 'config' :
handleConfigCommand();
break;
case 'fb' :
handleFileBaseCommand();
break;
default:
return printUsageAndSetExitCode(getHelpFor('General'), ExitCodes.BAD_COMMAND);
case 'user' : return handleUserCommand();
case 'config' : return handleConfigCommand();
case 'fb' : return handleFileBaseCommand();
case 'mb' : return handleMessageBaseCommand();
default : return printUsageAndSetExitCode(getHelpFor('General'), ExitCodes.BAD_COMMAND);
}
};

View file

@ -0,0 +1,150 @@
/* jslint node: true */
/* eslint-disable no-console */
'use strict';
const printUsageAndSetExitCode = require('./oputil_common.js').printUsageAndSetExitCode;
const ExitCodes = require('./oputil_common.js').ExitCodes;
const argv = require('./oputil_common.js').argv;
const initConfigAndDatabases = require('./oputil_common.js').initConfigAndDatabases;
const getHelpFor = require('./oputil_help.js').getHelpFor;
const Address = require('../ftn_address.js');
const Errors = require('../enig_error.js').Errors;
// deps
const async = require('async');
exports.handleMessageBaseCommand = handleMessageBaseCommand;
function areaFix() {
//
// oputil mb areafix CMD1 CMD2 ... ADDR [--password PASS]
//
if(argv._.length < 3) {
return printUsageAndSetExitCode(
getHelpFor('MessageBase'),
ExitCodes.ERROR
);
}
async.waterfall(
[
function init(callback) {
return initConfigAndDatabases(callback);
},
function validateAddress(callback) {
const addrArg = argv._.slice(-1)[0];
const ftnAddr = Address.fromString(addrArg);
if(!ftnAddr) {
return callback(Errors.Invalid(`"${addrArg}" is not a valid FTN address`));
}
//
// We need to validate the address targets a system we know unless
// the --force option is used
//
// :TODO:
return callback(null, ftnAddr);
},
function fetchFromUser(ftnAddr, callback) {
//
// --from USER || +op from system
//
// If possible, we want the user ID of the supplied user as well
//
const User = require('../user.js');
if(argv.from) {
User.getUserIdAndName(argv.from, (err, userId, fromName) => {
if(err) {
return callback(null, ftnAddr, argv.from, 0);
}
// fromName is the same as argv.from, but case may be differnet (yet correct)
return callback(null, ftnAddr, fromName, userId);
});
} else {
User.getUserName(User.RootUserID, (err, fromName) => {
return callback(null, ftnAddr, fromName || 'SysOp', err ? 0 : User.RootUserID);
});
}
},
function createMessage(ftnAddr, fromName, fromUserId, callback) {
//
// Build message as commands separated by line feed
//
// We need to remove quotes from arguments. These are required
// in the case of e.g. removing an area: "-SOME_AREA" would end
// up confusing minimist, therefor they must be quoted: "'-SOME_AREA'"
//
const messageBody = argv._.slice(2, -1).map(arg => {
return arg.replace(/["']/g, '');
}).join('\n') + '\n';
const Message = require('../message.js');
const message = new Message({
toUserName : argv.to || 'AreaFix',
fromUserName : fromName,
subject : argv.password || '',
message : messageBody,
areaTag : Message.WellKnownAreaTags.Private, // mark private
meta : {
FtnProperty : {
[ Message.FtnPropertyNames.FtnDestZone ] : ftnAddr.zone,
[ Message.FtnPropertyNames.FtnDestNetwork ] : ftnAddr.net,
[ Message.FtnPropertyNames.FtnDestNode ] : ftnAddr.node,
}
}
});
if(ftnAddr.point) {
message.meta.FtnProperty[Message.FtnPropertyNames.FtnDestPoint] = ftnAddr.point;
}
if(0 !== fromUserId) {
message.setLocalFromUserId(fromUserId);
}
return callback(null, message);
},
function persistMessage(message, callback) {
//
// :TODO:
// - Persist message in private outgoing (sysop out box)
// - Make necessary changes such that the message is exported properly
//
console.log(message);
message.persist(err => {
return callback(err);
});
}
],
err => {
if(err) {
process.exitCode = ExitCodes.ERROR;
console.error(`${err.message}${err.reason ? ': ' + err.reason : ''}`);
}
}
);
}
function handleMessageBaseCommand() {
function errUsage() {
return printUsageAndSetExitCode(
getHelpFor('MessageBase'),
ExitCodes.ERROR
);
}
if(true === argv.help) {
return errUsage();
}
const action = argv._[1];
return({
areafix : areaFix,
}[action] || errUsage)();
}