From 9f159ebde5c238ffe1e0e4ab24835aa671cdaf31 Mon Sep 17 00:00:00 2001 From: David Stark Date: Thu, 19 Dec 2019 03:16:08 +0000 Subject: [PATCH] wip --- main.go | 50 +++++++++++++++++++-- pkg/eve/FileSearch.go | 39 ++++++++++++++++ pkg/eve/FindActiveChannels.go | 28 +++++++++--- pkg/eve/watcher.go | 85 +++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+), 9 deletions(-) create mode 100644 pkg/eve/watcher.go diff --git a/main.go b/main.go index b694162..32ca345 100644 --- a/main.go +++ b/main.go @@ -3,15 +3,59 @@ package main import ( "fmt" "github.com/starkers/safer-eve/pkg/eve" + "time" ) func main() { fmt.Println("searching") + + //logDirs := eve.PathSearch() + //fmt.Println(a) fileList := eve.FileSearchUnix() - List := eve.GetActiveChannels(fileList) - for _, name := range List{ - fmt.Println(name) + //List := eve.GetActiveChannels(fileList) + //for _, name := range List{ + // fmt.Println(name) + //} + + //for _, d := range logDirs { + // fmt.Println(d) + // go eve.WatchPath(d) + //} + + + for _, f := range fileList{ + if eve.IsCurrentLog(f) { + //if strings.Contains(f, "delve.imperium") { + fmt.Printf("tailing file: %v\n", f) + go eve.Tail(f) + //} + } + //else { + // fmt.Println(f) + //} } + time.Sleep(9999 * time.Hour) } + +//func parseLine(text string){ +// fmt.Printf("parsing: '%v'\n", text) +//} +// +//func tailFile(filePath string) { +// +// // https://godoc.org/github.com/hpcloud/tail#Config +// t, err := tail.TailFile( +// filePath, +// tail.Config{ +// Follow: true, +// }) +// if err != nil { +// fmt.Println(err) +// } +// for line := range t.Lines{ +// parseLine(line.Text) +// } +// +//} diff --git a/pkg/eve/FileSearch.go b/pkg/eve/FileSearch.go index 3fe3342..d4ebdc2 100644 --- a/pkg/eve/FileSearch.go +++ b/pkg/eve/FileSearch.go @@ -59,3 +59,42 @@ func FileSearchUnix() []string { return filesPotential } +// returns paths with eve chat log files +func PathSearch() []string { + var pathsWithLogs []string + fmt.Println("PathSearch looking for chat log files..") + + searchPath = append(searchPath, "~/.local/share/Steam/steamapps/compatdata/8500/pfx/drive_c/users/steamuser/My Documents/EVE/logs/Chatlogs") + + userName, _ := user.Current() + homeDir := userName.HomeDir + + for _, d := range searchPath { + + // if has trailing slash, remove it + d = suffixTrim(d, "/") + + // do a unix like expansion if the path starts like `~/` + if strings.HasPrefix(d, "~/") { + d = filepath.Join(homeDir, d[2:]) + } + f, err := os.Open(d) + if err != nil { + log.Fatal(err) + } + files, err := f.Readdir(-1) + if err != nil { + log.Fatal(err) + } + err = f.Close() + if err != nil { + log.Fatal(err) + } + if len(files) > 0 { + pathsWithLogs = append(pathsWithLogs, d) + } + fmt.Println(len(files)) + + } + return pathsWithLogs +} diff --git a/pkg/eve/FindActiveChannels.go b/pkg/eve/FindActiveChannels.go index 70c9cb5..2be8ec3 100644 --- a/pkg/eve/FindActiveChannels.go +++ b/pkg/eve/FindActiveChannels.go @@ -2,12 +2,10 @@ package eve import ( "fmt" - "time" "strings" + "time" ) - - // GetActiveChannels will parse a list of files for ones with today's timestamps // This really just returns a list of human-readable channel names so // there will be some eve specific logic to guess the channel name @@ -35,7 +33,6 @@ func GetActiveChannels(fileList []string) []string { } } - for _, file := range candidateFiles { fmt.Println(file) chanList = append(chanList, chatNameFromFile(file)) @@ -46,10 +43,30 @@ func GetActiveChannels(fileList []string) []string { return chanList } +// IsCurrentLog will return True if the logfile has a timestamp that looks like +// Today or Yesterdays (there is an assumption that the eve client won't be run for more than 24hrs strait) +func IsCurrentLog(file string) bool { + timeToday := time.Now() + timeYesterday := time.Now().Add(-24 * time.Hour) + layout := "20060102" + todayString := fmt.Sprintf(timeToday.Format(layout)) + yesterdayString := fmt.Sprintf(timeYesterday.Format(layout)) + fileNameSplit := strings.Split(file, "/") + fileName := fileNameSplit[len(fileNameSplit)-1] // returns file name without any "/" prefixes + + if strings.Contains(fileName, todayString) { + return true + } + if strings.Contains(fileName, yesterdayString) { + return true + } + + return false +} func chatNameFromFile(fname string) string { a := strings.Split(fname, "/") - b := a[len(a)-1] // returns file name without any "/" prefixes + b := a[len(a)-1] // returns file name without any "/" prefixes // create a rune of the file name only c := []rune(b) @@ -64,7 +81,6 @@ func chatNameFromFile(fname string) string { return myShortString } - // TODO: run a uniq pass on the list of channels maybe //func unique(intSlice []int) []int { // keys := make(map[int]bool) diff --git a/pkg/eve/watcher.go b/pkg/eve/watcher.go new file mode 100644 index 0000000..26b4040 --- /dev/null +++ b/pkg/eve/watcher.go @@ -0,0 +1,85 @@ +package eve + +import ( + "fmt" + "github.com/hpcloud/tail" + "github.com/radovskyb/watcher" + "log" + "time" +) + +func Tail(file string) { + // https://godoc.org/github.com/hpcloud/tail#Config + t, err := tail.TailFile( + file, + tail.Config{ + Follow: true, + }) + if err != nil { + fmt.Println(err) + } + + //totalLines := 0 + //for _ = range t.Lines { + // totalLines ++ + //} + + //currentLine := 0 + for line := range t.Lines { + //fmt.Printf("%v/%v", currentLine, totalLines) + //currentLine ++ + //if currentLine > totalLines { + fmt.Printf("##%v##%v##\n", line.Time, line.Text) + //} + + } + +} + + +// sends newly created files for tail'ing +// WIP +func WatchPath(dir string) { + w := watcher.New() + + w.FilterOps() + w.FilterOps(watcher.Create, watcher.Remove, watcher.Write) + + //r := regexp.MustCompile("^abc$") + //w.AddFilterHook(watcher.RegexFilterHook(r, false)) + + // dispatch creation/removal events here + go func() { + for { + select { + case event := <-w.Event: + // fmt.Println(event) + fmt.Printf("%s (%s)\n", event.Path, event.Op) + case err := <-w.Error: + log.Fatalln(err) + case <-w.Closed: + return + } + } + }() + + if err := w.Add(dir); err != nil { + log.Fatalln(err) + } + + for path, f := range w.WatchedFiles() { + fmt.Printf("%s: %s\n", path, f.Name()) + } + + fmt.Println("=== startup complete ===") + + // go func() { + // w.Wait() + // w.TriggerEvent(watcher.Create, nil) + // }() + + if err := w.Start(time.Millisecond * 100); err != nil { + log.Fatalln(err) + } + +}