* 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: {
art: erc
module: erc_client
config: { config: {
host: 192.168.1.171 host: 192.168.1.171
port: 5001 port: 5001
bbsTag: SUPER bbsTag: SOME_TAG
} }
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,27 +28,30 @@ 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.chatEntryFormat = this.config.chatEntryFormat || '[{bbsTag}] {userName}: {message}';
this.systemEntryFormat = this.config.systemEntryFormat || '[*SYSTEM*] {message}';
this.finishedLoading = function() { this.finishedLoading = function() {
async.series( 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) {
@ -102,26 +60,49 @@ function ErcClientModule(options) {
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 if(data.startsWith('{')) {
try {
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 { } else {
chatMessageView.addText(data.toString()); // system message
if (chatMessageView.getLineCount() > 30) { 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);
let chatData = chatInput.getData();
if (chatData[0] === '/') {
if (chatData[1] === 'q' || chatInput[1] === 'Q') {
self.chatConnection.end();
}
} else { } else {
self.chatConnection.write(chatData + "\r\n"); chatDisplayView.scrollDown();
chatInput.clearText();
}
},
scrollUp : function(data, cb) {
let chatInput = self.viewControllers.menu.getView(MciViewIds.inputArea);
let chatMessageView = self.viewControllers.menu.getView(MciViewIds.chatDisplay);
chatMessageView.scrollUp();
chatMessageView.redraw();
chatInput.setFocus(true);
},
scrollDown : function(data, cb) {
let chatInput = self.viewControllers.menu.getView(MciViewIds.inputArea);
let chatMessageView = self.viewControllers.menu.getView(MciViewIds.chatDisplay);
chatMessageView.scrollDown();
chatMessageView.redraw();
chatInput.setFocus(true);
} }
chatDisplayView.redraw();
inputAreaView.setFocus(true);
}; };
this.menuMethods = {
inputAreaSubmit : function() {
const inputAreaView = self.viewControllers.menu.getView(MciViewIds.InputArea);
const inputData = inputAreaView.getData();
if('/quit' === inputData.toLowerCase()) {
self.chatConnection.end();
} else {
try {
self.chatConnection.write(`${inputData}\r\n`);
} catch(e) {
self.client.log.warn( { error : e.message }, 'ERC error');
}
inputAreaView.clearText();
}
},
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);