mirror of https://github.com/carlostrub/sisyphus
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.
275 lines
6.7 KiB
Go
275 lines
6.7 KiB
Go
7 years ago
|
package main
|
||
|
|
||
|
import (
|
||
7 years ago
|
"fmt"
|
||
7 years ago
|
"log"
|
||
7 years ago
|
"os"
|
||
7 years ago
|
"os/signal"
|
||
7 years ago
|
"strings"
|
||
7 years ago
|
"syscall"
|
||
7 years ago
|
|
||
7 years ago
|
"github.com/boltdb/bolt"
|
||
7 years ago
|
"github.com/fsnotify/fsnotify"
|
||
7 years ago
|
"github.com/urfave/cli"
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
const (
|
||
|
good = "0"
|
||
|
junk = "1"
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
func main() {
|
||
7 years ago
|
|
||
7 years ago
|
// 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
|
||
7 years ago
|
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.`
|
||
7 years ago
|
app.HelpName = "Intelligent Junk and Spam Mail Handler"
|
||
|
app.Version = "0.0.0"
|
||
7 years ago
|
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{
|
||
|
cli.Author{
|
||
7 years ago
|
Name: "Carlo Strub",
|
||
|
Email: "cs@carlostrub.ch",
|
||
|
},
|
||
|
}
|
||
|
|
||
7 years ago
|
maildirPaths := cli.StringSlice([]string{})
|
||
7 years ago
|
|
||
|
var pidfile *string
|
||
|
pidfile = new(string)
|
||
|
|
||
7 years ago
|
app.Flags = []cli.Flag{
|
||
7 years ago
|
|
||
|
cli.StringSliceFlag{
|
||
|
Name: "maildirs, d",
|
||
|
Value: &maildirPaths,
|
||
|
EnvVar: "SISYPHUS_DIRS",
|
||
|
Usage: "Comma separated list of paths to the Maildir directories",
|
||
7 years ago
|
},
|
||
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
|
}
|
||
|
|
||
7 years ago
|
app.Commands = []cli.Command{
|
||
|
{
|
||
7 years ago
|
Name: "run",
|
||
|
Aliases: []string{"u"},
|
||
|
Usage: "run sisyphus",
|
||
|
Action: func(c *cli.Context) {
|
||
|
|
||
7 years ago
|
// 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
|
|
||
7 years ago
|
███████╗██╗███████╗██╗ ██╗██████╗ ██╗ ██╗██╗ ██╗███████╗
|
||
|
██╔════╝██║██╔════╝╚██╗ ██╔╝██╔══██╗██║ ██║██║ ██║██╔════╝
|
||
|
███████╗██║███████╗ ╚████╔╝ ██████╔╝███████║██║ ██║███████╗
|
||
|
╚════██║██║╚════██║ ╚██╔╝ ██╔═══╝ ██╔══██║██║ ██║╚════██║
|
||
|
███████║██║███████║ ██║ ██║ ██║ ██║╚██████╔╝███████║
|
||
|
╚══════╝╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝
|
||
7 years ago
|
|
||
7 years ago
|
by Carlo Strub <cs@carlostrub.ch>
|
||
|
|
||
7 years ago
|
|
||
|
`)
|
||
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)
|
||
|
|
||
|
}()
|
||
7 years ago
|
|
||
|
// Load the Maildir
|
||
|
if len(maildirPaths) < 1 {
|
||
|
log.Fatal("No Maildir set.")
|
||
|
}
|
||
|
if len(maildirPaths) > 1 {
|
||
|
log.Fatal("Sorry... only one Maildir supported as of today.")
|
||
|
}
|
||
|
|
||
7 years ago
|
CreateDirs(maildirPaths[0])
|
||
7 years ago
|
|
||
7 years ago
|
mails, err := Index(maildirPaths[0])
|
||
7 years ago
|
if err != nil {
|
||
|
log.Fatal("Wrong path to Maildir")
|
||
|
}
|
||
|
|
||
|
// Open the database
|
||
|
db, err := openDB(maildirPaths[0])
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer db.Close()
|
||
|
|
||
7 years ago
|
// Handle all mails after startup
|
||
7 years ago
|
for i := range mails {
|
||
7 years ago
|
db.View(func(tx *bolt.Tx) error {
|
||
|
b := tx.Bucket([]byte("Processed"))
|
||
7 years ago
|
bMails := b.Bucket([]byte("Mails"))
|
||
|
v := bMails.Get([]byte(mails[i].Key))
|
||
7 years ago
|
if len(v) == 0 {
|
||
7 years ago
|
err = mails[i].Classify(db)
|
||
7 years ago
|
if err != nil {
|
||
|
log.Print(err)
|
||
|
}
|
||
7 years ago
|
err = mails[i].Learn(db)
|
||
7 years ago
|
if err != nil {
|
||
|
log.Print(err)
|
||
|
}
|
||
7 years ago
|
}
|
||
|
if string(v) == good && mails[i].Junk == true {
|
||
7 years ago
|
err = mails[i].Learn(db)
|
||
7 years ago
|
if err != nil {
|
||
|
log.Print(err)
|
||
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
if string(v) == junk && mails[i].Junk == false {
|
||
7 years ago
|
err = mails[i].Learn(db)
|
||
7 years ago
|
if err != nil {
|
||
|
log.Print(err)
|
||
|
}
|
||
7 years ago
|
}
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
7 years ago
|
// Handle mails as the arrive
|
||
|
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 {
|
||
7 years ago
|
mailName := strings.Split(event.Name, "/")
|
||
|
m := Mail{
|
||
|
Key: mailName[len(mailName)-1],
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
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)
|
||
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
}
|
||
|
case err := <-watcher.Errors:
|
||
|
log.Println("error:", err)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
7 years ago
|
|
||
7 years ago
|
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 {
|
||
7 years ago
|
|
||
7 years ago
|
err := daemonStart(*pidfile)
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
log.Fatal(err)
|
||
7 years ago
|
}
|
||
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
|
|
||
7 years ago
|
err := daemonStop(*pidfile)
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
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
|
|
||
7 years ago
|
err := daemonRestart(*pidfile)
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
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
|
}
|
||
|
|
||
7 years ago
|
app.Run(os.Args)
|
||
7 years ago
|
}
|