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
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/quotedprintable"
|
|
|
|
"strings"
|
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
|
|
|
|
2019-07-08 04:43:56 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/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
|
|
|
}
|
2019-07-08 04:43:57 +02:00
|
|
|
imapw.handleFetchMessages(msg, msg.Uids, items, section)
|
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")
|
|
|
|
section := &imap.BodySectionName{}
|
2019-05-20 22:42:44 +02:00
|
|
|
section.Path = msg.Part
|
2019-06-09 20:55:04 +02:00
|
|
|
items := []imap.FetchItem{
|
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
|
|
|
section.FetchItem(),
|
|
|
|
}
|
2019-07-08 04:43:57 +02:00
|
|
|
imapw.handleFetchMessages(msg, []uint32{msg.Uid}, items, section)
|
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{
|
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
|
|
|
section.FetchItem(),
|
|
|
|
}
|
2019-07-08 04:43:57 +02:00
|
|
|
imapw.handleFetchMessages(msg, msg.Uids, items, section)
|
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,
|
2019-03-31 18:35:51 +02:00
|
|
|
section *imap.BodySectionName) {
|
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() {
|
2019-04-28 15:01:56 +02:00
|
|
|
for _msg := range messages {
|
|
|
|
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
|
2020-01-04 21:13:52 +01:00
|
|
|
switch msg := msg.(type) {
|
2019-04-28 15:01:56 +02:00
|
|
|
case *types.FetchMessageHeaders:
|
2019-06-07 10:26:14 +02:00
|
|
|
reader := _msg.GetBody(section)
|
|
|
|
textprotoHeader, err := textproto.ReadHeader(bufio.NewReader(reader))
|
|
|
|
var header *mail.Header
|
|
|
|
if err == nil {
|
|
|
|
header = &mail.Header{message.Header{textprotoHeader}}
|
|
|
|
}
|
2019-04-28 15:01:56 +02:00
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
2019-07-08 04:43:56 +02:00
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 04:43:58 +02:00
|
|
|
BodyStructure: translateBodyStructure(_msg.BodyStructure),
|
|
|
|
Envelope: translateEnvelope(_msg.Envelope),
|
|
|
|
Flags: translateFlags(_msg.Flags),
|
2019-07-08 04:43:56 +02:00
|
|
|
InternalDate: _msg.InternalDate,
|
|
|
|
RFC822Headers: header,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-04-28 15:01:56 +02:00
|
|
|
}, nil)
|
|
|
|
case *types.FetchFullMessages:
|
2020-01-04 21:13:52 +01:00
|
|
|
r := _msg.GetBody(section)
|
|
|
|
if r == nil {
|
|
|
|
done <- fmt.Errorf("could not get section %#v", section)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reader, err := fullReader(r)
|
|
|
|
if err != nil {
|
|
|
|
done <- fmt.Errorf("could not read mail %#v", section)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-28 15:01:56 +02:00
|
|
|
imapw.worker.PostMessage(&types.FullMessage{
|
2019-03-15 03:19:04 +01:00
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 04:43:56 +02:00
|
|
|
Content: &models.FullMessage{
|
|
|
|
Reader: reader,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-04-28 15:01:56 +02:00
|
|
|
}, nil)
|
2019-06-09 20:55:04 +02:00
|
|
|
// Update flags (to mark message as read)
|
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 04:43:56 +02:00
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 04:43:58 +02:00
|
|
|
Flags: translateFlags(_msg.Flags),
|
2019-07-08 04:43:56 +02:00
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-06-09 20:55:04 +02:00
|
|
|
}, nil)
|
2019-04-28 15:01:56 +02:00
|
|
|
case *types.FetchMessageBodyPart:
|
2020-01-04 21:13:52 +01:00
|
|
|
reader, err := getDecodedPart(msg, _msg, section)
|
|
|
|
if err != nil {
|
|
|
|
done <- err
|
|
|
|
return
|
|
|
|
}
|
2019-04-28 15:01:56 +02:00
|
|
|
imapw.worker.PostMessage(&types.MessageBodyPart{
|
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 04:43:56 +02:00
|
|
|
Part: &models.MessageBodyPart{
|
|
|
|
Reader: reader,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-03-15 03:19:04 +01:00
|
|
|
}, nil)
|
2019-06-09 20:55:04 +02:00
|
|
|
// Update flags (to mark message as read)
|
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 04:43:56 +02:00
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 04:43:58 +02:00
|
|
|
Flags: translateFlags(_msg.Flags),
|
2019-07-08 04:43:56 +02:00
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-06-09 20:55:04 +02:00
|
|
|
}, nil)
|
2019-03-15 03:19:04 +01:00
|
|
|
}
|
2019-04-28 15:01:56 +02:00
|
|
|
}
|
2019-06-02 19:26:43 +02:00
|
|
|
done <- nil
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDecodedPart(task *types.FetchMessageBodyPart, msg *imap.Message,
|
|
|
|
section *imap.BodySectionName) (io.Reader, error) {
|
|
|
|
var r io.Reader
|
|
|
|
var err error
|
|
|
|
|
|
|
|
r = msg.GetBody(section)
|
|
|
|
|
|
|
|
if r == nil {
|
|
|
|
return nil, fmt.Errorf("getDecodedPart: no message body")
|
|
|
|
}
|
|
|
|
r = encodingReader(task.Encoding, r)
|
|
|
|
if task.Charset != "" {
|
|
|
|
r, err = message.CharsetReader(task.Charset, r)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func fullReader(r io.Reader) (io.Reader, error) {
|
|
|
|
// parse the header for the encoding and also return it in the reader
|
|
|
|
br := bufio.NewReader(r)
|
|
|
|
textprotoHeader, err := textproto.ReadHeader(br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
header := &mail.Header{message.Header{textprotoHeader}}
|
|
|
|
enc := header.Get("Content-Transfer-Encoding")
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = textproto.WriteHeader(&buf, textprotoHeader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
er := encodingReader(enc, br)
|
|
|
|
full := io.MultiReader(&buf, er)
|
|
|
|
return full, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodingReader(encoding string, r io.Reader) io.Reader {
|
|
|
|
reader := r
|
|
|
|
// email parts are encoded as 7bit (plaintext), quoted-printable, or base64
|
|
|
|
if strings.EqualFold(encoding, "base64") {
|
|
|
|
reader = base64.NewDecoder(base64.StdEncoding, r)
|
|
|
|
} else if strings.EqualFold(encoding, "quoted-printable") {
|
|
|
|
reader = quotedprintable.NewReader(r)
|
|
|
|
}
|
|
|
|
return reader
|
2019-03-15 03:19:04 +01:00
|
|
|
}
|