From 8ce5e2dadaa395bb5c6f69c3092c1dbfd4ad0604 Mon Sep 17 00:00:00 2001 From: Carlo Strub Date: Tue, 23 May 2017 21:30:24 +0000 Subject: [PATCH] re-enable watcher --- database.go | 2 +- sisyphus/sisyphus.go | 100 +++++++++++++++++++++---------------------- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/database.go b/database.go index 5f645b9..7a623f3 100644 --- a/database.go +++ b/database.go @@ -79,7 +79,7 @@ func CloseDatabases(databases map[Maildir]*bolt.DB) { if err != nil { log.WithFields(log.Fields{ "db": string(key) + "/sisyphus.db", - }).Warn("Unable to close database") + }).Error("Unable to close database") } log.WithFields(log.Fields{ "db": string(key) + "/sisyphus.db", diff --git a/sisyphus/sisyphus.go b/sisyphus/sisyphus.go index ab1fcba..8b8d7b4 100644 --- a/sisyphus/sisyphus.go +++ b/sisyphus/sisyphus.go @@ -4,8 +4,10 @@ import ( "fmt" "os" "os/signal" + "strings" "syscall" + "github.com/fsnotify/fsnotify" log "github.com/sirupsen/logrus" "github.com/carlostrub/sisyphus" @@ -151,58 +153,52 @@ func main() { } } - // // Classify on arrival - // watcher, err := fsnotify.NewWatcher() - // if err != nil { - //log.WithFields(log.Fields{ - // "err": err, - //}).Warning("Cannot create directory watcher") - // } - // defer watcher.Close() - // - // done := make(chan bool) - // go func() { - // for { - // select { - // case event := <-watcher.Events: - // if event.Op&fsnotify.Create == fsnotify.Create { - // mailName := strings.Split(event.Name, "/") - // m := sisyphus.Mail{ - // Key: mailName[len(mailName)-1], - // } - // - // if mailName[len(mailName)-2] == "new" { - // err = m.Classify(db) - // if err != nil { - // log.Print(err) - // } - // } else { - // err = m.Learn(db) - // if err != nil { - // log.Print(err) - // } - // } - // - // } - // case err := <-watcher.Errors: - // log.Println("error:", err) - // } - // } - // }() - // - // err = watcher.Add(maildirPaths[0] + "/cur") - // if err != nil { - // log.Fatal(err) - // } - // err = watcher.Add(maildirPaths[0] + "/new") - // if err != nil { - // log.Fatal(err) - // } - // err = watcher.Add(maildirPaths[0] + "/.Junk/cur") - // if err != nil { - // log.Fatal(err) - // } - // <-done + // Classify whenever a mail arrives in "new" + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.WithFields(log.Fields{ + "err": err, + }).Fatal("Cannot setup directory watcher") + } + defer watcher.Close() + + done := make(chan bool) + go func() { + for { + select { + case event := <-watcher.Events: + if event.Op&fsnotify.Create == fsnotify.Create { + mailName := strings.Split(event.Name, "/") + mailDir := strings.TrimRight(event.Name, "/new/"+mailName[len(mailName)-1]) + m := sisyphus.Mail{ + Key: mailName[len(mailName)-1], + } + + err = m.Classify(dbs[sisyphus.Maildir(mailDir)]) + if err != nil { + log.Print(err) + } + + } + case err := <-watcher.Errors: + log.WithFields(log.Fields{ + "err": err, + }).Warning("Problem with directory watcher") + } + } + }() + + for _, val := range maildirPaths { + err = watcher.Add(val + "/new") + if err != nil { + log.WithFields(log.Fields{ + "err": err, + "dir": val + "/new", + }).Error("Cannot watch directory") + } + } + + <-done }, }, {