enigma-bbs/core/system_menu_method.js

149 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-04-21 05:24:15 +00:00
/* jslint node: true */
'use strict';
// ENiGMA½
const removeClient = require('./client_connections.js').removeClient;
const ansiNormal = require('./ansi_term.js').normal;
const userLogin = require('./user_login.js').userLogin;
const messageArea = require('./message_area.js');
2015-04-21 05:24:15 +00:00
// deps
const _ = require('lodash');
const iconv = require('iconv-lite');
2015-04-21 05:24:15 +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;
2015-04-21 05:24:15 +00:00
function login(callingMenu, formData, extraArgs, cb) {
2015-04-21 05:24:15 +00:00
userLogin(callingMenu.client, formData.value.username, formData.value.password, err => {
2015-04-21 05:24:15 +00:00
if(err) {
// login failure
if(err.existingConn && _.has(callingMenu, 'menuConfig.config.tooNodeMenu')) {
return callingMenu.gotoMenu(callingMenu.menuConfig.config.tooNodeMenu, cb);
} else {
// Other error
return callingMenu.prevMenu(cb);
}
2015-04-21 05:24:15 +00:00
}
// success!
return callingMenu.nextMenu(cb);
2015-04-21 05:24:15 +00:00
});
}
function logoff(callingMenu, formData, extraArgs, cb) {
//
// Simple logoff. Note that recording of @ logoff properties/stats
// occurs elsewhere!
//
const client = callingMenu.client;
2015-04-21 05:24:15 +00:00
setTimeout( () => {
2015-08-21 19:47:01 +00:00
//
// For giggles...
//
client.term.write(
ansiNormal() + '\n' +
iconv.decode(require('crypto').randomBytes(Math.floor(Math.random() * 65) + 20), client.term.outputEncoding) +
'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
removeClient(client);
return cb(null);
}
);
2015-04-21 05:24:15 +00:00
}, 500);
}
function prevMenu(callingMenu, formData, extraArgs, cb) {
callingMenu.prevMenu( err => {
if(err) {
callingMenu.client.log.error( { error : err.toString() }, 'Error attempting to fallback!');
}
return cb(err);
});
}
function nextMenu(callingMenu, formData, extraArgs, cb) {
callingMenu.nextMenu( err => {
if(err) {
callingMenu.client.log.error( { error : err.toString() }, 'Error attempting to go to next menu!');
}
return cb(err);
});
}
// :TODO: prev/nextConf, prev/nextArea should use a NYI MenuModule.redraw() or such -- avoid pop/goto() hack!
function reloadMenu(menu, cb) {
const prevMenu = menu.client.menuStack.pop();
prevMenu.instance.leave();
menu.client.menuStack.goto(prevMenu.name, cb);
}
function prevConf(callingMenu, formData, extraArgs, cb) {
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
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
return reloadMenu(callingMenu, cb);
2016-07-13 04:43:13 +00:00
});
}
function nextConf(callingMenu, formData, extraArgs, cb) {
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
if(currIndex === confs.length - 1) {
currIndex = -1;
}
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
}
return reloadMenu(callingMenu, cb);
2016-07-13 04:43:13 +00:00
});
}
function prevArea(callingMenu, formData, extraArgs, cb) {
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
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
}
return reloadMenu(callingMenu, cb);
2016-07-13 04:43:13 +00:00
});
}
function nextArea(callingMenu, formData, extraArgs, cb) {
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
if(currIndex === areas.length - 1) {
currIndex = -1;
}
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
}
return reloadMenu(callingMenu, cb);
});
}