s/log/glog/ and default to Mailder with trailing slash.

This should fix the problem of not recursing when Mailder is a symlink.
This commit is contained in:
Bill Thiede 2023-02-24 21:59:33 -08:00
parent 0dceaa33c2
commit 23a63911de

View File

@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"flag"
"log"
"net/http"
_ "net/http/pprof"
"os"
@ -11,10 +10,12 @@ import (
"sort"
"strings"
"time"
"github.com/golang/glog"
)
var (
root = flag.String("root", filepath.Join(os.Getenv("HOME"), "Maildir"),
root = flag.String("root", filepath.Join(os.Getenv("HOME"), "Maildir/"),
"root directory to watch")
poll = flag.Duration("poll", time.Second*time.Duration(10),
"poll interval between new file check")
@ -64,6 +65,7 @@ func isNewEmail(path string) bool {
// 1340520791.M843511P28536.sagan.sf.xinu.tv,S=1467,W=1503:2,RS
// Unseen:
// 1377995543.50996_2.sagan.sf.xinu.tv:2,Rb
// 1677301126.3645317_2126.nixos-01,U=38925:2,
seen := false
i := strings.LastIndex(path, ":")
if i != -1 {
@ -126,7 +128,7 @@ func (mc *mailCounter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
err := enc.Encode(c)
if err != nil {
log.Println("Error printing JSON:", err)
glog.Infoln("Error printing JSON:", err)
}
}
@ -188,11 +190,12 @@ func skip(path string) bool {
func (mc *mailCounter) fillNeedUpdate() error {
mc.needUpdate = mc.needUpdate[:0]
// log.Println("BEGIN fillNeedUpdate", mc.needUpdate)
// glog.Infoln("BEGIN fillNeedUpdate", mc.needUpdate)
// defer func() {
// log.Println("END fillNeedUpdate", mc.needUpdate)
// glog.Infoln("END fillNeedUpdate", mc.needUpdate)
// }()
return filepath.Walk(mc.root, func(path string, info os.FileInfo, err error) error {
glog.Infof("path %s", path)
if err != nil {
return err
}
@ -218,7 +221,7 @@ func (mc *mailCounter) watch(root string, poll time.Duration) error {
}
modCnt := len(mc.needUpdate)
if modCnt > 0 {
log.Println(modCnt, "folders need update")
glog.Infoln(modCnt, "folders need update")
update := map[string]struct{}{}
for _, dir := range mc.needUpdate {
// Store parent directory, which is the IMAP folder
@ -283,7 +286,7 @@ func printCounts(counts map[string]int) {
sort.Sort(cl)
for _, c := range cl {
if c.c > 0 {
log.Printf("%s (%d)", c.f, c.c)
glog.Infof("%s (%d)", c.f, c.c)
}
}
}
@ -291,17 +294,18 @@ func printCounts(counts map[string]int) {
func main() {
flag.Parse()
glog.Info("Starting")
mc := newMailCounter()
go func() {
http.Handle("/unread", mc)
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal(err)
glog.Fatal(err)
}
}()
err := mc.watch(*root, *poll)
if err != nil {
log.Fatal(err)
glog.Fatal(err)
}
}