Skip to content

Commit 90081b1

Browse files
kernelsmithkernelsmith
authored andcommitted
tag completion aliases is getting there, still not quite right
also added cmd_#{name}_help method to register_alias
1 parent a986b46 commit 90081b1

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

plugins/alias.rb

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ def cmd_alias_help
121121
#
122122
def cmd_alias_tabs(str, words)
123123
if words.length <= 1
124-
return @@alias_opts.fmt.keys + tab_complete_aliases_and_commands(str, words)
124+
#puts "1 word or less"
125+
return @@alias_opts.fmt.keys + tab_complete_aliases_and_commands
125126
else
126-
return tab_complete_aliases_and_commands(str, words)
127+
#puts "more than 1 word"
128+
return tab_complete_aliases_and_commands
127129
end
128130
end
129131

@@ -145,12 +147,22 @@ def register_alias(name, value)
145147
# define a class instance method that will tab complete the aliased command
146148
# we just proxy to the top-level tab complete function and let them handle it
147149
define_method "cmd_#{name}_tabs" do |str, words|
148-
#print_good "Creating cmd_#{name}_tabs as driver.tab_complete(#{value} #{words.join(' ')})"
149-
#driver.tab_complete("MONKEY")
150-
words.delete(name)
151-
driver.tab_complete("#{value} #{words.join(' ')}")
150+
# we need to repair the tab complete string/words and pass back
151+
# replace alias name with the root alias value
152+
value_words = value.split(/[\s\t\n]+/) # in case value is e.g. 'sessions -l'
153+
words[0] = value_words[0]
154+
value_words.shift
155+
# insert any remaining parts of value and rebuild the line
156+
line = words.join(" ") + " " + value_words.join(" ") + " " + str
157+
#print_good "passing (#{line.strip}) back to tab_complete"
158+
# clear current tab_words
159+
driver.tab_words = []
160+
driver.tab_complete(line.strip)
161+
end
162+
# add a cmd_#{name}_help method
163+
define_method "cmd_#{name}_help" do |*args|
164+
driver.run_single("help #{value}")
152165
end
153-
# we don't need a cmd_#{name}_help method, we just let the original handle that
154166
end
155167
# add the alias to the list
156168
@aliases[name] = value
@@ -164,6 +176,7 @@ def deregister_alias(name)
164176
# remove the methods we defined for this alias
165177
remove_method("cmd_#{name}")
166178
remove_method("cmd_#{name}_tab")
179+
remove_method("cmd_#{name}_help")
167180
end
168181
end
169182

@@ -232,11 +245,17 @@ def is_valid_alias?(name,value)
232245
#
233246
# Provide tab completion list for aliases and commands
234247
#
235-
def tab_complete_aliases_and_commands(str, words)
248+
def tab_complete_aliases_and_commands
236249
items = []
237-
items.concat(driver.commands.keys) if driver.respond_to?('commands')
250+
# gather all the current commands the driver's dispatcher's have
251+
driver.dispatcher_stack.each do |dispatcher|
252+
next unless dispatcher.respond_to?(:commands)
253+
next if (dispatcher.commands.nil? or dispatcher.commands.length == 0)
254+
items << dispatcher.commands.keys
255+
end
256+
# add all the current aliases to the list
238257
items.concat(@aliases.keys)
239-
items
258+
return items
240259
end
241260

242261
end # end AliasCommandDispatcher class
@@ -248,8 +267,6 @@ def tab_complete_aliases_and_commands(str, words)
248267
# inheriting from Msf::Plugin to ensure that the framework attribute on
249268
# their instance gets set.
250269
#
251-
attr_accessor :controller
252-
253270
def initialize(framework, opts)
254271
super
255272

0 commit comments

Comments
 (0)