2015-04-21 05:24:15 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-06-26 04:43:12 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const removeClient = require('./client_connections.js').removeClient;
|
|
|
|
const ansiNormal = require('./ansi_term.js').normal;
|
|
|
|
const userLogin = require('./user_login.js').userLogin;
|
2017-02-27 04:28:05 +00:00
|
|
|
const messageArea = require('./message_area.js');
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2016-06-26 04:43:12 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const iconv = require('iconv-lite');
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2017-02-27 04:28:05 +00:00
|
|
|
exports.login = login;
|
|
|
|
exports.logoff = logoff;
|
|
|
|
exports.prevMenu = prevMenu;
|
|
|
|
exports.nextMenu = nextMenu;
|
|
|
|
exports.prevConf = prevConf;
|
|
|
|
exports.nextConf = nextConf;
|
|
|
|
exports.prevArea = prevArea;
|
|
|
|
exports.nextArea = nextArea;
|
|
|
|
exports.sendForgotPasswordEmail = sendForgotPasswordEmail;
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function login(callingMenu, formData, extraArgs, cb) {
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2016-06-26 04:43:12 +00:00
|
|
|
userLogin(callingMenu.client, formData.value.username, formData.value.password, err => {
|
2015-04-21 05:24:15 +00:00
|
|
|
if(err) {
|
2015-10-19 23:21:47 +00:00
|
|
|
// login failure
|
2015-12-07 03:54:23 +00:00
|
|
|
if(err.existingConn && _.has(callingMenu, 'menuConfig.config.tooNodeMenu')) {
|
2016-07-25 20:35:58 +00:00
|
|
|
return callingMenu.gotoMenu(callingMenu.menuConfig.config.tooNodeMenu, cb);
|
2015-10-19 23:21:47 +00:00
|
|
|
} else {
|
|
|
|
// Other error
|
2016-07-25 20:35:58 +00:00
|
|
|
return callingMenu.prevMenu(cb);
|
2015-08-05 04:35:59 +00:00
|
|
|
}
|
2015-04-21 05:24:15 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
// success!
|
|
|
|
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) {
|
2015-08-05 04:35:59 +00:00
|
|
|
//
|
|
|
|
// Simple logoff. Note that recording of @ logoff properties/stats
|
|
|
|
// occurs elsewhere!
|
|
|
|
//
|
2016-06-26 04:43:12 +00:00
|
|
|
const client = callingMenu.client;
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2016-06-26 04:43:12 +00:00
|
|
|
setTimeout( () => {
|
2015-08-21 19:47:01 +00:00
|
|
|
//
|
|
|
|
// For giggles...
|
|
|
|
//
|
|
|
|
client.term.write(
|
2016-06-26 04:43:12 +00:00
|
|
|
ansiNormal() + '\n' +
|
2016-07-12 09:57:07 +00:00
|
|
|
iconv.decode(require('crypto').randomBytes(Math.floor(Math.random() * 65) + 20), client.term.outputEncoding) +
|
2016-06-26 04:43:12 +00:00
|
|
|
'NO CARRIER', null, () => {
|
2015-08-21 19:47:01 +00:00
|
|
|
|
2016-01-02 19:10:54 +00:00
|
|
|
// after data is written, disconnect & remove the client
|
2016-07-25 20:35:58 +00:00
|
|
|
removeClient(client);
|
|
|
|
return cb(null);
|
2016-06-26 04:43:12 +00:00
|
|
|
}
|
|
|
|
);
|
2015-04-21 05:24:15 +00:00
|
|
|
}, 500);
|
2015-09-24 04:24:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function prevMenu(callingMenu, formData, extraArgs, cb) {
|
2017-02-08 03:20:10 +00:00
|
|
|
|
|
|
|
// :TODO: this is a pretty big hack -- need the whole key map concep there like other places
|
|
|
|
if(formData.key && 'return' === formData.key.name) {
|
|
|
|
callingMenu.submitFormData = formData;
|
|
|
|
}
|
|
|
|
|
2016-06-26 04:43:12 +00:00
|
|
|
callingMenu.prevMenu( err => {
|
2015-09-24 04:24:37 +00:00
|
|
|
if(err) {
|
2018-01-15 19:22:11 +00:00
|
|
|
callingMenu.client.log.error( { error : err.message }, 'Error attempting to fallback!');
|
2015-09-24 04:24:37 +00:00
|
|
|
}
|
2016-07-25 20:35:58 +00:00
|
|
|
return cb(err);
|
2015-09-24 04:24:37 +00:00
|
|
|
});
|
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) {
|
2016-06-26 04:43:12 +00:00
|
|
|
callingMenu.nextMenu( err => {
|
|
|
|
if(err) {
|
2017-02-08 03:20:10 +00:00
|
|
|
callingMenu.client.log.error( { error : err.message}, 'Error attempting to go to next menu!');
|
2016-06-26 04:43:12 +00:00
|
|
|
}
|
2016-07-25 20:35:58 +00:00
|
|
|
return cb(err);
|
2016-06-26 04:43:12 +00:00
|
|
|
});
|
|
|
|
}
|
2016-07-12 09:57:07 +00:00
|
|
|
|
2016-07-13 05:38:40 +00:00
|
|
|
// :TODO: prev/nextConf, prev/nextArea should use a NYI MenuModule.redraw() or such -- avoid pop/goto() hack!
|
2016-07-25 20:35:58 +00:00
|
|
|
function reloadMenu(menu, cb) {
|
2016-07-13 05:38:40 +00:00
|
|
|
const prevMenu = menu.client.menuStack.pop();
|
|
|
|
prevMenu.instance.leave();
|
2016-07-25 20:35:58 +00:00
|
|
|
menu.client.menuStack.goto(prevMenu.name, cb);
|
2016-07-13 05:38:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function prevConf(callingMenu, formData, extraArgs, cb) {
|
2016-07-13 05:38:40 +00:00
|
|
|
const confs = messageArea.getSortedAvailMessageConferences(callingMenu.client);
|
|
|
|
const currIndex = confs.findIndex( e => e.confTag === callingMenu.client.user.properties.message_conf_tag) || confs.length;
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2016-07-13 05:38:40 +00:00
|
|
|
messageArea.changeMessageConference(callingMenu.client, confs[currIndex - 1].confTag, err => {
|
|
|
|
if(err) {
|
2016-07-25 20:35:58 +00:00
|
|
|
return cb(err); // logged within changeMessageConference()
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
2016-07-13 04:43:13 +00:00
|
|
|
});
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function nextConf(callingMenu, formData, extraArgs, cb) {
|
2016-07-13 05:38:40 +00:00
|
|
|
const confs = messageArea.getSortedAvailMessageConferences(callingMenu.client);
|
|
|
|
let currIndex = confs.findIndex( e => e.confTag === callingMenu.client.user.properties.message_conf_tag);
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2016-07-13 05:38:40 +00:00
|
|
|
if(currIndex === confs.length - 1) {
|
|
|
|
currIndex = -1;
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
2016-07-13 05:38:40 +00:00
|
|
|
|
|
|
|
messageArea.changeMessageConference(callingMenu.client, confs[currIndex + 1].confTag, err => {
|
|
|
|
if(err) {
|
2016-07-25 20:35:58 +00:00
|
|
|
return cb(err); // logged within changeMessageConference()
|
2016-07-13 04:43:13 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
2016-07-13 04:43:13 +00:00
|
|
|
});
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function prevArea(callingMenu, formData, extraArgs, cb) {
|
2016-07-13 05:38:40 +00:00
|
|
|
const areas = messageArea.getSortedAvailMessageAreasByConfTag(callingMenu.client.user.properties.message_conf_tag);
|
|
|
|
const currIndex = areas.findIndex( e => e.areaTag === callingMenu.client.user.properties.message_area_tag) || areas.length;
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2016-07-13 05:38:40 +00:00
|
|
|
messageArea.changeMessageArea(callingMenu.client, areas[currIndex - 1].areaTag, err => {
|
|
|
|
if(err) {
|
2016-07-25 20:35:58 +00:00
|
|
|
return cb(err); // logged within changeMessageArea()
|
2016-07-13 04:43:13 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
2016-07-13 04:43:13 +00:00
|
|
|
});
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
function nextArea(callingMenu, formData, extraArgs, cb) {
|
2016-07-13 05:38:40 +00:00
|
|
|
const areas = messageArea.getSortedAvailMessageAreasByConfTag(callingMenu.client.user.properties.message_conf_tag);
|
|
|
|
let currIndex = areas.findIndex( e => e.areaTag === callingMenu.client.user.properties.message_area_tag);
|
2016-07-13 04:43:13 +00:00
|
|
|
|
2016-07-13 05:38:40 +00:00
|
|
|
if(currIndex === areas.length - 1) {
|
|
|
|
currIndex = -1;
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
2016-07-13 05:38:40 +00:00
|
|
|
|
|
|
|
messageArea.changeMessageArea(callingMenu.client, areas[currIndex + 1].areaTag, err => {
|
|
|
|
if(err) {
|
2016-07-25 20:35:58 +00:00
|
|
|
return cb(err); // logged within changeMessageArea()
|
2016-07-13 04:43:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
return reloadMenu(callingMenu, cb);
|
2016-07-13 05:38:40 +00:00
|
|
|
});
|
2016-07-12 09:57:07 +00:00
|
|
|
}
|
2017-02-27 04:28:05 +00:00
|
|
|
|
|
|
|
function sendForgotPasswordEmail(callingMenu, formData, extraArgs, cb) {
|
2018-01-15 19:22:11 +00:00
|
|
|
const username = formData.value.username || callingMenu.client.user.username;
|
2017-02-27 04:28:05 +00:00
|
|
|
|
|
|
|
const WebPasswordReset = require('./web_password_reset.js').WebPasswordReset;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-27 04:28:05 +00:00
|
|
|
WebPasswordReset.sendForgotPasswordEmail(username, err => {
|
|
|
|
if(err) {
|
|
|
|
callingMenu.client.log.warn( { err : err.message }, 'Failed sending forgot password email');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(extraArgs.next) {
|
|
|
|
return callingMenu.gotoMenu(extraArgs.next, cb);
|
2018-01-15 19:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return logoff(callingMenu, formData, extraArgs, cb);
|
2017-02-27 04:28:05 +00:00
|
|
|
});
|
|
|
|
}
|