* Various const, let vs var, etc. cleanup

* Some DRY
* Allow user formatting (e.g. in theme.hjson)
* Misc cleanup & formatting
This commit is contained in:
Bryan Ashby 2016-06-27 22:35:13 -06:00
parent 8be75d4713
commit 478a8a994a
1 changed files with 97 additions and 109 deletions

View File

@ -1,67 +1,22 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
var MenuModule = require('../core/menu_module.js').MenuModule;
var MenuModule = require('../core/menu_module.js').MenuModule;
// deps
const async = require('async'); const async = require('async');
const _ = require('lodash'); const _ = require('lodash');
const net = require('net'); const net = require('net');
const packageJson = require('../package.json');
/* /*
Expected configuration block: Expected configuration block example:
ercClient: { config: {
art: erc host: 192.168.1.171
module: erc_client port: 5001
config: { bbsTag: SOME_TAG
host: 192.168.1.171
port: 5001
bbsTag: SUPER
}
form: {
0: {
mci: {
MT1: {
width: 79
height: 21
mode: preview
autoScroll: true
}
ET3: {
autoScale: false
width: 77
argName: chattxt
focus: true
submit: true
}
}
submit: {
*: [
{
value: { chattxt: null }
action: @method:processInput
}
]
}
actionKeys: [
{
keys: [ "tab" ]
}
{
keys: [ "up arrow" ]
action: @method:scrollDown
}
{
keys: [ "down arrow" ]
action: @method:scrollUp
}
]
}
}
} }
*/ */
exports.getModule = ErcClientModule; exports.getModule = ErcClientModule;
@ -73,55 +28,81 @@ exports.moduleInfo = {
}; };
var MciViewIds = { var MciViewIds = {
chatDisplay : 1, ChatDisplay : 1,
inputArea : 3, InputArea : 3,
}; };
function ErcClientModule(options) { function ErcClientModule(options) {
MenuModule.call(this, options); MenuModule.call(this, options);
var self = this; const self = this;
this.config = options.menuConfig.config; this.config = options.menuConfig.config;
this.chatConnection = null;
this.finishedLoading = function() { this.chatEntryFormat = this.config.chatEntryFormat || '[{bbsTag}] {userName}: {message}';
async.series( this.systemEntryFormat = this.config.systemEntryFormat || '[*SYSTEM*] {message}';
this.finishedLoading = function() {
async.waterfall(
[ [
function validateConfig(callback) { function validateConfig(callback) {
if(_.isString(self.config.host) && if(_.isString(self.config.host) &&
_.isNumber(self.config.port) && _.isNumber(self.config.port) &&
_.isString(self.config.bbsTag)) _.isString(self.config.bbsTag))
{ {
callback(null); return callback(null);
} else { } else {
callback(new Error('Configuration is missing required option(s)')); return callback(new Error('Configuration is missing required option(s)'));
} }
}, },
function connectToServer(callback) { function connectToServer(callback) {
const connectOpts = { const connectOpts = {
port : self.config.port, port : self.config.port,
host : self.config.host, host : self.config.host,
}; };
const chatMessageView = self.viewControllers.menu.getView(MciViewIds.ChatDisplay);
var chatMessageView = self.viewControllers.menu.getView(MciViewIds.chatDisplay); chatMessageView.setText('Connecting to server...');
chatMessageView.setText("Connecting to server...");
chatMessageView.redraw(); chatMessageView.redraw();
self.viewControllers.menu.switchFocus(MciViewIds.inputArea);
self.viewControllers.menu.switchFocus(MciViewIds.InputArea);
self.chatConnection = net.createConnection(connectOpts.port, connectOpts.host); self.chatConnection = net.createConnection(connectOpts.port, connectOpts.host);
self.chatConnection.on('data', data => { self.chatConnection.on('data', data => {
var chatMessageView = self.viewControllers.menu.getView(MciViewIds.chatDisplay); data = data.toString();
if (data.toString().substring(0, 12) == "ERCHANDSHAKE") { if(data.startsWith('ERCHANDSHAKE')) {
self.chatConnection.write("ERCMAGIC|" + self.config.bbsTag + "|" + self.client.user.username + "\r\n"); self.chatConnection.write(`ERCMAGIC|${self.config.bbsTag}|${self.client.user.username}\r\n`);
} else { } else if(data.startsWith('{')) {
chatMessageView.addText(data.toString()); try {
if (chatMessageView.getLineCount() > 30) { data = JSON.parse(data);
} catch(e) {
return self.client.log.warn( { error : e.message }, 'ERC: Error parsing ERC data from server');
}
let text;
try {
if(data.userName) {
// user message
text = self.chatEntryFormat.format(data);
} else {
// system message
text = self.systemEntryFormat.format(data);
}
} catch(e) {
return self.client.log.warn( { error : e.message }, 'ERC: chatEntryFormat error');
}
chatMessageView.addText(text);
if(chatMessageView.getLineCount() > 30) { // :TODO: should probably be ChatDisplay.height?
chatMessageView.deleteLine(0); chatMessageView.deleteLine(0);
chatMessageView.scrollDown(); chatMessageView.scrollDown();
} }
chatMessageView.redraw(); chatMessageView.redraw();
self.viewControllers.menu.switchFocus(MciViewIds.inputArea); self.viewControllers.menu.switchFocus(MciViewIds.InputArea);
} }
}); });
@ -130,13 +111,13 @@ function ErcClientModule(options) {
}); });
self.chatConnection.once('error', err => { self.chatConnection.once('error', err => {
self.client.log.info(`Telnet bridge connection error: ${err.message}`); self.client.log.info(`ERC connection error: ${err.message}`);
}); });
} }
], ],
err => { err => {
if(err) { if(err) {
self.client.log.warn( { error : err.message }, 'Telnet connection error'); self.client.log.warn( { error : err.message }, 'ERC error');
} }
self.prevMenu(); self.prevMenu();
@ -144,37 +125,44 @@ function ErcClientModule(options) {
); );
}; };
this.scrollHandler = function(keyName) {
const inputAreaView = self.viewControllers.menu.getView(MciViewIds.InputArea);
const chatDisplayView = self.viewControllers.menu.getView(MciViewIds.ChatDisplay);
this.menuMethods = { if('up arrow' === keyName) {
processInput : function(data, cb) { chatDisplayView.scrollUp();
let chatInput = self.viewControllers.menu.getView(MciViewIds.inputArea); } else {
let chatData = chatInput.getData(); chatDisplayView.scrollDown();
if (chatData[0] === '/') { }
if (chatData[1] === 'q' || chatInput[1] === 'Q') {
self.chatConnection.end(); chatDisplayView.redraw();
} inputAreaView.setFocus(true);
} else { };
self.chatConnection.write(chatData + "\r\n");
chatInput.clearText();
} this.menuMethods = {
}, inputAreaSubmit : function() {
scrollUp : function(data, cb) { const inputAreaView = self.viewControllers.menu.getView(MciViewIds.InputArea);
let chatInput = self.viewControllers.menu.getView(MciViewIds.inputArea); const inputData = inputAreaView.getData();
let chatMessageView = self.viewControllers.menu.getView(MciViewIds.chatDisplay);
chatMessageView.scrollUp(); if('/quit' === inputData.toLowerCase()) {
chatMessageView.redraw(); self.chatConnection.end();
chatInput.setFocus(true); } else {
}, try {
scrollDown : function(data, cb) { self.chatConnection.write(`${inputData}\r\n`);
let chatInput = self.viewControllers.menu.getView(MciViewIds.inputArea); } catch(e) {
let chatMessageView = self.viewControllers.menu.getView(MciViewIds.chatDisplay); self.client.log.warn( { error : e.message }, 'ERC error');
chatMessageView.scrollDown(); }
chatMessageView.redraw(); inputAreaView.clearText();
chatInput.setFocus(true);
} }
}; },
scrollUp : function(formData) {
self.scrollHandler(formData.key.name);
},
scrollDown : function(formData) {
self.scrollHandler(formData.key.name);
}
};
} }
require('util').inherits(ErcClientModule, MenuModule); require('util').inherits(ErcClientModule, MenuModule);