Class: Ronin::Support::CLI::IOShell

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/cli/io_shell.rb

Overview

Starts an interactive shell for the IO object, where data read from the IO object is printed to STDOUT and data read from STDIN is written to the IO object.

Example

require 'ronin/support/cli/io_shell'

socket = TCPSocket.new('irc.undernet.org',6667)
CLI::IOShell.sstart(socket)
NOTICE AUTH :*** Looking up your hostname
NOTICE AUTH :*** Checking Ident
NOTICE AUTH :*** Found your hostname
NOTICE AUTH :*** No ident response
USER test_ruby * * *
NICK test_ruby
PING :3167790481
PONG 3167790481
:Chicago.IL.US.Undernet.Org 001 test_ruby :Welcome to the UnderNet IRC Network, test_ruby
:Chicago.IL.US.Undernet.Org 002 test_ruby :Your host is Chicago.IL.US.Undernet.Org, running version u2.10.12.19
:Chicago.IL.US.Undernet.Org 003 test_ruby :This server was created Wed Apr 15 2020 at 22:02:43 UTC
:Chicago.IL.US.Undernet.Org 004 test_ruby Chicago.IL.US.Undernet.Org u2.10.12.19 diOoswkgx biklmnopstvrDdRcC bklov
:Chicago.IL.US.Undernet.Org 005 test_ruby WHOX WALLCHOPS WALLVOICES USERIP CPRIVMSG CNOTICE SILENCE=25 MODES=6 MAXCHANNELS=40 MAXBANS=100 NICKLEN=12 :are supported by this server
:Chicago.IL.US.Undernet.Org 005 test_ruby MAXNICKLEN=15 TOPICLEN=160 AWAYLEN=160 KICKLEN=160 CHANNELLEN=200 MAXCHANNELLEN=200 CHANTYPES=#& PREFIX=(ov)@+ STATUSMSG=@+ CHANMODES=b,k,l,imnpstrDdRcC CASEMAPPING=rfc1459 NETWORK=UnderNet :are supported by this server
:Chicago.IL.US.Undernet.Org 251 test_ruby :There are 3241 users and 9182 invisible on 38 servers
:Chicago.IL.US.Undernet.Org 252 test_ruby 57 :operator(s) online
:Chicago.IL.US.Undernet.Org 253 test_ruby 23 :unknown connection(s)
:Chicago.IL.US.Undernet.Org 254 test_ruby 6230 :channels formed
:Chicago.IL.US.Undernet.Org 255 test_ruby :I have 1179 clients and 1 servers
:Chicago.IL.US.Undernet.Org NOTICE test_ruby :Highest connection count: 1388 (1387 clients)
:Chicago.IL.US.Undernet.Org 422 test_ruby :MOTD File is missing
:Chicago.IL.US.Undernet.Org NOTICE test_ruby :on 1 ca 1(4) ft 10(10)
Ctrl^D

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, stdin: $stdin, stdout: $stdout) ⇒ IOShell

Initialies the IO shell object.

Parameters:

  • io (IO)

    The IO object to interact with.

  • stdin (IO) (defaults to: $stdin)

    Alternative STDIN stream.

  • stdout (IO) (defaults to: $stdout)

    Alternative STDOUT stream.



89
90
91
92
93
94
# File 'lib/ronin/support/cli/io_shell.rb', line 89

def initialize(io, stdin: $stdin, stdout: $stdout)
  @io = io

  @stdin  = stdin
  @stdout = stdout
end

Instance Attribute Details

#ioIO (readonly)

The IO object to interact with.

Returns:



65
66
67
# File 'lib/ronin/support/cli/io_shell.rb', line 65

def io
  @io
end

#stdinIO (readonly)

The STDIN stream.

Returns:



70
71
72
# File 'lib/ronin/support/cli/io_shell.rb', line 70

def stdin
  @stdin
end

#stdoutIO (readonly)

The STDOUT stream.

Returns:



75
76
77
# File 'lib/ronin/support/cli/io_shell.rb', line 75

def stdout
  @stdout
end

Class Method Details

.start(io, **kwargs) ⇒ Object

Starts the interactive shell with the given IO object.

Parameters:

  • io (IO)

    The IO object to interact with.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #initialize.

Options Hash (**kwargs):

  • :stdin (IO)

    Optional alternative STDIN stream. Defaults to $stdin.

  • :stdout (IO)

    Optional alternative STDOUT stream. Defaults to $stdout.



111
112
113
# File 'lib/ronin/support/cli/io_shell.rb', line 111

def self.start(io,**kwargs)
  new(io,**kwargs).run
end

Instance Method Details

#runBoolean

Runs the interaction shell. Data read from the IO object is printed to STDOUT and data read from STDIN is written to the IO object. Ctrl^D or Ctrl^C will exit the session.

Returns:

  • (Boolean)

    Returns true if the user exited the interactive shell. Returns false if there an IOError was encountered.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ronin/support/cli/io_shell.rb', line 124

def run
  io_array = [@io, @stdin]

  loop do
    readable, _writable, errors = IO.select(io_array,nil,io_array)

    if errors.include?(@io) || errors.include?(@stdin)
      return false
    end

    if readable.include?(@io)
      data = begin
               @io.readpartial(4096)
             rescue EOFError
               return false
             end

      @stdout.write(data)
    end

    if readable.include?(@stdin)
      data = begin
               @stdin.readpartial(4096)
             rescue EOFError
               return true
             end

      @io.write(data)
    end
  end
rescue Interrupt
  return true
end