From 5ab89f952fb3c9e13fb3c44966c52b51ec26ec9d Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Mon, 20 Apr 2015 23:24:15 -0600 Subject: [PATCH] * New @systemMethod --- core/asset.js | 1 + core/bbs.js | 25 +++++-------------- core/menu_util.js | 43 ++++++++++++++++++-------------- core/system_menu_method.js | 51 ++++++++++++++++++++++++++++++++++++++ core/theme.js | 9 +++++++ mods/apply.js | 7 +++++- mods/menu.json | 4 +-- 7 files changed, 99 insertions(+), 41 deletions(-) create mode 100644 core/system_menu_method.js diff --git a/core/asset.js b/core/asset.js index 59769eb7..3a496b0c 100644 --- a/core/asset.js +++ b/core/asset.js @@ -13,6 +13,7 @@ var ALL_ASSETS = [ 'art', 'menu', 'method', + 'systemMethod', 'prompt', ]; diff --git a/core/bbs.js b/core/bbs.js index 28a4652d..8cf9a921 100644 --- a/core/bbs.js +++ b/core/bbs.js @@ -209,25 +209,12 @@ function prepareClient(client, cb) { if('*' === conf.config.preLoginTheme) { var theme = require('./theme.js'); - async.waterfall( - [ - function getRandTheme(callback) { - theme.getRandomTheme(function randTheme(err, themeId) { - client.user.properties.theme_id = themeId || ''; - callback(null); - }); - }, - function setCurrentThemeInfo(callback) { - theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) { - client.currentThemeInfo = info; - callback(null); - }); - } - ], - function complete(err) { - cb(err); - } - ); + client.user.properties.theme_id = theme.getRandomTheme() || ''; + + theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) { + client.currentThemeInfo = info; + cb(null); + }); } else { client.user.properties.theme_id = conf.config.preLoginTheme; cb(null); diff --git a/core/menu_util.js b/core/menu_util.js index b8b1ab3a..58a09977 100644 --- a/core/menu_util.js +++ b/core/menu_util.js @@ -167,29 +167,34 @@ function handleAction(client, formData, conf) { var actionAsset = asset.parseAsset(conf.action); assert(_.isObject(actionAsset)); - + + function callModuleMenuMethod(path) { + if('' === paths.extname(path)) { + path += '.js'; + } + + try { + var methodMod = require(path); + methodMod[actionAsset.asset](client.currentMenuModule, formData, conf.extraArgs); + } catch(e) { + Log.error( { error : e.toString(), methodName : actionAsset.asset }, 'Failed to execute asset method'); + } + } + switch(actionAsset.type) { case 'method' : + case 'systemMethod' : if(_.isString(actionAsset.location)) { - try { - // allow ".js" omission - if(''=== paths.extname(actionAsset.location)) { - actionAsset.location += '.js'; - } - - var methodMod = require(paths.join(Config.paths.mods, actionAsset.location)); - - if(_.isFunction(methodMod[actionAsset.asset])) { - methodMod[actionAsset.asset](client.currentMenuModule, formData, conf.extraArgs); - } - } catch(e) { - Log.error( { error : e, methodName : actionAsset.asset }, 'Failed to execute asset method'); - } + callModuleMenuMethod(paths.join(Config.paths.mods, actionAsset.location)); } else { - // local to current module - var currentModule = client.currentMenuModule; - if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) { - currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs); + if('systemMethod' === actionAsset.type) { + callModuleMenuMethod(paths.join(__dirname, 'system_menu_method.js')); + } else { + // local to current module + var currentModule = client.currentMenuModule; + if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) { + currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs); + } } } break; diff --git a/core/system_menu_method.js b/core/system_menu_method.js new file mode 100644 index 00000000..c41db392 --- /dev/null +++ b/core/system_menu_method.js @@ -0,0 +1,51 @@ +/* jslint node: true */ +'use strict'; + +var theme = require('../core/theme.js'); +var Log = require('../core/logger.js').log; +var ansi = require('../core/ansi_term.js'); + +var async = require('async'); + +exports.login = login; +exports.logoff = logoff; + +function login(callingMenu, formData, extraArgs) { + var client = callingMenu.client; + + client.user.authenticate(formData.value.username, formData.value.password, function authenticated(err) { + if(err) { + Log.info( { username : formData.value.username }, 'Failed login attempt %s', err); + + client.gotoMenuModule( { name : callingMenu.menuConfig.fallback } ); + } else { + // use client.user so we can get correct case + Log.info( { username : callingMenu.client.user.username }, 'Successful login'); + + async.parallel( + [ + function loadThemeConfig(callback) { + theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) { + client.currentThemeInfo = info; + callback(null); + }); + } + ], + function complete(err, results) { + client.gotoMenuModule( { name : callingMenu.menuConfig.next } ); + } + ); + } + }); +} + +function logoff(callingMenu, formData, extraArgs) { + var client = callingMenu.client; + + // :TODO: record this. + + setTimeout(function timeout() { + client.term.write(ansi.normal() + '\nATH0\n'); + client.end(); + }, 500); +} \ No newline at end of file diff --git a/core/theme.js b/core/theme.js index ab0afe08..5d82ac51 100644 --- a/core/theme.js +++ b/core/theme.js @@ -94,6 +94,14 @@ function initAvailableThemes(cb) { ); } +function getRandomTheme() { + if(Object.getOwnPropertyNames(availableThemes).length > 0) { + var themeIds = Object.keys(availableThemes); + return themeIds[Math.floor(Math.random() * themeIds.length)]; + } +} + +/* function getRandomTheme(cb) { if(Object.getOwnPropertyNames(availableThemes).length > 0) { var themeIds = Object.keys(availableThemes); @@ -102,6 +110,7 @@ function getRandomTheme(cb) { cb(new Error('No themes available')); } } +*/ function getThemeArt(name, themeID, options, cb) { // allow options to be optional diff --git a/mods/apply.js b/mods/apply.js index 5967a632..ffcd0be6 100644 --- a/mods/apply.js +++ b/mods/apply.js @@ -87,13 +87,18 @@ function submitApplication(callingMenu, formData, extraArgs) { email_address : formData.value.email, web_address : formData.value.web, - theme_id : Config.defaults.theme, // :TODO: allow '*' = random account_status : Config.users.requireActivation ? user.User.AccountStatus.inactive : user.User.AccountStatus.active, // :TODO: Other defaults // :TODO: should probably have a place to create defaults/etc. }; + if('*' === Config.defaults.theme) { + newUser.properties.theme_id = theme.getRandomTheme(); + } else { + newUser.properties.theme_id = Config.defaults.theme; + } + newUser.create( { password : formData.value.password }, function created(err) { if(err) { client.gotoMenuModule( { name : extraArgs.error } ); diff --git a/mods/menu.json b/mods/menu.json index 0e735c85..57007b92 100644 --- a/mods/menu.json +++ b/mods/menu.json @@ -71,7 +71,7 @@ "prompt" : "userCredentials", "fallback" : "matrix", "next" : "newUserActive", - "action" : "@method:general_menu_methods/login", + "action" : "@systemMethod:login", // :TODO: support alt submit method for prompts // if present, standard filters apply. No need for multiple submit ID's @@ -90,7 +90,7 @@ "logoff" : { "art" : "LOGOFF", //"module" : "logoff", - "action" : "@method:general_menu_methods/logoff", + "action" : "@systemMethod:logoff", "options" : { "cls" : true } }, "apply" : {