From 1386ac2275e183e5457f8df7dbdfc4247fd95706 Mon Sep 17 00:00:00 2001 From: Xpl0itU <24777100+Xpl0itU@users.noreply.github.com> Date: Sat, 9 Sep 2023 19:49:42 +0200 Subject: [PATCH] Fixed moving multiple messages --- main.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 60f072f..de444aa 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,9 @@ func checkEmailsWithFilters(filters []MailFilter, server, email, password, mailO c.Create(mailOkFolder) c.Create(mailFailedFolder) + failedSet := new(imap.SeqSet) + okSet := new(imap.SeqSet) + for _, filter := range filters { messages, err := searchEmails(c, filter) if err != nil { @@ -68,14 +71,28 @@ func checkEmailsWithFilters(filters []MailFilter, server, email, password, mailO for _, msg := range messages { if filter.FailIfFound { fmt.Printf("Error: %+v\n", filter) - moveMessage(c, msg, mailFailedFolder) + failedSet.AddNum(msg.SeqNum) anyErrors = true } else { - moveMessage(c, msg, mailOkFolder) + okSet.AddNum(msg.SeqNum) } } } + if !okSet.Empty() { + if err := moveMessages(c, okSet, mailOkFolder); err != nil { + fmt.Println(err) + } + fmt.Printf("Moved messages to %s\n", mailOkFolder) + } + + if !failedSet.Empty() { + if err := moveMessages(c, failedSet, mailFailedFolder); err != nil { + fmt.Println(err) + } + fmt.Printf("Moved messages to %s\n", mailFailedFolder) + } + if anyErrors { return errors.New("found errors") } @@ -134,13 +151,10 @@ func searchEmails(c *client.Client, filter MailFilter) ([]*imap.Message, error) return messages, nil } -func moveMessage(c *client.Client, msg *imap.Message, folderName string) error { - set := new(imap.SeqSet) - set.AddNum(msg.SeqNum) - err := c.Move(set, folderName) +func moveMessages(c *client.Client, messages *imap.SeqSet, folderName string) error { + err := c.Move(messages, folderName) if err != nil { return err } - fmt.Printf("Moved message to %s: %s\n", folderName, msg.Envelope.Subject) return nil }