Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** Convert a telnet IAC sequence from raw bytes to a human readable format that
  2.  *  can be output for debugging purposes.
  3.  * @example
  4.  * var IAC = "\xFF", DO = "\xFD", TTYPE = "\x18";
  5.  * DecafMUD.debugIAC(IAC + DO + TTYPE);
  6.  * // Returns: "IAC DO TERMINAL-TYPE"
  7.  * @param {String} seq The sequence to convert.
  8.  * @returns {String} The human readable description of the IAC sequence. */
  9. DecafMUD.debugIAC = function(seq) {
  10.     var out = '', t = DecafMUD.TN, state = 0, st = false, l = seq.length,
  11.         i2w = iacToWord;
  12.    
  13.     for( var i = 0; i < l; i++ ) {
  14.         var c = seq.charAt(i),
  15.             cc = c.charCodeAt(0);
  16.        
  17.         // TTYPE Sequence
  18.         if ( state === 2 ) {
  19.             if ( c === t.ECHO ) { out += 'SEND '; }
  20.             else if ( c === t.IS ) { out += 'IS '; }
  21.             else if ( c === t.IAC ) {
  22.                 if ( st ) { st = false; out += '" IAC '; }
  23.                 else { out += 'IAC '; }
  24.                 state = 0;
  25.             } else {
  26.                 if ( !st ) { st = true; out += '"'; }
  27.                 out += c;
  28.             }
  29.             continue;
  30.         }
  31.        
  32.         // MSSP / MSDP Sequence
  33.         else if ( state === 3 || state === 4 ) {
  34.             if ( c === t.IAC || (cc >= 1 && cc <= 4) ) {
  35.                 if ( st ) { st = false; out += '" '; }
  36.                 if ( c === t.IAC ) {
  37.                     out += 'IAC ';
  38.                     state = 0; }
  39.                 else if ( cc === 3 ) { out += 'MSDP_OPEN '; }
  40.                 else if ( cc === 4 ) { out += 'MSDP_CLOSE '; }
  41.                 else {
  42.                     if ( state === 3 ) { out += 'MSSP_'; }
  43.                     else { out += 'MSDP_'; }
  44.                     if ( cc === 1 ) { out += 'VAR '; }
  45.                     else { out += 'VAL '; }
  46.                 }
  47.             } else {
  48.                 if ( !st ) { st = true; out += '"'; }
  49.                 out += c;
  50.             }
  51.             continue;
  52.         }
  53.        
  54.         // NAWS Sequence
  55.         else if ( state === 5 ) {
  56.             if ( c === t.IAC ) {
  57.                 st = false; out += 'IAC ';
  58.                 state = 0;
  59.             } else {
  60.                 if ( st === false ) { st = cc * 255; }
  61.                 else {
  62.                     out += (cc + st).toString() + ' ';
  63.                     st = false;
  64.                 }
  65.             }
  66.             continue;
  67.         }
  68.        
  69.         // CHARSET Sequence
  70.         else if ( state === 6 ) {
  71.             if ( c === t.IAC || (cc > 0 && cc < 8) ) {
  72.                 if ( st ) { st = false; out += '" '; }
  73.                 if ( c === t.IAC ) {
  74.                     out += 'IAC ';
  75.                     state = 0; }
  76.                 else if ( cc === 1 ) { out += 'REQUEST '; }
  77.                 else if ( cc === 2 ) { out += 'ACCEPTED '; }
  78.                 else if ( cc === 3 ) { out += 'REJECTED '; }
  79.                 else if ( cc === 4 ) { out += 'TTABLE-IS '; }
  80.                 else if ( cc === 5 ) { out += 'TTABLE-REJECTED '; }
  81.                 else if ( cc === 6 ) { out += 'TTABLE-ACK '; }
  82.                 else if ( cc === 7 ) { out += 'TTABLE-NAK '; }
  83.             } else {
  84.                 if ( !st ) { st = true; out += '"'; }
  85.                 out += c;
  86.             }
  87.         }
  88.        
  89.         // ZMP Sequence
  90.         else if ( state === 7 ) {
  91.             if ( c === t.IAC || cc === 0 ) {
  92.                 if ( st ) { st = false; out += '" '; }
  93.                 if ( c === t.IAC ) {
  94.                     out += 'IAC ';
  95.                     state = 0; }
  96.                 else if ( cc === 0 ) { out += 'NUL '; }
  97.             } else {
  98.                 if ( !st ) { st = true; out += '"'; }
  99.                 out += c;
  100.             }
  101.         }
  102.        
  103.         // Normal Sequence
  104.         else if ( state < 2 ) {
  105.             out += i2w(c) + ' '; }
  106.        
  107.         if ( state === 0 ) {
  108.             if ( c === t.SB ) { state = 1; }
  109.         } else if ( state === 1 ) {
  110.             if ( c === t.TTYPE || c === t.TSPEED ) { state = 2; }
  111.             else if ( c === t.MSSP ) { state = 3; }
  112.             else if ( c === t.MSDP ) { state = 4; }
  113.             else if ( c === t.NAWS ) { state = 5; }
  114.             else if ( c === t.CHARSET ) { state = 6; }
  115.             else if ( c === t.SENDLOC ) { state = 6; }
  116.             else if ( c === t.GMCP ) { state = 6; }
  117.             else if ( c === t.ZMP ) { state = 7; }
  118.             else { state = 0; }
  119.         }
  120.     }
  121.    
  122.     return out.substr(0, out.length-1);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement