tweak to email message with updates, and cleanup temporary changelog file in case of delivery error

This commit is contained in:
Mechiel Lukkien 2023-03-05 23:15:34 +01:00
parent f867bcfa21
commit 1bee32679a
No known key found for this signature in database

View file

@ -263,8 +263,9 @@ requested, other TLS certificates are requested on demand.
var cl string var cl string
for _, c := range changelog.Changes { for _, c := range changelog.Changes {
cl += c.Text + "\n\n" cl += "----\n\n" + strings.TrimSpace(c.Text)
} }
cl += "----"
a, err := store.OpenAccount(mox.Conf.Static.Postmaster.Account) a, err := store.OpenAccount(mox.Conf.Static.Postmaster.Account)
if err != nil { if err != nil {
@ -280,18 +281,26 @@ requested, other TLS certificates are requested on demand.
log.Infox("making temporary message file for changelog delivery", err) log.Infox("making temporary message file for changelog delivery", err)
return next return next
} }
defer func() {
if f != nil {
err := os.Remove(f.Name())
log.Check(err, "removing temp changelog file")
err = f.Close()
log.Check(err, "closing temp changelog file")
}
}()
m := &store.Message{Received: time.Now(), Flags: store.Flags{Flagged: true}} m := &store.Message{Received: time.Now(), Flags: store.Flags{Flagged: true}}
n, err := fmt.Fprintf(f, "Date: %s\r\nSubject: mox update %s available, changelog\r\n\r\nHi!\r\n\r\nVersion %s of mox is available.\r\nThe changes compared to the previous update notification email:\r\n\r\n%s\r\n\r\nDon't forget to update, this install is at %s.\r\nPlease report any issues at https://github.com/mjl-/mox\r\n", time.Now().Format(message.RFC5322Z), latest, latest, strings.ReplaceAll(cl, "\n", "\r\n"), current) n, err := fmt.Fprintf(f, "Date: %s\r\nSubject: mox %s available\r\n\r\nHi!\r\n\r\nVersion %s of mox is available, this is install is at %s.\r\n\r\nChanges:\r\n\r\n%s\r\n\r\nPlease report any issues at https://github.com/mjl-/mox, thanks!\r\n\r\nCheers,\r\nmox\r\n", time.Now().Format(message.RFC5322Z), latest, latest, current, strings.ReplaceAll(cl, "\n", "\r\n"))
if err != nil { if err != nil {
log.Infox("writing temporary message file for changelog delivery", err) log.Infox("writing temporary message file for changelog delivery", err)
return next return next
} }
m.Size = int64(n) m.Size = int64(n)
if err := a.DeliverMailbox(log, mox.Conf.Static.Postmaster.Mailbox, m, f, true); err != nil { if err := a.DeliverMailbox(log, mox.Conf.Static.Postmaster.Mailbox, m, f, true); err != nil {
log.Infox("changelog delivery", err) log.Errorx("changelog delivery", err)
err := os.Remove(f.Name()) return next
log.Check(err, "removing temporary changelog message after delivery failure")
} }
f = nil
log.Info("delivered changelog", mlog.Field("current", current), mlog.Field("lastknown", lastknown), mlog.Field("latest", latest)) log.Info("delivered changelog", mlog.Field("current", current), mlog.Field("lastknown", lastknown), mlog.Field("latest", latest))
if err := mox.StoreLastKnown(latest); err != nil { if err := mox.StoreLastKnown(latest); err != nil {
// This will be awkward, we'll keep notifying the postmaster once every 24h... // This will be awkward, we'll keep notifying the postmaster once every 24h...