2019-03-15 02:19:04 +00:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2019-06-07 08:26:14 +00:00
|
|
|
"bufio"
|
|
|
|
|
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"
|
|
|
|
"github.com/emersion/go-message/mail"
|
|
|
|
"github.com/emersion/go-message/textproto"
|
2019-03-15 02:19:04 +00:00
|
|
|
|
2019-07-08 02:43:56 +00:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-05-18 00:57:10 +00:00
|
|
|
"git.sr.ht/~sircmpwn/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
|
|
|
}
|
2019-07-08 02:43:57 +00:00
|
|
|
imapw.handleFetchMessages(msg, msg.Uids, items, section)
|
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")
|
|
|
|
section := &imap.BodySectionName{}
|
2019-05-20 20:42:44 +00:00
|
|
|
section.Path = msg.Part
|
2019-06-09 18:55:04 +00:00
|
|
|
items := []imap.FetchItem{
|
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
|
|
|
section.FetchItem(),
|
|
|
|
}
|
2019-07-08 02:43:57 +00:00
|
|
|
imapw.handleFetchMessages(msg, []uint32{msg.Uid}, items, section)
|
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{
|
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchUid,
|
|
|
|
section.FetchItem(),
|
|
|
|
}
|
2019-07-08 02:43:57 +00:00
|
|
|
imapw.handleFetchMessages(msg, msg.Uids, items, section)
|
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,
|
2019-03-31 16:35:51 +00:00
|
|
|
section *imap.BodySectionName) {
|
2019-03-15 02:19:04 +00:00
|
|
|
|
2019-04-28 13:01:56 +00:00
|
|
|
messages := make(chan *imap.Message)
|
2019-06-02 17:26:43 +00:00
|
|
|
done := make(chan interface{})
|
2019-04-28 13:01:56 +00:00
|
|
|
|
2019-03-15 02:19:04 +00:00
|
|
|
go func() {
|
2019-04-28 13:01:56 +00:00
|
|
|
for _msg := range messages {
|
|
|
|
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
|
|
|
|
switch msg.(type) {
|
|
|
|
case *types.FetchMessageHeaders:
|
2019-06-07 08:26:14 +00: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 13:01:56 +00:00
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
2019-07-08 02:43:56 +00:00
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 02:43:58 +00:00
|
|
|
BodyStructure: translateBodyStructure(_msg.BodyStructure),
|
|
|
|
Envelope: translateEnvelope(_msg.Envelope),
|
|
|
|
Flags: translateFlags(_msg.Flags),
|
2019-07-08 02:43:56 +00:00
|
|
|
InternalDate: _msg.InternalDate,
|
|
|
|
RFC822Headers: header,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-04-28 13:01:56 +00:00
|
|
|
}, nil)
|
|
|
|
case *types.FetchFullMessages:
|
|
|
|
reader := _msg.GetBody(section)
|
|
|
|
imapw.worker.PostMessage(&types.FullMessage{
|
2019-03-15 02:19:04 +00:00
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 02:43:56 +00:00
|
|
|
Content: &models.FullMessage{
|
|
|
|
Reader: reader,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-04-28 13:01:56 +00:00
|
|
|
}, nil)
|
2019-06-09 18:55:04 +00:00
|
|
|
// Update flags (to mark message as read)
|
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 02:43:56 +00:00
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 02:43:58 +00:00
|
|
|
Flags: translateFlags(_msg.Flags),
|
2019-07-08 02:43:56 +00:00
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-06-09 18:55:04 +00:00
|
|
|
}, nil)
|
2019-04-28 13:01:56 +00:00
|
|
|
case *types.FetchMessageBodyPart:
|
|
|
|
reader := _msg.GetBody(section)
|
|
|
|
imapw.worker.PostMessage(&types.MessageBodyPart{
|
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 02:43:56 +00:00
|
|
|
Part: &models.MessageBodyPart{
|
|
|
|
Reader: reader,
|
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-03-15 02:19:04 +00:00
|
|
|
}, nil)
|
2019-06-09 18:55:04 +00:00
|
|
|
// Update flags (to mark message as read)
|
|
|
|
imapw.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
2019-07-08 02:43:56 +00:00
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 02:43:58 +00:00
|
|
|
Flags: translateFlags(_msg.Flags),
|
2019-07-08 02:43:56 +00:00
|
|
|
Uid: _msg.Uid,
|
|
|
|
},
|
2019-06-09 18:55:04 +00:00
|
|
|
}, nil)
|
2019-03-15 02:19:04 +00:00
|
|
|
}
|
2019-04-28 13:01:56 +00:00
|
|
|
}
|
2019-06-02 17:26:43 +00:00
|
|
|
done <- nil
|
2019-03-15 02:19:04 +00:00
|
|
|
}()
|
2019-04-28 13:01:56 +00:00
|
|
|
|
2019-07-08 02:43:57 +00:00
|
|
|
set := toSeqSet(uids)
|
|
|
|
if err := imapw.client.UidFetch(set, items, messages); err != nil {
|
2019-04-28 13:01:56 +00:00
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
} else {
|
2019-06-02 17:26:43 +00:00
|
|
|
<-done
|
2019-04-28 13:01:56 +00:00
|
|
|
imapw.worker.PostMessage(
|
|
|
|
&types.Done{types.RespondTo(msg)}, nil)
|
|
|
|
}
|
2019-03-15 02:19:04 +00:00
|
|
|
}
|