2015-04-21 05:24:15 +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 { removeClient } = require('./client_connections.js');
|
|
|
|
const ansiNormal = require('./ansi_term.js').normal;
|
|
|
|
const { userLogin } = require('./user_login.js');
|
|
|
|
const messageArea = require('./message_area.js');
|
|
|
|
const { ErrorReasons } = require('./enig_error.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
|
|
|
const { loginFactor2_OTP } = require('./user_2fa_otp.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
|
|
|
|
exports.login = login;
|
|
|
|
exports.login2FA_OTP = login2FA_OTP;
|
|
|
|
exports.logoff = logoff;
|
|
|
|
exports.prevMenu = prevMenu;
|
|
|
|
exports.nextMenu = nextMenu;
|
|
|
|
exports.prevConf = prevConf;
|
|
|
|
exports.nextConf = nextConf;
|
|
|
|
exports.prevArea = prevArea;
|
|
|
|
exports.nextArea = nextArea;
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.sendForgotPasswordEmail = sendForgotPasswordEmail;
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.optimizeDatabases = optimizeDatabases;
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2019-04-10 02:07:19 +00:00
|
|
|
const handleAuthFailures = (callingMenu, err, cb) => {
|
|
|
|
// already logged in with this user?
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
ErrorReasons.AlreadyLoggedIn === err.reasonCode &&
|
|
|
|
_.has(callingMenu, 'menuConfig.config.tooNodeMenu')
|
|
|
|
) {
|
2019-04-10 02:07:19 +00:00
|
|
|
return callingMenu.gotoMenu(callingMenu.menuConfig.config.tooNodeMenu, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// banned username results in disconnect
|
2022-06-05 20:04:25 +00:00
|
|
|
if (ErrorReasons.NotAllowed === err.reasonCode) {
|
2019-04-10 02:07:19 +00:00
|
|
|
return logoff(callingMenu, {}, {}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReasonsMenus = [
|
2022-06-05 20:04:25 +00:00
|
|
|
ErrorReasons.TooMany,
|
|
|
|
ErrorReasons.Disabled,
|
|
|
|
ErrorReasons.Inactive,
|
|
|
|
ErrorReasons.Locked,
|
2019-04-10 02:07:19 +00:00
|
|
|
];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (ReasonsMenus.includes(err.reasonCode)) {
|
|
|
|
const menu = _.get(callingMenu, [
|
|
|
|
'menuConfig',
|
|
|
|
'config',
|
|
|
|
err.reasonCode.toLowerCase(),
|
|
|
|
]);
|
2019-04-10 02:07:19 +00:00
|
|
|
return menu ? callingMenu.gotoMenu(menu, cb) : logoff(callingMenu, {}, {}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other error
|
|
|
|
return callingMenu.prevMenu(cb);
|
|
|
|
};
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function login(callingMenu, formData, extraArgs, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
userLogin(
|
|
|
|
callingMenu.client,
|
|
|
|
formData.value.username,
|
|
|
|
formData.value.password,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return handleAuthFailures(callingMenu, err, cb);
|
|
|
|
}
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// success!
|
|
|
|
return callingMenu.nextMenu(cb);
|
2019-04-10 02:07:19 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2019-04-10 02:07:19 +00:00
|
|
|
}
|
2018-11-23 06:07:37 +00:00
|
|
|
|
2019-04-10 02:07:19 +00:00
|
|
|
function login2FA_OTP(callingMenu, formData, extraArgs, cb) {
|
2019-05-09 03:06:10 +00:00
|
|
|
loginFactor2_OTP(callingMenu.client, formData.value.token, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2019-04-10 02:07:19 +00:00
|
|
|
return handleAuthFailures(callingMenu, err, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// success!
|
2018-06-22 05:15:04 +00:00
|
|
|
return callingMenu.nextMenu(cb);
|
|
|
|
});
|
2015-04-21 05:24:15 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function logoff(callingMenu, formData, extraArgs, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Simple logoff. Note that recording of @ logoff properties/stats
|
|
|
|
// occurs elsewhere!
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const client = callingMenu.client;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
setTimeout(() => {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// For giggles...
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
client.term.write(
|
2022-06-05 20:04:25 +00:00
|
|
|
ansiNormal() +
|
|
|
|
'\n' +
|
|
|
|
iconv.decode(
|
|
|
|
require('crypto').randomBytes(Math.floor(Math.random() * 65) + 20),
|
|
|
|
client.term.outputEncoding
|
|
|
|
) +
|
|
|
|
'NO CARRIER',
|
|
|
|
null,
|
|
|
|
() => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// after data is written, disconnect & remove the client
|
2018-06-22 05:15:04 +00:00
|
|
|
removeClient(client);
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, 500);
|
2015-09-24 04:24:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function prevMenu(callingMenu, formData, extraArgs, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: this is a pretty big hack -- need the whole key map concep there like other places
|
2022-06-05 20:04:25 +00:00
|
|
|
if (formData.key && 'return' === formData.key.name) {
|
2018-06-22 05:15:04 +00:00
|
|
|
callingMenu.submitFormData = formData;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
callingMenu.prevMenu(err => {
|
|
|
|
if (err) {
|
|
|
|
callingMenu.client.log.error(
|
|
|
|
{ error: err.message },
|
|
|
|
'Error attempting to fallback!'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
});
|
2015-09-24 05:41:06 +00:00
|
|
|
}
|
2016-06-26 04:43:12 +00:00
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function nextMenu(callingMenu, formData, extraArgs, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
callingMenu.nextMenu(err => {
|
|
|
|
if (err) {
|
|
|
|
callingMenu.client.log.error(
|
|
|
|
{ error: err.message },
|
|
|
|
'Error attempting to go to next menu!'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
});
|
2016-06-26 04:43:12 +00:00
|
|
|
}
|
2016-07-12 09:57:07 +00:00
|
|
|
|
2018-07-18 02:00:47 +00:00
|
|
|
// :TODO: need redrawMenu() and MenuModule.redraw()
|
2016-07-25 20:35:58 +00:00
|
|
|
function reloadMenu(menu, cb) {
|
2018-07-18 02:00:47 +00:00
|
|
|
return menu.reload(cb);
|
2016-07-13 05:38:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function prevConf(callingMenu, formData, extraArgs, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const confs = messageArea.getSortedAvailMessageConferences(callingMenu.client);
|
|
|
|
const currIndex =
|
|
|
|
confs.findIndex(
|
|
|
|
e =>
|
|
|
|
e.confTag === callingMenu.client.user.properties[UserProps.MessageConfTag]
|
|
|
|
) || confs.length;
|
|
|
|
|
|
|
|
messageArea.changeMessageConference(
|
|
|
|
callingMenu.client,
|
|
|
|
confs[currIndex - 1].confTag,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err); // logged within changeMessageConference()
|
|
|
|
}
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function nextConf(callingMenu, formData, extraArgs, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const confs = messageArea.getSortedAvailMessageConferences(callingMenu.client);
|
|
|
|
let currIndex = confs.findIndex(
|
|
|
|
e => e.confTag === callingMenu.client.user.properties[UserProps.MessageConfTag]
|
|
|
|
);
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (currIndex === confs.length - 1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
currIndex = -1;
|
|
|
|
}
|
2016-07-13 05:38:40 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
messageArea.changeMessageConference(
|
|
|
|
callingMenu.client,
|
|
|
|
confs[currIndex + 1].confTag,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err); // logged within changeMessageConference()
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
|
|
|
}
|
|
|
|
);
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function prevArea(callingMenu, formData, extraArgs, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const areas = messageArea.getSortedAvailMessageAreasByConfTag(
|
|
|
|
callingMenu.client.user.properties[UserProps.MessageConfTag]
|
|
|
|
);
|
|
|
|
const currIndex =
|
|
|
|
areas.findIndex(
|
|
|
|
e =>
|
|
|
|
e.areaTag === callingMenu.client.user.properties[UserProps.MessageAreaTag]
|
|
|
|
) || areas.length;
|
|
|
|
|
|
|
|
messageArea.changeMessageArea(
|
|
|
|
callingMenu.client,
|
|
|
|
areas[currIndex - 1].areaTag,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err); // logged within changeMessageArea()
|
|
|
|
}
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function nextArea(callingMenu, formData, extraArgs, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const areas = messageArea.getSortedAvailMessageAreasByConfTag(
|
|
|
|
callingMenu.client.user.properties[UserProps.MessageConfTag]
|
|
|
|
);
|
|
|
|
let currIndex = areas.findIndex(
|
|
|
|
e => e.areaTag === callingMenu.client.user.properties[UserProps.MessageAreaTag]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (currIndex === areas.length - 1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
currIndex = -1;
|
|
|
|
}
|
2016-07-13 05:38:40 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
messageArea.changeMessageArea(
|
|
|
|
callingMenu.client,
|
|
|
|
areas[currIndex + 1].areaTag,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err); // logged within changeMessageArea()
|
|
|
|
}
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
|
|
|
}
|
|
|
|
);
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
2017-02-27 04:28:05 +00:00
|
|
|
|
|
|
|
function sendForgotPasswordEmail(callingMenu, formData, extraArgs, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const username = formData.value.username || callingMenu.client.user.username;
|
2017-02-27 04:28:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const WebPasswordReset = require('./web_password_reset.js').WebPasswordReset;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
WebPasswordReset.sendForgotPasswordEmail(username, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
callingMenu.client.log.warn(
|
|
|
|
{ err: err.message },
|
|
|
|
'Failed sending forgot password email'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-02-27 04:28:05 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (extraArgs.next) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callingMenu.gotoMenu(extraArgs.next, cb);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return logoff(callingMenu, formData, extraArgs, cb);
|
|
|
|
});
|
2017-02-27 04:28:05 +00:00
|
|
|
}
|
2020-12-11 21:58:00 +00:00
|
|
|
|
|
|
|
function optimizeDatabases(callingMenu, formData, extraArgs, cb) {
|
|
|
|
const dbs = require('./database').dbs;
|
|
|
|
const client = callingMenu.client;
|
|
|
|
|
|
|
|
client.term.write('\r\n\r\n');
|
|
|
|
|
|
|
|
Object.keys(dbs).forEach(dbName => {
|
|
|
|
client.log.info({ dbName }, 'Optimizing database');
|
|
|
|
|
|
|
|
client.term.write(`Optimizing ${dbName}. Please wait...\r\n`);
|
|
|
|
|
|
|
|
// https://www.sqlite.org/pragma.html#pragma_optimize
|
|
|
|
dbs[dbName].run('PRAGMA optimize;', err => {
|
|
|
|
if (err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
client.log.error(
|
|
|
|
{ error: err, dbName },
|
|
|
|
'Error attempting to optimize database'
|
|
|
|
);
|
2020-12-11 21:58:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return callingMenu.prevMenu(cb);
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|