From a8780a32c16ed61ed3ff5d3cdbf9af0d81f1ef2f Mon Sep 17 00:00:00 2001 From: Aine Date: Tue, 15 Nov 2022 09:42:07 +0200 Subject: [PATCH] explicitly tell about enqueued email --- bot/command.go | 11 ++++++++--- bot/email.go | 28 ++++++++++++++++++++-------- bot/queue.go | 15 +++++++-------- cmd/cmd.go | 1 + 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/bot/command.go b/bot/command.go index b3de58f..9c7c88c 100644 --- a/bot/command.go +++ b/bot/command.go @@ -381,12 +381,17 @@ func (b *Bot) runSend(ctx context.Context) { for _, to := range tos { email := utils.NewEmail(ID, "", " "+ID, subject, from, to, body, "", nil) data := email.Compose(b.getBotSettings().DKIMPrivateKey()) - err = b.sendmail(from, to, data) + queued, err := b.Sendmail(evt.ID, from, to, data) + if queued { + b.log.Error("cannot send email: %v", err) + b.saveSentMetadata(ctx, queued, evt.ID, email, &cfg) + continue + } if err != nil { b.Error(ctx, evt.RoomID, "cannot send email to %s: %v", to, err) - } else { - b.saveSentMetadata(ctx, evt.ID, email, &cfg) + continue } + b.saveSentMetadata(ctx, false, evt.ID, email, &cfg) } if len(tos) > 1 { b.SendNotice(ctx, evt.RoomID, "All emails were sent.") diff --git a/bot/email.go b/bot/email.go index 2f6c080..9004f96 100644 --- a/bot/email.go +++ b/bot/email.go @@ -37,17 +37,17 @@ func (b *Bot) SetSendmail(sendmail func(string, string, string) error) { // Sendmail tries to send email immediately, but if it gets 4xx error (greylisting), // the email will be added to the queue and retried several times after that -func (b *Bot) Sendmail(eventID id.EventID, from, to, data string) error { +func (b *Bot) Sendmail(eventID id.EventID, from, to, data string) (bool, error) { err := b.sendmail(from, to, data) if err != nil { if strings.HasPrefix(err.Error(), "45") { b.log.Debug("email %s (from=%s to=%s) was added to the queue: %v", eventID, from, to, err) - return b.enqueueEmail(eventID.String(), from, to, data) + return true, b.enqueueEmail(eventID.String(), from, to, data) } - return err + return false, err } - return nil + return false, nil } // GetDKIMprivkey returns DKIM private key @@ -177,12 +177,19 @@ func (b *Bot) SendEmailReply(ctx context.Context) { email := utils.NewEmail(ID, meta.InReplyTo, meta.References, meta.Subject, fromMailbox, meta.To, body, "", nil) data := email.Compose(b.getBotSettings().DKIMPrivateKey()) - err = b.Sendmail(evt.ID, meta.From, meta.To, data) + queued, err := b.Sendmail(evt.ID, meta.From, meta.To, data) + if queued { + b.log.Error("cannot send email: %v", err) + b.saveSentMetadata(ctx, queued, meta.ThreadID, email, &cfg) + return + } + if err != nil { b.Error(ctx, evt.RoomID, "cannot send email: %v", err) return } - b.saveSentMetadata(ctx, meta.ThreadID, email, &cfg) + + b.saveSentMetadata(ctx, queued, meta.ThreadID, email, &cfg) } type parentEmail struct { @@ -264,10 +271,15 @@ func (b *Bot) getParentEmail(evt *event.Event) parentEmail { // saveSentMetadata used to save metadata from !pm sent and thread reply events to a separate notice message // because that metadata is needed to determine email thread relations -func (b *Bot) saveSentMetadata(ctx context.Context, threadID id.EventID, email *utils.Email, cfg *roomSettings) { +func (b *Bot) saveSentMetadata(ctx context.Context, queued bool, threadID id.EventID, email *utils.Email, cfg *roomSettings) { + text := "Email has been sent to " + email.To + if queued { + text = "Email to " + email.To + " has been queued" + } + evt := eventFromContext(ctx) content := email.Content(threadID, cfg.ContentOptions()) - notice := format.RenderMarkdown("Email has been sent to "+email.To, true, true) + notice := format.RenderMarkdown(text, true, true) notice.MsgType = event.MsgNotice msgContent, ok := content.Parsed.(event.MessageEventContent) if !ok { diff --git a/bot/queue.go b/bot/queue.go index 6615c09..b35b33a 100644 --- a/bot/queue.go +++ b/bot/queue.go @@ -2,12 +2,11 @@ package bot import ( "strconv" - "time" ) const ( defaultMaxQueueItems = 1 - defaultMaxQueueTries = 100 + defaultMaxQueueTries = 3 ) // ProcessQueue starts queue processing @@ -67,6 +66,7 @@ func (b *Bot) processQueueItem(itemkey string, maxRetries int) bool { b.log.Error("cannot retrieve a queue item %s: %v", itemkey, err) return false } + b.log.Debug("processing queue item %+v", item) attempts, err := strconv.Atoi(item["attempts"]) if err != nil { b.log.Error("cannot parse attempts of %s: %v", itemkey, err) @@ -97,12 +97,11 @@ func (b *Bot) processQueueItem(itemkey string, maxRetries int) bool { func (b *Bot) enqueueEmail(id, from, to, data string) error { itemkey := acQueueKey + "." + id item := map[string]string{ - "attemptedAt": time.Now().UTC().Format(time.RFC1123Z), - "attempts": "0", - "data": data, - "from": from, - "to": to, - "id": id, + "attempts": "0", + "data": data, + "from": from, + "to": to, + "id": id, } b.lock(itemkey) diff --git a/cmd/cmd.go b/cmd/cmd.go index ee80381..f856433 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -146,6 +146,7 @@ func startBot(statusMsg string) { func shutdown() { log.Info("Shutting down...") + cron.Shutdown() smtpm.Stop() mxb.Stop()