2017-04-15 20:23:26 +00:00
|
|
|
package sisyphus
|
2017-03-11 20:02:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-18 19:21:46 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-03-19 20:54:23 +00:00
|
|
|
|
2017-03-11 20:02:17 +00:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
2017-05-08 03:29:25 +00:00
|
|
|
// openDB creates and opens a new database and its respective buckets (if required)
|
|
|
|
func openDB(m Maildir) (db *bolt.DB, err error) {
|
2017-03-11 20:02:17 +00:00
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"maildir": m,
|
|
|
|
}).Info("Loading database")
|
2017-03-11 20:02:17 +00:00
|
|
|
// Open the sisyphus.db data file in your current directory.
|
|
|
|
// It will be created if it doesn't exist.
|
2017-05-08 03:29:25 +00:00
|
|
|
db, err = bolt.Open(string(m)+"/sisyphus.db", 0600, nil)
|
2017-03-11 20:02:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create DB bucket for the map of processed e-mail IDs
|
|
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
2017-05-08 03:29:25 +00:00
|
|
|
_, err := tx.CreateBucketIfNotExists([]byte("Statistics"))
|
2017-04-26 11:20:21 +00:00
|
|
|
return err
|
2017-03-19 20:54:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
2017-03-11 20:02:17 +00:00
|
|
|
// Create DB bucket for word lists
|
|
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists([]byte("Wordlists"))
|
2017-04-26 11:20:21 +00:00
|
|
|
return err
|
2017-03-11 20:02:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"))
|
2017-04-26 11:20:21 +00:00
|
|
|
return err
|
2017-03-11 20:02:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:13:39 +00:00
|
|
|
// Create DB bucket for Good inside bucket Wordlists
|
2017-03-11 20:02:17 +00:00
|
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket([]byte("Wordlists"))
|
|
|
|
_, err := b.CreateBucketIfNotExists([]byte("Good"))
|
2017-04-26 11:20:21 +00:00
|
|
|
return err
|
2017-03-11 20:02:17 +00:00
|
|
|
})
|
|
|
|
|
2017-03-14 20:27:05 +00:00
|
|
|
return db, err
|
2017-03-11 20:02:17 +00:00
|
|
|
}
|
2017-05-08 03:29:25 +00:00
|
|
|
|
|
|
|
// LoadDatabases loads all databases from a given slice of Maildirs
|
|
|
|
func LoadDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) {
|
|
|
|
databases = make(map[Maildir]*bolt.DB)
|
|
|
|
for _, val := range d {
|
|
|
|
databases[val], err = openDB(val)
|
|
|
|
if err != nil {
|
|
|
|
return databases, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:43:17 +00:00
|
|
|
log.Info("All databases loaded")
|
2017-05-10 03:45:11 +00:00
|
|
|
|
2017-05-08 03:29:25 +00:00
|
|
|
return databases, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseDatabases closes all databases from a given slice of Maildirs
|
|
|
|
func CloseDatabases(databases map[Maildir]*bolt.DB) {
|
2017-05-10 03:45:11 +00:00
|
|
|
for key, val := range databases {
|
2017-05-08 03:29:25 +00:00
|
|
|
err := val.Close()
|
|
|
|
if err != nil {
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"db": string(key) + "/sisyphus.db",
|
|
|
|
}).Warn("Unable to close database")
|
2017-05-08 03:29:25 +00:00
|
|
|
}
|
2017-05-23 20:43:17 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"db": string(key) + "/sisyphus.db",
|
|
|
|
}).Info("Database closed")
|
2017-05-08 03:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|