You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sisyphus/sisyphus/sisyphus.go

254 lines
6.5 KiB
Go

7 years ago
package main
import (
7 years ago
"fmt"
"os"
7 years ago
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/carlostrub/sisyphus"
"github.com/urfave/cli"
)
var (
version string
)
func main() {
// Define App
app := cli.NewApp()
7 years ago
app.Name = "Sisyphus"
app.Usage = "Intelligent Junk and Spam Mail Handler"
7 years ago
app.UsageText = `sisyphus [global options] command [command options]
Sisyphus applies artificial intelligence to filter
Junk mail in an unobtrusive way. Both, classification and learning
operate directly on the Maildir of a user in a fully transparent mode,
without any need for configuration or active operation.`
app.HelpName = "Intelligent Junk and Spam Mail Handler"
app.Version = version
app.Copyright = "(c) 2017, Carlo Strub. All rights reserved. This binary is licensed under a BSD 3-Clause License."
7 years ago
app.Authors = []cli.Author{
7 years ago
{
Name: "Carlo Strub",
Email: "cs@carlostrub.ch",
},
}
maildirPaths := cli.StringSlice([]string{})
7 years ago
var pidfile *string
pidfile = new(string)
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "maildirs, d",
Value: &maildirPaths,
EnvVar: "SISYPHUS_DIRS",
Usage: "Comma separated list of paths to the Maildir directories",
},
7 years ago
cli.StringFlag{
Name: "pidfile, p",
Value: "/tmp/sisyphus.pid",
EnvVar: "SISYPHUS_PID",
Usage: "Location of PID file",
Destination: pidfile,
},
}
7 years ago
app.Commands = []cli.Command{
{
7 years ago
Name: "run",
Aliases: []string{"u"},
Usage: "run sisyphus",
Action: func(c *cli.Context) {
// check if daemon already running.
if _, err := os.Stat(*pidfile); err == nil {
log.Fatal("sisyphus running or " + *pidfile + " file exists.")
}
7 years ago
fmt.Print(`
7 years ago
by Carlo Strub <cs@carlostrub.ch>
`)
7 years ago
// Make arrangement to remove PID file upon receiving the SIGTERM from kill command
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
signalType := <-ch
signal.Stop(ch)
log.Println("Exit command received. Exiting sisyphus...")
// this is a good place to flush everything to disk
// before terminating.
log.Println("Received signal type: ", signalType)
// remove PID file
os.Remove(*pidfile)
os.Exit(0)
}()
if len(maildirPaths) < 1 {
log.Fatal("No Maildir set. Please check the manual.")
}
// Populate maildir with the maildirs given by setting the flag.
var maildirs []sisyphus.Maildir
for _, val := range maildirPaths {
maildirs = append(maildirs, sisyphus.Maildir(val))
}
// Load all mails
mails, err := sisyphus.LoadMails(maildirs)
if err != nil {
log.Fatal(err)
}
// Open all databases
dbs, err := sisyphus.LoadDatabases(maildirs)
if err != nil {
log.Fatal(err)
}
defer sisyphus.CloseDatabases(dbs)
// Learn at startup
for _, d := range maildirs {
db := dbs[d]
m := mails[d]
for _, val := range m {
err := val.Learn(db)
if err != nil {
log.Fatal(err)
}
}
}
// // Classify on arrival
// watcher, err := fsnotify.NewWatcher()
// if err != nil {
// log.Fatal(err)
// }
// 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
7 years ago
},
},
{
// See
// https://www.socketloop.com/tutorials/golang-daemonizing-a-simple-web-server-process-example
// for the process we are using to daemonize
7 years ago
Name: "start",
Aliases: []string{"s"},
7 years ago
Usage: "start sisyphus daemon in the background",
7 years ago
Action: func(c *cli.Context) error {
err := sisyphus.Pidfile(*pidfile).DaemonStart()
7 years ago
if err != nil {
log.Fatal(err)
7 years ago
}
7 years ago
return nil
},
},
{
Name: "stop",
Aliases: []string{"e"},
Usage: "stop sisyphus daemon",
Action: func(c *cli.Context) error {
7 years ago
err := sisyphus.Pidfile(*pidfile).DaemonStop()
7 years ago
if err != nil {
log.Fatal(err)
7 years ago
}
7 years ago
return nil
},
},
{
Name: "restart",
Aliases: []string{"r"},
Usage: "restart sisyphus daemon",
Action: func(c *cli.Context) error {
7 years ago
err := sisyphus.Pidfile(*pidfile).DaemonRestart()
7 years ago
if err != nil {
log.Fatal(err)
7 years ago
}
7 years ago
return nil
},
},
{
Name: "status",
Aliases: []string{"i"},
Usage: "status of sisyphus",
Action: func(c *cli.Context) error {
7 years ago
log.Println("here, we should get statistics from the db, TBD...")
7 years ago
return nil
},
},
7 years ago
}
app.Run(os.Args)
7 years ago
}