Module: Rex::Ui::Text::DispatcherShell::CommandDispatcher

Included in:
Post::Meterpreter::Ui::Console::CommandDispatcher
Defined in:
lib/rex/ui/text/dispatcher_shell.rb

Overview

Empty template base class for command dispatchers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#shellObject

No tab completion items by default



183
184
185
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 183

def shell
  @shell
end

#tab_complete_itemsObject

No tab completion items by default



183
184
185
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 183

def tab_complete_items
  @tab_complete_items
end

Instance Method Details

#cmd_help(cmd = nil, *ignored) ⇒ Object Also known as: cmd_?

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_#<code>cmd</code>_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.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 97

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}")
				cmd_found = true
				break unless dispatcher.respond_to? "cmd_#{cmd}_help"
				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

#cmd_help_helpObject



85
86
87
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 85

def cmd_help_help
	print_line "There's only so much I can do"
end

#cmd_help_tabs(str, words) ⇒ Object

Tab completion for the help command

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



136
137
138
139
140
141
142
143
144
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 136

def cmd_help_tabs(str, words)
	return [] if words.length > 1

	tabs = []
	shell.dispatcher_stack.each { |dispatcher|
		tabs += dispatcher.commands.keys
	}
	return tabs
end

#commandsObject

Returns nil for an empty set of commands.

This method should be overridden



40
41
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 40

def commands
end

#help_to_s(opts = {}) ⇒ Object

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 152

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

#initialize(shell) ⇒ Object

Initializes the command dispatcher mixin.



30
31
32
33
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 30

def initialize(shell)
	self.shell = shell
	self.tab_complete_items = []
end

Wraps shell.print



74
75
76
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 74

def print(msg = '')
	shell.print(msg)
end

Wraps shell.print_error



46
47
48
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 46

def print_error(msg = '')
	shell.print_error(msg)
end

Wraps shell.print_good



67
68
69
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 67

def print_good(msg = '')
	shell.print_good(msg)
end

Wraps shell.print_line



60
61
62
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 60

def print_line(msg = '')
	shell.print_line(msg)
end

Wraps shell.print_status



53
54
55
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 53

def print_status(msg = '')
	shell.print_status(msg)
end

#tab_complete_filenames(str, words) ⇒ Object

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.



191
192
193
194
195
196
197
198
199
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 191

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) ⇒ Object

Wraps shell.update_prompt



81
82
83
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 81

def update_prompt(prompt=nil, prompt_char = nil, mode = false)
	shell.update_prompt(prompt, prompt_char, mode)
end