190 lines
3.4 KiB
Go
190 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"xinu.tv/email"
|
|
|
|
"github.com/golang/glog"
|
|
)
|
|
|
|
var (
|
|
dryrun = flag.Bool("dryrun", true, "don't actually rename files, just log actions.")
|
|
saveFile = flag.String("save", "", "filename to store unread status.")
|
|
loadFile = flag.String("load", "", "filename to load unread status.")
|
|
maildir = flag.String("maildir", "", "Maildir root")
|
|
)
|
|
|
|
type Status struct {
|
|
Hash string `json:"hash"`
|
|
Path string `json:"path,omitempthashy"`
|
|
Read bool `json:"read,omitempty"`
|
|
}
|
|
|
|
type Messages []Status
|
|
|
|
var headers = []string{"to", "from", "cc", "date", "subject"}
|
|
|
|
func (m *Messages) hashMail(path string, info os.FileInfo, err error) error {
|
|
glog.Infoln("Processing", path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
r, err := os.Open(path)
|
|
if err != nil {
|
|
glog.Fatal(err)
|
|
}
|
|
h, err := email.Hash(r, headers)
|
|
if err != nil {
|
|
glog.Errorf("%s %q", path, err.Error())
|
|
return nil
|
|
}
|
|
md := email.NewInfo(path)
|
|
*m = append(*m, Status{
|
|
Path: path,
|
|
Hash: fmt.Sprintf("%x", h.Sum(nil)),
|
|
Read: md.Seen,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (m *Messages) LoadStatus(statusFile string) error {
|
|
glog.Infof("Loading file %q", statusFile)
|
|
r, err := os.Open(statusFile)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer r.Close()
|
|
dec := json.NewDecoder(r)
|
|
return dec.Decode(m)
|
|
}
|
|
|
|
func (m *Messages) SaveStatus(maildir, statusFile string) error {
|
|
if err := filepath.Walk(maildir, m.hashMail); err != nil {
|
|
return err
|
|
}
|
|
|
|
b, err := json.MarshalIndent(m, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
glog.Infof("Saving file %q", statusFile)
|
|
f, err := os.Create(statusFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
n, err := f.Write(b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != len(b) {
|
|
return fmt.Errorf("Short write of save status: wrote %d of %d", n,
|
|
len(b))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m Messages) Reconcile(maildir string) error {
|
|
hashMap := make(map[string]*Status, len(m))
|
|
for i, msg := range m {
|
|
hashMap[msg.Hash] = &m[i]
|
|
}
|
|
|
|
return filepath.Walk(maildir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
r, err := os.Open(path)
|
|
if err != nil {
|
|
glog.Fatal(err)
|
|
}
|
|
|
|
h, err := email.Hash(r, headers)
|
|
if err != nil {
|
|
glog.Errorf("%s %q", path, err.Error())
|
|
return nil
|
|
}
|
|
|
|
chksum := fmt.Sprintf("%x", h.Sum(nil))
|
|
md := email.NewInfo(path)
|
|
s, ok := hashMap[chksum]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
glog.V(2).Infof("Comparing flags of %q to %q", path, s.Path)
|
|
if md.Seen != s.Read {
|
|
md.Seen = s.Read
|
|
newPath := md.String()
|
|
glog.Infof("%s => %s", path, newPath)
|
|
if !*dryrun {
|
|
os.Rename(path, newPath)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (m Messages) PrintStats() {
|
|
r, u, t := 0, 0, 0
|
|
for _, msg := range m {
|
|
t++
|
|
if msg.Read {
|
|
r++
|
|
} else {
|
|
u++
|
|
}
|
|
}
|
|
fmt.Println(r, "read")
|
|
fmt.Println(u, "unread")
|
|
fmt.Println(t, "total")
|
|
}
|
|
|
|
func main() {
|
|
defer glog.Flush()
|
|
flag.Parse()
|
|
m := Messages{}
|
|
|
|
if *maildir == "" {
|
|
fmt.Println("Must specify Maildir with -maildir")
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *saveFile == "" && *loadFile == "" {
|
|
fmt.Println("Must specify one of -save or -load")
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *saveFile != "" {
|
|
if err := m.SaveStatus(*maildir, *saveFile); err != nil {
|
|
glog.Fatal(err)
|
|
}
|
|
m.PrintStats()
|
|
}
|
|
|
|
if *loadFile != "" {
|
|
if err := m.LoadStatus(*loadFile); err != nil {
|
|
glog.Fatal(err)
|
|
}
|
|
m.PrintStats()
|
|
if err := m.Reconcile(*maildir); err != nil {
|
|
}
|
|
}
|
|
}
|