enigma-bbs/core/system_menu_method.js

75 lines
1.7 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;
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;
2015-04-21 05:24:15 +00:00
function login(callingMenu, formData) {
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')) {
callingMenu.gotoMenu(callingMenu.menuConfig.config.tooNodeMenu);
} else {
// Other error
callingMenu.prevMenu();
}
} else {
// success!
callingMenu.nextMenu();
2015-04-21 05:24:15 +00:00
}
});
}
function logoff(callingMenu) {
//
// 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
return removeClient(client);
}
);
2015-04-21 05:24:15 +00:00
}, 500);
}
function prevMenu(callingMenu) {
callingMenu.prevMenu( err => {
if(err) {
callingMenu.client.log.error( { error : err.toString() }, 'Error attempting to fallback!');
}
});
}
function nextMenu(callingMenu) {
callingMenu.nextMenu( err => {
if(err) {
callingMenu.client.log.error( { error : err.toString() }, 'Error attempting to go to next menu!');
}
});
}