2019-03-15 02:19:04 +00:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2019-06-07 08:26:14 +00:00
|
|
|
"bufio"
|
2020-01-04 20:13:52 +00:00
|
|
|
"fmt"
|
2019-06-07 08:26:14 +00:00
|
|
|
|
2019-03-15 02:19:04 +00:00
|
|
|
"github.com/emersion/go-imap"
|
2019-06-07 08:26:14 +00:00
|
|
|
"github.com/emersion/go-message"
|
2020-01-04 20:13:52 +00:00
|
|
|
_ "github.com/emersion/go-message/charset"
|
2019-06-07 08:26:14 +00:00
|
|
|
"github.com/emersion/go-message/mail"
|
|
|
|
"github.com/emersion/go-message/textproto"
|
2019-03-15 02:19:04 +00:00
|
|
|
|
2022-03-22 08:52:27 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-03-15 02:19:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (imapw *IMAPWorker) handleFetchMessageHeaders(
|
|
|
|
msg *types.FetchMessageHeaders) {
|
|
|
|
|
|
|
|
imapw.worker.Logger.Printf("Fetching message headers")
|
2019-06-07 08:26:14 +00:00
|
|
|
section := &imap.BodySectionName{
|
|
|
|
BodyPartName: imap.BodyPartName{
|
|
|
|
Specifier: imap.HeaderSpecifier,
|
|
|
|
},
|
2019-06-08 05:22:41 +00:00
|
|
|
Peek: true,
|
2019-06-07 08:26:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 02:35:53 +00:00
|
|
|
items := []imap.FetchItem{
|
2019-03-31 15:10:10 +00:00
|
|
|
imap.FetchBodyStructure,
|
2019-03-30 02:35:53 +00:00
|
|
|
imap.FetchEnvelope,
|
|
|
|
imap.FetchInternalDate,
|
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
2019-06-07 08:26:14 +00:00
|
|
|
section.FetchItem(),
|
2019-03-30 02:35:53 +00:00
|
|
|
}
|
2020-05-16 18:03:42 +00: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 {
|
2022-02-04 08:14:53 +00:00
|
|
|
imapw.worker.Logger.Printf(
|
|
|
|
"message %v: could not read header: %v", _msg.Uid, err)
|
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
return nil
|
2020-05-16 18:03:42 +00:00
|
|
|
}
|
2022-03-18 08:53:02 +00:00
|
|
|
header := &mail.Header{Header: message.Header{Header: textprotoHeader}}
|
2020-05-16 18:03:42 +00:00
|
|
|
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 02:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 16:14:37 +00:00
|
|
|
func (imapw *IMAPWorker) handleFetchMessageBodyPart(
|
|
|
|
msg *types.FetchMessageBodyPart) {
|
|
|
|
|
|
|
|
imapw.worker.Logger.Printf("Fetching message part")
|
2020-05-16 18:03:42 +00: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 18:55:04 +00:00
|
|
|
items := []imap.FetchItem{
|
2020-05-16 18:03:42 +00:00
|
|
|
imap.FetchEnvelope,
|
2019-06-09 18:55:04 +00:00
|
|
|
imap.FetchUid,
|
2020-05-16 18:03:42 +00:00
|
|
|
imap.FetchBodyStructure,
|
|
|
|
imap.FetchFlags,
|
|
|
|
partHeaderSection.FetchItem(),
|
|
|
|
partBodySection.FetchItem(),
|
2019-06-09 18:55:04 +00:00
|
|
|
}
|
2020-05-16 18:03:42 +00: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)
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:53:02 +00:00
|
|
|
part, err := message.New(message.Header{Header: h},
|
2020-05-16 18:03:42 +00:00
|
|
|
_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 16:14:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 16:17:57 +00:00
|
|
|
func (imapw *IMAPWorker) handleFetchFullMessages(
|
|
|
|
msg *types.FetchFullMessages) {
|
|
|
|
|
|
|
|
imapw.worker.Logger.Printf("Fetching full messages")
|
|
|
|
section := &imap.BodySectionName{}
|
2019-06-09 18:55:04 +00:00
|
|
|
items := []imap.FetchItem{
|
2020-01-18 23:59:20 +00:00
|
|
|
imap.FetchEnvelope,
|
2019-06-09 18:55:04 +00:00
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
|
|
|
section.FetchItem(),
|
|
|
|
}
|
2020-05-16 18:03:42 +00: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 16:17:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 02:35:53 +00:00
|
|
|
func (imapw *IMAPWorker) handleFetchMessages(
|
2019-07-08 02:43:57 +00:00
|
|
|
msg types.WorkerMessage, uids []uint32, items []imap.FetchItem,
|
2020-05-16 18:03:42 +00:00
|
|
|
procFunc func(*imap.Message) error) {
|
2019-03-15 02:19:04 +00:00
|
|
|
|
2019-04-28 13:01:56 +00:00
|
|
|
messages := make(chan *imap.Message)
|
2020-01-04 20:13:52 +00:00
|
|
|
done := make(chan error)
|
2019-04-28 13:01:56 +00:00
|
|
|
|
2019-03-15 02:19:04 +00:00
|
|
|
go func() {
|
2022-03-22 08:52:27 +00:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2020-05-16 18:03:42 +00:00
|
|
|
var reterr error
|
2019-04-28 13:01:56 +00:00
|
|
|
for _msg := range messages {
|
|
|
|
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
|
2020-05-16 18:03:42 +00:00
|
|
|
err := procFunc(_msg)
|
|
|
|
if err != nil {
|
|
|
|
if reterr == nil {
|
|
|
|
reterr = err
|
2019-06-07 08:26:14 +00:00
|
|
|
}
|
2020-05-16 18:03:42 +00:00
|
|
|
// drain the channel upon error
|
|
|
|
for range messages {
|
2020-01-04 20:13:52 +00:00
|
|
|
}
|
2019-03-15 02:19:04 +00:00
|
|
|
}
|
2019-04-28 13:01:56 +00:00
|
|
|
}
|
2020-05-16 18:03:42 +00:00
|
|
|
done <- reterr
|
2019-03-15 02:19:04 +00:00
|
|
|
}()
|
2019-04-28 13:01:56 +00:00
|
|
|
|
2020-01-04 20:13:52 +00:00
|
|
|
emitErr := func(err error) {
|
2019-04-28 13:01:56 +00:00
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
}
|
2020-01-04 20:13:52 +00: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(
|
2022-03-18 08:53:02 +00:00
|
|
|
&types.Done{Message: types.RespondTo(msg)}, nil)
|
2020-01-04 20:13:52 +00:00
|
|
|
}
|