Class: PWN::Plugins::REPL::PwnAIInput

Inherits:
Object
  • Object
show all
Defined in:
lib/pwn/plugins/repl.rb

Overview

Custom input handler for pwn-ai to support multi-line submissions:

  • SHIFT+ENTER (common terminal sequences) or Ctrl+J / Alt+Enter inserts a newline (continue editing)

  • plain ENTER submits the full prompt (possibly multi-line) to the AI

  • Multi-line pastes are supported (Reline handles n in buffer; submit with ENTER)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePwnAIInput

Returns a new instance of PwnAIInput.



23
24
25
# File 'lib/pwn/plugins/repl.rb', line 23

def initialize
  @line_buffer = ''
end

Instance Attribute Details

#line_bufferObject (readonly)

Returns the value of attribute line_buffer.



21
22
23
# File 'lib/pwn/plugins/repl.rb', line 21

def line_buffer
  @line_buffer
end

Instance Method Details

#readline(prompt) ⇒ Object



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
# File 'lib/pwn/plugins/repl.rb', line 27

def readline(prompt)
  # Common escape sequences for SHIFT+ENTER across terminals (xterm, modern, etc.)
  shift_enter_seqs = [
    # Common SHIFT+ENTER sequences across terminals (xterm, gnome-terminal, kitty, wezterm, recent xterm w/ modifyOtherKeys, etc.)
    # If SHIFT+ENTER still submits in your terminal, try Ctrl+J or Alt+Enter (M-^J) as alternatives (terminals vary in emitted bytes for SHIFT+ENTER)
    '\\e[13;2~',
    '\\e[1;2~',
    '\\e\\r',
    '\\e\\n',
    '\\e[13;2u',
    '\\u001b[13;2u',
    '\\e[27;2;13~',
    '\\e[27;2;13u',
    '\\e[13;2u',
    '\\e[1;2~',
    '\\e[27;2;13~'
  ]
  shift_enter_seqs.each do |seq|
    Reline.config.add_oneshot_key_binding(seq.bytes, :key_newline)
  end

  begin
    # readmultiline with confirm block that *always* returns true:
    #   => normal ENTER triggers finish/submit of the (multi-line) buffer
    # The bound SHIFT+ENTER (and built-in Ctrl+J / M-^M) trigger key_newline (insert \n, stay in edit)
    # Reline in multiline mode also handles multi-line pastes by splitting on \n in the buffer.
    @line_buffer = Reline.readmultiline(prompt, true) { |_buffer| true } || ''
  ensure
    Reline.config.reset_oneshot_key_bindings
  end
  @line_buffer
end

#tty?Boolean

Compatibility with Pry input expectations (used by hooks for line_buffer, and possibly completer/tty checks)

Returns:

  • (Boolean)


61
62
63
# File 'lib/pwn/plugins/repl.rb', line 61

def tty?
  true
end

#winsizeObject



65
66
67
# File 'lib/pwn/plugins/repl.rb', line 65

def winsize
  [TTY::Screen.rows || 24, TTY::Screen.columns || 80]
end