2021-01-31 01:14:37 +01:00
|
|
|
package imaputils
|
|
|
|
|
|
|
|
import (
|
2021-02-01 23:13:23 +01:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2021-02-01 23:13:23 +01:00
|
|
|
type FetchOpts struct {
|
|
|
|
Mailbox string
|
|
|
|
IdList []uint32
|
|
|
|
WithHeaders bool
|
|
|
|
WithBody bool
|
2021-03-18 23:45:06 +01:00
|
|
|
WithFlags bool
|
2021-02-01 23:13:23 +01:00
|
|
|
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
|
2021-02-01 19:48:45 +01:00
|
|
|
conn := NewConnection(conf)
|
|
|
|
|
|
|
|
err := conn.Start(debug)
|
|
|
|
if err != nil {
|
2021-04-05 19:49:39 +02:00
|
|
|
return empty, err
|
2021-02-01 19:48:45 +01:00
|
|
|
}
|
|
|
|
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
|
2021-02-01 23:13:23 +01:00
|
|
|
mbox, err := conn.client.Select(opts.Mailbox, true)
|
2021-02-01 19:48:45 +01:00
|
|
|
if err != nil {
|
2021-04-05 19:49:39 +02:00
|
|
|
return empty, err
|
2021-02-01 19:48:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 23:13:23 +01:00
|
|
|
return fetchMessage(mbox, conn, opts)
|
2021-02-01 19:48:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 23:13:23 +01:00
|
|
|
func fetchMessage(mbox *imap.MailboxStatus, conn *IMAPConnection, opts *FetchOpts) ([]*imap.Message, error) {
|
|
|
|
var messageAcc []*imap.Message
|
|
|
|
messages := make(chan *imap.Message, len(opts.IdList))
|
2021-02-01 19:48:45 +01:00
|
|
|
done := make(chan error, 1)
|
|
|
|
|
2021-02-01 23:13:23 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-02-01 19:48:45 +01:00
|
|
|
go func() {
|
|
|
|
done <- conn.client.Fetch(seqset, items, messages)
|
|
|
|
}()
|
|
|
|
|
2021-02-01 23:13:23 +01:00
|
|
|
for m := range messages {
|
|
|
|
messageAcc = append(messageAcc, m)
|
|
|
|
}
|
2021-02-01 19:48:45 +01:00
|
|
|
if err := <-done; err != nil {
|
2021-02-01 23:13:23 +01:00
|
|
|
return []*imap.Message{}, err
|
2021-02-01 19:48:45 +01:00
|
|
|
}
|
2021-02-01 23:13:23 +01:00
|
|
|
return messageAcc, nil
|
2021-02-01 19:48:45 +01:00
|
|
|
}
|