module Rex::Ui::Text::DispatcherShell::CommandDispatcher

Empty template base class for command dispatchers.

Attributes

shell[RW]

No tab completion items by default

tab_complete_items[RW]

No tab completion items by default

Public Class Methods

new(shell) click to toggle source

Initializes the command dispatcher mixin.

# File lib/rex/ui/text/dispatcher_shell.rb, line 30
def initialize(shell)
  self.shell = shell
  self.tab_complete_items = []
end

Public Instance Methods

cmd_?(cmd=nil, *ignored)
Alias for: cmd_help
cmd_help(cmd=nil, *ignored) click to toggle source

Displays the help banner. With no arguments, this is just a list of all commands grouped by dispatcher. Otherwise, tries to use a method named cmd_#{cmd}_help for the first dispatcher that has a command named cmd. If no such method exists, uses cmd as a regex to compare against each enstacked dispatcher's name and dumps commands of any that match.

# File lib/rex/ui/text/dispatcher_shell.rb, line 143
def cmd_help(cmd=nil, *ignored)
  if cmd
    help_found = false
    cmd_found = false
    shell.dispatcher_stack.each do |dispatcher|
      next unless dispatcher.respond_to?(:commands)
      next if (dispatcher.commands.nil?)
      next if (dispatcher.commands.length == 0)

      if dispatcher.respond_to?("cmd_#{cmd}", true)
        cmd_found = true
        break unless dispatcher.respond_to?("cmd_#{cmd}_help", true)
        dispatcher.send("cmd_#{cmd}_help")
        help_found = true
        break
      end
    end

    unless cmd_found
      # We didn't find a cmd, try it as a dispatcher name
      shell.dispatcher_stack.each do |dispatcher|
        if dispatcher.name =~ /#{cmd}/i
          print_line(dispatcher.help_to_s)
          cmd_found = help_found = true
        end
      end
    end
    print_error("No help for #{cmd}, try -h") if cmd_found and not help_found
    print_error("No such command") if not cmd_found
  else
    print(shell.help_to_s)
  end
end
Also aliased as: cmd_?
cmd_help_help() click to toggle source
# File lib/rex/ui/text/dispatcher_shell.rb, line 131
def cmd_help_help
  print_line "There's only so much I can do"
end
cmd_help_tabs(str, words) click to toggle source

Tab completion for the help command

By default just returns a list of all commands in all dispatchers.

# File lib/rex/ui/text/dispatcher_shell.rb, line 182
def cmd_help_tabs(str, words)
  return [] if words.length > 1

  tabs = []
  shell.dispatcher_stack.each { |dispatcher|
    tabs += dispatcher.commands.keys
  }
  return tabs
end
commands() click to toggle source

Returns nil for an empty set of commands.

This method should be overridden to return a Hash with command names for keys and brief help text for values.

# File lib/rex/ui/text/dispatcher_shell.rb, line 41
def commands
end
deprecated_cmd(method=nil, *args) click to toggle source

Print a warning that the called command is deprecated and optionally forward to the replacement method (useful for when commands are renamed).

# File lib/rex/ui/text/dispatcher_shell.rb, line 102
def deprecated_cmd(method=nil, *args)
  cmd = caller[0].match(/`cmd_(.*)'/)[1]
  print_error "The #{cmd} command is DEPRECATED"
  if cmd == "db_autopwn"
    print_error "See http://r-7.co/xY65Zr instead"
  elsif method and self.respond_to?("cmd_#{method}", true)
    print_error "Use #{method} instead"
    self.send("cmd_#{method}", *args)
  end
end
deprecated_commands() click to toggle source

Returns an empty set of commands.

This method should be overridden if the dispatcher has commands that should be treated as deprecated. Deprecated commands will not show up in help and will not tab-complete, but will still be callable.

# File lib/rex/ui/text/dispatcher_shell.rb, line 51
def deprecated_commands
  []
end
deprecated_help(method=nil) click to toggle source
# File lib/rex/ui/text/dispatcher_shell.rb, line 113
def deprecated_help(method=nil)
  cmd = caller[0].match(/`cmd_(.*)_help'/)[1]
  print_error "The #{cmd} command is DEPRECATED"
  if cmd == "db_autopwn"
    print_error "See http://r-7.co/xY65Zr instead"
  elsif method and self.respond_to?("cmd_#{method}_help", true)
    print_error "Use 'help #{method}' instead"
    self.send("cmd_#{method}_help")
  end
end
help_to_s(opts={}) click to toggle source

Return a pretty, user-readable table of commands provided by this dispatcher.

# File lib/rex/ui/text/dispatcher_shell.rb, line 198
def help_to_s(opts={})
  # If this dispatcher has no commands, we can't do anything useful.
  return "" if commands.nil? or commands.length == 0

  # Display the commands
  tbl = Table.new(
    'Header'  => "#{self.name} Commands",
    'Indent'  => opts['Indent'] || 4,
    'Columns' =>
      [
        'Command',
        'Description'
      ],
    'ColProps' =>
      {
        'Command' =>
          {
            'MaxWidth' => 12
          }
      })

  commands.sort.each { |c|
    tbl << c
  }

  return "\n" + tbl.to_s + "\n"
end
print(msg = '') click to toggle source

Wraps shell.print

print_error(msg = '') click to toggle source

Wraps shell.print_error

print_good(msg = '') click to toggle source

Wraps shell.print_good

print_line(msg = '') click to toggle source

Wraps shell.print_line

print_status(msg = '') click to toggle source

Wraps shell.print_status

print_warning(msg = '') click to toggle source

Wraps shell.print_warning

tab_complete_filenames(str, words) click to toggle source

Provide a generic tab completion for file names.

If the only completion is a directory, this descends into that directory and continues completions with filenames contained within.

# File lib/rex/ui/text/dispatcher_shell.rb, line 237
def tab_complete_filenames(str, words)
  matches = ::Readline::FILENAME_COMPLETION_PROC.call(str)
  if matches and matches.length == 1 and File.directory?(matches[0])
    dir = matches[0]
    dir += File::SEPARATOR if dir[-1,1] != File::SEPARATOR
    matches = ::Readline::FILENAME_COMPLETION_PROC.call(dir)
  end
  matches
end
update_prompt(prompt=nil, prompt_char = nil, mode = false) click to toggle source

Wraps shell.update_prompt

# File lib/rex/ui/text/dispatcher_shell.rb, line 127
def update_prompt(prompt=nil, prompt_char = nil, mode = false)
  shell.update_prompt(prompt, prompt_char, mode)
end