2016-12-31 21:50:29 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// enigma-bbs
|
2017-11-24 23:23:15 +00:00
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
2016-12-31 21:50:29 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'File transfer protocol selection',
|
|
|
|
desc : 'Select protocol / method for file transfer',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
|
|
|
protList : 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class FileTransferProtocolSelectModule extends MenuModule {
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.config = this.menuConfig.config || {};
|
2017-01-02 04:53:04 +00:00
|
|
|
|
|
|
|
if(options.extraArgs) {
|
|
|
|
if(options.extraArgs.direction) {
|
|
|
|
this.config.direction = options.extraArgs.direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 21:50:29 +00:00
|
|
|
this.config.direction = this.config.direction || 'send';
|
|
|
|
|
2017-01-02 04:53:04 +00:00
|
|
|
this.extraArgs = options.extraArgs;
|
2016-12-31 21:50:29 +00:00
|
|
|
|
|
|
|
if(_.has(options, 'lastMenuResult.sentFileIds')) {
|
|
|
|
this.sentFileIds = options.lastMenuResult.sentFileIds;
|
|
|
|
}
|
|
|
|
|
2017-01-12 05:51:00 +00:00
|
|
|
if(_.has(options, 'lastMenuResult.recvFilePaths')) {
|
|
|
|
this.recvFilePaths = options.lastMenuResult.recvFilePaths;
|
|
|
|
}
|
|
|
|
|
2016-12-31 21:50:29 +00:00
|
|
|
this.fallbackOnly = options.lastMenuResult ? true : false;
|
|
|
|
|
2017-02-01 06:10:17 +00:00
|
|
|
this.loadAvailProtocols();
|
|
|
|
|
2016-12-31 21:50:29 +00:00
|
|
|
this.menuMethods = {
|
|
|
|
selectProtocol : (formData, extraArgs, cb) => {
|
|
|
|
const protocol = this.protocols[formData.value.protocol];
|
|
|
|
const finalExtraArgs = this.extraArgs || {};
|
|
|
|
Object.assign(finalExtraArgs, { protocol : protocol.protocol, direction : this.config.direction }, extraArgs );
|
|
|
|
|
|
|
|
const modOpts = {
|
|
|
|
extraArgs : finalExtraArgs,
|
|
|
|
};
|
|
|
|
|
|
|
|
if('send' === this.config.direction) {
|
2017-01-02 04:53:04 +00:00
|
|
|
return this.gotoMenu(this.config.downloadFilesMenu || 'sendFilesToUser', modOpts, cb);
|
2016-12-31 21:50:29 +00:00
|
|
|
} else {
|
2017-01-02 04:53:04 +00:00
|
|
|
return this.gotoMenu(this.config.uploadFilesMenu || 'recvFilesFromUser', modOpts, cb);
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getMenuResult() {
|
|
|
|
if(this.sentFileIds) {
|
|
|
|
return { sentFileIds : this.sentFileIds };
|
|
|
|
}
|
2017-01-12 05:51:00 +00:00
|
|
|
|
|
|
|
if(this.recvFilePaths) {
|
|
|
|
return { recvFilePaths : this.recvFilePaths };
|
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
2017-01-12 05:51:00 +00:00
|
|
|
if(this.sentFileIds || this.recvFilePaths) {
|
|
|
|
// nothing to do here; move along (we're just falling through)
|
2016-12-31 21:50:29 +00:00
|
|
|
this.prevMenu();
|
|
|
|
} else {
|
|
|
|
super.initSequence();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
const loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu
|
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
},
|
|
|
|
function populateList(callback) {
|
|
|
|
const protListView = vc.getView(MciViewIds.protList);
|
|
|
|
|
|
|
|
const protListFormat = self.config.protListFormat || '{name}';
|
|
|
|
const protListFocusFormat = self.config.protListFocusFormat || protListFormat;
|
|
|
|
|
|
|
|
protListView.setItems(self.protocols.map(p => stringFormat(protListFormat, p) ) );
|
|
|
|
protListView.setFocusItems(self.protocols.map(p => stringFormat(protListFocusFormat, p) ) );
|
|
|
|
|
|
|
|
protListView.redraw();
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadAvailProtocols() {
|
|
|
|
this.protocols = _.map(Config.fileTransferProtocols, (protInfo, protocol) => {
|
2017-02-01 06:10:17 +00:00
|
|
|
return {
|
2016-12-31 21:50:29 +00:00
|
|
|
protocol : protocol,
|
|
|
|
name : protInfo.name,
|
2017-02-01 06:10:17 +00:00
|
|
|
hasBatch : _.has(protInfo, 'external.recvArgs'),
|
|
|
|
hasNonBatch : _.has(protInfo, 'external.recvArgsNonBatch'),
|
2017-02-02 02:42:27 +00:00
|
|
|
sort : protInfo.sort,
|
2016-12-31 21:50:29 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-02-01 06:10:17 +00:00
|
|
|
// Filter out batch vs non-batch only protocols
|
|
|
|
if(this.extraArgs.recvFileName) { // non-batch aka non-blind
|
|
|
|
this.protocols = this.protocols.filter( prot => prot.hasNonBatch );
|
|
|
|
} else {
|
|
|
|
this.protocols = this.protocols.filter( prot => prot.hasBatch );
|
|
|
|
}
|
|
|
|
|
2017-02-02 02:42:27 +00:00
|
|
|
// natural sort taking explicit orders into consideration
|
|
|
|
this.protocols.sort( (a, b) => {
|
|
|
|
if(_.isNumber(a.sort) && _.isNumber(b.sort)) {
|
|
|
|
return a.sort - b.sort;
|
|
|
|
} else {
|
|
|
|
return a.name.localeCompare(b.name, { sensitivity : false, numeric : true } );
|
|
|
|
}
|
|
|
|
});
|
2016-12-31 21:50:29 +00:00
|
|
|
}
|
|
|
|
};
|