This is the main dovel repository, it has the Go code to run dovel SMTP server.
Author: bryon (git@mail.blmayer.dev)
Date: Fri Jul 21 22:14:25 2023 -0300
Parent: ea1e16e
Added update to list templates
commit 1c8af10ee8a102cc9d2866f36f5651061a58137e Author: bryon <git@mail.blmayer.dev> Date: Fri Jul 21 22:14:25 2023 -0300 Added update to list templates diff --git a/model/html/html.go b/model/html/html.go index a68c234..eb99598 100644 --- a/model/html/html.go +++ b/model/html/html.go @@ -12,6 +12,7 @@ import ( "net/textproto" "os" "path" + "sort" "strings" "text/template" "time" @@ -32,6 +33,7 @@ import ( // saved, each according to its configuration. type HTMLHandler struct { root string + out string domain string privateKey string indexTpl *template.Template @@ -41,7 +43,7 @@ type HTMLHandler struct { } func NewHTMLHandler(c model.HTMLConfig) (HTMLHandler, error) { - f := HTMLHandler{root: c.Root, domain: c.Domain} + f := HTMLHandler{root: c.Root, domain: c.Domain, out: c.Out} var err error f.mailTpl, err = template.ParseFiles(c.MailTpl) @@ -136,8 +138,9 @@ func (h HTMLHandler) Save(from, to string, r io.Reader) error { } } + htmlDir := path.Join(h.out, to, subj) htmlName := fmt.Sprintf("%s:%s.html", from, date.Format(time.RFC3339)) - file, err = os.Create(path.Join(mailDir, htmlName)) + file, err = os.Create(path.Join(htmlDir, htmlName)) if err != nil { return err } @@ -145,8 +148,101 @@ func (h HTMLHandler) Save(from, to string, r io.Reader) error { if err != nil { return err } - return file.Close() + file.Close() // TODO: Update index and lists + dir, err := os.ReadDir(path.Join(h.out)) + if err != nil { + fmt.Println("readDir error:", err.Error()) + return err + } + + var threads []struct { + Title string + LastMod time.Time + } + for _, d := range dir { + if !d.IsDir() { + continue + } + + info, err := d.Info() + if err != nil { + fmt.Println("dir info", err.Error()) + continue + } + t := struct { + Title string + LastMod time.Time + }{ + Title: d.Name(), + LastMod: info.ModTime(), + } + + threads = append(threads, t) + } + sort.Slice( + threads, + func(i, j int) bool { + return threads[i].LastMod.After(threads[j].LastMod) + }, + ) + + file, err = os.Create(path.Join(h.out, "index.html")) + if err != nil { + return err + } + err = h.indexTpl.Execute(file, payload) + if err != nil { + return err + } + file.Close() + + dir, err = os.ReadDir(path.Join(h.out, subj)) + if err != nil { + fmt.Println("readDir error:", err.Error()) + return err + } + + threads = []struct { + Title string + LastMod time.Time + }{} + for _, d := range dir { + if !d.IsDir() { + continue + } + + info, err := d.Info() + if err != nil { + fmt.Println("dir info", err.Error()) + continue + } + t := struct { + Title string + LastMod time.Time + }{ + Title: d.Name(), + LastMod: info.ModTime(), + } + + threads = append(threads, t) + } + sort.Slice( + threads, + func(i, j int) bool { + return threads[i].LastMod.After(threads[j].LastMod) + }, + ) + + file, err = os.Create(path.Join(h.out, subj, "index.html")) + if err != nil { + return err + } + err = h.listTpl.Execute(file, payload) + if err != nil { + return err + } + return file.Close() }
commit 1c8af10ee8a102cc9d2866f36f5651061a58137e Author: bryon <git@mail.blmayer.dev> Date: Fri Jul 21 22:14:25 2023 -0300 Added update to list templates diff --git a/model/main.go b/model/main.go index 9d0c241..ae187c5 100644 --- a/model/main.go +++ b/model/main.go @@ -48,6 +48,7 @@ type InboxConfig struct { type HTMLConfig struct { CommonConfig + Out string IndexTpl string ListTpl string MailsTpl string