Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Convert a telnet IAC sequence from raw bytes to a human readable format that
- * can be output for debugging purposes.
- * @example
- * var IAC = "\xFF", DO = "\xFD", TTYPE = "\x18";
- * DecafMUD.debugIAC(IAC + DO + TTYPE);
- * // Returns: "IAC DO TERMINAL-TYPE"
- * @param {String} seq The sequence to convert.
- * @returns {String} The human readable description of the IAC sequence. */
- DecafMUD.debugIAC = function(seq) {
- var out = '', t = DecafMUD.TN, state = 0, st = false, l = seq.length,
- i2w = iacToWord;
- for( var i = 0; i < l; i++ ) {
- var c = seq.charAt(i),
- cc = c.charCodeAt(0);
- // TTYPE Sequence
- if ( state === 2 ) {
- if ( c === t.ECHO ) { out += 'SEND '; }
- else if ( c === t.IS ) { out += 'IS '; }
- else if ( c === t.IAC ) {
- if ( st ) { st = false; out += '" IAC '; }
- else { out += 'IAC '; }
- state = 0;
- } else {
- if ( !st ) { st = true; out += '"'; }
- out += c;
- }
- continue;
- }
- // MSSP / MSDP Sequence
- else if ( state === 3 || state === 4 ) {
- if ( c === t.IAC || (cc >= 1 && cc <= 4) ) {
- if ( st ) { st = false; out += '" '; }
- if ( c === t.IAC ) {
- out += 'IAC ';
- state = 0; }
- else if ( cc === 3 ) { out += 'MSDP_OPEN '; }
- else if ( cc === 4 ) { out += 'MSDP_CLOSE '; }
- else {
- if ( state === 3 ) { out += 'MSSP_'; }
- else { out += 'MSDP_'; }
- if ( cc === 1 ) { out += 'VAR '; }
- else { out += 'VAL '; }
- }
- } else {
- if ( !st ) { st = true; out += '"'; }
- out += c;
- }
- continue;
- }
- // NAWS Sequence
- else if ( state === 5 ) {
- if ( c === t.IAC ) {
- st = false; out += 'IAC ';
- state = 0;
- } else {
- if ( st === false ) { st = cc * 255; }
- else {
- out += (cc + st).toString() + ' ';
- st = false;
- }
- }
- continue;
- }
- // CHARSET Sequence
- else if ( state === 6 ) {
- if ( c === t.IAC || (cc > 0 && cc < 8) ) {
- if ( st ) { st = false; out += '" '; }
- if ( c === t.IAC ) {
- out += 'IAC ';
- state = 0; }
- else if ( cc === 1 ) { out += 'REQUEST '; }
- else if ( cc === 2 ) { out += 'ACCEPTED '; }
- else if ( cc === 3 ) { out += 'REJECTED '; }
- else if ( cc === 4 ) { out += 'TTABLE-IS '; }
- else if ( cc === 5 ) { out += 'TTABLE-REJECTED '; }
- else if ( cc === 6 ) { out += 'TTABLE-ACK '; }
- else if ( cc === 7 ) { out += 'TTABLE-NAK '; }
- } else {
- if ( !st ) { st = true; out += '"'; }
- out += c;
- }
- }
- // ZMP Sequence
- else if ( state === 7 ) {
- if ( c === t.IAC || cc === 0 ) {
- if ( st ) { st = false; out += '" '; }
- if ( c === t.IAC ) {
- out += 'IAC ';
- state = 0; }
- else if ( cc === 0 ) { out += 'NUL '; }
- } else {
- if ( !st ) { st = true; out += '"'; }
- out += c;
- }
- }
- // Normal Sequence
- else if ( state < 2 ) {
- out += i2w(c) + ' '; }
- if ( state === 0 ) {
- if ( c === t.SB ) { state = 1; }
- } else if ( state === 1 ) {
- if ( c === t.TTYPE || c === t.TSPEED ) { state = 2; }
- else if ( c === t.MSSP ) { state = 3; }
- else if ( c === t.MSDP ) { state = 4; }
- else if ( c === t.NAWS ) { state = 5; }
- else if ( c === t.CHARSET ) { state = 6; }
- else if ( c === t.SENDLOC ) { state = 6; }
- else if ( c === t.GMCP ) { state = 6; }
- else if ( c === t.ZMP ) { state = 7; }
- else { state = 0; }
- }
- }
- return out.substr(0, out.length-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement