2019-03-15 03:19:04 +01:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2019-06-07 10:26:14 +02:00
|
|
|
"bufio"
|
2020-01-04 21:13:52 +01:00
|
|
|
"fmt"
|
2019-06-07 10:26:14 +02:00
|
|
|
|
2019-03-15 03:19:04 +01:00
|
|
|
"github.com/emersion/go-imap"
|
2019-06-07 10:26:14 +02:00
|
|
|
"github.com/emersion/go-message"
|
2020-01-04 21:13:52 +01:00
|
|
|
_ "github.com/emersion/go-message/charset"
|
2019-06-07 10:26:14 +02:00
|
|
|
"github.com/emersion/go-message/mail"
|
|
|
|
"github.com/emersion/go-message/textproto"
|
2019-03-15 03:19:04 +01:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-03-15 03:19:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (imapw *IMAPWorker) handleFetchMessageHeaders(
|
|
|
|
msg *types.FetchMessageHeaders) {
|
|
|
|
|
|
|
|
imapw.worker.Logger.Printf("Fetching message headers")
|
2019-06-07 10:26:14 +02:00
|
|
|
section := &imap.BodySectionName{
|
|
|
|
BodyPartName: imap.BodyPartName{
|
|
|
|
Specifier: imap.HeaderSpecifier,
|
|
|
|
},
|
2019-06-08 07:22:41 +02:00
|
|
|
Peek: true,
|
2019-06-07 10:26:14 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 03:35:53 +01:00
|
|
|
items := []imap.FetchItem{
|
2019-03-31 17:10:10 +02:00
|
|
|
imap.FetchBodyStructure,
|
2019-03-30 03:35:53 +01:00
|
|
|
imap.FetchEnvelope,
|
|
|
|
imap.FetchInternalDate,
|
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
2019-06-07 10:26:14 +02:00
|
|
|
section.FetchItem(),
|
2019-03-30 03:35:53 +01:00
|
|
|
}
|
2020-05-16 20:03:42 +02:00
|
|
|
imapw.handleFetchMessages(msg, msg.Uids, items,
|
|
|
|
func(_msg *imap.Message) error {
|
|
|
|
reader := _msg.GetBody(section)
|
|
|
|
textprotoHeader, err := textproto.ReadHeader(bufio.NewReader(reader))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read header: %v", err)
|
|
|
|
}
|
|
|
|
header := &mail.Header{message.Header{textprotoHeader}}
|
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: &models.MessageInfo{
|
|
|
|
BodyStructure: translateBodyStructure(_msg.BodyStructure),
|
|
|
|
Envelope: translateEnvelope(_msg.Envelope),
|
|
|
|
Flags: translateImapFlags(_msg.Flags),
|
|
|
|
InternalDate: _msg.InternalDate,
|
|
|
|
RFC822Headers: header,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
})
|
2019-03-30 03:35:53 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 18:14:37 +02:00
|
|
|
func (imapw *IMAPWorker) handleFetchMessageBodyPart(
|
|
|
|
msg *types.FetchMessageBodyPart) {
|
|
|
|
|
|
|
|
imapw.worker.Logger.Printf("Fetching message part")
|
2020-05-16 20:03:42 +02:00
|
|
|
|
|
|
|
var partHeaderSection imap.BodySectionName
|
|
|
|
partHeaderSection.Peek = true
|
|
|
|
if len(msg.Part) > 0 {
|
|
|
|
partHeaderSection.Specifier = imap.MIMESpecifier
|
|
|
|
} else {
|
|
|
|
partHeaderSection.Specifier = imap.HeaderSpecifier
|
|
|
|
}
|
|
|
|
partHeaderSection.Path = msg.Part
|
|
|
|
|
|
|
|
var partBodySection imap.BodySectionName
|
|
|
|
if len(msg.Part) > 0 {
|
|
|
|
partBodySection.Specifier = imap.EntireSpecifier
|
|
|
|
} else {
|
|
|
|
partBodySection.Specifier = imap.TextSpecifier
|
|
|
|
}
|
|
|
|
partBodySection.Path = msg.Part
|
|
|
|
|
2019-06-09 20:55:04 +02:00
|
|
|
items := []imap.FetchItem{
|
2020-05-16 20:03:42 +02:00
|
|
|
imap.FetchEnvelope,
|
2019-06-09 20:55:04 +02:00
|
|
|
imap.FetchUid,
|
2020-05-16 20:03:42 +02:00
|
|
|
imap.FetchBodyStructure,
|
|
|
|
imap.FetchFlags,
|
|
|
|
partHeaderSection.FetchItem(),
|
|
|
|
partBodySection.FetchItem(),
|
2019-06-09 20:55:04 +02:00
|
|
|
}
|
2020-05-16 20:03:42 +02:00
|
|
|
imapw.handleFetchMessages(msg, []uint32{msg.Uid}, items,
|
|
|
|
func(_msg *imap.Message) error {
|
|
|
|
headerReader := bufio.NewReader(_msg.GetBody(&partHeaderSection))
|
|
|
|
h, err := textproto.ReadHeader(headerReader)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read part header: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
part, err := message.New(message.Header{h},
|
|
|
|
_msg.GetBody(&partBodySection))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create message reader: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
imapw.worker.PostMessage(&types.MessageBodyPart{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Part: &models.MessageBodyPart{
|
|
|
|
Reader: part.Body,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
// Update flags (to mark message as read)
|
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: &models.MessageInfo{
|
|
|
|
Flags: translateImapFlags(_msg.Flags),
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
})
|
2019-03-31 18:14:37 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 18:17:57 +02:00
|
|
|
func (imapw *IMAPWorker) handleFetchFullMessages(
|
|
|
|
msg *types.FetchFullMessages) {
|
|
|
|
|
|
|
|
imapw.worker.Logger.Printf("Fetching full messages")
|
|
|
|
section := &imap.BodySectionName{}
|
2019-06-09 20:55:04 +02:00
|
|
|
items := []imap.FetchItem{
|
2020-01-19 00:59:20 +01:00
|
|
|
imap.FetchEnvelope,
|
2019-06-09 20:55:04 +02:00
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
|
|
|
section.FetchItem(),
|
|
|
|
}
|
2020-05-16 20:03:42 +02:00
|
|
|
imapw.handleFetchMessages(msg, msg.Uids, items,
|
|
|
|
func(_msg *imap.Message) error {
|
|
|
|
r := _msg.GetBody(section)
|
|
|
|
if r == nil {
|
|
|
|
return fmt.Errorf("could not get section %#v", section)
|
|
|
|
}
|
|
|
|
imapw.worker.PostMessage(&types.FullMessage{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Content: &models.FullMessage{
|
|
|
|
Reader: bufio.NewReader(r),
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
// Update flags (to mark message as read)
|
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: &models.MessageInfo{
|
|
|
|
Flags: translateImapFlags(_msg.Flags),
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
})
|
2019-03-31 18:17:57 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 03:35:53 +01:00
|
|
|
func (imapw *IMAPWorker) handleFetchMessages(
|
2019-07-08 04:43:57 +02:00
|
|
|
msg types.WorkerMessage, uids []uint32, items []imap.FetchItem,
|
2020-05-16 20:03:42 +02:00
|
|
|
procFunc func(*imap.Message) error) {
|
2019-03-15 03:19:04 +01:00
|
|
|
|
2019-04-28 15:01:56 +02:00
|
|
|
messages := make(chan *imap.Message)
|
2020-01-04 21:13:52 +01:00
|
|
|
done := make(chan error)
|
2019-04-28 15:01:56 +02:00
|
|
|
|
2019-03-15 03:19:04 +01:00
|
|
|
go func() {
|
2020-05-16 20:03:42 +02:00
|
|
|
var reterr error
|
2019-04-28 15:01:56 +02:00
|
|
|
for _msg := range messages {
|
|
|
|
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
|
2020-05-16 20:03:42 +02:00
|
|
|
err := procFunc(_msg)
|
|
|
|
if err != nil {
|
|
|
|
if reterr == nil {
|
|
|
|
reterr = err
|
2019-06-07 10:26:14 +02:00
|
|
|
}
|
2020-05-16 20:03:42 +02:00
|
|
|
// drain the channel upon error
|
|
|
|
for range messages {
|
2020-01-04 21:13:52 +01:00
|
|
|
}
|
2019-03-15 03:19:04 +01:00
|
|
|
}
|
2019-04-28 15:01:56 +02:00
|
|
|
}
|
2020-05-16 20:03:42 +02:00
|
|
|
done <- reterr
|
2019-03-15 03:19:04 +01:00
|
|
|
}()
|
2019-04-28 15:01:56 +02:00
|
|
|
|
2020-01-04 21:13:52 +01:00
|
|
|
emitErr := func(err error) {
|
2019-04-28 15:01:56 +02:00
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
}
|
2020-01-04 21:13:52 +01:00
|
|
|
|
|
|
|
set := toSeqSet(uids)
|
|
|
|
if err := imapw.client.UidFetch(set, items, messages); err != nil {
|
|
|
|
emitErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := <-done; err != nil {
|
|
|
|
emitErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
imapw.worker.PostMessage(
|
|
|
|
&types.Done{types.RespondTo(msg)}, nil)
|
|
|
|
}
|