diff --git a/classify.go b/classify.go index de60b54..fdcde4f 100644 --- a/classify.go +++ b/classify.go @@ -32,7 +32,8 @@ func classificationLikelihoodWordcounts(db *bolt.DB, word string) (gN, jN float6 good := b.Bucket([]byte("Good")) gWordRaw := good.Get([]byte(word)) if len(gWordRaw) > 0 { - gWordHLL, err := hllpp.Unmarshal(gWordRaw) + var gWordHLL *hllpp.HLLPP + gWordHLL, err = hllpp.Unmarshal(gWordRaw) if err != nil { return err } @@ -41,7 +42,8 @@ func classificationLikelihoodWordcounts(db *bolt.DB, word string) (gN, jN float6 junk := b.Bucket([]byte("Junk")) jWordRaw := junk.Get([]byte(word)) if len(jWordRaw) > 0 { - jWordHLL, err := hllpp.Unmarshal(jWordRaw) + var jWordHLL *hllpp.HLLPP + jWordHLL, err = hllpp.Unmarshal(jWordRaw) if err != nil { return err } @@ -62,7 +64,8 @@ func classificationStatistics(db *bolt.DB) (gTotal, jTotal float64, err error) { p := tx.Bucket([]byte("Statistics")) gRaw := p.Get([]byte("ProcessedGood")) if len(gRaw) > 0 { - gHLL, err := hllpp.Unmarshal(gRaw) + var gHLL *hllpp.HLLPP + gHLL, err = hllpp.Unmarshal(gRaw) if err != nil { return err } @@ -70,7 +73,8 @@ func classificationStatistics(db *bolt.DB) (gTotal, jTotal float64, err error) { } jRaw := p.Get([]byte("ProcessedJunk")) if len(jRaw) > 0 { - jHLL, err := hllpp.Unmarshal(jRaw) + var jHLL *hllpp.HLLPP + jHLL, err = hllpp.Unmarshal(jRaw) if err != nil { return err } @@ -169,7 +173,7 @@ func (m *Mail) Classify(db *bolt.DB, dir Maildir) (err error) { // Move mail around if junk. if junk { - err := os.Rename(string(dir)+"/new/"+m.Key, string(dir)+"/.Junk/cur/"+m.Key) + err = os.Rename(string(dir)+"/new/"+m.Key, string(dir)+"/.Junk/cur/"+m.Key) if err != nil { return err } diff --git a/daemon.go b/daemon.go deleted file mode 100644 index 849b5ea..0000000 --- a/daemon.go +++ /dev/null @@ -1,144 +0,0 @@ -package sisyphus - -import ( - "io/ioutil" - "os" - "os/exec" - "strconv" - - log "github.com/sirupsen/logrus" -) - -// See -// https://www.socketloop.com/tutorials/golang-daemonizing-a-simple-web-server-process-example -// for the process we are using to daemonize - -// Pidfile holds the Process ID file of sisyphus -type Pidfile string - -// savePID stores a pidfile -func (p Pidfile) savePID(process int) error { - file, err := os.Create(string(p)) - if err != nil { - return err - } - defer file.Close() - - _, err = file.WriteString(strconv.Itoa(process)) - if err != nil { - return err - } - - err = file.Sync() - - return err -} - -// DaemonStart starts sisyphus as a backgound process -func (p Pidfile) DaemonStart() { - // check if daemon already running. - if _, err := os.Stat(string(p)); err == nil { - - log.WithFields(log.Fields{ - "pidfile": p, - }).Fatal("Already running or pidfile exists") - - } - - log.Info("Starting sisyphus daemon") - cmd := exec.Command(os.Args[0], "run") - cmd.Start() - - log.WithFields(log.Fields{ - "pid": cmd.Process.Pid, - }).Info("Sisyphus started") - err := (p).savePID(cmd.Process.Pid) - if err != nil { - log.WithFields(log.Fields{ - "err": err, - }).Error("Save process ID file") - } - log.WithFields(log.Fields{ - "pidfile": p, - }).Info("Process ID file stored") - - return -} - -// DaemonStop stops a running sisyphus background process -func (p Pidfile) DaemonStop() { - - _, err := os.Stat(string(p)) - if err != nil { - log.Fatal("Sisyphus is not running") - } - - processIDRaw, err := ioutil.ReadFile(string(p)) - if err != nil { - log.Fatal("Sisyphus is not running") - } - - processID, err := strconv.Atoi(string(processIDRaw)) - if err != nil { - log.WithFields(log.Fields{ - "pid": p, - }).Fatal("Unable to read process ID") - } - - process, err := os.FindProcess(processID) - if err != nil { - log.WithFields(log.Fields{ - "pid": p, - "err": err, - }).Fatal("Unable to find process ID") - } - - // remove PID file - err = os.Remove(string(p)) - if err != nil { - log.Error("Unable to remove process ID file") - } - - log.WithFields(log.Fields{ - "pid": processID, - }).Info("Stopping sisyphus process") - // kill process and exit immediately - err = process.Kill() - if err != nil { - log.WithFields(log.Fields{ - "pid": processID, - "err": err, - }).Fatal("Unable to kill sisyphus process") - } - - log.Info("Sisyphus stopped") - os.Exit(0) - - return -} - -// DaemonRestart restarts a running sisyphus background process -func (p Pidfile) DaemonRestart() { - _, err := os.Stat(string(p)) - if err != nil { - log.Fatal("Sisyphus not running") - } - - pid, err := ioutil.ReadFile(string(p)) - if err != nil { - log.Fatal("Sisyphus not running") - } - - log.WithFields(log.Fields{ - "pid": string(pid), - }).Info("Stopping sisyphus process") - cmd := exec.Command(os.Args[0], "stop") - cmd.Start() - - cmd = exec.Command(os.Args[0], "start") - cmd.Start() - - log.Info("Sisyphus restarted") - - return -} diff --git a/database.go b/database.go index e104bbd..179083f 100644 --- a/database.go +++ b/database.go @@ -21,7 +21,7 @@ func openDB(m Maildir) (db *bolt.DB, err error) { // Create DB bucket for the map of processed e-mail IDs err = db.Update(func(tx *bolt.Tx) error { - _, err := tx.CreateBucketIfNotExists([]byte("Statistics")) + _, err = tx.CreateBucketIfNotExists([]byte("Statistics")) return err }) if err != nil { @@ -30,7 +30,7 @@ func openDB(m Maildir) (db *bolt.DB, err error) { // Create DB bucket for word lists err = db.Update(func(tx *bolt.Tx) error { - _, err := tx.CreateBucketIfNotExists([]byte("Wordlists")) + _, err = tx.CreateBucketIfNotExists([]byte("Wordlists")) return err }) if err != nil { @@ -40,7 +40,7 @@ func openDB(m Maildir) (db *bolt.DB, err error) { // Create DB bucket for Junk inside bucket Wordlists err = db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("Wordlists")) - _, err := b.CreateBucketIfNotExists([]byte("Junk")) + _, err = b.CreateBucketIfNotExists([]byte("Junk")) return err }) if err != nil { @@ -50,7 +50,7 @@ func openDB(m Maildir) (db *bolt.DB, err error) { // Create DB bucket for Good inside bucket Wordlists err = db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("Wordlists")) - _, err := b.CreateBucketIfNotExists([]byte("Good")) + _, err = b.CreateBucketIfNotExists([]byte("Good")) return err }) @@ -85,6 +85,4 @@ func CloseDatabases(databases map[Maildir]*bolt.DB) { "db": string(key) + "/sisyphus.db", }).Info("Database closed") } - - return } diff --git a/learn.go b/learn.go index 3d56063..4de90f0 100644 --- a/learn.go +++ b/learn.go @@ -90,7 +90,7 @@ func (m *Mail) Learn(db *bolt.DB, dir Maildir) (err error) { // Learn words for _, val := range list { - err := m.learnWordlist(val, db) + err = m.learnWordlist(val, db) if err != nil { return err } diff --git a/mail.go b/mail.go index a8e9dc1..4b18a9c 100644 --- a/mail.go +++ b/mail.go @@ -86,7 +86,6 @@ func (d Maildir) Index() (m []*Mail, err error) { func (m *Mail) Load(dir Maildir) (err error) { var message *mail.Message - message = new(mail.Message) switch { case m.Junk: