Skip to content

Commit

Permalink
Fixed moving multiple messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpl0itU committed Sep 9, 2023
1 parent d290cfc commit 1386ac2
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
}

0 comments on commit 1386ac2

Please sign in to comment.