Skip to content

Commit

Permalink
Fix: Handle any combination or order of CR and LF as command delimite…
Browse files Browse the repository at this point in the history
…rs. (#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.
  • Loading branch information
markscho authored and shawncplus committed Aug 14, 2019
1 parent 91a154d commit 0a39dd2
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
}
}

Expand Down

0 comments on commit 0a39dd2

Please sign in to comment.