Module: Net::Telnet::RFC2217::TelnetExtensions

Included in:
Net::Telnet
Defined in:
lib/net/telnet/rfc2217/telnet.rb

Instance Method Summary collapse

Instance Method Details

#preprocess(string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/net/telnet/rfc2217/telnet.rb', line 5

def preprocess(string)
  # combine CR+NULL into CR
  string = string.gsub(/#{CR}#{NULL}/no, CR) if @options["Telnetmode"]

  # combine EOL into "\n"
  string = string.gsub(/#{EOL}/no, "\n") unless @options["Binmode"]

  # remove NULL
  string = string.gsub(/#{NULL}/no, '') unless @options["Binmode"]

  string.gsub(/#{IAC}(
              [#{IAC}#{AO}#{AYT}#{DM}#{IP}#{NOP}]|
              [#{DO}#{DONT}#{WILL}#{WONT}]
                [#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{44.chr}#{OPT_EXOPL}]|
              #{SB}[^#{IAC}]*#{IAC}#{SE}
            )/xno) do
    if block_given? && IAC != $1
      # already handled; don't handle it ourselves
      next '' if yield $1
    end

    if    IAC == $1  # handle escaped IAC characters
      IAC
    elsif AYT == $1  # respond to "IAC AYT" (are you there)
      self.write("nobody here but us pigeons" + EOL)
      ''
    elsif DO[0] == $1[0]  # respond to "IAC DO x"
      if OPT_BINARY[0] == $1[1]
        unless @telnet_option["BINARY"] == true
          @telnet_option["BINARY"] = true
          self.write(IAC + WILL + OPT_BINARY)
        end
      else
        self.write(IAC + WONT + $1[1..1])
      end
      ''
    elsif DONT[0] == $1[0]  # respond to "IAC DON'T x" with "IAC WON'T x"
      self.write(IAC + WONT + $1[1..1])
      ''
    elsif WILL[0] == $1[0]  # respond to "IAC WILL x"
      if    OPT_BINARY[0] == $1[1]
        self.write(IAC + DO + OPT_BINARY)
      elsif OPT_ECHO[0] == $1[1]
        self.write(IAC + DO + OPT_ECHO)
      elsif OPT_SGA[0]  == $1[1]
        unless @telnet_option["SGA"]
          @telnet_option["SGA"] = true
          self.write(IAC + DO + OPT_SGA)
        end
      else
        self.write(IAC + DONT + $1[1..1])
      end
      ''
    elsif WONT[0] == $1[0]  # respond to "IAC WON'T x"
      if    OPT_ECHO[0] == $1[1]
        self.write(IAC + DONT + OPT_ECHO)
      elsif OPT_SGA[0]  == $1[1]
        unless @telnet_option["SGA"] == false
          @telnet_option["SGA"] = false
          self.write(IAC + DONT + OPT_SGA)
        end
      else
        self.write(IAC + DONT + $1[1..1])
      end
      ''
    elsif SB[0] == $1[0] # yield sub option
      ''
    else
      ''
    end
  end
end