2017-04-15 20:23:26 +00:00
|
|
|
package sisyphus
|
2017-03-11 20:02:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-19 20:54:23 +00:00
|
|
|
"log"
|
|
|
|
|
2017-03-11 20:02:17 +00:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
2017-04-17 21:13:18 +00:00
|
|
|
// OpenDB creates and opens a new database and its respective buckets (if required)
|
|
|
|
func OpenDB(maildir string) (db *bolt.DB, err error) {
|
2017-03-11 20:02:17 +00:00
|
|
|
|
2017-03-19 20:54:23 +00:00
|
|
|
log.Println("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-03-12 22:50:43 +00:00
|
|
|
db, err = bolt.Open(maildir+"/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 {
|
|
|
|
_, err := tx.CreateBucketIfNotExists([]byte("Processed"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
2017-03-19 20:54:23 +00:00
|
|
|
// Create DB bucket for Mails inside bucket Processed
|
|
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket([]byte("Processed"))
|
|
|
|
_, err := b.CreateBucketIfNotExists([]byte("Mails"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create DB bucket for Counters inside bucket Processed
|
|
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket([]byte("Processed"))
|
|
|
|
_, err := b.CreateBucketIfNotExists([]byte("Counters"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-03-19 20:54:23 +00:00
|
|
|
log.Println("database loaded")
|
2017-03-14 20:27:05 +00:00
|
|
|
return db, err
|
2017-03-11 20:02:17 +00:00
|
|
|
}
|