From e7ac26f264ae8c13d99dfe68b1c2883b5c56abef Mon Sep 17 00:00:00 2001 From: Carlo Strub Date: Sat, 3 Feb 2018 22:08:12 +0100 Subject: [PATCH] Add dry run mode that does not move mails after classification (fixes #8) --- CHANGELOG.md | 18 ++++++++++++++++++ README.md | 9 +++------ classify.go | 16 ++++++++++++---- mail.go | 1 + sisyphus/sisyphus.go | 9 +++++++-- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df28fc9..d1a28f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,21 @@ +# Release 1.2.0 +## Added +- SISYPHUS_DRY_RUN flag to allow dry runs without moving files. In + fact, it does create missing folders, the database, learns, and + classifies mails. But, sisyphus does not move files between + folders. (#8) + +## Changed +- + +## Fixed +- Do not require config to get to help (#7) + +## Known Issues +- There seems to be an issue with quotedprintable not properly reading in + malformed mails. Currently, such is likely to pass the filter. + + # Release 1.1.1 ## Added - diff --git a/README.md b/README.md index 9ae73d2..8c5e57f 100644 --- a/README.md +++ b/README.md @@ -57,24 +57,21 @@ can put in your `$PATH`. (You can also take a look at `make install` to install for you.) ## Usage -First, set the environment variables necessary for operation: +First, set the environment variable necessary for operation: ``` $ setenv SISYPHUS_DIRS PATHTOMAILDIR -$ setenv SISYPHUS_DURATION 24h ``` or ``` $ export SISYPHUS_DIRS=PATHTOMAILDIR -$ export SISYPHUS_DURATION=24h ``` or for Windows ``` $ set SISYPHUS_DIRS=PATHTOMAILDIR -$ set SISYPHUS_DURATION=24h ``` - -Sisyphus help can be started by running +For all other configuration options, please consult the help. It can +be started by running ``` $ sisyphus help ``` diff --git a/classify.go b/classify.go index 5087887..9ce6e04 100644 --- a/classify.go +++ b/classify.go @@ -174,13 +174,21 @@ func (m *Mail) Classify(db *bolt.DB, dir Maildir) (err error) { // Move mail around if junk. if junk { - err = os.Rename(filepath.Join(string(dir), "new", m.Key), filepath.Join(string(dir), ".Junk", "cur", m.Key)) - if err != nil { - return err + if !m.DryRun { + err = os.Rename(filepath.Join(string(dir), "new", m.Key), filepath.Join(string(dir), ".Junk", "cur", m.Key)) + if err != nil { + return err + } } + + var dryRun string + if m.DryRun { + dryRun = "-- dry run (nothing happened to this mail!)" + } + log.WithFields(log.Fields{ "mail": m.Key, - }).Info("Moved to Junk folder") + }).Info("Moved to Junk folder" + dryRun) } err = m.Unload(dir) diff --git a/mail.go b/mail.go index 4867c81..3489d4d 100644 --- a/mail.go +++ b/mail.go @@ -27,6 +27,7 @@ type Mail struct { Key string Subject, Body *string Junk, New bool + DryRun bool } // CreateDirs creates all the required dirs -- if not already there. diff --git a/sisyphus/sisyphus.go b/sisyphus/sisyphus.go index 2083232..7eef10a 100644 --- a/sisyphus/sisyphus.go +++ b/sisyphus/sisyphus.go @@ -48,7 +48,9 @@ func main() { SISYPHUS_DIRS: Comma-separated list of maildirs, e.g. ./Maildir,/home/JohnDoe/Maildir - SISYPHUS_DURATION: Interval between learning periods, e.g. 12h + SISYPHUS_DURATION: Interval between learning periods, e.g. 12h. Default is set to 24h. + + SISYPHUS_DRY_RUN : If set, sisyphus will not move any mails around. `, } } @@ -143,8 +145,11 @@ COPYRIGHT: case event := <-watcher.Events: if event.Op&fsnotify.Create == fsnotify.Create { path := strings.Split(event.Name, "/new/") + + _, dryRun := os.LookupEnv("SISYPHUS_DRY_RUN") m := sisyphus.Mail{ - Key: path[1], + Key: path[1], + DryRun: dryRun, } err = m.Classify(dbs[sisyphus.Maildir(path[0])], sisyphus.Maildir(path[0]))