# File lib/network/protocol/telnetfilter.rb, line 84
  def filter_in(str)
#    init_subneg
    return "" if str.nil? || str.empty?
    buf = ""

    @sc ? @sc.concat(str) : @sc = StringScanner.new(str)
    while b = @sc.get_byte

      # OOB sync data
      if @pstack.urgent_on || b[0] == DM
        log.debug("(#{@pstack.conn.object_id}) Sync mode on")
        @pstack.urgent_on = false
        @synch = true
        break
      end

      case mode?
      when :normal
        case b[0]
        when CR
          next if @synch
          set_mode(:cr) if !@pstack.binary_on
        when LF  # LF or LF/CR may be issued by broken mud servers and clients
          next if @synch
          set_mode(:lf) if !@pstack.binary_on
          buf << LF.chr
          echo(CR.chr + LF.chr)
        when IAC
          set_mode(:cmd)
        when NUL  # ignore NULs in stream when in normal mode
          next if @synch
          if @pstack.binary_on
            buf << b
            echo(b)
          else
            log.debug("(#{@pstack.conn.object_id}) unexpected NUL found in stream")
          end
        when BS, DEL
          next if @synch
          # Leaves BS, DEL in input stream for higher filter to deal with.
          buf << b
          echo(BS.chr)
        else
          next if @synch
          ### NOTE - we will allow 8-bit NVT against RFC 1123 recommendation "should not"
          ###
          # Only let 7-bit values through in normal mode
          #if (b[0] & 0x80 == 0) && !@pstack.binary_on
            buf << b
            echo(b)
          #else
          #  log.debug("(#{@pstack.conn.object_id}) unexpected 8-bit byte found in stream '#{b[0]}'")
          #end
        end
      when :cr
        # handle CRLF and CRNUL by insertion of LF into buffer
        case b[0]
        when LF
          buf << LF.chr
          echo(CR.chr + LF.chr)
        when NUL
          if @server.service_type == :client  # Don't xlate CRNUL when client
            buf << CR.chr
            echo(CR.chr)
          else
            buf << LF.chr
            echo(CR.chr + LF.chr)
          end
        else # eat lone CR
          buf << b
          echo(b)
        end
        set_mode(:normal)
      when :lf
        # liberally handle LF, LFCR for clients that aren't telnet correct
        case b[0]
        when CR # Handle LFCR by swallowing CR
        else  # Handle other stuff that follows - single LF
          buf << b
          echo(b)
        end
        set_mode(:normal)
      when :cmd
        case b[0]
        when IAC
          # IAC escapes IAC
          buf << IAC.chr
          set_mode(:normal)
        when AYT
          log.debug("(#{@pstack.conn.object_id}) AYT sent - Msg returned")
          @pstack.conn.sock.send("TeensyMUD is here.\n",0)
          set_mode(:normal)
        when AO
          log.debug("(#{@pstack.conn.object_id}) AO sent - Synch returned")
          @pstack.conn.sockio.write_flush
          @pstack.conn.sock.send(IAC.chr + DM.chr, 0)
          @pstack.conn.sockio.write_urgent(DM.chr)
          set_mode(:normal)
        when IP
          @pstack.conn.sockio.read_flush
          @pstack.conn.sockio.write_flush
          log.debug("(#{@pstack.conn.object_id}) IP sent")
          set_mode(:normal)
        when GA, NOP, BRK  # not implemented or ignored
          log.debug("(#{@pstack.conn.object_id}) GA, NOP or BRK sent")
          set_mode(:normal)
        when DM
          log.debug("(#{@pstack.conn.object_id}) Synch mode off")
          @synch = false
          set_mode(:normal)
        when EC
          next if @synch
          log.debug("(#{@pstack.conn.object_id}) EC sent")
          if buf.size > 1
            buf.slice!(-1)
          elsif @pstack.conn.inbuffer.size > 0
            @pstack.conn.inbuffer.slice(-1)
          end
          set_mode(:normal)
        when EL
          next if @synch
          log.debug("(#{@pstack.conn.object_id}) EL sent")
          p = buf.rindex("\n")
          if p
            buf.slice!(p+1..-1)
          else
            buf = ""
            p = @pstack.conn.inbuffer.rindex("\n")
            if p
              @pstack.conn.inbuffer.slice!(p+1..-1)
            end
          end
          set_mode(:normal)
        when DO, DONT, WILL, WONT
          if @sc.eos?
            @sc.unscan
            break
          end
          opt = @sc.get_byte
          case b[0]
          when WILL
            replies_him(opt[0],true)
          when WONT
            replies_him(opt[0],false)
          when DO
            requests_us(opt[0],true)
          when DONT
            requests_us(opt[0],false)
          end
          # Update interesting things in ProtocolStack after negotiation
          case opt[0]
          when ECHO
            @pstack.echo_on = enabled?(ECHO, :us)
          when BINARY
            @pstack.binary_on = enabled?(BINARY, :us)
          when ZMP
            @pstack.zmp_on = enabled?(ZMP, :us)
          end
          set_mode(:normal)
        when SB
          @sc.unscan
          break if @sc.check_until(/#{IAC.chr}#{SE.chr}/).nil?
          @sc.get_byte
          opt = @sc.get_byte
          data = @sc.scan_until(/#{IAC.chr}#{SE.chr}/).chop.chop
          parse_subneg(opt[0],data)
          set_mode(:normal)
        else
          log.debug("(#{@pstack.conn.object_id}) Unknown Telnet command - #{b[0]}")
          set_mode(:normal)
        end
      end
    end  # while b

    @sc = nil if @sc.eos?
    buf
  end