From 0a39dd2ab9d4243b3464f4a56d5a9831827f4d71 Mon Sep 17 00:00:00 2001 From: markscho <10503254+markscho@users.noreply.github.com> Date: Wed, 14 Aug 2019 11:26:14 -0500 Subject: [PATCH] Fix: Handle any combination or order of CR and LF as command delimiters. (#5) * Fix: Game now handles any combination or order of CR and LF as command delimiters. * Fix: Now works if you just hit the ENTER key. * Fix: Game no longer accepts ASCII control codes as valid input. * Fix: sendGMCP() now actually sends GMCP data. --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d92ab76..767f976 100644 --- a/index.js +++ b/index.js @@ -170,9 +170,14 @@ class TelnetSocket extends EventEmitter // them separately. Some client auto-connect features do this let bucket = []; for (let i = 0; i < inputlen; i++) { - if (databuf[i] !== 10) { // \n + if (databuf[i] !== 10 && databuf[i] !== 13) { // neither LF nor CR bucket.push(databuf[i]); } else { + // look ahead to see if our newline delimiter is part of a combo. + if (i+1 < inputlen && (databuf[i+1] === 10 || databuf[i+1 === 13]) + && databuf[i] !== databuf[i+1]) { + i++; + } this.input(Buffer.from(bucket)); bucket = []; } @@ -219,7 +224,11 @@ class TelnetSocket extends EventEmitter while (i < inputbuf.length) { if (inputbuf[i] !== Seq.IAC) { - cleanbuf[cleanlen++] = inputbuf[i++]; + if (inputbuf[i] < 32) { // Skip any freaky control codes. + i++; + } else { + cleanbuf[cleanlen++] = inputbuf[i++]; + } continue; } @@ -325,7 +334,7 @@ class TelnetSocket extends EventEmitter * @event TelnetSocket#data * @param {Buffer} data */ - this.emit('data', cleanbuf.slice(0, cleanlen - 1)); + this.emit('data', cleanbuf.slice(0, cleanlen >= cleanbuf.length ? undefined : cleanlen)); // special processing required for slice() to work. } }