Merge pull request #302 from alexmozzhakov/master

Improvements to autocomplete
This commit is contained in:
evilsocket 2018-07-23 06:20:32 +02:00 committed by GitHub
commit 5ee7a9cf82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,6 +24,7 @@ import (
"github.com/bettercap/bettercap/packets"
"github.com/adrianmo/go-nmea"
"io/ioutil"
)
const HistoryFile = "~/bettercap.history"
@ -206,12 +207,21 @@ func (s *Session) setupReadline() error {
var appendedOption = strings.Join(parts[1:], " ")
if len(appendedOption) > 0 {
if len(appendedOption) > 0 && !containsCapitals(appendedOption) {
tree[name] = append(tree[name], appendedOption)
}
}
}
searchForCap("./", tree, false)
searchForCap("./caplets/", tree, true)
capspath := core.Trim(os.Getenv("CAPSPATH"))
var paths []string
paths = append(paths, core.SepSplit(capspath, ":")...)
for _, path := range paths {
searchForCap(path, tree, false)
}
for root, subElems := range tree {
item := readline.PcItem(root)
item.Children = []readline.PrefixCompleterInterface{}
@ -239,6 +249,31 @@ func (s *Session) setupReadline() error {
return err
}
func searchForCap(path string, tree map[string][]string, recursive bool) {
_searchForCap(path, tree, recursive, path)
}
func _searchForCap(path string, tree map[string][]string, recursive bool, prefix string) {
subFiles, _ := ioutil.ReadDir(path)
for _, subF := range subFiles {
if strings.HasSuffix(subF.Name(), ".cap") {
tree[strings.TrimPrefix(path, prefix)+strings.Replace(subF.Name(), ".cap", "", -1)] = []string{}
} else if subF.IsDir() && recursive {
_searchForCap(path+subF.Name()+"/", tree, true, prefix)
}
}
}
func containsCapitals(s string) bool {
for _, ch := range s {
if ch < 133 && ch > 101 {
return false
}
}
return true
}
func (s *Session) Close() {
fmt.Printf("\nStopping modules and cleaning session state ...\n")