papero/imaputils/fetch_messages.go

93 lines
2 KiB
Go
Raw Permalink Normal View History

2021-01-31 01:14:37 +01:00
package imaputils
import (
"fmt"
2021-04-01 18:09:04 +02:00
"git.sr.ht/~blallo/papero/config"
2021-01-31 01:14:37 +01:00
"github.com/emersion/go-imap"
)
type FetchOpts struct {
Mailbox string
IdList []uint32
WithHeaders bool
WithBody bool
2021-03-18 23:45:06 +01:00
WithFlags bool
Peek bool
}
func (o *FetchOpts) String() string {
return fmt.Sprintf(
"FetchOpts{Mailbox: %s, IdList: %v, WithHeaders: %t, WithBody: %t, Peek: %t}",
o.Mailbox,
o.IdList,
o.WithHeaders,
o.WithBody,
o.Peek,
)
}
func FetchMessages(conf *config.AccountData, opts *FetchOpts, debug bool) ([]*imap.Message, error) {
2021-04-05 19:49:39 +02:00
var empty []*imap.Message
conn := NewConnection(conf)
err := conn.Start(debug)
if err != nil {
2021-04-05 19:49:39 +02:00
return empty, err
}
defer conn.Close()
2021-04-05 19:49:39 +02:00
return FetchMessagesInSession(conn, opts)
}
func FetchMessagesInSession(conn *IMAPConnection, opts *FetchOpts) ([]*imap.Message, error) {
var empty []*imap.Message
mbox, err := conn.client.Select(opts.Mailbox, true)
if err != nil {
2021-04-05 19:49:39 +02:00
return empty, err
}
return fetchMessage(mbox, conn, opts)
}
func fetchMessage(mbox *imap.MailboxStatus, conn *IMAPConnection, opts *FetchOpts) ([]*imap.Message, error) {
var messageAcc []*imap.Message
messages := make(chan *imap.Message, len(opts.IdList))
done := make(chan error, 1)
seqset := new(imap.SeqSet)
for _, id := range opts.IdList {
seqset.AddNum(id)
}
var items []imap.FetchItem
if opts.WithHeaders {
section := &imap.BodySectionName{Peek: opts.Peek, BodyPartName: imap.BodyPartName{Specifier: imap.HeaderSpecifier}}
items = append(items, section.FetchItem())
}
items = append(items, imap.FetchEnvelope)
if opts.WithBody {
section := &imap.BodySectionName{Peek: opts.Peek, BodyPartName: imap.BodyPartName{Specifier: imap.TextSpecifier}}
items = append(items, section.FetchItem())
}
2021-03-18 23:45:06 +01:00
if opts.WithFlags {
items = append(items, imap.FetchFlags)
}
go func() {
done <- conn.client.Fetch(seqset, items, messages)
}()
for m := range messages {
messageAcc = append(messageAcc, m)
}
if err := <-done; err != nil {
return []*imap.Message{}, err
}
return messageAcc, nil
}