From fd61df4e9e4af30e4aa8ba795b8325fe6141960e Mon Sep 17 00:00:00 2001 From: James Lee Date: Thu, 26 Nov 2009 02:18:02 +0000 Subject: [PATCH] add full support for persistent history, works in msfweb as well. fixes #523 git-svn-id: file:///home/svn/framework3/trunk@7621 4d416f70-5f16-0410-b530-b9f4589650da --- lib/msf/ui/console/driver.rb | 15 ++++++++++++--- lib/rex/ui/text/dispatcher_shell.rb | 2 +- lib/rex/ui/text/shell.rb | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/msf/ui/console/driver.rb b/lib/msf/ui/console/driver.rb index aa360ba2e2..55793ee47d 100644 --- a/lib/msf/ui/console/driver.rb +++ b/lib/msf/ui/console/driver.rb @@ -49,6 +49,10 @@ class Driver < Msf::Ui::Driver # RealReadline # # Whether or to use the system Readline or the RBReadline (default) + # + # HistFile + # + # Name of a file to store command history # def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {}) @@ -65,11 +69,16 @@ class Driver < Msf::Ui::Driver # Default to the RbReadline wrapper require 'readline_compatible' if(not rl) - hist = Msf::Config.history_file - File.readlines(hist).each {|e| Readline::HISTORY << e.chomp } if File.exists?(hist) + histfile = opts['HistFile'] || Msf::Config.history_file + + if histfile and File.exists?(histfile) + File.readlines(histfile).each { |e| + Readline::HISTORY << e.chomp + } + end # Call the parent - super(prompt, prompt_char) + super(prompt, prompt_char, histfile) # Temporarily disable output self.disable_output = true diff --git a/lib/rex/ui/text/dispatcher_shell.rb b/lib/rex/ui/text/dispatcher_shell.rb index 8731829b46..cdb922a282 100644 --- a/lib/rex/ui/text/dispatcher_shell.rb +++ b/lib/rex/ui/text/dispatcher_shell.rb @@ -87,7 +87,7 @@ module DispatcherShell # # Initialize the dispatcher shell. # - def initialize(prompt, prompt_char = '>') + def initialize(prompt, prompt_char = '>', histfile = nil) super # Initialze the dispatcher array diff --git a/lib/rex/ui/text/shell.rb b/lib/rex/ui/text/shell.rb index 4171755f02..05b926b3bb 100644 --- a/lib/rex/ui/text/shell.rb +++ b/lib/rex/ui/text/shell.rb @@ -36,7 +36,7 @@ module Shell # # Initializes a shell that has a prompt and can be interacted with. # - def initialize(prompt, prompt_char = '>') + def initialize(prompt, prompt_char = '>', histfile = nil) # Set the stop flag to false self.stop_flag = false self.disable_output = false @@ -46,6 +46,8 @@ module Shell self.init_prompt = prompt self.prompt_char = prompt_char + self.histfile = histfile || '' + # Initialize the user interface handles init_ui(Input::Stdio.new, Output::Stdio.new) end @@ -134,7 +136,14 @@ module Shell else # Otherwise, call what should be an overriden instance method to # process the line. - run_single(line) + ret = run_single(line) + # don't bother saving lines that couldn't be found as a + # command + if ret and File.exists?(self.histfile) + File.open(self.histfile, "a") { |f| + f.puts(line) + } + end self.stop_count = 0 end @@ -278,6 +287,7 @@ protected attr_accessor :stop_flag, :init_prompt # :nodoc: attr_accessor :prompt # :nodoc: attr_accessor :prompt_char, :tab_complete_proc # :nodoc: + attr_accessor :histfile # :nodoc: attr_accessor :log_source, :stop_count # :nodoc: end